Eliminating the Monitor Daemon: New PSI and Poll Backends for Glib's GMemoryMonitor API
The GMemoryMonitor [1] API always works with a memory monitoring backend that periodically monitors the memory usage and then signals the GMemoryMonitor API about the low memory status. The monitor daemon monitors the system memory usage and listens to the Linux Kernel memory pressure stall information (PSI) [2], then reports the event to the GMemoryMonitor API via DBus signal. The daemon signals the application and then the application starts to reduce its memory usage demand. However, the reliance on a daemon means GMemoryMonitor may not be able to work if the daemon fails or if the application is running in a container. These backends daemons support the API and Linux system, including RHEL and Fedora, to run.To simplify the data flow and reduce the number of active daemons in RHEL and Fedora, integrating into Glib was a good option. The last question was how Glib monitors the memory usage.
This work integrated the monitoring algorithm into Glib itself to monitor the local memory usage and memory PSI events. Two backends are introduced to the GMemoryMonitor API. According to systemd [3], the Linux kernel PSI event backend is a Linux-specific backend and it watches the Linux kernel memory PSI event and reports the event to the applications. The polling backend polls the memory usage periodically and estimates the free memory ratio. If the free memory ratio drops to a certain threshold, it signals the applications. The original DBus backend was kept for compatibility. The GMemoryMonitor API now runs independently without a daemon to feed data and is 100% compatible with the original API usage.
The Linux kernel PSI monitors and estimates the resource usage in a given time period. It generates an event for the listener when it detects usage problems, such as task stall time. The basic concept is that if a task is blocked by a resource for a period of time, the kernel signals the listener. The kernel takes on all the monitoring and event reporting tasks, which is beneficial. GMemoryMonitor sets up the PSI monitoring parameters and waits for the memory events. The poll backend is a fallback implementation when GMemoryMonitor can’t find any supported backends. It periodically polls the system memory usage and estimates the free memory ratio. If the ratio is lower than a certain threshold, it signals the caller.
By integrating the memory monitoring algorithm directly into Glib, this work successfully introduced the Linux PSI and Poll backends for the GMemoryMonitor API. This fundamental shift eliminates the dependency on an external monitor daemon, allowing GMemoryMonitor to operate independently and making it more reliable, particularly in containerized environments. Crucially, the new approach remains 100% compatible with the original API usage, while simplifying the system’s architecture and streamlining the data flow for memory pressure signals.
[1] https://docs.gtk.org/gio/iface.MemoryMonitor.html
[2] https://docs.kernel.org/accounting/psi.html
[3] https://systemd.io/MEMORY_PRESSURE/
