This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-10-quiet-head
in repository terminology.
View the commit online.
commit 1afe5f17fb9f0787b455412d959c2a0eb68e71b2
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 02:38:30 2026 -0600
tymuxd: rebuild compressed pages on a thread
A PAGE_REQ for a compressed page rebuilt its memfd on the daemon's main
loop, which stalled the shell and every client meanwhile. The rebuild
now runs on an ecore thread, the way the in-process store prefetches
its pages, and the reply goes out once it is done.
The numbers below are from a one-off timing with a throwaway program,
not from a committed benchmark. Main loop time per request, median of
20, release build, Ryzen 7 6800U:
16 MB page: 17.2 ms before, 0.06 ms after
64 MB page: 68.9 ms before, 0.06 ms after
The client holds the page from its request on, so that fd pressure
cannot drop the rebuilt memfd before it is sent, and later requests
wait behind it, so that the replies keep their order. A page reopened
by pop meanwhile is rebuilt on the spot, as before, and the thread's
result dropped: the worker only reads the blob, which the page hands
over to it instead of freeing it. A store freed meanwhile, as a reset
does, never calls back: the requests left waiting on it are dropped
once their pages are seen evicted.
---
src/bin/backlog_store.c | 579 +++++++++++++++++++++++++++++++++++++++---------
src/bin/backlog_store.h | 14 ++
src/bin/termd_tymux.c | 203 ++++++++++++++---
src/bin/tytest.c | 2 +
src/bin/tytest_tymux.c | 216 ++++++++++++++++++
src/bin/tytest_tymux.h | 1 +
src/bin/unit_tests.h | 1 +
tests/meson.build | 3 +-
8 files changed, 879 insertions(+), 140 deletions(-)
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index fff41857..1553fd11 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -128,20 +128,36 @@ typedef struct
Eina_Semaphore done;
} BacklogStoreJob;
-/* The rebuild of a COMPRESSED page of an ANON store, ahead of its use: the
- * worker reads its own copies, as the page may let go of its blob */
+/* Who backlog_store_page_fd_async() calls once a rebuild ends */
+typedef struct
+{
+ void (*done)(void *data, uint32_t page_id, int fd);
+ void *data;
+} BacklogStoreWaiter;
+
+/* The rebuild of a COMPRESSED page on a thread: for an ANON store into
+ * anonymous memory, ahead of its use, for a MEMFD store into a memfd, for
+ * the waiters. The worker reads the blob and header of the page: a page
+ * that lets go of them first hands them over, and the rebuild is stale. */
typedef struct
{
BacklogStore *bs; /* NULL once the store is gone */
uint32_t page_id;
Eina_Binbuf *blob;
TermSrvBacklogPage *header;
+ Eina_Bool stored_raw;
+ Eina_Bool stale; /* blob and header are its own */
+ Eina_Bool ending; /* the worker is done with them */
+ Eina_Bool ran; /* else the job was cancelled before it ran */
uint32_t header_size;
size_t cells_size;
size_t map_size;
+ size_t file_size;
+ char memfd_name[128]; /* empty for an ANON store */
void *map; /* the rebuilt page, NULL if it failed */
- Eina_Bool ran; /* else the job was cancelled before it ran */
-} BacklogStorePrefetch;
+ int fd; /* not counted in _memfds until installed */
+ Eina_List *waiters;
+} BacklogStoreRebuild;
struct tag_BacklogStore
{
@@ -155,7 +171,7 @@ struct tag_BacklogStore
uint32_t head_cells;
uint64_t total_entries;
Eina_List *jobs;
- Eina_List *prefetches;
+ Eina_List *rebuilds;
void (*prefetch_cb)(void *data, uint32_t page_id);
void *prefetch_data;
/* evicted pages still held, each a BacklogStorePage of its own */
@@ -177,6 +193,8 @@ struct tag_BacklogStore
/* Compression jobs not ended yet, of every store */
static unsigned int _jobs_alive = 0;
+/* Rebuilds not ended yet, of every store */
+static unsigned int _rebuilds_alive = 0;
/* The MEMFD stores of the process share a budget of memfds */
static Eina_List *_memfd_stores = NULL;
static uint32_t _memfds = 0;
@@ -273,16 +291,47 @@ _page_export_drop(BacklogStorePage *page)
_page_fd_close(&page->export_fd);
}
+/* The rebuild of page_id whose worker may still read the page */
+static BacklogStoreRebuild *
+_rebuild_find(BacklogStore *bs, uint32_t page_id)
+{
+ BacklogStoreRebuild *rebuild;
+ Eina_List *l;
+
+ EINA_LIST_FOREACH(bs->rebuilds, l, rebuild)
+ {
+ if ((rebuild->page_id == page_id) && !rebuild->stale &&
+ !rebuild->ending)
+ return rebuild;
+ }
+ return NULL;
+}
+
+/* For a COMPRESSED page about to free its blob and header: a rebuild still
+ * reading them takes them over. */
+static void
+_page_rebuild_hand_over(BacklogStore *bs, BacklogStorePage *page)
+{
+ BacklogStoreRebuild *rebuild = _rebuild_find(bs, page->id);
+
+ if (!rebuild)
+ return;
+ rebuild->stale = EINA_TRUE;
+ page->header = NULL;
+ page->blob = NULL;
+}
+
/* A page whose job outlives the store has no header left: the job owns
* its mapping. */
static void
-_page_close(BacklogStorePage *page)
+_page_close(BacklogStore *bs, BacklogStorePage *page)
{
free(page->widths);
free(page->links.refs);
free(page->blocks.refs);
if (page->state == PAGE_COMPRESSED)
{
+ _page_rebuild_hand_over(bs, page);
free(page->header);
eina_binbuf_free(page->blob);
_page_export_drop(page);
@@ -293,24 +342,29 @@ _page_close(BacklogStorePage *page)
_page_fd_close(&page->fd);
}
-#if defined(__linux__)
-static void *
-_page_memfd_map(const BacklogStore *bs, const char *kind, uint32_t page_id,
- size_t size, int *fd_out)
+static void
+_page_memfd_name(const BacklogStore *bs, const char *kind, uint32_t page_id,
+ char *memfd_name, size_t len)
+{
+ snprintf(memfd_name, len, "%s-%s-%u", kind, bs->name, page_id);
+}
+
+#if defined(__linux__)
+/* Safe to call from any thread: the memfd is not counted in _memfds. */
+static void *
+_memfd_map(const char *memfd_name, size_t file_size, size_t size,
+ int *fd_out)
{
- char memfd_name[128];
void *map;
int fd;
- snprintf(memfd_name, sizeof(memfd_name), "%s-%s-%u", kind, bs->name,
- page_id);
fd = memfd_create(memfd_name, MFD_CLOEXEC);
if (fd < 0)
{
ERR("memfd_create: %s", strerror(errno));
return NULL;
}
- if (ftruncate(fd, bs->geo.page_size) < 0)
+ if (ftruncate(fd, file_size) < 0)
{
ERR("ftruncate: %s", strerror(errno));
close(fd);
@@ -324,20 +378,32 @@ _page_memfd_map(const BacklogStore *bs, const char *kind, uint32_t page_id,
return NULL;
}
*fd_out = fd;
- _memfds++;
return map;
}
#else
/* backlog_store_new() makes no MEMFD store off Linux */
static void *
-_page_memfd_map(const BacklogStore *bs EINA_UNUSED,
- const char *kind EINA_UNUSED, uint32_t page_id EINA_UNUSED,
- size_t size EINA_UNUSED, int *fd_out EINA_UNUSED)
+_memfd_map(const char *memfd_name EINA_UNUSED, size_t file_size EINA_UNUSED,
+ size_t size EINA_UNUSED, int *fd_out EINA_UNUSED)
{
return NULL;
}
#endif
+static void *
+_page_memfd_map(const BacklogStore *bs, const char *kind, uint32_t page_id,
+ size_t size, int *fd_out)
+{
+ char memfd_name[128];
+ void *map;
+
+ _page_memfd_name(bs, kind, page_id, memfd_name, sizeof(memfd_name));
+ map = _memfd_map(memfd_name, bs->geo.page_size, size, fd_out);
+ if (map)
+ _memfds++;
+ return map;
+}
+
/* Safe to call from any thread. */
static void *
_page_anon_map(size_t size)
@@ -695,7 +761,7 @@ _page_recycle(BacklogStore *bs, BacklogStorePage *page)
bs->spare = page->header;
bs->spare_used = page->header_size + page->cells_size;
page->header = NULL;
- _page_close(page);
+ _page_close(bs, page);
return EINA_TRUE;
}
@@ -715,7 +781,7 @@ _evict(BacklogStore *bs)
{
_page_refs_release(bs, &bs->pages[0]);
if (!_page_recycle(bs, &bs->pages[0]))
- _page_close(&bs->pages[0]);
+ _page_close(bs, &bs->pages[0]);
}
/* without memory to keep it, the page stays until a later append */
else if (!_page_detach(bs, &bs->pages[0]))
@@ -762,7 +828,7 @@ void
backlog_store_free(BacklogStore *bs)
{
BacklogStorePage *detached;
- BacklogStorePrefetch *prefetch;
+ BacklogStoreRebuild *rebuild;
BacklogStoreJob *job;
uint32_t i;
@@ -786,18 +852,18 @@ backlog_store_free(BacklogStore *bs)
job->orphan_size = page->map_size;
page->header = NULL;
}
- EINA_LIST_FREE(bs->prefetches, prefetch)
- prefetch->bs = NULL;
for (i = 0; i < bs->count + bs->parked; i++)
{
_page_refs_release(bs, &bs->pages[i]);
- _page_close(&bs->pages[i]);
+ _page_close(bs, &bs->pages[i]);
}
EINA_LIST_FREE(bs->detached, detached)
{
- _page_close(detached);
+ _page_close(bs, detached);
free(detached);
}
+ EINA_LIST_FREE(bs->rebuilds, rebuild)
+ rebuild->bs = NULL;
if (bs->spare)
munmap(bs->spare, bs->geo.page_size);
free(bs->pages);
@@ -1053,6 +1119,22 @@ backlog_store_memory_get(const BacklogStore *bs)
return bytes;
}
+/* Write a page back from its header and the blob of its cells into map.
+ * Safe to call from any thread. */
+static Eina_Bool
+_page_rebuild(unsigned char *map, const TermSrvBacklogPage *header,
+ uint32_t header_size, const Eina_Binbuf *blob,
+ Eina_Bool stored_raw, size_t cells_size)
+{
+ memcpy(map, header, header_size);
+ if (stored_raw)
+ {
+ memcpy(map + header_size, eina_binbuf_string_get(blob), cells_size);
+ return EINA_TRUE;
+ }
+ return backlog_uncompress(blob, map + header_size, cells_size);
+}
+
/* Rebuild a COMPRESSED page into export_map, in the layout it had, mapped
* as a whole page so that it can be the head again. It expands on the
* calling thread: up to TERMSRV_BACKLOG_PAGE_SEAL_CELLS cells, which takes
@@ -1067,12 +1149,8 @@ _page_export(const BacklogStore *bs, BacklogStorePage *page)
map = _page_map(bs, "ty-export", page->id, size, &fd);
if (!map)
return EINA_FALSE;
- memcpy(map, page->header, page->header_size);
- if (page->stored_raw)
- memcpy(map + page->header_size, eina_binbuf_string_get(page->blob),
- page->cells_size);
- else if (!backlog_uncompress(page->blob, map + page->header_size,
- page->cells_size))
+ if (!_page_rebuild(map, page->header, page->header_size, page->blob,
+ page->stored_raw, page->cells_size))
{
ERR("cannot rebuild page %u of '%s'", page->id,
bs->name);
@@ -1150,76 +1228,202 @@ backlog_store_prefetch_cb_set(BacklogStore *bs,
}
static void
-_prefetch_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
+_rebuild_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
{
- BacklogStorePrefetch *prefetch = data;
- unsigned char *map = _page_anon_map(prefetch->map_size);
+ BacklogStoreRebuild *rebuild = data;
+ unsigned char *map;
- prefetch->ran = EINA_TRUE;
+ rebuild->ran = EINA_TRUE;
+ if (rebuild->memfd_name[0])
+ map = _memfd_map(rebuild->memfd_name, rebuild->file_size,
+ rebuild->map_size, &rebuild->fd);
+ else
+ map = _page_anon_map(rebuild->map_size);
if (!map)
return;
- memcpy(map, prefetch->header, prefetch->header_size);
- if (!backlog_uncompress(prefetch->blob, map + prefetch->header_size,
- prefetch->cells_size))
+ if (!_page_rebuild(map, rebuild->header, rebuild->header_size,
+ rebuild->blob, rebuild->stored_raw,
+ rebuild->cells_size))
{
- munmap(map, prefetch->map_size);
+ munmap(map, rebuild->map_size);
+ if (rebuild->fd >= 0)
+ close(rebuild->fd);
+ rebuild->fd = -1;
return;
}
- prefetch->map = map;
+ rebuild->map = map;
}
-/* Install the rebuilt page, unless the page got one meanwhile, or is gone */
static void
-_prefetch_done(void *data, Ecore_Thread *thread EINA_UNUSED)
+_rebuild_free(BacklogStoreRebuild *rebuild)
{
- BacklogStorePrefetch *prefetch = data;
- BacklogStore *bs = prefetch->bs;
+ void *waiter;
- if (bs)
+ if (rebuild->map)
+ munmap(rebuild->map, rebuild->map_size);
+ if (rebuild->fd >= 0)
+ close(rebuild->fd);
+ if (rebuild->stale)
{
- BacklogStorePage *page = _page_find(bs, prefetch->page_id);
-
- bs->prefetches = eina_list_remove(bs->prefetches, prefetch);
- if (page && (page->state == PAGE_COMPRESSED) && !page->export_map &&
- prefetch->map)
- {
- page->export_map = prefetch->map;
- page->export_size = prefetch->map_size;
- page->export_fd = -1;
- page->idle_since = ecore_time_get();
- prefetch->map = NULL;
- }
- else if (!prefetch->map && prefetch->ran)
- ERR("cannot rebuild page %u of '%s' ahead", prefetch->page_id,
- bs->name);
- if (bs->prefetch_cb)
- bs->prefetch_cb(bs->prefetch_data, prefetch->page_id);
+ free(rebuild->header);
+ eina_binbuf_free(rebuild->blob);
}
- if (prefetch->map)
- munmap(prefetch->map, prefetch->map_size);
- eina_binbuf_free(prefetch->blob);
- free(prefetch->header);
- free(prefetch);
+ EINA_LIST_FREE(rebuild->waiters, waiter)
+ free(waiter);
+ _rebuilds_alive--;
+ free(rebuild);
}
-static BacklogStorePrefetch *
-_prefetch_find(BacklogStore *bs, uint32_t page_id)
+/* What backlog_store_page_fd() would give without a rebuild */
+static int
+_page_fd_ready(BacklogStore *bs, uint32_t page_id)
{
- BacklogStorePrefetch *prefetch;
- Eina_List *l;
+ BacklogStorePage *page = _page_find(bs, page_id);
- EINA_LIST_FOREACH(bs->prefetches, l, prefetch)
+ if (!page)
+ return -1;
+ page->idle_since = ecore_time_get();
+ if (page->state == PAGE_COMPRESSED)
+ return page->export_fd;
+ return page->fd;
+}
+
+/* Install the rebuilt page, unless the page got one meanwhile, was reopened
+ * or is gone, then give the waiters what the page now has. Also called
+ * when the worker never ran. */
+static void
+_rebuild_end(void *data, Ecore_Thread *thread EINA_UNUSED)
+{
+ BacklogStoreRebuild *rebuild = data;
+ BacklogStore *bs = rebuild->bs;
+ BacklogStorePage *page;
+ BacklogStoreWaiter *waiter;
+
+ if (!bs)
{
- if (prefetch->page_id == page_id)
- return prefetch;
+ _rebuild_free(rebuild);
+ return;
}
- return NULL;
+ page = _page_find(bs, rebuild->page_id);
+ if (!rebuild->stale && page && (page->state == PAGE_COMPRESSED) &&
+ !page->export_map && rebuild->map)
+ {
+ page->export_map = rebuild->map;
+ page->export_size = rebuild->map_size;
+ page->export_fd = rebuild->fd;
+ if (rebuild->fd >= 0)
+ _memfds++;
+ page->idle_since = ecore_time_get();
+ rebuild->map = NULL;
+ rebuild->fd = -1;
+ }
+ else if (!rebuild->map && rebuild->ran)
+ ERR("cannot rebuild page %u of '%s'", rebuild->page_id, bs->name);
+ rebuild->ending = EINA_TRUE;
+ /* a waiter may free the store */
+ while (rebuild->bs && rebuild->waiters)
+ {
+ waiter = eina_list_data_get(rebuild->waiters);
+ rebuild->waiters = eina_list_remove_list(rebuild->waiters,
+ rebuild->waiters);
+ waiter->done(waiter->data, rebuild->page_id,
+ _page_fd_ready(bs, rebuild->page_id));
+ free(waiter);
+ }
+ if (rebuild->bs)
+ {
+ bs->rebuilds = eina_list_remove(bs->rebuilds, rebuild);
+ if ((bs->backing == BACKLOG_STORE_ANON) && bs->prefetch_cb)
+ bs->prefetch_cb(bs->prefetch_data, rebuild->page_id);
+ }
+ _rebuild_free(rebuild);
+}
+
+/* A rebuild of the COMPRESSED page, not started yet: add its waiters
+ * before ecore_thread_run(), which may end it at once. */
+static BacklogStoreRebuild *
+_rebuild_new(BacklogStore *bs, BacklogStorePage *page)
+{
+ BacklogStoreRebuild *rebuild = calloc(1, sizeof(*rebuild));
+
+ if (!rebuild)
+ return NULL;
+ rebuild->bs = bs;
+ rebuild->page_id = page->id;
+ rebuild->blob = page->blob;
+ rebuild->header = page->header;
+ rebuild->stored_raw = page->stored_raw;
+ rebuild->header_size = page->header_size;
+ rebuild->cells_size = page->cells_size;
+ rebuild->map_size = page->map_size;
+ rebuild->file_size = bs->geo.page_size;
+ rebuild->fd = -1;
+ if (bs->backing == BACKLOG_STORE_MEMFD)
+ _page_memfd_name(bs, "ty-export", page->id, rebuild->memfd_name,
+ sizeof(rebuild->memfd_name));
+ bs->rebuilds = eina_list_append(bs->rebuilds, rebuild);
+ _rebuilds_alive++;
+ return rebuild;
+}
+
+static void
+_rebuild_run(BacklogStoreRebuild *rebuild)
+{
+ /* when the job cannot run, the cancel callback is called at once */
+ ecore_thread_run(_rebuild_worker, _rebuild_end, _rebuild_end, rebuild);
+}
+
+Eina_Bool
+backlog_store_page_fd_async(BacklogStore *bs, uint32_t page_id,
+ void (*done)(void *data, uint32_t page_id, int fd),
+ void *data)
+{
+ BacklogStoreRebuild *rebuild;
+ BacklogStoreWaiter *waiter;
+ BacklogStorePage *page;
+ Eina_Bool started = EINA_FALSE;
+
+ EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
+ EINA_SAFETY_ON_NULL_RETURN_VAL(done, EINA_FALSE);
+ page = _page_find(bs, page_id);
+ if (!page)
+ return EINA_FALSE;
+ if ((bs->backing != BACKLOG_STORE_MEMFD) ||
+ (page->state != PAGE_COMPRESSED) || page->export_map)
+ {
+ done(data, page_id, backlog_store_page_fd(bs, page_id));
+ return EINA_TRUE;
+ }
+ page->idle_since = ecore_time_get();
+ rebuild = _rebuild_find(bs, page_id);
+ if (!rebuild)
+ {
+ rebuild = _rebuild_new(bs, page);
+ started = (rebuild != NULL);
+ }
+ waiter = rebuild ? malloc(sizeof(*waiter)) : NULL;
+ if (waiter)
+ {
+ waiter->done = done;
+ waiter->data = ""
+ rebuild->waiters = eina_list_append(rebuild->waiters, waiter);
+ if (eina_list_last_data_get(rebuild->waiters) != waiter)
+ {
+ free(waiter);
+ waiter = NULL;
+ }
+ }
+ if (started)
+ _rebuild_run(rebuild);
+ if (!waiter)
+ done(data, page_id, -1);
+ return EINA_TRUE;
}
Eina_Bool
backlog_store_page_prefetch(BacklogStore *bs, uint32_t page_id)
{
- BacklogStorePrefetch *prefetch;
+ BacklogStoreRebuild *rebuild;
BacklogStorePage *page;
EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
@@ -1227,31 +1431,12 @@ backlog_store_page_prefetch(BacklogStore *bs, uint32_t page_id)
if (!page || (bs->backing != BACKLOG_STORE_ANON) ||
(page->state != PAGE_COMPRESSED) || page->export_map)
return EINA_FALSE;
- if (_prefetch_find(bs, page_id))
+ if (_rebuild_find(bs, page_id))
return EINA_TRUE;
- prefetch = calloc(1, sizeof(*prefetch));
- if (!prefetch)
+ rebuild = _rebuild_new(bs, page);
+ if (!rebuild)
return EINA_FALSE;
- prefetch->header = malloc(page->header_size);
- prefetch->blob = eina_binbuf_new();
- if (!prefetch->header || !prefetch->blob ||
- !eina_binbuf_append_buffer(prefetch->blob, page->blob))
- {
- eina_binbuf_free(prefetch->blob);
- free(prefetch->header);
- free(prefetch);
- return EINA_FALSE;
- }
- memcpy(prefetch->header, page->header, page->header_size);
- prefetch->bs = bs;
- prefetch->page_id = page_id;
- prefetch->header_size = page->header_size;
- prefetch->cells_size = page->cells_size;
- prefetch->map_size = page->map_size;
- bs->prefetches = eina_list_append(bs->prefetches, prefetch);
- /* when the job cannot run, the cancel callback is called at once */
- ecore_thread_run(_prefetch_worker, _prefetch_done, _prefetch_done,
- prefetch);
+ _rebuild_run(rebuild);
return EINA_TRUE;
}
@@ -1291,7 +1476,7 @@ backlog_store_release(BacklogStore *bs, uint32_t page_id)
{
bs->detached = eina_list_remove_list(bs->detached, l);
if (!_page_recycle(bs, page))
- _page_close(page);
+ _page_close(bs, page);
free(page);
}
return;
@@ -1521,6 +1706,7 @@ _page_reopen(BacklogStore *bs, BacklogStorePage *page)
{
if (!page->export_map && !_page_export(bs, page))
return EINA_FALSE;
+ _page_rebuild_hand_over(bs, page);
free(page->header);
eina_binbuf_free(page->blob);
page->blob = NULL;
@@ -1713,7 +1899,7 @@ backlog_store_maintain(BacklogStore *bs, double now)
else
pending = EINA_TRUE;
}
- return pending || bs->jobs || bs->prefetches;
+ return pending || bs->jobs || bs->rebuilds;
}
uint32_t
@@ -2182,12 +2368,10 @@ _test_pages_fill(BacklogStore *bs, uint32_t first, uint32_t count)
}
}
-/* Compare page_id as a client would read it with the rows
- * _test_pages_fill() stored. */
+/* Compare page, of page_id, with the rows _test_pages_fill() stored. */
static void
-_test_page_check(BacklogStore *bs, uint32_t page_id)
+_test_page_rows_check(const TermSrvBacklogPage *page, uint32_t page_id)
{
- const TermSrvBacklogPage *page = _test_page_map(bs, page_id);
const TermSrvCell *cells = _test_page_cells(page);
uint32_t i, j, n = atomic_load(&page->num_entries);
Termcell row[TEST_ROW_W];
@@ -2204,6 +2388,16 @@ _test_page_check(BacklogStore *bs, uint32_t page_id)
assert(!memcmp(&cells[_test_off(page)[i] + j], &row[j],
sizeof(row[j])));
}
+}
+
+/* Compare page_id as a client would read it with the rows
+ * _test_pages_fill() stored. */
+static void
+_test_page_check(BacklogStore *bs, uint32_t page_id)
+{
+ const TermSrvBacklogPage *page = _test_page_map(bs, page_id);
+
+ _test_page_rows_check(page, page_id);
_test_page_unmap(bs, page);
}
@@ -3894,6 +4088,181 @@ tytest_backlog_store_fd_budget(void)
return 0;
}
+#if defined(__linux__)
+typedef struct
+{
+ uint32_t calls;
+ uint32_t page_id;
+ int fd; /* a duplicate of the one given, or -1 */
+} Test_Fd_Async;
+
+static void
+_test_fd_async_done(void *data, uint32_t page_id, int fd)
+{
+ Test_Fd_Async *got = data;
+
+ got->calls++;
+ got->page_id = page_id;
+ got->fd = (fd >= 0) ? dup(fd) : -1;
+}
+
+static Eina_Bool
+_test_rebuilds_poll_cb(void *data EINA_UNUSED)
+{
+ if (_rebuilds_alive == 0)
+ ecore_main_loop_quit();
+ return ECORE_CALLBACK_RENEW;
+}
+
+/* Run the main loop until the rebuilds of every store, freed ones
+ * included, have ended. */
+static void
+_test_rebuilds_wait(void)
+{
+ Test_Wait wait = { NULL, EINA_FALSE };
+ Ecore_Timer *poll = ecore_timer_add(0.005, _test_rebuilds_poll_cb, NULL);
+ Ecore_Timer *timeout = ecore_timer_add(20.0, _test_timeout_cb, &wait);
+
+ ecore_main_loop_begin();
+ assert(!wait.timed_out);
+ ecore_timer_del(poll);
+ ecore_timer_del(timeout);
+}
+
+static void
+_test_fd_async_check(BacklogStore *bs, Test_Fd_Async *got, uint32_t page_id)
+{
+ const TermSrvBacklogPage *page;
+
+ assert(got->calls == 1);
+ assert(got->page_id == page_id);
+ assert(got->fd >= 0);
+ page = mmap(NULL, bs->geo.page_size, PROT_READ, MAP_SHARED, got->fd, 0);
+ assert(page != MAP_FAILED);
+ assert(page->page_id == page_id);
+ _test_page_rows_check(page, page_id);
+ munmap((void *)page, bs->geo.page_size);
+ close(got->fd);
+ got->fd = -1;
+ got->calls = 0;
+}
+
+/* Pages 0 and 1 compressed, the head 2 raw with 88 entries */
+static BacklogStore *
+_test_fd_async_store_new(BacklogStoreBacking backing, uint32_t max_entries)
+{
+ BacklogStore *bs = _test_store_new(backing, max_entries);
+
+ _test_pages_fill(bs, 0, 2 * _TEST_ENTRIES + 88);
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ _test_jobs_wait(bs);
+ assert(_page_find(bs, 0)->state == PAGE_COMPRESSED);
+ assert(_page_find(bs, 1)->state == PAGE_COMPRESSED);
+ assert(!_page_find(bs, 0)->export_map && !_page_find(bs, 1)->export_map);
+ return bs;
+}
+#endif
+
+/* Test: backlog_store_page_fd_async() rebuilds a compressed page of a MEMFD
+ * store on a thread, once for all who ask meanwhile, and gives a raw or
+ * rebuilt page at once. A page reopened by pop meanwhile is given as it
+ * now is, an evicted one as -1, and nothing is given once the store is
+ * freed. */
+int
+tytest_backlog_store_page_fd_async(void)
+{
+#if defined(__linux__)
+ Test_Fd_Async got = { 0, 0, -1 }, got2 = { 0, 0, -1 };
+ BacklogStore *bs;
+ unsigned int fds;
+ uint32_t raw_fds, i;
+
+ ecore_init();
+ fds = _test_fd_count();
+ bs = _test_fd_async_store_new(BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ raw_fds = backlog_store_raw_fds_get();
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got2));
+ assert(got.calls == 0 && got2.calls == 0);
+ assert(!_page_find(bs, 0)->export_map);
+ assert(backlog_store_raw_fds_get() == raw_fds);
+ _test_rebuilds_wait();
+ assert(_page_find(bs, 0)->export_map);
+ assert(backlog_store_raw_fds_get() == raw_fds + 1);
+ _test_fd_async_check(bs, &got, 0);
+ _test_fd_async_check(bs, &got2, 0);
+
+ /* the rebuilt page and the raw head come at once */
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+ _test_fd_async_check(bs, &got, 0);
+ assert(backlog_store_page_fd_async(bs, 2, _test_fd_async_done, &got));
+ _test_fd_async_check(bs, &got, 2);
+ assert(!backlog_store_page_fd_async(bs, 3, _test_fd_async_done, &got));
+ assert(got.calls == 0);
+
+ /* pop makes page 1 the head again while it is rebuilt: the rebuild is
+ * dropped, and the reopened page given */
+ assert(backlog_store_page_fd_async(bs, 1, _test_fd_async_done, &got));
+ for (i = 0; i <= 88; i++)
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 2);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ assert(got.calls == 0);
+ _test_rebuilds_wait();
+ _test_fd_async_check(bs, &got, 1);
+ assert(backlog_store_raw_fds_get() == raw_fds + 2);
+ backlog_store_free(bs);
+ assert(backlog_store_raw_fds_get() == 0);
+
+ /* the pages are evicted while they are rebuilt, one closed, one held */
+ bs = _test_fd_async_store_new(BACKLOG_STORE_MEMFD, 2 * _TEST_ENTRIES);
+ backlog_store_hold(bs, 1);
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+ assert(backlog_store_page_fd_async(bs, 1, _test_fd_async_done, &got2));
+ _test_pages_fill(bs, 2 * _TEST_ENTRIES + 88, 2 * _TEST_ENTRIES);
+ assert(backlog_store_first_id(bs) >= 2);
+ _test_rebuilds_wait();
+ assert(got.calls == 1 && got.fd == -1);
+ assert(got2.calls == 1 && got2.fd == -1);
+ got.calls = got2.calls = 0;
+ backlog_store_release(bs, 1);
+ assert(!bs->detached);
+ backlog_store_free(bs);
+
+ /* a held page rebuilt under fd pressure keeps its memfd */
+ bs = _test_fd_async_store_new(BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ backlog_store_hold(bs, 0);
+ backlog_store_fd_budget_set(0);
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+ _test_rebuilds_wait();
+ backlog_store_maintain(bs, ecore_time_get());
+ assert(_page_find(bs, 0)->export_map);
+ _test_fd_async_check(bs, &got, 0);
+ backlog_store_fd_budget_set(UINT32_MAX);
+ _test_jobs_wait(bs);
+ backlog_store_free(bs);
+
+ /* the store goes first: done is never called */
+ bs = _test_fd_async_store_new(BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+ assert(backlog_store_page_fd_async(bs, 1, _test_fd_async_done, &got2));
+ backlog_store_free(bs);
+ _test_rebuilds_wait();
+ assert(got.calls == 0 && got2.calls == 0);
+
+ /* an ANON store has no memfd to give */
+ bs = _test_fd_async_store_new(BACKLOG_STORE_ANON, _TEST_SCROLLBACK);
+ assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+ assert(got.calls == 1 && got.fd == -1);
+ backlog_store_free(bs);
+
+ assert(backlog_store_raw_fds_get() == 0);
+ assert(_test_fd_count() == fds);
+ ecore_shutdown();
+#endif
+ return 0;
+}
+
#if defined(__linux__)
/* Whether the mapping at addr is left out of the children fork() makes. */
static Eina_Bool
diff --git a/src/bin/backlog_store.h b/src/bin/backlog_store.h
index 9c244e55..db23e493 100644
--- a/src/bin/backlog_store.h
+++ b/src/bin/backlog_store.h
@@ -145,6 +145,20 @@ void backlog_store_first_id_set(BacklogStore *bs, uint32_t first_id);
* evicted page, if the rebuild fails, or for an ANON store. */
int backlog_store_page_fd(BacklogStore *bs, uint32_t page_id);
+/* What backlog_store_page_fd() returns, given to done(data, page_id, fd),
+ * on the main loop, with the same rules for fd, which is -1 if the page
+ * could not be rebuilt or went away meanwhile. A compressed page of a
+ * MEMFD store is rebuilt on a thread first: done is then called once that
+ * is over, and not at all if the store is freed before. Otherwise done is
+ * called before this returns, as it may be if the thread cannot start.
+ * The memfd of a rebuild only counts against backlog_store_fd_budget_set()
+ * once it is the page's. Returns EINA_FALSE, without calling done, for an
+ * unknown or evicted page. */
+Eina_Bool backlog_store_page_fd_async(BacklogStore *bs, uint32_t page_id,
+ void (*done)(void *data,
+ uint32_t page_id, int fd),
+ void *data);
+
/* The mapping of page_id. A compressed page is rebuilt first, into
* anonymous memory or into the memfd that backlog_store_page_fd() returns.
* Hold the page to keep the mapping valid: it then stays valid, even once
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
index f0741dbe..94bda4e5 100644
--- a/src/bin/termd_tymux.c
+++ b/src/bin/termd_tymux.c
@@ -41,6 +41,12 @@ typedef struct
int fd;
} QueuedFd;
+typedef struct
+{
+ uint32_t page_id;
+ Eina_Bool hold_taken; /* the client did not hold the page before */
+} ClientPageReq;
+
typedef struct
{
int fd;
@@ -52,8 +58,13 @@ typedef struct
TermSrvReader reader;
Eina_Bool dispatching : 1;
Eina_Bool dead : 1;
+ Eina_Bool page_waiting : 1; /* the store has page_reqs' oldest */
+ Eina_Bool page_reqs_running : 1;
/* ids of the backlog pages this client holds, as uintptr_t, ascending */
Eina_List *held_pages;
+ /* PAGE_REQs not answered yet, oldest first: only the oldest waits on the
+ * store, so that the replies keep their order */
+ Eina_List *page_reqs;
/* memfds waiting to go out with their message, in order: each rides on
* the first byte of the message that starts at offset in out */
Eina_List *queued_fds;
@@ -436,72 +447,193 @@ _client_page_refuse(Client *client, const TermSrvMsgBacklogPageId *reply)
_client_hang_up(client);
}
+static void
+_client_page_hold(Client *client, uint32_t page_id)
+{
+ client->held_pages = eina_list_sorted_insert(client->held_pages,
+ _page_id_cmp,
+ (void *)(uintptr_t)page_id);
+ backlog_store_hold(client->td->pty->backlog_store, page_id);
+}
+
+static void _client_page_release(Client *client, uint32_t page_id);
+
/* Queue the reply in the output, ahead of what comes later, with a duplicate
* of the page memfd that _client_flush() passes along with it, so that a
- * full socket only delays the page. A page that exists but cannot be
- * exported is answered without a memfd, so the client stops waiting for
- * it. An unknown or evicted page gets no reply. */
-static void
-_client_page_send(Client *client, uint32_t page_id)
+ * full socket only delays the page. Returns EINA_FALSE if it was not
+ * sent. */
+static Eina_Bool
+_client_page_fd_send(Client *client, const TermSrvMsgBacklogPageId *reply,
+ int page_fd)
{
- TermSrvMsgBacklogPageId reply = { page_id };
- BacklogStore *backlog = client->td->pty->backlog_store;
size_t length;
- int page_fd;
QueuedFd *queued;
- _client_pages_prune(client);
- if (!backlog_store_page_exists(backlog, page_id))
- return;
- page_fd = backlog_store_page_fd(backlog, page_id);
- if (page_fd < 0)
- {
- _client_page_refuse(client, &reply);
- return;
- }
if (eina_list_count(client->queued_fds) >= TERMSRV_READER_FDS_MAX)
{
WRN("client %d asks for too many pages", client->fd);
_client_hang_up(client);
- return;
+ return EINA_FALSE;
}
- length = sizeof(TermSrvMsgHdr) + sizeof(reply);
+ length = sizeof(TermSrvMsgHdr) + sizeof(*reply);
if (eina_binbuf_length_get(client->out) + length > CLIENT_OUT_MAX)
{
WRN("client %d does not keep up", client->fd);
_client_hang_up(client);
- return;
+ return EINA_FALSE;
}
queued = malloc(sizeof(*queued));
if (!queued)
{
- _client_page_refuse(client, &reply);
- return;
+ _client_page_refuse(client, reply);
+ return EINA_FALSE;
}
queued->fd = fcntl(page_fd, F_DUPFD_CLOEXEC, 0);
if (queued->fd < 0)
{
free(queued);
- _client_page_refuse(client, &reply);
- return;
+ _client_page_refuse(client, reply);
+ return EINA_FALSE;
}
/* in the FIFO before the message: sending it already tries to flush, and
* queued may be gone when that returns */
queued->offset = eina_binbuf_length_get(client->out);
queued->length = length;
client->queued_fds = eina_list_append(client->queued_fds, queued);
- if (!_client_send(client, TSRV_MSG_BACKLOG_PAGE, &reply, sizeof(reply)))
+ if (!_client_send(client, TSRV_MSG_BACKLOG_PAGE, reply, sizeof(*reply)))
{
+ _client_hang_up(client);
+ return EINA_FALSE;
+ }
+ return EINA_TRUE;
+}
+
+/* Answer the oldest PAGE_REQ with fd, the memfd of its page or -1. A page
+ * that exists but cannot be exported is answered without a memfd, so the
+ * client stops waiting for it. An evicted page gets no reply. A client holds
+ * every page it was sent: the hold of a refused request goes, and a later
+ * request for the same page that is sent takes it again. */
+static void
+_client_page_answer(Client *client, int fd)
+{
+ ClientPageReq *req = eina_list_data_get(client->page_reqs);
+ TermSrvMsgBacklogPageId reply = { req->page_id };
+ Eina_Bool sent = EINA_FALSE;
+
+ client->page_reqs = eina_list_remove_list(client->page_reqs,
+ client->page_reqs);
+ client->page_waiting = EINA_FALSE;
+ if (fd >= 0)
+ sent = _client_page_fd_send(client, &reply, fd);
+ else if (backlog_store_page_exists(client->td->pty->backlog_store,
+ req->page_id))
+ _client_page_refuse(client, &reply);
+ if (!sent && req->hold_taken)
+ _client_page_release(client, req->page_id);
+ else if (sent && !_client_page_held(client, req->page_id))
+ _client_page_hold(client, req->page_id);
+ free(req);
+}
+
+static void _client_page_reqs_run(Client *client);
+
+/* The store has the memfd of page_id ready. Every client whose oldest
+ * request waits on that page gets the same, so the first one found takes
+ * it: each request that waited is called back once, those of the clients
+ * gone since included. */
+static void
+_client_page_ready(void *data, uint32_t page_id, int fd)
+{
+ TermDTymux *td = data;
+ Eina_List *l;
+ Client *client;
+
+ EINA_LIST_FOREACH(td->clients, l, client)
+ {
+ ClientPageReq *req = eina_list_data_get(client->page_reqs);
+
+ if (!client->page_waiting || (req->page_id != page_id))
+ continue;
+ _client_page_answer(client, fd);
+ _client_page_reqs_run(client);
+ return;
+ }
+}
+
+/* Ask the store for the page of the oldest request, then for the next
+ * ones, as long as it has them at once. */
+static void
+_client_page_reqs_run(Client *client)
+{
+ if (client->page_reqs_running)
+ return;
+ client->page_reqs_running = EINA_TRUE;
+ while (!client->dead && !client->page_waiting && client->page_reqs)
+ {
+ ClientPageReq *req = eina_list_data_get(client->page_reqs);
+
+ client->page_waiting = EINA_TRUE;
+ if (!backlog_store_page_fd_async(client->td->pty->backlog_store,
+ req->page_id, _client_page_ready,
+ client->td))
+ _client_page_answer(client, -1);
+ }
+ client->page_reqs_running = EINA_FALSE;
+}
+
+/* Drop the requests for pages evicted since: a reset replaces the store,
+ * whose pages are never answered for. Their holds go with the client's. */
+static void
+_client_page_reqs_prune(Client *client)
+{
+ uint32_t first = backlog_store_first_id(client->td->pty->backlog_store);
+ Eina_List *l, *l_next;
+ ClientPageReq *req;
+
+ EINA_LIST_FOREACH_SAFE(client->page_reqs, l, l_next, req)
+ {
+ if (req->page_id >= first)
+ continue;
+ if (l == client->page_reqs)
+ client->page_waiting = EINA_FALSE;
+ client->page_reqs = eina_list_remove_list(client->page_reqs, l);
+ free(req);
+ }
+}
+
+/* The client holds the page from its request on, so that the store keeps
+ * the memfd it rebuilds for it. An unknown or evicted page gets no
+ * reply. */
+static void
+_client_page_request(Client *client, uint32_t page_id)
+{
+ BacklogStore *backlog = client->td->pty->backlog_store;
+ ClientPageReq *req;
+
+ _client_pages_prune(client);
+ _client_page_reqs_prune(client);
+ if (!backlog_store_page_exists(backlog, page_id))
+ return;
+ if (eina_list_count(client->page_reqs) >= TERMSRV_READER_FDS_MAX)
+ {
+ WRN("client %d asks for too many pages", client->fd);
_client_hang_up(client);
return;
}
- /* the flush may have deleted the client, which let go of its holds */
- if (client->dead || _client_page_held(client, page_id))
- return;
- client->held_pages = eina_list_sorted_insert(client->held_pages,
- _page_id_cmp,
- (void *)(uintptr_t)page_id);
- backlog_store_hold(backlog, page_id);
+ req = malloc(sizeof(*req));
+ if (!req)
+ {
+ TermSrvMsgBacklogPageId reply = { page_id };
+
+ _client_page_refuse(client, &reply);
+ return;
+ }
+ req->page_id = page_id;
+ req->hold_taken = !_client_page_held(client, page_id);
+ if (req->hold_taken)
+ _client_page_hold(client, page_id);
+ client->page_reqs = eina_list_append(client->page_reqs, req);
+ _client_page_reqs_run(client);
}
static void
@@ -557,8 +689,8 @@ _client_msg_handle(Client *client, int type, void *payload, uint32_t size)
break;
case TSRV_MSG_BACKLOG_PAGE_REQ:
if (size == sizeof(TermSrvMsgBacklogPageId))
- _client_page_send(client,
- ((TermSrvMsgBacklogPageId *)payload)->page_id);
+ _client_page_request(client,
+ ((TermSrvMsgBacklogPageId *)payload)->page_id);
break;
case TSRV_MSG_BACKLOG_PAGE_RELEASE:
if (size == sizeof(TermSrvMsgBacklogPageId))
@@ -622,6 +754,7 @@ _cb_client_data(void *data, Ecore_Fd_Handler *fd_handler)
static void
_client_free(Client *client)
{
+ ClientPageReq *req;
QueuedFd *queued;
if (client->handler)
@@ -630,6 +763,8 @@ _client_free(Client *client)
eina_binbuf_free(client->out);
termsrv_reader_clear(&client->reader);
eina_list_free(client->held_pages);
+ EINA_LIST_FREE(client->page_reqs, req)
+ free(req);
EINA_LIST_FREE(client->queued_fds, queued)
{
close(queued->fd);
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 0d870f8c..7eb58237 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -181,6 +181,7 @@ static struct {
{ "backlog_store_links", tytest_backlog_store_links},
{ "backlog_store_blocks", tytest_backlog_store_blocks},
{ "backlog_store_fd_budget", tytest_backlog_store_fd_budget},
+ { "backlog_store_page_fd_async", tytest_backlog_store_page_fd_async},
{ "backlog_view_sync", tytest_backlog_view_sync},
{ "backlog_view_ring_full", tytest_backlog_view_ring_full},
{ "backlog_view_map_again", tytest_backlog_view_map_again},
@@ -257,6 +258,7 @@ static const struct {
tytest_tymux_live_scrollback_config},
{ "tymux_live_scrollback_zero", tytest_tymux_live_scrollback_zero},
{ "tymux_live_fd_budget", tytest_tymux_live_fd_budget},
+ { "tymux_live_page_async", tytest_tymux_live_page_async},
#endif
{ NULL, NULL},
};
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index 400cfb54..bccfd524 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -1571,3 +1571,219 @@ tytest_tymux_live_fd_budget(void)
{
return tytest_tymux_live_run(_live_fd_budget_body);
}
+
+#define ASYNC_WIDE_CELLS (1u << 20)
+#define ASYNC_WIDE_LINE \
+ "head -c 1048576 /dev/zero | tr '\\0' a; echo; "
+
+static int
+_async_attach(TermSrvMsgAttached *attached)
+{
+ uint8_t mode = TSRV_ATTACH_READONLY;
+ int fd, shm_fd;
+
+ fd = termsrv_connect(SESSION);
+ assert(fd >= 0);
+ assert(termsrv_msg_send(fd, TSRV_MSG_ATTACH, &mode, sizeof(mode)) == 0);
+ assert(termsrv_recv_attached(fd, &shm_fd, attached) == 0);
+ close(shm_fd);
+ return fd;
+}
+
+/* The pages from first on that hold an entry of ASYNC_WIDE_CELLS cells, in
+ * wide[], up to max of them. Returns how many there are. */
+static unsigned int
+_async_wide_pages_find(int fd, TermSrvReader *reader, uint32_t first,
+ uint32_t count, uint32_t *wide, unsigned int max)
+{
+ TermSrvMsgBacklogMapReq req = { first, count };
+ unsigned int found = 0;
+ void *payload;
+ uint32_t size, off = sizeof(req), page_id, num, i;
+ const uint32_t *widths;
+ int type;
+
+ assert(termsrv_msg_send(fd, TSRV_MSG_BACKLOG_MAP_REQ, &req,
+ sizeof(req)) == 0);
+ for (;;)
+ {
+ type = _pages_recv(reader, fd, 5000, &payload, &size, NULL);
+ assert(type > 0);
+ if (type == TSRV_MSG_BACKLOG_MAP)
+ break;
+ free(payload);
+ }
+ while (termsrv_backlog_map_page_next(payload, size, &off, &page_id,
+ &num, &widths) > 0)
+ {
+ for (i = 0; i < num && found < max; i++)
+ {
+ if (widths[i] != ASYNC_WIDE_CELLS)
+ continue;
+ wide[found++] = page_id;
+ break;
+ }
+ }
+ free(payload);
+ return found;
+}
+
+/* The next PAGE from fd within 5 s, its id in *page_id and its memfd, -1
+ * if it came without one. Returns EINA_FALSE if none came. If notify is
+ * set, the NOTIFY messages read meanwhile are copied there. */
+static Eina_Bool
+_async_page_recv(int fd, TermSrvReader *reader, uint32_t *page_id,
+ int *page_fd, TermSrvMsgNotify *notify)
+{
+ void *payload;
+ uint32_t size;
+ int type;
+
+ for (;;)
+ {
+ *page_fd = -1;
+ type = _pages_recv(reader, fd, 5000, &payload, &size, page_fd);
+ if (type <= 0)
+ return EINA_FALSE;
+ if (type == TSRV_MSG_BACKLOG_PAGE)
+ break;
+ if (notify && type == TSRV_MSG_NOTIFY && size == sizeof(*notify))
+ memcpy(notify, payload, sizeof(*notify));
+ free(payload);
+ if (*page_fd >= 0)
+ close(*page_fd);
+ }
+ assert(size == sizeof(TermSrvMsgBacklogPageId));
+ *page_id = ((TermSrvMsgBacklogPageId *)payload)->page_id;
+ free(payload);
+ return EINA_TRUE;
+}
+
+static void
+_async_page_req(int fd, uint32_t page_id)
+{
+ TermSrvMsgBacklogPageId req = { page_id };
+
+ assert(termsrv_msg_send(fd, TSRV_MSG_BACKLOG_PAGE_REQ, &req,
+ sizeof(req)) == 0);
+}
+
+/* page_fd maps a page that holds the line ASYNC_WIDE_LINE prints. */
+static void
+_async_wide_check(int page_fd, uint32_t page_id)
+{
+ const TermSrvBacklogPage *page;
+ const _Atomic uint32_t *w;
+ const TermSrvCell *cells;
+ struct stat st;
+ uint32_t i;
+
+ assert(fstat(page_fd, &st) == 0);
+ page = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, page_fd, 0);
+ assert(page != MAP_FAILED);
+ close(page_fd);
+ assert(page->magic == TERMSRV_BACKLOG_MAGIC);
+ assert(page->page_id == page_id);
+ w = termsrv_backlog_entry_w((TermSrvBacklogPage *)page,
+ page->entries_per_page);
+ for (i = 0; i < page->num_entries && w[i] != ASYNC_WIDE_CELLS; i++)
+ ;
+ assert(i < page->num_entries);
+ cells = (const TermSrvCell *)((const unsigned char *)page +
+ page->header_size);
+ cells += termsrv_backlog_entry_off((TermSrvBacklogPage *)page)[i];
+ assert(cells[0].codepoint == 'a');
+ assert(cells[ASYNC_WIDE_CELLS - 1].codepoint == 'a');
+ munmap((void *)page, st.st_size);
+}
+
+/* The daemon rebuilds a compressed page on a thread before it answers: a
+ * client gone meanwhile gets nothing, the replies keep the order of the
+ * requests, and a reset that replaces the store meanwhile does not leave
+ * the client's later requests unanswered. */
+static int
+_live_page_async_body(void)
+{
+ TermSrvMsgAttached attached;
+ TermSrvMsgNotify notify = { 0 };
+ TermSrvReader reader = { 0 }, gone_reader = { 0 };
+ TermTymux *ts;
+ Termpty *ty;
+ Compress_Wait wait;
+ uint32_t wide[2], head, page_id;
+ int fd, gone_fd, page_fd;
+
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+ termpty_backlog_viewport_set(ts->shadow_pty, 0);
+ termtymux_resize(ts, 80, SCREEN_ROWS);
+ assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+ assert(tytest_tymux_wait(_prompt_done, ty, 5.0));
+ /* two pages of one 16 MB entry each, then enough pages that the window
+ * lets go of them */
+ assert(tytest_tymux_type(ts, ty, ASYNC_WIDE_LINE ASYNC_WIDE_LINE
+ "seq 1 6000; echo " WINDOW_MARKER
+ "; seq 1 30\n"));
+ assert(tytest_tymux_wait(_window_marker_done, ty, 60.0));
+ termpty_backlog_viewport_set(ts->shadow_pty, 0);
+ assert(tytest_tymux_wait(_window_bottom_done, ts, 10.0));
+ wait.ts = ts;
+ assert(tytest_tymux_wait(_compress_memfds_done, &wait, 30.0));
+
+ gone_fd = _async_attach(&attached);
+ assert(_async_wide_pages_find(gone_fd, &gone_reader,
+ attached.first_page_id, attached.page_count,
+ wide, 2) == 2);
+ head = attached.first_page_id + attached.page_count - 1;
+ assert(wide[1] + BACKLOG_KEEP_BEHIND + BACKLOG_RELEASE_HYSTERESIS < head);
+ _async_page_req(gone_fd, wide[0]);
+ termsrv_reader_clear(&gone_reader);
+ close(gone_fd);
+
+ fd = _async_attach(&attached);
+ _async_page_req(fd, wide[0]);
+ _async_page_req(fd, head);
+ assert(_async_page_recv(fd, &reader, &page_id, &page_fd, NULL));
+ assert(page_id == wide[0] && page_fd >= 0);
+ _async_wide_check(page_fd, wide[0]);
+ assert(_async_page_recv(fd, &reader, &page_id, &page_fd, NULL));
+ assert(page_id == head && page_fd >= 0);
+ close(page_fd);
+
+ _async_page_req(fd, wide[1]);
+ assert(tytest_tymux_type(ts, ty, "printf '\\033c'; seq 7001 7030\n"));
+ assert(tytest_tymux_wait(_reset_done, ty, 10.0));
+ /* whether the page came before the reset or not, a NOTIFY tells the
+ * store that followed: one that tells of no page yet, as the store that
+ * the reset made is empty until rows are saved into it, has none to ask
+ * for */
+ while (notify.first_page_id <= head || notify.page_count == 0)
+ {
+ void *payload;
+ uint32_t size;
+ int type = _pages_recv(&reader, fd, 5000, &payload, &size, &page_fd);
+
+ assert(type > 0);
+ if (type == TSRV_MSG_NOTIFY && size == sizeof(notify))
+ memcpy(¬ify, payload, sizeof(notify));
+ free(payload);
+ if (page_fd >= 0)
+ close(page_fd);
+ page_fd = -1;
+ }
+ head = notify.first_page_id + notify.page_count - 1;
+ _async_page_req(fd, head);
+ assert(_async_page_recv(fd, &reader, &page_id, &page_fd, NULL));
+ assert(page_id == head && page_fd >= 0);
+ close(page_fd);
+
+ termsrv_reader_clear(&reader);
+ close(fd);
+ tytest_tymux_stop(ts);
+ return 0;
+}
+
+int
+tytest_tymux_live_page_async(void)
+{
+ return tytest_tymux_live_run(_live_page_async_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index a2ea572d..e85a36a1 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -54,5 +54,6 @@ int tytest_tymux_live_clear(void);
int tytest_tymux_live_scrollback_config(void);
int tytest_tymux_live_scrollback_zero(void);
int tytest_tymux_live_fd_budget(void);
+int tytest_tymux_live_page_async(void);
#endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index e216a606..ed432460 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -136,6 +136,7 @@ int tytest_backlog_store_next_id(void);
int tytest_backlog_store_links(void);
int tytest_backlog_store_blocks(void);
int tytest_backlog_store_fd_budget(void);
+int tytest_backlog_store_page_fd_async(void);
int tytest_backlog_view_sync(void);
int tytest_backlog_view_ring_full(void);
int tytest_backlog_view_map_again(void);
diff --git a/tests/meson.build b/tests/meson.build
index db8cbb67..d7ca73f4 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -104,7 +104,8 @@ if tests
'tymux_live_pages_busy', 'tymux_live_window',
'tymux_live_compress', 'tymux_live_resize_no_dup',
'tymux_live_clear', 'tymux_live_scrollback_config',
- 'tymux_live_scrollback_zero', 'tymux_live_fd_budget'],
+ 'tymux_live_scrollback_zero', 'tymux_live_fd_budget',
+ 'tymux_live_page_async'],
depends: [tymuxd],
timeout: 120)
endif
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.