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 2dc9793c6a9ddee76a5f7c2b6f409e04bbd48dd5
Author: Cedric BAIL <[email protected]>
AuthorDate: Sat Nov 21 00:13:28 2026 -0600

    backlog_store: compress a quiet head nobody holds
    
    With sealed pages compressed, the raw head of each terminal is now
    the bulk of its scrollback memory, up to 6.6 MB for a full page. A
    head that no client holds and that got no output for
    BACKLOG_QUIET_SECONDS is now compressed like a sealed page.
    
    An append only flags the store; maintain turns the flag into the time
    it saw it. The loop clock is an Eo call, which cost 5 to 9% of pty
    intake when read for every row, and quiet only needs to be known to
    within a maintain period.
    
    Its newest entry is left out of the blob and kept aside, so that an
    append can tell whether it wraps, and a reflow can read it, without
    expanding the page. For now, anything that needs a compressed head
    restores it on the main loop, as a page reopened by pop is; views
    always hold their head, so nothing changes at runtime yet.
    
    backlog_store_pop's reopen test kept a head unheld through a
    maintain 600 seconds ahead to check that it was not compressed; it
    now stays within the quiet time, which keeps that check meaningful.
---
 src/bin/backlog_store.c | 563 ++++++++++++++++++++++++++++++++++++++++++++++--
 src/bin/backlog_store.h |  23 +-
 src/bin/tytest.c        |   9 +
 src/bin/unit_tests.h    |   5 +
 4 files changed, 577 insertions(+), 23 deletions(-)

diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index b387a23e..d214142d 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -105,6 +105,9 @@ typedef struct
    TermSrvBacklogPage   *export_map;
    size_t                export_size;
    int                   export_fd;
+   /* A head being compressed, or compressed, keeps its newest entry here:
+    * the blob covers the cells before it. NULL otherwise. */
+   TermSrvCell          *tail;
 } BacklogStorePage;
 
 typedef enum
@@ -179,6 +182,10 @@ struct tag_BacklogStore
    BacklogStoreGeometry geo;
    uint32_t            head_cells;
    Eina_Bool           last_append_failed; /* the row after it starts an entry */
+   /* Appends only set appended: the clock is an Eo call, too dear per
+    * row. maintain turns it into last_append, in its own clock. */
+   Eina_Bool           appended;
+   double              last_append;
    uint64_t            total_entries;
    Eina_List          *jobs;
    Eina_List          *rebuilds;
@@ -286,6 +293,37 @@ _page_find_held(BacklogStore *bs, uint32_t page_id)
    return &bs->pages[idx];
 }
 
+static Eina_Bool
+_page_is_head(const BacklogStore *bs, const BacklogStorePage *page)
+{
+   return bs->count && (page == &bs->pages[bs->count - 1]);
+}
+
+static size_t
+_page_tail_size(const BacklogStorePage *page)
+{
+   if (!page->tail)
+     return 0;
+   return page->widths[page->entries - 1] * sizeof(TermSrvCell);
+}
+
+static void
+_page_tail_drop(BacklogStorePage *page)
+{
+   free(page->tail);
+   page->tail = NULL;
+}
+
+/* Whether the head may be compressed as a sealed page is: maintain saw no
+ * append to it for BACKLOG_QUIET_SECONDS, and it holds more than its
+ * newest entry, which stays raw. */
+static Eina_Bool
+_head_quiet(const BacklogStore *bs, const BacklogStorePage *head, double now)
+{
+   return (head->entries > 1) && !bs->appended &&
+      (now - bs->last_append >= BACKLOG_QUIET_SECONDS);
+}
+
 /* The fds of pages are all memfds that _page_memfd_map() opened. */
 static void
 _page_fd_close(int *fd)
@@ -344,6 +382,7 @@ _page_close(BacklogStore *bs, BacklogStorePage *page)
    free(page->widths);
    free(page->links.refs);
    free(page->blocks.refs);
+   free(page->tail);
    if (page->state == PAGE_COMPRESSED)
      {
         _page_rebuild_hand_over(bs, page);
@@ -844,9 +883,13 @@ _page_add(BacklogStore *bs)
      }
    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);
+        BacklogStorePage *sealed = &bs->pages[bs->count - 1];
+
+        sealed->idle_since = ecore_time_get();
+        sealed->cells_size = bs->head_cells * sizeof(TermSrvCell);
+        /* what the head was found to be says nothing of the whole page */
+        sealed->compress_poor = EINA_FALSE;
+        sealed->attempts = 0;
      }
    bs->count++;
    bs->head_cells = 0;
@@ -998,6 +1041,22 @@ backlog_store_free(BacklogStore *bs)
    free(bs);
 }
 
