Skip to content

Commit

Permalink
reduce packetloss cache size
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwlqx committed Apr 10, 2024
1 parent c7b6aec commit 44eb55c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 17 additions & 4 deletions pkg/exporter/probe/tracepacketloss/packetloss.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var (

func init() {
var err error
_packetLossProbe.cache, err = lru.New[probe.Tuple, *Counter](102400)
_packetLossProbe.cache, err = lru.New[probe.Tuple, *Counter](10240)
if err != nil {
panic(fmt.Sprintf("cannot create lru cache for packetloss probe:%v", err))
}
Expand Down Expand Up @@ -122,10 +122,17 @@ func (p *metricsProbe) collectOnce(emit probe.Emit) error {
continue
}

if time.Now().UnixNano()-counter.lastUpdate > 60*time.Second.Nanoseconds() && counter.snatched {
_packetLossProbe.cache.Remove(tuple)
counter.Total = 0
counter.Netfilter = 0
}

labels := probe.BuildTupleMetricsLabels(&tuple)
labels = append(labels, nettop.GetNodeName())
emit(packetLossTotal, labels, float64(counter.Total))
emit(packetLossNetfilter, labels, float64(counter.Netfilter))
counter.snatched = true
}
return nil
}
Expand Down Expand Up @@ -153,8 +160,10 @@ func (e *eventProbe) Stop(_ context.Context) error {
}

type Counter struct {
Total uint32
Netfilter uint32
lastUpdate int64
snatched bool
Total uint32
Netfilter uint32
}

type probeConfig struct {
Expand Down Expand Up @@ -389,8 +398,12 @@ func (p *packetLossProbe) perfLoop() {
v.Total++
p.countByLocation(event.Location, v)

now := time.Now().UnixNano()
v.lastUpdate = now
v.snatched = false

evt := &probe.Event{
Timestamp: time.Now().UnixNano(),
Timestamp: now,
Type: PacketLoss,
Labels: probe.BuildTupleEventLabels(tuple),
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/exporter/probe/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package probe

type Tuple struct {
Protocol uint8
Src string
Dst string
Sport uint16
Dport uint16
Src string
Dst string
}

0 comments on commit 44eb55c

Please sign in to comment.