shm: make max pool size user configurable (via a 'tweak' setting)

This commit is contained in:
Daniel Eklöf 2020-03-25 20:48:02 +01:00
parent e9f1638750
commit 0baa249d8b
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
5 changed files with 39 additions and 11 deletions

View file

@ -22,6 +22,7 @@
#include "wayland.h" #include "wayland.h"
#define ALEN(v) (sizeof(v) / sizeof(v[0])) #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_foreground = 0xdcdccc;
static const uint32_t default_background = 0x111111; static const uint32_t default_background = 0x111111;
@ -621,6 +622,18 @@ parse_section_tweak(
LOG_WARN("tweak: delayed-render-upper=%lu", ns); 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 { else {
LOG_ERR("%s:%u: invalid key: %s", path, lineno, key); LOG_ERR("%s:%u: invalid key: %s", path, lineno, key);
return false; return false;
@ -890,6 +903,7 @@ config_load(struct config *conf, const char *conf_path)
.tweak = { .tweak = {
.delayed_render_lower_ns = 500000, /* 0.5ms */ .delayed_render_lower_ns = 500000, /* 0.5ms */
.delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */ .delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */
.max_shm_pool_size = 512 * 1024 * 1024,
}, },
}; };

View file

@ -77,6 +77,7 @@ struct config {
struct { struct {
uint64_t delayed_render_lower_ns; uint64_t delayed_render_lower_ns;
uint64_t delayed_render_upper_ns; uint64_t delayed_render_upper_ns;
off_t max_shm_pool_size;
} tweak; } tweak;
}; };

2
main.c
View file

@ -330,6 +330,8 @@ main(int argc, char *const *argv)
} while (errno == ERANGE); } while (errno == ERANGE);
} }
shm_set_max_pool_size(conf.tweak.max_shm_pool_size);
if ((fdm = fdm_init()) == NULL) if ((fdm = fdm_init()) == NULL)
goto out; goto out;

32
shm.c
View file

@ -42,15 +42,23 @@
* *
* On 32-bit the available address space is too small and SHM * On 32-bit the available address space is too small and SHM
* scrolling is disabled. * 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 off_t max_pool_size = 512 * 1024 * 1024;
//static const off_t max_pool_size = INT32_MAX;
static tll(struct buffer) buffers; static tll(struct buffer) buffers;
static bool can_punch_hole = false; static bool can_punch_hole = false;
static bool can_punch_hole_initialized = 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 static void
buffer_destroy_dont_close(struct buffer *buf) buffer_destroy_dont_close(struct buffer *buf)
{ {
@ -80,6 +88,15 @@ buffer_destroy(struct buffer *buf)
buf->fd = -1; buf->fd = -1;
} }
void
shm_fini(void)
{
tll_foreach(buffers, it) {
buffer_destroy(&it->item);
tll_remove(buffers, it);
}
}
static void static void
buffer_release(void *data, struct wl_buffer *wl_buffer) 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; off_t memfd_size = scrollable ? max_pool_size : size;
#endif #endif
LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, initial_offset);
if (ftruncate(pool_fd, memfd_size) == -1) { if (ftruncate(pool_fd, memfd_size) == -1) {
LOG_ERRNO("failed to set size of SHM backing memory file"); LOG_ERRNO("failed to set size of SHM backing memory file");
goto err; goto err;
@ -333,15 +352,6 @@ err:
return NULL; return NULL;
} }
void
shm_fini(void)
{
tll_foreach(buffers, it) {
buffer_destroy(&it->item);
tll_remove(buffers, it);
}
}
bool bool
shm_can_scroll(const struct buffer *buf) shm_can_scroll(const struct buffer *buf)
{ {

1
shm.h
View file

@ -37,6 +37,7 @@ struct buffer *shm_get_buffer(
struct wl_shm *shm, int width, int height, unsigned long cookie, bool scrollable); struct wl_shm *shm, int width, int height, unsigned long cookie, bool scrollable);
void shm_fini(void); 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_can_scroll(const struct buffer *buf);
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 top_margin, int top_keep_rows,