Debian Codegraph @

man/sd_event_add_memory_pressure.xml

raw
1 <?xml version='1.0'?>
2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
5
6 <refentry id="sd_event_add_memory_pressure" xmlns:xi="http://www.w3.org/2001/XInclude">
7
8 <refentryinfo>
9 <title>sd_event_add_memory_pressure</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>sd_event_add_memory_pressure</refentrytitle>
15 <manvolnum>3</manvolnum>
16 </refmeta>
17
18 <refnamediv>
19 <refname>sd_event_add_memory_pressure</refname>
20 <refname>sd_event_source_set_memory_pressure_type</refname>
21 <refname>sd_event_source_set_memory_pressure_period</refname>
22 <refname>sd_event_trim_memory</refname>
23
24 <refname>sd_event_add_cpu_pressure</refname>
25 <refname>sd_event_source_set_cpu_pressure_type</refname>
26 <refname>sd_event_source_set_cpu_pressure_period</refname>
27
28 <refname>sd_event_add_io_pressure</refname>
29 <refname>sd_event_source_set_io_pressure_type</refname>
30 <refname>sd_event_source_set_io_pressure_period</refname>
31
32 <refpurpose>Add and configure an event source for memory, CPU, or IO pressure notifications</refpurpose>
33 </refnamediv>
34
35 <refsynopsisdiv>
36 <funcsynopsis>
37 <funcsynopsisinfo>#include &lt;systemd/sd-event.h&gt;</funcsynopsisinfo>
38
39 <funcsynopsisinfo><token>typedef</token> struct sd_event_source sd_event_source;</funcsynopsisinfo>
40
41 <funcprototype>
42 <funcdef>int <function>sd_event_add_memory_pressure</function></funcdef>
43 <paramdef>sd_event *<parameter>event</parameter></paramdef>
44 <paramdef>sd_event_source **<parameter>ret_source</parameter></paramdef>
45 <paramdef>sd_event_handler_t <parameter>handler</parameter></paramdef>
46 <paramdef>void *<parameter>userdata</parameter></paramdef>
47 </funcprototype>
48
49 <funcprototype>
50 <funcdef>int <function>sd_event_source_set_memory_pressure_type</function></funcdef>
51 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
52 <paramdef>const char *<parameter>type</parameter></paramdef>
53 </funcprototype>
54
55 <funcprototype>
56 <funcdef>int <function>sd_event_source_set_memory_pressure_period</function></funcdef>
57 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
58 <paramdef>uint64_t <parameter>threshold_usec</parameter></paramdef>
59 <paramdef>uint64_t <parameter>window_usec</parameter></paramdef>
60 </funcprototype>
61
62 <funcprototype>
63 <funcdef>int <function>sd_event_add_cpu_pressure</function></funcdef>
64 <paramdef>sd_event *<parameter>event</parameter></paramdef>
65 <paramdef>sd_event_source **<parameter>ret_source</parameter></paramdef>
66 <paramdef>sd_event_handler_t <parameter>handler</parameter></paramdef>
67 <paramdef>void *<parameter>userdata</parameter></paramdef>
68 </funcprototype>
69
70 <funcprototype>
71 <funcdef>int <function>sd_event_source_set_cpu_pressure_type</function></funcdef>
72 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
73 <paramdef>const char *<parameter>type</parameter></paramdef>
74 </funcprototype>
75
76 <funcprototype>
77 <funcdef>int <function>sd_event_source_set_cpu_pressure_period</function></funcdef>
78 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
79 <paramdef>uint64_t <parameter>threshold_usec</parameter></paramdef>
80 <paramdef>uint64_t <parameter>window_usec</parameter></paramdef>
81 </funcprototype>
82
83 <funcprototype>
84 <funcdef>int <function>sd_event_add_io_pressure</function></funcdef>
85 <paramdef>sd_event *<parameter>event</parameter></paramdef>
86 <paramdef>sd_event_source **<parameter>ret_source</parameter></paramdef>
87 <paramdef>sd_event_handler_t <parameter>handler</parameter></paramdef>
88 <paramdef>void *<parameter>userdata</parameter></paramdef>
89 </funcprototype>
90
91 <funcprototype>
92 <funcdef>int <function>sd_event_source_set_io_pressure_type</function></funcdef>
93 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
94 <paramdef>const char *<parameter>type</parameter></paramdef>
95 </funcprototype>
96
97 <funcprototype>
98 <funcdef>int <function>sd_event_source_set_io_pressure_period</function></funcdef>
99 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
100 <paramdef>uint64_t <parameter>threshold_usec</parameter></paramdef>
101 <paramdef>uint64_t <parameter>window_usec</parameter></paramdef>
102 </funcprototype>
103
104 <funcprototype>
105 <funcdef>int <function>sd_event_trim_memory</function></funcdef>
106 <paramdef>void</paramdef>
107 </funcprototype>
108 </funcsynopsis>
109 </refsynopsisdiv>
110
111 <refsect1>
112 <title>Description</title>
113
114 <para><function>sd_event_add_memory_pressure()</function> adds a new event source that is triggered
115 whenever memory pressure is seen. Similarly,
116 <function>sd_event_add_cpu_pressure()</function> and <function>sd_event_add_io_pressure()</function> add
117 new event sources that are triggered whenever CPU or IO pressure is seen, respectively. This functionality
118 is built around the Linux kernel's <ulink
119 url="https://docs.kernel.org/accounting/psi.html">Pressure Stall Information (PSI)</ulink> logic.</para>
120
121 <para>These functions expect an event loop object as first parameter, and return the allocated event source
122 object in the second parameter, on success. The <parameter>handler</parameter> parameter is a function to
123 call when pressure is seen, or <constant>NULL</constant>. The handler function will be passed the
124 <parameter>userdata</parameter> pointer, which may be chosen freely by the caller. The handler may return
125 negative to signal an error (see below), other return values are ignored. If
126 <parameter>handler</parameter> is <constant>NULL</constant>, a default handler is used. For
127 <function>sd_event_add_memory_pressure()</function>, the default handler compacts allocation caches
128 maintained by <filename>libsystemd</filename> as well as glibc (via <citerefentry
129 project='man-pages'><refentrytitle>malloc_trim</refentrytitle><manvolnum>3</manvolnum></citerefentry>).
130 For <function>sd_event_add_cpu_pressure()</function> and
131 <function>sd_event_add_io_pressure()</function>, the default handler is a no-op. It is recommended to
132 pass a custom handler for CPU and IO pressure to take meaningful action when pressure is
133 detected.</para>
134
135 <para>To destroy an event source object use
136 <citerefentry><refentrytitle>sd_event_source_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
137 but note that the event source is only removed from the event loop when all references to the event
138 source are dropped. To make sure an event source does not fire anymore, even if it is still referenced,
139 disable the event source using
140 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
141 with <constant>SD_EVENT_OFF</constant>.</para>
142
143 <para>If the second parameter of <function>sd_event_add_memory_pressure()</function>,
144 <function>sd_event_add_cpu_pressure()</function>, or <function>sd_event_add_io_pressure()</function> is
145 <constant>NULL</constant> no reference to the event source object is returned. In this case, the event
146 source is considered "floating", and will be destroyed implicitly when the event loop itself is
147 destroyed.</para>
148
149 <para>The memory pressure event source will fire according to the following logic:</para>
150
151 <orderedlist>
152 <listitem><para>If the
153 <varname>$MEMORY_PRESSURE_WATCH</varname>/<varname>$MEMORY_PRESSURE_WRITE</varname> environment
154 variables are set at the time the event source is established, it will watch the file, FIFO or AF_UNIX
155 socket specified via <varname>$MEMORY_PRESSURE_WATCH</varname> (which must contain an absolute path
156 name) for <constant>POLLPRI</constant> (in case it is a regular file) or <constant>POLLIN</constant>
157 events (otherwise). After opening the inode, it will write the (decoded) Base64 data provided via
158 <varname>$MEMORY_PRESSURE_WRITE</varname> into it before it starts polling on it (the variable may be
159 unset in which case this is skipped). Typically, if used, <varname>$MEMORY_PRESSURE_WATCH</varname>
160 will contain a path such as <filename>/proc/pressure/memory</filename> or a path to a specific
161 <filename>memory.pressure</filename> file in the control group file system
162 (cgroupfs).</para></listitem>
163
164 <listitem><para>If these environment variables are not set, the local PSI interface file
165 <filename>memory.pressure</filename> of the control group the invoking process is running in is
166 used.</para></listitem>
167
168 <listitem><para>If that file does not exist, the system-wide PSI interface file
169 <filename>/proc/pressure/memory</filename> is watched instead.</para></listitem>
170 </orderedlist>
171
172 <para>The CPU pressure event source follows the same logic, but uses the
173 <varname>$CPU_PRESSURE_WATCH</varname>/<varname>$CPU_PRESSURE_WRITE</varname> environment variables,
174 the <filename>cpu.pressure</filename> cgroup file, and the system-wide PSI interface file
175 <filename>/proc/pressure/cpu</filename> instead. Note that <filename>/proc/pressure/cpu</filename> only
176 provides the <literal>some</literal> line, not the <literal>full</literal> line, so only
177 <literal>some</literal> is valid when watching at the system level.</para>
178
179 <para>The IO pressure event source follows the same logic, but uses the
180 <varname>$IO_PRESSURE_WATCH</varname>/<varname>$IO_PRESSURE_WRITE</varname> environment variables,
181 the <filename>io.pressure</filename> cgroup file, and the system-wide PSI interface file
182 <filename>/proc/pressure/io</filename> instead.</para>
183
184 <para>Or in other words: preferably any explicit configuration passed in by an invoking service manager
185 (or similar) is used as notification source, before falling back to local notifications of the service,
186 and finally to global notifications of the system.</para>
187
188 <para>Well-behaving services and applications are recommended to react to memory pressure events by
189 executing one or more of the following operations, in order to ensure optimal behaviour even on loaded
190 and resource-constrained systems:</para>
191
192 <itemizedlist>
193 <listitem><para>Release allocation caches such as <function>malloc_trim()</function> or similar, both
194 implemented in the libraries consumed by the program and in private allocation caches of the program
195 itself.</para></listitem>
196
197 <listitem><para>Release any other form of in-memory caches that can easily be recovered if
198 needed (e.g. browser caches).</para></listitem>
199
200 <listitem><para>Terminate idle worker threads or processes, or similar.</para></listitem>
201
202 <listitem><para>Even exit entirely from the program if it is idle and can be automatically started when
203 needed (for example via socket or bus activation).</para></listitem>
204 </itemizedlist>
205
206 <para>Any of the suggested operations should help easing memory pressure situations and allowing the
207 system to make progress by reclaiming the memory for other purposes.</para>
208
209 <para>This event source typically fires on memory pressure stalls, i.e. when operational latency above a
210 configured threshold already has been seen. This should be taken into consideration when discussing
211 whether later latency to re-acquire any released resources is acceptable: it is usually more important to
212 think of the latencies that already happened than those coming up in future.</para>
213
214 <para>The <function>sd_event_source_set_memory_pressure_type()</function> and
215 <function>sd_event_source_set_memory_pressure_period()</function> functions can be used to fine-tune the
216 PSI parameters for memory pressure notifications. The former takes either <literal>some</literal>,
217 <literal>full</literal> as second parameter, the latter takes threshold and period times in microseconds
218 as parameters. For details about these three parameters see the PSI documentation. Note that these two
219 calls must be invoked immediately after allocating the event source, as they must be configured before
220 polling begins. Also note that these calls will fail if memory pressure parameterization has been passed
221 in via the <varname>$MEMORY_PRESSURE_WATCH</varname>/<varname>$MEMORY_PRESSURE_WRITE</varname>
222 environment variables (or in other words: configuration supplied by a service manager wins over internal
223 settings).</para>
224
225 <para>Similarly, <function>sd_event_source_set_cpu_pressure_type()</function> and
226 <function>sd_event_source_set_cpu_pressure_period()</function> can be used to fine-tune the PSI
227 parameters for CPU pressure notifications, and
228 <function>sd_event_source_set_io_pressure_type()</function> and
229 <function>sd_event_source_set_io_pressure_period()</function> can be used to fine-tune the PSI
230 parameters for IO pressure notifications. They work identically to their memory pressure counterparts.
231 The type parameter takes either <literal>some</literal> or <literal>full</literal>, and the period
232 function takes threshold and period times in microseconds. The same constraints apply: these calls must
233 be invoked immediately after allocating the event source, and will fail if pressure parameterization
234 has been passed in via the corresponding
235 <varname>$*_PRESSURE_WATCH</varname>/<varname>$*_PRESSURE_WRITE</varname> environment
236 variables.</para>
237
238 <para>The <function>sd_event_trim_memory()</function> function releases various internal allocation
239 caches maintained by <filename>libsystemd</filename> and then invokes glibc's <citerefentry
240 project='man-pages'><refentrytitle>malloc_trim</refentrytitle><manvolnum>3</manvolnum></citerefentry>. This
241 makes the operation executed when the handler function parameter of
242 <function>sd_event_add_memory_pressure</function> is passed as <constant>NULL</constant> directly
243 accessible for invocation at any time (see above). This function will log a structured log message at
244 <constant>LOG_DEBUG</constant> level (with message ID f9b0be465ad540d0850ad32172d57c21) about the memory
245 pressure operation.</para>
246
247 <para>For further details see <ulink url="https://systemd.io/PRESSURE">Resource Pressure Handling in
248 systemd</ulink>.</para>
249 </refsect1>
250
251 <refsect1>
252 <title>Return Value</title>
253
254 <para>On success, these functions return 0 or a positive
255 integer. On failure, they return a negative errno-style error
256 code.</para>
257
258 <refsect2>
259 <title>Errors</title>
260
261 <para>Returned errors may indicate the following problems:</para>
262
263 <variablelist>
264 <varlistentry>
265 <term><constant>-ENOMEM</constant></term>
266
267 <listitem><para>Not enough memory to allocate an object.</para>
268
269 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
270 </varlistentry>
271
272 <varlistentry>
273 <term><constant>-EINVAL</constant></term>
274
275 <listitem><para>An invalid argument has been passed.</para>
276
277 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
278 </varlistentry>
279
280 <varlistentry>
281 <term><constant>-EHOSTDOWN</constant></term>
282
283 <listitem><para>The <varname>$MEMORY_PRESSURE_WATCH</varname>,
284 <varname>$CPU_PRESSURE_WATCH</varname>, or <varname>$IO_PRESSURE_WATCH</varname> variable has been
285 set to the literal string <filename>/dev/null</filename>, in order to explicitly disable pressure
286 handling.</para>
287
288 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
289 </varlistentry>
290
291 <varlistentry>
292 <term><constant>-EBADMSG</constant></term>
293
294 <listitem><para>The <varname>$MEMORY_PRESSURE_WATCH</varname>,
295 <varname>$CPU_PRESSURE_WATCH</varname>, or <varname>$IO_PRESSURE_WATCH</varname> variable has been
296 set to an invalid string, for example a relative rather than an absolute path.</para>
297
298 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
299 </varlistentry>
300
301 <varlistentry>
302 <term><constant>-ENOTTY</constant></term>
303
304 <listitem><para>The <varname>$MEMORY_PRESSURE_WATCH</varname>,
305 <varname>$CPU_PRESSURE_WATCH</varname>, or <varname>$IO_PRESSURE_WATCH</varname> variable points
306 to a regular file outside of the procfs or cgroupfs file systems.</para>
307
308 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
309 </varlistentry>
310
311 <varlistentry>
312 <term><constant>-EOPNOTSUPP</constant></term>
313
314 <listitem><para>No configuration via <varname>$MEMORY_PRESSURE_WATCH</varname>,
315 <varname>$CPU_PRESSURE_WATCH</varname>, or <varname>$IO_PRESSURE_WATCH</varname> has been specified
316 and the local kernel does not support the PSI interface.</para>
317
318 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
319 </varlistentry>
320
321 <varlistentry>
322 <term><constant>-EBUSY</constant></term>
323
324 <listitem><para>This is returned by <function>sd_event_source_set_memory_pressure_type()</function>,
325 <function>sd_event_source_set_memory_pressure_period()</function>,
326 <function>sd_event_source_set_cpu_pressure_type()</function>,
327 <function>sd_event_source_set_cpu_pressure_period()</function>,
328 <function>sd_event_source_set_io_pressure_type()</function>,
329 and <function>sd_event_source_set_io_pressure_period()</function> if invoked on event sources
330 at a time later than immediately after allocating them.</para>
331
332 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
333 </varlistentry>
334
335 <varlistentry>
336 <term><constant>-ESTALE</constant></term>
337
338 <listitem><para>The event loop is already terminated.</para>
339
340 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
341 </varlistentry>
342
343 <varlistentry>
344 <term><constant>-ECHILD</constant></term>
345
346 <listitem><para>The event loop has been created in a different process, library or module instance.</para>
347
348 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
349 </varlistentry>
350
351 <varlistentry>
352 <term><constant>-EDOM</constant></term>
353
354 <listitem><para>The passed event source is not a signal event source.</para>
355
356 <xi:include href="version-info.xml" xpointer="v254"/></listitem>
357 </varlistentry>
358
359 </variablelist>
360 </refsect2>
361 </refsect1>
362
363 <xi:include href="libsystemd-pkgconfig.xml" />
364
365 <refsect1>
366 <title>History</title>
367 <para><function>sd_event_add_memory_pressure()</function>,
368 <function>sd_event_source_set_memory_pressure_type()</function>,
369 <function>sd_event_source_set_memory_pressure_period()</function>, and
370 <function>sd_event_trim_memory()</function> were added in version 254.</para>
371 <para><function>sd_event_add_cpu_pressure()</function>,
372 <function>sd_event_source_set_cpu_pressure_type()</function>,
373 <function>sd_event_source_set_cpu_pressure_period()</function>,
374 <function>sd_event_add_io_pressure()</function>,
375 <function>sd_event_source_set_io_pressure_type()</function>, and
376 <function>sd_event_source_set_io_pressure_period()</function> were added in version 261.</para>
377 </refsect1>
378
379 <refsect1>
380 <title>See Also</title>
381
382 <para><simplelist type="inline">
383 <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
384 <member><citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
385 <member><citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
386 <member><citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
387 <member><citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
388 <member><citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
389 <member><citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
390 <member><citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
391 <member><citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
392 <member><citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
393 <member><citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
394 <member><citerefentry><refentrytitle>sd_event_source_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
395 </simplelist></para>
396 </refsect1>
397
398 </refentry>