-
Notifications
You must be signed in to change notification settings - Fork 71
/
phcdiff.go
86 lines (73 loc) · 2.45 KB
/
phcdiff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Copyright (c) Facebook, Inc. and its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"encoding/json"
"fmt"
"time"
"github.com/facebook/time/phc"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
type phcStats struct {
PHCOffset time.Duration `json:"ptp.phc.offset_ns"`
PHC1Delay time.Duration `json:"ptp.phc.1.delay_ns"`
PHC2Delay time.Duration `json:"ptp.phc.2.delay_ns"`
}
var (
phcDiffDeviceA string
phcDiffDeviceB string
phcDiffIsJSON bool
)
func init() {
RootCmd.AddCommand(phcdiffCmd)
phcdiffCmd.Flags().StringVarP(&phcDiffDeviceA, "deviceA", "a", "/dev/ptp0", "First PHC device")
phcdiffCmd.Flags().StringVarP(&phcDiffDeviceB, "deviceB", "b", "/dev/ptp2", "Second PHC device")
phcdiffCmd.Flags().BoolVarP(&phcDiffIsJSON, "json", "j", false, "produce json output")
}
func phcdiffRun(deviceA, deviceB string, isJSON bool) error {
extendedA, err := phc.ReadPTPSysOffsetExtended(deviceA, phc.ExtendedNumProbes)
if err != nil {
return err
}
extendedB, err := phc.ReadPTPSysOffsetExtended(deviceB, phc.ExtendedNumProbes)
if err != nil {
return err
}
timeAndOffsetA := phc.SysoffEstimateExtended(extendedA)
timeAndOffsetB := phc.SysoffEstimateExtended(extendedB)
phcOffset := phc.OffsetBetweenExtendedReadings(extendedA, extendedB)
if isJSON {
stats := phcStats{PHCOffset: phcOffset, PHC1Delay: timeAndOffsetA.Delay, PHC2Delay: timeAndOffsetB.Delay}
str, err := json.Marshal(stats)
if err != nil {
return fmt.Errorf("marshaling json: %w", err)
}
fmt.Println(string(str))
} else {
fmt.Printf("PHC offset: %s\n", phcOffset)
fmt.Printf("Delay for PHC1: %s\n", timeAndOffsetA.Delay)
fmt.Printf("Delay for PHC2: %s\n", timeAndOffsetB.Delay)
}
return nil
}
var phcdiffCmd = &cobra.Command{
Use: "phcdiff",
Short: "Print diff in ns between 2 PHCs",
Run: func(_ *cobra.Command, _ []string) {
ConfigureVerbosity()
if err := phcdiffRun(phcDiffDeviceA, phcDiffDeviceB, phcDiffIsJSON); err != nil {
log.Fatal(err)
}
},
}