]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/cgroup.h
Merge pull request #3589 from brauner/cgroup_namespace
[thirdparty/systemd.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 typedef struct CGroupContext CGroupContext;
29 typedef struct CGroupDeviceAllow CGroupDeviceAllow;
30 typedef struct CGroupIODeviceWeight CGroupIODeviceWeight;
31 typedef struct CGroupIODeviceLimit CGroupIODeviceLimit;
32 typedef struct CGroupBlockIODeviceWeight CGroupBlockIODeviceWeight;
33 typedef struct CGroupBlockIODeviceBandwidth CGroupBlockIODeviceBandwidth;
34
35 typedef enum CGroupDevicePolicy {
36
37 /* When devices listed, will allow those, plus built-in ones,
38 if none are listed will allow everything. */
39 CGROUP_AUTO,
40
41 /* Everything forbidden, except built-in ones and listed ones. */
42 CGROUP_CLOSED,
43
44 /* Everythings forbidden, except for the listed devices */
45 CGROUP_STRICT,
46
47 _CGROUP_DEVICE_POLICY_MAX,
48 _CGROUP_DEVICE_POLICY_INVALID = -1
49 } CGroupDevicePolicy;
50
51 struct CGroupDeviceAllow {
52 LIST_FIELDS(CGroupDeviceAllow, device_allow);
53 char *path;
54 bool r:1;
55 bool w:1;
56 bool m:1;
57 };
58
59 struct CGroupIODeviceWeight {
60 LIST_FIELDS(CGroupIODeviceWeight, device_weights);
61 char *path;
62 uint64_t weight;
63 };
64
65 struct CGroupIODeviceLimit {
66 LIST_FIELDS(CGroupIODeviceLimit, device_limits);
67 char *path;
68 uint64_t limits[_CGROUP_IO_LIMIT_TYPE_MAX];
69 };
70
71 struct CGroupBlockIODeviceWeight {
72 LIST_FIELDS(CGroupBlockIODeviceWeight, device_weights);
73 char *path;
74 uint64_t weight;
75 };
76
77 struct CGroupBlockIODeviceBandwidth {
78 LIST_FIELDS(CGroupBlockIODeviceBandwidth, device_bandwidths);
79 char *path;
80 uint64_t rbps;
81 uint64_t wbps;
82 };
83
84 struct CGroupContext {
85 bool cpu_accounting;
86 bool io_accounting;
87 bool blockio_accounting;
88 bool memory_accounting;
89 bool tasks_accounting;
90
91 /* For unified hierarchy */
92 uint64_t io_weight;
93 uint64_t startup_io_weight;
94 LIST_HEAD(CGroupIODeviceWeight, io_device_weights);
95 LIST_HEAD(CGroupIODeviceLimit, io_device_limits);
96
97 uint64_t memory_low;
98 uint64_t memory_high;
99 uint64_t memory_max;
100
101 /* For legacy hierarchies */
102 uint64_t cpu_shares;
103 uint64_t startup_cpu_shares;
104 usec_t cpu_quota_per_sec_usec;
105
106 uint64_t blockio_weight;
107 uint64_t startup_blockio_weight;
108 LIST_HEAD(CGroupBlockIODeviceWeight, blockio_device_weights);
109 LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
110
111 uint64_t memory_limit;
112
113 CGroupDevicePolicy device_policy;
114 LIST_HEAD(CGroupDeviceAllow, device_allow);
115
116 /* Common */
117 uint64_t tasks_max;
118
119 bool delegate;
120 };
121
122 #include "unit.h"
123
124 void cgroup_context_init(CGroupContext *c);
125 void cgroup_context_done(CGroupContext *c);
126 void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix);
127
128 CGroupMask cgroup_context_get_mask(CGroupContext *c);
129
130 void cgroup_context_free_device_allow(CGroupContext *c, CGroupDeviceAllow *a);
131 void cgroup_context_free_io_device_weight(CGroupContext *c, CGroupIODeviceWeight *w);
132 void cgroup_context_free_io_device_limit(CGroupContext *c, CGroupIODeviceLimit *l);
133 void cgroup_context_free_blockio_device_weight(CGroupContext *c, CGroupBlockIODeviceWeight *w);
134 void cgroup_context_free_blockio_device_bandwidth(CGroupContext *c, CGroupBlockIODeviceBandwidth *b);
135
136 CGroupMask unit_get_own_mask(Unit *u);
137 CGroupMask unit_get_siblings_mask(Unit *u);
138 CGroupMask unit_get_members_mask(Unit *u);
139 CGroupMask unit_get_subtree_mask(Unit *u);
140
141 CGroupMask unit_get_target_mask(Unit *u);
142 CGroupMask unit_get_enable_mask(Unit *u);
143
144 void unit_update_cgroup_members_masks(Unit *u);
145
146 char *unit_default_cgroup_path(Unit *u);
147 int unit_set_cgroup_path(Unit *u, const char *path);
148
149 int unit_realize_cgroup(Unit *u);
150 void unit_release_cgroup(Unit *u);
151 void unit_prune_cgroup(Unit *u);
152 int unit_watch_cgroup(Unit *u);
153
154 int unit_attach_pids_to_cgroup(Unit *u);
155
156 int manager_setup_cgroup(Manager *m);
157 void manager_shutdown_cgroup(Manager *m, bool delete);
158
159 unsigned manager_dispatch_cgroup_queue(Manager *m);
160
161 Unit *manager_get_unit_by_cgroup(Manager *m, const char *cgroup);
162 Unit *manager_get_unit_by_pid_cgroup(Manager *m, pid_t pid);
163 Unit* manager_get_unit_by_pid(Manager *m, pid_t pid);
164
165 int unit_search_main_pid(Unit *u, pid_t *ret);
166 int unit_watch_all_pids(Unit *u);
167
168 int unit_get_memory_current(Unit *u, uint64_t *ret);
169 int unit_get_tasks_current(Unit *u, uint64_t *ret);
170 int unit_get_cpu_usage(Unit *u, nsec_t *ret);
171 int unit_reset_cpu_usage(Unit *u);
172
173 bool unit_cgroup_delegate(Unit *u);
174
175 int unit_notify_cgroup_empty(Unit *u);
176 int manager_notify_cgroup_empty(Manager *m, const char *group);
177
178 void unit_invalidate_cgroup(Unit *u, CGroupMask m);
179
180 void manager_invalidate_startup_units(Manager *m);
181
182 const char* cgroup_device_policy_to_string(CGroupDevicePolicy i) _const_;
183 CGroupDevicePolicy cgroup_device_policy_from_string(const char *s) _pure_;