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
@@ -119,7 +119,7 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
119
119
status [1023 ] = '\0' ;
120
120
uid = strstr (status , "Uid:" );
121
121
if (uid )
122
- proc -> uid = strtoul (uid + 4 , NULL , 0 );
122
+ proc -> uid = strtoul (uid + 4 , NULL , 0 );
123
123
close (fd );
124
124
}
125
125
free (path );
@@ -146,78 +146,78 @@ int hwloc_ps_read_process(hwloc_topology_t topology, hwloc_const_bitmap_t topocp
146
146
unsigned nbth = 0 ;
147
147
/* count threads */
148
148
while ((taskdirent = readdir (taskdir ))) {
149
- tid = strtol (taskdirent -> d_name , & end , 10 );
150
- if (* end )
151
- /* Not a number */
152
- continue ;
153
- nbth ++ ;
149
+ tid = strtol (taskdirent -> d_name , & end , 10 );
150
+ if (* end )
151
+ /* Not a number */
152
+ continue ;
153
+ nbth ++ ;
154
154
}
155
155
if (nbth > 1 ) {
156
- /* if there's more than one thread, see if some are bound */
157
- proc -> threads = calloc (nbth , sizeof (* proc -> threads ));
158
- if (proc -> threads ) {
159
- /* reread the directory but gather info now */
160
- rewinddir (taskdir );
161
- unsigned i = 0 ;
162
- while ((taskdirent = readdir (taskdir ))) {
163
- char * path2 ;
164
- unsigned path2len ;
165
-
166
- tid = strtol (taskdirent -> d_name , & end , 10 );
167
- if (* end )
168
- /* Not a number */
169
- continue ;
170
-
171
- proc -> threads [i ].tid = tid ;
172
-
173
- path2len = pathlen + 1 + 21 + 1 + 4 + 1 ;
174
- path2 = malloc (path2len );
175
- if (path2 ) {
176
- int commfd ;
177
- snprintf (path2 , path2len , "%s/%ld/comm" , path , tid );
178
- commfd = open (path2 , O_RDWR );
179
- if (commfd >= 0 ) {
180
- n = read (commfd , proc -> threads [i ].name , sizeof (proc -> threads [i ].name ));
181
- close (commfd );
182
- if (n <= 0 )
183
- proc -> threads [i ].name [0 ] = '\0' ;
184
- else if ((size_t )n < sizeof (proc -> threads [i ].name ))
185
- proc -> threads [i ].name [n ] = '\0' ;
186
- proc -> threads [i ].name [sizeof (proc -> threads [i ].name )- 1 ] = '\0' ;
187
- end = strchr (proc -> threads [i ].name , '\n' );
188
- if (end )
189
- * end = '\0' ;
190
- }
191
- free (path2 );
192
- }
193
-
194
- if (flags & HWLOC_PS_FLAG_LASTCPULOCATION ) {
195
- if (hwloc_linux_get_tid_last_cpu_location (topology , tid , cpuset ))
196
- goto next ;
197
- } else {
198
- if (hwloc_linux_get_tid_cpubind (topology , tid , cpuset ))
199
- goto next ;
200
- }
201
- hwloc_bitmap_and (cpuset , cpuset , topocpuset );
202
- if (hwloc_bitmap_iszero (cpuset ))
203
- goto next ;
204
-
205
- proc -> threads [i ].cpuset = hwloc_bitmap_dup (cpuset );
206
- if (!hwloc_bitmap_isequal (cpuset , topocpuset )) {
207
- proc -> threads [i ].bound = 1 ;
208
- proc -> nboundthreads ++ ;
209
- }
210
-
211
- next :
212
- i ++ ;
213
- proc -> nthreads ++ ;
214
- if (i == nbth )
215
- /* ignore the lastly created threads, I'm too lazy to reallocate */
216
- break ;
217
- }
218
- } else {
219
- /* failed to alloc, behave as if there were no threads */
220
- }
156
+ /* if there's more than one thread, see if some are bound */
157
+ proc -> threads = calloc (nbth , sizeof (* proc -> threads ));
158
+ if (proc -> threads ) {
159
+ /* reread the directory but gather info now */
160
+ rewinddir (taskdir );
161
+ unsigned i = 0 ;
162
+ while ((taskdirent = readdir (taskdir ))) {
163
+ char * path2 ;
164
+ unsigned path2len ;
165
+
166
+ tid = strtol (taskdirent -> d_name , & end , 10 );
167
+ if (* end )
168
+ /* Not a number */
169
+ continue ;
170
+
171
+ proc -> threads [i ].tid = tid ;
172
+
173
+ path2len = pathlen + 1 + 21 + 1 + 4 + 1 ;
174
+ path2 = malloc (path2len );
175
+ if (path2 ) {
176
+ int commfd ;
177
+ snprintf (path2 , path2len , "%s/%ld/comm" , path , tid );
178
+ commfd = open (path2 , O_RDWR );
179
+ if (commfd >= 0 ) {
180
+ n = read (commfd , proc -> threads [i ].name , sizeof (proc -> threads [i ].name ));
181
+ close (commfd );
182
+ if (n <= 0 )
183
+ proc -> threads [i ].name [0 ] = '\0' ;
184
+ else if ((size_t )n < sizeof (proc -> threads [i ].name ))
185
+ proc -> threads [i ].name [n ] = '\0' ;
186
+ proc -> threads [i ].name [sizeof (proc -> threads [i ].name )- 1 ] = '\0' ;
187
+ end = strchr (proc -> threads [i ].name , '\n' );
188
+ if (end )
189
+ * end = '\0' ;
190
+ }
191
+ free (path2 );
192
+ }
193
+
194
+ if (flags & HWLOC_PS_FLAG_LASTCPULOCATION ) {
195
+ if (hwloc_linux_get_tid_last_cpu_location (topology , tid , cpuset ))
196
+ goto next ;
197
+ } else {
198
+ if (hwloc_linux_get_tid_cpubind (topology , tid , cpuset ))
199
+ goto next ;
200
+ }
201
+ hwloc_bitmap_and (cpuset , cpuset , topocpuset );
202
+ if (hwloc_bitmap_iszero (cpuset ))
203
+ goto next ;
204
+
205
+ proc -> threads [i ].cpuset = hwloc_bitmap_dup (cpuset );
206
+ if (!hwloc_bitmap_isequal (cpuset , topocpuset )) {
207
+ proc -> threads [i ].bound = 1 ;
208
+ proc -> nboundthreads ++ ;
209
+ }
210
+
211
+ next :
212
+ i ++ ;
213
+ proc -> nthreads ++ ;
214
+ if (i == nbth )
215
+ /* ignore the lastly created threads, I'm too lazy to reallocate */
216
+ break ;
217
+ }
218
+ } else {
219
+ /* failed to alloc, behave as if there were no threads */
220
+ }
221
221
}
222
222
closedir (taskdir );
223
223
}
@@ -341,16 +341,16 @@ void hwloc_ps_free_process(struct hwloc_ps_process *proc)
341
341
if (proc -> nthreads )
342
342
for (i = 0 ; i < proc -> nthreads ; i ++ )
343
343
if (proc -> threads [i ].cpuset )
344
- hwloc_bitmap_free (proc -> threads [i ].cpuset );
344
+ hwloc_bitmap_free (proc -> threads [i ].cpuset );
345
345
free (proc -> threads );
346
346
347
347
hwloc_bitmap_free (proc -> cpuset );
348
348
}
349
349
350
350
int hwloc_ps_foreach_process (hwloc_topology_t topology , hwloc_const_bitmap_t topocpuset ,
351
- void (* callback )(hwloc_topology_t topology , struct hwloc_ps_process * proc , void * cbdata ),
352
- void * cbdata ,
353
- unsigned long flags , const char * only_name , long uid )
351
+ void (* callback )(hwloc_topology_t topology , struct hwloc_ps_process * proc , void * cbdata ),
352
+ void * cbdata ,
353
+ unsigned long flags , const char * only_name , long uid )
354
354
{
355
355
#ifdef HAVE_DIRENT_H
356
356
DIR * dir ;
0 commit comments