Codebergdnkl
foot
master
1#include "shm.h"
3#include <stdio.h>
4#include <string.h>
5#include <errno.h>
6#include <unistd.h>
7#include <limits.h>
8#include <time.h>
10#include <sys/types.h>
11#include <sys/mman.h>
12#include <fcntl.h>
14#include <pixman.h>
16#include <tllist.h>
18#define LOG_MODULE "shm"
19#define LOG_ENABLE_DBG 0
20#include "log.h"
21#include "debug.h"
22#include "macros.h"
23#include "stride.h"
24#include "xmalloc.h"
26#if !defined(MAP_UNINITIALIZED)
27 #define MAP_UNINITIALIZED 0
28#endif
30#if !defined(MFD_NOEXEC_SEAL)
31 #define MFD_NOEXEC_SEAL 0
32#endif
34#define TIME_SCROLL 0
36#define FORCED_DOUBLE_BUFFERING 0
38/*
39 * Maximum memfd size allowed.
40 *
41 * On 64-bit, we could in theory use up to 2GB (wk_shm_create_pool()
42 * is limited to int32_t), since we never mmap() the entire region.
43 *
44 * The compositor is different matter - it needs to mmap() the entire
45 * range, and *keep* the mapping for as long as is has buffers
46 * referencing it (thus - always). And if we open multiple terminals,
47 * then the required address space multiples...
48 *
49 * That said, 128TB (the total amount of available user address space
50 * on 64-bit) is *a lot*; we can fit 67108864 2GB memfds into
51 * that. But, let's be conservative for now.
52 *
53 * On 32-bit the available address space is too small and SHM
54 * scrolling is disabled.
55 *
56 * Note: this is the _default_ size. It can be overridden by calling
57 * shm_set_max_pool_size();
58 */
59static off_t max_pool_size = 512 * 1024 * 1024;
61static bool can_punch_hole = false;
62static bool can_punch_hole_initialized = false;
64static size_t min_stride_alignment = 0;
66struct buffer_pool {
67 int fd; /* memfd */
68 struct wl_shm_pool *wl_pool;
70 void *real_mmapped; /* Address returned from mmap */
71 size_t mmap_size; /* Size of mmap (>= size) */
73 size_t ref_count;
74};
76struct buffer_chain;
77struct buffer_private {
78 struct buffer public;
79 struct buffer_chain *chain;
81 size_t ref_count;
82 bool busy; /* Owned by compositor */
84 struct buffer_pool *pool;
85 off_t offset; /* Offset into memfd where data begins */
86 size_t size;
88 bool scrollable;
90 void (*release_cb)(struct buffer *buf, void *data);
91 void *cb_data;
92};
94struct buffer_chain {
95 tll(struct buffer_private *) bufs;
96 struct wl_shm *shm;
97 size_t pix_instances;
98 bool scrollable;
100 pixman_format_code_t pixman_fmt;
101 enum wl_shm_format shm_format;
103 void (*release_cb)(struct buffer *buf, void *data);
104 void *cb_data;
105};
107static tll(struct buffer_private *) deferred;
109#undef MEASURE_SHM_ALLOCS
110#if defined(MEASURE_SHM_ALLOCS)
111static size_t max_alloced = 0;
112#endif
114void
115shm_set_max_pool_size(off_t _max_pool_size)
116{
117 max_pool_size = _max_pool_size;
118}
120void
121shm_set_min_stride_alignment(size_t _min_stride_alignment)
122{
123 min_stride_alignment = _min_stride_alignment;
124}
126static void
127buffer_destroy_dont_close(struct buffer *buf)
128{
129 if (buf->pix != NULL) {
130 for (size_t i = 0; i < buf->pix_instances; i++)
131 if (buf->pix[i] != NULL)
132 pixman_image_unref(buf->pix[i]);
133 }
135 if (buf->wl_buf != NULL)
136 wl_buffer_destroy(buf->wl_buf);
138 free(buf->pix);
139 buf->pix = NULL;
140 buf->wl_buf = NULL;
141 buf->data = NULL;
142}
144static void
145pool_unref(struct buffer_pool *pool)
146{
147 if (pool == NULL)
148 return;
150 xassert(pool->ref_count > 0);
151 pool->ref_count--;
153 if (pool->ref_count > 0)
154 return;
156 if (pool->real_mmapped != MAP_FAILED)
157 munmap(pool->real_mmapped, pool->mmap_size);
158 if (pool->wl_pool != NULL)
159 wl_shm_pool_destroy(pool->wl_pool);
160 if (pool->fd >= 0)
161 close(pool->fd);
163 pool->real_mmapped = MAP_FAILED;
164 pool->wl_pool = NULL;
165 pool->fd = -1;
166 free(pool);
167}
169static void
170buffer_destroy(struct buffer_private *buf)
171{
172 buffer_destroy_dont_close(&buf->public);
173 pool_unref(buf->pool);
174 buf->pool = NULL;
176 for (size_t i = 0; i < buf->public.pix_instances; i++)
177 pixman_region32_fini(&buf->public.dirty[i]);
178 free(buf->public.dirty);
179 free(buf);
180}
182static bool
183buffer_unref_no_remove_from_chain(struct buffer_private *buf)
184{
185 xassert(buf->ref_count > 0);
186 buf->ref_count--;
188 if (buf->ref_count > 0)
189 return false;
191 if (buf->busy)
192 tll_push_back(deferred, buf);
193 else
194 buffer_destroy(buf);
195 return true;
196}
198void
199shm_fini(void)
200{
201 LOG_DBG("deferred buffers: %zu", tll_length(deferred));
203 tll_foreach(deferred, it) {
204 buffer_destroy(it->item);
205 tll_remove(deferred, it);
206 }
208#if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS
209 LOG_INFO("max total allocations was: %zu MB", max_alloced / 1024 / 1024);
210#endif
211}
213static void
214buffer_release(void *data, struct wl_buffer *wl_buffer)
215{
216 struct buffer_private *buffer = data;
218 xassert(buffer->public.wl_buf == wl_buffer);
219 xassert(buffer->busy);
220 buffer->busy = false;
222 if (buffer->ref_count == 0) {
223 bool found = false;
224 tll_foreach(deferred, it) {
225 if (it->item == buffer) {
226 found = true;
227 tll_remove(deferred, it);
228 break;
229 }
230 }
232 buffer_destroy(buffer);
234 xassert(found);
235 if (!found)
236 LOG_WARN("deferred delete: buffer not on the 'deferred' list");
237 } else {
238 if (buffer->release_cb != NULL) {
239 buffer->release_cb(&buffer->public, buffer->cb_data);
240 }
241 }
242}
244static const struct wl_buffer_listener buffer_listener = {
245 .release = &buffer_release,
246};
248static size_t
249page_size(void)
250{
251 static size_t size = 0;
252 if (size == 0) {
253 long n = sysconf(_SC_PAGE_SIZE);
254 if (n <= 0) {
255 LOG_ERRNO("failed to get page size");
256 size = 4096;
257 } else {
258 size = (size_t)n;
259 }
260 }
261 xassert(size > 0);
262 return size;
263}
265static bool
266instantiate_offset(struct buffer_private *buf, off_t new_offset)
267{
268 xassert(buf->public.data == NULL);
269 xassert(buf->public.pix == NULL);
270 xassert(buf->public.wl_buf == NULL);
271 xassert(buf->pool != NULL);
273 const struct buffer_pool *pool = buf->pool;
275 void *mmapped = MAP_FAILED;
276 struct wl_buffer *wl_buf = NULL;
277 pixman_image_t **pix = xcalloc(buf->public.pix_instances, sizeof(pix[0]));
279 mmapped = (uint8_t *)pool->real_mmapped + new_offset;
281 wl_buf = wl_shm_pool_create_buffer(
282 pool->wl_pool, new_offset,
283 buf->public.width, buf->public.height, buf->public.stride,
284 buf->chain->shm_format);
286 if (wl_buf == NULL) {
287 LOG_ERR("failed to create SHM buffer");
288 goto err;
289 }
291 /* One pixman image for each worker thread (do we really need multiple?) */
292 for (size_t i = 0; i < buf->public.pix_instances; i++) {
293 pix[i] = pixman_image_create_bits_no_clear(
294 buf->chain->pixman_fmt,
295 buf->public.width, buf->public.height,
296 (uint32_t *)mmapped, buf->public.stride);
298 if (pix[i] == NULL) {
299 LOG_ERR("failed to create pixman image");
300 goto err;
301 }
302 }
304 buf->public.data = mmapped;
305 buf->public.wl_buf = wl_buf;
306 buf->public.pix = pix;
307 buf->offset = new_offset;
309 wl_buffer_add_listener(wl_buf, &buffer_listener, buf);
310 return true;
312err:
313 if (pix != NULL) {
314 for (size_t i = 0; i < buf->public.pix_instances; i++)
315 if (pix[i] != NULL)
316 pixman_image_unref(pix[i]);
317 }
318 free(pix);
319 if (wl_buf != NULL)
320 wl_buffer_destroy(wl_buf);
322 abort();
323 return false;
324}
326static void NOINLINE
327get_new_buffers(struct buffer_chain *chain, size_t count,
328 int widths[static count], int heights[static count],
329 struct buffer *bufs[static count], bool immediate_purge)
330{
331 xassert(count == 1 || !chain->scrollable);
332 /*
333 * No existing buffer available. Create a new one by:
334 *
335 * 1. open a memory backed "file" with memfd_create()
336 * 2. mmap() the memory file, to be used by the pixman image
337 * 3. create a wayland shm buffer for the same memory file
338 *
339 * The pixman image and the wayland buffer are now sharing memory.
340 */
342 int stride[count];
343 int sizes[count];
345 size_t total_size = 0;
346 for (size_t i = 0; i < count; i++) {
347 stride[i] = stride_for_format_and_width(
348 chain->pixman_fmt, widths[i]);
350 if (min_stride_alignment > 0) {
351 const size_t m = min_stride_alignment;
352 stride[i] = (stride[i] + m - 1) / m * m;
353 }
355 xassert(min_stride_alignment == 0 || stride[i] % min_stride_alignment == 0);
356 sizes[i] = stride[i] * heights[i];
357 total_size += sizes[i];
358 }
359 if (total_size == 0)
360 return;
362 int pool_fd = -1;
364 void *real_mmapped = MAP_FAILED;
365 struct wl_shm_pool *wl_pool = NULL;
366 struct buffer_pool *pool = NULL;
368 /* Backing memory for SHM */
369#if defined(MEMFD_CREATE)
370 /*
371 * Older kernels reject MFD_NOEXEC_SEAL with EINVAL. Try first
372 * *with* it, and if that fails, try again *without* it.
373 */
374 errno = 0;
375 pool_fd = memfd_create(
376 "foot-wayland-shm-buffer-pool",
377 MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
379 if (pool_fd < 0 && errno == EINVAL && MFD_NOEXEC_SEAL != 0) {
380 pool_fd = memfd_create(
381 "foot-wayland-shm-buffer-pool", MFD_CLOEXEC | MFD_ALLOW_SEALING);
382 }
384#elif defined(__FreeBSD__)
385 // memfd_create on FreeBSD 13 is SHM_ANON without sealing support
386 pool_fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600);
387#else
388 char name[] = "/tmp/foot-wayland-shm-buffer-pool-XXXXXX";
389 pool_fd = mkostemp(name, O_CLOEXEC);
390 unlink(name);
391#endif
392 if (pool_fd == -1) {
393 LOG_ERRNO("failed to create SHM backing memory file");
394 goto err;
395 }
397 const size_t page_sz = page_size();
399#if __SIZEOF_POINTER__ == 8
400 off_t offset = chain->scrollable && max_pool_size > 0
401 ? (max_pool_size / 4) & ~(page_sz - 1)
402 : 0;
403 off_t memfd_size = chain->scrollable && max_pool_size > 0
404 ? max_pool_size
405 : total_size;
406#else
407 off_t offset = 0;
408 off_t memfd_size = total_size;
409 (void)max_pool_size;
410#endif
412 /* Page align */
413 memfd_size = (memfd_size + page_sz - 1) & ~(page_sz - 1);
415 LOG_DBG("memfd-size: %lu, initial offset: %lu", memfd_size, offset);
417 if (ftruncate(pool_fd, memfd_size) == -1) {
418 LOG_ERRNO("failed to set size of SHM backing memory file");
419 goto err;
420 }
422 if (!can_punch_hole_initialized) {
423 can_punch_hole_initialized = true;
424#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)
425 can_punch_hole = fallocate(
426 pool_fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 1) == 0;
428 if (!can_punch_hole) {
429 LOG_WARN(
430 "fallocate(FALLOC_FL_PUNCH_HOLE) not "
431 "supported (%s): expect lower performance", strerror(errno));
432 }
433#else
434 /* This is mostly to make sure we skip the warning issued
435 * above */
436 can_punch_hole = false;
437#endif
438 }
440 if (chain->scrollable && !can_punch_hole) {
441 offset = 0;
442 memfd_size = total_size;
443 chain->scrollable = false;
445 /* Page align */
446 memfd_size = (memfd_size + page_sz - 1) & ~(page_sz - 1);
448 if (ftruncate(pool_fd, memfd_size) < 0) {
449 LOG_ERRNO("failed to set size of SHM backing memory file");
450 goto err;
451 }
452 }
454 real_mmapped = mmap(
455 NULL, memfd_size, PROT_READ | PROT_WRITE,
456 MAP_SHARED | MAP_UNINITIALIZED, pool_fd, 0);
458 if (real_mmapped == MAP_FAILED) {
459 LOG_ERRNO("failed to mmap SHM backing memory file");
460 goto err;
461 }
463#if defined(MEMFD_CREATE)
464 /* Seal file - we no longer allow any kind of resizing */
465 /* TODO: wayland mmaps(PROT_WRITE), for some unknown reason, hence we cannot use F_SEAL_FUTURE_WRITE */
466 if (fcntl(pool_fd, F_ADD_SEALS,
467 F_SEAL_GROW | F_SEAL_SHRINK | /*F_SEAL_FUTURE_WRITE |*/ F_SEAL_SEAL) < 0)
468 {
469 LOG_ERRNO("failed to seal SHM backing memory file");
470 /* This is not a fatal error */
471 }
472#endif
474 wl_pool = wl_shm_create_pool(chain->shm, pool_fd, memfd_size);
475 if (wl_pool == NULL) {
476 LOG_ERR("failed to create SHM pool");
477 goto err;
478 }
480 pool = xmalloc(sizeof(*pool));
481 if (pool == NULL) {
482 LOG_ERRNO("failed to allocate buffer pool");
483 goto err;
484 }
486 *pool = (struct buffer_pool){
487 .fd = pool_fd,
488 .wl_pool = wl_pool,
489 .real_mmapped = real_mmapped,
490 .mmap_size = memfd_size,
491 .ref_count = 0,
492 };
494 for (size_t i = 0; i < count; i++) {
495 if (sizes[i] == 0) {
496 bufs[i] = NULL;
497 continue;
498 }
500 /* Push to list of available buffers, but marked as 'busy' */
501 struct buffer_private *buf = xmalloc(sizeof(*buf));
502 *buf = (struct buffer_private){
503 .public = {
504 .width = widths[i],
505 .height = heights[i],
506 .stride = stride[i],
507 .pix_instances = chain->pix_instances,
508 .age = 1234, /* Force a full repaint */
509 },
510 .chain = chain,
511 .ref_count = immediate_purge ? 0 : 1,
512 .busy = true,
513 .pool = pool,
514 .offset = 0,
515 .size = sizes[i],
516 .scrollable = chain->scrollable,
517 .release_cb = chain->release_cb,
518 .cb_data = chain->cb_data,
519 };
521 if (!instantiate_offset(buf, offset)) {
522 free(buf);
523 goto err;
524 }
526 if (immediate_purge)
527 tll_push_front(deferred, buf);
528 else
529 tll_push_front(chain->bufs, buf);
531 buf->public.dirty = xmalloc(
532 chain->pix_instances * sizeof(buf->public.dirty[0]));
534 for (size_t j = 0; j < chain->pix_instances; j++)
535 pixman_region32_init(&buf->public.dirty[j]);
537 pool->ref_count++;
538 offset += buf->size;
539 bufs[i] = &buf->public;
540 }
542#if defined(MEASURE_SHM_ALLOCS) && MEASURE_SHM_ALLOCS
543 {
544 size_t currently_alloced = 0;
545 tll_foreach(buffers, it)
546 currently_alloced += it->item.size;
547 if (currently_alloced > max_alloced)
548 max_alloced = currently_alloced;
549 }
550#endif
552 if (!(bufs[0] && shm_can_scroll(bufs[0]))) {
553 /* We only need to keep the pool FD open if we're going to SHM
554 * scroll it */
555 close(pool_fd);
556 pool->fd = -1;
557 }
559 return;
561err:
562 pool_unref(pool);
563 if (wl_pool != NULL)
564 wl_shm_pool_destroy(wl_pool);
565 if (real_mmapped != MAP_FAILED)
566 munmap(real_mmapped, memfd_size);
567 if (pool_fd != -1)
568 close(pool_fd);
570 /* We don't handle this */
571 abort();
572}
574void
575shm_did_not_use_buf(struct buffer *_buf)
576{
577 struct buffer_private *buf = (struct buffer_private *)_buf;
578 buf->busy = false;
579}
581void
582shm_get_many(struct buffer_chain *chain, size_t count,
583 int widths[static count], int heights[static count],
584 struct buffer *bufs[static count])
585{
586 get_new_buffers(chain, count, widths, heights, bufs, true);
587}
589struct buffer *
590shm_get_buffer(struct buffer_chain *chain, int width, int height)
591{
592 LOG_DBG(
593 "chain=%p: looking for a reusable %dx%d buffer "
594 "among %zu potential buffers",
595 (void *)chain, width, height, tll_length(chain->bufs));
597 struct buffer_private *cached = NULL;
598 tll_foreach(chain->bufs, it) {
599 struct buffer_private *buf = it->item;
601 if (buf->public.width != width || buf->public.height != height) {
602 LOG_DBG("purging mismatching buffer %p", (void *)buf);
603 if (buffer_unref_no_remove_from_chain(buf))
604 tll_remove(chain->bufs, it);
605 continue;
606 }
608 if (buf->busy)
609 buf->public.age++;
610 else
611#if FORCED_DOUBLE_BUFFERING
612 if (buf->public.age == 0)
613 buf->public.age++;
614 else
615#endif
616 {
617 if (cached == NULL) {
618 cached = buf;
619 } else {
620 /* We have multiple buffers eligible for
621 * reuse. Pick the "youngest" one, and mark the
622 * other one for purging */
623 if (buf->public.age < cached->public.age) {
624 shm_unref(&cached->public);
625 cached = buf;
626 } else {
627 /*
628 * TODO: I think we _can_ use shm_unref()
629 * here...
630 *
631 * shm_unref() may remove 'it', but that
632 * should be safe; "our" tll_foreach() already
633 * holds the next pointer.
634 */
635 if (buffer_unref_no_remove_from_chain(buf))
636 tll_remove(chain->bufs, it);
637 }
638 }
639 }
640 }
642 if (cached != NULL) {
643 LOG_DBG("reusing buffer %p from cache", (void *)cached);
644 cached->busy = true;
645 for (size_t i = 0; i < cached->public.pix_instances; i++)
646 pixman_region32_clear(&cached->public.dirty[i]);
647 xassert(cached->public.pix_instances == chain->pix_instances);
648 return &cached->public;
649 }
651 struct buffer *ret;
652 get_new_buffers(chain, 1, &width, &height, &ret, false);
653 return ret;
654}
656bool
657shm_can_scroll(const struct buffer *_buf)
658{
659#if __SIZEOF_POINTER__ == 8
660 const struct buffer_private *buf = (const struct buffer_private *)_buf;
661 return can_punch_hole && max_pool_size > 0 && buf->scrollable;
662#else
663 /* Not enough virtual address space in 32-bit */
664 return false;
665#endif
666}
668#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)
669static bool
670wrap_buffer(struct buffer_private *buf, off_t new_offset)
671{
672 struct buffer_pool *pool = buf->pool;
673 xassert(pool->ref_count == 1);
675 /* We don't allow overlapping offsets */
676 off_t UNUSED diff = new_offset < buf->offset
677 ? buf->offset - new_offset
678 : new_offset - buf->offset;
679 xassert(diff > buf->size);
681 memcpy((uint8_t *)pool->real_mmapped + new_offset,
682 buf->public.data,
683 buf->size);
685 off_t trim_ofs, trim_len;
686 if (new_offset > buf->offset) {
687 /* Trim everything *before* the new offset */
688 trim_ofs = 0;
689 trim_len = new_offset;
690 } else {
691 /* Trim everything *after* the new buffer location */
692 trim_ofs = new_offset + buf->size;
693 trim_len = pool->mmap_size - trim_ofs;
694 }
696 if (fallocate(
697 pool->fd,
698 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
699 trim_ofs, trim_len) < 0)
700 {
701 LOG_ERRNO("failed to trim SHM backing memory file");
702 return false;
703 }
705 /* Re-instantiate pixman+wl_buffer+raw pointersw */
706 buffer_destroy_dont_close(&buf->public);
707 return instantiate_offset(buf, new_offset);
708}
710static bool
711shm_scroll_forward(struct buffer_private *buf, int rows,
712 int top_margin, int top_keep_rows,
713 int bottom_margin, int bottom_keep_rows)
714{
715 struct buffer_pool *pool = buf->pool;
717 xassert(can_punch_hole);
718 xassert(buf->busy);
719 xassert(buf->public.pix != NULL);
720 xassert(buf->public.wl_buf != NULL);
721 xassert(pool != NULL);
722 xassert(pool->ref_count == 1);
723 xassert(pool->fd >= 0);
725 LOG_DBG("scrolling %d rows (%d bytes)", rows, rows * buf->public.stride);
727 const off_t diff = rows * buf->public.stride;
728 xassert(rows > 0);
729 xassert(diff < buf->size);
731 if (buf->offset + diff + buf->size > max_pool_size) {
732 LOG_DBG("memfd offset wrap around");
733 if (!wrap_buffer(buf, 0))
734 goto err;
735 }
737 off_t new_offset = buf->offset + diff;
738 xassert(new_offset > buf->offset);
739 xassert(new_offset + buf->size <= max_pool_size);
741#if TIME_SCROLL
742 struct timespec tot;
743 struct timespec time1;
744 clock_gettime(CLOCK_MONOTONIC, &time1);
746 struct timespec time2 = time1;
747#endif
749 if (top_keep_rows > 0) {
750 /* Copy current 'top' region to its new location */
751 const int stride = buf->public.stride;
752 uint8_t *base = buf->public.data;
754 memmove(
755 base + (top_margin + rows) * stride,
756 base + (top_margin + 0) * stride,
757 top_keep_rows * stride);
759#if TIME_SCROLL
760 clock_gettime(CLOCK_MONOTONIC, &time2);
761 timespec_sub(&time2, &time1, &tot);
762 LOG_INFO("memmove (top region): %lds %ldns",
763 (long)tot.tv_sec, tot.tv_nsec);
764#endif
765 }
767 /* Destroy old objects (they point to the old offset) */
768 buffer_destroy_dont_close(&buf->public);
770 /* Free unused memory - everything up until the new offset */
771 const off_t trim_ofs = 0;
772 const off_t trim_len = new_offset;
774 if (fallocate(
775 pool->fd,
776 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
777 trim_ofs, trim_len) < 0)
778 {
779 LOG_ERRNO("failed to trim SHM backing memory file");
780 goto err;
781 }
783#if TIME_SCROLL
784 struct timespec time3;
785 clock_gettime(CLOCK_MONOTONIC, &time3);
786 timespec_sub(&time3, &time2, &tot);
787 LOG_INFO("PUNCH HOLE: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);
788#endif
790 /* Re-instantiate pixman+wl_buffer+raw pointersw */
791 bool ret = instantiate_offset(buf, new_offset);
793#if TIME_SCROLL
794 struct timespec time4;
795 clock_gettime(CLOCK_MONOTONIC, &time4);
796 timespec_sub(&time4, &time3, &tot);
797 LOG_INFO("instantiate offset: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);
798#endif
800 if (ret && bottom_keep_rows > 0) {
801 /* Copy 'bottom' region to its new location */
802 const size_t size = buf->size;
803 const int stride = buf->public.stride;
804 uint8_t *base = buf->public.data;
806 memmove(
807 base + size - (bottom_margin + bottom_keep_rows) * stride,
808 base + size - (bottom_margin + rows + bottom_keep_rows) * stride,
809 bottom_keep_rows * stride);
811#if TIME_SCROLL
812 struct timespec time5;
813 clock_gettime(CLOCK_MONOTONIC, &time5);
815 timespec_sub(&time5, &time4, &tot);
816 LOG_INFO("memmove (bottom region): %lds %ldns",
817 (long)tot.tv_sec, tot.tv_nsec);
818#endif
819 }
821 return ret;
823err:
824 abort();
825 return false;
826}
828static bool
829shm_scroll_reverse(struct buffer_private *buf, int rows,
830 int top_margin, int top_keep_rows,
831 int bottom_margin, int bottom_keep_rows)
832{
833 xassert(rows > 0);
835 struct buffer_pool *pool = buf->pool;
836 xassert(pool->ref_count == 1);
838 const off_t diff = rows * buf->public.stride;
839 if (diff > buf->offset) {
840 LOG_DBG("memfd offset reverse wrap-around");
841 if (!wrap_buffer(buf, (max_pool_size - buf->size) & ~(page_size() - 1)))
842 goto err;
843 }
845 off_t new_offset = buf->offset - diff;
846 xassert(new_offset < buf->offset);
847 xassert(new_offset <= max_pool_size);
849#if TIME_SCROLL
850 struct timespec time0;
851 clock_gettime(CLOCK_MONOTONIC, &time0);
853 struct timespec tot;
854 struct timespec time1 = time0;
855#endif
857 if (bottom_keep_rows > 0) {
858 /* Copy 'bottom' region to its new location */
859 const size_t size = buf->size;
860 const int stride = buf->public.stride;
861 uint8_t *base = buf->public.data;
863 memmove(
864 base + size - (bottom_margin + rows + bottom_keep_rows) * stride,
865 base + size - (bottom_margin + bottom_keep_rows) * stride,
866 bottom_keep_rows * stride);
868#if TIME_SCROLL
869 clock_gettime(CLOCK_MONOTONIC, &time1);
870 timespec_sub(&time1, &time0, &tot);
871 LOG_INFO("memmove (bottom region): %lds %ldns",
872 (long)tot.tv_sec, tot.tv_nsec);
873#endif
874 }
876 /* Destroy old objects (they point to the old offset) */
877 buffer_destroy_dont_close(&buf->public);
879 /* Free unused memory - everything after the relocated buffer */
880 const off_t trim_ofs = new_offset + buf->size;
881 const off_t trim_len = pool->mmap_size - trim_ofs;
883 if (fallocate(
884 pool->fd,
885 FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
886 trim_ofs, trim_len) < 0)
887 {
888 LOG_ERRNO("failed to trim SHM backing memory");
889 goto err;
890 }
891#if TIME_SCROLL
892 struct timespec time2;
893 clock_gettime(CLOCK_MONOTONIC, &time2);
894 timespec_sub(&time2, &time1, &tot);
895 LOG_INFO("fallocate: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);
896#endif
898 /* Re-instantiate pixman+wl_buffer+raw pointers */
899 bool ret = instantiate_offset(buf, new_offset);
901#if TIME_SCROLL
902 struct timespec time3;
903 clock_gettime(CLOCK_MONOTONIC, &time3);
904 timespec_sub(&time3, &time2, &tot);
905 LOG_INFO("instantiate offset: %lds %ldns", (long)tot.tv_sec, tot.tv_nsec);
906#endif
908 if (ret && top_keep_rows > 0) {
909 /* Copy current 'top' region to its new location */
910 const int stride = buf->public.stride;
911 uint8_t *base = buf->public.data;
913 memmove(
914 base + (top_margin + 0) * stride,
915 base + (top_margin + rows) * stride,
916 top_keep_rows * stride);
918#if TIME_SCROLL
919 struct timespec time4;
920 clock_gettime(CLOCK_MONOTONIC, &time4);
921 timespec_sub(&time4, &time3, &tot);
922 LOG_INFO("memmove (top region): %lds %ldns",
923 (long)tot.tv_sec, tot.tv_nsec);
924#endif
925 }
927 return ret;
929err:
930 abort();
931 return false;
932}
933#endif /* FALLOC_FL_PUNCH_HOLE */
935bool
936shm_scroll(struct buffer *_buf, int rows,
937 int top_margin, int top_keep_rows,
938 int bottom_margin, int bottom_keep_rows)
939{
940#if __SIZEOF_POINTER__ == 8 && defined(FALLOC_FL_PUNCH_HOLE)
941 if (!shm_can_scroll(_buf))
942 return false;
944 struct buffer_private *buf = (struct buffer_private *)_buf;
946 xassert(rows != 0);
947 return rows > 0
948 ? shm_scroll_forward(buf, rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows)
949 : shm_scroll_reverse(buf, -rows, top_margin, top_keep_rows, bottom_margin, bottom_keep_rows);
950#else
951 return false;
952#endif
953}
955void
956shm_purge(struct buffer_chain *chain)
957{
958 LOG_DBG("chain: %p: purging all buffers", (void *)chain);
960 /* Purge old buffers associated with this cookie */
961 tll_foreach(chain->bufs, it) {
962 if (buffer_unref_no_remove_from_chain(it->item))
963 tll_remove(chain->bufs, it);
964 }
965}
967void
968shm_addref(struct buffer *_buf)
969{
970 struct buffer_private *buf = (struct buffer_private *)_buf;
971 buf->ref_count++;
972}
974void
975shm_unref(struct buffer *_buf)
976{
977 if (_buf == NULL)
978 return;
980 struct buffer_private *buf = (struct buffer_private *)_buf;
981 struct buffer_chain *chain = buf->chain;
983 tll_foreach(chain->bufs, it) {
984 if (it->item != buf)
985 continue;
987 if (buffer_unref_no_remove_from_chain(buf))
988 tll_remove(chain->bufs, it);
989 break;
990 }
991}
993struct buffer_chain *
994shm_chain_new(struct wayland *wayl, bool scrollable, size_t pix_instances,
995 enum shm_bit_depth desired_bit_depth,
996 void (*release_cb)(struct buffer *buf, void *data), void *cb_data)
997{
998 pixman_format_code_t pixman_fmt = PIXMAN_a8r8g8b8;
999 enum wl_shm_format shm_fmt = WL_SHM_FORMAT_ARGB8888;
1001 static bool have_logged = false;
1002 static bool have_logged_10_fallback = false;
1004#if defined(HAVE_PIXMAN_RGBA_16)
1005 static bool have_logged_16_fallback = false;
1007 if (desired_bit_depth == SHM_BITS_16) {
1008 if (wayl->shm_have_abgr161616) {
1009 pixman_fmt = PIXMAN_a16b16g16r16;
1010 shm_fmt = WL_SHM_FORMAT_ABGR16161616;
1012 if (!have_logged) {
1013 have_logged = true;
1014 LOG_INFO("using 16-bit BGR surfaces");
1015 }
1016 } else {
1017 if (!have_logged_16_fallback) {
1018 have_logged_16_fallback = true;
1020 LOG_WARN(
1021 "16-bit surfaces requested, but compositor does not "
1022 "implement ABGR161616+XBGR161616");
1023 }
1024 }
1025 }
1026#endif
1028 if (desired_bit_depth >= SHM_BITS_10 && pixman_fmt == PIXMAN_a8r8g8b8) {
1029 if (wayl->shm_have_argb2101010) {
1030 pixman_fmt = PIXMAN_a2r10g10b10;
1031 shm_fmt = WL_SHM_FORMAT_ARGB2101010;
1033 if (!have_logged) {
1034 have_logged = true;
1035 LOG_INFO("using 10-bit RGB surfaces");
1036 }
1037 }
1039 else if (wayl->shm_have_abgr2101010) {
1040 pixman_fmt = PIXMAN_a2b10g10r10;
1041 shm_fmt = WL_SHM_FORMAT_ABGR2101010;
1043 if (!have_logged) {
1044 have_logged = true;
1045 LOG_INFO("using 10-bit BGR surfaces");
1046 }
1047 }
1049 else {
1050 if (!have_logged_10_fallback) {
1051 have_logged_10_fallback = true;
1053 LOG_WARN(
1054 "10-bit surfaces requested, but compositor does not "
1055 "implement ARGB2101010+XRGB2101010, or "
1056 "ABGR2101010+XBGR2101010");
1057 }
1058 }
1059 } else {
1060 if (!have_logged) {
1061 have_logged = true;
1062 LOG_INFO("using 8-bit RGB surfaces");
1063 }
1064 }
1066 struct buffer_chain *chain = xmalloc(sizeof(*chain));
1067 *chain = (struct buffer_chain){
1068 .bufs = tll_init(),
1069 .shm = wayl->shm,
1070 .pix_instances = pix_instances,
1071 .scrollable = scrollable,
1073 .pixman_fmt = pixman_fmt,
1074 .shm_format = shm_fmt,
1076 .release_cb = release_cb,
1077 .cb_data = cb_data,
1078 };
1079 return chain;
1080}
1082void
1083shm_chain_free(struct buffer_chain *chain)
1084{
1085 if (chain == NULL)
1086 return;
1088 shm_purge(chain);
1090 if (tll_length(chain->bufs) > 0) {
1091 BUG("chain=%p: there are buffers remaining; "
1092 "is there a missing call to shm_unref()?", (void *)chain);
1093 }
1095 free(chain);
1096}
1098enum shm_bit_depth
1099shm_chain_bit_depth(const struct buffer_chain *chain)
1100{
1101 const pixman_format_code_t fmt = chain->pixman_fmt;
1103 return fmt == PIXMAN_a8r8g8b8
1104 ? SHM_BITS_8
1105#if defined(HAVE_PIXMAN_RGBA_16)
1106 : fmt == PIXMAN_a16b16g16r16
1107 ? SHM_BITS_16
1108#endif
1109 : SHM_BITS_10;
1110}
Diagnostics6 git calls · 538 ms
CallStatusSizeTime0538 ms
discover200178 B22 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=21 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB · hit22 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=21 git.response.bytes=10322 git.response.cache=HIT git.response.duration_ms=22 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchtree:0 · deepen 102009.3 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetchtree:0 · deepen 12001.1 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=1154 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=1154 git.pkt.payload.max=1054 git.sideband.band1.bytes=1054 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1054 git.pack.trailer=f9afe4130b8e969286b1fa36663bbb8999db09eaOpen pack map
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetch2007.0 KiB512 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=512 git.response.bytes=7217 git.response.cache=MISS git.response.duration_ms=512 git.fetch.wants=66d725b21b76164f27656c4be53657067e903c70 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=7217 git.pkt.payload.max=7190 git.sideband.band1.bytes=7190 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e15f7008a8725b
- Request
- beec50bb…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format