1// Copyright 2019 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 14package procfs 15 16import ( 17 "testing" 18) 19 20func TestSchedstat(t *testing.T) { 21 stats, err := getProcFixtures(t).Schedstat() 22 if err != nil { 23 t.Fatal(err) 24 } 25 26 if len(stats.CPUs) != 2 { 27 t.Errorf("expected 2 CPUs, got %v", len(stats.CPUs)) 28 } 29 30 var cpu *SchedstatCPU 31 for _, cpu = range stats.CPUs { 32 if cpu.CPUNum == "0" { 33 break 34 } 35 } 36 37 if cpu == nil || cpu.CPUNum != "0" { 38 t.Error("could not find cpu0") 39 } 40 41 if want, have := uint64(2045936778163039), cpu.RunningNanoseconds; want != have { 42 t.Errorf("want RunningNanoseconds %v, have %v", want, have) 43 } 44 45 if want, have := uint64(343796328169361), cpu.WaitingNanoseconds; want != have { 46 t.Errorf("want WaitingNanoseconds %v, have %v", want, have) 47 } 48 49 if want, have := uint64(4767485306), cpu.RunTimeslices; want != have { 50 t.Errorf("want RunTimeslices %v, have %v", want, have) 51 } 52} 53 54func TestProcSchedstat(t *testing.T) { 55 p1, err := getProcFixtures(t).Proc(26231) 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 schedstat, err := p1.Schedstat() 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 if want, have := uint64(411605849), schedstat.RunningNanoseconds; want != have { 66 t.Errorf("want RunningNanoseconds %v, have %v", want, have) 67 } 68 69 if want, have := uint64(93680043), schedstat.WaitingNanoseconds; want != have { 70 t.Errorf("want WaitingNanoseconds %v, have %v", want, have) 71 } 72 73 if want, have := uint64(79), schedstat.RunTimeslices; want != have { 74 t.Errorf("want RunTimeslices %v, have %v", want, have) 75 } 76} 77 78func TestProcSchedstatErrors(t *testing.T) { 79 p1, err := getProcFixtures(t).Proc(26232) 80 if err != nil { 81 t.Fatal(err) 82 } 83 84 _, err = p1.Schedstat() 85 if err == nil { 86 t.Error("proc 26232 doesn't have schedstat -- should have gotten an error") 87 } 88 89 p2, err := getProcFixtures(t).Proc(26233) 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 _, err = p2.Schedstat() 95 if err == nil { 96 t.Error("proc 26233 has malformed schedstat -- should have gotten an error") 97 } 98} 99 100// schedstat can have a 2nd line: it should be ignored 101func TestProcSchedstatMultipleLines(t *testing.T) { 102 schedstat, err := parseProcSchedstat("123 456 789\n10 11\n") 103 if err != nil { 104 t.Fatal(err) 105 } 106 if want, have := uint64(123), schedstat.RunningNanoseconds; want != have { 107 t.Errorf("want RunningNanoseconds %v, have %v", want, have) 108 } 109 if want, have := uint64(456), schedstat.WaitingNanoseconds; want != have { 110 t.Errorf("want WaitingNanoseconds %v, have %v", want, have) 111 } 112 if want, have := uint64(789), schedstat.RunTimeslices; want != have { 113 t.Errorf("want RunTimeslices %v, have %v", want, have) 114 } 115} 116 117func TestProcSchedstatUnparsableInt(t *testing.T) { 118 if _, err := parseProcSchedstat("abc 456 789\n"); err == nil { 119 t.Error("schedstat should have been unparsable\n") 120 } 121 122 if _, err := parseProcSchedstat("123 abc 789\n"); err == nil { 123 t.Error("schedstat should have been unparsable\n") 124 } 125 126 if _, err := parseProcSchedstat("123 456 abc\n"); err == nil { 127 t.Error("schedstat should have been unparsable\n") 128 } 129} 130