File: pcp.go

package info (click to toggle)
golang-github-performancecopilot-speed 4.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: makefile: 38
file content (395 lines) | stat: -rw-r--r-- 8,118 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package mmvdump

import "strconv"

// MMVVersion is the current mmv format version
const MMVVersion = 1

const (
	// NameMax is the maximum allowed length of a name
	NameMax = 64

	// StringMax is the maximum allowed length of a string
	StringMax = 256

	// NoIndom is a constant used to indicate absence of an indom from a metric
	NoIndom = -1
)

// Header describes the data in a MMV header
type Header struct {
	Magic            [4]byte
	Version          int32
	G1, G2           uint64
	Toc              int32
	Flag             int32
	Process, Cluster int32
}

// TocType is an enumerated type with different types as values
type TocType int32

// Values for TocType
const (
	TocIndoms TocType = iota + 1
	TocInstances
	TocMetrics
	TocValues
	TocStrings
)

//go:generate stringer --type=TocType

// Toc defines the contents in a valid TOC
type Toc struct {
	Type   TocType
	Count  int32
	Offset uint64
}

// Instance is the base type for all instances
type Instance interface {
	Indom() uint64
	Internal() int32
	Padding() uint32
}

// InstanceBase defines the common contents in a valid instance
type InstanceBase struct {
	indom    uint64
	padding  uint32
	internal int32
}

// Indom returns the indom offset
func (i InstanceBase) Indom() uint64 { return i.indom }

// Internal returns the internal id
func (i InstanceBase) Internal() int32 { return i.internal }

// Padding returns the padding value
func (i InstanceBase) Padding() uint32 { return i.padding }

// Instance1 defines the contents in a valid mmv1 instance
type Instance1 struct {
	InstanceBase
	External [NameMax]byte
}

// Instance2 defines the contents in a valid mmv2 instance
type Instance2 struct {
	InstanceBase
	External uint64
}

// InstanceDomain defines the contents in a valid instance domain
type InstanceDomain struct {
	Serial, Count               uint32
	Offset, Shorttext, Longtext uint64
}

// Metric is the base type for all metrics
type Metric interface {
	Item() uint32
	Typ() Type
	Sem() Semantics
	Unit() Unit
	Indom() int32
	Padding() uint32
	ShortText() uint64
	LongText() uint64
}

// MetricBase defines the common contents in a valid Metric
type MetricBase struct {
	item                uint32
	typ                 Type
	sem                 Semantics
	unit                Unit
	indom               int32
	padding             uint32
	shorttext, longtext uint64
}

// Item returns the item id
func (m MetricBase) Item() uint32 { return m.item }

// Typ returns the type
func (m MetricBase) Typ() Type { return m.typ }

// Sem returns the semantics
func (m MetricBase) Sem() Semantics { return m.sem }

// Unit returns the unit
func (m MetricBase) Unit() Unit { return m.unit }

// Indom returns the indom id
func (m MetricBase) Indom() int32 { return m.indom }

// Padding returns the padding value
func (m MetricBase) Padding() uint32 { return m.padding }

// ShortText returns the shorttext offset
func (m MetricBase) ShortText() uint64 { return m.shorttext }

// LongText returns the longtext offset
func (m MetricBase) LongText() uint64 { return m.longtext }

// Metric1 defines the contents in a valid Metric
type Metric1 struct {
	Name [NameMax]byte
	MetricBase
}

// Metric2 defines the contents in a valid Metric
type Metric2 struct {
	Name uint64
	MetricBase
}

// Value defines the contents in a PCP Value
type Value struct {
	// uint64 is a holder type here, while printing it is expected that
	// the user will infer the value using the Val functions
	Val uint64

	Extra    int64
	Metric   uint64
	Instance uint64
}

// String wraps the payload for a PCP String
type String struct {
	Payload [StringMax]byte
}

// Type is an enumerated type representing all valid types for a metric
type Type int32

// Possible values for a Type
const (
	NoSupportType Type = iota - 1
	Int32Type
	Uint32Type
	Int64Type
	Uint64Type
	FloatType
	DoubleType
	StringType
	UnknownType Type = 255
)

//go:generate stringer --type=Type

// Unit is an enumerated type with all possible units as values
type Unit uint32

// Values for Space Units
const (
	ByteUnit Unit = 1<<28 | iota<<16
	KilobyteUnit
	MegabyteUnit
	GigabyteUnit
	TerabyteUnit
	PetabyteUnit
	ExabyteUnit
)

