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