@@ -41,6 +41,7 @@ static int json_server = 0;
4141static int json_port = JSON_PORT ;
4242static FILE * json_output = NULL ;
4343static int verbose = 0 ;
44+ static FILE * lstopo_misc_output = NULL ;
4445
4546void usage (const char * name , FILE * where )
4647{
@@ -66,6 +67,7 @@ void usage(const char *name, FILE *where)
6667 fprintf (where , " --pid-cmd <cmd> Append the output of <cmd> <pid> to each PID line\n" );
6768 fprintf (where , " --short-name Show only the process short name instead of the path\n" );
6869 fprintf (where , " --disallowed Include objects disallowed by administrative limitations\n" );
70+ fprintf (where , " --lstopo-misc <file> Output Misc object to be given to lstopo --misc-from <file>\n" );
6971 fprintf (where , " --json-server Run as a JSON server\n" );
7072 fprintf (where , " --json-port <n> Use port <n> for JSON server (default is %d)\n" , JSON_PORT );
7173 fprintf (where , " -v --verbose Increase verbosity\n" );
@@ -135,6 +137,45 @@ static void print_process(hwloc_topology_t topology,
135137 print_task (topology , proc -> threads [i ].tid , proc -> threads [i ].name , proc -> threads [i ].cpuset , NULL , 1 );
136138}
137139
140+ static void print_process_lstopo_misc (hwloc_topology_t topology __hwloc_attribute_unused ,
141+ struct hwloc_ps_process * proc )
142+ {
143+ /* sort of similar to foreach_process_cb() in lstopo.c */
144+ char name [100 ];
145+ char * s ;
146+ unsigned i ;
147+
148+ snprintf (name , sizeof (name ), "%ld" , proc -> pid );
149+ if (* proc -> name )
150+ snprintf (name , sizeof (name ), "%ld %s" , proc -> pid , proc -> name );
151+ hwloc_bitmap_asprintf (& s , proc -> cpuset );
152+ fprintf (lstopo_misc_output ,
153+ "name=%s\n"
154+ "cpuset=%s\n"
155+ "subtype=Process\n"
156+ "\n" ,
157+ name , s );
158+ free (s );
159+
160+ if (proc -> nthreads )
161+ for (i = 0 ; i < proc -> nthreads ; i ++ )
162+ if (proc -> threads [i ].cpuset ) {
163+ char task_name [150 ];
164+ if (* proc -> threads [i ].name )
165+ snprintf (task_name , sizeof (task_name ), "%s %li %s" , name , proc -> threads [i ].tid , proc -> threads [i ].name );
166+ else
167+ snprintf (task_name , sizeof (task_name ), "%s %li" , name , proc -> threads [i ].tid );
168+ hwloc_bitmap_asprintf (& s , proc -> threads [i ].cpuset );
169+ fprintf (lstopo_misc_output ,
170+ "name=%s\n"
171+ "cpuset=%s\n"
172+ "subtype=Thread\n"
173+ "\n" ,
174+ task_name , s );
175+ free (s );
176+ }
177+ }
178+
138179static void print_process_json (hwloc_topology_t topology ,
139180 struct hwloc_ps_process * proc )
140181{
@@ -207,6 +248,8 @@ static void foreach_process_cb(hwloc_topology_t topology,
207248
208249 if (json_output )
209250 print_process_json (topology , proc );
251+ else if (lstopo_misc_output )
252+ print_process_lstopo_misc (topology , proc );
210253 else
211254 print_process (topology , proc );
212255}
@@ -241,6 +284,8 @@ static int run(hwloc_topology_t topology, hwloc_const_bitmap_t topocpuset,
241284
242285 if (json_output )
243286 print_process_json (topology , & proc );
287+ else if (lstopo_misc_output )
288+ print_process_lstopo_misc (topology , & proc );
244289 else
245290 print_process (topology , & proc );
246291 }
@@ -467,6 +512,21 @@ int main(int argc, char *argv[])
467512 pidcmd = argv [1 ];
468513 opt = 1 ;
469514
515+ } else if (!strcmp (argv [0 ], "--lstopo-misc" )) {
516+ if (argc < 2 ) {
517+ usage (callname , stderr );
518+ exit (EXIT_FAILURE );
519+ }
520+ if (!strcmp (argv [1 ], "-" ))
521+ lstopo_misc_output = stdout ;
522+ else
523+ lstopo_misc_output = fopen (argv [1 ], "w" );
524+ if (!lstopo_misc_output ) {
525+ fprintf (stderr , "Failed to open --lstopo-misc output `%s' for writing (%s)\n" , argv [1 ], strerror (errno ));
526+ exit (EXIT_FAILURE );
527+ }
528+ opt = 1 ;
529+
470530 } else if (!strcmp (argv [0 ], "--json-server" )) {
471531 json_server = 1 ;
472532 } else if (!strcmp (argv [0 ], "--json-port" )) {
@@ -540,5 +600,7 @@ int main(int argc, char *argv[])
540600 out_with_topology :
541601 hwloc_topology_destroy (topology );
542602 out :
603+ if (lstopo_misc_output && lstopo_misc_output != stdout )
604+ fclose (lstopo_misc_output );
543605 return err ;
544606}
0 commit comments