Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
test_pipe_cache_host_stale.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Guillaume Stutin.
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
28
30#include "system/atomic.h"
31#include "system/dtpthread.h"
32
33#include <stdarg.h>
34#include <stddef.h>
35// cmocka.h declares `extern jmp_buf global_expect_assert_env' at file scope without including
36// <setjmp.h> itself. Same suppression as test_metadata_notify.c, and for the same reason.
37#include <setjmp.h> // NOLINT(misc-include-cleaner)
38#include <stdint.h>
39#include <string.h>
40#include <cmocka.h>
41
42#ifdef _WIN32
43#include "win/main_wrapper.h"
44#endif
45
46#define LINE_BYTES (256 * 1024)
47#define HASH_A 0x0A0A0A0A0A0A0A0Aull
48#define HASH_B 0x0B0B0B0B0B0B0B0Bull
49#define HASH_C 0x0C0C0C0C0C0C0C0Cull
50
51static int _setup(void **state __attribute__((unused)))
52{
53 return dt_dev_pixelpipe_cache_init((size_t)64 * 1024 * 1024, FALSE, FALSE) ? 0 : -1;
54}
55
56static int _teardown(void **state __attribute__((unused)))
57{
59 return 0;
60}
61
62/* Take a writable line the way the pipeline does, fill its host buffer if asked, and publish it:
63 * write lock and reference released. Returns the entry's metadata snapshot, which is what a piece
64 * keeps as its reuse hint. */
66 const gboolean allow_rekey, const gboolean write_host,
68{
69 void *data = NULL;
70 dt_pixel_cache_entry_t *entry = NULL;
72 = dt_dev_pixelpipe_cache_get_writable(hash, LINE_BYTES, "test line", 0, write_host, allow_rekey,
73 hint, NULL, &data, &entry);
74 assert_int_equal(status, expected);
75 assert_non_null(entry);
76
77 if(write_host)
78 {
79 void *buffer = dt_pixel_cache_alloc(entry);
80 assert_non_null(buffer);
81 memset(buffer, (int)(hash & 0xff), LINE_BYTES);
83 }
84
85 const dt_pixel_cache_entry_t snapshot = *entry;
88 return snapshot;
89}
90
93{
94 const dt_pixel_cache_entry_t first
96 const dt_pixel_cache_entry_t second
98
99 // Same line, moved: the rekey is what this is about.
100 assert_int_equal(second.serial, first.serial);
102
104 assert_non_null(entry);
105 // The producer still reaches the buffer it backs its output with ...
106 assert_ptr_equal(dt_pixel_cache_entry_get_buffer(entry), first.data);
107 // ... and no reader sees it as the pixels of HASH_B.
108 assert_null(dt_pixel_cache_entry_get_data(entry));
109
110 void *data = NULL;
111 dt_pixel_cache_entry_t *held = NULL;
113 assert_null(data);
114
115 // The pipeline's own exact-hit lookup finds the line but no pixels, and must not take it as a hit.
117 assert_null(data);
119
120 // A caller owning no device cannot recover them either.
121 assert_false(dt_dev_pixelpipe_cache_peek(HASH_B, &data, NULL, -1, NULL));
122}
123
126{
127 const dt_pixel_cache_entry_t first
129 const dt_pixel_cache_entry_t second
131 assert_int_equal(second.serial, first.serial);
132
133 void *data = NULL;
134 dt_pixel_cache_entry_t *held = NULL;
136 assert_non_null(data);
137 assert_int_equal(((const unsigned char *)data)[0], (int)(HASH_C & 0xff));
138 assert_int_equal(((const unsigned char *)data)[LINE_BYTES - 1], (int)(HASH_C & 0xff));
140}
141
144{
145 const dt_pixel_cache_entry_t first
147 const dt_pixel_cache_entry_t second
149 assert_int_not_equal(second.serial, first.serial);
150
151 // Switching back is an exact hit on the untouched line, host pixels included.
152 void *data = NULL;
153 dt_pixel_cache_entry_t *held = NULL;
155 assert_int_equal(((const unsigned char *)data)[0], (int)(HASH_A & 0xff));
157
158 // And the pipeline, asking to write HASH_A again, is told to take it as it is: the line comes back
159 // already referenced, so nothing can evict it before the pipeline reads it.
160 data = NULL;
161 held = NULL;
162 assert_int_equal(dt_dev_pixelpipe_cache_get_writable(HASH_A, LINE_BYTES, "test line", 0, TRUE, FALSE,
163 &second, NULL, &data, &held),
165 assert_null(data);
166 assert_non_null(held);
167 assert_int_equal(held->serial, first.serial);
168 // One reference, the caller's own, and no lock: the line is readable, and nothing else holds it.
169 assert_int_equal(dt_atomic_get_int(&held->refcount), 1);
170 gboolean unlocked = FALSE;
171 if(dt_pthread_rwlock_trywrlock(&held->lock) == 0)
172 {
173 unlocked = TRUE;
175 }
176 assert_true(unlocked);
178}
179
180int main(int argc, char *argv[])
181{
182 const struct CMUnitTest tests[] = {
183 cmocka_unit_test_setup_teardown(_rekeyed_line_publishes_no_host_pixels, _setup, _teardown),
184 cmocka_unit_test_setup_teardown(_rewritten_line_publishes_its_host_pixels, _setup, _teardown),
185 cmocka_unit_test_setup_teardown(_without_rekey_the_previous_output_stays, _setup, _teardown),
186 };
187 return cmocka_run_group_tests(tests, NULL, NULL);
188}
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
int dt_atomic_get_int(dt_atomic_int *var)
static int dt_pthread_rwlock_trywrlock(dt_pthread_rwlock_t *rwlock) TRY_ACQUIRE(0
static int dt_pthread_rwlock_unlock(dt_pthread_rwlock_t *rwlock) RELEASE_GENERIC(rwlock) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:217
void * data
Definition geometry.h:111
hash
Definition layers.h:90
dt_imageio_retval_t status
Definition mipmap_cache.c:8
void dt_dev_pixelpipe_cache_wrlock_entry(gboolean lock, dt_pixel_cache_entry_t *cache_entry)
Lock or release the write lock on the entry.
void dt_dev_pixelpipe_cache_cleanup(void)
Release everything dt_dev_pixelpipe_cache_init() created, the instance included. Only after a success...
void * dt_pixel_cache_entry_get_data(dt_pixel_cache_entry_t *entry)
Peek the host pixels a cache entry holds for its current hash, without allocating.
void dt_dev_pixelpipe_cache_ref_count_entry(gboolean lock, dt_pixel_cache_entry_t *cache_entry)
Take or release one reference on a cache line. A referenced line is never evicted,...
gboolean dt_dev_pixelpipe_cache_peek(const uint64_t hash, void **data, dt_pixel_cache_entry_t **entry, const int preferred_devid, void **cl_mem_output)
Non-owning lookup of an existing cache line.
void * dt_pixel_cache_alloc(dt_pixel_cache_entry_t *cache_entry)
Actually allocate the memory buffer attached to the cache entry once you create it with dt_dev_pixelp...
void * dt_pixel_cache_entry_get_buffer(dt_pixel_cache_entry_t *entry)
The host buffer of a cache entry, whatever hash its content describes.
dt_pixel_cache_entry_t * dt_dev_pixelpipe_cache_get_entry(const uint64_t hash)
Get an internal reference to the cache entry matching hash. If you are going to access this entry mor...
gboolean dt_dev_pixelpipe_cache_init(size_t max_memory, const gboolean verbose, const gboolean verbose_detail)
Reserve the cache's arena and start it.
void dt_dev_pixelpipe_cache_flag_host_written(dt_pixel_cache_entry_t *entry)
Record that the host buffer of entry now holds the pixels of its current hash.
gboolean dt_dev_pixelpipe_cache_ref_entry_by_hash(const uint64_t hash, void **data, dt_pixel_cache_entry_t **entry)
Resolve and retain an existing cache entry by hash.
dt_dev_pixelpipe_cache_writable_status_t dt_dev_pixelpipe_cache_get_writable(const uint64_t hash, const size_t size, const char *name, const int id, const gboolean alloc, const gboolean allow_rekey_reuse, const dt_pixel_cache_entry_t *reuse_hint, const dt_pixel_cache_entry_t *reuse_hint_prev, void **data, dt_pixel_cache_entry_t **entry)
gboolean dt_dev_pixelpipe_cache_ref_host_entry_by_hash(const uint64_t hash, void **data, dt_pixel_cache_entry_t **entry)
Resolve and retain an existing cacheline that already holds HOST pixels.
Pixelpipe cache for storing intermediate results in the pixelpipe.
dt_dev_pixelpipe_cache_writable_status_t
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_REKEYED
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_CREATED
@ DT_DEV_PIXELPIPE_CACHE_WRITABLE_EXACT_HIT
int main()
Definition prova.c:47
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
const float uint32_t state[4]
unsigned __int64 uint64_t
Definition strptime.c:75
dt_atomic_int refcount
void * data
uint64_t serial
dt_pthread_rwlock_t lock
static int _teardown(void **state __attribute__((unused)))
static int _setup(void **state __attribute__((unused)))
#define LINE_BYTES
static void _rewritten_line_publishes_its_host_pixels(void **state __attribute__((unused)))
static int _teardown(void **state __attribute__((unused)))
#define HASH_B
static int _setup(void **state __attribute__((unused)))
static void _without_rekey_the_previous_output_stays(void **state __attribute__((unused)))
static dt_pixel_cache_entry_t _produce(const uint64_t hash, const dt_pixel_cache_entry_t *hint, const gboolean allow_rekey, const gboolean write_host, const dt_dev_pixelpipe_cache_writable_status_t expected)
#define HASH_C
static void _rekeyed_line_publishes_no_host_pixels(void **state __attribute__((unused)))