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 21bafbbe11a6c0ff65e0e5d4ae7cd6c697319ad5
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 01:57:02 2026 -0600
backlog_store: free descriptors early when tymuxd holds too many
Every raw page of a tymuxd store holds a memfd. With a large
scrollback, or many sessions in one daemon, those can run the process
out of descriptors long before the 5 s idle grace lets compression
close them.
tymuxd now gives its MEMFD stores a shared budget of a quarter of the
RLIMIT_NOFILE soft limit, counting the rebuilt pages it hands to
clients as well. Above it, the store first drops the rebuilt pages no
client holds, which only costs a rebuild later. Then it compresses the
sealed pages nobody holds right away, least recently used first across
stores, through the usual jobs. A page that compresses poorly is
copied as it is into ordinary memory so its memfd closes anyway; it is
rebuilt with a memcpy, and in-process stores, which hold no fd, never
do this. A page whose copy keeps failing is given up on after three
tries, as compression is.
Each job end starts the next one while the budget is exceeded. The
opening of a new page also checks the budget, because a flood fills
pages much faster than the 1 s maintain runs. That is one comparison
per page, not per row. In tymux-live, a daemon limited to 64
descriptors is within its 16 memfds a few milliseconds after a
100000-line flood, where the idle path alone takes about 10 s.
tybench, each corpus in its own process, taskset -c 2 perf stat
cycles:u, -i 10 -w 1, median of 3 interleaved runs, against the parent
commit. Mcycles and change, default build and aligned (built with
-falign-functions=64 -falign-loops=64 -falign-jumps=32), "al.":
corpus @50 default @50 aligned @2000 default @2000 al.
fill 2097 +0.7% 2117 +1.0% 2098 +0.2% 2108 +0.3%
hyperlink 3367 -2.1% 3372 +0.5% 3390 -0.6% 3350 -0.7%
insert 1961 -0.8% 1942 +0.4% 1981 -1.5% 1939 +0.6%
insert-mode 3233 +0.2% 3222 -0.7% 3263 -0.6% 3283 -1.9%
long-lines 1614 +0.9% 1614 -0.7% 1642 +0.5% 1648 -0.8%
plain-ascii 1658 +0.5% 1662 +0.9% 1694 -0.7% 1709 +0.0%
rep 4588 +0.5% 4604 -0.7% 4629 -0.7% 4625 -1.1%
scroll 2857 +0.6% 2880 +1.2% 2900 +0.7% 2918 +1.3%
scroll-region 1444 -0.2% 1457 -1.3% 1446 +0.3% 1446 -0.5%
sgr 1869 +0.2% 1894 -1.0% 1901 -0.7% 1906 -0.7%
unicode 1975 -0.3% 1959 +0.2% 1980 +0.4% 1974 +0.2%
strings (-c 3) 2290 +0.5% 2162 -0.4%
Instructions change by +0.26% at most (scroll), and only on the
corpora that store rows.
tybenchd -i 5 1000000, release build, median of 5, three runs:
before: 0.345 s, 0.338 s, 0.339 s
after: 0.340 s, 0.336 s, 0.343 s
---
src/bin/backlog_store.c | 519 ++++++++++++++++++++++++++++++++++++++++++++----
src/bin/backlog_store.h | 27 ++-
src/bin/tymuxd.c | 19 ++
src/bin/tytest.c | 2 +
src/bin/tytest_tymux.c | 82 ++++++++
src/bin/tytest_tymux.h | 1 +
src/bin/unit_tests.h | 1 +
tests/meson.build | 2 +-
8 files changed, 606 insertions(+), 47 deletions(-)
diff --git a/src/bin/backlog_store.c b/src/bin/backlog_store.c
index 9f4aeb79..fff41857 100644
--- a/src/bin/backlog_store.c
+++ b/src/bin/backlog_store.c
@@ -81,11 +81,18 @@ typedef struct
/* 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;
+ /* the failed tries at compressing the page, or once compress_poor at
+ * copying it */
uint32_t attempts;
size_t cells_size; /* the bytes of cells in use */
uint32_t header_size; /* the store's, kept for _page_close() */
size_t map_size;
Eina_Binbuf *blob;
+ /* blob holds the cells as they are: compression did not help, but the
+ * memfd had to go */
+ Eina_Bool stored_raw;
+ /* compressing the page would not free enough to be worth it */
+ Eina_Bool compress_poor;
TermSrvBacklogPage *export_map;
size_t export_size;
int export_fd;
@@ -109,7 +116,12 @@ typedef struct
void *orphan_map;
size_t orphan_size;
size_t cells_size;
+ /* started to free a memfd: what compression does not help is copied */
+ Eina_Bool pressure;
+ Eina_Bool copy_only;
+ Eina_Bool poor; /* compression was not worth it */
Eina_Binbuf *blob;
+ Eina_Binbuf *raw; /* the copy of the cells, if poor under pressure */
/* 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 */
@@ -165,6 +177,18 @@ struct tag_BacklogStore
/* Compression jobs not ended yet, of every store */
static unsigned int _jobs_alive = 0;
+/* The MEMFD stores of the process share a budget of memfds */
+static Eina_List *_memfd_stores = NULL;
+static uint32_t _memfds = 0;
+static uint32_t _memfd_budget = UINT32_MAX;
+
+static Eina_Bool
+_fds_over_budget(void)
+{
+ return _memfds > _memfd_budget;
+}
+
+static void _fds_relieve(void);
void
backlog_store_geometry_get(uint32_t scrollback, size_t sys_page_size,
@@ -229,15 +253,24 @@ _page_find_held(BacklogStore *bs, uint32_t page_id)
return &bs->pages[idx];
}
+/* The fds of pages are all memfds that _page_memfd_map() opened. */
+static void
+_page_fd_close(int *fd)
+{
+ if (*fd < 0)
+ return;
+ close(*fd);
+ *fd = -1;
+ _memfds--;
+}
+
static void
_page_export_drop(BacklogStorePage *page)
{
if (page->export_map)
munmap(page->export_map, page->export_size);
page->export_map = NULL;
- if (page->export_fd >= 0)
- close(page->export_fd);
- page->export_fd = -1;
+ _page_fd_close(&page->export_fd);
}
/* A page whose job outlives the store has no header left: the job owns
@@ -257,8 +290,7 @@ _page_close(BacklogStorePage *page)
}
if (page->header)
munmap(page->header, page->map_size);
- if (page->fd >= 0)
- close(page->fd);
+ _page_fd_close(&page->fd);
}
#if defined(__linux__)
@@ -292,6 +324,7 @@ _page_memfd_map(const BacklogStore *bs, const char *kind, uint32_t page_id,
return NULL;
}
*fd_out = fd;
+ _memfds++;
return map;
}
#else
@@ -365,10 +398,8 @@ _page_compressed_set(BacklogStorePage *page, Eina_Binbuf *blob)
return EINA_FALSE;
memcpy(copy, page->header, page->header_size);
munmap(page->header, page->map_size);
- if (page->fd >= 0)
- close(page->fd);
+ _page_fd_close(&page->fd);
page->header = copy;
- page->fd = -1;
page->blob = blob;
page->state = PAGE_COMPRESSED;
return EINA_TRUE;
@@ -646,12 +677,8 @@ _page_detach(BacklogStore *bs, const BacklogStorePage *page)
*detached = *page;
memset(&detached->links, 0, sizeof(detached->links));
memset(&detached->blocks, 0, sizeof(detached->blocks));
- if (detached->fd >= 0)
- close(detached->fd);
- detached->fd = -1;
- if (detached->export_fd >= 0)
- close(detached->export_fd);
- detached->export_fd = -1;
+ _page_fd_close(&detached->fd);
+ _page_fd_close(&detached->export_fd);
bs->detached = eina_list_append(bs->detached, detached);
return EINA_TRUE;
}
@@ -726,6 +753,8 @@ backlog_store_new(const char *name, BacklogStoreBacking backing,
sys_page_size = sysconf(_SC_PAGESIZE);
backlog_store_geometry_get(scrollback, sys_page_size > 0 ? sys_page_size :
4096, &bs->geo);
+ if (backing == BACKLOG_STORE_MEMFD)
+ _memfd_stores = eina_list_append(_memfd_stores, bs);
return bs;
}
@@ -739,6 +768,8 @@ backlog_store_free(BacklogStore *bs)
if (!bs)
return;
+ if (bs->backing == BACKLOG_STORE_MEMFD)
+ _memfd_stores = eina_list_remove(_memfd_stores, bs);
/* a page is not evicted while being compressed, so each job finds its
* own */
EINA_LIST_FREE(bs->jobs, job)
@@ -855,6 +886,13 @@ backlog_store_append(BacklogStore *bs, const Termcell *cells, ssize_t w,
(bs->total_entries - bs->pages[0].entries >= bs->geo.max_entries))
_evict(bs);
}
+ /* a flood fills pages faster than maintain runs */
+ if (added && _fds_over_budget())
+ {
+ _fds_relieve();
+ /* a job end that it ran may have evicted pages */
+ head_page = &bs->pages[bs->count - 1];
+ }
if (saved)
{
saved->w = head_page->widths[n - 1];
@@ -1030,14 +1068,16 @@ _page_export(const BacklogStore *bs, BacklogStorePage *page)
if (!map)
return EINA_FALSE;
memcpy(map, page->header, page->header_size);
- if (!backlog_uncompress(page->blob, map + page->header_size,
- page->cells_size))
+ if (page->stored_raw)
+ memcpy(map + page->header_size, eina_binbuf_string_get(page->blob),
+ page->cells_size);
+ else if (!backlog_uncompress(page->blob, map + page->header_size,
+ page->cells_size))
{
ERR("cannot rebuild page %u of '%s'", page->id,
bs->name);
munmap(map, size);
- if (fd >= 0)
- close(fd);
+ _page_fd_close(&fd);
return EINA_FALSE;
}
page->export_map = (TermSrvBacklogPage *)map;
@@ -1258,6 +1298,34 @@ backlog_store_release(BacklogStore *bs, uint32_t page_id)
}
}
+static Eina_Bool
+_page_worth_compressing(size_t cells_size, const Eina_Binbuf *blob)
+{
+ size_t len = eina_binbuf_length_get(blob);
+
+ return (len <= COMPRESS_RATIO_MAX * cells_size) &&
+ (_page_gain(cells_size, len) >= (ssize_t)COMPRESS_GAIN_MIN);
+}
+
+/* Safe to call from any thread. */
+static Eina_Binbuf *
+_cells_copy(const TermSrvCell *cells, size_t size)
+{
+ unsigned char *copy;
+ Eina_Binbuf *raw;
+
+ if (size == 0)
+ return eina_binbuf_new();
+ copy = malloc(size);
+ if (!copy)
+ return NULL;
+ memcpy(copy, cells, size);
+ raw = eina_binbuf_manage_new(copy, size, EINA_FALSE);
+ if (!raw)
+ free(copy);
+ return raw;
+}
+
static void
_job_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
{
@@ -1266,7 +1334,16 @@ _job_worker(void *data, Ecore_Thread *thread EINA_UNUSED)
if (!atomic_compare_exchange_strong(&job->claim, &queued, JOB_RUNNING))
return;
- job->blob = backlog_compress(job->cells, job->cells_size);
+ if (job->copy_only)
+ job->poor = EINA_TRUE;
+ else
+ {
+ job->blob = backlog_compress(job->cells, job->cells_size);
+ job->poor = job->blob &&
+ !_page_worth_compressing(job->cells_size, job->blob);
+ }
+ if (job->poor && job->pressure)
+ job->raw = _cells_copy(job->cells, job->cells_size);
eina_semaphore_release(&job->done, 1);
}
@@ -1279,6 +1356,8 @@ _job_free(BacklogStoreJob *job)
munmap(job->orphan_map, job->orphan_size);
if (job->blob)
eina_binbuf_free(job->blob);
+ if (job->raw)
+ eina_binbuf_free(job->raw);
free(job);
}
@@ -1295,18 +1374,28 @@ _job_end(void *data, Ecore_Thread *thread EINA_UNUSED)
bs->jobs = eina_list_remove(bs->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)
+ if (job->poor)
+ {
+ if (!page->compress_poor)
+ {
+ page->compress_poor = EINA_TRUE;
+ page->attempts = 0;
+ }
+ if (job->pressure && (page->refs == 0) &&
+ _fds_over_budget())
+ {
+ if (job->raw && _page_compressed_set(page, job->raw))
+ {
+ job->raw = NULL;
+ page->stored_raw = EINA_TRUE;
+ }
+ else
+ page->attempts++;
+ }
+ }
+ else if (!job->blob)
page->attempts++;
- else if (!worthwhile)
- page->attempts = COMPRESS_ATTEMPTS_MAX;
/* a client got the page meanwhile: it needs the mapping */
else if (page->refs == 0)
{
@@ -1319,6 +1408,7 @@ _job_end(void *data, Ecore_Thread *thread EINA_UNUSED)
_evict(bs);
}
_job_free(job);
+ _fds_relieve();
}
/* Also called by ecore_thread_run() itself when it cannot start the job,
@@ -1343,25 +1433,33 @@ _job_cancel(void *data, Ecore_Thread *thread EINA_UNUSED)
_job_free(job);
}
-static void
-_job_start(BacklogStore *bs, BacklogStorePage *page)
+/* Start compressing page, or under pressure copying it if compression is
+ * known not to help. Returns whether the job was started. */
+static Eina_Bool
+_job_start(BacklogStore *bs, BacklogStorePage *page, Eina_Bool pressure)
{
BacklogStoreJob *job;
+ uint32_t job_page_id = page->id;
/* even a blob of nothing could not free enough */
- if (_page_gain(page->cells_size, 0) < (ssize_t)COMPRESS_GAIN_MIN)
+ if (!page->compress_poor &&
+ (_page_gain(page->cells_size, 0) < (ssize_t)COMPRESS_GAIN_MIN))
{
- page->attempts = COMPRESS_ATTEMPTS_MAX;
- return;
+ page->compress_poor = EINA_TRUE;
+ page->attempts = 0;
}
+ if (page->compress_poor && !pressure)
+ return EINA_FALSE;
job = calloc(1, sizeof(*job));
if (!job || !eina_semaphore_new(&job->done, 0))
{
free(job);
page->attempts++;
- return;
+ return EINA_FALSE;
}
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->page_id = page->id;
@@ -1370,6 +1468,10 @@ _job_start(BacklogStore *bs, BacklogStorePage *page)
bs->jobs = eina_list_append(bs->jobs, job);
_jobs_alive++;
ecore_thread_run(_job_worker, _job_end, _job_cancel, job);
+ /* when the job cannot run, the cancel callback was called already, and
+ * the end of another job may have evicted pages, not this one */
+ page = _page_find(bs, job_page_id);
+ return page && (page->state == PAGE_COMPRESSING);
}
/* Take a COMPRESSING page back from its job, RAW. If the worker already
@@ -1427,8 +1529,10 @@ _page_reopen(BacklogStore *bs, BacklogStorePage *page)
page->export_map = NULL;
page->export_fd = -1;
page->state = PAGE_RAW;
+ page->stored_raw = EINA_FALSE;
}
page->attempts = 0;
+ page->compress_poor = EINA_FALSE;
return EINA_TRUE;
}
@@ -1495,6 +1599,87 @@ 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. */
+static BacklogStorePage *
+_fds_victim_find(BacklogStore **bs_out, uint32_t *jobs)
+{
+ BacklogStorePage *victim = NULL;
+ BacklogStore *bs;
+ const Eina_List *l;
+ uint32_t i;
+
+ *jobs = 0;
+ EINA_LIST_FOREACH(_memfd_stores, l, bs)
+ {
+ *jobs += eina_list_count(bs->jobs);
+ if (eina_list_count(bs->jobs) >= COMPRESS_JOBS_MAX)
+ continue;
+ for (i = 0; i + 1 < bs->count; i++)
+ {
+ BacklogStorePage *page = &bs->pages[i];
+
+ if ((page->state != PAGE_RAW) || (page->refs > 0) ||
+ (page->attempts >= COMPRESS_ATTEMPTS_MAX))
+ continue;
+ if (!victim || (page->idle_since < victim->idle_since))
+ {
+ victim = page;
+ *bs_out = bs;
+ }
+ }
+ }
+ return victim;
+}
+
+/* While the MEMFD stores hold more memfds than the budget, drop the rebuilt
+ * pages nobody holds, which only costs a rebuild, then start jobs on hidden
+ * pages without their idle grace. A job frees the memfd of its page once it
+ * ends, which starts the next one. */
+static void
+_fds_relieve(void)
+{
+ static Eina_Bool relieving = EINA_FALSE;
+ BacklogStorePage *victim;
+ BacklogStore *bs;
+ const Eina_List *l;
+ uint32_t i, jobs;
+
+ /* a job end can run within ecore_thread_run() */
+ if (!_fds_over_budget() || relieving)
+ return;
+ relieving = EINA_TRUE;
+ EINA_LIST_FOREACH(_memfd_stores, l, bs)
+ {
+ for (i = 0; (i < bs->count) && _fds_over_budget(); i++)
+ {
+ if (bs->pages[i].refs == 0)
+ _page_export_drop(&bs->pages[i]);
+ }
+ }
+ for (;;)
+ {
+ victim = _fds_victim_find(&bs, &jobs);
+ if (!victim || ((uint64_t)_memfds <= (uint64_t)_memfd_budget + jobs) ||
+ !_job_start(bs, victim, EINA_TRUE))
+ break;
+ }
+ relieving = EINA_FALSE;
+}
+
+void
+backlog_store_fd_budget_set(uint32_t max_raw_fds)
+{
+ _memfd_budget = max_raw_fds;
+}
+
+uint32_t
+backlog_store_raw_fds_get(void)
+{
+ return _memfds;
+}
+
Eina_Bool
backlog_store_maintain(BacklogStore *bs, double now)
{
@@ -1502,6 +1687,7 @@ backlog_store_maintain(BacklogStore *bs, double now)
uint32_t i;
EINA_SAFETY_ON_NULL_RETURN_VAL(bs, EINA_FALSE);
+ _fds_relieve();
for (i = 0; i < bs->count; i++)
{
BacklogStorePage *page = &bs->pages[i];
@@ -1519,11 +1705,11 @@ backlog_store_maintain(BacklogStore *bs, double now)
BacklogStorePage *page = &bs->pages[i];
if (page->state != PAGE_RAW || page->refs > 0 ||
- page->attempts >= COMPRESS_ATTEMPTS_MAX)
+ page->compress_poor || page->attempts >= COMPRESS_ATTEMPTS_MAX)
continue;
if ((eina_list_count(bs->jobs) < COMPRESS_JOBS_MAX) &&
(now - page->idle_since >= COMPRESS_IDLE_SECONDS))
- _job_start(bs, page);
+ _job_start(bs, page, EINA_FALSE);
else
pending = EINA_TRUE;
}
@@ -1645,12 +1831,15 @@ _test_store_new(BacklogStoreBacking backing, uint32_t max_entries)
typedef void (*Test_Backing_Cb)(BacklogStoreBacking backing);
+/* Each test frees its stores: no memfd is left counted. */
static void
_test_backings_run(Test_Backing_Cb cb)
{
cb(BACKLOG_STORE_ANON);
+ assert(backlog_store_raw_fds_get() == 0);
#if defined(__linux__)
cb(BACKLOG_STORE_MEMFD);
+ assert(backlog_store_raw_fds_get() == 0);
#endif
}
@@ -2225,7 +2414,7 @@ _test_compress_skip(BacklogStoreBacking backing)
assert(eina_list_count(bs->jobs) == 1);
_test_jobs_wait(bs);
assert(_page_find(bs, 0)->state == PAGE_RAW);
- assert(_page_find(bs, 0)->attempts == 3);
+ assert(_page_find(bs, 0)->compress_poor);
backlog_store_maintain(bs, ecore_time_get() + 600.0);
assert(eina_list_count(bs->jobs) == 0);
backlog_store_free(bs);
@@ -2243,9 +2432,9 @@ _test_compress_skip(BacklogStoreBacking backing)
backlog_store_maintain(bs, ecore_time_get() + 6.0);
assert(eina_list_count(bs->jobs) == 0);
assert(_page_find(bs, 0)->state == PAGE_RAW);
- assert(_page_find(bs, 0)->attempts == 3);
+ assert(_page_find(bs, 0)->compress_poor);
assert(_page_find(bs, 1)->state == PAGE_RAW);
- assert(_page_find(bs, 1)->attempts == 3);
+ assert(_page_find(bs, 1)->compress_poor);
backlog_store_free(bs);
ecore_shutdown();
}
@@ -3455,6 +3644,256 @@ tytest_backlog_store_blocks(void)
return 0;
}
+#if defined(__linux__)
+/* Noise that compression cannot shrink, the same for the same seed */
+static void
+_test_noise_row(Termcell *row, uint32_t seed)
+{
+ unsigned char *bytes = (unsigned char *)row;
+ size_t i;
+
+ for (i = 0; i < TEST_ROW_W * sizeof(Termcell); i++)
+ {
+ seed = seed * 1664525u + 1013904223u;
+ bytes[i] = seed >> 24;
+ }
+}
+
+/* Noise on the even pages of a store, letters on the odd ones */
+static void
+_test_budget_row(Termcell *row, uint32_t store, uint32_t entry)
+{
+ if ((entry / _TEST_ENTRIES) % 2 == 0)
+ _test_noise_row(row, (store << 20) | entry);
+ else
+ _test_row_fill(row, TEST_ROW_W, 'a' + entry % 26);
+}
+
+/* Seal pages pages, and leave a head of one entry. */
+static void
+_test_budget_fill(BacklogStore *bs, uint32_t store, uint32_t pages)
+{
+ Termcell row[TEST_ROW_W];
+ uint32_t i;
+
+ for (i = 0; i <= pages * _TEST_ENTRIES; i++)
+ {
+ _test_budget_row(row, store, i);
+ assert(backlog_store_append(bs, row, TEST_ROW_W, EINA_FALSE,
+ EINA_FALSE, NULL));
+ }
+ assert(backlog_store_count(bs) == pages + 1);
+}
+
+/* Compare page_id, as a client maps it, with what _test_budget_fill()
+ * stored. */
+static void
+_test_budget_check(BacklogStore *bs, uint32_t store, uint32_t page_id)
+{
+ const TermSrvBacklogPage *page = _test_page_map(bs, page_id);
+ const TermSrvCell *cells = _test_page_cells(page);
+ uint32_t i, n = atomic_load(&page->num_entries);
+ Termcell row[TEST_ROW_W];
+
+ assert(n > 0);
+ for (i = 0; i < n; i++)
+ {
+ _test_budget_row(row, store, page_id * _TEST_ENTRIES + i);
+ assert(_test_off(page)[i] == i * TEST_ROW_W);
+ assert(atomic_load(&_test_w(page)[i]) == TEST_ROW_W);
+ assert(!memcmp(&cells[_test_off(page)[i]], row, sizeof(row)));
+ }
+ _test_page_unmap(bs, page);
+}
+#endif
+
+/* Test: while the MEMFD stores of the process hold more memfds than their
+ * budget, the pages nobody holds are compressed at once, the oldest first,
+ * across stores, and those that do not shrink are copied as they are, so
+ * that their memfd closes too. They come back byte for byte, and as a head
+ * again. A held page keeps its memfd, the rebuilt pages nobody holds go
+ * first, a new page starts the work, and an ANON store holds no memfd. */
+int
+tytest_backlog_store_fd_budget(void)
+{
+#if defined(__linux__)
+ const TermSrvBacklogPage *page;
+ BacklogStore *bs[2], *anon;
+ BacklogStorePage *p;
+ Termcell row[TEST_ROW_W];
+ unsigned int fds;
+ uint32_t i, j;
+
+ ecore_init();
+ assert(backlog_store_raw_fds_get() == 0);
+ fds = _test_fd_count();
+ for (i = 0; i < 2; i++)
+ {
+ bs[i] = backlog_store_new("test", BACKLOG_STORE_MEMFD,
+ _TEST_SCROLLBACK);
+ assert(bs[i]);
+ _test_budget_fill(bs[i], i, 6);
+ }
+ assert(backlog_store_raw_fds_get() == 2 * 7);
+ assert(_test_fd_count() == fds + 2 * 7);
+
+ backlog_store_hold(bs[0], 1);
+ backlog_store_fd_budget_set(4);
+ backlog_store_maintain(bs[0], ecore_time_get() + 0.1);
+ assert(eina_list_count(bs[0]->jobs) == COMPRESS_JOBS_MAX);
+ assert(eina_list_count(bs[1]->jobs) == COMPRESS_JOBS_MAX);
+ _test_jobs_wait(NULL);
+ assert(backlog_store_raw_fds_get() <= 4 + 2);
+ assert(_test_fd_count() == fds + backlog_store_raw_fds_get());
+ p = _page_find(bs[0], 1);
+ assert(p->state == PAGE_RAW && p->fd >= 0);
+ for (i = 0; i < 2; i++)
+ for (j = 0; j < 6; j += 2)
+ {
+ p = _page_find(bs[i], j);
+ assert(p->state == PAGE_COMPRESSED && p->stored_raw);
+ assert(p->fd < 0);
+ assert(_page_memory(p, p->cells_size) ==
+ p->header_size + p->cells_size);
+ }
+ /* only the newest of a store can be left */
+ for (i = 0; i < 2; i++)
+ for (j = 1; j < 6; j += 2)
+ {
+ p = _page_find(bs[i], j);
+ assert(!p->stored_raw);
+ if ((j < 5) && ((i != 0) || (j != 1)))
+ assert(p->state == PAGE_COMPRESSED);
+ }
+
+ /* rebuilt for clients, and dropped unless held */
+ backlog_store_hold(bs[1], 0);
+ for (i = 0; i < 2; i++)
+ for (j = 0; j < 7; j++)
+ _test_budget_check(bs[i], i, j);
+ assert(backlog_store_raw_fds_get() > 4 + 2);
+ backlog_store_maintain(bs[0], ecore_time_get());
+ for (i = 0; i < 2; i++)
+ for (j = 0; j < 6; j++)
+ assert((_page_find(bs[i], j)->export_fd >= 0) == (i == 1 && j == 0));
+ _test_jobs_wait(NULL);
+ assert(backlog_store_raw_fds_get() <= 4 + 2);
+ assert(_test_fd_count() == fds + backlog_store_raw_fds_get());
+ backlog_store_release(bs[0], 1);
+ backlog_store_release(bs[1], 0);
+ for (i = 0; i < 2; i++)
+ backlog_store_free(bs[i]);
+ assert(backlog_store_raw_fds_get() == 0);
+
+ /* a page stored raw becomes the head again */
+ backlog_store_fd_budget_set(0);
+ bs[0] = backlog_store_new("test", BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ assert(bs[0]);
+ _test_budget_fill(bs[0], 0, 1);
+ backlog_store_maintain(bs[0], ecore_time_get());
+ _test_jobs_wait(NULL);
+ assert(_page_find(bs[0], 0)->stored_raw);
+ assert(backlog_store_pop(bs[0]));
+ assert(backlog_store_pop(bs[0]));
+ assert(backlog_store_count(bs[0]) == 1);
+ p = _page_find(bs[0], 0);
+ assert(p->state == PAGE_RAW && !p->stored_raw && !p->blob);
+ assert(p->fd >= 0);
+ _test_budget_row(row, 0, _TEST_ENTRIES - 1);
+ assert(backlog_store_append(bs[0], row, TEST_ROW_W, EINA_FALSE,
+ EINA_FALSE, NULL));
+ page = _test_page_map(bs[0], 0);
+ assert(atomic_load(&page->num_entries) == _TEST_ENTRIES);
+ _test_page_unmap(bs[0], page);
+ _test_budget_check(bs[0], 0, 0);
+ backlog_store_free(bs[0]);
+
+ /* a page that a client takes while it is copied keeps its memfd, and
+ * once released is only copied */
+ bs[0] = backlog_store_new("test", BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ assert(bs[0]);
+ _test_budget_fill(bs[0], 0, 1);
+ assert(_page_find(bs[0], 0)->state == PAGE_COMPRESSING);
+ backlog_store_hold(bs[0], 0);
+ _test_jobs_wait(NULL);
+ p = _page_find(bs[0], 0);
+ assert(p->state == PAGE_RAW && p->fd >= 0);
+ assert(p->compress_poor && !p->stored_raw);
+ backlog_store_release(bs[0], 0);
+ backlog_store_maintain(bs[0], ecore_time_get());
+ assert(((BacklogStoreJob *)eina_list_data_get(bs[0]->jobs))->copy_only);
+ _test_jobs_wait(NULL);
+ assert(_page_find(bs[0], 0)->stored_raw);
+ _test_budget_check(bs[0], 0, 0);
+ backlog_store_free(bs[0]);
+
+ /* a page whose copies keep failing is given up on */
+ backlog_store_fd_budget_set(UINT32_MAX);
+ bs[0] = backlog_store_new("test", BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ assert(bs[0]);
+ _test_budget_fill(bs[0], 0, 1);
+ backlog_store_maintain(bs[0], ecore_time_get() + 6.0);
+ _test_jobs_wait(NULL);
+ p = _page_find(bs[0], 0);
+ assert(p->state == PAGE_RAW);
+ assert(p->compress_poor && (p->attempts == 0));
+ p->attempts = COMPRESS_ATTEMPTS_MAX;
+ backlog_store_fd_budget_set(0);
+ backlog_store_maintain(bs[0], ecore_time_get() + 600.0);
+ assert(!bs[0]->jobs);
+ assert(_page_find(bs[0], 0)->state == PAGE_RAW);
+ backlog_store_free(bs[0]);
+
+ /* a held page evicted meanwhile lets go of its memfd, keeps its
+ * mapping, and is no longer the store's to free */
+ backlog_store_fd_budget_set(UINT32_MAX);
+ bs[0] = _test_store_new(BACKLOG_STORE_MEMFD, 300);
+ _test_pages_fill(bs[0], 0, 300);
+ backlog_store_hold(bs[0], 0);
+ page = backlog_store_page_get(bs[0], 0);
+ backlog_store_fd_budget_set(0);
+ _test_pages_fill(bs[0], 300, 300);
+ assert(eina_list_count(bs[0]->detached) == 1);
+ _test_jobs_wait(NULL);
+ assert(_test_fd_count() == fds + backlog_store_raw_fds_get());
+ assert(backlog_store_raw_fds_get() <= backlog_store_count(bs[0]));
+ _test_page_mapping_check(page, 0);
+ backlog_store_release(bs[0], 0);
+ assert(!bs[0]->detached);
+ assert(_test_fd_count() == fds + backlog_store_raw_fds_get());
+ backlog_store_free(bs[0]);
+ assert(backlog_store_raw_fds_get() == 0);
+
+ /* opening a page starts the work, without waiting for maintain */
+ bs[0] = backlog_store_new("test", BACKLOG_STORE_MEMFD, _TEST_SCROLLBACK);
+ assert(bs[0]);
+ _test_budget_fill(bs[0], 0, 2);
+ assert(eina_list_count(bs[0]->jobs) == 2);
+ _test_jobs_wait(NULL);
+ assert(_page_find(bs[0], 0)->stored_raw);
+ assert(_page_find(bs[0], 1)->state == PAGE_COMPRESSED);
+ assert(!_page_find(bs[0], 1)->stored_raw);
+ assert(backlog_store_raw_fds_get() == 1);
+ _test_budget_check(bs[0], 0, 0);
+ _test_budget_check(bs[0], 0, 1);
+ backlog_store_free(bs[0]);
+
+ /* an ANON store is left to its idle grace */
+ anon = backlog_store_new("test", BACKLOG_STORE_ANON, _TEST_SCROLLBACK);
+ assert(anon);
+ _test_budget_fill(anon, 0, 2);
+ backlog_store_maintain(anon, ecore_time_get() + 0.1);
+ assert(!anon->jobs);
+ assert(backlog_store_raw_fds_get() == 0);
+ backlog_store_free(anon);
+
+ backlog_store_fd_budget_set(UINT32_MAX);
+ assert(_test_fd_count() == fds);
+ ecore_shutdown();
+#endif
+ return 0;
+}
+
#if defined(__linux__)
/* Whether the mapping at addr is left out of the children fork() makes. */
static Eina_Bool
diff --git a/src/bin/backlog_store.h b/src/bin/backlog_store.h
index 236d1e9d..9c244e55 100644
--- a/src/bin/backlog_store.h
+++ b/src/bin/backlog_store.h
@@ -93,8 +93,8 @@ Termcell *backlog_store_newest_get(BacklogStore *bs, uint32_t *w,
void backlog_store_page_dirty(BacklogStore *bs, uint32_t page_id);
/* The bytes that the store's pages use: the cells and headers of the raw
- * pages, the blobs and header copies of the compressed ones, and the pages
- * rebuilt from them. */
+ * pages, the blobs, or uncompressed copies, and header copies of the
+ * compressed ones, and the pages rebuilt from them. */
size_t backlog_store_memory_get(const BacklogStore *bs);
/* Take the newest entry back, to wrap it again. Its cells stay mapped:
@@ -183,12 +183,27 @@ void backlog_store_release(BacklogStore *bs, uint32_t page_id);
* 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. Returns whether a later call has work left
- * to do, as long as no page is released, sealed or written to meanwhile. */
+ * 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
+ * drops the rebuilt pages that no client holds, then compresses the sealed
+ * pages of all those stores that no client holds at once, the least
+ * recently used first; one that hardly shrinks is copied as it is into
+ * ordinary memory, so that its memfd is closed all the same. 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. Returns whether a later call has
+ * work left to do, as long as no page is released, sealed or written to
+ * meanwhile. */
Eina_Bool backlog_store_maintain(BacklogStore *bs, double now);
+/* The most memfds that the MEMFD stores of the process may hold together,
+ * their raw pages and the pages they rebuilt for clients: no limit by
+ * default. Beyond it, backlog_store_maintain() and the opening of a new
+ * page free some early. Only the stores' own memfds count: not the
+ * duplicates that are still on their way to clients. */
+void backlog_store_fd_budget_set(uint32_t max_raw_fds);
+/* The memfds that the MEMFD stores of the process hold. */
+uint32_t backlog_store_raw_fds_get(void);
+
/* 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/tymuxd.c b/src/bin/tymuxd.c
index 936d74a5..387737f8 100644
--- a/src/bin/tymuxd.c
+++ b/src/bin/tymuxd.c
@@ -5,12 +5,14 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <Elementary.h>
#include <Emile.h>
#include "termpty.h"
#include "termsrv.h"
+#include "backlog_store.h"
#include "termd_tymux.h"
int _log_domain = -1;
@@ -102,6 +104,22 @@ _listen_socket_new(const struct sockaddr_un *sa, const char *name)
return fd;
}
+/* Raw scrollback pages each hold a memfd: keep three quarters of the
+ * descriptors for the clients' sockets and everything else. */
+static void
+_fd_budget_set(void)
+{
+ struct rlimit rl;
+
+ if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
+ {
+ ERR("getrlimit: %s", strerror(errno));
+ return;
+ }
+ if (rl.rlim_cur != RLIM_INFINITY)
+ backlog_store_fd_budget_set(MIN(rl.rlim_cur / 4, UINT32_MAX));
+}
+
int
main(int argc, char **argv)
{
@@ -130,6 +148,7 @@ main(int argc, char **argv)
EINA_LOG_ERR("Could not create log domain 'tymuxd'");
signal(SIGPIPE, SIG_IGN);
+ _fd_budget_set();
ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, _cb_signal_exit, NULL);
if (termsrv_ensure_tymux_dir() < 0)
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 4e43f6e8..0d870f8c 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -180,6 +180,7 @@ static struct {
{ "backlog_store_next_id", tytest_backlog_store_next_id},
{ "backlog_store_links", tytest_backlog_store_links},
{ "backlog_store_blocks", tytest_backlog_store_blocks},
+ { "backlog_store_fd_budget", tytest_backlog_store_fd_budget},
{ "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},
@@ -255,6 +256,7 @@ static const struct {
{ "tymux_live_scrollback_config",
tytest_tymux_live_scrollback_config},
{ "tymux_live_scrollback_zero", tytest_tymux_live_scrollback_zero},
+ { "tymux_live_fd_budget", tytest_tymux_live_fd_budget},
#endif
{ NULL, NULL},
};
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index 70b9d415..400cfb54 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -10,6 +10,7 @@
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
+#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <Elementary.h>
@@ -1489,3 +1490,84 @@ tytest_tymux_live_scrollback_zero(void)
{
return tytest_tymux_live_run(_live_scrollback_zero_body);
}
+
+#define FD_LIMIT 64
+/* pages of TERMSRV_BACKLOG_ENTRIES_MAX entries: about 25 of them */
+#define FD_SCROLLBACK 100000
+#define FD_MARKER "fd-done"
+/* how long the daemon may take to get within its budget once the output
+ * stopped, well under the 5 s that pages wait before being compressed
+ * without fd pressure */
+#define FD_SETTLE_SECONDS 2.5
+
+static Eina_Bool
+_fd_marker_done(void *data)
+{
+ return _backlog_find(data, FD_MARKER) != 0;
+}
+
+typedef struct
+{
+ TermTymux *ts;
+ int budget;
+} Fd_Wait;
+
+static Eina_Bool
+_fd_budget_done(void *data)
+{
+ Fd_Wait *wait = data;
+ int raw = tytest_tymux_daemon_memfds(wait->ts, "ty-backlog-");
+ int exported = tytest_tymux_daemon_memfds(wait->ts, "ty-export-");
+
+ return (raw >= 0) && (exported >= 0) && (raw + exported <= wait->budget);
+}
+
+/* A daemon started with few descriptors keeps its pages' memfds within a
+ * quarter of them, without waiting for the pages to idle. */
+static int
+_live_fd_budget_body(void)
+{
+ BacklogStoreGeometry geo;
+ struct rlimit rl;
+ TermTymux *ts;
+ Termpty *ty;
+ Fd_Wait wait;
+ char cmd[128];
+ double start;
+
+ _daemon_scrollback_set(FD_SCROLLBACK);
+ backlog_store_geometry_get(FD_SCROLLBACK, sysconf(_SC_PAGESIZE), &geo);
+ /* tymuxd inherits the limit */
+ assert(getrlimit(RLIMIT_NOFILE, &rl) == 0);
+ assert(rl.rlim_max >= FD_LIMIT);
+ rl.rlim_cur = FD_LIMIT;
+ assert(setrlimit(RLIMIT_NOFILE, &rl) == 0);
+ wait.budget = FD_LIMIT / 4;
+ /* the daemon keeps more pages than that */
+ assert(geo.max_entries / geo.entries_per_page > (uint32_t)wait.budget);
+
+ assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+ wait.ts = ts;
+ termpty_backlog_viewport_set(ts->shadow_pty, 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));
+ /* quoted, so that the command line echoed as it is typed does not
+ * match, and pushed into the backlog */
+ snprintf(cmd, sizeof(cmd), "seq 1 %u; echo fd-''done; seq 1 30\n",
+ geo.max_entries + 2 * geo.entries_per_page);
+ assert(tytest_tymux_type(ts, ty, cmd));
+ assert(tytest_tymux_wait(_fd_marker_done, ty, 60.0));
+ start = _now();
+ assert(tytest_tymux_wait(_fd_budget_done, &wait, 30.0));
+ assert(_now() - start < FD_SETTLE_SECONDS);
+
+ tytest_tymux_stop(ts);
+ return 0;
+}
+
+int
+tytest_tymux_live_fd_budget(void)
+{
+ return tytest_tymux_live_run(_live_fd_budget_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index 0e6fccd4..a2ea572d 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -53,5 +53,6 @@ int tytest_tymux_live_resize_no_dup(void);
int tytest_tymux_live_clear(void);
int tytest_tymux_live_scrollback_config(void);
int tytest_tymux_live_scrollback_zero(void);
+int tytest_tymux_live_fd_budget(void);
#endif
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 56d5ff8f..e216a606 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -135,6 +135,7 @@ int tytest_backlog_store_pop(void);
int tytest_backlog_store_next_id(void);
int tytest_backlog_store_links(void);
int tytest_backlog_store_blocks(void);
+int tytest_backlog_store_fd_budget(void);
int tytest_backlog_view_sync(void);
int tytest_backlog_view_ring_full(void);
int tytest_backlog_view_map_again(void);
diff --git a/tests/meson.build b/tests/meson.build
index d22bd416..db8cbb67 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -104,7 +104,7 @@ if tests
'tymux_live_pages_busy', 'tymux_live_window',
'tymux_live_compress', 'tymux_live_resize_no_dup',
'tymux_live_clear', 'tymux_live_scrollback_config',
- 'tymux_live_scrollback_zero'],
+ 'tymux_live_scrollback_zero', 'tymux_live_fd_budget'],
depends: [tymuxd],
timeout: 120)
endif
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.