summaryrefslogtreecommitdiffstats
path: root/src/cgroups.cpp
blob: 80911952e9625d5b5c6f9c62c12592f0199bf5c4 (plain)
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
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <config.h>
#include "cgroups.h"
#include "util.h"

#include <map>
#include <unordered_map>
#include <utility>

bool
cgroups_enabled(void)
{
    static bool initialized = false;
    static bool has_cgroups;

    if (not initialized) {
        initialized = true;
        has_cgroups = Glib::file_test("/proc/cgroups", Glib::FileTest::FILE_TEST_EXISTS);
    }

    return has_cgroups;
}



static const std::pair<std::string, std::string> &
parse_cgroup_line(const std::string& line) {

    static std::unordered_map<std::string, std::pair<std::string, std::string>> line_cache;

    auto it = line_cache.insert({line, {"", ""} });
    if (it.second) { // inserted new
        std::string::size_type cat_start, name_start;

        if ((cat_start = line.find(':')) != std::string::npos
            && (name_start = line.find(':', cat_start + 1)) != std::string::npos) {

            // printf("%s %lu %lu\n", line.c_str(), cat_start, name_start);
            auto cat = line.substr(cat_start + 1, name_start - cat_start - 1);
            auto name = line.substr(name_start + 1);

            // strip the name= prefix
            if (cat.find("name=") == 0) {
                cat.erase(0, 5);
            }

            if (!name.empty() && name != "/") {
                it.first->second = {name, cat};
            }
        }
    }

    return it.first->second;
}


static const std::string&
get_process_cgroup_string(pid_t pid) {

    static std::unordered_map<std::string, std::string> file_cache{ {"", ""} };

    /* read out of /proc/pid/cgroup */
    auto path = "/proc/" + std::to_string(pid) + "/cgroup";
    std::string text;

    try {
        text = Glib::file_get_contents(path);
    } catch (...) {
        return file_cache[""];
    }

    auto it = file_cache.insert({ text, "" });

    if (it.second) { // inserted new

        // name -> [cat...], sorted by name;
        std::map<std::string, std::vector<std::string>> names;

        std::string::size_type last = 0, eol;

        // for each line in the file
        while ((eol = text.find('\n', last)) != std::string::npos) {
            auto line = text.substr(last, eol - last);
            last = eol + 1;

            const auto& p = parse_cgroup_line(line);
            if (!p.first.empty()) {
                names[p.first].push_back(p.second);
            }
        }


        // name (cat1, cat2), ...
        // sorted by name, categories
        std::vector<std::string> groups;

        for (auto& i : names) {
            std::sort(begin(i.second), end(i.second));
            std::string cats = procman::join(i.second, ", ");
            groups.push_back(i.first + " (" + cats + ')');
        }

        it.first->second = procman::join(groups, ", ");
    }

    return it.first->second;
}


void get_process_cgroup_info(ProcInfo& info) {
    if (not cgroups_enabled())
        return;

    const auto& cgroup_string = get_process_cgroup_string(info.pid);
    info.cgroup_name = cgroup_string;
}