From 1224807f503cf6aaef33388af682f754dcbbc3df Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 22 Mar 2020 20:06:44 +0100 Subject: shm: new function, shm_scroll() This function "scrolls" the buffer by the specified number of (pixel) rows. The idea is move the image offset by re-sizing the underlying memfd object. I.e. to scroll forward, increase the size of the memfd file, and move the pixman image offset forward (and the Wayland SHM buffer as well). Only increasing the file size would, obviously, cause the memfd file to grow indefinitely. To deal with this, we "punch" a whole from the beginning of the file to the new offset. This frees the associated memory. Thus, while we have a memfd file whose size is (as seen by e.g. fstat()) is ever growing, the actual file size is always the original buffer size. Some notes: * FALLOC_FL_PUNCH_HOLE can be quite slow when the number of used pages to drop is large. * all normal fallocate() usages have been replaced with ftruncate(), as this is *much* faster. fallocate() guarantees subsequent writes wont fail. I.e. it actually reserves (disk) space. While it doesn't allocate on-disk blocks for on-disk files, it *does* zero-initialize the in-memory blocks. And this is slow. ftruncate() doesn't do this. TODO: implement reverse scrolling (i.e. a negative row count). --- shm.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'shm.h') diff --git a/shm.h b/shm.h index eb9987ed..2b3a9b19 100644 --- a/shm.h +++ b/shm.h @@ -13,20 +13,27 @@ struct buffer { int height; int stride; - bool purge; - bool busy; - size_t size; - void *mmapped; + size_t size; /* Buffer size */ + void *mmapped; /* Raw data */ struct wl_buffer *wl_buf; pixman_image_t *pix; + + /* Internal */ + int fd; /* memfd */ + void *real_mmapped; /* Address returned from mmap */ + size_t mmap_size; /* Size of mmap (>= size) */ + size_t offset; /* Offset into memfd where data begins */ + bool purge; /* True if this buffer should be destroyed */ }; struct buffer *shm_get_buffer( struct wl_shm *shm, int width, int height, unsigned long cookie); void shm_fini(void); +bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows); + void shm_purge(struct wl_shm *shm, unsigned long cookie); struct terminal; -- cgit v1.3 From 5ffee087481ddf3ad6e3b8e74c6acd3f0d736d82 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 23 Mar 2020 19:31:05 +0100 Subject: shm: add shm_can_scroll() --- shm.c | 7 +++++++ shm.h | 1 + 2 files changed, 8 insertions(+) (limited to 'shm.h') diff --git a/shm.c b/shm.c index b9cc325c..59dcb2ac 100644 --- a/shm.c +++ b/shm.c @@ -280,9 +280,16 @@ shm_fini(void) } } +bool +shm_can_scroll(void) +{ + return can_punch_hole; +} + bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows) { + assert(can_punch_hole); assert(buf->busy); assert(buf->pix); assert(buf->wl_buf); diff --git a/shm.h b/shm.h index 2b3a9b19..6801ed93 100644 --- a/shm.h +++ b/shm.h @@ -32,6 +32,7 @@ struct buffer *shm_get_buffer( struct wl_shm *shm, int width, int height, unsigned long cookie); void shm_fini(void); +bool shm_can_scroll(void); bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows); void shm_purge(struct wl_shm *shm, unsigned long cookie); -- cgit v1.3 From 0de3701984ff3b09a804d8369dafc3cd5baefdcc Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 23 Mar 2020 20:45:27 +0100 Subject: shm: scroll: move top/bottom region handling from renderer into shm This allows us to restore the regions without copying the contents to temporary memory. --- render.c | 63 +++++++++++---------------------------------------------------- shm.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ shm.h | 4 +++- 3 files changed, 57 insertions(+), 59 deletions(-) (limited to 'shm.h') diff --git a/render.c b/render.c index 89b8fd0e..2eab275e 100644 --- a/render.c +++ b/render.c @@ -551,7 +551,6 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, gettimeofday(&start_time, NULL); #endif - uint8_t *raw = buf->mmapped; int dst_y = term->margins.top + (dmg->scroll.region.start + 0) * term->cell_height; int src_y = term->margins.top + (dmg->scroll.region.start + dmg->scroll.lines) * term->cell_height; @@ -563,12 +562,13 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, * memmove, while scrolling a *small* number of lines is faster * with SHM scrolling. * - * However, since we need to restore the bottom scrolling region - * when SHM scrolling, we also need to take this into account. + * However, since we need to restore the scrolling regions when + * SHM scrolling, we also need to take this into account. * - * Finally, restoring the window margins is a *huge* performance - * hit when scrolling a large number of lines (in addition to the - * sloweness of SHM scrolling as method). + * Finally, we also have to restore the window margins, and this + * is a *huge* performance hit when scrolling a large number of + * lines (in addition to the sloweness of SHM scrolling as + * method). * * So, we need to figure out when to SHM scroll, and when to * memmove. @@ -597,19 +597,11 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, //try_shm_scroll = false; //try_shm_scroll = true; - /* TODO: this could easily use up all stack */ - uint8_t top_region[dmg->scroll.region.start * term->cell_height * buf->stride]; - if (try_shm_scroll) { - if (dmg->scroll.region.start > 0) { - /* Store a copy of the top region - we need to restore it after scrolling */ - memcpy(top_region, - (uint8_t *)buf->mmapped + term->margins.top * buf->stride, - sizeof(top_region)); - } - did_shm_scroll = shm_scroll( - term->wl->shm, buf, dmg->scroll.lines * term->cell_height); + term->wl->shm, buf, dmg->scroll.lines * term->cell_height, + term->margins.top, dmg->scroll.region.start * term->cell_height, + term->margins.bottom, (term->rows - dmg->scroll.region.end) * term->cell_height); if (!did_shm_scroll) LOG_DBG("fast scroll failed"); @@ -620,47 +612,14 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, dmb->scroll.region.start, scroll_region_lines, term->rows); } - /* - * When SHM scrolling succeeded, the scrolled in area is made up - * of newly allocated, zero-initialized memory. Thus we'll need to - * both copy the bottom scrolling region, and re-render the window - * margins. - * - * This is different from when we scroll with a simple memmove, - * since in that case, the scrolling region and margins are - * *copied*, and thus the original region+margin remains in place. - */ if (did_shm_scroll) { - /* Mmap changed - update buffer pointer */ - raw = buf->mmapped; - - /* Restore top scrolling region */ - memcpy(raw + term->margins.top * buf->stride, top_region, sizeof(top_region)); - - /* Restore bottom scrolling region */ - if (dmg->scroll.region.end < term->rows) { - int src = dmg->scroll.region.end - dmg->scroll.lines; - int dst = dmg->scroll.region.end; - size_t amount = term->rows - dmg->scroll.region.end; - - LOG_DBG("memmoving %zu lines of scroll region", amount); - assert(src >= 0); - - memmove( - raw + (term->margins.top + dst * term->cell_height) * buf->stride, - raw + (term->margins.top + src * term->cell_height) * buf->stride, - amount * term->cell_height * buf->stride); - } - /* Restore margins */ render_margin( term, buf, dmg->scroll.region.end - dmg->scroll.lines, term->rows, true, true); - } - - /* Fallback for when we either cannot do SHM scrolling, or it failed */ - if (!did_shm_scroll) { + } else { + /* Fallback for when we either cannot do SHM scrolling, or it failed */ uint8_t *raw = buf->mmapped; memmove(raw + dst_y * buf->stride, raw + src_y * buf->stride, diff --git a/shm.c b/shm.c index 59dcb2ac..d05210a2 100644 --- a/shm.c +++ b/shm.c @@ -287,7 +287,9 @@ shm_can_scroll(void) } bool -shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows) +shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows, + int top_margin, int top_keep_rows, + int bottom_margin, int bottom_keep_rows) { assert(can_punch_hole); assert(buf->busy); @@ -323,8 +325,24 @@ shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows) struct timeval tot; timersub(&time1, &time0, &tot); - LOG_INFO("fallocate: %lds %ldus", tot.tv_sec, tot.tv_usec); + LOG_INFO("ftruncate: %lds %ldus", tot.tv_sec, tot.tv_usec); + + struct timeval time2 = time1; +#endif + + if (top_keep_rows > 0) { + /* Copy current 'top' region to its new location */ + memmove( + (uint8_t *)buf->mmapped + (top_margin + rows) * buf->stride, + (uint8_t *)buf->mmapped + (top_margin + 0) * buf->stride, + top_keep_rows * buf->stride); + +#if TIME_SCROLL + gettimeofday(&time2, NULL); + timersub(&time2, &time1, &tot); + LOG_INFO("memmove (top region): %lds %ldus", tot.tv_sec, tot.tv_usec); #endif + } /* Destroy old objects (they point to the old offset) */ pixman_image_unref(buf->pix); @@ -341,13 +359,32 @@ shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows) } #if TIME_SCROLL - struct timeval time2; - gettimeofday(&time2, NULL); - timersub(&time2, &time1, &tot); + struct timeval time3; + gettimeofday(&time3, NULL); + timersub(&time3, &time2, &tot); LOG_INFO("PUNCH HOLE: %lds %ldus", tot.tv_sec, tot.tv_usec); #endif - return instantiate_offset(shm, buf, new_offset); + + bool ret = instantiate_offset(shm, buf, new_offset); + + if (ret && bottom_keep_rows > 0) { + /* Copy 'bottom' region to its new location */ + memmove( + (uint8_t *)buf->mmapped + buf->size - (bottom_margin + bottom_keep_rows) * buf->stride, + (uint8_t *)buf->mmapped + buf->size - (bottom_margin + rows + bottom_keep_rows) * buf->stride, + bottom_keep_rows * buf->stride); + +#if TIME_SCROLL + struct timeval time4; + gettimeofday(&time4, NULL); + + timersub(&time4, &time3, &tot); + LOG_INFO("memmove (bottom region): %lds %ldus", tot.tv_sec, tot.tv_usec); +#endif + } + + return ret; } void diff --git a/shm.h b/shm.h index 6801ed93..6637182c 100644 --- a/shm.h +++ b/shm.h @@ -33,7 +33,9 @@ struct buffer *shm_get_buffer( void shm_fini(void); bool shm_can_scroll(void); -bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows); +bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows, + int top_margin, int top_keep_rows, + int bottom_margin, int bottom_keep_rows); void shm_purge(struct wl_shm *shm, unsigned long cookie); -- cgit v1.3 From 5c5f1d096c3a2a6cb8fb3dc5cb3efad5782c1d78 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 24 Mar 2020 17:46:48 +0100 Subject: shm: scroll: implement offset wrap-around * Impose a maximum memfd size limit. In theory, this can be 2GB (wl_shm_create_pool() is the limiting factor - its size argument is an int32_t). For now, use 256MB. This is mainly to reduce the amount of virtual address space used by the compositor, which keeps at least one mmapping (of the entire memfd) around. One mmapping *per terminal window* that is. Given that we have 128TB with 48-bit virtual addresses, we could probably bump this to 2GB without any issues. However, 256MB should be enough. TODO: check how much we typically move the offset when scrolling in a fullscreen window on a 4K monitor. 256MB may turn out to be too small. On 32-bit shm_scroll() is completely disabled. There simply isn't enough address space. * Wrapping is done by moving the offset to "the other end" of the memfd, and copying the buffer contents to the new, wrapped offset. The "normal" scrolling code then does the actual scrolling. This means we'll re-instantiate all objects twice when wrapping. --- shm.c | 193 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- shm.h | 3 +- 2 files changed, 154 insertions(+), 42 deletions(-) (limited to 'shm.h') diff --git a/shm.c b/shm.c index 1f2bd1a6..a7ce6850 100644 --- a/shm.c +++ b/shm.c @@ -23,13 +23,33 @@ #define TIME_SCROLL 0 +/* + * Maximum memfd size allowed. + * + * On 64-bit, we could in theory use up to 2GB (wk_shm_create_pool() + * is limited to int32_t), since we never mmap() the entire region. + * + * The compositor is different matter - it needs to mmap() the entire + * range, and *keep* the mapping for as long as is has buffers + * referencing it (thus - always). And if we open multiple terminals, + * then the required address space multiples... + * + * That said, 128TB (the total amount of available user address space + * on 64-bit) is *a lot*; we can fit 67108864 2GB memfds into + * that. But, let's be conservative for now. + * + * On 32-bit the available address space is too small and SHM + * scrolling is disabled. + */ +static const off_t max_pool_size = 256 * 1024 * 1024; + static tll(struct buffer) buffers; static bool can_punch_hole = false; static bool can_punch_hole_initialized = false; static void -buffer_destroy(struct buffer *buf) +buffer_destroy_dont_close(struct buffer *buf) { if (buf->pix != NULL) pixman_image_unref(buf->pix); @@ -37,8 +57,20 @@ buffer_destroy(struct buffer *buf) wl_buffer_destroy(buf->wl_buf); if (buf->real_mmapped != MAP_FAILED) munmap(buf->real_mmapped, buf->mmap_size); + + buf->pix = NULL; + buf->wl_buf = NULL; + buf->real_mmapped = NULL; + buf->mmapped = NULL; +} + +static void +buffer_destroy(struct buffer *buf) +{ + buffer_destroy_dont_close(buf); if (buf->fd >= 0) close(buf->fd); + buf->fd = -1; } static void @@ -55,24 +87,30 @@ static const struct wl_buffer_listener buffer_listener = { .release = &buffer_release, }; +static size_t +page_size(void) +{ + static size_t size = 0; + if (size == 0) { + size = sysconf(_SC_PAGE_SIZE); + if (size < 0) { + LOG_ERRNO("failed to get page size"); + size = 4096; + } + } + assert(size > 0); + return size; +} + static bool -instantiate_offset(struct wl_shm *shm, struct buffer *buf, size_t new_offset) +instantiate_offset(struct wl_shm *shm, struct buffer *buf, off_t new_offset) { assert(buf->fd >= 0); assert(buf->mmapped == NULL); assert(buf->real_mmapped == NULL); assert(buf->wl_buf == NULL); assert(buf->pix == NULL); - - static size_t page_size = 0; - if (page_size == 0) { - page_size = sysconf(_SC_PAGE_SIZE); - if (page_size < 0) { - LOG_ERRNO("failed to get page size"); - page_size = 4096; - } - } - assert(page_size > 0); + assert(new_offset + buf->size <= max_pool_size); void *real_mmapped = MAP_FAILED; void *mmapped = MAP_FAILED; @@ -81,8 +119,8 @@ instantiate_offset(struct wl_shm *shm, struct buffer *buf, size_t new_offset) pixman_image_t *pix = NULL; /* mmap offset must be page aligned */ - size_t aligned_offset = new_offset & ~(page_size - 1); - size_t page_offset = new_offset & (page_size - 1); + off_t aligned_offset = new_offset & ~(page_size() - 1); + size_t page_offset = new_offset & (page_size() - 1); size_t mmap_size = buf->size + page_offset; assert(aligned_offset <= new_offset); @@ -222,7 +260,17 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) goto err; } - if (ftruncate(pool_fd, size) == -1) { + /* + * If we can figure our if we can punch holes *before* this, we + * could set the initial offset to somewhere in the middle of the + * avaiable address space. This would allow both backward and + * forward scrolling without immediately needing to wrap. + */ + //off_t initial_offset = (max_pool_size / 4) & ~(page_size() - 1); + off_t initial_offset = 0; + off_t memfd_size = initial_offset + size; + + if (ftruncate(pool_fd, memfd_size) == -1) { LOG_ERRNO("failed to truncate SHM pool"); goto err; } @@ -256,7 +304,7 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) ); struct buffer *ret = &tll_back(buffers); - if (!instantiate_offset(shm, ret, 0)) + if (!instantiate_offset(shm, ret, initial_offset)) goto err; return ret; @@ -281,7 +329,36 @@ shm_fini(void) bool shm_can_scroll(void) { +#if defined(__i386__) + /* Not enough virtual address space in 32-bit */ + return false; +#else return can_punch_hole; +#endif +} + +static bool +wrap_buffer(struct wl_shm *shm, struct buffer *buf, off_t new_offset) +{ + off_t aligned_offset = new_offset & ~(page_size() - 1); + size_t page_offset = new_offset & (page_size() - 1); + size_t mmap_size = buf->size + page_offset; + + assert(aligned_offset <= new_offset); + assert(mmap_size >= buf->size); + + uint8_t *m = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_UNINITIALIZED, buf->fd, aligned_offset); + if (m == MAP_FAILED) { + LOG_ERRNO("failed to mmap"); + return false; + } + + memcpy(m + page_offset, buf->mmapped, buf->size); + munmap(m, mmap_size); + + /* Re-instantiate pixman+wl_buffer+raw pointersw */ + buffer_destroy_dont_close(buf); + return instantiate_offset(shm, buf, new_offset); } static bool @@ -303,7 +380,22 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, assert(rows > 0); assert(rows * buf->stride < buf->size); - const size_t new_offset = buf->offset + rows * buf->stride; + + if (buf->offset + rows * buf->stride + buf->size > max_pool_size) { + LOG_INFO("memfd offset wrap around"); + assert(buf->offset > buf->size); + + /* + * Wrap around by moving the offset to the beginning of the + * memfd. The ftruncate() we do below takes care of trimming + * down the size. + */ + if (!wrap_buffer(shm, buf, 0)) + goto err; + } + + off_t new_offset = buf->offset + rows * buf->stride; + assert(new_offset + buf->size <= max_pool_size); #if TIME_SCROLL struct timeval time0; @@ -312,9 +404,9 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, /* Increase file size */ if (ftruncate(buf->fd, new_offset + buf->size) < 0) { - LOG_ERRNO("failed increase memfd size from %zu -> %zu", + LOG_ERRNO("failed to resize memfd from %zu -> %zu", buf->offset + buf->size, new_offset + buf->size); - return false; + goto err; } #if TIME_SCROLL @@ -343,19 +435,17 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, } /* Destroy old objects (they point to the old offset) */ - pixman_image_unref(buf->pix); - wl_buffer_destroy(buf->wl_buf); - munmap(buf->real_mmapped, buf->mmap_size); - - buf->pix = NULL; - buf->wl_buf = NULL; - buf->real_mmapped = buf->mmapped = NULL; + buffer_destroy_dont_close(buf); /* Free unused memory */ - if (fallocate(buf->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, new_offset) < 0) { - abort(); + if (new_offset > 0 && + fallocate(buf->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, new_offset) < 0) + { + LOG_ERRNO("fallocate(FALLOC_FL_PUNCH_HOLE, 0, %lu) failed", new_offset); + goto err; } + #if TIME_SCROLL struct timeval time3; gettimeofday(&time3, NULL); @@ -363,6 +453,7 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, LOG_INFO("PUNCH HOLE: %lds %ldus", tot.tv_sec, tot.tv_usec); #endif + /* Re-instantiate pixman+wl_buffer+raw pointersw */ bool ret = instantiate_offset(shm, buf, new_offset); if (ret && bottom_keep_rows > 0) { @@ -382,6 +473,10 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, } return ret; + +err: + abort(); + return false; } static bool @@ -391,12 +486,27 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, { assert(rows > 0); - size_t diff = rows * buf->stride; - if (diff > buf->offset) - return false; + off_t diff = rows * buf->stride; + if (diff > buf->offset) { + LOG_INFO("memfd offset reverse wrap-around"); + + /* + * Wrap around by resizing the memfd and moving the offset to + * the end of the file, taking care the new offset is aligned. + */ + + if (ftruncate(buf->fd, max_pool_size) < 0) { + LOG_ERRNO("failed to resize memfd from %zu -> %zu", + buf->offset + buf->size, max_pool_size - buf->size); + goto err; + } + + if (!wrap_buffer(shm, buf, (max_pool_size - buf->size) & ~(page_size() - 1))) + goto err; + } - const size_t new_offset = buf->offset - rows * buf->stride; - assert(new_offset < buf->offset); + off_t new_offset = buf->offset - diff; + assert(new_offset <= max_pool_size); #if TIME_SCROLL struct timeval time0; @@ -421,16 +531,12 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, } /* Destroy old objects (they point to the old offset) */ - pixman_image_unref(buf->pix); - wl_buffer_destroy(buf->wl_buf); - munmap(buf->real_mmapped, buf->mmap_size); - - buf->pix = NULL; - buf->wl_buf = NULL; - buf->real_mmapped = buf->mmapped = NULL; + buffer_destroy_dont_close(buf); if (ftruncate(buf->fd, new_offset + buf->size) < 0) { - abort(); + LOG_ERRNO("failed to resize memfd from %zu -> %zu", + buf->offset + buf->size, new_offset + buf->size); + goto err; } #if TIME_SCROLL @@ -440,6 +546,7 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, LOG_INFO("ftruncate: %lds %ldus", tot.tv_sec, tot.tv_usec); #endif + /* Re-instantiate pixman+wl_buffer+raw pointers */ bool ret = instantiate_offset(shm, buf, new_offset); if (ret && top_keep_rows > 0) { @@ -458,6 +565,10 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, } return ret; + +err: + abort(); + return false; } bool diff --git a/shm.h b/shm.h index 6637182c..d67b3567 100644 --- a/shm.h +++ b/shm.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -24,7 +25,7 @@ struct buffer { int fd; /* memfd */ void *real_mmapped; /* Address returned from mmap */ size_t mmap_size; /* Size of mmap (>= size) */ - size_t offset; /* Offset into memfd where data begins */ + off_t offset; /* Offset into memfd where data begins */ bool purge; /* True if this buffer should be destroyed */ }; -- cgit v1.3 From 03319560f5c7c908b1c1d229b3ba53ed1ac6b692 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 25 Mar 2020 18:26:58 +0100 Subject: shm: scroll: keep shm pool around, and fix its size at max allowed This lessens the burden on (primarily) the compositor, since we no longer tear down and re-create the SHM pool when scrolling. The SHM pool is setup once, and its size is fixed at the maximum allowed (512MB for now, 2GB would be possible). This also allows us to mmap() the memfd once. The exposed raw pointer is simply an offset from the memfd mmapping. Note that this means e.g. rouge rendering code will be able to write outside the buffer. Finally, only do this if the caller explicitly wants to enable scrolling. The memfd of other buffers are sized to the requested size. --- render.c | 24 +++--- shm.c | 255 ++++++++++++++++++++++++++++++++------------------------------- shm.h | 10 ++- 3 files changed, 150 insertions(+), 139 deletions(-) (limited to 'shm.h') diff --git a/render.c b/render.c index 4238a4b8..f77b7b3d 100644 --- a/render.c +++ b/render.c @@ -588,9 +588,10 @@ grid_render_scroll(struct terminal *term, struct buffer *buf, * SHM. Otherwise use memmove. */ bool try_shm_scroll = - shm_can_scroll() && (dmg->scroll.lines + - dmg->scroll.region.start + - (term->rows - dmg->scroll.region.end)) < term->rows / 2; + shm_can_scroll(buf) && ( + dmg->scroll.lines + + dmg->scroll.region.start + + (term->rows - dmg->scroll.region.end)) < term->rows / 2; bool did_shm_scroll = false; @@ -657,9 +658,10 @@ grid_render_scroll_reverse(struct terminal *term, struct buffer *buf, int dst_y = term->margins.top + (dmg->scroll.region.start + dmg->scroll.lines) * term->cell_height; bool try_shm_scroll = - shm_can_scroll() && (dmg->scroll.lines + - dmg->scroll.region.start + - (term->rows - dmg->scroll.region.end)) < term->rows / 2; + shm_can_scroll(buf) && ( + dmg->scroll.lines + + dmg->scroll.region.start + + (term->rows - dmg->scroll.region.end)) < term->rows / 2; bool did_shm_scroll = false; @@ -912,7 +914,7 @@ render_csd_title(struct terminal *term) unsigned long cookie = shm_cookie_csd(term, CSD_SURF_TITLE); struct buffer *buf = shm_get_buffer( - term->wl->shm, info.width, info.height, cookie); + term->wl->shm, info.width, info.height, cookie, false); uint32_t _color = term->colors.default_fg; uint16_t alpha = 0xffff; @@ -943,7 +945,7 @@ render_csd_border(struct terminal *term, enum csd_surface surf_idx) unsigned long cookie = shm_cookie_csd(term, surf_idx); struct buffer *buf = shm_get_buffer( - term->wl->shm, info.width, info.height, cookie); + term->wl->shm, info.width, info.height, cookie, false); pixman_color_t color = color_hex_to_pixman_with_alpha(0, 0); render_csd_part(term, surf, buf, info.width, info.height, &color); @@ -1112,7 +1114,7 @@ render_csd_button(struct terminal *term, enum csd_surface surf_idx) unsigned long cookie = shm_cookie_csd(term, surf_idx); struct buffer *buf = shm_get_buffer( - term->wl->shm, info.width, info.height, cookie); + term->wl->shm, info.width, info.height, cookie, false); uint32_t _color; uint16_t alpha = 0xffff; @@ -1239,7 +1241,7 @@ grid_render(struct terminal *term) unsigned long cookie = shm_cookie_grid(term); struct buffer *buf = shm_get_buffer( - term->wl->shm, term->width, term->height, cookie); + term->wl->shm, term->width, term->height, cookie, true); pixman_image_set_clip_region(buf->pix, NULL); @@ -1526,7 +1528,7 @@ render_search_box(struct terminal *term) size_t glyph_offset = term->render.search_glyph_offset; unsigned long cookie = shm_cookie_search(term); - struct buffer *buf = shm_get_buffer(term->wl->shm, width, height, cookie); + struct buffer *buf = shm_get_buffer(term->wl->shm, width, height, cookie, false); /* Background - yellow on empty/match, red on mismatch */ pixman_color_t color = color_hex_to_pixman( diff --git a/shm.c b/shm.c index a7ce6850..e4772936 100644 --- a/shm.c +++ b/shm.c @@ -1,9 +1,11 @@ #include "shm.h" -#include +#include #include #include #include +#include +#include #include #include @@ -41,7 +43,8 @@ * On 32-bit the available address space is too small and SHM * scrolling is disabled. */ -static const off_t max_pool_size = 256 * 1024 * 1024; +static const off_t max_pool_size = 512 * 1024 * 1024; +//static const off_t max_pool_size = INT32_MAX; static tll(struct buffer) buffers; @@ -55,12 +58,9 @@ buffer_destroy_dont_close(struct buffer *buf) pixman_image_unref(buf->pix); if (buf->wl_buf != NULL) wl_buffer_destroy(buf->wl_buf); - if (buf->real_mmapped != MAP_FAILED) - munmap(buf->real_mmapped, buf->mmap_size); buf->pix = NULL; buf->wl_buf = NULL; - buf->real_mmapped = NULL; buf->mmapped = NULL; } @@ -68,8 +68,15 @@ static void buffer_destroy(struct buffer *buf) { buffer_destroy_dont_close(buf); + if (buf->real_mmapped != MAP_FAILED) + munmap(buf->real_mmapped, buf->mmap_size); + if (buf->pool != NULL) + wl_shm_pool_destroy(buf->pool); if (buf->fd >= 0) close(buf->fd); + + buf->real_mmapped = MAP_FAILED; + buf->pool = NULL; buf->fd = -1; } @@ -107,51 +114,23 @@ instantiate_offset(struct wl_shm *shm, struct buffer *buf, off_t new_offset) { assert(buf->fd >= 0); assert(buf->mmapped == NULL); - assert(buf->real_mmapped == NULL); assert(buf->wl_buf == NULL); assert(buf->pix == NULL); assert(new_offset + buf->size <= max_pool_size); - void *real_mmapped = MAP_FAILED; void *mmapped = MAP_FAILED; - struct wl_shm_pool *pool = NULL; struct wl_buffer *wl_buf = NULL; pixman_image_t *pix = NULL; - /* mmap offset must be page aligned */ - off_t aligned_offset = new_offset & ~(page_size() - 1); - size_t page_offset = new_offset & (page_size() - 1); - size_t mmap_size = buf->size + page_offset; - - assert(aligned_offset <= new_offset); - assert(mmap_size >= buf->size); - - LOG_DBG("size=%zx, offset=%zx, size-aligned=%zx, offset-aligned=%zx", - buf->size, buf->offset, mmap_size, aligned_offset); - - real_mmapped = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_UNINITIALIZED, buf->fd, aligned_offset); - if (real_mmapped == MAP_FAILED) { - LOG_ERRNO("failed to mmap SHM backing memory file"); - goto err; - } - mmapped = real_mmapped + page_offset; - - pool = wl_shm_create_pool(shm, buf->fd, new_offset + buf->size); - if (pool == NULL) { - LOG_ERR("failed to create SHM pool"); - goto err; - } + mmapped = (uint8_t *)buf->real_mmapped + new_offset; wl_buf = wl_shm_pool_create_buffer( - pool, new_offset, buf->width, buf->height, buf->stride, WL_SHM_FORMAT_ARGB8888); + buf->pool, new_offset, buf->width, buf->height, buf->stride, WL_SHM_FORMAT_ARGB8888); if (wl_buf == NULL) { LOG_ERR("failed to create SHM buffer"); goto err; } - /* We use the entire pool for our single buffer */ - wl_shm_pool_destroy(pool); pool = NULL; - /* One pixman image for each worker thread (do we really need multiple?) */ pix = pixman_image_create_bits_no_clear( PIXMAN_a8r8g8b8, buf->width, buf->height, (uint32_t *)mmapped, buf->stride); @@ -161,9 +140,7 @@ instantiate_offset(struct wl_shm *shm, struct buffer *buf, off_t new_offset) } buf->offset = new_offset; - buf->real_mmapped = real_mmapped; buf->mmapped = mmapped; - buf->mmap_size = mmap_size; buf->wl_buf = wl_buf; buf->pix = pix; @@ -175,17 +152,13 @@ err: pixman_image_unref(pix); if (wl_buf != NULL) wl_buffer_destroy(wl_buf); - if (pool != NULL) - wl_shm_pool_destroy(pool); - if (real_mmapped != MAP_FAILED) - munmap(real_mmapped, mmap_size); abort(); return false; } struct buffer * -shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) +shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, bool scrollable) { /* Purge buffers marked for purging */ tll_foreach(buffers, it) { @@ -251,27 +224,28 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) const int stride = stride_for_format_and_width(PIXMAN_a8r8g8b8, width); const size_t size = stride * height; + void *real_mmapped = MAP_FAILED; + struct wl_shm_pool *pool = NULL; + LOG_DBG("cookie=%lx: allocating new buffer: %zu KB", cookie, size / 1024); /* Backing memory for SHM */ - pool_fd = memfd_create("foot-wayland-shm-buffer-pool", MFD_CLOEXEC); + pool_fd = memfd_create("foot-wayland-shm-buffer-pool", MFD_CLOEXEC | MFD_ALLOW_SEALING); if (pool_fd == -1) { LOG_ERRNO("failed to create SHM backing memory file"); goto err; } - /* - * If we can figure our if we can punch holes *before* this, we - * could set the initial offset to somewhere in the middle of the - * avaiable address space. This would allow both backward and - * forward scrolling without immediately needing to wrap. - */ - //off_t initial_offset = (max_pool_size / 4) & ~(page_size() - 1); +#if defined(__i386__) off_t initial_offset = 0; - off_t memfd_size = initial_offset + size; + off_t memfd_size = size; +#else + off_t initial_offset = scrollable ? (max_pool_size / 4) & ~(page_size() - 1) : 0; + off_t memfd_size = scrollable ? max_pool_size : size; +#endif if (ftruncate(pool_fd, memfd_size) == -1) { - LOG_ERRNO("failed to truncate SHM pool"); + LOG_ERRNO("failed to set size of SHM backing memory file"); goto err; } @@ -287,6 +261,28 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) } } + if (scrollable && !can_punch_hole) { + initial_offset = 0; + memfd_size = size; + ftruncate(pool_fd, memfd_size); + scrollable = false; + } + + real_mmapped = mmap( + NULL, memfd_size, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_UNINITIALIZED, pool_fd, 0); + + if (real_mmapped == MAP_FAILED) { + LOG_ERRNO("failed to mmap SHM backing memory file"); + goto err; + } + + pool = wl_shm_create_pool(shm, pool_fd, memfd_size); + if (pool == NULL) { + LOG_ERR("failed to create SHM pool"); + goto err; + } + /* Push to list of available buffers, but marked as 'busy' */ tll_push_back( buffers, @@ -298,7 +294,10 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) .busy = true, .size = size, .fd = pool_fd, - .mmap_size = size, + .pool = pool, + .scrollable = scrollable, + .real_mmapped = real_mmapped, + .mmap_size = memfd_size, .offset = 0} ) ); @@ -309,6 +308,10 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie) return ret; err: + if (pool != NULL) + wl_shm_pool_destroy(pool); + if (real_mmapped != MAP_FAILED) + munmap(real_mmapped, memfd_size); if (pool_fd != -1) close(pool_fd); @@ -327,35 +330,46 @@ shm_fini(void) } bool -shm_can_scroll(void) +shm_can_scroll(const struct buffer *buf) { #if defined(__i386__) /* Not enough virtual address space in 32-bit */ return false; #else - return can_punch_hole; + return can_punch_hole && buf->scrollable; #endif } static bool wrap_buffer(struct wl_shm *shm, struct buffer *buf, off_t new_offset) { - off_t aligned_offset = new_offset & ~(page_size() - 1); - size_t page_offset = new_offset & (page_size() - 1); - size_t mmap_size = buf->size + page_offset; - - assert(aligned_offset <= new_offset); - assert(mmap_size >= buf->size); + /* We don't allow overlapping offsets */ + off_t diff __attribute__((unused)) = + new_offset < buf->offset ? buf->offset - new_offset : new_offset - buf->offset; + assert(diff > buf->size); + + memcpy((uint8_t *)buf->real_mmapped + new_offset, buf->mmapped, buf->size); + + off_t trim_ofs, trim_len; + if (new_offset > buf->offset) { + /* Trim everything *before* the new offset */ + trim_ofs = 0; + trim_len = new_offset; + } else { + /* Trim everything *after* the new buffer location */ + trim_ofs = new_offset + buf->size; + trim_len = buf->mmap_size - trim_ofs; + } - uint8_t *m = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_UNINITIALIZED, buf->fd, aligned_offset); - if (m == MAP_FAILED) { - LOG_ERRNO("failed to mmap"); + if (fallocate( + buf->fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + trim_ofs, trim_len) < 0) + { + LOG_ERRNO("failed to trim SHM backing memory file"); return false; } - memcpy(m + page_offset, buf->mmapped, buf->size); - munmap(m, mmap_size); - /* Re-instantiate pixman+wl_buffer+raw pointersw */ buffer_destroy_dont_close(buf); return instantiate_offset(shm, buf, new_offset); @@ -370,7 +384,6 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, assert(buf->busy); assert(buf->pix); assert(buf->wl_buf); - assert(buf->real_mmapped); assert(buf->fd >= 0); if (!can_punch_hole) @@ -378,45 +391,24 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, LOG_DBG("scrolling %d rows (%d bytes)", rows, rows * buf->stride); + const off_t diff = rows * buf->stride; assert(rows > 0); - assert(rows * buf->stride < buf->size); + assert(diff < buf->size); - if (buf->offset + rows * buf->stride + buf->size > max_pool_size) { - LOG_INFO("memfd offset wrap around"); - assert(buf->offset > buf->size); - - /* - * Wrap around by moving the offset to the beginning of the - * memfd. The ftruncate() we do below takes care of trimming - * down the size. - */ + if (buf->offset + diff + buf->size > max_pool_size) { + LOG_DBG("memfd offset wrap around"); if (!wrap_buffer(shm, buf, 0)) goto err; } - off_t new_offset = buf->offset + rows * buf->stride; + off_t new_offset = buf->offset + diff; + assert(new_offset > buf->offset); assert(new_offset + buf->size <= max_pool_size); -#if TIME_SCROLL - struct timeval time0; - gettimeofday(&time0, NULL); -#endif - - /* Increase file size */ - if (ftruncate(buf->fd, new_offset + buf->size) < 0) { - LOG_ERRNO("failed to resize memfd from %zu -> %zu", - buf->offset + buf->size, new_offset + buf->size); - goto err; - } - #if TIME_SCROLL struct timeval time1; gettimeofday(&time1, NULL); - struct timeval tot; - timersub(&time1, &time0, &tot); - LOG_INFO("ftruncate: %lds %ldus", tot.tv_sec, tot.tv_usec); - struct timeval time2 = time1; #endif @@ -437,15 +429,19 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, /* Destroy old objects (they point to the old offset) */ buffer_destroy_dont_close(buf); - /* Free unused memory */ - if (new_offset > 0 && - fallocate(buf->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, new_offset) < 0) + /* Free unused memory - everything up until the new offset */ + const off_t trim_ofs = 0; + const off_t trim_len = new_offset; + + if (fallocate( + buf->fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + trim_ofs, trim_len) < 0) { - LOG_ERRNO("fallocate(FALLOC_FL_PUNCH_HOLE, 0, %lu) failed", new_offset); + LOG_ERRNO("failed to trim SHM backing memory file"); goto err; } - #if TIME_SCROLL struct timeval time3; gettimeofday(&time3, NULL); @@ -456,6 +452,13 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, /* Re-instantiate pixman+wl_buffer+raw pointersw */ bool ret = instantiate_offset(shm, buf, new_offset); +#if TIME_SCROLL + struct timeval time4; + gettimeofday(&time4, NULL); + timersub(&time4, &time3, &tot); + LOG_INFO("instantiate offset: %lds %ldus", tot.tv_sec, tot.tv_usec); +#endif + if (ret && bottom_keep_rows > 0) { /* Copy 'bottom' region to its new location */ memmove( @@ -464,10 +467,10 @@ shm_scroll_forward(struct wl_shm *shm, struct buffer *buf, int rows, bottom_keep_rows * buf->stride); #if TIME_SCROLL - struct timeval time4; - gettimeofday(&time4, NULL); + struct timeval time5; + gettimeofday(&time5, NULL); - timersub(&time4, &time3, &tot); + timersub(&time5, &time4, &tot); LOG_INFO("memmove (bottom region): %lds %ldus", tot.tv_sec, tot.tv_usec); #endif } @@ -486,26 +489,15 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, { assert(rows > 0); - off_t diff = rows * buf->stride; + const off_t diff = rows * buf->stride; if (diff > buf->offset) { - LOG_INFO("memfd offset reverse wrap-around"); - - /* - * Wrap around by resizing the memfd and moving the offset to - * the end of the file, taking care the new offset is aligned. - */ - - if (ftruncate(buf->fd, max_pool_size) < 0) { - LOG_ERRNO("failed to resize memfd from %zu -> %zu", - buf->offset + buf->size, max_pool_size - buf->size); - goto err; - } - + LOG_DBG("memfd offset reverse wrap-around"); if (!wrap_buffer(shm, buf, (max_pool_size - buf->size) & ~(page_size() - 1))) goto err; } off_t new_offset = buf->offset - diff; + assert(new_offset < buf->offset); assert(new_offset <= max_pool_size); #if TIME_SCROLL @@ -533,22 +525,35 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, /* Destroy old objects (they point to the old offset) */ buffer_destroy_dont_close(buf); - if (ftruncate(buf->fd, new_offset + buf->size) < 0) { - LOG_ERRNO("failed to resize memfd from %zu -> %zu", - buf->offset + buf->size, new_offset + buf->size); + /* Free unused memory - everything after the relocated buffer */ + const off_t trim_ofs = new_offset + buf->size; + const off_t trim_len = buf->mmap_size - trim_ofs; + + if (fallocate( + buf->fd, + FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + trim_ofs, trim_len) < 0) + { + LOG_ERRNO("failed to trim SHM backing memory"); goto err; } - #if TIME_SCROLL struct timeval time2; gettimeofday(&time2, NULL); timersub(&time2, &time1, &tot); - LOG_INFO("ftruncate: %lds %ldus", tot.tv_sec, tot.tv_usec); + LOG_INFO("fallocate: %lds %ldus", tot.tv_sec, tot.tv_usec); #endif /* Re-instantiate pixman+wl_buffer+raw pointers */ bool ret = instantiate_offset(shm, buf, new_offset); +#if TIME_SCROLL + struct timeval time3; + gettimeofday(&time3, NULL); + timersub(&time3, &time2, &tot); + LOG_INFO("instantiate offset: %lds %ldus", tot.tv_sec, tot.tv_usec); +#endif + if (ret && top_keep_rows > 0) { /* Copy current 'top' region to its new location */ memmove( @@ -557,9 +562,9 @@ shm_scroll_reverse(struct wl_shm *shm, struct buffer *buf, int rows, top_keep_rows * buf->stride); #if TIME_SCROLL - struct timeval time3; - gettimeofday(&time3, NULL); - timersub(&time3, &time2, &tot); + struct timeval time4; + gettimeofday(&time4, NULL); + timersub(&time4, &time2, &tot); LOG_INFO("memmove (top region): %lds %ldus", tot.tv_sec, tot.tv_usec); #endif } diff --git a/shm.h b/shm.h index d67b3567..fe3f4b93 100644 --- a/shm.h +++ b/shm.h @@ -16,24 +16,28 @@ struct buffer { bool busy; size_t size; /* Buffer size */ - void *mmapped; /* Raw data */ + void *mmapped; /* Raw data (TODO: rename) */ struct wl_buffer *wl_buf; pixman_image_t *pix; /* Internal */ int fd; /* memfd */ + struct wl_shm_pool *pool; + void *real_mmapped; /* Address returned from mmap */ size_t mmap_size; /* Size of mmap (>= size) */ off_t offset; /* Offset into memfd where data begins */ + + bool scrollable; bool purge; /* True if this buffer should be destroyed */ }; struct buffer *shm_get_buffer( - struct wl_shm *shm, int width, int height, unsigned long cookie); + struct wl_shm *shm, int width, int height, unsigned long cookie, bool scrollable); void shm_fini(void); -bool shm_can_scroll(void); +bool shm_can_scroll(const struct buffer *buf); bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows, int top_margin, int top_keep_rows, int bottom_margin, int bottom_keep_rows); -- cgit v1.3 From 0baa249d8b59007913dbf7db8aa8d94e6c2b9d44 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 25 Mar 2020 20:48:02 +0100 Subject: shm: make max pool size user configurable (via a 'tweak' setting) --- config.c | 14 ++++++++++++++ config.h | 1 + main.c | 2 ++ shm.c | 32 +++++++++++++++++++++----------- shm.h | 1 + 5 files changed, 39 insertions(+), 11 deletions(-) (limited to 'shm.h') diff --git a/config.c b/config.c index 80163e2e..16831248 100644 --- a/config.c +++ b/config.c @@ -22,6 +22,7 @@ #include "wayland.h" #define ALEN(v) (sizeof(v) / sizeof(v[0])) +#define min(x, y) ((x) < (y) ? (x) : (y)) static const uint32_t default_foreground = 0xdcdccc; static const uint32_t default_background = 0x111111; @@ -621,6 +622,18 @@ parse_section_tweak( LOG_WARN("tweak: delayed-render-upper=%lu", ns); } + else if (strcmp(key, "max-shm-pool-size-mb") == 0) { + unsigned long mb; + if (!str_to_ulong(value, 10, &mb)) { + LOG_ERR("%s:%d: expected an integer: %s", path, lineno, value); + return false; + } + + conf->tweak.max_shm_pool_size = min(mb * 1024 * 1024, INT32_MAX); + LOG_WARN("tweak: max-shm-pool-size=%lu bytes", + conf->tweak.max_shm_pool_size); + } + else { LOG_ERR("%s:%u: invalid key: %s", path, lineno, key); return false; @@ -890,6 +903,7 @@ config_load(struct config *conf, const char *conf_path) .tweak = { .delayed_render_lower_ns = 500000, /* 0.5ms */ .delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */ + .max_shm_pool_size = 512 * 1024 * 1024, }, }; diff --git a/config.h b/config.h index 596aa368..f53a1b7b 100644 --- a/config.h +++ b/config.h @@ -77,6 +77,7 @@ struct config { struct { uint64_t delayed_render_lower_ns; uint64_t delayed_render_upper_ns; + off_t max_shm_pool_size; } tweak; }; diff --git a/main.c b/main.c index b6ca0fa4..b7dbef13 100644 --- a/main.c +++ b/main.c @@ -330,6 +330,8 @@ main(int argc, char *const *argv) } while (errno == ERANGE); } + shm_set_max_pool_size(conf.tweak.max_shm_pool_size); + if ((fdm = fdm_init()) == NULL) goto out; diff --git a/shm.c b/shm.c index 7ac1f6c8..c2e99004 100644 --- a/shm.c +++ b/shm.c @@ -42,15 +42,23 @@ * * On 32-bit the available address space is too small and SHM * scrolling is disabled. + * + * Note: this is the _default_ size. It can be overridden by calling + * shm_set_max_pool_size(); */ -static const off_t max_pool_size = 512 * 1024 * 1024; -//static const off_t max_pool_size = INT32_MAX; +static off_t max_pool_size = 512 * 1024 * 1024; static tll(struct buffer) buffers; static bool can_punch_hole = false; static bool can_punch_hole_initialized = false; +void +shm_set_max_pool_size(off_t _max_pool_size) +{ + max_pool_size = _max_pool_size; +} + static void buffer_destroy_dont_close(struct buffer *buf) { @@ -80,6 +88,15 @@ buffer_destroy(struct buffer *buf) buf->fd = -1; } +void +shm_fini(void) +{ + tll_foreach(buffers, it) { + buffer_destroy(&it->item); + tll_remove(buffers, it); + } +} + static void buffer_release(void *data, struct wl_buffer *wl_buffer) { @@ -244,6 +261,8 @@ shm_get_buffer(struct wl_shm *shm, int width, int height, unsigned long cookie, off_t memfd_size = scrollable ? max_pool_size : size; #endif + LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, initial_offset); + if (ftruncate(pool_fd, memfd_size) == -1) { LOG_ERRNO("failed to set size of SHM backing memory file"); goto err; @@ -333,15 +352,6 @@ err: return NULL; } -void -shm_fini(void) -{ - tll_foreach(buffers, it) { - buffer_destroy(&it->item); - tll_remove(buffers, it); - } -} - bool shm_can_scroll(const struct buffer *buf) { diff --git a/shm.h b/shm.h index fe3f4b93..aff53d76 100644 --- a/shm.h +++ b/shm.h @@ -37,6 +37,7 @@ struct buffer *shm_get_buffer( struct wl_shm *shm, int width, int height, unsigned long cookie, bool scrollable); void shm_fini(void); +void shm_set_max_pool_size(off_t max_pool_size); bool shm_can_scroll(const struct buffer *buf); bool shm_scroll(struct wl_shm *shm, struct buffer *buf, int rows, int top_margin, int top_keep_rows, -- cgit v1.3