]> git.puffer.fish Git - matthieu/frr.git/commitdiff
zebra: Use built in data structure counter
authorDonald Sharp <sharpd@nvidia.com>
Thu, 13 Jun 2024 19:30:00 +0000 (15:30 -0400)
committerDonald Sharp <sharpd@nvidia.com>
Thu, 13 Jun 2024 19:30:00 +0000 (15:30 -0400)
Instead of keeping a counter that is independent
of the queue's data structure.  Just use the queue's
built-in counter.  Ensure that it's pthread safe by
keeping it wrapped inside the mutex for adding/deleting
to the queue.

Signed-off-by: Donald Sharp <sharpd@nvidia.com>
zebra/dplane_fpm_nl.c
zebra/zebra_dplane.c
zebra/zebra_dplane.h

index 9ad92d6269dd873fcdca6b39b1e7f955d7c8fc41..28c481d1d873fd0257b10334b699a30a19b53659 100644 (file)
@@ -136,8 +136,6 @@ struct fpm_nl_ctx {
 
                /* Amount of data plane context processed. */
                _Atomic uint32_t dplane_contexts;
-               /* Amount of data plane contexts enqueued. */
-               _Atomic uint32_t ctxqueue_len;
                /* Peak amount of data plane contexts enqueued. */
                _Atomic uint32_t ctxqueue_len_peak;
 
@@ -399,6 +397,12 @@ DEFUN(fpm_show_counters, fpm_show_counters_cmd,
       FPM_STR
       "FPM statistic counters\n")
 {
+       uint32_t curr_queue_len;
+
+       frr_with_mutex (&gfnc->ctxqueue_mutex) {
+               curr_queue_len = dplane_ctx_queue_count(&gfnc->ctxqueue);
+       }
+
        vty_out(vty, "%30s\n%30s\n", "FPM counters", "============");
 
 #define SHOW_COUNTER(label, counter) \
@@ -412,8 +416,7 @@ DEFUN(fpm_show_counters, fpm_show_counters_cmd,
        SHOW_COUNTER("Connection errors", gfnc->counters.connection_errors);
        SHOW_COUNTER("Data plane items processed",
                     gfnc->counters.dplane_contexts);
-       SHOW_COUNTER("Data plane items enqueued",
-                    gfnc->counters.ctxqueue_len);
+       SHOW_COUNTER("Data plane items enqueued", curr_queue_len);
        SHOW_COUNTER("Data plane items queue peak",
                     gfnc->counters.ctxqueue_len_peak);
        SHOW_COUNTER("Buffer full hits", gfnc->counters.buffer_full);
@@ -432,6 +435,12 @@ DEFUN(fpm_show_counters_json, fpm_show_counters_json_cmd,
       "FPM statistic counters\n"
       JSON_STR)
 {
+       uint32_t curr_queue_len;
+
+       frr_with_mutex (&gfnc->ctxqueue_mutex) {
+               curr_queue_len = dplane_ctx_queue_count(&gfnc->ctxqueue);
+       }
+
        struct json_object *jo;
 
        jo = json_object_new_object();
@@ -445,8 +454,7 @@ DEFUN(fpm_show_counters_json, fpm_show_counters_json_cmd,
                            gfnc->counters.connection_errors);
        json_object_int_add(jo, "data-plane-contexts",
                            gfnc->counters.dplane_contexts);
-       json_object_int_add(jo, "data-plane-contexts-queue",
-                           gfnc->counters.ctxqueue_len);
+       json_object_int_add(jo, "data-plane-contexts-queue", curr_queue_len);
        json_object_int_add(jo, "data-plane-contexts-queue-peak",
                            gfnc->counters.ctxqueue_len_peak);
        json_object_int_add(jo, "buffer-full-hits", gfnc->counters.buffer_full);
@@ -1495,8 +1503,6 @@ static void fpm_process_queue(struct event *t)
 
                /* Account the processed entries. */
                processed_contexts++;
-               atomic_fetch_sub_explicit(&fnc->counters.ctxqueue_len, 1,
-                                         memory_order_relaxed);
 
                dplane_ctx_set_status(ctx, ZEBRA_DPLANE_REQUEST_SUCCESS);
                dplane_provider_enqueue_out_ctx(fnc->prov, ctx);
@@ -1670,7 +1676,7 @@ static int fpm_nl_process(struct zebra_dplane_provider *prov)
        struct zebra_dplane_ctx *ctx;
        struct fpm_nl_ctx *fnc;
        int counter, limit;
-       uint64_t cur_queue, peak_queue = 0, stored_peak_queue;
+       uint64_t cur_queue = 0, peak_queue = 0, stored_peak_queue;
 
        fnc = dplane_provider_get_data(prov);
        limit = dplane_provider_get_work_limit(prov);
@@ -1684,20 +1690,12 @@ static int fpm_nl_process(struct zebra_dplane_provider *prov)
                 * anyway.
                 */
                if (fnc->socket != -1 && fnc->connecting == false) {
-                       /*
-                        * Update the number of queued contexts *before*
-                        * enqueueing, to ensure counter consistency.
-                        */
-                       atomic_fetch_add_explicit(&fnc->counters.ctxqueue_len,
-                                                 1, memory_order_relaxed);
-
                        frr_with_mutex (&fnc->ctxqueue_mutex) {
                                dplane_ctx_enqueue_tail(&fnc->ctxqueue, ctx);
+                               cur_queue =
+                                       dplane_ctx_queue_count(&fnc->ctxqueue);
                        }
 
-                       cur_queue = atomic_load_explicit(
-                               &fnc->counters.ctxqueue_len,
-                               memory_order_relaxed);
                        if (peak_queue < cur_queue)
                                peak_queue = cur_queue;
                        continue;
@@ -1714,9 +1712,7 @@ static int fpm_nl_process(struct zebra_dplane_provider *prov)
                atomic_store_explicit(&fnc->counters.ctxqueue_len_peak,
                                      peak_queue, memory_order_relaxed);
 
-       if (atomic_load_explicit(&fnc->counters.ctxqueue_len,
-                                memory_order_relaxed)
-           > 0)
+       if (cur_queue > 0)
                event_add_event(fnc->fthread->master, fpm_process_queue, fnc, 0,
                                &fnc->t_dequeue);
 
index 06b34da20932b25b2eb69029b8cb02fe2639e74b..ee48a571b8321946835c921c95372990e96274ce 100644 (file)
@@ -969,6 +969,11 @@ struct zebra_dplane_ctx *dplane_ctx_dequeue(struct dplane_ctx_list_head *q)
        return ctx;
 }
 
+uint32_t dplane_ctx_queue_count(struct dplane_ctx_list_head *q)
+{
+       return dplane_ctx_list_count(q);
+}
+
 /*
  * Accessors for information from the context object
  */
index 060b1c8b9e032ddb7d154ad89b2a8e2118bd7dc4..0e9a8bfb99f56573581eb3038dd6206d1786397f 100644 (file)
@@ -323,6 +323,8 @@ struct zebra_dplane_ctx *dplane_ctx_get_head(struct dplane_ctx_list_head *q);
 /* Init a list of contexts */
 void dplane_ctx_q_init(struct dplane_ctx_list_head *q);
 
+uint32_t dplane_ctx_queue_count(struct dplane_ctx_list_head *q);
+
 /*
  * Accessors for information from the context object
  */