Skip to content

Commit

Permalink
ptpcheck: phc: add clock setting function (#402)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #402

Enable the `ptpcheck phc` command to set the PTP clock to a given value.

The new flag, `-s`, takes a Unix timestamp as an argument, and writes it to the PTP device. The resolution is thus limited to whole seconds, so the clock then requires a more precise tuning – achievable, for example, with the sptp program.

Reviewed By: abulimov

Differential Revision: D64134612

fbshipit-source-id: 5f89bb96a5539760476a72d11cc24191b6184ab7
  • Loading branch information
yarikk authored and facebook-github-bot committed Oct 10, 2024
1 parent bca0eb8 commit e23b36e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
11 changes: 11 additions & 0 deletions clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (

// Adjtime issues CLOCK_ADJTIME syscall to either adjust the parameters of given clock,
// or read them if buf is empty. man(2) clock_adjtime
// TODO: replace this with a call to https://pkg.go.dev/golang.org/x/sys/unix#ClockAdjtime
func Adjtime(clockid int32, buf *unix.Timex) (state int, err error) {
r0, _, errno := unix.Syscall(unix.SYS_CLOCK_ADJTIME, uintptr(clockid), uintptr(unsafe.Pointer(buf)), 0)
state = int(r0)
Expand All @@ -67,6 +68,16 @@ func Adjtime(clockid int32, buf *unix.Timex) (state int, err error) {
return state, err
}

// Settime issues clock_settime(3) syscall to set the time of the specified clock.
// TODO: issue a PR for golang.org/x/sys/unix to add this function there
func Settime(clockid int32, time *unix.Timespec) (err error) {
_, _, errno := unix.Syscall(unix.SYS_CLOCK_SETTIME, uintptr(clockid), uintptr(unsafe.Pointer(time)), 0)
if errno != 0 {
err = errno
}
return err
}

// FrequencyPPB reads device frequency in PPB
func FrequencyPPB(clockid int32) (freqPPB float64, state int, err error) {
tx := &unix.Timex{}
Expand Down
30 changes: 28 additions & 2 deletions cmd/ptpcheck/cmd/phc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
method string
freq float64
step time.Duration
unixSec int64
setAndPrint bool
)

Expand All @@ -52,13 +53,20 @@ func init() {
)
flags.Float64VarP(&freq, "freq", "f", math.NaN(), "set the frequency (PPB)")
flags.DurationVarP(&step, "step", "t", 0, "step the clock")
flags.Int64VarP(&unixSec, "set", "s", -1, "set the clock to Unix seconds, like $(date +%s)")
flags.BoolVarP(&setAndPrint, "print", "p", false, "print clock status after changes")
}

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

ConfigureVerbosity()
if unixSec != -1 {
if err := setPHC(device, unixSec); err != nil {
log.Fatal(err)
}
doPrint = setAndPrint
}
if step != 0 {
if err := stepPHC(device, step); err != nil {
log.Fatal(err)
Expand All @@ -78,6 +86,20 @@ func runPhcCmd(_ *cobra.Command, _ []string) {
}
}

func setPHC(device string, unixSec int64) 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)

t := time.Unix(unixSec, 0)

fmt.Printf("Setting the clock to %v (%v in Unix seconds)\n", t, unixSec)
return dev.SetTime(t)
}

func stepPHC(device string, step time.Duration) error {
f, err := os.OpenFile(device, os.O_RDWR, 0)
if err != nil {
Expand Down Expand Up @@ -121,8 +143,12 @@ func printPHC(device string, method phc.TimeMethod) error {
return err
}
}
fmt.Printf("PHC clock: %s\n", timeAndOffset.PHCTime)
fmt.Printf("SYS clock: %s\n", timeAndOffset.SysTime)

timePHC := timeAndOffset.PHCTime
timeSys := timeAndOffset.SysTime

fmt.Printf("PHC clock: %s (%v in Unix seconds)\n", timePHC, timePHC.Unix())
fmt.Printf("SYS clock: %s (%v in Unix seconds)\n", timeSys, timeSys.Unix())
fmt.Printf("Offset: %s\n", timeAndOffset.Offset)
fmt.Printf("Delay: %s\n", timeAndOffset.Delay)

Expand Down
8 changes: 8 additions & 0 deletions phc/adjtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,11 @@ func clockStep(dev *Device, step time.Duration) error {
}
return err
}

func clockSetTime(dev *Device, t time.Time) error {
ts, err := unix.TimeToTimespec(t)
if err != nil {
return err
}
return clock.Settime(dev.ClockID(), &ts)
}
3 changes: 3 additions & 0 deletions phc/phc.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ func (dev *Device) AdjFreq(freqPPB float64) error { return clockAdjFreq(dev, fre
// Step steps the PHC clock by given duration
func (dev *Device) Step(step time.Duration) error { return clockStep(dev, step) }

// SetTime sets the time of the PHC clock
func (dev *Device) SetTime(t time.Time) error { return clockSetTime(dev, t) }

func (dev *Device) Read(buffer []byte) (int, error) {
return syscall.Read(int(dev.Fd()), buffer)
}

0 comments on commit e23b36e

Please sign in to comment.