@@ -145,8 +145,6 @@ int show_version;
145
145
int in_error_handling = 0 ;
146
146
pid_t runpipe_pid = -1 ;
147
147
148
- bool is_cgroup_v2 = false ;
149
-
150
148
double walltimelimit[2 ], cputimelimit[2 ]; /* in seconds, soft and hard limits */
151
149
int walllimit_reached, cpulimit_reached; /* 1=soft, 2=hard, 3=both limits reached */
152
150
rlim_t memsize;
@@ -514,11 +512,7 @@ std::set<unsigned> read_cpuset(const char *path)
514
512
void check_remaining_procs ()
515
513
{
516
514
char path[1024 ];
517
- if (is_cgroup_v2) {
518
- snprintf (path, 1023 , " /sys/fs/cgroup/%s/cgroup.procs" , cgroupname);
519
- } else {
520
- snprintf (path, 1023 , " /sys/fs/cgroup/cpuacct/%s/cgroup.procs" , cgroupname);
521
- }
515
+ snprintf (path, 1023 , " /sys/fs/cgroup/%s/cgroup.procs" , cgroupname);
522
516
523
517
FILE *file = fopen (path, " r" );
524
518
if (file == nullptr ) {
@@ -532,33 +526,8 @@ void check_remaining_procs()
532
526
if (fclose (file) != 0 ) error (errno, " closing file `%s'" , path);
533
527
}
534
528
535
- void output_cgroup_stats_v1 (double *cputime)
536
- {
537
- struct cgroup *cg;
538
- if ( (cg = cgroup_new_cgroup (cgroupname))==nullptr ) error (0 ," cgroup_new_cgroup" );
539
-
540
- int ret;
541
- if ((ret = cgroup_get_cgroup (cg)) != 0 ) error (ret," get cgroup information" );
542
-
543
- int64_t max_usage = 0 ;
544
- struct cgroup_controller *cg_controller = cgroup_get_controller (cg, " memory" );
545
- ret = cgroup_get_value_int64 (cg_controller, " memory.memsw.max_usage_in_bytes" , &max_usage);
546
- if ( ret!=0 ) error (ret," get cgroup value memory.memsw.max_usage_in_bytes" );
547
-
548
- verbose (" total memory used: %" PRId64 " kB" , max_usage/1024 );
549
- write_meta (" memory-bytes" ," %" PRId64, max_usage);
550
-
551
- int64_t cpu_time_int;
552
- cg_controller = cgroup_get_controller (cg, " cpuacct" );
553
- ret = cgroup_get_value_int64 (cg_controller, " cpuacct.usage" , &cpu_time_int);
554
- if ( ret!=0 ) error (ret," get cgroup value cpuacct.usage" );
555
529
556
- *cputime = (double ) cpu_time_int / 1 .e9 ;
557
-
558
- cgroup_free (&cg);
559
- }
560
-
561
- void output_cgroup_stats_v2 (double *cputime)
530
+ void output_cgroup_stats (double *cputime)
562
531
{
563
532
struct cgroup *cg;
564
533
if ( (cg = cgroup_new_cgroup (cgroupname))==nullptr ) error (0 ," cgroup_new_cgroup" );
@@ -615,18 +584,13 @@ void cgroup_create()
615
584
}
616
585
617
586
int ret;
618
- if (is_cgroup_v2) {
619
- // TODO: do we want to set cpu.weight here as well?
620
- if (memsize != RLIM_INFINITY) {
621
- cgroup_add_value (uint64, " memory.max" , memsize);
622
- cgroup_add_value (uint64, " memory.swap.max" , 0 );
623
- } else {
624
- cgroup_add_value (string, " memory.max" , " max" );
625
- cgroup_add_value (string, " memory.swap.max" , " max" );
626
- }
587
+ // TODO: do we want to set cpu.weight here as well?
588
+ if (memsize != RLIM_INFINITY) {
589
+ cgroup_add_value (uint64, " memory.max" , memsize);
590
+ cgroup_add_value (uint64, " memory.swap.max" , 0 );
627
591
} else {
628
- cgroup_add_value (uint64 , " memory.limit_in_bytes " , memsize );
629
- cgroup_add_value (uint64 , " memory.memsw.limit_in_bytes " , memsize );
592
+ cgroup_add_value (string , " memory.max " , " max " );
593
+ cgroup_add_value (string , " memory.swap.max " , " max " );
630
594
}
631
595
632
596
/* Set up cpu restrictions; we pin the task to a specific set of
@@ -645,15 +609,6 @@ void cgroup_create()
645
609
verbose (" cpuset undefined" );
646
610
}
647
611
648
- if (!is_cgroup_v2) {
649
- if ( (cg_controller = cgroup_add_controller (cg, " cpu" ))==nullptr ) {
650
- error (0 ," cgroup_add_controller cpu" );
651
- }
652
- if ((cg_controller = cgroup_add_controller (cg, " cpuacct" )) == nullptr ) {
653
- error (0 , " cgroup_add_controller cpuacct" );
654
- }
655
- }
656
-
657
612
/* Perform the actual creation of the cgroup */
658
613
if ( (ret = cgroup_create_cgroup (cg, 1 ))!=0 ) error (ret," creating cgroup" );
659
614
@@ -663,46 +618,21 @@ void cgroup_create()
663
618
664
619
#undef cgroup_setval
665
620
666
- void cgroup_attach ()
667
- {
668
- struct cgroup *cg;
669
- cg = cgroup_new_cgroup (cgroupname);
670
- if (!cg) error (0 ," cgroup_new_cgroup" );
671
-
672
- int ret;
673
- if ( (ret = cgroup_get_cgroup (cg))!=0 ) error (ret," get cgroup information" );
674
-
675
- /* Attach task to the cgroup */
676
- if ( (ret = cgroup_attach_task (cg))!=0 ) error (ret," attach task to cgroup" );
677
-
678
- cgroup_free (&cg);
679
- }
680
621
681
622
void cgroup_kill ()
682
623
{
683
624
/* kill any remaining tasks, and wait for them to be gone */
684
625
char mem_controller[10 ] = " memory" ;
685
- if (is_cgroup_v2) {
686
- int size;
687
- do {
688
- pid_t * pids;
689
- int ret = cgroup_get_procs (cgroupname, mem_controller, &pids, &size);
690
- if (ret != 0 ) error (ret, " cgroup_get_procs" );
691
- for (int i = 0 ; i < size; i++) {
692
- kill (pids[i], SIGKILL);
693
- }
694
- free (pids);
695
- } while (size > 0 );
696
- } else {
697
- while (1 ) {
698
- void *handle = nullptr ;
699
- pid_t pid;
700
- int ret = cgroup_get_task_begin (cgroupname, mem_controller, &handle, &pid);
701
- cgroup_get_task_end (&handle);
702
- if (ret == ECGEOF) break ;
703
- kill (pid, SIGKILL);
626
+ int size;
627
+ do {
628
+ pid_t * pids;
629
+ int ret = cgroup_get_procs (cgroupname, mem_controller, &pids, &size);
630
+ if (ret != 0 ) error (ret, " cgroup_get_procs" );
631
+ for (int i = 0 ; i < size; i++) {
632
+ kill (pids[i], SIGKILL);
704
633
}
705
- }
634
+ free (pids);
635
+ } while (size > 0 );
706
636
}
707
637
708
638
void cgroup_delete ()
@@ -712,9 +642,6 @@ void cgroup_delete()
712
642
if (!cg) error (0 ," cgroup_new_cgroup" );
713
643
714
644
if (cgroup_add_controller (cg, " cpu" ) == nullptr ) error (0 , " cgroup_add_controller cpu" );
715
- if (!is_cgroup_v2) {
716
- if (cgroup_add_controller (cg, " cpuacct" ) == nullptr ) error (0 , " cgroup_add_controller cpuacct" );
717
- }
718
645
if ( cgroup_add_controller (cg, " memory" )==nullptr ) error (0 ," cgroup_add_controller memory" );
719
646
720
647
if ( cpuset!=nullptr && strlen (cpuset)>0 ) {
@@ -958,13 +885,9 @@ void setrestrictions()
958
885
}
959
886
960
887
/* Put the child process in the cgroup */
961
- if (is_cgroup_v2) {
962
- const char *controllers[] = { " memory" , nullptr };
963
- if (cgroup_change_cgroup_path (cgroupname, getpid (), controllers) != 0 ) {
964
- error (0 , " Failed to move the process to the cgroup" );
965
- }
966
- } else {
967
- cgroup_attach ();
888
+ const char *controllers[] = { " memory" , nullptr };
889
+ if (cgroup_change_cgroup_path (cgroupname, getpid (), controllers) != 0 ) {
890
+ error (0 , " Failed to move the process to the cgroup" );
968
891
}
969
892
970
893
/* Run the command in a separate process group so that the command
@@ -1109,28 +1032,6 @@ void pump_pipes(fd_set* readfds, size_t data_read[], size_t data_passed[])
1109
1032
1110
1033
}
1111
1034
1112
- bool cgroup_is_v2 () {
1113
- bool ret = false ;
1114
- FILE *fp = setmntent (" /proc/mounts" , " r" );
1115
- if (!fp) {
1116
- perror (" Error opening /proc/mounts" );
1117
- return false ;
1118
- }
1119
-
1120
- struct mntent *entry;
1121
- while ((entry = getmntent (fp)) != nullptr ) {
1122
- if (strcmp (entry->mnt_dir , " /sys/fs/cgroup" ) == 0 ) {
1123
- if (strcmp (entry->mnt_type , " cgroup2" ) == 0 ) {
1124
- ret = true ;
1125
- }
1126
- break ;
1127
- }
1128
- }
1129
-
1130
- endmntent (fp);
1131
-
1132
- return ret;
1133
- }
1134
1035
1135
1036
int main (int argc, char **argv)
1136
1037
{
@@ -1306,8 +1207,6 @@ int main(int argc, char **argv)
1306
1207
cmdname = argv[optind];
1307
1208
cmdargs = argv+optind;
1308
1209
1309
- is_cgroup_v2 = cgroup_is_v2 ();
1310
-
1311
1210
if ( outputmeta && (metafile = fopen (metafilename," w" ))==nullptr ) {
1312
1211
error (errno," cannot open `%s'" ,metafilename);
1313
1212
}
@@ -1630,11 +1529,7 @@ int main(int argc, char **argv)
1630
1529
check_remaining_procs ();
1631
1530
1632
1531
double cputime = -1 ;
1633
- if (is_cgroup_v2) {
1634
- output_cgroup_stats_v2 (&cputime);
1635
- } else {
1636
- output_cgroup_stats_v1 (&cputime);
1637
- }
1532
+ output_cgroup_stats (&cputime);
1638
1533
cgroup_kill ();
1639
1534
cgroup_delete ();
1640
1535
0 commit comments