1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
/* mapped-ring-buffer.c
*
* Copyright 2020-2025 Christian Hergert <chergert@redhat.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Subject to the terms and conditions of this license, each copyright holder
* and contributor hereby grants to those receiving rights under this license
* a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
* irrevocable (except for failure to satisfy the conditions of this license)
* patent license to make, have made, use, offer to sell, sell, import, and
* otherwise transfer this software, where such license applies only to those
* patent claims, already acquired or hereafter acquired, licensable by such
* copyright holder or contributor that are necessarily infringed by:
*
* (a) their Contribution(s) (the licensed copyrights of copyright holders
* and non-copyrightable additions of contributors, in source or binary
* form) alone; or
*
* (b) combination of their Contribution(s) with the work of authorship to
* which such Contribution(s) was added by such copyright holder or
* contributor, if, at the time the Contribution is added, such addition
* causes such combination to be necessarily infringed. The patent license
* shall not apply to any other combinations which include the
* Contribution.
*
* Except as expressly stated above, no rights or licenses from any copyright
* holder or contributor is granted under this license, whether expressly, by
* implication, estoppel or otherwise.
*
* DISCLAIMER
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
#include "config.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include "sysprof-capture-util-private.h"
#include "sysprof-macros-internal.h"
#include "sysprof-platform.h"
#include "mapped-ring-buffer.h"
#define DEFAULT_N_PAGES 63
#define BUFFER_MAX_SIZE ((UINT32_MAX/2)-_sysprof_getpagesize())
enum {
MODE_READER = 1,
MODE_WRITER = 2,
MODE_READWRITE = 3,
};
/*
* MappedRingHeader is the header of the first page of the
* buffer. We use the whole buffer so that we can double map
* the body of the buffer.
*/
typedef struct _MappedRingHeader
{
uint32_t head;
uint32_t tail;
uint32_t offset;
uint32_t size;
} MappedRingHeader;
SYSPROF_STATIC_ASSERT (sizeof (MappedRingHeader) == 16, "MappedRingHeader changed size");
/*
* MappedRingBuffer is used to wrap both the reader and writer
* mapping structures.
*/
struct _MappedRingBuffer
{
volatile int ref_count;
int mode;
int fd;
void *map;
size_t body_size;
size_t page_size;
unsigned has_failed : 1;
};
static inline MappedRingHeader *
get_header (MappedRingBuffer *self)
{
return (MappedRingHeader *)self->map;
}
static inline void *
get_body_at_pos (MappedRingBuffer *self,
size_t pos)
{
assert (pos < (self->body_size + self->body_size));
return (uint8_t *)self->map + self->page_size + pos;
}
static void *
map_head_and_body_twice (int fd,
size_t head_size,
size_t body_size)
{
void *map;
void *second;
/* First we map FD to the total size we want so that we can be
* certain the OS will give us a contiguous mapping for our buffers
* even though we can't dereference a portion of the mapping yet.
*
* We'll overwrite the secondary mapping in a moment to deal with
* wraparound for the ring buffer.
*/
map = mmap (NULL,
head_size + body_size + body_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fd,
0);
if (map == MAP_FAILED)
return NULL;
/* At this point, we have [HEAD|BODY|BUSERR] mapped. But we want to map
* the body again over what would currently cause a BusError. That way
* when we need to wraparound we don't need to copy anything, we just
* have to check in mapped_ring_buffer_allocate() that the size does not
* step over what would be the real reader position.
*
* By mmap()'ing over the old region, the previous region is automatically
* munmap()'d for us.
*/
second = mmap ((uint8_t *)map + head_size + body_size,
body_size,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_FIXED,
fd,
head_size);
if (second == MAP_FAILED)
{
munmap (map, head_size + body_size + body_size);
return NULL;
}
assert (second == (void *)((uint8_t *)map + head_size + body_size));
return map;
}
/**
* mapped_ring_buffer_new_reader:
* @buffer_size: the size of the buffer, which must be page-aligned
*
* Creates a new #MappedRingBuffer.
*
* This should be called by the process reading the buffer. It should
* then pass the FD of the buffer to another process or thread to
* advance the ring buffer by writing data.
*
* The other process or thread should create a new #MappedRingBuffer
* using mapped_ring_buffer_new_writer() with the FD retrieved from
* the reader using mapped_ring_buffer_get_fd(). If crossing a process
* boundary, you probably also want to dup() the FD and set O_CLOEXEC.
*
* @buffer_size must be a multiple of the system's page size which can
* be retrieved using sysprof_getpagesize().
*
* Returns: (transfer full): a #MappedRingBuffer
*/
MappedRingBuffer *
mapped_ring_buffer_new_reader (size_t buffer_size)
{
MappedRingBuffer *self;
MappedRingHeader *header;
size_t page_size;
void *map;
int fd;
assert ((buffer_size % _sysprof_getpagesize ()) == 0);
assert (buffer_size < BUFFER_MAX_SIZE);
page_size = _sysprof_getpagesize ();
/* Add 1 page for coordination header */
if (buffer_size == 0)
buffer_size = page_size * DEFAULT_N_PAGES;
buffer_size += page_size;
/* Create our memfd (or tmpfs) for writing */
if ((fd = sysprof_memfd_create ("[sysprof-ring-buffer]")) == -1)
return NULL;
/* Size our memfd to reserve space */
if (ftruncate (fd, buffer_size) != 0)
{
close (fd);
return NULL;
}
/* Map ring buffer [HEAD|BODY|BODY] */
if (!(map = map_head_and_body_twice (fd, page_size, buffer_size - page_size)))
{
close (fd);
return NULL;
}
/* Setup initial header */
header = map;
header->head = 0;
header->tail = 0;
header->offset = page_size;
header->size = buffer_size - page_size;
self = sysprof_malloc0 (sizeof (MappedRingBuffer));
if (self == NULL)
return NULL;
self->ref_count = 1;
self->mode = MODE_READER;
self->body_size = buffer_size - page_size;
self->fd = fd;
self->map = map;
self->page_size = page_size;
return sysprof_steal_pointer (&self);
}
MappedRingBuffer *
mapped_ring_buffer_new_readwrite (size_t buffer_size)
{
MappedRingBuffer *self;
if ((self = mapped_ring_buffer_new_reader (buffer_size)))
self->mode = MODE_READWRITE;
return self;
}
/**
* mapped_ring_buffer_new_writer:
* @fd: a FD to map
*
* Creates a new #MappedRingBuffer using a copy of @fd.
*
* The caller may close(fd) after calling this function regardless of
* success creating the #MappedRingBuffer.
*
* Returns: (transfer full) (nullable): a new #MappedRingBuffer
*/
MappedRingBuffer *
mapped_ring_buffer_new_writer (int fd)
{
MappedRingBuffer *self;
MappedRingHeader *header;
ssize_t buffer_size;
size_t page_size;
void *map;
assert (fd > -1);
page_size = _sysprof_getpagesize ();
/* Make our own copy of the FD */
if ((fd = dup (fd)) < 0)
{
fprintf (stderr, "Failed to dup() fd, cannot continue\n");
return NULL;
}
/* Seek to end to get buffer size */
if ((buffer_size = lseek (fd, 0, SEEK_END)) < 0)
{
fprintf (stderr, "Failed to seek to end of file. Cannot determine buffer size.\n");
return NULL;
}
/* Ensure non-zero sized buffer */
if (buffer_size < (page_size + page_size))
{
fprintf (stderr, "Buffer is too small, cannot continue.\n");
return NULL;
}
/* Make sure it is less than our max size */
if ((buffer_size - page_size) > BUFFER_MAX_SIZE)
{
fprintf (stderr, "Buffer is too large, cannot continue.\n");
return NULL;
}
/* Ensure we have page-aligned buffer */
if ((buffer_size % page_size) != 0)
{
fprintf (stderr, "Invalid buffer size, not page aligned.\n");
return NULL;
}
/* Map ring buffer [HEAD|BODY|BODY] */
if (!(map = map_head_and_body_twice (fd, page_size, buffer_size - page_size)))
{
close (fd);
return NULL;
}
/* Validate we got proper data in header */
header = map;
if (header->offset != page_size ||
header->size != (buffer_size - page_size))
{
munmap (map, page_size + ((buffer_size - page_size) * 2));
close (fd);
return NULL;
}
self = sysprof_malloc0 (sizeof (MappedRingBuffer));
if (self == NULL)
{
munmap (map, page_size + ((buffer_size - page_size) * 2));
close (fd);
return NULL;
}
self->ref_count = 1;
self->mode = MODE_WRITER;
self->fd = fd;
self->body_size = buffer_size - page_size;
self->map = map;
self->page_size = page_size;
return sysprof_steal_pointer (&self);
}
static void
mapped_ring_buffer_finalize (MappedRingBuffer *self)
{
if (self->map != NULL)
{
munmap (self->map, self->page_size + self->body_size + self->body_size);
self->map = NULL;
}
if (self->fd != -1)
{
close (self->fd);
self->fd = -1;
}
free (self);
}
void
mapped_ring_buffer_unref (MappedRingBuffer *self)
{
assert (self != NULL);
assert (self->ref_count > 0);
if (__atomic_fetch_sub (&self->ref_count, 1, __ATOMIC_SEQ_CST) == 1)
mapped_ring_buffer_finalize (self);
}
MappedRingBuffer *
mapped_ring_buffer_ref (MappedRingBuffer *self)
{
assert (self != NULL);
assert (self->ref_count > 0);
__atomic_fetch_add (&self->ref_count, 1, __ATOMIC_SEQ_CST);
return self;
}
int
mapped_ring_buffer_get_fd (MappedRingBuffer *self)
{
assert (self != NULL);
return self->fd;
}
/**
* mapped_ring_buffer_allocate:
* @self: a #MappedRingBuffer
*
* Ensures that @length bytes are available at the next position in
* the ring buffer and returns a pointer to the beginning of that zone.
*
* If the reader has not read enough bytes to allow @length to be added
* then a mark will be added or incremented notifying the peer of how
* many records they have lost and %NULL is returned.
*
* You must always check for %NULL before dereferencing the result of
* this function as space may not be immediately available.
*
* This only ensure that the space is available for you to write. To
* notify the peer that the zone is ready for reading you must call
* mapped_ring_buffer_advance() with the number of bytes to advance.
* This is useful in case you need to allocate more memory than you
* might need up-front but commit a smaller amount.
*
* Returns: (nullable): a pointer to data of at least @length bytes
* or %NULL if there is not enough space.
*/
void *
mapped_ring_buffer_allocate (MappedRingBuffer *self,
size_t length)
{
MappedRingHeader *header;
uint32_t headpos;
uint32_t tailpos;
assert (self != NULL);
assert (self->mode & MODE_WRITER);
assert (length > 0);
assert (length < self->body_size);
assert ((length & 0x7) == 0);
for (unsigned i = 0; i < 1000; i++)
{
header = get_header (self);
__atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST);
__atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST);
/* We need to check that there is enough space for @length at the
* current position in the write buffer. We cannot fully catch up
* to head, we must be at least one byte short of it. If we do not
* have enough space, then return NULL.
*
* When we have finished writing our frame data, we will push the tail
* forward with an atomic write.
*/
if (tailpos == headpos)
return get_body_at_pos (self, tailpos);
if (headpos < tailpos)
headpos += self->body_size;
if (tailpos + length < headpos)
return get_body_at_pos (self, tailpos);
if (self->has_failed)
break;
usleep (1000); /* 1 msec */
}
self->has_failed = true;
return NULL;
}
/**
* mapped_ring_buffer_advance:
* @self: a #MappedRingBuffer
* @length: a 8-byte aligned length
*
* Advances the ring buffer @length bytes forward. @length must be
* 8-byte aligned so that the buffer may avoid memcpy() to read
* framing data.
*
* This should only be called by a writer created with
* mapped_ring_buffer_new_writer().
*
* Call this after writing your data into the buffer using
* mapped_ring_buffer_allocate().
*
* It is a programming error to call this with a value greater
* than was called to mapped_ring_buffer_allocate().
*/
void
mapped_ring_buffer_advance (MappedRingBuffer *self,
size_t length)
{
MappedRingHeader *header;
uint32_t tail;
assert (self != NULL);
assert (self->mode & MODE_WRITER);
assert (length > 0);
assert (length < self->body_size);
assert ((length & 0x7) == 0);
header = get_header (self);
tail = header->tail;
/* Calculate the new tail position */
tail = tail + length;
if (tail >= self->body_size)
tail -= self->body_size;
/* We have already checked that we could advance the buffer when the
* application called mapped_ring_buffer_allocate(), so at this point
* we just update the position as the only way the head could have
* moved is forward.
*/
__atomic_store (&header->tail, &tail, __ATOMIC_SEQ_CST);
}
/**
* mapped_ring_buffer_drain:
* @self: a #MappedRingBuffer
* @callback: (scope call): a callback to execute for each frame
* @user_data: closure data for @callback
*
* Drains the buffer by calling @callback for each frame.
*
* This should only be called by a reader created with
* mapped_ring_buffer_new_reader().
*
* Returns: %TRUE if the buffer was drained, %FALSE if @callback prematurely
* returned while draining.
*/
bool
mapped_ring_buffer_drain (MappedRingBuffer *self,
MappedRingBufferCallback callback,
void *user_data)
{
MappedRingHeader *header;
uint32_t headpos;
uint32_t tailpos;
assert (self != NULL);
assert (self->mode & MODE_READER);
assert (callback != NULL);
header = get_header (self);
__atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST);
__atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST);
assert (headpos < self->body_size);
assert (tailpos < self->body_size);
if (headpos == tailpos)
return true;
/* If head needs to wrap around to get to tail, we can just rely on
* our double mapping instead actually manually wrapping/copying data.
*/
if (tailpos < headpos)
tailpos += self->body_size;
assert (headpos < tailpos);
while (headpos < tailpos)
{
const void *data = get_body_at_pos (self, headpos);
size_t len = tailpos - headpos;
uint32_t new_headpos;
if (!callback (data, &len, user_data))
return false;
if (len > (tailpos - headpos))
return false;
headpos += len;
if (headpos >= self->body_size)
new_headpos = headpos - self->body_size;
else
new_headpos = headpos;
__atomic_store (&header->head, &new_headpos, __ATOMIC_SEQ_CST);
}
return true;
}
/**
* mapped_ring_buffer_is_empty:
* @self: a #MappedRingBuffer
*
* Checks whether the ring buffer is currently empty.
*
* This should only be called by a reader created with
* mapped_ring_buffer_new_reader().
*
* Returns: %TRUE if the buffer is empty, %FALSE otherwise
*/
bool
mapped_ring_buffer_is_empty (MappedRingBuffer *self)
{
MappedRingHeader *header;
uint32_t headpos, tailpos;
header = get_header (self);
__atomic_load (&header->head, &headpos, __ATOMIC_SEQ_CST);
__atomic_load (&header->tail, &tailpos, __ATOMIC_SEQ_CST);
return headpos == tailpos;
}
/**
* mapped_ring_buffer_clear:
* @self: a #MappedRingBuffer
*
* Resets the head and tail positions back to 0.
*
* This function is only safe to call when you control both the reader
* and writer sides with mapped_ring_buffer_new_readwrite(), or are in
* control of when each side reads or writes.
*/
void
mapped_ring_buffer_clear (MappedRingBuffer *self)
{
MappedRingHeader *header;
assert (self != NULL);
header = get_header (self);
header->head = 0;
header->tail = 0;
}
|