+static Eina_Bool _page_reopen(BacklogStore *bs, BacklogStorePage *page);
+
+/* Make the head RAW again before it is written to or read: this waits for
+ * a running compression job, or expands a compressed head on the main
+ * loop. */
+static Eina_Bool
+_head_restore(BacklogStore *bs, BacklogStorePage *head)
+{
+   if (EINA_LIKELY(head->state == PAGE_RAW))
+     return EINA_TRUE;
+   if (_page_reopen(bs, head))
+     return EINA_TRUE;
+   ERR("cannot restore the head %u of '%s'", head->id, bs->name);
+   return EINA_FALSE;
+}
+
 /* Clients follow the head through its atomics without a lock: the cells
  * are written before the width that covers them is published, and a new
  * entry's offset and width before num_entries counts it. A row that fails
@@ -1016,12 +1075,15 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
    EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
    EINA_SAFETY_ON_TRUE_RETURN_VAL(w < 0 || (w > 0 && !cells), EINA_FALSE);
    first_id = bs->first_id;
+   bs->appended = EINA_TRUE;
    if (bs->last_append_failed)
      join = BACKLOG_STORE_NEW;
 
    if (bs->count)
      {
         head_page = &bs->pages[bs->count - 1];
+        if (!_head_restore(bs, head_page))
+          goto failed;
         head = head_page->header;
         n = head_page->entries;
      }
@@ -1199,7 +1261,7 @@ backlog_store_newest_get(BacklogStore *bs, uint32_t *w, uint32_t *page_id)
      return NULL;
    head = &bs->pages[bs->count - 1];
    n = head->entries;
-   if (n == 0)
+   if ((n == 0) || !_head_restore(bs, head))
      return NULL;
    *w = head->widths[n - 1];
    *page_id = head->id;
@@ -1207,7 +1269,27 @@ backlog_store_newest_get(BacklogStore *bs, uint32_t *w, uint32_t *page_id)
       bs->head_cells - *w;
 }
 
-static Eina_Bool _page_reopen(BacklogStore *bs, BacklogStorePage *page);
+const Termcell *
+backlog_store_head_tail_get(const BacklogStore *bs, uint32_t *w)
+{
+   const BacklogStorePage *head;
+   uint32_t n;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(w, NULL);
+   *w = 0;
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bs, NULL);
+   if (!bs->count)
+     return NULL;
+   head = &bs->pages[bs->count - 1];
+   n = head->entries;
+   if (n == 0)
+     return NULL;
+   *w = head->widths[n - 1];
+   if (head->tail)
+     return (const Termcell *)head->tail;
+   return (const Termcell *)_page_cells(head->header, head->header_size) +
+      bs->head_cells - *w;
+}
 
 void
 backlog_store_page_dirty(BacklogStore *bs, uint32_t page_id)
@@ -1227,7 +1309,7 @@ backlog_store_page_dirty(BacklogStore *bs, uint32_t page_id)
 static size_t
 _page_memory(const BacklogStorePage *page, size_t cells_size)
 {
-   size_t bytes = page->header_size;
+   size_t bytes = page->header_size + _page_tail_size(page);
 
    if (page->state != PAGE_COMPRESSED)
      return bytes + cells_size;
@@ -1288,6 +1370,7 @@ static Eina_Bool
 _page_export(const BacklogStore *bs, BacklogStorePage *page)
 {
    size_t size = _page_used_size(bs, page->cells_size);
+   size_t blob_cells_size = page->cells_size - _page_tail_size(page);
    unsigned char *map;
    int fd;
 
@@ -1295,7 +1378,7 @@ _page_export(const BacklogStore *bs, BacklogStorePage *page)
    if (!map)
      return EINA_FALSE;
    if (!_page_rebuild(map, page->header, page->header_size, page->blob,
-                      page->stored_raw, page->cells_size))
+                      page->stored_raw, blob_cells_size))
      {
         ERR("cannot rebuild page %u of '%s'", page->id,
             bs->name);
@@ -1303,6 +1386,9 @@ _page_export(const BacklogStore *bs, BacklogStorePage *page)
         _page_fd_close(&fd);
         return EINA_FALSE;
      }
+   if (page->tail)
+     memcpy(map + page->header_size + blob_cells_size, page->tail,
+            _page_tail_size(page));
    page->export_map = (TermSrvBacklogPage *)map;
    page->export_size = size;
    page->export_fd = fd;
@@ -1321,6 +1407,8 @@ backlog_store_page_fd(BacklogStore *bs, uint32_t page_id)
    if (!page)
      return -1;
    page->idle_since = ecore_time_get();
+   if ((page->state == PAGE_COMPRESSED) && _page_is_head(bs, page))
+     return _head_restore(bs, page) ? page->fd : -1;
    if (page->state != PAGE_COMPRESSED)
      return page->fd;
    if (!page->export_map && !_page_export(bs, page))
@@ -1338,6 +1426,8 @@ backlog_store_page_get(BacklogStore *bs, uint32_t page_id)
    if (!page)
      return NULL;
    page->idle_since = ecore_time_get();
+   if ((page->state == PAGE_COMPRESSED) && _page_is_head(bs, page))
+     return _head_restore(bs, page) ? page->header : NULL;
    if (page->state != PAGE_COMPRESSED)
      return page->header;
    if (!page->export_map && !_page_export(bs, page))
@@ -1558,7 +1648,8 @@ backlog_store_page_fd_async(BacklogStore *bs, uint32_t page_id,
    if (!page)
      return EINA_FALSE;
    if ((bs->backing != BACKLOG_STORE_MEMFD) ||
-       (page->state != PAGE_COMPRESSED) || page->export_map)
+       (page->state != PAGE_COMPRESSED) || page->export_map ||
+       _page_is_head(bs, page))
      {
         done(data, page_id, backlog_store_page_fd(bs, page_id));
         return EINA_TRUE;
@@ -1598,7 +1689,8 @@ backlog_store_page_prefetch(BacklogStore *bs, uint32_t page_id)
    EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
    page = _page_find(bs, page_id);
    if (!page || (bs->backing != BACKLOG_STORE_ANON) ||
-       (page->state != PAGE_COMPRESSED) || page->export_map)
+       (page->state != PAGE_COMPRESSED) || page->export_map ||
+       _page_is_head(bs, page))
      return EINA_FALSE;
    if (_rebuild_find(bs, page_id))
      return EINA_TRUE;
@@ -1758,6 +1850,8 @@ _job_end(void *data, Ecore_Thread *thread EINA_UNUSED)
                   else
                     page->attempts++;
                }
+             if (page->state == PAGE_RAW)
+               _page_tail_drop(page);
           }
         _evict(bs);
      }
@@ -1782,22 +1876,32 @@ _job_cancel(void *data, Ecore_Thread *thread EINA_UNUSED)
           {
              page->state = PAGE_RAW;
              page->attempts++;
+             _page_tail_drop(page);
           }
      }
    _job_free(job);
 }
 
 /* Start compressing page, or under pressure copying it if compression is
- * known not to help. Returns whether the job was started. */
+ * known not to help. The head is compressed but for its newest entry,
+ * which is copied aside. Returns whether the job was started. */
 static Eina_Bool
 _job_start(BacklogStore *bs, BacklogStorePage *page, Eina_Bool pressure)
 {
    BacklogStoreJob *job;
+   const TermSrvCell *cells = _page_cells(page->header, page->header_size);
    uint32_t job_page_id = page->id;
+   size_t tail_size = 0, blob_cells_size;
 
+   if (_page_is_head(bs, page))
+     {
+        page->cells_size = (size_t)bs->head_cells * sizeof(TermSrvCell);
+        tail_size = page->widths[page->entries - 1] * sizeof(TermSrvCell);
+     }
+   blob_cells_size = page->cells_size - tail_size;
    /* even a blob of nothing could not free enough */
    if (!page->compress_poor &&
-       (_page_gain(page->cells_size, 0) < (ssize_t)COMPRESS_GAIN_MIN))
+       (_page_gain(blob_cells_size, 0) < (ssize_t)COMPRESS_GAIN_MIN))
      {
         page->compress_poor = EINA_TRUE;
         page->attempts = 0;
@@ -1811,13 +1915,28 @@ _job_start(BacklogStore *bs, BacklogStorePage *page, Eina_Bool pressure)
         page->attempts++;
         return EINA_FALSE;
      }
+   if (_page_is_head(bs, page))
+     {
+        /* never NULL, even for an empty entry: it tells a head that keeps
+         * its newest entry aside */
+        page->tail = malloc(MAX(tail_size, sizeof(TermSrvCell)));
+        if (!page->tail)
+          {
+             eina_semaphore_free(&job->done);
+             free(job);
+             page->attempts++;
+             return EINA_FALSE;
+          }
+        memcpy(page->tail, cells + blob_cells_size / sizeof(TermSrvCell),
+               tail_size);
+     }
    job->bs = bs;
    job->pressure = pressure;
    job->copy_only = page->compress_poor;
    atomic_init(&job->claim, JOB_QUEUED);
-   job->cells = _page_cells(page->header, page->header_size);
+   job->cells = cells;
    job->page_id = page->id;
-   job->cells_size = page->cells_size;
+   job->cells_size = blob_cells_size;
    page->state = PAGE_COMPRESSING;
    bs->jobs = eina_list_append(bs->jobs, job);
    _jobs_alive++;
@@ -1887,6 +2006,7 @@ _page_reopen(BacklogStore *bs, BacklogStorePage *page)
         page->state = PAGE_RAW;
         page->stored_raw = EINA_FALSE;
      }
+   _page_tail_drop(page);
    page->attempts = 0;
    page->compress_poor = EINA_FALSE;
    return EINA_TRUE;
@@ -1930,6 +2050,8 @@ backlog_store_pop(BacklogStore *bs)
    if (!bs->count)
      return EINA_FALSE;
    head = &bs->pages[bs->count - 1];
+   if (!_head_restore(bs, head))
+     return EINA_FALSE;
    while (head->entries == 0)
      {
         if (bs->count == 1 || !_page_reopen(bs, head - 1))
@@ -1956,9 +2078,9 @@ backlog_store_pop(BacklogStore *bs)
    return EINA_TRUE;
 }
 
