Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright 2025 Red Hat, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : */
20 : :
21 : : #include "config.h"
22 : :
23 : : #include "gcancellable.h"
24 : : #include "gdbusnamewatching.h"
25 : : #include "gdbusproxy.h"
26 : : #include "gfile.h"
27 : : #include "ginitable.h"
28 : : #include "gioerror.h"
29 : : #include "giomodule-priv.h"
30 : : #include "glib/glib-private.h"
31 : : #include "glib/gstdio.h"
32 : : #include "glibintl.h"
33 : : #include "gmemorymonitor.h"
34 : : #include "gmemorymonitorbase.h"
35 : : #include "gmemorymonitorpsi.h"
36 : : #include "gsocket.h"
37 : : #include "gunixsocketaddress.h"
38 : :
39 : : #include <errno.h>
40 : : #include <fcntl.h>
41 : : #include <sys/stat.h>
42 : : #include <unistd.h>
43 : :
44 : : /**
45 : : * GMemoryMonitorPsi:
46 : : *
47 : : * A Linux [iface@Gio.MemoryMonitor] which uses the kernel
48 : : * [pressure stall information](https://www.kernel.org/doc/html/latest/accounting/psi.html) (PSI).
49 : : *
50 : : * When it receives a PSI event, it emits
51 : : * [signal@Gio.MemoryMonitor::low-memory-warning] with an appropriate warning
52 : : * level.
53 : : *
54 : : * Since: 2.86
55 : : */
56 : :
57 : : /* Unprivileged users can also create monitors, with
58 : : * the only limitation that the window size must be a
59 : : * `multiple of 2s`, in order to prevent excessive resource usage.
60 : : * see: https://www.kernel.org/doc/html/latest/accounting/psi.html*/
61 : : #define PSI_WINDOW_SEC 2
62 : :
63 : : typedef enum {
64 : : PROP_PROC_PATH = 1,
65 : : } GMemoryMonitorPsiProperty;
66 : :
67 : : typedef enum
68 : : {
69 : : MEMORY_PRESSURE_MONITOR_TRIGGER_SOME,
70 : : MEMORY_PRESSURE_MONITOR_TRIGGER_FULL,
71 : : MEMORY_PRESSURE_MONITOR_TRIGGER_MFD
72 : : } MemoryPressureMonitorTriggerType;
73 : :
74 : : /* Each trigger here results in an open fd for the lifetime
75 : : * of the `GMemoryMonitor`, so don’t add too many */
76 : : static const struct
77 : : {
78 : : MemoryPressureMonitorTriggerType trigger_type;
79 : : int threshold_ms;
80 : : } triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_COUNT] = {
81 : : { MEMORY_PRESSURE_MONITOR_TRIGGER_SOME, 70 }, /* 70ms out of 2sec for partial stall */
82 : : { MEMORY_PRESSURE_MONITOR_TRIGGER_SOME, 100 }, /* 100ms out of 2sec for partial stall */
83 : : { MEMORY_PRESSURE_MONITOR_TRIGGER_FULL, 100 }, /* 100ms out of 2sec for complete stall */
84 : : };
85 : :
86 : : typedef struct
87 : : {
88 : : GSource source;
89 : : GPollFD *pollfd;
90 : : GMemoryMonitorLowMemoryLevel level_type;
91 : : GWeakRef monitor_weak;
92 : : } MemoryMonitorSource;
93 : :
94 : : typedef gboolean (*MemoryMonitorCallbackFunc) (GMemoryMonitorPsi *monitor,
95 : : GMemoryMonitorLowMemoryLevel level_type,
96 : : void *user_data);
97 : :
98 : : #define G_MEMORY_MONITOR_PSI_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))
99 : :
100 : : static void g_memory_monitor_psi_iface_init (GMemoryMonitorInterface *iface);
101 : : static void g_memory_monitor_psi_initable_iface_init (GInitableIface *iface);
102 : :
103 : : struct _GMemoryMonitorPsi
104 : : {
105 : : GMemoryMonitorBase parent_instance;
106 : : GMainContext *worker; /* (unowned) */
107 : : GSource *triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_COUNT]; /* (owned) (nullable) */
108 : :
109 : : char *cg_path;
110 : : char *proc_path;
111 : :
112 : : gboolean mem_pressure_watch_override;
113 : : char *mem_pressure_write;
114 : : size_t mem_pressure_write_len;
115 : :
116 : : /* if the cg_path is a unix socket */
117 : : GSocket *socket;
118 : : GSocketAddress *socket_addr;
119 : :
120 : : gboolean proc_override;
121 : : };
122 : :
123 : 325 : G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorPsi, g_memory_monitor_psi, G_TYPE_MEMORY_MONITOR_BASE,
124 : : G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
125 : : g_memory_monitor_psi_initable_iface_init)
126 : : G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
127 : : g_memory_monitor_psi_iface_init)
128 : : _g_io_modules_ensure_extension_points_registered ();
129 : : g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
130 : : g_define_type_id,
131 : : "psi",
132 : : 20))
133 : :
134 : : static void
135 : 7 : g_memory_monitor_psi_init (GMemoryMonitorPsi *psi)
136 : : {
137 : 7 : }
138 : :
139 : : static void
140 : 7 : g_memory_monitor_psi_set_property (GObject *object,
141 : : guint prop_id,
142 : : const GValue *value,
143 : : GParamSpec *pspec)
144 : : {
145 : 7 : GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (object);
146 : :
147 : 7 : switch ((GMemoryMonitorPsiProperty) prop_id)
148 : : {
149 : 7 : case PROP_PROC_PATH:
150 : : /* Construct only */
151 : 7 : g_assert (monitor->proc_path == NULL);
152 : 7 : monitor->proc_path = g_value_dup_string (value);
153 : 7 : if (monitor->proc_path != NULL)
154 : 6 : monitor->proc_override = TRUE;
155 : 7 : break;
156 : 0 : default:
157 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
158 : 0 : break;
159 : : }
160 : 7 : }
161 : :
162 : : static void
163 : 0 : g_memory_monitor_psi_get_property (GObject *object,
164 : : guint prop_id,
165 : : GValue *value,
166 : : GParamSpec *pspec)
167 : : {
168 : 0 : GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (object);
169 : :
170 : 0 : switch ((GMemoryMonitorPsiProperty) prop_id)
171 : : {
172 : 0 : case PROP_PROC_PATH:
173 : 0 : g_value_set_string (value, monitor->proc_override ? monitor->proc_path : NULL);
174 : 0 : break;
175 : 0 : default:
176 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177 : 0 : break;
178 : : }
179 : 0 : }
180 : :
181 : : static gboolean
182 : 9 : g_memory_monitor_low_trigger_cb (GMemoryMonitorPsi *monitor,
183 : : GMemoryMonitorLowMemoryLevel level_type,
184 : : void *user_data)
185 : : {
186 : : gdouble mem_ratio;
187 : :
188 : : /* Should be executed in the worker context */
189 : 9 : g_assert (g_main_context_is_owner (monitor->worker));
190 : :
191 : 9 : mem_ratio = g_memory_monitor_base_query_mem_ratio ();
192 : :
193 : : /* if the test is running, skip memory ratio test */
194 : 9 : if (!monitor->proc_override)
195 : : {
196 : : /* if mem free ratio > 0.5, don't signal */
197 : 0 : if (mem_ratio < 0.0)
198 : 0 : return G_SOURCE_REMOVE;
199 : 0 : if (mem_ratio > 0.5)
200 : 0 : return G_SOURCE_CONTINUE;
201 : : }
202 : :
203 : 9 : g_memory_monitor_base_send_event_to_user (G_MEMORY_MONITOR_BASE (monitor), level_type);
204 : :
205 : 9 : return G_SOURCE_CONTINUE;
206 : : }
207 : :
208 : : static gboolean
209 : 12 : event_check (GSource *source)
210 : : {
211 : 12 : MemoryMonitorSource *ev_source = (MemoryMonitorSource *) source;
212 : :
213 : 12 : if (ev_source->pollfd->revents)
214 : 12 : return G_SOURCE_CONTINUE;
215 : :
216 : 0 : return G_SOURCE_REMOVE;
217 : : }
218 : :
219 : : static gboolean
220 : 11 : event_dispatch (GSource *source,
221 : : GSourceFunc callback,
222 : : gpointer user_data)
223 : : {
224 : 11 : MemoryMonitorSource *ev_source = (MemoryMonitorSource *) source;
225 : 11 : GMemoryMonitorPsi *monitor = NULL;
226 : :
227 : 11 : monitor = g_weak_ref_get (&ev_source->monitor_weak);
228 : 11 : if (monitor == NULL)
229 : 2 : return G_SOURCE_REMOVE;
230 : :
231 : 9 : if (monitor->proc_override)
232 : : {
233 : 9 : if (!(g_source_query_unix_fd (source, ev_source->pollfd) & G_IO_IN))
234 : : {
235 : 0 : g_object_unref (monitor);
236 : 0 : return G_SOURCE_CONTINUE;
237 : : }
238 : : }
239 : : else
240 : : {
241 : 0 : if (!(g_source_query_unix_fd (source, ev_source->pollfd) & G_IO_PRI))
242 : : {
243 : 0 : g_object_unref (monitor);
244 : 0 : return G_SOURCE_CONTINUE;
245 : : }
246 : : }
247 : :
248 : 9 : if (callback)
249 : 9 : ((MemoryMonitorCallbackFunc) callback) (monitor, ev_source->level_type, user_data);
250 : :
251 : 9 : g_object_unref (monitor);
252 : :
253 : 9 : return G_SOURCE_CONTINUE;
254 : : }
255 : :
256 : : static void
257 : 8 : event_finalize (GSource *source)
258 : : {
259 : 8 : MemoryMonitorSource *ev_source = (MemoryMonitorSource *) source;
260 : :
261 : 8 : g_weak_ref_clear (&ev_source->monitor_weak);
262 : 8 : }
263 : :
264 : : static GSourceFuncs memory_monitor_event_funcs = {
265 : : .check = event_check,
266 : : .dispatch = event_dispatch,
267 : : .finalize = event_finalize,
268 : : };
269 : :
270 : : static GSource *
271 : 8 : g_memory_monitor_create_source (GMemoryMonitorPsi *monitor,
272 : : int fd,
273 : : GMemoryMonitorLowMemoryLevel level_type,
274 : : gboolean is_path_override)
275 : : {
276 : : MemoryMonitorSource *source;
277 : :
278 : 8 : source = (MemoryMonitorSource *) g_source_new (&memory_monitor_event_funcs, sizeof (MemoryMonitorSource));
279 : 8 : if (is_path_override)
280 : 8 : source->pollfd = g_source_add_unix_fd ((GSource *) source, fd, G_IO_IN | G_IO_ERR);
281 : : else
282 : 0 : source->pollfd = g_source_add_unix_fd ((GSource *) source, fd, G_IO_PRI | G_IO_ERR);
283 : 8 : source->level_type = level_type;
284 : 8 : g_weak_ref_init (&source->monitor_weak, monitor);
285 : :
286 : 8 : return (GSource *) source;
287 : : }
288 : :
289 : : static gboolean
290 : 7 : g_memory_monitor_psi_calculate_mem_pressure_path (GMemoryMonitorPsi *monitor,
291 : : GError **error)
292 : : {
293 : : pid_t pid;
294 : 7 : gchar *path_read = NULL;
295 : 7 : gchar *replacement = NULL;
296 : :
297 : 7 : if (!monitor->proc_override)
298 : : {
299 : 1 : pid = getpid ();
300 : 1 : monitor->proc_path = g_strdup_printf ("/proc/%d/cgroup", pid);
301 : : }
302 : :
303 : : /* if $MEMORY_PRESSURE_WATCH is set, use the user defined PSI path and return TRUE */
304 : 7 : if (monitor->cg_path && monitor->mem_pressure_watch_override)
305 : 5 : return TRUE;
306 : :
307 : 2 : if (!g_file_get_contents (monitor->proc_path, &path_read, NULL, error))
308 : : {
309 : 0 : g_free (path_read);
310 : 0 : return FALSE;
311 : : }
312 : :
313 : : /* cgroupv2 is only supported and the format is shown as follows:
314 : : * ex: 0::/user.slice/user-0.slice/session-c3.scope */
315 : 2 : if (!g_str_has_prefix (path_read, "0::"))
316 : : {
317 : 0 : g_debug ("Unsupported cgroup path information.");
318 : 0 : g_free (path_read);
319 : 0 : return FALSE;
320 : : }
321 : :
322 : : /* drop "0::" */
323 : 2 : replacement = g_strdup (path_read + strlen ("0::"));
324 : 2 : g_free (path_read);
325 : 2 : if (replacement == NULL)
326 : : {
327 : 0 : g_debug ("Unsupported cgroup path format.");
328 : 0 : return FALSE;
329 : : }
330 : :
331 : 2 : replacement = g_strstrip (replacement);
332 : :
333 : 2 : if (monitor->proc_override)
334 : : {
335 : 1 : monitor->cg_path = g_steal_pointer (&replacement);
336 : 1 : return TRUE;
337 : : }
338 : :
339 : 1 : monitor->cg_path = g_build_filename ("/sys/fs/cgroup", replacement, "memory.pressure", NULL);
340 : 1 : g_debug ("cgroup path is %s", monitor->cg_path);
341 : :
342 : 1 : g_free (replacement);
343 : 1 : return g_file_test (monitor->cg_path, G_FILE_TEST_EXISTS);
344 : : }
345 : :
346 : : static gboolean
347 : 9 : g_memory_monitor_psi_is_socket (const gchar *path)
348 : : {
349 : : GStatBuf statbuf;
350 : :
351 : 9 : if (g_stat (path, &statbuf) != 0)
352 : 0 : return FALSE;
353 : :
354 : 9 : if ((statbuf.st_mode & S_IFMT) == S_IFSOCK)
355 : 1 : return TRUE;
356 : :
357 : 8 : return FALSE;
358 : : }
359 : :
360 : : static int
361 : 1 : g_memory_monitor_psi_new_socket_fd (GMemoryMonitorPsi *monitor,
362 : : const gchar *trigger,
363 : : size_t len,
364 : : GError **error)
365 : : {
366 : 1 : int fd = -1;
367 : :
368 : 1 : monitor->socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, 0, error);
369 : 1 : if (!monitor->socket)
370 : : {
371 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Socket creation failed");
372 : 0 : return -1;
373 : : }
374 : :
375 : 1 : monitor->socket_addr = g_unix_socket_address_new (monitor->cg_path);
376 : 1 : if (!g_socket_connect (monitor->socket, monitor->socket_addr, NULL, error))
377 : : {
378 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Connection failed");
379 : 0 : g_socket_close (monitor->socket, NULL);
380 : 0 : g_clear_object (&monitor->socket);
381 : 0 : g_clear_object (&monitor->socket_addr);
382 : 0 : return -1;
383 : : }
384 : :
385 : 1 : fd = g_socket_get_fd (monitor->socket);
386 : 1 : g_assert (fd >= 0);
387 : :
388 : 1 : return fd;
389 : : }
390 : :
391 : : static GSource *
392 : 9 : g_memory_monitor_psi_write (GMemoryMonitorPsi *monitor,
393 : : const gchar *trigger,
394 : : GMemoryMonitorLowMemoryLevel level_type,
395 : : size_t len,
396 : : GError **error)
397 : : {
398 : : GSource *source;
399 : : int fd;
400 : : size_t wlen;
401 : : int ret;
402 : :
403 : 9 : if (g_memory_monitor_psi_is_socket (monitor->cg_path))
404 : : {
405 : 1 : fd = g_memory_monitor_psi_new_socket_fd (monitor, trigger, len, error);
406 : 1 : if (fd < 0)
407 : 0 : return NULL;
408 : : }
409 : : else
410 : : {
411 : 8 : fd = g_open (monitor->cg_path, O_RDWR | O_NONBLOCK | O_CLOEXEC);
412 : 8 : if (fd < 0)
413 : : {
414 : 1 : int errsv = errno;
415 : 1 : g_debug ("Error on opening %s: %s", monitor->cg_path, g_strerror (errsv));
416 : 1 : g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
417 : : "Error on opening ‘%s’: %s", monitor->cg_path, g_strerror (errsv));
418 : 1 : return NULL;
419 : : }
420 : : }
421 : :
422 : 8 : errno = 0;
423 : :
424 : 8 : wlen = len;
425 : :
426 : 15 : while (wlen > 0)
427 : : {
428 : : int errsv;
429 : 7 : ret = write (fd, trigger, wlen);
430 : 7 : errsv = errno;
431 : 7 : if (ret < 0)
432 : : {
433 : 0 : if (errsv == EINTR)
434 : : {
435 : : /* interrupted by signal, retry */
436 : 0 : continue;
437 : : }
438 : : else
439 : : {
440 : 0 : g_set_error (error,
441 : : G_IO_ERROR, G_IO_ERROR_FAILED,
442 : : "Error on setting PSI configurations: %s",
443 : : g_strerror (errsv));
444 : 0 : if (monitor->socket != NULL)
445 : : {
446 : 0 : g_socket_close (monitor->socket, NULL);
447 : 0 : g_clear_object (&monitor->socket);
448 : 0 : g_clear_object (&monitor->socket_addr);
449 : : }
450 : : else
451 : : {
452 : 0 : close (fd);
453 : : }
454 : 0 : return NULL;
455 : : }
456 : : }
457 : 7 : wlen -= ret;
458 : : }
459 : :
460 : 8 : g_debug ("Create source for low memory level %d", level_type);
461 : 8 : source = g_memory_monitor_create_source (monitor, fd, level_type, monitor->proc_override);
462 : 8 : g_source_set_callback (source, G_SOURCE_FUNC (g_memory_monitor_low_trigger_cb), NULL, NULL);
463 : :
464 : 8 : return g_steal_pointer (&source);
465 : : }
466 : :
467 : : static GSource *
468 : 4 : g_memory_monitor_psi_setup_trigger (GMemoryMonitorPsi *monitor,
469 : : GMemoryMonitorLowMemoryLevel level_type,
470 : : int threshold_us,
471 : : int window_us,
472 : : GError **error)
473 : : {
474 : : GSource *source;
475 : 4 : gchar *trigger = NULL;
476 : :
477 : : /* The kernel PSI [1] trigger format is:
478 : : * <some|full> <stall amount in us> <time window in us>
479 : : * The “some” indicates the share of time in which at least some tasks are stalled on a given resource.
480 : : * The “full” indicates the share of time in which all non-idle tasks are stalled on a given resource simultaneously.
481 : : * "stall amount in us": The total stall time in us.
482 : : * "time window in us": The specific time window in us.
483 : : * e.g. writing "some 150000 1000000" would add 150ms threshold for partial memory stall measured within 1sec time window
484 : : *
485 : : * [1] https://docs.kernel.org/accounting/psi.html
486 : : */
487 : 4 : trigger = g_strdup_printf ("%s %d %d",
488 : 4 : (triggers[level_type].trigger_type == MEMORY_PRESSURE_MONITOR_TRIGGER_SOME) ? "some" : "full",
489 : : threshold_us,
490 : : window_us);
491 : :
492 : 4 : source = g_memory_monitor_psi_write (monitor, trigger, level_type, strlen (trigger) + 1, error);
493 : :
494 : 4 : g_free (trigger);
495 : :
496 : 4 : return g_steal_pointer (&source);
497 : : }
498 : :
499 : : static GSource *
500 : 5 : g_memory_monitor_psi_setup_trigger_data (GMemoryMonitorPsi *monitor,
501 : : const gchar *trigger,
502 : : GMemoryMonitorLowMemoryLevel level_type,
503 : : size_t len,
504 : : GError **error)
505 : : {
506 : 5 : return g_memory_monitor_psi_write (monitor, trigger, level_type, len, error);
507 : : }
508 : :
509 : : static gboolean
510 : 7 : g_memory_monitor_setup_psi (GMemoryMonitorPsi *monitor,
511 : : GError **error)
512 : : {
513 : 7 : if (!g_memory_monitor_psi_calculate_mem_pressure_path (monitor, error))
514 : 0 : return FALSE;
515 : :
516 : : /* If $MEMORY_PRESSURE_WRITE is set, write the user defined configurations to the
517 : : * user defined PSI path. */
518 : 7 : if (monitor->mem_pressure_watch_override)
519 : : {
520 : : /* The G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW trigger is used to monitor the user defined PSI event. */
521 : 10 : monitor->triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW] = g_memory_monitor_psi_setup_trigger_data (monitor,
522 : 5 : monitor->mem_pressure_write,
523 : : G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW,
524 : : monitor->mem_pressure_write_len,
525 : : error);
526 : 5 : if (monitor->triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW] == NULL)
527 : 0 : return FALSE;
528 : :
529 : 5 : return TRUE;
530 : : }
531 : :
532 : 5 : for (size_t i = 0; i < G_N_ELEMENTS (triggers); i++)
533 : : {
534 : : /* The user defined PSI is estimated per second and the unit is in micro second(us). */
535 : 8 : monitor->triggers[i] = g_memory_monitor_psi_setup_trigger (monitor,
536 : : i,
537 : 4 : triggers[i].threshold_ms * 1000,
538 : : PSI_WINDOW_SEC * 1000 * 1000,
539 : : error);
540 : 4 : if (monitor->triggers[i] == NULL)
541 : 1 : return FALSE;
542 : : }
543 : :
544 : 1 : return TRUE;
545 : : }
546 : :
547 : : static gboolean
548 : 7 : g_memory_monitor_psi_initable_init (GInitable *initable,
549 : : GCancellable *cancellable,
550 : : GError **error)
551 : : {
552 : 7 : const gchar *memory_pressure_watch = NULL;
553 : 7 : const gchar *memory_pressure_write = NULL;
554 : :
555 : 7 : GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (initable);
556 : :
557 : 7 : monitor->worker = GLIB_PRIVATE_CALL (g_get_worker_context) ();
558 : :
559 : : /* Skip if the process was executed as setuid */
560 : 7 : if (!GLIB_PRIVATE_CALL (g_check_setuid) ())
561 : : {
562 : : /* check environment variables MEMORY_PRESSURE_WATCH and MEMORY_PRESSURE_WRITE
563 : : * See https://systemd.io/MEMORY_PRESSURE/ for details */
564 : 7 : memory_pressure_watch = g_getenv ("MEMORY_PRESSURE_WATCH");
565 : 7 : memory_pressure_write = g_getenv ("MEMORY_PRESSURE_WRITE");
566 : : }
567 : :
568 : 7 : if (memory_pressure_watch)
569 : : {
570 : 5 : g_debug ("MEMORY_PRESSURE_WATCH is %s", memory_pressure_watch);
571 : 5 : if (g_file_test (memory_pressure_watch, G_FILE_TEST_EXISTS))
572 : : {
573 : 5 : monitor->cg_path = g_strdup (memory_pressure_watch);
574 : 5 : monitor->mem_pressure_watch_override = TRUE;
575 : : }
576 : : }
577 : :
578 : 7 : if (memory_pressure_write && monitor->mem_pressure_watch_override)
579 : 4 : monitor->mem_pressure_write = (gchar *) g_base64_decode (memory_pressure_write,
580 : 4 : &monitor->mem_pressure_write_len);
581 : :
582 : 7 : if (g_memory_monitor_setup_psi (monitor, error))
583 : : {
584 : : /* If $MEMORY_PRESSURE_WATCH is set, only watch the
585 : : * G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW trigger array index */
586 : 6 : if (monitor->mem_pressure_watch_override)
587 : : {
588 : 5 : g_source_attach (monitor->triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW], monitor->worker);
589 : : }
590 : : else
591 : : {
592 : 4 : for (size_t i = 0; i < G_N_ELEMENTS (monitor->triggers); i++)
593 : : {
594 : 3 : g_source_attach (monitor->triggers[i], monitor->worker);
595 : : }
596 : : }
597 : : }
598 : : else
599 : : {
600 : 1 : g_clear_error (error);
601 : 1 : g_debug ("PSI is not supported.");
602 : 1 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "PSI is not supported.");
603 : 1 : return FALSE;
604 : : }
605 : :
606 : 6 : return TRUE;
607 : : }
608 : :
609 : : static void
610 : 7 : g_memory_monitor_psi_finalize (GObject *object)
611 : : {
612 : 7 : GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (object);
613 : :
614 : 7 : g_free (monitor->cg_path);
615 : 7 : g_free (monitor->proc_path);
616 : 7 : g_free (monitor->mem_pressure_write);
617 : :
618 : 28 : for (size_t i = 0; i < G_N_ELEMENTS (monitor->triggers); i++)
619 : : {
620 : 21 : if (monitor->triggers[i] != NULL)
621 : : {
622 : 8 : g_source_destroy (monitor->triggers[i]);
623 : 8 : g_source_unref (monitor->triggers[i]);
624 : : }
625 : : }
626 : :
627 : 7 : if (monitor->socket != NULL)
628 : : {
629 : 1 : g_socket_close (monitor->socket, NULL);
630 : 1 : g_object_unref (monitor->socket);
631 : : }
632 : 7 : g_clear_object (&monitor->socket_addr);
633 : :
634 : 7 : G_OBJECT_CLASS (g_memory_monitor_psi_parent_class)->finalize (object);
635 : 7 : }
636 : :
637 : : static void
638 : 3 : g_memory_monitor_psi_class_init (GMemoryMonitorPsiClass *klass)
639 : : {
640 : 3 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
641 : :
642 : 3 : object_class->set_property = g_memory_monitor_psi_set_property;
643 : 3 : object_class->get_property = g_memory_monitor_psi_get_property;
644 : 3 : object_class->finalize = g_memory_monitor_psi_finalize;
645 : :
646 : : /**
647 : : * GMemoryMonitorPsi:proc-path: (nullable)
648 : : *
649 : : * Kernel PSI path to use, if not the default.
650 : : *
651 : : * This is typically only used for test purposes.
652 : : *
653 : : * Since: 2.86
654 : : */
655 : 3 : g_object_class_install_property (object_class,
656 : : PROP_PROC_PATH,
657 : : g_param_spec_string ("proc-path", NULL, NULL,
658 : : NULL,
659 : : G_PARAM_READWRITE |
660 : : G_PARAM_CONSTRUCT_ONLY |
661 : : G_PARAM_STATIC_STRINGS));
662 : 3 : }
663 : :
664 : : static void
665 : 3 : g_memory_monitor_psi_iface_init (GMemoryMonitorInterface *monitor_iface)
666 : : {
667 : 3 : }
668 : :
669 : : static void
670 : 3 : g_memory_monitor_psi_initable_iface_init (GInitableIface *iface)
671 : : {
672 : 3 : iface->init = g_memory_monitor_psi_initable_init;
673 : 3 : }
|