Skip to content

Commit

Permalink
Merge pull request #225 from aneeshkp/concurrent-map
Browse files Browse the repository at this point in the history
OCPBUGS-20413: [release 4-12] concurrent map reading master offset map in linuxptpdaemon
  • Loading branch information
openshift-merge-bot[bot] committed Nov 22, 2023
2 parents 61e1363 + eac0718 commit b3896c2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
1 change: 1 addition & 0 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func New(
if !stdoutToSocket {
RegisterMetrics(nodeName)
}
InitializeOffsetMaps()
return &Daemon{
nodeName: nodeName,
namespace: namespace,
Expand Down
72 changes: 53 additions & 19 deletions pkg/daemon/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ const (
UNKNOWN
)

type masterOffsetInterface struct { // by slave iface with masked index
sync.RWMutex
name map[string]string
}
type slaveInterface struct { // current slave iface name
sync.RWMutex
name map[string]string
}

var (
masterOffsetIfaceName map[string]string // by slave iface with masked index
slaveIfaceName map[string]string // current slave iface name
NodeName = ""
masterOffsetIface *masterOffsetInterface // by slave iface with masked index
slaveIface *slaveInterface // current slave iface name
NodeName = ""

Offset = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -151,9 +160,17 @@ func RegisterMetrics(nodeName string) {

NodeName = nodeName
})
}

masterOffsetIfaceName = map[string]string{}
slaveIfaceName = map[string]string{}
func InitializeOffsetMaps() {
masterOffsetIface = &masterOffsetInterface{
RWMutex: sync.RWMutex{},
name: map[string]string{},
}
slaveIface = &slaveInterface{
RWMutex: sync.RWMutex{},
name: map[string]string{},
}
}

// updatePTPMetrics ...
Expand Down Expand Up @@ -204,14 +221,14 @@ func extractMetrics(configName, processName string, ifaces []string, output stri
UpdateInterfaceRoleMetrics(processName, ifaces[portId-1], role)
if role == SLAVE {
r := []rune(ifaces[portId-1])
masterOffsetIfaceName[configName] = string(r[:len(r)-1]) + "x"
slaveIfaceName[configName] = ifaces[portId-1]
masterOffsetIface.set(configName, string(r[:len(r)-1])+"x")
slaveIface.set(configName, ifaces[portId-1])
} else if role == FAULTY {
if isSlaveFaulty(configName, ifaces[portId-1]) {
updatePTPMetrics(master, processName, getMasterOffsetIfaceName(configName), faultyOffset, faultyOffset, 0, 0)
if slaveIface.isFaulty(configName, ifaces[portId-1]) {
updatePTPMetrics(master, processName, masterOffsetIface.get(configName), faultyOffset, faultyOffset, 0, 0)
updatePTPMetrics(phc, phcProcessName, clockRealTime, faultyOffset, faultyOffset, 0, 0)
masterOffsetIfaceName[configName] = ""
slaveIfaceName[configName] = ""
masterOffsetIface.set(configName, "")
slaveIface.set(configName, "")
}
}
}
Expand Down Expand Up @@ -253,8 +270,8 @@ func extractSummaryMetrics(configName, processName, output string) (iface string
fields = append(fields, "") // Making space for the new element
// 0 1 2
//ptp4l.0.config rms 53 max 74 freq -16642 +/- 40 delay 1089 +/- 20
copy(fields[2:], fields[1:]) // Shifting elements
fields[1] = getMasterOffsetIfaceName(configName) // Copying/inserting the value
copy(fields[2:], fields[1:]) // Shifting elements
fields[1] = masterOffsetIface.get(configName) // Copying/inserting the value
// 0 0 1 2
//ptp4l.0.config master rms 53 max 74 freq -16642 +/- 40 delay 1089 +/- 20
} else if fields[1] != "CLOCK_REALTIME" {
Expand Down Expand Up @@ -329,7 +346,7 @@ func extractRegularMetrics(configName, processName, output string) (err error, i

// replace master offset from master to slaveInterface - index + x ens01== ens0X
if iface == master {
iface = getMasterOffsetIfaceName(configName)
iface = masterOffsetIface.get(configName)
}

ptpOffset, e := strconv.ParseFloat(fields[3], 64)
Expand Down Expand Up @@ -497,16 +514,33 @@ func StartMetricsServer(bindAddress string) {
}, 5*time.Second, utilwait.NeverStop)
}

func getMasterOffsetIfaceName(configName string) string {
if s, found := masterOffsetIfaceName[configName]; found {
func (m *masterOffsetInterface) get(configName string) string {
m.RLock()
defer m.RUnlock()
if s, found := m.name[configName]; found {
return s
}
return ""
}

func isSlaveFaulty(configName string, iface string) bool {
if s, found := slaveIfaceName[configName]; found {
if s == iface {
func (m *masterOffsetInterface) set(configName string, value string) {
m.Lock()
defer m.Unlock()
m.name[configName] = value
}

func (s *slaveInterface) set(configName string, value string) {
s.Lock()
defer s.Unlock()
s.name[configName] = value
}

func (s *slaveInterface) isFaulty(configName string, iface string) bool {
s.RLock()
defer s.RUnlock()

if si, found := s.name[configName]; found {
if si == iface {
return true
}
}
Expand Down

0 comments on commit b3896c2

Please sign in to comment.