Skip to contentSkip to conversation

cxl/list: Add parent_dport attribute to memdev and root port listing

PATCH New [email protected] 1 participant 2 messages Jun 15, 2023 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/2 cxl/list: Add parent_dport attribute to memdev and root port listing

From: Fan Ni <[email protected]>

Add parent_dport attribute to memdev and root port when listing by leveraging cxl_port_get_parent_dport function. The attribute will be used to describe the cxl topology when plotting cxl topology graph.

---
 cxl/json.c | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)
cxl/json.c +20 -3
index 9a4b5c7..f7600dc 100644
@@ -540,6 +540,23 @@ struct json_object *util_cxl_memdev_to_json(struct cxl_memdev *memdev,
 	if (jobj)
 		json_object_object_add(jdev, "host", jobj);
 
+	/* add parent_dport attribute to memdev */
+	if (cxl_memdev_get_endpoint(memdev)) {
+		struct cxl_endpoint *ep = cxl_memdev_get_endpoint(memdev);
+
+		if (ep) {
+			struct cxl_port *port = cxl_endpoint_get_port(ep);
+
+			if (cxl_port_get_parent_dport(port)) {
+				struct cxl_dport *dport = cxl_port_get_parent_dport(port);
+
+				jobj = json_object_new_string(cxl_dport_get_devname(dport));
+				if (jobj)
+					json_object_object_add(jdev, "parent_dport", jobj);
+			}
+		}
+	}
+
 	if (!cxl_memdev_is_enabled(memdev)) {
 		jobj = json_object_new_string("disabled");
 		if (jobj)
@@ -1009,9 +1026,9 @@ static struct json_object *__util_cxl_port_to_json(struct cxl_port *port,
 		struct cxl_dport *dport = cxl_port_get_parent_dport(port);
 
 		jobj = json_object_new_string(cxl_dport_get_devname(dport));
-		if (jobj)
-			json_object_object_add(jport, "parent_dport", jobj);
-	}
+	} else
+		jobj = json_object_new_string("ACPI0017:00");
+	json_object_object_add(jport, "parent_dport", jobj);
 
 	jobj = json_object_new_int(cxl_port_get_depth(port));
 	if (jobj)
Signature · 1 line
2.39.2
#2 lore raw

PATCH v2 2/2 cxl-graph: Add cxl graph command to construct CXL topology graph images

In reply to a message not in the archive

From: Fan Ni <[email protected]>

This adds a new command "cxl graph" to plot the cxl topology graph or dump cxl topology to a json formatted file based on the output format type (-t graph/plain). The command requires cxl port/memdev components have the "parent_dport" attribute so the topology can be correctly plotted. The CXL topology can be either read from a file passed to the command with "--input" option or parsed at runtime as "cxl list" does, and output will be stored in a file given by "--output".

This addition makes it easier to visualize the CXL topology. Also, being able to choose to use an input text file allows one to visualize the CXL topology while not having access to CXL hardware, which may be convenient.

The change is based on Matthew Ho' patch as list below[1].

The main changes include,
1) Instead of implementing the topology plotting function as a subcommand of 'cxl list', we add a new cxl command 'cxl graph', and use '-t' option to determine whether plotting the cxl topology graph or dumping the topology to json formatted plain file.
2) Instead of using root port attribute for plotting the topology, the parent_dport attribute of the memdev and port.objects are leveraged to plot the topology which enables plotting graph for more complicated cxl topology.
3) Add a function to validate the cxl topology input file when --input option is used.
4) Move all the graph related functions to graph.c.
5) Add a new '-t graph/plain' option indicating the output format (graph or json formatted plain file).
6). Add Documentation/cxl/cxl-graph.txt.
7). Add support for plotting cxl topology with inactive memdev. 8). Fix some issues in graph plotting related functions.
9). Fix the error messages, using error() instead of printf.

[1] Matthew Ho's patch:
https://lore.kernel.org/linux-cxl/[email protected]/

---
 Documentation/cxl/cxl-graph.txt | 106 +++++
 config.h.meson                  |   3 +
 cxl/builtin.h                   |   1 +
 cxl/cxl.c                       |   1 +
 cxl/filter.c                    |  15 +-
 cxl/filter.h                    |   5 +
 cxl/graph.c                     | 806 ++++++++++++++++++++++++++++++++
 cxl/meson.build                 |   9 +
 meson.build                     |   5 +
 meson_options.txt               |   1 +
 ndctl.spec.in                   |   1 +
 11 files changed, 949 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/cxl/cxl-graph.txt
 create mode 100644 cxl/graph.c
