Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gmemorymonitorpsi.c
Line
Count
Source (jump to first uncovered line)
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 "ginitable.h"
27
#include "gioerror.h"
28
#include "giomodule-priv.h"
29
#include "glibintl.h"
30
#include "glib/glib-private.h"
31
#include "glib/gstdio.h"
32
#include "gmemorymonitor.h"
33
#include "gmemorymonitorbase.h"
34
#include "gmemorymonitorpsi.h"
35
36
#include <errno.h>
37
#include <fcntl.h>
38
#include <unistd.h>
39
40
/**
41
 * GMemoryMonitorPsi:
42
 *
43
 * A Linux [iface@Gio.MemoryMonitor] which uses the kernel
44
 * [pressure stall information](https://www.kernel.org/doc/html/latest/accounting/psi.html) (PSI).
45
 *
46
 * When it receives a PSI event, it emits
47
 * [signal@Gio.MemoryMonitor::low-memory-warning] with an appropriate warning
48
 * level.
49
 *
50
 * Since: 2.86
51
 */
52
53
/* Unprivileged users can also create monitors, with
54
 * the only limitation that the window size must be a
55
 * `multiple of 2s`, in order to prevent excessive resource usage.
56
 * see: https://www.kernel.org/doc/html/latest/accounting/psi.html*/
57
0
#define PSI_WINDOW_SEC 2
58
59
typedef enum {
60
  PROP_PROC_PATH = 1,
61
} GMemoryMonitorPsiProperty;
62
63
typedef enum
64
{
65
  MEMORY_PRESSURE_MONITOR_TRIGGER_SOME,
66
  MEMORY_PRESSURE_MONITOR_TRIGGER_FULL,
67
  MEMORY_PRESSURE_MONITOR_TRIGGER_MFD
68
} MemoryPressureMonitorTriggerType;
69
70
/* Each trigger here results in an open fd for the lifetime
71
 * of the `GMemoryMonitor`, so don’t add too many */
72
static const struct
73
{
74
  MemoryPressureMonitorTriggerType trigger_type;
75
  int threshold_ms;
76
} triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_COUNT] = {
77
  { MEMORY_PRESSURE_MONITOR_TRIGGER_SOME, 70 },  /* 70ms out of 2sec for partial stall */
78
  { MEMORY_PRESSURE_MONITOR_TRIGGER_SOME, 100 }, /* 100ms out of 2sec for partial stall */
79
  { MEMORY_PRESSURE_MONITOR_TRIGGER_FULL, 100 }, /* 100ms out of 2sec for complete stall */
80
};
81
82
typedef struct
83
{
84
  GSource source;
85
  GPollFD *pollfd;
86
  GMemoryMonitorLowMemoryLevel level_type;
87
  GWeakRef monitor_weak;
88
} MemoryMonitorSource;
89
90
typedef gboolean (*MemoryMonitorCallbackFunc) (GMemoryMonitorPsi            *monitor,
91
                                               GMemoryMonitorLowMemoryLevel  level_type,
92
                                               void                         *user_data);
93
94
#define G_MEMORY_MONITOR_PSI_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))
95
96
static void g_memory_monitor_psi_iface_init (GMemoryMonitorInterface *iface);
97
static void g_memory_monitor_psi_initable_iface_init (GInitableIface *iface);
98
99
struct _GMemoryMonitorPsi
100
{
101
  GMemoryMonitorBase parent_instance;
102
  GMainContext *worker;  /* (unowned) */
103
  GSource *triggers[G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_COUNT];  /* (owned) (nullable) */
104
105
  char *cg_path;
106
  char *proc_path;
107
108
  gboolean proc_override;
109
};
110
111
G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorPsi, g_memory_monitor_psi, G_TYPE_MEMORY_MONITOR_BASE,
112
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
113
                                                g_memory_monitor_psi_initable_iface_init)
114
                         G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
