From 229f52203976e95dc80e4ee202f1778f37f3208f Mon Sep 17 00:00:00 2001 From: Vladimir Lavor Date: Fri, 16 Feb 2024 15:12:27 +0100 Subject: [PATCH] decrease the size of the trace prealocation Signed-off-by: Vladimir Lavor --- core/request_handler.go | 41 +++++++++++++++++++++++++---------------- core/trace.go | 13 +++++-------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/core/request_handler.go b/core/request_handler.go index a4956564..fcea561e 100644 --- a/core/request_handler.go +++ b/core/request_handler.go @@ -111,11 +111,14 @@ func (c *Connection) processRequest(ch *Channel, req *vppRequest) error { // send the request to VPP if err = func() (err error) { - r := &api.Record{Message: req.msg, Timestamp: time.Now(), ChannelID: ch.id} - registered := c.trace.newRegisteredRecord(r) - defer c.trace.send(registered) - if err = c.vppClient.SendMsg(context, data); err == nil { - r.Succeeded = true + timestamp := c.trace.registerNew() + if err = c.vppClient.SendMsg(context, data); c.trace != nil { + c.trace.send(&api.Record{ + Message: req.msg, + Timestamp: timestamp, + ChannelID: ch.id, + Succeeded: err == nil, + }) } return }(); err != nil { @@ -150,11 +153,14 @@ func (c *Connection) processRequest(ch *Channel, req *vppRequest) error { } // send the control ping request to VPP if err = func() (err error) { - r := &api.Record{Message: c.msgControlPing, Timestamp: time.Now(), ChannelID: ch.id} - registered := c.trace.newRegisteredRecord(r) - defer c.trace.send(registered) - if err = c.vppClient.SendMsg(context, pingData); err == nil { - r.Succeeded = true + timestamp := c.trace.registerNew() + if err = c.vppClient.SendMsg(context, pingData); c.trace != nil { + c.trace.send(&api.Record{ + Message: c.msgControlPing, + Timestamp: timestamp, + ChannelID: ch.id, + Succeeded: err == nil, + }) } return }(); err != nil { @@ -200,12 +206,15 @@ func (c *Connection) msgCallback(msgID uint16, data []byte) { // decode and trace the message msg = reflect.New(reflect.TypeOf(msg).Elem()).Interface().(api.Message) if err = func() (err error) { - r := &api.Record{Timestamp: time.Now(), IsReceived: true, ChannelID: chanID} - registered := c.trace.newRegisteredRecord(r) - defer c.trace.send(registered) - if err = c.codec.DecodeMsg(data, msg); err == nil { - r.Message = msg - r.Succeeded = true + timestamp := c.trace.registerNew() + if err = c.codec.DecodeMsg(data, msg); c.trace != nil { + c.trace.send(&api.Record{ + Message: msg, + Timestamp: timestamp, + IsReceived: true, + ChannelID: chanID, + Succeeded: err == nil, + }) } return }(); err != nil { diff --git a/core/trace.go b/core/trace.go index a2a8cb3c..73825f05 100644 --- a/core/trace.go +++ b/core/trace.go @@ -18,6 +18,7 @@ import ( "go.fd.io/govpp/api" "sort" "sync" + "time" ) // default buffer size @@ -103,19 +104,15 @@ func (t *Trace) Close() { t.closeFunc() } -type registeredRecord struct { - r *api.Record -} - -func (t *Trace) newRegisteredRecord(rc *api.Record) *registeredRecord { +func (t *Trace) registerNew() time.Time { if t != nil { t.wg.Add(1) } - return ®isteredRecord{r: rc} + return time.Now() } -func (t *Trace) send(record *registeredRecord) { +func (t *Trace) send(record *api.Record) { if t != nil { - t.buffer <- record.r + t.buffer <- record } }