Documentation/cxl/cxl-graph.txt new file +106 -0
new file mode 100644
index 0000000..f8040e0
@@ -0,0 +1,106 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (C) 2022 Fan Ni <[email protected]>
+
+cxl-graph(1)
+===========
+
+NAME
+----
+cxl-graph - plot or dump the CXL topology to a graph or a json formatted file.
+
+SYNOPSIS
+--------
+[verse]
+'cxl graph' [<options>]
+
+options:
+-m::
+--memdev=::
+	Specify CXL memory device name(s) ("mem0"), device id(s) ("0"),
+	and/or host device name(s) ("0000:35:00.0") to filter the listing.
+-M::
+--memdevs::
+	Include CXL memory devices in the listing
+
+-e::
+--endpoint::
+	Specify CXL endpoint device name(s), or device id(s) to filter
+	the emitted endpoint(s).
+-E::
+--endpoints::
+	Include endpoint objects (CXL Memory Device decoders) in the
+	listing.
+
+-b::
+--bus=::
+	Specify CXL root device name(s), device id(s), and / or CXL bus provider
+	names to filter the listing. The supported provider names are "ACPI.CXL"
+	and "cxl_test".
+
+-p::
+--port=::
+	Specify CXL Port device name(s) ("port2"), device id(s) ("2"), host
+	device name(s) ("pci0000:34"), and / or port type name(s) to filter the
+	listing. The supported port type names are "root" and "switch".
+
+-v::
+--verbose::
+	Increase verbosity of the output. This can be specified
+	multiple times to be even more verbose on the
+	informational and miscellaneous output, and can be used
+	to override omitted flags for showing specific
+	information. Note that cxl list --verbose --verbose is
+	equivalent to cxl list -vv.
+	- *-v*
+	  Enable --memdevs, --regions, --buses,
+	  --ports, --decoders, and --targets.
+	- *-vv*
+	  Everything *-v* provides, plus include disabled
+	  devices with --idle.
+	- *-vvv*
+	  Everything *-vv* provides, plus enable
+	  --health and --partition.
+
+-t::
+	Identify the output format of the CXL topology, with "plain"
+	it will dump the cxl topology to a json formatted file, and
+	with "graph" it will generate a graph showing the cxl topology.
+	"graph" is the choice by default.
+
+-o::
+--output-file::
+	Create an image of the CXL topology or a json formatted representation of
+	the CXL based on the output format given by "-t" option. If no output file
+	is given, by default cxl-topology-graph.png is used for "graph" output
+	format, and "cxl-topology-plain.json" is used for "plain" output format.
+
+--input::
+	Take a json-formatted file with valid CXL topology and generate
+	a graph showing the CXL topology accordingly or dump it to the output file
+	based on the output format.
+
+--debug::
+	If the cxl tool was built with debug enabled, turn on debug
+	messages.
+
+EXAMPLE
+-------
+----
+1. Taking 'cxl list' output as input to plot a graph
+	cxl list -vvv > input.json
+	cxl graph -t graph --input input.json [-o graph.png]
+
+2. Parsing the cxl topology as cxl list does and plotting a graph
+	cxl graph -vvv -t graph [-o a.png/a.jpg/a.jpeg]
+
+3. Parsing the cxl topology as cxl list does and dump the topology to a json
+formatted file
+	cxl graph -vvv -t plain -o a.txt/a.json
+
+include::human-option.txt[]
+
+include::../copyright.txt[]
+
+SEE ALSO
+--------
+linkcxl:cxl-graph[1]
config.h.meson +3 -0
index 5441dff..162e8ba 100644
@@ -22,6 +22,9 @@
 /* cxl monitor support */
 #mesondefine ENABLE_LIBTRACEFS
 
+/* cxl graph support */
+#mesondefine Enable_LIBGVC
+
 /* Define to 1 if big-endian-arch */
 #mesondefine HAVE_BIG_ENDIAN
 
cxl/builtin.h +1 -0
index 9baa43b..773eb00 100644
@@ -22,6 +22,7 @@ int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_graph(int argc, const char **argv, struct cxl_ctx *ctx);
 #ifdef ENABLE_LIBTRACEFS
 int cmd_monitor(int argc, const char **argv, struct cxl_ctx *ctx);
 #else
cxl/cxl.c +1 -0
index 3be7026..76493e4 100644
@@ -77,6 +77,7 @@ static struct cmd_struct commands[] = {
 	{ "disable-region", .c_fn = cmd_disable_region },
 	{ "destroy-region", .c_fn = cmd_destroy_region },
 	{ "monitor", .c_fn = cmd_monitor },
+	{ "graph", .c_fn = cmd_graph },
 };
 
 int main(int argc, const char **argv)
cxl/filter.c +11 -4
index 6e8d421..b06dff5 100644
@@ -1170,10 +1170,6 @@ struct json_object *cxl_filter_walk(struct cxl_ctx *ctx,
 			}
 		}
 walk_children:
