1
1
/*
2
- ** ATOP - System & Process Monitor
3
- **
2
+ ** ATOP - System & Process Monitor
3
+ **
4
4
** The program 'atop' offers the possibility to view the activity of
5
5
** the system on system-level as well as process-/thread-level.
6
- **
6
+ **
7
7
** This source-file contains functions to read the process-administration
8
8
** of every running process from kernel-space and extract the required
9
9
** activity-counters.
@@ -118,10 +118,12 @@ photoproc(struct tstat *tasklist, int maxtask)
118
118
{
119
119
char line [128 ];
120
120
121
- if (fgets (line , sizeof line , fp ))
121
+ while (fgets (line , sizeof line , fp ))
122
122
{
123
- if (memcmp (line , "0::" , 3 ) == 0 ) // equal?
123
+ if (memcmp (line , "0::" , 3 ) == 0 ) { // equal?
124
124
supportflags |= CGROUPV2 ;
125
+ break ;
126
+ }
125
127
}
126
128
127
129
fclose (fp );
@@ -144,20 +146,20 @@ photoproc(struct tstat *tasklist, int maxtask)
144
146
*/
145
147
regainrootprivs ();
146
148
147
- /*
148
- ** if kernel module is not active on this system,
149
+ /*
150
+ ** if kernel module is not active on this system,
149
151
** netatop-bpf will try tp run;
150
152
*/
151
153
if (!(supportflags & NETATOPD )) {
152
154
netatop_bpf_probe ();
153
155
}
154
- /*
155
- ** if netatop-bpf is not active on this system,
156
+ /*
157
+ ** if netatop-bpf is not active on this system,
156
158
** kernel module will try to run;
157
159
*/
158
160
if (!(supportflags & NETATOPBPF )) {
159
161
netatop_probe ();
160
- }
162
+ }
161
163
162
164
/*
163
165
** if netatop-bpf is active on this system, skip call
@@ -245,7 +247,7 @@ photoproc(struct tstat *tasklist, int maxtask)
245
247
*/
246
248
if (getwchan )
247
249
procwchan (curtask );
248
-
250
+
249
251
if (supportflags & NETATOPBPF ) {
250
252
struct taskcount * tc = g_hash_table_lookup (ghash_net , & (curtask -> gen .tgid ));
251
253
if (tc ) {
@@ -288,7 +290,7 @@ photoproc(struct tstat *tasklist, int maxtask)
288
290
*/
289
291
curtask -> cpu .rundelay = 0 ;
290
292
curtask -> cpu .blkdelay = 0 ;
291
-
293
+
292
294
/*
293
295
** nvcsw and nivcsw on process level only
294
296
** concerns the delays of the main thread;
@@ -305,15 +307,15 @@ photoproc(struct tstat *tasklist, int maxtask)
305
307
unsigned long cur_nth = 0 ;
306
308
307
309
dirtask = opendir ("." );
308
-
310
+
309
311
/*
310
312
** due to race condition, opendir() might
311
313
** have failed (leave task and process-level
312
314
** directories)
313
315
*/
314
- if ( dirtask == NULL )
316
+ if ( dirtask == NULL )
315
317
{
316
- if (chdir ("../.." ) == -1 );
318
+ if (chdir ("../.." ) == -1 );
317
319
continue ;
318
320
}
319
321
@@ -333,7 +335,7 @@ photoproc(struct tstat *tasklist, int maxtask)
333
335
if ( chdir (".." ) == -1 );
334
336
continue ;
335
337
}
336
-
338
+
337
339
if ( !procstatus (curthr ) )
338
340
{
339
341
if ( chdir (".." ) == -1 );
@@ -442,7 +444,7 @@ counttasks(void)
442
444
char origdir [1024 ];
443
445
444
446
/*
445
- ** determine total number of threads
447
+ ** determine total number of threads
446
448
*/
447
449
if ( (fp = fopen ("/proc/loadavg" , "r" )) != NULL )
448
450
{
@@ -461,7 +463,7 @@ counttasks(void)
461
463
462
464
463
465
/*
464
- ** add total number of processes
466
+ ** add total number of processes
465
467
*/
466
468
if ( getcwd (origdir , sizeof origdir ) == NULL )
467
469
mcleanstop (53 , "cannot determine cwd\n" );
@@ -983,7 +985,7 @@ procschedstat(struct tstat *curtask)
983
985
static char * schedstatfile = "schedstat" ;
984
986
985
987
/*
986
- ** open the schedstat file
988
+ ** open the schedstat file
987
989
*/
988
990
if ( (fp = fopen (schedstatfile , "r" )) )
989
991
{
@@ -1054,31 +1056,33 @@ proccgroupv2(struct tstat *curtask)
1054
1056
int hash , pathlen , restlen , nslash ;
1055
1057
struct cgroupv2vals * pvals = NULL , * ptarget ;
1056
1058
char * p , * slashes [MAXSLASH ];
1059
+ int ret ;
1057
1060
1058
1061
/*
1059
1062
** open the cgroup file of the current process and
1060
1063
** read one line that should start with '0::' for cgroup v2
1061
1064
*/
1062
1065
if ( (fp = fopen ("cgroup" , "r" )) )
1063
1066
{
1064
- if (fgets (line , sizeof line , fp ))
1067
+ while (fgets (line , sizeof line , fp ))
1065
1068
{
1066
- if ( memcmp (line , "0::" , 3 ) ) // unequal ?
1069
+ if ( memcmp (line , "0::" , 3 ) == 0 ) // equal ?
1067
1070
{
1068
- fclose (fp );
1069
- curtask -> gen .cgpath [0 ] = '\0' ;
1070
- return 0 ; // no cgroupv2 support
1071
+ ret = 1 ;
1072
+ line [ strlen (line )- 1 ] = '\0' ; // remove newline
1073
+
1074
+ relpath = line + 3 ;
1075
+
1076
+ strncpy (curtask -> gen .cgpath , relpath ,
1077
+ sizeof curtask -> gen .cgpath );
1078
+ curtask -> gen .cgpath [sizeof curtask -> gen .cgpath - 1 ] = '\0' ;
1071
1079
}
1072
1080
}
1073
- fclose (fp );
1074
-
1075
- line [ strlen (line )- 1 ] = '\0' ; // remove newline
1076
1081
1077
- relpath = line + 3 ;
1082
+ if (ret != 1 )
1083
+ curtask -> gen .cgpath [0 ] = '\0' ;
1078
1084
1079
- strncpy (curtask -> gen .cgpath , relpath ,
1080
- sizeof curtask -> gen .cgpath );
1081
- curtask -> gen .cgpath [sizeof curtask -> gen .cgpath - 1 ] = '\0' ;
1085
+ fclose (fp );
1082
1086
}
1083
1087
else // open failed; no permission
1084
1088
{
@@ -1088,7 +1092,7 @@ proccgroupv2(struct tstat *curtask)
1088
1092
1089
1093
/*
1090
1094
** cgroup v2 pathname of this process is known;
1091
- ** prepare absolute pathname of cgroup
1095
+ ** prepare absolute pathname of cgroup
1092
1096
*/
1093
1097
pathlen = snprintf (abspath , sizeof abspath , "%s%s/" ,
1094
1098
CGROUPROOT , relpath );
@@ -1293,7 +1297,7 @@ fillcgroupv2(struct cgroupv2vals *pvals, char *abspath, char *extpath,
1293
1297
**
1294
1298
** return value: number of entries in retvals filled
1295
1299
*/
1296
- int
1300
+ int
1297
1301
readcgroupv2 (char * abspath , char * extpath , char * fname , int restlen ,
1298
1302
long retvals [])
1299
1303
{
@@ -1343,7 +1347,7 @@ readcgroupv2(char *abspath, char *extpath, char *fname, int restlen,
1343
1347
*/
1344
1348
static struct cgroupv2vals *
1345
1349
findhashcgroupv2 (char * relpath , int * phash )
1346
- {
1350
+ {
1347
1351
struct cgroupv2vals * p ;
1348
1352
char * s ;
1349
1353
int hash = 0 ;
0 commit comments