11/*
2- * Copyright © 2009-2022 Inria. All rights reserved.
2+ * Copyright © 2009-2023 Inria. All rights reserved.
33 * Copyright © 2009-2012 Université Bordeaux
44 * Copyright © 2009-2011 Cisco Systems, Inc. All rights reserved.
55 * See COPYING in top-level directory.
@@ -35,6 +35,7 @@ static int logical = 1;
3535static int single_ancestor = 0 ;
3636#define NO_ONLY_PID -1
3737static long only_pid = NO_ONLY_PID ;
38+ static long children_of_pid = NO_ONLY_PID ;
3839static long only_uid ;
3940static int json_server = 0 ;
4041static int json_port = JSON_PORT ;
@@ -47,6 +48,7 @@ void usage(const char *name, FILE *where)
4748 fprintf (where , "Options:\n" );
4849 fprintf (where , " -a Show all processes, including those that are not bound\n" );
4950 fprintf (where , " --pid <pid> Only show process of pid number <pid>\n" );
51+ fprintf (where , " --children-of-pid <pid> Only show process of pid number <pid> and its children\n" );
5052 fprintf (where , " --name <name> Only show processes whose name contains <name>\n" );
5153#ifdef HWLOC_LINUX_SYS
5254 fprintf (where , " --uid <uid> Only show processes of the user with the given uid\n" );
@@ -197,7 +199,7 @@ static void foreach_process_cb(hwloc_topology_t topology,
197199 const char * pidcmd = cbdata ;
198200
199201 /* don't print anything if the process isn't bound and if no threads are bound and if not showing all */
200- if (!proc -> bound && (!proc -> nthreads || !proc -> nboundthreads ) && !show_all && !only_name )
202+ if (!proc -> bound && (!proc -> nthreads || !proc -> nboundthreads ) && !show_all && !only_name && children_of_pid == NO_ONLY_PID )
201203 return ;
202204
203205 if (pidcmd )
@@ -212,7 +214,11 @@ static void foreach_process_cb(hwloc_topology_t topology,
212214static int run (hwloc_topology_t topology , hwloc_const_bitmap_t topocpuset ,
213215 unsigned long psflags , char * pidcmd )
214216{
215- if (only_pid == NO_ONLY_PID ) {
217+ if (children_of_pid != NO_ONLY_PID ) {
218+ /* show children */
219+ return hwloc_ps_foreach_child (topology , topocpuset , children_of_pid , foreach_process_cb , pidcmd , psflags , only_name , only_uid );
220+
221+ } else if (only_pid == NO_ONLY_PID ) {
216222 /* show all */
217223 return hwloc_ps_foreach_process (topology , topocpuset , foreach_process_cb , pidcmd , psflags , only_name , only_uid );
218224
@@ -317,6 +323,7 @@ run_json_server(hwloc_topology_t topology, hwloc_const_bitmap_t topocpuset)
317323
318324 only_name = NULL ;
319325 only_pid = NO_ONLY_PID ;
326+ children_of_pid = NO_ONLY_PID ;
320327 current = req ;
321328 while (* current ) {
322329 if (!strncmp (current , "lastcpulocation " , 16 )) {
@@ -338,6 +345,10 @@ run_json_server(hwloc_topology_t topology, hwloc_const_bitmap_t topocpuset)
338345 psflags |= HWLOC_PS_FLAG_THREADS ;
339346 show_all = 1 ;
340347 break ;
348+ } else if (!strncmp (current , "childrenofpid=" , 14 )) {
349+ children_of_pid = atoi (current + 14 );
350+ show_all = 1 ;
351+ break ;
341352 } else if (!strncmp (current , "name=" , 5 )) {
342353 only_name = current + 5 ;
343354 show_all = 1 ;
@@ -418,6 +429,13 @@ int main(int argc, char *argv[])
418429 }
419430 only_pid = strtol (argv [1 ], NULL , 10 );
420431 opt = 1 ;
432+ } else if (!strcmp (argv [0 ], "--children-of-pid" )) {
433+ if (argc < 2 ) {
434+ usage (callname , stderr );
435+ exit (EXIT_FAILURE );
436+ }
437+ children_of_pid = strtol (argv [1 ], NULL , 10 );
438+ opt = 1 ;
421439 } else if (!strcmp (argv [0 ], "--name" )) {
422440 if (argc < 2 ) {
423441 usage (callname , stderr );
0 commit comments