Skip to content

Commit

Permalink
Some clock-id's can overflow int64 so instead used (math/big).Int
Browse files Browse the repository at this point in the history
  • Loading branch information
nocturnalastro committed May 22, 2024
1 parent 5dac8ea commit f3a3b4b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
48 changes: 26 additions & 22 deletions pkg/collectors/devices/dpll_netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"math/big"
"strings"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -45,11 +45,11 @@ func (dpllInfo *DevNetlinkDPLLInfo) GetAnalyserFormat() ([]*callbacks.AnalyserFo
}

type NetlinkEntry struct {
LockStatus string `json:"lock-status"` //nolint:tagliatelle // not my choice
Driver string `json:"module-name"` //nolint:tagliatelle // not my choice
ClockType string `json:"type"` //nolint:tagliatelle // not my choice
ClockID int64 `json:"clock-id"` //nolint:tagliatelle // not my choice
ID int `json:"id"` //nolint:tagliatelle // not my choice
ClockID *big.Int `json:"clock-id"` //nolint:tagliatelle // These are form the tool output
LockStatus string `json:"lock-status"` //nolint:tagliatelle // These are form the tool output
Driver string `json:"module-name"` //nolint:tagliatelle // These are form the tool output
ClockType string `json:"type"`
ID int `json:"id"`
}

// # Example output
Expand All @@ -69,16 +69,16 @@ type NetlinkEntry struct {
// 'type': 'pps'}]

var (
dpllNetlinkFetcher map[int64]*fetcher.Fetcher
dpllNetlinkFetcher map[string]*fetcher.Fetcher
dpllClockIDFetcher map[string]*fetcher.Fetcher
)

func init() {
dpllNetlinkFetcher = make(map[int64]*fetcher.Fetcher)
dpllNetlinkFetcher = make(map[string]*fetcher.Fetcher)
dpllClockIDFetcher = make(map[string]*fetcher.Fetcher)
}

func buildPostProcessDPLLNetlink(clockID int64) fetcher.PostProcessFuncType {
func buildPostProcessDPLLNetlink(clockID *big.Int) fetcher.PostProcessFuncType {
return func(result map[string]string) (map[string]any, error) {
processedResult := make(map[string]any)

Expand All @@ -91,7 +91,7 @@ func buildPostProcessDPLLNetlink(clockID int64) fetcher.PostProcessFuncType {

log.Debug("entries: ", entries)
for _, entry := range entries {
if entry.ClockID == clockID {
if entry.ClockID.Cmp(clockID) == 0 {
state, ok := states[entry.LockStatus]
if !ok {
log.Errorf("Unknown state: %s", state)
Expand All @@ -106,7 +106,7 @@ func buildPostProcessDPLLNetlink(clockID int64) fetcher.PostProcessFuncType {

// BuildDPLLNetlinkInfoFetcher popluates the fetcher required for
// collecting the DPLLInfo
func BuildDPLLNetlinkInfoFetcher(clockID int64) error { //nolint:dupl // Further dedup risks be too abstract or fragile
func BuildDPLLNetlinkInfoFetcher(clockID *big.Int) error { //nolint:dupl // Further dedup risks be too abstract or fragile
fetcherInst, err := fetcher.FetcherFactory(
[]*clients.Cmd{dateCmd},
[]fetcher.AddCommandArgs{
Expand All @@ -121,21 +121,21 @@ func BuildDPLLNetlinkInfoFetcher(clockID int64) error { //nolint:dupl // Further
log.Errorf("failed to create fetcher for dpll netlink: %s", err.Error())
return fmt.Errorf("failed to create fetcher for dpll netlink: %w", err)
}
dpllNetlinkFetcher[clockID] = fetcherInst
dpllNetlinkFetcher[clockID.String()] = fetcherInst
fetcherInst.SetPostProcessor(buildPostProcessDPLLNetlink(clockID))
return nil
}

// GetDevDPLLInfo returns the device DPLL info for an interface.
func GetDevDPLLNetlinkInfo(ctx clients.ExecContext, clockID int64) (DevNetlinkDPLLInfo, error) {
func GetDevDPLLNetlinkInfo(ctx clients.ExecContext, clockID *big.Int) (DevNetlinkDPLLInfo, error) {
dpllInfo := DevNetlinkDPLLInfo{}
fetcherInst, fetchedInstanceOk := dpllNetlinkFetcher[clockID]
fetcherInst, fetchedInstanceOk := dpllNetlinkFetcher[clockID.String()]
if !fetchedInstanceOk {
err := BuildDPLLNetlinkInfoFetcher(clockID)
if err != nil {
return dpllInfo, err
}
fetcherInst, fetchedInstanceOk = dpllNetlinkFetcher[clockID]
fetcherInst, fetchedInstanceOk = dpllNetlinkFetcher[clockID.String()]
if !fetchedInstanceOk {
return dpllInfo, errors.New("failed to create fetcher for DPLLInfo using netlink interface")
}
Expand All @@ -153,10 +153,10 @@ func BuildClockIDFetcher(interfaceName string) error {
[]*clients.Cmd{dateCmd},
[]fetcher.AddCommandArgs{
{
Key: "dpll-netlink-clock-id",
Key: "dpll-netlink-serial-number",
Command: fmt.Sprintf(
`export IFNAME=%s; export BUSID=$(readlink /sys/class/net/$IFNAME/device | xargs basename | cut -d ':' -f 2,3);`+
` echo $(("16#$(lspci -v | grep $BUSID -A 20 |grep 'Serial Number' | awk '{print $NF}' | tr -d '-')"))`,
` echo $(lspci -v | grep $BUSID -A 20 |grep 'Serial Number' | awk '{print $NF}' | tr -d '-')`,
interfaceName,
),
Trim: true,
Expand All @@ -174,17 +174,21 @@ func BuildClockIDFetcher(interfaceName string) error {

func postProcessDPLLNetlinkClockID(result map[string]string) (map[string]any, error) {
processedResult := make(map[string]any)
clockID, err := strconv.ParseInt(result["dpll-netlink-clock-id"], 10, 64)
if err != nil {
return processedResult, fmt.Errorf("failed to parse int for clock id: %w", err)
clockID := new(big.Int)
_, ok := clockID.SetString(result["dpll-netlink-serial-number"], 16) //nolint:gomnd

Check failure on line 178 in pkg/collectors/devices/dpll_netlink.go

View workflow job for this annotation

GitHub Actions / Run Linter and Vet

whyNoLint: include an explanation for nolint directive (gocritic)

if !ok {
return processedResult, fmt.Errorf(
"failed to parse int for clock id from serial number: %s", result["dpll-netlink-serial-number"],
)
}
processedResult["clockID"] = clockID
return processedResult, nil
}

type NetlinkClockID struct {
Timestamp string `fetcherKey:"date" json:"timestamp"`
ClockID int64 `fetcherKey:"clockID" json:"clockId"`
ClockID *big.Int `fetcherKey:"clockID" json:"clockId"`
Timestamp string `fetcherKey:"date" json:"timestamp"`
}

func GetClockID(ctx clients.ExecContext, interfaceName string) (NetlinkClockID, error) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/collectors/dpll_collector_netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package collectors

import (
"fmt"
"math/big"

log "github.com/sirupsen/logrus"

Expand All @@ -16,8 +17,8 @@ import (
type DPLLNetlinkCollector struct {
*baseCollector
ctx *clients.ContainerCreationExecContext
clockID *big.Int
interfaceName string
clockID int64
}

const (
Expand Down

0 comments on commit f3a3b4b

Please sign in to comment.