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 894f86719b361e0960edf06a23c861edda663942
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 07:27:39 2026 -0600
backlog_store: map pages as far as their cells go
Every page mapped the 80 MiB a page can reach, the cells at which it
seals and an entry of the largest size more, so that the head could
always take a row. A 2000-line terminal had 16 of them, 1.3 GiB of
address space. That does not fit a 32-bit process or a ulimit -v, and
with vm.overcommit_memory=2, which ignores MAP_NORESERVE, or on FreeBSD,
which has none, every page was charged in full. A single terminal
failed under a ulimit -v of 1 GiB, as termpty_store_basic showed.
A page now maps what its entries are expected to take, 128 cells each
(260 KiB at 2000 lines, 2 MiB at 10000), and the head doubles its
mapping with mremap() when its cells need more, up to a whole page.
Sealing costs nothing: a sealed page keeps what it maps. Pages rebuilt
from their compressed copy map their cells only, and grow the same way
if pop makes one the head again. The spare keeps its size.
A mapping that cannot grow where it is moves. The store tells the view
through a new callback each time the head grows, whether it moved or
not, and the view points the ring entries at the page again, as when
the page changes. Nothing else keeps a pointer into
a head: a compression job is taken back before its page is reopened,
and rebuilds read the compressed copy. In tymuxd the daemon's own view
follows the same way. Its clients map the memfd, which keeps the size
of a whole page, so their size checks are unchanged, and the mapping
that grows is the one made before the seal, which keeps its write
access. Off Linux, where there is no mremap(), the head is copied into
a larger mapping.
10 Termpty of 80x24, scrollback 2000, from a tytest program, before ->
after: fed seq 1 2000, VmSize +12.5 GiB -> +41.6 MiB, and the commit
charge without MAP_NORESERVE, as overcommit=2 counts it, +12.5 GiB ->
+41.6 MiB; fed 2000 lines of 80 columns, the same VmSize and a charge
of +12.5 GiB -> +49.7 MiB. tytest termpty_store_basic now passes under
ulimit -v 1048576.
perf stat user cycles, tybench -i 10 -w 1, one process per corpus on
one CPU, median of 3 interleaved runs: at 2000 lines every corpus is
within 1.3%, but fill -2.1% and hyperlink +3.3%, with the same
instructions, which is -2.6% with -falign-functions=64
-falign-loops=64 -falign-jumps=32. At 50 lines all within 1.6%, but
hyperlink +4.0% (+0.6% aligned). Callgrind instructions, tybench -s 2000
-i 1 -w 0, before -> after: scroll +1.5%, plain-ascii +1.2%, long-lines
+0.9%, hyperlink 0.0%. The scroll corpus makes as many mapping calls as
before. tybenchd -i 5 1000000: 20.2 20.1 20.1 -> 20.2 19.9 20.1 MB/s.
---
src/bin/backlog_store.c | 305 ++++++++++++++++++++++++++++++++++++++++++++----
src/bin/backlog_store.h | 16 ++-
src/bin/backlog_view.c | 35 +++++-
src/bin/termpty.c | 210 +++++++++++++++++++++++++++++++++
src/bin/termpty.h | 3 +-
src/bin/tytest.c | 2 +
src/bin/unit_tests.h | 2 +
7 files changed, 547 insertions(+), 26 deletions(-)
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index 037e5b1e..6e2de4f9 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -35,6 +35,12 @@
# define MAP_NORESERVE 0
#endif
+/* A page first maps what its entries take at this many cells each, and
+ * grows by doubling as its cells need more, up to a whole page: a whole
+ * page is 80 MiB of address space, which a 32-bit process, a strict
+ * overcommit or a ulimit -v cannot give each page. */
+#define PAGE_RESERVE_ENTRY_CELLS 128u
+
_Static_assert(sizeof(Termcell) == sizeof(TermSrvCell),
"TermSrvCell must mirror Termcell");
@@ -87,6 +93,8 @@ typedef struct
uint32_t attempts;
size_t cells_size; /* the bytes of cells in use */
uint32_t header_size; /* the store's, kept for _page_close() */
+ /* What header maps, of the whole page: the head grows it as its cells
+ * need, and a sealed page keeps what it had. */
size_t map_size;
Eina_Binbuf *blob;
/* blob holds the cells as they are: compression did not help, but the
@@ -176,6 +184,8 @@ struct tag_BacklogStore
Eina_List *rebuilds;
void (*prefetch_cb)(void *data, uint32_t page_id);
void *prefetch_data;
+ void (*moved_cb)(void *data, uint32_t page_id);
+ void *moved_data;
/* evicted pages still held, each a BacklogStorePage of its own */
Eina_List *detached;
/* heads that backlog_store_pop() dropped, after the count pages in
@@ -185,6 +195,9 @@ struct tag_BacklogStore
* one: a new mapping costs a page fault per 4 KiB its cells fill */
TermSrvBacklogPage *spare;
size_t spare_used; /* the bytes its page had in use */
+ size_t spare_size; /* what it maps */
+ size_t reserve_size; /* what a new page maps */
+ size_t sys_page_size;
void (*link_release_cb)(void *data, uint16_t link_id,
uint16_t count);
void *link_release_data;
@@ -430,13 +443,79 @@ _page_anon_map(size_t size)
#if defined(MADV_DONTFORK)
/* termpty forks the shell: the child would otherwise share the pages
* copy-on-write until it execs, and the next write to the head would
- * copy it */
+ * copy it. mremap() keeps it. */
if (madvise(map, size, MADV_DONTFORK) < 0)
WRN("madvise: %s", strerror(errno));
#endif
return map;
}
+/* What a page whose cells take cells_size bytes needs mapped */
+static size_t
+_page_used_size(const BacklogStore *bs, size_t cells_size)
+{
+ size_t used = bs->geo.header_size + cells_size;
+
+ return (used + bs->sys_page_size - 1) / bs->sys_page_size *
+ bs->sys_page_size;
+}
+
+/* Resizes the mapping of size bytes at map to new_size, where it is or
+ * elsewhere, its cells kept. fd is its memfd, -1 for anonymous memory.
+ * Returns where it is now, or NULL, the mapping then left as it was. */
+static void *
+_map_resize(void *map, size_t size, size_t new_size, int fd)
+{
+ void *got;
+
+#if defined(__linux__)
+ /* a memfd mapping keeps the write access it had before the seal */
+ (void)fd;
+ got = mremap(map, size, new_size, MREMAP_MAYMOVE);
+ if (got == MAP_FAILED)
+ {
+ ERR("mremap: %s", strerror(errno));
+ return NULL;
+ }
+#else
+ (void)fd;
+ got = _page_anon_map(new_size);
+ if (!got)
+ return NULL;
+ memcpy(got, map, MIN(size, new_size));
+ munmap(map, size);
+#endif
+ return got;
+}
+
+/* Makes the head map at least its first bytes, doubling what it maps up to
+ * a whole page. Its mapping may move; the moved callback is told of every
+ * growth. */
+static Eina_Bool
+_page_grow(BacklogStore *bs, BacklogStorePage *page, size_t bytes)
+{
+ size_t size = page->map_size;
+ void *map;
+
+ if (EINA_LIKELY(bytes <= size))
+ return EINA_TRUE;
+ while (size < bytes)
+ size *= 2;
+ size = MIN(size, bs->geo.page_size);
+ map = _map_resize(page->header, page->map_size, size, page->fd);
+ if (!map)
+ {
+ ERR("cannot grow page %u of '%s' to %zu bytes", page->id, bs->name,
+ size);
+ return EINA_FALSE;
+ }
+ page->map_size = size;
+ page->header = map;
+ if (bs->moved_cb)
+ bs->moved_cb(bs->moved_data, page->id);
+ return EINA_TRUE;
+}
+
/* Map the first size bytes of a new page: anonymous memory, or for a MEMFD
* store a memfd named "<kind>-<name>-<page_id>" of a whole page, that
* clients map, returned in *fd_out. *fd_out is -1 otherwise. Returns NULL
@@ -709,6 +788,7 @@ _page_new(BacklogStore *bs, uint32_t page_id)
if (bs->spare)
{
page->header = bs->spare;
+ page->map_size = bs->spare_size;
page->fd = -1;
bs->spare = NULL;
/* what lies past the entries' offsets and widths is never read */
@@ -717,8 +797,11 @@ _page_new(BacklogStore *bs, uint32_t page_id)
2 * sizeof(uint32_t) * bs->geo.entries_per_page));
}
else
- page->header = _page_map(bs, "ty-backlog", page_id,
- bs->geo.page_size, &page->fd);
+ {
+ page->map_size = bs->reserve_size;
+ page->header = _page_map(bs, "ty-backlog", page_id, page->map_size,
+ &page->fd);
+ }
if (!page->header)
{
free(page->widths);
@@ -726,7 +809,6 @@ _page_new(BacklogStore *bs, uint32_t page_id)
}
page->id = page_id;
page->header_size = bs->geo.header_size;
- page->map_size = bs->geo.page_size;
page->header->magic = TERMSRV_BACKLOG_MAGIC;
page->header->version = TERMSRV_BACKLOG_VERSION;
page->header->page_id = page_id;
@@ -796,9 +878,10 @@ static Eina_Bool
_page_recycle(BacklogStore *bs, BacklogStorePage *page)
{
if (bs->backing != BACKLOG_STORE_ANON || bs->spare ||
- page->state != PAGE_RAW || page->map_size != bs->geo.page_size)
+ page->state != PAGE_RAW)
return EINA_FALSE;
bs->spare = page->header;
+ bs->spare_size = page->map_size;
bs->spare_used = page->header_size + page->cells_size;
page->header = NULL;
_page_close(bs, page);
@@ -857,8 +940,12 @@ backlog_store_new(const char *name, BacklogStoreBacking backing,
bs->name = eina_stringshare_add(name);
bs->backing = backing;
sys_page_size = sysconf(_SC_PAGESIZE);
- backlog_store_geometry_get(scrollback, sys_page_size > 0 ? sys_page_size :
- 4096, &bs->geo);
+ bs->sys_page_size = sys_page_size > 0 ? sys_page_size : 4096;
+ backlog_store_geometry_get(scrollback, bs->sys_page_size, &bs->geo);
+ bs->reserve_size = MIN(_page_used_size(bs, (size_t)bs->geo.entries_per_page *
+ PAGE_RESERVE_ENTRY_CELLS *
+ sizeof(TermSrvCell)),
+ bs->geo.page_size);
if (backing == BACKLOG_STORE_MEMFD)
_memfd_stores = eina_list_append(_memfd_stores, bs);
return bs;
@@ -905,7 +992,7 @@ backlog_store_free(BacklogStore *bs)
EINA_LIST_FREE(bs->rebuilds, rebuild)
rebuild->bs = NULL;
if (bs->spare)
- munmap(bs->spare, bs->geo.page_size);
+ munmap(bs->spare, bs->spare_size);
free(bs->pages);
eina_stringshare_del(bs->name);
free(bs);
@@ -953,6 +1040,10 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
add = MIN((size_t)w, TERMSRV_BACKLOG_ENTRY_MAX_CELLS - cur_w);
if (add > 0)
{
+ if (!_page_grow(bs, head_page, bs->geo.header_size +
+ (bs->head_cells + add) * sizeof(Termcell)))
+ goto failed;
+ head = head_page->header;
if (refs && !_page_refs_add(head_page, cells, add))
goto failed;
memcpy(_page_cells(head, bs->geo.header_size) + bs->head_cells,
@@ -976,6 +1067,10 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
added = EINA_TRUE;
}
add = MIN((size_t)w, TERMSRV_BACKLOG_ENTRY_MAX_CELLS);
+ if (!_page_grow(bs, head_page, bs->geo.header_size +
+ (bs->head_cells + add) * sizeof(Termcell)))
+ goto failed;
+ head = head_page->header;
if (refs && !_page_refs_add(head_page, cells, add))
goto failed;
if (add > 0)
@@ -1185,13 +1280,14 @@ _page_rebuild(unsigned char *map, const TermSrvBacklogPage *header,
}
/* 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
- * some milliseconds. */
+ * as far as its cells go: the head it may become again grows it. The memfd
+ * of a MEMFD store still has the size of a whole page, which clients map.
+ * It expands on the calling thread: up to TERMSRV_BACKLOG_PAGE_SEAL_CELLS
+ * cells, which takes some milliseconds. */
static Eina_Bool
_page_export(const BacklogStore *bs, BacklogStorePage *page)
{
- size_t size = page->map_size;
+ size_t size = _page_used_size(bs, page->cells_size);
unsigned char *map;
int fd;
@@ -1276,6 +1372,16 @@ backlog_store_prefetch_cb_set(BacklogStore *bs,
bs->prefetch_data = data;
}
+void
+backlog_store_moved_cb_set(BacklogStore *bs,
+ void (*cb)(void *data, uint32_t page_id),
+ void *data)
+{
+ EINA_SAFETY_ON_NULL_RETURN(bs);
+ bs->moved_cb = cb;
+ bs->moved_data = data;
+}
+
static void
_rebuild_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
{
@@ -1404,7 +1510,7 @@ _rebuild_new(BacklogStore *bs, BacklogStorePage *page)
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->map_size = _page_used_size(bs, page->cells_size);
rebuild->file_size = bs->geo.page_size;
rebuild->fd = -1;
if (bs->backing == BACKLOG_STORE_MEMFD)
@@ -1760,6 +1866,7 @@ _page_reopen(BacklogStore *bs, BacklogStorePage *page)
eina_binbuf_free(page->blob);
page->blob = NULL;
page->header = page->export_map;
+ page->map_size = page->export_size;
page->fd = page->export_fd;
page->export_map = NULL;
page->export_fd = -1;
@@ -2794,6 +2901,7 @@ tytest_backlog_store_anon(void)
const void *raw;
#if defined(__linux__)
unsigned int fds;
+ size_t raw_size;
#endif
ecore_init();
@@ -2820,7 +2928,8 @@ tytest_backlog_store_anon(void)
raw = page;
assert(!backlog_store_page_get(bs, 4));
#if defined(__linux__)
- assert(_test_range_mapped(raw, bs->geo.page_size));
+ raw_size = _page_find(bs, 0)->map_size;
+ assert(_test_range_mapped(raw, raw_size));
#endif
/* the oldest pages go first: page 0 is in the first round */
@@ -2829,7 +2938,7 @@ tytest_backlog_store_anon(void)
_test_jobs_wait(bs);
assert(_page_find(bs, 0)->state == PAGE_COMPRESSED);
#if defined(__linux__)
- assert(!_test_range_mapped(raw, bs->geo.page_size));
+ assert(!_test_range_mapped(raw, raw_size));
#else
(void)raw;
#endif
@@ -2876,7 +2985,7 @@ static void
_test_held_evict(BacklogStoreBacking backing)
{
const TermSrvBacklogPage *raw, *rebuilt;
- size_t rebuilt_size, page_size;
+ size_t rebuilt_size, raw_size;
BacklogStore *bs;
ecore_init();
@@ -2887,6 +2996,7 @@ _test_held_evict(BacklogStoreBacking backing)
backlog_store_hold(bs, 0);
raw = backlog_store_page_get(bs, 0);
assert(raw);
+ raw_size = _page_find(bs, 0)->map_size;
_test_pages_fill(bs, 300, 300);
assert(backlog_store_first_id(bs) > 0);
assert(!backlog_store_page_exists(bs, 0));
@@ -2895,7 +3005,7 @@ _test_held_evict(BacklogStoreBacking backing)
backlog_store_hold(bs, 0);
_test_page_mapping_check(raw, 0);
#if defined(__linux__)
- assert(_test_range_mapped(raw, bs->geo.page_size));
+ assert(_test_range_mapped(raw, raw_size));
#endif
backlog_store_release(bs, 0);
/* an ANON store keeps the mapping for its next page */
@@ -2903,7 +3013,7 @@ _test_held_evict(BacklogStoreBacking backing)
assert(bs->spare == (TermSrvBacklogPage *)raw);
#if defined(__linux__)
else
- assert(!_test_range_mapped(raw, bs->geo.page_size));
+ assert(!_test_range_mapped(raw, raw_size));
#endif
assert(!bs->detached);
backlog_store_release(bs, 0);
@@ -2940,16 +3050,16 @@ _test_held_evict(BacklogStoreBacking backing)
/* freeing the store ends the holds */
bs = _test_store_new(backing, 300);
- page_size = bs->geo.page_size;
assert(bs);
_test_pages_fill(bs, 0, 300);
backlog_store_hold(bs, 0);
raw = backlog_store_page_get(bs, 0);
+ raw_size = _page_find(bs, 0)->map_size;
_test_pages_fill(bs, 300, 300);
assert(eina_list_count(bs->detached) == 1);
backlog_store_free(bs);
#if defined(__linux__)
- assert(!_test_range_mapped(raw, page_size));
+ assert(!_test_range_mapped(raw, raw_size));
#endif
ecore_shutdown();
}
@@ -3381,6 +3491,161 @@ _test_pop_compressing(BacklogStoreBacking backing)
free(big);
}
+#if defined(__linux__)
+static uint32_t _test_moved_id, _test_moved_calls;
+
+static void
+_test_moved_cb(void *data EINA_UNUSED, uint32_t page_id)
+{
+ _test_moved_id = page_id;
+ _test_moved_calls++;
+}
+
+/* Maps a page right after the mapping of page, unless something is there
+ * already: either way the page cannot grow in place. Returns the mapping to
+ * unmap, or NULL. */
+static void *
+_test_after_block(const BacklogStorePage *page)
+{
+ void *want = (unsigned char *)page->header + page->map_size, *got;
+
+ got = mmap(want, 4096, PROT_NONE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
+ if (got == MAP_FAILED)
+ {
+ assert(errno == EEXIST);
+ return NULL;
+ }
+ assert(got == want);
+ return got;
+}
+
+#define TEST_GROW_CHUNK 65536u
+#define TEST_GROW_CELLS (32u * TEST_GROW_CHUNK)
+
+/* Test: a page maps what its entries are expected to take, and the head
+ * doubles it as an entry outgrows it, up to the whole page, and whoever set
+ * the moved callback is told of each growth. With something mapped right
+ * after it, it moves: where it was is unmapped, and the cells are all there,
+ * through the store and, for a MEMFD store, through the memfd that clients
+ * map. A page keeps what it mapped once sealed, and so does the spare. */
+static void
+_test_grow(BacklogStoreBacking backing)
+{
+ const TermSrvBacklogPage *client;
+ const Termcell *cells;
+ BacklogStorePage *page;
+ const void *old;
+ BacklogStore *bs;
+ Termcell *chunk;
+ void *block;
+ size_t reserve, map_size;
+ uint32_t i, w, page_id, grows = 0;
+ struct stat st;
+
+ chunk = calloc(TEST_GROW_CHUNK, sizeof(Termcell));
+ assert(chunk);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ backlog_store_moved_cb_set(bs, _test_moved_cb, NULL);
+ _test_moved_calls = 0;
+ reserve = _page_used_size(bs, _TEST_ENTRIES * PAGE_RESERVE_ENTRY_CELLS *
+ sizeof(Termcell));
+ assert(bs->reserve_size == reserve && reserve < bs->geo.page_size);
+ _test_pages_fill(bs, 0, 3);
+ page = _page_find(bs, 0);
+ assert(page->map_size == reserve);
+ if (backing == BACKLOG_STORE_MEMFD)
+ assert(fstat(page->fd, &st) == 0 &&
+ (size_t)st.st_size == bs->geo.page_size);
+
+ old = page->header;
+ block = _test_after_block(page);
+ for (i = 0; i < TEST_GROW_CELLS; i += TEST_GROW_CHUNK)
+ {
+ uint32_t k;
+
+ for (k = 0; k < TEST_GROW_CHUNK; k++)
+ chunk[k].codepoint = 'a' + (i + k) % 26;
+ map_size = page->map_size;
+ assert(backlog_store_append(bs, chunk, TEST_GROW_CHUNK, i > 0,
+ EINA_TRUE, NULL));
+ page = _page_find(bs, 0);
+ if (page->map_size != map_size)
+ {
+ assert(page->map_size == MIN(2 * map_size, bs->geo.page_size) ||
+ page->map_size > 2 * map_size);
+ grows++;
+ }
+ if (i == 0)
+ {
+ /* it had to move */
+ assert(_test_moved_calls == 1 && _test_moved_id == 0);
+ assert(page->header != old);
+ assert(!_test_range_mapped(old, 1));
+ }
+ }
+ assert(grows >= 4);
+ assert(_test_moved_calls == grows);
+ assert(page->map_size >= bs->geo.header_size + (3 * TEST_ROW_W +
+ TEST_GROW_CELLS) *
+ sizeof(Termcell));
+ cells = backlog_store_newest_get(bs, &w, &page_id);
+ assert(cells && page_id == 0 && w == TEST_GROW_CELLS);
+ for (i = 0; i < w; i++)
+ assert(cells[i].codepoint == 'a' + i % 26);
+ client = _test_page_map(bs, 0);
+ assert(atomic_load(&client->num_entries) == 4);
+ assert(atomic_load(&_test_w(client)[3]) == TEST_GROW_CELLS);
+ assert(_test_page_cells(client)[_test_off(client)[3] + TEST_GROW_CELLS - 1]
+ .codepoint == 'a' + (TEST_GROW_CELLS - 1) % 26);
+ for (i = 0; i < 3; i++)
+ assert(_test_page_cells(client)[_test_off(client)[i]].codepoint ==
+ 'a' + i);
+ _test_page_unmap(bs, client);
+
+ /* sealed, it keeps what it maps */
+ map_size = page->map_size;
+ _test_pages_fill(bs, 3, 1);
+ assert(backlog_store_count(bs) == 2);
+ assert(_page_find(bs, 0)->map_size == map_size);
+ assert(_page_find(bs, 1)->map_size == reserve);
+ if (block)
+ munmap(block, 4096);
+ backlog_store_free(bs);
+
+ /* the spare keeps its size, and the page made of it too */
+ if (backing == BACKLOG_STORE_ANON)
+ {
+ bs = backlog_store_new("test", backing, 100);
+ assert(bs && bs->geo.entries_per_page == 64);
+ assert(backlog_store_append(bs, chunk, TEST_GROW_CHUNK, EINA_FALSE,
+ EINA_TRUE, NULL));
+ map_size = _page_find(bs, 0)->map_size;
+ assert(map_size > bs->reserve_size);
+ _test_pages_fill(bs, 0, 3 * 64 - 1);
+ assert(backlog_store_first_id(bs) == 1);
+ assert(bs->spare && bs->spare_size == map_size);
+ _test_pages_fill(bs, 0, 1);
+ assert(!bs->spare);
+ assert(_page_find(bs, 3)->map_size == map_size);
+ backlog_store_free(bs);
+ }
+ free(chunk);
+}
+#endif
+
+int
+tytest_backlog_store_grow(void)
+{
+#if defined(__linux__)
+ ecore_init();
+ _test_backings_run(_test_grow);
+ ecore_shutdown();
+#endif
+ return 0;
+}
+
static void
_test_pop(BacklogStoreBacking backing)
{
diff --git a/src/bin/backlog_store.h b/src/bin/backlog_store.h
index c53e4d77..718d37c6 100644
--- a/src/bin/backlog_store.h
+++ b/src/bin/backlog_store.h
@@ -87,7 +87,8 @@ const BacklogStoreGeometry *backlog_store_geometry(const BacklogStore *bs);
void backlog_store_scrollback_set(BacklogStore *bs, uint32_t scrollback);
/* The cells of the newest entry, in the head page, its width in *w and the
- * id of the head in *page_id. The cells do not move while the entry grows.
+ * id of the head in *page_id. The cells may move when backlog_store_append()
+ * grows the head's mapping, which backlog_store_moved_cb_set() reports.
* Returns NULL if the head holds no entry, as when it is new or
* backlog_store_pop() emptied it: backlog_store_append() always leaves one
* there. */
@@ -170,8 +171,10 @@ Eina_Bool backlog_store_page_fd_async(BacklogStore *bs, uint32_t page_id,
* 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
* the page is evicted, until the last backlog_store_release() of the page
- * or backlog_store_free(). Returns NULL for an unknown or evicted page, or
- * if the rebuild fails. */
+ * or backlog_store_free(). The exception is the head, whose mapping may be
+ * replaced as it grows: the moved callback is told, and the page must be
+ * got again. Returns NULL for an unknown or evicted page, or if the rebuild
+ * fails. */
const TermSrvBacklogPage *backlog_store_page_get(BacklogStore *bs,
uint32_t page_id);
@@ -193,6 +196,13 @@ void backlog_store_prefetch_cb_set(BacklogStore *bs,
void (*cb)(void *data, uint32_t page_id),
void *data);
+/* Call cb when the mapping of the head page_id grows for the cells appended
+ * to it. It may have moved: what backlog_store_page_get() gave for it must
+ * be got again. Called from within backlog_store_append(). */
+void backlog_store_moved_cb_set(BacklogStore *bs,
+ void (*cb)(void *data, uint32_t page_id),
+ void *data);
+
/* Count a client holding page_id, or one letting go of it. Holds do not
* keep a page from being evicted: an evicted page is gone from the store,
* but its mappings are only released with its last hold, which must still
diff --git a/src/bin/backlog_view.c b/src/bin/backlog_view.c
index 8c378c86..0db9a54b 100644
--- a/src/bin/backlog_view.c
+++ b/src/bin/backlog_view.c
@@ -13,6 +13,11 @@
#include "backlog_view.h"
#include "backlog_window.h"
+/* FreeBSD removed MAP_NORESERVE */
+#ifndef MAP_NORESERVE
+# define MAP_NORESERVE 0
+#endif
+
#define PAGE_CELLS_MAX \
((size_t)TERMSRV_BACKLOG_PAGE_SEAL_CELLS + TERMSRV_BACKLOG_ENTRY_MAX_CELLS)
@@ -31,7 +36,7 @@ _blank_cells_get(void)
if (_blank_cells)
return _blank_cells;
cells = mmap(NULL, TERMSRV_BACKLOG_ENTRY_MAX_CELLS * sizeof(Termcell),
- PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
if (cells == MAP_FAILED)
{
ERR("backlog_view: mmap blank cells: %s", strerror(errno));
@@ -1019,6 +1024,28 @@ _local_page_prefetched(void *data, uint32_t page_id)
_local_page_map(view, page_id);
}
+/* The page now lives elsewhere: its entries point at it again before
+ * anything reads them */
+static void
+_local_page_moved(void *data, uint32_t page_id)
+{
+ BacklogView *view = data;
+ BacklogViewPage *page = backlog_view_page_find(view, page_id);
+ const TermSrvBacklogPage *mapped;
+
+ if (!page || !page->mapped)
+ return;
+ mapped = backlog_store_page_get(view->store, page_id);
+ if (!mapped)
+ {
+ ERR("backlog_view: page %u moved out of reach", page_id);
+ return;
+ }
+ page->mapped = mapped;
+ _page_entries_repoint(view, page);
+ view->changed = EINA_TRUE;
+}
+
static void
_local_page_release(void *data, uint32_t page_id)
{
@@ -1067,6 +1094,7 @@ backlog_view_local_new(Termpty *ty, BacklogStore *bs)
view->data = ""
view->store = bs;
backlog_store_prefetch_cb_set(bs, _local_page_prefetched, view);
+ backlog_store_moved_cb_set(bs, _local_page_moved, view);
return view;
}
@@ -1170,7 +1198,10 @@ backlog_view_free(BacklogView *view)
if (view->release_idler)
ecore_idler_del(view->release_idler);
if (view->store)
- backlog_store_prefetch_cb_set(view->store, NULL, NULL);
+ {
+ backlog_store_prefetch_cb_set(view->store, NULL, NULL);
+ backlog_store_moved_cb_set(view->store, NULL, NULL);
+ }
while (view->pages_count)
_page_forget_oldest(view);
if (view->ty->backlog_view == view)
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 6f31fb0a..3de4898d 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -5806,6 +5806,216 @@ tytest_backlog_view_local_pop_compressed(void)
return 0;
}
+#if defined(__linux__)
+#include <sys/mman.h>
+
+/* Where the mapping holding addr ends, or NULL if none holds it. */
+static void *
+_ty_mapping_end(const void *addr)
+{
+ FILE *maps = fopen("/proc/self/maps", "r");
+ unsigned long start, end;
+ char line[512];
+ void *found = NULL;
+
+ assert(maps);
+ while (!found && fgets(line, sizeof(line), maps))
+ {
+ if (sscanf(line, "%lx-%lx", &start, &end) == 2 &&
+ (uintptr_t)addr >= start && (uintptr_t)addr < end)
+ found = (void *)(uintptr_t)end;
+ }
+ fclose(maps);
+ return found;
+}
+
+/* Maps a page right after the mapping that holds addr, unless something is
+ * there already. Returns what to unmap, or NULL. */
+static void *
+_ty_after_block(const void *addr)
+{
+ void *end = _ty_mapping_end(addr), *block;
+
+ assert(end);
+ block = mmap(end, 4096, PROT_NONE,
+ MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);
+ return (block == MAP_FAILED) ? NULL : block;
+}
+
+#define TY_LONG_LINE (80 * 4000)
+
+/* A line of TY_LONG_LINE letters, then a newline */
+static void
+_ty_long_line_feed(Termpty *ty)
+{
+ Eina_Unicode *buf = malloc((TY_LONG_LINE + 2) * sizeof(Eina_Unicode));
+ int i;
+
+ assert(buf);
+ for (i = 0; i < TY_LONG_LINE; i++)
+ buf[i] = 'a' + i % 26;
+ buf[i++] = '\r';
+ buf[i++] = '\n';
+ termpty_handle_buf(ty, buf, i);
+ free(buf);
+}
+
+/* No entry of page_id in the ring points into the old_size bytes at old
+ * any more: they point into the size bytes at mapped, unless they read as
+ * blank, the page let go of. */
+static void
+_ty_backlog_page_entries_moved(Termpty *ty, uint32_t page_id,
+ const void *old, size_t old_size,
+ const void *mapped, size_t size)
+{
+ const char *start = mapped, *old_start = old;
+ int y, in = 0;
+
+ termpty_backlog_follow(ty);
+ for (y = 1; (y < (int)ty->backsize) && BACKLOG_ROW_GET(ty, y)->cells; y++)
+ {
+ const Termsave *ts = BACKLOG_ROW_GET(ty, y);
+ const char *cells = (const char *)ts->cells;
+
+ if (ts->page_id != page_id)
+ continue;
+ assert(cells < old_start || cells >= old_start + old_size);
+ if ((cells < start) || (cells >= start + size))
+ continue;
+ assert(cells + ts->w * sizeof(Termcell) <= start + size);
+ in++;
+ }
+ assert(in > 0);
+}
+
+/* The number the entry at y starts with */
+static int
+_ty_backlog_entry_number(Termpty *ty, int y)
+{
+ const Termsave *ts;
+ int n = 0, i;
+
+ termpty_backlog_follow(ty);
+ ts = BACKLOG_ROW_GET(ty, y);
+ assert(ts->cells);
+ for (i = 0; (i < (int)ts->w) && (ts->cells[i].codepoint >= '0') &&
+ (ts->cells[i].codepoint <= '9'); i++)
+ n = n * 10 + ts->cells[i].codepoint - '0';
+ return n;
+}
+
+/* The entry at y is the long line of _ty_long_line_feed() */
+static void
+_ty_backlog_long_line_check(Termpty *ty, int y)
+{
+ const Termsave *ts;
+ int i;
+
+ termpty_backlog_follow(ty);
+ ts = BACKLOG_ROW_GET(ty, y);
+ assert(ts->cells && ts->w == TY_LONG_LINE);
+ for (i = 0; i < TY_LONG_LINE; i++)
+ assert(ts->cells[i].codepoint == (Eina_Unicode)('a' + i % 26));
+}
+#endif
+
+/* Test: a head whose cells outgrow what it maps grows, and moves when
+ * something is mapped right after it: the entries of the ring that pointed
+ * into it point into its new mapping, with the same cells, and where it
+ * was is unmapped. So does a page that pop made the head again, mapped as
+ * far as its cells went. Also for the memfd pages of tymuxd. */
+int
+tytest_backlog_view_local_grow_moved(void)
+{
+#if defined(__linux__)
+ const BacklogStoreGeometry *geo;
+ const TermSrvBacklogPage *old, *moved;
+ Termpty ty;
+ uint32_t head, prev;
+ size_t old_size;
+ void *block;
+ int backing, len, i;
+
+ for (backing = BACKLOG_STORE_ANON; backing <= BACKLOG_STORE_MEMFD;
+ backing++)
+ {
+ /* the head */
+ _ty_test_init(&ty, 80, 24);
+ termpty_backlog_size_set(&ty, 0);
+ termpty_backlog_store_backing_set(&ty, backing);
+ termpty_backlog_size_set(&ty, 2000);
+ geo = backlog_store_geometry(ty.backlog_store);
+ _ty_padded_feed(&ty, 1, 50 + 23, 0);
+ head = backlog_store_first_id(ty.backlog_store) +
+ backlog_store_count(ty.backlog_store) - 1;
+ old = backlog_store_page_get(ty.backlog_store, head);
+ old_size = (char *)_ty_mapping_end(old) - (const char *)old;
+ block = _ty_after_block(old);
+ _ty_long_line_feed(&ty);
+ _ty_padded_feed(&ty, 100, 100 + 23, 0);
+ assert(head == backlog_store_first_id(ty.backlog_store) +
+ backlog_store_count(ty.backlog_store) - 1);
+ moved = backlog_store_page_get(ty.backlog_store, head);
+ assert(moved != old);
+ assert(!_ty_mapping_end(old));
+ _ty_backlog_page_entries_moved(&ty, head, old, old_size, moved,
+ geo->page_size);
+ _ty_backlog_ring_readable(&ty);
+ _ty_backlog_long_line_check(&ty, 2);
+ assert(_ty_backlog_entry_number(&ty, 3) == 50 + 23);
+ _ty_backlog_pages_check(&ty);
+ if (block)
+ munmap(block, 4096);
+ _ty_test_shutdown(&ty);
+
+ /* a compressed page made the head again by pop */
+ _ty_test_init(&ty, 80, 24);
+ termpty_backlog_size_set(&ty, 0);
+ termpty_backlog_store_backing_set(&ty, backing);
+ /* 64 entries a page, 5 pages */
+ termpty_backlog_size_set(&ty, 300);
+ geo = backlog_store_geometry(ty.backlog_store);
+ _ty_padded_feed(&ty, 1, 4 * 64 + 1 + 23, 0);
+ prev = backlog_store_first_id(ty.backlog_store) + 3;
+ len = termpty_backlog_length(&ty);
+ termpty_backlog_viewport_set(&ty, len);
+ _ty_backlog_settle(&ty);
+ assert(!backlog_store_page_ready(ty.backlog_store, prev));
+ /* the view holds it rebuilt, its entries pointing into it */
+ assert(_ty_backlog_number(&ty, 2) == 256);
+ old = backlog_store_page_get(ty.backlog_store, prev);
+ assert(old);
+ /* room in it for what the screen lets go of, the long line too */
+ for (i = 0; i < 32; i++)
+ assert(termpty_backlog_pop(&ty));
+ assert(backlog_store_page_get(ty.backlog_store, prev) == old);
+ old_size = (char *)_ty_mapping_end(old) - (const char *)old;
+ block = _ty_after_block(old);
+ _ty_long_line_feed(&ty);
+ _ty_padded_feed(&ty, 1000, 1000 + 23, 0);
+ moved = backlog_store_page_get(ty.backlog_store, prev);
+ assert(moved != old);
+ assert(!_ty_mapping_end(old));
+ _ty_backlog_page_entries_moved(&ty, prev, old, old_size, moved,
+ geo->page_size);
+ _ty_backlog_ring_readable(&ty);
+ /* the screen went to it, the entries popped are gone */
+ assert(_ty_backlog_entry_number(&ty, 3) == 280);
+ assert(_ty_backlog_entry_number(&ty, 25) == 258);
+ assert(_ty_backlog_entry_number(&ty, 26) == 225);
+ _ty_backlog_long_line_check(&ty, 2);
+ termpty_backlog_viewport_set(&ty, 0);
+ _ty_backlog_settle(&ty);
+ _ty_backlog_ring_readable(&ty);
+ _ty_backlog_rows_check(&ty);
+ if (block)
+ munmap(block, 4096);
+ _ty_test_shutdown(&ty);
+ }
+#endif
+ return 0;
+}
+
/* Test: a full ring keeps the newest entries, one leaving it for each new
* one, rather than a page at a time; taking the newest back shows the last
* one that left again, as long as its page is still there, as a ring
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index d649e243..bff0c468 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -427,7 +427,8 @@ void termpty_palette_changed(Termpty *ty);
/* The cells of row y, negative in the backlog. A backlog row of a pty with
* a store is read from its page, mapped for it if it was not: the cells are
- * valid until the main loop runs again. */
+ * valid until the main loop runs again, or, for a row of the head page, until
+ * a row is saved, which may move them. */
const Termcell *termpty_cellrow_get(Termpty *ty, int y, ssize_t *wret);
/* The same, to be written to. The writer must not change the link_id of
* backlog cells: the store counts the references of its pages from the
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 0a78c467..cdadc1e1 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -178,6 +178,7 @@ static struct {
{ "backlog_store_anon", tytest_backlog_store_anon},
{ "backlog_store_held_evict", tytest_backlog_store_held_evict},
{ "backlog_store_pop", tytest_backlog_store_pop},
+ { "backlog_store_grow", tytest_backlog_store_grow},
{ "backlog_store_next_id", tytest_backlog_store_next_id},
{ "backlog_store_links", tytest_backlog_store_links},
{ "backlog_store_blocks", tytest_backlog_store_blocks},
@@ -202,6 +203,7 @@ static struct {
{ "backlog_view_local_compressed_read", tytest_backlog_view_local_compressed_read},
{ "backlog_view_link_after_rebuild", tytest_backlog_view_link_after_rebuild},
{ "backlog_view_local_pop_compressed", tytest_backlog_view_local_pop_compressed},
+ { "backlog_view_local_grow_moved", tytest_backlog_view_local_grow_moved},
{ "backlog_view_local_prefetch", tytest_backlog_view_local_prefetch},
{ "backlog_view_local_seal_pending", tytest_backlog_view_local_seal_pending},
{ "backlog_view_cell_follow", tytest_backlog_view_cell_follow},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index f09f9be5..f40acda1 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -133,6 +133,7 @@ int tytest_backlog_store_compress_widths(void);
int tytest_backlog_store_anon(void);
int tytest_backlog_store_held_evict(void);
int tytest_backlog_store_pop(void);
+int tytest_backlog_store_grow(void);
int tytest_backlog_store_next_id(void);
int tytest_backlog_store_links(void);
int tytest_backlog_store_blocks(void);
@@ -157,6 +158,7 @@ int tytest_backlog_view_local_slide(void);
int tytest_backlog_view_local_compressed_read(void);
int tytest_backlog_view_link_after_rebuild(void);
int tytest_backlog_view_local_pop_compressed(void);
+int tytest_backlog_view_local_grow_moved(void);
int tytest_backlog_view_local_prefetch(void);
int tytest_backlog_view_local_seal_pending(void);
int tytest_backlog_view_cell_follow(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.