@cryptotaxi247 / netdata-1 / commits / 0fd8ea414

cgroups: properly remove not available cgroups from memory

Costa Tsaousis committed Apr 30, 2016 at 01:59 UTC 0fd8ea41441df27adea1e991348a19393937b59b
1 file changed +31 -23
src/sys_fs_cgroup.c
+31 -23
@@ -686,30 +686,13 @@ struct cgroup *cgroup_add(const char *id) {
686 return cg;
687 }
688
689 -void cgroup_remove(struct cgroup *cg) {
689 +void cgroup_free(struct cgroup *cg) {
690 debug(D_CGROUP, "removing cgroup '%s'", cg->id);
691
692 // FIXME
693 // switch this to debug(D_CGROUP, ...
694 info("Removing cgroup '%s' with chart id '%s' (was %s and %s)", cg->id, cg->chart_id, (cg->enabled)?"enabled":"disabled", (cg->available)?"available":"not available");
695
696 - if(cg == cgroup_root) {
697 - cgroup_root = cg->next;
698 - }
699 - else {
700 - struct cgroup *e;
701 - for(e = cgroup_root; e->next ;e = e->next)
702 - if(unlikely(e->next == cg)) break;
703 -
704 - if(e->next != cg) {
705 - error("Cannot find cgroup '%s' in list of cgroups", cg->id);
706 - }
707 - else {
708 - e->next = cg->next;
709 - cg->next = NULL;
710 - }
711 - }
712 -
696 free(cg->cpuacct_usage.cpu_percpu);
697
698 free(cg->cpuacct_stat.filename);
@@ -838,7 +821,31 @@ void mark_all_cgroups_as_not_available() {
821 cg->available = 0;
822 }
823
841 -struct cgroup *find_all_cgroups() {
824 +void cleanup_all_cgroups() {
825 + struct cgroup *cg, *last;
826 +
827 + for(cg = cgroup_root, last = NULL; cg ; last = cg) {
828 + if(!cg->available) {
829 +
830 + if(!last)
831 + cgroup_root = cg->next;
832 + else
833 + last->next = cg->next;
834 +
835 + cgroup_free(cg);
836 +
837 + if(!last)
838 + cg = cgroup_root;
839 + else
840 + cg = last->next;
841 +
842 + continue;
843 + }
844 + cg = cg->next;
845 + }
846 +}
847 +
848 +void find_all_cgroups() {
849 debug(D_CGROUP, "searching for cgroups");
850
851 mark_all_cgroups_as_not_available();
@@ -852,14 +859,15 @@ struct cgroup *find_all_cgroups() {
859 if(cgroup_enable_memory)
860 find_dir_in_subdirs(cgroup_memory_base, NULL, found_dir_in_subdir);
861
862 + // remove any non-existing cgroups
863 + cleanup_all_cgroups();
864 +
865 struct cgroup *cg;
866 for(cg = cgroup_root; cg ; cg = cg->next) {
867 // fprintf(stderr, " >>> CGROUP '%s' (%u - %s) with name '%s'\n", cg->id, cg->hash, cg->available?"available":"stopped", cg->name);
868
859 - if(!cg->available) {
860 - cgroup_remove(cg);
869 + if(unlikely(!cg->available))
870 continue;
862 - }
871
872 debug(D_CGROUP, "checking paths for cgroup '%s'", cg->id);
873
@@ -916,7 +924,7 @@ struct cgroup *find_all_cgroups() {
924 }
925
926 debug(D_CGROUP, "done searching for cgroups");
919 - return cgroup_root;
927 + return;
928 }
929
930 // ----------------------------------------------------------------------------