115
                                                g_memory_monitor_psi_iface_init)
116
                         _g_io_modules_ensure_extension_points_registered ();
117
                         g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
118
                                                         g_define_type_id,
119
                                                         "psi",
120
                                                         20))
121
122
static void
123
g_memory_monitor_psi_init (GMemoryMonitorPsi *psi)
124
0
{
125
0
}
126
127
static void
128
g_memory_monitor_psi_set_property (GObject      *object,
129
                                    guint         prop_id,
130
                                    const GValue *value,
131
                                    GParamSpec   *pspec)
132
0
{
133
0
  GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (object);
134
135
0
  switch ((GMemoryMonitorPsiProperty) prop_id)
136
0
    {
137
0
    case PROP_PROC_PATH:
138
      /* Construct only */
139
0
      g_assert (monitor->proc_path == NULL);
140
0
      monitor->proc_path = g_value_dup_string (value);
141
0
      if (monitor->proc_path != NULL)
142
0
        monitor->proc_override = TRUE;
143
0
      break;
144
0
    default:
145
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146
0
      break;
147
0
    }
148
0
}
149
150
static void
151
g_memory_monitor_psi_get_property (GObject    *object,
152
                                   guint       prop_id,
153
                                   GValue     *value,
154
                                   GParamSpec *pspec)
155
0
{
156
0
  GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (object);
157
158
0
  switch ((GMemoryMonitorPsiProperty) prop_id)
159
0
    {
160
0
    case PROP_PROC_PATH:
161
0
      g_value_set_string (value, monitor->proc_override ? monitor->proc_path : NULL);
162
0
      break;
163
0
    default:
164
0
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165
0
      break;
166
0
    }
167
0
}
168
169
static gboolean
170
g_memory_monitor_low_trigger_cb (GMemoryMonitorPsi            *monitor,
171
                                 GMemoryMonitorLowMemoryLevel  level_type,
172
                                 void                         *user_data)
173
0
{
174
0
  gdouble mem_ratio;
175
176
  /* Should be executed in the worker context */
177
0
  g_assert (g_main_context_is_owner (monitor->worker));
178
179
0
  mem_ratio = g_memory_monitor_base_query_mem_ratio ();
180
181
  /* if mem free ratio > 0.5, don't signal */
182
0
  if (mem_ratio < 0.0)
183
0
    return G_SOURCE_REMOVE;
184
0
  else if (mem_ratio > 0.5)
185
0
    return G_SOURCE_CONTINUE;
186
187
0
  g_memory_monitor_base_send_event_to_user (G_MEMORY_MONITOR_BASE (monitor), level_type);
188
189
0
  return G_SOURCE_CONTINUE;
190
0
}
191
192
static gboolean
193
event_check (GSource *source)
194
0
{
195
0
  MemoryMonitorSource *ev_source = (MemoryMonitorSource *) source;
196
197
0
  if (ev_source->pollfd->revents)
198
0
    return G_SOURCE_CONTINUE;
199
200
0
  return G_SOURCE_REMOVE;
201
0
}
202
203
static gboolean
204
event_dispatch (GSource     *source,
205
                GSourceFunc  callback,
206
                gpointer     user_data)
