From 1828aede84d1121d2bdda4128e03cc80951388f5 Mon Sep 17 00:00:00 2001 From: remi Date: Thu, 20 Oct 2022 18:31:06 +0200 Subject: [PATCH 1/3] handle more column from softnet_data * add CPUCollision for linux 2.6 * add ReceivedRps and FlowLimitCount for linux >4.17 * add SoftnetBacklogLen and Index for linux 5.10 https://github.com/torvalds/linux/commit/7d58e6555870d01d85b181ba2a16b217a1ea8bdd * add Width so anyone can choose to use Index instead of line index, see * the commit https://github.com/torvalds/linux/commit/7d58e6555870d01d85b181ba2a16b217a1ea8bdd Signed-off-by: remi --- net_softnet.go | 76 +++++++++++++++++++++++++++++++++++++++------ net_softnet_test.go | 43 +++++++++++++++++++++---- 2 files changed, 103 insertions(+), 16 deletions(-) diff --git a/net_softnet.go b/net_softnet.go index a94f86dc4..b1b31d0db 100644 --- a/net_softnet.go +++ b/net_softnet.go @@ -28,7 +28,9 @@ import ( // See: // * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343 // * Linux 4.17 https://elixir.bootlin.com/linux/v4.17/source/net/core/net-procfs.c#L162 -// and https://elixir.bootlin.com/linux/v4.17/source/include/linux/netdevice.h#L2810. +// * Linux 5.10 https://elixir.bootlin.com/linux/v5.10/source/net/core/net-procfs.c#L172 +// * https://elixir.bootlin.com/linux/v4.17/source/include/linux/netdevice.h#L2810 +// * https://elixir.bootlin.com/linux/v5.10/source/include/linux/netdevice.h#L3181 // SoftnetStat contains a single row of data from /proc/net/softnet_stat. type SoftnetStat struct { @@ -38,6 +40,18 @@ type SoftnetStat struct { Dropped uint32 // Number of times processing packets ran out of quota. TimeSqueezed uint32 + // Number of collision occur while obtaining device lock while transmitting. + CPUCollision uint32 + // Number of times cpu woken up received_rps. + ReceivedRps uint32 + // number of times flow limit has been reached. + FlowLimitCount uint32 + // Softnet Backlog status. + SoftnetBacklogLen uint32 + // CPU id owning this softnet_data. + Index uint32 + // softnet_data's Width + Width int } var softNetProcFile = "net/softnet_stat" @@ -71,17 +85,59 @@ func parseSoftnet(r io.Reader) ([]SoftnetStat, error) { return nil, fmt.Errorf("%d columns were detected, but at least %d were expected", width, minColumns) } - // We only parse the first three columns at the moment. - us, err := parseHexUint32s(columns[0:3]) - if err != nil { - return nil, err + // * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343 + if width == minColumns { + us, err := parseHexUint32s(columns[0:9]) + if err != nil { + return nil, err + } + + stats = append(stats, SoftnetStat{ + Processed: us[0], + Dropped: us[1], + TimeSqueezed: us[2], + CPUCollision: us[8], + Width: width, + }) } - stats = append(stats, SoftnetStat{ - Processed: us[0], - Dropped: us[1], - TimeSqueezed: us[2], - }) + // * Linux 4.17 https://elixir.bootlin.com/linux/v4.17/source/net/core/net-procfs.c#L162 + if width == 11 { + us, err := parseHexUint32s(columns[0:11]) + if err != nil { + return nil, err + } + + stats = append(stats, SoftnetStat{ + Processed: us[0], + Dropped: us[1], + TimeSqueezed: us[2], + CPUCollision: us[8], + ReceivedRps: us[9], + FlowLimitCount: us[10], + Width: width, + }) + } + + // * Linux 5.10 https://elixir.bootlin.com/linux/v5.10/source/net/core/net-procfs.c#L172 + if width >= 13 { + us, err := parseHexUint32s(columns[0:13]) + if err != nil { + return nil, err + } + + stats = append(stats, SoftnetStat{ + Processed: us[0], + Dropped: us[1], + TimeSqueezed: us[2], + CPUCollision: us[8], + ReceivedRps: us[9], + FlowLimitCount: us[10], + SoftnetBacklogLen: us[11], + Index: us[12], + Width: width, + }) + } } return stats, nil diff --git a/net_softnet_test.go b/net_softnet_test.go index f25e09f34..ca966fabd 100644 --- a/net_softnet_test.go +++ b/net_softnet_test.go @@ -25,15 +25,46 @@ func TestNetSoftnet(t *testing.T) { t.Fatal(err) } - want := []SoftnetStat{{ - Processed: 0x00015c73, - Dropped: 0x00020e76, - TimeSqueezed: 0xf0000769, - }, + want := []SoftnetStat{ + { + Processed: 0x0446fcea, + Dropped: 0x0012e8f8, + TimeSqueezed: 0x00000000, + CPUCollision: 0x00000000, + ReceivedRps: 0x015f20c8, + FlowLimitCount: 0x00000000, + SoftnetBacklogLen: 0x00000000, + Index: 0x00000033, + Width: 13, + }, + { + Processed: 0x00031f27, + Dropped: 0x00020e76, + TimeSqueezed: 0x00000000, + CPUCollision: 0x00000000, + ReceivedRps: 0x00020e76, + FlowLimitCount: 0x00020e76, + SoftnetBacklogLen: 0x00000000, + Index: 0x00000001, + Width: 13, + }, + { + Processed: 0x00015c73, + Dropped: 0x00020e76, + TimeSqueezed: 0xf0000769, + CPUCollision: 0x00000000, + ReceivedRps: 0x00020e76, + FlowLimitCount: 0x00000000, + Width: 11, + }, { Processed: 0x01663fb2, + Dropped: 0x00000000, TimeSqueezed: 0x0109a4, - }} + CPUCollision: 0x00020e76, + Width: 9, + }, + } got, err := fs.NetSoftnetStat() if err != nil { From 9fa034a7b79105d942c50bcf3d4234a05aa1645b Mon Sep 17 00:00:00 2001 From: remi Date: Fri, 4 Nov 2022 18:43:42 +0100 Subject: [PATCH 2/3] following the merge of https://github.com/RedHatInsights/insights-core/pull/3561, adding support for 9,10,11 and 13 columns, unitest has also more case Signed-off-by: remi --- net_softnet.go | 72 +++++++++++++++++++----------------------- net_softnet_test.go | 36 +++++++++++++-------- testdata/fixtures.ttar | 9 ++++-- 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/net_softnet.go b/net_softnet.go index b1b31d0db..06b7b8f21 100644 --- a/net_softnet.go +++ b/net_softnet.go @@ -27,10 +27,9 @@ import ( // For the proc file format details, // See: // * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343 -// * Linux 4.17 https://elixir.bootlin.com/linux/v4.17/source/net/core/net-procfs.c#L162 -// * Linux 5.10 https://elixir.bootlin.com/linux/v5.10/source/net/core/net-procfs.c#L172 -// * https://elixir.bootlin.com/linux/v4.17/source/include/linux/netdevice.h#L2810 -// * https://elixir.bootlin.com/linux/v5.10/source/include/linux/netdevice.h#L3181 +// * Linux 2.6.39 https://elixir.bootlin.com/linux/v2.6.39/source/net/core/dev.c#L4086 +// * Linux 4.18 https://elixir.bootlin.com/linux/v4.18/source/net/core/net-procfs.c#L162 +// * Linux 5.14 https://elixir.bootlin.com/linux/v5.14/source/net/core/net-procfs.c#L169 // SoftnetStat contains a single row of data from /proc/net/softnet_stat. type SoftnetStat struct { @@ -46,11 +45,11 @@ type SoftnetStat struct { ReceivedRps uint32 // number of times flow limit has been reached. FlowLimitCount uint32 - // Softnet Backlog status. + // Softnet backlog status. SoftnetBacklogLen uint32 // CPU id owning this softnet_data. Index uint32 - // softnet_data's Width + // softnet_data's Width. Width int } @@ -80,64 +79,57 @@ func parseSoftnet(r io.Reader) ([]SoftnetStat, error) { for s.Scan() { columns := strings.Fields(s.Text()) width := len(columns) + softnetStat := SoftnetStat{} if width < minColumns { return nil, fmt.Errorf("%d columns were detected, but at least %d were expected", width, minColumns) } - // * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343 - if width == minColumns { + // Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2347 + if width >= minColumns { us, err := parseHexUint32s(columns[0:9]) if err != nil { return nil, err } - stats = append(stats, SoftnetStat{ - Processed: us[0], - Dropped: us[1], - TimeSqueezed: us[2], - CPUCollision: us[8], - Width: width, - }) + softnetStat.Processed = us[0] + softnetStat.Dropped = us[1] + softnetStat.TimeSqueezed = us[2] + softnetStat.CPUCollision = us[8] } - // * Linux 4.17 https://elixir.bootlin.com/linux/v4.17/source/net/core/net-procfs.c#L162 - if width == 11 { - us, err := parseHexUint32s(columns[0:11]) + // Linux 2.6.39 https://elixir.bootlin.com/linux/v2.6.39/source/net/core/dev.c#L4086 + if width >= 10 { + us, err := parseHexUint32s(columns[9:10]) if err != nil { return nil, err } - stats = append(stats, SoftnetStat{ - Processed: us[0], - Dropped: us[1], - TimeSqueezed: us[2], - CPUCollision: us[8], - ReceivedRps: us[9], - FlowLimitCount: us[10], - Width: width, - }) + softnetStat.ReceivedRps = us[0] } - // * Linux 5.10 https://elixir.bootlin.com/linux/v5.10/source/net/core/net-procfs.c#L172 + // Linux 4.18 https://elixir.bootlin.com/linux/v4.18/source/net/core/net-procfs.c#L162 + if width >= 11 { + us, err := parseHexUint32s(columns[10:11]) + if err != nil { + return nil, err + } + + softnetStat.FlowLimitCount = us[0] + } + + // Linux 5.14 https://elixir.bootlin.com/linux/v5.14/source/net/core/net-procfs.c#L169 if width >= 13 { - us, err := parseHexUint32s(columns[0:13]) + us, err := parseHexUint32s(columns[11:13]) if err != nil { return nil, err } - stats = append(stats, SoftnetStat{ - Processed: us[0], - Dropped: us[1], - TimeSqueezed: us[2], - CPUCollision: us[8], - ReceivedRps: us[9], - FlowLimitCount: us[10], - SoftnetBacklogLen: us[11], - Index: us[12], - Width: width, - }) + softnetStat.SoftnetBacklogLen = us[0] + softnetStat.Index = us[1] } + softnetStat.Width = width + stats = append(stats, softnetStat) } return stats, nil diff --git a/net_softnet_test.go b/net_softnet_test.go index ca966fabd..deedc1ba9 100644 --- a/net_softnet_test.go +++ b/net_softnet_test.go @@ -27,24 +27,24 @@ func TestNetSoftnet(t *testing.T) { want := []SoftnetStat{ { - Processed: 0x0446fcea, - Dropped: 0x0012e8f8, + Processed: 0x00358fe3, + Dropped: 0x00006283, TimeSqueezed: 0x00000000, CPUCollision: 0x00000000, - ReceivedRps: 0x015f20c8, - FlowLimitCount: 0x00000000, + ReceivedRps: 0x000855fc, + FlowLimitCount: 0x00000076, SoftnetBacklogLen: 0x00000000, - Index: 0x00000033, + Index: 0x00000000, Width: 13, }, { - Processed: 0x00031f27, - Dropped: 0x00020e76, - TimeSqueezed: 0x00000000, + Processed: 0x00953d1a, + Dropped: 0x00000446, + TimeSqueezed: 0x000000b1, CPUCollision: 0x00000000, - ReceivedRps: 0x00020e76, - FlowLimitCount: 0x00020e76, - SoftnetBacklogLen: 0x00000000, + ReceivedRps: 0x008eeb9a, + FlowLimitCount: 0x0000002b, + SoftnetBacklogLen: 0x000000dc, Index: 0x00000001, Width: 13, }, @@ -52,9 +52,9 @@ func TestNetSoftnet(t *testing.T) { Processed: 0x00015c73, Dropped: 0x00020e76, TimeSqueezed: 0xf0000769, - CPUCollision: 0x00000000, - ReceivedRps: 0x00020e76, - FlowLimitCount: 0x00000000, + CPUCollision: 0x00000004, + ReceivedRps: 0x00000003, + FlowLimitCount: 0x00000002, Width: 11, }, { @@ -64,6 +64,14 @@ func TestNetSoftnet(t *testing.T) { CPUCollision: 0x00020e76, Width: 9, }, + { + Processed: 0x00008e78, + Dropped: 0x00000001, + TimeSqueezed: 0x00000011, + CPUCollision: 0x00000020, + ReceivedRps: 0x00000010, + Width: 10, + }, } got, err := fs.NetSoftnetStat() diff --git a/testdata/fixtures.ttar b/testdata/fixtures.ttar index 8400ec765..2a1bb793d 100644 --- a/testdata/fixtures.ttar +++ b/testdata/fixtures.ttar @@ -2378,9 +2378,12 @@ FRAG6: inuse 0 memory 0 Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/proc/net/softnet_stat -Lines: 2 -00015c73 00020e76 F0000769 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 -01663fb2 00000000 000109a4 00000000 00000000 00000000 00000000 00000000 00000000 +Lines: 5 +00358fe3 00006283 00000000 00000000 00000000 00000000 00000000 00000000 00000000 000855fc 00000076 00000000 00000000 +00953d1a 00000446 000000b1 00000000 00000000 00000000 00000000 00000000 00000000 008eeb9a 0000002b 000000dc 00000001 +00015c73 00020e76 f0000769 00000000 00000000 00000000 00000000 00000000 00000004 00000003 00000002 +01663fb2 00000000 000109a4 00000000 00000000 00000000 00000000 00000000 00020e76 +00008e78 00000001 00000011 00000000 00000000 00000000 00000000 00000000 00000020 00000010 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: fixtures/proc/net/softnet_stat.broken From 4d1f937a7b028f867a015260cf52816eb993c285 Mon Sep 17 00:00:00 2001 From: remi Date: Fri, 4 Nov 2022 18:50:33 +0100 Subject: [PATCH 3/3] go fmt on net_softnet_test.go Signed-off-by: remi --- net_softnet_test.go | 50 ++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/net_softnet_test.go b/net_softnet_test.go index deedc1ba9..1620c86b4 100644 --- a/net_softnet_test.go +++ b/net_softnet_test.go @@ -27,50 +27,50 @@ func TestNetSoftnet(t *testing.T) { want := []SoftnetStat{ { - Processed: 0x00358fe3, - Dropped: 0x00006283, - TimeSqueezed: 0x00000000, - CPUCollision: 0x00000000, - ReceivedRps: 0x000855fc, - FlowLimitCount: 0x00000076, + Processed: 0x00358fe3, + Dropped: 0x00006283, + TimeSqueezed: 0x00000000, + CPUCollision: 0x00000000, + ReceivedRps: 0x000855fc, + FlowLimitCount: 0x00000076, SoftnetBacklogLen: 0x00000000, - Index: 0x00000000, - Width: 13, + Index: 0x00000000, + Width: 13, }, { - Processed: 0x00953d1a, - Dropped: 0x00000446, - TimeSqueezed: 0x000000b1, - CPUCollision: 0x00000000, - ReceivedRps: 0x008eeb9a, - FlowLimitCount: 0x0000002b, + Processed: 0x00953d1a, + Dropped: 0x00000446, + TimeSqueezed: 0x000000b1, + CPUCollision: 0x00000000, + ReceivedRps: 0x008eeb9a, + FlowLimitCount: 0x0000002b, SoftnetBacklogLen: 0x000000dc, - Index: 0x00000001, - Width: 13, + Index: 0x00000001, + Width: 13, }, { - Processed: 0x00015c73, - Dropped: 0x00020e76, - TimeSqueezed: 0xf0000769, - CPUCollision: 0x00000004, - ReceivedRps: 0x00000003, + Processed: 0x00015c73, + Dropped: 0x00020e76, + TimeSqueezed: 0xf0000769, + CPUCollision: 0x00000004, + ReceivedRps: 0x00000003, FlowLimitCount: 0x00000002, - Width: 11, + Width: 11, }, { Processed: 0x01663fb2, Dropped: 0x00000000, TimeSqueezed: 0x0109a4, CPUCollision: 0x00020e76, - Width: 9, + Width: 9, }, { Processed: 0x00008e78, Dropped: 0x00000001, TimeSqueezed: 0x00000011, CPUCollision: 0x00000020, - ReceivedRps: 0x00000010, - Width: 10, + ReceivedRps: 0x00000010, + Width: 10, }, }