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 b694fb94fb6541f32c56b1feff666fa8cbf08961
Author: Cedric BAIL <[email protected]>
AuthorDate: Thu Nov 19 19:57:18 2026 -0600
backlog_store: let the newest entry be taken back
Resizing reflows the newest backlog line: termpty pulls it back onto
the screen and wraps it again. The store therefore has to give its last
entry back, which replaces the rule that a head page only grows. The
head's num_entries and last entry_w may now go down, and an emptied
head is dropped so that the page before is written to again.
The entries taken back may be stored again, at the same offsets and
with other widths, before a client looks: the count alone cannot tell.
Each page header gets a generation that every entry taken back bumps,
and a client points its entries at the cells again when it changes.
A dropped head is kept aside, mapping and holds included, and comes
back as the next head under the same id. tymuxd counts holds per client
by page id, so a release that arrives late must reach the page that was
held, not a new page with the same id. Page ids and widths are kept in
the store, as the header of a memfd can be written by any client.
A page still being compressed is taken back from its job without
running the main loop. ecore_thread_cancel() cannot be used for that:
it leaves a queued job in the queue, and the worker then skips it
without a trace, so whoever waits on it waits forever.
tybenchd -i 5 1000000, median of 5 iterations, three runs each:
before 0.343 0.341 0.344 s, after 0.341 0.343 0.343 s.
---
src/bin/backlog.c | 19 ++
src/bin/backlog.h | 4 +
src/bin/backlog_store.c | 652 +++++++++++++++++++++++++++++++++++++++++++++---
src/bin/backlog_store.h | 15 +-
src/bin/termsrv.h | 11 +-
src/bin/termtymux.c | 254 ++++++++++++++++++-
src/bin/termtymux.h | 2 +
src/bin/tytest.c | 3 +
src/bin/unit_tests.h | 2 +
9 files changed, 917 insertions(+), 45 deletions(-)
diff --git a/src/bin/backlog.c b/src/bin/backlog.c
index 303de71f..484ab032 100644
--- a/src/bin/backlog.c
+++ b/src/bin/backlog.c
@@ -328,3 +328,22 @@ termpty_backlog_external_drop_oldest(Termpty *ty, size_t count)
}
}
}
+
+size_t
+termpty_backlog_external_drop_newest(Termpty *ty, size_t count)
+{
+ size_t dropped;
+
+ for (dropped = 0; dropped < count && ty->backsize; dropped++)
+ {
+ Termsave *ts = BACKLOG_ROW_GET(ty, 1);
+
+ if (!ts->cells)
+ break;
+ termpty_save_free(ty, ts);
+ ty->backpos = (ty->backpos + ty->backsize - 1) % ty->backsize;
+ }
+ ty->backlog_beacon.screen_y = 0;
+ ty->backlog_beacon.backlog_y = 0;
+ return dropped;
+}
diff --git a/src/bin/backlog.h b/src/bin/backlog.h
index 7dcfa1bd..58336b4a 100644
--- a/src/bin/backlog.h
+++ b/src/bin/backlog.h
@@ -40,6 +40,10 @@ termpty_backlog_external_set(Termpty *ty, int backlog_y,
Termcell *cells, ssize_t w);
void
termpty_backlog_external_drop_oldest(Termpty *ty, size_t count);
+/* Returns how many entries were dropped, fewer than count if the backlog
+ * holds fewer. */
+size_t
+termpty_backlog_external_drop_newest(Termpty *ty, size_t count);
#define BACKLOG_ROW_GET(Ty, Y) \
(&(Ty)->back[((Ty)->backsize - 1 + (Ty)->backpos - (Y)) % (Ty)->backsize])
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index b26798e7..5713d41a 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -53,10 +53,11 @@ typedef enum
typedef struct
{
TermSrvBacklogPage *header;
- /* What the daemon reads back about the entries is kept here: the memfd
- * is writable by the clients, the header is only ever stored to. */
+ /* What the store reads back about the page is kept here: the header is
+ * only ever stored to, as clients can write to a memfd. */
+ uint32_t id;
uint32_t entries;
- uint32_t last_w;
+ uint32_t *widths; /* entries_per_page of them */
int fd;
uint32_t refs;
BacklogStorePageState state;
@@ -73,6 +74,13 @@ typedef struct
int export_fd;
} BacklogStorePage;
+typedef enum
+{
+ JOB_QUEUED,
+ JOB_RUNNING,
+ JOB_ABANDONED /* taken back before it ran: the worker does nothing */
+} BacklogStoreJobClaim;
+
typedef struct
{
BacklogStore *bs; /* NULL once the store is gone */
@@ -85,6 +93,10 @@ typedef struct
size_t orphan_size;
size_t cells_size;
Eina_Binbuf *blob;
+ /* whether the worker or _job_abandon() got the job first */
+ _Atomic int claim;
+ /* posted by a JOB_RUNNING worker once it no longer reads the cells */
+ Eina_Semaphore done;
} BacklogStoreJob;
struct tag_BacklogStore
@@ -101,6 +113,9 @@ struct tag_BacklogStore
Eina_List *jobs;
/* evicted pages still held, each a BacklogStorePage of its own */
Eina_List *detached;
+ /* heads that backlog_store_pop() dropped, after the count pages in
+ * pages, the next head first: they come back as heads with their id */
+ uint32_t parked;
};
/* Compression jobs not ended yet, of every store */
@@ -158,6 +173,17 @@ _page_find(BacklogStore *bs, uint32_t page_id)
return &bs->pages[idx];
}
+/* Also finds a parked page, which only a hold may still refer to. */
+static BacklogStorePage *
+_page_find_held(BacklogStore *bs, uint32_t page_id)
+{
+ uint32_t idx = page_id - bs->first_id;
+
+ if (idx >= bs->count + bs->parked)
+ return NULL;
+ return &bs->pages[idx];
+}
+
static void
_page_export_drop(BacklogStorePage *page)
{
@@ -174,6 +200,7 @@ _page_export_drop(BacklogStorePage *page)
static void
_page_close(BacklogStorePage *page)
{
+ free(page->widths);
if (page->state == PAGE_COMPRESSED)
{
free(page->header);
@@ -286,11 +313,10 @@ _page_compressed_set(BacklogStorePage *page, Eina_Binbuf *blob)
return EINA_TRUE;
}
-static TermSrvBacklogPage *
-_page_add(BacklogStore *bs)
+static BacklogStorePage *
+_page_new(BacklogStore *bs, uint32_t page_id)
{
BacklogStorePage *page;
- uint32_t page_id = bs->first_id + bs->count;
if (bs->count == bs->alloc)
{
@@ -309,23 +335,51 @@ _page_add(BacklogStore *bs)
page = &bs->pages[bs->count];
memset(page, 0, sizeof(*page));
page->export_fd = -1;
+ page->widths = calloc(bs->geo.entries_per_page, sizeof(uint32_t));
+ if (!page->widths)
+ return NULL;
page->header = _page_map(bs, "ty-backlog", page_id,
bs->geo.page_size, &page->fd);
if (!page->header)
- return NULL;
+ {
+ free(page->widths);
+ return NULL;
+ }
+ 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;
+ page->header->header_size = bs->geo.header_size;
+ page->header->entries_per_page = bs->geo.entries_per_page;
+ return page;
+}
+
+/* A new head, or the parked one, which is still empty. */
+static TermSrvBacklogPage *
+_page_add(BacklogStore *bs)
+{
+ BacklogStorePage *page;
+ uint32_t page_id = bs->first_id + bs->count;
+
+ if (bs->parked)
+ {
+ bs->parked--;
+ page = &bs->pages[bs->count];
+ }
+ else
+ {
+ page = _page_new(bs, page_id);
+ if (!page)
+ return NULL;
+ }
if (bs->count > 0)
{
bs->pages[bs->count - 1].idle_since = ecore_time_get();
bs->pages[bs->count - 1].cells_size =
bs->head_cells * sizeof(TermSrvCell);
}
- 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;
@@ -370,7 +424,7 @@ _evict(BacklogStore *bs)
return;
bs->count--;
memmove(&bs->pages[0], &bs->pages[1],
- bs->count * sizeof(bs->pages[0]));
+ (bs->count + bs->parked) * sizeof(bs->pages[0]));
bs->first_id++;
bs->total_entries -= oldest;
}
@@ -427,7 +481,7 @@ backlog_store_free(BacklogStore *bs)
job->orphan_size = page->map_size;
page->header = NULL;
}
- for (i = 0; i < bs->count; i++)
+ for (i = 0; i < bs->count + bs->parked; i++)
_page_close(&bs->pages[i]);
EINA_LIST_FREE(bs->detached, detached)
{
@@ -462,7 +516,7 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
if (extends && n > 0)
{
- cur_w = head_page->last_w;
+ cur_w = head_page->widths[n - 1];
add = MIN((size_t)w, TERMSRV_BACKLOG_ENTRY_MAX_CELLS - cur_w);
if (add == 0)
return EINA_TRUE;
@@ -470,7 +524,7 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
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;
+ head_page->widths[n - 1] = cur_w + add;
bs->head_cells += add;
return EINA_TRUE;
}
@@ -493,7 +547,7 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
memory_order_release);
atomic_store_explicit(&head->num_entries, n + 1, memory_order_release);
head_page->entries = n + 1;
- head_page->last_w = add;
+ head_page->widths[n] = add;
bs->head_cells += add;
bs->total_entries++;
_evict(bs);
@@ -514,24 +568,25 @@ backlog_store_count(const BacklogStore *bs)
return bs->count;
}
-/* Rebuild a COMPRESSED page into export_map, in the layout it had. It
- * expands on the calling thread: up to TERMSRV_BACKLOG_PAGE_SEAL_CELLS
- * cells, which takes some milliseconds. */
+/* 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. */
static Eina_Bool
_page_export(const BacklogStore *bs, BacklogStorePage *page)
{
- size_t size = page->header_size + page->cells_size;
+ size_t size = page->map_size;
unsigned char *map;
int fd;
- map = _page_map(bs, "ty-export", page->header->page_id, size, &fd);
+ map = _page_map(bs, "ty-export", page->id, size, &fd);
if (!map)
return EINA_FALSE;
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,
+ ERR("cannot rebuild page %u of '%s'", page->id,
bs->name);
munmap(map, size);
if (fd >= 0)
@@ -593,7 +648,7 @@ backlog_store_hold(BacklogStore *bs, uint32_t page_id)
BacklogStorePage *page;
EINA_SAFETY_ON_NULL_RETURN(bs);
- page = _page_find(bs, page_id);
+ page = _page_find_held(bs, page_id);
if (page)
page->refs++;
}
@@ -605,7 +660,7 @@ backlog_store_release(BacklogStore *bs, uint32_t page_id)
Eina_List *l;
EINA_SAFETY_ON_NULL_RETURN(bs);
- page = _page_find(bs, page_id);
+ page = _page_find_held(bs, page_id);
if (page && page->refs > 0)
{
page->refs--;
@@ -616,7 +671,7 @@ backlog_store_release(BacklogStore *bs, uint32_t page_id)
return;
EINA_LIST_FOREACH(bs->detached, l, page)
{
- if (page->header->page_id != page_id)
+ if (page->id != page_id)
continue;
if (--page->refs == 0)
{
@@ -632,14 +687,19 @@ static void
_job_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
{
BacklogStoreJob *job = data;
+ int queued = JOB_QUEUED;
+ if (!atomic_compare_exchange_strong(&job->claim, &queued, JOB_RUNNING))
+ return;
job->blob = backlog_compress(job->cells, job->cells_size);
+ eina_semaphore_release(&job->done, 1);
}
static void
_job_free(BacklogStoreJob *job)
{
_jobs_alive--;
+ eina_semaphore_free(&job->done);
if (job->orphan_map)
munmap(job->orphan_map, job->orphan_size);
if (job->blob)
@@ -720,14 +780,16 @@ _job_start(BacklogStore *bs, BacklogStorePage *page)
return;
}
job = calloc(1, sizeof(*job));
- if (!job)
+ if (!job || !eina_semaphore_new(&job->done, 0))
{
+ free(job);
page->attempts++;
return;
}
job->bs = bs;
+ atomic_init(&job->claim, JOB_QUEUED);
job->cells = _page_cells(page->header, page->header_size);
- job->page_id = page->header->page_id;
+ job->page_id = page->id;
job->cells_size = page->cells_size;
page->state = PAGE_COMPRESSING;
bs->jobs = eina_list_append(bs->jobs, job);
@@ -735,6 +797,98 @@ _job_start(BacklogStore *bs, BacklogStorePage *page)
ecore_thread_run(_job_worker, _job_end, _job_cancel, job);
}
+/* Take a COMPRESSING page back from its job, RAW. If the worker already
+ * runs, this waits for it to stop reading the cells, without running the
+ * main loop: its callbacks could move the pages. The job is then left to
+ * end alone, as if its store were gone. Not ecore_thread_cancel(): it
+ * leaves a queued job in the queue, flagged, and the worker then skips it
+ * without ever posting. */
+static Eina_Bool
+_job_abandon(BacklogStore *bs, BacklogStorePage *page)
+{
+ BacklogStoreJob *job;
+ Eina_List *l;
+ int queued = JOB_QUEUED;
+
+ EINA_LIST_FOREACH(bs->jobs, l, job)
+ {
+ if (job->page_id == page->id)
+ break;
+ }
+ if (!job)
+ {
+ ERR("no compression job for page %u", page->id);
+ return EINA_FALSE;
+ }
+ if (!atomic_compare_exchange_strong(&job->claim, &queued, JOB_ABANDONED) &&
+ !eina_semaphore_lock(&job->done))
+ {
+ ERR("cannot wait for the compression of page %u", page->id);
+ return EINA_FALSE;
+ }
+ job->bs = NULL;
+ bs->jobs = eina_list_remove_list(bs->jobs, l);
+ page->state = PAGE_RAW;
+ return EINA_TRUE;
+}
+
+/* Make a sealed page writable again, to be the head. A COMPRESSED page
+ * takes its rebuilt page as its own, which those it was handed to keep
+ * reading. */
+static Eina_Bool
+_page_reopen(BacklogStore *bs, BacklogStorePage *page)
+{
+ if (page->state == PAGE_COMPRESSING && !_job_abandon(bs, page))
+ return EINA_FALSE;
+ if (page->state == PAGE_COMPRESSED)
+ {
+ if (!page->export_map && !_page_export(bs, page))
+ return EINA_FALSE;
+ free(page->header);
+ eina_binbuf_free(page->blob);
+ page->blob = NULL;
+ page->header = page->export_map;
+ page->fd = page->export_fd;
+ page->export_map = NULL;
+ page->export_fd = -1;
+ page->state = PAGE_RAW;
+ }
+ page->attempts = 0;
+ return EINA_TRUE;
+}
+
+Eina_Bool
+backlog_store_pop(BacklogStore *bs)
+{
+ BacklogStorePage *head;
+ uint32_t n;
+
+ EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
+ if (!bs->count)
+ return EINA_FALSE;
+ head = &bs->pages[bs->count - 1];
+ while (head->entries == 0)
+ {
+ if (bs->count == 1 || !_page_reopen(bs, head - 1))
+ return EINA_FALSE;
+ bs->count--;
+ bs->parked++;
+ head--;
+ bs->head_cells = head->cells_size / sizeof(TermSrvCell);
+ }
+ n = head->entries - 1;
+ atomic_store_explicit(&head->header->num_entries, n, memory_order_release);
+ atomic_store_explicit(&_page_entry_w(bs, head->header)[n], 0,
+ memory_order_relaxed);
+ atomic_fetch_add_explicit(&head->header->generation, 1,
+ memory_order_release);
+ bs->head_cells -= head->widths[n];
+ head->widths[n] = 0;
+ head->entries = n;
+ bs->total_entries--;
+ return EINA_TRUE;
+}
+
void
backlog_store_maintain(BacklogStore *bs, double now)
{
@@ -855,6 +1009,8 @@ tytest_backlog_store_geometry(void)
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));
+ assert(geo.header_size >= sizeof(TermSrvBacklogPage) +
+ 2 * sizeof(uint32_t) * geo.entries_per_page);
}
return 0;
}
@@ -1757,4 +1913,444 @@ tytest_backlog_store_held_evict(void)
_test_backings_run(_test_held_evict);
return 0;
}
+
+/* The widths a client wrote into the memfd of the head do not steer pop,
+ * nor do the page ids it wrote into a page being compressed, or into a held
+ * page once evicted. */
+static void
+_test_pop_untrusted(void)
+{
+ BacklogStore *bs = backlog_store_new("test", BACKLOG_STORE_MEMFD,
+ _TEST_SCROLLBACK);
+ TermSrvBacklogPage *rw;
+
+ assert(bs);
+ _test_pages_fill(bs, 0, 3);
+ rw = mmap(NULL, bs->geo.page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ backlog_store_page_fd(bs, 0), 0);
+ assert(rw != MAP_FAILED);
+ atomic_store(&rw->num_entries, 77);
+ atomic_store(&_test_w(rw)[1], 0xffffffff);
+ atomic_store(&_test_w(rw)[2], 1);
+ assert(backlog_store_pop(bs));
+ assert(atomic_load(&rw->num_entries) == 2);
+ assert(backlog_store_pop(bs));
+ assert(atomic_load(&rw->num_entries) == 1);
+ _test_pages_fill(bs, 1, 1);
+ assert(atomic_load(&rw->num_entries) == 2);
+ assert(_test_off(rw)[1] == TEST_ROW_W);
+ assert(atomic_load(&_test_w(rw)[1]) == TEST_ROW_W);
+ munmap(rw, bs->geo.page_size);
+ backlog_store_free(bs);
+
+ bs = _test_store_new(BACKLOG_STORE_MEMFD, 300);
+ assert(bs);
+ _test_pages_fill(bs, 0, 2 * _TEST_ENTRIES + 1);
+ backlog_store_hold(bs, 0);
+ rw = mmap(NULL, bs->geo.page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
+ backlog_store_page_fd(bs, 1), 0);
+ assert(rw != MAP_FAILED);
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ assert(_page_find(bs, 1)->state == PAGE_COMPRESSING);
+ rw->page_id = 999;
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ _test_jobs_wait(bs);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ assert(atomic_load(&rw->num_entries) == _TEST_ENTRIES - 1);
+ _test_pages_fill(bs, 2 * _TEST_ENTRIES - 1, 1);
+ assert(atomic_load(&rw->num_entries) == _TEST_ENTRIES);
+ assert(_test_off(rw)[_TEST_ENTRIES - 1] ==
+ (_TEST_ENTRIES - 1) * TEST_ROW_W);
+ munmap(rw, bs->geo.page_size);
+
+ _test_pages_fill(bs, 2 * _TEST_ENTRIES, 300);
+ assert(backlog_store_first_id(bs) > 0);
+ assert(eina_list_count(bs->detached) == 1);
+ ((BacklogStorePage *)eina_list_data_get(bs->detached))->header->page_id =
+ 7;
+ backlog_store_release(bs, 0);
+ assert(!bs->detached);
+ backlog_store_free(bs);
+}
+
+/* Test: backlog_store_pop() takes back the newest entry of the head, and
+ * the next append starts where it started or extends the entry before. */
+static void
+_test_pop_head(BacklogStoreBacking backing)
+{
+ const TermSrvBacklogPage *page;
+ BacklogStore *bs;
+ Termcell row[TEST_ROW_W];
+ uint32_t off;
+
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ assert(!backlog_store_pop(bs));
+ _test_pages_fill(bs, 0, 1);
+ assert(backlog_store_pop(bs));
+ assert(!backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 1);
+ backlog_store_free(bs);
+
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ _test_pages_fill(bs, 0, 3);
+ page = _test_page_map(bs, 0);
+ off = _test_off(page)[2];
+ assert(atomic_load(&page->generation) == 0);
+ assert(backlog_store_pop(bs));
+ assert(atomic_load(&page->generation) == 1);
+ assert(atomic_load(&page->num_entries) == 2);
+ assert(atomic_load(&_test_w(page)[2]) == 0);
+ _test_pages_fill(bs, 5, 1);
+ assert(atomic_load(&page->num_entries) == 3);
+ assert(_test_off(page)[2] == off);
+ assert(atomic_load(&_test_w(page)[2]) == TEST_ROW_W);
+ assert(_test_page_cells(page)[off].codepoint == 'a' + 5);
+
+ assert(backlog_store_pop(bs));
+ _test_row_fill(row, 5, 'A');
+ assert(backlog_store_append(bs, row, 5, EINA_TRUE));
+ assert(atomic_load(&page->num_entries) == 2);
+ assert(atomic_load(&_test_w(page)[1]) == TEST_ROW_W + 5);
+ assert(_test_page_cells(page)[_test_off(page)[1] + TEST_ROW_W]
+ .codepoint == 'A');
+ _test_page_unmap(bs, page);
+ backlog_store_free(bs);
+
+ if (backing == BACKLOG_STORE_MEMFD)
+ _test_pop_untrusted();
+}
+
+/* Test: an empty head is dropped and the previous page, RAW or COMPRESSED,
+ * is the head again, with its cells as they were. The dropped head, held
+ * or not, comes back as the next head with its id and memory, and is
+ * hidden from the store meanwhile: a late release reaches it, and eviction
+ * and free deal with it. */
+static void
+_test_pop_reopen(BacklogStoreBacking backing)
+{
+ const TermSrvBacklogPage *rebuilt, *page;
+ static uint32_t buf[TERMSRV_MAX_MSG_PAYLOAD / sizeof(uint32_t)];
+ TermSrvBacklogPage *parked;
+ BacklogStore *bs;
+ uint32_t i;
+ TermSrvMsgBacklogMapReq *echo = (TermSrvMsgBacklogMapReq *)buf;
+ int parked_fd, rebuilt_fd;
+
+ /* a RAW previous page, and a held head dropped then released late */
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ _test_pages_fill(bs, 0, _TEST_ENTRIES + 1);
+ assert(backlog_store_count(bs) == 2);
+ backlog_store_hold(bs, 1);
+ parked = _page_find(bs, 1)->header;
+ parked_fd = backlog_store_page_fd(bs, 1);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 2);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 1);
+ assert(backlog_store_first_id(bs) == 0);
+ assert(!backlog_store_page_exists(bs, 1));
+ assert(!backlog_store_page_get(bs, 1));
+ assert(backlog_store_page_fd(bs, 1) < 0);
+ assert(backlog_store_map_write(bs, 0, 10, buf, sizeof(buf)) >
+ sizeof(*echo));
+ assert(echo->first_page_id == 0 && echo->count == 1);
+ backlog_store_maintain(bs, ecore_time_get() + 600.0);
+ assert(eina_list_count(bs->jobs) == 0);
+ page = _test_page_map(bs, 0);
+ assert(atomic_load(&page->num_entries) == _TEST_ENTRIES - 1);
+ _test_page_unmap(bs, page);
+ _test_page_check(bs, 0);
+ backlog_store_release(bs, 1);
+ assert(bs->pages[1].refs == 0);
+ _test_pages_fill(bs, _TEST_ENTRIES - 1, 1);
+ assert(backlog_store_count(bs) == 1);
+ _test_page_check(bs, 0);
+ _test_pages_fill(bs, _TEST_ENTRIES, 1);
+ assert(backlog_store_count(bs) == 2);
+ assert(_page_find(bs, 1)->header == parked);
+ assert(backlog_store_page_fd(bs, 1) == parked_fd);
+ assert(_page_find(bs, 1)->refs == 0);
+ _test_page_check(bs, 1);
+
+ /* held across the drop and back: the release ends that hold */
+ backlog_store_hold(bs, 1);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ _test_pages_fill(bs, _TEST_ENTRIES - 1, 2);
+ assert(_page_find(bs, 1)->refs == 1);
+ backlog_store_release(bs, 1);
+ assert(_page_find(bs, 1)->refs == 0);
+ /* freed with a parked page */
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ backlog_store_free(bs);
+
+ /* COMPRESSED previous pages, rebuilt already or not */
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ _test_pages_fill(bs, 0, 2 * _TEST_ENTRIES + 1);
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ _test_jobs_wait(bs);
+ assert(_page_find(bs, 0)->state == PAGE_COMPRESSED);
+ assert(_page_find(bs, 1)->state == PAGE_COMPRESSED);
+ rebuilt = backlog_store_page_get(bs, 1);
+ assert(rebuilt);
+ rebuilt_fd = backlog_store_page_fd(bs, 1);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 2);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ assert(!_page_find(bs, 1)->blob);
+ assert(!_page_find(bs, 1)->export_map);
+ assert(_page_find(bs, 1)->header == rebuilt);
+ assert(backlog_store_page_get(bs, 1) == rebuilt);
+ assert(backlog_store_page_fd(bs, 1) == rebuilt_fd);
+ _test_page_check(bs, 1);
+ _test_pages_fill(bs, 2 * _TEST_ENTRIES - 1, 1);
+ _test_page_check(bs, 1);
+ for (i = 0; i < _TEST_ENTRIES + 1; i++)
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 1);
+ assert(_page_find(bs, 0)->state == PAGE_RAW);
+ _test_page_check(bs, 0);
+ _test_pages_fill(bs, _TEST_ENTRIES - 1, _TEST_ENTRIES + 2);
+ assert(backlog_store_count(bs) == 3);
+ for (i = 0; i < 3; i++)
+ _test_page_check(bs, i);
+ backlog_store_free(bs);
+
+ /* eviction, at the end of a job, moves the parked page along */
+ bs = _test_store_new(backing, 200);
+ assert(bs);
+ _test_pages_fill(bs, 0, _TEST_ENTRIES + 1);
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ assert(_page_find(bs, 0)->state == PAGE_COMPRESSING);
+ _test_pages_fill(bs, _TEST_ENTRIES + 1, _TEST_ENTRIES);
+ assert(backlog_store_count(bs) == 3);
+ parked = _page_find(bs, 2)->header;
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_count(bs) == 2);
+ _test_jobs_wait(bs);
+ assert(backlog_store_first_id(bs) == 1);
+ assert(backlog_store_count(bs) == 1);
+ _test_pages_fill(bs, 2 * _TEST_ENTRIES - 1, 2);
+ assert(backlog_store_count(bs) == 2);
+ assert(_page_find(bs, 2)->header == parked);
+ _test_page_check(bs, 1);
+ _test_page_check(bs, 2);
+ backlog_store_free(bs);
+}
+
+typedef struct
+{
+ Eina_Semaphore release;
+ _Atomic int started;
+ int alive;
+} Test_Blockers;
+
+static void
+_test_blocker_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
+{
+ Test_Blockers *blockers = data;
+
+ atomic_fetch_add(&blockers->started, 1);
+ eina_semaphore_lock(&blockers->release);
+}
+
+static void
+_test_blocker_end(void *data, Ecore_Thread *thread EINA_UNUSED)
+{
+ Test_Blockers *blockers = data;
+
+ blockers->alive--;
+}
+
+static Eina_Bool
+_test_blockers_poll_cb(void *data)
+{
+ Test_Blockers *blockers = data;
+
+ if (blockers->alive == 0)
+ ecore_main_loop_quit();
+ return ECORE_CALLBACK_RENEW;
+}
+
+/* Occupy every thread the pool may run, so that the jobs started next stay
+ * queued. */
+static void
+_test_blockers_start(Test_Blockers *blockers)
+{
+ int i, max = ecore_thread_max_get();
+ double until = ecore_time_get() + 10.0;
+
+ assert(max > 0);
+ memset(blockers, 0, sizeof(*blockers));
+ assert(eina_semaphore_new(&blockers->release, 0));
+ for (i = 0; i < max; i++)
+ {
+ assert(ecore_thread_run(_test_blocker_worker, _test_blocker_end,
+ _test_blocker_end, blockers));
+ blockers->alive++;
+ }
+ while (atomic_load(&blockers->started) < max)
+ {
+ assert(ecore_time_get() < until);
+ usleep(1000);
+ }
+ assert(ecore_thread_available_get() == 0);
+}
+
+static void
+_test_blockers_end(Test_Blockers *blockers)
+{
+ Ecore_Timer *poll;
+ int i;
+
+ /* the count of eina_semaphore_release() is ignored with POSIX */
+ for (i = 0; i < blockers->alive; i++)
+ eina_semaphore_release(&blockers->release, 1);
+ poll = ecore_timer_add(0.005, _test_blockers_poll_cb, blockers);
+ ecore_main_loop_begin();
+ ecore_timer_del(poll);
+ eina_semaphore_free(&blockers->release);
+}
+
+static BacklogStoreJob *
+_test_job_find(BacklogStore *bs, uint32_t page_id)
+{
+ BacklogStoreJob *job;
+ Eina_List *l;
+
+ EINA_LIST_FOREACH(bs->jobs, l, job)
+ {
+ if (job->page_id == page_id)
+ return job;
+ }
+ return NULL;
+}
+
+/* Test: popping into a page being compressed takes the page back from its
+ * job, whether the job has not started, without waiting for it, has ended
+ * on its thread, or is running, and what is left of the job later leaves
+ * the page alone. */
+static void
+_test_pop_compressing(BacklogStoreBacking backing)
+{
+ Test_Blockers blockers;
+ BacklogStoreJob *job;
+ BacklogStore *bs;
+ const TermSrvBacklogPage *raw;
+ Termcell *big;
+ unsigned int jobs_alive;
+ uint32_t i;
+
+ /* not started: the worker will skip it */
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ _test_pages_fill(bs, 0, 2 * _TEST_ENTRIES + 1);
+ raw = _page_find(bs, 1)->header;
+ _test_blockers_start(&blockers);
+ jobs_alive = _jobs_alive;
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ assert(eina_list_count(bs->jobs) == 2);
+ assert(_page_find(bs, 1)->state == PAGE_COMPRESSING);
+ job = _test_job_find(bs, 1);
+ assert(job);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ assert(job->claim == JOB_ABANDONED && !job->bs);
+ assert(_jobs_alive == jobs_alive + 2);
+ assert(eina_list_count(bs->jobs) == 1);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ assert(_page_find(bs, 1)->header == raw);
+ _test_blockers_end(&blockers);
+ _test_jobs_wait(NULL);
+ assert(_page_find(bs, 0)->state == PAGE_COMPRESSED);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ _test_page_check(bs, 1);
+ backlog_store_free(bs);
+
+ /* ended on its thread, its end not run yet: detached */
+ bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+ assert(bs);
+ _test_pages_fill(bs, 0, 2 * _TEST_ENTRIES + 1);
+ raw = _page_find(bs, 1)->header;
+ jobs_alive = _jobs_alive;
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ job = _test_job_find(bs, 1);
+ assert(job);
+ eina_semaphore_lock(&job->done);
+ eina_semaphore_release(&job->done, 1);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ assert(job->claim == JOB_RUNNING && !job->bs);
+ assert(_jobs_alive == jobs_alive + 2);
+ assert(eina_list_count(bs->jobs) == 1);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ _test_jobs_wait(NULL);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ assert(_page_find(bs, 1)->header == raw);
+ assert(_page_find(bs, 1)->attempts == 0);
+ assert(!_page_find(bs, 1)->blob);
+ _test_page_check(bs, 1);
+ _test_pages_fill(bs, 2 * _TEST_ENTRIES - 1, 2);
+ _test_page_check(bs, 1);
+ _test_page_check(bs, 2);
+ backlog_store_free(bs);
+
+ /* running, on pages that take a while to compress: pop waits for it */
+ big = calloc(TERMSRV_BACKLOG_PAGE_SEAL_CELLS, sizeof(Termcell));
+ assert(big);
+ for (i = 0; i < TERMSRV_BACKLOG_PAGE_SEAL_CELLS; i++)
+ big[i].codepoint = 'a' + i % 26;
+ 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,
+ EINA_FALSE));
+ backlog_store_maintain(bs, ecore_time_get() + 6.0);
+ assert(eina_list_count(bs->jobs) == 2);
+ job = _test_job_find(bs, 1);
+ assert(job);
+ while (atomic_load(&job->claim) == JOB_QUEUED)
+ usleep(100);
+ assert(backlog_store_pop(bs));
+ assert(backlog_store_pop(bs));
+ assert(job->claim == JOB_RUNNING && !job->bs);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ assert(backlog_store_append(bs, big, 10, EINA_FALSE));
+ _test_jobs_wait(NULL);
+ assert(_page_find(bs, 1)->state == PAGE_RAW);
+ raw = _test_page_map(bs, 1);
+ assert(atomic_load(&raw->num_entries) == 1);
+ assert(_test_off(raw)[0] == 0);
+ assert(atomic_load(&_test_w(raw)[0]) == 10);
+ assert(_test_page_cells(raw)[9].codepoint == 'j');
+ _test_page_unmap(bs, raw);
+ backlog_store_free(bs);
+ free(big);
+}
+
+static void
+_test_pop(BacklogStoreBacking backing)
+{
+ ecore_init();
+ _test_pop_head(backing);
+ _test_pop_reopen(backing);
+ _test_pop_compressing(backing);
+ ecore_shutdown();
+}
+
+int
+tytest_backlog_store_pop(void)
+{
+ _test_backings_run(_test_pop);
+ return 0;
+}
#endif
diff --git a/src/bin/backlog_store.h b/src/bin/backlog_store.h
index 571f06ab..dd363f6a 100644
--- a/src/bin/backlog_store.h
+++ b/src/bin/backlog_store.h
@@ -2,9 +2,9 @@
#define TERMINOLOGY_BACKLOG_STORE_H_ 1
/* A scrollback kept in pages of logical lines (see TermSrvBacklogPage in
- * termsrv.h), whose idle pages are compressed. Pages are identified by an
- * id that only grows; the last page is the head, the only one still
- * written to. Include termpty.h and termsrv.h first. */
+ * termsrv.h), whose idle pages are compressed. Pages are identified by
+ * consecutive ids; the last page is the head, the only one still written
+ * to. Include termpty.h and termsrv.h first. */
typedef struct tag_BacklogStore BacklogStore;
typedef enum
@@ -49,6 +49,15 @@ void backlog_store_free(BacklogStore *bs);
Eina_Bool backlog_store_append(BacklogStore *bs, const Termcell *cells,
ssize_t w, Eina_Bool extends);
+/* Take the newest entry back, to wrap it again. Its cells stay mapped:
+ * a reader still pointing at them reads stale cells. If the head is empty,
+ * it is dropped and the page before becomes the head again, rebuilt if it
+ * was compressed, and the entry comes from that page. A dropped head keeps
+ * its holds and comes back, with the same id and memory, as the next head.
+ * Returns EINA_FALSE if there is no entry, or if the previous page could not
+ * be rebuilt. */
+Eina_Bool backlog_store_pop(BacklogStore *bs);
+
/* The id of the oldest page, and how many pages there are up to the head. */
uint32_t backlog_store_first_id(const BacklogStore *bs);
uint32_t backlog_store_count(const BacklogStore *bs);
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index dad81d20..2ebf0687 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -101,8 +101,10 @@ typedef struct
* 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. */
+ * so it always fits. The head may also give its newest entries back, when
+ * the screen is resized: num_entries and the last entry_w then go down,
+ * and the page before may become the head again. The cells stay mapped.
+ * 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_ENTRIES_MIN 64u
@@ -118,8 +120,11 @@ typedef struct
_Atomic uint32_t num_entries; /* release-stored after the entry */
uint32_t header_size; /* bytes, a multiple of the system page */
uint32_t entries_per_page;
+ /* bumped, release-stored, each time an entry is taken back: the entries
+ * after it may then reuse its offset and get new widths */
+ _Atomic uint32_t generation;
/* then uint32_t entry_off[entries_per_page] (in cells), then
- * _Atomic uint32_t entry_w[entries_per_page] (only grows) */
+ * _Atomic uint32_t entry_w[entries_per_page] */
} TermSrvBacklogPage;
static inline uint32_t *
diff --git a/src/bin/termtymux.c b/src/bin/termtymux.c
index 584d0086..8d733468 100644
--- a/src/bin/termtymux.c
+++ b/src/bin/termtymux.c
@@ -334,6 +334,19 @@ _page_id_send(TermTymux *ts, TermSrvMsgType type, uint32_t id)
return EINA_TRUE;
}
+static void
+_page_unmap(TermTymux *ts, TermTymuxPage *page, Eina_Bool release)
+{
+ ts->pushed_total -= page->pushed;
+ if (page->mapped)
+ {
+ munmap(page->mapped, termsrv_backlog_page_size(page->header_size));
+ if (release)
+ _page_id_send(ts, TSRV_MSG_BACKLOG_PAGE_RELEASE, page->id);
+ }
+ free(page->widths);
+}
+
/* Drops the oldest page and its entries. The daemon is told when it still
* holds the page for us, not when it evicted it. */
static void
@@ -343,18 +356,92 @@ _page_forget_oldest(TermTymux *ts, Eina_Bool release)
if (page->pushed)
termpty_backlog_external_drop_oldest(ts->shadow_pty, page->pushed);
- ts->pushed_total -= page->pushed;
- if (page->mapped)
- {
- munmap(page->mapped, termsrv_backlog_page_size(page->header_size));
- if (release)
- _page_id_send(ts, TSRV_MSG_BACKLOG_PAGE_RELEASE, page->id);
- }
- free(page->widths);
+ _page_unmap(ts, page, release);
ts->pages_count--;
memmove(ts->pages, ts->pages + 1, ts->pages_count * sizeof(*ts->pages));
}
+/* Drops the head, that the daemon took back, and its entries. The daemon
+ * still counts our hold on it. */
+static void
+_page_forget_newest(TermTymux *ts)
+{
+ TermTymuxPage *page = &ts->pages[ts->pages_count - 1];
+
+ if (page->pushed)
+ termpty_backlog_external_drop_newest(ts->shadow_pty, page->pushed);
+ _page_unmap(ts, page, EINA_TRUE);
+ ts->pages_count--;
+}
+
+/* The head gives back its newest entries when the daemon's screen is
+ * resized. Drops what the shadow pty holds past the entries a page has
+ * now, along with the entries of the pages after it, to be pushed again in
+ * order. Returns whether entries were dropped. */
+static Eina_Bool
+_pages_retract(TermTymux *ts)
+{
+ unsigned int i, j;
+
+ for (i = 0; i < ts->pages_count; i++)
+ {
+ TermTymuxPage *page = &ts->pages[i];
+ Eina_Bool known;
+ uint32_t entries = _page_entries_get(page, &known), w;
+ size_t drop;
+ Termcell *cells;
+
+ if (!known || entries >= page->pushed)
+ continue;
+ drop = page->pushed - entries + _pages_pushed_after(ts, page);
+ ts->pushed_total -=
+ termpty_backlog_external_drop_newest(ts->shadow_pty, drop);
+ page->pushed = entries;
+ for (j = i + 1; j < ts->pages_count; j++)
+ ts->pages[j].pushed = 0;
+ /* it may have grown again since it was pushed */
+ if (entries > 0)
+ {
+ cells = _page_entry_get(page, entries - 1, &w);
+ if (cells)
+ {
+ termpty_backlog_external_set(ts->shadow_pty, 1, cells, w);
+ page->last_w = w;
+ }
+ }
+ return EINA_TRUE;
+ }
+ return EINA_FALSE;
+}
+
+/* A page that gave entries back may have taken others since, at the same
+ * offsets or with other widths, however many entries it now has: the
+ * entries pushed of the mapped pages whose generation changed are pointed
+ * at their cells again. Returns whether any was. */
+static Eina_Bool
+_pages_generation_check(TermTymux *ts)
+{
+ Eina_Bool changed = EINA_FALSE;
+ unsigned int i;
+
+ for (i = 0; i < ts->pages_count; i++)
+ {
+ TermTymuxPage *page = &ts->pages[i];
+ uint32_t generation;
+
+ if (!page->mapped)
+ continue;
+ generation = atomic_load_explicit(&page->mapped->generation,
+ memory_order_acquire);
+ if (generation == page->generation)
+ continue;
+ page->generation = generation;
+ _page_entries_repoint(ts, page);
+ changed = EINA_TRUE;
+ }
+ return changed;
+}
+
/* Unmaps a sealed page, keeping its widths: its entries read as blank
* until it is mapped again. */
static void
@@ -389,10 +476,12 @@ termtymux_backlog_sync(TermTymux *ts)
Termpty *ty;
uint32_t newer;
unsigned int i;
- Eina_Bool changed = EINA_FALSE;
+ Eina_Bool changed;
EINA_SAFETY_ON_NULL_RETURN_VAL(ts, EINA_FALSE);
ty = ts->shadow_pty;
+ changed = _pages_generation_check(ts);
+ changed |= _pages_retract(ts);
newer = ts->pushed_total;
for (i = 0; i < ts->pages_count; i++)
{
@@ -597,6 +686,36 @@ _pages_map_request(TermTymux *ts)
ts->pages[i].map_asked = EINA_TRUE;
}
+/* Drops the heads the daemon took back, the pages from id end on. The page
+ * before is the head again: it is followed through its mapping, as its
+ * entries may go down. Returns whether entries left the shadow pty's
+ * backlog. */
+static Eina_Bool
+_pages_newest_drop(TermTymux *ts, uint64_t end)
+{
+ TermTymuxPage *head;
+ Eina_Bool changed = EINA_FALSE;
+
+ if (!ts->pages_count || ts->pages[ts->pages_count - 1].id < end)
+ return EINA_FALSE;
+ while (ts->pages_count && ts->pages[ts->pages_count - 1].id >= end)
+ {
+ changed |= ts->pages[ts->pages_count - 1].pushed > 0;
+ _page_forget_newest(ts);
+ }
+ ts->pages_next_id = end;
+ if (!ts->pages_count)
+ return changed;
+ head = &ts->pages[ts->pages_count - 1];
+ if (!head->mapped)
+ {
+ free(head->widths);
+ head->widths = NULL;
+ head->num_widths = 0;
+ }
+ return changed;
+}
+
/* The daemon holds the pages [first_id, first_id + count), the last one
* being the head. Returns whether entries left the shadow pty's backlog. */
static Eina_Bool
@@ -620,8 +739,10 @@ _pages_range_set(TermTymux *ts, uint32_t first_id, uint32_t count)
changed |= ts->pages[0].pushed > 0;
_page_forget_oldest(ts, ts->pages[0].id >= evicted);
}
- /* an evicted page gets no reply */
- if (ts->page_pending && ts->page_pending_id < first_id)
+ changed |= _pages_newest_drop(ts, (uint64_t)first_id + count);
+ /* an evicted or dropped page gets no reply */
+ if (ts->page_pending && (ts->page_pending_id < first_id ||
+ ts->page_pending_id - first_id >= count))
ts->page_pending = EINA_FALSE;
if (ts->pages_next_id < first_id)
ts->pages_next_id = first_id;
@@ -771,6 +892,8 @@ _page_received(TermTymux *ts, const void *payload, uint32_t size, int fd)
page->mapped = mapped;
page->header_size = header_size;
page->entries_per_page = entries_per_page;
+ page->generation = atomic_load_explicit(&mapped->generation,
+ memory_order_acquire);
free(page->widths);
page->widths = NULL;
page->num_widths = 0;
@@ -2429,6 +2552,115 @@ tytest_termtymux_backlog_anchor(void)
return 0;
}
+/* Test: a head that gives entries back, or narrows its newest one, is
+ * followed by dropping or trimming the newest entries of the shadow pty,
+ * and entries stored again in their place are read anew.
+ * A head that goes away takes its entries along and is released, and the
+ * page before, the head again, is followed through its mapping rather than
+ * the widths known for it. */
+int
+tytest_termtymux_backlog_head_shrink(void)
+{
+ const uint32_t sealed_w[] = { 4, 4, 4 }, head_w[] = { 1, 2, 25, 4, 5 };
+ TermSrvMsgBacklogPageId release;
+ TermSrvBacklogPage *head;
+ _Atomic uint32_t *head_ew;
+ Termcell *head_cells;
+ Termpty *ty = _tt_test_pty_new(10, 2);
+ TermTymux ts;
+ int peer;
+
+ assert(ty);
+ _tt_tymux_init(&ts, ty, 32);
+ _tt_tymux_connect(&ts, &peer);
+ _pages_range_set(&ts, 0, 2);
+ ts.page_pending = EINA_FALSE;
+ ts.map_pending = EINA_FALSE;
+ ts.pages[0].widths = malloc(sizeof(sealed_w));
+ assert(ts.pages[0].widths);
+ memcpy(ts.pages[0].widths, sealed_w, sizeof(sealed_w));
+ ts.pages[0].num_widths = 3;
+ head = _tt_page_new(1, 5, head_w, 'k');
+ head_ew = termsrv_backlog_entry_w(head, _TT_ENTRIES_PER_PAGE);
+ _tt_page_attach(&ts.pages[1], head);
+ assert(termtymux_backlog_sync(&ts));
+ assert(ts.pushed_total == 8);
+ assert(termpty_backlog_length(ty) == 3 + 1 + 1 + 3 + 1 + 1);
+
+ /* the daemon takes 2 entries back */
+ atomic_store(&head->num_entries, 3);
+ atomic_store(&head_ew[3], 0);
+ atomic_store(&head_ew[4], 0);
+ assert(termtymux_backlog_sync(&ts));
+ assert(ts.pages[1].pushed == 3);
+ assert(ts.pushed_total == 6);
+ assert(termpty_backlog_length(ty) == 3 + 1 + 1 + 3);
+ assert(BACKLOG_ROW_GET(ty, 1)->cells[0].codepoint == 'k' + 2);
+ assert(BACKLOG_ROW_GET(ty, 1)->w == 25);
+ assert(BACKLOG_ROW_GET(ty, 3)->cells[0].codepoint == 'k');
+ assert(BACKLOG_ROW_GET(ty, 4)->w == 4);
+ assert(!termtymux_backlog_sync(&ts));
+
+ /* then its newest entry narrows */
+ atomic_store(&head_ew[2], 7);
+ assert(termtymux_backlog_sync(&ts));
+ assert(BACKLOG_ROW_GET(ty, 1)->w == 7);
+ assert(termpty_backlog_length(ty) == 3 + 1 + 1 + 1);
+
+ /* 2 entries taken back and 2 others stored between two syncs: as many
+ * entries, the newest as wide, but other offsets and widths */
+ head_cells = (Termcell *)((char *)head + head->header_size);
+ termsrv_backlog_entry_off(head)[1] = 1;
+ atomic_store(&head_ew[1], 3);
+ head_cells[1].codepoint = 'X';
+ termsrv_backlog_entry_off(head)[2] = 4;
+ atomic_store(&head_ew[2], 7);
+ head_cells[4].codepoint = 'Y';
+ atomic_fetch_add(&head->generation, 2);
+ assert(termtymux_backlog_sync(&ts));
+ assert(ts.pages[1].pushed == 3);
+ assert(BACKLOG_ROW_GET(ty, 1)->cells[0].codepoint == 'Y');
+ assert(BACKLOG_ROW_GET(ty, 1)->w == 7);
+ assert(BACKLOG_ROW_GET(ty, 2)->cells[0].codepoint == 'X');
+ assert(BACKLOG_ROW_GET(ty, 2)->w == 3);
+ assert(BACKLOG_ROW_GET(ty, 3)->cells[0].codepoint == 'k');
+ assert(!termtymux_backlog_sync(&ts));
+
+ /* the head goes, page 0 is the head again */
+ (void)_tt_sent(peer, TSRV_MSG_BACKLOG_PAGE_RELEASE, NULL, 0);
+ ts.page_pending = EINA_TRUE;
+ ts.page_pending_id = 1;
+ assert(_pages_range_set(&ts, 0, 1));
+ assert(ts.pages_count == 1);
+ assert(ts.pages_next_id == 1);
+ assert(ts.pushed_total == 3);
+ assert(termpty_backlog_length(ty) == 3);
+ assert(BACKLOG_ROW_GET(ty, 1)->w == 4);
+ assert(BACKLOG_ROW_GET(ty, 4)->cells == NULL);
+ assert(_tt_sent(peer, TSRV_MSG_BACKLOG_PAGE_RELEASE, &release,
+ sizeof(release)) == 1);
+ assert(release.page_id == 1);
+ assert(!ts.pages[0].widths);
+ assert(ts.page_pending && ts.page_pending_id == 0);
+ assert(!termtymux_backlog_sync(&ts));
+
+ /* its mapping comes, with an entry taken back meanwhile */
+ _tt_page_attach(&ts.pages[0], _tt_page_new(0, 2, sealed_w, 'a'));
+ _page_entries_repoint(&ts, &ts.pages[0]);
+ assert(termtymux_backlog_sync(&ts));
+ assert(ts.pages[0].pushed == 2);
+ assert(ts.pushed_total == 2);
+ assert(termpty_backlog_length(ty) == 2);
+ assert(BACKLOG_ROW_GET(ty, 1)->cells[0].codepoint == 'b');
+ assert(BACKLOG_ROW_GET(ty, 2)->cells[0].codepoint == 'a');
+ assert(BACKLOG_ROW_GET(ty, 3)->cells == NULL);
+
+ _pages_clear(&ts);
+ _tt_tymux_disconnect(&ts, peer);
+ termpty_free(ty);
+ return 0;
+}
+
/* Test: termtymux_title_compose() prefixes the session name in brackets,
* drops the title when it is empty, and returns NULL (no session to show)
* when tymux_name is NULL or empty, whatever title is. */
diff --git a/src/bin/termtymux.h b/src/bin/termtymux.h
index af7bedaf..b2ad3fb7 100644
--- a/src/bin/termtymux.h
+++ b/src/bin/termtymux.h
@@ -29,6 +29,8 @@ typedef struct
uint32_t num_widths;
uint32_t pushed; /* entries the shadow pty holds */
uint32_t last_w; /* width the last of them was given */
+ uint32_t generation; /* of the mapping, when they were
+ * pointed at its cells */
Eina_Bool failed; /* could not be mapped, not asked again
* before the next NOTIFY or a move of
* the viewport */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 0b1d2bbc..90419ad4 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -171,6 +171,7 @@ static struct {
{ "backlog_store_compress_widths", tytest_backlog_store_compress_widths},
{ "backlog_store_anon", tytest_backlog_store_anon},
{ "backlog_store_held_evict", tytest_backlog_store_held_evict},
+ { "backlog_store_pop", tytest_backlog_store_pop},
{ "backlog_compress", tytest_backlog_compress},
#if defined(HAVE_TYMUX)
{ "termsrv_name", tytest_termsrv_name},
@@ -210,6 +211,8 @@ static struct {
{ "termtymux_backlog_map_head", tytest_termtymux_backlog_map_head},
{ "termtymux_backlog_release", tytest_termtymux_backlog_release},
{ "termtymux_backlog_anchor", tytest_termtymux_backlog_anchor},
+ { "termtymux_backlog_head_shrink",
+ tytest_termtymux_backlog_head_shrink},
{ "termtymux_input_action_get", tytest_termtymux_input_action_get},
{ "termtymux_input_filter_long", tytest_termtymux_input_filter_long},
{ "termtymux_title_compose", tytest_termtymux_title_compose},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index e6da08cd..86cd0e49 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -126,6 +126,7 @@ int tytest_backlog_store_compress_skip(void);
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_compress(void);
#if defined(HAVE_TYMUX)
int tytest_termsrv_name(void);
@@ -164,6 +165,7 @@ 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);
+int tytest_termtymux_backlog_head_shrink(void);
int tytest_termtymux_input_action_get(void);
int tytest_termtymux_input_filter_long(void);
int tytest_termtymux_title_compose(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.