207
0
{
208
0
  MemoryMonitorSource *ev_source = (MemoryMonitorSource *) source;
209
0
  GMemoryMonitorPsi *monitor = NULL;
210
211
0
  monitor = g_weak_ref_get (&ev_source->monitor_weak);
212
0
  if (monitor == NULL)
213
0
    return G_SOURCE_REMOVE;
214
215
0
  if (monitor->proc_override)
216
0
    {
217
0
      if (!(g_source_query_unix_fd (source, ev_source->pollfd) & G_IO_IN))
218
0
        {
219
0
          g_object_unref (monitor);
220
0
          return G_SOURCE_CONTINUE;
221
0
        }
222
0
    }
223
0
  else
224
0
    {
225
0
      if (!(g_source_query_unix_fd (source, ev_source->pollfd) & G_IO_PRI))
226
0
        {
227
0
          g_object_unref (monitor);
228
0
          return G_SOURCE_CONTINUE;
229
0
        }
230
0
    }
231
232
0
  if (callback)
233
0
    ((MemoryMonitorCallbackFunc) callback) (monitor, ev_source->level_type, user_data);
234
235
0
  g_object_unref (monitor);
236
237
0
  return G_SOURCE_CONTINUE;
238
0
}
239
240
static void
241
event_finalize (GSource *source)
242
0
{
243
0
  MemoryMonitorSource *ev_source = (MemoryMonitorSource *) source;
244
245
0
  g_weak_ref_clear (&ev_source->monitor_weak);
246
0
}
247
248
static GSourceFuncs memory_monitor_event_funcs = {
249
  .check = event_check,
250
  .dispatch = event_dispatch,
251
  .finalize = event_finalize,
252
};
253
254
static GSource *
255
g_memory_monitor_create_source (GMemoryMonitorPsi            *monitor,
256
                                int                           fd,
257
                                GMemoryMonitorLowMemoryLevel  level_type,
258
                                gboolean                      is_path_override)
259
0
{
260
0
  MemoryMonitorSource *source;
261
262
0
  source = (MemoryMonitorSource *) g_source_new (&memory_monitor_event_funcs, sizeof (MemoryMonitorSource));
263
0
  if (is_path_override)
264
0
    source->pollfd = g_source_add_unix_fd ((GSource *) source, fd, G_IO_IN | G_IO_ERR);
265
0
  else
266
0
    source->pollfd = g_source_add_unix_fd ((GSource *) source, fd, G_IO_PRI | G_IO_ERR);
267
0
  source->level_type = level_type;
268
0
  g_weak_ref_init (&source->monitor_weak, monitor);
269
270
0
  return (GSource *) source;
271
0
}
272
273
static gboolean
274
g_memory_monitor_psi_calculate_mem_pressure_path (GMemoryMonitorPsi  *monitor,
275
                                                  GError            **error)
276
0
{
277
0
  pid_t pid;
278
0
  gchar *path_read = NULL;
279
0
  gchar *replacement = NULL;
280
0
  GRegex *regex = NULL;
281
282
0
  if (!monitor->proc_override)
283
0
    {
284
0
      pid = getpid ();
285
0
      monitor->proc_path = g_strdup_printf ("/proc/%d/cgroup", pid);
286
0
    }
287
288
0
  if (!g_file_get_contents (monitor->proc_path, &path_read, NULL, error))
289
0
    {
290
0
      g_free (path_read);
291
0
      return FALSE;
292
0
    }
293
294
  /* cgroupv2 is only supportted and the format is shown as follows:
295
   * ex: 0::/user.slice/user-0.slice/session-c3.scope */
296
0
  regex = g_regex_new ("^0::", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, error);
297
298
0
  if (!g_regex_match (regex, path_read, G_REGEX_MATCH_DEFAULT, NULL))
299
0
    {
300
0
      g_debug ("Unsupported cgroup path information.");
301
0
      g_free (path_read);
302
0
      g_regex_unref (regex);
303
0
      return FALSE;
304
0
    }
305
306
  /* drop "0::" */
307
0
  replacement = g_regex_replace (regex, path_read,
308
0
                                 -1, 0,
309
0
                                 "", G_REGEX_MATCH_DEFAULT, error);
310
0
  if (replacement == NULL)
311
0
    {
312
0
      g_debug ("Unsupported cgroup path format.");
313
0
      g_free (path_read);
314
0
      g_regex_unref (regex);
315
0
      return FALSE;
316
0
    }
317
318
0
  replacement = g_strstrip (replacement);
319
320
0
  if (monitor->proc_override)
321
0
    {
322
0
      monitor->cg_path = g_steal_pointer (&replacement);
323
0
      g_free (path_read);
324
0
      g_regex_unref (regex);
325
0
      return TRUE;
326
0
    }
327
328
0
  monitor->cg_path = g_build_filename ("/sys/fs/cgroup", replacement, "memory.pressure", NULL);
329
0
  g_debug ("cgroup path is %s", monitor->cg_path);
330
331
0
  g_free (path_read);
332
0
  g_free (replacement);
333
0
  g_regex_unref (regex);
334
0
  return g_file_test (monitor->cg_path, G_FILE_TEST_EXISTS);
335
0
}
336
337
static GSource *
338
g_memory_monitor_psi_setup_trigger (GMemoryMonitorPsi             *monitor,
339
                                    GMemoryMonitorLowMemoryLevel   level_type,
340
                                    int                            threshold_us,
341
                                    int                            window_us,
342
                                    GError                       **error)
