Skip to content

Commit

Permalink
Fix for when ncpu <= 0
Browse files Browse the repository at this point in the history
  • Loading branch information
dlopes7 committed Dec 8, 2019
1 parent 775a12f commit aa25a3c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 75 deletions.
70 changes: 0 additions & 70 deletions c_api/api_aix.go

This file was deleted.

68 changes: 63 additions & 5 deletions collector/cpu_aix.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
package collector

/*
#cgo LDFLAGS: -lperfstat
#include <stdlib.h>
#include <stdio.h>
#include <libperfstat.h>
#include <string.h>
#include <time.h>
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);
if (cputotal <= 0){
return -1;
}
statp = calloc(cputotal, sizeof(perfstat_cpu_t));
if(statp==NULL){
return -1;
}
ncpus = perfstat_cpu(&firstcpu, statp, sizeof(perfstat_cpu_t), cputotal);
if (ncpus <= 0) {
ncpus = cputotal;
}
*cpu_ticks_len = ncpus*4;
*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;
}
return 0;
}
*/
import "C"

import (
"errors"
"fmt"
"github.com/dlopes7/aix-prometheus-exporter/c_api"
"github.com/prometheus/client_golang/prometheus"
"unsafe"
)

const ClocksPerSec = float64(C.CLK_TCK)
const maxCPUTimesLen = 1024 * 4

type statCollector struct {
cpu *prometheus.Desc
}
Expand All @@ -24,12 +71,23 @@ func (c *statCollector) Update(ch chan<- prometheus.Metric) error {
var fieldsCount = 4
cpuFields := []string{"user", "sys", "wait", "idle"}

cpuTimes, err := c_api.GetAIXCPUTimes()
if err != nil {
return err
var (
cpuTimesC *C.uint64_t
cpuTimesLength C.size_t
)

if C.getCPUTicks(&cpuTimesC, &cpuTimesLength) == -1 {
return 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)
for i, value := range cput {
cpuTicks[i] = float64(value) / ClocksPerSec
}

for i, value := range cpuTimes {
for i, value := range cpuTicks {
cpux := fmt.Sprintf("CPU %d", i/fieldsCount)
ch <- prometheus.MustNewConstMetric(c.cpu, prometheus.CounterValue, value, cpux, cpuFields[i%fieldsCount])
}
Expand Down

0 comments on commit aa25a3c

Please sign in to comment.