From 11dd7a065d8a6b4e0304254583f936fd3d244441 Mon Sep 17 00:00:00 2001 From: Ralph Tice Date: Fri, 9 Jun 2023 13:42:10 -0700 Subject: [PATCH 1/3] reorder struct fields for compactness using https://github.com/dkorunic/betteralign had to duplicate flow/flow.go to tmp.go to get the tool to pick up the structs: /Users/rtice/src/gopath/libkflow/tmp.go:3:11: struct of size 520 could be 512 /Users/rtice/src/gopath/libkflow/tmp.go:73:13: struct of size 104 could be 88 /Users/rtice/src/gopath/libkflow/config.go:19:13: struct with 168 pointer bytes could be 136 /Users/rtice/src/gopath/libkflow/send.go:21:13: struct with 104 pointer bytes could be 72 --- config.go | 18 ++++----- flow/flow.go | 110 +++++++++++++++++++++++++-------------------------- send.go | 6 +-- 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/config.go b/config.go index 98329c3..a23944c 100644 --- a/config.go +++ b/config.go @@ -17,21 +17,21 @@ import ( // Config describes the libkflow configuration. type Config struct { - email string - token string - capture Capture + logger interface{} + metrics *url.URL + flow *url.URL proxy *url.URL api *url.URL - flow *url.URL - metrics *url.URL - sample int - timeout time.Duration - retries int - logger interface{} program string + email string + token string version string metricsPrefix string + capture Capture + sample int + timeout time.Duration + retries int metricsInterval time.Duration } diff --git a/flow/flow.go b/flow/flow.go index 79f6601..8301c08 100644 --- a/flow/flow.go +++ b/flow/flow.go @@ -17,73 +17,73 @@ type Ckflow C.kflow const MAX_CUSTOM_STR_LEN = 384 type Flow struct { - TimestampNano int64 - DstAs uint32 - DstGeo uint32 - DstMac uint32 // IGNORED - use DstEthMac - HeaderLen uint32 + SrcBgpCommunity string + SrcFlowTags string + DstFlowTags string + FlowTags string + DstBgpAsPath string + DstBgpCommunity string + SrcBgpAsPath string + Ipv6SrcAddr []byte + Ipv6SrcRoutePrefix []byte + Customs []Custom + Ipv6DstRoutePrefix []byte + Ipv6DstAddr []byte + Ipv6SrcNextHop []byte + Ipv6DstNextHop []byte InBytes uint64 InPkts uint64 - InputPort uint32 - IpSize uint32 - Ipv4DstAddr uint32 - Ipv4SrcAddr uint32 - L4DstPort uint32 - L4SrcPort uint32 - OutputPort uint32 - Protocol uint32 - SampledPacketSize uint32 - SrcAs uint32 - SrcGeo uint32 - SrcMac uint32 // IGNORED - use SrcEthMac - TcpFlags uint32 - Tos uint32 - VlanIn uint32 - VlanOut uint32 - Ipv4NextHop uint32 - MplsType uint32 + Timestamp int64 + DstEthMac uint64 + TimestampNano int64 OutBytes uint64 + SrcEthMac uint64 OutPkts uint64 + SampleRate uint32 + DstThirdAsn uint32 + MplsType uint32 + VlanOut uint32 + VlanIn uint32 TcpRetransmit uint32 AppProtocol uint32 - SrcFlowTags string - DstFlowTags string - SampleRate uint32 + Tos uint32 + TcpFlags uint32 + SrcMac uint32 // IGNORED - use SrcEthMac DeviceId uint32 - FlowTags string - Timestamp int64 - DstBgpAsPath string - DstBgpCommunity string - SrcBgpAsPath string - SrcBgpCommunity string + SrcGeo uint32 + SrcAs uint32 + SampledPacketSize uint32 + Protocol uint32 + OutputPort uint32 + L4SrcPort uint32 SrcNextHopAs uint32 DstNextHopAs uint32 SrcGeoRegion uint32 DstGeoRegion uint32 SrcGeoCity uint32 DstGeoCity uint32 - Big bool - SampleAdj bool + DstAs uint32 + DstGeo uint32 Ipv4DstNextHop uint32 Ipv4SrcNextHop uint32 SrcRoutePrefix uint32 DstRoutePrefix uint32 - SrcRouteLength uint8 - DstRouteLength uint8 + DstMac uint32 // IGNORED - use DstEthMac + HeaderLen uint32 SrcSecondAsn uint32 DstSecondAsn uint32 SrcThirdAsn uint32 - DstThirdAsn uint32 - Ipv6DstAddr []byte - Ipv6SrcAddr []byte - SrcEthMac uint64 - DstEthMac uint64 - Ipv6SrcNextHop []byte - Ipv6DstNextHop []byte - Ipv6SrcRoutePrefix []byte - Ipv6DstRoutePrefix []byte + Ipv4NextHop uint32 + L4DstPort uint32 + Ipv4SrcAddr uint32 + Ipv4DstAddr uint32 + IpSize uint32 + InputPort uint32 + DstRouteLength uint8 + SrcRouteLength uint8 + SampleAdj bool IsMetric bool - Customs []Custom + Big bool } type Type int @@ -104,20 +104,20 @@ const ( ) type Custom struct { - ID uint32 - Type Type Str string - U8 byte - U16 uint16 - U32 uint32 U64 uint64 - I8 int8 - I16 int16 - I32 int32 + Type Type + F64 float64 I64 int64 + I32 int32 + U32 uint32 + ID uint32 F32 float32 - F64 float64 + I16 int16 + U16 uint16 Addr [17]byte + I8 int8 + U8 byte } func New(cflow *Ckflow) Flow { diff --git a/send.go b/send.go index 2f3a519..b0f464d 100644 --- a/send.go +++ b/send.go @@ -22,15 +22,15 @@ type Sender struct { agg *agg.Agg exit chan struct{} url *url.URL - timeout time.Duration client *api.Client - sample int ticker *time.Ticker - workers sync.WaitGroup dns chan []byte Device *api.Device Errors chan<- error Metrics *metrics.Metrics + workers sync.WaitGroup + timeout time.Duration + sample int } func newSender(url *url.URL, timeout time.Duration) *Sender { From 312f9767dd4288a1520d11d5fd9a2c6cedb218dd Mon Sep 17 00:00:00 2001 From: Ralph Tice Date: Fri, 9 Jun 2023 14:28:02 -0700 Subject: [PATCH 2/3] revert chanegs to send and config --- config.go | 18 +++++++++--------- send.go | 6 +++--- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index a23944c..98329c3 100644 --- a/config.go +++ b/config.go @@ -17,21 +17,21 @@ import ( // Config describes the libkflow configuration. type Config struct { - logger interface{} - metrics *url.URL - flow *url.URL + email string + token string + capture Capture proxy *url.URL api *url.URL + flow *url.URL + metrics *url.URL + sample int + timeout time.Duration + retries int + logger interface{} program string - email string - token string version string metricsPrefix string - capture Capture - sample int - timeout time.Duration - retries int metricsInterval time.Duration } diff --git a/send.go b/send.go index b0f464d..2f3a519 100644 --- a/send.go +++ b/send.go @@ -22,15 +22,15 @@ type Sender struct { agg *agg.Agg exit chan struct{} url *url.URL + timeout time.Duration client *api.Client + sample int ticker *time.Ticker + workers sync.WaitGroup dns chan []byte Device *api.Device Errors chan<- error Metrics *metrics.Metrics - workers sync.WaitGroup - timeout time.Duration - sample int } func newSender(url *url.URL, timeout time.Duration) *Sender { From 3cea9423a9df254ce5e034349d77184c7fad6ca1 Mon Sep 17 00:00:00 2001 From: Ralph Tice Date: Fri, 9 Jun 2023 14:32:39 -0700 Subject: [PATCH 3/3] add notes for flow and custom about their microoptimized field orders --- flow/flow.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/flow/flow.go b/flow/flow.go index 8301c08..faf1d95 100644 --- a/flow/flow.go +++ b/flow/flow.go @@ -16,6 +16,7 @@ type Ckflow C.kflow const MAX_CUSTOM_STR_LEN = 384 +// Flow has had its fields reordered via https://github.com/dkorunic/betteralign to minimize memory usage type Flow struct { SrcBgpCommunity string SrcFlowTags string @@ -103,6 +104,7 @@ const ( Addr ) +// Custom has had its fields reordered via https://github.com/dkorunic/betteralign to minimize memory usage type Custom struct { Str string U64 uint64