Skip to content

Commit

Permalink
Fix memory allocation
Browse files Browse the repository at this point in the history
Change cpuTimesC from u_lonlong_t to uint64_t
  • Loading branch information
dlopes7 committed Dec 8, 2019
1 parent 358b951 commit 775a12f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
17 changes: 9 additions & 8 deletions c_api/api_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package c_api
/*
#cgo LDFLAGS: -lperfstat
#include <stdlib.h>
#include <stdio.h>
#include <libperfstat.h>
#include <string.h>
#include <time.h>
int getCPUTicks(u_longlong_t **cputicks, size_t *cpu_ticks_len) {
u_longlong_t **ref;
int getCPUTicks(uint64_t **cputicks, size_t *cpu_ticks_len) {
int i, ncpus, cputotal;
perfstat_id_t firstcpu;
perfstat_cpu_t *statp;
cputotal = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
cputotal = perfstat_cpu(NULL, NULL, sizeof(perfstat_cpu_t), 0);
if (cputotal <= 0){
return -1;
}
Expand All @@ -24,14 +27,14 @@ int getCPUTicks(u_longlong_t **cputicks, size_t *cpu_ticks_len) {
ncpus = perfstat_cpu(&firstcpu, statp, sizeof(perfstat_cpu_t), cputotal);
*cpu_ticks_len = ncpus*4;
u_longlong_t user, wait, sys, idle;
*cputicks = (u_longlong_t *) malloc(sizeof(u_longlong_t)*(*cpu_ticks_len));
*cputicks = (uint64_t *) malloc(sizeof(uint64_t)*(*cpu_ticks_len));
for (i = 0; i < ncpus; i++) {
int offset = 4 * i;
(*cputicks)[offset] = statp[i].user;
(*cputicks)[offset+1] = statp[i].sys;
(*cputicks)[offset+2] = statp[i].wait;
(*cputicks)[offset+3] = statp[i].idle;
printf("%d", statp[i].user);
}
return 0;
}
Expand All @@ -47,17 +50,15 @@ const ClocksPerSec = float64(C.CLK_TCK)
const maxCPUTimesLen = 1024 * 4

func GetAIXCPUTimes() ([]float64, error) {

var (
cpuTimesC *C.u_longlong_t
cpuTimesLength C.size_t
cpuTimesC *C.uint64_t
cpuTimesLength C.size_t
)

if C.getCPUTicks(&cpuTimesC, &cpuTimesLength) == -1 {
return nil, errors.New("could not retrieve CPU times")
}
defer C.free(unsafe.Pointer(cpuTimesC))

cput := (*[maxCPUTimesLen]C.u_longlong_t)(unsafe.Pointer(cpuTimesC))[:cpuTimesLength:cpuTimesLength]

cpuTicks := make([]float64, cpuTimesLength)
Expand Down
1 change: 0 additions & 1 deletion collector/cpu_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ func NewCPUCollector() (Collector, error) {
}

func (c *statCollector) Update(ch chan<- prometheus.Metric) error {

var fieldsCount = 4
cpuFields := []string{"user", "sys", "wait", "idle"}

Expand Down

0 comments on commit 775a12f

Please sign in to comment.