...

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

Documentation: github.com/prometheus/procfs/sysfs

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

View as plain text