diff mbox series

[ndctl,v1,6/8] daxctl: include mappings when listing

Message ID 20200716184707.23018-7-joao.m.martins@oracle.com (mailing list archive)
State New, archived
Headers show
Series daxctl: Add device align and range mapping allocation | expand

Commit Message

Joao Martins July 16, 2020, 6:47 p.m. UTC
Iterate over the device mappings and print @page_offset,
 @start, @end and a computed size.

Signed-off-by: Joao Martins <joao.m.martins@oracle.com>
---
Perhaps hiding it behind a -v|--verbose?
---
 util/json.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 util/json.h |  3 +++
 2 files changed, 56 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/util/json.c b/util/json.c
index 4d9787381d6b..c814298ad0dc 100644
--- a/util/json.c
+++ b/util/json.c
@@ -277,7 +277,8 @@  struct json_object *util_daxctl_dev_to_json(struct daxctl_dev *dev,
 {
 	struct daxctl_memory *mem = daxctl_dev_get_memory(dev);
 	const char *devname = daxctl_dev_get_devname(dev);
-	struct json_object *jdev, *jobj;
+	struct json_object *jdev, *jobj, *jmappings = NULL;
+	struct daxctl_mapping *mapping = NULL;
 	int node, movable, align;
 
 	jdev = json_object_new_object();
@@ -331,6 +332,22 @@  struct json_object *util_daxctl_dev_to_json(struct daxctl_dev *dev,
 			json_object_object_add(jdev, "state", jobj);
 	}
 
+	daxctl_mapping_foreach(dev, mapping) {
+		struct json_object *jmapping;
+
+		if (!jmappings) {
+			jmappings = json_object_new_array();
+			if (!jmappings)
+				continue;
+
+			json_object_object_add(jdev, "mappings", jmappings);
+		}
+
+		jmapping = util_daxctl_mapping_to_json(mapping, flags);
+		if (!jmapping)
+			continue;
+		json_object_array_add(jmappings, jmapping);
+	}
 	return jdev;
 }
 
@@ -1180,6 +1197,41 @@  struct json_object *util_mapping_to_json(struct ndctl_mapping *mapping,
 	return NULL;
 }
 
+struct json_object *util_daxctl_mapping_to_json(struct daxctl_mapping *mapping,
+		unsigned long flags)
+{
+	struct json_object *jmapping = json_object_new_object();
+	struct json_object *jobj;
+
+	if (!jmapping)
+		return NULL;
+
+	jobj = util_json_object_hex(daxctl_mapping_get_offset(mapping), flags);
+	if (!jobj)
+		goto err;
+	json_object_object_add(jmapping, "page_offset", jobj);
+
+	jobj = util_json_object_hex(daxctl_mapping_get_start(mapping), flags);
+	if (!jobj)
+		goto err;
+	json_object_object_add(jmapping, "start", jobj);
+
+	jobj = util_json_object_hex(daxctl_mapping_get_end(mapping), flags);
+	if (!jobj)
+		goto err;
+	json_object_object_add(jmapping, "end", jobj);
+
+	jobj = util_json_object_size(daxctl_mapping_get_size(mapping), flags);
+	if (!jobj)
+		goto err;
+	json_object_object_add(jmapping, "size", jobj);
+
+	return jmapping;
+ err:
+	json_object_put(jmapping);
+	return NULL;
+}
+
 struct json_object *util_badblock_rec_to_json(u64 block, u64 count,
 		unsigned long flags)
 {
diff --git a/util/json.h b/util/json.h
index 6d39d3aa4693..c32074939068 100644
--- a/util/json.h
+++ b/util/json.h
@@ -15,6 +15,7 @@ 
 #include <stdio.h>
 #include <stdbool.h>
 #include <ndctl/libndctl.h>
+#include <daxctl/libdaxctl.h>
 #include <ccan/short_types/short_types.h>
 
 enum util_json_flags {
@@ -36,6 +37,8 @@  struct json_object *util_dimm_to_json(struct ndctl_dimm *dimm,
 		unsigned long flags);
 struct json_object *util_mapping_to_json(struct ndctl_mapping *mapping,
 		unsigned long flags);
+struct json_object *util_daxctl_mapping_to_json(struct daxctl_mapping *mapping,
+		unsigned long flags);
 struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,
 		unsigned long flags);
 struct json_object *util_badblock_rec_to_json(u64 block, u64 count,