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 c44b860d1ff33832dc053613e416d1ecae90f918
Author: Cedric BAIL <[email protected]>
AuthorDate: Thu Nov 19 19:21:36 2026 -0600
termsrv: size backlog pages from the system page and the scrollback
A page header of a fixed 4096 bytes with 256 entries does not fit
every host: a system page of 16 KiB or more leaves the cells
unaligned, and a scrollback of a million entries would need thousands
of pages while a small one wastes a header on few entries.
The header now takes a whole number of system pages, and the pages
hold scrollback / 16 entries rounded up to a power of two, between 64
and 4096. The page header records both values, so tymux reads them
from the page rather than from constants, and refuses a page whose
header is inconsistent with itself or with the size of its memfd, or
that holds fewer entries than the daemon already sent widths for. The
daemon does not trust the geometry written in a page, which clients
can write to.
The client cannot know the entries per page before the first page
arrives, so its ring is sized for the largest page.
This change is not meant to speed up the intake path. tybenchd -i 5
1000000 was run to check that it does not get slower: release build,
median of 5 iterations, three runs each, before (the parent commit) and
after:
before: 0.364 s 18.9 MB/s, 0.354 s 19.5 MB/s, 0.361 s 19.1 MB/s
after: 0.339 s 20.3 MB/s, 0.342 s 20.1 MB/s, 0.343 s 20.1 MB/s
---
src/bin/backlog_store.c | 296 +++++++++++++++++++++++++++++--------------
src/bin/backlog_store.h | 24 +++-
src/bin/termsrv.c | 6 +-
src/bin/termsrv.h | 71 ++++++++---
src/bin/termtymux.c | 324 +++++++++++++++++++++++++++++++++++++++++-------
src/bin/termtymux.h | 2 +
src/bin/tytest.c | 4 +
src/bin/tytest_tymux.c | 43 +++++--
src/bin/unit_tests.h | 3 +
9 files changed, 597 insertions(+), 176 deletions(-)
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index eb23846b..b26798e7 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -65,6 +65,8 @@ typedef struct
double idle_since;
uint32_t attempts;
size_t cells_size; /* the bytes of cells in use */
+ uint32_t header_size; /* the store's, kept for _page_close() */
+ size_t map_size;
Eina_Binbuf *blob;
TermSrvBacklogPage *export_map;
size_t export_size;
@@ -80,6 +82,7 @@ typedef struct
* ends */
const TermSrvCell *cells;
void *orphan_map;
+ size_t orphan_size;
size_t cells_size;
Eina_Binbuf *blob;
} BacklogStoreJob;
@@ -92,7 +95,7 @@ struct tag_BacklogStore
uint32_t count;
uint32_t alloc;
uint32_t first_id;
- uint32_t max_entries;
+ BacklogStoreGeometry geo;
uint32_t head_cells;
uint64_t total_entries;
Eina_List *jobs;
@@ -103,11 +106,46 @@ struct tag_BacklogStore
/* Compression jobs not ended yet, of every store */
static unsigned int _jobs_alive = 0;
-static TermSrvCell *
-_page_cells(TermSrvBacklogPage *header)
+void
+backlog_store_geometry_get(uint32_t scrollback, size_t sys_page_size,
+ BacklogStoreGeometry *geo)
{
- return (TermSrvCell *)
- ((unsigned char *)header + TERMSRV_BACKLOG_HEADER_SIZE);
+ uint64_t wanted = ((uint64_t)scrollback + 15) / 16;
+ uint64_t entries = TERMSRV_BACKLOG_ENTRIES_MIN;
+ uint64_t max_entries;
+
+ while (entries < wanted && entries < TERMSRV_BACKLOG_ENTRIES_MAX)
+ entries <<= 1;
+ max_entries = ((uint64_t)scrollback + entries - 1) / entries * entries;
+ if (max_entries == 0)
+ max_entries = entries;
+ if (max_entries > UINT32_MAX)
+ max_entries -= entries;
+ geo->entries_per_page = entries;
+ geo->max_entries = max_entries;
+ geo->header_size = termsrv_backlog_header_size(entries, sys_page_size);
+ geo->page_size = termsrv_backlog_page_size(geo->header_size);
+}
+
+static TermSrvCell *
+_page_cells(TermSrvBacklogPage *header, uint32_t header_size)
+{
+ return (TermSrvCell *)((unsigned char *)header + header_size);
+}
+
+/* Not termsrv_backlog_entry_*(): the clients can write the header, so the
+ * daemon never trusts the geometry it holds. */
+static uint32_t *
+_page_entry_off(TermSrvBacklogPage *header)
+{
+ return (uint32_t *)(header + 1);
+}
+
+static _Atomic uint32_t *
+_page_entry_w(const BacklogStore *bs, TermSrvBacklogPage *header)
+{
+ return (_Atomic uint32_t *)(_page_entry_off(header) +
+ bs->geo.entries_per_page);
}
static BacklogStorePage *
@@ -144,7 +182,7 @@ _page_close(BacklogStorePage *page)
return;
}
if (page->header)
- munmap(page->header, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap(page->header, page->map_size);
if (page->fd >= 0)
close(page->fd);
}
@@ -166,7 +204,7 @@ _page_memfd_map(const BacklogStore *bs, const char *kind, uint32_t page_id,
ERR("memfd_create: %s", strerror(errno));
return NULL;
}
- if (ftruncate(fd, TERMSRV_BACKLOG_PAGE_SIZE) < 0)
+ if (ftruncate(fd, bs->geo.page_size) < 0)
{
ERR("ftruncate: %s", strerror(errno));
close(fd);
@@ -233,12 +271,12 @@ _page_gain(size_t cells_size, size_t blob_len)
static Eina_Bool
_page_compressed_set(BacklogStorePage *page, Eina_Binbuf *blob)
{
- TermSrvBacklogPage *copy = malloc(sizeof(*copy));
+ TermSrvBacklogPage *copy = malloc(page->header_size);
if (!copy)
return EINA_FALSE;
- memcpy(copy, page->header, sizeof(*copy));
- munmap(page->header, TERMSRV_BACKLOG_PAGE_SIZE);
+ memcpy(copy, page->header, page->header_size);
+ munmap(page->header, page->map_size);
if (page->fd >= 0)
close(page->fd);
page->header = copy;
@@ -272,9 +310,11 @@ _page_add(BacklogStore *bs)
memset(page, 0, sizeof(*page));
page->export_fd = -1;
page->header = _page_map(bs, "ty-backlog", page_id,
- TERMSRV_BACKLOG_PAGE_SIZE, &page->fd);
+ bs->geo.page_size, &page->fd);
if (!page->header)
return NULL;
+ page->header_size = bs->geo.header_size;
+ page->map_size = bs->geo.page_size;
if (bs->count > 0)
{
bs->pages[bs->count - 1].idle_since = ecore_time_get();
@@ -284,6 +324,8 @@ _page_add(BacklogStore *bs)
page->header->magic = TERMSRV_BACKLOG_MAGIC;
page->header->version = TERMSRV_BACKLOG_VERSION;
page->header->page_id = page_id;
+ page->header->header_size = bs->geo.header_size;
+ page->header->entries_per_page = bs->geo.entries_per_page;
bs->count++;
bs->head_cells = 0;
return page->header;
@@ -319,7 +361,7 @@ _evict(BacklogStore *bs)
/* the job still reads it; its end evicts again */
if (bs->pages[0].state == PAGE_COMPRESSING)
return;
- if (bs->total_entries - oldest < bs->max_entries)
+ if (bs->total_entries - oldest < bs->geo.max_entries)
return;
if (bs->pages[0].refs == 0)
_page_close(&bs->pages[0]);
@@ -336,9 +378,10 @@ _evict(BacklogStore *bs)
BacklogStore *
backlog_store_new(const char *name, BacklogStoreBacking backing,
- uint32_t max_entries)
+ uint32_t scrollback)
{
BacklogStore *bs;
+ long sys_page_size;
EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
#if !defined(__linux__)
@@ -353,7 +396,9 @@ backlog_store_new(const char *name, BacklogStoreBacking backing,
return NULL;
bs->name = eina_stringshare_add(name);
bs->backing = backing;
- bs->max_entries = max_entries;
+ sys_page_size = sysconf(_SC_PAGESIZE);
+ backlog_store_geometry_get(scrollback, sys_page_size > 0 ? sys_page_size :
+ 4096, &bs->geo);
return bs;
}
@@ -379,6 +424,7 @@ backlog_store_free(BacklogStore *bs)
continue;
}
job->orphan_map = page->header;
+ job->orphan_size = page->map_size;
page->header = NULL;
}
for (i = 0; i < bs->count; i++)
@@ -420,16 +466,16 @@ 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)
return EINA_TRUE;
- memcpy(_page_cells(head) + bs->head_cells, cells,
- add * sizeof(Termcell));
- atomic_store_explicit(&head->entry_w[n - 1], cur_w + add,
+ memcpy(_page_cells(head, bs->geo.header_size) + bs->head_cells,
+ cells, add * sizeof(Termcell));
+ atomic_store_explicit(&_page_entry_w(bs, head)[n - 1], cur_w + add,
memory_order_release);
head_page->last_w = cur_w + add;
bs->head_cells += add;
return EINA_TRUE;
}
- if (!head || n >= TERMSRV_BACKLOG_PAGE_ENTRIES ||
+ if (!head || n >= bs->geo.entries_per_page ||
bs->head_cells >= TERMSRV_BACKLOG_PAGE_SEAL_CELLS)
{
head = _page_add(bs);
@@ -440,10 +486,11 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
}
add = MIN((size_t)w, TERMSRV_BACKLOG_ENTRY_MAX_CELLS);
if (add > 0)
- memcpy(_page_cells(head) + bs->head_cells, cells,
- add * sizeof(Termcell));
- head->entry_off[n] = bs->head_cells;
- atomic_store_explicit(&head->entry_w[n], add, memory_order_release);
+ memcpy(_page_cells(head, bs->geo.header_size) + bs->head_cells,
+ cells, add * sizeof(Termcell));
+ _page_entry_off(head)[n] = bs->head_cells;
+ atomic_store_explicit(&_page_entry_w(bs, head)[n], add,
+ memory_order_release);
atomic_store_explicit(&head->num_entries, n + 1, memory_order_release);
head_page->entries = n + 1;
head_page->last_w = add;
@@ -473,15 +520,15 @@ backlog_store_count(const BacklogStore *bs)
static Eina_Bool
_page_export(const BacklogStore *bs, BacklogStorePage *page)
{
- size_t size = TERMSRV_BACKLOG_HEADER_SIZE + page->cells_size;
+ size_t size = page->header_size + page->cells_size;
unsigned char *map;
int fd;
map = _page_map(bs, "ty-export", page->header->page_id, size, &fd);
if (!map)
return EINA_FALSE;
- memcpy(map, page->header, sizeof(*page->header));
- if (!backlog_uncompress(page->blob, map + TERMSRV_BACKLOG_HEADER_SIZE,
+ memcpy(map, page->header, page->header_size);
+ if (!backlog_uncompress(page->blob, map + page->header_size,
page->cells_size))
{
ERR("cannot rebuild page %u of '%s'", page->header->page_id,
@@ -594,7 +641,7 @@ _job_free(BacklogStoreJob *job)
{
_jobs_alive--;
if (job->orphan_map)
- munmap(job->orphan_map, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap(job->orphan_map, job->orphan_size);
if (job->blob)
eina_binbuf_free(job->blob);
free(job);
@@ -679,7 +726,7 @@ _job_start(BacklogStore *bs, BacklogStorePage *page)
return;
}
job->bs = bs;
- job->cells = _page_cells(page->header);
+ job->cells = _page_cells(page->header, page->header_size);
job->page_id = page->header->page_id;
job->cells_size = page->cells_size;
page->state = PAGE_COMPRESSING;
@@ -738,7 +785,7 @@ backlog_store_map_write(BacklogStore *bs, uint32_t first, uint32_t count,
if (termsrv_backlog_map_page_append(buf, bufsz, &off, page_id,
page->entries,
- page->header->entry_w) < 0)
+ _page_entry_w(bs, page->header)) < 0)
break;
echo.count++;
}
@@ -759,6 +806,72 @@ _test_row_fill(Termcell *cells, int w, Eina_Unicode first)
cells[i].codepoint = first + i;
}
+/* 4096 entries of scrollback make pages of 256 entries */
+#define _TEST_ENTRIES 256u
+#define _TEST_SCROLLBACK 4096u
+
+static const uint32_t *
+_test_off(const TermSrvBacklogPage *page)
+{
+ return termsrv_backlog_entry_off((TermSrvBacklogPage *)page);
+}
+
+static const _Atomic uint32_t *
+_test_w(const TermSrvBacklogPage *page)
+{
+ return termsrv_backlog_entry_w((TermSrvBacklogPage *)page,
+ page->entries_per_page);
+}
+
+/* Test: the geometry of the pages follows the scrollback and the system
+ * page size. */
+int
+tytest_backlog_store_geometry(void)
+{
+ const struct
+ {
+ uint32_t scrollback;
+ size_t sys_page_size;
+ uint32_t entries_per_page, max_entries, header_size;
+ } cases[] = {
+ { 2000, 4096, 128, 2048, 4096 },
+ { 10000, 4096, 1024, 10240, 12288 },
+ { 100, 4096, 64, 128, 4096 },
+ { 1000000, 4096, 4096, 1003520, 36864 },
+ { 1000000, 16384, 4096, 1003520, 49152 },
+ { 1, 4096, 64, 64, 4096 },
+ { 0, 4096, 64, 64, 4096 },
+ { UINT32_MAX, 4096, 4096, UINT32_MAX - (UINT32_MAX % 4096), 36864 },
+ };
+ unsigned int i;
+
+ for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++)
+ {
+ BacklogStoreGeometry geo;
+
+ backlog_store_geometry_get(cases[i].scrollback, cases[i].sys_page_size,
+ &geo);
+ assert(geo.entries_per_page == cases[i].entries_per_page);
+ assert(geo.max_entries == cases[i].max_entries);
+ assert(geo.header_size == cases[i].header_size);
+ assert(geo.page_size == termsrv_backlog_page_size(geo.header_size));
+ }
+ return 0;
+}
+
+/* A store of pages of _TEST_ENTRIES entries that evicts at max_entries,
+ * which the geometry would round up to whole pages. */
+static BacklogStore *
+_test_store_new(BacklogStoreBacking backing, uint32_t max_entries)
+{
+ BacklogStore *bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+
+ assert(bs);
+ assert(bs->geo.entries_per_page == _TEST_ENTRIES);
+ bs->geo.max_entries = max_entries;
+ return bs;
+}
+
typedef void (*Test_Backing_Cb)(BacklogStoreBacking backing);
static void
@@ -787,7 +900,7 @@ _test_page_map(BacklogStore *bs, uint32_t page_id)
else
{
assert(fd >= 0);
- page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
+ page = mmap(NULL, bs->geo.page_size, PROT_READ, MAP_SHARED,
fd, 0);
assert(page != MAP_FAILED);
}
@@ -801,17 +914,17 @@ static const TermSrvCell *
_test_page_cells(const TermSrvBacklogPage *page)
{
return (const TermSrvCell *)
- ((const unsigned char *)page + TERMSRV_BACKLOG_HEADER_SIZE);
+ ((const unsigned char *)page + page->header_size);
}
static void
_test_page_unmap(const BacklogStore *bs, const TermSrvBacklogPage *page)
{
if (bs->backing == BACKLOG_STORE_MEMFD)
- munmap((void *)page, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap((void *)page, bs->geo.page_size);
}
-/* Test: entries fill pages of TERMSRV_BACKLOG_PAGE_ENTRIES, a row that
+/* Test: entries fill pages of _TEST_ENTRIES, a row that
* extends grows the last entry in place, and a client mapping the head
* follows it live. A page also seals on TERMSRV_BACKLOG_PAGE_SEAL_CELLS,
* an entry stops growing at TERMSRV_BACKLOG_ENTRY_MAX_CELLS, and extends
@@ -825,7 +938,7 @@ _test_pages(BacklogStoreBacking backing)
Termcell row[10], *big;
uint32_t i, last;
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
assert(backlog_store_count(bs) == 0);
for (i = 0; i < 600; i++)
@@ -846,32 +959,32 @@ _test_pages(BacklogStoreBacking backing)
assert(backlog_store_count(bs) == 3);
assert(atomic_load(&page->num_entries) == 600 - 2 * 256 + 1);
last = atomic_load(&page->num_entries) - 1;
- assert(page->entry_off[last] == 600 - 2 * 256);
- assert(atomic_load(&page->entry_w[last]) == 30);
- assert(atomic_load(&page->entry_w[last - 1]) == 1);
+ assert(_test_off(page)[last] == 600 - 2 * 256);
+ assert(atomic_load(&_test_w(page)[last]) == 30);
+ assert(atomic_load(&_test_w(page)[last - 1]) == 1);
page_cells = _test_page_cells(page);
- assert(page_cells[page->entry_off[last - 1]].codepoint == 599);
+ assert(page_cells[_test_off(page)[last - 1]].codepoint == 599);
for (i = 0; i < 30; i++)
- assert(page_cells[page->entry_off[last] + i].codepoint == 'a' + i);
+ assert(page_cells[_test_off(page)[last] + i].codepoint == 'a' + i);
_test_page_unmap(bs, page);
/* the memfd is writable by the clients: what they put in the head must
* not steer the daemon */
if (backing == BACKLOG_STORE_MEMFD)
{
- TermSrvBacklogPage *rw = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE,
+ TermSrvBacklogPage *rw = mmap(NULL, bs->geo.page_size,
PROT_READ | PROT_WRITE, MAP_SHARED,
backlog_store_page_fd(bs, 2), 0);
assert(rw != MAP_FAILED);
atomic_store(&rw->num_entries, 0x7fffffff);
- atomic_store(&rw->entry_w[last], 0xffffffff);
+ atomic_store(&_test_w(rw)[last], 0xffffffff);
_test_row_fill(row, 10, 'a');
assert(backlog_store_append(bs, row, 10, EINA_TRUE));
- assert(atomic_load(&rw->entry_w[last]) == 40);
+ assert(atomic_load(&_test_w(rw)[last]) == 40);
assert(backlog_store_append(bs, row, 10, EINA_FALSE));
assert(atomic_load(&rw->num_entries) == last + 2);
- munmap(rw, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap(rw, bs->geo.page_size);
}
assert(backlog_store_page_fd(bs, 3) < 0);
@@ -885,7 +998,7 @@ _test_pages(BacklogStoreBacking backing)
big = calloc(TERMSRV_BACKLOG_ENTRY_MAX_CELLS, sizeof(Termcell));
assert(big);
big[0].codepoint = 'x';
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
assert(backlog_store_append(bs, big, TERMSRV_BACKLOG_ENTRY_MAX_CELLS - 5,
EINA_TRUE));
@@ -893,7 +1006,7 @@ _test_pages(BacklogStoreBacking backing)
assert(backlog_store_append(bs, row, 10, EINA_TRUE));
page = _test_page_map(bs, 0);
assert(atomic_load(&page->num_entries) == 1);
- assert(atomic_load(&page->entry_w[0]) == TERMSRV_BACKLOG_ENTRY_MAX_CELLS);
+ assert(atomic_load(&_test_w(page)[0]) == TERMSRV_BACKLOG_ENTRY_MAX_CELLS);
page_cells = _test_page_cells(page);
assert(page_cells[0].codepoint == 'x');
assert(page_cells[TERMSRV_BACKLOG_ENTRY_MAX_CELLS - 1].codepoint == 'e');
@@ -903,7 +1016,7 @@ _test_pages(BacklogStoreBacking backing)
assert(backlog_store_count(bs) == 2);
page = _test_page_map(bs, 1);
assert(atomic_load(&page->num_entries) == 1);
- assert(page->entry_off[0] == 0);
+ assert(_test_off(page)[0] == 0);
assert(_test_page_cells(page)[0].codepoint == 'a');
_test_page_unmap(bs, page);
backlog_store_free(bs);
@@ -929,7 +1042,7 @@ _test_evict(BacklogStoreBacking backing)
TermSrvMsgBacklogMapReq *echo = (TermSrvMsgBacklogMapReq *)buf;
int page0_fd = -1;
- bs = backlog_store_new("test", backing, 300);
+ bs = _test_store_new(backing, 300);
assert(bs);
_test_row_fill(row, 1, 0);
assert(backlog_store_append(bs, row, 1, EINA_FALSE));
@@ -954,7 +1067,7 @@ _test_evict(BacklogStoreBacking backing)
kept += atomic_load(&page->num_entries);
_test_page_unmap(bs, page);
}
- assert(kept >= 300 && kept < 300 + TERMSRV_BACKLOG_PAGE_ENTRIES);
+ assert(kept >= 300 && kept < 300 + _TEST_ENTRIES);
backlog_store_release(bs, 0);
assert(backlog_store_map_write(bs, 0, 100, buf, sizeof(buf)) >=
@@ -963,14 +1076,14 @@ _test_evict(BacklogStoreBacking backing)
if (backing == BACKLOG_STORE_MEMFD)
{
- page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
+ page = mmap(NULL, bs->geo.page_size, PROT_READ, MAP_SHARED,
page0_fd, 0);
assert(page != MAP_FAILED);
assert(page->magic == TERMSRV_BACKLOG_MAGIC && page->page_id == 0);
assert(atomic_load(&page->num_entries) ==
- TERMSRV_BACKLOG_PAGE_ENTRIES);
- assert(_test_page_cells(page)[TERMSRV_BACKLOG_PAGE_ENTRIES - 1]
- .codepoint == TERMSRV_BACKLOG_PAGE_ENTRIES - 1);
+ _TEST_ENTRIES);
+ assert(_test_page_cells(page)[_TEST_ENTRIES - 1]
+ .codepoint == _TEST_ENTRIES - 1);
_test_page_unmap(bs, page);
close(page0_fd);
}
@@ -987,7 +1100,7 @@ tytest_backlog_store_evict(void)
static uint32_t
_test_map_width(uint32_t page_id, uint32_t entry)
{
- return (page_id * TERMSRV_BACKLOG_PAGE_ENTRIES + entry) % 7;
+ return (page_id * _TEST_ENTRIES + entry) % 7;
}
/* Test: backlog_store_map_write() lists the widths of the pages asked for
@@ -1002,7 +1115,7 @@ _test_map(BacklogStoreBacking backing)
Termcell row[7];
uint32_t i, j, len, off, page_id, num_entries, two_pages;
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
_test_row_fill(row, 7, 'a');
for (i = 0; i < 600; i++)
@@ -1017,8 +1130,8 @@ _test_map(BacklogStoreBacking backing)
assert(termsrv_backlog_map_page_next(buf, len, &off, &page_id,
&num_entries, &widths) == 1);
assert(page_id == i);
- assert(num_entries == (i < 2 ? TERMSRV_BACKLOG_PAGE_ENTRIES :
- 600 - 2 * TERMSRV_BACKLOG_PAGE_ENTRIES));
+ assert(num_entries == (i < 2 ? _TEST_ENTRIES :
+ 600 - 2 * _TEST_ENTRIES));
for (j = 0; j < num_entries; j++)
assert(widths[j] == _test_map_width(i, j));
}
@@ -1026,7 +1139,7 @@ _test_map(BacklogStoreBacking backing)
&num_entries, &widths) == 0);
two_pages = sizeof(*echo) +
- 2 * (2 + TERMSRV_BACKLOG_PAGE_ENTRIES) * sizeof(uint32_t);
+ 2 * (2 + _TEST_ENTRIES) * sizeof(uint32_t);
len = backlog_store_map_write(bs, 0, 3, buf, two_pages + 4);
assert(len == two_pages);
assert(echo->first_page_id == 0 && echo->count == 2);
@@ -1121,13 +1234,13 @@ _test_page_check(BacklogStore *bs, uint32_t page_id)
assert(n > 0);
for (i = 0; i < n; i++)
{
- uint32_t entry = page_id * TERMSRV_BACKLOG_PAGE_ENTRIES + i;
+ uint32_t entry = page_id * _TEST_ENTRIES + i;
_test_row_fill(row, TEST_ROW_W, 'a' + entry % 26);
- assert(atomic_load(&page->entry_w[i]) == TEST_ROW_W);
- assert(page->entry_off[i] == i * TEST_ROW_W);
+ assert(atomic_load(&_test_w(page)[i]) == TEST_ROW_W);
+ assert(_test_off(page)[i] == i * TEST_ROW_W);
for (j = 0; j < TEST_ROW_W; j++)
- assert(!memcmp(&cells[page->entry_off[i] + j], &row[j],
+ assert(!memcmp(&cells[_test_off(page)[i] + j], &row[j],
sizeof(row[j])));
}
_test_page_unmap(bs, page);
@@ -1152,7 +1265,7 @@ _test_compress(BacklogStoreBacking backing)
int fd, client_fd = -1, raw_fd;
ecore_init();
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
_test_pages_fill(bs, 0, 600);
assert(backlog_store_count(bs) == 3);
@@ -1198,18 +1311,18 @@ _test_compress(BacklogStoreBacking backing)
assert(_page_find(bs, 0)->state == PAGE_COMPRESSED);
if (backing == BACKLOG_STORE_MEMFD)
{
- client_page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ,
+ client_page = mmap(NULL, bs->geo.page_size, PROT_READ,
MAP_SHARED, client_fd, 0);
assert(client_page != MAP_FAILED);
assert(client_page->page_id == 0);
- munmap((void *)client_page, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap((void *)client_page, bs->geo.page_size);
close(client_fd);
}
_test_page_check(bs, 0);
backlog_store_free(bs);
/* a held page stays raw */
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
_test_pages_fill(bs, 0, 600);
backlog_store_hold(bs, 0);
@@ -1238,7 +1351,7 @@ _test_compress(BacklogStoreBacking backing)
backlog_store_free(bs);
/* the oldest page is not evicted while it is compressed */
- bs = backlog_store_new("test", backing, 300);
+ bs = _test_store_new(backing, 300);
assert(bs);
_test_pages_fill(bs, 0, 300);
backlog_store_maintain(bs, ecore_time_get() + 6.0);
@@ -1257,7 +1370,7 @@ _test_compress(BacklogStoreBacking backing)
assert(big);
for (i = 0; i < TERMSRV_BACKLOG_PAGE_SEAL_CELLS; i++)
big[i].codepoint = 'a' + i % 26;
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
for (i = 0; i < 3; i++)
assert(backlog_store_append(bs, big, TERMSRV_BACKLOG_PAGE_SEAL_CELLS,
@@ -1297,15 +1410,15 @@ _test_varying_check(BacklogStore *bs, uint32_t page_id)
Termcell row[70];
uint32_t i, off = 0, n = atomic_load(&page->num_entries);
- assert(n == TERMSRV_BACKLOG_PAGE_ENTRIES);
+ assert(n == _TEST_ENTRIES);
for (i = 0; i < n; i++)
{
- uint32_t entry = page_id * TERMSRV_BACKLOG_PAGE_ENTRIES + i;
+ uint32_t entry = page_id * _TEST_ENTRIES + i;
uint32_t w = _test_varying_w(entry);
_test_row_fill(row, w, 'a' + entry % 26);
- assert(page->entry_off[i] == off);
- assert(atomic_load(&page->entry_w[i]) == w);
+ assert(_test_off(page)[i] == off);
+ assert(atomic_load(&_test_w(page)[i]) == w);
assert(!memcmp(&cells[off], row, w * sizeof(Termcell)));
off += w;
}
@@ -1321,7 +1434,7 @@ _test_compress_skip(BacklogStoreBacking backing)
uint32_t i, seed = 12345;
ecore_init();
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
/* page 0: noise, a blob that is not smaller than 85% of the cells */
for (i = 0; i < 300; i++)
@@ -1346,7 +1459,7 @@ _test_compress_skip(BacklogStoreBacking backing)
backlog_store_free(bs);
/* pages too small to free memory, and pages without any cell */
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
for (i = 0; i < 256; i++)
assert(backlog_store_append(bs, NULL, 0, EINA_FALSE));
@@ -1381,7 +1494,7 @@ _test_compress_widths(BacklogStoreBacking backing)
uint32_t i;
ecore_init();
- bs = backlog_store_new("test", backing, 100000);
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
assert(bs);
for (i = 0; i < 600; i++)
{
@@ -1456,14 +1569,14 @@ _test_anon_page_check(const TermSrvBacklogPage *page, uint32_t first_entry)
Termcell row[TEST_ANON_ROW_W];
uint32_t i, n = atomic_load(&page->num_entries);
- assert(n == TERMSRV_BACKLOG_PAGE_ENTRIES);
+ assert(n == _TEST_ENTRIES);
for (i = 0; i < n; i++)
{
_test_row_fill(row, TEST_ANON_ROW_W,
(first_entry + i) * TEST_ANON_ROW_W);
- assert(page->entry_off[i] == i * TEST_ANON_ROW_W);
- assert(atomic_load(&page->entry_w[i]) == TEST_ANON_ROW_W);
- assert(!memcmp(&cells[page->entry_off[i]], row, sizeof(row)));
+ assert(_test_off(page)[i] == i * TEST_ANON_ROW_W);
+ assert(atomic_load(&_test_w(page)[i]) == TEST_ANON_ROW_W);
+ assert(!memcmp(&cells[_test_off(page)[i]], row, sizeof(row)));
}
}
@@ -1486,7 +1599,7 @@ tytest_backlog_store_anon(void)
#if defined(__linux__)
fds = _test_fd_count();
#endif
- bs = backlog_store_new("test", BACKLOG_STORE_ANON, 2048);
+ bs = _test_store_new(BACKLOG_STORE_ANON, 2048);
assert(bs);
for (i = 0; i < 1000; i++)
{
@@ -1506,7 +1619,7 @@ tytest_backlog_store_anon(void)
raw = page;
assert(!backlog_store_page_get(bs, 4));
#if defined(__linux__)
- assert(_test_range_mapped(raw, TERMSRV_BACKLOG_PAGE_SIZE));
+ assert(_test_range_mapped(raw, bs->geo.page_size));
#endif
/* the oldest pages go first: page 0 is in the first round */
@@ -1515,7 +1628,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, TERMSRV_BACKLOG_PAGE_SIZE));
+ assert(!_test_range_mapped(raw, bs->geo.page_size));
#else
(void)raw;
#endif
@@ -1548,13 +1661,13 @@ _test_page_mapping_check(const TermSrvBacklogPage *page, uint32_t page_id)
uint32_t i, n = atomic_load(&page->num_entries);
assert(page->magic == TERMSRV_BACKLOG_MAGIC && page->page_id == page_id);
- assert(n == TERMSRV_BACKLOG_PAGE_ENTRIES);
+ assert(n == _TEST_ENTRIES);
for (i = 0; i < n; i++)
{
_test_row_fill(row, TEST_ROW_W,
- 'a' + (page_id * TERMSRV_BACKLOG_PAGE_ENTRIES + i) % 26);
- assert(atomic_load(&page->entry_w[i]) == TEST_ROW_W);
- assert(!memcmp(&cells[page->entry_off[i]], row, sizeof(row)));
+ 'a' + (page_id * _TEST_ENTRIES + i) % 26);
+ assert(atomic_load(&_test_w(page)[i]) == TEST_ROW_W);
+ assert(!memcmp(&cells[_test_off(page)[i]], row, sizeof(row)));
}
}
@@ -1562,12 +1675,12 @@ static void
_test_held_evict(BacklogStoreBacking backing)
{
const TermSrvBacklogPage *raw, *rebuilt;
- size_t rebuilt_size;
+ size_t rebuilt_size, page_size;
BacklogStore *bs;
ecore_init();
/* a raw page, held, outlives its eviction until released */
- bs = backlog_store_new("test", backing, 300);
+ bs = _test_store_new(backing, 300);
assert(bs);
_test_pages_fill(bs, 0, 300);
backlog_store_hold(bs, 0);
@@ -1581,18 +1694,18 @@ _test_held_evict(BacklogStoreBacking backing)
backlog_store_hold(bs, 0);
_test_page_mapping_check(raw, 0);
#if defined(__linux__)
- assert(_test_range_mapped(raw, TERMSRV_BACKLOG_PAGE_SIZE));
+ assert(_test_range_mapped(raw, bs->geo.page_size));
#endif
backlog_store_release(bs, 0);
#if defined(__linux__)
- assert(!_test_range_mapped(raw, TERMSRV_BACKLOG_PAGE_SIZE));
+ assert(!_test_range_mapped(raw, bs->geo.page_size));
#endif
assert(!bs->detached);
backlog_store_release(bs, 0);
backlog_store_free(bs);
/* so does a compressed page, through its rebuilt mapping, held twice */
- bs = backlog_store_new("test", backing, 300);
+ bs = _test_store_new(backing, 300);
assert(bs);
_test_pages_fill(bs, 0, 300);
backlog_store_maintain(bs, ecore_time_get() + 6.0);
@@ -1621,7 +1734,8 @@ _test_held_evict(BacklogStoreBacking backing)
backlog_store_free(bs);
/* freeing the store ends the holds */
- bs = backlog_store_new("test", backing, 300);
+ 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);
@@ -1630,7 +1744,7 @@ _test_held_evict(BacklogStoreBacking backing)
assert(eina_list_count(bs->detached) == 1);
backlog_store_free(bs);
#if defined(__linux__)
- assert(!_test_range_mapped(raw, TERMSRV_BACKLOG_PAGE_SIZE));
+ assert(!_test_range_mapped(raw, page_size));
#endif
ecore_shutdown();
}
diff --git a/src/bin/backlog_store.h b/src/bin/backlog_store.h
index 2bc85e9c..571f06ab 100644
--- a/src/bin/backlog_store.h
+++ b/src/bin/backlog_store.h
@@ -16,11 +16,27 @@ typedef enum
BACKLOG_STORE_MEMFD
} BacklogStoreBacking;
-/* A store that evicts its oldest pages once the others still hold
- * max_entries entries. The memfds of a MEMFD store are named
- * "ty-backlog-<name>-<id>". Returns NULL on failure. */
+typedef struct
+{
+ uint32_t entries_per_page;
+ uint32_t max_entries;
+ uint32_t header_size;
+ size_t page_size;
+} BacklogStoreGeometry;
+
+/* The page geometry for a scrollback of scrollback entries, with pages
+ * whose header is a multiple of sys_page_size: entries_per_page is
+ * scrollback / 16 rounded up to a power of two within
+ * [TERMSRV_BACKLOG_ENTRIES_MIN, TERMSRV_BACKLOG_ENTRIES_MAX], and
+ * max_entries is scrollback rounded up to a whole number of pages. */
+void backlog_store_geometry_get(uint32_t scrollback, size_t sys_page_size,
+ BacklogStoreGeometry *geo);
+
+/* A store that evicts its oldest pages once the others still hold as many
+ * entries as the geometry of scrollback gives. The memfds of a MEMFD
+ * store are named "ty-backlog-<name>-<id>". Returns NULL on failure. */
BacklogStore *backlog_store_new(const char *name, BacklogStoreBacking backing,
- uint32_t max_entries);
+ uint32_t scrollback);
/* Frees the store, along with the pages still held: the mappings that
* backlog_store_page_get() returned are invalid from then on. */
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 9ec6d62f..471987b8 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -1725,8 +1725,8 @@ tytest_termsrv_backlog_page_fd(void)
int
tytest_termsrv_backlog_map(void)
{
- _Alignas(uint32_t) unsigned char buf[2048];
- _Atomic uint32_t widths[TERMSRV_BACKLOG_PAGE_ENTRIES];
+ _Alignas(uint32_t) unsigned char buf[4 * (TERMSRV_BACKLOG_ENTRIES_MAX + 4)];
+ _Atomic uint32_t widths[TERMSRV_BACKLOG_ENTRIES_MAX];
const uint32_t *got;
uint32_t off = 0, end, page_id, num_entries, bad;
@@ -1762,7 +1762,7 @@ tytest_termsrv_backlog_map(void)
&num_entries, &got) < 0);
/* more entries than a page holds */
- bad = TERMSRV_BACKLOG_PAGE_ENTRIES + 1;
+ bad = TERMSRV_BACKLOG_ENTRIES_MAX + 1;
memcpy(buf, &(uint32_t){ 9 }, sizeof(uint32_t));
memcpy(buf + sizeof(uint32_t), &bad, sizeof(bad));
memset(buf + 2 * sizeof(uint32_t), 0, bad * sizeof(uint32_t));
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index 42a23194..dad81d20 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -94,22 +94,21 @@ typedef struct
#define TERMSRV_SHM_CELLS(shm) \
((TermSrvCell *)((uint8_t *)(shm) + TERMSRV_SHM_HEADER_SIZE))
-/* A backlog page is a memfd: a TERMSRV_BACKLOG_HEADER_SIZE header, then the
- * cells of its entries packed with no gap, entry_off[] in cells. A page takes
- * no new entry once it holds TERMSRV_BACKLOG_PAGE_ENTRIES entries or
- * TERMSRV_BACKLOG_PAGE_SEAL_CELLS cells. The entry in progress may still
- * grow to TERMSRV_BACKLOG_ENTRY_MAX_CELLS, so it always fits. The memfd is
- * sparse: only the cells actually written take memory. */
+/* A backlog page is a memfd: a header_size header, then the cells of its
+ * entries packed with no gap, entry_off[] in cells. The header is the
+ * TermSrvBacklogPage fields followed by entry_off[entries_per_page] and
+ * entry_w[entries_per_page]; header_size is a multiple of the system page
+ * size so the cells start page aligned. A page takes no new entry once it
+ * holds entries_per_page entries or TERMSRV_BACKLOG_PAGE_SEAL_CELLS cells.
+ * The entry in progress may still grow to TERMSRV_BACKLOG_ENTRY_MAX_CELLS,
+ * so it always fits. The memfd is sparse: only the cells actually written
+ * take memory. */
#define TERMSRV_BACKLOG_MAGIC 0x544D4250u /* "TMBP" */
#define TERMSRV_BACKLOG_VERSION 1u
-#define TERMSRV_BACKLOG_PAGE_ENTRIES 256u
+#define TERMSRV_BACKLOG_ENTRIES_MIN 64u
+#define TERMSRV_BACKLOG_ENTRIES_MAX 4096u
#define TERMSRV_BACKLOG_PAGE_SEAL_CELLS (1u << 20)
#define TERMSRV_BACKLOG_ENTRY_MAX_CELLS ((1u << 22) - 1) /* Termsave.w */
-#define TERMSRV_BACKLOG_HEADER_SIZE 4096u
-#define TERMSRV_BACKLOG_PAGE_SIZE \
- (TERMSRV_BACKLOG_HEADER_SIZE + \
- ((size_t)TERMSRV_BACKLOG_PAGE_SEAL_CELLS + \
- TERMSRV_BACKLOG_ENTRY_MAX_CELLS) * sizeof(TermSrvCell))
typedef struct
{
@@ -117,12 +116,46 @@ typedef struct
uint32_t version;
uint32_t page_id;
_Atomic uint32_t num_entries; /* release-stored after the entry */
- uint32_t entry_off[TERMSRV_BACKLOG_PAGE_ENTRIES]; /* in cells */
- _Atomic uint32_t entry_w[TERMSRV_BACKLOG_PAGE_ENTRIES]; /* only grows */
+ uint32_t header_size; /* bytes, a multiple of the system page */
+ uint32_t entries_per_page;
+ /* then uint32_t entry_off[entries_per_page] (in cells), then
+ * _Atomic uint32_t entry_w[entries_per_page] (only grows) */
} TermSrvBacklogPage;
-_Static_assert(sizeof(TermSrvBacklogPage) <= TERMSRV_BACKLOG_HEADER_SIZE,
- "the backlog page header must fit its header area");
+static inline uint32_t *
+termsrv_backlog_entry_off(TermSrvBacklogPage *page)
+{
+ return (uint32_t *)(page + 1);
+}
+
+/* entries_per_page is the value the caller trusts, not the shared
+ * header's. */
+static inline _Atomic uint32_t *
+termsrv_backlog_entry_w(TermSrvBacklogPage *page, uint32_t entries_per_page)
+{
+ return (_Atomic uint32_t *)(termsrv_backlog_entry_off(page) +
+ entries_per_page);
+}
+
+static inline size_t
+termsrv_backlog_header_size(uint32_t entries_per_page, size_t sys_page_size)
+{
+ size_t need = sizeof(TermSrvBacklogPage) +
+ 2 * sizeof(uint32_t) * (size_t)entries_per_page;
+
+ if (sys_page_size == 0)
+ sys_page_size = 4096;
+
+ return (need + sys_page_size - 1) / sys_page_size * sys_page_size;
+}
+
+static inline size_t
+termsrv_backlog_page_size(uint32_t header_size)
+{
+ return (size_t)header_size +
+ ((size_t)TERMSRV_BACKLOG_PAGE_SEAL_CELLS +
+ TERMSRV_BACKLOG_ENTRY_MAX_CELLS) * sizeof(TermSrvCell);
+}
typedef enum
{
@@ -408,7 +441,7 @@ int termsrv_recv_attached(int fd, int *shm_fd_out,
* Append one page to buf (of bufsz bytes, *off already used, at least
* sizeof(TermSrvMsgBacklogMapReq) once the echoed range is written). widths
* is TermSrvBacklogPage.entry_w of the page. Returns 0 on success, -1 if it
- * does not fit or has more than TERMSRV_BACKLOG_PAGE_ENTRIES entries. */
+ * does not fit or has more than TERMSRV_BACKLOG_ENTRIES_MAX entries. */
static inline int
termsrv_backlog_map_page_append(void *buf, uint32_t bufsz, uint32_t *off,
uint32_t page_id, uint32_t num_entries,
@@ -419,7 +452,7 @@ termsrv_backlog_map_page_append(void *buf, uint32_t bufsz, uint32_t *off,
unsigned char *out = buf;
uint32_t i;
- if (num_entries > TERMSRV_BACKLOG_PAGE_ENTRIES ||
+ if (num_entries > TERMSRV_BACKLOG_ENTRIES_MAX ||
*off > bufsz || need > (uint64_t)(bufsz - *off))
return -1;
memcpy(out + *off, &page_id, sizeof(page_id));
@@ -451,7 +484,7 @@ termsrv_backlog_map_page_next(const void *payload, uint32_t size,
if (*off > size || size - *off < 2 * sizeof(uint32_t))
return -1;
memcpy(&count, in + *off + sizeof(uint32_t), sizeof(count));
- if (count > TERMSRV_BACKLOG_PAGE_ENTRIES ||
+ if (count > TERMSRV_BACKLOG_ENTRIES_MAX ||
(uint64_t)count * sizeof(uint32_t) >
(uint64_t)(size - *off - 2 * sizeof(uint32_t)))
return -1;
diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index befc4944..584d0086 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -170,13 +170,15 @@ _shadow_pty_new(int cols, int rows, Config *config)
/* One entry per entry of the daemon's backlog pages, so scrolling,
* selection, search and the miniview work exactly as on a locally
* spawned terminal. The daemon keeps up to a page more than its
- * scrollback before it evicts the oldest one. The ring keeps one slot
- * free, so it takes one more to hold a whole page when the scrollback
- * is 0. Without a config (unit tests building a bare shadow pty) it is
- * left at 0, to be sized once by the test. */
+ * scrollback before it evicts the oldest one, and the size of its pages
+ * is only known from the first one that arrives: the ring is sized for
+ * the largest page. The ring keeps one slot free, so it takes one more
+ * to hold a whole page when the scrollback is 0. Without a config
+ * (unit tests building a bare shadow pty) it is left at 0, to be sized
+ * once by the test. */
if (config)
termpty_backlog_size_set(ty, config->scrollback +
- TERMSRV_BACKLOG_PAGE_ENTRIES + 1);
+ TERMSRV_BACKLOG_ENTRIES_MAX + 1);
ty->backlog_external_only = 1;
return ty;
@@ -228,7 +230,20 @@ _page_entries_get(const TermTymuxPage *page, Eina_Bool *known)
return 0;
}
n = atomic_load_explicit(&page->mapped->num_entries, memory_order_acquire);
- return MIN(n, TERMSRV_BACKLOG_PAGE_ENTRIES);
+ return MIN(n, page->entries_per_page);
+}
+
+static const uint32_t *
+_page_entry_off_get(const TermTymuxPage *page)
+{
+ return (const uint32_t *)(page->mapped + 1);
+}
+
+static const _Atomic uint32_t *
+_page_entry_w_get(const TermTymuxPage *page)
+{
+ return (const _Atomic uint32_t *)(_page_entry_off_get(page) +
+ page->entries_per_page);
}
/* The cells and width of entry k of page. An entry that would not fit in
@@ -245,15 +260,21 @@ _page_entry_get(const TermTymuxPage *page, uint32_t k, uint32_t *w)
*w = page->widths[k];
return _blank_cells_get();
}
- off = mapped->entry_off[k];
- *w = atomic_load_explicit(&mapped->entry_w[k], memory_order_acquire);
+ if (k >= page->entries_per_page)
+ {
+ *w = 0;
+ return _blank_cells_get();
+ }
+ off = _page_entry_off_get(page)[k];
+ *w = atomic_load_explicit(&_page_entry_w_get(page)[k],
+ memory_order_acquire);
if ((*w > TERMSRV_BACKLOG_ENTRY_MAX_CELLS) ||
((size_t)off + *w > PAGE_CELLS_MAX))
{
off = 0;
*w = 0;
}
- return (Termcell *)((char *)mapped + TERMSRV_BACKLOG_HEADER_SIZE) + off;
+ return (Termcell *)((char *)mapped + page->header_size) + off;
}
static TermTymuxPage *
@@ -325,7 +346,7 @@ _page_forget_oldest(TermTymux *ts, Eina_Bool release)
ts->pushed_total -= page->pushed;
if (page->mapped)
{
- munmap(page->mapped, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap(page->mapped, termsrv_backlog_page_size(page->header_size));
if (release)
_page_id_send(ts, TSRV_MSG_BACKLOG_PAGE_RELEASE, page->id);
}
@@ -348,7 +369,7 @@ _page_release(TermTymux *ts, TermTymuxPage *page)
n = atomic_load_explicit(&mapped->num_entries, memory_order_acquire);
/* the entries already pushed keep their widths, whatever the header
* says now */
- n = MIN(MAX(n, page->pushed), TERMSRV_BACKLOG_PAGE_ENTRIES);
+ n = MIN(MAX(n, page->pushed), page->entries_per_page);
widths = malloc(MAX(n, 1) * sizeof(uint32_t));
if (!widths)
return;
@@ -358,7 +379,7 @@ _page_release(TermTymux *ts, TermTymuxPage *page)
page->num_widths = n;
page->mapped = NULL;
_page_entries_repoint(ts, page);
- munmap(mapped, TERMSRV_BACKLOG_PAGE_SIZE);
+ munmap(mapped, termsrv_backlog_page_size(page->header_size));
_page_id_send(ts, TSRV_MSG_BACKLOG_PAGE_RELEASE, page->id);
}
@@ -645,33 +666,54 @@ _pages_clear(TermTymux *ts)
ts->pages_cap = 0;
}
-/* Maps the memfd of page id, NULL if it is not a backlog page */
-static TermSrvBacklogPage *
-_page_map(uint32_t id, int fd)
+#define PAGE_HEADER_SIZE_MAX (1u << 20)
+#define PAGE_HEADER_ALIGN 4096u
+
+/* Whether hdr, read from the first bytes of a memfd of file_size bytes, is
+ * the header of backlog page id that the cells can be trusted to follow. */
+static Eina_Bool
+_page_header_valid(const TermSrvBacklogPage *hdr, uint32_t id,
+ uint64_t file_size)
{
- TermSrvBacklogPage *mapped;
+ if (hdr->magic != TERMSRV_BACKLOG_MAGIC ||
+ hdr->version != TERMSRV_BACKLOG_VERSION ||
+ hdr->page_id != id)
+ return EINA_FALSE;
+ if (hdr->entries_per_page < TERMSRV_BACKLOG_ENTRIES_MIN ||
+ hdr->entries_per_page > TERMSRV_BACKLOG_ENTRIES_MAX)
+ return EINA_FALSE;
+ if (hdr->header_size % PAGE_HEADER_ALIGN ||
+ hdr->header_size > PAGE_HEADER_SIZE_MAX ||
+ hdr->header_size < termsrv_backlog_header_size(hdr->entries_per_page,
+ PAGE_HEADER_ALIGN))
+ return EINA_FALSE;
+ return file_size == termsrv_backlog_page_size(hdr->header_size);
+}
+
+/* Maps the memfd of page id, NULL if it is not a backlog page. The
+ * geometry of the page is returned in *header_size and *entries_per_page. */
+static TermSrvBacklogPage *
+_page_map(uint32_t id, int fd, uint32_t *header_size,
+ uint32_t *entries_per_page)
+{
+ TermSrvBacklogPage *mapped, hdr;
struct stat st;
- if (fstat(fd, &st) < 0 || st.st_size < (off_t)TERMSRV_BACKLOG_PAGE_SIZE)
+ if (fstat(fd, &st) < 0 ||
+ pread(fd, &hdr, sizeof(hdr), 0) != (ssize_t)sizeof(hdr) ||
+ !_page_header_valid(&hdr, id, st.st_size))
{
- ERR("termtymux: page %u is too small", id);
+ ERR("termtymux: page %u is not a backlog page", id);
return NULL;
}
- mapped = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
- fd, 0);
+ mapped = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (mapped == MAP_FAILED)
{
ERR("termtymux: mmap page %u: %s", id, strerror(errno));
return NULL;
}
- if (mapped->magic != TERMSRV_BACKLOG_MAGIC ||
- mapped->version != TERMSRV_BACKLOG_VERSION ||
- mapped->page_id != id)
- {
- ERR("termtymux: page %u is not a backlog page", id);
- munmap(mapped, TERMSRV_BACKLOG_PAGE_SIZE);
- return NULL;
- }
+ *header_size = hdr.header_size;
+ *entries_per_page = hdr.entries_per_page;
return mapped;
}
@@ -680,7 +722,7 @@ _page_received(TermTymux *ts, const void *payload, uint32_t size, int fd)
{
TermSrvBacklogPage *mapped = NULL;
TermTymuxPage *page;
- uint32_t id;
+ uint32_t id, header_size = 0, entries_per_page = 0;
if (size != sizeof(TermSrvMsgBacklogPageId))
{
@@ -706,8 +748,17 @@ _page_received(TermTymux *ts, const void *payload, uint32_t size, int fd)
return;
}
if (page)
- mapped = _page_map(id, fd);
+ mapped = _page_map(id, fd, &header_size, &entries_per_page);
close(fd);
+ /* the widths the daemon sent before may be more than this page holds */
+ if (mapped && (page->pushed > entries_per_page ||
+ page->num_widths > entries_per_page))
+ {
+ ERR("termtymux: page %u holds %u entries, the map had more", id,
+ entries_per_page);
+ munmap(mapped, termsrv_backlog_page_size(header_size));
+ mapped = NULL;
+ }
if (!mapped)
{
/* not asked again before the next NOTIFY or viewport move */
@@ -718,6 +769,8 @@ _page_received(TermTymux *ts, const void *payload, uint32_t size, int fd)
return;
}
page->mapped = mapped;
+ page->header_size = header_size;
+ page->entries_per_page = entries_per_page;
free(page->widths);
page->widths = NULL;
page->num_widths = 0;
@@ -1703,36 +1756,58 @@ tytest_termtymux_attach_escape_parse(void)
return 0;
}
+#define _TT_ENTRIES_PER_PAGE 128u
+
+/* Gives page the mapping that _tt_page_new() made. */
+static void
+_tt_page_attach(TermTymuxPage *page, TermSrvBacklogPage *mapped)
+{
+ page->mapped = mapped;
+ page->header_size = mapped->header_size;
+ page->entries_per_page = mapped->entries_per_page;
+}
+
/* A page image as the daemon would publish it, n entries of widths[] cells,
* entry k starting with the codepoint first + k. Anonymous memory rather
* than heap, as the client unmaps a page it drops. */
static TermSrvBacklogPage *
-_tt_page_new(uint32_t id, uint32_t n, const uint32_t *widths,
- Eina_Unicode first)
+_tt_page_sized_new(uint32_t id, uint32_t n, const uint32_t *widths,
+ Eina_Unicode first, uint32_t entries_per_page)
{
TermSrvBacklogPage *page;
Termcell *cells;
uint32_t k, off = 0;
+ uint32_t header_size = termsrv_backlog_header_size(entries_per_page, 4096);
- page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ | PROT_WRITE,
- MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ page = mmap(NULL, termsrv_backlog_page_size(header_size),
+ PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(page != MAP_FAILED);
- cells = (Termcell *)((char *)page + TERMSRV_BACKLOG_HEADER_SIZE);
+ cells = (Termcell *)((char *)page + header_size);
page->magic = TERMSRV_BACKLOG_MAGIC;
page->version = TERMSRV_BACKLOG_VERSION;
page->page_id = id;
+ page->header_size = header_size;
+ page->entries_per_page = entries_per_page;
for (k = 0; k < n; k++)
{
- page->entry_off[k] = off;
+ termsrv_backlog_entry_off(page)[k] = off;
if (widths[k] > 0)
cells[off].codepoint = first + k;
- atomic_store(&page->entry_w[k], widths[k]);
+ atomic_store(&termsrv_backlog_entry_w(page, entries_per_page)[k],
+ widths[k]);
off += widths[k];
}
atomic_store(&page->num_entries, n);
return page;
}
+static TermSrvBacklogPage *
+_tt_page_new(uint32_t id, uint32_t n, const uint32_t *widths,
+ Eina_Unicode first)
+{
+ return _tt_page_sized_new(id, n, widths, first, _TT_ENTRIES_PER_PAGE);
+}
+
static void
_tt_tymux_init(TermTymux *ts, Termpty *ty, size_t backsize)
{
@@ -1808,7 +1883,7 @@ tytest_termtymux_backlog_sync(void)
_tt_tymux_init(&ts, ty, 32);
_pages_range_set(&ts, 0, 1);
- ts.pages[0].mapped = _tt_page_new(0, 3, first_w, 'a');
+ _tt_page_attach(&ts.pages[0], _tt_page_new(0, 3, first_w, 'a'));
assert(termtymux_backlog_sync(&ts));
assert(termpty_backlog_length(ty) == 3);
assert(BACKLOG_ROW_GET(ty, 1)->cells[0].codepoint == 'c');
@@ -1816,7 +1891,8 @@ tytest_termtymux_backlog_sync(void)
assert(!termtymux_backlog_sync(&ts));
/* the newest entry grows from 5 to 25 cells, 3 rows of 10 */
- atomic_store(&ts.pages[0].mapped->entry_w[2], 25);
+ atomic_store(&termsrv_backlog_entry_w(ts.pages[0].mapped,
+ _TT_ENTRIES_PER_PAGE)[2], 25);
assert(termtymux_backlog_sync(&ts));
assert(termpty_backlog_length(ty) == 5);
assert(BACKLOG_ROW_GET(ty, 1)->w == 25);
@@ -1824,7 +1900,7 @@ tytest_termtymux_backlog_sync(void)
/* page 1 sealed before it was mapped, page 2 is the new head: nothing
* of the head goes in before the widths of page 1 are known */
_pages_range_set(&ts, 0, 3);
- ts.pages[2].mapped = _tt_page_new(2, 1, head_w, 'z');
+ _tt_page_attach(&ts.pages[2], _tt_page_new(2, 1, head_w, 'z'));
assert(!termtymux_backlog_sync(&ts));
assert(termpty_backlog_length(ty) == 5);
@@ -1868,8 +1944,8 @@ tytest_termtymux_backlog_ring_full(void)
/* 5 entries fit */
_tt_tymux_init(&ts, ty, 6);
_pages_range_set(&ts, 7, 2);
- ts.pages[0].mapped = _tt_page_new(7, 3, w, 'a');
- ts.pages[1].mapped = _tt_page_new(8, 3, w, 'd');
+ _tt_page_attach(&ts.pages[0], _tt_page_new(7, 3, w, 'a'));
+ _tt_page_attach(&ts.pages[1], _tt_page_new(8, 3, w, 'd'));
assert(termtymux_backlog_sync(&ts));
assert(ts.pages_count == 1);
assert(ts.pages[0].id == 8);
@@ -1880,7 +1956,7 @@ tytest_termtymux_backlog_ring_full(void)
/* and the next page pushes page 8 out in turn */
_pages_range_set(&ts, 8, 2);
- ts.pages[1].mapped = _tt_page_new(9, 3, w, 'g');
+ _tt_page_attach(&ts.pages[1], _tt_page_new(9, 3, w, 'g'));
assert(termtymux_backlog_sync(&ts));
assert(ts.pages[0].id == 9);
assert(ts.pages[0].pushed == 3);
@@ -1954,7 +2030,7 @@ tytest_termtymux_backlog_map_again(void)
_pages_range_set(&ts, 0, 3);
assert(_tt_sent(peer, TSRV_MSG_BACKLOG_MAP_REQ, NULL, 0) == 0);
/* page 0 arrives before the MAP reply */
- ts.pages[0].mapped = _tt_page_new(0, 1, w, 'a');
+ _tt_page_attach(&ts.pages[0], _tt_page_new(0, 1, w, 'a'));
ts.page_pending = EINA_FALSE;
memcpy(map, &echo, sizeof(echo));
@@ -2034,6 +2110,160 @@ tytest_termtymux_backlog_pending_evicted(void)
return 0;
}
+/* A memfd holding a backlog page header with the given fields, of
+ * file_size bytes. */
+static int
+_tt_page_memfd_new(uint32_t id, uint32_t header_size, uint32_t entries,
+ size_t file_size)
+{
+ TermSrvBacklogPage hdr = { 0 };
+ int fd = memfd_create("tt-page", MFD_CLOEXEC);
+
+ assert(fd >= 0);
+ assert(ftruncate(fd, file_size) == 0);
+ hdr.magic = TERMSRV_BACKLOG_MAGIC;
+ hdr.version = TERMSRV_BACKLOG_VERSION;
+ hdr.page_id = id;
+ hdr.header_size = header_size;
+ hdr.entries_per_page = entries;
+ assert(pwrite(fd, &hdr, sizeof(hdr), 0) == (ssize_t)sizeof(hdr));
+ return fd;
+}
+
+/* Test: the geometry of a page comes from its header, which is refused
+ * unless it is consistent with itself and with the size of the memfd. */
+int
+tytest_termtymux_backlog_header_checks(void)
+{
+ const struct
+ {
+ uint32_t header_size, entries;
+ size_t file_size;
+ Eina_Bool valid;
+ } cases[] = {
+ { 4096, 128, termsrv_backlog_page_size(4096), EINA_TRUE },
+ { 4096, 64, termsrv_backlog_page_size(4096), EINA_TRUE },
+ { 36864, 4096, termsrv_backlog_page_size(36864), EINA_TRUE },
+ /* bigger than needed, still a multiple of the page */
+ { 8192, 128, termsrv_backlog_page_size(8192), EINA_TRUE },
+ { 4096, 63, termsrv_backlog_page_size(4096), EINA_FALSE },
+ { 4096, 0, termsrv_backlog_page_size(4096), EINA_FALSE },
+ { 36864, 4097, termsrv_backlog_page_size(36864), EINA_FALSE },
+ { 4100, 128, termsrv_backlog_page_size(4100), EINA_FALSE },
+ { 0, 128, termsrv_backlog_page_size(0), EINA_FALSE },
+ /* the entries do not fit in the header */
+ { 4096, 1024, termsrv_backlog_page_size(4096), EINA_FALSE },
+ { (1u << 20) + 4096, 128, termsrv_backlog_page_size((1u << 20) + 4096),
+ EINA_FALSE },
+ /* the memfd is not the size the header says */
+ { 4096, 128, termsrv_backlog_page_size(4096) - 1, EINA_FALSE },
+ { 4096, 128, termsrv_backlog_page_size(4096) + 4096, EINA_FALSE },
+ { 4096, 128, 4096, EINA_FALSE },
+ };
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ TermSrvMsgBacklogPageId reply = { 0 };
+ TermTymux ts;
+ unsigned int i;
+ int peer;
+
+ assert(ty);
+ _tt_tymux_init(&ts, ty, 32);
+ _tt_tymux_connect(&ts, &peer);
+ for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++)
+ {
+ uint32_t header_size = 0, entries = 0;
+ TermSrvBacklogPage *mapped;
+ int fd = _tt_page_memfd_new(0, cases[i].header_size,
+ cases[i].entries, cases[i].file_size);
+
+ mapped = _page_map(0, fd, &header_size, &entries);
+ assert((mapped != NULL) == cases[i].valid);
+ if (mapped)
+ {
+ assert(header_size == cases[i].header_size);
+ assert(entries == cases[i].entries);
+ munmap(mapped, cases[i].file_size);
+ }
+ close(fd);
+ }
+
+ /* a refused page is marked, and not asked for again */
+ _pages_range_set(&ts, 0, 1);
+ reply.page_id = 0;
+ _page_received(&ts, &reply, sizeof(reply),
+ _tt_page_memfd_new(0, 4096, 63,
+ termsrv_backlog_page_size(4096)));
+ assert(ts.pages[0].failed && !ts.pages[0].mapped);
+ _tt_sent(peer, TSRV_MSG_BACKLOG_PAGE_RELEASE, NULL, 0);
+
+ /* another page's id in the header */
+ ts.pages[0].failed = EINA_FALSE;
+ _page_received(&ts, &reply, sizeof(reply),
+ _tt_page_memfd_new(3, 4096, 128,
+ termsrv_backlog_page_size(4096)));
+ assert(ts.pages[0].failed && !ts.pages[0].mapped);
+
+ /* widths for more entries than the page the daemon then exports holds */
+ ts.pages[0].failed = EINA_FALSE;
+ ts.pages[0].widths = calloc(100, sizeof(uint32_t));
+ ts.pages[0].num_widths = 100;
+ _page_received(&ts, &reply, sizeof(reply),
+ _tt_page_memfd_new(0, 4096, 64,
+ termsrv_backlog_page_size(4096)));
+ assert(ts.pages[0].failed && !ts.pages[0].mapped);
+ assert(ts.pages[0].widths && ts.pages[0].num_widths == 100);
+
+ /* as many as it holds is fine */
+ ts.pages[0].failed = EINA_FALSE;
+ ts.pages[0].num_widths = 64;
+ _page_received(&ts, &reply, sizeof(reply),
+ _tt_page_memfd_new(0, 4096, 64,
+ termsrv_backlog_page_size(4096)));
+ assert(!ts.pages[0].failed && ts.pages[0].mapped);
+ assert(ts.pages[0].entries_per_page == 64);
+
+ _pages_clear(&ts);
+ _tt_tymux_disconnect(&ts, peer);
+ termpty_free(ty);
+ return 0;
+}
+
+/* Test: the ring of the shadow pty holds the scrollback and a page of the
+ * largest size the daemon can make, as the size of its pages is only known
+ * once one arrives. */
+int
+tytest_termtymux_backlog_ring_size(void)
+{
+ uint32_t widths[TERMSRV_BACKLOG_ENTRIES_MAX], k;
+ Config config = { 0 };
+ Termpty *ty;
+ TermTymux ts;
+
+ config.scrollback = 100;
+ ty = _shadow_pty_new(10, 2, &config);
+ assert(ty);
+ assert(ty->backsize == 100 + TERMSRV_BACKLOG_ENTRIES_MAX + 1);
+
+ /* a whole page of the largest size is held, with the scrollback to spare */
+ for (k = 0; k < TERMSRV_BACKLOG_ENTRIES_MAX; k++)
+ widths[k] = 1;
+ memset(&ts, 0, sizeof(ts));
+ ts.sock_fd = -1;
+ ts.shadow_pty = ty;
+ _pages_range_set(&ts, 0, 1);
+ _tt_page_attach(&ts.pages[0],
+ _tt_page_sized_new(0, TERMSRV_BACKLOG_ENTRIES_MAX, widths,
+ 'a', TERMSRV_BACKLOG_ENTRIES_MAX));
+ assert(termtymux_backlog_sync(&ts));
+ assert(ts.pages[0].pushed == TERMSRV_BACKLOG_ENTRIES_MAX);
+ assert(ts.pushed_total == TERMSRV_BACKLOG_ENTRIES_MAX);
+
+ _pages_clear(&ts);
+ ty->config = NULL;
+ termpty_free(ty);
+ return 0;
+}
+
/* Test: a PAGE reply without a memfd, for a page the daemon could not
* export, ends the wait for it and marks the page: the next page is asked
* for, and the failed ones only again once the range of pages changes, not
@@ -2129,8 +2359,8 @@ tytest_termtymux_backlog_release(void)
_pages_range_set(&ts, 0, 2);
ts.page_pending = EINA_FALSE;
ts.map_pending = EINA_FALSE;
- ts.pages[0].mapped = _tt_page_new(0, 2, sealed_w, 'a');
- ts.pages[1].mapped = _tt_page_new(1, 1, head_w, 'z');
+ _tt_page_attach(&ts.pages[0], _tt_page_new(0, 2, sealed_w, 'a'));
+ _tt_page_attach(&ts.pages[1], _tt_page_new(1, 1, head_w, 'z'));
assert(termtymux_backlog_sync(&ts));
assert(BACKLOG_ROW_GET(ty, 3)->cells[0].codepoint == 'a');
(void)_tt_sent(peer, TSRV_MSG_BACKLOG_PAGE_RELEASE, NULL, 0);
@@ -2170,7 +2400,7 @@ tytest_termtymux_backlog_anchor(void)
_tt_tymux_init(&ts, ty, 32);
_pages_range_set(&ts, 0, 6);
for (i = 0; i < 6; i++)
- ts.pages[i].mapped = _tt_page_new(i, 2, w, 'a' + 2 * i);
+ _tt_page_attach(&ts.pages[i], _tt_page_new(i, 2, w, 'a' + 2 * i));
assert(termtymux_backlog_sync(&ts));
assert(termpty_backlog_length(ty) == 12);
diff --git a/src/bin/termtymux.h b/src/bin/termtymux.h
index e25b1913..af7bedaf 100644
--- a/src/bin/termtymux.h
+++ b/src/bin/termtymux.h
@@ -23,6 +23,8 @@ typedef struct
{
uint32_t id;
TermSrvBacklogPage *mapped; /* read-only, or NULL */
+ uint32_t header_size; /* of the mapping, checked when */
+ uint32_t entries_per_page; /* it was made: not the header's */
uint32_t *widths; /* final widths, or NULL if unknown */
uint32_t num_widths;
uint32_t pushed; /* entries the shadow pty holds */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index bc8fb60c..0b1d2bbc 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -165,6 +165,7 @@ static struct {
{ "backlog_store_pages", tytest_backlog_store_pages},
{ "backlog_store_evict", tytest_backlog_store_evict},
{ "backlog_store_map", tytest_backlog_store_map},
+ { "backlog_store_geometry", tytest_backlog_store_geometry},
{ "backlog_store_compress", tytest_backlog_store_compress},
{ "backlog_store_compress_skip", tytest_backlog_store_compress_skip},
{ "backlog_store_compress_widths", tytest_backlog_store_compress_widths},
@@ -203,6 +204,9 @@ static struct {
{ "termtymux_backlog_map_empty", tytest_termtymux_backlog_map_empty},
{ "termtymux_backlog_pending_evicted", tytest_termtymux_backlog_pending_evicted},
{ "termtymux_backlog_page_refused", tytest_termtymux_backlog_page_refused},
+ { "termtymux_backlog_header_checks",
+ tytest_termtymux_backlog_header_checks},
+ { "termtymux_backlog_ring_size", tytest_termtymux_backlog_ring_size},
{ "termtymux_backlog_map_head", tytest_termtymux_backlog_map_head},
{ "termtymux_backlog_release", tytest_termtymux_backlog_release},
{ "termtymux_backlog_anchor", tytest_termtymux_backlog_anchor},
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index 93d377c4..50593ab4 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -652,8 +652,11 @@ _pages_check_oldest(int fd, TermSrvReader *reader, uint32_t first,
uint32_t size, i;
int type, page_fd = -1;
long expected = -1;
+ const uint32_t *off;
+ const _Atomic uint32_t *w;
Eina_Bool ok = EINA_TRUE;
- size_t map_size = TERMSRV_BACKLOG_PAGE_SIZE;
+ size_t map_size;
+ struct stat st;
if (termsrv_msg_send(fd, TSRV_MSG_BACKLOG_PAGE_REQ, &req, sizeof(req)) < 0)
return EINA_FALSE;
@@ -674,18 +677,26 @@ _pages_check_oldest(int fd, TermSrvReader *reader, uint32_t first,
reply = payload;
ok = reply->page_id == first;
free(payload);
+ if (fstat(page_fd, &st) < 0)
+ {
+ close(page_fd);
+ return EINA_FALSE;
+ }
+ map_size = st.st_size;
page = mmap(NULL, map_size, PROT_READ, MAP_SHARED, page_fd, 0);
close(page_fd);
if (page == MAP_FAILED)
return EINA_FALSE;
cells = (const TermSrvCell *)((const unsigned char *)page +
- TERMSRV_BACKLOG_HEADER_SIZE);
+ page->header_size);
ok = ok && page->magic == TERMSRV_BACKLOG_MAGIC &&
page->page_id == first && page->num_entries == num_entries;
+ off = termsrv_backlog_entry_off((TermSrvBacklogPage *)page);
+ w = termsrv_backlog_entry_w((TermSrvBacklogPage *)page,
+ page->entries_per_page);
for (i = 0; ok && i < num_entries; i++)
{
- long n = _pages_entry_number(cells + page->entry_off[i],
- page->entry_w[i]);
+ long n = _pages_entry_number(cells + off[i], w[i]);
if (expected < 0)
expected = (n >= 0) ? n + 1 : -1;
@@ -807,6 +818,7 @@ _live_pages_busy_body(void)
void *payload;
uint32_t size;
int fd, shm_fd, type, page_fd = -1;
+ struct stat st;
uint8_t mode = TSRV_ATTACH_READONLY;
size_t after_page = 0;
@@ -856,12 +868,14 @@ _live_pages_busy_body(void)
}
assert(after_page > 4096);
assert(page_fd >= 0);
- page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
- page_fd, 0);
+ assert(fstat(page_fd, &st) == 0);
+ page = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, page_fd, 0);
assert(page != MAP_FAILED);
assert(page->magic == TERMSRV_BACKLOG_MAGIC);
assert(page->page_id == attached.first_page_id);
- munmap((void *)page, TERMSRV_BACKLOG_PAGE_SIZE);
+ assert(page->header_size % 4096 == 0);
+ assert((size_t)st.st_size == termsrv_backlog_page_size(page->header_size));
+ munmap((void *)page, st.st_size);
close(page_fd);
assert(!reader.eof);
@@ -995,6 +1009,7 @@ _daemon_oldest_number(uint32_t *first_page_id)
int fd, shm_fd, type, page_fd = -1;
uint8_t mode = TSRV_ATTACH_READONLY;
long value;
+ struct stat st;
fd = termsrv_connect(SESSION);
assert(fd >= 0);
@@ -1017,15 +1032,19 @@ _daemon_oldest_number(uint32_t *first_page_id)
}
while (type > 0 && type != TSRV_MSG_BACKLOG_PAGE);
assert(type == TSRV_MSG_BACKLOG_PAGE && page_fd >= 0);
- page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
- page_fd, 0);
+ 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);
value = _pages_entry_number((const TermSrvCell *)
((const unsigned char *)page +
- TERMSRV_BACKLOG_HEADER_SIZE) +
- page->entry_off[0], page->entry_w[0]);
- munmap((void *)page, TERMSRV_BACKLOG_PAGE_SIZE);
+ page->header_size) +
+ termsrv_backlog_entry_off
+ ((TermSrvBacklogPage *)page)[0],
+ termsrv_backlog_entry_w
+ ((TermSrvBacklogPage *)page,
+ page->entries_per_page)[0]);
+ munmap((void *)page, st.st_size);
termsrv_reader_clear(&reader);
close(fd);
return value;
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index ed5a80a7..e6da08cd 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -120,6 +120,7 @@ int tytest_fuzz_resize_max(void);
int tytest_backlog_store_pages(void);
int tytest_backlog_store_evict(void);
int tytest_backlog_store_map(void);
+int tytest_backlog_store_geometry(void);
int tytest_backlog_store_compress(void);
int tytest_backlog_store_compress_skip(void);
int tytest_backlog_store_compress_widths(void);
@@ -158,6 +159,8 @@ int tytest_termtymux_backlog_map_again(void);
int tytest_termtymux_backlog_map_empty(void);
int tytest_termtymux_backlog_pending_evicted(void);
int tytest_termtymux_backlog_page_refused(void);
+int tytest_termtymux_backlog_header_checks(void);
+int tytest_termtymux_backlog_ring_size(void);
int tytest_termtymux_backlog_map_head(void);
int tytest_termtymux_backlog_release(void);
int tytest_termtymux_backlog_anchor(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.