23
23
#include "misc.h"
24
24
25
25
int 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 )
28
28
{
29
29
#ifdef HAVE_DIRENT_H
30
30
hwloc_pid_t realpid ;
@@ -68,9 +68,9 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
68
68
n = read (fd , comm , sizeof (comm ) - 1 );
69
69
close (fd );
70
70
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' ;
74
74
}
75
75
76
76
} else {
@@ -81,19 +81,19 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
81
81
snprintf (path , pathlen , "/proc/%ld/stat" , proc -> pid );
82
82
fd = open (path , O_RDONLY );
83
83
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
+ }
97
97
}
98
98
}
99
99
@@ -147,78 +147,78 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
147
147
unsigned nbth = 0 ;
148
148
/* count threads */
149
149
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 ++ ;
155
155
}
156
156
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
+ }
222
222
}
223
223
closedir (taskdir );
224
224
}
@@ -342,16 +342,16 @@ void hwloc_ps_free_process(struct hwloc_ps_process *proc)
342
342
if (proc -> nthreads )
343
343
for (i = 0 ; i < proc -> nthreads ; i ++ )
344
344
if (proc -> threads [i ].cpuset )
345
- hwloc_bitmap_free (proc -> threads [i ].cpuset );
345
+ hwloc_bitmap_free (proc -> threads [i ].cpuset );
346
346
free (proc -> threads );
347
347
348
348
hwloc_bitmap_free (proc -> cpuset );
349
349
}
350
350
351
351
int 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 )
355
355
{
356
356
#ifdef HAVE_DIRENT_H
357
357
DIR * dir ;
0 commit comments