Skip to content

Commit

Permalink
fix clock class updates
Browse files Browse the repository at this point in the history
This commit introduces periodical clock class checks to
indicate when there is a change

Signed-off-by: Vitaly Grinberg <[email protected]>
  • Loading branch information
vitus133 authored and josephdrichard committed Jun 7, 2024
1 parent 2c21933 commit cdda3ed
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/daemon/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
type cliParams struct {
updateInterval int
profileDir string
pmcPollInterval int
}

// Parse Command line flags
Expand All @@ -34,6 +35,8 @@ func flagInit(cp *cliParams) {
"Interval to update PTP status")
flag.StringVar(&cp.profileDir, "linuxptp-profile-path", config.DefaultProfilePath,
"profile to start linuxptp processes")
flag.IntVar(&cp.pmcPollInterval, "pmc-poll-interval", config.DefaultPmcPollInterval,
"Interval for periodical PMC poll")
}

func main() {
Expand All @@ -43,6 +46,7 @@ func main() {

glog.Infof("resync period set to: %d [s]", cp.updateInterval)
glog.Infof("linuxptp profile path set to: %s", cp.profileDir)
glog.Infof("pmc poll interval set to: %d [s]", cp.pmcPollInterval)

cfg, err := config.GetKubeConfig()
if err != nil {
Expand Down Expand Up @@ -106,6 +110,7 @@ func main() {
plugins,
&hwconfigs,
closeProcessManager,
cp.pmcPollInterval,
).Run()

tickerPull := time.NewTicker(time.Second * time.Duration(cp.updateInterval))
Expand Down
1 change: 1 addition & 0 deletions pkg/daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const (
DefaultUpdateInterval = 30
DefaultProfilePath = "/etc/linuxptp"
DefaultPmcPollInterval = 5
)

func GetKubeConfig() (*rest.Config, error) {
Expand Down
55 changes: 55 additions & 0 deletions pkg/daemon/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type ptpProcess struct {
cmd *exec.Cmd
depProcess []process // this could gpsd and other process which needs to be stopped if the parent process is stopped
nodeProfile *ptpv1.PtpProfile
parentClockClass float64
pmcCheck bool
}

func (p *ptpProcess) Stopped() bool {
Expand Down Expand Up @@ -82,6 +84,8 @@ type Daemon struct {
// channel ensure LinuxPTP.Run() exit when main function exits.
// stopCh is created by main function and passed by Daemon via NewLinuxPTP()
stopCh <-chan struct{}
pmcPollInterval int


// Allow vendors to include plugins
pluginManager PluginManager
Expand All @@ -96,6 +100,7 @@ func New(
stopCh <-chan struct{},
plugins []string,
hwconfigs *[]ptpv1.HwConfig,
pmcPollInterval int,
) *Daemon {
RegisterMetrics(nodeName)
pluginManager := registerPlugins(plugins)
Expand All @@ -108,18 +113,24 @@ func New(
stopCh: stopCh,
pluginManager: pluginManager,
hwconfigs: hwconfigs,
refreshNodePtpDevice: refreshNodePtpDevice,
}
}

// Run in a for loop to listen for any LinuxPTPConfUpdate changes
func (dn *Daemon) Run() {
tickerPmc := time.NewTicker(time.Second * time.Duration(dn.pmcPollInterval))
defer tickerPmc.Stop()
for {
select {
case <-dn.ptpUpdate.UpdateCh:
err := dn.applyNodePTPProfiles()
if err != nil {
glog.Errorf("linuxPTP apply node profile failed: %v", err)
}
case <-tickerPmc.C:
dn.HandlePmcTicker()

case <-dn.stopCh:
for _, p := range dn.processManager.process {
if p != nil {
Expand Down Expand Up @@ -407,6 +418,13 @@ func (dn *Daemon) applyNodePtpProfile(runID int, nodeProfile *ptpv1.PtpProfile)
return nil
}

func (dn *Daemon) HandlePmcTicker() {
for _, process := range dn.processManager.process {
if process.name == ptp4lProcessName {
process.pmcCheck = true
}
}

// Add fifo scheduling if specified in nodeProfile
func addScheduling(nodeProfile *ptpv1.PtpProfile, cmdLine string) string {
if nodeProfile.PtpSchedulingPolicy != nil && *nodeProfile.PtpSchedulingPolicy == "SCHED_FIFO" {
Expand Down Expand Up @@ -434,6 +452,38 @@ func processStatus(processName, messageTag string, status int64) {
glog.Infof("%s\n", deadProcessMsg)
}

func (p *ptpProcess) updateClockClass(c *net.Conn) {
if _, matches, e := pmc.RunPMCExp(p.configName, pmc.CmdParentDataSet, pmc.ClockClassChangeRegEx); e == nil {
//regex: 'gm.ClockClass[[:space:]]+(\d+)'
//match 1: 'gm.ClockClass 135'
//match 2: '135'
if len(matches) > 1 {
var parseError error
var clockClass float64
if clockClass, parseError = strconv.ParseFloat(matches[1], 64); parseError == nil {
if clockClass != p.parentClockClass {
p.parentClockClass = clockClass
glog.Infof("clock change event identified")
//ptp4l[5196819.100]: [ptp4l.0.config] CLOCK_CLASS_CHANGE:248
clockClassOut := fmt.Sprintf("%s[%d]:[%s] CLOCK_CLASS_CHANGE %f\n", p.name, time.Now().Unix(), p.configName, clockClass)
fmt.Printf("%s", clockClassOut)

_, err := (*c).Write([]byte(clockClassOut))
if err != nil {
glog.Errorf("failed to write class change event %s", err.Error())
}
}
} else {
glog.Errorf("parse error in clock class value %s", parseError)
}
} else {
glog.Infof("clock class change value not found via PMC")
}
} else {
glog.Error("error parsing PMC util for clock class change event")
}
}

// cmdRun runs given ptpProcess and restarts on errors
func (p *ptpProcess) cmdRun() {
done := make(chan struct{}) // Done setting up logging. Go ahead and wait for process
Expand Down Expand Up @@ -464,6 +514,11 @@ func (p *ptpProcess) cmdRun() {
go func() {
for scanner.Scan() {
output := scanner.Text()
if p.pmcCheck {
p.pmcCheck = false
go p.updateClockClass(&c)
}

if regexErr != nil || !logFilterRegex.MatchString(output) {
fmt.Printf("%s\n", output)
}
Expand Down

0 comments on commit cdda3ed

Please sign in to comment.