...

Source file src/github.com/prometheus/procfs/sysfs/system_cpu_test.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2018 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  //go:build linux
    15  // +build linux
    16  
    17  package sysfs
    18  
    19  import (
    20  	"errors"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  func makeUint64(v uint64) *uint64 {
    26  	return &v
    27  }
    28  
    29  func TestCPUTopology(t *testing.T) {
    30  	fs, err := NewFS(sysTestFixtures)
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	cpus, err := fs.CPUs()
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if want, have := 3, len(cpus); want != have {
    39  		t.Errorf("incorrect number of CPUs, have %v, want %v", want, have)
    40  	}
    41  	if want, have := "0", cpus[0].Number(); want != have {
    42  		t.Errorf("incorrect name, have %v, want %v", want, have)
    43  	}
    44  	cpu0Topology, err := cpus[0].Topology()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if want, have := "0", cpu0Topology.CoreID; want != have {
    49  		t.Errorf("incorrect core ID, have %v, want %v", want, have)
    50  	}
    51  	if want, have := "0-7", cpu0Topology.CoreSiblingsList; want != have {
    52  		t.Errorf("incorrect core siblings list, have %v, want %v", want, have)
    53  	}
    54  	cpu1Topology, err := cpus[1].Topology()
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	if want, have := "0", cpu1Topology.PhysicalPackageID; want != have {
    59  		t.Errorf("incorrect package ID, have %v, want %v", want, have)
    60  	}
    61  	if want, have := "1,5", cpu1Topology.ThreadSiblingsList; want != have {
    62  		t.Errorf("incorrect thread siblings list, have %v, want %v", want, have)
    63  	}
    64  }
    65  
    66  func TestCPUThermalThrottle(t *testing.T) {
    67  	fs, err := NewFS(sysTestFixtures)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	cpus, err := fs.CPUs()
    72  	if err != nil {
    73  		t.Fatal(err)
    74  	}
    75  	if want, have := 3, len(cpus); want != have {
    76  		t.Errorf("incorrect number of CPUs, have %v, want %v", want, have)
    77  	}
    78  	cpu0Throttle, err := cpus[0].ThermalThrottle()
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if want, have := uint64(34818), cpu0Throttle.PackageThrottleCount; want != have {
    83  		t.Errorf("incorrect package throttle count, have %v, want %v", want, have)
    84  	}
    85  
    86  	cpu1Throttle, err := cpus[1].ThermalThrottle()
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	if want, have := uint64(523), cpu1Throttle.CoreThrottleCount; want != have {
    91  		t.Errorf("incorrect core throttle count, have %v, want %v", want, have)
    92  	}
    93  }
    94  
    95  func TestSystemCpufreq(t *testing.T) {
    96  	fs, err := NewFS(sysTestFixtures)
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  
   101  	c, err := fs.SystemCpufreq()
   102  	if err != nil {
   103  		t.Fatal(err)
   104  	}
   105  
   106  	systemCpufreq := []SystemCPUCpufreqStats{
   107  		// Has missing `cpuinfo_cur_freq` file.
   108  		{
   109  			Name:                     "0",
   110  			CpuinfoCurrentFrequency:  nil,
   111  			CpuinfoMinimumFrequency:  makeUint64(800000),
   112  			CpuinfoMaximumFrequency:  makeUint64(2400000),
   113  			CpuinfoTransitionLatency: makeUint64(0),
   114  			ScalingCurrentFrequency:  makeUint64(1219917),
   115  			ScalingMinimumFrequency:  makeUint64(800000),
   116  			ScalingMaximumFrequency:  makeUint64(2400000),
   117  			AvailableGovernors:       "performance powersave",
   118  			Driver:                   "intel_pstate",
   119  			Governor:                 "powersave",
   120  			RelatedCpus:              "0",
   121  			SetSpeed:                 "<unsupported>",
   122  		},
   123  		// Has missing `scaling_cur_freq` file.
   124  		{
   125  			Name:                     "1",
   126  			CpuinfoCurrentFrequency:  makeUint64(1200195),
   127  			CpuinfoMinimumFrequency:  makeUint64(1200000),
   128  			CpuinfoMaximumFrequency:  makeUint64(3300000),
   129  			CpuinfoTransitionLatency: makeUint64(4294967295),
   130  			ScalingCurrentFrequency:  nil,
   131  			ScalingMinimumFrequency:  makeUint64(1200000),
   132  			ScalingMaximumFrequency:  makeUint64(3300000),
   133  			AvailableGovernors:       "performance powersave",
   134  			Driver:                   "intel_pstate",
   135  			Governor:                 "powersave",
   136  			RelatedCpus:              "1",
   137  			SetSpeed:                 "<unsupported>",
   138  		},
   139  	}
   140  
   141  	if !reflect.DeepEqual(systemCpufreq, c) {
   142  		t.Errorf("Result not correct: want %v, have %v", systemCpufreq, c)
   143  	}
   144  }
   145  
   146  func TestIsolatedParsingCPU(t *testing.T) {
   147  	var testParams = []struct {
   148  		in  []byte
   149  		res []uint16
   150  		err error
   151  	}{
   152  		{[]byte(""), []uint16{}, nil},
   153  		{[]byte("1\n"), []uint16{1}, nil},
   154  		{[]byte("1"), []uint16{1}, nil},
   155  		{[]byte("1,2"), []uint16{1, 2}, nil},
   156  		{[]byte("1-2"), []uint16{1, 2}, nil},
   157  		{[]byte("1-3"), []uint16{1, 2, 3}, nil},
   158  		{[]byte("1,2-4"), []uint16{1, 2, 3, 4}, nil},
   159  		{[]byte("1,3-4"), []uint16{1, 3, 4}, nil},
   160  		{[]byte("1,3-4,7,20-21"), []uint16{1, 3, 4, 7, 20, 21}, nil},
   161  
   162  		{[]byte("1,"), []uint16{1}, nil},
   163  		{[]byte("1,2-"), nil, errors.New(`invalid cpu end range: strconv.Atoi: parsing "": invalid syntax`)},
   164  		{[]byte("1,-3"), nil, errors.New(`invalid cpu start range: strconv.Atoi: parsing "": invalid syntax`)},
   165  	}
   166  	for _, params := range testParams {
   167  		t.Run("blabla", func(t *testing.T) {
   168  			res, err := parseCPURange(params.in)
   169  			if !reflect.DeepEqual(res, params.res) {
   170  				t.Fatalf("should have %v result: got %v", params.res, res)
   171  			}
   172  			if err != nil && params.err != nil && err.Error() != params.err.Error() {
   173  				t.Fatalf("should have '%v' error: got '%v'", params.err, err)
   174  			}
   175  			if (err == nil || params.err == nil) && err != params.err {
   176  				t.Fatalf("should have %v error: got %v", params.err, err)
   177  			}
   178  
   179  		})
   180  	}
   181  }
   182  func TestIsolatedCPUs(t *testing.T) {
   183  	fs, err := NewFS(sysTestFixtures)
   184  	if err != nil {
   185  		t.Fatal(err)
   186  	}
   187  	isolated, err := fs.IsolatedCPUs()
   188  	expected := []uint16{1, 2, 3, 4, 5, 6, 7, 9}
   189  	if !reflect.DeepEqual(isolated, expected) {
   190  		t.Errorf("Result not correct: want %v, have %v", expected, isolated)
   191  	}
   192  	if err != nil {
   193  		t.Errorf("Error not correct: want %v, have %v", nil, err)
   194  	}
   195  }
   196  
   197  func TestBinSearch(t *testing.T) {
   198  	var testParams = []struct {
   199  		elem  uint16
   200  		elems []uint16
   201  		res   bool
   202  	}{
   203  		{3, []uint16{1, 3, 5, 7, 9}, true},
   204  		{4, []uint16{1, 3, 5, 7, 9}, false},
   205  		{2, []uint16{}, false},
   206  	}
   207  
   208  	for _, param := range testParams {
   209  		res := binSearch(param.elem, &param.elems)
   210  
   211  		if res != param.res {
   212  			t.Fatalf("Result not correct: want %v, have %v", param.res, res)
   213  		}
   214  
   215  	}
   216  }
   217  

View as plain text