343
0
{
344
0
  GSource *source;
345
0
  int fd;
346
0
  int ret;
347
0
  size_t wlen;
348
0
  gchar *trigger = NULL;
349
350
0
  fd = g_open (monitor->cg_path, O_RDWR | O_NONBLOCK | O_CLOEXEC);
351
0
  if (fd < 0)
352
0
    {
353
0
      int errsv = errno;
354
0
      g_debug ("Error on opening %s: %s", monitor->cg_path, g_strerror (errsv));
355
0
      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
356
0
                   "Error on opening ‘%s’: %s", monitor->cg_path, g_strerror (errsv));
357
0
      return NULL;
358
0
    }
359
360
  /* The kernel PSI [1] trigger format is:
361
   * <some|full> <stall amount in us> <time window in us>
362
   * The “some” indicates the share of time in which at least some tasks are stalled on a given resource.
363
   * The “full” indicates the share of time in which all non-idle tasks are stalled on a given resource simultaneously.
364
   * "stall amount in us": The total stall time in us.
365
   * "time window in us": The specific time window in us.
366
   * e.g. writing "some 150000 1000000" would add 150ms threshold for partial memory stall measured within 1sec time window
367
   *
368
   * [1] https://docs.kernel.org/accounting/psi.html
369
   */
370
0
  trigger = g_strdup_printf ("%s %d %d",
371
0
                             (triggers[level_type].trigger_type == MEMORY_PRESSURE_MONITOR_TRIGGER_SOME) ? "some" : "full",
372
0
                             threshold_us,
373
0
                             window_us);
374
375
0
  errno = 0;
376
0
  wlen = strlen (trigger) + 1;
377
0
  while (wlen > 0)
378
0
    {
379
0
      int errsv;
380
0
      g_debug ("Write trigger %s", trigger);
381
0
      ret = write (fd, trigger, wlen);
382
0
      errsv = errno;
383
0
      if (ret < 0)
384
0
        {
385
0
          if (errsv == EINTR)
386
0
            {
387
              /* interrupted by signal, retry */
388
0
              continue;
389
0
            }
390
0
          else
391
0
            {
392
0
              g_set_error (error,
393
0
                           G_IO_ERROR, G_IO_ERROR_FAILED,
394
0
                           "Error on setting PSI configurations: %s",
395
0
                           g_strerror (errsv));
396
0
              g_free (trigger);
397
0
              close (fd);
398
0
              return NULL;
399
0
            }
400
0
        }
401
0
      wlen -= ret;
402
0
    }
403
0
  g_free (trigger);
404
405
0
  source = g_memory_monitor_create_source (monitor, fd, level_type, monitor->proc_override);
406
0
  g_source_set_callback (source, G_SOURCE_FUNC (g_memory_monitor_low_trigger_cb), NULL, NULL);
407
408
0
  return g_steal_pointer (&source);
409
0
}
410
411
static gboolean
412
g_memory_monitor_setup_psi (GMemoryMonitorPsi  *monitor,
413
                            GError            **error)
