New function to dump large and sparsely populated data structures
like struct flow.

Signed-off-by: Ilya Maximets <[email protected]>
---
 include/openvswitch/dynamic-string.h |  2 ++
 lib/dynamic-string.c                 | 36 +++++++++++++++++++++-------
 2 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/include/openvswitch/dynamic-string.h 
b/include/openvswitch/dynamic-string.h
index ee1821710..1c262b049 100644
--- a/include/openvswitch/dynamic-string.h
+++ b/include/openvswitch/dynamic-string.h
@@ -61,6 +61,8 @@ void ds_put_printable(struct ds *, const char *, size_t);
 void ds_put_hex(struct ds *ds, const void *buf, size_t size);
 void ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
                      uintptr_t ofs, bool ascii);
+void ds_put_sparse_hex_dump(struct ds *ds, const void *buf_, size_t size,
+                            uintptr_t ofs, bool ascii);
 int ds_get_line(struct ds *, FILE *);
 int ds_get_preprocessed_line(struct ds *, FILE *, int *line_number);
 int ds_get_test_line(struct ds *, FILE *);
diff --git a/lib/dynamic-string.c b/lib/dynamic-string.c
index 3b4520f87..8e9555a63 100644
--- a/lib/dynamic-string.c
+++ b/lib/dynamic-string.c
@@ -389,13 +389,9 @@ ds_put_hex(struct ds *ds, const void *buf_, size_t size)
     }
 }
 
-/* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
- * line.  Numeric offsets are also included, starting at 'ofs' for the first
- * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
- * are also rendered alongside. */
-void
-ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
-                uintptr_t ofs, bool ascii)
+static void
+ds_put_hex_dump__(struct ds *ds, const void *buf_, size_t size,
+                  uintptr_t ofs, bool ascii, bool skip_zero_lines)
 {
     const uint8_t *buf = buf_;
     const size_t per_line = 16; /* Maximum bytes per line. */
@@ -411,6 +407,10 @@ ds_put_hex_dump(struct ds *ds, const void *buf_, size_t 
size,
             end = start + size;
         n = end - start;
 
+        if (skip_zero_lines && is_all_zeros(&buf[start], n)) {
+            goto next;
+        }
+
         /* Print line. */
         ds_put_format(ds, "%08"PRIxMAX"  ",
                       (uintmax_t) ROUND_DOWN(ofs, per_line));
@@ -438,13 +438,33 @@ ds_put_hex_dump(struct ds *ds, const void *buf_, size_t 
size,
             ds_chomp(ds, ' ');
         }
         ds_put_format(ds, "\n");
-
+next:
         ofs += n;
         buf += n;
         size -= n;
     }
 }
 
+/* Writes the 'size' bytes in 'buf' to 'string' as hex bytes arranged 16 per
+ * line.  Numeric offsets are also included, starting at 'ofs' for the first
+ * byte in 'buf'.  If 'ascii' is true then the corresponding ASCII characters
+ * are also rendered alongside. */
+void
+ds_put_hex_dump(struct ds *ds, const void *buf_, size_t size,
+                uintptr_t ofs, bool ascii)
+{
+    ds_put_hex_dump__(ds, buf_, size, ofs, ascii, false);
+}
+
+/* Same as 'ds_put_hex_dump', but doesn't print lines that only contains
+ * zero bytes. */
+void
+ds_put_sparse_hex_dump(struct ds *ds, const void *buf_, size_t size,
+                       uintptr_t ofs, bool ascii)
+{
+    ds_put_hex_dump__(ds, buf_, size, ofs, ascii, true);
+}
+
 int
 ds_last(const struct ds *ds)
 {
-- 
2.34.3

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to