// Values for Time Units
const (
	NanosecondUnit Unit = 1<<24 | iota<<12
	MicrosecondUnit
	MillisecondUnit
	SecondUnit
	MinuteUnit
	HourUnit
)

// Values for Count Units
const (
	OneUnit Unit = 1<<20 | iota<<8
)

// SpaceScale gets the Space Scale of a unit
//
// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#398
func (u Unit) SpaceScale() uint8 {
	return uint8((u >> 16) & 0xF)
}

// TimeScale gets the Time Scale of a unit
//
// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#402
func (u Unit) TimeScale() uint8 {
	return uint8((u >> 12) & 0xF)
}

// CountScale gets the Count Scale of a unit
//
// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#406
func (u Unit) CountScale() uint8 {
	return uint8((u >> 8) & 0xF)
}

// SpaceDim gets the space dimension of the unit
// the right shift is on int32 to get an arithmetic right shift as
// the dimension is serialized in 2s complement form
//
// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#410
func (u Unit) SpaceDim() int8 {
	return int8(int32(u) >> 28)
}

// TimeDim gets the time dimension of the unit
// the right shift is on int32 to get an arithmetic right shift as
// the dimension is serialized in 2s complement form
//
// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#410
func (u Unit) TimeDim() int8 {
	return int8(int32(u<<4) >> 28)
}

// CountDim gets the count dimension of the unit
// the right shift is on int32 to get an arithmetic right shift as
// the dimension is serialized in 2s complement form
//
// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#410
func (u Unit) CountDim() int8 {
	return int8(int32(u<<8) >> 28)
}

func abs(dim int8) int8 {
	if dim < 0 {
		return -dim
	}

	return dim
}

func stringSpaceDim(scale uint8, dim int8) string {
	suf := ""
	if abs(dim) > 1 {
		suf = "^" + strconv.Itoa(int(dim))
	}

	switch scale {
	case 0:
		return "B" + suf
	case 1:
		return "KiB" + suf
	case 2:
		return "MiB" + suf
	case 3:
		return "GiB" + suf
	case 4:
		return "TiB" + suf
	case 5:
		return "PiB" + suf
	case 6:
		return "EiB" + suf
	default:
		return "<invalid>"
	}
}

func stringTimeDim(scale uint8, dim int8) string {
	suf := ""
	if abs(dim) > 1 {
		suf = "^" + strconv.Itoa(int(dim))
	}

	switch scale {
	case 0:
		return "nsec" + suf
	case 1:
		return "usec" + suf
	case 2:
		return "msec" + suf
	case 3:
		return "sec" + suf
	case 4:
		return "min" + suf
	case 5:
		return "hr" + suf
	default:
		return "<invalid>"
	}
}

func stringCountDim(scale uint8, dim int8) string {
	suf := ""
	if abs(dim) > 1 {
		suf = "^" + strconv.Itoa(int(dim))
	}

	switch scale {
	case 0:
		return "count" + suf
	default:
		return "<invalid>"
	}
}

// https://docs.rs/hornet/0.1.0/src/hornet/client/metric/mod.rs.html#454
func (u Unit) String() string {
	ss, sd, ts, td, cs, cd := u.SpaceScale(), u.SpaceDim(), u.TimeScale(), u.TimeDim(), u.CountScale(), u.CountDim()

	ans := ""

	if sd > 0 {
		ans += stringSpaceDim(ss, sd)
	}

	if td > 0 {
		ans += stringTimeDim(ts, td)
	}

	if cd > 0 {
		ans += stringCountDim(cs, cd)
	}

	if sd < 0 || td < 0 || cd < 0 {
		ans += " / "

		if sd < 0 {
			ans += stringSpaceDim(ss, sd)
		}

		if td < 0 {
			ans += stringTimeDim(ts, td)
		}

		if cd < 0 {
			ans += stringCountDim(cs, cd)
		}
	}

	return ans
}

// Semantics represents an enumerated type representing all possible semantics of a metric
type Semantics int32

// Values for Semantics
const (
	NoSemantics Semantics = 0
	CounterSemantics
	_
	InstantSemantics
	DiscreteSemantics
)

//go:generate stringer -type=Semantics

// Byte Lengths for Different Components
const (
	HeaderLength         uint64 = 40
	TocLength            uint64 = 16
	Metric1Length        uint64 = 104
	Metric2Length        uint64 = 48
	ValueLength          uint64 = 32
	Instance1Length      uint64 = 80
	Instance2Length      uint64 = 24
	InstanceDomainLength uint64 = 32
	StringLength         uint64 = 256
)