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

Message ID 1511947742-16839-1-git-send-email-hofors@lysator.liu.se (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers

Checks

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

Commit Message

Mattias Rönnblom Nov. 29, 2017, 9:29 a.m. UTC
  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@lysator.liu.se>
---
 doc/guides/prog_guide/metrics_lib.rst | 40 ++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 17 deletions(-)
  

Comments

Kovacevic, Marko Dec. 12, 2017, 2:45 p.m. UTC | #1
> -----Original Message-----

> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Mattias Rönnblom

> Sent: Wednesday, November 29, 2017 9:29 AM

> To: Mcnamara, John <john.mcnamara@intel.com>

> Cc: dev@dpdk.org; Mattias Rönnblom <hofors@lysator.liu.se>

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

> 

> 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@lysator.liu.se>


Would it be more beneficial to keep the return in these two cases as not to do two jumps to go out instead of just one
And to remove the out: ; at the very end completely.

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;
         }
  

Patch

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:
+        ;
     }