-		dbg(p, "walk decoders\n");
-		walk_decoders(port, p, pick_array(jchilddecoders, jbusdecoders),
-			      pick_array(jchildregions, jregions), flags);
-
 		dbg(p, "walk rch endpoints\n");
 		if (p->endpoints || p->memdevs || p->decoders)
 			walk_endpoints(port, p,
@@ -1182,12 +1178,23 @@ walk_children:
 				       pick_array(jchilddecoders, jepdecoders),
 				       flags);
 
+		/*
+		 * Need to walk ports before walking decoder so dport will be
+		 * properly initialized which is needed to get the correct
+		 * parent_dport of a port. This is needed if we plot the cxl
+		 * graph after some region is created.
+		 */
 		dbg(p, "walk ports\n");
 		walk_child_ports(port, p, pick_array(jchildports, jports),
 				 pick_array(jchilddecoders, jportdecoders),
 				 pick_array(jchildeps, jeps),
 				 pick_array(jchilddecoders, jepdecoders),
 				 pick_array(jchilddevs, jdevs), flags);
+
+		dbg(p, "walk decoders\n");
+		walk_decoders(port, p, pick_array(jchilddecoders, jbusdecoders),
+			      pick_array(jchildregions, jregions), flags);
+
 		cond_add_put_array_suffix(jbus, "ports", devname, jchildports);
 		cond_add_put_array_suffix(jbus, "endpoints", devname,
 					  jchildeps);
cxl/filter.h +5 -0
index c486514..0efc446 100644
@@ -6,6 +6,8 @@
 #include <stdbool.h>
 #include <util/log.h>
 #include <util/json.h>
+#include <json-c/json.h>
+#include <graphviz/gvc.h>
 
 struct cxl_filter_params {
 	const char *memdev_filter;
@@ -31,6 +33,9 @@ struct cxl_filter_params {
 	bool dax;
 	int verbose;
 	struct log_ctx ctx;
+	const char *input_file;
+	const char *output_file;
+	const char *output_format; /*plain/graph*/
 };
 
 struct cxl_memdev *util_cxl_memdev_filter(struct cxl_memdev *memdev,
cxl/graph.c new file +806 -0

806 lines not on this page · raw message

cxl/meson.build +9 -0
index 61b4d87..346768f 100644
@@ -25,6 +25,15 @@ deps = [
   versiondep,
 ]
 
+if get_option('libgvc').enabled()
+  cxl_src += [
+    'graph.c',
+  ]
+  deps += [
+    graphviz,
+  ]
+endif
+
 if get_option('libtracefs').enabled()
   cxl_src += [
     'event_trace.c',
meson.build +5 -0
index 50e83cf..4eabaf0 100644
@@ -143,6 +143,10 @@ kmod = dependency('libkmod')
 libudev = dependency('libudev')
 uuid = dependency('uuid')
 json = dependency('json-c')
+if get_option('libgvc').enabled()
+  graphviz = dependency('libgvc')
+endif
+
 if get_option('libtracefs').enabled()
   traceevent = dependency('libtraceevent')
   tracefs = dependency('libtracefs')
@@ -237,6 +241,7 @@ conf.set('ENABLE_DESTRUCTIVE', get_option('destructive').enabled())
 conf.set('ENABLE_LOGGING', get_option('logging').enabled())
 conf.set('ENABLE_DEBUG', get_option('dbg').enabled())
 conf.set('ENABLE_LIBTRACEFS', get_option('libtracefs').enabled())
+conf.set('ENABLE_LIBGVC', get_option('libgvc').enabled())
 
 typeof_code = '''
   void func() {
meson_options.txt +1 -0
index 5c41b1a..08e6c3a 100644
@@ -3,6 +3,7 @@ option('version-tag', type : 'string',
 option('docs', type : 'feature', value : 'enabled')
 option('asciidoctor', type : 'feature', value : 'enabled')
 option('libtracefs', type : 'feature', value : 'enabled')
+option('libgvc', type : 'feature', value : 'enabled')
 option('systemd', type : 'feature', value : 'enabled')
 option('keyutils', type : 'feature', value : 'enabled',
   description : 'enable nvdimm device passphrase management')
ndctl.spec.in +1 -0
index 7702f95..66cdf71 100644
@@ -31,6 +31,7 @@ BuildRequires:	pkgconfig(uuid)
 BuildRequires:	pkgconfig(json-c)
 BuildRequires:	pkgconfig(bash-completion)
 BuildRequires:	pkgconfig(systemd)
+BuildRequires:  graphviz
 BuildRequires:	keyutils-libs-devel
 BuildRequires:	systemd-rpm-macros
 BuildRequires:	iniparser-devel
Signature · 1 line
2.39.2