phc

package
v0.0.0-...-b56da86 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package phc contains code to work with PTP Hardware Clock (PHC). It allows getting PHC time via different APIs (syscall, ioctl).

It also provides means to calculate offset between sys clock and PHC.

Index

Constants

View Source
const DefaultMaxClockFreqPPB = 500000.0

DefaultMaxClockFreqPPB value came from linuxptp project (clockadj.c)

View Source
const (
	// ExtendedNumProbes is the number of samples we request for IOCTL SYS_OFFSET_EXTENDED
	ExtendedNumProbes = 9
)

Variables

SupportedMethods is a list of supported TimeMethods

Functions

func ClockAdjFreq

func ClockAdjFreq(phcDevice string, freqPPB float64) error

ClockAdjFreq adjusts PHC clock frequency in PPB

func ClockStep

func ClockStep(phcDevice string, step time.Duration) error

ClockStep steps PHC clock by given step

func DeviceFromIface

func DeviceFromIface(iface string) (string, error)

DeviceFromIface returns a path to a PHC device from a network interface

func FDToClockID

func FDToClockID(fd uintptr) int32

FDToClockID converts file descriptor number to clockID. see man(3) clock_gettime, FD_TO_CLOCKID macros

func FrequencyPPB

func FrequencyPPB(iface string) (float64, error)

FrequencyPPB reads network card PHC device frequency in PPB

func FrequencyPPBFromDevice

func FrequencyPPBFromDevice(phcDevice string) (freqPPB float64, err error)

FrequencyPPBFromDevice reads PHC device frequency in PPB

func IfaceToPHCDevice

func IfaceToPHCDevice(iface string) (string, error)

IfaceToPHCDevice returns path to PHC device associated with given network card iface

func MaxFreqAdjPPBFromDevice

func MaxFreqAdjPPBFromDevice(phcDevice string) (maxFreq float64, err error)

MaxFreqAdjPPBFromDevice reads max value for frequency adjustments (in PPB) from ptp device

func OffsetBetweenDevices

func OffsetBetweenDevices(deviceA, deviceB string) (time.Duration, error)

OffsetBetweenDevices returns estimated difference between two PHC devices

func OffsetBetweenExtendedReadings

func OffsetBetweenExtendedReadings(extendedA, extendedB *PTPSysOffsetExtended) time.Duration

OffsetBetweenExtendedReadings returns estimated difference between two PHC SYS_OFFSET_EXTENDED readings

func Time

func Time(iface string, method TimeMethod) (time.Time, error)

Time returns time we got from network card

func TimeFromDevice

func TimeFromDevice(device string) (time.Time, error)

TimeFromDevice returns time we got from PTP device

Types

type EthtoolTSinfo

type EthtoolTSinfo struct {
	Cmd            uint32
	SOtimestamping uint32
	PHCIndex       int32
	TXTypes        uint32
	TXReserved     [3]uint32
	RXFilters      uint32
	RXReserved     [3]uint32
}

EthtoolTSinfo holds a device's timestamping and PHC association as per Linux kernel's include/uapi/linux/ethtool.h

func IfaceInfo

func IfaceInfo(iface string) (*EthtoolTSinfo, error)

IfaceInfo uses SIOCETHTOOL ioctl to get information for the give nic, i.e. eth0.

type IfaceData

type IfaceData struct {
	Iface  net.Interface
	TSInfo EthtoolTSinfo
}

IfaceData has both net.Interface and EthtoolTSinfo

func IfacesInfo

func IfacesInfo() ([]IfaceData, error)

IfacesInfo is like net.Interfaces() but with added EthtoolTSinfo

type Ifreq

type Ifreq struct {
	Name [unix.IFNAMSIZ]byte
	Data uintptr
}

Ifreq is the request we send with SIOCETHTOOL IOCTL as per Linux kernel's include/uapi/linux/if.h

type PTPClockCaps

type PTPClockCaps struct {
	MaxAdj  int32 /* Maximum frequency adjustment in parts per billon. */
	NAalarm int32 /* Number of programmable alarms. */
	NExtTs  int32 /* Number of external time stamp channels. */
	NPerOut int32 /* Number of programmable periodic signals. */
	PPS     int32 /* Whether the clock supports a PPS callback. */
	NPins   int32 /* Number of input/output pins. */
	/* Whether the clock supports precise system-device cross timestamps */
	CrossTimestamping int32
	/* Whether the clock supports adjust phase */
	AdjustPhase int32
	Rsv         [12]int32 /* Reserved for future use. */
}

PTPClockCaps as defined in linux/ptp_clock.h

func ReadPTPClockCapsFromDevice

func ReadPTPClockCapsFromDevice(phcDevice string) (*PTPClockCaps, error)

ReadPTPClockCapsFromDevice reads ptp capabilities using ioctl

type PTPClockTime

type PTPClockTime struct {
	Sec      int64  /* seconds */
	NSec     uint32 /* nanoseconds */
	Reserved uint32
}

PTPClockTime as defined in linux/ptp_clock.h

func (PTPClockTime) Time

func (t PTPClockTime) Time() time.Time

Time returns PTPClockTime as time.Time

type PTPSysOffsetExtended

type PTPSysOffsetExtended struct {
	NSamples uint32    /* Desired number of measurements. */
	Reserved [3]uint32 /* Reserved for future use. */
	/*
	 * Array of [system, phc, system] time stamps. The kernel will provide
	 * 3*n_samples time stamps.
	 * - system time right before reading the lowest bits of the PHC timestamp
	 * - PHC time
	 * - system time immediately after reading the lowest bits of the PHC timestamp
	 */
	TS [ptpMaxSamples][3]PTPClockTime
}

PTPSysOffsetExtended as defined in linux/ptp_clock.h

func ReadPTPSysOffsetExtended

func ReadPTPSysOffsetExtended(device string, nsamples int) (*PTPSysOffsetExtended, error)

ReadPTPSysOffsetExtended gets precise time from PHC along with SYS time to measure the call delay.

type SysoffResult

type SysoffResult struct {
	Offset  time.Duration
	Delay   time.Duration
	SysTime time.Time
	PHCTime time.Time
}

SysoffResult is a result of PHC time measurement with related data

func SysoffEstimateBasic

func SysoffEstimateBasic(ts1, rt, ts2 time.Time) SysoffResult

SysoffEstimateBasic logic based on calculate_offset from ptp4l phc_ctl.c

func SysoffEstimateExtended

func SysoffEstimateExtended(extended *PTPSysOffsetExtended) SysoffResult

SysoffEstimateExtended finds sample which took least time to be read, logic loosely based on sysoff_estimate from ptp4l sysoff.c

func TimeAndOffset

func TimeAndOffset(iface string, method TimeMethod) (SysoffResult, error)

TimeAndOffset returns time we got from network card + offset

func TimeAndOffsetFromDevice

func TimeAndOffsetFromDevice(device string, method TimeMethod) (SysoffResult, error)

TimeAndOffsetFromDevice returns time we got from phc device + offset

type TimeMethod

type TimeMethod string

TimeMethod is method we use to get time

const (
	MethodSyscallClockGettime    TimeMethod = "syscall_clock_gettime"
	MethodIoctlSysOffsetExtended TimeMethod = "ioctl_PTP_SYS_OFFSET_EXTENDED"
)

Methods we support to get time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL