Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
src/caches/cache.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2011-2015 johannes hanika.
4 Copyright (C) 2012 Raphael Manfredi.
5 Copyright (C) 2012 Richard Wonka.
6 Copyright (C) 2012-2014, 2016 Tobias Ellinghaus.
7 Copyright (C) 2013-2016 Roman Lebedev.
8 Copyright (C) 2019 Andreas Schneider.
9 Copyright (C) 2019, 2023, 2025-2026 Aurélien PIERRE.
10 Copyright (C) 2020 Heiko Bauke.
11 Copyright (C) 2020-2021 Pascal Obry.
12 Copyright (C) 2021 Ralf Brown.
13 Copyright (C) 2022 Martin Bařinka.
14 Copyright (C) 2024 Alynx Zhou.
15 Copyright (C) 2026 Guillaume Stutin.
16
17 darktable is free software: you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation, either version 3 of the License, or
20 (at your option) any later version.
21
22 darktable is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with darktable. If not, see <http://www.gnu.org/licenses/>.
29*/
30
31#include "config.h"
32
33#include "caches/cache.h"
34#include "system/dtpthread.h"
35#include "system/macros.h"
36#include "system/mem_alloc.h"
37
38#include <assert.h>
39#include <inttypes.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43
44// this implements a concurrent LRU cache
45
46static void _cache_gc_locked(dt_cache_t *cache, const float fill_ratio);
47
49 dt_cache_t *cache,
50 size_t entry_size,
51 size_t cost_quota)
52{
53 cache->cost = 0;
54 cache->lru = 0;
55 cache->entry_size = entry_size;
56 cache->cost_quota = cost_quota;
57 dt_pthread_mutex_init(&cache->lock, 0);
58 cache->allocate = 0;
59 cache->allocate_data = 0;
60 cache->cleanup = 0;
61 cache->cleanup_data = 0;
62 cache->hashtable = g_hash_table_new(0, 0);
63}
64
66{
67 g_hash_table_destroy(cache->hashtable);
68 for(GList *l = cache->lru; l; l = g_list_next(l))
69 {
71
72 if(cache->cleanup)
73 {
74 assert(entry->data_size);
76
77 cache->cleanup(cache->cleanup_data, entry);
78 }
79 else
80 {
81 dt_free_align(entry->data);
82 entry->data = NULL;
83 }
84
86 g_slice_free1(sizeof(*entry), entry);
87 }
88 g_list_free(cache->lru);
89 cache->lru = NULL;
91}
92
93int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key)
94{
96 int32_t result = g_hash_table_contains(cache->hashtable, GINT_TO_POINTER(key));
98 return result;
99}
100
102 dt_cache_t *cache,
103 int (*process)(const uint32_t key, const void *data, void *user_data),
104 void *user_data)
105{
107 GHashTableIter iter;
108 gpointer key, value;
109
110 g_hash_table_iter_init (&iter, cache->hashtable);
111 while (g_hash_table_iter_next (&iter, &key, &value))
112 {
114 const int err = process(GPOINTER_TO_INT(key), entry->data, user_data);
115 if(err)
116 {
118 return err;
119 }
120 }
122 return 0;
123}
124
125// return read locked bucket, or NULL if it's not already there.
126// never attempt to allocate a new slot.
127dt_cache_entry_t *dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode)
128{
129 gpointer orig_key, value;
130 gboolean res;
132 res = g_hash_table_lookup_extended(
133 cache->hashtable, GINT_TO_POINTER(key), &orig_key, &value);
134 if(res)
135 {
137 // lock the cache entry
138 const int result
139 = (mode == 'w') ? dt_pthread_rwlock_trywrlock(&entry->lock) : dt_pthread_rwlock_tryrdlock(&entry->lock);
140 if(result)
141 { // need to give up mutex so other threads have a chance to get in between and
142 // free the lock we're trying to acquire:
144 return 0;
145 }
146 // bubble up in lru list:
147 cache->lru = g_list_remove_link(cache->lru, entry->link);
148 cache->lru = g_list_concat(cache->lru, entry->link);
150
151 if(mode == 'w')
152 {
153 assert(entry->data_size);
155 }
156
157 // WARNING: do *NOT* unpoison here. it must be done by the caller!
158
159 return entry;
160 }
162 return 0;
163}
164
165// if found, the data void* is returned. if not, it is set to be
166// the given *data and a new hash table entry is created, which can be
167// found using the given key later on.
168dt_cache_entry_t *dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line)
169{
170 gpointer orig_key, value;
171 gboolean res;
172 int result;
173restart:
175 res = g_hash_table_lookup_extended(
176 cache->hashtable, GINT_TO_POINTER(key), &orig_key, &value);
177 if(res)
178 { // yay, found. read lock and pass on.
180 if(mode == 'w') result = dt_pthread_rwlock_trywrlock_with_caller(&entry->lock, file, line);
181 else result = dt_pthread_rwlock_tryrdlock_with_caller(&entry->lock, file, line);
182 if(result)
183 { // need to give up mutex so other threads have a chance to get in between and
184 // free the lock we're trying to acquire:
186 g_usleep(5);
187 goto restart;
188 }
189 // bubble up in lru list:
190 cache->lru = g_list_remove_link(cache->lru, entry->link);
191 cache->lru = g_list_concat(cache->lru, entry->link);
193
194 if(mode == 'w')
195 {
196 assert(entry->data_size);
198 }
199
200 // WARNING: do *NOT* unpoison here. it must be done by the caller!
201
202 return entry;
203 }
204
205 // else, not found, need to allocate.
206
207 // first try to clean up.
208 // also wait if we can't free more than the requested fill ratio.
209 if(cache->cost > 0.8f * cache->cost_quota)
210 {
211 // need to roll back all the way to get a consistent lock state:
212 _cache_gc_locked(cache, 0.8f);
213 }
214
215 // here dies your 32-bit system:
216 dt_cache_entry_t *entry = (dt_cache_entry_t *)g_slice_alloc(sizeof(dt_cache_entry_t));
217 entry->data = 0;
218 entry->data_size = cache->entry_size;
219 entry->cost = 1;
220 entry->link = g_list_append(0, entry);
221 entry->key = key;
222 entry->_lock_demoting = 0;
223
224 if(cache->allocate)
225 cache->allocate(cache->allocate_data, entry);
226 else
227 entry->data = dt_alloc_align(entry->data_size);
228
229 if(IS_NULL_PTR(entry->data))
230 {
231 /* The entry came from g_slice_alloc(), which before GLib 2.76 is not g_free()'s allocator. Its
232 * LRU cell goes with it: the entry was never inserted, so nothing else would free it. */
233 g_list_free(entry->link);
234 g_slice_free1(sizeof(*entry), entry);
236 return NULL;
237 }
238
239 int ret = dt_pthread_rwlock_init(&entry->lock, 0);
240 if(ret) fprintf(stderr, "rwlock init: %d\n", ret);
241
242 g_hash_table_insert(cache->hashtable, GINT_TO_POINTER(key), entry);
243 assert(cache->allocate || entry->data_size);
244
245 assert(entry->data_size);
247
248 // if allocate callback is given, always return a write lock
249 const int write = ((mode == 'w') || cache->allocate);
250
251 // write lock in case the caller requests it:
252 if(write) dt_pthread_rwlock_wrlock_with_caller(&entry->lock, file, line);
253 else dt_pthread_rwlock_rdlock_with_caller(&entry->lock, file, line);
254
255 cache->cost += entry->cost;
256
257 // put at end of lru list (most recently used):
258 cache->lru = g_list_concat(cache->lru, entry->link);
259
261 // WARNING: do *NOT* unpoison here. it must be done by the caller!
262
263 return entry;
264}
265
266int dt_cache_remove(dt_cache_t *cache, const uint32_t key)
267{
268 gpointer orig_key, value;
269 gboolean res;
270 int result;
271 dt_cache_entry_t *entry;
272restart:
274
275 res = g_hash_table_lookup_extended(
276 cache->hashtable, GINT_TO_POINTER(key), &orig_key, &value);
277 entry = (dt_cache_entry_t *)value;
278 if(!res)
279 { // not found in cache, not deleting.
281 return 1;
282 }
283 // need write lock to be able to delete:
284 result = dt_pthread_rwlock_trywrlock(&entry->lock);
285 if(result)
286 {
288 g_usleep(5);
289 goto restart;
290 }
291
292 if(entry->_lock_demoting)
293 {
294 // oops, we are currently demoting (rw -> r) lock to this entry in some thread. do not touch!
297 g_usleep(5);
298 goto restart;
299 }
300
301 gboolean removed = g_hash_table_remove(cache->hashtable, GINT_TO_POINTER(key));
302 (void)removed; // make non-assert compile happy
303 assert(removed);
304 cache->lru = g_list_delete_link(cache->lru, entry->link);
305
306 if(cache->cleanup)
307 {
308 assert(entry->data_size);
310
311 cache->cleanup(cache->cleanup_data, entry);
312 }
313 else
314 {
315 dt_free_align(entry->data);
316 entry->data = NULL;
317 }
318
321 cache->cost -= entry->cost;
322 g_slice_free1(sizeof(*entry), entry);
323
325 return 0;
326}
327
328size_t dt_cache_gc(dt_cache_t *cache, const float fill_ratio)
329{
331 const size_t before = cache->cost;
332 _cache_gc_locked(cache, fill_ratio);
333 const size_t reclaimed = before - cache->cost;
335 return reclaimed;
336}
337
338// best-effort garbage collection. never blocks, never fails. well, sometimes it just doesn't free anything.
339// The caller holds cache->lock.
340static void _cache_gc_locked(dt_cache_t *cache, const float fill_ratio)
341{
342 GList *l = cache->lru;
343 while(l)
344 {
346 assert(entry->link->data == entry);
347 l = g_list_next(l); // we might remove this element, so walk to the next one while we still have the pointer..
348 if(cache->cost < cache->cost_quota * fill_ratio) break;
349
350 // if still locked by anyone else give up:
351 if(dt_pthread_rwlock_trywrlock(&entry->lock)) continue;
352
353 if(entry->_lock_demoting)
354 {
355 // oops, we are currently demoting (rw -> r) lock to this entry in some thread. do not touch!
357 continue;
358 }
359
360 // delete!
361 g_hash_table_remove(cache->hashtable, GINT_TO_POINTER(entry->key));
362 cache->lru = g_list_delete_link(cache->lru, entry->link);
363 cache->cost -= entry->cost;
364
365 if(cache->cleanup)
366 {
367 assert(entry->data_size);
369
370 cache->cleanup(cache->cleanup_data, entry);
371 }
372 else
373 {
374 dt_free_align(entry->data);
375 entry->data = NULL;
376 }
377
380 g_slice_free1(sizeof(*entry), entry);
381 }
382}
383
384void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line)
385{
386#if((__has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)) && 1)
387 /* Yes, this is *HIGHLY* unportable and is accessing implementation details.
388 *
389 * There used to be an #ifdef _DEBUG split here, because dt_pthread_rwlock_t was a bare
390 * pthread_rwlock_t in release builds and a struct wrapping one in debug builds. It is the
391 * struct in both since the same-thread recursive-writer tracking was ported into the
392 * release path, so the non-_DEBUG arm stopped compiling -- and nobody found out, because
393 * this whole block only exists under AddressSanitizer and nothing in CI builds with it.
394 * An ASAN build of this tree has been failing here ever since. One arm now. */
395# if defined(HAVE_THREAD_RWLOCK_ARCH_T_READERS)
396 if(entry->lock.lock.__data.__readers <= 1)
397# elif defined(HAVE_THREAD_RWLOCK_ARCH_T_NR_READERS)
398 if(entry->lock.lock.__data.__nr_readers <= 1)
399# else /* HAVE_THREAD_RWLOCK_ARCH_T_(NR_)READERS */
400 // Neither glibc reader-count field was detected -- true on any platform whose
401 // pthread_rwlock_t isn't glibc's (e.g. winpthreads on Windows never exposes this
402 // internal layout at all), so there is no portable way to introspect the reader
403 // count here. Skip the poisoning rather than guess: an entry that stays
404 // unpoisoned is still safe, whereas poisoning it while another reader may still
405 // be touching it is exactly the bug this whole block exists to avoid.
406 if(0)
407# endif /* HAVE_THREAD_RWLOCK_ARCH_T_(NR_)READERS */
408 {
409 // only if there are no other reades we may poison.
410 assert(entry->data_size);
412 }
413#endif
414
416}
417
418int dt_cache_seed(dt_cache_t *cache, const uint32_t key, const void *data, size_t data_size, size_t cost)
419{
420 if(IS_NULL_PTR(cache) || IS_NULL_PTR(data) || data_size == 0) return -1;
421
423 if(g_hash_table_contains(cache->hashtable, GINT_TO_POINTER(key)))
424 {
426 return 1;
427 }
428
429 if(cache->cost > 0.8f * cache->cost_quota)
430 _cache_gc_locked(cache, 0.8f);
431
432 dt_cache_entry_t *entry = (dt_cache_entry_t *)g_slice_alloc(sizeof(dt_cache_entry_t));
433 entry->data = 0;
434 entry->data_size = data_size;
435 entry->cost = cost ? cost : data_size;
436 entry->link = g_list_append(0, entry);
437 entry->key = key;
438 entry->_lock_demoting = 0;
439
440 /* dt_alloc_align, always: this file frees entry->data with dt_free_align() wherever a
441 * cache has no cleanup callback, so the two have to be the same family. See cache.h. */
442 entry->data = dt_alloc_align(entry->data_size);
443 if(IS_NULL_PTR(entry->data))
444 {
445 // same as the failure path in dt_cache_get_with_caller(): the LRU cell goes with the entry
446 g_list_free(entry->link);
447 g_slice_free1(sizeof(*entry), entry);
449 return -1;
450 }
451
452 memcpy(entry->data, data, entry->data_size);
453
454 int ret = dt_pthread_rwlock_init(&entry->lock, 0);
455 if(ret) fprintf(stderr, "rwlock init: %d\n", ret);
456
457 g_hash_table_insert(cache->hashtable, GINT_TO_POINTER(key), entry);
458 cache->lru = g_list_concat(cache->lru, entry->link);
459 cache->cost += entry->cost;
460
461 assert(entry->data_size);
463
465 return 0;
466}
467
469{
470 return (dt_cache_entry_t *)calloc(1, sizeof(dt_cache_entry_t));
471}
472
474{
475 if(IS_NULL_PTR(entry)) return;
476 dt_free_align(entry->data);
477 free(entry);
478}
479
480// clang-format off
481// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
482// vim: shiftwidth=2 expandtab tabstop=2 cindent
483// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
484// clang-format on
__DT_CLONE_TARGETS__ int process(struct dt_iop_module_t *self, const dt_dev_pixelpipe_t *pipe, const dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid)
Definition ashift.c:3179
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
#define ASAN_POISON_MEMORY_REGION(addr, size)
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)
void * dt_alloc_align(size_t size)
Allocate cacheline-aligned memory.
Definition darktable.c:511
dev roi_request value
static int dt_pthread_rwlock_trywrlock(dt_pthread_rwlock_t *rwlock) TRY_ACQUIRE(0
#define dt_pthread_rwlock_wrlock_with_caller(A, B, C)
Definition dtpthread.h:361
#define dt_pthread_rwlock_trywrlock_with_caller(A, B, C)
Definition dtpthread.h:363
static int dt_pthread_rwlock_unlock(dt_pthread_rwlock_t *rwlock) RELEASE_GENERIC(rwlock) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:217
const int res
Definition dtpthread.h:351
#define dt_pthread_rwlock_tryrdlock_with_caller(A, B, C)
Definition dtpthread.h:362
static int dt_pthread_mutex_unlock(dt_pthread_mutex_t *mutex) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:127
static int dt_pthread_mutex_init(dt_pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
Initialise a mutex. With mutexattr NULL – which is how 54 of the 56 call sites in this tree spell it ...
Definition dtpthread.h:104
static int dt_pthread_rwlock_destroy(dt_pthread_rwlock_t *lock)
Definition dtpthread.h:212
static int dt_pthread_rwlock_tryrdlock(dt_pthread_rwlock_t *rwlock) TRY_ACQUIRE_SHARED(0
static int dt_pthread_mutex_destroy(dt_pthread_mutex_t *mutex)
Definition dtpthread.h:132
static int dt_pthread_rwlock_init(dt_pthread_rwlock_t *lock, const pthread_rwlockattr_t *attr)
Definition dtpthread.h:192
static int dt_pthread_mutex_lock(dt_pthread_mutex_t *mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:117
#define dt_pthread_rwlock_rdlock_with_caller(A, B, C)
Definition dtpthread.h:360
memcpy(roundtrip, origin, sizeof(roundtrip))
void * data
Definition geometry.h:111
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define dt_free_align(ptr)
Release memory from dt_alloc_align() and set ptr to NULL.
Definition mem_alloc.h:214
char * key
int32_t dt_cache_contains(dt_cache_t *cache, const uint32_t key)
void dt_cache_entry_free_detached(dt_cache_entry_t *entry)
Release an entry from dt_cache_entry_new_detached(), and its data. NULL-safe.
void dt_cache_release_with_caller(dt_cache_t *cache, dt_cache_entry_t *entry, const char *file, int line)
static void _cache_gc_locked(dt_cache_t *cache, const float fill_ratio)
dt_cache_entry_t * dt_cache_testget(dt_cache_t *cache, const uint32_t key, char mode)
int dt_cache_seed(dt_cache_t *cache, const uint32_t key, const void *data, size_t data_size, size_t cost)
dt_cache_entry_t * dt_cache_entry_new_detached(void)
Allocate a cache entry that belongs to NO cache, for a one-shot decode.
void dt_cache_init(dt_cache_t *cache, size_t entry_size, size_t cost_quota)
dt_cache_entry_t * dt_cache_get_with_caller(dt_cache_t *cache, const uint32_t key, char mode, const char *file, int line)
size_t dt_cache_gc(dt_cache_t *cache, const float fill_ratio)
int dt_cache_remove(dt_cache_t *cache, const uint32_t key)
int dt_cache_for_all(dt_cache_t *cache, int(*process)(const uint32_t key, const void *data, void *user_data), void *user_data)
void dt_cache_cleanup(dt_cache_t *cache)
uint32_t key
GList * link
size_t data_size
dt_pthread_rwlock_t lock
int _lock_demoting
void * data
size_t cost
size_t cost
GList * lru
size_t cost_quota
void * allocate_data
GHashTable * hashtable
dt_cache_allocate_t allocate
dt_pthread_mutex_t lock
size_t entry_size
dt_cache_allocate_t cleanup
void * cleanup_data