-/* The sealed page that no client holds and that was used the longest ago,
- * among the MEMFD stores with a job to spare, that a job can free the memfd
- * of. Its store goes in *bs_out. */
+/* The sealed page, or quiet head, that no client holds and that was used
+ * the longest ago, among the MEMFD stores with a job to spare, that a job
+ * can free the memfd of. Its store goes in *bs_out. */
 static BacklogStorePage *
 _fds_victim_find(BacklogStore **bs_out, uint32_t *jobs)
 {
@@ -1973,13 +2095,16 @@ _fds_victim_find(BacklogStore **bs_out, uint32_t *jobs)
         *jobs += eina_list_count(bs->jobs);
         if (eina_list_count(bs->jobs) >= COMPRESS_JOBS_MAX)
           continue;
-        for (i = 0; i + 1 < bs->count; i++)
+        for (i = 0; i < bs->count; i++)
           {
              BacklogStorePage *page = &bs->pages[i];
 
              if ((page->state != PAGE_RAW) || (page->refs > 0) ||
                  (page->attempts >= COMPRESS_ATTEMPTS_MAX))
                continue;
+             if ((i + 1 == bs->count) &&
+                 !_head_quiet(bs, page, ecore_loop_time_get()))
+               continue;
              if (!victim || (page->idle_since < victim->idle_since))
                {
                   victim = page;
@@ -2044,6 +2169,17 @@ backlog_store_maintain(BacklogStore *bs, double now)
    uint32_t i;
 
    EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
+   if (bs->appended)
+     {
+        bs->last_append = now;
+        bs->appended = EINA_FALSE;
+        /* what was not worth compressing may be once it grew */
+        if (bs->count)
+          {
+             bs->pages[bs->count - 1].compress_poor = EINA_FALSE;
+             bs->pages[bs->count - 1].attempts = 0;
+          }
+     }
    _fds_relieve();
    for (i = 0; i < bs->count; i++)
      {
@@ -2056,14 +2192,19 @@ backlog_store_maintain(BacklogStore *bs, double now)
         else
           pending = EINA_TRUE;
      }
-   /* the head is still written to */
-   for (i = 0; i + 1 < bs->count; i++)
+   for (i = 0; i < bs->count; i++)
      {
         BacklogStorePage *page = &bs->pages[i];
 
         if (page->state != PAGE_RAW || page->refs > 0 ||
             page->compress_poor || page->attempts >= COMPRESS_ATTEMPTS_MAX)
           continue;
+        if ((i + 1 == bs->count) && !_head_quiet(bs, page, now))
+          {
+             if (page->entries > 1)
+               pending = EINA_TRUE;
+             continue;
+          }
         if ((eina_list_count(bs->jobs) < COMPRESS_JOBS_MAX) &&
             (now - page->idle_since >= COMPRESS_IDLE_SECONDS))
           _job_start(bs, page, EINA_FALSE);
@@ -3227,7 +3368,9 @@ _test_pop_reopen(BacklogStoreBacking backing)
    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);
+   backlog_store_maintain(bs, ecore_loop_time_get());
+   backlog_store_maintain(bs, ecore_loop_time_get() + BACKLOG_QUIET_SECONDS -
+                          1.0);
    assert(eina_list_count(bs->jobs) == 0);
    page = _test_page_map(bs, 0);
    assert(atomic_load(&page->num_entries) == _TEST_ENTRIES - 1);
@@ -4929,4 +5072,384 @@ tytest_backlog_store_append_fail(void)
    _test_backings_run(_test_append_fail_links);
    return 0;
 }
+
+#define TEST_HEAD_ROW_W   100
+#define TEST_HEAD_ENTRIES 200
+
+static void
+_test_head_row(Termcell *row, uint32_t entry)
+{
+   _test_row_fill(row, TEST_HEAD_ROW_W, entry * TEST_HEAD_ROW_W);
+}
+
+static void
+_test_head_fill(BacklogStore *bs, uint32_t first, uint32_t count)
+{
+   Termcell row[TEST_HEAD_ROW_W];
+   uint32_t i;
+
+   for (i = first; i < first + count; i++)
+     {
+        _test_head_row(row, i);
+        assert(backlog_store_append(bs, row, TEST_HEAD_ROW_W,
+                                    BACKLOG_STORE_NEW, EINA_TRUE, NULL));
+     }
+}
+
+/* Compare page 0 with the count rows _test_head_fill() stored. */
+static void
+_test_head_rows_check(const TermSrvBacklogPage *page, uint32_t count)
+{
+   const TermSrvCell *cells = _test_page_cells(page);
+   Termcell row[TEST_HEAD_ROW_W];
+   uint32_t i;
+
+   assert(page->page_id == 0);
+   assert(atomic_load(&page->num_entries) == count);
+   for (i = 0; i < count; i++)
+     {
+        _test_head_row(row, i);
+        assert(_test_off(page)[i] == i * TEST_HEAD_ROW_W);
+        assert(atomic_load(&_test_w(page)[i]) == TEST_HEAD_ROW_W);
+        assert(!memcmp(&cells[_test_off(page)[i]], row, sizeof(row)));
+     }
+}
+
+static void
+_test_head_check(BacklogStore *bs, uint32_t count)
+{
+   const TermSrvBacklogPage *page = backlog_store_page_get(bs, 0);
+
+   assert(page);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   _test_head_rows_check(page, count);
+}
+
+static void
+_test_head_tail_check(const BacklogStore *bs, uint32_t entry)
+{
+   Termcell row[TEST_HEAD_ROW_W];
+   const Termcell *tail;
+   uint32_t w;
+
+   tail = backlog_store_head_tail_get(bs, &w);
+   _test_head_row(row, entry);
+   assert(tail && (w == TEST_HEAD_ROW_W));
+   assert(!memcmp(tail, row, sizeof(row)));
+}
+
+/* Run maintain as it would when it sees the last appends, then once
+ * nothing was appended for BACKLOG_QUIET_SECONDS, and wait for the head of
+ * a single page store to be compressed. */
+static void
+_test_head_compress(BacklogStore *bs)
+{
+   double now = ecore_loop_time_get();
+
+   assert(backlog_store_count(bs) == 1);
+   backlog_store_maintain(bs, now);
+   assert(!bs->jobs);
+   backlog_store_maintain(bs, now + BACKLOG_QUIET_SECONDS);
+   assert(eina_list_count(bs->jobs) == 1);
+   _test_jobs_wait(bs);
+   assert(_page_find(bs, 0)->state == PAGE_COMPRESSED);
+}
+
+/* Test: the head that nobody holds is compressed like a sealed page once
+ * nothing was appended to it for BACKLOG_QUIET_SECONDS, but for its newest
+ * entry, which stays readable. A held head, a head appended to lately or
+ * holding only one entry, and a head too small to shrink stay raw; the
+ * last is compressed once it grew. Asking for the compressed head gives
+ * it back raw, without a rebuild. */
+int
+tytest_backlog_store_head_compress(void)
+{
+   BacklogStore *bs;
+   uint32_t w;
+   size_t raw_size;
+   double now;
+
+   ecore_init();
+   bs = backlog_store_new("test", BACKLOG_STORE_ANON, _TEST_SCROLLBACK);
+   assert(bs);
+   assert(!backlog_store_head_tail_get(bs, &w) && (w == 0));
+   _test_head_fill(bs, 0, TEST_HEAD_ENTRIES);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES - 1);
+   raw_size = backlog_store_memory_get(bs);
+   now = ecore_loop_time_get();
+
+   backlog_store_maintain(bs, now);
+   backlog_store_maintain(bs, now + BACKLOG_QUIET_SECONDS - 1.0);
+   assert(!bs->jobs);
+   backlog_store_hold(bs, 0);
+   backlog_store_maintain(bs, now + BACKLOG_QUIET_SECONDS);
+   assert(!bs->jobs);
+   backlog_store_release(bs, 0);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   _test_head_compress(bs);
+   assert(backlog_store_memory_get(bs) < raw_size);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES - 1);
+
+   assert(!backlog_store_page_ready(bs, 0));
+   assert(!backlog_store_page_prefetch(bs, 0));
+   assert(!bs->rebuilds);
+   _test_head_check(bs, TEST_HEAD_ENTRIES);
+   assert(!_page_find(bs, 0)->export_map);
+   assert(backlog_store_memory_get(bs) == raw_size);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES - 1);
+   backlog_store_free(bs);
+
+   /* nothing to compress but the newest entry */
+   bs = backlog_store_new("test", BACKLOG_STORE_ANON, _TEST_SCROLLBACK);
+   assert(bs);
+   _test_head_fill(bs, 0, 1);
+   now = ecore_loop_time_get();
+   backlog_store_maintain(bs, now);
+   backlog_store_maintain(bs, now + 600.0);
+   assert(!bs->jobs);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   backlog_store_free(bs);
+
+   /* too small to be worth it, until it grew */
+   bs = backlog_store_new("test", BACKLOG_STORE_ANON, _TEST_SCROLLBACK);
+   assert(bs);
+   _test_head_fill(bs, 0, 2);
+   now = ecore_loop_time_get();
+   backlog_store_maintain(bs, now);
+   backlog_store_maintain(bs, now + BACKLOG_QUIET_SECONDS);
+   assert(!bs->jobs);
+   _test_head_fill(bs, 2, TEST_HEAD_ENTRIES - 2);
+   _test_head_compress(bs);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES - 1);
+   _test_head_check(bs, TEST_HEAD_ENTRIES);
+   backlog_store_free(bs);
+   ecore_shutdown();
+   return 0;
+}
+
+/* Test: compressing the head of a MEMFD store closes its memfd, and a
+ * client asking for the head, at once or on a thread, gets the head
+ * itself back raw, with a memfd of its own, never a rebuilt copy. Over the
+ * memfd budget, a quiet head is relieved too, and copied as it is when it
+ * does not shrink. */
+int
+tytest_backlog_store_head_compress_memfd(void)
+{
+#if defined(__linux__)
+   Test_Fd_Async got = { 0, 0, -1 };
+   const TermSrvBacklogPage *client;
+   BacklogStorePage *page;
+   BacklogStore *bs;
+   Termcell row[TEST_ROW_W];
+   uint32_t raw_fds, i;
+   size_t raw_size;
+   double now;
+   int fd;
+
+   ecore_init();
+   bs = backlog_store_new("test", BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+   assert(bs);
+   _test_head_fill(bs, 0, TEST_HEAD_ENTRIES);
+   raw_fds = backlog_store_raw_fds_get();
+   raw_size = backlog_store_memory_get(bs);
+   now = ecore_loop_time_get();
+
+   backlog_store_maintain(bs, now);
+   backlog_store_maintain(bs, now + BACKLOG_QUIET_SECONDS - 1.0);
+   assert(!bs->jobs);
+   backlog_store_hold(bs, 0);
+   backlog_store_maintain(bs, now + BACKLOG_QUIET_SECONDS);
+   assert(!bs->jobs);
+   backlog_store_release(bs, 0);
+   _test_head_compress(bs);
+   assert(backlog_store_raw_fds_get() == raw_fds - 1);
+   assert(backlog_store_memory_get(bs) < raw_size);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES - 1);
+
+   fd = backlog_store_page_fd(bs, 0);
+   page = _page_find(bs, 0);
+   assert(fd >= 0);
+   assert((page->state == PAGE_RAW) && (page->fd == fd));
+   assert(!page->export_map && (page->export_fd < 0));
+   assert(backlog_store_raw_fds_get() == raw_fds);
+   client = mmap(NULL, bs->geo.page_size, PROT_READ, MAP_SHARED, fd, 0);
+   assert(client != MAP_FAILED);
+   _test_head_rows_check(client, TEST_HEAD_ENTRIES);
+   munmap((void *)client, bs->geo.page_size);
+   _test_head_check(bs, TEST_HEAD_ENTRIES);
+
+   _test_head_compress(bs);
+   assert(backlog_store_page_fd_async(bs, 0, _test_fd_async_done, &got));
+   assert((got.calls == 1) && (got.fd >= 0));
+   assert(!bs->rebuilds);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   assert(backlog_store_raw_fds_get() == raw_fds);
+   client = mmap(NULL, bs->geo.page_size, PROT_READ, MAP_SHARED, got.fd, 0);
+   assert(client != MAP_FAILED);
+   _test_head_rows_check(client, TEST_HEAD_ENTRIES);
+   munmap((void *)client, bs->geo.page_size);
+   close(got.fd);
+   backlog_store_free(bs);
+   assert(backlog_store_raw_fds_get() == 0);
+
+   /* over the memfd budget, a quiet head that does not shrink is copied
+    * as it is, its newest entry kept aside */
+   bs = backlog_store_new("test", BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+   assert(bs);
+   for (i = 0; i < _TEST_ENTRIES - 1; i++)
+     {
+        _test_budget_row(row, 0, i);
+        assert(backlog_store_append(bs, row, TEST_ROW_W, BACKLOG_STORE_NEW,
+                                    EINA_FALSE, NULL));
+     }
+   assert(backlog_store_count(bs) == 1);
+   now = ecore_loop_time_get();
+   backlog_store_maintain(bs, now);
+   assert(!bs->jobs);
+   /* the memfd budget runs on the loop clock, not on maintain's now */
+   bs->last_append -= BACKLOG_QUIET_SECONDS;
+   backlog_store_fd_budget_set(0);
+   backlog_store_maintain(bs, now);
+   assert(eina_list_count(bs->jobs) == 1);
+   _test_jobs_wait(NULL);
+   page = _page_find(bs, 0);
+   assert((page->state == PAGE_COMPRESSED) && page->stored_raw);
+   assert(page->tail);
+   assert(backlog_store_raw_fds_get() == 0);
+   backlog_store_fd_budget_set(UINT32_MAX);
+   _test_budget_check(bs, 0, 0);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   assert(atomic_load(&backlog_store_page_get(bs, 0)->num_entries) ==
+          _TEST_ENTRIES - 1);
+   backlog_store_free(bs);
+   assert(backlog_store_raw_fds_get() == 0);
+   ecore_shutdown();
+#endif
+   return 0;
+}
+
+/* Test: an append to a compressed head restores it in place: the new
+ * entry follows the others, which come back as they were. */
+static void
+_test_head_compressed_append(BacklogStoreBacking backing)
+{
+   BacklogStore *bs;
+
+   ecore_init();
+   bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+   assert(bs);
+   _test_head_fill(bs, 0, TEST_HEAD_ENTRIES);
+   _test_head_compress(bs);
+   _test_head_fill(bs, TEST_HEAD_ENTRIES, 1);
+   assert(backlog_store_count(bs) == 1);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   _test_head_check(bs, TEST_HEAD_ENTRIES + 1);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES);
+   backlog_store_free(bs);
+   ecore_shutdown();
+}
+
+int
+tytest_backlog_store_head_compressed_append(void)
+{
+   _test_backings_run(_test_head_compressed_append);
+   return 0;
+}
+
+/* Test: a wrapped newest entry of a compressed head is extended by the
+ * next row, right after its cells; one that does not wrap is not. */
+static void
+_test_head_compressed_extend(BacklogStoreBacking backing)
+{
+   const TermSrvBacklogPage *page;
+   const TermSrvCell *cells;
+   const Termcell *tail;
+   BacklogStore *bs;
+   Termcell row[TEST_HEAD_ROW_W];
+   uint32_t i, w, off, last = TEST_HEAD_ENTRIES - 1;
+
+   ecore_init();
+   bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+   assert(bs);
+   _test_head_fill(bs, 0, last);
+   _test_head_row(row, last);
+   row[TEST_HEAD_ROW_W - 1].att.autowrapped = 1;
+   assert(backlog_store_append(bs, row, TEST_HEAD_ROW_W, BACKLOG_STORE_NEW,
+                               EINA_TRUE, NULL));
+   _test_head_compress(bs);
+   tail = backlog_store_head_tail_get(bs, &w);
+   assert(tail && (w == TEST_HEAD_ROW_W));
+   assert(tail[TEST_HEAD_ROW_W - 1].att.autowrapped);
+
+   _test_row_fill(row, 20, 'A');
+   assert(backlog_store_append(bs, row, 20, BACKLOG_STORE_EXTEND_WRAPPED,
+                               EINA_TRUE, NULL));
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   page = backlog_store_page_get(bs, 0);
+   assert(page);
+   assert(atomic_load(&page->num_entries) == TEST_HEAD_ENTRIES);
+   assert(atomic_load(&_test_w(page)[last]) == TEST_HEAD_ROW_W + 20);
+   cells = _test_page_cells(page);
+   off = _test_off(page)[last];
+   assert(off == last * TEST_HEAD_ROW_W);
+   for (i = 0; i < TEST_HEAD_ROW_W; i++)
+     assert(cells[off + i].codepoint == last * TEST_HEAD_ROW_W + i);
+   for (i = 0; i < 20; i++)
+     assert(cells[off + TEST_HEAD_ROW_W + i].codepoint == 'A' + i);
+   assert(cells[_test_off(page)[last - 1]].codepoint ==
+          (last - 1) * TEST_HEAD_ROW_W);
+
+   _test_head_compress(bs);
+   assert(backlog_store_append(bs, row, 20, BACKLOG_STORE_EXTEND_WRAPPED,
+                               EINA_TRUE, NULL));
+   page = backlog_store_page_get(bs, 0);
+   assert(page);
+   assert(atomic_load(&page->num_entries) == TEST_HEAD_ENTRIES + 1);
+   assert(atomic_load(&_test_w(page)[last]) == TEST_HEAD_ROW_W + 20);
+   assert(atomic_load(&_test_w(page)[last + 1]) == 20);
+   backlog_store_free(bs);
+   ecore_shutdown();
+}
+
+int
+tytest_backlog_store_head_compressed_extend(void)
+{
+   _test_backings_run(_test_head_compressed_extend);
+   return 0;
+}
+
+/* Test: pop and page_dirty on a compressed head restore it first: pop then
+ * takes the newest entry back, and the one before is the newest. */
+static void
+_test_head_compressed_pop(BacklogStoreBacking backing)
+{
+   BacklogStore *bs;
+
+   ecore_init();
+   bs = backlog_store_new("test", backing, _TEST_SCROLLBACK);
+   assert(bs);
+   _test_head_fill(bs, 0, TEST_HEAD_ENTRIES);
+   _test_head_compress(bs);
+   assert(backlog_store_pop(bs));
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   assert(_page_find(bs, 0)->entries == TEST_HEAD_ENTRIES - 1);
+   _test_head_tail_check(bs, TEST_HEAD_ENTRIES - 2);
+   _test_head_check(bs, TEST_HEAD_ENTRIES - 1);
+
+   _test_head_compress(bs);
+   backlog_store_page_dirty(bs, 0);
+   assert(_page_find(bs, 0)->state == PAGE_RAW);
+   _test_head_check(bs, TEST_HEAD_ENTRIES - 1);
+   _test_head_fill(bs, TEST_HEAD_ENTRIES - 1, 2);
+   _test_head_check(bs, TEST_HEAD_ENTRIES + 1);
+   backlog_store_free(bs);
+   ecore_shutdown();
+}
+
+int
+tytest_backlog_store_head_compressed_pop(void)
+{
+   _test_backings_run(_test_head_compressed_pop);
+   return 0;
+}
 #endif
