From c3d84d821fcdc15245f7cc9838cf97b4947a48e2 Mon Sep 17 00:00:00 2001
From: yangchen <yangchen145@huawei.com>
Date: Wed, 13 Mar 2024 13:01:38 +0800
Subject: [PATCH] fix LATENCY_WRITE increase in recv process

---
 src/common/dpdk_common.h               |  5 +++++
 src/common/gazelle_dfx_msg.h           |  2 ++
 src/lstack/core/lstack_lwip.c          |  4 ++++
 src/lstack/core/lstack_stack_stat.c    | 20 +++++++++++++++++++-
 src/lstack/include/lstack_stack_stat.h |  1 +
 5 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/common/dpdk_common.h b/src/common/dpdk_common.h
index a79a0f2..cb41747 100644
--- a/src/common/dpdk_common.h
+++ b/src/common/dpdk_common.h
@@ -23,6 +23,8 @@
 
 #define GAZELLE_KNI_NAME                     "kni"   // will be removed during dpdk update
 
+#define GAZELLE_LATENCY_RD      0
+#define GAZELLE_LATENCY_WR      1
 
 /* Layout:
  * | rte_mbuf | mbuf_private | payload |
@@ -31,6 +33,7 @@
 struct latency_timestamp {
         uint64_t stamp; // time stamp
         uint64_t check; // just for later vaild check
+        uint16_t type;  // latency type
 };
 struct mbuf_private {
     /* struct pbuf_custom must at first */
@@ -92,6 +95,7 @@ static __rte_always_inline void time_stamp_into_mbuf(uint32_t rx_count, struct r
         lt = &mbuf_to_private(buf[i])->lt;
         lt->stamp = time_stamp;
         lt->check = ~(time_stamp);
+        lt->type = GAZELLE_LATENCY_RD;
     }
 }
 
@@ -102,6 +106,7 @@ static __rte_always_inline void time_stamp_into_pbuf(uint32_t tx_count, struct p
         lt = &pbuf_to_private(buf[i])->lt;
         lt->stamp = time_stamp;
         lt->check = ~(time_stamp);
+        lt->type = GAZELLE_LATENCY_WR;
     }
 }
 
diff --git a/src/common/gazelle_dfx_msg.h b/src/common/gazelle_dfx_msg.h
index 8d528d6..d7ba80f 100644
--- a/src/common/gazelle_dfx_msg.h
+++ b/src/common/gazelle_dfx_msg.h
@@ -69,6 +69,8 @@ enum GAZELLE_STAT_MODE {
 enum GAZELLE_LATENCY_TYPE {
     GAZELLE_LATENCY_READ_LWIP,
     GAZELLE_LATENCY_READ_LSTACK,
+    GAZELLE_LATENCY_READ_MAX,
+
     GAZELLE_LATENCY_WRITE_LWIP,
     GAZELLE_LATENCY_WRITE_LSTACK,
     GAZELLE_LATENCY_MAX,
diff --git a/src/lstack/core/lstack_lwip.c b/src/lstack/core/lstack_lwip.c
index 8aae433..f9b83c7 100644
--- a/src/lstack/core/lstack_lwip.c
+++ b/src/lstack/core/lstack_lwip.c
@@ -257,6 +257,10 @@ struct pbuf *do_lwip_get_from_sendring(struct lwip_sock *sock, uint16_t remain_s
         int size = (remain_size + MBUF_MAX_DATA_LEN - 1) / MBUF_MAX_DATA_LEN - 1;
         struct pbuf *pbuf_used[size];
         gazelle_ring_sc_dequeue(sock->send_ring, (void **)&pbuf_used, size);
+
+        for (uint32_t i = 0; get_protocol_stack_group()->latency_start && i < size; i++) {
+            calculate_lstack_latency(&sock->stack->latency, pbuf_used[i], GAZELLE_LATENCY_WRITE_LWIP);
+        }
     }
 
     if (get_protocol_stack_group()->latency_start) {
diff --git a/src/lstack/core/lstack_stack_stat.c b/src/lstack/core/lstack_stack_stat.c
index 25b5557..3e016b7 100644
--- a/src/lstack/core/lstack_stack_stat.c
+++ b/src/lstack/core/lstack_stack_stat.c
@@ -48,10 +48,27 @@ uint64_t get_current_time(void)
     return (rte_rdtsc() / g_cycles_per_us);
 }
 
+void time_stamp_transfer_pbuf(struct pbuf *pbuf_old, struct pbuf *pbuf_new)
+{
+    if (!get_protocol_stack_group()->latency_start) {
+        return;
+    }
+    struct latency_timestamp *lt_old;
+    struct latency_timestamp *lt_new;
+
+    lt_old = &pbuf_to_private(pbuf_old)->lt;
+    lt_new = &pbuf_to_private(pbuf_new)->lt;
+
+    lt_new->stamp = lt_old->stamp;
+    lt_new->check = lt_old->check;
+    lt_new->type = lt_old->type;
+}
+
 void calculate_lstack_latency(struct gazelle_stack_latency *stack_latency, const struct pbuf *pbuf,
     enum GAZELLE_LATENCY_TYPE type)
 {
     uint64_t latency;
+    uint16_t lt_type;
     const struct latency_timestamp *lt;
     struct stack_latency *latency_stat;
 
@@ -60,7 +77,8 @@ void calculate_lstack_latency(struct gazelle_stack_latency *stack_latency, const
     }
 
     lt = &pbuf_to_private(pbuf)->lt;
-    if (lt->stamp != ~(lt->check) || lt->stamp < stack_latency->start_time) {
+    lt_type = (type / GAZELLE_LATENCY_READ_MAX) ? GAZELLE_LATENCY_WR : GAZELLE_LATENCY_RD;
+    if (lt->stamp != ~(lt->check) || lt->stamp < stack_latency->start_time || lt_type != lt->type) {
         return;
     }
 
diff --git a/src/lstack/include/lstack_stack_stat.h b/src/lstack/include/lstack_stack_stat.h
index d5a4ec7..87951aa 100644
--- a/src/lstack/include/lstack_stack_stat.h
+++ b/src/lstack/include/lstack_stack_stat.h
@@ -30,5 +30,6 @@ uint64_t get_current_time(void);
 void lstack_get_low_power_info(struct gazelle_stat_low_power_info *low_power_info);
 void unregister_wakeup(struct protocol_stack *stack, struct wakeup_poll *wakeup);
 void lstack_calculate_aggregate(int type, uint32_t len);
+void time_stamp_transfer_pbuf(struct pbuf *pbuf_old, struct pbuf *pbuf_new);
 
 #endif /* GAZELLE_STACK_STAT_H */
-- 
2.33.0

