-
Notifications
You must be signed in to change notification settings - Fork 291
/
duration.go
90 lines (71 loc) · 1.55 KB
/
duration.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// (C) Copyright 2021-2022 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
//go:build linux && (amd64 || arm64)
// +build linux
// +build amd64 arm64
//
package telemetry
/*
#cgo LDFLAGS: -lgurt
#include "gurt/telemetry_common.h"
#include "gurt/telemetry_consumer.h"
*/
import "C"
import (
"context"
"fmt"
"time"
)
type Duration struct {
statsMetric
}
func (d *Duration) Type() MetricType {
return MetricTypeDuration
}
func (d *Duration) Value() time.Duration {
if d.handle == nil || d.node == nil {
return BadDuration
}
var tms C.struct_timespec
res := C.d_tm_get_duration(d.handle.ctx, &tms, &d.stats, d.node)
if res == C.DER_SUCCESS {
return time.Duration(tms.tv_sec)*time.Second + time.Duration(tms.tv_nsec)*time.Nanosecond
}
return BadDuration
}
func (d *Duration) FloatValue() float64 {
return float64(d.Value())
}
func newDuration(hdl *handle, path string, name *string, node *C.struct_d_tm_node_t) *Duration {
d := &Duration{
statsMetric: statsMetric{
metricBase: metricBase{
handle: hdl,
path: path,
name: name,
node: node,
},
},
}
// Load up statistics
_ = d.Value()
return d
}
func GetDuration(ctx context.Context, name string) (*Duration, error) {
hdl, err := getHandle(ctx)
if err != nil {
return nil, err
}
node, err := findNode(hdl, name)
if err != nil {
return nil, err
}
if (node.dtn_type & C.D_TM_DURATION) == 0 {
return nil, fmt.Errorf("metric %q is not a duration", name)
}
n, p := splitFullName(name)
return newDuration(hdl, p, &n, node), nil
}