Skip to content

Commit

Permalink
ptpcheck: merge adjfreq subcommand into phc subcommand
Browse files Browse the repository at this point in the history
Summary: Make `ptpcheck phc` subcommand to incorporate the functionality of `ptpcheck adjfreq`: `ptpcheck phc -f `*freq* now sets the frequency on device; `-t` steps the clock by a delta. When we do either of these settings, printing of resulting state is suppressed – unless `-p` is also specified.

Reviewed By: leoleovich

Differential Revision: D63980265

fbshipit-source-id: 9e4822ca493bde7686dfe037d02e2ccfcca20f4b
  • Loading branch information
yarikk authored and facebook-github-bot committed Oct 7, 2024
1 parent ca26163 commit 93d8b25
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 105 deletions.
85 changes: 0 additions & 85 deletions cmd/ptpcheck/cmd/adjfreq.go

This file was deleted.

114 changes: 94 additions & 20 deletions cmd/ptpcheck/cmd/phc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,95 @@ package cmd

import (
"fmt"
"math"
"os"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/facebook/time/phc"
)

// flag
var device string
var method string
// flags
var (
device string
method string
freq float64
step time.Duration
setAndPrint bool
)

var phcCmd = &cobra.Command{
Use: "phc",
Short: "Print PHC clock information. Use `phc_ctl` cli for richer functionality",
Run: runPhcCmd,
}

func init() {
RootCmd.AddCommand(phcCmd)
phcCmd.Flags().StringVarP(&device, "device", "d", "/dev/ptp0", "PTP device to get time from")
phcCmd.Flags().StringVarP(
&method,
"method",
"m",
string(phc.MethodIoctlSysOffsetExtended),
flags := phcCmd.Flags()
flags.StringVarP(&device, "device", "d", "/dev/ptp0", "PTP device to get time from")
flags.StringVarP(&method, "method", "m", string(phc.MethodIoctlSysOffsetExtended),
fmt.Sprintf("Method to get PHC time: %v", phc.SupportedMethods),
)
flags.Float64VarP(&freq, "freq", "f", math.NaN(), "set the frequency (PPB)")
flags.DurationVarP(&step, "step", "t", 0, "step the clock")
flags.BoolVarP(&setAndPrint, "print", "p", false, "print clock status after changes")
}

func runPhcCmd(_ *cobra.Command, _ []string) {
var doPrint = true

ConfigureVerbosity()
if step != 0 {
if err := stepPHC(device, step); err != nil {
log.Fatal(err)
}
doPrint = setAndPrint
}
if !math.IsNaN(freq) {
if err := tunePHC(device, freq); err != nil {
log.Fatal(err)
}
doPrint = setAndPrint
}
if doPrint {
if err := printPHC(device, phc.TimeMethod(method)); err != nil {
log.Fatal(err)
}
}
}

func stepPHC(device string, step time.Duration) error {
f, err := os.OpenFile(device, os.O_RDWR, 0)
if err != nil {
return fmt.Errorf("opening device %q: %w", device, err)
}
defer f.Close()
dev := phc.FromFile(f)

fmt.Printf("Stepping the clock by %v\n", step)
return dev.Step(step)
}

func tunePHC(device string, freq float64) error {
f, err := os.OpenFile(device, os.O_RDWR, 0)
if err != nil {
return fmt.Errorf("opening device %q: %w", device, err)
}
defer f.Close()
dev := phc.FromFile(f)

maxFreq, err := dev.MaxFreqAdjPPB()
if err != nil {
return err
}
if freq < -maxFreq || freq > maxFreq {
return fmt.Errorf("frequncy %f is out supported range", freq)
}
fmt.Printf("Setting new frequency value %f\n", freq)
return dev.AdjFreq(freq)
}

func printPHC(device string, method phc.TimeMethod) error {
Expand All @@ -57,17 +125,23 @@ func printPHC(device string, method phc.TimeMethod) error {
fmt.Printf("SYS clock: %s\n", timeAndOffset.SysTime)
fmt.Printf("Offset: %s\n", timeAndOffset.Offset)
fmt.Printf("Delay: %s\n", timeAndOffset.Delay)
return nil
}

var phcCmd = &cobra.Command{
Use: "phc",
Short: "Print PHC clock information. Use `phc_ctl` cli for richer functionality",
Run: func(_ *cobra.Command, _ []string) {
ConfigureVerbosity()
f, err := os.OpenFile(device, os.O_RDWR, 0)
if err != nil {
return fmt.Errorf("opening device %q to read frequency: %w", device, err)
}
defer f.Close()
dev := phc.FromFile(f)

if err := printPHC(device, phc.TimeMethod(method)); err != nil {
log.Fatal(err)
}
},
curFreq, err := dev.FreqPPB()
if err != nil {
return err
}
maxFreq, err := dev.MaxFreqAdjPPB()
if err != nil {
return err
}
fmt.Printf("Current frequency: %f\n", curFreq)
fmt.Printf("Frequency range: [%.2f, %.2f]\n", -maxFreq, maxFreq)
return nil
}

0 comments on commit 93d8b25

Please sign in to comment.