forked from Atoptool/atop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgroups.c
1685 lines (1385 loc) · 38.2 KB
/
cgroups.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-/thread-level.
**
** This source-file contains functions to read and handle cgroup metrics.
** ==========================================================================
** Author: Gerlof Langeveld
** E-mail: [email protected]
** Initial date: January 2024
** --------------------------------------------------------------------------
** Copyright (C) 2024 Gerlof Langeveld
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
** --------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <sys/param.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <regex.h>
#include "atop.h"
#include "cgroups.h"
#include "photosyst.h"
#include "photoproc.h"
#include "showgeneric.h"
static void cgcalcdeviate(void);
static void cgrewind(struct cgchainer **);
static struct cgchainer *cgnext(struct cgchainer **, struct cgchainer **);
static void cgwipe(struct cgchainer **, struct cgchainer **,
struct cgchainer **, struct cgchainer **);
static unsigned long walkcgroup(char *, struct cgchainer *, int, long, int, int);
static void getconfig(struct cstat *, struct cstat *);
static int readconfigval(char *, count_t []);
static void getmetrics(struct cstat *);
static count_t gettotpressure(char *fname);
static long hashcalc(char *, long, int);
static void hashadd(struct cgchainer *[], struct cgchainer *);
static struct cgchainer *hashfind(struct cgchainer *[], long);
static int cgroupfilter(struct cstat *, int, char);
#define CGROUPROOT "/sys/fs/cgroup"
#define CGROUPNHASH 128 // power of 2
#define CGROUPMASK (CGROUPNHASH-1)
// cgroup administration
// =====================
// three types of cgroup collections are maintained:
//
// - current linked list
// -------------------
// this administration consists of a chain of cgchainer structs
// that each refer to a cstat struct containing the cgroup metrics
//
// this chain is built while gathering the current information by the
// walkcgroup() function
// this function mallocs one cgchainer struct and one cstat struct
//
// - previous linked list
// --------------------
// this administration consists of a chain of cgchainer structs
// that each refer to a cstat struct containing the previous cgroup metrics
//
// before building a new current chain, the current chain will be
// saved as the previous chain
//
// - deviation array
// ---------------
// this administration consists of an array(!) of cgchainer structs in which
// the cgchainer struct are still chained to the next element in the array
//
// current cgroup admi
//
static struct cgchainer *cgcurfirst, // first in linked list
*cgcurlast, // last in linked list
*cgcurcursor;
static unsigned int cgcursize; // total size of all cstat structs
static unsigned int cgcurnum; // number of cstat structs
static unsigned int cgcurprocs; // number of processes in current list
static int cgcursequence; // maintain sequence number to be
// assigned to every cgchainer struct
//
// later on this value can be used
// as index in the deviation array
// previous cgroup admi
//
static struct cgchainer *cgprefirst, // first in linked list
*cgprelast, // last in linked list
*cgprecursor,
*cgprehash[CGROUPNHASH];
// deviation cgroup admi
//
static struct cgchainer *cgdevfirst, // pointer to array (!)
*cgdevcursor;
// Check if cgroup v2 is supported on this machine
//
// Return value: 1 - yes
// 0 - no
//
int
cgroupv2support(void)
{
FILE *fp;
char line[128];
// check if this kernel offers cgroups version 2
//
if ( (fp = fopen("/proc/1/cgroup", "r")) )
{
while (fgets(line, sizeof line, fp))
{
if (memcmp(line, "0::", 3) == 0) // equal?
{
supportflags |= CGROUPV2;
break;
}
}
fclose(fp);
}
return (supportflags&CGROUPV2) > 0;
}
// Gather all metrics from the entire cgroup tree,
// creating a chain of cgchainer structs
//
void
photocgroup(void)
{
char origdir[4096];
// wipe previous cgroup chain (not needed any more)
//
cgwipe(&cgprefirst, &cgprelast, &cgprecursor, cgprehash);
// move current cgroup chain to previous cgroup chain
//
cgprefirst = cgcurfirst;
cgprelast = cgcurlast;
cgcurfirst = cgcurlast = cgcurcursor = NULL;
// wipe deviation cgroup memory
// - wipe all contiguous cstat structs (start address in first cgchainer)
// - wipe pidlist (start address in first cgchainer)
// - wipe all contiguous cgchainer structs
//
if (cgdevfirst)
{
free(cgdevfirst->cstat);
free(cgdevfirst->proclist);
free(cgdevfirst);
}
// save current directory and move to top directory
// of cgroup fs
//
if ( getcwd(origdir, sizeof origdir) == NULL)
mcleanstop(53, "failed to save current dir\n");
if ( chdir(CGROUPROOT) == -1)
mcleanstop(54, "failed to change to " CGROUPROOT "\n");
// read all subdirectory names below the cgroup top directory
//
cgcursequence = 0;
cgcursize = 0;
cgcurnum = 0;
cgcurprocs = 0;
walkcgroup(".", NULL, -1, 0, 0, 0);
// return to original directory
//
if ( chdir(origdir) == -1)
mcleanstop(55, "cannot change to %s\n", origdir);
}
// Gather statistics from one cgroup directory level
// and walk the directory tree further downwards by nested calls.
// Every call to this function will add one struct to the current
// cgroup chain.
//
// Return value: number of processes in this dir and the dirs underneath
// -1 = fail
//
static unsigned long
walkcgroup(char *dirname, struct cgchainer *cparent, int parentseq,
long upperhash, int upperlen, int depth)
{
FILE *fp;
DIR *dirp;
struct dirent *entp;
int namelen = strlen(dirname);
int cstatlen, i;
unsigned long procsbelow=0, proccnt=0, hash;
struct cgchainer *ccp;
// change to new directory
//
if ( chdir(dirname) == -1)
return -1;
// --------------------------------------------
// gather statistics for this cgroup directory
// --------------------------------------------
// - create new cgchainer struct
//
ccp = calloc(1, sizeof(struct cgchainer));
ptrverify(ccp, "Malloc failed for current cgchainer\n");
// - add cgchainer to current chain
//
if (cgcurfirst)
{
cgcurlast->next = ccp;
cgcurlast = ccp;
}
else
{
cgcurfirst = ccp;
cgcurlast = ccp;
}
// - create corresponding cstat struct
// with a rounded length to avoid unaligned structs later on
//
cstatlen = sizeof(struct cstat) + namelen + 1;
if (cstatlen & 0x7) // length is not 64-bit multiple?
cstatlen = ((cstatlen >> 3) + 1) << 3; // round up to 64-bit
ccp->cstat = calloc(1, cstatlen);
ptrverify(ccp->cstat, "Malloc failed for cstat of %d bytes\n", cstatlen);
cgcursize += cstatlen;
cgcurnum++;
// - determine number of processes in this cgroup directory
//
if ( (fp = fopen("cgroup.procs", "r")) )
{
char line[64];
while (fgets(line, sizeof line, fp))
proccnt++;
fclose(fp);
}
// - create corresponding pid list to cgchainer
// and fill it with the PIDs
//
if (proccnt)
{
ccp->proclist = malloc(sizeof(pid_t) * proccnt);
ptrverify(ccp->proclist,
"Malloc failed for proclist (%d pids)\n", proccnt);
}
else
{
ccp->proclist = NULL;
}
i = 0;
if ( (fp = fopen("cgroup.procs", "r")) )
{
char line[64];
while (fgets(line, sizeof line, fp))
{
*(ccp->proclist+i) = strtol(line, NULL, 10);
if (++i >= proccnt)
break;
}
fclose(fp);
}
proccnt = i; // number of processes might have shrunk
cgcurprocs += proccnt;
// - fill basic info in current cgchainer
//
strcpy(ccp->cstat->cgname, dirname); // copy directory name
if (*dirname == '.') // top directory?
{
ccp->cstat->cgname[0] = '\0'; // wipe name
namelen = 0;
}
hash = hashcalc(ccp->cstat->cgname, upperhash, upperlen);
ccp->cstat->gen.structlen = cstatlen;
ccp->cstat->gen.sequence = cgcursequence++; // assign unique sequence number
ccp->cstat->gen.parentseq = parentseq;
ccp->cstat->gen.depth = depth;
ccp->cstat->gen.nprocs = proccnt;
ccp->cstat->gen.namelen = namelen;
ccp->cstat->gen.fullnamelen = upperlen + namelen; // excluding slashes
ccp->cstat->gen.namehash = hash;
// - gather and store current cgroup configuration
//
getconfig(ccp->cstat, cparent ? cparent->cstat : NULL);
// - gather and store current cgroup metrics
//
getmetrics(ccp->cstat);
// --------------------------------------------
// walk subdirectories by nested calls
// --------------------------------------------
//
dirp = opendir(".");
while ( (entp = readdir(dirp)) )
{
// skip dot files/directories
//
if (entp->d_name[0] == '.')
continue;
// check if this entry represents a subdirectory
// to be walked as well
//
if (entp->d_type == DT_DIR)
procsbelow += walkcgroup(entp->d_name,
ccp, ccp->cstat->gen.sequence,
hash, upperlen+namelen, depth+1);
}
closedir(dirp);
// return to original directory
//
if ( chdir("..") == -1)
return -1;
ccp->cstat->gen.procsbelow = procsbelow;
return procsbelow + proccnt;
}
// Gather configuration values for one specific cgroup
// value -2: undefined
// value -1: maximum
//
static void
getconfig(struct cstat *csp, struct cstat *csparent)
{
count_t retvals[2];
// get cpu.weight
//
csp->conf.cpuweight = -2; // initial value (undefined)
switch (readconfigval("cpu.weight", retvals))
{
case 1:
csp->conf.cpuweight = retvals[0];
break;
}
// get cpu.max limitation
//
csp->conf.cpumax = -2; // initial value (undefined)
switch (readconfigval("cpu.max", retvals))
{
case 2:
if (retvals[0] == -1)
csp->conf.cpumax = -1; // max
else
csp->conf.cpumax = retvals[0] * 100 / retvals[1];
break;
}
// get io.bpf.weight
//
csp->conf.dskweight = -2; // initial value (undefined)
switch (readconfigval("io.bfq.weight", retvals))
{
case 2:
csp->conf.dskweight = retvals[1];
break;
}
// get memory.max limitation
//
csp->conf.memmax = -2; // initial value (undefined)
switch (readconfigval("memory.max", retvals))
{
case 1:
if (retvals[0] == -1)
csp->conf.memmax = -1; // max
else
csp->conf.memmax = retvals[0] / pagesize;
break;
}
// get memory.swap.max limitation
//
csp->conf.swpmax = -2; // initial value (undefined)
switch (readconfigval("memory.swap.max", retvals))
{
case 1:
if (retvals[0] == -1)
csp->conf.swpmax = -1; // max
else
csp->conf.swpmax = retvals[0] / pagesize;
break;
}
}
// read line with one or two values and
// fill one or (maximum) two values
// in retvals (value 'max' converted into -1)
//
// return value: number of entries in retvals filled
//
static int
readconfigval(char *fname, count_t retvals[])
{
char line[64];
int n;
FILE *fp;
if ( (fp = fopen(fname, "r")) )
{
char firststr[16];
if (!fgets(line, sizeof line, fp))
{
fclose(fp);
return 0;
}
fclose(fp);
switch (n = sscanf(line, "%15s %llu", firststr, &retvals[1]))
{
case 0:
return 0;
case 1:
case 2:
if ( strcmp(firststr, "max") == 0)
retvals[0] = -1;
else
retvals[0] = atol(firststr);
return n;
default:
return 0;
}
}
return 0;
}
// Gather metrics for one specific cgroup
//
static void
getmetrics(struct cstat *csp)
{
FILE *fp;
char line[256];
int cnt;
// gather CPU metrics
//
csp->cpu.utime = -1; // undefined
csp->cpu.stime = -1; // undefined
cnt = 0;
if ( (fp = fopen("cpu.stat", "r")) )
{
while (fgets(line, sizeof line, fp) && cnt < 2)
{
if (memcmp(line, "user_usec ", 10) == 0)
{
sscanf(line, "user_usec %lld", &(csp->cpu.utime));
cnt++;
continue;
}
if (memcmp(line, "system_usec ", 12) == 0)
{
sscanf(line, "system_usec %lld", &(csp->cpu.stime));
cnt++;
continue;
}
}
fclose(fp);
}
csp->cpu.pressure = gettotpressure("cpu.pressure");
// gather memory metrics
//
csp->mem.current = -1; // undefined
csp->mem.anon = -1; // undefined
csp->mem.file = -1; // undefined
csp->mem.kernel = -1; // undefined
csp->mem.shmem = -1; // undefined
cnt = 0;
if ( (fp = fopen("memory.current", "r")) )
{
if (fgets(line, sizeof line, fp) != NULL)
csp->mem.current = strtoll(line, NULL, 10) / pagesize;
fclose(fp);
}
if ( (fp = fopen("memory.stat", "r")) )
{
while (fgets(line, sizeof line, fp) && cnt < 4)
{
if (memcmp(line, "anon ", 5) == 0)
{
sscanf(line, "anon %lld", &(csp->mem.anon));
csp->mem.anon /= pagesize;
cnt++;
continue;
}
if (memcmp(line, "file ", 5) == 0)
{
sscanf(line, "file %lld", &(csp->mem.file));
csp->mem.file /= pagesize;
cnt++;
continue;
}
if (memcmp(line, "kernel ", 7) == 0)
{
sscanf(line, "kernel %lld", &(csp->mem.kernel));
csp->mem.kernel /= pagesize;
cnt++;
continue;
}
if (memcmp(line, "shmem ", 6) == 0)
{
sscanf(line, "shmem %lld", &(csp->mem.shmem));
csp->mem.shmem /= pagesize;
cnt++;
continue;
}
}
fclose(fp);
}
csp->mem.pressure = gettotpressure("memory.pressure");
// gather disk I/O metrics
//
// 253:2 rbytes=2544128 wbytes=2192896 rios=313 wios=22 dbytes=0 dios=0
// 253:1 rbytes=143278080 wbytes=3843604480 rios=34222 wios=853554 ...
// 253:0 rbytes=19492288000 wbytes=105266814976 rios=386493 wios=1691322
// 11:0
// 8:0 rbytes=328956266496 wbytes=109243312640 rios=764129 wios=1415575
//
csp->dsk.rbytes = 0;
csp->dsk.wbytes = 0;
csp->dsk.rios = 0;
csp->dsk.wios = 0;
if ( (fp = fopen("io.stat", "r")) )
{
int major, minor;
count_t rbytes, wbytes, rios, wios;
while (fgets(line, sizeof line, fp))
{
if (sscanf(line,
"%d:%d rbytes=%lld wbytes=%lld rios=%lld wios=%lld",
&major, &minor,
&rbytes, &wbytes, &rios, &wios) == 6)
{
if (isdisk_major(major) == DSKTYPE)
{
csp->dsk.rbytes += rbytes;
csp->dsk.wbytes += wbytes;
csp->dsk.rios += rios;
csp->dsk.wios += wios;
}
}
}
fclose(fp);
}
else
{
csp->dsk.rbytes = -1; // undefined
csp->dsk.wbytes = -1; // undefined
csp->dsk.rios = -1; // undefined
csp->dsk.wios = -1; // undefined
}
csp->dsk.pressure = gettotpressure("io.pressure");
}
// Get total pressure value from file with a format similar to:
//
// some avg10=0.00 avg60=0.00 avg300=0.00 total=9660682563
// full avg10=0.00 avg60=0.00 avg300=0.00 total=9376892229 <--
//
count_t
gettotpressure(char *fname)
{
char psiformat[] = "%c%*s avg10=%f avg60=%f avg300=%f total=%llu",
linebuf[256], psitype;
float a10, a60, a300;
count_t totpressure;
FILE *fp;
totpressure = -1; // undefined
if ( (fp = fopen(fname, "r")) != NULL)
{
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL) // skip 'some' line
{
// only handle second line: 'total' pressure
//
if ( fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
sscanf(linebuf, psiformat,
&psitype, &a10, &a60, &a300, &totpressure);
}
}
fclose(fp);
}
return totpressure;
}
// Calculate deviations between current cgroup chain and
// previous cgroup chain.
// Notice that the deviation chain structure is identical to
// the current chain structure, so they can be walked alongside.
// Also the deviation cstat struct is already a copy of the current
// cstat structure.
//
static void
cgcalcdeviate(void)
{
struct cgchainer *dp, *pp;
int insync = 1, prevfound;
// walk thru the deviation chain and calculate the differences
// with the previous chain
//
// when no cgroups have been added or removed since the
// previous sample, we might assume that the previous chain
// has the same order of elements as the deviation chain (insync)
//
// when cgroups have been added or removed since the
// previous sample (!insync), we build a hash list for the
// previous chain to find the correct entry based on the hashed name
//
cgrewind(&cgdevcursor);
cgrewind(&cgprecursor);
while ( (dp = cgnext(&cgdevfirst, &cgdevcursor)) )
{
// find previous cgroup struct
//
prevfound = 0;
if (insync)
{
// assume unchanged cgroup chain: take next from previous chain
//
pp = cgnext(&cgprefirst, &cgprecursor);
if (pp && dp->cstat->gen.namehash == pp->cstat->gen.namehash)
{
prevfound = 1;
}
else
{
insync = 0;
// build hash list for previous chain to find names
// based on hashed name for the rest of this loop
//
cgrewind(&cgprecursor);
while ( (pp = cgnext(&cgprefirst, &cgprecursor)) )
hashadd(cgprehash, pp);
}
}
if (!insync)
{
// modified cgroup chain: search previous by name hash
//
if ( (pp = hashfind(cgprehash, dp->cstat->gen.namehash)) )
prevfound = 1;
}
// calculate deviations related to previous sample
//
if (prevfound)
{
if (dp->cstat->cpu.utime != -1) // defined?
dp->cstat->cpu.utime -= pp->cstat->cpu.utime;
if (dp->cstat->cpu.stime != -1) // defined?
dp->cstat->cpu.stime -= pp->cstat->cpu.stime;
if (dp->cstat->cpu.pressure != -1) // defined?
dp->cstat->cpu.pressure -= pp->cstat->cpu.pressure;
if (dp->cstat->mem.pressure != -1) // defined?
dp->cstat->mem.pressure -= pp->cstat->mem.pressure;
if (dp->cstat->dsk.rbytes != -1) // defined?
{
dp->cstat->dsk.rbytes -= pp->cstat->dsk.rbytes;
dp->cstat->dsk.wbytes -= pp->cstat->dsk.wbytes;
dp->cstat->dsk.rios -= pp->cstat->dsk.rios;
dp->cstat->dsk.wios -= pp->cstat->dsk.wios;
}
if (dp->cstat->dsk.pressure != -1) // defined?
dp->cstat->dsk.pressure -= pp->cstat->dsk.pressure;
}
}
}
// Rewind cglist
//
static void
cgrewind(struct cgchainer **cursor)
{
*cursor = NULL;
}
// Get next cgchainer from current cglist
//
static struct cgchainer *
cgnext(struct cgchainer **first, struct cgchainer **cursor)
{
if (! *cursor)
*cursor = *first;
else
*cursor = (*cursor)->next;
if (*cursor)
return *cursor;
else
return NULL;
}
// Wipe current cgroup chain
//
void
cgwipecur(void)
{
cgwipe(&cgcurfirst, &cgcurlast, &cgcurcursor, NULL);
}
// Wipe all struct's from cgroup admi
//
static void
cgwipe(struct cgchainer **first, struct cgchainer **last,
struct cgchainer **cursor, struct cgchainer **hashlist)
{
struct cgchainer *cp, *cpn;
if (! *first) // empty already?
return;
// free all chain members
//
for (cp=*first; cp; cp=cpn)
{
cpn = cp->next;
if (cp->proclist)
free(cp->proclist);
free(cp->cstat);
free(cp);
}
*first = *last = *cursor = NULL;
if (hashlist)
memset(hashlist, 0, sizeof(struct cgchainer *) * CGROUPNHASH);
}
// Create enough memory to copy all cstat structures from
// the current list to one contiguous area.
// Also create enough memory to copy all pid lists from
// the current list to one contiguous area.
// Next, create and fill an array of cgchainer structs.
//
// Returns: number of cgchainer structs
//
int
deviatcgroup(struct cgchainer **cdpp, int *npids)
{
struct cgchainer *ccp = cgcurfirst;
char *allc, *allp, *cp, *pp;
// allocate contiguous memory areas for all cstat structs
// and all pid lists
//
allc = calloc(1, cgcursize);
allp = malloc(sizeof(pid_t) * cgcurprocs);
ptrverify(allc, "Malloc failed for contiguous cstats (%d bytes)\n", cgcursize);
ptrverify(allp, "Malloc failed for contiguous pids (%d pids)\n", cgcurprocs);
// concatenate all current cstat structs and all pid lists
// into the new memory areas
//
ccp = cgcurfirst;
cp = allc;
pp = allp;
while (ccp)
{
int cstatlen = ccp->cstat->gen.structlen;
int plistlen = ccp->cstat->gen.nprocs * sizeof(pid_t);
memcpy(cp, ccp->cstat, cstatlen);
cp += cstatlen;
if (ccp->proclist)
{
memcpy(pp, ccp->proclist, plistlen);
pp += plistlen;
}
ccp = ccp->next;
}
// build array of cgchainer structs and a hash list for the
// concatenated cstat structs for the deviation values
//
cgbuildarray(&cgdevfirst, allc, allp, cgcurnum);
// calculate deviation values
//
cgcalcdeviate();
*cdpp = cgdevfirst;
*npids = cgcurprocs;
return cgcurnum;
}
// Create concatenated array of cgchainer structs referring to
// proper location in the concatenated cstat structs and
// the concatenated pid lists.
//
void
cgbuildarray(struct cgchainer **firstp, char *cstats, char *pids, int ncstats)
{
struct cgchainer *cdp;
int i;
*firstp = malloc(sizeof(struct cgchainer) * ncstats);
ptrverify(firstp, "Malloc failed for contiguous cgchainers (%d)\n", ncstats);
for (cdp=*firstp, i=0; i < ncstats; cdp++, i++)
{
cdp->next = cdp+1;
cdp->hashnext = NULL;
cdp->proclist = (pid_t *)pids;
pids += sizeof(pid_t) * ((struct cstat *)cstats)->gen.nprocs;
cdp->cstat = (struct cstat *)cstats;
cstats += ((struct cstat *)cstats)->gen.structlen;
}
cdp = *firstp + ncstats - 1;
cdp->next = NULL; // terminate chain
}
// Assemble full pathname of cgroup directory (from cgroup top directory)
// from the deviation array
//
// Returns: malloc'ed string with full path name to be freed later on
//
char *
cggetpath(struct cgchainer *cdp, struct cgchainer *cdbase)
{
// calculate path length including slashes (depth) and terminating 0-byte
//
// in case of the top directory (without a parent), reserve one extra
// byte for the '/' character
//
int pathlen = cdp->cstat->gen.fullnamelen +
cdp->cstat->gen.depth + 1 +
(cdp->cstat->gen.sequence ? 0 : 1);
char *path = calloc(1, pathlen);
char *ps;
ptrverify(path, "Malloc failed for concatenated cgpath (%d)\n", pathlen);
// not the top directory?
//
if (cdp->cstat->gen.sequence)
{
// any other path: place all path components
// including preceding slashes
//
while (cdp->cstat->gen.parentseq != -1)
{
// determine location of preceding slash
// of this specific path component
//
ps = path + cdp->cstat->gen.fullnamelen -
cdp->cstat->gen.namelen +
cdp->cstat->gen.depth - 1;
// store slash and path component
//
*ps = '/';
memcpy(ps+1, cdp->cstat->cgname, cdp->cstat->gen.namelen);
// climb upwards
//
cdp = cdbase + cdp->cstat->gen.parentseq;
}
*(path+pathlen-1) = '\0'; // terminate string
}
else // exceptional case only for top directory
{