[v2,08/12] examples/l2fwd: move periodic tasks to new function

Message ID 1531289248-20025-9-git-send-email-anoob.joseph@caviumnetworks.com
State New
Delegated to: Thomas Monjalon
Headers show
Series
  • preparing l2fwd for eventmode additions
Related show

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Anoob Joseph July 11, 2018, 6:07 a.m.
Move the periodic operations (stats flush and drain buffers) to a new
function.

Signed-off-by: Anoob Joseph <anoob.joseph@caviumnetworks.com>
---
v2:
* No change

v1:
* Replaced 'unsigned' with 'unsigned int'

 examples/l2fwd/l2fwd_worker.c | 83 ++++++++++++++++++++++++-------------------
 examples/l2fwd/l2fwd_worker.h |  6 ++++
 2 files changed, 52 insertions(+), 37 deletions(-)

Patch

diff --git a/examples/l2fwd/l2fwd_worker.c b/examples/l2fwd/l2fwd_worker.c
index f847832..868d0c6 100644
--- a/examples/l2fwd/l2fwd_worker.c
+++ b/examples/l2fwd/l2fwd_worker.c
@@ -94,6 +94,45 @@  l2fwd_drain_buffers(struct lcore_queue_conf *qconf)
 	}
 }
 
+static inline void
+l2fwd_periodic_drain_stats_monitor(struct lcore_queue_conf *qconf,
+		struct tsc_tracker *t, int is_master_core)
+{
+	uint64_t diff_tsc, cur_tsc;
+
+	cur_tsc = rte_rdtsc();
+
+	/*
+	 * TX burst queue drain
+	 */
+	diff_tsc = cur_tsc - t->prev_tsc;
+	if (unlikely(diff_tsc > t->drain_tsc)) {
+
+		/* Drain buffers */
+		l2fwd_drain_buffers(qconf);
+
+		/* if timer is enabled */
+		if (timer_period > 0) {
+
+			/* advance the timer */
+			t->timer_tsc += diff_tsc;
+
+			/* if timer has reached its timeout */
+			if (unlikely(t->timer_tsc >= timer_period)) {
+
+				/* do this only on master core */
+				if (is_master_core) {
+					print_stats();
+					/* reset the timer */
+					t->timer_tsc = 0;
+				}
+			}
+		}
+
+		t->prev_tsc = cur_tsc;
+	}
+}
+
 static void
 l2fwd_mac_updating(struct rte_mbuf *m, unsigned int dest_portid)
 {
@@ -135,19 +174,18 @@  l2fwd_main_loop(void)
 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
 	struct rte_mbuf *m;
 	unsigned int lcore_id;
-	uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
 	unsigned int i, j, portid, nb_rx;
 	struct lcore_queue_conf *qconf;
-	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
-			/ US_PER_S * BURST_TX_DRAIN_US;
 	int is_master_core;
-
-	prev_tsc = 0;
-	timer_tsc = 0;
+	struct tsc_tracker tsc = {0};
 
 	lcore_id = rte_lcore_id();
 	qconf = &lcore_queue_conf[lcore_id];
 
+	/* Set drain tsc */
+	tsc.drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
+			US_PER_S * BURST_TX_DRAIN_US;
+
 	if (qconf->n_rx_port == 0) {
 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
 		return;
@@ -167,37 +205,8 @@  l2fwd_main_loop(void)
 
 	while (!force_quit) {
 
-		cur_tsc = rte_rdtsc();
-
-		/*
-		 * TX burst queue drain
-		 */
-		diff_tsc = cur_tsc - prev_tsc;
-		if (unlikely(diff_tsc > drain_tsc)) {
-
-			/* Drain buffers */
-			l2fwd_drain_buffers(qconf);
-
-			/* if timer is enabled */
-			if (timer_period > 0) {
-
-				/* advance the timer */
-				timer_tsc += diff_tsc;
-
-				/* if timer has reached its timeout */
-				if (unlikely(timer_tsc >= timer_period)) {
-
-					/* do this only on master core */
-					if (is_master_core) {
-						print_stats();
-						/* reset the timer */
-						timer_tsc = 0;
-					}
-				}
-			}
-
-			prev_tsc = cur_tsc;
-		}
+		/* Do periodic operations (buffer drain & stats monitor) */
+		l2fwd_periodic_drain_stats_monitor(qconf, &tsc, is_master_core);
 
 		/*
 		 * Read packet from RX queues
diff --git a/examples/l2fwd/l2fwd_worker.h b/examples/l2fwd/l2fwd_worker.h
index 8971a6a..65650aa 100644
--- a/examples/l2fwd/l2fwd_worker.h
+++ b/examples/l2fwd/l2fwd_worker.h
@@ -4,6 +4,12 @@ 
 #ifndef _L2FWD_WORKER_H_
 #define _L2FWD_WORKER_H_
 
+struct tsc_tracker {
+	uint64_t prev_tsc;
+	uint64_t timer_tsc;
+	uint64_t drain_tsc;
+};
+
 int
 l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy);