File: gmemorymonitorpoll.c

package info (click to toggle)
glib2.0 2.86.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 67,020 kB
  • sloc: ansic: 544,698; python: 9,702; sh: 1,612; xml: 1,482; perl: 1,222; cpp: 535; makefile: 308; javascript: 11
file content (309 lines) | stat: -rw-r--r-- 10,038 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright 2025 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
#include "config.h"

#include "gcancellable.h"
#include "gdbusnamewatching.h"
#include "gdbusproxy.h"
#include "ginitable.h"
#include "gioerror.h"
#include "giomodule-priv.h"
#include "glib/gstdio.h"
#include "glib/glib-private.h"
#include "glibintl.h"
#include "gmemorymonitor.h"
#include "gmemorymonitorpoll.h"

#include <fcntl.h>
#include <unistd.h>

/**
 * GMemoryMonitorPoll:
 *
 * A [iface@Gio.MemoryMonitor] which polls the system free/used
 * memory ratio on a fixed timer.
 *
 * It polls, for example, every 10 seconds, and emits different
 * [signal@Gio.MemoryMonitor::low-memory-warning] signals if it falls below several
 * ‘low’ thresholds.
 *
 * The system free/used memory ratio is queried using [`sysinfo()`](man:sysinfo(2)).
 *
 * This is intended as a fallback implementation of [iface@Gio.MemoryMonitor] in case
 * other, more performant, implementations are not supported on the system.
 *
 * Since: 2.86
 */

typedef enum {
  PROP_MEM_FREE_RATIO = 1,
  PROP_POLL_INTERVAL_MS,
} GMemoryMonitorPollProperty;

#define G_MEMORY_MONITOR_POLL_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))

static void g_memory_monitor_poll_iface_init (GMemoryMonitorInterface *iface);
static void g_memory_monitor_poll_initable_iface_init (GInitableIface *iface);

struct _GMemoryMonitorPoll
{
  GMemoryMonitorBase parent_instance;

  GMainContext *worker;  /* (unowned) */
  GSource *source_timeout;  /* (owned) */

  /* it overrides the default timeout when running the test */
  unsigned int poll_interval_ms;  /* zero to use the default */
  gdouble mem_free_ratio;
};

/* the default monitor timeout */
#define G_MEMORY_MONITOR_PSI_DEFAULT_SEC 10

G_DEFINE_TYPE_WITH_CODE (GMemoryMonitorPoll, g_memory_monitor_poll, G_TYPE_MEMORY_MONITOR_BASE,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                g_memory_monitor_poll_initable_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_MEMORY_MONITOR,
                                                g_memory_monitor_poll_iface_init)
                         _g_io_modules_ensure_extension_points_registered ();
                         g_io_extension_point_implement (G_MEMORY_MONITOR_EXTENSION_POINT_NAME,
                                                         g_define_type_id,
                                                         "poll",
                                                         10))

static void
g_memory_monitor_poll_init (GMemoryMonitorPoll *mem_poll)
{
  mem_poll->mem_free_ratio = -1.0;
}

static void
g_memory_monitor_poll_set_property (GObject      *object,
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
  GMemoryMonitorPoll *monitor = G_MEMORY_MONITOR_POLL (object);

  switch ((GMemoryMonitorPollProperty) prop_id)
    {
    case PROP_MEM_FREE_RATIO:
      monitor->mem_free_ratio = g_value_get_double (value);
      break;
    case PROP_POLL_INTERVAL_MS:
      monitor->poll_interval_ms = g_value_get_uint (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
g_memory_monitor_poll_get_property (GObject    *object,
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
  GMemoryMonitorPoll *monitor = G_MEMORY_MONITOR_POLL (object);

  switch ((GMemoryMonitorPollProperty) prop_id)
    {
    case PROP_MEM_FREE_RATIO:
      g_value_set_double (value, monitor->mem_free_ratio);
      break;
    case PROP_POLL_INTERVAL_MS:
      g_value_set_uint (value, monitor->poll_interval_ms);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

typedef struct
{
  GWeakRef monitor_weak;  /* (element-type GMemoryMonitorPoll) */
} CallbackData;

static CallbackData *
callback_data_new (GMemoryMonitorPoll *monitor)
{
  CallbackData *data = g_new0 (CallbackData, 1);
  g_weak_ref_init (&data->monitor_weak, monitor);
  return g_steal_pointer (&data);
}

static void
callback_data_free (CallbackData *data)
{
  g_weak_ref_clear (&data->monitor_weak);
  g_free (data);
}

static gboolean
g_memory_monitor_mem_ratio_cb (gpointer user_data)
{
  CallbackData *data = user_data;
  GMemoryMonitorPoll *monitor = NULL;
  gdouble mem_ratio;
  GMemoryMonitorLowMemoryLevel warning_level = G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_INVALID;

  /* It’s possible for the dispatch of this callback to race with finalising
   * the `GMemoryMonitorPoll`, hence the use of a thread-safe weak ref. */
  monitor = g_weak_ref_get (&data->monitor_weak);
  if (monitor == NULL)
    return G_SOURCE_REMOVE;

  /* Should be executed in the worker context */
  g_assert (g_main_context_is_owner (monitor->worker));

  mem_ratio = g_memory_monitor_base_query_mem_ratio ();

  /* free ratio override */
  if (monitor->mem_free_ratio >= 0.0)
    mem_ratio = monitor->mem_free_ratio;

  if (mem_ratio < 0.0)
    {
      g_clear_object (&monitor);
      return G_SOURCE_REMOVE;
    }

  if (mem_ratio > 0.5)
    {
      g_clear_object (&monitor);
      return G_SOURCE_CONTINUE;
    }

  g_debug ("memory free ratio %f", mem_ratio);

  if (mem_ratio < 0.2)
    warning_level = G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_CRITICAL;
  else if (mem_ratio < 0.3)
    warning_level = G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_MEDIUM;
  else if (mem_ratio < 0.4)
    warning_level = G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_LOW;

  if (warning_level != G_MEMORY_MONITOR_LOW_MEMORY_LEVEL_INVALID)
    g_memory_monitor_base_send_event_to_user (G_MEMORY_MONITOR_BASE (monitor), warning_level);

  g_clear_object (&monitor);

  return G_SOURCE_CONTINUE;
}

static gboolean
g_memory_monitor_poll_initable_init (GInitable     *initable,
                                     GCancellable  *cancellable,
                                     GError       **error)
{
  GMemoryMonitorPoll *monitor = G_MEMORY_MONITOR_POLL (initable);
  GSource *source;

  if (monitor->poll_interval_ms > 0)
    {
      if (monitor->poll_interval_ms < G_TIME_SPAN_MILLISECOND)
        source = g_timeout_source_new (monitor->poll_interval_ms);
      else
        source = g_timeout_source_new_seconds (monitor->poll_interval_ms / G_TIME_SPAN_MILLISECOND);
    }
  else
    {
      /* default 10 second */
      source = g_timeout_source_new_seconds (G_MEMORY_MONITOR_PSI_DEFAULT_SEC);
    }

  g_source_set_callback (source, g_memory_monitor_mem_ratio_cb,
                         callback_data_new (monitor), (GDestroyNotify) callback_data_free);
  monitor->worker = GLIB_PRIVATE_CALL (g_get_worker_context) ();
  g_source_attach (source, monitor->worker);
  monitor->source_timeout = g_steal_pointer (&source);

  return TRUE;
}

static void
g_memory_monitor_poll_finalize (GObject *object)
{
  GMemoryMonitorPoll *monitor = G_MEMORY_MONITOR_POLL (object);

  g_source_destroy (monitor->source_timeout);
  g_source_unref (monitor->source_timeout);

  G_OBJECT_CLASS (g_memory_monitor_poll_parent_class)->finalize (object);
}

static void
g_memory_monitor_poll_class_init (GMemoryMonitorPollClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->set_property = g_memory_monitor_poll_set_property;
  object_class->get_property = g_memory_monitor_poll_get_property;
  object_class->finalize = g_memory_monitor_poll_finalize;

  /**
   * GMemoryMonitorPoll:mem-free-ratio:
   *
   * Override the memory free ratio
   *
   * Since: 2.86
   */
  g_object_class_install_property (object_class,
                                   PROP_MEM_FREE_RATIO,
                                   g_param_spec_double ("mem-free-ratio", NULL, NULL,
                                                        -1.0, 1.0,
                                                        -1.0,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY |
                                                        G_PARAM_STATIC_STRINGS));

  /**
   * GMemoryMonitorPoll:poll-interval-ms:
   *
   * Override the poll interval for monitoring the memory usage.
   *
   * The interval is in milliseconds. Zero means to use the default interval.
   *
   * Since: 2.86
   */
  g_object_class_install_property (object_class,
                                   PROP_POLL_INTERVAL_MS,
                                   g_param_spec_uint ("poll-interval-ms", NULL, NULL,
                                                      0, G_MAXUINT,
                                                      0,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY |
                                                      G_PARAM_STATIC_STRINGS));

}

static void
g_memory_monitor_poll_iface_init (GMemoryMonitorInterface *monitor_iface)
{
}

static void
g_memory_monitor_poll_initable_iface_init (GInitableIface *iface)
{
  iface->init = g_memory_monitor_poll_initable_init;
}