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 14// +build !windows 15 16package sysfs 17 18import ( 19 "path/filepath" 20 "strings" 21 22 "github.com/prometheus/procfs/internal/util" 23) 24 25// ClockSource contains metrics related to the clock source 26type ClockSource struct { 27 Name string 28 Available []string 29 Current string 30} 31 32// ClockSources returns clocksource information including current and available clocksources 33// read from '/sys/devices/system/clocksource' 34func (fs FS) ClockSources() ([]ClockSource, error) { 35 36 clocksourcePaths, err := filepath.Glob(fs.sys.Path("devices/system/clocksource/clocksource[0-9]*")) 37 if err != nil { 38 return nil, err 39 } 40 41 clocksources := make([]ClockSource, len(clocksourcePaths)) 42 for i, clocksourcePath := range clocksourcePaths { 43 clocksourceName := strings.TrimPrefix(filepath.Base(clocksourcePath), "clocksource") 44 45 clocksource, err := parseClocksource(clocksourcePath) 46 if err != nil { 47 return nil, err 48 } 49 clocksource.Name = clocksourceName 50 clocksources[i] = *clocksource 51 } 52 53 return clocksources, nil 54} 55 56func parseClocksource(clocksourcePath string) (*ClockSource, error) { 57 58 stringFiles := []string{ 59 "available_clocksource", 60 "current_clocksource", 61 } 62 stringOut := make([]string, len(stringFiles)) 63 var err error 64 65 for i, f := range stringFiles { 66 stringOut[i], err = util.SysReadFile(filepath.Join(clocksourcePath, f)) 67 if err != nil { 68 return &ClockSource{}, err 69 } 70 } 71 72 return &ClockSource{ 73 Available: strings.Fields(stringOut[0]), 74 Current: stringOut[1], 75 }, nil 76} 77