1 | /* GIO - GLib Input, Output and Streaming Library |
2 | * |
3 | * Copyright 2019 Red Hat, Inc |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2.1 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General |
16 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | #include "config.h" |
20 | #include "glib.h" |
21 | #include "glibintl.h" |
22 | |
23 | #include "gmemorymonitor.h" |
24 | #include "ginetaddress.h" |
25 | #include "ginetsocketaddress.h" |
26 | #include "ginitable.h" |
27 | #include "gioenumtypes.h" |
28 | #include "giomodule-priv.h" |
29 | #include "gtask.h" |
30 | |
31 | /** |
32 | * SECTION:gmemorymonitor |
33 | * @title: GMemoryMonitor |
34 | * @short_description: Memory usage monitor |
35 | * @include: gio/gio.h |
36 | * |
37 | * #GMemoryMonitor will monitor system memory and suggest to the application |
38 | * when to free memory so as to leave more room for other applications. |
39 | * It is implemented on Linux using the [Low Memory Monitor](https://gitlab.freedesktop.org/hadess/low-memory-monitor/) |
40 | * ([API documentation](https://hadess.pages.freedesktop.org/low-memory-monitor/)). |
41 | * |
42 | * There is also an implementation for use inside Flatpak sandboxes. |
43 | * |
44 | * Possible actions to take when the signal is received are: |
45 | * - Free caches |
46 | * - Save files that haven't been looked at in a while to disk, ready to be reopened when needed |
47 | * - Run a garbage collection cycle |
48 | * - Try and compress fragmented allocations |
49 | * - Exit on idle if the process has no reason to stay around |
50 | * - Call [`malloc_trim(3)`](man:malloc_trim) to return cached heap pages to |
51 | * the kernel (if supported by your libc) |
52 | * |
53 | * Note that some actions may not always improve system performance, and so |
54 | * should be profiled for your application. `malloc_trim()`, for example, may |
55 | * make future heap allocations slower (due to releasing cached heap pages back |
56 | * to the kernel). |
57 | * |
58 | * See #GMemoryMonitorWarningLevel for details on the various warning levels. |
59 | * |
60 | * |[<!-- language="C" --> |
61 | * static void |
62 | * warning_cb (GMemoryMonitor *m, GMemoryMonitorWarningLevel level) |
63 | * { |
64 | * g_debug ("Warning level: %d", level); |
65 | * if (warning_level > G_MEMORY_MONITOR_WARNING_LEVEL_LOW) |
66 | * drop_caches (); |
67 | * } |
68 | * |
69 | * static GMemoryMonitor * |
70 | * monitor_low_memory (void) |
71 | * { |
72 | * GMemoryMonitor *m; |
73 | * m = g_memory_monitor_dup_default (); |
74 | * g_signal_connect (G_OBJECT (m), "low-memory-warning", |
75 | * G_CALLBACK (warning_cb), NULL); |
76 | * return m; |
77 | * } |
78 | * ]| |
79 | * |
80 | * Don't forget to disconnect the #GMemoryMonitor::low-memory-warning |
81 | * signal, and unref the #GMemoryMonitor itself when exiting. |
82 | * |
83 | * Since: 2.64 |
84 | */ |
85 | |
86 | /** |
87 | * GMemoryMonitor: |
88 | * |
89 | * #GMemoryMonitor monitors system memory and indicates when |
90 | * the system is low on memory. |
91 | * |
92 | * Since: 2.64 |
93 | */ |
94 | |
95 | /** |
96 | * GMemoryMonitorInterface: |
97 | * @g_iface: The parent interface. |
98 | * @low_memory_warning: the virtual function pointer for the |
99 | * #GMemoryMonitor::low-memory-warning signal. |
100 | * |
101 | * The virtual function table for #GMemoryMonitor. |
102 | * |
103 | * Since: 2.64 |
104 | */ |
105 | |
106 | G_DEFINE_INTERFACE_WITH_CODE (GMemoryMonitor, g_memory_monitor, G_TYPE_OBJECT, |
107 | g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE)) |
108 | |
109 | enum { |
110 | LOW_MEMORY_WARNING, |
111 | LAST_SIGNAL |
112 | }; |
113 | |
114 | static guint signals[LAST_SIGNAL] = { 0 }; |
115 | |
116 | /** |
117 | * g_memory_monitor_dup_default: |
118 | * |
119 | * Gets a reference to the default #GMemoryMonitor for the system. |
120 | * |
121 | * Returns: (not nullable) (transfer full): a new reference to the default #GMemoryMonitor |
122 | * |
123 | * Since: 2.64 |
124 | */ |
125 | GMemoryMonitor * |
126 | g_memory_monitor_dup_default (void) |
127 | { |
128 | return g_object_ref (_g_io_module_get_default (G_MEMORY_MONITOR_EXTENSION_POINT_NAME, |
129 | "GIO_USE_MEMORY_MONITOR" , |
130 | NULL)); |
131 | } |
132 | |
133 | static void |
134 | g_memory_monitor_default_init (GMemoryMonitorInterface *iface) |
135 | { |
136 | /** |
137 | * GMemoryMonitor::low-memory-warning: |
138 | * @monitor: a #GMemoryMonitor |
139 | * @level: the #GMemoryMonitorWarningLevel warning level |
140 | * |
141 | * Emitted when the system is running low on free memory. The signal |
142 | * handler should then take the appropriate action depending on the |
143 | * warning level. See the #GMemoryMonitorWarningLevel documentation for |
144 | * details. |
145 | * |
146 | * Since: 2.64 |
147 | */ |
148 | signals[LOW_MEMORY_WARNING] = |
149 | g_signal_new (I_("low-memory-warning" ), |
150 | G_TYPE_MEMORY_MONITOR, |
151 | signal_flags: G_SIGNAL_RUN_LAST, |
152 | G_STRUCT_OFFSET (GMemoryMonitorInterface, low_memory_warning), |
153 | NULL, NULL, |
154 | NULL, |
155 | G_TYPE_NONE, n_params: 1, |
156 | G_TYPE_MEMORY_MONITOR_WARNING_LEVEL); |
157 | } |
158 | |