chiark / gitweb /
Apply updates from upstream
[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 #include "ip-address-access.h"
28
29 #if 0 /// UNNEEDED by elogind
30 typedef struct CGroupContext CGroupContext;
31 typedef struct CGroupDeviceAllow CGroupDeviceAllow;
32 typedef struct CGroupIODeviceWeight CGroupIODeviceWeight;
33 typedef struct CGroupIODeviceLimit CGroupIODeviceLimit;
34 typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
35 typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
36
37 typedef enum CGroupDevicePolicy {
38
39         /* When devices listed, will allow those, plus built-in ones,
40         if none are listed will allow everything. */
41         CGROUP_AUTO,
42
43         /* Everything forbidden, except built-in ones and listed ones. */
44         CGROUP_CLOSED,
45
46         /* Everythings forbidden, except for the listed devices */
47         CGROUP_STRICT,
48
49         _CGROUP_DEVICE_POLICY_MAX,
50         _CGROUP_DEVICE_POLICY_INVALID = -1
51 } CGroupDevicePolicy;
52
53 struct CGroupDeviceAllow {
54         LIST_FIELDS(CGroupDeviceAllow, device_allow);
55         char *path;
56         bool r:1;
57         bool w:1;
58         bool m:1;
59 };
60
61 struct CGroupIODeviceWeight {
62         LIST_FIELDS(CGroupIODeviceWeight, device_weights);
63         char *path;
64         uint64_t weight;
65 };
66
67 struct CGroupIODeviceLimit {
68         LIST_FIELDS(CGroupIODeviceLimit, device_limits);
69         char *path;
70         uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
71 };
72
73 struct CGroupBlockIODeviceWeight {
74         LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
75         char *path;
76         uint64_t weight;
77 };
78
79 struct CGroupBlockIODeviceBandwidth {
80         LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
81         char *path;
82         uint64_t rbps;
83         uint64_t wbps;
84 };
85
86 struct CGroupContext {
87         bool cpu_accounting;
88         bool io_accounting;
89         bool blockio_accounting;
90         bool memory_accounting;
91         bool tasks_accounting;
92         bool ip_accounting;
93
94         /* For unified hierarchy */
95         uint64_t cpu_weight;
96         uint64_t startup_cpu_weight;
97         usec_t cpu_quota_per_sec_usec;
98
99         uint64_t io_weight;
100         uint64_t startup_io_weight;
101         LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
102         LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
103
104         uint64_t memory_low;
105         uint64_t memory_high;
106         uint64_t memory_max;
107         uint64_t memory_swap_max;
108
109         LIST_HEAD(IPAddressAccessItem, ip_address_allow);
110         LIST_HEAD(IPAddressAccessItem, ip_address_deny);
111
112         /* For legacy hierarchies */
113         uint64_t cpu_shares;
114         uint64_t startup_cpu_shares;
115
116         uint64_t blockio_weight;
117         uint64_t startup_blockio_weight;
118         LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
119         LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
120
121         uint64_t memory_limit;
122
123         CGroupDevicePolicy device_policy;
124         LIST_HEAD(CGroupDeviceAllow, device_allow);
125
126         /* Common */
127         uint64_t tasks_max;
128
129         bool delegate;
130 };
131
132 /* Used when querying IP accounting data */
133 typedef enum CGroupIPAccountingMetric {
134         CGROUP_IP_INGRESS_BYTES,
135         CGROUP_IP_INGRESS_PACKETS,
136         CGROUP_IP_EGRESS_BYTES,
137         CGROUP_IP_EGRESS_PACKETS,
138         _CGROUP_IP_ACCOUNTING_METRIC_MAX,
139         _CGROUP_IP_ACCOUNTING_METRIC_INVALID = -1,
140 } CGroupIPAccountingMetric;
141
142 #include "unit.h"
143
144 void cgroup_context_init(CGroupContext *c);
145 void cgroup_context_done(CGroupContext *c);
146 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
147
148 CGroupMask cgroup_context_get_mask(CGroupContext *c);
149
150 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
151 void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
152 void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
153 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
154 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
155
156 CGroupMask unit_get_own_mask(Unit *u);
157 CGroupMask unit_get_siblings_mask(Unit *u);
158 CGroupMask unit_get_members_mask(Unit *u);
159 CGroupMask unit_get_subtree_mask(Unit *u);
160
161 CGroupMask unit_get_target_mask(Unit *u);
162 CGroupMask unit_get_enable_mask(Unit *u);
163
164 bool unit_get_needs_bpf(Unit *u);
165
166 void unit_update_cgroup_members_masks(Unit *u);
167
168 char *unit_default_cgroup_path(Unit *u);
169 int unit_set_cgroup_path(Unit *u, const char *path);
170
171 int unit_realize_cgroup(Unit *u);
172 void unit_release_cgroup(Unit *u);
173 void unit_prune_cgroup(Unit *u);
174 int unit_watch_cgroup(Unit *u);
175
176 void unit_add_to_cgroup_empty_queue(Unit *u);
177
178 int unit_attach_pids_to_cgroup(Unit *u);
179 #else
180 # include "logind.h"
181 #endif // 0
182
183 int manager_setup_cgroup(Manager *m);
184 void manager_shutdown_cgroup(Manager *m, bool delete);
185
186 #if 0 /// UNNEEDED by elogind
187 unsigned manager_dispatch_cgroup_realize_queue(Manager *m);
188
189 Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
190 Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
191 Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
192
193 int unit_search_main_pid(Unit *u, pid_t *ret);
194 int unit_watch_all_pids(Unit *u);
195
196 int unit_get_memory_current(Unit *u, uint64_t *ret);
197 int unit_get_tasks_current(Unit *u, uint64_t *ret);
198 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
199 int unit_get_ip_accounting(Unit *u, CGroupIPAccountingMetric metric, uint64_t *ret);
200
201 int unit_reset_cpu_accounting(Unit *u);
202 int unit_reset_ip_accounting(Unit *u);
203
204 #define UNIT_CGROUP_BOOL(u, name)                       \
205         ({                                              \
206         CGroupContext *cc = unit_get_cgroup_context(u); \
207         cc ? cc->name : false;                          \
208         })
209
210 #endif // 0
211 int manager_notify_cgroup_empty(Manager *m, const char *group);
212
213 #if 0 /// UNNEEDED by elogind
214 void unit_invalidate_cgroup(Unit *u, CGroupMask m);
215 void unit_invalidate_cgroup_bpf(Unit *u);
216
217 void manager_invalidate_startup_units(Manager *m);
218
219 const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
220 CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;
221 #endif // 0