414
0
{
415
0
  if (!g_memory_monitor_psi_calculate_mem_pressure_path (monitor, error))
416
0
    return FALSE;
417
418
0
  for (size_t i = 0; i < G_N_ELEMENTS (triggers); i++)
419
0
    {
420
      /* the user defined PSI is estimated per second and the unit is in micro second(us). */
421
0
      monitor->triggers[i] = g_memory_monitor_psi_setup_trigger (monitor,
422
0
                                                                 i,
423
0
                                                                 triggers[i].threshold_ms * 1000,
424
0
                                                                 PSI_WINDOW_SEC * 1000 * 1000,
425
0
                                                                 error);
426
0
      if (monitor->triggers[i] == NULL)
427
0
        return FALSE;
428
0
    }
429
430
0
  return TRUE;
431
0
}
432
433
static gboolean
434
g_memory_monitor_psi_initable_init (GInitable     *initable,
435
                                    GCancellable  *cancellable,
436
                                    GError       **error)
437
0
{
438
0
  GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (initable);
439
440
0
  monitor->worker = GLIB_PRIVATE_CALL (g_get_worker_context) ();
441
442
0
  if (g_memory_monitor_setup_psi (monitor, error))
443
0
    {
444
0
      for (size_t i = 0; i < G_N_ELEMENTS (monitor->triggers); i++)
445
0
        {
446
0
          g_source_attach (monitor->triggers[i], monitor->worker);
447
0
        }
448
0
    }
449
0
  else
450
0
    {
451
0
      g_debug ("PSI is not supported.");
452
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "PSI is not supported.");
453
0
      return FALSE;
454
0
    }
455
456
0
  return TRUE;
457
0
}
458
459
static void
460
g_memory_monitor_psi_finalize (GObject *object)
461
0
{
462
0
  GMemoryMonitorPsi *monitor = G_MEMORY_MONITOR_PSI (object);
463
464
0
  g_free (monitor->cg_path);
465
0
  g_free (monitor->proc_path);
466
467
0
  for (size_t i = 0; i < G_N_ELEMENTS (monitor->triggers); i++)
468
0
    {
469
0
      g_source_destroy (monitor->triggers[i]);
470
0
      g_source_unref (monitor->triggers[i]);
471
0
    }
472
473
0
  G_OBJECT_CLASS (g_memory_monitor_psi_parent_class)->finalize (object);
474
0
}
475
476
static void
477
g_memory_monitor_psi_class_init (GMemoryMonitorPsiClass *klass)
478
0
{
479
0
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
480
481
0
  object_class->set_property = g_memory_monitor_psi_set_property;
482
0
  object_class->get_property = g_memory_monitor_psi_get_property;
483
0
  object_class->finalize = g_memory_monitor_psi_finalize;
484
485
  /**
486
   * GMemoryMonitorPsi:proc-path: (nullable)
487
   *
488
   * Kernel PSI path to use, if not the default.
489
   *
490
   * This is typically only used for test purposes.
491
   *
492
   * Since: 2.86
493
   */
494
0
  g_object_class_install_property (object_class,
495
0
                                   PROP_PROC_PATH,
496
0
                                   g_param_spec_string ("proc-path", NULL, NULL,
497
0
                                                        NULL,
498
0
                                                        G_PARAM_READWRITE |
499
0
                                                        G_PARAM_CONSTRUCT_ONLY |
500
0
                                                        G_PARAM_STATIC_STRINGS));
501
0
}
502
503
static void
504
g_memory_monitor_psi_iface_init (GMemoryMonitorInterface *monitor_iface)
505
0
{
506
0
}
507
508
static void
509
g_memory_monitor_psi_initable_iface_init (GInitableIface *iface)
510
0
{
511
0
  iface->init = g_memory_monitor_psi_initable_init;
512
0
}