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 d9d101fb2f007b87ba7e4ca62fd9060a84b23d30
Author: Cedric BAIL <[email protected]>
AuthorDate: Thu Nov 19 10:09:49 2026 -0600
termd_backlog: compress the sealed pages no client holds
The page store kept every sealed page as a raw memfd for the whole
session, however long since a client looked at it. A sealed page that
nobody holds and that was idle for 5 s is now compressed on an ecore
thread (2 at a time, from a 1 s daemon timer) and its memfd dropped; a
copy of its header stays for the page map and eviction. The cells are
sized from the daemon's own count, set when the page seals, not from the
header, which clients can write to.
A page whose blob is larger than 85% of the cells, or frees less than
16 KiB once the blob and the header copy are paid for, stays raw for
good: short lines make 20 KB pages, where compressing cost more than it
freed.
A client asking for a compressed page gets it rebuilt into a new memfd,
cached until unused for 60 s. The rebuild runs on the main loop: 2.7 ms
for a 2 MB page, 19 ms for a 16 MB page, and 78 ms for a 64 MB page (one
full 4M-cell entry). The largest possible page, about 80 MB, would take
about 100 ms by the same rate. A page requested while its job runs is
served from the memfd still there and stays raw. Freeing the store
detaches the jobs in flight; each worker has its own dup of the memfd.
The exact size of the cells is stored, as backlog_uncompress() cannot
detect a length that is too large.
VmRSS of the daemon, in kB, 9 s after the workload with one client
attached (two runs before, three after):
seq 1 10000: before 15584 15564, after 15576 15512 15624
80 column text: before 27040 27056, after 23440 23444 23408
tybenchd -i 5 1000000, median of 5, three runs:
before 0.368 0.355 0.358 s, after 0.353 0.357 0.349 s
---
src/bin/termd_backlog.c | 640 +++++++++++++++++++++++++++++++++++++++++++++++-
src/bin/termd_backlog.h | 16 +-
src/bin/termd_tymux.c | 18 ++
src/bin/tytest.c | 4 +
src/bin/tytest_tymux.c | 73 +++++-
src/bin/tytest_tymux.h | 1 +
src/bin/unit_tests.h | 3 +
tests/meson.build | 3 +-
8 files changed, 742 insertions(+), 16 deletions(-)
diff --git a/src/bin/termd_backlog.c b/src/bin/termd_backlog.c
index 5b2c9971..45b2d6e4 100644
--- a/src/bin/termd_backlog.c
+++ b/src/bin/termd_backlog.c
@@ -3,27 +3,74 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <Elementary.h>
#include "termpty.h"
#include "termsrv.h"
#include "termd_backlog.h"
+#include "backlog_compress.h"
+
+/* A sealed page nobody holds is compressed once it has been idle this
+ * long, so that scrolling back to the newest history right after it left
+ * the screen does not pay for a rebuild. */
+#define COMPRESS_IDLE_SECONDS 5.0
+/* Rebuilt pages are cached until their last use is this old. A client that
+ * was sent one keeps its own duplicate of the memfd alive. */
+#define EXPORT_STALE_SECONDS 60.0
+#define COMPRESS_JOBS_MAX 2
+#define COMPRESS_ATTEMPTS_MAX 3
+/* A blob that saves less than 15% is not worth the rebuilds it costs. */
+#define COMPRESS_RATIO_MAX 0.85
+/* What dropping the memfd must free, after the blob, the header copy and
+ * the allocator's overhead: below it the page costs more resident memory
+ * than it saves once fragmentation is counted. */
+#define COMPRESS_GAIN_MIN (16u * 1024)
+#define COMPRESS_FIXED_COST (sizeof(TermSrvBacklogPage) + 32)
_Static_assert(sizeof(Termcell) == sizeof(TermSrvCell),
"TermSrvCell must mirror Termcell");
+typedef enum
+{
+ PAGE_RAW,
+ PAGE_COMPRESSING,
+ PAGE_COMPRESSED
+} TermDBacklogPageState;
+
+/* A RAW or COMPRESSING page is a mapping of fd. A COMPRESSED page has let
+ * go of both: header is a private copy of the page header, and the cells
+ * are in blob, to be rebuilt into export_fd when a client asks for the
+ * page. */
typedef struct
{
- TermSrvBacklogPage *header;
+ 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. */
- uint32_t entries;
- uint32_t last_w;
- int fd;
- uint32_t refs;
+ uint32_t entries;
+ uint32_t last_w;
+ int fd;
+ uint32_t refs;
+ TermDBacklogPageState state;
+ /* when the page was sealed, or last held or sent: it is compressed or
+ * its export dropped once that is old enough */
+ double idle_since;
+ uint32_t attempts;
+ size_t cells_size; /* the bytes of cells in use */
+ Eina_Binbuf *blob;
+ int export_fd;
} TermDBacklogPage;
+typedef struct
+{
+ TermDBacklog *bl; /* NULL once the store is gone */
+ uint32_t page_id;
+ int fd; /* a duplicate of the page memfd, the worker's own */
+ size_t cells_size;
+ Eina_Binbuf *blob;
+} TermDBacklogJob;
+
struct tag_TermDBacklog
{
const char *name;
@@ -34,6 +81,7 @@ struct tag_TermDBacklog
uint32_t max_entries;
uint32_t head_cells;
uint64_t total_entries;
+ Eina_List *jobs;
};
static TermSrvCell *
@@ -53,13 +101,59 @@ _page_find(TermDBacklog *bl, uint32_t page_id)
return &bl->pages[idx];
}
+static void
+_page_export_drop(TermDBacklogPage *page)
+{
+ if (page->export_fd >= 0)
+ close(page->export_fd);
+ page->export_fd = -1;
+}
+
static void
_page_close(TermDBacklogPage *page)
{
+ if (page->state == PAGE_COMPRESSED)
+ {
+ free(page->header);
+ eina_binbuf_free(page->blob);
+ _page_export_drop(page);
+ return;
+ }
munmap(page->header, TERMSRV_BACKLOG_PAGE_SIZE);
close(page->fd);
}
+/* The resident memory that compressing the cells of a page into a blob of
+ * blob_len bytes frees, the header copy and the allocator's overhead
+ * counted. */
+static ssize_t
+_page_gain(size_t cells_size, size_t blob_len)
+{
+ size_t resident = (cells_size + 4095) & ~(size_t)4095;
+
+ return (ssize_t)resident - (ssize_t)(blob_len + COMPRESS_FIXED_COST);
+}
+
+/* Replace the mapping of a RAW page by a copy of its header, keeping the
+ * blob of its cells. Returns EINA_FALSE, and changes nothing, if the copy
+ * cannot be made. */
+static Eina_Bool
+_page_compressed_set(TermDBacklogPage *page, Eina_Binbuf *blob)
+{
+ TermSrvBacklogPage *copy = malloc(sizeof(*copy));
+
+ if (!copy)
+ return EINA_FALSE;
+ memcpy(copy, page->header, sizeof(*copy));
+ munmap(page->header, TERMSRV_BACKLOG_PAGE_SIZE);
+ close(page->fd);
+ page->header = copy;
+ page->fd = -1;
+ page->blob = blob;
+ page->state = PAGE_COMPRESSED;
+ return EINA_TRUE;
+}
+
static TermSrvBacklogPage *
_page_add(TermDBacklog *bl)
{
@@ -97,6 +191,8 @@ _page_add(TermDBacklog *bl)
return NULL;
}
page = &bl->pages[bl->count];
+ memset(page, 0, sizeof(*page));
+ page->export_fd = -1;
page->header = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (page->header == MAP_FAILED)
@@ -106,7 +202,12 @@ _page_add(TermDBacklog *bl)
return NULL;
}
page->fd = fd;
- page->refs = 0;
+ if (bl->count > 0)
+ {
+ bl->pages[bl->count - 1].idle_since = ecore_time_get();
+ bl->pages[bl->count - 1].cells_size =
+ bl->head_cells * sizeof(TermSrvCell);
+ }
page->header->magic = TERMSRV_BACKLOG_MAGIC;
page->header->version = TERMSRV_BACKLOG_VERSION;
page->header->page_id = page_id;
@@ -122,6 +223,9 @@ _evict(TermDBacklog *bl)
{
uint32_t oldest = bl->pages[0].entries;
+ /* the job still reads it; its end evicts again */
+ if (bl->pages[0].state == PAGE_COMPRESSING)
+ return;
if (bl->total_entries - oldest < bl->max_entries)
return;
_page_close(&bl->pages[0]);
@@ -150,10 +254,15 @@ termd_backlog_new(const char *name, uint32_t max_entries)
void
termd_backlog_free(TermDBacklog *bl)
{
+ TermDBacklogJob *job;
uint32_t i;
if (!bl)
return;
+ /* the workers have duplicates of what they read, and their end
+ * callbacks must find that nothing is left to update */
+ EINA_LIST_FREE(bl->jobs, job)
+ job->bl = NULL;
for (i = 0; i < bl->count; i++)
_page_close(&bl->pages[i]);
free(bl->pages);
@@ -235,6 +344,54 @@ termd_backlog_count(const TermDBacklog *bl)
return bl->count;
}
+/* Rebuild a COMPRESSED page into a memfd of the layout it had. It
+ * expands on the calling thread: up to TERMSRV_BACKLOG_PAGE_SEAL_CELLS
+ * cells, which takes some milliseconds. */
+static int
+_page_export(const TermDBacklog *bl, TermDBacklogPage *page)
+{
+ char memfd_name[128];
+ unsigned char *map;
+ size_t map_size = TERMSRV_BACKLOG_HEADER_SIZE + page->cells_size;
+ Eina_Bool ok;
+ int fd;
+
+ snprintf(memfd_name, sizeof(memfd_name), "ty-export-%s-%u",
+ bl->name, page->header->page_id);
+ fd = memfd_create(memfd_name, MFD_CLOEXEC);
+ if (fd < 0)
+ {
+ ERR("memfd_create: %s", strerror(errno));
+ return -1;
+ }
+ if (ftruncate(fd, TERMSRV_BACKLOG_PAGE_SIZE) < 0)
+ {
+ ERR("ftruncate: %s", strerror(errno));
+ close(fd);
+ return -1;
+ }
+ map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ if (map == MAP_FAILED)
+ {
+ ERR("mmap: %s", strerror(errno));
+ close(fd);
+ return -1;
+ }
+ memcpy(map, page->header, sizeof(*page->header));
+ ok = backlog_uncompress(page->blob, map + TERMSRV_BACKLOG_HEADER_SIZE,
+ page->cells_size);
+ munmap(map, map_size);
+ if (!ok)
+ {
+ ERR("cannot rebuild page %u of '%s'", page->header->page_id,
+ bl->name);
+ close(fd);
+ return -1;
+ }
+ page->export_fd = fd;
+ return fd;
+}
+
int
termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id)
{
@@ -242,7 +399,14 @@ termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id)
EINA_SAFETY_ON_NULL_RETURN_VAL(bl, -1);
page = _page_find(bl, page_id);
- return page ? page->fd : -1;
+ if (!page)
+ return -1;
+ page->idle_since = ecore_time_get();
+ if (page->state != PAGE_COMPRESSED)
+ return page->fd;
+ if (page->export_fd >= 0)
+ return page->export_fd;
+ return _page_export(bl, page);
}
Eina_Bool
@@ -271,7 +435,153 @@ termd_backlog_release(TermDBacklog *bl, uint32_t page_id)
EINA_SAFETY_ON_NULL_RETURN(bl);
page = _page_find(bl, page_id);
if (page && page->refs > 0)
- page->refs--;
+ {
+ page->refs--;
+ page->idle_since = ecore_time_get();
+ }
+}
+
+static void
+_job_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
+{
+ TermDBacklogJob *job = data;
+ void *cells;
+
+ cells = mmap(NULL, job->cells_size, PROT_READ, MAP_SHARED, job->fd,
+ TERMSRV_BACKLOG_HEADER_SIZE);
+ if (cells != MAP_FAILED)
+ {
+ job->blob = backlog_compress(cells, job->cells_size);
+ munmap(cells, job->cells_size);
+ }
+ close(job->fd);
+ job->fd = -1;
+}
+
+static void
+_job_end(void *data, Ecore_Thread *thread EINA_UNUSED)
+{
+ TermDBacklogJob *job = data;
+ TermDBacklog *bl = job->bl;
+
+ if (bl)
+ {
+ TermDBacklogPage *page = _page_find(bl, job->page_id);
+
+ bl->jobs = eina_list_remove(bl->jobs, job);
+ if (page && page->state == PAGE_COMPRESSING)
+ {
+ Eina_Bool worthwhile = job->blob &&
+ eina_binbuf_length_get(job->blob) <=
+ COMPRESS_RATIO_MAX * job->cells_size &&
+ _page_gain(job->cells_size,
+ eina_binbuf_length_get(job->blob)) >=
+ (ssize_t)COMPRESS_GAIN_MIN;
+
+ page->state = PAGE_RAW;
+ if (!job->blob)
+ page->attempts++;
+ else if (!worthwhile)
+ page->attempts = COMPRESS_ATTEMPTS_MAX;
+ /* a client got the page meanwhile: it needs the memfd */
+ else if (page->refs == 0)
+ {
+ if (_page_compressed_set(page, job->blob))
+ job->blob = NULL;
+ else
+ page->attempts++;
+ }
+ }
+ _evict(bl);
+ }
+ if (job->blob)
+ eina_binbuf_free(job->blob);
+ free(job);
+}
+
+/* Also called by ecore_thread_run() itself when it cannot start the job,
+ * and at ecore shutdown, when the worker never ran. */
+static void
+_job_cancel(void *data, Ecore_Thread *thread EINA_UNUSED)
+{
+ TermDBacklogJob *job = data;
+ TermDBacklog *bl = job->bl;
+
+ if (bl)
+ {
+ TermDBacklogPage *page = _page_find(bl, job->page_id);
+
+ bl->jobs = eina_list_remove(bl->jobs, job);
+ if (page && page->state == PAGE_COMPRESSING)
+ {
+ page->state = PAGE_RAW;
+ page->attempts++;
+ }
+ }
+ if (job->fd >= 0)
+ close(job->fd);
+ if (job->blob)
+ eina_binbuf_free(job->blob);
+ free(job);
+}
+
+static void
+_job_start(TermDBacklog *bl, TermDBacklogPage *page)
+{
+ TermDBacklogJob *job;
+
+ /* even a blob of nothing could not free enough */
+ if (_page_gain(page->cells_size, 0) < (ssize_t)COMPRESS_GAIN_MIN)
+ {
+ page->attempts = COMPRESS_ATTEMPTS_MAX;
+ return;
+ }
+ job = calloc(1, sizeof(*job));
+ if (!job)
+ {
+ page->attempts++;
+ return;
+ }
+ job->fd = fcntl(page->fd, F_DUPFD_CLOEXEC, 0);
+ if (job->fd < 0)
+ {
+ page->attempts++;
+ free(job);
+ return;
+ }
+ job->bl = bl;
+ job->page_id = page->header->page_id;
+ job->cells_size = page->cells_size;
+ page->state = PAGE_COMPRESSING;
+ bl->jobs = eina_list_append(bl->jobs, job);
+ ecore_thread_run(_job_worker, _job_end, _job_cancel, job);
+}
+
+void
+termd_backlog_maintain(TermDBacklog *bl, double now)
+{
+ uint32_t i;
+
+ EINA_SAFETY_ON_NULL_RETURN(bl);
+ for (i = 0; i < bl->count; i++)
+ {
+ TermDBacklogPage *page = &bl->pages[i];
+
+ if (page->export_fd >= 0 && page->refs == 0 &&
+ now - page->idle_since >= EXPORT_STALE_SECONDS)
+ _page_export_drop(page);
+ }
+ /* the head is still written to */
+ for (i = 0; i + 1 < bl->count &&
+ eina_list_count(bl->jobs) < COMPRESS_JOBS_MAX; i++)
+ {
+ TermDBacklogPage *page = &bl->pages[i];
+
+ if (page->state == PAGE_RAW && page->refs == 0 &&
+ page->attempts < COMPRESS_ATTEMPTS_MAX &&
+ now - page->idle_since >= COMPRESS_IDLE_SECONDS)
+ _job_start(bl, page);
+ }
}
uint32_t
@@ -559,4 +869,318 @@ tytest_termd_backlog_map(void)
termd_backlog_free(bl);
return 0;
}
+typedef struct
+{
+ TermDBacklog *bl;
+ Eina_Bool timed_out;
+} Test_Wait;
+
+static Eina_Bool
+_test_jobs_poll_cb(void *data)
+{
+ Test_Wait *wait = data;
+
+ if (eina_list_count(wait->bl->jobs) == 0)
+ ecore_main_loop_quit();
+ return ECORE_CALLBACK_RENEW;
+}
+
+static Eina_Bool
+_test_timeout_cb(void *data)
+{
+ Test_Wait *wait = data;
+
+ wait->timed_out = EINA_TRUE;
+ ecore_main_loop_quit();
+ return ECORE_CALLBACK_CANCEL;
+}
+
+/* Run the main loop until no compression job is left. */
+static void
+_test_jobs_wait(TermDBacklog *bl)
+{
+ Test_Wait wait = { bl, EINA_FALSE };
+ Ecore_Timer *poll = ecore_timer_add(0.005, _test_jobs_poll_cb, &wait);
+ Ecore_Timer *timeout = ecore_timer_add(20.0, _test_timeout_cb, &wait);
+
+ ecore_main_loop_begin();
+ assert(!wait.timed_out);
+ ecore_timer_del(poll);
+ ecore_timer_del(timeout);
+}
+
+/* Run the main loop for a while, with nobody to wait for. */
+static void
+_test_loop_run(double seconds)
+{
+ Test_Wait wait = { NULL, EINA_FALSE };
+
+ ecore_timer_add(seconds, _test_timeout_cb, &wait);
+ ecore_main_loop_begin();
+ assert(wait.timed_out);
+}
+
+#define TEST_ROW_W 8
+
+static void
+_test_pages_fill(TermDBacklog *bl, uint32_t first, uint32_t count)
+{
+ Termcell row[TEST_ROW_W];
+ uint32_t i;
+
+ for (i = first; i < first + count; i++)
+ {
+ _test_row_fill(row, TEST_ROW_W, 'a' + i % 26);
+ assert(termd_backlog_append(bl, row, TEST_ROW_W, EINA_FALSE));
+ }
+}
+
+/* Compare page_id as a client would read it with the rows
+ * _test_pages_fill() stored. */
+static void
+_test_page_check(TermDBacklog *bl, uint32_t page_id)
+{
+ const TermSrvBacklogPage *page = _test_page_map(bl, page_id);
+ const TermSrvCell *cells = _test_page_cells(page);
+ uint32_t i, j, n = atomic_load(&page->num_entries);
+ Termcell row[TEST_ROW_W];
+
+ assert(n > 0);
+ for (i = 0; i < n; i++)
+ {
+ uint32_t entry = page_id * TERMSRV_BACKLOG_PAGE_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);
+ for (j = 0; j < TEST_ROW_W; j++)
+ assert(!memcmp(&cells[page->entry_off[i] + j], &row[j],
+ sizeof(row[j])));
+ }
+ _test_page_unmap(page);
+}
+
+/* Test: the sealed pages that nobody holds are compressed on threads once
+ * idle for 5 seconds and come back byte for byte, a held page is not
+ * compressed, a page asked for meanwhile keeps its memfd, the rebuilt
+ * memfd outlives the cache and a client holding it, a page being
+ * compressed is not evicted before its job ends, and the store can be
+ * freed with jobs in flight. */
+int
+tytest_termd_backlog_compress(void)
+{
+ TermDBacklog *bl;
+ TermDBacklogPage *page;
+ const TermSrvBacklogPage *client_page;
+ double now;
+ int fd, client_fd, raw_fd;
+
+ ecore_init();
+ bl = termd_backlog_new("test", 100000);
+ assert(bl);
+ _test_pages_fill(bl, 0, 600);
+ assert(termd_backlog_count(bl) == 3);
+ now = ecore_time_get();
+
+ termd_backlog_maintain(bl, now + 1.0);
+ assert(eina_list_count(bl->jobs) == 0);
+ termd_backlog_maintain(bl, now + 6.0);
+ assert(eina_list_count(bl->jobs) == 2);
+ _test_jobs_wait(bl);
+ assert(_page_find(bl, 0)->state == PAGE_COMPRESSED);
+ assert(_page_find(bl, 1)->state == PAGE_COMPRESSED);
+ assert(_page_find(bl, 2)->state == PAGE_RAW);
+ termd_backlog_maintain(bl, now + 600.0);
+ assert(eina_list_count(bl->jobs) == 0);
+ assert(_page_find(bl, 2)->state == PAGE_RAW);
+
+ assert(_page_find(bl, 0)->export_fd < 0);
+ _test_page_check(bl, 0);
+ _test_page_check(bl, 1);
+ fd = termd_backlog_page_fd(bl, 0);
+ assert(fd == _page_find(bl, 0)->export_fd);
+ assert(termd_backlog_page_fd(bl, 0) == fd);
+ client_fd = dup(fd);
+ assert(client_fd >= 0);
+ termd_backlog_hold(bl, 0);
+ termd_backlog_maintain(bl, ecore_time_get() + 600.0);
+ assert(_page_find(bl, 0)->export_fd == fd);
+ termd_backlog_release(bl, 0);
+ termd_backlog_maintain(bl, ecore_time_get() + 30.0);
+ assert(_page_find(bl, 0)->export_fd == fd);
+ termd_backlog_maintain(bl, ecore_time_get() + 600.0);
+ assert(_page_find(bl, 0)->export_fd < 0);
+ client_page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ,
+ MAP_SHARED, client_fd, 0);
+ assert(client_page != MAP_FAILED);
+ assert(client_page->page_id == 0);
+ _test_page_unmap(client_page);
+ close(client_fd);
+ _test_page_check(bl, 0);
+ termd_backlog_free(bl);
+
+ /* a held page stays raw */
+ bl = termd_backlog_new("test", 100000);
+ assert(bl);
+ _test_pages_fill(bl, 0, 600);
+ termd_backlog_hold(bl, 0);
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ assert(eina_list_count(bl->jobs) == 1);
+ _test_jobs_wait(bl);
+ assert(_page_find(bl, 0)->state == PAGE_RAW);
+ assert(_page_find(bl, 1)->state == PAGE_COMPRESSED);
+
+ /* a page that a client takes while it is compressed stays raw */
+ termd_backlog_release(bl, 0);
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ page = _page_find(bl, 0);
+ assert(page->state == PAGE_COMPRESSING);
+ raw_fd = termd_backlog_page_fd(bl, 0);
+ assert(raw_fd == page->fd);
+ termd_backlog_hold(bl, 0);
+ _test_jobs_wait(bl);
+ page = _page_find(bl, 0);
+ assert(page->state == PAGE_RAW && page->fd == raw_fd);
+ assert(page->attempts == 0);
+ _test_page_check(bl, 0);
+ termd_backlog_free(bl);
+
+ /* the oldest page is not evicted while it is compressed */
+ bl = termd_backlog_new("test", 300);
+ assert(bl);
+ _test_pages_fill(bl, 0, 300);
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ assert(_page_find(bl, 0)->state == PAGE_COMPRESSING);
+ _test_pages_fill(bl, 300, 300);
+ assert(termd_backlog_first_id(bl) == 0);
+ _test_jobs_wait(bl);
+ assert(termd_backlog_first_id(bl) == 1);
+ assert(termd_backlog_page_fd(bl, 0) < 0);
+ termd_backlog_free(bl);
+
+ /* the end of a job finds its store gone */
+ bl = termd_backlog_new("test", 100000);
+ assert(bl);
+ _test_pages_fill(bl, 0, 600);
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ assert(eina_list_count(bl->jobs) == 2);
+ termd_backlog_free(bl);
+ _test_loop_run(0.3);
+ ecore_shutdown();
+ return 0;
+}
+static uint32_t
+_test_varying_w(uint32_t entry)
+{
+ return 20 + (entry * 7) % 50;
+}
+
+/* Fill with rows of different widths that compress well, then compare the
+ * page header and cells of page_id, as a client reads them, with what was
+ * stored. */
+static void
+_test_varying_check(TermDBacklog *bl, uint32_t page_id)
+{
+ const TermSrvBacklogPage *page = _test_page_map(bl, page_id);
+ const TermSrvCell *cells = _test_page_cells(page);
+ Termcell row[70];
+ uint32_t i, off = 0, n = atomic_load(&page->num_entries);
+
+ assert(n == TERMSRV_BACKLOG_PAGE_ENTRIES);
+ for (i = 0; i < n; i++)
+ {
+ uint32_t entry = page_id * TERMSRV_BACKLOG_PAGE_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(!memcmp(&cells[off], row, w * sizeof(Termcell)));
+ off += w;
+ }
+ _test_page_unmap(page);
+}
+
+/* Test: pages that compression cannot help are left alone for good. */
+int
+tytest_termd_backlog_compress_skip(void)
+{
+ TermDBacklog *bl;
+ Termcell row[TEST_ROW_W];
+ uint32_t i, seed = 12345;
+
+ ecore_init();
+ bl = termd_backlog_new("test", 100000);
+ assert(bl);
+ /* page 0: noise, a blob that is not smaller than 85% of the cells */
+ for (i = 0; i < 300; i++)
+ {
+ unsigned char *bytes = (unsigned char *)row;
+ size_t j;
+
+ for (j = 0; j < sizeof(row); j++)
+ {
+ seed = seed * 1664525u + 1013904223u;
+ bytes[j] = seed >> 24;
+ }
+ assert(termd_backlog_append(bl, row, TEST_ROW_W, EINA_FALSE));
+ }
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ assert(eina_list_count(bl->jobs) == 1);
+ _test_jobs_wait(bl);
+ assert(_page_find(bl, 0)->state == PAGE_RAW);
+ assert(_page_find(bl, 0)->attempts == 3);
+ termd_backlog_maintain(bl, ecore_time_get() + 600.0);
+ assert(eina_list_count(bl->jobs) == 0);
+ termd_backlog_free(bl);
+
+ /* pages too small to free memory, and pages without any cell */
+ bl = termd_backlog_new("test", 100000);
+ assert(bl);
+ for (i = 0; i < 256; i++)
+ assert(termd_backlog_append(bl, NULL, 0, EINA_FALSE));
+ _test_row_fill(row, 1, 'a');
+ for (i = 0; i < 257; i++)
+ assert(termd_backlog_append(bl, row, 1, EINA_FALSE));
+ assert(termd_backlog_count(bl) == 3);
+ assert(_page_find(bl, 0)->cells_size == 0);
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ assert(eina_list_count(bl->jobs) == 0);
+ assert(_page_find(bl, 0)->state == PAGE_RAW);
+ assert(_page_find(bl, 0)->attempts == 3);
+ assert(_page_find(bl, 1)->state == PAGE_RAW);
+ assert(_page_find(bl, 1)->attempts == 3);
+ termd_backlog_free(bl);
+ ecore_shutdown();
+ return 0;
+}
+
+/* Test: a rebuilt page has the entries it had, of any width. */
+int
+tytest_termd_backlog_compress_widths(void)
+{
+ TermDBacklog *bl;
+ Termcell row[70];
+ uint32_t i;
+
+ ecore_init();
+ bl = termd_backlog_new("test", 100000);
+ assert(bl);
+ for (i = 0; i < 600; i++)
+ {
+ uint32_t w = _test_varying_w(i);
+
+ _test_row_fill(row, w, 'a' + i % 26);
+ assert(termd_backlog_append(bl, row, w, EINA_FALSE));
+ }
+ termd_backlog_maintain(bl, ecore_time_get() + 6.0);
+ _test_jobs_wait(bl);
+ assert(_page_find(bl, 0)->state == PAGE_COMPRESSED);
+ assert(_page_find(bl, 1)->state == PAGE_COMPRESSED);
+ _test_varying_check(bl, 0);
+ _test_varying_check(bl, 1);
+ termd_backlog_free(bl);
+ ecore_shutdown();
+ return 0;
+}
#endif
diff --git a/src/bin/termd_backlog.h b/src/bin/termd_backlog.h
index eeaf9869..cf7c1c46 100644
--- a/src/bin/termd_backlog.h
+++ b/src/bin/termd_backlog.h
@@ -25,8 +25,11 @@ Eina_Bool termd_backlog_append(TermDBacklog *bl, const Termcell *cells,
uint32_t termd_backlog_first_id(const TermDBacklog *bl);
uint32_t termd_backlog_count(const TermDBacklog *bl);
-/* The memfd of page_id, owned by the store: send it, do not close it.
- * Returns -1 for an unknown or evicted page. */
+/* The memfd of page_id, owned by the store: send it, do not close it, and
+ * do not use it past the next call into the store, which may close it. A
+ * compressed page is rebuilt into a new memfd, named "ty-export-", and a
+ * client that was sent a duplicate keeps it. Returns -1 for an unknown or
+ * evicted page, or if the rebuild fails. */
int termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id);
/* Whether page_id is held, whatever state it is in. */
@@ -37,6 +40,15 @@ Eina_Bool termd_backlog_page_exists(TermDBacklog *bl, uint32_t page_id);
void termd_backlog_hold(TermDBacklog *bl, uint32_t page_id);
void termd_backlog_release(TermDBacklog *bl, 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
+ * a client asks for while it is compressed stays as it is. Drops the
+ * rebuilt pages that were not used for 60 seconds. A job still running
+ * when the store is freed is left to finish by itself. The main loop must
+ * run for the jobs to complete. */
+void termd_backlog_maintain(TermDBacklog *bl, double now);
+
/* Write a TSRV_MSG_BACKLOG_MAP payload into buf for the pages in
* [first, first + count): the TermSrvMsgBacklogMapReq echo of the range
* covered, clipped to the pages that exist and to what fits in bufsz, then
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
index 3714a823..3257eaca 100644
--- a/src/bin/termd_tymux.c
+++ b/src/bin/termd_tymux.c
@@ -17,6 +17,8 @@
_Static_assert(sizeof(Termcell) == sizeof(TermSrvCell),
"TermSrvCell must mirror Termcell");
+#define BACKLOG_MAINTAIN_SECONDS 1.0
+
typedef enum
{
CLIENT_PENDING, /* connected, ATTACH not received yet */
@@ -68,6 +70,7 @@ struct tag_TermDTymux
/* clients paused by a hijack, most recent first */
Eina_List *paused;
Ecore_Animator *notify_anim;
+ Ecore_Timer *backlog_timer;
Eina_Bool dirty : 1;
Eina_Bool backlog_append_failed : 1;
};
@@ -705,6 +708,15 @@ termd_tymux_client_add(TermDTymux *td, int cfd)
td->clients = eina_list_append(td->clients, client);
}
+static Eina_Bool
+_cb_backlog_timer(void *data)
+{
+ TermDTymux *td = data;
+
+ termd_backlog_maintain(td->backlog, ecore_time_get());
+ return ECORE_CALLBACK_RENEW;
+}
+
TermDTymux *
termd_tymux_new(const char *name, const char *cmd)
{
@@ -725,6 +737,10 @@ termd_tymux_new(const char *name, const char *cmd)
td->backlog = termd_backlog_new(name, TERMD_TYMUX_SCROLLBACK);
if (!td->backlog)
goto err;
+ td->backlog_timer = ecore_timer_add(BACKLOG_MAINTAIN_SECONDS,
+ _cb_backlog_timer, td);
+ if (!td->backlog_timer)
+ goto err;
snprintf(memfd_name, sizeof(memfd_name), "tymux-%s", name);
td->shm_fd = memfd_create(memfd_name, MFD_CLOEXEC);
@@ -779,6 +795,8 @@ termd_tymux_free(TermDTymux *td)
return;
if (td->notify_anim)
ecore_animator_del(td->notify_anim);
+ if (td->backlog_timer)
+ ecore_timer_del(td->backlog_timer);
EINA_LIST_FREE(td->clients, client)
{
struct timeval timeout = { .tv_sec = 0, .tv_usec = 200 * 1000 };
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 47a50fc2..54337ca2 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -182,6 +182,9 @@ static struct {
{ "termd_backlog_pages", tytest_termd_backlog_pages},
{ "termd_backlog_evict", tytest_termd_backlog_evict},
{ "termd_backlog_map", tytest_termd_backlog_map},
+ { "termd_backlog_compress", tytest_termd_backlog_compress},
+ { "termd_backlog_compress_skip", tytest_termd_backlog_compress_skip},
+ { "termd_backlog_compress_widths", tytest_termd_backlog_compress_widths},
{ "backlog_compress", tytest_backlog_compress},
{ "termtymux_frame_apply_same_size", tytest_termtymux_frame_apply_same_size},
{ "termtymux_frame_apply_resize", tytest_termtymux_frame_apply_resize},
@@ -219,6 +222,7 @@ static const struct {
{ "tymux_live_pages", tytest_tymux_live_pages},
{ "tymux_live_pages_busy", tytest_tymux_live_pages_busy},
{ "tymux_live_window", tytest_tymux_live_window},
+ { "tymux_live_compress", tytest_tymux_live_compress},
#endif
{ NULL, NULL},
};
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index d3e52ea0..43922a06 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -375,6 +375,8 @@ tytest_tymux_daemon_memfds(TermTymux *ts, const char *prefix)
#define SESSION "ty-live"
#define SEQ_CHECKED 2990
+/* lines long enough for a page to be worth compressing */
+#define COMPRESS_PAD "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
#define REATTACH_RUN_MIN 1900
#define WIDE_MIN 5000
#define SCREEN_ROWS 10
@@ -409,20 +411,20 @@ _backlog_find(Termpty *ty, const char *text)
* the oldest, that read last_value, last_value - 1, ...; 0 if there is no
* entry reading last_value. */
static int
-_backlog_run(Termpty *ty, int last_value)
+_backlog_run_padded(Termpty *ty, int last_value, const char *pad)
{
- char last_text[16];
+ char last_text[256];
int y, run = 0, value = last_value, n = _backlog_entries(ty);
- snprintf(last_text, sizeof(last_text), "%d", last_value);
+ snprintf(last_text, sizeof(last_text), "%d%s", last_value, pad);
y = _backlog_find(ty, last_text);
if (y == 0) return 0;
for (; y <= n && value >= 1; y++, value--)
{
- char expected[16], *line = tytest_tymux_backlog_line(ty, y);
+ char expected[256], *line = tytest_tymux_backlog_line(ty, y);
Eina_Bool match;
- snprintf(expected, sizeof(expected), "%d", value);
+ snprintf(expected, sizeof(expected), "%d%s", value, pad);
match = line && !strcmp(line, expected);
free(line);
if (!match) break;
@@ -431,6 +433,12 @@ _backlog_run(Termpty *ty, int last_value)
return run;
}
+static int
+_backlog_run(Termpty *ty, int last_value)
+{
+ return _backlog_run_padded(ty, last_value, "");
+}
+
static int
_backlog_wide_find(Termpty *ty)
{
@@ -1072,3 +1080,58 @@ tytest_tymux_live_window(void)
{
return tytest_tymux_live_run(_live_window_body);
}
+
+typedef struct
+{
+ TermTymux *ts;
+} Compress_Wait;
+
+/* Only the head and the pages the client still maps are left raw in the
+ * daemon, the others were compressed. */
+static Eina_Bool
+_compress_memfds_done(void *data)
+{
+ Compress_Wait *wait = data;
+
+ return tytest_tymux_daemon_memfds(wait->ts, "ty-backlog-") ==
+ _client_pages_mapped(wait->ts);
+}
+
+/* Pages that the client let go of are compressed by the daemon, and
+ * scrolling back to them rebuilds them. */
+static int
+_live_compress_body(void)
+{
+ TermTymux *ts;
+ Termpty *ty;
+ Compress_Wait wait;
+
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+ termtymux_viewport_set(ts, 0);
+ termtymux_resize(ts, 80, SCREEN_ROWS);
+ assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+ assert(tytest_tymux_wait(_prompt_done, ty, 5.0));
+ assert(tytest_tymux_type(ts, ty, "seq -f '%g" COMPRESS_PAD "' 1 4000; echo " WINDOW_MARKER
+ "; seq 1 30\n"));
+ assert(tytest_tymux_wait(_window_marker_done, ty, 30.0));
+ assert(ts->pages_count > 3);
+ termtymux_viewport_set(ts, 0);
+ assert(tytest_tymux_wait(_window_bottom_done, ts, 10.0));
+
+ wait.ts = ts;
+ assert(tytest_tymux_wait(_compress_memfds_done, &wait, 30.0));
+ assert(_client_pages_mapped(ts) < (int)ts->pages_count);
+
+ termtymux_viewport_set(ts, termpty_backlog_length(ty));
+ assert(_backlog_run_padded(ty, SEQ_CHECKED, COMPRESS_PAD) == SEQ_CHECKED);
+ assert(tytest_tymux_daemon_memfds(ts, "ty-export-") > 0);
+
+ tytest_tymux_stop(ts);
+ return 0;
+}
+
+int
+tytest_tymux_live_compress(void)
+{
+ return tytest_tymux_live_run(_live_compress_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index b3ebe041..8f938892 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -48,5 +48,6 @@ int tytest_tymux_live_backlog(void);
int tytest_tymux_live_pages(void);
int tytest_tymux_live_pages_busy(void);
int tytest_tymux_live_window(void);
+int tytest_tymux_live_compress(void);
#endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 67797f2d..f72b9814 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -137,6 +137,9 @@ int tytest_termsrv_backlog_map(void);
int tytest_termd_backlog_pages(void);
int tytest_termd_backlog_evict(void);
int tytest_termd_backlog_map(void);
+int tytest_termd_backlog_compress(void);
+int tytest_termd_backlog_compress_skip(void);
+int tytest_termd_backlog_compress_widths(void);
int tytest_backlog_compress(void);
int tytest_termtymux_frame_apply_same_size(void);
int tytest_termtymux_frame_apply_resize(void);
diff --git a/tests/meson.build b/tests/meson.build
index c868191a..e4dbda62 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -101,7 +101,8 @@ if tests
test('tymux-live',
tytest,
args: ['tymux_live_backlog', 'tymux_live_pages',
- 'tymux_live_pages_busy', 'tymux_live_window'],
+ 'tymux_live_pages_busy', 'tymux_live_window',
+ 'tymux_live_compress'],
depends: [tymuxd],
timeout: 120)
endif
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.