2323#include "misc.h"
2424
2525int hwloc_ps_read_process (hwloc_topology_t topology , hwloc_const_bitmap_t topocpuset ,
26- struct hwloc_ps_process * proc ,
27- unsigned long flags )
26+ struct hwloc_ps_process * proc ,
27+ unsigned long flags )
2828{
2929#ifdef HAVE_DIRENT_H
3030 hwloc_pid_t realpid ;
@@ -68,9 +68,9 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
6868 n = read (fd , comm , sizeof (comm ) - 1 );
6969 close (fd );
7070 if (n > 0 ) {
71- comm [n ] = '\0' ;
72- if (n > 1 && comm [n - 1 ] == '\n' )
73- comm [n - 1 ] = '\0' ;
71+ comm [n ] = '\0' ;
72+ if (n > 1 && comm [n - 1 ] == '\n' )
73+ comm [n - 1 ] = '\0' ;
7474 }
7575
7676 } else {
@@ -81,19 +81,19 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
8181 snprintf (path , pathlen , "/proc/%ld/stat" , proc -> pid );
8282 fd = open (path , O_RDONLY );
8383 if (fd >= 0 ) {
84- /* "pid (comm) ..." */
85- n = read (fd , stats , sizeof (stats ) - 1 );
86- close (fd );
87- if (n > 0 ) {
88- stats [n ] = '\0' ;
89- parenl = strchr (stats , '(' );
90- parenr = strchr (stats , ')' );
91- if (!parenr )
92- parenr = & stats [sizeof (stats )- 1 ];
93- * parenr = '\0' ;
94- if (parenl )
95- snprintf (comm , sizeof (comm ), "%s" , parenl + 1 );
96- }
84+ /* "pid (comm) ..." */
85+ n = read (fd , stats , sizeof (stats ) - 1 );
86+ close (fd );
87+ if (n > 0 ) {
88+ stats [n ] = '\0' ;
89+ parenl = strchr (stats , '(' );
90+ parenr = strchr (stats , ')' );
91+ if (!parenr )
92+ parenr = & stats [sizeof (stats )- 1 ];
93+ * parenr = '\0' ;
94+ if (parenl )
95+ snprintf (comm , sizeof (comm ), "%s" , parenl + 1 );
96+ }
9797 }
9898 }
9999
@@ -147,78 +147,78 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
147147 unsigned nbth = 0 ;
148148 /* count threads */
149149 while ((taskdirent = readdir (taskdir ))) {
150- tid = strtol (taskdirent -> d_name , & end , 10 );
151- if (* end )
152- /* Not a number */
153- continue ;
154- nbth ++ ;
150+ tid = strtol (taskdirent -> d_name , & end , 10 );
151+ if (* end )
152+ /* Not a number */
153+ continue ;
154+ nbth ++ ;
155155 }
156156 if (nbth > 1 ) {
157- /* if there's more than one thread, see if some are bound */
158- proc -> threads = calloc (nbth , sizeof (* proc -> threads ));
159- if (proc -> threads ) {
160- /* reread the directory but gather info now */
161- rewinddir (taskdir );
162- unsigned i = 0 ;
163- while ((taskdirent = readdir (taskdir ))) {
164- char * path2 ;
165- unsigned path2len ;
166-
167- tid = strtol (taskdirent -> d_name , & end , 10 );
168- if (* end )
169- /* Not a number */
170- continue ;
171-
172- proc -> threads [i ].tid = tid ;
173-
174- path2len = pathlen + 1 + 21 + 1 + 4 + 1 ;
175- path2 = malloc (path2len );
176- if (path2 ) {
177- int commfd ;
178- snprintf (path2 , path2len , "%s/%ld/comm" , path , tid );
179- commfd = open (path2 , O_RDWR );
180- if (commfd >= 0 ) {
181- n = read (commfd , proc -> threads [i ].name , sizeof (proc -> threads [i ].name ));
182- close (commfd );
183- if (n <= 0 )
184- proc -> threads [i ].name [0 ] = '\0' ;
185- else if ((size_t )n < sizeof (proc -> threads [i ].name ))
186- proc -> threads [i ].name [n ] = '\0' ;
187- proc -> threads [i ].name [sizeof (proc -> threads [i ].name )- 1 ] = '\0' ;
188- end = strchr (proc -> threads [i ].name , '\n' );
189- if (end )
190- * end = '\0' ;
191- }
192- free (path2 );
193- }
194-
195- if (flags & HWLOC_PS_FLAG_LASTCPULOCATION ) {
196- if (hwloc_linux_get_tid_last_cpu_location (topology , tid , cpuset ))
197- goto next ;
198- } else {
199- if (hwloc_linux_get_tid_cpubind (topology , tid , cpuset ))
200- goto next ;
201- }
202- hwloc_bitmap_and (cpuset , cpuset , topocpuset );
203- if (hwloc_bitmap_iszero (cpuset ))
204- goto next ;
205-
206- proc -> threads [i ].cpuset = hwloc_bitmap_dup (cpuset );
207- if (!hwloc_bitmap_isequal (cpuset , topocpuset )) {
208- proc -> threads [i ].bound = 1 ;
209- proc -> nboundthreads ++ ;
210- }
211-
212- next :
213- i ++ ;
214- proc -> nthreads ++ ;
215- if (i == nbth )
216- /* ignore the lastly created threads, I'm too lazy to reallocate */
217- break ;
218- }
219- } else {
220- /* failed to alloc, behave as if there were no threads */
221- }
157+ /* if there's more than one thread, see if some are bound */
158+ proc -> threads = calloc (nbth , sizeof (* proc -> threads ));
159+ if (proc -> threads ) {
160+ /* reread the directory but gather info now */
161+ rewinddir (taskdir );
162+ unsigned i = 0 ;
163+ while ((taskdirent = readdir (taskdir ))) {
164+ char * path2 ;
165+ unsigned path2len ;
166+
167+ tid = strtol (taskdirent -> d_name , & end , 10 );
168+ if (* end )
169+ /* Not a number */
170+ continue ;
171+
172+ proc -> threads [i ].tid = tid ;
173+
174+ path2len = pathlen + 1 + 21 + 1 + 4 + 1 ;
175+ path2 = malloc (path2len );
176+ if (path2 ) {
177+ int commfd ;
178+ snprintf (path2 , path2len , "%s/%ld/comm" , path , tid );
179+ commfd = open (path2 , O_RDWR );
180+ if (commfd >= 0 ) {
181+ n = read (commfd , proc -> threads [i ].name , sizeof (proc -> threads [i ].name ));
182+ close (commfd );
183+ if (n <= 0 )
184+ proc -> threads [i ].name [0 ] = '\0' ;
185+ else if ((size_t )n < sizeof (proc -> threads [i ].name ))
186+ proc -> threads [i ].name [n ] = '\0' ;
187+ proc -> threads [i ].name [sizeof (proc -> threads [i ].name )- 1 ] = '\0' ;
188+ end = strchr (proc -> threads [i ].name , '\n' );
189+ if (end )
190+ * end = '\0' ;
191+ }
192+ free (path2 );
193+ }
194+
195+ if (flags & HWLOC_PS_FLAG_LASTCPULOCATION ) {
196+ if (hwloc_linux_get_tid_last_cpu_location (topology , tid , cpuset ))
197+ goto next ;
198+ } else {
199+ if (hwloc_linux_get_tid_cpubind (topology , tid , cpuset ))
200+ goto next ;
201+ }
202+ hwloc_bitmap_and (cpuset , cpuset , topocpuset );
203+ if (hwloc_bitmap_iszero (cpuset ))
204+ goto next ;
205+
206+ proc -> threads [i ].cpuset = hwloc_bitmap_dup (cpuset );
207+ if (!hwloc_bitmap_isequal (cpuset , topocpuset )) {
208+ proc -> threads [i ].bound = 1 ;
209+ proc -> nboundthreads ++ ;
210+ }
211+
212+ next :
213+ i ++ ;
214+ proc -> nthreads ++ ;
215+ if (i == nbth )
216+ /* ignore the lastly created threads, I'm too lazy to reallocate */
217+ break ;
218+ }
219+ } else {
220+ /* failed to alloc, behave as if there were no threads */
221+ }
222222 }
223223 closedir (taskdir );
224224 }
@@ -342,16 +342,16 @@ void hwloc_ps_free_process(struct hwloc_ps_process *proc)
342342 if (proc -> nthreads )
343343 for (i = 0 ; i < proc -> nthreads ; i ++ )
344344 if (proc -> threads [i ].cpuset )
345- hwloc_bitmap_free (proc -> threads [i ].cpuset );
345+ hwloc_bitmap_free (proc -> threads [i ].cpuset );
346346 free (proc -> threads );
347347
348348 hwloc_bitmap_free (proc -> cpuset );
349349}
350350
351351int hwloc_ps_foreach_process (hwloc_topology_t topology , hwloc_const_bitmap_t topocpuset ,
352- void (* callback )(hwloc_topology_t topology , struct hwloc_ps_process * proc , void * cbdata ),
353- void * cbdata ,
354- unsigned long flags , const char * only_name , long uid )
352+ void (* callback )(hwloc_topology_t topology , struct hwloc_ps_process * proc , void * cbdata ),
353+ void * cbdata ,
354+ unsigned long flags , const char * only_name , long uid )
355355{
356356#ifdef HAVE_DIRENT_H
357357 DIR * dir ;
0 commit comments