Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
sys_resources.h
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4 Copyright (C) 2026 Guillaume Stutin.
5
6 Ansel is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Ansel is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef DT_SYSTEM_SYS_RESOURCES_H
21#define DT_SYSTEM_SYS_RESOURCES_H
22
23/* Memory and worker budgets. The values are owned by the orchestrator
24 * (darktable.c, which fills dt_sys_resources_t once at startup), but the
25 * consumers are low-level compute units -- the pixelpipe cache, tiling, the mipmap
26 * cache, the memory arena -- which must not have to include the whole application
27 * to ask how much RAM they may use. */
28
29#include <glib.h>
30#include <stddef.h>
31#include <stdint.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/* System-wide free RAM Ansel always leaves to the OS and other applications, in bytes.
38 * Absolute on purpose: the kernel's needs do not scale with installed RAM, so a fixed
39 * reserve is both sufficient on a small machine and non-wasteful on a large one.
40 * Overridable through the `memory_pressure_floor` conf key (MiB, 0 = use this). */
41#define DT_MEMORY_PRESSURE_FLOOR_DEFAULT ((int64_t)200 * 1024 * 1024)
42
43typedef struct dt_sys_resources_t
44{
45 size_t total_memory; // All RAM on system
46 size_t mipmap_memory; // RAM allocated to mipmap cache
47 size_t headroom_memory; // RAM left to OS & other Apps
48 size_t pixelpipe_memory; // RAM used by the pixelpipe cache (approx.)
49 size_t pressure_floor_memory; // System-wide available RAM under which we shed caches
51
52void dt_configure_runtime_performance(dt_sys_resources_t *resources, gboolean init_gui);
53
54// Number of workers, on top of reserved workers (1 for main preview, 1 for thumbnail in darkroom)
55// This is currently set to 2, so 4 workers total, without user config.
56// Workers will process a queue of jobs that they share together (except for reserved ones).
57// It is useless to use more than 2 workers
58// since those jobs very often lock some mutex that prevents concurrent running.
59// All jobs finding an idle worker will "start" immediately, as far as the OS knows from outside the program,
60// but may do nothing internally except for waiting a mutex locked by another worker/thread.
61// In that situation, we loose the ability to flush the queue, since jobs are "running".
62// So it's better to have few workers with long queues, rather
63// than many workers, to be able to control queued jobs.
65
66// Unused: tile planning asks dt_dev_pixelpipe_cache_get_available(), which counts as room only what
67// eviction can take back. This one counts half the pipeline cache, held lines included, and takes the
68// budget lowered under kernel pressure for a hard limit.
70
71// Get the maximum size for the whole mipmap cache
72size_t dt_get_mipmap_mem();
73
74// Get the total memory (bytes) the process budgets against: physical RAM, capped by
75// a container/cgroup limit and the host_memory_limit config. Set once at startup by
76// dt_configure_runtime_performance(), never mutated afterwards.
77size_t dt_get_total_mem(void);
78
79// Probe the system for currently-available (free + reclaimable) physical RAM, in bytes.
80// This is a live system-wide measurement, unrelated to our internal budgets: it shrinks
81// when OTHER applications allocate memory. On Linux it also honors a cgroup v2 memory
82// limit (containers, Flatpak, systemd slices) when one is set. Returns 0 when the
83// platform gives us no way to know — callers must treat 0 as "no information", not as
84// "out of memory".
85// The value is cached for a few tens of milliseconds (the probe reads several /proc and
86// /sys files), so it may lag reality by that much.
88
89// Drop the cached probe value so the next dt_get_system_available_mem() re-reads the OS.
90// For callers that just changed the situation themselves (freeing caches) and need the
91// resulting number to be ground truth rather than a pre-change snapshot.
93
94// System-wide available RAM floor (bytes) under which caches must be shed to
95// keep the OS and other applications breathing, regardless of anselrc budgets.
96// See dt_configure_runtime_performance() for how it is derived.
98
100
101#ifdef __cplusplus
102}
103#endif
104
105#endif // DT_SYSTEM_SYS_RESOURCES_H
106
107// clang-format off
108// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
109// vim: shiftwidth=2 expandtab tabstop=2 cindent
110// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
111// clang-format on
void dt_invalidate_system_available_mem(void)
Definition darktable.c:2367
size_t dt_get_system_available_mem(void)
Definition darktable.c:2348
int dt_worker_threads()
Definition darktable.c:2390
void dt_print_mem_usage()
Definition darktable.c:2595
size_t dt_get_memory_pressure_floor(void)
Definition darktable.c:2427
size_t dt_get_total_mem(void)
Definition darktable.c:2422
size_t dt_get_mipmap_mem()
Definition darktable.c:2417
void dt_configure_runtime_performance(dt_sys_resources_t *resources, gboolean init_gui)
Definition darktable.c:2432
size_t dt_get_available_mem()
Definition darktable.c:2395