@@ -99,11 +99,13 @@ func setupTests(t *testing.T, f func([]int) string) {
99
99
func (tc * testutil.TestCase ) error {
100
100
t := tc .T
101
101
// create a new container client
102
- var samples samples
102
+ var v1samples samplesV1
103
+ var v2samples samplesV2
103
104
var cfg testConfig
104
105
tc .UnmarshalInputsStrict (map [string ]interface {}{
105
- "input_samples.yaml" : & samples ,
106
- "input_test.yaml" : & cfg ,
106
+ "input_samples.yaml" : & v1samples ,
107
+ "input_samples_v2.yaml" : & v2samples ,
108
+ "input_test.yaml" : & cfg ,
107
109
})
108
110
109
111
// setup the testdata by copying it to a tmp directory
@@ -129,18 +131,36 @@ func setupTests(t *testing.T, f func([]int) string) {
129
131
require .NoError (t , os .WriteFile (filepath .Join (testdataCopy , rel ), b , 0600 ))
130
132
return nil
131
133
})
134
+
132
135
fs := & fakeFS {
133
- FS : os .DirFS (testdataCopy ),
134
- root : testdataCopy ,
135
- samples : samples ,
136
- index : make (map [string ]int ),
137
- time : make (map [string ]time.Time ),
136
+ FS : os .DirFS (testdataCopy ),
137
+ root : testdataCopy ,
138
+ samplesV1 : v1samples ,
139
+ samplesV2 : v2samples ,
140
+ index : make (map [string ]int ),
141
+ time : make (map [string ]time.Time ),
138
142
}
139
143
140
144
// get 2 free ports
141
145
ports , err := testutil .GetFreePorts (2 )
142
146
require .NoError (t , err )
143
147
148
+ var cpuPaths []samplerserverv1alpha1.MetricsFilepath
149
+ var memoryPaths []samplerserverv1alpha1.MetricsFilepath
150
+ if cfg .Config .Reader .CGroupVersion == samplerserverv1alpha1 .CGroupV2 {
151
+ cpuPaths = []samplerserverv1alpha1.MetricsFilepath {
152
+ samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" ))}
153
+ memoryPaths = []samplerserverv1alpha1.MetricsFilepath {
154
+ samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" ))}
155
+ } else {
156
+ cpuPaths = []samplerserverv1alpha1.MetricsFilepath {
157
+ samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" , "cpu" )),
158
+ samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" , "cpuacct" )),
159
+ }
160
+ memoryPaths = []samplerserverv1alpha1.MetricsFilepath {
161
+ samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" , "memory" ))}
162
+ }
163
+
144
164
server := sampler.Server {
145
165
SortResults : true , // so the test results are consistent
146
166
MetricsNodeSampler : samplerserverv1alpha1.MetricsNodeSampler {
@@ -149,12 +169,9 @@ func setupTests(t *testing.T, f func([]int) string) {
149
169
Size : cfg .SampleSize ,
150
170
},
151
171
Reader : samplerserverv1alpha1.Reader {
152
- CPUPaths : []samplerserverv1alpha1.MetricsFilepath {
153
- samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" , "cpu" )),
154
- samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" , "cpuacct" )),
155
- },
156
- MemoryPaths : []samplerserverv1alpha1.MetricsFilepath {
157
- samplerserverv1alpha1 .MetricsFilepath (filepath .Join ("sys" , "fs" , "cgroup" , "memory" ))},
172
+ CGroupVersion : cfg .Config .Reader .CGroupVersion ,
173
+ CPUPaths : cpuPaths ,
174
+ MemoryPaths : memoryPaths ,
158
175
NodeAggregationLevelGlobs : append (samplerserverv1alpha1 .DefaultNodeAggregationLevels ,
159
176
samplerserverv1alpha1 .NodeAggregationLevel ("system.slice/*" ),
160
177
samplerserverv1alpha1 .NodeAggregationLevel ("*" ),
@@ -221,43 +238,66 @@ func setupTests(t *testing.T, f func([]int) string) {
221
238
})
222
239
}
223
240
224
- type samples struct {
225
- MemorySamples map [string ][]MemorySample `yaml:"memorySamples" json:"memorySamples"`
226
- MemoryOOMKillSamples map [string ][]MemoryOOMKillSample `yaml:"oomSamples" json:"oomSamples"`
227
- MemoryOOMSamples map [string ][]MemoryOOMSample `yaml:"oomKillSamples" json:"oomKillSamples"`
228
- CPUUsageSamples map [string ][]CPUUsageSample `yaml:"cpuUsageSamples" json:"cpuUsageSamples"`
229
- CPUThrottlingSamples map [string ][]CPUThrottlingSample `yaml:"cpuThrottlingSamples" json:"cpuThrottlingSamples"`
241
+ type samplesV1 struct {
242
+ MemorySamplesV1 map [string ][]MemorySampleV1 `yaml:"memorySamples" json:"memorySamples"`
243
+ MemoryOOMKillSamplesV1 map [string ][]MemoryOOMKillSampleV1 `yaml:"oomSamples" json:"oomSamples"`
244
+ MemoryOOMSamplesV1 map [string ][]MemoryOOMSampleV1 `yaml:"oomKillSamples" json:"oomKillSamples"`
245
+ CPUUsageSamplesV1 map [string ][]CPUUsageSampleV1 `yaml:"cpuUsageSamples" json:"cpuUsageSamples"`
246
+ CPUThrottlingSamplesV1 map [string ][]CPUThrottlingSampleV1 `yaml:"cpuThrottlingSamples" json:"cpuThrottlingSamples"`
247
+ }
248
+
249
+ type samplesV2 struct {
250
+ MemorySamplesV2 map [string ][]MemorySampleV2 `yaml:"memorySamples" json:"memorySamplesV2"`
251
+ MemoryOOMSamplesV2 map [string ][]MemoryOOMSampleV2 `yaml:"memorySamples" json:"oomSamplesV2"`
252
+ CPUSamplesV2 map [string ][]CPUSampleV2 `yaml:"memorySamples" json:"cpuSamplesV2"`
230
253
}
231
254
232
255
type fakeFS struct {
233
- samples
256
+ samplesV1
257
+ samplesV2
234
258
index map [string ]int
235
259
time map [string ]time.Time
236
260
root string
237
261
fs.FS
238
262
}
239
263
240
- type MemorySample struct {
264
+ type MemorySampleV1 struct {
241
265
RSS int `yaml:"total_rss" json:"total_rss"`
242
266
Cache int `yaml:"total_cache" json:"total_cache"`
243
267
}
244
268
245
- type MemoryOOMKillSample struct {
269
+ type MemoryOOMKillSampleV1 struct {
246
270
OOMKill int `yaml:"oom_kill" json:"oom_kill"`
247
271
}
248
272
249
- type MemoryOOMSample int
273
+ type MemoryOOMSampleV1 int
250
274
251
- type CPUUsageSample struct {
275
+ type CPUUsageSampleV1 struct {
252
276
Usage int `yaml:"usage" json:"usage"`
253
277
}
254
278
255
- type CPUThrottlingSample struct {
279
+ type CPUThrottlingSampleV1 struct {
256
280
ThrottledTime int `yaml:"throttled_time" json:"throttled_time"`
257
281
Periods int `yaml:"nr_periods" json:"nr_periods"`
258
282
ThrottledPeriods int `yaml:"nr_throttled" json:"nr_throttled"`
259
283
}
260
284
285
+ type MemorySampleV2 struct {
286
+ Current int `yaml:"total_rss" json:"usage"`
287
+ }
288
+
289
+ type MemoryOOMSampleV2 struct {
290
+ OOMKill int `yaml:"oom_kill" json:"oom_kill"`
291
+ OOM int `yaml:"oom_kill" json:"oom"`
292
+ }
293
+
294
+ type CPUSampleV2 struct {
295
+ Usage int `yaml:"usage" json:"usage_usec"`
296
+ ThrottledTime int `yaml:"throttled_time" json:"throttled_usec"`
297
+ Periods int `yaml:"nr_periods" json:"nr_periods"`
298
+ ThrottledPeriods int `yaml:"nr_throttled" json:"nr_throttled"`
299
+ }
300
+
261
301
func (fakeFS * fakeFS ) Time (name string ) time.Time {
262
302
t , ok := fakeFS .time [name ]
263
303
if ! ok {
@@ -270,7 +310,7 @@ func (fakeFS *fakeFS) Time(name string) time.Time {
270
310
}
271
311
272
312
func (fakeFS * fakeFS ) Open (name string ) (fs.File , error ) {
273
- if val , ok := fakeFS .MemorySamples [name ]; ok {
313
+ if val , ok := fakeFS .MemorySamplesV1 [name ]; ok {
274
314
// update the file value by setting its value
275
315
index := fakeFS .index [name ] % len (val )
276
316
fakeFS .index [name ] = (index + 1 )
@@ -280,7 +320,7 @@ func (fakeFS *fakeFS) Open(name string) (fs.File, error) {
280
320
if err != nil {
281
321
return nil , err
282
322
}
283
- } else if val , ok := fakeFS .CPUUsageSamples [name ]; ok {
323
+ } else if val , ok := fakeFS .CPUUsageSamplesV1 [name ]; ok {
284
324
var i int
285
325
// update the file value by incrementing it
286
326
index := fakeFS .index [name ] % len (val )
@@ -300,7 +340,7 @@ func (fakeFS *fakeFS) Open(name string) (fs.File, error) {
300
340
if err != nil {
301
341
return nil , err
302
342
}
303
- } else if val , ok := fakeFS .CPUThrottlingSamples [name ]; ok {
343
+ } else if val , ok := fakeFS .CPUThrottlingSamplesV1 [name ]; ok {
304
344
var throttledTime , periods , periodsThrottled int
305
345
// update the file value by incrementing it
306
346
index := fakeFS .index [name ] % len (val )
@@ -339,7 +379,7 @@ func (fakeFS *fakeFS) Open(name string) (fs.File, error) {
339
379
if err != nil {
340
380
return nil , err
341
381
}
342
- } else if val , ok := fakeFS .MemoryOOMKillSamples [name ]; ok {
382
+ } else if val , ok := fakeFS .MemoryOOMKillSamplesV1 [name ]; ok {
343
383
// update the file value by setting its value
344
384
index := fakeFS .index [name ] % len (val )
345
385
fakeFS .index [name ] = (index + 1 )
@@ -359,7 +399,7 @@ func (fakeFS *fakeFS) Open(name string) (fs.File, error) {
359
399
if err != nil {
360
400
return nil , err
361
401
}
362
- } else if val , ok := fakeFS .MemoryOOMSamples [name ]; ok {
402
+ } else if val , ok := fakeFS .MemoryOOMSamplesV1 [name ]; ok {
363
403
// update the file value by setting its value
364
404
index := fakeFS .index [name ] % len (val )
365
405
fakeFS .index [name ] = (index + 1 )
@@ -377,6 +417,92 @@ func (fakeFS *fakeFS) Open(name string) (fs.File, error) {
377
417
if err != nil {
378
418
return nil , err
379
419
}
420
+ } else if val , ok := fakeFS .MemorySamplesV2 [name ]; ok {
421
+ // update the file value by setting its value
422
+ index := fakeFS .index [name ] % len (val )
423
+ fakeFS .index [name ] = (index + 1 )
424
+ newVal := val [index ]
425
+ b := fmt .Sprintf ("%d\n " , newVal .Current )
426
+ err := os .WriteFile (filepath .Join (fakeFS .root , name ), []byte (b ), 0600 )
427
+ if err != nil {
428
+ return nil , err
429
+ }
430
+ } else if val , ok := fakeFS .CPUSamplesV2 [name ]; ok {
431
+ var usage , throttledTime , periods , periodsThrottled int
432
+ // update the file value by incrementing it
433
+ index := fakeFS .index [name ] % len (val )
434
+ fakeFS .index [name ] = (index + 1 )
435
+ inc := val [index ]
436
+
437
+ b , err := os .ReadFile (filepath .Join (fakeFS .root , name ))
438
+ if err != nil {
439
+ return nil , err
440
+ }
441
+ // parse the value out
442
+ for _ , line := range strings .Split (string (b ), "\n " ) {
443
+ fields := strings .Fields (line )
444
+ value , err := strconv .ParseUint (fields [1 ], 10 , 64 )
445
+ if err != nil {
446
+ return nil , err
447
+ }
448
+
449
+ switch fields [0 ] {
450
+ case "usage_usec" :
451
+ usage = int (value )
452
+ case "throttled_usec" :
453
+ throttledTime = int (value )
454
+ case "nr_periods" :
455
+ periods = int (value )
456
+ case "nr_throttled" :
457
+ periodsThrottled = int (value )
458
+ }
459
+ }
460
+
461
+ usage += inc .Usage
462
+ throttledTime += inc .ThrottledTime
463
+ periods += inc .Periods
464
+ periodsThrottled += inc .ThrottledPeriods
465
+
466
+ err = os .WriteFile (filepath .Join (fakeFS .root , name ), []byte (fmt .Sprintf (
467
+ "usage_usec %d\n throttled_usec %d\n nr_periods %d\n nr_throttled %d" , usage , throttledTime , periods , periodsThrottled )), 0600 )
468
+ if err != nil {
469
+ return nil , err
470
+ }
471
+ } else if val , ok := fakeFS .MemoryOOMSamplesV2 [name ]; ok {
472
+ var oom , oomKill int
473
+ // update the file value by setting its value
474
+ index := fakeFS .index [name ] % len (val )
475
+ fakeFS .index [name ] = (index + 1 )
476
+ newVal := val [index ]
477
+
478
+ b , err := os .ReadFile (filepath .Join (fakeFS .root , name ))
479
+ if err != nil {
480
+ return nil , err
481
+ }
482
+ // parse the value out
483
+ for _ , line := range strings .Split (string (b ), "\n " ) {
484
+ fields := strings .Fields (line )
485
+ value , err := strconv .ParseUint (fields [1 ], 10 , 64 )
486
+ if err != nil {
487
+ return nil , err
488
+ }
489
+
490
+ switch fields [0 ] {
491
+ case "oom" :
492
+ oom = int (value )
493
+ case "oom_kill" :
494
+ oomKill = int (value )
495
+ }
496
+ }
497
+
498
+ oom += newVal .OOM
499
+ oomKill += newVal .OOMKill
500
+
501
+ err = os .WriteFile (filepath .Join (fakeFS .root , name ), []byte (fmt .Sprintf ("oom %d\n oom_kill %d" , oom , oomKill )), 0600 )
502
+ if err != nil {
503
+ return nil , err
504
+ }
380
505
}
506
+
381
507
return fakeFS .FS .Open (name )
382
508
}
0 commit comments