chiark / gitweb /
core: add cgroup memory controller support on the unified hierarchy (#3315)
[elogind.git] / src / core / cgroup.h
1 #pragma once
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdbool.h>
23
24 //#include "list.h"
25 //#include "time-util.h"
26 #include "cgroup-util.h"
27
28 #if 0 /// UNNEEDED by elogind
29 typedef struct CGroupContext CGroupContext;
30 typedef struct CGroupDeviceAllow CGroupDeviceAllow;
31 typedef struct CGroupIODeviceWeight CGroupIODeviceWeight;
32 typedef struct CGroupIODeviceLimit CGroupIODeviceLimit;
33 typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
34 typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
35
36 typedef enum CGroupDevicePolicy {
37
38         /* When devices listed, will allow those, plus built-in ones,
39         if none are listed will allow everything. */
40         CGROUP_AUTO,
41
42         /* Everything forbidden, except built-in ones and listed ones. */
43         CGROUP_CLOSED,
44
45         /* Everythings forbidden, except for the listed devices */
46         CGROUP_STRICT,
47
48         _CGROUP_DEVICE_POLICY_MAX,
49         _CGROUP_DEVICE_POLICY_INVALID = -1
50 } CGroupDevicePolicy;
51
52 struct CGroupDeviceAllow {
53         LIST_FIELDS(CGroupDeviceAllow, device_allow);
54         char *path;
55         bool r:1;
56         bool w:1;
57         bool m:1;
58 };
59
60 struct CGroupIODeviceWeight {
61         LIST_FIELDS(CGroupIODeviceWeight, device_weights);
62         char *path;
63         uint64_t weight;
64 };
65
66 struct CGroupIODeviceLimit {
67         LIST_FIELDS(CGroupIODeviceLimit, device_limits);
68         char *path;
69         uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
70 };
71
72 struct CGroupBlockIODeviceWeight {
73         LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
74         char *path;
75         uint64_t weight;
76 };
77
78 struct CGroupBlockIODeviceBandwidth {
79         LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
80         char *path;
81         uint64_t rbps;
82         uint64_t wbps;
83 };
84
85 struct CGroupContext {
86         bool cpu_accounting;
87         bool io_accounting;
88         bool blockio_accounting;
89         bool memory_accounting;
90         bool tasks_accounting;
91
92         /* For unified hierarchy */
93         uint64_t io_weight;
94         uint64_t startup_io_weight;
95         LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
96         LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
97
98         uint64_t memory_low;
99         uint64_t memory_high;
100         uint64_t memory_max;
101
102         /* For legacy hierarchies */
103         uint64_t cpu_shares;
104         uint64_t startup_cpu_shares;
105         usec_t cpu_quota_per_sec_usec;
106
107         uint64_t blockio_weight;
108         uint64_t startup_blockio_weight;
109         LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
110         LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
111
112         uint64_t memory_limit;
113
114         CGroupDevicePolicy device_policy;
115         LIST_HEAD(CGroupDeviceAllow, device_allow);
116
117         /* Common */
118         uint64_t tasks_max;
119
120         bool delegate;
121 };
122
123 #include "cgroup-util.h"
124 #include "unit.h"
125
126 void cgroup_context_init(CGroupContext *c);
127 void cgroup_context_done(CGroupContext *c);
128 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
129 void cgroup_context_apply(CGroupContext *c, CGroupMask mask, const char *path, ManagerState state);
130
131 CGroupMask cgroup_context_get_mask(CGroupContext *c);
132
133 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
134 void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
135 void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
136 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
137 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
138
139 CGroupMask unit_get_own_mask(Unit *u);
140 CGroupMask unit_get_siblings_mask(Unit *u);
141 CGroupMask unit_get_members_mask(Unit *u);
142 CGroupMask unit_get_subtree_mask(Unit *u);
143
144 CGroupMask unit_get_target_mask(Unit *u);
145 CGroupMask unit_get_enable_mask(Unit *u);
146
147 void unit_update_cgroup_members_masks(Unit *u);
148
149 char *unit_default_cgroup_path(Unit *u);
150 int unit_set_cgroup_path(Unit *u, const char *path);
151
152 int unit_realize_cgroup(Unit *u);
153 void unit_release_cgroup(Unit *u);
154 void unit_prune_cgroup(Unit *u);
155 int unit_watch_cgroup(Unit *u);
156
157 int unit_attach_pids_to_cgroup(Unit *u);
158 #else
159 # include "logind.h"
160 # define MANAGER_IS_SYSTEM(m) (1)
161 #endif // 0
162
163 int manager_setup_cgroup(Manager *m);
164 void manager_shutdown_cgroup(Manager *m, bool delete);
165
166 #if 0 /// UNNEEDED by elogind
167 unsigned manager_dispatch_cgroup_queue(Manager *m);
168
169 Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
170 Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
171 Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
172
173 int unit_search_main_pid(Unit *u, pid_t *ret);
174 int unit_watch_all_pids(Unit *u);
175
176 int unit_get_memory_current(Unit *u, uint64_t *ret);
177 int unit_get_tasks_current(Unit *u, uint64_t *ret);
178 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
179 int unit_reset_cpu_usage(Unit *u);
180
181 bool unit_cgroup_delegate(Unit *u);
182
183 int unit_notify_cgroup_empty(Unit *u);
184 int manager_notify_cgroup_empty(Manager *m, const char *group);
185
186 void unit_invalidate_cgroup(Unit *u, CGroupMask m);
187
188 void manager_invalidate_startup_units(Manager *m);
189
190 const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
191 CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
192 #endif // 0