...

Source file src/github.com/opencontainers/runc/libcontainer/cgroups/fs/cpuacct_test.go

Documentation: github.com/opencontainers/runc/libcontainer/cgroups/fs

     1  package fs
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/opencontainers/runc/libcontainer/cgroups"
     8  )
     9  
    10  const (
    11  	cpuAcctUsageContents       = "12262454190222160"
    12  	cpuAcctUsagePerCPUContents = "1564936537989058 1583937096487821 1604195415465681 1596445226820187 1481069084155629 1478735613864327 1477610593414743 1476362015778086"
    13  	cpuAcctStatContents        = "user 452278264\nsystem 291429664"
    14  	cpuAcctUsageAll            = `cpu user system
    15  	0 962250696038415 637727786389114
    16  	1 981956408513304 638197595421064
    17  	2 1002658817529022 638956774598358
    18  	3 994937703492523 637985531181620
    19  	4 874843781648690 638837766495476
    20  	5 872544369885276 638763309884944
    21  	6 870104915696359 640081778921247
    22  	7 870202363887496 638716766259495
    23  	`
    24  )
    25  
    26  func TestCpuacctStats(t *testing.T) {
    27  	path := tempDir(t, "cpuacct")
    28  	writeFileContents(t, path, map[string]string{
    29  		"cpuacct.usage":        cpuAcctUsageContents,
    30  		"cpuacct.usage_percpu": cpuAcctUsagePerCPUContents,
    31  		"cpuacct.stat":         cpuAcctStatContents,
    32  		"cpuacct.usage_all":    cpuAcctUsageAll,
    33  	})
    34  
    35  	cpuacct := &CpuacctGroup{}
    36  	actualStats := *cgroups.NewStats()
    37  	err := cpuacct.GetStats(path, &actualStats)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  
    42  	expectedStats := cgroups.CpuUsage{
    43  		TotalUsage: uint64(12262454190222160),
    44  		PercpuUsage: []uint64{
    45  			1564936537989058, 1583937096487821, 1604195415465681, 1596445226820187,
    46  			1481069084155629, 1478735613864327, 1477610593414743, 1476362015778086,
    47  		},
    48  		PercpuUsageInKernelmode: []uint64{
    49  			637727786389114, 638197595421064, 638956774598358, 637985531181620,
    50  			638837766495476, 638763309884944, 640081778921247, 638716766259495,
    51  		},
    52  		PercpuUsageInUsermode: []uint64{
    53  			962250696038415, 981956408513304, 1002658817529022, 994937703492523,
    54  			874843781648690, 872544369885276, 870104915696359, 870202363887496,
    55  		},
    56  		UsageInKernelmode: (uint64(291429664) * nanosecondsInSecond) / clockTicks,
    57  		UsageInUsermode:   (uint64(452278264) * nanosecondsInSecond) / clockTicks,
    58  	}
    59  
    60  	if !reflect.DeepEqual(expectedStats, actualStats.CpuStats.CpuUsage) {
    61  		t.Errorf("Expected CPU usage %#v but found %#v\n",
    62  			expectedStats, actualStats.CpuStats.CpuUsage)
    63  	}
    64  }
    65  
    66  func TestCpuacctStatsWithoutUsageAll(t *testing.T) {
    67  	path := tempDir(t, "cpuacct")
    68  	writeFileContents(t, path, map[string]string{
    69  		"cpuacct.usage":        cpuAcctUsageContents,
    70  		"cpuacct.usage_percpu": cpuAcctUsagePerCPUContents,
    71  		"cpuacct.stat":         cpuAcctStatContents,
    72  	})
    73  
    74  	cpuacct := &CpuacctGroup{}
    75  	actualStats := *cgroups.NewStats()
    76  	err := cpuacct.GetStats(path, &actualStats)
    77  	if err != nil {
    78  		t.Fatal(err)
    79  	}
    80  
    81  	expectedStats := cgroups.CpuUsage{
    82  		TotalUsage: uint64(12262454190222160),
    83  		PercpuUsage: []uint64{
    84  			1564936537989058, 1583937096487821, 1604195415465681, 1596445226820187,
    85  			1481069084155629, 1478735613864327, 1477610593414743, 1476362015778086,
    86  		},
    87  		PercpuUsageInKernelmode: []uint64{},
    88  		PercpuUsageInUsermode:   []uint64{},
    89  		UsageInKernelmode:       (uint64(291429664) * nanosecondsInSecond) / clockTicks,
    90  		UsageInUsermode:         (uint64(452278264) * nanosecondsInSecond) / clockTicks,
    91  	}
    92  
    93  	if !reflect.DeepEqual(expectedStats, actualStats.CpuStats.CpuUsage) {
    94  		t.Errorf("Expected CPU usage %#v but found %#v\n",
    95  			expectedStats, actualStats.CpuStats.CpuUsage)
    96  	}
    97  }
    98  

View as plain text