diff --git a/src/bin/backlog_store.h b/src/bin/backlog_store.h
index c7b6e445..63419cc1 100644
--- a/src/bin/backlog_store.h
+++ b/src/bin/backlog_store.h
@@ -7,6 +7,10 @@
  * to. Include termpty.h and termsrv.h first. */
 typedef struct tag_BacklogStore BacklogStore;
 
+/* A head that nobody holds is compressed once nothing was appended to it
+ * for this long. */
+#define BACKLOG_QUIET_SECONDS 10.0
+
 typedef enum
 {
    /* pages in anonymous memory, for a store read in-process: it holds no
@@ -87,7 +91,8 @@ const BacklogStoreGeometry *backlog_store_geometry(const BacklogStore *bs);
 void backlog_store_scrollback_set(BacklogStore *bs, uint32_t scrollback);
 
 /* The cells of the newest entry, in the head page, its width in *w and the
- * id of the head in *page_id. The cells may move when backlog_store_append()
+ * id of the head in *page_id. A compressed head is restored first, which
+ * waits for zlib. The cells may move when backlog_store_append()
  * grows the head's mapping, which backlog_store_moved_cb_set() reports.
  * Returns NULL if the head holds no entry, as when it is new or
  * backlog_store_pop() emptied it: backlog_store_append() always leaves one
@@ -95,6 +100,14 @@ void backlog_store_scrollback_set(BacklogStore *bs, uint32_t scrollback);
 Termcell *backlog_store_newest_get(BacklogStore *bs, uint32_t *w,
                                    uint32_t *page_id);
 
+/* The cells of the newest entry and its width in *w, whatever state the
+ * head is in: a compressed head keeps them aside. Valid until the next call
+ * that may restore the head (append, pop, page_get, page_fd, page_fd_async,
+ * page_dirty, newest_get), free, or a return to the main loop. Returns
+ * NULL, and 0 in *w, if there is no entry. */
+const Termcell *backlog_store_head_tail_get(const BacklogStore *bs,
+                                            uint32_t *w);
+
 /* Tell the store that cells of page_id were written to, through a mapping
  * that backlog_store_page_get() returned: a compressed copy of the page is
  * stale, and is dropped. */
@@ -168,7 +181,8 @@ Eina_Bool backlog_store_page_fd_async(BacklogStore *bs, uint32_t page_id,
                                       void *data);
 
 /* The mapping of page_id. A compressed page is rebuilt first, into
- * anonymous memory or into the memfd that backlog_store_page_fd() returns.
+ * anonymous memory or into the memfd that backlog_store_page_fd() returns;
+ * a compressed head is restored as the head itself, never as a copy.
  * Hold the page to keep the mapping valid: it then stays valid, even once
  * the page is evicted, until the last backlog_store_release() of the page
  * or backlog_store_free(). The exception is the head, whose mapping may be
@@ -216,7 +230,10 @@ void backlog_store_release(BacklogStore *bs, uint32_t page_id);
 
 /* To be called about once a second, with the ecore_time_get() clock.
  * Compresses, on up to two threads, the sealed pages that no client holds
- * and that were idle for 5 seconds, unless they hardly shrink. A page that
+ * and that were idle for 5 seconds, unless they hardly shrink, and the head
+ * too once no call saw an append to it for BACKLOG_QUIET_SECONDS, but for
+ * its newest entry, which backlog_store_head_tail_get() still reads: what
+ * needs the compressed head restores it on the main loop. A page that
  * a client asks for while it is compressed stays as it is. Drops the
  * rebuilt pages that were not used for 60 seconds. While the MEMFD stores
  * hold more memfds than backlog_store_fd_budget_set() allows, it first
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 5464fbf2..ec266bf8 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -187,6 +187,15 @@ static struct {
        { "backlog_store_page_fd_async", tytest_backlog_store_page_fd_async},
        { "backlog_store_sealed", tytest_backlog_store_sealed},
        { "backlog_store_append_fail", tytest_backlog_store_append_fail},
+       { "backlog_store_head_compress", tytest_backlog_store_head_compress},
+       { "backlog_store_head_compress_memfd",
+         tytest_backlog_store_head_compress_memfd},
+       { "backlog_store_head_compressed_append",
+         tytest_backlog_store_head_compressed_append},
+       { "backlog_store_head_compressed_extend",
+         tytest_backlog_store_head_compressed_extend},
+       { "backlog_store_head_compressed_pop",
+         tytest_backlog_store_head_compressed_pop},
        { "backlog_view_sync", tytest_backlog_view_sync},
        { "backlog_view_ring_full", tytest_backlog_view_ring_full},
        { "backlog_view_map_again", tytest_backlog_view_map_again},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 32963207..518a53ac 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -142,6 +142,11 @@ int tytest_backlog_store_fd_budget(void);
 int tytest_backlog_store_page_fd_async(void);
 int tytest_backlog_store_sealed(void);
 int tytest_backlog_store_append_fail(void);
+int tytest_backlog_store_head_compress(void);
+int tytest_backlog_store_head_compress_memfd(void);
+int tytest_backlog_store_head_compressed_append(void);
+int tytest_backlog_store_head_compressed_extend(void);
+int tytest_backlog_store_head_compressed_pop(void);
 int tytest_backlog_view_sync(void);
 int tytest_backlog_view_ring_full(void);
 int tytest_backlog_view_map_again(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to