-
Notifications
You must be signed in to change notification settings - Fork 4
/
stats.go
59 lines (52 loc) · 1.18 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Package vago
package vago
/*
#cgo pkg-config: varnishapi
#cgo LDFLAGS: -lvarnishapi -lm
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <vapi/vsc.h>
#include <vapi/vsm.h>
#include <vapi/vsl.h>
int listCallback(void *priv, struct VSC_point *);
*/
import "C"
import (
"unsafe"
)
// Stats returns a map with all stat counters and their values.
func (v *Varnish) Stats() map[string]uint64 {
items := make(map[string]uint64)
handle := ptrHandles.track(items)
defer ptrHandles.untrack(handle)
C.VSC_Iter(v.vsc, v.vsm,
(*C.VSC_iter_f)(C.listCallback),
handle)
return items
}
// Stat takes a Varnish stat field and returns its value and true if found,
// 0 and false otherwise.
func (v *Varnish) Stat(s string) (uint64, bool) {
stats := v.Stats()
value, ok := stats[s]
return value, ok
}
// do_list_cb()
//
//export listCallback
func listCallback(handle unsafe.Pointer, pt *C.struct_VSC_point) C.int {
priv := ptrHandles.get(handle)
if pt == nil || priv == nil {
return 1
}
name := C.GoString(pt.name)
value := *(*uint64)(unsafe.Pointer(pt.ptr))
items, ok := priv.(map[string]uint64)
if !ok {
return 1
}
items[name] = value
return 0
}