|
1 | | -package analytics |
2 | | - |
3 | | -import ( |
4 | | - "log" |
5 | | - "time" |
6 | | - |
7 | | - xdp "bedrock-xdp/xdp_utils" |
8 | | - |
9 | | - "github.com/cilium/ebpf" |
10 | | -) |
11 | | - |
12 | | -var ( |
13 | | - udpDropBPS *ebpf.Map // udp_drop_bps |
14 | | - otherDropBPS *ebpf.Map // other_drop_bps |
15 | | -) |
16 | | - |
17 | | -//Counter reset logic and Map Lookups should probably be reworked but what do you expect, this is open source. |
18 | | - |
19 | | -func StartDroppedBPS(Collection *ebpf.Collection) { |
20 | | - udpDropBPS = xdp.GetMap("udp_drop_bps", Collection) |
21 | | - otherDropBPS = xdp.GetMap("other_drop_bps", Collection) |
22 | | - |
23 | | - ticker := time.NewTicker(1 * time.Second) |
24 | | - for range ticker.C { |
25 | | - resetDropBitCount() |
26 | | - } |
27 | | -} |
28 | | - |
29 | | -func resetDropBitCount() { |
30 | | - var resetCount uint64 = 0 |
31 | | - var totalKey uint32 = 0 |
32 | | - |
33 | | - if udpDropBPS != nil { |
34 | | - udpDropBPS.Update(&totalKey, &resetCount, ebpf.UpdateAny) |
35 | | - } |
36 | | - if otherDropBPS != nil { |
37 | | - otherDropBPS.Update(&totalKey, &resetCount, ebpf.UpdateAny) |
38 | | - } |
39 | | - |
40 | | - resetByteCountMap(udpDropBPS) |
41 | | - resetByteCountMap(otherDropBPS) |
42 | | -} |
43 | | - |
44 | | -func resetByteCountMap(byteCountMap *ebpf.Map) { |
45 | | - if byteCountMap == nil { |
46 | | - return |
47 | | - } |
48 | | - var key uint32 |
49 | | - var resetCount uint64 = 0 |
50 | | - |
51 | | - iter := byteCountMap.Iterate() |
52 | | - for iter.Next(&key, &resetCount) { |
53 | | - byteCountMap.Update(&key, &resetCount, ebpf.UpdateAny) |
54 | | - } |
55 | | - |
56 | | - if err := iter.Err(); err != nil { |
57 | | - log.Printf("Error resetting byte count map: %s\n", err) |
58 | | - } |
59 | | -} |
60 | | - |
61 | | -func GetTotalDroppedBPS(check string) uint64 { |
62 | | - switch check { |
63 | | - case "udp": |
64 | | - return getMapTotalCount(udpDropBPS) |
65 | | - case "other": |
66 | | - return getMapTotalCount(otherDropBPS) |
67 | | - default: |
68 | | - return getMapTotalCount(udpDropBPS) + getMapTotalCount(otherDropBPS) |
69 | | - } |
70 | | -} |
| 1 | +package analytics |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + xdp "bedrock-xdp/xdp_utils" |
| 7 | + |
| 8 | + "github.com/cilium/ebpf" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + udpDropBPS *ebpf.Map // udp_drop_bps |
| 13 | + otherDropBPS *ebpf.Map // other_drop_bps |
| 14 | +) |
| 15 | + |
| 16 | +//Counter reset logic and Map Lookups should probably be reworked but what do you expect, this is open source. |
| 17 | + |
| 18 | +func StartDroppedBPS(Collection *ebpf.Collection) { |
| 19 | + udpDropBPS = xdp.GetMap("udp_drop_bps", Collection) |
| 20 | + otherDropBPS = xdp.GetMap("other_drop_bps", Collection) |
| 21 | +} |
| 22 | + |
| 23 | +// ResetDropBPS clears dropped bit counters. |
| 24 | +func ResetDropBPS() { |
| 25 | + var resetCount uint64 = 0 |
| 26 | + var totalKey uint32 = 0 |
| 27 | + |
| 28 | + if udpDropBPS != nil { |
| 29 | + udpDropBPS.Update(&totalKey, &resetCount, ebpf.UpdateAny) |
| 30 | + } |
| 31 | + if otherDropBPS != nil { |
| 32 | + otherDropBPS.Update(&totalKey, &resetCount, ebpf.UpdateAny) |
| 33 | + } |
| 34 | + |
| 35 | + resetByteCountMap(udpDropBPS) |
| 36 | + resetByteCountMap(otherDropBPS) |
| 37 | +} |
| 38 | + |
| 39 | +func resetByteCountMap(byteCountMap *ebpf.Map) { |
| 40 | + if byteCountMap == nil { |
| 41 | + return |
| 42 | + } |
| 43 | + var key uint32 |
| 44 | + var resetCount uint64 = 0 |
| 45 | + |
| 46 | + iter := byteCountMap.Iterate() |
| 47 | + for iter.Next(&key, &resetCount) { |
| 48 | + byteCountMap.Update(&key, &resetCount, ebpf.UpdateAny) |
| 49 | + } |
| 50 | + |
| 51 | + if err := iter.Err(); err != nil { |
| 52 | + log.Printf("Error resetting byte count map: %s\n", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func GetTotalDroppedBPS(check string) uint64 { |
| 57 | + switch check { |
| 58 | + case "udp": |
| 59 | + return getMapTotalCount(udpDropBPS) / uint64(StatIntervalSec) |
| 60 | + case "other": |
| 61 | + return getMapTotalCount(otherDropBPS) / uint64(StatIntervalSec) |
| 62 | + default: |
| 63 | + return (getMapTotalCount(udpDropBPS) + getMapTotalCount(otherDropBPS)) / uint64(StatIntervalSec) |
| 64 | + } |
| 65 | +} |
0 commit comments