[dpdk-dev] [PATCH] doc: fix issues in metrics example

Mattias Rönnblom hofors at lysator.liu.se
Wed Nov 29 10:29:02 CET 2017


The metrics example didn't retrieve the metrics' names, and also had
some more minor issues with repetitive error handling code and missing
variable declarations.

Signed-off-by: Mattias Rönnblom <hofors at lysator.liu.se>
---
 doc/guides/prog_guide/metrics_lib.rst | 40 ++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git a/doc/guides/prog_guide/metrics_lib.rst b/doc/guides/prog_guide/metrics_lib.rst
index d52204f..6326980 100644
--- a/doc/guides/prog_guide/metrics_lib.rst
+++ b/doc/guides/prog_guide/metrics_lib.rst
@@ -143,41 +143,47 @@ print out all metrics for a given port:
 
 .. code-block:: c
 
-    void print_metrics() {
+    void print_metrics(int port_id)
+    {
         struct rte_metric_value *metrics;
         struct rte_metric_name *names;
         int len;
+        int ret;
+        int i;
 
         len = rte_metrics_get_names(NULL, 0);
         if (len < 0) {
-            printf("Cannot get metrics count\n");
-            return;
-        }
-        if (len == 0) {
-            printf("No metrics to display (none have been registered)\n");
-            return;
+            printf("Cannot get metrics count.\n");
+            goto out;
+        } else if (len == 0) {
+            printf("No metrics to display (none have been registered).\n");
+            goto out;
         }
         metrics = malloc(sizeof(struct rte_metric_value) * len);
-        names =  malloc(sizeof(struct rte_metric_name) * len);
+        names = malloc(sizeof(struct rte_metric_name) * len);
         if (metrics == NULL || names == NULL) {
-            printf("Cannot allocate memory\n");
-            free(metrics);
-            free(names);
-            return;
+            printf("Cannot allocate memory.\n");
+            goto out_free;
+        }
+        ret = rte_metrics_get_names(names, len);
+        if (ret < 0 || ret > len) {
+            printf("Cannot get metrics names.\n");
+            goto out_free;
         }
         ret = rte_metrics_get_values(port_id, metrics, len);
         if (ret < 0 || ret > len) {
-            printf("Cannot get metrics values\n");
-            free(metrics);
-            free(names);
-            return;
+            printf("Cannot get metrics values.\n");
+            goto out_free;
         }
         printf("Metrics for port %i:\n", port_id);
         for (i = 0; i < len; i++)
             printf("  %s: %"PRIu64"\n",
-                names[metrics[i].key].name, metrics[i].value);
+                   names[metrics[i].key].name, metrics[i].value);
+     out_free:
         free(metrics);
         free(names);
+     out:
+        ;
     }
 
 
-- 
2.7.4



More information about the dev mailing list