diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index d055e586..a8b7b496 100755 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -10,3 +10,4 @@ ef787ba565f7c4e6bb1ba5f9259300254e95e266 141621deb9177fb2fcd74d7e14965da0e267beab 67843e4a329e0b49308ee13eedbad0683bdf7221 2f5e5f2fdc872e39a157bb7086c3bdde28fce239 +6a2d754739d1ed1a806ba2528bd2c37c198000a3 diff --git a/include/vamos-buffers/core/arbiter.h b/include/vamos-buffers/core/arbiter.h index 1e3a8fb4..788b35de 100644 --- a/include/vamos-buffers/core/arbiter.h +++ b/include/vamos-buffers/core/arbiter.h @@ -8,73 +8,75 @@ * at the same time is not thread-safe. ************************************************/ -#ifndef SHAMON_ARBITER_H_ -#define SHAMON_ARBITER_H_ +#ifndef VAMOS_ARBITER_H_ +#define VAMOS_ARBITER_H_ #include #include #include "stream.h" -typedef struct _shm_arbiter_buffer shm_arbiter_buffer; -typedef struct _shm_event shm_event; +typedef struct _vms_arbiter_buffer vms_arbiter_buffer; +typedef struct _vms_event vms_event; -void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, +void vms_arbiter_buffer_init(vms_arbiter_buffer *buffer, vms_stream *stream, size_t out_event_size, size_t capacity); -shm_arbiter_buffer *shm_arbiter_buffer_create(shm_stream *stream, +vms_arbiter_buffer *vms_arbiter_buffer_create(vms_stream *stream, size_t out_event_size, size_t capacity); -size_t shm_arbiter_buffer_set_drop_space_threshold(shm_arbiter_buffer *buffer, +size_t vms_arbiter_buffer_set_drop_space_threshold(vms_arbiter_buffer *buffer, size_t thr); -void shm_arbiter_buffer_free(shm_arbiter_buffer *buffer); -void shm_arbiter_buffer_destroy(shm_arbiter_buffer *buffer); -void shm_arbiter_buffer_set_active(shm_arbiter_buffer *buffer, bool val); -size_t shm_arbiter_buffer_elem_size(shm_arbiter_buffer *q); -shm_stream *shm_arbiter_buffer_stream(shm_arbiter_buffer *buffer); -bool shm_arbiter_buffer_active(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_size(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_capacity(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_free_space(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_sizeof(void); +void vms_arbiter_buffer_free(vms_arbiter_buffer *buffer); +void vms_arbiter_buffer_destroy(vms_arbiter_buffer *buffer); +void vms_arbiter_buffer_set_active(vms_arbiter_buffer *buffer, bool val); +size_t vms_arbiter_buffer_elem_size(vms_arbiter_buffer *q); +vms_stream *vms_arbiter_buffer_stream(vms_arbiter_buffer *buffer); +bool vms_arbiter_buffer_active(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_size(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_capacity(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_free_space(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_sizeof(void); /* writer's API */ -void shm_arbiter_buffer_push(shm_arbiter_buffer *q, const void *elem, +void vms_arbiter_buffer_push(vms_arbiter_buffer *q, const void *elem, size_t size); /* cannot be mixed with push */ -void *shm_arbiter_buffer_write_ptr(shm_arbiter_buffer *q); -void shm_arbiter_buffer_write_finish(shm_arbiter_buffer *q); -void shm_arbiter_buffer_get_str(shm_arbiter_buffer *q, size_t elem); +void *vms_arbiter_buffer_write_ptr(vms_arbiter_buffer *q); +void vms_arbiter_buffer_write_finish(vms_arbiter_buffer *q); +void vms_arbiter_buffer_get_str(vms_arbiter_buffer *q, size_t elem); /* reader's API */ /* multiple threads can use top and peek if none of them uses drop/pop * at the time */ -shm_event *shm_arbiter_buffer_top(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_peek(shm_arbiter_buffer *buffer, size_t n, +vms_event *vms_arbiter_buffer_top(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_peek(vms_arbiter_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2); /* peek 1 event */ -size_t shm_arbiter_buffer_peek1(shm_arbiter_buffer *buffer, void **data); -size_t shm_arbiter_buffer_drop(shm_arbiter_buffer *buffer, size_t n); -size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, - shm_eventid id); -bool shm_arbiter_buffer_pop(shm_arbiter_buffer *q, void *buff); +size_t vms_arbiter_buffer_peek1(vms_arbiter_buffer *buffer, void **data); +size_t vms_arbiter_buffer_drop(vms_arbiter_buffer *buffer, size_t n); +size_t vms_arbiter_buffer_drop_older_than(vms_arbiter_buffer *buffer, + vms_eventid id); +bool vms_arbiter_buffer_pop(vms_arbiter_buffer *q, void *buff); -void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer); +void *vms_stream_fetch(vms_stream *stream); +void *vms_stream_fetch_dropping(vms_stream *stream, vms_arbiter_buffer *buffer); -void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, - shm_stream_filter_fn filter); +void *vms_stream_fetch_dropping_filter(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter); -bool shm_arbiter_buffer_is_done(shm_arbiter_buffer *buffer); +bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_dropped_num(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_dropped_times(shm_arbiter_buffer *buffer); -size_t shm_arbiter_buffer_written_num(shm_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_dropped_num(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_dropped_times(vms_arbiter_buffer *buffer); +size_t vms_arbiter_buffer_written_num(vms_arbiter_buffer *buffer); -void shm_arbiter_buffer_notify_dropped(shm_arbiter_buffer *buffer, +void vms_arbiter_buffer_notify_dropped(vms_arbiter_buffer *buffer, uint64_t begin_id, uint64_t end_id); -void shm_arbiter_buffer_dump_stats(shm_arbiter_buffer *buffer); -#endif /* SHAMON_ARBITER_H_ */ +void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer); +#endif /* VAMOS_ARBITER_H_ */ diff --git a/include/vamos-buffers/core/event.h b/include/vamos-buffers/core/event.h index ac5e92d0..035c8f6a 100644 --- a/include/vamos-buffers/core/event.h +++ b/include/vamos-buffers/core/event.h @@ -1,25 +1,38 @@ -#ifndef SHAMON_EVENT_H_ -#define SHAMON_EVENT_H_ +#ifndef VMS_EVENT_H_ +#define VMS_EVENT_H_ + +#ifdef __cplusplus +extern "C" { +#endif #include #include #include -typedef uint64_t shm_kind; -typedef uint64_t shm_eventid; -typedef struct _shm_stream shm_stream; +typedef uint64_t vms_kind; +typedef uint64_t vms_eventid; +typedef struct _vms_stream vms_stream; // TODO: make this opaque? but how to do the inheritance, then? Via // pointers to an interface? -typedef struct _shm_event { - shm_kind kind; - shm_eventid id; -} shm_event; +typedef struct _vms_event { + vms_kind kind; + vms_eventid id; + +#ifdef __cplusplus + _vms_event() {} + _vms_event(vms_kind kind) : kind(kind) {} + _vms_event(vms_kind kind, vms_eventid id) : kind(kind), id(id) {} + + vms_kind get_kind() const { return kind; } + vms_eventid get_id() const { return id; } +#endif +} vms_event; -typedef struct _shm_event_default_hole { - shm_event base; +typedef struct _vms_event_default_hole { + vms_event base; size_t n; /* number of dropped events */ -} shm_event_default_hole; +} vms_event_default_hole; /* Must be called before using event API. * It is called from shamon_create */ @@ -28,12 +41,26 @@ void initialize_events(void) __attribute__((deprecated)); void deinitialize_events(void) __attribute__((deprecated)); // EVENTS -shm_eventid shm_event_id(shm_event *event); -shm_kind shm_event_kind(shm_event *event); +#define VMS_EVENT_HOLE_KIND 1 +#define VMS_EVENT_DONE_KIND 2 +#define VMS_EVENT_LAST_SPECIAL_KIND 9 + +vms_eventid vms_event_id(const vms_event *event); +vms_kind vms_event_kind(const vms_event *event); + +// Sometimes we want to generate the event step-by-step +// and then we may need this fun. +void vms_event_set_id(vms_event *event, vms_eventid id); // DROP EVENT -bool shm_event_is_hole(shm_event *); -shm_kind shm_get_hole_kind(void); -shm_kind shm_get_last_special_kind(void); +bool vms_event_is_hole(const vms_event *); +bool vms_event_is_done(const vms_event *); +vms_kind vms_event_get_hole_kind(void); +vms_kind vms_event_get_done_kind(void); +vms_kind vms_event_get_last_special_kind(void); + +#ifdef __cplusplus +} +#endif -#endif // SHAMON_EVENT_H_ +#endif // VMS_EVENT_H diff --git a/include/vamos-buffers/core/list-embedded.h b/include/vamos-buffers/core/list-embedded.h index 3d503409..ed7038fd 100644 --- a/include/vamos-buffers/core/list-embedded.h +++ b/include/vamos-buffers/core/list-embedded.h @@ -1,46 +1,46 @@ -#ifndef SHAMON_LIST_EMBEDDED_H_ -#define SHAMON_LIST_EMBEDDED_H_ +#ifndef VAMOS_LIST_EMBEDDED_H_ +#define VAMOS_LIST_EMBEDDED_H_ #include /* circular embedded doubly-linked list */ -typedef struct _shm_list_embedded { - struct _shm_list_embedded *next; - struct _shm_list_embedded *prev; -} shm_list_embedded; - -void shm_list_embedded_init(shm_list_embedded *list); -void shm_list_embedded_insert_after(shm_list_embedded *list, - shm_list_embedded *elm); -void shm_list_embedded_insert_list(shm_list_embedded *list, - shm_list_embedded *other); -void shm_list_embedded_remove(shm_list_embedded *elm); -size_t shm_list_embedded_size(const shm_list_embedded *list); -_Bool shm_list_embedded_empty(const shm_list_embedded *list); - -#define shm_list_embedded_object_of(ptr, tyobj, member) \ +typedef struct _vms_list_embedded { + struct _vms_list_embedded *next; + struct _vms_list_embedded *prev; +} vms_list_embedded; + +void vms_list_embedded_init(vms_list_embedded *list); +void vms_list_embedded_insert_after(vms_list_embedded *list, + vms_list_embedded *elm); +void vms_list_embedded_insert_list(vms_list_embedded *list, + vms_list_embedded *other); +void vms_list_embedded_remove(vms_list_embedded *elm); +size_t vms_list_embedded_size(const vms_list_embedded *list); +_Bool vms_list_embedded_empty(const vms_list_embedded *list); + +#define vms_list_embedded_object_of(ptr, tyobj, member) \ (__typeof__(tyobj))((char *)(ptr)-offsetof(__typeof__(*tyobj), member)) -#define shm_list_embedded_foreach(pos, head, member) \ - for (pos = shm_list_embedded_object_of((head)->next, pos, member); \ +#define vms_list_embedded_foreach(pos, head, member) \ + for (pos = vms_list_embedded_object_of((head)->next, pos, member); \ &pos->member != (head); \ - pos = shm_list_embedded_object_of(pos->member.next, pos, member)) + pos = vms_list_embedded_object_of(pos->member.next, pos, member)) -#define shm_list_embedded_foreach_safe(pos, tmp, head, member) \ - for (pos = shm_list_embedded_object_of((head)->next, pos, member), \ - tmp = shm_list_embedded_object_of((pos)->member.next, tmp, member); \ +#define vms_list_embedded_foreach_safe(pos, tmp, head, member) \ + for (pos = vms_list_embedded_object_of((head)->next, pos, member), \ + tmp = vms_list_embedded_object_of((pos)->member.next, tmp, member); \ &pos->member != (head); pos = tmp, \ - tmp = shm_list_embedded_object_of(pos->member.next, tmp, member)) + tmp = vms_list_embedded_object_of(pos->member.next, tmp, member)) -#define shm_list_embedded_foreach_reverse(pos, head, member) \ - for (pos = shm_list_embedded_object_of((head)->prev, pos, member); \ +#define vms_list_embedded_foreach_reverse(pos, head, member) \ + for (pos = vms_list_embedded_object_of((head)->prev, pos, member); \ &pos->member != (head); \ - pos = shm_list_embedded_object_of(pos->member.prev, pos, member)) + pos = vms_list_embedded_object_of(pos->member.prev, pos, member)) -#define shm_list_embedded_foreach_reverse_safe(pos, tmp, head, member) \ - for (pos = shm_list_embedded_object_of((head)->prev, pos, member), \ - tmp = shm_list_embedded_object_of((pos)->member.prev, tmp, member); \ +#define vms_list_embedded_foreach_reverse_safe(pos, tmp, head, member) \ + for (pos = vms_list_embedded_object_of((head)->prev, pos, member), \ + tmp = vms_list_embedded_object_of((pos)->member.prev, tmp, member); \ &pos->member != (head); pos = tmp, \ - tmp = shm_list_embedded_object_of(pos->member.prev, tmp, member)) + tmp = vms_list_embedded_object_of(pos->member.prev, tmp, member)) -#endif /* SHAMON_LIST_EMBEDDED_H_ */ +#endif /* VAMOS_LIST_EMBEDDED_H_ */ diff --git a/include/vamos-buffers/core/list.h b/include/vamos-buffers/core/list.h index d6a162a1..02e4a47f 100644 --- a/include/vamos-buffers/core/list.h +++ b/include/vamos-buffers/core/list.h @@ -1,36 +1,36 @@ -#ifndef SHAMON_LIST_H_ -#define SHAMON_LIST_H_ +#ifndef VAMOS_LIST_H_ +#define VAMOS_LIST_H_ #include /* non-circular doubly-linked-list */ -typedef struct _shm_list_elem { - struct _shm_list_elem *next, *prev; +typedef struct _vms_list_elem { + struct _vms_list_elem *next, *prev; void *data; -} shm_list_elem; +} vms_list_elem; -typedef struct _shm_list { - shm_list_elem *first; - shm_list_elem *last; +typedef struct _vms_list { + vms_list_elem *first; + vms_list_elem *last; size_t size; -} shm_list; +} vms_list; -typedef void(shm_list_elem_destroy_fn)(void *); +typedef void(vms_list_elem_destroy_fn)(void *); -void shm_list_init(shm_list *list); -void shm_list_destroy(shm_list *list, shm_list_elem_destroy_fn destroy); +void vms_list_init(vms_list *list); +void vms_list_destroy(vms_list *list, vms_list_elem_destroy_fn destroy); -size_t shm_list_prepend(shm_list *list, void *elem); -size_t shm_list_append(shm_list *list, void *elem); -size_t shm_list_append_elem(shm_list *list, shm_list_elem *elem); -size_t shm_list_insert_after(shm_list *list, shm_list_elem *elem, void *data); -size_t shm_list_insert_before(shm_list *list, shm_list_elem *elem, void *data); -size_t shm_list_insert_elem_after(shm_list *list, shm_list_elem *elem, - shm_list_elem *new); -size_t shm_list_remove(shm_list *list, shm_list_elem *elem); -size_t shm_list_size(shm_list *list); +size_t vms_list_prepend(vms_list *list, void *elem); +size_t vms_list_append(vms_list *list, void *elem); +size_t vms_list_append_elem(vms_list *list, vms_list_elem *elem); +size_t vms_list_insert_after(vms_list *list, vms_list_elem *elem, void *data); +size_t vms_list_insert_before(vms_list *list, vms_list_elem *elem, void *data); +size_t vms_list_insert_elem_after(vms_list *list, vms_list_elem *elem, + vms_list_elem *new); +size_t vms_list_remove(vms_list *list, vms_list_elem *elem); +size_t vms_list_size(vms_list *list); -shm_list_elem *shm_list_first(shm_list *list); -shm_list_elem *shm_list_last(shm_list *list); +vms_list_elem *vms_list_first(vms_list *list); +vms_list_elem *vms_list_last(vms_list *list); -#endif /* SHAMON_LIST_H_ */ +#endif /* VAMOS_LIST_H_ */ diff --git a/include/vamos-buffers/core/monitor.h b/include/vamos-buffers/core/monitor.h index a67fd08e..8657c4fb 100644 --- a/include/vamos-buffers/core/monitor.h +++ b/include/vamos-buffers/core/monitor.h @@ -2,65 +2,65 @@ * Implementation of the buffer for the monitor. ************************************************/ -#ifndef SHAMON_MONITOR_H_ -#define SHAMON_MONITOR_H_ +#ifndef VAMOS_MONITOR_H_ +#define VAMOS_MONITOR_H_ #include #include #include -typedef struct _shm_monitor_buffer shm_monitor_buffer; -typedef struct _shm_event shm_event; +typedef struct _vms_monitor_buffer vms_monitor_buffer; +typedef struct _vms_event vms_event; -void shm_monitor_buffer_init(shm_monitor_buffer *buffer, size_t event_size, +void vms_monitor_buffer_init(vms_monitor_buffer *buffer, size_t event_size, size_t capacity); -shm_monitor_buffer *shm_monitor_buffer_create(size_t event_size, +vms_monitor_buffer *vms_monitor_buffer_create(size_t event_size, size_t capacity); -void shm_monitor_set_finished(shm_monitor_buffer *buffer); +void vms_monitor_set_finished(vms_monitor_buffer *buffer); -void shm_monitor_buffer_free(shm_monitor_buffer *buffer); -void shm_monitor_buffer_destroy(shm_monitor_buffer *buffer); +void vms_monitor_buffer_free(vms_monitor_buffer *buffer); +void vms_monitor_buffer_destroy(vms_monitor_buffer *buffer); -size_t shm_monitor_buffer_elem_size(shm_monitor_buffer *q); -bool shm_monitor_buffer_active(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_size(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_capacity(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_free_space(shm_monitor_buffer *buffer); -// size_t shm_monitor_buffer_sizeof(void); +size_t vms_monitor_buffer_elem_size(vms_monitor_buffer *q); +bool vms_monitor_buffer_active(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_size(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_capacity(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_free_space(vms_monitor_buffer *buffer); +// size_t vms_monitor_buffer_sizeof(void); /* writer's API */ /* Get write pointer to the buffer, block until space is available */ -void *shm_monitor_buffer_write_ptr(shm_monitor_buffer *q); +void *vms_monitor_buffer_write_ptr(vms_monitor_buffer *q); /* Get write pointer to the buffer or null if no space is available */ -void *shm_monitor_buffer_write_ptr_or_null(shm_monitor_buffer *q); -void shm_monitor_buffer_write_finish(shm_monitor_buffer *q); -/*void shm_monitor_buffer_get_str(shm_monitor_buffer *q, size_t elem); */ +void *vms_monitor_buffer_write_ptr_or_null(vms_monitor_buffer *q); +void vms_monitor_buffer_write_finish(vms_monitor_buffer *q); +/*void vms_monitor_buffer_get_str(vms_monitor_buffer *q, size_t elem); */ /* reader's API */ -void *fetch_arbiter_stream(shm_monitor_buffer *buffer); -void shm_monitor_buffer_consume(shm_monitor_buffer *buffer, size_t n); +void *fetch_arbiter_stream(vms_monitor_buffer *buffer); +void vms_monitor_buffer_consume(vms_monitor_buffer *buffer, size_t n); /* multiple threads can use top and peek if none of them uses drop/pop * at the time -shm_event *shm_monitor_buffer_top(shm_monitor_buffer *buffer); -size_t shm_monitor_buffer_peek(shm_monitor_buffer *buffer, size_t n, +vms_event *vms_monitor_buffer_top(vms_monitor_buffer *buffer); +size_t vms_monitor_buffer_peek(vms_monitor_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2); */ /* peek 1 event -size_t shm_monitor_buffer_peek1(shm_monitor_buffer *buffer, void **data); -size_t shm_monitor_buffer_drop(shm_monitor_buffer *buffer, size_t n); -bool shm_monitor_buffer_pop(shm_monitor_buffer *q, void *buff); +size_t vms_monitor_buffer_peek1(vms_monitor_buffer *buffer, void **data); +size_t vms_monitor_buffer_drop(vms_monitor_buffer *buffer, size_t n); +bool vms_monitor_buffer_pop(vms_monitor_buffer *q, void *buff); -void *stream_fetch(shm_stream *stream, shm_monitor_buffer *buffer); +void *stream_fetch(vms_stream *stream, vms_monitor_buffer *buffer); -void *stream_filter_fetch(shm_stream *stream, shm_monitor_buffer *buffer, - shm_stream_filter_fn filter); +void *stream_filter_fetch(vms_stream *stream, vms_monitor_buffer *buffer, + vms_stream_filter_fn filter); */ /* -void shm_monitor_buffer_notify_dropped(shm_monitor_buffer *buffer, +void vms_monitor_buffer_notify_dropped(vms_monitor_buffer *buffer, uint64_t begin_id, uint64_t end_id); */ -#endif /* SHAMON_MONITOR_H_ */ +#endif /* VAMOS_MONITOR_H_ */ diff --git a/include/vamos-buffers/core/par_queue.h b/include/vamos-buffers/core/par_queue.h index 78b71325..4fac2c23 100644 --- a/include/vamos-buffers/core/par_queue.h +++ b/include/vamos-buffers/core/par_queue.h @@ -1,51 +1,51 @@ -#ifndef SHAMON_PARALLEL_QUEUE_H -#define SHAMON_PARALLEL_QUEUE_H +#ifndef VAMOS_PARALLEL_QUEUE_H +#define VAMOS_PARALLEL_QUEUE_H #include #include #include "spsc_ringbuf.h" -typedef struct _shm_event shm_event; +typedef struct _vms_event vms_event; /** - * shm_par_queue is a single-producer single-consumer (SPSC) lock-free + * vms_par_queue is a single-producer single-consumer (SPSC) lock-free * concurrent queue. */ -typedef struct _shm_par_queue { - shm_spsc_ringbuf ringbuf; +typedef struct _vms_par_queue { + vms_spsc_ringbuf ringbuf; size_t elem_size; size_t capacity; unsigned char *data; char __padding[CACHELINE_SIZE - 2 * sizeof(size_t) - sizeof(unsigned char *)]; -} shm_par_queue; - -void shm_par_queue_init(shm_par_queue *q, size_t capacity, size_t elem_size); -void shm_par_queue_destroy(shm_par_queue *q); -bool shm_par_queue_push(shm_par_queue *q, const void *elem, size_t size); -bool shm_par_queue_pop(shm_par_queue *q, void *buff); -void shm_par_queue_drop(shm_par_queue *q, size_t k); -size_t shm_par_queue_size(shm_par_queue *q); -size_t shm_par_queue_elem_size(shm_par_queue *q); -size_t shm_par_queue_capacity(shm_par_queue *q); -size_t shm_par_queue_free_num(shm_par_queue *q); -shm_event *shm_par_queue_top(shm_par_queue *q); -size_t shm_par_queue_peek(shm_par_queue *q, size_t n, void **ptr1, size_t *len1, +} vms_par_queue; + +void vms_par_queue_init(vms_par_queue *q, size_t capacity, size_t elem_size); +void vms_par_queue_destroy(vms_par_queue *q); +bool vms_par_queue_push(vms_par_queue *q, const void *elem, size_t size); +bool vms_par_queue_pop(vms_par_queue *q, void *buff); +void vms_par_queue_drop(vms_par_queue *q, size_t k); +size_t vms_par_queue_size(vms_par_queue *q); +size_t vms_par_queue_elem_size(vms_par_queue *q); +size_t vms_par_queue_capacity(vms_par_queue *q); +size_t vms_par_queue_free_num(vms_par_queue *q); +vms_event *vms_par_queue_top(vms_par_queue *q); +size_t vms_par_queue_peek(vms_par_queue *q, size_t n, void **ptr1, size_t *len1, void **ptr2, size_t *len2); /* peek 1 event */ -size_t shm_par_queue_peek1(shm_par_queue *q, void **data); +size_t vms_par_queue_peek1(vms_par_queue *q, void **data); /* peek at a given element. k = 0 is the first elem (top), k = 1 the second, ... */ -shm_event *shm_par_queue_peek_at(shm_par_queue *q, size_t k); +vms_event *vms_par_queue_peek_at(vms_par_queue *q, size_t k); /* peek at a given element or the last possible readable element * if there is not enough elements. 'k' is set to the index (starting from 0) * of the peeked element. Returns NULL only if the queue is empty. */ -shm_event *shm_par_queue_peek_atmost_at(shm_par_queue *q, size_t *k); +vms_event *vms_par_queue_peek_atmost_at(vms_par_queue *q, size_t *k); -void *shm_par_queue_write_ptr(shm_par_queue *q); -void shm_par_queue_write_finish(shm_par_queue *q); +void *vms_par_queue_write_ptr(vms_par_queue *q); +void vms_par_queue_write_finish(vms_par_queue *q); -#endif /* SHAMON_PARALLEL_QUEUE_H */ +#endif /* VAMOS_PARALLEL_QUEUE_H */ diff --git a/include/vamos-buffers/core/queue_spsc.h b/include/vamos-buffers/core/queue_spsc.h index b98985f7..40f5fe5c 100644 --- a/include/vamos-buffers/core/queue_spsc.h +++ b/include/vamos-buffers/core/queue_spsc.h @@ -1,16 +1,16 @@ -#ifndef SHAMON_QUEUE_SPSC_H -#define SHAMON_QUEUE_SPSC_H +#ifndef VAMOS_QUEUE_SPSC_H +#define VAMOS_QUEUE_SPSC_H #include #include -typedef struct _shm_event shm_event; +typedef struct _vms_event vms_event; /** - * shm_queue_spsc is a single-producer single-consumer (SPSC) lock-free + * vms_queue_spsc is a single-producer single-consumer (SPSC) lock-free * concurrent queue. */ -typedef struct _shm_queue_spsc { +typedef struct _vms_queue_spsc { size_t capacity; /* modifications of this variable are going to be atomic, * but that is the all atomicity that we need! */ @@ -21,52 +21,52 @@ typedef struct _shm_queue_spsc { size_t partial_head; size_t writing_k_elems; #endif -} shm_queue_spsc; +} vms_queue_spsc; -void shm_queue_spsc_init(shm_queue_spsc *q, size_t capacity); -void shm_queue_spsc_destroy(shm_queue_spsc *q); +void vms_queue_spsc_init(vms_queue_spsc *q, size_t capacity); +void vms_queue_spsc_destroy(vms_queue_spsc *q); /* ask to write `k` elements, returns the offset where to start writing and saves to `k` the actual number of elements that can be written continuously from that offset (i.e., if *k == 0, then the queue is empty) -size_t shm_queue_spsc_write_offsets(shm_queue_spsc *q, size_t k, +size_t vms_queue_spsc_write_offsets(vms_queue_spsc *q, size_t k, size_t *len, size_t *wrapped_len); */ /* get next offset for writing the next element. \return true if * the write is possible, or false (which meanst that the queue is full) */ -bool shm_queue_spsc_write_offset(shm_queue_spsc *q, size_t *offset); +bool vms_queue_spsc_write_offset(vms_queue_spsc *q, size_t *offset); /* notify queue that a write was finished and that `written_num` * offsets were used */ -void shm_queue_spsc_writes_finish(shm_queue_spsc *q, size_t written_num); +void vms_queue_spsc_writes_finish(vms_queue_spsc *q, size_t written_num); /* notify queue that a single write was finished -- must match a single call of - * shm_queue_spcs_write_offset (calls to shm_queue_spcs_write_offsets must be - * "finished" by shm_queue_spcs_writes_finish */ -void shm_queue_spsc_write_finish(shm_queue_spsc *q); + * vms_queue_spcs_write_offset (calls to vms_queue_spcs_write_offsets must be + * "finished" by vms_queue_spcs_writes_finish */ +void vms_queue_spsc_write_finish(vms_queue_spsc *q); -size_t shm_queue_spsc_consume(shm_queue_spsc *q, size_t k); +size_t vms_queue_spsc_consume(vms_queue_spsc *q, size_t k); -size_t shm_queue_spsc_capacity(shm_queue_spsc *q); -size_t shm_queue_spsc_size(shm_queue_spsc *q); -size_t shm_queue_spsc_free_num(shm_queue_spsc *q); +size_t vms_queue_spsc_capacity(vms_queue_spsc *q); +size_t vms_queue_spsc_size(vms_queue_spsc *q); +size_t vms_queue_spsc_free_num(vms_queue_spsc *q); /* returns the number of elements in the queue. Note that it is not safe to access the whole range of offsets from [offset, offset+retval] as there can be a wrap-around. Either count with the possible wrap-around, - or use shm_queue_spsc_peek() when reading more than one offset. */ -size_t shm_queue_spsc_read_offset(shm_queue_spsc *q, size_t *offset); + or use vms_queue_spsc_peek() when reading more than one offset. */ +size_t vms_queue_spsc_read_offset(vms_queue_spsc *q, size_t *offset); -size_t shm_queue_spsc_peek(shm_queue_spsc *q, size_t n, size_t *off1, +size_t vms_queue_spsc_peek(vms_queue_spsc *q, size_t n, size_t *off1, size_t *len1, size_t *off2, size_t *len2); /* peek at a given element. k = 0 is the first elem (top), k = 1 the second, ... */ -size_t shm_queue_spsc_peek_at(shm_queue_spsc *q, size_t k); +size_t vms_queue_spsc_peek_at(vms_queue_spsc *q, size_t k); /* peek at a given element or the last possible readable element * if there is not enough elements. 'k' is set to the index (starting from 0) * of the peeked element. Returns the number of elements in the queue. */ -size_t shm_queue_spsc_peek_atmost_at(shm_queue_spsc *q, size_t *k, +size_t vms_queue_spsc_peek_atmost_at(vms_queue_spsc *q, size_t *k, size_t *offset); -#endif /* SHAMON_QUEUE_SPSC_H */ +#endif /* VAMOS_QUEUE_SPSC_H */ diff --git a/include/vamos-buffers/core/shamon.h b/include/vamos-buffers/core/shamon.h index a0e940a1..3d38d5e7 100644 --- a/include/vamos-buffers/core/shamon.h +++ b/include/vamos-buffers/core/shamon.h @@ -1,16 +1,16 @@ -#ifndef SHAMON_H_ -#define SHAMON_H_ +#ifndef VAMOS_H_ +#define VAMOS_H_ #include #include typedef struct _shamon shamon; -typedef struct _shm_event shm_event; -typedef struct _shm_stream shm_stream; -typedef struct _shm_vector shm_vector; +typedef struct _vms_event vms_event; +typedef struct _vms_stream vms_stream; +typedef struct _vms_vector vms_vector; -typedef shm_event *(*shamon_process_events_fn)(shm_vector *buffers, void *data, - shm_stream **); +typedef vms_event *(*shamon_process_events_fn)(vms_vector *buffers, void *data, + vms_stream **); shamon *shamon_create(shamon_process_events_fn process_events, void *process_events_data); @@ -19,10 +19,10 @@ bool shamon_is_ready(shamon *); /* for error handling only... */ void shamon_detach(shamon *shmn); -void shamon_add_stream(shamon *shmn, shm_stream *stream, +void shamon_add_stream(shamon *shmn, vms_stream *stream, size_t buffer_capacity); -shm_event *shamon_get_next_ev(shamon *, shm_stream **); -shm_vector *shamon_get_buffers(shamon *); -shm_stream **shamon_get_streams(shamon *, size_t *); +vms_event *shamon_get_next_ev(shamon *, vms_stream **); +vms_vector *shamon_get_buffers(shamon *); +vms_stream **shamon_get_streams(shamon *, size_t *); -#endif // SHAMON_H_ +#endif // VAMOS_H_ diff --git a/include/vamos-buffers/core/shm_string.h b/include/vamos-buffers/core/shm_string.h deleted file mode 100644 index b28a2b5c..00000000 --- a/include/vamos-buffers/core/shm_string.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef SHAMON_STRING_H_ -#define SHAMON_STRING_H_ - -#include - -#include "shm_string-macro.h" - -typedef struct _shm_string_ref { - size_t size; - const char *data; -} shm_string_ref; - -typedef struct _shm_string { - STRING(data); -} shm_string; - -/* Either the user can manipulate the string manually, - * with the macros from shm_string-macro.h - * or with the functions below. The macros are used - on the data of the string, e.g., - STRING_RESIZE(s->data). Note that macros are not applicable - always (they just expand to code that may be syntactically wrong - in some contexts). */ -void shm_string_init(shm_string *s); -void shm_string_destroy(shm_string *s); -void shm_string_clear(shm_string *s); -size_t shm_string_append(shm_string *s, char c); -char *shm_string_extend(shm_string *s); -size_t shm_string_pop(shm_string *s); -size_t shm_string_size(shm_string *s); -char *shm_string_at_ptr(shm_string *s, ssize_t idx); -char shm_string_at(shm_string *s, ssize_t idx); -void shm_string_swap(shm_string *s, shm_string *with); -void shm_string_grow(shm_string *s, size_t size); - -#endif /* SHAMON_VECTOR_H_ */ diff --git a/include/vamos-buffers/core/signatures.h b/include/vamos-buffers/core/signatures.h index 343e127e..30a90f54 100644 --- a/include/vamos-buffers/core/signatures.h +++ b/include/vamos-buffers/core/signatures.h @@ -1,12 +1,12 @@ -#ifndef SHAMON_SIGNATURES_H_ -#define SHAMON_SIGNATURES_H_ +#ifndef VAMOS_SIGNATURES_H_ +#define VAMOS_SIGNATURES_H_ #include #include /* * c = (unsigned) char - * s = (unsigned) short + * h = (unsigned) short * i = (unsigned) int * l = (unsigned) long * f = float diff --git a/include/vamos-buffers/core/source.h b/include/vamos-buffers/core/source.h index 746102cf..c598cb87 100644 --- a/include/vamos-buffers/core/source.h +++ b/include/vamos-buffers/core/source.h @@ -1,42 +1,40 @@ -#ifndef SHAMON_SOURCE_H_ -#define SHAMON_SOURCE_H_ +#ifndef VAMOS_SOURCE_H_ +#define VAMOS_SOURCE_H_ #include "event.h" -struct event_record { +struct vms_event_record { char name[64]; - shm_kind kind; + vms_kind kind; size_t size; unsigned char signature[32]; }; -struct source_control { +struct vms_source_control { size_t size; /* size of this structure (it's variable) */ - struct event_record events[]; + struct vms_event_record events[]; }; -size_t source_control_get_records_num(struct source_control *sc); +size_t vms_source_control_get_records_num(struct vms_source_control *sc); -struct source_control *source_control_define(size_t ev_nums, ...); -struct source_control *source_control_define_pairwise(size_t ev_nums, - const char *names[], - const char *signatures[]); +struct vms_source_control *vms_source_control_define(size_t ev_nums, ...); +struct vms_source_control *vms_source_control_define_pairwise( + size_t ev_nums, const char *names[], const char *signatures[]); /* string is in the format "event-name:signature,event-name:signature,..." */ -struct source_control *source_control_define_str(const char *str); +struct vms_source_control *vms_source_control_define_str(const char *str); -size_t source_control_max_event_size(struct source_control *control); +size_t vms_source_control_max_event_size(struct vms_source_control *control); -struct event_record *source_control_get_event(struct source_control *control, - const char *name); +struct vms_event_record *vms_source_control_get_event( + struct vms_source_control *control, const char *name); -struct source_control *source_control_allocate(size_t ev_nums); +struct vms_source_control *vms_source_control_allocate(size_t ev_nums); -_Bool source_control_define_pairwise_partially(struct source_control *control, - size_t from, size_t ev_nums, - const char *names[], - const char *signatures[]); +_Bool vms_source_control_define_pairwise_partially( + struct vms_source_control *control, size_t from, size_t ev_nums, + const char *names[], const char *signatures[]); -_Bool source_control_define_partially(struct source_control *control, - size_t from, size_t ev_nums, ...); -#endif /* SHAMON_SOURCE_H_ */ +_Bool vms_source_control_define_partially(struct vms_source_control *control, + size_t from, size_t ev_nums, ...); +#endif /* VAMOS_SOURCE_H_ */ diff --git a/include/vamos-buffers/core/spsc_ringbuf.h b/include/vamos-buffers/core/spsc_ringbuf.h index 83e0e9d1..ed4f933e 100644 --- a/include/vamos-buffers/core/spsc_ringbuf.h +++ b/include/vamos-buffers/core/spsc_ringbuf.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SPSC_RINGBUF_H -#define SHAMON_SPSC_RINGBUF_H +#ifndef VAMOS_SPSC_RINGBUF_H +#define VAMOS_SPSC_RINGBUF_H #include #include @@ -10,7 +10,7 @@ /** * Single-producer single-consumer (SPSC) lock-free concurrent ring-buffer. */ -typedef struct _shm_spsc_ringbuf { +typedef struct _vms_spsc_ringbuf { /* capacity needs to be also aligned as it is read by both reader and writer */ /* TODO: cache capacity locally (do a proxy to acces the ringbuf? @@ -35,30 +35,30 @@ typedef struct _shm_spsc_ringbuf { size_t n; } write_in_progress; #endif -} shm_spsc_ringbuf; +} vms_spsc_ringbuf; -void shm_spsc_ringbuf_init(shm_spsc_ringbuf *b, size_t capacity); -void shm_spsc_ringbuf_consume(shm_spsc_ringbuf *b, size_t n); -size_t shm_spsc_ringbuf_consume_upto(shm_spsc_ringbuf *b, size_t n); -size_t shm_spsc_ringbuf_write_off(shm_spsc_ringbuf *b, size_t *n, +void vms_spsc_ringbuf_init(vms_spsc_ringbuf *b, size_t capacity); +void vms_spsc_ringbuf_consume(vms_spsc_ringbuf *b, size_t n); +size_t vms_spsc_ringbuf_consume_upto(vms_spsc_ringbuf *b, size_t n); +size_t vms_spsc_ringbuf_write_off(vms_spsc_ringbuf *b, size_t *n, size_t *wrap_n); -size_t shm_spsc_ringbuf_write_off_nowrap(shm_spsc_ringbuf *b, size_t *n); -size_t shm_spsc_ringbuf_acquire(shm_spsc_ringbuf *b, size_t *n, size_t *wrap); -size_t shm_spsc_ringbuf_acquire_nowrap(shm_spsc_ringbuf *b, size_t *n); -void shm_spsc_ringbuf_write_finish(shm_spsc_ringbuf *q, size_t n); +size_t vms_spsc_ringbuf_write_off_nowrap(vms_spsc_ringbuf *b, size_t *n); +size_t vms_spsc_ringbuf_acquire(vms_spsc_ringbuf *b, size_t *n, size_t *wrap); +size_t vms_spsc_ringbuf_acquire_nowrap(vms_spsc_ringbuf *b, size_t *n); +void vms_spsc_ringbuf_write_finish(vms_spsc_ringbuf *q, size_t n); -size_t shm_spsc_ringbuf_size(shm_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_max_size(shm_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_size(vms_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_max_size(vms_spsc_ringbuf *b); /* capacity == max_size + 1, because we use one element as a separator. Capacity - 1 is the maximal offset that the ringbuf considers. */ -size_t shm_spsc_ringbuf_capacity(shm_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_free_num(shm_spsc_ringbuf *b); -bool shm_spsc_ringbuf_full(shm_spsc_ringbuf *b); -bool shm_spsc_ringbuf_empty(shm_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_capacity(vms_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_free_num(vms_spsc_ringbuf *b); +bool vms_spsc_ringbuf_full(vms_spsc_ringbuf *b); +bool vms_spsc_ringbuf_empty(vms_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_read_off_nowrap(shm_spsc_ringbuf *b, size_t *n); -size_t shm_spsc_ringbuf_read_acquire(shm_spsc_ringbuf *b, size_t *n); -void *shm_spsc_ringbuf_top(shm_spsc_ringbuf *b); -size_t shm_spsc_ringbuf_peek(shm_spsc_ringbuf *b, size_t n, size_t *off, +size_t vms_spsc_ringbuf_read_off_nowrap(vms_spsc_ringbuf *b, size_t *n); +size_t vms_spsc_ringbuf_read_acquire(vms_spsc_ringbuf *b, size_t *n); +void *vms_spsc_ringbuf_top(vms_spsc_ringbuf *b); +size_t vms_spsc_ringbuf_peek(vms_spsc_ringbuf *b, size_t n, size_t *off, size_t *len1, size_t *len2); -#endif /* SHAMON_SPSC_RINGBUF_H */ +#endif /* VAMOS_SPSC_RINGBUF_H */ diff --git a/include/vamos-buffers/core/stream.h b/include/vamos-buffers/core/stream.h index 1edc28d2..28dd1221 100644 --- a/include/vamos-buffers/core/stream.h +++ b/include/vamos-buffers/core/stream.h @@ -1,50 +1,51 @@ -#ifndef SHAMON_STREAMS_H -#define SHAMON_STREAMS_H +#ifndef VAMOS_STREAMS_H +#define VAMOS_STREAMS_H #include "event.h" #include "source.h" #include "vector-macro.h" -typedef struct _shm_arbiter_buffer shm_arbiter_buffer; +typedef struct _vms_arbiter_buffer vms_arbiter_buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; -typedef size_t (*shm_stream_buffer_events_fn)(struct _shm_stream *, - shm_arbiter_buffer *buffer); -typedef bool (*shm_stream_is_ready_fn)(struct _shm_stream *); -typedef void (*shm_stream_destroy_fn)(struct _shm_stream *); +typedef size_t (*vms_stream_buffer_events_fn)(struct _vms_stream *, + vms_arbiter_buffer *buffer); +typedef bool (*vms_stream_is_ready_fn)(struct _vms_stream *); +typedef void (*vms_stream_destroy_fn)(struct _vms_stream *); -typedef bool (*shm_stream_filter_fn)(shm_stream *, shm_event *); -typedef void (*shm_stream_alter_fn)(shm_stream *, shm_event *, shm_event *); -typedef void (*shm_stream_hole_init_fn)(shm_event *); -typedef void (*shm_stream_hole_update_fn)(shm_event *, shm_event *); +typedef bool (*vms_stream_filter_fn)(vms_stream *, vms_event *); +typedef void (*vms_stream_alter_fn)(vms_stream *, vms_event *, vms_event *); +typedef void (*vms_stream_hole_init_fn)(vms_event *); +typedef void (*vms_stream_hole_update_fn)(vms_event *, vms_event *); -typedef struct _shm_stream_hole_handling { +typedef struct _vms_stream_hole_handling { size_t hole_event_size; - shm_stream_hole_init_fn init; - shm_stream_hole_update_fn update; -} shm_stream_hole_handling; + vms_stream_hole_init_fn init; + vms_stream_hole_update_fn update; +} vms_stream_hole_handling; // TODO: make this opaque -typedef struct _shm_stream { +typedef struct _vms_stream { uint64_t id; char *name; char *type; size_t event_size; /* shared-memory buffer */ - struct buffer *incoming_events_buffer; + vms_shm_buffer *incoming_events_buffer; /* the number of created substreams (sub-buffers) for the * shared memory buffer */ size_t substreams_no; /* cached info about events in 'incoming_events_buffer' */ - struct event_record *events_cache; + struct vms_event_record *events_cache; /* callbacks */ - shm_stream_is_ready_fn is_ready; - shm_stream_filter_fn filter; - shm_stream_alter_fn alter; - shm_stream_destroy_fn destroy; - shm_stream_hole_handling hole_handling; + vms_stream_is_ready_fn is_ready; + vms_stream_filter_fn filter; + vms_stream_alter_fn alter; + vms_stream_destroy_fn destroy; + vms_stream_hole_handling hole_handling; /* substreams of this stream and the link to the parent */ - shm_stream *parent_stream; - VEC(substreams, struct _shm_stream *); + vms_stream *parent_stream; + VEC(substreams, struct _vms_stream *); #ifndef NDEBUG /* for checking consistency */ size_t last_event_id; @@ -56,69 +57,69 @@ typedef struct _shm_stream { size_t dropped_events; size_t slept_waiting_for_ev; #endif -} shm_stream; +} vms_stream; -void shm_stream_init(shm_stream *stream, struct buffer *incoming_events_buffer, - size_t event_size, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, - const shm_stream_hole_handling *hole_handling, +void vms_stream_init(vms_stream *stream, vms_shm_buffer *incoming_events_buffer, + size_t event_size, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, + const vms_stream_hole_handling *hole_handling, const char *const type, const char *const name); -const char *shm_stream_get_name(shm_stream *); -const char *shm_stream_get_type(shm_stream *); -size_t shm_stream_event_size(shm_stream *); -size_t shm_stream_id(shm_stream *); +const char *vms_stream_get_name(vms_stream *); +const char *vms_stream_get_type(vms_stream *); +size_t vms_stream_event_size(vms_stream *); +size_t vms_stream_id(vms_stream *); -struct event_record *shm_stream_get_avail_events(shm_stream *, size_t *); -struct event_record *shm_stream_get_event_record(shm_stream *, shm_kind); -struct event_record *shm_stream_get_event_record_no_cache(shm_stream *, - shm_kind); +struct vms_event_record *vms_stream_get_avail_events(vms_stream *, size_t *); +struct vms_event_record *vms_stream_get_event_record(vms_stream *, vms_kind); +struct vms_event_record *vms_stream_get_event_record_no_cache(vms_stream *, + vms_kind); /* register to receiving events particular events */ -int shm_stream_register_event(shm_stream *, const char *, shm_kind); -int shm_stream_register_events(shm_stream *, size_t sz, ...); -int shm_stream_register_all_events(shm_stream *); +int vms_stream_register_event(vms_stream *, const char *, vms_kind); +int vms_stream_register_events(vms_stream *, size_t sz, ...); +int vms_stream_register_all_events(vms_stream *); -_Bool shm_stream_has_new_substreams(shm_stream *stream); -_Bool shm_stream_is_substream(shm_stream *stream); +_Bool vms_stream_has_new_substreams(vms_stream *stream); +_Bool vms_stream_is_substream(vms_stream *stream); -shm_stream *shm_stream_create_substream( - shm_stream *stream, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, shm_stream_hole_handling *hole_handling); +vms_stream *vms_stream_create_substream( + vms_stream *stream, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, vms_stream_hole_handling *hole_handling); /* the number of elements in the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_size(shm_stream *); +size_t vms_stream_buffer_size(vms_stream *); /* the capacity the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_capacity(shm_stream *); +size_t vms_stream_buffer_capacity(vms_stream *); -void *shm_stream_read_events(shm_stream *, size_t *); -bool shm_stream_consume(shm_stream *stream, size_t num); -const char *shm_stream_get_str(shm_stream *stream, uint64_t elem); +void *vms_stream_read_events(vms_stream *, size_t *); +bool vms_stream_consume(vms_stream *stream, size_t num); +const char *vms_stream_get_str(vms_stream *stream, uint64_t elem); -void shm_stream_notify_last_processed_id(shm_stream *stream, shm_eventid id); -bool shm_stream_is_ready(shm_stream *); -void shm_stream_attach(shm_stream *stream); -void shm_stream_detach(shm_stream *stream); +void vms_stream_notify_last_processed_id(vms_stream *stream, vms_eventid id); +bool vms_stream_is_ready(vms_stream *); +void vms_stream_attach(vms_stream *stream); +void vms_stream_detach(vms_stream *stream); -void shm_stream_prepare_hole_event(shm_stream *stream, shm_event *ev, size_t id, +void vms_stream_prepare_hole_event(vms_stream *stream, vms_event *ev, size_t id, uint64_t n); /* * returns true if there will come no events in the future, false otherwise * */ -bool shm_stream_is_finished(shm_stream *); +bool vms_stream_is_finished(vms_stream *); -void shm_stream_notify_dropped(shm_stream *stream, uint64_t begin_id, +void vms_stream_notify_dropped(vms_stream *stream, uint64_t begin_id, uint64_t end_id); -void shm_stream_destroy(shm_stream *stream); +void vms_stream_destroy(vms_stream *stream); #ifndef NDEBUG -inline size_t shm_stream_last_event_id(shm_stream *s) { +inline size_t vms_stream_last_event_id(vms_stream *s) { return s->last_event_id; } #endif -void shm_stream_dump_events(shm_stream *stream); -#endif // SHAMON_STREAMS_H +void vms_stream_dump_events(vms_stream *stream); +#endif // VAMOS_STREAMS_H diff --git a/include/vamos-buffers/core/utils.h b/include/vamos-buffers/core/utils.h index 8ee05927..5cc6e62c 100644 --- a/include/vamos-buffers/core/utils.h +++ b/include/vamos-buffers/core/utils.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_UTILS_H_ -#define SHAMON_UTILS_H_ +#ifndef VAMOS_UTILS_H_ +#define VAMOS_UTILS_H_ #include /* size_t */ #include @@ -20,4 +20,4 @@ void *xalloc_aligned(size_t size, size_t alignment); char *xstrdup(const char *str); -#endif /* SHAMON_UTILS_H_ */ +#endif /* VAMOS_UTILS_H_ */ diff --git a/include/vamos-buffers/core/vector-aligned.h b/include/vamos-buffers/core/vector-aligned.h index 1e14ac89..12720eb8 100644 --- a/include/vamos-buffers/core/vector-aligned.h +++ b/include/vamos-buffers/core/vector-aligned.h @@ -1,23 +1,23 @@ -#ifndef SHAMON_VECTOR_ALIGNED_H_ -#define SHAMON_VECTOR_ALIGNED_H_ +#ifndef VAMOS_VECTOR_ALIGNED_H_ +#define VAMOS_VECTOR_ALIGNED_H_ #include #include "vector.h" -/* shm_vector that allocates aligned memory */ -typedef struct _shm_vector_aligned { - shm_vector vec; +/* vms_vector that allocates aligned memory */ +typedef struct _vms_vector_aligned { + vms_vector vec; size_t alignment; -} shm_vector_aligned; +} vms_vector_aligned; -void shm_vector_aligned_init(shm_vector_aligned *vec, size_t elem_size, +void vms_vector_aligned_init(vms_vector_aligned *vec, size_t elem_size, size_t alignment); -size_t shm_vector_aligned_push(shm_vector *vec, void *elem); -void *shm_vector_aligned_extend(shm_vector *vec); -void shm_vector_aligned_swap(shm_vector *vec, shm_vector *with); -void shm_vector_aligned_resize(shm_vector *vec, size_t size); +size_t vms_vector_aligned_push(vms_vector *vec, void *elem); +void *vms_vector_aligned_extend(vms_vector *vec); +void vms_vector_aligned_swap(vms_vector *vec, vms_vector *with); +void vms_vector_aligned_resize(vms_vector *vec, size_t size); -/* the rest of functions is inherited from shm_vector, use those */ +/* the rest of functions is inherited from vms_vector, use those */ -#endif /* SHAMON_VECTOR_ALIGNED_H_ */ +#endif /* VAMOS_VECTOR_ALIGNED_H_ */ diff --git a/include/vamos-buffers/core/vector-macro.h b/include/vamos-buffers/core/vector-macro.h index 5456f323..8630d94e 100644 --- a/include/vamos-buffers/core/vector-macro.h +++ b/include/vamos-buffers/core/vector-macro.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_VECTOR_MACRO_H_ -#define SHAMON_VECTOR_MACRO_H_ +#ifndef VAMOS_VECTOR_MACRO_H_ +#define VAMOS_VECTOR_MACRO_H_ /** * Macro for creating inlined vector, that is, a piece of continuous memory @@ -46,7 +46,7 @@ * * VEC() can be usually also used in struct declarations, but not always. * For example if a struct is opaque and the vector must be accessed from - * outside. In such cases, we provide `shm_vector` data type. + * outside. In such cases, we provide `vms_vector` data type. */ #include @@ -121,7 +121,6 @@ VEC_SIZE(vec) += 1; \ } while (0) - #define VEC_EXTEND(vec, outptr) VEC_EXTEND_N(vec, outptr, 16) /** @@ -150,6 +149,23 @@ --VEC_SIZE(vec); \ } while (0) +/** + * Remove the first occurence of `elem` from `vec`. `elem` must be a simple + * type, since we use == with it. + * IMPORTANT: The removal does shuffle the elements, in particular, + * the last element will be copied to the place of the removed one. + */ +#define VEC_UNORD_REMOVE(vec, elem) \ + do { \ + for (int i = 0; i < VEC_SIZE(vec); ++i) { \ + if (VEC_AT(i) == elem) { \ + vec[i] = vec[VEC_SIZE(vec) - 1]; \ + --VEC_SIZE(vec); \ + break; \ + } \ + } \ + while (0) + /** * Return the pointer to the top element in the vector. * There is no check if there is any element. If there is none, @@ -176,4 +192,4 @@ */ #define VEC_POP_TOP(vec) (--VEC_SIZE(vec), *((vec) + VEC_SIZE(vec))) -#endif /* SHAMON_VECTOR_MACRO_H_ */ +#endif /* VAMOS_VECTOR_MACRO_H_ */ diff --git a/include/vamos-buffers/core/vector.h b/include/vamos-buffers/core/vector.h index e582fa70..426da4fe 100644 --- a/include/vamos-buffers/core/vector.h +++ b/include/vamos-buffers/core/vector.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_VECTOR_H_ -#define SHAMON_VECTOR_H_ +#ifndef VAMOS_VECTOR_H_ +#define VAMOS_VECTOR_H_ /** * Implementation of a vector similar to std::vector in C++. @@ -10,19 +10,18 @@ #include -typedef struct _shm_vector { +typedef struct _vms_vector { size_t size; size_t element_size; size_t alloc_size; void *data; -} shm_vector; +} vms_vector; -void shm_vector_init(shm_vector *vec, size_t elem_size); -void shm_vector_destroy(shm_vector *vec); - -size_t shm_vector_size(shm_vector *vec); -size_t shm_vector_elem_size(shm_vector *vec); +void vms_vector_init(vms_vector *vec, size_t elem_size); +void vms_vector_destroy(vms_vector *vec); +size_t vms_vector_size(vms_vector *vec); +size_t vms_vector_elem_size(vms_vector *vec); /** * Return the pointer to the element at index `idx`. @@ -30,22 +29,20 @@ size_t shm_vector_elem_size(shm_vector *vec); * In debug mode, OOB checks are performed. * In release mode, if `idx` is OOB, the behavior is undefined. */ -void *shm_vector_at(shm_vector *vec, size_t idx); +void *vms_vector_at(vms_vector *vec, size_t idx); /** * Return the pointer to the element at index `idx`. * If `idx` is OOB, return NULL. */ -void *shm_vector_at_checked(shm_vector *vec, size_t idx); - +void *vms_vector_at_checked(vms_vector *vec, size_t idx); /** * Push an element pointed by `elem` into `vec`. * Memory is reallocated if needed. By default, the new memory can store * 10 more elements than the previously allocated memory. */ -size_t shm_vector_push(shm_vector *vec, void *elem); - +size_t vms_vector_push(vms_vector *vec, void *elem); /** * Return the pointer to the next free space in the vector and increase @@ -53,32 +50,29 @@ size_t shm_vector_push(shm_vector *vec, void *elem); * The returned pointer should be used to write the element into the vector, * e.g.: * - * int *ptr = shm_vector_extend(vec); + * int *ptr = vms_vector_extend(vec); * *ptr = 1; * * If there is not enough space to write a single element, the memory is * reallocated, by default to hold 10 new elements. */ -void *shm_vector_extend(shm_vector *vec); +void *vms_vector_extend(vms_vector *vec); /** * Remove the last element from the vector (decrease its size by 1) */ -size_t shm_vector_pop(shm_vector *vec); - +size_t vms_vector_pop(vms_vector *vec); /** * Return the pointer to the top element or NULL * if the vector is empty. */ -void *shm_vector_top(shm_vector *vec); - +void *vms_vector_top(vms_vector *vec); /** * Swap the contents of two vectors. */ -void shm_vector_swap(shm_vector *vec, shm_vector *with); - +void vms_vector_swap(vms_vector *vec, vms_vector *with); /** * Resize the vector to hold `size` elements. If the current size @@ -89,10 +83,10 @@ void shm_vector_swap(shm_vector *vec, shm_vector *with); * * Note that the vector may have allocated memory for more than `size` * elements. In that case the memory remains non-0'ed. - * This should not be a problem as the uninitialized memory should not be touched - * as the user knows only that there is space for `size` elements - * but nothing more. + * This should not be a problem as the uninitialized memory should not be + * touched as the user knows only that there is space for `size` elements but + * nothing more. */ -void shm_vector_resize(shm_vector *vec, size_t size); +void vms_vector_resize(vms_vector *vec, size_t size); -#endif /* SHAMON_VECTOR_H_ */ +#endif /* VAMOS_VECTOR_H_ */ diff --git a/include/vamos-buffers/core/shm_string-macro.h b/include/vamos-buffers/core/vms_string-macro.h similarity index 96% rename from include/vamos-buffers/core/shm_string-macro.h rename to include/vamos-buffers/core/vms_string-macro.h index 92becf9a..faab4cd2 100644 --- a/include/vamos-buffers/core/shm_string-macro.h +++ b/include/vamos-buffers/core/vms_string-macro.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_STRING_MACRO_H_ -#define SHAMON_STRING_MACRO_H_ +#ifndef VAMOS_STRING_MACRO_H_ +#define VAMOS_STRING_MACRO_H_ #include "vector-macro.h" @@ -57,4 +57,4 @@ #define STRING_TOP_PTR(s) ((s) + STRING_SIZE(s) - 1) #define STRING_TOP(s) (*STRING_TOP_PTR(s)) -#endif /* SHAMON_STRING_MACRO_H_ */ +#endif /* VAMOS_STRING_MACRO_H_ */ diff --git a/include/vamos-buffers/core/vms_string.h b/include/vamos-buffers/core/vms_string.h new file mode 100644 index 00000000..45e2775e --- /dev/null +++ b/include/vamos-buffers/core/vms_string.h @@ -0,0 +1,36 @@ +#ifndef VAMOS_STRING_H_ +#define VAMOS_STRING_H_ + +#include + +#include "vms_string-macro.h" + +typedef struct _vms_string_ref { + size_t size; + const char *data; +} vms_string_ref; + +typedef struct _vms_string { + STRING(data); +} vms_string; + +/* Either the user can manipulate the string manually, + * with the macros from vms_string-macro.h + * or with the functions below. The macros are used + on the data of the string, e.g., + STRING_RESIZE(s->data). Note that macros are not applicable + always (they just expand to code that may be syntactically wrong + in some contexts). */ +void vms_string_init(vms_string *s); +void vms_string_destroy(vms_string *s); +void vms_string_clear(vms_string *s); +size_t vms_string_append(vms_string *s, char c); +char *vms_string_extend(vms_string *s); +size_t vms_string_pop(vms_string *s); +size_t vms_string_size(vms_string *s); +char *vms_string_at_ptr(vms_string *s, ssize_t idx); +char vms_string_at(vms_string *s, ssize_t idx); +void vms_string_swap(vms_string *s, vms_string *with); +void vms_string_grow(vms_string *s, size_t size); + +#endif /* VAMOS_VECTOR_H_ */ diff --git a/include/vamos-buffers/cpp/event.h b/include/vamos-buffers/cpp/event.h new file mode 100644 index 00000000..c75d5106 --- /dev/null +++ b/include/vamos-buffers/cpp/event.h @@ -0,0 +1,36 @@ +#ifndef VAMOS_BUFFERS_EVENT_H +#define VAMOS_BUFFERS_EVENT_H + +#include + +namespace vamos { + +struct Event : public vms_event { + Event() {} + Event(vms_kind kind) : _vms_event(kind) {} + Event(vms_kind kind, vms_eventid id) : _vms_event(kind, id) {} + + bool is_hole() const { return vms_event_is_hole(this); } + bool is_done() const { return vms_event_is_done(this); } + void set_id(vms_eventid id) { vms_event_set_id(this, id); } + + static Event getDone(vms_eventid id) { + return Event(vms_event_get_done_kind(), id); + } + + static constexpr vms_kind holeKind() { return VMS_EVENT_HOLE_KIND; } + + static constexpr vms_kind doneKind() { return VMS_EVENT_DONE_KIND; } + + static constexpr vms_kind lastSpecialKind() { + return VMS_EVENT_LAST_SPECIAL_KIND; + } + + static constexpr vms_kind firstValidKind() { + return VMS_EVENT_LAST_SPECIAL_KIND + 1; + } +}; + +} // namespace vamos + +#endif diff --git a/include/vamos-buffers/cpp/ring_buffer.h b/include/vamos-buffers/cpp/ring_buffer.h new file mode 100644 index 00000000..d0bef452 --- /dev/null +++ b/include/vamos-buffers/cpp/ring_buffer.h @@ -0,0 +1,394 @@ +#ifndef VAMOS_CPP_RING_BUFFER_H +#define VAMOS_CPP_RING_BUFFER_H + +#include +#include + +namespace vamos { + +#define __predict_false(x) __builtin_expect((x) != 0, 0) +#define __predict_true(x) __builtin_expect((x) != 0, 1) + +/* On x86 and ARM the cache line has 64 bytes, change if needed. */ +#define CACHELINE_SIZE 64 +#define CACHELINE_ALIGNED alignas(CACHELINE_SIZE) +#define ADDR_IS_CACHE_ALIGNED(addr) \ + (((unsigned long long)(addr)) % CACHELINE_SIZE == 0) + +/** + * Single-producer single-consumer (SPSC) lock-free concurrent ring-buffer + * of a fixed size; + * This implementation is rewritten `spsc_ringbuf.h`, but it is not kept + * in sync, so the two implementations might diverge. + */ + +namespace { +static inline size_t _is_empty(size_t head, size_t tail) { + return head == tail; +} + +template +size_t _is_full(size_t head, size_t tail) { + return head + 1 == tail || (head == capacity - 1 && tail == 0); +} + +template +size_t __written_num(size_t head, size_t tail) { + if (tail < head) { + return head - tail; + } else { + if (__predict_false(tail == head)) { + return 0; + } else { + return capacity - tail + head; + } + } +} + +template +size_t __free_num(size_t head, size_t tail) { + assert(head < capacity); + assert(tail < capacity); + + if (tail < head) { + return (capacity - head - 1) + tail; + } + + if (__predict_false(head == tail)) { + return capacity - 1; + } + + return tail - head - 1; +} + +#define CHECK_IF(c, t) assert(!(c) || (t)) +#define CHECK_IFF(c, t) \ + do { \ + CHECK_IF(c, t); \ + CHECK_IF(t, c); \ + } while (0) + +template +size_t _written_num(size_t head, size_t tail) { + /* pre */ + assert(head < capacity); + assert(tail < capacity); + + size_t ret = __written_num(head, tail); + + /* post */ + assert(ret < capacity); + CHECK_IFF(_is_empty(head, tail), ret == 0); + CHECK_IFF(_is_full(head, tail), ret == capacity - 1); + CHECK_IF(tail < head, ret == head - tail); + CHECK_IF(tail > head, ret == (head + (capacity - tail))); + assert(ret == capacity - 1 - __free_num(head, tail)); + + return ret; +} + +template +size_t _free_num(size_t head, size_t tail) { + assert(head < capacity); + assert(tail < capacity); + + size_t ret = __free_num(head, tail); + + assert(ret < capacity); + CHECK_IFF(_is_empty(head, tail), ret == capacity - 1); + CHECK_IFF(_is_full(head, tail), ret == 0); + assert(ret == capacity - 1 - __written_num(head, tail)); + + return ret; +} + +} // namespace + +template +class RingBuffer { + CACHELINE_ALIGNED std::atomic _tail; + CACHELINE_ALIGNED std::atomic _head; + + public: + static constexpr uint64_t Capacity = CAPACITY; + + constexpr uint64_t capacity() const { return CAPACITY; } + + RingBuffer() : _tail(0), _head(0) { static_assert(CAPACITY > 0); } + + size_t load_head(std::memory_order mo) const { return _head.load(mo); } + + size_t load_tail(std::memory_order mo) const { return _tail.load(mo); } + + void set_head(size_t new_head, std::memory_order mo) { + _head.store(new_head, mo); + } + + void set_tail(size_t new_tail, std::memory_order mo) { + _tail.store(new_tail, mo); + } + + bool is_full() const { + return __predict_false( + _is_full(load_head(std::memory_order_relaxed), + load_tail(std::memory_order_relaxed))); + } +}; + +template +class RingBufferAccessor { + static constexpr uint64_t _capacity = RingBufferTy::Capacity; + + protected: + RingBufferTy &rb; + // the cache for the head/tail last seen by this thread + size_t seen{0}; + + public: + RingBufferAccessor(RingBufferTy &rb) + : rb(rb) {} //, _capacity(rb.capacity()) {} + + constexpr size_t capacity() const { return _capacity; } + + size_t max_size() const { + /* we use one element as a separator */ + return capacity() - 1; + } + + size_t size() const { + return _written_num( + rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); + } + + bool empty() const { + return _is_empty( + rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); + } + + size_t free_num() const { + return _free_num( + rb.load_head(std::memory_order_relaxed), + rb.load_tail(std::memory_order_relaxed)); + } +}; + +namespace { +/* Compute the write offset (with wrapping). Returns the num of free elements */ +template +size_t _write_off(size_t head, size_t tail, size_t &n, size_t *wrap_n) { + if (tail < head) { + if (__predict_false(tail == 0)) { + n = capacity - head - 1; + if (wrap_n) { + *wrap_n = 0; + } + return capacity - head - 1; + } + + n = capacity - head; + if (wrap_n) { + *wrap_n = tail - 1; + } + return capacity - head + tail - 1; + } + + if (__predict_false(tail == head)) { + n = capacity - 1; + if (wrap_n) { + *wrap_n = 0; + } + return capacity - 1; + } + + n = tail - head - 1; + if (wrap_n) { + *wrap_n = 0; + } + return tail - head - 1; +} + +} // namespace + +template +class RingBufferWriter : public RingBufferAccessor { + public: + RingBufferWriter(RingBufferTy &rb) : RingBufferAccessor(rb) {} + + /* Return the offset for writing and set the number of writable elements to + * `n` and also to `wrap_n` if there is a wrap-around the end of the buffer + */ + size_t write_off(size_t &n, size_t *wrap_n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + if (_write_off(head, this->seen, n, wrap_n) == + 0) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, wrap_n); + } + + return head; + } + + size_t write_off(size_t &n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + /* Update the cache if seen_tail is 0 (which very likely means it has + * not been updated yet, but can be of course also after wrapping + * around) or if there is no space left considering the cached + * information. */ + if (this->seen == 0 || _write_off( + head, this->seen, n, NULL) == 0) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, NULL); + } + + return head; + } + + /* Ask for at least *n elements */ + size_t acquire(size_t &n, size_t *wrap_n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + size_t req = n; + if (_write_off(head, this->seen, n, wrap_n) < + req) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, wrap_n); + } + + return head; + } + + /* Ask for at least *n elements */ + size_t acquire(size_t &n) { + const size_t head = this->rb.load_head(std::memory_order_acquire); + + size_t req = n; + if (_write_off(head, this->seen, n, NULL) < + req) { + this->seen = this->rb.load_tail(std::memory_order_relaxed); + _write_off(head, this->seen, n, NULL); + } + return head; + } + + void write_finish(size_t n) { + constexpr auto c = RingBufferTy::Capacity; + assert(n <= c); + assert((c < (~((size_t)0)) - n) && "Possible overflow"); + + size_t new_head = this->rb.load_head(std::memory_order_relaxed) + n; + + /* buffer full, rotate it */ + if (__predict_false(new_head >= c)) { + new_head -= c; + } + + assert(new_head != this->rb.load_tail(std::memory_order_relaxed) && + "Invalid head move"); + + this->rb.set_head(new_head, std::memory_order_release); + } +}; + +template +class RingBufferReader : public RingBufferAccessor { + public: + RingBufferReader(RingBufferTy &rb) : RingBufferAccessor(rb) {} + + size_t read_off(size_t &n) { + const size_t tail = this->rb.load_tail(std::memory_order_acquire); + size_t tmp = _written_num(this->seen, tail); + if (tmp == 0) { + this->seen = this->rb.load_head(std::memory_order_acquire); + tmp = _written_num(this->seen, tail); + } + + n = tmp; + + return tail; + } + + size_t available() const { + return _written_num( + this->rb.load_head(std::memory_order_relaxed), + this->rb.load_tail(std::memory_order_relaxed)); + } + + size_t acquire(size_t &n) { + const size_t tail = this->rb.load_tail(std::memory_order_acquire); + size_t tmp = _written_num(this->seen, tail); + if (tmp < n) { + this->seen = this->rb.load_head(std::memory_order_acquire); + tmp = _written_num(this->seen, tail); + } + + n = tmp; + + return tail; + } + + /* + * Consume n items from the ringbuffer. There must be at least n items. + */ + void consume(size_t n) { + assert(n > 0 && "Consume 0 elems"); + assert(n <= this->capacity()); + assert((this->capacity() < (~((size_t)0)) - n) && "Possible overflow"); + + size_t new_tail = this->rb.load_tail(std::memory_order_relaxed) + n; + if (__predict_false(new_tail >= this->capacity())) { + new_tail -= this->capacity(); + } + + this->rb.set_tail(new_tail, std::memory_order_release); + } + + /* If return value is 0, values *off, *len1 and *len2 may not have been set + */ + size_t peek(size_t n, size_t &off, size_t &len1, size_t &len2) { + const size_t tail = this->rb.load_tail(std::memory_order_acquire); + size_t head = this->seen; + size_t cur_elem_num = _written_num(head, tail); + /* update the information if needed or when n == 0 (which means we want + * to get an up-to-date the number of elements) */ + if (cur_elem_num < n || n == 0) { + this->seen = head = this->rb.load_head(std::memory_order_acquire); + cur_elem_num = _written_num(head, tail); + } + + if (n == 0) + return cur_elem_num; + + if (__predict_false(cur_elem_num == 0)) + return 0; + + if (cur_elem_num < n) { + n = cur_elem_num; + } + + assert(n <= cur_elem_num); + + if (tail < head) { + len1 = n; + len2 = 0; + } else { + assert(tail != head && "Ringbuf is empty"); + constexpr size_t c = this->capacity(); + const size_t l1 = c - tail > n ? n : c - tail; + len1 = l1; + len2 = head < (n - l1) ? head : n - l1; + } + + assert(len1 + len2 == n); + off = tail; + + return cur_elem_num; + } +}; + +} // namespace vamos + +#endif diff --git a/include/vamos-buffers/cpp/vector.h b/include/vamos-buffers/cpp/vector.h new file mode 100644 index 00000000..a1ad9a74 --- /dev/null +++ b/include/vamos-buffers/cpp/vector.h @@ -0,0 +1,162 @@ +#ifndef VAMOS_BUFFERS_CPP_VECTOR_H +#define VAMOS_BUFFERS_CPP_VECTOR_H + +#include + +#error "This class must be debugged and fixed, it does not work" + +namespace vamos { + +template +class vec { + ElemTy *_data{nullptr}; + size_t _size{0}; + size_t _alloc_size{0}; + + public: + ~vec() { free(_data); } + + void swap(vec &rhs) { + auto tmp = rhs; + rhs = *this; + *this = rhs; + } + + size_t size() const { return _size; } + size_t empty() const { return _size == 0; } + size_t alloc_size() const { return _alloc_size; } + constexpr size_t elem_size() { return sizeof(ElemTy); } + + ElemTy *at(size_t idx) { + assert(idx < _size); + return _data + idx; + } + + ElemTy *get(size_t idx) { + if (idx < _size) + return _data + idx; + return nullptr; + } + + const ElemTy *get(size_t idx) const { + return const_cast(get(idx)); + } + + /** + * Return the pointer to the next free space in the vector and increase + * the number of elements in the vector by one. + * The returned pointer should be used to write the element into the vector, + * e.g.: + * + * int *ptr = VEC_EXTEND_N(vec, 4); + * *ptr = 1; + * + * If there is not enough space to write a single element, the memory is + * reallocated to hold `n` new elements. + */ + ElemTy *extend_n(size_t n) { + if (_size >= _alloc_size) { + _alloc_size += n; + _data = static_cast( + realloc(_data, _alloc_size * elem_size())); + assert(_data != nullptr && "Memory re-allocation failed"); + } + assert((_size < _alloc_size) && "Vector too small"); + return _data[_size++]; + } + + ElemTy *extend() { return extend_n(16); } + + size_t push(const ElemTy &e) { + if (_size >= _alloc_size) { + _alloc_size += 10; + _data = static_cast( + realloc(_data, _alloc_size * elem_size())); + assert(_data != nullptr && "Memory re-allocation failed"); + } + assert((_size < _alloc_size) && "Vector too small"); + _data[_size++] = e; + return _size; + } + + size_t push(const ElemTy *e) { return push(*e); } + + size_t push(ElemTy &&e) { + if (_size >= _alloc_size) { + _alloc_size += 10; + _data = static_cast( + realloc(_data, _alloc_size * elem_size())); + assert(_data != nullptr && "Memory re-allocation failed"); + } + assert((_size < _alloc_size) && "Vector too small"); + _data[_size++] = std::move(e); + return _size; + } + + /** + * Remove the last element from the vector (decrease its size by 1) + */ + size_t pop() { return --_size; } + void clear() { _size = 0; } + + ElemTy *back() { return &_data[_size]; } + const ElemTy *back() const { return &_data[_size]; } + + template + class _iterator { + protected: + VecTy *v; + size_t pos; + static const size_t npos = ~static_cast(0); + + _iterator(VecTy &v0) : v(&v0), pos(v0._size == 0 ? npos : 0) {} + _iterator() : pos(npos) {} + _iterator(const _iterator &) = default; + + friend class vec; + + public: + _iterator &operator++() { + ++pos; + if (pos == v->_size) + pos = npos; + return *this; + } + + _iterator operator++(int) { + auto tmp = *this; + operator++(); + return tmp; + } + + bool operator==(const _iterator &rhs) const { return pos == rhs.pos; } + bool operator!=(const _iterator &rhs) const { return !operator==(rhs); } + }; + + struct iterator : public _iterator { + iterator(vec &v) : _iterator(v) {} + iterator() : _iterator() {} + iterator(const iterator &) = default; + + ElemTy &operator*() { return this->v->_data[this->pos]; } + ElemTy *operator->() { return &this->v->_data[this->pos]; } + }; + + struct const_iterator : public _iterator { + const_iterator(const vec &v) : _iterator(v) {} + const_iterator() : _iterator() {} + const_iterator(const const_iterator &) = default; + + const ElemTy &operator*() const { return this->v->_data[this->pos]; } + const ElemTy *operator->() const { return &this->v->_data[this->pos]; } + }; + + iterator begin() { return iterator(*this); } + iterator end() { return iterator(); } + const_iterator begin() const { return const_iterator(*this); } + const_iterator end() const { return const_iterator(); } +}; + +} // namespace vamos + +#endif diff --git a/include/vamos-buffers/shmbuf/buffer.h b/include/vamos-buffers/shmbuf/buffer.h index a7ab8555..500d0274 100644 --- a/include/vamos-buffers/shmbuf/buffer.h +++ b/include/vamos-buffers/shmbuf/buffer.h @@ -1,101 +1,124 @@ -#ifndef SHAMON_SHM_BUFFER_H -#define SHAMON_SHM_BUFFER_H +#ifndef VAMOS_SHM_BUFFER_H +#define VAMOS_SHM_BUFFER_H #include #include #include #include -struct source_control; -struct event_record; -struct buffer; - -struct buffer *create_shared_buffer(const char *key, size_t capacity, - const struct source_control *control); -struct buffer *create_shared_buffer_adv(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - const struct source_control *control); -struct buffer *create_shared_sub_buffer(struct buffer *buffer, size_t capacity, - const struct source_control *control); -size_t buffer_get_sub_buffers_no(struct buffer *buffer); - -struct buffer *try_get_shared_buffer(const char *key, size_t retry); -struct buffer *get_shared_buffer(const char *key); -struct event_record *buffer_get_avail_events(struct buffer *, size_t *); - -int buffer_get_key_path(struct buffer *, char keypath[], size_t keypathsize); -int buffer_get_ctrl_key_path(struct buffer *, char keypath[], - size_t keypathsize); -const char *buffer_get_key(struct buffer *); -char *get_sub_buffer_key(const char *key, size_t idx); - -void release_shared_buffer(struct buffer *); -void destroy_shared_buffer(struct buffer *); -void destroy_shared_sub_buffer(struct buffer *); -void release_shared_sub_buffer(struct buffer *); - -bool buffer_is_ready(struct buffer *); -bool buffer_monitor_attached(struct buffer *); -void buffer_set_attached(struct buffer *, bool); -void buffer_set_destroyed(struct buffer *buff); - -bool buffer_pop(struct buffer *buff, void *dst); -bool buffer_push(struct buffer *buff, const void *elem, size_t size); -void *buffer_get_str(struct buffer *buff, uint64_t elem); - -void *buffer_read_pointer(struct buffer *buff, size_t *size); -bool buffer_drop_k(struct buffer *buff, size_t size); -size_t buffer_consume(struct buffer *buff, size_t k); - -size_t buffer_size(struct buffer *buff); -size_t buffer_capacity(struct buffer *buff); -size_t buffer_elem_size(struct buffer *buff); - -void *buffer_start_push(struct buffer *buff); - -void *buffer_partial_push(struct buffer *buff, void *prev_push, - const void *elem, size_t size); +struct vms_source_control; +struct vms_event_record; +typedef struct _vms_shm_buffer vms_shm_buffer; + +/* ---------------------------------------------------------------------- */ +/* WRITER API */ +/* ---------------------------------------------------------------------- */ +vms_shm_buffer *vms_shm_buffer_create(const char *key, size_t capacity, + const struct vms_source_control *control); +vms_shm_buffer *vms_shm_buffer_create_adv( + const char *key, mode_t mode, size_t elem_size, size_t capacity, + const struct vms_source_control *control); +vms_shm_buffer *vms_shm_buffer_create_sub_buffer( + vms_shm_buffer *buffer, size_t capacity, + const struct vms_source_control *control); +size_t vms_shm_buffer_get_sub_buffers_no(vms_shm_buffer *buffer); + +void vms_shm_buffer_destroy(vms_shm_buffer *); +void vms_shm_buffer_destroy_sub_buffer(vms_shm_buffer *); + +bool vms_shm_buffer_push(vms_shm_buffer *buff, const void *elem, size_t size); +void *vms_shm_buffer_start_push(vms_shm_buffer *buff); + +void *vms_shm_buffer_partial_push(vms_shm_buffer *buff, void *prev_push, + const void *elem, size_t size); /* evid may be ~(0LL), but then there's no garbage collection */ -void *buffer_partial_push_str(struct buffer *buff, void *prev_push, - uint64_t evid, const char *str); -void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, - uint64_t evid, const char *str, size_t len); -void buffer_finish_push(struct buffer *buff); +void *vms_shm_buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str); +void *vms_shm_buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str, + size_t len); +void vms_shm_buffer_finish_push(vms_shm_buffer *buff); + +/* ---------------------------------------------------------------------- */ +/* READER API */ +/* ---------------------------------------------------------------------- */ +vms_shm_buffer *vms_shm_buffer_try_connect(const char *key, size_t retry); +vms_shm_buffer *vms_shm_buffer_connect(const char *key); +struct vms_event_record *vms_shm_buffer_get_avail_events(vms_shm_buffer *, + size_t *); + +void vms_shm_buffer_release(vms_shm_buffer *); +void vms_shm_buffer_release_sub_buffer(vms_shm_buffer *); + +bool vms_shm_buffer_pop(vms_shm_buffer *buff, void *dst); +void *vms_shm_buffer_read_str(vms_shm_buffer *buff, uint64_t elem); +void *vms_shm_buffer_read_pointer(vms_shm_buffer *buff, size_t *size); +bool vms_shm_buffer_drop_k(vms_shm_buffer *buff, size_t size); +size_t vms_shm_buffer_consume(vms_shm_buffer *buff, size_t k); + +/* The reader is supposed to let the writer know what events it wants + * to receive. This is done via this set of "register_event" functions. + * These functions must be called before `set_reader_is_ready`. + */ +int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, + uint64_t kind); +int vms_shm_buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...); +int vms_shm_buffer_register_all_events(vms_shm_buffer *b); + +/* ---------------------------------------------------------------------- */ +/* GENERIC AND AUXILIARY API */ +/* ---------------------------------------------------------------------- */ + +size_t vms_shm_buffer_size(vms_shm_buffer *buff); +size_t vms_shm_buffer_capacity(vms_shm_buffer *buff); +size_t vms_shm_buffer_elem_size(vms_shm_buffer *buff); + +/* default flags */ +enum _vms_shm_buffer_flag { + READER_IS_READY = 1UL << 0, + READER_FINISHED = 1UL << 1, + EVENTS_REGISTERED = 1UL << 2, + LAST_FLAG = READER_FINISHED +}; + +uint64_t vms_shm_buffer_get_flags(vms_shm_buffer *); +void vms_shm_buffer_set_flags(vms_shm_buffer *, uint64_t); +void vms_shm_buffer_unset_flags(vms_shm_buffer *, uint64_t); + +/** + * @brief vms_shm_buffer_reader_is_ready checks if the reader has called + * vms_shm_buffer_set_reader_is_ready(); + * vms_shm_buffer_reader_is_ready and vms_shm_buffer_reader_is_ready _may_ be + * used to synchronize the writer and the reader before transmiting data. + */ +bool vms_shm_buffer_reader_is_ready(vms_shm_buffer *); +void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *); + +/* buffer is ready to transmit data (it can be written to if there is space) */ +bool vms_shm_buffer_is_ready(vms_shm_buffer *); +/* buffer was not destroyed on the writer's side */ +bool vms_shm_buffer_is_destroyed(vms_shm_buffer *); + +void vms_shm_buffer_set_destroyed(vms_shm_buffer *buff); + +const char *vms_shm_buffer_key(vms_shm_buffer *); + +int vms_shm_buffer_compute_key_path(vms_shm_buffer *, char keypath[], + size_t keypathsize); +int vms_shm_buffer_compute_ctrl_key_path(vms_shm_buffer *, char keypath[], + size_t keypathsize); +char *vms_shm_buffer_compute_sub_buffer_key(const char *key, size_t idx); + +/* ---------------------------------------------------------------------- */ +/* AUXILIARY BUFFERS */ +/* ---------------------------------------------------------------------- */ struct aux_buff_ptr { uint32_t buffer_id; uint32_t offset; } __attribute__((packed, aligned(64))); -void buffer_set_last_processed_id(struct buffer *buff, uint64_t id); -void buffer_notify_dropped(struct buffer *buffer, uint64_t begin_id, - uint64_t end_id); - -int buffer_register_event(struct buffer *b, const char *name, uint64_t kind); -int buffer_register_events(struct buffer *b, size_t ev_nums, ...); -int buffer_register_all_events(struct buffer *b); - -/** - * dbg buffers - */ - -typedef struct _vms_shm_dbg_buffer vms_shm_dbg_buffer; -vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, - uint16_t key_size, - uint16_t element_size); -vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key); -void vms_shm_dbg_buffer_release(vms_shm_dbg_buffer *); -void vms_shm_dbg_buffer_destroy(vms_shm_dbg_buffer *); - -size_t vms_shm_dbg_buffer_size(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_capacity(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_key_size(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_value_size(vms_shm_dbg_buffer *b); -size_t vms_shm_dbg_buffer_rec_size(vms_shm_dbg_buffer *b); -unsigned char *vms_shm_dbg_buffer_data(vms_shm_dbg_buffer *b); - -void vms_shm_dbg_buffer_inc_size(vms_shm_dbg_buffer *b, size_t size); -size_t vms_shm_dbg_buffer_version(vms_shm_dbg_buffer *b); -void vms_shm_dbg_buffer_bump_version(vms_shm_dbg_buffer *b); - -#endif /* SHAMON_SHM_BUFFER_H */ +void vms_shm_buffer_set_last_processed_id(vms_shm_buffer *buff, uint64_t id); +void vms_shm_buffer_notify_dropped(vms_shm_buffer *buffer, uint64_t begin_id, + uint64_t end_id); +#endif /* VAMOS_SHM_BUFFER_H */ diff --git a/include/vamos-buffers/shmbuf/client.h b/include/vamos-buffers/shmbuf/client.h index 2a007237..a01aafa1 100644 --- a/include/vamos-buffers/shmbuf/client.h +++ b/include/vamos-buffers/shmbuf/client.h @@ -1,8 +1,14 @@ -#ifndef SHAMON_CLIENT_H -#define SHAMON_CLIENT_H +#ifndef VAMOS_CLIENT_H +#define VAMOS_CLIENT_H -struct buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; -int buffer_wait_for_monitor(struct buffer *); +/** + * @brief vms_shm_buffer_wait_for_reader waits until a reader connects to the + * vms_shm_buffer + * @param buff + * @return negative errno value on error, otherwise 0 + */ +int vms_shm_buffer_wait_for_reader(vms_shm_buffer *); #endif diff --git a/include/vamos-buffers/shmbuf/dbg.h b/include/vamos-buffers/shmbuf/dbg.h new file mode 100644 index 00000000..00b0afab --- /dev/null +++ b/include/vamos-buffers/shmbuf/dbg.h @@ -0,0 +1,27 @@ +#ifndef VAMOS_SHM_DBG_BUFFER_H +#define VAMOS_SHM_DBG_BUFFER_H + +/** + * dbg buffers + */ + +typedef struct _vms_shm_dbg_buffer vms_shm_dbg_buffer; +vms_vms_dbg_buffer *vms_vms_dbg_buffer_create(const char *key, size_t capacity, + uint16_t key_size, + uint16_t element_size); +vms_vms_dbg_buffer *vms_vms_dbg_buffer_get(const char *key); +void vms_vms_dbg_buffer_release(vms_vms_dbg_buffer *); +void vms_vms_dbg_buffer_destroy(vms_vms_dbg_buffer *); + +size_t vms_vms_dbg_buffer_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_capacity(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_key_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_value_size(vms_vms_dbg_buffer *b); +size_t vms_vms_dbg_buffer_rec_size(vms_vms_dbg_buffer *b); +unsigned char *vms_vms_dbg_buffer_data(vms_vms_dbg_buffer *b); + +void vms_vms_dbg_buffer_inc_size(vms_vms_dbg_buffer *b, size_t size); +size_t vms_vms_dbg_buffer_version(vms_vms_dbg_buffer *b); +void vms_vms_dbg_buffer_bump_version(vms_vms_dbg_buffer *b); + +#endif diff --git a/include/vamos-buffers/streams/stream-drregex.h b/include/vamos-buffers/streams/stream-drregex.h index a5ee061b..8c277686 100644 --- a/include/vamos-buffers/streams/stream-drregex.h +++ b/include/vamos-buffers/streams/stream-drregex.h @@ -6,8 +6,8 @@ #define DRREGEX_ONLY_ARGS -typedef struct _shm_event_drregex { - shm_event base; +typedef struct _vms_event_drregex { + vms_event base; #ifndef DRREGEX_ONLY_ARGS bool write; /* true = write, false = read */ int fd; @@ -15,12 +15,12 @@ typedef struct _shm_event_drregex { #endif /* the event arguments */ unsigned char args[]; -} shm_event_drregex; +} vms_event_drregex; -typedef struct _shm_stream_drregex { - shm_stream base; - struct buffer *shmbuffer; -} shm_stream_drregex; +typedef struct _vms_stream_drregex { + vms_stream base; + vms_shm_buffer *shmbuffer; +} vms_stream_drregex; -shm_stream *shm_create_drregex_stream(const char *key, const char *name); -void shm_destroy_drregex_stream(shm_stream_drregex *ss); +vms_stream *vms_create_drregex_stream(const char *key, const char *name); +void vms_destroy_drregex_stream(vms_stream_drregex *ss); diff --git a/include/vamos-buffers/streams/stream-funs.h b/include/vamos-buffers/streams/stream-funs.h index c51ac20c..26d960d2 100644 --- a/include/vamos-buffers/streams/stream-funs.h +++ b/include/vamos-buffers/streams/stream-funs.h @@ -4,34 +4,34 @@ #include "vamos-buffers/core/event.h" #include "vamos-buffers/core/stream.h" -typedef struct _shm_event_funcall { - shm_event base; +typedef struct _vms_event_funcall { + vms_event base; /* the event arguments */ char signature[8]; unsigned char args[]; -} shm_event_funcall; +} vms_event_funcall; -typedef struct _shm_stream_funs { - shm_stream base; - struct buffer *shmbuffer; - struct event_record *events; +typedef struct _vms_stream_funs { + vms_stream base; + vms_shm_buffer *shmbuffer; + struct vms_event_record *events; /* buffer for reading and publishing the event */ - shm_event_funcall *ev; + vms_event_funcall *ev; void *ev_buff; size_t spec_count; -} shm_stream_funs; +} vms_stream_funs; -bool funs_has_event(shm_stream *stream); +bool funs_has_event(vms_stream *stream); -shm_event_funcall *funs_get_next_event(shm_stream *stream); +vms_event_funcall *funs_get_next_event(vms_stream *stream); -shm_stream *shm_create_funs_stream(const char *key, const char *name); +vms_stream *vms_create_funs_stream(const char *key, const char *name); -struct event_record *shm_funs_stream_get_event_spec(shm_stream_funs *stream, - shm_kind kind); +struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, + vms_kind kind); -void shm_event_funcall_release(shm_event_funcall *fev); +void vms_event_funcall_release(vms_event_funcall *fev); -const char *shm_stream_funs_get_str(shm_stream_funs *fstream, uint64_t elem); +const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem); -void shm_destroy_funs_stream(shm_stream_funs *ss); +void vms_destroy_funs_stream(vms_stream_funs *ss); diff --git a/include/vamos-buffers/streams/stream-generic.h b/include/vamos-buffers/streams/stream-generic.h index 78feb2a5..04514052 100644 --- a/include/vamos-buffers/streams/stream-generic.h +++ b/include/vamos-buffers/streams/stream-generic.h @@ -7,24 +7,24 @@ #include "vamos-buffers/core/event.h" #include "vamos-buffers/core/stream.h" - -struct buffer; +typedef struct _vms_shm_buffer vms_shm_buffer; /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ -typedef struct _shm_event_generic { - shm_event base; +typedef struct _vms_event_generic { + vms_event base; /* the event arguments */ unsigned char args[]; -} shm_event_generic; +} vms_event_generic; -typedef struct _shm_stream_generic { - shm_stream base; - struct buffer *shmbuffer; -} shm_stream_generic; +typedef struct _vms_stream_generic { + vms_stream base; + vms_shm_buffer *shmbuffer; +} vms_stream_generic; -shm_stream *shm_create_generic_stream(const char *key, const char *name, - shm_stream_hole_handling *hole_handling); +vms_stream *vms_create_generic_stream( + const char *key, const char *name, + const vms_stream_hole_handling *hole_handling); #endif /* SHMN_STREAM_GENERIC_H_ */ diff --git a/include/vamos-buffers/streams/stream-regex.h b/include/vamos-buffers/streams/stream-regex.h index 075f7983..8d6245f4 100644 --- a/include/vamos-buffers/streams/stream-regex.h +++ b/include/vamos-buffers/streams/stream-regex.h @@ -2,21 +2,21 @@ #include #include "vamos-buffers/core/event.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/shmbuf/buffer.h" -typedef struct _shm_event_regex { - shm_event base; +typedef struct _vms_event_regex { + vms_event base; /* the event arguments */ unsigned char args[]; -} shm_event_regex; +} vms_event_regex; -typedef struct _shm_stream_sregex { - shm_stream base; - struct buffer *shmbuffer; -} shm_stream_sregex; +typedef struct _vms_stream_sregex { + vms_stream base; + vms_shm_buffer *shmbuffer; +} vms_stream_sregex; -shm_stream *shm_create_sregex_stream( +vms_stream *vms_create_sregex_stream( const char *key, const char *name, - const shm_stream_hole_handling *hole_handling); -void shm_destroy_sregex_stream(shm_stream_sregex *ss); + const vms_stream_hole_handling *hole_handling); +void vms_destroy_sregex_stream(vms_stream_sregex *ss); diff --git a/include/vamos-buffers/streams/stream-regexrw.h b/include/vamos-buffers/streams/stream-regexrw.h index 119c667e..98caa6f8 100644 --- a/include/vamos-buffers/streams/stream-regexrw.h +++ b/include/vamos-buffers/streams/stream-regexrw.h @@ -2,19 +2,19 @@ #include #include "vamos-buffers/core/event.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/shmbuf/buffer.h" -typedef struct _shm_event_regexrw { - shm_event base; +typedef struct _vms_event_regexrw { + vms_event base; /* the event arguments */ unsigned char args[]; -} shm_event_regexrw; +} vms_event_regexrw; -typedef struct _shm_stream_sregexrw { - shm_stream base; - struct buffer *shmbuffer; -} shm_stream_sregexrw; +typedef struct _vms_stream_sregexrw { + vms_stream base; + vms_shm_buffer *shmbuffer; +} vms_stream_sregexrw; -shm_stream *shm_create_sregexrw_stream(const char *key, const char *name); -void shm_destroy_sregexrw_stream(shm_stream_sregexrw *ss); +vms_stream *vms_create_sregexrw_stream(const char *key, const char *name); +void vms_destroy_sregexrw_stream(vms_stream_sregexrw *ss); diff --git a/include/vamos-buffers/streams/streams.h b/include/vamos-buffers/streams/streams.h index bae0d0e1..2c75a32e 100644 --- a/include/vamos-buffers/streams/streams.h +++ b/include/vamos-buffers/streams/streams.h @@ -1,12 +1,12 @@ -#ifndef SHAMON_STREAMS_H_ -#define SHAMON_STREAMS_H_ +#ifndef VAMOS_STREAMS_H_ +#define VAMOS_STREAMS_H_ #include "vamos-buffers/core/stream.h" -shm_stream *shm_stream_create_from_argv( +vms_stream *vms_stream_create_from_argv( const char *stream_name, int argc, char *argv[], - const shm_stream_hole_handling *hole_handling); -shm_stream *shm_stream_create(const char *stream_name, const char *spec, - const shm_stream_hole_handling *hole_handling); + const vms_stream_hole_handling *hole_handling); +vms_stream *vms_stream_create(const char *stream_name, const char *spec, + const vms_stream_hole_handling *hole_handling); -#endif /* SHAMON_STREAMS_H_ */ +#endif /* VAMOS_STREAMS_H_ */ diff --git a/install.sh b/install.sh index ef7e0e4e..b6e550f5 100755 --- a/install.sh +++ b/install.sh @@ -1,10 +1,10 @@ #!/usr/bin/env bash -SHAMONDIR=$(readlink -f $(dirname $0)) -echo $SHAMONDIR +VAMOSDIR=$(readlink -f $(dirname $0)) +echo $VAMOSDIR for FILE in bin/*; do - echo $SHAMONDIR/$FILE; - sed "s|\$BASEPATH/shamon|$SHAMONDIR|" $FILE > tmpfile + echo $VAMOSDIR/$FILE; + sed "s|\$BASEPATH/shamon|$VAMOSDIR|" $FILE > tmpfile sudo chmod +xxx tmpfile sudo mv tmpfile /usr/$FILE done diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 6cf6bd89..54b6a3a6 100755 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -3,7 +3,7 @@ add_library(vamos-buffers-list STATIC list.c list-embedded.c) add_library(vamos-buffers-event STATIC event.c) add_library(vamos-buffers-queue-spsc STATIC queue_spsc.c) add_library(vamos-buffers-vector STATIC vector.c vector-aligned.c) -add_library(vamos-buffers-string STATIC shm_string.c) +add_library(vamos-buffers-string STATIC vms_string.c) add_library(vamos-buffers-source STATIC source.c) add_library(vamos-buffers-stream STATIC stream.c) add_library(vamos-buffers-arbiter STATIC arbiter.c) @@ -16,6 +16,7 @@ add_library(vamos-buffers-monitor-buffer STATIC monitor.c) set_property(TARGET vamos-buffers-utils PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-source PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-arbiter PROPERTY POSITION_INDEPENDENT_CODE 1) +set_property(TARGET vamos-buffers-event PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-signature PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-stream PROPERTY POSITION_INDEPENDENT_CODE 1) set_property(TARGET vamos-buffers-shamon PROPERTY POSITION_INDEPENDENT_CODE 1) diff --git a/src/core/arbiter.c b/src/core/arbiter.c index e57b2b6e..0fbc5c98 100644 --- a/src/core/arbiter.c +++ b/src/core/arbiter.c @@ -1,16 +1,17 @@ +#include "vamos-buffers/core/arbiter.h" + #include #include #include -#include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/core/par_queue.h" #include "vamos-buffers/core/stream.h" #include "vamos-buffers/core/utils.h" #define DROP_SPACE_DEFAULT_THRESHOLD 1 -typedef struct _shm_arbiter_buffer { - shm_par_queue buffer; // the buffer itself +typedef struct _vms_arbiter_buffer { + vms_par_queue buffer; // the buffer itself size_t drop_space_threshold; // the number of elements to keep free before // pushing dropped() event size_t dropped_num; // the number of dropped events @@ -24,44 +25,44 @@ typedef struct _shm_arbiter_buffer { int last_was_drop; // true if the last event written was drop() size_t waited_to_push; // how many times the buffer waited to push #endif - shm_eventid drop_begin_id; // the id of the next 'dropped' event + vms_eventid drop_begin_id; // the id of the next 'dropped' event - shm_stream *stream; // the source for the buffer - shm_event *hole_event; + vms_stream *stream; // the source for the buffer + vms_event *hole_event; bool active; // true while the events are being queued -} shm_arbiter_buffer; +} vms_arbiter_buffer; -size_t shm_arbiter_buffer_sizeof(void) { return sizeof(shm_arbiter_buffer); } +size_t vms_arbiter_buffer_sizeof(void) { return sizeof(vms_arbiter_buffer); } -void *shm_arbiter_buffer_write_ptr(shm_arbiter_buffer *q) { - return shm_par_queue_write_ptr(&q->buffer); +void *vms_arbiter_buffer_write_ptr(vms_arbiter_buffer *q) { + return vms_par_queue_write_ptr(&q->buffer); } -void shm_arbiter_buffer_write_finish(shm_arbiter_buffer *q) { +void vms_arbiter_buffer_write_finish(vms_arbiter_buffer *q) { #ifdef DUMP_STATS ++q->written_num; #endif - shm_par_queue_write_finish(&q->buffer); + vms_par_queue_write_finish(&q->buffer); } -void shm_arbiter_buffer_finish_push(shm_arbiter_buffer *q); +void vms_arbiter_buffer_finish_push(vms_arbiter_buffer *q); -size_t shm_arbiter_buffer_size(shm_arbiter_buffer *buffer) { - return shm_par_queue_size(&buffer->buffer); +size_t vms_arbiter_buffer_size(vms_arbiter_buffer *buffer) { + return vms_par_queue_size(&buffer->buffer); } -size_t shm_arbiter_buffer_free_space(shm_arbiter_buffer *buffer) { - return shm_par_queue_free_num(&buffer->buffer); +size_t vms_arbiter_buffer_free_space(vms_arbiter_buffer *buffer) { + return vms_par_queue_free_num(&buffer->buffer); } -size_t shm_arbiter_buffer_capacity(shm_arbiter_buffer *buffer) { - return shm_par_queue_capacity(&buffer->buffer); +size_t vms_arbiter_buffer_capacity(vms_arbiter_buffer *buffer) { + return vms_par_queue_capacity(&buffer->buffer); } -size_t shm_arbiter_buffer_set_drop_space_threshold(shm_arbiter_buffer *buffer, +size_t vms_arbiter_buffer_set_drop_space_threshold(vms_arbiter_buffer *buffer, size_t thr) { assert(thr >= 1); - assert(thr < shm_arbiter_buffer_capacity(buffer)); + assert(thr < vms_arbiter_buffer_capacity(buffer)); size_t old = buffer->drop_space_threshold; buffer->drop_space_threshold = thr; @@ -70,22 +71,22 @@ size_t shm_arbiter_buffer_set_drop_space_threshold(shm_arbiter_buffer *buffer, /* drop an event and notify buffer the buffer that it may free up * the payload of this and older events */ -size_t shm_arbiter_buffer_drop(shm_arbiter_buffer *buffer, size_t k) { +size_t vms_arbiter_buffer_drop(vms_arbiter_buffer *buffer, size_t k) { #ifdef DUMP_STATS buffer->volunt_dropped_num_asked += k; #endif --k; /* peek_*_at takes index from 0 */ - shm_event *ev = shm_par_queue_peek_atmost_at(&buffer->buffer, &k); + vms_event *ev = vms_par_queue_peek_atmost_at(&buffer->buffer, &k); if (!ev) return 0; /* empty queue */ - shm_eventid last_id = shm_event_id(ev); + vms_eventid last_id = vms_event_id(ev); #ifndef NDEBUG size_t n = #endif ++k; /* k is index, we must increase it back by one */ - shm_par_queue_drop(&buffer->buffer, k); + vms_par_queue_drop(&buffer->buffer, k); assert(n == k && "Something changed the queue in between"); - shm_stream_notify_last_processed_id(buffer->stream, last_id); + vms_stream_notify_last_processed_id(buffer->stream, last_id); #ifdef DUMP_STATS buffer->volunt_dropped_num += k; #endif @@ -94,26 +95,26 @@ size_t shm_arbiter_buffer_drop(shm_arbiter_buffer *buffer, size_t k) { /* Drop all events with ID less or equal to the one of ev. * Return how many events were dropped */ -size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, - shm_eventid id) { +size_t vms_arbiter_buffer_drop_older_than(vms_arbiter_buffer *buffer, + vms_eventid id) { /* we first must find the event in the queue */ void *ptr1, *ptr2; size_t len1, len2; const size_t n = - shm_par_queue_peek(&buffer->buffer, 0, &ptr1, &len1, &ptr2, &len2); + vms_par_queue_peek(&buffer->buffer, 0, &ptr1, &len1, &ptr2, &len2); if (n == 0) return 0; - const size_t elem_size = shm_par_queue_elem_size(&buffer->buffer); + const size_t elem_size = vms_par_queue_elem_size(&buffer->buffer); size_t k; /* the number of events to be dropped */ size_t bot = 0, top; unsigned char *events; assert(len1 > 0 && "Underflow"); top = len1 - 1; - shm_event *ev = (shm_event *)((unsigned char *)ptr1 + (top)*elem_size); + vms_event *ev = (vms_event *)((unsigned char *)ptr1 + (top)*elem_size); /* in which part of the buffer may the event be? */ - if (id <= shm_event_id(ev)) { + if (id <= vms_event_id(ev)) { events = ptr1; } else if (len2 > 0) { events = ptr2; @@ -124,8 +125,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, k = len1; /* now consume everything up to the found event */ if (k > 0) { - shm_par_queue_drop(&buffer->buffer, k); - shm_stream_notify_last_processed_id(buffer->stream, id); + vms_par_queue_drop(&buffer->buffer, k); + vms_stream_notify_last_processed_id(buffer->stream, id); #ifdef DUMP_STATS buffer->volunt_dropped_num_asked += k; @@ -147,8 +148,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, /* the search should terminate much earlier then after n steps */ assert(steps++ < n && "BUG in binary search"); - ev = (shm_event *)(events + pivot * elem_size); - if (id < shm_event_id(ev)) { + ev = (vms_event *)(events + pivot * elem_size); + if (id < vms_event_id(ev)) { if (pivot == 0) { assert(bot == 0); assert(top == 1); @@ -160,7 +161,7 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, } else { if (bot == pivot) { assert(top == bot + 1); - if (shm_event_id(ev) == id) { + if (vms_event_id(ev) == id) { top = bot; } else { bot = top; @@ -172,8 +173,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, assert(bot <= top); if (top == bot) { - ev = (shm_event *)(events + top * elem_size); - if (id < shm_event_id(ev)) { + ev = (vms_event *)(events + top * elem_size); + if (id < vms_event_id(ev)) { /* the found event is the one to the left */ if (bot == 0) { /* There is no event to the left in this part of the buffer. @@ -199,8 +200,8 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, /* now consume everything up to the found event */ if (k > 0) { - shm_par_queue_drop(&buffer->buffer, k); - shm_stream_notify_last_processed_id(buffer->stream, id); + vms_par_queue_drop(&buffer->buffer, k); + vms_stream_notify_last_processed_id(buffer->stream, id); #ifdef DUMP_STATS buffer->volunt_dropped_num_asked += k; @@ -211,38 +212,38 @@ size_t shm_arbiter_buffer_drop_older_than(shm_arbiter_buffer *buffer, return k; } -bool shm_arbiter_buffer_active(shm_arbiter_buffer *buffer) { +bool vms_arbiter_buffer_active(vms_arbiter_buffer *buffer) { return buffer->active; } -void shm_arbiter_buffer_set_active(shm_arbiter_buffer *buffer, bool val) { +void vms_arbiter_buffer_set_active(vms_arbiter_buffer *buffer, bool val) { buffer->active = val; if (val) { - shm_stream_attach(buffer->stream); + vms_stream_attach(buffer->stream); } else { - shm_stream_attach(buffer->stream); + vms_stream_detach(buffer->stream); } } -shm_stream *shm_arbiter_buffer_stream(shm_arbiter_buffer *buffer) { +vms_stream *vms_arbiter_buffer_stream(vms_arbiter_buffer *buffer) { return buffer->stream; } -size_t shm_arbiter_buffer_dropped_num(shm_arbiter_buffer *buffer) { +size_t vms_arbiter_buffer_dropped_num(vms_arbiter_buffer *buffer) { return buffer->total_dropped_num; } -size_t shm_arbiter_buffer_dropped_times(shm_arbiter_buffer *buffer) { +size_t vms_arbiter_buffer_dropped_times(vms_arbiter_buffer *buffer) { return buffer->total_dropped_times; } #ifdef DUMP_STATS -size_t shm_arbiter_buffer_written_num(shm_arbiter_buffer *buffer) { +size_t vms_arbiter_buffer_written_num(vms_arbiter_buffer *buffer) { return buffer->written_num; } #endif -void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, +void vms_arbiter_buffer_init(vms_arbiter_buffer *buffer, vms_stream *stream, size_t out_event_size, size_t capacity) { assert(ADDR_IS_CACHE_ALIGNED(buffer) && "The memory for the buffer is missaligned"); @@ -257,7 +258,7 @@ void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, if (hole_event_size > 0 && event_size < hole_event_size) event_size = hole_event_size; - shm_par_queue_init(&buffer->buffer, capacity, event_size); + vms_par_queue_init(&buffer->buffer, capacity, event_size); buffer->drop_space_threshold = DROP_SPACE_DEFAULT_THRESHOLD; buffer->hole_event = xalloc(stream->hole_handling.hole_event_size); @@ -276,34 +277,34 @@ void shm_arbiter_buffer_init(shm_arbiter_buffer *buffer, shm_stream *stream, #endif } -shm_arbiter_buffer *shm_arbiter_buffer_create(shm_stream *stream, +vms_arbiter_buffer *vms_arbiter_buffer_create(vms_stream *stream, size_t out_event_size, size_t capacity) { /* some elements are cache-aligned, so we must make sure * that the memory is not allocated mis-aligned */ - shm_arbiter_buffer *b = - xalloc_aligned(shm_arbiter_buffer_sizeof(), CACHELINE_SIZE); - shm_arbiter_buffer_init(b, stream, out_event_size, capacity); + vms_arbiter_buffer *b = + xalloc_aligned(vms_arbiter_buffer_sizeof(), CACHELINE_SIZE); + vms_arbiter_buffer_init(b, stream, out_event_size, capacity); return b; } -void shm_arbiter_buffer_free(shm_arbiter_buffer *buffer) { - shm_arbiter_buffer_destroy(buffer); +void vms_arbiter_buffer_free(vms_arbiter_buffer *buffer) { + vms_arbiter_buffer_destroy(buffer); free(buffer); } -void shm_arbiter_buffer_destroy(shm_arbiter_buffer *buffer) { - shm_par_queue_destroy(&buffer->buffer); +void vms_arbiter_buffer_destroy(vms_arbiter_buffer *buffer) { + vms_par_queue_destroy(&buffer->buffer); free(buffer->hole_event); } -size_t shm_arbiter_buffer_elem_size(shm_arbiter_buffer *q) { - return shm_par_queue_elem_size(&q->buffer); +size_t vms_arbiter_buffer_elem_size(vms_arbiter_buffer *q) { + return vms_par_queue_elem_size(&q->buffer); } -void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, +void vms_arbiter_buffer_push(vms_arbiter_buffer *buffer, const void *elem, size_t size) { - assert(shm_arbiter_buffer_active(buffer)); - while (!shm_par_queue_push(&buffer->buffer, elem, size)) { + assert(vms_arbiter_buffer_active(buffer)); + while (!vms_par_queue_push(&buffer->buffer, elem, size)) { #ifdef DUMP_STATS ++buffer->waited_to_push; #endif @@ -312,24 +313,24 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, } #if 0 -void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, +void vms_arbiter_buffer_push(vms_arbiter_buffer *buffer, const void *elem, size_t size) { - assert(shm_arbiter_buffer_active(buffer)); - shm_par_queue *queue = &buffer->buffer; + assert(vms_arbiter_buffer_active(buffer)); + vms_par_queue *queue = &buffer->buffer; if (buffer->dropped_num > 0) { - if (shm_par_queue_free_num(queue) < 2) { + if (vms_par_queue_free_num(queue) < 2) { ++buffer->dropped_num; } else { - shm_stream_prepare_hole_event(buffer->stream, buffer->hole_event, + vms_stream_prepare_hole_event(buffer->stream, buffer->hole_event, buffer->drop_begin_id, buffer->dropped_num); assert(buffer->stream->hole_handling.hole_event_size <= - shm_par_queue_elem_size(queue)); + vms_par_queue_elem_size(queue)); #ifndef NDEBUG bool ret = #endif - shm_par_queue_push( + vms_par_queue_push( queue, buffer->hole_event, buffer->stream->hole_handling.hole_event_size); #ifdef DUMP_STATS @@ -339,7 +340,7 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, #ifndef NDEBUG ret = #endif - shm_par_queue_push(&buffer->buffer, elem, size); + vms_par_queue_push(&buffer->buffer, elem, size); #ifdef DUMP_STATS ++buffer->written_num; #endif @@ -349,13 +350,13 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, buffer->dropped_num = 0; /* the end id may not be precise, but we need just the upper bound */ - shm_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, - shm_event_id((shm_event *)elem) - + vms_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, + vms_event_id((vms_event *)elem) - 1); } } else { - if (!shm_par_queue_push(&buffer->buffer, elem, size)) { - buffer->drop_begin_id = shm_event_id((shm_event *)elem); + if (!vms_par_queue_push(&buffer->buffer, elem, size)) { + buffer->drop_begin_id = vms_event_id((vms_event *)elem); ++buffer->dropped_num; } #ifdef DUMP_STATS @@ -369,27 +370,27 @@ void shm_arbiter_buffer_push(shm_arbiter_buffer *buffer, const void *elem, /* NOTE: does not notify about processing the event, must * be done manually once all the work with data is done */ -bool shm_arbiter_buffer_pop(shm_arbiter_buffer *buffer, void *elem) { - return shm_par_queue_pop(&buffer->buffer, elem); +bool vms_arbiter_buffer_pop(vms_arbiter_buffer *buffer, void *elem) { + return vms_par_queue_pop(&buffer->buffer, elem); } -shm_event *shm_arbiter_buffer_top(shm_arbiter_buffer *buffer) { - return shm_par_queue_top(&buffer->buffer); +vms_event *vms_arbiter_buffer_top(vms_arbiter_buffer *buffer) { + return vms_par_queue_top(&buffer->buffer); } -size_t shm_arbiter_buffer_peek(shm_arbiter_buffer *buffer, size_t n, +size_t vms_arbiter_buffer_peek(vms_arbiter_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2) { - return shm_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); + return vms_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); } -size_t shm_arbiter_buffer_peek1(shm_arbiter_buffer *buffer, void **data) { - return shm_par_queue_peek1(&buffer->buffer, data); +size_t vms_arbiter_buffer_peek1(vms_arbiter_buffer *buffer, void **data) { + return vms_par_queue_peek1(&buffer->buffer, data); } /* get an event from the stream, block until there is some and return it * or return NULL if the stream ended */ -static void *get_event(shm_stream *stream) { +static void *get_event(vms_stream *stream) { /* TODO: if there is no filtering and modifications, we can push multiple events forward. if there are filtering and modifications, we could have an additional thread to handle the load of data if we copy them in chunks @@ -400,7 +401,7 @@ static void *get_event(shm_stream *stream) { void *ev; while (1) { /* wait for the event */ - ev = shm_stream_read_events(stream, &num); + ev = vms_stream_read_events(stream, &num); if (ev) { #ifdef DUMP_STATS ++stream->read_events; @@ -424,7 +425,7 @@ static void *get_event(shm_stream *stream) { /* checking for the readiness is not cheap, * so do it only after we haven't read any * event for some time */ - if (!shm_stream_is_ready(stream)) { + if (!vms_stream_is_ready(stream)) { return NULL; } } @@ -434,40 +435,40 @@ static void *get_event(shm_stream *stream) { assert(0 && "Unreachable"); } -static void push_dropped_event(shm_stream *stream, shm_arbiter_buffer *buffer, +static void push_dropped_event(vms_stream *stream, vms_arbiter_buffer *buffer, size_t notify_id) { - shm_stream_prepare_hole_event(stream, buffer->hole_event, notify_id, + vms_stream_prepare_hole_event(stream, buffer->hole_event, notify_id, buffer->dropped_num); - shm_par_queue_push(&buffer->buffer, buffer->hole_event, + vms_par_queue_push(&buffer->buffer, buffer->hole_event, stream->hole_handling.hole_event_size); #ifdef DUMP_STATS ++buffer->written_num; #endif buffer->total_dropped_num += buffer->dropped_num; ++buffer->total_dropped_times; - shm_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, notify_id); - assert(shm_arbiter_buffer_free_space(buffer) > 0); + vms_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, notify_id); + assert(vms_arbiter_buffer_free_space(buffer) > 0); /* printf("PUSHED DROPPED event { kind = %lu, id = %lu, n = %lu}\n", - ((shm_event*)&dropped_ev)->kind, - ((shm_event*)&dropped_ev)->id, + ((vms_event*)&dropped_ev)->kind, + ((vms_event*)&dropped_ev)->id, dropped_ev.n); */ } -void *handle_stream_end(shm_stream *stream, shm_arbiter_buffer *buffer, +void *handle_stream_end(vms_stream *stream, vms_arbiter_buffer *buffer, size_t last_ev_id) { uint64_t sleep_time = SLEEP_TIME_INIT_NS; while (buffer->dropped_num > 0) { assert(buffer->drop_space_threshold < - shm_arbiter_buffer_capacity(buffer)); - if (shm_arbiter_buffer_free_space(buffer) > + vms_arbiter_buffer_capacity(buffer)); + if (vms_arbiter_buffer_free_space(buffer) > buffer->drop_space_threshold) { /* the end id may not be precise, but we need just the upper * bound */ push_dropped_event(stream, buffer, last_ev_id - 1); - assert(shm_arbiter_buffer_free_space(buffer) > 0); + assert(vms_arbiter_buffer_free_space(buffer) > 0); #ifdef DUMP_STATS buffer->last_was_drop = 1; #endif @@ -481,13 +482,13 @@ void *handle_stream_end(shm_stream *stream, shm_arbiter_buffer *buffer, assert(buffer->dropped_num > 0); } } - assert(!shm_stream_is_ready(stream)); + assert(!vms_stream_is_ready(stream)); assert(buffer->dropped_num == 0); return NULL; /* stream ended */ } -static inline void send_dropped_event(shm_stream *stream, - shm_arbiter_buffer *buffer, +static inline void send_dropped_event(vms_stream *stream, + vms_arbiter_buffer *buffer, size_t last_ev_id) { assert(last_ev_id == buffer->dropped_num + buffer->drop_begin_id && "Drop IDs are wrong"); @@ -495,16 +496,16 @@ static inline void send_dropped_event(shm_stream *stream, buffer->dropped_num = 0; /* printf("FETCH: stopped dropping { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ - assert(shm_arbiter_buffer_free_space(buffer) > 0); + assert(vms_arbiter_buffer_free_space(buffer) > 0); } -static inline void start_dropping(shm_stream *stream, - shm_arbiter_buffer *buffer, - shm_event *event) { - buffer->drop_begin_id = shm_event_id(event); +static inline void start_dropping(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_event *event) { + buffer->drop_begin_id = vms_event_id(event); assert(buffer->dropped_num == 0); ++buffer->dropped_num; @@ -513,15 +514,15 @@ static inline void start_dropping(shm_stream *stream, stream->hole_handling.update(buffer->hole_event, event); /* printf("FETCH: start dropping { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } -static inline void continue_dropping(shm_stream *stream, - shm_arbiter_buffer *buffer, - shm_event *event) { +static inline void continue_dropping(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_event *event) { ++buffer->dropped_num; stream->hole_handling.update(buffer->hole_event, event); @@ -529,24 +530,24 @@ static inline void continue_dropping(shm_stream *stream, * long time to generate the dropped event */ /* FIXME: % is slow... and make it configurable */ if (buffer->dropped_num % 10000 == 0) { - shm_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, - shm_event_id(event) - 1); + vms_arbiter_buffer_notify_dropped(buffer, buffer->drop_begin_id, + vms_event_id(event) - 1); } /* consume the dropped event */ - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } /* return true if the event should be dropped and false if the event should be forwarded */ -static bool handle_dropping_event(shm_stream *stream, - shm_arbiter_buffer *buffer, - shm_event *event) { +static bool handle_dropping_event(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_event *event) { if (buffer->dropped_num > 0) { assert(buffer->drop_space_threshold < - shm_arbiter_buffer_capacity(buffer)); - if (shm_arbiter_buffer_free_space(buffer) > + vms_arbiter_buffer_capacity(buffer)); + if (vms_arbiter_buffer_free_space(buffer) > buffer->drop_space_threshold) { - send_dropped_event(stream, buffer, shm_event_id(event)); + send_dropped_event(stream, buffer, vms_event_id(event)); return false; /* forward the current event */ } @@ -555,18 +556,37 @@ static bool handle_dropping_event(shm_stream *stream, } assert(buffer->dropped_num == 0); - if (shm_arbiter_buffer_free_space(buffer) == 0) { + if (vms_arbiter_buffer_free_space(buffer) == 0) { start_dropping(stream, buffer, event); return true; } - assert(shm_arbiter_buffer_free_space(buffer) > 0); + assert(vms_arbiter_buffer_free_space(buffer) > 0); assert(buffer->dropped_num == 0); return false; } /* wait for an event on the 'stream' */ -void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer) { +void *vms_stream_fetch(vms_stream *stream) { + void *ev; + if ((ev = get_event(stream))) { + assert(ev && "Dont have event"); + assert(!vms_event_is_hole((vms_event *)ev) && "Got dropped event"); + assert(vms_event_id(ev) == ++stream->last_event_id && + "IDs are inconsistent"); + /* + printf("FETCH: read event { kind = %lu, id = %lu}\n", + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); + */ + } + + return ev; +} + +/* Wait for an event on the 'stream'. If `buffer` is full, start dropping. */ +void *vms_stream_fetch_dropping(vms_stream *stream, + vms_arbiter_buffer *buffer) { void *ev; size_t last_ev_id = 1; while (1) { @@ -576,14 +596,14 @@ void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer) { } assert(ev && "Dont have event"); - assert(!shm_event_is_hole((shm_event *)ev) && "Got dropped event"); + assert(!vms_event_is_hole((vms_event *)ev) && "Got dropped event"); - last_ev_id = shm_event_id(ev); + last_ev_id = vms_event_id(ev); assert(last_ev_id == ++stream->last_event_id && "IDs are inconsistent"); /* printf("FETCH: read event { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ if (!handle_dropping_event(stream, buffer, ev)) { @@ -593,13 +613,14 @@ void *stream_fetch(shm_stream *stream, shm_arbiter_buffer *buffer) { return ev; } - /* got to next iteration to try the next event */ + /* go to next iteration to try the next event */ } } /* FIXME: do not duplicate the code */ -void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, - shm_stream_filter_fn filter) { +void *vms_stream_fetch_dropping_filter(vms_stream *stream, + vms_arbiter_buffer *buffer, + vms_stream_filter_fn filter) { void *ev; size_t last_ev_id = 1; while (1) { @@ -609,21 +630,21 @@ void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, } assert(ev && "Dont have event"); - assert(!shm_event_is_hole((shm_event *)ev) && "Got hole event"); + assert(!vms_event_is_hole((vms_event *)ev) && "Got hole event"); - last_ev_id = shm_event_id(ev); + last_ev_id = vms_event_id(ev); assert(last_ev_id == ++stream->last_event_id && "IDs are inconsistent"); if (filter && !filter(stream, ev)) { /* consume the filtered event */ - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); continue; } /* printf("FETCH: read event { kind = %lu, id = %lu}\n", - ((shm_event*)ev)->kind, - ((shm_event*)ev)->id); + ((vms_event*)ev)->kind, + ((vms_event*)ev)->id); */ if (!handle_dropping_event(stream, buffer, ev)) { @@ -633,17 +654,16 @@ void *stream_filter_fetch(shm_stream *stream, shm_arbiter_buffer *buffer, return ev; } - /* got to next iteration to try the next event */ + /* go to next iteration to try the next event */ } } - -bool shm_arbiter_buffer_is_done(shm_arbiter_buffer *buffer) { +bool vms_arbiter_buffer_is_done(vms_arbiter_buffer *buffer) { /* XXX: should we rather use a flag that we set to true when stream-fetch * knows that the stream is done? */ return (buffer->dropped_num == 0 && - shm_par_queue_size(&buffer->buffer) == 0) && - !shm_stream_is_ready(buffer->stream) && + vms_par_queue_size(&buffer->buffer) == 0) && + !vms_stream_is_ready(buffer->stream) && /* there is a race when the buffer may be empty, but after checking * the first part of the condition above (resulting in true) a new * event is put there. Then the SHM buffer gets destroyed and the @@ -652,13 +672,12 @@ bool shm_arbiter_buffer_is_done(shm_arbiter_buffer *buffer) { * event in it. So re-check once more that the buffer is really empty * after the stream was destroyed */ (buffer->dropped_num == 0 && - shm_par_queue_size(&buffer->buffer) == 0); + vms_par_queue_size(&buffer->buffer) == 0); } - -void shm_arbiter_buffer_notify_dropped(shm_arbiter_buffer *buffer, +void vms_arbiter_buffer_notify_dropped(vms_arbiter_buffer *buffer, uint64_t begin_id, uint64_t end_id) { - shm_stream_notify_dropped(buffer->stream, begin_id, end_id); + vms_stream_notify_dropped(buffer->stream, begin_id, end_id); } #ifdef DUMP_STATS @@ -669,8 +688,8 @@ void shm_arbiter_buffer_notify_dropped(shm_arbiter_buffer *buffer, } #define COLOR_RESET fprintf(stderr, "\033[0m"); -void shm_arbiter_buffer_dump_stats(shm_arbiter_buffer *buffer) { - shm_stream *s = buffer->stream; +void vms_arbiter_buffer_dump_stats(vms_arbiter_buffer *buffer) { + vms_stream *s = buffer->stream; fprintf(stderr, "-- Buffer for stream %lu (%s) --\n", s->id, s->name); fprintf(stderr, " Stream read %lu events from SHM\n", s->read_events); COLOR_RED_IF(s->read_events != s->consumed_events) @@ -684,10 +703,10 @@ void shm_arbiter_buffer_dump_stats(shm_arbiter_buffer *buffer) { s->last_event_id); COLOR_RESET #endif - fprintf(stderr, " stream_fetch() fetched %lu events\n", + fprintf(stderr, " vms_stream_fetch*() fetched %lu events\n", s->fetched_events); fprintf(stderr, - " stream_fetch() totally dropped %lu events in %lu holes\n", + " vms_stream_fetch*() totally dropped %lu events in %lu holes\n", buffer->total_dropped_num, buffer->total_dropped_times); fprintf(stderr, " Last event was drop: %s\n", buffer->last_was_drop ? "true" : "false"); diff --git a/src/core/event.c b/src/core/event.c index b7ce27ba..397aa39a 100644 --- a/src/core/event.c +++ b/src/core/event.c @@ -1,11 +1,14 @@ +#include "vamos-buffers/core/event.h" + #include #include #include -#include "vamos-buffers/core/event.h" - -static const shm_kind hole_kind = 1; -static const shm_kind last_special_kind = 1; +/** The kind of `hole` event that represents a gap **/ +static const vms_kind hole_kind = VMS_EVENT_HOLE_KIND; +/** The kind of `done` event that represents the end of stream **/ +static const vms_kind done_kind = VMS_EVENT_DONE_KIND; +static const vms_kind last_special_kind = VMS_EVENT_LAST_SPECIAL_KIND; void initialize_events(void) { assert(hole_kind == 1 && "We assume that the 'hole_kind' is 1 for now"); @@ -14,22 +17,35 @@ void initialize_events(void) { void deinitialize_events(void) {} -bool shm_event_is_hole(shm_event *ev) { +bool vms_event_is_hole(const vms_event *ev) { assert(hole_kind > 0); - return shm_event_kind(ev) == hole_kind; + return vms_event_kind(ev) == hole_kind; } -shm_kind shm_get_hole_kind(void) { +bool vms_event_is_done(const vms_event *ev) { + assert(done_kind > 0); + return vms_event_kind(ev) == done_kind; +} + +vms_kind vms_event_get_hole_kind(void) { assert(hole_kind > 0); assert(last_special_kind >= hole_kind); return hole_kind; } -shm_kind shm_get_last_special_kind(void) { +vms_kind vms_event_get_done_kind(void) { + assert(done_kind > 0); + assert(last_special_kind >= done_kind); + return done_kind; +} + +vms_kind vms_event_get_last_special_kind(void) { assert(last_special_kind > 0); return last_special_kind; } -shm_eventid shm_event_id(shm_event *event) { return event->id; } +vms_eventid vms_event_id(const vms_event *event) { return event->id; } + +void vms_event_set_id(vms_event *event, vms_eventid id) { event->id = id; } -shm_kind shm_event_kind(shm_event *event) { return event->kind; } +vms_kind vms_event_kind(const vms_event *event) { return event->kind; } diff --git a/src/core/legacy/queue-test.c b/src/core/legacy/queue-test.c index bbecf970..e58bfc8a 100644 --- a/src/core/legacy/queue-test.c +++ b/src/core/legacy/queue-test.c @@ -6,20 +6,20 @@ int main(void) { int i; int num[] = {0, 1, 2, 3}; - shm_queue q; - shm_queue_init(&q, 3, sizeof(int)); + vms_queue q; + vms_queue_init(&q, 3, sizeof(int)); - assert(shm_queue_pop(&q, &i) == false); - assert(shm_queue_pop(&q, &i) == false); - assert(shm_queue_push(&q, &num[1]) == true); - assert(shm_queue_push(&q, &num[2]) == true); - assert(shm_queue_push(&q, &num[3]) == true); - assert(shm_queue_push(&q, &num[3]) == false); - assert(shm_queue_pop(&q, &i) == true && i == 1); - assert(shm_queue_push(&q, &num[3]) == true); - assert(shm_queue_pop(&q, &i) == true && i == 2); - assert(shm_queue_pop(&q, &i) == true && i == 3); - assert(shm_queue_pop(&q, &i) == true && i == 3); + assert(vms_queue_pop(&q, &i) == false); + assert(vms_queue_pop(&q, &i) == false); + assert(vms_queue_push(&q, &num[1]) == true); + assert(vms_queue_push(&q, &num[2]) == true); + assert(vms_queue_push(&q, &num[3]) == true); + assert(vms_queue_push(&q, &num[3]) == false); + assert(vms_queue_pop(&q, &i) == true && i == 1); + assert(vms_queue_push(&q, &num[3]) == true); + assert(vms_queue_pop(&q, &i) == true && i == 2); + assert(vms_queue_pop(&q, &i) == true && i == 3); + assert(vms_queue_pop(&q, &i) == true && i == 3); - shm_queue_destroy(&q); + vms_queue_destroy(&q); } diff --git a/src/core/legacy/queue.c b/src/core/legacy/queue.c index d97c72ec..7dfbe5bf 100644 --- a/src/core/legacy/queue.c +++ b/src/core/legacy/queue.c @@ -5,7 +5,7 @@ #include #include -void shm_queue_init(shm_queue *q, size_t size, size_t elem_size) { +void vms_queue_init(vms_queue *q, size_t size, size_t elem_size) { assert(q); assert(size > 0); assert(elem_size > 0); @@ -16,13 +16,13 @@ void shm_queue_init(shm_queue *q, size_t size, size_t elem_size) { q->data = malloc(size * elem_size); } -void shm_queue_destroy(shm_queue *q) { free(q->data); } +void vms_queue_destroy(vms_queue *q) { free(q->data); } // allocate space for one element // and return the pointer to this element. // If there is no room, NULL is returned. // The returned memory can be filled manually. -void *shm_queue_extend(shm_queue *q) { +void *vms_queue_extend(vms_queue *q) { // queue is full if (q->elem_num == q->size) return NULL; @@ -40,8 +40,8 @@ void *shm_queue_extend(shm_queue *q) { return pos; } -bool shm_queue_push(shm_queue *q, const void *elem) { - void *pos = shm_queue_extend(q); +bool vms_queue_push(vms_queue *q, const void *elem) { + void *pos = vms_queue_extend(q); if (pos) { memcpy(pos, elem, q->elem_size); return true; @@ -49,7 +49,7 @@ bool shm_queue_push(shm_queue *q, const void *elem) { return false; } -bool shm_queue_pop(shm_queue *q, void *buff) { +bool vms_queue_pop(vms_queue *q, void *buff) { if (q->elem_num == 0) { return false; } @@ -63,6 +63,6 @@ bool shm_queue_pop(shm_queue *q, void *buff) { return true; } -size_t shm_queue_max_size(shm_queue *q) { return q->size; } +size_t vms_queue_max_size(vms_queue *q) { return q->size; } -size_t shm_queue_size(shm_queue *q) { return q->elem_num; } +size_t vms_queue_size(vms_queue *q) { return q->elem_num; } diff --git a/src/core/legacy/queue.h b/src/core/legacy/queue.h index 96cd78d1..a89caa07 100644 --- a/src/core/legacy/queue.h +++ b/src/core/legacy/queue.h @@ -1,27 +1,27 @@ -#ifndef SHAMON_QUEUE_H -#define SHAMON_QUEUE_H +#ifndef VAMOS_QUEUE_H +#define VAMOS_QUEUE_H #include #include -typedef struct _shm_queue { +typedef struct _vms_queue { size_t size; size_t elem_num; size_t elem_size; size_t head, tail; unsigned char *data; -} shm_queue; +} vms_queue; -void shm_queue_init(shm_queue *q, size_t size, size_t elem_size); -void shm_queue_destroy(shm_queue *q); -bool shm_queue_push(shm_queue *q, const void *elem); -bool shm_queue_pop(shm_queue *q, void *buff); -size_t shm_queue_size(shm_queue *q); -size_t shm_queue_max_size(shm_queue *q); +void vms_queue_init(vms_queue *q, size_t size, size_t elem_size); +void vms_queue_destroy(vms_queue *q); +bool vms_queue_push(vms_queue *q, const void *elem); +bool vms_queue_pop(vms_queue *q, void *buff); +size_t vms_queue_size(vms_queue *q); +size_t vms_queue_max_size(vms_queue *q); // allocate space for one element // and return the pointer to this element. // If there is no room, NULL is returned. // The returned memory can be filled manually. -void *shm_queue_extend(shm_queue *q); +void *vms_queue_extend(vms_queue *q); -#endif // SHAMON_QUEUE_H +#endif // VAMOS_QUEUE_H diff --git a/src/core/list-embedded.c b/src/core/list-embedded.c index 6983356b..fd6da4fc 100644 --- a/src/core/list-embedded.c +++ b/src/core/list-embedded.c @@ -1,25 +1,25 @@ +#include "vamos-buffers/core/list-embedded.h" + #include #include #include -#include "vamos-buffers/core/list-embedded.h" - -void shm_list_embedded_init(shm_list_embedded *list) { +void vms_list_embedded_init(vms_list_embedded *list) { list->prev = list; list->next = list; } -void shm_list_embedded_insert_after(shm_list_embedded *list, - shm_list_embedded *elm) { +void vms_list_embedded_insert_after(vms_list_embedded *list, + vms_list_embedded *elm) { elm->prev = list; elm->next = list->next; list->next = elm; elm->next->prev = elm; } -void shm_list_embedded_insert_list(shm_list_embedded *list, - shm_list_embedded *other) { - if (shm_list_embedded_empty(other)) +void vms_list_embedded_insert_list(vms_list_embedded *list, + vms_list_embedded *other) { + if (vms_list_embedded_empty(other)) return; other->next->prev = list; @@ -28,16 +28,16 @@ void shm_list_embedded_insert_list(shm_list_embedded *list, list->next = other->next; } -void shm_list_embedded_remove(shm_list_embedded *elm) { +void vms_list_embedded_remove(vms_list_embedded *elm) { elm->prev->next = elm->next; elm->next->prev = elm->prev; elm->prev = NULL; elm->next = NULL; } -size_t shm_list_embedded_size(const shm_list_embedded *list) { +size_t vms_list_embedded_size(const vms_list_embedded *list) { size_t n = 0; - shm_list_embedded *cur = list->next; + vms_list_embedded *cur = list->next; while (cur != list) { cur = cur->next; @@ -47,6 +47,6 @@ size_t shm_list_embedded_size(const shm_list_embedded *list) { return n; } -_Bool shm_list_embedded_empty(const shm_list_embedded *list) { +_Bool vms_list_embedded_empty(const vms_list_embedded *list) { return list->next == list; } diff --git a/src/core/list.c b/src/core/list.c index 392b08a7..c45ef077 100644 --- a/src/core/list.c +++ b/src/core/list.c @@ -1,13 +1,13 @@ +#include "vamos-buffers/core/list.h" + #include #include #include -#include "vamos-buffers/core/list.h" - -void shm_list_init(shm_list *list) { memset(list, 0, sizeof(shm_list)); } +void vms_list_init(vms_list *list) { memset(list, 0, sizeof(vms_list)); } -void shm_list_destroy(shm_list *list, shm_list_elem_destroy_fn destroy) { - shm_list_elem *cur = list->first; +void vms_list_destroy(vms_list *list, vms_list_elem_destroy_fn destroy) { + vms_list_elem *cur = list->first; while (cur) { if (destroy) destroy(cur->data); @@ -16,8 +16,8 @@ void shm_list_destroy(shm_list *list, shm_list_elem_destroy_fn destroy) { } } -size_t shm_list_insert_elem_after(shm_list *list, shm_list_elem *elem, - shm_list_elem *new) { +size_t vms_list_insert_elem_after(vms_list *list, vms_list_elem *elem, + vms_list_elem *new) { assert(elem); new->next = elem->next; new->prev = elem; @@ -32,17 +32,17 @@ size_t shm_list_insert_elem_after(shm_list *list, shm_list_elem *elem, return ++list->size; } -size_t shm_list_insert_after(shm_list *list, shm_list_elem *elem, void *data) { +size_t vms_list_insert_after(vms_list *list, vms_list_elem *elem, void *data) { assert(elem); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; - return shm_list_insert_elem_after(list, elem, new); + return vms_list_insert_elem_after(list, elem, new); } -size_t shm_list_insert_before(shm_list *list, shm_list_elem *elem, void *data) { +size_t vms_list_insert_before(vms_list *list, vms_list_elem *elem, void *data) { assert(elem); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; new->next = elem; @@ -60,9 +60,9 @@ size_t shm_list_insert_before(shm_list *list, shm_list_elem *elem, void *data) { return ++list->size; } -size_t shm_list_prepend(shm_list *list, void *data) { +size_t vms_list_prepend(vms_list *list, void *data) { assert(!list->first || list->first->prev == NULL); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; new->next = list->first; @@ -75,9 +75,9 @@ size_t shm_list_prepend(shm_list *list, void *data) { return ++list->size; } -size_t shm_list_append(shm_list *list, void *data) { +size_t vms_list_append(vms_list *list, void *data) { assert(list->last == NULL || list->last->next == NULL); - shm_list_elem *new = malloc(sizeof(*new)); + vms_list_elem *new = malloc(sizeof(*new)); assert(new); new->data = data; new->next = NULL; @@ -92,7 +92,7 @@ size_t shm_list_append(shm_list *list, void *data) { return ++list->size; } -size_t shm_list_append_elem(shm_list *list, shm_list_elem *elem) { +size_t vms_list_append_elem(vms_list *list, vms_list_elem *elem) { assert(list->last == NULL || list->last->next == NULL); assert(elem); elem->next = NULL; @@ -107,7 +107,7 @@ size_t shm_list_append_elem(shm_list *list, shm_list_elem *elem) { return ++list->size; } -size_t shm_list_remove(shm_list *list, shm_list_elem *elem) { +size_t vms_list_remove(vms_list *list, vms_list_elem *elem) { assert(elem); assert(list->size > 0); assert(list->first); @@ -143,18 +143,18 @@ size_t shm_list_remove(shm_list *list, shm_list_elem *elem) { return --list->size; } -size_t shm_list_size(shm_list *list) { +size_t vms_list_size(vms_list *list) { assert(list->size > 0 || (!list->first && !list->last)); assert(!list->first == !list->last); return list->size; } -shm_list_elem *shm_list_first(shm_list *list) { +vms_list_elem *vms_list_first(vms_list *list) { assert(!list->first == !list->last); return list->first; } -shm_list_elem *shm_list_last(shm_list *list) { +vms_list_elem *vms_list_last(vms_list *list) { assert(!list->first == !list->last); return list->last; } diff --git a/src/core/monitor.c b/src/core/monitor.c index 2060de00..c7d932c5 100644 --- a/src/core/monitor.c +++ b/src/core/monitor.c @@ -1,86 +1,87 @@ +#include "vamos-buffers/core/monitor.h" + #include #include #include #include #include -#include "vamos-buffers/core/monitor.h" #include "vamos-buffers/core/par_queue.h" #include "vamos-buffers/core/utils.h" -typedef struct _shm_monitor_buffer { - shm_par_queue buffer; // the buffer itself +typedef struct _vms_monitor_buffer { + vms_par_queue buffer; // the buffer itself bool finished; // the arbiter has finished? -} shm_monitor_buffer; +} vms_monitor_buffer; -size_t shm_monitor_buffer_sizeof(void) { return sizeof(shm_monitor_buffer); } -size_t shm_monitor_buffer_size(shm_monitor_buffer *buffer) { - return shm_par_queue_size(&buffer->buffer); +size_t vms_monitor_buffer_sizeof(void) { return sizeof(vms_monitor_buffer); } +size_t vms_monitor_buffer_size(vms_monitor_buffer *buffer) { + return vms_par_queue_size(&buffer->buffer); } -size_t shm_monitor_buffer_free_space(shm_monitor_buffer *buffer) { - return shm_par_queue_free_num(&buffer->buffer); +size_t vms_monitor_buffer_free_space(vms_monitor_buffer *buffer) { + return vms_par_queue_free_num(&buffer->buffer); } -size_t shm_monitor_buffer_capacity(shm_monitor_buffer *buffer) { - return shm_par_queue_capacity(&buffer->buffer); +size_t vms_monitor_buffer_capacity(vms_monitor_buffer *buffer) { + return vms_par_queue_capacity(&buffer->buffer); } -void shm_monitor_buffer_init(shm_monitor_buffer *buffer, size_t event_size, +void vms_monitor_buffer_init(vms_monitor_buffer *buffer, size_t event_size, size_t capacity) { - shm_par_queue_init(&buffer->buffer, capacity, event_size); + vms_par_queue_init(&buffer->buffer, capacity, event_size); buffer->finished = false; } -void shm_monitor_set_finished(shm_monitor_buffer *buffer) { +void vms_monitor_set_finished(vms_monitor_buffer *buffer) { buffer->finished = true; } -shm_monitor_buffer *shm_monitor_buffer_create(size_t event_size, +vms_monitor_buffer *vms_monitor_buffer_create(size_t event_size, size_t capacity) { - shm_monitor_buffer *b = malloc(shm_monitor_buffer_sizeof()); + vms_monitor_buffer *b = malloc(vms_monitor_buffer_sizeof()); assert(b && "Malloc failed"); - shm_monitor_buffer_init(b, event_size, capacity); + vms_monitor_buffer_init(b, event_size, capacity); return b; } -void shm_monitor_buffer_free(shm_monitor_buffer *buffer) { - shm_monitor_buffer_destroy(buffer); +void vms_monitor_buffer_free(vms_monitor_buffer *buffer) { + vms_monitor_buffer_destroy(buffer); free(buffer); } -void shm_monitor_buffer_destroy(shm_monitor_buffer *buffer) { - shm_par_queue_destroy(&buffer->buffer); +void vms_monitor_buffer_destroy(vms_monitor_buffer *buffer) { + vms_par_queue_destroy(&buffer->buffer); } -size_t shm_monitor_buffer_elem_size(shm_monitor_buffer *q) { - return shm_par_queue_elem_size(&q->buffer); +size_t vms_monitor_buffer_elem_size(vms_monitor_buffer *q) { + return vms_par_queue_elem_size(&q->buffer); } -shm_event *shm_monitor_buffer_top(shm_monitor_buffer *buffer) { - return shm_par_queue_top(&buffer->buffer); +vms_event *vms_monitor_buffer_top(vms_monitor_buffer *buffer) { + return vms_par_queue_top(&buffer->buffer); } -size_t shm_monitor_buffer_peek(shm_monitor_buffer *buffer, size_t n, +size_t vms_monitor_buffer_peek(vms_monitor_buffer *buffer, size_t n, void **data1, size_t *size1, void **data2, size_t *size2) { - return shm_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); + return vms_par_queue_peek(&buffer->buffer, n, data1, size1, data2, size2); } -size_t shm_monitor_buffer_peek1(shm_monitor_buffer *buffer, void **data) { - return shm_par_queue_peek1(&buffer->buffer, data); +size_t vms_monitor_buffer_peek1(vms_monitor_buffer *buffer, void **data) { + return vms_par_queue_peek1(&buffer->buffer, data); } -void *shm_monitor_buffer_write_ptr_or_null(shm_monitor_buffer *q) { +void *vms_monitor_buffer_write_ptr_or_null(vms_monitor_buffer *q) { assert(!q->finished && "Asking a pointer from a finished buffer"); - return shm_par_queue_write_ptr(&q->buffer); + return vms_par_queue_write_ptr(&q->buffer); } -void *shm_monitor_buffer_write_ptr(shm_monitor_buffer *q) { +void *vms_monitor_buffer_write_ptr(vms_monitor_buffer *q) { assert(!q->finished && "Asking a pointer from a finished buffer"); - void *ptr = shm_par_queue_write_ptr(&q->buffer); + void *ptr = vms_par_queue_write_ptr(&q->buffer); if (ptr) return ptr; @@ -101,28 +102,28 @@ void *shm_monitor_buffer_write_ptr(shm_monitor_buffer *q) { #endif assert(!q->finished && "Asking a pointer from a finished buffer"); - ptr = shm_par_queue_write_ptr(&q->buffer); + ptr = vms_par_queue_write_ptr(&q->buffer); } while (!ptr); assert(ptr); return ptr; } -void shm_monitor_buffer_write_finish(shm_monitor_buffer *q) { +void vms_monitor_buffer_write_finish(vms_monitor_buffer *q) { assert(!q->finished && "Asking a pointer from a finished buffer"); - shm_par_queue_write_finish(&q->buffer); + vms_par_queue_write_finish(&q->buffer); } /* get an event from the stream, block until there is some and return it * or return NULL if the stream ended */ -void *fetch_arbiter_stream(shm_monitor_buffer *buffer) { +void *fetch_arbiter_stream(vms_monitor_buffer *buffer) { size_t sleep_time = SLEEP_TIME_INIT_NS; size_t spinned = 0; void *ev; while (1) { /* wait for the event */ - if ((ev = shm_monitor_buffer_top(buffer))) { + if ((ev = vms_monitor_buffer_top(buffer))) { return ev; } @@ -144,13 +145,13 @@ void *fetch_arbiter_stream(shm_monitor_buffer *buffer) { abort(); } -void shm_monitor_buffer_consume(shm_monitor_buffer *buffer, size_t k) { - shm_par_queue_drop(&buffer->buffer, k); +void vms_monitor_buffer_consume(vms_monitor_buffer *buffer, size_t k) { + vms_par_queue_drop(&buffer->buffer, k); } /* wait for an event on the 'stream' -void shm_monitor_buffer_notify_dropped(shm_monitor_buffer *buffer, +void vms_monitor_buffer_notify_dropped(vms_monitor_buffer *buffer, uint64_t begin_id, uint64_t end_id) { - shm_stream_notify_dropped(buffer->stream, begin_id, end_id); + vms_stream_notify_dropped(buffer->stream, begin_id, end_id); } */ diff --git a/src/core/par_queue.c b/src/core/par_queue.c index a171fdfd..8603e446 100644 --- a/src/core/par_queue.c +++ b/src/core/par_queue.c @@ -1,20 +1,20 @@ +#include "vamos-buffers/core/par_queue.h" + #include #include #include #include #include -#include "vamos-buffers/core/par_queue.h" - #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) -void shm_par_queue_init(shm_par_queue *q, size_t capacity, size_t elem_size) { +void vms_par_queue_init(vms_par_queue *q, size_t capacity, size_t elem_size) { assert(q); assert(capacity > 0); assert(elem_size > 0); - shm_spsc_ringbuf_init(&q->ringbuf, capacity + 1); + vms_spsc_ringbuf_init(&q->ringbuf, capacity + 1); q->capacity = capacity; q->elem_size = elem_size; @@ -25,79 +25,79 @@ void shm_par_queue_init(shm_par_queue *q, size_t capacity, size_t elem_size) { } } -void shm_par_queue_destroy(shm_par_queue *q) { free(q->data); } +void vms_par_queue_destroy(vms_par_queue *q) { free(q->data); } /* Pointer to the next writable slot */ -void *shm_par_queue_write_ptr(shm_par_queue *q) { +void *vms_par_queue_write_ptr(vms_par_queue *q) { size_t n; - size_t off = shm_spsc_ringbuf_write_off_nowrap(&q->ringbuf, &n); + size_t off = vms_spsc_ringbuf_write_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { return q->data + (off * q->elem_size); } return NULL; } -void shm_par_queue_write_finish(shm_par_queue *q) { - shm_spsc_ringbuf_write_finish(&q->ringbuf, 1); +void vms_par_queue_write_finish(vms_par_queue *q) { + vms_spsc_ringbuf_write_finish(&q->ringbuf, 1); } /* push an element into the queue. * 'size' is the actual size of the pushed element * and it must hold that 'size' <= 'elem_size' */ -bool shm_par_queue_push(shm_par_queue *q, const void *elem, size_t size) { +bool vms_par_queue_push(vms_par_queue *q, const void *elem, size_t size) { assert(q->elem_size >= size && "Size does not fit the slot"); - void *ptr = shm_par_queue_write_ptr(q); + void *ptr = vms_par_queue_write_ptr(q); if (__predict_true(ptr)) { memcpy(ptr, elem, size); - shm_par_queue_write_finish(q); + vms_par_queue_write_finish(q); return true; } return false; } -bool shm_par_queue_pop(shm_par_queue *q, void *buff) { +bool vms_par_queue_pop(vms_par_queue *q, void *buff) { size_t n; - const size_t off = shm_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); + const size_t off = vms_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { memcpy(buff, q->data + (off * q->elem_size), q->elem_size); - shm_spsc_ringbuf_consume(&q->ringbuf, 1); + vms_spsc_ringbuf_consume(&q->ringbuf, 1); return true; } return false; } -void shm_par_queue_drop(shm_par_queue *q, size_t k) { - shm_spsc_ringbuf_consume(&q->ringbuf, k); +void vms_par_queue_drop(vms_par_queue *q, size_t k) { + vms_spsc_ringbuf_consume(&q->ringbuf, k); } -size_t shm_par_queue_free_num(shm_par_queue *q) { - return shm_spsc_ringbuf_free_num(&q->ringbuf); +size_t vms_par_queue_free_num(vms_par_queue *q) { + return vms_spsc_ringbuf_free_num(&q->ringbuf); } -size_t shm_par_queue_capacity(shm_par_queue *q) { return q->capacity; } +size_t vms_par_queue_capacity(vms_par_queue *q) { return q->capacity; } -size_t shm_par_queue_size(shm_par_queue *q) { - return shm_spsc_ringbuf_size(&q->ringbuf); +size_t vms_par_queue_size(vms_par_queue *q) { + return vms_spsc_ringbuf_size(&q->ringbuf); } -size_t shm_par_queue_elem_size(shm_par_queue *q) { return q->elem_size; } +size_t vms_par_queue_elem_size(vms_par_queue *q) { return q->elem_size; } -shm_event *shm_par_queue_top(shm_par_queue *q) { +vms_event *vms_par_queue_top(vms_par_queue *q) { size_t n; - const size_t off = shm_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); + const size_t off = vms_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { - return (shm_event *)(q->data + (off * q->elem_size)); + return (vms_event *)(q->data + (off * q->elem_size)); } - return (shm_event *)0; + return (vms_event *)0; } /* n == 0 means arbitrary n */ -size_t shm_par_queue_peek(shm_par_queue *q, size_t n, void **ptr1, size_t *len1, +size_t vms_par_queue_peek(vms_par_queue *q, size_t n, void **ptr1, size_t *len1, void **ptr2, size_t *len2) { size_t off; - const size_t cur_elem_num = shm_spsc_ringbuf_peek( + const size_t cur_elem_num = vms_spsc_ringbuf_peek( &q->ringbuf, n == 0 ? ~((size_t)0) : n, &off, len1, len2); if (__predict_true(cur_elem_num > 0)) { *ptr1 = q->data + (off * q->elem_size); @@ -110,38 +110,38 @@ size_t shm_par_queue_peek(shm_par_queue *q, size_t n, void **ptr1, size_t *len1, } /* peak1 -- it is like top + return the number of elements */ -size_t shm_par_queue_peek1(shm_par_queue *q, void **data) { +size_t vms_par_queue_peek1(vms_par_queue *q, void **data) { size_t n; - const size_t off = shm_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); + const size_t off = vms_spsc_ringbuf_read_off_nowrap(&q->ringbuf, &n); if (__predict_true(n > 0)) { *data = q->data + (off * q->elem_size); } return n; } -shm_event *shm_par_queue_peek_at(shm_par_queue *q, size_t k) { +vms_event *vms_par_queue_peek_at(vms_par_queue *q, size_t k) { unsigned char *ptr1, *ptr2; size_t len1, len2; - size_t n = shm_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, + size_t n = vms_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, (void **)&ptr1, &len1, (void **)&ptr2, &len2); if (n <= k) { return NULL; } if (k < len1) { - return (shm_event *)(ptr1 + (k * q->elem_size)); + return (vms_event *)(ptr1 + (k * q->elem_size)); } k -= len1; assert(k < len2); - return (shm_event *)(ptr2 + (k * q->elem_size)); + return (vms_event *)(ptr2 + (k * q->elem_size)); } -shm_event *shm_par_queue_peek_atmost_at(shm_par_queue *q, size_t *want_k) { +vms_event *vms_par_queue_peek_atmost_at(vms_par_queue *q, size_t *want_k) { void *ptr1, *ptr2; size_t len1, len2; size_t k = *want_k; - size_t n = shm_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, &ptr1, + size_t n = vms_par_queue_peek(q, k == (~((size_t)0)) ? k : k + 1, &ptr1, &len1, &ptr2, &len2); if (n == 0) { *want_k = 0; @@ -152,9 +152,9 @@ shm_event *shm_par_queue_peek_atmost_at(shm_par_queue *q, size_t *want_k) { *want_k = k; } if (k < len1) { - return (shm_event *)((unsigned char *)ptr1 + (k * q->elem_size)); + return (vms_event *)((unsigned char *)ptr1 + (k * q->elem_size)); } k -= len1; assert(k < len2); - return (shm_event *)((unsigned char *)ptr2 + (k * q->elem_size)); + return (vms_event *)((unsigned char *)ptr2 + (k * q->elem_size)); } diff --git a/src/core/queue_spsc.c b/src/core/queue_spsc.c index af5b2ce3..470be6d9 100644 --- a/src/core/queue_spsc.c +++ b/src/core/queue_spsc.c @@ -1,15 +1,15 @@ +#include "vamos-buffers/core/queue_spsc.h" + #include #include #include #include #include -#include "vamos-buffers/core/queue_spsc.h" - #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) -static inline void elem_num_inc(shm_queue_spsc *q, int k) { +static inline void elem_num_inc(vms_queue_spsc *q, int k) { /* Do ++q->elem_num atomically. */ /* The increment must come after everything is done. The release order makes sure that the written element @@ -17,16 +17,16 @@ static inline void elem_num_inc(shm_queue_spsc *q, int k) { atomic_fetch_add_explicit(&q->elem_num, k, memory_order_release); } -static inline void elem_num_dec(shm_queue_spsc *q, int k) { +static inline void elem_num_dec(vms_queue_spsc *q, int k) { /* Do q->elem_num -= k atomically. */ atomic_fetch_sub_explicit(&q->elem_num, k, memory_order_acquire); } -static inline size_t elem_num(shm_queue_spsc *q) { +static inline size_t elem_num(vms_queue_spsc *q) { return atomic_load_explicit(&q->elem_num, memory_order_relaxed); } -void shm_queue_spsc_init(shm_queue_spsc *q, size_t capacity) { +void vms_queue_spsc_init(vms_queue_spsc *q, size_t capacity) { assert(q); assert(capacity > 0); q->elem_num = 0; @@ -34,12 +34,12 @@ void shm_queue_spsc_init(shm_queue_spsc *q, size_t capacity) { q->head = q->tail = 0; } -void shm_queue_spsc_destroy(shm_queue_spsc *q) { +void vms_queue_spsc_destroy(vms_queue_spsc *q) { q->capacity = 0; q->elem_num = 0; } -bool shm_queue_spsc_write_offset(shm_queue_spsc *q, size_t *offset) { +bool vms_queue_spsc_write_offset(vms_queue_spsc *q, size_t *offset) { if (__predict_false(elem_num(q) == q->capacity)) { return false; } @@ -54,7 +54,7 @@ bool shm_queue_spsc_write_offset(shm_queue_spsc *q, size_t *offset) { return true; } -void shm_queue_spsc_writes_finish(shm_queue_spsc *q, size_t written_num) { +void vms_queue_spsc_writes_finish(vms_queue_spsc *q, size_t written_num) { assert(q->partial_head == q->head && "Inconsistent head"); assert(written_num <= q->writing_k_elems); assert(q->head + written_num <= q->capacity); @@ -69,11 +69,11 @@ void shm_queue_spsc_writes_finish(shm_queue_spsc *q, size_t written_num) { elem_num_inc(q, written_num); } -void shm_queue_spsc_write_finish(shm_queue_spsc *q) { - shm_queue_spsc_writes_finish(q, 1); +void vms_queue_spsc_write_finish(vms_queue_spsc *q) { + vms_queue_spsc_writes_finish(q, 1); } -size_t shm_queue_spsc_consume(shm_queue_spsc *q, size_t k) { +size_t vms_queue_spsc_consume(vms_queue_spsc *q, size_t k) { assert(k > 0); size_t num = elem_num(q); if (__predict_false(num < k)) { @@ -90,15 +90,15 @@ size_t shm_queue_spsc_consume(shm_queue_spsc *q, size_t k) { return k; } -size_t shm_queue_spsc_free_num(shm_queue_spsc *q) { +size_t vms_queue_spsc_free_num(vms_queue_spsc *q) { return q->capacity - elem_num(q); } -size_t shm_queue_spsc_capacity(shm_queue_spsc *q) { return q->capacity; } +size_t vms_queue_spsc_capacity(vms_queue_spsc *q) { return q->capacity; } -size_t shm_queue_spsc_size(shm_queue_spsc *q) { return elem_num(q); } +size_t vms_queue_spsc_size(vms_queue_spsc *q) { return elem_num(q); } -size_t shm_queue_spsc_peek(shm_queue_spsc *q, size_t n, size_t *off1, +size_t vms_queue_spsc_peek(vms_queue_spsc *q, size_t n, size_t *off1, size_t *len1, size_t *off2, size_t *len2) { size_t cur_elem_num = elem_num(q); if (n > cur_elem_num) @@ -120,27 +120,27 @@ size_t shm_queue_spsc_peek(shm_queue_spsc *q, size_t n, size_t *off1, } /* peak1 -- it is like top + return the number of elements */ -size_t shm_queue_spsc_read_offset(shm_queue_spsc *q, size_t *offset) { +size_t vms_queue_spsc_read_offset(vms_queue_spsc *q, size_t *offset) { *offset = q->tail; return elem_num(q); } /* -size_t shm_queue_spsc_peek_at(shm_queue_spsc *q, size_t idx) { +size_t vms_queue_spsc_peek_at(vms_queue_spsc *q, size_t idx) { size_t cur_elem_num = elem_num(q); if (idx >= cur_elem_num) return NULL; size_t end = k + q->tail; if (end >= q->capacity) { - return (shm_event *)(q->data + (end - q->capacity) * q->elem_size); + return (vms_event *)(q->data + (end - q->capacity) * q->elem_size); } - return (shm_event *)(q->data + (q->tail + k) * q->elem_size); + return (vms_event *)(q->data + (q->tail + k) * q->elem_size); } */ -size_t shm_queue_spsc_peek_atmost_at(shm_queue_spsc *q, size_t *want_k, +size_t vms_queue_spsc_peek_atmost_at(vms_queue_spsc *q, size_t *want_k, size_t *offset) { size_t num = elem_num(q); if (__predict_false(num == 0)) diff --git a/src/core/shamon.c b/src/core/shamon.c index aff02686..f4d22c1d 100644 --- a/src/core/shamon.c +++ b/src/core/shamon.c @@ -18,112 +18,112 @@ #include "vamos-buffers/core/vector-macro.h" typedef struct _shamon { - VEC(streams, shm_stream *); - /* shm_arbiter_buffers stored in this vector assume + VEC(streams, vms_stream *); + /* vms_arbiter_buffers stored in this vector assume * they are aligned in memory, so use aligned vector */ - shm_vector_aligned buffers; + vms_vector_aligned buffers; VEC(buffer_threads, thrd_t); /* callbacks and their data */ shamon_process_events_fn process_events; void *process_events_data; /* the memory for passing the next event in the default process_events handler */ - shm_event *_ev; + vms_event *_ev; size_t _ev_size; } shamon; -#define _buffers(shmn) ((shm_vector *)&shmn->buffers) +#define _buffers(shmn) ((vms_vector *)&shmn->buffers) #define SLEEP_NS_INIT (50) #define SLEEP_THRESHOLD_NS (10000000) static int default_buffer_manager_thrd(void *data) { - shm_arbiter_buffer *buffer = (shm_arbiter_buffer *)data; - shm_stream *stream = shm_arbiter_buffer_stream(buffer); - register shm_stream_alter_fn alter = stream->alter; - register shm_stream_filter_fn filter = stream->filter; + vms_arbiter_buffer *buffer = (vms_arbiter_buffer *)data; + vms_stream *stream = vms_arbiter_buffer_stream(buffer); + register vms_stream_alter_fn alter = stream->alter; + register vms_stream_filter_fn filter = stream->filter; // wait for buffer->active - while (!shm_arbiter_buffer_active(buffer)) _mm_pause(); + while (!vms_arbiter_buffer_active(buffer)) _mm_pause(); printf("Running fetch & autodrop for stream %s\n", - shm_stream_get_name(stream)); + vms_stream_get_name(stream)); void *ev, *out; while (1) { - ev = stream_fetch(stream, buffer); + ev = vms_stream_fetch_dropping(stream, buffer); if (!ev) { break; } if (filter && !filter(stream, ev)) { - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); continue; } - out = shm_arbiter_buffer_write_ptr(buffer); + out = vms_arbiter_buffer_write_ptr(buffer); assert(out && "No space in the buffer"); alter(stream, ev, out); - shm_arbiter_buffer_write_finish(buffer); - shm_stream_consume(stream, 1); + vms_arbiter_buffer_write_finish(buffer); + vms_stream_consume(stream, 1); } // TODO: we should check if the stream is finished and remove it // in that case printf("BMM for stream %lu (%s) exits\n", stream->id, - shm_stream_get_type(stream)); + vms_stream_get_type(stream)); thrd_exit(EXIT_SUCCESS); } -static shm_event *default_process_events(shm_vector *buffers, void *data, - shm_stream **streamret) { +static vms_event *default_process_events(vms_vector *buffers, void *data, + vms_stream **streamret) { assert(buffers); assert(data); shamon *shmn = (shamon *)data; - shm_stream *stream; + vms_stream *stream; // use static counter to do round robin -- so that some stream // does not starve static unsigned i = 0; - shm_arbiter_buffer *buffer = NULL; + vms_arbiter_buffer *buffer = NULL; size_t qsize; // reset counter if we're at the end - if (i >= shm_vector_size(buffers)) + if (i >= vms_vector_size(buffers)) i = 0; - shm_event *inevent; + vms_event *inevent; - while (i < shm_vector_size(buffers)) { - buffer = ((shm_arbiter_buffer *)shm_vector_at(buffers, i)); - if (!shm_arbiter_buffer_active(buffer)) + while (i < vms_vector_size(buffers)) { + buffer = ((vms_arbiter_buffer *)vms_vector_at(buffers, i)); + if (!vms_arbiter_buffer_active(buffer)) continue; assert(buffer); - stream = shm_arbiter_buffer_stream(buffer); + stream = vms_arbiter_buffer_stream(buffer); ++i; - qsize = shm_arbiter_buffer_peek1(buffer, (void **)&inevent); + qsize = vms_arbiter_buffer_peek1(buffer, (void **)&inevent); if (qsize > 0) { assert(shmn->_ev); - const uint64_t c = shm_arbiter_buffer_capacity(buffer); + const uint64_t c = vms_arbiter_buffer_capacity(buffer); /* is the buffer full from 75 or more percent? */ if (qsize > 0.8 * c) { /* drop half of the buffer */ - shm_eventid id = shm_event_id(inevent); - if (!shm_arbiter_buffer_drop(buffer, c / 2)) { + vms_eventid id = vms_event_id(inevent); + if (!vms_arbiter_buffer_drop(buffer, c / 2)) { assert(0 && "Failed dropping events"); } - shm_stream_prepare_hole_event(stream, shmn->_ev, id, c / 2); + vms_stream_prepare_hole_event(stream, shmn->_ev, id, c / 2); *streamret = stream; return shmn->_ev; } /* TODO: ideally, we do not copy the event here but pass it directly * to the monitor */ - memcpy(shmn->_ev, inevent, shm_arbiter_buffer_elem_size(buffer)); + memcpy(shmn->_ev, inevent, vms_arbiter_buffer_elem_size(buffer)); #ifndef NDEBUG size_t n = #endif - shm_arbiter_buffer_drop(buffer, 1); + vms_arbiter_buffer_drop(buffer, 1); assert(n == 1); *streamret = stream; return shmn->_ev; @@ -141,10 +141,10 @@ shamon *shamon_create(shamon_process_events_fn process_events, assert(shmn); VEC_INIT(shmn->streams); - shm_vector_aligned_init(&shmn->buffers, shm_arbiter_buffer_sizeof(), + vms_vector_aligned_init(&shmn->buffers, vms_arbiter_buffer_sizeof(), CACHELINE_SIZE); VEC_INIT(shmn->buffer_threads); - shmn->_ev_size = sizeof(shm_event_default_hole); + shmn->_ev_size = sizeof(vms_event_default_hole); shmn->_ev = malloc(shmn->_ev_size); shmn->process_events = process_events ? process_events : default_process_events; @@ -153,9 +153,9 @@ shamon *shamon_create(shamon_process_events_fn process_events, return shmn; } -shm_vector *shamon_get_buffers(shamon *shmn) { return _buffers(shmn); } +vms_vector *shamon_get_buffers(shamon *shmn) { return _buffers(shmn); } -shm_stream **shamon_get_streams(shamon *shmn, size_t *size) { +vms_stream **shamon_get_streams(shamon *shmn, size_t *size) { *size = VEC_SIZE(shmn->streams); return shmn->streams; } @@ -165,42 +165,42 @@ shm_stream **shamon_get_streams(shamon *shmn, size_t *size) { * the program is about to be killed */ void shamon_detach(shamon *shmn) { for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - shm_stream_detach(shmn->streams[i]); + vms_stream_detach(shmn->streams[i]); } } void shamon_destroy(shamon *shmn) { - assert(VEC_SIZE(shmn->buffer_threads) == shm_vector_size(_buffers(shmn))); + assert(VEC_SIZE(shmn->buffer_threads) == vms_vector_size(_buffers(shmn))); for (size_t i = 0; i < VEC_SIZE(shmn->buffer_threads); ++i) { thrd_join(shmn->buffer_threads[i], NULL); /* - shm_arbiter_buffer *buff - = *((shm_arbiter_buffer **)shm_vector_at(_buffers(shmn), i)); - shm_arbiter_buffer_set_active(buff, false); - shm_arbiter_buffer_destroy(buff); + vms_arbiter_buffer *buff + = *((vms_arbiter_buffer **)vms_vector_at(_buffers(shmn), i)); + vms_arbiter_buffer_set_active(buff, false); + vms_arbiter_buffer_destroy(buff); */ } for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - if (!shm_stream_is_substream(shmn->streams[i])) { - shm_stream_destroy(shmn->streams[i]); + if (!vms_stream_is_substream(shmn->streams[i])) { + vms_stream_destroy(shmn->streams[i]); } } VEC_DESTROY(shmn->streams); VEC_DESTROY(shmn->buffer_threads); - shm_vector_destroy(_buffers(shmn)); + vms_vector_destroy(_buffers(shmn)); free(shmn->_ev); free(shmn); } bool shamon_is_ready(shamon *shmn) { for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - shm_stream *s = shmn->streams[i]; - if (shm_stream_is_ready(s) || shm_stream_has_new_substreams(s)) { + vms_stream *s = shmn->streams[i]; + if (vms_stream_is_ready(s) || vms_stream_has_new_substreams(s)) { return true; } else { - shm_arbiter_buffer *buff = shm_vector_at(_buffers(shmn), i); - if (shm_arbiter_buffer_size(buff) > 0) { + vms_arbiter_buffer *buff = vms_vector_at(_buffers(shmn), i); + if (vms_arbiter_buffer_size(buff) > 0) { return true; } } @@ -209,18 +209,18 @@ bool shamon_is_ready(shamon *shmn) { return false; } -shm_event *shamon_get_next_ev(shamon *shmn, shm_stream **streamret) { +vms_event *shamon_get_next_ev(shamon *shmn, vms_stream **streamret) { for (size_t i = 0; i < VEC_SIZE(shmn->streams); ++i) { - shm_stream *s = shmn->streams[i]; - shm_stream *new_stream = - shm_stream_create_substream(s, NULL, NULL, NULL, NULL, NULL); + vms_stream *s = shmn->streams[i]; + vms_stream *new_stream = + vms_stream_create_substream(s, NULL, NULL, NULL, NULL, NULL); if (new_stream) { fprintf(stderr, "Stream %lu has a new dynamic substream\n", - shm_stream_id(s)); - shm_stream_register_all_events(new_stream); - shm_arbiter_buffer *buffer = shm_vector_at(_buffers(shmn), i); + vms_stream_id(s)); + vms_stream_register_all_events(new_stream); + vms_arbiter_buffer *buffer = vms_vector_at(_buffers(shmn), i); shamon_add_stream(shmn, new_stream, - shm_arbiter_buffer_capacity(buffer)); + vms_arbiter_buffer_capacity(buffer)); } } @@ -228,7 +228,7 @@ shm_event *shamon_get_next_ev(shamon *shmn, shm_stream **streamret) { streamret); } -void shamon_add_stream(shamon *shmn, shm_stream *stream, +void shamon_add_stream(shamon *shmn, vms_stream *stream, size_t buffer_capacity) { for (unsigned i = 0; i < VEC_SIZE(shmn->streams); ++i) { if (strcmp(shmn->streams[i]->name, stream->name) == 0) { @@ -239,7 +239,7 @@ void shamon_add_stream(shamon *shmn, shm_stream *stream, VEC_PUSH(shmn->streams, &stream); assert(shmn->streams[VEC_SIZE(shmn->streams) - 1] == stream && - "BUG: shm_vector_push"); + "BUG: vms_vector_push"); size_t strm_event_size = stream->hole_handling.hole_event_size; if (stream->event_size > strm_event_size) @@ -249,9 +249,9 @@ void shamon_add_stream(shamon *shmn, shm_stream *stream, shmn->_ev_size = strm_event_size; } - shm_arbiter_buffer *buffer = shm_vector_aligned_extend(_buffers(shmn)); + vms_arbiter_buffer *buffer = vms_vector_aligned_extend(_buffers(shmn)); /* 0 as the output event size means same as the stream */ - shm_arbiter_buffer_init(buffer, stream, + vms_arbiter_buffer_init(buffer, stream, /* output event size = */ 0, buffer_capacity); thrd_t thread_id; @@ -261,5 +261,5 @@ void shamon_add_stream(shamon *shmn, shm_stream *stream, printf("Added a stream id %lu: '%s'\n", VEC_SIZE(shmn->streams) - 1, stream->type); - shm_arbiter_buffer_set_active(buffer, true); + vms_arbiter_buffer_set_active(buffer, true); } diff --git a/src/core/source.c b/src/core/source.c index 849d80cc..9a4d783e 100644 --- a/src/core/source.c +++ b/src/core/source.c @@ -7,13 +7,13 @@ #include "vamos-buffers/core/signatures.h" -size_t source_control_get_records_num(struct source_control *sc) { - return ((sc->size - sizeof(struct source_control)) / - sizeof(struct event_record)); +size_t vms_source_control_get_records_num(struct vms_source_control *sc) { + return ((sc->size - sizeof(struct vms_source_control)) / + sizeof(struct vms_event_record)); } -size_t source_control_max_event_size(struct source_control *control) { - const size_t en = source_control_get_records_num(control); +size_t vms_source_control_max_event_size(struct vms_source_control *control) { + const size_t en = vms_source_control_get_records_num(control); size_t max_size = 0; for (size_t i = 0; i < en; ++i) { if (max_size < control->events[i].size) @@ -22,9 +22,9 @@ size_t source_control_max_event_size(struct source_control *control) { return max_size; } -struct event_record *source_control_get_event(struct source_control *control, - const char *name) { - const size_t en = source_control_get_records_num(control); +struct vms_event_record *vms_source_control_get_event( + struct vms_source_control *control, const char *name) { + const size_t en = vms_source_control_get_records_num(control); for (size_t i = 0; i < en; ++i) { if (strncmp(control->events[i].name, name, sizeof(control->events[0].name)) == 0) { @@ -34,7 +34,7 @@ struct event_record *source_control_get_event(struct source_control *control, return NULL; } -static inline void init_record(struct event_record *ev, const char *name, +static inline void init_record(struct vms_event_record *ev, const char *name, const char *sig) { const size_t max_name_size = sizeof(ev->name) - 1; const size_t max_sig_size = sizeof(ev->signature) - 1; @@ -48,14 +48,14 @@ static inline void init_record(struct event_record *ev, const char *name, ev->signature[max_sig_size] = '\0'; ev->size = - signature_get_size((const unsigned char *)sig) + sizeof(shm_event); + signature_get_size((const unsigned char *)sig) + sizeof(vms_event); ev->kind = 0; } -struct source_control *source_control_allocate(size_t ev_nums) { +struct vms_source_control *vms_source_control_allocate(size_t ev_nums) { size_t control_size = - sizeof(size_t) + ev_nums * sizeof(struct event_record); - struct source_control *control = malloc(control_size); + sizeof(size_t) + ev_nums * sizeof(struct vms_event_record); + struct vms_source_control *control = malloc(control_size); if (!control) { assert(0 && "Allocation failed"); abort(); @@ -66,8 +66,8 @@ struct source_control *source_control_allocate(size_t ev_nums) { return control; } -struct source_control *source_control_define(size_t ev_nums, ...) { - struct source_control *control = source_control_allocate(ev_nums); +struct vms_source_control *vms_source_control_define(size_t ev_nums, ...) { + struct vms_source_control *control = vms_source_control_allocate(ev_nums); va_list ap; va_start(ap, ev_nums); @@ -82,9 +82,9 @@ struct source_control *source_control_define(size_t ev_nums, ...) { return control; } -struct source_control *source_control_define_pairwise( +struct vms_source_control *vms_source_control_define_pairwise( size_t ev_nums, const char *names[], const char *signatures[]) { - struct source_control *control = source_control_allocate(ev_nums); + struct vms_source_control *control = vms_source_control_allocate(ev_nums); for (size_t i = 0; i < ev_nums; ++i) { init_record(control->events + i, names[i], signatures[i]); } @@ -92,7 +92,7 @@ struct source_control *source_control_define_pairwise( return control; } -struct source_control *source_control_define_str(const char *str) { +struct vms_source_control *vms_source_control_define_str(const char *str) { size_t ev_nums = 0; size_t com_nums = 0; /* count the number of events */ @@ -108,17 +108,17 @@ struct source_control *source_control_define_str(const char *str) { } size_t control_size = - sizeof(size_t) + ev_nums * sizeof(struct event_record); - struct source_control *control = malloc(control_size); + sizeof(size_t) + ev_nums * sizeof(struct vms_event_record); + struct vms_source_control *control = malloc(control_size); assert(control); control->size = control_size; const size_t max_name_size = - sizeof(((struct event_record *)NULL)->name) - 1; + sizeof(((struct vms_event_record *)NULL)->name) - 1; #ifndef NDEBUG const size_t max_sig_size = - sizeof(((struct event_record *)NULL)->signature) - 1; + sizeof(((struct vms_event_record *)NULL)->signature) - 1; #endif char name[max_name_size]; @@ -178,8 +178,8 @@ struct source_control *source_control_define_str(const char *str) { return NULL; } -_Bool source_control_define_partially(struct source_control *control, - size_t from, size_t ev_nums, ...) { +_Bool vms_source_control_define_partially(struct vms_source_control *control, + size_t from, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); @@ -192,7 +192,7 @@ _Bool source_control_define_partially(struct source_control *control, const size_t end = from + ev_nums; for (size_t i = from; i < end; ++i) { - assert(i < source_control_get_records_num(control)); + assert(i < vms_source_control_get_records_num(control)); const char *name = va_arg(ap, const char *); const char *sig = va_arg(ap, const char *); init_record(control->events + i, name, sig); @@ -203,10 +203,9 @@ _Bool source_control_define_partially(struct source_control *control, return 1; } -_Bool source_control_define_pairwise_partially(struct source_control *control, - size_t from, size_t ev_nums, - const char *names[], - const char *signatures[]) { +_Bool vms_source_control_define_pairwise_partially( + struct vms_source_control *control, size_t from, size_t ev_nums, + const char *names[], const char *signatures[]) { #ifndef NDEBUG for (size_t i = 0; i < from; ++i) { assert(control->events[i].size > 0 && @@ -217,7 +216,7 @@ _Bool source_control_define_pairwise_partially(struct source_control *control, size_t n = 0; const size_t end = from + ev_nums; for (size_t i = from; i < end; ++i) { - assert(i < source_control_get_records_num(control)); + assert(i < vms_source_control_get_records_num(control)); init_record(control->events + i, names[n], signatures[n]); ++n; } diff --git a/src/core/spsc_ringbuf.c b/src/core/spsc_ringbuf.c index 4972f326..c1610c39 100644 --- a/src/core/spsc_ringbuf.c +++ b/src/core/spsc_ringbuf.c @@ -1,12 +1,12 @@ +#include "vamos-buffers/core/spsc_ringbuf.h" + #include #include -#include "vamos-buffers/core/spsc_ringbuf.h" - #define __predict_false(x) __builtin_expect((x) != 0, 0) #define __predict_true(x) __builtin_expect((x) != 0, 1) -void shm_spsc_ringbuf_init(shm_spsc_ringbuf *b, size_t capacity) { +void vms_spsc_ringbuf_init(vms_spsc_ringbuf *b, size_t capacity) { /* we use one element as a separator */ assert(capacity > 0); assert(capacity < SIZE_MAX - 1 && "Arith. in operations can overflow."); @@ -32,7 +32,7 @@ static inline size_t _get_written_num(size_t head, size_t tail, return head - tail; } else { if (__predict_false(tail == head)) { - return 0; + return 0; } else { return capacity - tail + head; } @@ -94,40 +94,40 @@ static inline size_t get_free_num(size_t head, size_t tail, size_t capacity) { return ret; } -size_t shm_spsc_ringbuf_capacity(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_capacity(vms_spsc_ringbuf *b) { /* we use one element as a separator */ return b->capacity; } -size_t shm_spsc_ringbuf_max_size(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_max_size(vms_spsc_ringbuf *b) { /* we use one element as a separator */ return b->capacity - 1; } -size_t shm_spsc_ringbuf_size(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_size(vms_spsc_ringbuf *b) { return get_written_num(atomic_load_explicit(&b->head, memory_order_relaxed), atomic_load_explicit(&b->tail, memory_order_relaxed), b->capacity); } -bool shm_spsc_ringbuf_empty(shm_spsc_ringbuf *b) { +bool vms_spsc_ringbuf_empty(vms_spsc_ringbuf *b) { return _is_empty(atomic_load_explicit(&b->head, memory_order_relaxed), atomic_load_explicit(&b->tail, memory_order_relaxed)); } -size_t shm_spsc_ringbuf_free_num(shm_spsc_ringbuf *b) { +size_t vms_spsc_ringbuf_free_num(vms_spsc_ringbuf *b) { return get_free_num(atomic_load_explicit(&b->head, memory_order_relaxed), atomic_load_explicit(&b->tail, memory_order_relaxed), b->capacity); } /** - * @brief shm_spsc_ringbuf_full checks if the ringbuffer is full. + * @brief vms_spsc_ringbuf_full checks if the ringbuffer is full. * It ignores cached head/tail and queries the shared head/tail directly. * However, no synchronization is used (the memory order is relaxed). * @return true if the ringbuf is full else false */ -bool shm_spsc_ringbuf_full(shm_spsc_ringbuf *b) { +bool vms_spsc_ringbuf_full(vms_spsc_ringbuf *b) { size_t head = atomic_load_explicit(&b->head, memory_order_relaxed); size_t tail = atomic_load_explicit(&b->tail, memory_order_relaxed); return __predict_false(_is_full(head, tail, b->capacity)); @@ -167,7 +167,7 @@ static inline size_t get_write_off(size_t head, size_t tail, size_t capacity, return tail - head - 1; } -size_t shm_spsc_ringbuf_write_off(shm_spsc_ringbuf *b, size_t *n, +size_t vms_spsc_ringbuf_write_off(vms_spsc_ringbuf *b, size_t *n, size_t *wrap_n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); @@ -184,12 +184,12 @@ size_t shm_spsc_ringbuf_write_off(shm_spsc_ringbuf *b, size_t *n, return head; } -size_t shm_spsc_ringbuf_write_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_write_off_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); - /* Update the cache if seen_tail is 0 (which very likely means it has not been updated yet, - * but can be of course also after wrapping around) or if there is no space left considering - * the cached information. */ + /* Update the cache if seen_tail is 0 (which very likely means it has not + * been updated yet, but can be of course also after wrapping around) or if + * there is no space left considering the cached information. */ if (b->seen_tail == 0 || get_write_off(head, b->seen_tail, b->capacity, n, NULL) == 0) { b->seen_tail = atomic_load_explicit(&b->tail, memory_order_relaxed); @@ -205,7 +205,7 @@ size_t shm_spsc_ringbuf_write_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { } /* Ask for at least *n elements */ -size_t shm_spsc_ringbuf_acquire(shm_spsc_ringbuf *b, size_t *n, +size_t vms_spsc_ringbuf_acquire(vms_spsc_ringbuf *b, size_t *n, size_t *wrap_n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); @@ -224,7 +224,7 @@ size_t shm_spsc_ringbuf_acquire(shm_spsc_ringbuf *b, size_t *n, } /* Ask for at least *n elements */ -size_t shm_spsc_ringbuf_acquire_nowrap(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_acquire_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t head = atomic_load_explicit(&b->head, memory_order_acquire); size_t req = *n; @@ -241,7 +241,7 @@ size_t shm_spsc_ringbuf_acquire_nowrap(shm_spsc_ringbuf *b, size_t *n) { return head; } -void shm_spsc_ringbuf_write_finish(shm_spsc_ringbuf *b, size_t n) { +void vms_spsc_ringbuf_write_finish(vms_spsc_ringbuf *b, size_t n) { assert(n <= b->capacity); assert((b->capacity < (~((size_t)0)) - n) && "Possible overflow"); #ifndef NDEBUG @@ -265,7 +265,7 @@ void shm_spsc_ringbuf_write_finish(shm_spsc_ringbuf *b, size_t n) { atomic_store_explicit(&b->head, new_head, memory_order_release); } -size_t shm_spsc_ringbuf_read_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_read_off_nowrap(vms_spsc_ringbuf *b, size_t *n) { const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t tmp = get_written_num(b->seen_head, tail, b->capacity); if (tmp == 0) { @@ -278,7 +278,7 @@ size_t shm_spsc_ringbuf_read_off_nowrap(shm_spsc_ringbuf *b, size_t *n) { return tail; } -size_t shm_spsc_ringbuf_read_acquire(shm_spsc_ringbuf *b, size_t *n) { +size_t vms_spsc_ringbuf_read_acquire(vms_spsc_ringbuf *b, size_t *n) { const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t tmp = get_written_num(b->seen_head, tail, b->capacity); if (tmp < *n) { @@ -294,7 +294,7 @@ size_t shm_spsc_ringbuf_read_acquire(shm_spsc_ringbuf *b, size_t *n) { /* * Consume n items from the ringbuffer. There must be at least n items. */ -void shm_spsc_ringbuf_consume(shm_spsc_ringbuf *b, size_t n) { +void vms_spsc_ringbuf_consume(vms_spsc_ringbuf *b, size_t n) { assert(n > 0 && "Consume 0 elems"); assert(n <= b->capacity); assert((b->capacity < (~((size_t)0)) - n) && "Possible overflow"); @@ -312,7 +312,7 @@ void shm_spsc_ringbuf_consume(shm_spsc_ringbuf *b, size_t n) { * Consume up to n items from the ringbuffer. Return the number of consumed * events. */ -size_t shm_spsc_ringbuf_consume_upto(shm_spsc_ringbuf *b, size_t n) { +size_t vms_spsc_ringbuf_consume_upto(vms_spsc_ringbuf *b, size_t n) { assert(n <= b->capacity); assert((b->capacity < (~((size_t)0)) - n) && "Possible overflow"); @@ -328,20 +328,20 @@ size_t shm_spsc_ringbuf_consume_upto(shm_spsc_ringbuf *b, size_t n) { } if (n > 0) { - shm_spsc_ringbuf_consume(b, n); + vms_spsc_ringbuf_consume(b, n); } return n; } /* If return value is 0, values *off, *len1 and *len2 may not have been set */ -size_t shm_spsc_ringbuf_peek(shm_spsc_ringbuf *b, size_t n, size_t *off, +size_t vms_spsc_ringbuf_peek(vms_spsc_ringbuf *b, size_t n, size_t *off, size_t *len1, size_t *len2) { const size_t tail = atomic_load_explicit(&b->tail, memory_order_acquire); size_t head = b->seen_head; size_t cur_elem_num = get_written_num(head, tail, b->capacity); - /* update the information if needed or when n == 0 (which means we want to get an up-to-date the number - * of elements) */ + /* update the information if needed or when n == 0 (which means we want to + * get an up-to-date the number of elements) */ if (cur_elem_num < n || n == 0) { b->seen_head = head = atomic_load_explicit(&b->head, memory_order_acquire); diff --git a/src/core/stream.c b/src/core/stream.c index a0ca1564..1e7a24b3 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -2,12 +2,14 @@ #include #include +#include #include #include +#include "vamos-buffers/core/event.h" +#include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" #include "vamos-buffers/shmbuf/buffer.h" -#include "vamos-buffers/core/utils.h" /***** * STREAM @@ -15,28 +17,28 @@ /* FIXME: we duplicate the counter in stream here. Maybe rather set the funs to * NULL? */ -static void default_hole_init(shm_event *ev) { - ev->kind = shm_get_hole_kind(); - ((shm_event_default_hole *)ev)->n = 0; +static void default_hole_init(vms_event *ev) { + ev->kind = vms_event_get_hole_kind(); + ((vms_event_default_hole *)ev)->n = 0; } -static void default_hole_update(shm_event *hole, shm_event *ev) { +static void default_hole_update(vms_event *hole, vms_event *ev) { (void)ev; - ++((shm_event_default_hole *)hole)->n; + ++((vms_event_default_hole *)hole)->n; } -static shm_stream_hole_handling default_hole_handling = { - .hole_event_size = sizeof(shm_event_default_hole), +static vms_stream_hole_handling default_hole_handling = { + .hole_event_size = sizeof(vms_event_default_hole), .init = default_hole_init, .update = default_hole_update}; static uint64_t last_stream_id = 0; -void shm_stream_init(shm_stream *stream, struct buffer *incoming_events_buffer, - size_t event_size, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, - const shm_stream_hole_handling *hole_handling, +void vms_stream_init(vms_stream *stream, vms_shm_buffer *incoming_events_buffer, + size_t event_size, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, + const vms_stream_hole_handling *hole_handling, const char *const type, const char *const name) { stream->id = ++last_stream_id; stream->event_size = event_size; @@ -72,18 +74,18 @@ void shm_stream_init(shm_stream *stream, struct buffer *incoming_events_buffer, VEC_INIT(stream->substreams); } -static void shm_substream_destroy(shm_stream *stream) { - shm_stream_detach(stream); +static void vms_substream_destroy(vms_stream *stream) { + vms_stream_detach(stream); for (unsigned i = 0; i < VEC_SIZE(stream->substreams); ++i) { - shm_stream *sub = stream->substreams[i]; - if (shm_stream_is_ready(sub)) { + vms_stream *sub = stream->substreams[i]; + if (vms_stream_is_ready(sub)) { fprintf(stderr, "warn: destroying substream %lu of %s (%lu) which is still " "ready\n", - shm_stream_id(sub), stream->name, shm_stream_id(stream)); + vms_stream_id(sub), stream->name, vms_stream_id(stream)); } - shm_substream_destroy(sub); + vms_substream_destroy(sub); } if (stream->destroy) { @@ -94,22 +96,22 @@ static void shm_substream_destroy(shm_stream *stream) { free(stream->name); free(stream->events_cache); - release_shared_sub_buffer(stream->incoming_events_buffer); + vms_shm_buffer_release_sub_buffer(stream->incoming_events_buffer); free(stream); } -void shm_stream_destroy(shm_stream *stream) { - shm_stream_detach(stream); +void vms_stream_destroy(vms_stream *stream) { + vms_stream_detach(stream); for (unsigned i = 0; i < VEC_SIZE(stream->substreams); ++i) { - shm_stream *sub = stream->substreams[i]; - if (shm_stream_is_ready(sub)) { + vms_stream *sub = stream->substreams[i]; + if (vms_stream_is_ready(sub)) { fprintf(stderr, "warn: destroying substream %lu of %s (%lu) which is still " "ready\n", - shm_stream_id(sub), stream->name, shm_stream_id(stream)); + vms_stream_id(sub), stream->name, vms_stream_id(stream)); } - shm_substream_destroy(sub); + vms_substream_destroy(sub); } if (stream->destroy) { @@ -120,58 +122,58 @@ void shm_stream_destroy(shm_stream *stream) { free(stream->name); free(stream->events_cache); - release_shared_buffer(stream->incoming_events_buffer); + vms_shm_buffer_release(stream->incoming_events_buffer); free(stream); } -size_t shm_stream_id(shm_stream *stream) { return stream->id; } +size_t vms_stream_id(vms_stream *stream) { return stream->id; } -const char *shm_stream_get_name(shm_stream *stream) { +const char *vms_stream_get_name(vms_stream *stream) { assert(stream); return stream->name; } -const char *shm_stream_get_type(shm_stream *stream) { +const char *vms_stream_get_type(vms_stream *stream) { assert(stream); return stream->type; } -_Bool shm_stream_has_new_substreams(shm_stream *stream) { - return buffer_get_sub_buffers_no(stream->incoming_events_buffer) > +_Bool vms_stream_has_new_substreams(vms_stream *stream) { + return vms_shm_buffer_get_sub_buffers_no(stream->incoming_events_buffer) > stream->substreams_no; } -_Bool shm_stream_is_substream(shm_stream *stream) { +_Bool vms_stream_is_substream(vms_stream *stream) { return stream->parent_stream != NULL; } -shm_stream *shm_stream_create_substream( - shm_stream *stream, shm_stream_is_ready_fn is_ready, - shm_stream_filter_fn filter, shm_stream_alter_fn alter, - shm_stream_destroy_fn destroy, shm_stream_hole_handling *hole_handling) { - if (!shm_stream_has_new_substreams(stream)) { +vms_stream *vms_stream_create_substream( + vms_stream *stream, vms_stream_is_ready_fn is_ready, + vms_stream_filter_fn filter, vms_stream_alter_fn alter, + vms_stream_destroy_fn destroy, vms_stream_hole_handling *hole_handling) { + if (!vms_stream_has_new_substreams(stream)) { return NULL; } size_t substream_no = ++stream->substreams_no; - char *key = get_sub_buffer_key( - buffer_get_key(stream->incoming_events_buffer), substream_no); - struct buffer *shmbuffer = get_shared_buffer(key); + char *key = vms_shm_buffer_compute_sub_buffer_key( + vms_shm_buffer_key(stream->incoming_events_buffer), substream_no); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - shm_stream *substream = xalloc(sizeof *substream); + vms_stream *substream = xalloc(sizeof *substream); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - char *substream_name = - get_sub_buffer_key(shm_stream_get_name(stream), substream_no); + char *substream_name = vms_shm_buffer_compute_sub_buffer_key( + vms_stream_get_name(stream), substream_no); - shm_stream_init( + vms_stream_init( substream, shmbuffer, elem_size, is_ready ? is_ready : stream->is_ready, filter ? filter : stream->filter, alter ? alter : stream->alter, destroy ? destroy : stream->destroy, hole_handling ? hole_handling : &stream->hole_handling, - shm_stream_get_type(stream), substream_name); + vms_stream_get_type(stream), substream_name); substream->parent_stream = stream; free(substream_name); @@ -180,14 +182,15 @@ shm_stream *shm_stream_create_substream( return substream; } -struct event_record *shm_stream_get_avail_events(shm_stream *s, size_t *sz) { - return buffer_get_avail_events(s->incoming_events_buffer, sz); +struct vms_event_record *vms_stream_get_avail_events(vms_stream *s, + size_t *sz) { + return vms_shm_buffer_get_avail_events(s->incoming_events_buffer, sz); } #define MAX_EVENTS_CACHE_SIZE 128 -static shm_kind get_max_kind(struct event_record *recs, size_t size) { - shm_kind ret = 0; +static vms_kind get_max_kind(struct vms_event_record *recs, size_t size) { + vms_kind ret = 0; for (size_t i = 0; i < size; ++i) { if (recs[i].kind > ret) ret = recs[i].kind; @@ -196,30 +199,31 @@ static shm_kind get_max_kind(struct event_record *recs, size_t size) { return ret; } -struct event_record *shm_stream_get_event_record(shm_stream *stream, - shm_kind kind) { +struct vms_event_record *vms_stream_get_event_record(vms_stream *stream, + vms_kind kind) { assert(kind > 0 && "Got invalid kind"); - struct event_record *rec = NULL; + struct vms_event_record *rec = NULL; if (stream->events_cache) { if (kind < MAX_EVENTS_CACHE_SIZE) { rec = &stream->events_cache[kind]; assert(rec->kind == kind); return rec; } else { - return shm_stream_get_event_record_no_cache(stream, kind); + return vms_stream_get_event_record_no_cache(stream, kind); } } else { /* create cache */ size_t sz; - struct event_record *recs = - buffer_get_avail_events(stream->incoming_events_buffer, &sz); + struct vms_event_record *recs = vms_shm_buffer_get_avail_events( + stream->incoming_events_buffer, &sz); size_t max_kind = get_max_kind(recs, sz); size_t cache_sz = (max_kind > MAX_EVENTS_CACHE_SIZE ? MAX_EVENTS_CACHE_SIZE : max_kind) + 1; assert(cache_sz > 0 && "Invalid cache size"); - stream->events_cache = malloc(cache_sz * sizeof(struct event_record)); + stream->events_cache = + malloc(cache_sz * sizeof(struct vms_event_record)); /* cache elements that fit into the cache (cache is indexed by kinds) */ for (size_t i = 0; i < sz; ++i) { if (recs[i].kind < cache_sz) { @@ -232,11 +236,11 @@ struct event_record *shm_stream_get_event_record(shm_stream *stream, } } -struct event_record *shm_stream_get_event_record_no_cache(shm_stream *stream, - shm_kind kind) { +struct vms_event_record *vms_stream_get_event_record_no_cache( + vms_stream *stream, vms_kind kind) { size_t sz; - struct event_record *recs = - buffer_get_avail_events(stream->incoming_events_buffer, &sz); + struct vms_event_record *recs = + vms_shm_buffer_get_avail_events(stream->incoming_events_buffer, &sz); for (size_t i = 0; i < sz; ++i) { if (recs[i].kind == kind) return &recs[i]; @@ -245,17 +249,17 @@ struct event_record *shm_stream_get_event_record_no_cache(shm_stream *stream, } /* the number of elements in the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_size(shm_stream *s) { - return buffer_size(s->incoming_events_buffer); +size_t vms_stream_buffer_size(vms_stream *s) { + return vms_shm_buffer_size(s->incoming_events_buffer); } /* the capacity the (shared memory) buffer of the stream */ -size_t shm_stream_buffer_capacity(shm_stream *s) { - return buffer_capacity(s->incoming_events_buffer); +size_t vms_stream_buffer_capacity(vms_stream *s) { + return vms_shm_buffer_capacity(s->incoming_events_buffer); } /* FIXME: no longer related to stream */ -void shm_stream_prepare_hole_event(shm_stream *stream, shm_event *hole_event, +void vms_stream_prepare_hole_event(vms_stream *stream, vms_event *hole_event, size_t id, uint64_t n) { assert(hole_event->kind > 0 && "init fun set wrong kind"); hole_event->id = id; @@ -267,71 +271,75 @@ void shm_stream_prepare_hole_event(shm_stream *stream, shm_event *hole_event, #endif } -bool shm_stream_is_ready(shm_stream *s) { return s->is_ready(s); } +bool vms_stream_is_ready(vms_stream *s) { return s->is_ready(s); } -bool shm_stream_is_finished(shm_stream *s) { - return shm_stream_buffer_size(s) == 0 && !shm_stream_is_ready(s); +bool vms_stream_is_finished(vms_stream *s) { + return vms_stream_buffer_size(s) == 0 && !vms_stream_is_ready(s); } -void *shm_stream_read_events(shm_stream *s, size_t *num) { +void *vms_stream_read_events(vms_stream *s, size_t *num) { /* the buffer may be already destroyed on the client's side, * but still may have some events to read */ - /* assert(shm_stream_is_ready(s)); */ - return buffer_read_pointer(s->incoming_events_buffer, num); + /* assert(vms_stream_is_ready(s)); */ + return vms_shm_buffer_read_pointer(s->incoming_events_buffer, num); } -bool shm_stream_consume(shm_stream *stream, size_t num) { +bool vms_stream_consume(vms_stream *stream, size_t num) { #ifdef DUMP_STATS stream->consumed_events += num; #endif - return buffer_drop_k(stream->incoming_events_buffer, num); + return vms_shm_buffer_drop_k(stream->incoming_events_buffer, num); } -const char *shm_stream_get_str(shm_stream *stream, uint64_t elem) { - return buffer_get_str(stream->incoming_events_buffer, elem); +const char *vms_stream_get_str(vms_stream *stream, uint64_t elem) { + return vms_shm_buffer_read_str(stream->incoming_events_buffer, elem); } -size_t shm_stream_event_size(shm_stream *s) { return s->event_size; } +size_t vms_stream_event_size(vms_stream *s) { return s->event_size; } -int shm_stream_register_event(shm_stream *stream, const char *name, +int vms_stream_register_event(vms_stream *stream, const char *name, size_t kind) { - return buffer_register_event(stream->incoming_events_buffer, name, kind); + assert(kind > vms_event_get_last_special_kind() && + "Invalid event kind, it overlaps special kinds"); + return vms_shm_buffer_register_event(stream->incoming_events_buffer, name, + kind); } -int shm_stream_register_events(shm_stream *stream, size_t ev_nums, ...) { +int vms_stream_register_events(vms_stream *stream, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); - int ret = - buffer_register_events(stream->incoming_events_buffer, ev_nums, ap); + int ret = vms_shm_buffer_register_events(stream->incoming_events_buffer, + ev_nums, ap); va_end(ap); return ret; } -int shm_stream_register_all_events(shm_stream *stream) { - return buffer_register_all_events(stream->incoming_events_buffer); +int vms_stream_register_all_events(vms_stream *stream) { + return vms_shm_buffer_register_all_events(stream->incoming_events_buffer); } -void shm_stream_notify_last_processed_id(shm_stream *stream, shm_eventid id) { - buffer_set_last_processed_id(stream->incoming_events_buffer, id); +void vms_stream_notify_last_processed_id(vms_stream *stream, vms_eventid id) { + vms_shm_buffer_set_last_processed_id(stream->incoming_events_buffer, id); } -void shm_stream_notify_dropped(shm_stream *stream, uint64_t begin_id, +void vms_stream_notify_dropped(vms_stream *stream, uint64_t begin_id, uint64_t end_id) { - buffer_notify_dropped(stream->incoming_events_buffer, begin_id, end_id); + vms_shm_buffer_notify_dropped(stream->incoming_events_buffer, begin_id, + end_id); } -void shm_stream_attach(shm_stream *stream) { - buffer_set_attached(stream->incoming_events_buffer, true); +void vms_stream_attach(vms_stream *stream) { + vms_shm_buffer_set_reader_is_ready(stream->incoming_events_buffer); } -void shm_stream_detach(shm_stream *stream) { - buffer_set_attached(stream->incoming_events_buffer, false); +void vms_stream_detach(vms_stream *stream) { + vms_shm_buffer_unset_flags(stream->incoming_events_buffer, READER_IS_READY); } -void shm_stream_dump_events(shm_stream *stream) { +void vms_stream_dump_events(vms_stream *stream) { size_t evs_num; - struct event_record *events = - buffer_get_avail_events(stream->incoming_events_buffer, &evs_num); + struct vms_event_record *events = vms_shm_buffer_get_avail_events( + stream->incoming_events_buffer, &evs_num); for (size_t i = 0; i < evs_num; ++i) { fprintf(stderr, "[%s:%s] event: %-20s, kind: %-3lu, size: %-3lu, sig: %s\n", diff --git a/src/core/vector-aligned.c b/src/core/vector-aligned.c index 12b5b2ea..1ad26607 100644 --- a/src/core/vector-aligned.c +++ b/src/core/vector-aligned.c @@ -1,28 +1,29 @@ +#include "vamos-buffers/core/vector-aligned.h" + #include #include #include -#include "vamos-buffers/core/vector-aligned.h" #include "vamos-buffers/core/utils.h" -void shm_vector_aligned_init(shm_vector_aligned *vec, size_t elem_size, +void vms_vector_aligned_init(vms_vector_aligned *vec, size_t elem_size, size_t alignment) { assert(alignment > 0); - shm_vector_init(&vec->vec, elem_size); + vms_vector_init(&vec->vec, elem_size); vec->alignment = alignment; } -void shm_vector_aligned_swap(shm_vector *vec, shm_vector *with) { - shm_vector_swap(vec, with); +void vms_vector_aligned_swap(vms_vector *vec, vms_vector *with) { + vms_vector_swap(vec, with); - shm_vector_aligned *avec = (shm_vector_aligned *)vec; + vms_vector_aligned *avec = (vms_vector_aligned *)vec; size_t alignment = avec->alignment; - avec->alignment = ((shm_vector_aligned *)with)->alignment; - ((shm_vector_aligned *)with)->alignment = alignment; + avec->alignment = ((vms_vector_aligned *)with)->alignment; + ((vms_vector_aligned *)with)->alignment = alignment; } -void shm_vector_aligned_resize(shm_vector *vec, size_t size) { - shm_vector_aligned *avec = (shm_vector_aligned *)vec; +void vms_vector_aligned_resize(vms_vector *vec, size_t size) { + vms_vector_aligned *avec = (vms_vector_aligned *)vec; if (vec->size >= size) return; @@ -42,8 +43,8 @@ void shm_vector_aligned_resize(shm_vector *vec, size_t size) { vec->size = size; } -void *shm_vector_aligned_extend(shm_vector *vec) { - shm_vector_aligned *avec = (shm_vector_aligned *)vec; +void *vms_vector_aligned_extend(vms_vector *vec) { + vms_vector_aligned *avec = (vms_vector_aligned *)vec; if (vec->size >= vec->alloc_size) { // TODO: exp. growth? @@ -63,7 +64,7 @@ void *shm_vector_aligned_extend(shm_vector *vec) { return addr; } -size_t shm_vector_aligned_push(shm_vector *vec, void *elem) { - memcpy(shm_vector_aligned_extend(vec), elem, vec->element_size); +size_t vms_vector_aligned_push(vms_vector *vec, void *elem) { + memcpy(vms_vector_aligned_extend(vec), elem, vec->element_size); return vec->size; } diff --git a/src/core/vector.c b/src/core/vector.c index 8a967403..17a4f405 100644 --- a/src/core/vector.c +++ b/src/core/vector.c @@ -4,15 +4,15 @@ #include #include -void shm_vector_init(shm_vector *vec, size_t elem_size) { +void vms_vector_init(vms_vector *vec, size_t elem_size) { assert(elem_size > 0); memset(vec, 0, sizeof(*vec)); vec->element_size = elem_size; } -void shm_vector_destroy(shm_vector *vec) { free(vec->data); } +void vms_vector_destroy(vms_vector *vec) { free(vec->data); } -void shm_vector_swap(shm_vector *vec, shm_vector *with) { +void vms_vector_swap(vms_vector *vec, vms_vector *with) { assert(vec->element_size == with->element_size); void *data = vec->data; size_t size = vec->size; @@ -25,7 +25,7 @@ void shm_vector_swap(shm_vector *vec, shm_vector *with) { with->alloc_size = asize; } -void shm_vector_resize(shm_vector *vec, size_t size) { +void vms_vector_resize(vms_vector *vec, size_t size) { if (vec->size >= size) { return; } @@ -46,7 +46,7 @@ void shm_vector_resize(shm_vector *vec, size_t size) { vec->size = size; } -void *shm_vector_extend(shm_vector *vec) { +void *vms_vector_extend(vms_vector *vec) { if (vec->size >= vec->alloc_size) { // TODO: exp. growth? vec->alloc_size += 10; @@ -62,30 +62,30 @@ void *shm_vector_extend(shm_vector *vec) { return addr; } -size_t shm_vector_push(shm_vector *vec, void *elem) { - memcpy(shm_vector_extend(vec), elem, vec->element_size); +size_t vms_vector_push(vms_vector *vec, void *elem) { + memcpy(vms_vector_extend(vec), elem, vec->element_size); return vec->size; } -size_t shm_vector_pop(shm_vector *vec) { +size_t vms_vector_pop(vms_vector *vec) { assert(vec->size > 0); return --vec->size; } -size_t shm_vector_size(shm_vector *vec) { return vec->size; } +size_t vms_vector_size(vms_vector *vec) { return vec->size; } -size_t shm_vector_elem_size(shm_vector *vec) { +size_t vms_vector_elem_size(vms_vector *vec) { assert(0 < vec->element_size); return vec->element_size; } -void *shm_vector_at(shm_vector *vec, size_t idx) { +void *vms_vector_at(vms_vector *vec, size_t idx) { assert(idx < vec->size); assert(0 < vec->element_size); return (void *)(((unsigned char *)vec->data) + idx * vec->element_size); } -void *shm_vector_at_checked(shm_vector *vec, size_t idx) { +void *vms_vector_at_checked(vms_vector *vec, size_t idx) { assert(0 < vec->element_size); if (idx < vec->size) { @@ -95,7 +95,7 @@ void *shm_vector_at_checked(shm_vector *vec, size_t idx) { return NULL; } -void *shm_vector_top(shm_vector *vec) { +void *vms_vector_top(vms_vector *vec) { if (vec->size == 0) return NULL; assert(0 < vec->element_size); diff --git a/src/core/shm_string.c b/src/core/vms_string.c similarity index 58% rename from src/core/shm_string.c rename to src/core/vms_string.c index a3e52634..0a48f88b 100644 --- a/src/core/shm_string.c +++ b/src/core/vms_string.c @@ -1,19 +1,19 @@ -#include "vamos-buffers/core/shm_string.h" +#include "vamos-buffers/core/vms_string.h" #include #include #include -#include "vamos-buffers/core/shm_string-macro.h" +#include "vamos-buffers/core/vms_string-macro.h" -void shm_string_init(shm_string *s) { +void vms_string_init(vms_string *s) { STRING_INIT(s->data); memset(s, 0, sizeof(*s)); } -void shm_string_destroy(shm_string *s) { STRING_DESTROY(s->data); } +void vms_string_destroy(vms_string *s) { STRING_DESTROY(s->data); } -void shm_string_swap(shm_string *s, shm_string *with) { +void vms_string_swap(vms_string *s, vms_string *with) { char *data = s->data; size_t size = STRING_SIZE(s->data); size_t asize = STRING_ALLOC_SIZE(s->data); @@ -26,34 +26,34 @@ void shm_string_swap(shm_string *s, shm_string *with) { STRING_ALLOC_SIZE(with->data) = asize; } -void shm_string_grow(shm_string *s, size_t size) { +void vms_string_grow(vms_string *s, size_t size) { if (STRING_SIZE(s->data) >= size) return; STRING_GROW(s->data, size); } -char *shm_string_extend(shm_string *s) { +char *vms_string_extend(vms_string *s) { char *ret; STRING_EXTEND(s->data, ret); return ret; } -size_t shm_string_append(shm_string *s, char c) { +size_t vms_string_append(vms_string *s, char c) { STRING_APPEND(s->data, c); return STRING_SIZE(s->data); } -size_t shm_string_pop(shm_string *s) { return --STRING_SIZE(s->data); } +size_t vms_string_pop(vms_string *s) { return --STRING_SIZE(s->data); } -void shm_string_clear(shm_string *s) { STRING_SIZE(s->data) = 0; } +void vms_string_clear(vms_string *s) { STRING_SIZE(s->data) = 0; } -size_t shm_string_size(shm_string *s) { return STRING_SIZE(s->data); } +size_t vms_string_size(vms_string *s) { return STRING_SIZE(s->data); } /* * Return the pointer to the element at index 'idx' */ -char *shm_string_at_ptr(shm_string *s, ssize_t idx) { +char *vms_string_at_ptr(vms_string *s, ssize_t idx) { if (idx < 0) { assert((size_t)(-idx) <= STRING_SIZE(s->data) && "idx OOB"); return &s->data[STRING_SIZE(s->data) + idx]; @@ -62,6 +62,6 @@ char *shm_string_at_ptr(shm_string *s, ssize_t idx) { return &s->data[idx]; } -char shm_string_at(shm_string *s, ssize_t idx) { - return *shm_string_at_ptr(s, idx); +char vms_string_at(vms_string *s, ssize_t idx) { + return *vms_string_at_ptr(s, idx); } diff --git a/src/shmbuf/buffer-aux.c b/src/shmbuf/buffer-aux.c index 5dc28a5f..d596bb15 100644 --- a/src/shmbuf/buffer-aux.c +++ b/src/shmbuf/buffer-aux.c @@ -6,13 +6,13 @@ #include "shm.h" HIDE_SYMBOL -void drop_ranges_unlock(struct buffer *buff) { +void drop_ranges_unlock(vms_shm_buffer *buff) { /* FIXME: use explicit memory ordering, seq_cnt is not needed here */ buff->shmbuffer->info.dropped_ranges_lock = false; } HIDE_SYMBOL -void drop_ranges_lock(struct buffer *buff) { +void drop_ranges_lock(vms_shm_buffer *buff) { _Atomic bool *l = &buff->shmbuffer->info.dropped_ranges_lock; bool unlocked; do { @@ -20,7 +20,6 @@ void drop_ranges_lock(struct buffer *buff) { } while (atomic_compare_exchange_weak(l, &unlocked, true)); } - HIDE_SYMBOL void aux_buffer_release(struct aux_buffer *buffer) { assert((buffer->size + sizeof(struct aux_buffer)) % sysconf(_SC_PAGESIZE) == @@ -48,13 +47,13 @@ void aux_buffer_destroy(struct aux_buffer *buffer) { aux_buffer_release(buffer); - if (shamon_shm_unlink(key) != 0) { - perror("aux_buffer_destroy: shm_unlink failure"); + if (vms_shm_unlink(key) != 0) { + perror("aux_buffer_destroy: vms_unlink failure"); } } */ -static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { +static struct aux_buffer *new_aux_buffer(vms_shm_buffer *buff, size_t size) { size_t idx = buff->aux_buf_idx++; const size_t pg_size = sysconf(_SC_PAGESIZE); size = (((size + sizeof(struct aux_buffer)) / pg_size) + 2) * pg_size; @@ -65,9 +64,9 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { /* printf("Initializing aux buffer %s\n", key); */ - int fd = shamon_shm_open(key, O_RDWR | O_CREAT, buff->mode); + int fd = vms_shm_open(key, O_RDWR | O_CREAT, buff->mode); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); abort(); } @@ -82,8 +81,8 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } abort(); } @@ -98,15 +97,15 @@ static struct aux_buffer *new_aux_buffer(struct buffer *buff, size_t size) { VEC_PUSH(buff->aux_buffers, &ab); assert(VEC_TOP(buff->aux_buffers) == ab); - shm_list_append(&buff->aux_buffers_age, ab); - assert(shm_list_last(&buff->aux_buffers_age)->data == ab); + vms_list_append(&buff->aux_buffers_age, ab); + assert(vms_list_last(&buff->aux_buffers_age)->data == ab); buff->cur_aux_buff = ab; return ab; } -static inline bool ab_was_dropped(struct aux_buffer *ab, struct buffer *buff) { - struct buffer_info *info = &buff->shmbuffer->info; +static inline bool ab_was_dropped(struct aux_buffer *ab, vms_shm_buffer *buff) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; drop_ranges_lock(buff); for (size_t i = 0; i < DROPPED_RANGES_NUM; ++i) { struct dropped_range *r = &info->dropped_ranges[i]; @@ -123,12 +122,12 @@ static inline bool ab_was_dropped(struct aux_buffer *ab, struct buffer *buff) { } HIDE_SYMBOL -struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { +struct aux_buffer *writer_get_aux_buffer(vms_shm_buffer *buff, size_t size) { if (!buff->cur_aux_buff || aux_buffer_free_space(buff->cur_aux_buff) < size) { /* try to find a free buffer */ struct aux_buffer *ab; - shm_list_elem *cur = shm_list_first(&buff->aux_buffers_age); + vms_list_elem *cur = vms_list_first(&buff->aux_buffers_age); while (cur) { ab = (struct aux_buffer *)cur->data; if (ab->last_event_id <= buff->shmbuffer->info.last_processed_id || @@ -139,10 +138,10 @@ struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { ab->last_event_id = ~(0LL); } if (ab->reusable && ab->size >= size) { - assert(shm_list_last(&buff->aux_buffers_age)->data == + assert(vms_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); - shm_list_remove(&buff->aux_buffers_age, cur); - shm_list_append_elem(&buff->aux_buffers_age, cur); + vms_list_remove(&buff->aux_buffers_age, cur); + vms_list_append_elem(&buff->aux_buffers_age, cur); buff->cur_aux_buff = ab; ab->reusable = false; return ab; @@ -156,12 +155,12 @@ struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size) { /* use the current buffer */ /* it must always be the last in the aux_buffers_age */ - assert(shm_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); + assert(vms_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); return buff->cur_aux_buff; } HIDE_SYMBOL -struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { +struct aux_buffer *reader_get_aux_buffer(vms_shm_buffer *buff, size_t idx) { /* cache the last use */ if (buff->cur_aux_buff && buff->cur_aux_buff->idx == idx) return buff->cur_aux_buff; @@ -182,9 +181,9 @@ struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { // printf("Getting aux buffer %s\n", key); - int fd = shamon_shm_open(key, O_RDWR, S_IRWXU); + int fd = vms_shm_open(key, O_RDWR, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); abort(); } @@ -202,8 +201,8 @@ struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } abort(); } diff --git a/src/shmbuf/buffer-control.c b/src/shmbuf/buffer-control.c index 8ba40fc4..e248bf8d 100644 --- a/src/shmbuf/buffer-control.c +++ b/src/shmbuf/buffer-control.c @@ -7,14 +7,14 @@ #include "vamos-buffers/core/source.h" HIDE_SYMBOL -struct source_control *get_shared_control_buffer(const char *buff_key) { +struct vms_source_control *get_shared_control_buffer(const char *buff_key) { char key[SHM_NAME_MAXLEN]; - shamon_map_ctrl_key(buff_key, key); + vms_shm_map_ctrl_key(buff_key, key); /* fprintf(stderr, "getting control buffer '%s'\n", key); */ - int fd = shamon_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); + int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); return NULL; } @@ -31,35 +31,36 @@ struct source_control *get_shared_control_buffer(const char *buff_key) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } /* FIXME: we leak fd */ - return (struct source_control *)mem; + return (struct vms_source_control *)mem; } HIDE_SYMBOL -struct source_control *create_shared_control_buffer( - const char *buff_key, mode_t mode, const struct source_control *control) { +struct vms_source_control *create_shared_control_buffer( + const char *buff_key, mode_t mode, + const struct vms_source_control *control) { char key[SHM_NAME_MAXLEN]; - shamon_map_ctrl_key(buff_key, key); + vms_shm_map_ctrl_key(buff_key, key); size_t size = control->size; /* fprintf(stderr, "Initializing control buffer '%s' of size '%lu'\n", key, * size); */ char tmpkey[SHM_NAME_MAXLEN] = ""; - if (shamon_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { + if (vms_shm_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { fprintf(stderr, "Failed creating a tmpkey for '%s'\n", key); return NULL; } - int fd = shamon_shm_open(tmpkey, O_RDWR | O_CREAT, mode); + int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT, mode); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); return NULL; } @@ -82,22 +83,22 @@ struct source_control *create_shared_control_buffer( if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } memcpy(mem, control, size); - if (shamon_shm_rename(tmpkey, key) < 0) { + if (vms_shm_rename(tmpkey, key) < 0) { perror("renaming SHM file"); if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } if (munmap(mem, size) != 0) { perror("munmap of control buffer"); @@ -105,11 +106,11 @@ struct source_control *create_shared_control_buffer( return NULL; } - return (struct source_control *)mem; + return (struct vms_source_control *)mem; } HIDE_SYMBOL -void release_shared_control_buffer(struct source_control *buffer) { +void release_shared_control_buffer(struct vms_source_control *buffer) { if (munmap(buffer, buffer->size) != 0) { perror("munmap failure"); } @@ -119,12 +120,12 @@ void release_shared_control_buffer(struct source_control *buffer) { HIDE_SYMBOL void destroy_shared_control_buffer(const char *buffkey, - struct source_control *buffer) { + struct vms_source_control *buffer) { release_shared_control_buffer(buffer); char key[SHM_NAME_MAXLEN]; - shamon_map_ctrl_key(buffkey, key); - if (shamon_shm_unlink(key) != 0) { - perror("destroy_shared_control_buffer: shm_unlink failure"); + vms_shm_map_ctrl_key(buffkey, key); + if (vms_shm_unlink(key) != 0) { + perror("destroy_shared_control_buffer: vms_unlink failure"); } } diff --git a/src/shmbuf/buffer-dbg.c b/src/shmbuf/buffer-dbg.c index e9ed13ce..e61d401e 100644 --- a/src/shmbuf/buffer-dbg.c +++ b/src/shmbuf/buffer-dbg.c @@ -6,11 +6,11 @@ #include "shm.h" /** - * @brief The __shm_dbg_buffer class + * @brief The __vms_dbg_buffer class * * vms_shm_dbg_buffer is a versioned array of key+value records */ -struct __shm_dbg_buffer { +struct __vms_dbg_buffer { CACHELINE_ALIGNED struct { /* allocation size of the buffer */ size_t allocation_size; @@ -32,7 +32,7 @@ struct __shm_dbg_buffer { typedef struct _vms_shm_dbg_buffer { /* shared-memory buffer */ - struct __shm_dbg_buffer *buffer; + struct __vms_dbg_buffer *buffer; /* mmap fd, key, etc. */ int fd; @@ -41,7 +41,7 @@ typedef struct _vms_shm_dbg_buffer { unsigned char *data; /* = buffer->data */ } vms_shm_dbg_buffer; -static void dbg_buffer_init(struct __shm_dbg_buffer *b, size_t allocation_size, +static void dbg_buffer_init(struct __vms_dbg_buffer *b, size_t allocation_size, size_t capacity, uint16_t key_size, uint16_t value_size) { b->info.allocation_size = allocation_size; @@ -58,20 +58,20 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, printf("Initializing dbg buffer '%s' of capacity '%lu'\n", key, capacity); char tmpkey[SHM_NAME_MAXLEN] = ""; - if (shamon_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { + if (vms_shm_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { fprintf(stderr, "Failed creating a tmpkey for '%s'\n", key); return NULL; } - int fd = shamon_shm_open(tmpkey, O_RDWR | O_CREAT, S_IRWXU); + int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); return NULL; } - size_t allocation_size = compute_shm_buffer_size( - sizeof(struct __shm_dbg_buffer), key_size + value_size, capacity); - assert(allocation_size > sizeof(struct __shm_dbg_buffer)); + size_t allocation_size = compute_vms_buffer_size( + sizeof(struct __vms_dbg_buffer), key_size + value_size, capacity); + assert(allocation_size > sizeof(struct __vms_dbg_buffer)); if ((ftruncate(fd, allocation_size)) == -1) { perror("ftruncate"); @@ -85,21 +85,21 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } vms_shm_dbg_buffer *buff = (vms_shm_dbg_buffer *)xalloc(sizeof(*buff)); - buff->buffer = (struct __shm_dbg_buffer *)mem; + buff->buffer = (struct __vms_dbg_buffer *)mem; dbg_buffer_init(buff->buffer, allocation_size, capacity, key_size, value_size); buff->data = buff->buffer->data; buff->key = xstrdup(key); buff->fd = fd; - if (shamon_shm_rename(tmpkey, key) < 0) { + if (vms_shm_rename(tmpkey, key) < 0) { perror("renaming SHM file"); free(buff); @@ -107,8 +107,8 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } if (munmap(mem, allocation_size) != 0) { perror("munmap of control buffer"); @@ -122,13 +122,13 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_create(const char *key, size_t capacity, vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key) { printf("Getting dbg buffer '%s'\n", key); - int fd = shamon_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); + int fd = vms_shm_open(key, O_RDWR | O_CREAT, S_IRWXU); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); return NULL; } - struct __shm_dbg_buffer tmp; + struct __vms_dbg_buffer tmp; if (pread(fd, &tmp, sizeof(tmp), 0) == -1) { perror("reading info about dbg buffer"); return NULL; @@ -151,7 +151,7 @@ vms_shm_dbg_buffer *vms_shm_dbg_buffer_get(const char *key) { } vms_shm_dbg_buffer *buff = (vms_shm_dbg_buffer *)xalloc(sizeof(*buff)); - buff->buffer = (struct __shm_dbg_buffer *)mem; + buff->buffer = (struct __vms_dbg_buffer *)mem; buff->data = buff->buffer->data; buff->key = xstrdup(key); buff->fd = fd; @@ -172,8 +172,8 @@ void vms_shm_dbg_buffer_release(vms_shm_dbg_buffer *buff) { } void vms_shm_dbg_buffer_destroy(vms_shm_dbg_buffer *buff) { - if (shamon_shm_unlink(buff->key) != 0) { - perror("shm_unlink when destroying a dbg buffer"); + if (vms_shm_unlink(buff->key) != 0) { + perror("vms_unlink when destroying a dbg buffer"); } vms_shm_dbg_buffer_release(buff); diff --git a/src/shmbuf/buffer-local.c b/src/shmbuf/buffer-local.c index 51044b7d..041cb724 100644 --- a/src/shmbuf/buffer-local.c +++ b/src/shmbuf/buffer-local.c @@ -14,41 +14,41 @@ #include #include "buffer-private.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/list.h" #include "vamos-buffers/core/source.h" #include "vamos-buffers/core/spsc_ringbuf.h" #include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" +#include "vamos-buffers/shmbuf/buffer.h" /* FOR TESTING */ -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - size_t capacity, - struct source_control *control) { +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + size_t capacity, + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); printf("Initializing LOCAL buffer '%s' with elem size '%lu'\n", key, elem_size); void *mem; - const size_t memsize = compute_shm_size(elem_size, capacity); + const size_t memsize = compute_vms_size(elem_size, capacity); int succ = posix_memalign(&mem, 64, memsize); if (succ != 0) { perror("allocation failure"); return NULL; } - struct buffer *buff = malloc(sizeof(struct buffer)); + vms_shm_buffer *buff = malloc(sizeof(vms_shm_buffer)); assert(buff && "Memory allocation failed"); buff->shmbuffer = (struct shmbuffer *)mem; assert(ADDR_IS_CACHE_ALIGNED(buff->shmbuffer->data)); assert(ADDR_IS_CACHE_ALIGNED(&buff->shmbuffer->info.ringbuf)); - memset(buff->shmbuffer, 0, sizeof(struct buffer_info)); + memset(buff->shmbuffer, 0, sizeof(vms_shm_buffer_info)); /* ringbuf has one dummy element */ buff->shmbuffer->info.capacity = capacity; buff->shmbuffer->info.allocated_size = memsize; - shm_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); + vms_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); printf(" .. buffer allocated size = %lu, capacity = %lu\n", buff->shmbuffer->info.allocated_size, buff->shmbuffer->info.capacity); @@ -59,7 +59,7 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, buff->key = strdup(key); VEC_INIT(buff->aux_buffers); - shm_list_init(&buff->aux_buffers_age); + vms_list_init(&buff->aux_buffers_age); buff->aux_buf_idx = 0; buff->cur_aux_buff = NULL; buff->fd = -1; @@ -69,7 +69,7 @@ struct buffer *initialize_local_buffer(const char *key, size_t elem_size, return buff; } -void release_local_buffer(struct buffer *buff) { +void release_local_buffer(vms_shm_buffer *buff) { free(buff->key); size_t vecsize = VEC_SIZE(buff->aux_buffers); diff --git a/src/shmbuf/buffer-private.h b/src/shmbuf/buffer-private.h index 3aa4d0c1..02676766 100644 --- a/src/shmbuf/buffer-private.h +++ b/src/shmbuf/buffer-private.h @@ -1,5 +1,5 @@ -#ifndef SHAMON_SHM_BUFFER_PRIVATE_H -#define SHAMON_SHM_BUFFER_PRIVATE_H +#ifndef VAMOS_SHM_BUFFER_PRIVATE_H +#define VAMOS_SHM_BUFFER_PRIVATE_H #include #include @@ -11,9 +11,9 @@ #include "vamos-buffers/core/spsc_ringbuf.h" #include "vamos-buffers/core/vector-macro.h" -struct source_control; -struct event_record; -struct buffer; +struct vms_source_control; +struct vms_event_record; +typedef struct _vms_shm_buffer vms_shm_buffer; #define MAX_AUX_BUF_KEY_SIZE 16 #define DROPPED_RANGES_NUM 5 @@ -26,29 +26,32 @@ struct buffer; struct dropped_range { /* the range of autodropped events (for garbage collection) */ - shm_eventid begin; - shm_eventid end; + vms_eventid begin; + vms_eventid end; }; -struct buffer_info { - shm_spsc_ringbuf ringbuf; +typedef struct _vms_shm_buffer_info { + vms_spsc_ringbuf ringbuf; size_t allocated_size; size_t capacity; size_t elem_size; - shm_eventid last_processed_id; + vms_eventid last_processed_id; struct dropped_range dropped_ranges[DROPPED_RANGES_NUM]; size_t dropped_ranges_next; _Atomic _Bool dropped_ranges_lock; /* spin lock */ /* Number of sub-buffers. Sub-buffers are numbered from 1. */ - volatile _Atomic size_t subbuffers_no; - /* the monitored program exited/destroyed the buffer */ - volatile _Bool destroyed; - volatile _Bool monitor_attached; -} __attribute__((aligned(CACHELINE_SIZE))); + _Atomic size_t subbuffers_no; + /* the writer program/thread exited/destroyed the buffer */ + /* TODO: turn this into a flag */ + _Atomic _Bool destroyed; + /* flags that are used to pass information (e.g., syncing) the writer and + * reader */ + _Atomic uint64_t flags; +} vms_shm_buffer_info __attribute__((aligned(CACHELINE_SIZE))); struct shmbuffer { - struct buffer_info info; + vms_shm_buffer_info info; /* pointer to the beginning of data */ unsigned char data[]; }; @@ -65,16 +68,16 @@ struct aux_buffer { /* TODO: cache the shared state in local state (e.g., elem_size, etc.). Maybe we could also inline - the shm_spsc_ringbuf so that we can keep local cache */ -struct buffer { + the vms_spsc_ringbuf so that we can keep local cache */ +typedef struct _vms_shm_buffer { struct shmbuffer *shmbuffer; - struct source_control *control; + struct vms_source_control *control; /* shared memory of auxiliary buffer */ struct aux_buffer *cur_aux_buff; /* the known aux_buffers that might still be needed */ VEC(aux_buffers, struct aux_buffer *); size_t aux_buf_idx; - shm_list aux_buffers_age; + vms_list aux_buffers_age; /* shm filedescriptor */ int fd; /* shm key */ @@ -83,42 +86,43 @@ struct buffer { mode_t mode; /* The number of the last subbufer */ _Atomic size_t last_subbufer_no; -}; +} vms_shm_buffer; #define _ringbuf(buff) (&buff->shmbuffer->info.ringbuf) -struct buffer *initialize_shared_buffer(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - struct source_control *control); +vms_shm_buffer *_vms_shm_buffer_initialize(const char *key, mode_t mode, + size_t elem_size, size_t capacity, + struct vms_source_control *control); -struct buffer *get_shared_buffer(const char *key); -struct buffer *try_get_shared_buffer(const char *key, size_t retry); +// vms_shm_buffer *get_shared_buffer(const char *key); +// vms_shm_buffer *try_get_shared_buffer(const char *key, size_t retry); -size_t compute_shm_size(size_t elem_size, size_t capacity); -size_t compute_shm_buffer_size(size_t nondata_size, size_t elem_size, +size_t compute_vms_size(size_t elem_size, size_t capacity); +size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, size_t capacity); /*** LOCAL buffers ***/ -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - size_t capacity, - struct source_control *control); -void release_local_buffer(struct buffer *buff); +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + size_t capacity, + struct vms_source_control *control); +void release_local_buffer(vms_shm_buffer *buff); /*** CONTROL buffers ***/ -struct source_control *get_shared_control_buffer(const char *buff_key); -struct source_control *create_shared_control_buffer( - const char *buff_key, mode_t mode, const struct source_control *control); -void release_shared_control_buffer(struct source_control *buffer); +struct vms_source_control *get_shared_control_buffer(const char *buff_key); +struct vms_source_control *create_shared_control_buffer( + const char *buff_key, mode_t mode, + const struct vms_source_control *control); +void release_shared_control_buffer(struct vms_source_control *buffer); void destroy_shared_control_buffer(const char *buffkey, - struct source_control *buffer); + struct vms_source_control *buffer); /*** AUX buffers ***/ size_t aux_buffer_free_space(struct aux_buffer *buff); void aux_buffer_release(struct aux_buffer *buffer); -struct aux_buffer *writer_get_aux_buffer(struct buffer *buff, size_t size); -struct aux_buffer *reader_get_aux_buffer(struct buffer *buff, size_t idx); +struct aux_buffer *writer_get_aux_buffer(vms_shm_buffer *buff, size_t size); +struct aux_buffer *reader_get_aux_buffer(vms_shm_buffer *buff, size_t idx); -void drop_ranges_lock(struct buffer *buff); -void drop_ranges_unlock(struct buffer *buff); +void drop_ranges_lock(vms_shm_buffer *buff); +void drop_ranges_unlock(vms_shm_buffer *buff); -#endif /* SHAMON_SHM_BUFFER_PRIVATE_H */ +#endif /* VAMOS_SHM_BUFFER_PRIVATE_H */ diff --git a/src/shmbuf/buffer-sub.c b/src/shmbuf/buffer-sub.c index 031dcd32..3ec5130c 100644 --- a/src/shmbuf/buffer-sub.c +++ b/src/shmbuf/buffer-sub.c @@ -3,12 +3,11 @@ #include "buffer-private.h" #include "shm.h" - -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/source.h" #include "vamos-buffers/core/vector-macro.h" +#include "vamos-buffers/shmbuf/buffer.h" -char *get_sub_buffer_key(const char *key, size_t idx) { +char *vms_shm_buffer_compute_sub_buffer_key(const char *key, size_t idx) { size_t tmpsize = strlen(key) + 16 /* space for index */; char *tmp = xalloc(tmpsize); int written = snprintf(tmp, tmpsize, "%s.sub.%lu", key, idx); @@ -19,21 +18,23 @@ char *get_sub_buffer_key(const char *key, size_t idx) { return tmp; } -struct buffer *create_shared_sub_buffer(struct buffer *buffer, size_t capacity, - const struct source_control *control) { - char *key = get_sub_buffer_key(buffer->key, ++buffer->last_subbufer_no); - struct source_control *ctrl = +vms_shm_buffer *vms_shm_buffer_create_sub_buffer( + vms_shm_buffer *buffer, size_t capacity, + const struct vms_source_control *control) { + char *key = vms_shm_buffer_compute_sub_buffer_key( + buffer->key, ++buffer->last_subbufer_no); + struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { fprintf(stderr, "Failed creating subbuffer\n"); return NULL; } - size_t elem_size = source_control_max_event_size(ctrl); + size_t elem_size = vms_source_control_max_event_size(ctrl); if (capacity == 0) - capacity = buffer_capacity(buffer); - struct buffer *sbuf = - initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); + capacity = vms_shm_buffer_capacity(buffer); + vms_shm_buffer *sbuf = + _vms_shm_buffer_initialize(key, S_IRWXU, elem_size, capacity, ctrl); /* XXX: we copy the key in 'initialize_shared_buffer' which is redundant as * we have created it in `get_sub_buffer_key` and can just move it */ free(key); @@ -43,7 +44,7 @@ struct buffer *create_shared_sub_buffer(struct buffer *buffer, size_t capacity, return sbuf; } -size_t buffer_get_sub_buffers_no(struct buffer *buffer) { +size_t vms_shm_buffer_get_sub_buffers_no(vms_shm_buffer *buffer) { return buffer->shmbuffer->info.subbuffers_no; } @@ -56,12 +57,12 @@ size_t buffer_get_sub_buffers_no(struct buffer *buffer) { * therefore destroying the buffer entirely would lead to accessing * dangling data. */ -void buffer_set_destroyed(struct buffer *buff) { +void vms_shm_buffer_set_destroyed(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; } /* for writers */ -void destroy_shared_sub_buffer(struct buffer *buff) { +void vms_shm_buffer_destroy_sub_buffer(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; size_t vecsize = VEC_SIZE(buff->aux_buffers); @@ -77,10 +78,10 @@ void destroy_shared_sub_buffer(struct buffer *buff) { #endif if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { - perror("destroy_shared_buffer: munmap failure"); + perror("vms_shm_buffer_destroy: munmap failure"); } if (close(buff->fd) == -1) { - perror("destroy_shared_buffer: failed closing mmap fd"); + perror("vms_shm_buffer_destroy: failed closing mmap fd"); } release_shared_control_buffer(buff->control); @@ -90,7 +91,7 @@ void destroy_shared_sub_buffer(struct buffer *buff) { } /* for readers */ -void release_shared_sub_buffer(struct buffer *buff) { +void vms_shm_buffer_release_sub_buffer(vms_shm_buffer *buff) { if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { perror("release_shared_sub_buffer: munmap failure"); } @@ -105,8 +106,8 @@ void release_shared_sub_buffer(struct buffer *buff) { } VEC_DESTROY(buff->aux_buffers); - if (shamon_shm_unlink(buff->key) != 0) { - perror("release_shared_sub_buffer: shm_unlink failure"); + if (vms_shm_unlink(buff->key) != 0) { + perror("release_shared_sub_buffer: vms_unlink failure"); } destroy_shared_control_buffer(buff->key, buff->control); diff --git a/src/shmbuf/buffer.c b/src/shmbuf/buffer.c index aef5c1c0..006609d2 100644 --- a/src/shmbuf/buffer.c +++ b/src/shmbuf/buffer.c @@ -2,6 +2,7 @@ #include #include +#include /* _mm_mfence */ #include #include #include @@ -23,51 +24,55 @@ #include "vamos-buffers/core/utils.h" #include "vamos-buffers/core/vector-macro.h" -bool buffer_is_ready(struct buffer *buff) { - return !buff->shmbuffer->info.destroyed; +bool vms_shm_buffer_is_destroyed(vms_shm_buffer *buff) { + return buff->shmbuffer->info.destroyed; } -bool buffer_monitor_attached(struct buffer *buff) { - return buff->shmbuffer->info.monitor_attached; +bool vms_shm_buffer_is_ready(vms_shm_buffer *buff) { + return !vms_shm_buffer_is_destroyed(buff); } -size_t buffer_capacity(struct buffer *buff) { +bool vms_shm_buffer_reader_is_ready(vms_shm_buffer *buff) { + return (buff->shmbuffer->info.flags & READER_IS_READY); +} + +size_t vms_shm_buffer_capacity(vms_shm_buffer *buff) { return buff->shmbuffer->info.capacity; } -size_t buffer_size(struct buffer *buff) { - return shm_spsc_ringbuf_size(_ringbuf(buff)); +size_t vms_shm_buffer_size(vms_shm_buffer *buff) { + return vms_spsc_ringbuf_size(_ringbuf(buff)); } -size_t buffer_elem_size(struct buffer *buff) { +size_t vms_shm_buffer_elem_size(vms_shm_buffer *buff) { return buff->shmbuffer->info.elem_size; } -const char *buffer_get_key(struct buffer *buffer) { return buffer->key; } +const char *vms_shm_buffer_key(vms_shm_buffer *buffer) { return buffer->key; } -int buffer_get_key_path(struct buffer *buff, char keypath[], - size_t keypathsize) { +int vms_shm_buffer_compute_key_path(vms_shm_buffer *buff, char keypath[], + size_t keypathsize) { if (SHM_NAME_MAXLEN <= keypathsize) return -1; - if (shm_mapname(buff->key, keypath) == NULL) { + if (vms_shm_mapname(buff->key, keypath) == NULL) { return -2; } return 0; } -int buffer_get_ctrl_key_path(struct buffer *buff, char keypath[], - size_t keypathsize) { +int vms_shm_buffer_compute_ctrl_key_path(vms_shm_buffer *buff, char keypath[], + size_t keypathsize) { if (SHM_NAME_MAXLEN <= keypathsize) return -1; char ctrlkey[SHM_NAME_MAXLEN]; - if (shamon_map_ctrl_key(buff->key, ctrlkey) == NULL) { + if (vms_shm_map_ctrl_key(buff->key, ctrlkey) == NULL) { return -3; } - if (shm_mapname(ctrlkey, keypath) == NULL) { + if (vms_shm_mapname(ctrlkey, keypath) == NULL) { return -2; } @@ -82,14 +87,14 @@ int buffer_get_ctrl_key_path(struct buffer *buff, char keypath[], ((unsigned char *)b->data + (b->info.elem_size * (b->info.capacity + 1))) HIDE_SYMBOL -struct buffer *initialize_shared_buffer(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - struct source_control *control) { +vms_shm_buffer *_vms_shm_buffer_initialize(const char *key, mode_t mode, + size_t elem_size, size_t capacity, + struct vms_source_control *control) { assert(elem_size > 0 && "Element size is 0"); assert(capacity > 0 && "Capacity is 0"); /* the ringbuffer has one unusable dummy element, so increase the capacity * by one */ - const size_t memsize = compute_shm_size(elem_size, capacity + 1); + const size_t memsize = compute_vms_size(elem_size, capacity + 1); #ifndef NDEBUG fprintf(stderr, @@ -104,14 +109,14 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, * we avoid a race when the other side opens the SHM file and reads from * it before it is fully initialized */ char tmpkey[SHM_NAME_MAXLEN] = ""; - if (shamon_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { + if (vms_shm_get_tmp_key(key, tmpkey, SHM_NAME_MAXLEN) == -1) { fprintf(stderr, "Failed creating a tmpkey for '%s'\n", key); return NULL; } - int fd = shamon_shm_open(tmpkey, O_RDWR | O_CREAT | O_TRUNC, mode); + int fd = vms_shm_open(tmpkey, O_RDWR | O_CREAT | O_TRUNC, mode); if (fd < 0) { - perror("shm_open"); + perror("vms_shm_open"); return NULL; } @@ -126,23 +131,23 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } - struct buffer *buff = xalloc(sizeof(struct buffer)); + vms_shm_buffer *buff = xalloc(sizeof(vms_shm_buffer)); buff->shmbuffer = (struct shmbuffer *)shmem; assert(ADDR_IS_CACHE_ALIGNED(buff->shmbuffer->data)); assert(ADDR_IS_CACHE_ALIGNED(&buff->shmbuffer->info.ringbuf)); - memset(buff->shmbuffer, 0, sizeof(struct buffer_info)); + memset(buff->shmbuffer, 0, sizeof(vms_shm_buffer_info)); buff->shmbuffer->info.allocated_size = memsize; buff->shmbuffer->info.capacity = capacity; /* ringbuf has one dummy element and we allocated the space for it */ - shm_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); + vms_spsc_ringbuf_init(_ringbuf(buff), capacity + 1); buff->shmbuffer->info.elem_size = elem_size; buff->shmbuffer->info.last_processed_id = 0; buff->shmbuffer->info.dropped_ranges_next = 0; @@ -169,7 +174,7 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, buff->key = strdup(key); VEC_INIT(buff->aux_buffers); - shm_list_init(&buff->aux_buffers_age); + vms_list_init(&buff->aux_buffers_age); buff->aux_buf_idx = 0; buff->cur_aux_buff = NULL; buff->fd = fd; @@ -177,14 +182,14 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, buff->mode = mode; buff->last_subbufer_no = 0; - if (shamon_shm_rename(tmpkey, key) < 0) { + if (vms_shm_rename(tmpkey, key) < 0) { perror("renaming SHM file"); if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(tmpkey) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(tmpkey) != 0) { + perror("vms_unlink after mmap failure"); } free(buff); return NULL; @@ -193,23 +198,24 @@ struct buffer *initialize_shared_buffer(const char *key, mode_t mode, return buff; } -struct buffer *create_shared_buffer(const char *key, size_t capacity, - const struct source_control *control) { - struct source_control *ctrl = +vms_shm_buffer *vms_shm_buffer_create( + const char *key, size_t capacity, + const struct vms_source_control *control) { + struct vms_source_control *ctrl = create_shared_control_buffer(key, S_IRWXU, control); if (!ctrl) { fprintf(stderr, "Failed creating control buffer\n"); return NULL; } - size_t elem_size = source_control_max_event_size(ctrl); - return initialize_shared_buffer(key, S_IRWXU, elem_size, capacity, ctrl); + size_t elem_size = vms_source_control_max_event_size(ctrl); + return _vms_shm_buffer_initialize(key, S_IRWXU, elem_size, capacity, ctrl); } -struct buffer *create_shared_buffer_adv(const char *key, mode_t mode, - size_t elem_size, size_t capacity, - const struct source_control *control) { - struct source_control *ctrl = +vms_shm_buffer *vms_shm_buffer_create_adv( + const char *key, mode_t mode, size_t elem_size, size_t capacity, + const struct vms_source_control *control) { + struct vms_source_control *ctrl = create_shared_control_buffer(key, mode, control); if (!ctrl) { fprintf(stderr, "Failed creating control buffer\n"); @@ -217,23 +223,23 @@ struct buffer *create_shared_buffer_adv(const char *key, mode_t mode, } if (elem_size == 0) { - elem_size = source_control_max_event_size(ctrl); + elem_size = vms_source_control_max_event_size(ctrl); } if (mode == 0) { mode = S_IRWXU; } - return initialize_shared_buffer(key, mode, elem_size, capacity, ctrl); + return _vms_shm_buffer_initialize(key, mode, elem_size, capacity, ctrl); } -struct buffer *try_get_shared_buffer(const char *key, size_t retry) { +vms_shm_buffer *vms_shm_buffer_try_connect(const char *key, size_t retry) { // fprintf(stderr, "getting shared buffer '%s'\n", key); int fd = -1; ++retry; do { - fd = shamon_shm_open(key, O_RDWR, S_IRWXU); + fd = vms_shm_open(key, O_RDWR, S_IRWXU); if (fd >= 0) { break; } @@ -241,12 +247,12 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { } while (--retry > 0); if (fd == -1) { - perror("shm_open"); + perror("vms_shm_open"); fprintf(stderr, "Failed getting shared buffer '%s'\n", key); return NULL; } - struct buffer_info info; + vms_shm_buffer_info info; if (pread(fd, &info, sizeof(info), 0) == -1) { perror("reading info of shared buffer"); close(fd); @@ -268,7 +274,7 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { goto before_mmap_clean; } - struct buffer *buff = malloc(sizeof(*buff)); + vms_shm_buffer *buff = malloc(sizeof(*buff)); if (!buff) { fprintf(stderr, "%s:%d: memory allocation failed\n", __func__, __LINE__); @@ -310,40 +316,66 @@ struct buffer *try_get_shared_buffer(const char *key, size_t retry) { if (close(fd) == -1) { perror("closing fd after mmap failure"); } - if (shamon_shm_unlink(key) != 0) { - perror("shm_unlink after mmap failure"); + if (vms_shm_unlink(key) != 0) { + perror("vms_unlink after mmap failure"); } return NULL; } -struct buffer *get_shared_buffer(const char *key) { - return try_get_shared_buffer(key, 10); +vms_shm_buffer *vms_shm_buffer_connect(const char *key) { + return vms_shm_buffer_try_connect(key, 10); } -struct event_record *buffer_get_avail_events(struct buffer *buff, - size_t *evs_num) { +struct vms_event_record *vms_shm_buffer_get_avail_events(vms_shm_buffer *buff, + size_t *evs_num) { assert(buff); assert(evs_num); assert(buff->control); - *evs_num = source_control_get_records_num(buff->control); +#ifndef NDEBUG + /* NOTE: this assert does the fence */ + assert((vms_shm_buffer_get_flags(buff) & EVENTS_REGISTERED) && + "Events were not registered"); +#else + /* make sure that any concurrent changes to the source control buffer are + * visible now */ + _mm_mfence(); +#endif + + *evs_num = vms_source_control_get_records_num(buff->control); return buff->control->events; } -void buffer_set_attached(struct buffer *buff, bool val) { +uint64_t vms_shm_buffer_get_flags(vms_shm_buffer *buff) { + return buff->shmbuffer->info.flags; +} + +void vms_shm_buffer_set_flags(vms_shm_buffer *buff, uint64_t flags) { + buff->shmbuffer->info.flags |= flags; +} + +void vms_shm_buffer_unset_flags(vms_shm_buffer *buff, uint64_t flags) { + buff->shmbuffer->info.flags &= (~flags); +} + +void vms_shm_buffer_set_reader_is_ready(vms_shm_buffer *buff) { + assert((vms_shm_buffer_get_flags(buff) & EVENTS_REGISTERED) && + "No events registered before going to the ready state"); + if (!buff->shmbuffer->info.destroyed) - buff->shmbuffer->info.monitor_attached = val; + vms_shm_buffer_set_flags(buff, READER_IS_READY); } /* set the ID of the last processed event */ -void buffer_set_last_processed_id(struct buffer *buff, shm_eventid id) { +void vms_shm_buffer_set_last_processed_id(vms_shm_buffer *buff, + vms_eventid id) { assert(buff->shmbuffer->info.last_processed_id <= id && "The IDs are not monotonic"); buff->shmbuffer->info.last_processed_id = id; } /* for readers */ -void release_shared_buffer(struct buffer *buff) { +void vms_shm_buffer_release(vms_shm_buffer *buff) { if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { perror("release_shared_buffer: munmap failure"); } @@ -365,7 +397,7 @@ void release_shared_buffer(struct buffer *buff) { } /* for writers */ -void destroy_shared_buffer(struct buffer *buff) { +void vms_shm_buffer_destroy(vms_shm_buffer *buff) { buff->shmbuffer->info.destroyed = 1; size_t vecsize = VEC_SIZE(buff->aux_buffers); @@ -381,13 +413,13 @@ void destroy_shared_buffer(struct buffer *buff) { #endif if (munmap(buff->shmbuffer, buff->shmbuffer->info.allocated_size) != 0) { - perror("destroy_shared_buffer: munmap failure"); + perror("vms_shm_buffer_destroy: munmap failure"); } if (close(buff->fd) == -1) { - perror("destroy_shared_buffer: failed closing mmap fd"); + perror("vms_shm_buffer_destroy: failed closing mmap fd"); } - if (shamon_shm_unlink(buff->key) != 0) { - perror("destroy_shared_buffer: shm_unlink failure"); + if (vms_shm_unlink(buff->key) != 0) { + perror("vms_shm_buffer_destroy: vms_unlink failure"); } destroy_shared_control_buffer(buff->key, buff->control); @@ -396,9 +428,9 @@ void destroy_shared_buffer(struct buffer *buff) { free(buff); } -void *buffer_read_pointer(struct buffer *buff, size_t *size) { - struct buffer_info *info = &buff->shmbuffer->info; - size_t tail = shm_spsc_ringbuf_read_off_nowrap(&info->ringbuf, size); +void *vms_shm_buffer_read_pointer(vms_shm_buffer *buff, size_t *size) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; + size_t tail = vms_spsc_ringbuf_read_off_nowrap(&info->ringbuf, size); if (*size == 0) return NULL; /* TODO: get rid of the multiplication, @@ -406,22 +438,22 @@ void *buffer_read_pointer(struct buffer *buff, size_t *size) { return buff->shmbuffer->data + tail * info->elem_size; } -bool buffer_drop_k(struct buffer *buff, size_t k) { - return shm_spsc_ringbuf_consume_upto(_ringbuf(buff), k) == k; +bool vms_shm_buffer_drop_k(vms_shm_buffer *buff, size_t k) { + return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k) == k; } -size_t buffer_consume(struct buffer *buff, size_t k) { - return shm_spsc_ringbuf_consume_upto(_ringbuf(buff), k); +size_t vms_shm_buffer_consume(vms_shm_buffer *buff, size_t k) { + return vms_spsc_ringbuf_consume_upto(_ringbuf(buff), k); } /* buffer_push broken down into several operations: * - * p = buffer_start_push(...) - * p = buffer_partial_push(..., p, ...) + * p = vms_shm_buffer_start_push(...) + * p = vms_shm_buffer_partial_push(..., p, ...) * ... - * p = buffer_partial_push(..., p, ...) - * buffer_partial_push(..., p, ...) - * buffer_finish_push(...) + * p = vms_shm_buffer_partial_push(..., p, ...) + * vms_shm_buffer_partial_push(..., p, ...) + * vms_shm_buffer_finish_push(...) * * All the partial push may push only a single element in sum, * i.e., what can be done with buffer_push. Partial pushes @@ -430,12 +462,12 @@ size_t buffer_consume(struct buffer *buff, size_t k) { * multiple steps. */ -void *buffer_start_push(struct buffer *buff) { - struct buffer_info *info = &buff->shmbuffer->info; +void *vms_shm_buffer_start_push(vms_shm_buffer *buff) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; assert(!info->destroyed && "Writing to a destroyed buffer"); size_t n; - size_t off = shm_spsc_ringbuf_write_off_nowrap(_ringbuf(buff), &n); + size_t off = vms_spsc_ringbuf_write_off_nowrap(_ringbuf(buff), &n); if (n == 0) { /* ++info->dropped; */ return NULL; @@ -450,9 +482,10 @@ void *buffer_start_push(struct buffer *buff) { return mem; } -void *buffer_partial_push(struct buffer *buff, void *prev_push, - const void *elem, size_t size) { - assert(buffer_is_ready(buff) && "Writing to a destroyed buffer"); +void *vms_shm_buffer_partial_push(vms_shm_buffer *buff, void *prev_push, + const void *elem, size_t size) { + assert(!vms_shm_buffer_is_destroyed(buff) && + "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); assert((unsigned char *)prev_push < BUFF_END(buff->shmbuffer)); assert((unsigned char *)prev_push <= BUFF_END(buff->shmbuffer) - size); @@ -462,15 +495,15 @@ void *buffer_partial_push(struct buffer *buff, void *prev_push, return (unsigned char *)prev_push + size; } -static size_t _buffer_push_strn(struct buffer *buff, const void *data, +static size_t _buffer_push_strn(vms_shm_buffer *buff, const void *data, size_t size); -static uint64_t buffer_push_str(struct buffer *buff, uint64_t evid, +static uint64_t buffer_push_str(vms_shm_buffer *buff, uint64_t evid, const char *str); -static uint64_t buffer_push_strn(struct buffer *buff, uint64_t evid, +static uint64_t buffer_push_strn(vms_shm_buffer *buff, uint64_t evid, const char *str, size_t len); -void *buffer_partial_push_str(struct buffer *buff, void *prev_push, - uint64_t evid, const char *str) { +void *vms_shm_buffer_partial_push_str(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); assert((unsigned char *)prev_push < BUFF_END(buff->shmbuffer)); @@ -480,8 +513,9 @@ void *buffer_partial_push_str(struct buffer *buff, void *prev_push, return (unsigned char *)prev_push + sizeof(uint64_t); } -void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, - uint64_t evid, const char *str, size_t len) { +void *vms_shm_buffer_partial_push_str_n(vms_shm_buffer *buff, void *prev_push, + uint64_t evid, const char *str, + size_t len) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(BUFF_START(buff->shmbuffer) <= (unsigned char *)prev_push); assert((unsigned char *)prev_push < BUFF_END(buff->shmbuffer)); @@ -491,46 +525,46 @@ void *buffer_partial_push_str_n(struct buffer *buff, void *prev_push, return (unsigned char *)prev_push + sizeof(uint64_t); } -void buffer_finish_push(struct buffer *buff) { +void vms_shm_buffer_finish_push(vms_shm_buffer *buff) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); - shm_spsc_ringbuf_write_finish(_ringbuf(buff), 1); + vms_spsc_ringbuf_write_finish(_ringbuf(buff), 1); } -bool buffer_push(struct buffer *buff, const void *elem, size_t size) { +bool vms_shm_buffer_push(vms_shm_buffer *buff, const void *elem, size_t size) { assert(!buff->shmbuffer->info.destroyed && "Writing to a destroyed buffer"); assert(buff->shmbuffer->info.elem_size >= size && "Size does not fit the slot"); - void *dst = buffer_start_push(buff); + void *dst = vms_shm_buffer_start_push(buff); if (dst == NULL) return false; memcpy(dst, elem, size); - buffer_finish_push(buff); + vms_shm_buffer_finish_push(buff); return true; } -bool buffer_pop(struct buffer *buff, void *dst) { +bool vms_shm_buffer_pop(vms_shm_buffer *buff, void *dst) { assert(!buff->shmbuffer->info.destroyed && "Reading from a destroyed buffer"); size_t size; - void *pos = buffer_read_pointer(buff, &size); + void *pos = vms_shm_buffer_read_pointer(buff, &size); if (size > 0) { memcpy(dst, pos, buff->shmbuffer->info.elem_size); - shm_spsc_ringbuf_consume(_ringbuf(buff), 1); + vms_spsc_ringbuf_consume(_ringbuf(buff), 1); return true; } return false; } -size_t _buffer_push_strn(struct buffer *buff, const void *data, size_t size) { +size_t _buffer_push_strn(vms_shm_buffer *buff, const void *data, size_t size) { struct aux_buffer *ab = writer_get_aux_buffer(buff, size); assert(ab); assert(ab == buff->cur_aux_buff); - assert(shm_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); + assert(vms_list_last(&buff->aux_buffers_age)->data == buff->cur_aux_buff); size_t off = ab->head; assert(off < (1LU << 32)); @@ -540,21 +574,21 @@ size_t _buffer_push_strn(struct buffer *buff, const void *data, size_t size) { return off; } -void *buffer_get_str(struct buffer *buff, uint64_t elem) { +void *vms_shm_buffer_read_str(vms_shm_buffer *buff, uint64_t elem) { size_t idx = elem >> 32; struct aux_buffer *ab = reader_get_aux_buffer(buff, idx); size_t off = elem & 0xffffffff; return ab->data + off; } -uint64_t buffer_push_str(struct buffer *buff, uint64_t evid, const char *str) { +uint64_t buffer_push_str(vms_shm_buffer *buff, uint64_t evid, const char *str) { size_t len = strlen(str) + 1; size_t off = buffer_push_strn(buff, evid, str, len); assert(buff->cur_aux_buff); return (off | (buff->cur_aux_buff->idx << 32)); } -uint64_t buffer_push_strn(struct buffer *buff, uint64_t evid, const char *str, +uint64_t buffer_push_strn(vms_shm_buffer *buff, uint64_t evid, const char *str, size_t len) { size_t off = _buffer_push_strn(buff, str, len); struct aux_buffer *ab = buff->cur_aux_buff; @@ -565,9 +599,9 @@ uint64_t buffer_push_strn(struct buffer *buff, uint64_t evid, const char *str, return (off | (buff->cur_aux_buff->idx << 32)); } -void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, - uint64_t end_id) { - struct buffer_info *info = &buff->shmbuffer->info; +void vms_shm_buffer_notify_dropped(vms_shm_buffer *buff, uint64_t begin_id, + uint64_t end_id) { + vms_shm_buffer_info *info = &buff->shmbuffer->info; size_t idx = info->dropped_ranges_next; struct dropped_range *r = &info->dropped_ranges[idx]; if (r->begin == begin_id || r->end == r->begin - 1) { @@ -591,8 +625,15 @@ void buffer_notify_dropped(struct buffer *buff, uint64_t begin_id, drop_ranges_unlock(buff); } -int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { - struct event_record *rec = source_control_get_event(b->control, name); +int vms_shm_buffer_register_event(vms_shm_buffer *b, const char *name, + uint64_t kind) { + assert(kind > vms_event_get_last_special_kind() && + "Invalid event kind, it overlaps special kinds"); + + vms_shm_buffer_set_flags(b, EVENTS_REGISTERED); + + struct vms_event_record *rec = + vms_source_control_get_event(b->control, name); if (rec == NULL) { return -1; } @@ -601,14 +642,16 @@ int buffer_register_event(struct buffer *b, const char *name, uint64_t kind) { return 0; } -int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { +int vms_shm_buffer_register_events(vms_shm_buffer *b, size_t ev_nums, ...) { va_list ap; va_start(ap, ev_nums); for (size_t i = 0; i < ev_nums; ++i) { const char *name = va_arg(ap, const char *); - shm_kind kind = va_arg(ap, shm_kind); - if (buffer_register_event(b, name, kind) < 0) { + vms_kind kind = va_arg(ap, vms_kind); + assert(kind > vms_event_get_last_special_kind() && + "Invalid event kind, it overlaps special kinds"); + if (vms_shm_buffer_register_event(b, name, kind) < 0) { va_end(ap); return -1; } @@ -619,12 +662,14 @@ int buffer_register_events(struct buffer *b, size_t ev_nums, ...) { return 0; } -int buffer_register_all_events(struct buffer *b) { - struct event_record *recs = b->control->events; - const size_t ev_nums = source_control_get_records_num(b->control); +int vms_shm_buffer_register_all_events(vms_shm_buffer *b) { + struct vms_event_record *recs = b->control->events; + const size_t ev_nums = vms_source_control_get_records_num(b->control); + + vms_shm_buffer_set_flags(b, EVENTS_REGISTERED); for (size_t i = 0; i < ev_nums; ++i) { - recs[i].kind = 1 + i + shm_get_last_special_kind(); + recs[i].kind = 1 + i + vms_event_get_last_special_kind(); } return 0; diff --git a/src/shmbuf/client.c b/src/shmbuf/client.c index 2daf139d..15893ede 100644 --- a/src/shmbuf/client.c +++ b/src/shmbuf/client.c @@ -1,13 +1,14 @@ +#include "vamos-buffers/shmbuf/client.h" + #include #include #include #include #include -#include "vamos-buffers/shmbuf/buffer.h" #include "shm.h" -#include "vamos-buffers/shmbuf/client.h" #include "vamos-buffers/core/utils.h" +#include "vamos-buffers/shmbuf/buffer.h" #define SLEEP_TIME 20 @@ -24,12 +25,12 @@ static inline void restore_sigfunc(void (*sigfunc)(int)) { } } -int buffer_wait_for_monitor(struct buffer *buff) { +int vms_shm_buffer_wait_for_reader(vms_shm_buffer *buff) { void (*sigfunc)(int); sigfunc = signal(SIGINT, sig_int); int err = 0; - while (!buffer_monitor_attached(buff)) { + while (!vms_shm_buffer_reader_is_ready(buff)) { if (sleep_ms(SLEEP_TIME) != 0) { err = -errno; break; @@ -38,6 +39,11 @@ int buffer_wait_for_monitor(struct buffer *buff) { err = -EINTR; break; } + + if (vms_shm_buffer_is_destroyed(buff)) { + err = -EOWNERDEAD; + break; + } } restore_sigfunc(sigfunc); diff --git a/src/shmbuf/shm.c b/src/shmbuf/shm.c index def2d041..30382bd1 100644 --- a/src/shmbuf/shm.c +++ b/src/shmbuf/shm.c @@ -11,12 +11,12 @@ #include #include -const char *shm_dir = "/dev/shm/"; -const size_t shm_dirlen = 9; +const char *SHM_DIR = "/dev/shm/"; +const size_t SHM_DIRlen = 9; /* adapted function from musl project, src/mman/shm_open.c * and from libc sysdeps/posix/shm-directory.h */ -char *shm_mapname(const char *name, char *buf) { +char *vms_shm_mapname(const char *name, char *buf) { assert(name[0] == '/'); /* Construct the filename. */ while (name[0] == '/') ++name; @@ -27,18 +27,18 @@ char *shm_mapname(const char *name, char *buf) { assert(0 && "invalid shm name"); return 0; } - if (SHM_NAME_MAXLEN <= shm_dirlen + namelen) { + if (SHM_NAME_MAXLEN <= SHM_DIRlen + namelen) { assert(0 && "buffer too short"); return 0; } - memcpy(buf, shm_dir, shm_dirlen); - memcpy(buf + shm_dirlen, name, namelen); - buf[shm_dirlen + namelen] = '\0'; + memcpy(buf, SHM_DIR, SHM_DIRlen); + memcpy(buf + SHM_DIRlen, name, namelen); + buf[SHM_DIRlen + namelen] = '\0'; return buf; } -char *shamon_map_ctrl_key(const char *buffkey, char key[SHM_NAME_MAXLEN]) { +char *vms_shm_map_ctrl_key(const char *buffkey, char key[SHM_NAME_MAXLEN]) { const size_t tmplen = strlen(buffkey); assert(tmplen < SHM_NAME_MAXLEN - 6); memcpy(key, buffkey, tmplen); @@ -49,14 +49,14 @@ char *shamon_map_ctrl_key(const char *buffkey, char key[SHM_NAME_MAXLEN]) { return key; } -int shamon_shm_open(const char *key, int flags, mode_t mode) { +int vms_shm_open(const char *key, int flags, mode_t mode) { char name[SHM_NAME_MAXLEN]; - if (shm_mapname(key, name) == 0) + if (vms_shm_mapname(key, name) == 0) abort(); return open(name, flags | O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK, mode); } -int shamon_get_tmp_key(const char *key, char *buf, size_t bufsize) { +int vms_shm_get_tmp_key(const char *key, char *buf, size_t bufsize) { if (strlen(key) + 5 > bufsize) { return -1; } @@ -66,22 +66,22 @@ int shamon_get_tmp_key(const char *key, char *buf, size_t bufsize) { return 0; } -int shamon_shm_rename(const char *old_key, const char *new_key) { +int vms_shm_rename(const char *old_key, const char *new_key) { char old_name[SHM_NAME_MAXLEN]; char new_name[SHM_NAME_MAXLEN]; - if (shm_mapname(old_key, old_name) == 0) { + if (vms_shm_mapname(old_key, old_name) == 0) { return -1; } - if (shm_mapname(new_key, new_name) == 0) { + if (vms_shm_mapname(new_key, new_name) == 0) { return -1; } return rename(old_name, new_name); } -int shamon_shm_unlink(const char *key) { +int vms_shm_unlink(const char *key) { /* fprintf(stderr, "UNLINK: %s\n", key); */ char name[SHM_NAME_MAXLEN]; - if (shm_mapname(key, name) == 0) + if (vms_shm_mapname(key, name) == 0) abort(); return unlink(name); } diff --git a/src/shmbuf/shm.h b/src/shmbuf/shm.h index 408fc8d5..5958df5c 100644 --- a/src/shmbuf/shm.h +++ b/src/shmbuf/shm.h @@ -1,20 +1,20 @@ -#ifndef SHAMON_SHM_H -#define SHAMON_SHM_H +#ifndef VAMOS_SHM_H +#define VAMOS_SHM_H #include #include #define SHM_NAME_MAXLEN 256 -const char *shamon_shm_default_key(void); -const char *shamon_shm_default_ctrl_key(void); +const char *shamon_vms_default_key(void); +const char *shamon_vms_default_ctrl_key(void); -int shamon_shm_open(const char *key, int flags, mode_t mode); -int shamon_shm_rename(const char *old_key, const char *new_key); -int shamon_shm_unlink(const char *key); +int vms_shm_open(const char *key, int flags, mode_t mode); +int vms_shm_rename(const char *old_key, const char *new_key); +int vms_shm_unlink(const char *key); -char *shm_mapname(const char *name, char *buf); -int shamon_get_tmp_key(const char *key, char *buf, size_t bufsize); -char *shamon_map_ctrl_key(const char *key, char name[SHM_NAME_MAXLEN]); +char *vms_shm_mapname(const char *name, char *buf); +int vms_shm_get_tmp_key(const char *key, char *buf, size_t bufsize); +char *vms_shm_map_ctrl_key(const char *key, char name[SHM_NAME_MAXLEN]); -#endif /* SHAMON_SHM_H */ +#endif /* VAMOS_SHM_H */ diff --git a/src/shmbuf/utils.c b/src/shmbuf/utils.c index 4717cc10..440f0285 100644 --- a/src/shmbuf/utils.c +++ b/src/shmbuf/utils.c @@ -3,7 +3,7 @@ #include "buffer-private.h" -size_t compute_shm_size(size_t elem_size, size_t capacity) { +size_t compute_vms_size(size_t elem_size, size_t capacity) { /* compute how much memory we need */ size_t size = (elem_size * capacity) + sizeof(struct shmbuffer); /* round it up to page size. @@ -24,7 +24,7 @@ size_t compute_shm_size(size_t elem_size, size_t capacity) { return size; } -size_t compute_shm_buffer_size(size_t nondata_size, size_t elem_size, +size_t compute_vms_buffer_size(size_t nondata_size, size_t elem_size, size_t capacity) { /* compute how much memory we need */ size_t size = (elem_size * capacity) + nondata_size; diff --git a/src/streams/legacy/stream-fastbuf-io.c b/src/streams/legacy/stream-fastbuf-io.c index ee3db0d6..070254e1 100644 --- a/src/streams/legacy/stream-fastbuf-io.c +++ b/src/streams/legacy/stream-fastbuf-io.c @@ -7,7 +7,7 @@ #include #include -#include "fastbuf/shm_monitor.h" +#include "fastbuf/vms_monitor.h" // FIXME: do these local to a stream static int monitoring_active = 0; @@ -82,11 +82,11 @@ int register_monitored_thread(monitor_buffer buffer) { thrd_create(&thrd, &monitoring_thread, buffer); } -static bool io_has_event(shm_stream *stream) { - // shm_stream_io *fs = (shm_stream_io *) stream; +static bool io_has_event(vms_stream *stream) { + // vms_stream_io *fs = (vms_stream_io *) stream; //// dispatch pending events if available - // if (shm_queue_size(fs->pending_events) > 0) + // if (vms_queue_size(fs->pending_events) > 0) // return true; //// check for new events @@ -98,43 +98,43 @@ static bool io_has_event(shm_stream *stream) { // size_t num = read_events(fs); // // num can be 0 if all io get closed (which // // is what poll detects) - // assert(num == 0 || shm_queue_size(fs->pending_events)); + // assert(num == 0 || vms_queue_size(fs->pending_events)); // } - // return shm_queue_size(fs->pending_events); + // return vms_queue_size(fs->pending_events); return true; } -static shm_event_io *io_get_next_event(shm_stream *stream) { - // shm_stream_io *ss = (shm_stream_io *) stream; - // static shm_event_fd_in ev; - // if (shm_queue_pop(ss->pending_events, &ev)) +static vms_event_io *io_get_next_event(vms_stream *stream) { + // vms_stream_io *ss = (vms_stream_io *) stream; + // static vms_event_fd_in ev; + // if (vms_queue_pop(ss->pending_events, &ev)) // return &ev; return NULL; } -shm_stream *shm_create_io_stream(pid_t pid) { +vms_stream *vms_create_io_stream(pid_t pid) { char *name = malloc(21); // FIXME: we leak this assert(name); snprintf(name, 21, "io-stream(%d)", pid); - shm_stream_io *ss = malloc(sizeof *ss); + vms_stream_io *ss = malloc(sizeof *ss); assert(ss); - shm_stream_init((shm_stream *)ss, sizeof(shm_event_io), - (shm_stream_has_event_fn)io_has_event, - (shm_stream_get_next_event_fn)io_get_next_event, name); + vms_stream_init((vms_stream *)ss, sizeof(vms_event_io), + (vms_stream_has_event_fn)io_has_event, + (vms_stream_get_next_event_fn)io_get_next_event, name); ss->ev_kind_in = - shm_mk_event_kind("io-in", sizeof(shm_event_io), NULL, NULL); + vms_mk_event_kind("io-in", sizeof(vms_event_io), NULL, NULL); ss->ev_kind_out = - shm_mk_event_kind("io-out", sizeof(shm_event_io), NULL, NULL); + vms_mk_event_kind("io-out", sizeof(vms_event_io), NULL, NULL); monitoring_active = 1; monitored_process proc = attach_to_process(pid, ®ister_monitored_thread); // wait_for_process(proc); - return (shm_stream *)ss; + return (vms_stream *)ss; } -void shm_destroy_io_stream(shm_stream_io *s) { monitoring_active = 0; } +void vms_destroy_io_stream(vms_stream_io *s) { monitoring_active = 0; } diff --git a/src/streams/legacy/stream-fastbuf-io.h b/src/streams/legacy/stream-fastbuf-io.h index 4d45e36c..398f4707 100644 --- a/src/streams/legacy/stream-fastbuf-io.h +++ b/src/streams/legacy/stream-fastbuf-io.h @@ -5,25 +5,25 @@ #include "event.h" #include "stream.h" -typedef struct _shm_event_io { +typedef struct _vms_event_io { // mandatory - shm_event base; + vms_event base; // data int fd; uint64_t time; - shm_string_ref str_ref; -} shm_event_io; + vms_string_ref str_ref; +} vms_event_io; -typedef struct _shm_stream_io { - shm_stream base; +typedef struct _vms_stream_io { + vms_stream base; pid_t pid; // the kind that we assign to events. If a stream can have only one // kind of events, we can move this to base (derive it from the stream name) - shm_kind ev_kind_in; - shm_kind ev_kind_out; -} shm_stream_io; + vms_kind ev_kind_in; + vms_kind ev_kind_out; +} vms_stream_io; -shm_stream *shm_create_io_stream(pid_t pid); -void shm_destroy_io_stream(shm_stream_io *s); +vms_stream *vms_create_io_stream(pid_t pid); +void vms_destroy_io_stream(vms_stream_io *s); -// void shm_destroy_io_stream(shm_stream_stdin *ss); +// void vms_destroy_io_stream(vms_stream_stdin *ss); diff --git a/src/streams/legacy/stream-fds.c b/src/streams/legacy/stream-fds.c index c39a7e41..c5f02b55 100644 --- a/src/streams/legacy/stream-fds.c +++ b/src/streams/legacy/stream-fds.c @@ -8,15 +8,15 @@ #include "arbiter.h" -static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { +static size_t read_events(vms_stream_fds *ss, vms_arbiter_buffer *buffer) { size_t read_ev = 0; size_t remove_num = 0; - shm_event_fd_in ev; + vms_event_fd_in ev; for (unsigned i = 0; i < ss->fds_num; ++i) { struct pollfd *pfd = ss->fds + i; if (pfd->revents & POLLIN) { - shm_string *str = ss->fds_buffer + i; + vms_string *str = ss->fds_buffer + i; // FIXME: set O_NONBLOCK and read all the data right now // to have the right timestamps? ssize_t len = read(pfd->fd, str->data, str->alloc_size); @@ -32,13 +32,13 @@ static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { } else { assert(len > 0); ev.time = clock(); - // ev.base.stream = (shm_stream *) ss; + // ev.base.stream = (vms_stream *) ss; ev.base.kind = ss->ev_kind_in; - ev.base.id = shm_stream_get_next_id((shm_stream *)ss); + ev.base.id = vms_stream_get_next_id((vms_stream *)ss); ev.fd = pfd->fd; ev.str_ref.size = len; ev.str_ref.data = str->data; - shm_arbiter_buffer_push(buffer, &ev, sizeof(ev)); + vms_arbiter_buffer_push(buffer, &ev, sizeof(ev)); ++read_ev; } } @@ -54,7 +54,7 @@ static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { struct pollfd *new_fds = malloc(ss->fds_num * sizeof(struct pollfd)); assert(new_fds); // we could just trim the memory, but... - shm_string *new_fds_buffer = malloc(ss->fds_num * sizeof(shm_string)); + vms_string *new_fds_buffer = malloc(ss->fds_num * sizeof(vms_string)); assert(new_fds_buffer); unsigned idx = 0; @@ -74,9 +74,9 @@ static size_t read_events(shm_stream_fds *ss, shm_arbiter_buffer *buffer) { return read_ev; } -static size_t fds_buffer_events(shm_stream *stream, - shm_arbiter_buffer *buffer) { - shm_stream_fds *fs = (shm_stream_fds *)stream; +static size_t fds_buffer_events(vms_stream *stream, + vms_arbiter_buffer *buffer) { + vms_stream_fds *fs = (vms_stream_fds *)stream; int ret = poll(fs->fds, fs->fds_num, 0); if (ret == -1) { @@ -88,33 +88,33 @@ static size_t fds_buffer_events(shm_stream *stream, return 0; } -static bool fds_is_ready(shm_stream *stream) { - return ((shm_stream_fds *)stream)->fds_num > 0; +static bool fds_is_ready(vms_stream *stream) { + return ((vms_stream_fds *)stream)->fds_num > 0; } -shm_stream *shm_create_fds_stream() { - shm_stream_fds *ss = malloc(sizeof *ss); - shm_stream_init((shm_stream *)ss, sizeof(shm_event_fd_in), +vms_stream *vms_create_fds_stream() { + vms_stream_fds *ss = malloc(sizeof *ss); + vms_stream_init((vms_stream *)ss, sizeof(vms_event_fd_in), fds_buffer_events, NULL, fds_is_ready, "fds-stream"); - ss->ev_kind_in = shm_mk_event_kind("fd-in", (shm_stream *)ss, - sizeof(shm_event_fd_in), NULL, NULL); + ss->ev_kind_in = vms_mk_event_kind("fd-in", (vms_stream *)ss, + sizeof(vms_event_fd_in), NULL, NULL); ss->fds = NULL; ss->fds_size = 0; ss->fds_num = 0; ss->fds_buffer = NULL; - shm_queue_init(&ss->pending_events, 32, sizeof(shm_event_fd_in)); + vms_queue_init(&ss->pending_events, 32, sizeof(vms_event_fd_in)); - return (shm_stream *)ss; + return (vms_stream *)ss; } -void shm_stream_fds_add_fd(shm_stream_fds *stream, int fd) { +void vms_stream_fds_add_fd(vms_stream_fds *stream, int fd) { size_t idx = stream->fds_num++; if (idx >= stream->fds_size) { stream->fds_size += 8; stream->fds = realloc(stream->fds, sizeof(struct pollfd) * stream->fds_size); stream->fds_buffer = - realloc(stream->fds_buffer, sizeof(shm_string) * stream->fds_size); + realloc(stream->fds_buffer, sizeof(vms_string) * stream->fds_size); } assert(stream->fds_num < stream->fds_size); @@ -127,7 +127,7 @@ void shm_stream_fds_add_fd(shm_stream_fds *stream, int fd) { stream->fds_buffer[idx].size = 0; } -void shm_destroy_fds_stream(shm_stream_fds *ss) { +void vms_destroy_fds_stream(vms_stream_fds *ss) { for (unsigned i = 0; i < ss->fds_num; ++i) { free(ss->fds_buffer[i].data); } diff --git a/src/streams/legacy/stream-fds.h b/src/streams/legacy/stream-fds.h index 6547cefe..ed999552 100644 --- a/src/streams/legacy/stream-fds.h +++ b/src/streams/legacy/stream-fds.h @@ -6,17 +6,17 @@ #include "queue.h" #include "stream.h" -typedef struct _shm_event_fd_in { +typedef struct _vms_event_fd_in { // mandatory - shm_event base; + vms_event base; // data int fd; uint64_t time; - shm_string_ref str_ref; -} shm_event_fd_in; + vms_string_ref str_ref; +} vms_event_fd_in; -typedef struct _shm_stream_fds { - shm_stream base; +typedef struct _vms_stream_fds { + vms_stream base; // user can add here some auxiliary data if needed // ... // the pointer to the read lines @@ -24,7 +24,7 @@ typedef struct _shm_stream_fds { // the kind that we assign to events. If a stream can have only one // kind of events, we can move this to base (derive it // from the stream name) - shm_kind ev_kind_in; + vms_kind ev_kind_in; // filedescriptors that we track struct pollfd *fds; // allocated size @@ -32,12 +32,12 @@ typedef struct _shm_stream_fds { // number of filedescriptors size_t fds_num; // buffer for each fd for reading - shm_string *fds_buffer; + vms_string *fds_buffer; // events to be pushed to the monitor - shm_queue pending_events; -} shm_stream_fds; + vms_queue pending_events; +} vms_stream_fds; -shm_stream *shm_create_fds_stream(); -void shm_stream_fds_add_fd(shm_stream_fds *stream, int fd); +vms_stream *vms_create_fds_stream(); +void vms_stream_fds_add_fd(vms_stream_fds *stream, int fd); -void shm_destroy_fds_stream(shm_stream_fds *ss); +void vms_destroy_fds_stream(vms_stream_fds *ss); diff --git a/src/streams/legacy/stream-stdin.c b/src/streams/legacy/stream-stdin.c index 31a0959d..20ad927c 100644 --- a/src/streams/legacy/stream-stdin.c +++ b/src/streams/legacy/stream-stdin.c @@ -7,11 +7,11 @@ #include "arbiter.h" -bool stdin_is_ready(shm_stream *stream) { return true; } +bool stdin_is_ready(vms_stream *stream) { return true; } -void stdin_copy_event(shm_stream *stream, shm_event *in, shm_event *out) { - static shm_event_stdin ev; - shm_stream_stdin *ss = (shm_stream_stdin *)stream; +void stdin_copy_event(vms_stream *stream, vms_event *in, vms_event *out) { + static vms_event_stdin ev; + vms_stream_stdin *ss = (vms_stream_stdin *)stream; ssize_t len = getline(&ss->line, &ss->line_len, stdin); // TODO: return end-of-stream event @@ -26,22 +26,22 @@ void stdin_copy_event(shm_stream *stream, shm_event *in, shm_event *out) { ev.str_ref.size = len; ev.str_ref.data = ss->line; - shm_arbiter_buffer_push(buffer, &ev, sizeof(ev)); + vms_arbiter_buffer_push(buffer, &ev, sizeof(ev)); return 1; } -shm_stream *shm_create_stdin_stream() { - shm_stream_stdin *ss = malloc(sizeof *ss); - shm_stream_init((shm_stream *)ss, sizeof(shm_event_stdin), stdin_is_ready, +vms_stream *vms_create_stdin_stream() { + vms_stream_stdin *ss = malloc(sizeof *ss); + vms_stream_init((vms_stream *)ss, sizeof(vms_event_stdin), stdin_is_ready, /* filter */ NULL, copy_event, "stdin-stream"); ss->line = NULL; ss->line_len = 0; - ss->ev_kind = shm_mk_event_kind("stdin", (shm_stream *)ss, - sizeof(shm_event_stdin), NULL, NULL); - return (shm_stream *)ss; + ss->ev_kind = vms_mk_event_kind("stdin", (vms_stream *)ss, + sizeof(vms_event_stdin), NULL, NULL); + return (vms_stream *)ss; } -void shm_destroy_stdin_stream(shm_stream_stdin *ss) { +void vms_destroy_stdin_stream(vms_stream_stdin *ss) { free(ss->line); free(ss); } diff --git a/src/streams/legacy/stream-stdin.h b/src/streams/legacy/stream-stdin.h index af2feeb8..28f03e56 100644 --- a/src/streams/legacy/stream-stdin.h +++ b/src/streams/legacy/stream-stdin.h @@ -5,17 +5,17 @@ #include "event.h" #include "stream.h" -typedef struct _shm_event_stdin { +typedef struct _vms_event_stdin { // mandatory - shm_event base; + vms_event base; // data int fd; uint64_t time; - shm_string_ref str_ref; -} shm_event_stdin; + vms_string_ref str_ref; +} vms_event_stdin; -typedef struct _shm_stream_stdin { - shm_stream base; +typedef struct _vms_stream_stdin { + vms_stream base; // user can add here some auxiliary data if needed // ... // the pointer to the read lines @@ -24,14 +24,14 @@ typedef struct _shm_stream_stdin { // the kind that we assign to events. If a stream can have only one // kind of events, we can move this to base (derive it // from the stream name) - shm_kind ev_kind; - shm_eventid last_event_id; -} shm_stream_stdin; + vms_kind ev_kind; + vms_eventid last_event_id; +} vms_stream_stdin; -bool stdin_has_event(shm_stream *stream); +bool stdin_has_event(vms_stream *stream); -size_t stdin_buffer_events(shm_stream *stream, shm_arbiter_buffer *buffer); +size_t stdin_buffer_events(vms_stream *stream, vms_arbiter_buffer *buffer); -shm_stream *shm_create_stdin_stream(); +vms_stream *vms_create_stdin_stream(); -void shm_destroy_stdin_stream(shm_stream_stdin *ss); +void vms_destroy_stdin_stream(vms_stream_stdin *ss); diff --git a/src/streams/stream-drregex.c b/src/streams/stream-drregex.c index fa9f3252..3894ec0a 100644 --- a/src/streams/stream-drregex.c +++ b/src/streams/stream-drregex.c @@ -1,33 +1,33 @@ +#include "vamos-buffers/streams/stream-drregex.h" + #include #include #include #include -#include "vamos-buffers/streams/stream-drregex.h" #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" - -bool drregex_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_drregex *)stream)->shmbuffer; +bool drregex_is_ready(vms_stream *stream) { + vms_shm_buffer *b = ((vms_stream_drregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } -void drregex_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void drregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_drregex_stream(const char *key, const char *name) { - shm_stream_drregex *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); +vms_stream *vms_create_drregex_stream(const char *key, const char *name) { + vms_stream_drregex *ss = malloc(sizeof *ss); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, drregex_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, drregex_is_ready, NULL, drregex_alter, NULL, NULL, "drregex-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/stream-funs.c b/src/streams/stream-funs.c index 333abdca..9c09671f 100644 --- a/src/streams/stream-funs.c +++ b/src/streams/stream-funs.c @@ -1,48 +1,48 @@ +#include "vamos-buffers/streams/stream-funs.h" + #include #include #include #include -#include "vamos-buffers/streams/stream-funs.h" #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" - -bool funs_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_funs *)stream)->shmbuffer; +bool funs_is_ready(vms_stream *stream) { + vms_shm_buffer *b = ((vms_stream_funs *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_reader_is_ready(b) || vms_shm_buffer_size(b) > 0; } -void funs_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void funs_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -size_t stream_mk_event_kinds(const char *stream_name, struct buffer *shmbuffer, +size_t stream_mk_event_kinds(const char *stream_name, vms_shm_buffer *shmbuffer, size_t *max_ev_size); -shm_stream *shm_create_funs_stream(const char *key, const char *name) { - shm_stream_funs *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); +vms_stream *vms_create_funs_stream(const char *key, const char *name) { + vms_stream_funs *ss = malloc(sizeof *ss); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, funs_is_ready, NULL, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, funs_is_ready, NULL, funs_alter, NULL, NULL, "funs-stream", name); ss->shmbuffer = shmbuffer; ss->ev_buff = NULL; - return (shm_stream *)ss; + return (vms_stream *)ss; } -const char *shm_stream_funs_get_str(shm_stream_funs *fstream, uint64_t elem) { - return buffer_get_str(fstream->shmbuffer, elem); +const char *vms_stream_funs_get_str(vms_stream_funs *fstream, uint64_t elem) { + return vms_shm_buffer_read_str(fstream->shmbuffer, elem); } -struct event_record *shm_funs_stream_get_event_spec(shm_stream_funs *stream, - shm_kind kind) { +struct vms_event_record *vms_funs_stream_get_event_spec(vms_stream_funs *stream, + vms_kind kind) { for (size_t i = 0; i < stream->spec_count; ++i) if (stream->events[i].kind == kind) return stream->events + i; diff --git a/src/streams/stream-generic.c b/src/streams/stream-generic.c index 64eb179b..63a9beb9 100644 --- a/src/streams/stream-generic.c +++ b/src/streams/stream-generic.c @@ -8,32 +8,32 @@ #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" - /* A generic stream for events stored in shared memory buffer, no filter nor modification of events supported (unless done manually). */ -static bool generic_is_ready(shm_stream *stream) { - struct buffer *b = stream->incoming_events_buffer; +static bool generic_is_ready(vms_stream *stream) { + vms_shm_buffer *b = stream->incoming_events_buffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } -static void generic_alter(shm_stream *stream, shm_event *in, shm_event *out) { +static void generic_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_generic_stream(const char *key, const char *name, - shm_stream_hole_handling *hole_handling) { - shm_stream_generic *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); +vms_stream *vms_create_generic_stream( + const char *key, const char *name, + const vms_stream_hole_handling *hole_handling) { + vms_stream_generic *ss = malloc(sizeof *ss); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, generic_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, generic_is_ready, NULL, generic_alter, NULL, hole_handling, "generic-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/stream-regex.c b/src/streams/stream-regex.c index c5463dad..ffbee55f 100644 --- a/src/streams/stream-regex.c +++ b/src/streams/stream-regex.c @@ -8,29 +8,29 @@ #include "vamos-buffers/core/arbiter.h" #include "vamos-buffers/shmbuf/buffer.h" -bool sregex_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_sregex *)stream)->shmbuffer; +bool sregex_is_ready(vms_stream *stream) { + vms_shm_buffer *b = ((vms_stream_sregex *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } -void sregex_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void sregex_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_sregex_stream( +vms_stream *vms_create_sregex_stream( const char *key, const char *name, - const shm_stream_hole_handling *hole_handling) { - shm_stream_sregex *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); + const vms_stream_hole_handling *hole_handling) { + vms_stream_sregex *ss = malloc(sizeof *ss); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, sregex_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, sregex_is_ready, /* filter = */ NULL, sregex_alter, NULL, hole_handling, "regex-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/stream-regexrw.c b/src/streams/stream-regexrw.c index 8fc94221..30f8c7a1 100644 --- a/src/streams/stream-regexrw.c +++ b/src/streams/stream-regexrw.c @@ -1,32 +1,33 @@ +#include "vamos-buffers/streams/stream-regexrw.h" + #include #include #include #include -#include "vamos-buffers/streams/stream-regexrw.h" #include "vamos-buffers/core/arbiter.h" -bool sregexrw_is_ready(shm_stream *stream) { - struct buffer *b = ((shm_stream_sregexrw *)stream)->shmbuffer; +bool sregexrw_is_ready(vms_stream *stream) { + vms_shm_buffer *b = ((vms_stream_sregexrw *)stream)->shmbuffer; /* buffer must be ready or it may not be ready anymore, but it * still has some data that we haven't read */ - return buffer_is_ready(b) || buffer_size(b) > 0; + return vms_shm_buffer_is_ready(b) || vms_shm_buffer_size(b) > 0; } -void sregexrw_alter(shm_stream *stream, shm_event *in, shm_event *out) { +void sregexrw_alter(vms_stream *stream, vms_event *in, vms_event *out) { memcpy(out, in, stream->event_size); } -shm_stream *shm_create_sregexrw_stream(const char *key, const char *name) { - shm_stream_sregexrw *ss = malloc(sizeof *ss); - struct buffer *shmbuffer = get_shared_buffer(key); +vms_stream *vms_create_sregexrw_stream(const char *key, const char *name) { + vms_stream_sregexrw *ss = malloc(sizeof *ss); + vms_shm_buffer *shmbuffer = vms_shm_buffer_connect(key); assert(shmbuffer && "Getting the shm buffer failed"); - size_t elem_size = buffer_elem_size(shmbuffer); + size_t elem_size = vms_shm_buffer_elem_size(shmbuffer); assert(elem_size > 0); - shm_stream_init((shm_stream *)ss, shmbuffer, elem_size, sregexrw_is_ready, + vms_stream_init((vms_stream *)ss, shmbuffer, elem_size, sregexrw_is_ready, NULL, sregexrw_alter, NULL, NULL, "regexrw-stream", name); ss->shmbuffer = shmbuffer; - return (shm_stream *)ss; + return (vms_stream *)ss; } diff --git a/src/streams/streams.c b/src/streams/streams.c index f385a73e..24623d3d 100644 --- a/src/streams/streams.c +++ b/src/streams/streams.c @@ -3,12 +3,12 @@ #include #include +#include "vamos-buffers/core/stream.h" #include "vamos-buffers/streams/stream-drregex.h" #include "vamos-buffers/streams/stream-funs.h" #include "vamos-buffers/streams/stream-generic.h" #include "vamos-buffers/streams/stream-regex.h" #include "vamos-buffers/streams/stream-regexrw.h" -#include "vamos-buffers/core/stream.h" const char *find_next_part(const char *params) { params = strchr(params, ':'); @@ -59,8 +59,8 @@ static struct stream_rec avail_streams[] = { {NULL, NULL} /* to mark the end */ }; -shm_stream *shm_stream_create(const char *stream_name, const char *spec, - shm_stream_hole_handling *hole_handling) { +vms_stream *vms_stream_create(const char *stream_name, const char *spec, + vms_stream_hole_handling *hole_handling) { const char *next = find_next_part(spec); /* skip the name */ if (!next) { fprintf(stderr, @@ -92,7 +92,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, return NULL; } - return shm_create_funs_stream(key, stream_name); + return vms_create_funs_stream(key, stream_name); } else if (strncmp(source, "regex", 6) == 0) { if (!next || *next == 0) { fprintf( @@ -108,7 +108,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_sregex_stream(key, stream_name, hole_handling); + return vms_create_sregex_stream(key, stream_name, hole_handling); } else if (strncmp(source, "regexrw", 8) == 0) { if (!next || *next == 0) { fprintf( @@ -124,7 +124,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_sregexrw_stream(key, stream_name); + return vms_create_sregexrw_stream(key, stream_name); } else if (strncmp(source, "drregex", 8) == 0) { if (!next || *next == 0) { fprintf( @@ -140,7 +140,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_drregex_stream(key, stream_name); + return vms_create_drregex_stream(key, stream_name); } else if (strncmp(source, "generic", 8) == 0) { if (!next || *next == 0) { fprintf( @@ -156,7 +156,7 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, "parameter (FOR NOW)\n"); } - return shm_create_generic_stream(key, stream_name, hole_handling); + return vms_create_generic_stream(key, stream_name, hole_handling); } fprintf(stderr, "Unknown stream. Available streams:\n"); @@ -169,9 +169,9 @@ shm_stream *shm_stream_create(const char *stream_name, const char *spec, return NULL; } -shm_stream *shm_stream_create_from_argv( +vms_stream *vms_stream_create_from_argv( const char *stream_name, int argc, char *argv[], - shm_stream_hole_handling *hole_handling) { + vms_stream_hole_handling *hole_handling) { const char *spec = get_spec(stream_name, argc, argv); if (!spec) { fprintf(stderr, "error: did not find spec for stream '%s'\n", @@ -179,5 +179,5 @@ shm_stream *shm_stream_create_from_argv( return NULL; } - return shm_stream_create(stream_name, spec, hole_handling); + return vms_stream_create(stream_name, spec, hole_handling); } diff --git a/tests/arbiter-buffer.c b/tests/arbiter-buffer.c index 90d19eb6..9bd705bf 100644 --- a/tests/arbiter-buffer.c +++ b/tests/arbiter-buffer.c @@ -8,75 +8,75 @@ #include #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" +#include "vamos-buffers/shmbuf/buffer.h" -static bool is_ready(shm_stream *s) { +static bool is_ready(vms_stream *s) { (void)s; return false; } struct event { - shm_event base; + vms_event base; int i; }; -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - size_t capacity, - struct source_control *control); +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + size_t capacity, + struct vms_source_control *control); int main(void) { - struct buffer *lbuffer = + vms_shm_buffer *lbuffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(lbuffer); - shm_stream dummy_stream; - shm_stream *stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream *stream = &dummy_stream; - shm_stream_init(stream, lbuffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, lbuffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - shm_arbiter_buffer *b = - shm_arbiter_buffer_create(stream, sizeof(struct event), 20); - shm_arbiter_buffer_set_active(b, true); + vms_arbiter_buffer *b = + vms_arbiter_buffer_create(stream, sizeof(struct event), 20); + vms_arbiter_buffer_set_active(b, true); struct event ev; for (int i = 1; i < 21; ++i) { ev.base.id = i; ev.i = i; - shm_arbiter_buffer_push(b, &ev, sizeof(struct event)); + vms_arbiter_buffer_push(b, &ev, sizeof(struct event)); } - assert(shm_arbiter_buffer_size(b) == 20); - assert(shm_arbiter_buffer_drop_older_than(b, 10) == 10); - assert(shm_arbiter_buffer_size(b) == 10); - assert(shm_arbiter_buffer_drop(b, 20) == 10); - assert(shm_arbiter_buffer_size(b) == 0); + assert(vms_arbiter_buffer_size(b) == 20); + assert(vms_arbiter_buffer_drop_older_than(b, 10) == 10); + assert(vms_arbiter_buffer_size(b) == 10); + assert(vms_arbiter_buffer_drop(b, 20) == 10); + assert(vms_arbiter_buffer_size(b) == 0); for (int i = 1; i < 21; ++i) { ev.base.id = 20 + 2 * i; ev.i = i; - shm_arbiter_buffer_push(b, &ev, sizeof(struct event)); + vms_arbiter_buffer_push(b, &ev, sizeof(struct event)); } - assert(shm_arbiter_buffer_size(b) == 20); - assert(shm_arbiter_buffer_drop_older_than(b, 39) == 9); - assert(shm_arbiter_buffer_size(b) == 11); - assert(shm_arbiter_buffer_drop_older_than(b, 58) == 10); - assert(shm_arbiter_buffer_size(b) == 1); - assert(shm_arbiter_buffer_drop_older_than(b, 70) == 1); - assert(shm_arbiter_buffer_size(b) == 0); + assert(vms_arbiter_buffer_size(b) == 20); + assert(vms_arbiter_buffer_drop_older_than(b, 39) == 9); + assert(vms_arbiter_buffer_size(b) == 11); + assert(vms_arbiter_buffer_drop_older_than(b, 58) == 10); + assert(vms_arbiter_buffer_size(b) == 1); + assert(vms_arbiter_buffer_drop_older_than(b, 70) == 1); + assert(vms_arbiter_buffer_size(b) == 0); for (int i = 1; i < 21; ++i) { ev.base.id = 40 + 2 * i; ev.i = i; - shm_arbiter_buffer_push(b, &ev, sizeof(struct event)); + vms_arbiter_buffer_push(b, &ev, sizeof(struct event)); } - assert(shm_arbiter_buffer_drop_older_than(b, 3) == 0); - assert(shm_arbiter_buffer_size(b) == 20); - assert(shm_arbiter_buffer_drop_older_than(b, 90) == 20); - assert(shm_arbiter_buffer_size(b) == 0); + assert(vms_arbiter_buffer_drop_older_than(b, 3) == 0); + assert(vms_arbiter_buffer_size(b) == 20); + assert(vms_arbiter_buffer_drop_older_than(b, 90) == 20); + assert(vms_arbiter_buffer_size(b) == 0); return 0; } diff --git a/tests/bench/ringbufs.c b/tests/bench/ringbufs.c index fe3d431a..c569c6a3 100644 --- a/tests/bench/ringbufs.c +++ b/tests/bench/ringbufs.c @@ -30,8 +30,8 @@ static double report_time(const char *msg, struct timespec *start, #if 0 static void run_shmbuf_push_pop_st() { - struct source_control *ctrl = source_control_define(1, "dummy", "i"); - struct buffer *buff = create_shared_buffer("/test", sizeof(int), ctrl); + struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); + vms_shm_buffer *buff = vms_shm_buffer_create("/test", sizeof(int), ctrl); assert(buff); assert(buffer_capacity(buff) >= N); @@ -59,18 +59,18 @@ static void run_shmbuf_push_pop_st() { printf("\033[34m[shmbuf], single-threaded, push/pop: %lf seconds.\033[0m\n", elapsed); assert(buffer_size(buff) == 0); - destroy_shared_buffer(buff); + vms_shm_buffer_destroy(buff); } /* to see if there is any difference between malloce'd and shared memory */ -struct buffer *initialize_local_buffer(const char *key, size_t elem_size, - struct source_control *control); -void release_local_buffer(struct buffer *buff); +vms_shm_buffer *initialize_local_buffer(const char *key, size_t elem_size, + struct vms_source_control *control); +void release_local_buffer(vms_shm_buffer *buff); static void run_local_shmbuf_push_pop_st() { - struct source_control *ctrl = source_control_define(1, "dummy", "i"); - struct buffer *buff = initialize_local_buffer("/test", sizeof(int), ctrl); + struct vms_source_control *ctrl = vms_source_control_define(1, "dummy", "i"); + vms_shm_buffer *buff = initialize_local_buffer("/test", sizeof(int), ctrl); assert(buff); @@ -104,26 +104,26 @@ static void run_local_shmbuf_push_pop_st() { #endif static void run_par_queue_push_pop_st(void) { - shm_par_queue q; - shm_par_queue_init(&q, N, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, N, sizeof(int)); struct timespec start, mid, end; double elapsed = 0; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (int i = 0; i < N; ++i) { - shm_par_queue_push(&q, &i, sizeof(int)); + vms_par_queue_push(&q, &i, sizeof(int)); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); elapsed += report_time("[par-queue] single-threaded, push", &start, &mid); - assert(shm_par_queue_size(&q) == N); + assert(vms_par_queue_size(&q) == N); int j; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); for (int i = 0; i < N; ++i) { - shm_par_queue_pop(&q, &j); + vms_par_queue_pop(&q, &j); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); @@ -132,13 +132,13 @@ static void run_par_queue_push_pop_st(void) { "\033[34m[par-queue], single-threaded, push/pop: %lf seconds.\033[0m\n", elapsed); - assert(shm_par_queue_size(&q) == 0); - shm_par_queue_destroy(&q); + assert(vms_par_queue_size(&q) == 0); + vms_par_queue_destroy(&q); } static void run_queue_spsc_push_pop_st(void) { - shm_queue_spsc q; - shm_queue_spsc_init(&q, N); + vms_queue_spsc q; + vms_queue_spsc_init(&q, N); int *buff = malloc(sizeof(int) * N); assert(buff); @@ -150,10 +150,10 @@ static void run_queue_spsc_push_pop_st(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (int i = 0; i < N; ++i) { - shm_queue_spsc_write_offset(&q, &off); + vms_queue_spsc_write_offset(&q, &off); assert(off == (unsigned)i); buff[off] = i; - shm_queue_spsc_write_finish(&q); + vms_queue_spsc_write_finish(&q); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); @@ -161,10 +161,10 @@ static void run_queue_spsc_push_pop_st(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); for (int i = 0; i < N; ++i) { - shm_queue_spsc_read_offset(&q, &off); + vms_queue_spsc_read_offset(&q, &off); assert(off == (unsigned)i); memcpy(&j, buff + i, sizeof(int)); - shm_queue_spsc_consume(&q, 1); + vms_queue_spsc_consume(&q, 1); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); @@ -186,18 +186,18 @@ struct thr_data { static int par_queue_push_pop_1_writer(void *arg) { struct thr_data *data = (struct thr_data *)arg; - shm_par_queue *q = (shm_par_queue *)data->ringbuffer; + vms_par_queue *q = (vms_par_queue *)data->ringbuffer; int i; struct timespec start, mid; int *addr; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (i = 0; i < N; ++i) { - while (!(addr = shm_par_queue_write_ptr(q))) { + while (!(addr = vms_par_queue_write_ptr(q))) { ++data->writer_waited; } *addr = i; - shm_par_queue_write_finish(q); + vms_par_queue_write_finish(q); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); @@ -207,8 +207,8 @@ static int par_queue_push_pop_1_writer(void *arg) { } static void run_par_queue_push_pop_1(void) { - shm_par_queue q; - shm_par_queue_init(&q, N, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, N, sizeof(int)); thrd_t tid; struct thr_data data = { @@ -222,7 +222,7 @@ static void run_par_queue_push_pop_1(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); while (n < N) { - if (shm_par_queue_pop(&q, &j) == false) { + if (vms_par_queue_pop(&q, &j) == false) { ++reader_waited; continue; } @@ -242,7 +242,7 @@ static void run_par_queue_push_pop_1(void) { static int queue_spsc_push_pop_1_writer(void *arg) { struct thr_data *data = (struct thr_data *)arg; - shm_queue_spsc *q = (shm_queue_spsc *)data->ringbuffer; + vms_queue_spsc *q = (vms_queue_spsc *)data->ringbuffer; int *buff = data->data_buffer; int i; @@ -250,11 +250,11 @@ static int queue_spsc_push_pop_1_writer(void *arg) { struct timespec start, mid; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start); for (i = 0; i < N; ++i) { - while (!shm_queue_spsc_write_offset(q, &off)) { + while (!vms_queue_spsc_write_offset(q, &off)) { ++data->writer_waited; } buff[off] = i; - shm_queue_spsc_write_finish(q); + vms_queue_spsc_write_finish(q); } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); @@ -264,8 +264,8 @@ static int queue_spsc_push_pop_1_writer(void *arg) { } static void run_queue_spsc_push_pop_1(void) { - shm_queue_spsc q; - shm_queue_spsc_init(&q, N); + vms_queue_spsc q; + vms_queue_spsc_init(&q, N); int *buff = malloc(sizeof(int) * N); assert(buff); @@ -282,12 +282,12 @@ static void run_queue_spsc_push_pop_1(void) { clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mid); while (n < N) { - if (shm_queue_spsc_read_offset(&q, &off) == 0) { + if (vms_queue_spsc_read_offset(&q, &off) == 0) { ++reader_waited; continue; } memcpy(&j, buff + n, sizeof(int)); - shm_queue_spsc_consume(&q, 1); + vms_queue_spsc_consume(&q, 1); ++n; } clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end); diff --git a/tests/buffer-test.c b/tests/buffer-test.c index 0bf2f8f3..96362f3d 100644 --- a/tests/buffer-test.c +++ b/tests/buffer-test.c @@ -1,64 +1,65 @@ #undef NDEBUG +#include "vamos-buffers/shmbuf/buffer.h" + #include #include -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/source.h" int main(void) { size_t i; - const size_t ctrl_size = sizeof(size_t) + sizeof(struct event_record); - struct source_control *ctrl = malloc(ctrl_size); + const size_t ctrl_size = sizeof(size_t) + sizeof(struct vms_event_record); + struct vms_source_control *ctrl = malloc(ctrl_size); ctrl->size = ctrl_size; ctrl->events[0].size = sizeof(size_t); ctrl->events[0].kind = 2; ctrl->events[0].name[0] = '\0'; ctrl->events[0].signature[0] = '\0'; - struct buffer *b = create_shared_buffer("/testkey", 128, ctrl); + vms_shm_buffer *b = vms_shm_buffer_create("/testkey", 128, ctrl); assert(b); - assert(buffer_elem_size(b) >= sizeof(size_t)); + assert(vms_shm_buffer_elem_size(b) >= sizeof(size_t)); free(ctrl); - assert(buffer_size(b) == 0); + assert(vms_shm_buffer_size(b) == 0); for (i = 1; i < 101; ++i) { - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); } - assert(buffer_size(b) == 100); + assert(vms_shm_buffer_size(b) == 100); size_t j; for (i = 1; i < 101; ++i) { - assert(buffer_pop(b, &j) == true); + assert(vms_shm_buffer_pop(b, &j) == true); assert(j == i); } - assert(buffer_size(b) == 0); + assert(vms_shm_buffer_size(b) == 0); // pop k for (i = 1; i < 101; ++i) { - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); } - assert(buffer_size(b) == 100); + assert(vms_shm_buffer_size(b) == 100); size_t num; for (i = 0; i < 100; ++i) { - assert(buffer_pop(b, &num) == true); + assert(vms_shm_buffer_pop(b, &num) == true); assert(num == i + 1); } // pop k with rotate - const size_t capacity = buffer_capacity(b); + const size_t capacity = vms_shm_buffer_capacity(b); for (i = 1; i < capacity + 1; ++i) { - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); } - assert(buffer_push(b, &i, sizeof(size_t)) == false); - assert(buffer_pop(b, &i) == true && i == 1); - assert(buffer_pop(b, &i) == true && i == 2); - assert(buffer_pop(b, &i) == true && i == 3); - assert(buffer_pop(b, &i) == true && i == 4); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == false); + assert(vms_shm_buffer_pop(b, &i) == true && i == 1); + assert(vms_shm_buffer_pop(b, &i) == true && i == 2); + assert(vms_shm_buffer_pop(b, &i) == true && i == 3); + assert(vms_shm_buffer_pop(b, &i) == true && i == 4); i = capacity + 1; - assert(buffer_push(b, &i, sizeof(size_t)) == true); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); i = capacity + 2; - assert(buffer_push(b, &i, sizeof(size_t)) == true); - // buffer contains 5, ... capacity + 1 - assert(buffer_size(b) == capacity - 2); - destroy_shared_buffer(b); + assert(vms_shm_buffer_push(b, &i, sizeof(size_t)) == true); + // vms_shm_buffer contains 5, ... capacity + 1 + assert(vms_shm_buffer_size(b) == capacity - 2); + vms_shm_buffer_destroy(b); } diff --git a/tests/fetch-test-2.c b/tests/fetch-test-2.c index 5909da95..a46ee96b 100644 --- a/tests/fetch-test-2.c +++ b/tests/fetch-test-2.c @@ -7,29 +7,28 @@ #include #include +#include "shmbuf/buffer-private.h" #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" - -#include "shmbuf/buffer-private.h" +#include "vamos-buffers/shmbuf/buffer.h" static int stream_ready = 1; static int main_finished = 0; -static bool is_ready(shm_stream* s) { +static bool is_ready(vms_stream* s) { (void)s; return !!stream_ready; } struct event { - shm_event base; + vms_event base; size_t n; }; static size_t failed_push = 0; void* filler_thread(void* data) { - struct buffer* buffer = (struct buffer*)data; + vms_shm_buffer* buffer = (vms_shm_buffer*)data; struct event ev; - ev.base.kind = shm_get_last_special_kind() + 1; + ev.base.kind = vms_event_get_last_special_kind() + 1; for (size_t i = 1; i <= 10000; ++i) { ev.base.id = i; ev.n = i; @@ -37,21 +36,21 @@ void* filler_thread(void* data) { printf("PUSH Event: {{%lu, %lu}, %lu}\n", ev.base.id, ev.base.kind, ev.n); */ - while (!buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; + while (!vms_shm_buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; } stream_ready = 0; pthread_exit(NULL); } void* reader_thread(void* data) { - shm_arbiter_buffer* arbiter_buffer_r = (shm_arbiter_buffer*)data; + vms_arbiter_buffer* arbiter_buffer_r = (vms_arbiter_buffer*)data; struct event* ev; size_t num; size_t next_id = 1; while (1) { - num = shm_arbiter_buffer_peek1(arbiter_buffer_r, (void**)&ev); + num = vms_arbiter_buffer_peek1(arbiter_buffer_r, (void**)&ev); if (num > 0) { - if (shm_event_is_hole((shm_event*)ev)) { + if (vms_event_is_hole((vms_event*)ev)) { next_id = 0; } else { assert(ev->n > 0); @@ -63,16 +62,16 @@ void* reader_thread(void* data) { if (ev->n != next_id) { fprintf(stderr, "%lu != %lu\n", ev->n, next_id); } - assert(next_id == shm_event_id((shm_event*)ev)); + assert(next_id == vms_event_id((vms_event*)ev)); assert(ev->n == next_id); - assert(ev->n == shm_event_id((shm_event*)ev)); + assert(ev->n == vms_event_id((vms_event*)ev)); ++next_id; } /* printf("Abuf Event: {{%lu, %lu}, %lu}\n", ev->base.id, ev->base.kind, ev->n); */ - assert(shm_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); + assert(vms_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); } if (num == 0 && stream_ready == 0 && main_finished == 1) break; @@ -82,25 +81,25 @@ void* reader_thread(void* data) { } int main(void) { - struct buffer* buffer = + vms_shm_buffer* buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); - shm_stream dummy_stream; - shm_stream* stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream* stream = &dummy_stream; - shm_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - /* into this buffer, stream_fetch() will push dropped() events if any */ - shm_arbiter_buffer* arbiter_buffer = - shm_arbiter_buffer_create(stream, sizeof(struct event), 10); + /* into this buffer, vms_stream_fetch() will push dropped() events if any */ + vms_arbiter_buffer* arbiter_buffer = + vms_arbiter_buffer_create(stream, sizeof(struct event), 10); /* into this buffer, we will push events here and read them */ - shm_arbiter_buffer* arbiter_buffer_r = - shm_arbiter_buffer_create(stream, sizeof(struct event), 3); + vms_arbiter_buffer* arbiter_buffer_r = + vms_arbiter_buffer_create(stream, sizeof(struct event), 3); - shm_arbiter_buffer_set_active(arbiter_buffer, 1); - shm_arbiter_buffer_set_active(arbiter_buffer_r, 1); + vms_arbiter_buffer_set_active(arbiter_buffer, 1); + vms_arbiter_buffer_set_active(arbiter_buffer_r, 1); pthread_t tid, tida; pthread_create(&tid, NULL, filler_thread, buffer); @@ -108,9 +107,9 @@ int main(void) { struct event* ev; for (size_t i = 1; i <= 10000; ++i) { - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); /* there should be no dropped event generated */ - assert(shm_arbiter_buffer_size(arbiter_buffer) == 0); + assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); assert(ev); assert(ev->n == i); @@ -118,11 +117,11 @@ int main(void) { // printf("POP Event: {{%lu, %lu}, %lu}\n", ev->base.id, ev->base.kind, // ev->n); - shm_arbiter_buffer_push(arbiter_buffer_r, ev, sizeof(struct event)); - shm_stream_consume(stream, 1); + vms_arbiter_buffer_push(arbiter_buffer_r, ev, sizeof(struct event)); + vms_stream_consume(stream, 1); } - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(!ev); main_finished = 1; diff --git a/tests/fetch-test-3.c b/tests/fetch-test-3.c index 244de255..ac50c794 100644 --- a/tests/fetch-test-3.c +++ b/tests/fetch-test-3.c @@ -7,61 +7,60 @@ #include #include +#include "shmbuf/buffer-private.h" #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" - -#include "shmbuf/buffer-private.h" +#include "vamos-buffers/shmbuf/buffer.h" static int stream_ready = 1; static int main_finished = 0; -static bool is_ready(shm_stream *s) { +static bool is_ready(vms_stream *s) { (void)s; return !!stream_ready; } struct event { - shm_event base; + vms_event base; size_t n; }; static size_t failed_push = 0; void *filler_thread(void *data) { - struct buffer *buffer = (struct buffer *)data; + vms_shm_buffer *buffer = (vms_shm_buffer *)data; struct event ev; - ev.base.kind = shm_get_last_special_kind() + 1; + ev.base.kind = vms_event_get_last_special_kind() + 1; for (size_t i = 0; i < 10000; ++i) { ev.base.id = i + 1; ev.n = i; // printf("PUSH Event: {{%lu, %lu}, %lu}\n", ev.base.id, ev.base.kind, // ev.n); - while (!buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; + while (!vms_shm_buffer_push(buffer, &ev, sizeof(ev))) ++failed_push; } stream_ready = 0; pthread_exit(NULL); } void *reader_thread(void *data) { - shm_arbiter_buffer *arbiter_buffer_r = (shm_arbiter_buffer *)data; + vms_arbiter_buffer *arbiter_buffer_r = (vms_arbiter_buffer *)data; struct event *ev; size_t num; size_t last_id = 0; size_t total = 0; while (1) { - num = shm_arbiter_buffer_peek1(arbiter_buffer_r, (void **)&ev); + num = vms_arbiter_buffer_peek1(arbiter_buffer_r, (void **)&ev); if (num > 0) { - assert(!shm_event_is_hole((shm_event *)ev)); + assert(!vms_event_is_hole((vms_event *)ev)); assert(ev->n == last_id); - assert(++last_id == shm_event_id((shm_event *)ev)); + assert(++last_id == vms_event_id((vms_event *)ev)); ++total; /* printf("Abuf Event: {{%lu, %lu}, %lu}\n", ev->base.id, ev->base.kind, ev->n); */ - assert(shm_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); + assert(vms_arbiter_buffer_drop(arbiter_buffer_r, 1) == 1); } if (stream_ready == 0 && main_finished == 1 && - shm_arbiter_buffer_size(arbiter_buffer_r) == 0) + vms_arbiter_buffer_size(arbiter_buffer_r) == 0) break; } @@ -73,25 +72,25 @@ void *reader_thread(void *data) { static size_t waiting_for_arbbuf = 0; int main(void) { - struct buffer *buffer = + vms_shm_buffer *buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); - shm_stream dummy_stream; - shm_stream *stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream *stream = &dummy_stream; - shm_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - /* into this buffer, stream_fetch() will push dropped() events if any */ - shm_arbiter_buffer *arbiter_buffer = - shm_arbiter_buffer_create(stream, sizeof(struct event), 10); + /* into this buffer, vms_stream_fetch() will push dropped() events if any */ + vms_arbiter_buffer *arbiter_buffer = + vms_arbiter_buffer_create(stream, sizeof(struct event), 10); /* into this buffer, we will push events here and read them */ - shm_arbiter_buffer *arbiter_buffer_r = - shm_arbiter_buffer_create(stream, sizeof(struct event), 3); + vms_arbiter_buffer *arbiter_buffer_r = + vms_arbiter_buffer_create(stream, sizeof(struct event), 3); - shm_arbiter_buffer_set_active(arbiter_buffer, 1); - shm_arbiter_buffer_set_active(arbiter_buffer_r, 1); + vms_arbiter_buffer_set_active(arbiter_buffer, 1); + vms_arbiter_buffer_set_active(arbiter_buffer_r, 1); pthread_t tid, tida; pthread_create(&tid, NULL, filler_thread, buffer); @@ -100,10 +99,10 @@ int main(void) { struct event *ev; struct event *out; for (size_t i = 0; i < 10000; ++i) { - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); /* there should be no dropped event generated * since we are outputing the events into another buffer */ - assert(shm_arbiter_buffer_size(arbiter_buffer) == 0); + assert(vms_arbiter_buffer_size(arbiter_buffer) == 0); assert(ev); assert(ev->n == i); @@ -112,17 +111,17 @@ int main(void) { // ev->n); /* this function also automatically drops events */ - while (!(out = shm_arbiter_buffer_write_ptr(arbiter_buffer_r))) { + while (!(out = vms_arbiter_buffer_write_ptr(arbiter_buffer_r))) { ++waiting_for_arbbuf; } *out = *ev; - shm_arbiter_buffer_write_finish(arbiter_buffer_r); + vms_arbiter_buffer_write_finish(arbiter_buffer_r); - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(!ev); main_finished = 1; diff --git a/tests/fetch-test.c b/tests/fetch-test.c index 972729b4..bfb2cc47 100644 --- a/tests/fetch-test.c +++ b/tests/fetch-test.c @@ -7,66 +7,65 @@ #include #include +#include "shmbuf/buffer-private.h" #include "vamos-buffers/core/arbiter.h" -#include "vamos-buffers/shmbuf/buffer.h" #include "vamos-buffers/core/stream.h" - -#include "shmbuf/buffer-private.h" +#include "vamos-buffers/shmbuf/buffer.h" static int stream_ready = 1; -static bool is_ready(shm_stream *s) { +static bool is_ready(vms_stream *s) { (void)s; return !!stream_ready; } struct event { - shm_event base; + vms_event base; int n; }; void *filler_thread(void *data) { - struct buffer *buffer = (struct buffer *)data; + vms_shm_buffer *buffer = (vms_shm_buffer *)data; struct event ev; - ev.base.kind = shm_get_last_special_kind() + 1; + ev.base.kind = vms_event_get_last_special_kind() + 1; for (int i = 0; i < 4; ++i) { ev.base.id = i + 1; ev.n = i; // printf("PUSH Event: {{%lu, %lu}, %d}\n", ev.base.id, ev.base.kind, // ev.n); - assert(buffer_push(buffer, &ev, sizeof(ev)) == true); + assert(vms_shm_buffer_push(buffer, &ev, sizeof(ev)) == true); } stream_ready = 0; pthread_exit(NULL); } int main(void) { - struct buffer *buffer = + vms_shm_buffer *buffer = initialize_local_buffer("/dummy", sizeof(struct event), 30, NULL); assert(buffer); - shm_stream dummy_stream; - shm_stream *stream = &dummy_stream; + vms_stream dummy_stream; + vms_stream *stream = &dummy_stream; - shm_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, + vms_stream_init(stream, buffer, sizeof(struct event), is_ready, NULL, NULL, NULL, NULL, "dummy-stream", "dummy"); - shm_arbiter_buffer *arbiter_buffer = - shm_arbiter_buffer_create(stream, sizeof(int), 3); - shm_arbiter_buffer_set_active(arbiter_buffer, 1); + vms_arbiter_buffer *arbiter_buffer = + vms_arbiter_buffer_create(stream, sizeof(int), 3); + vms_arbiter_buffer_set_active(arbiter_buffer, 1); pthread_t tid; pthread_create(&tid, NULL, filler_thread, buffer); struct event *ev; for (int i = 0; i < 4; ++i) { - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(ev); assert(ev->n == i); // printf("POP Event: {{%lu, %lu}, %d}\n", ev->base.id, ev->base.kind, // ev->n); - shm_stream_consume(stream, 1); + vms_stream_consume(stream, 1); } - ev = stream_fetch(stream, arbiter_buffer); + ev = vms_stream_fetch_dropping(stream, arbiter_buffer); assert(!ev); pthread_join(tid, NULL); diff --git a/tests/list-test.c b/tests/list-test.c index e36a9482..9615fc1c 100644 --- a/tests/list-test.c +++ b/tests/list-test.c @@ -5,25 +5,25 @@ int main(void) { int num[] = {0, 1, 2, 3}; - shm_list l; - shm_list_init(&l); - assert(shm_list_size(&l) == 0); - assert(shm_list_append(&l, num + 0) == 1); - assert(shm_list_size(&l) == 1); - assert(shm_list_remove(&l, l.first) == 0); - assert(shm_list_size(&l) == 0); - assert(shm_list_prepend(&l, num + 0) == 1); - assert(shm_list_size(&l) == 1); - assert(shm_list_remove(&l, l.last) == 0); - assert(shm_list_size(&l) == 0); + vms_list l; + vms_list_init(&l); + assert(vms_list_size(&l) == 0); + assert(vms_list_append(&l, num + 0) == 1); + assert(vms_list_size(&l) == 1); + assert(vms_list_remove(&l, l.first) == 0); + assert(vms_list_size(&l) == 0); + assert(vms_list_prepend(&l, num + 0) == 1); + assert(vms_list_size(&l) == 1); + assert(vms_list_remove(&l, l.last) == 0); + assert(vms_list_size(&l) == 0); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); + assert(vms_list_append(&l, num + i) == i + 1); - shm_list_elem *cur = l.first; + vms_list_elem *cur = l.first; int i = 0; int sum = 0; - shm_list_elem *x = NULL; + vms_list_elem *x = NULL; while (cur) { assert(cur->data == num + i); if (i == 2) @@ -34,7 +34,7 @@ int main(void) { } assert(sum == 6); - assert(shm_list_remove(&l, x) == 3); + assert(vms_list_remove(&l, x) == 3); cur = l.first; while (cur) { sum += *((int *)cur->data); @@ -42,24 +42,24 @@ int main(void) { } assert(sum == 10); // TODO: we leak it, destroy assumes that the list is dynamically allocated - // shm_list_destroy(&l, NULL); + // vms_list_destroy(&l, NULL); - shm_list_init(&l); + vms_list_init(&l); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); + assert(vms_list_append(&l, num + i) == i + 1); for (size_t i = 0; i < 4; ++i) - assert(shm_list_remove(&l, shm_list_last(&l)) == 3 - i); - assert(shm_list_size(&l) == 0); + assert(vms_list_remove(&l, vms_list_last(&l)) == 3 - i); + assert(vms_list_size(&l) == 0); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); + assert(vms_list_append(&l, num + i) == i + 1); for (size_t i = 0; i < 4; ++i) - assert(shm_list_remove(&l, shm_list_first(&l)) == 3 - i); - assert(shm_list_size(&l) == 0); + assert(vms_list_remove(&l, vms_list_first(&l)) == 3 - i); + assert(vms_list_size(&l) == 0); for (size_t i = 0; i < 4; ++i) - assert(shm_list_append(&l, num + i) == i + 1); - assert(shm_list_remove(&l, shm_list_first(&l)->next->next) == 3); - assert(shm_list_remove(&l, shm_list_first(&l)->next->next) == 2); - assert(shm_list_last(&l) == shm_list_first(&l)->next); - assert(shm_list_remove(&l, shm_list_first(&l)->next) == 1); - assert(shm_list_last(&l) == shm_list_first(&l)); + assert(vms_list_append(&l, num + i) == i + 1); + assert(vms_list_remove(&l, vms_list_first(&l)->next->next) == 3); + assert(vms_list_remove(&l, vms_list_first(&l)->next->next) == 2); + assert(vms_list_last(&l) == vms_list_first(&l)->next); + assert(vms_list_remove(&l, vms_list_first(&l)->next) == 1); + assert(vms_list_last(&l) == vms_list_first(&l)); } diff --git a/tests/queue-par-cbmc.c b/tests/queue-par-cbmc.c index 6b80a5aa..d36fd334 100644 --- a/tests/queue-par-cbmc.c +++ b/tests/queue-par-cbmc.c @@ -7,10 +7,10 @@ int buffer[4]; void *reader(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 1; while (n < 4) { - if (shm_par_queue_pop(q, &buffer[n])) { + if (vms_par_queue_pop(q, &buffer[n])) { ++n; } } @@ -18,17 +18,17 @@ void *reader(void *data) { } void *writer(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; for (int i = 1; i < 4; ++i) { - assert(shm_par_queue_push(q, &i, sizeof(i))); + assert(vms_par_queue_push(q, &i, sizeof(i))); } pthread_exit(0); } int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, 3, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, 3, sizeof(int)); pthread_t r, w; pthread_create(&r, NULL, reader, &q); @@ -41,5 +41,5 @@ int main(void) { } // CBMC does not handle calls to free() with threads... - // shm_par_queue_destroy(&q); + // vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-cbmc_2.c b/tests/queue-par-cbmc_2.c index c43b6066..4c1e781c 100644 --- a/tests/queue-par-cbmc_2.c +++ b/tests/queue-par-cbmc_2.c @@ -7,10 +7,10 @@ int buffer[4]; void *reader(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 0; while (n < 3) { - if (shm_par_queue_pop(q, &buffer[n + 1])) { + if (vms_par_queue_pop(q, &buffer[n + 1])) { ++n; } } @@ -19,10 +19,10 @@ void *reader(void *data) { void *writer(void *data) { int num[] = {0, 1, 2, 3}; - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 0; while (n < 3) { - if (shm_par_queue_push(q, &num[n + 1], sizeof(int))) { + if (vms_par_queue_push(q, &num[n + 1], sizeof(int))) { ++n; } } @@ -31,8 +31,8 @@ void *writer(void *data) { } int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, 1, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, 1, sizeof(int)); pthread_t r, w; pthread_create(&r, NULL, reader, &q); @@ -45,5 +45,5 @@ int main(void) { } // CBMC does not handle calls to free() with threads... - // shm_par_queue_destroy(&q); + // vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-cbmc_3.c b/tests/queue-par-cbmc_3.c index 7f7fc5d0..a174c456 100644 --- a/tests/queue-par-cbmc_3.c +++ b/tests/queue-par-cbmc_3.c @@ -6,38 +6,38 @@ extern bool nondet(void); #define CAPACITY 2 int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, CAPACITY, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, CAPACITY, sizeof(int)); size_t elem_num = 0; int x = 0xbee; while (1) { if (nondet()) { /* POP */ - bool ret = shm_par_queue_pop(&q, &x); + bool ret = vms_par_queue_pop(&q, &x); if (ret) { assert(elem_num > 0); --elem_num; - assert(shm_par_queue_size(&q) < CAPACITY); + assert(vms_par_queue_size(&q) < CAPACITY); assert(x == 0xbee); } else { assert(elem_num == 0); - assert(shm_par_queue_size(&q) == 0); + assert(vms_par_queue_size(&q) == 0); } } else { /* PUSH */ - if (shm_par_queue_push(&q, &x, sizeof(int))) { + if (vms_par_queue_push(&q, &x, sizeof(int))) { ++elem_num; assert(elem_num <= CAPACITY); - assert(shm_par_queue_size(&q) <= CAPACITY); + assert(vms_par_queue_size(&q) <= CAPACITY); } else { assert(elem_num == CAPACITY); - assert(shm_par_queue_size(&q) == CAPACITY); - assert(shm_par_queue_size(&q) == shm_par_queue_capacity(&q)); + assert(vms_par_queue_size(&q) == CAPACITY); + assert(vms_par_queue_size(&q) == vms_par_queue_capacity(&q)); } - assert(shm_par_queue_size(&q) > 0); + assert(vms_par_queue_size(&q) > 0); } } - shm_par_queue_destroy(&q); + vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-test-2.c b/tests/queue-par-test-2.c index 06125b59..e32cce5d 100644 --- a/tests/queue-par-test-2.c +++ b/tests/queue-par-test-2.c @@ -8,123 +8,123 @@ int main(void) { size_t i; int *p; int num[] = {0, 1, 2, 3, 4, 5, 6}; - shm_par_queue q; - shm_par_queue_init(&q, 3, sizeof(int)); - assert(shm_par_queue_size(&q) == 0); - assert(shm_par_queue_free_num(&q) == 3); - assert(shm_par_queue_peek_at(&q, 2) == NULL); + vms_par_queue q; + vms_par_queue_init(&q, 3, sizeof(int)); + assert(vms_par_queue_size(&q) == 0); + assert(vms_par_queue_free_num(&q) == 3); + assert(vms_par_queue_peek_at(&q, 2) == NULL); i = 2; - assert(shm_par_queue_peek_atmost_at(&q, &i) == NULL); + assert(vms_par_queue_peek_atmost_at(&q, &i) == NULL); assert(i == 0); - assert(shm_par_queue_push(&q, num + 1, sizeof(int)) == true); - assert(shm_par_queue_free_num(&q) == 2); - assert(shm_par_queue_size(&q) == 1); - assert(shm_par_queue_peek_at(&q, 2) == NULL); + assert(vms_par_queue_push(&q, num + 1, sizeof(int)) == true); + assert(vms_par_queue_free_num(&q) == 2); + assert(vms_par_queue_size(&q) == 1); + assert(vms_par_queue_peek_at(&q, 2) == NULL); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 1); - assert(shm_par_queue_push(&q, num + 2, sizeof(int)) == true); - assert(shm_par_queue_free_num(&q) == 1); - assert(shm_par_queue_size(&q) == 2); - assert(shm_par_queue_peek_at(&q, 2) == NULL); + assert(vms_par_queue_push(&q, num + 2, sizeof(int)) == true); + assert(vms_par_queue_free_num(&q) == 1); + assert(vms_par_queue_size(&q) == 2); + assert(vms_par_queue_peek_at(&q, 2) == NULL); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 1); assert(*p == 2); - assert(shm_par_queue_push(&q, num + 3, sizeof(int)) == true); - assert(shm_par_queue_free_num(&q) == 0); - assert(shm_par_queue_size(&q) == 3); - assert((p = (int *)shm_par_queue_peek_at(&q, 2)) != NULL); + assert(vms_par_queue_push(&q, num + 3, sizeof(int)) == true); + assert(vms_par_queue_free_num(&q) == 0); + assert(vms_par_queue_size(&q) == 3); + assert((p = (int *)vms_par_queue_peek_at(&q, 2)) != NULL); assert(*p == 3); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 2); assert(*p == 3); i = 5; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 2); assert(*p == 3); i = 0; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 1); - assert((p = (int *)shm_par_queue_peek_at(&q, 0)) != NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 0)) != NULL); assert(*p == 1); i = 1; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 1); assert(*p == 2); - assert((p = (int *)shm_par_queue_peek_at(&q, 1)) != NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 1)) != NULL); assert(*p == 2); - assert(shm_par_queue_push(&q, num + 4, sizeof(int)) == false); - assert(shm_par_queue_free_num(&q) == 0); - assert(shm_par_queue_size(&q) == 3); - for (i = 0; i < shm_par_queue_size(&q); ++i) { + assert(vms_par_queue_push(&q, num + 4, sizeof(int)) == false); + assert(vms_par_queue_free_num(&q) == 0); + assert(vms_par_queue_size(&q) == 3); + for (i = 0; i < vms_par_queue_size(&q); ++i) { size_t j = i; - p = (int *)shm_par_queue_peek_at(&q, i); + p = (int *)vms_par_queue_peek_at(&q, i); assert(*p == (int)i + 1); - p = (int *)shm_par_queue_peek_atmost_at(&q, &j); + p = (int *)vms_par_queue_peek_atmost_at(&q, &j); assert(j == i); assert(*p == (int)i + 1); } - assert(shm_par_queue_pop(&q, &i) == true && i == 1); - assert(shm_par_queue_size(&q) == 2); - assert(shm_par_queue_free_num(&q) == 1); - assert(shm_par_queue_pop(&q, &i) == true && i == 2); - assert(shm_par_queue_size(&q) == 1); - assert(shm_par_queue_free_num(&q) == 2); + assert(vms_par_queue_pop(&q, &i) == true && i == 1); + assert(vms_par_queue_size(&q) == 2); + assert(vms_par_queue_free_num(&q) == 1); + assert(vms_par_queue_pop(&q, &i) == true && i == 2); + assert(vms_par_queue_size(&q) == 1); + assert(vms_par_queue_free_num(&q) == 2); - assert((p = (int *)shm_par_queue_peek_at(&q, 1)) == NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 1)) == NULL); i = 1; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 3); - assert((p = (int *)shm_par_queue_peek_at(&q, 0)) != NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 0)) != NULL); assert(*p == 3); i = 0; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 3); i = 2; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) != NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) != NULL); assert(i == 0); assert(*p == 3); - assert(shm_par_queue_pop(&q, &i) == true && i == 3); - assert(shm_par_queue_free_num(&q) == 3); - assert(shm_par_queue_size(&q) == 0); - assert((p = (int *)shm_par_queue_peek_at(&q, 1)) == NULL); + assert(vms_par_queue_pop(&q, &i) == true && i == 3); + assert(vms_par_queue_free_num(&q) == 3); + assert(vms_par_queue_size(&q) == 0); + assert((p = (int *)vms_par_queue_peek_at(&q, 1)) == NULL); i = 1; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) == NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) == NULL); assert(i == 0); - assert((p = (int *)shm_par_queue_peek_at(&q, 0)) == NULL); + assert((p = (int *)vms_par_queue_peek_at(&q, 0)) == NULL); i = 0; - assert((p = (int *)shm_par_queue_peek_atmost_at(&q, &i)) == NULL); + assert((p = (int *)vms_par_queue_peek_atmost_at(&q, &i)) == NULL); assert(i == 0); - assert(shm_par_queue_push(&q, num + 0, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 1, sizeof(int)) == true); - assert(shm_par_queue_pop(&q, &i) == true && i == 0); - assert(shm_par_queue_push(&q, num + 2, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 3, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 4, sizeof(int)) == false); - assert(shm_par_queue_pop(&q, &i) == true && i == 1); - assert(shm_par_queue_pop(&q, &i) == true && i == 2); - assert(shm_par_queue_pop(&q, &i) == true && i == 3); - assert(shm_par_queue_size(&q) == 0); - assert(shm_par_queue_push(&q, num + 2, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 3, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 4, sizeof(int)) == true); - assert(shm_par_queue_push(&q, num + 5, sizeof(int)) == false); + assert(vms_par_queue_push(&q, num + 0, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 1, sizeof(int)) == true); + assert(vms_par_queue_pop(&q, &i) == true && i == 0); + assert(vms_par_queue_push(&q, num + 2, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 3, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 4, sizeof(int)) == false); + assert(vms_par_queue_pop(&q, &i) == true && i == 1); + assert(vms_par_queue_pop(&q, &i) == true && i == 2); + assert(vms_par_queue_pop(&q, &i) == true && i == 3); + assert(vms_par_queue_size(&q) == 0); + assert(vms_par_queue_push(&q, num + 2, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 3, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 4, sizeof(int)) == true); + assert(vms_par_queue_push(&q, num + 5, sizeof(int)) == false); int *d1, *d2; size_t s1, s2; - size_t n = shm_par_queue_peek(&q, 5, (void **)&d1, &s1, (void **)&d2, &s2); + size_t n = vms_par_queue_peek(&q, 5, (void **)&d1, &s1, (void **)&d2, &s2); assert(n == 3); assert(s1 + s2 == 3); size_t si = 0; @@ -138,25 +138,25 @@ int main(void) { ++si; } - assert(*(int *)shm_par_queue_peek_at(&q, 0) == 2); - assert(*(int *)shm_par_queue_peek_at(&q, 1) == 3); - assert(*(int *)shm_par_queue_peek_at(&q, 2) == 4); + assert(*(int *)vms_par_queue_peek_at(&q, 0) == 2); + assert(*(int *)vms_par_queue_peek_at(&q, 1) == 3); + assert(*(int *)vms_par_queue_peek_at(&q, 2) == 4); size_t k = 0; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 2 && k == 0); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 2 && k == 0); k = 1; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 3 && k == 1); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 3 && k == 1); k = 2; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 3; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 4; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 4444; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = 111111; - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); k = ~(0LL); - assert(*(int *)shm_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); + assert(*(int *)vms_par_queue_peek_atmost_at(&q, &k) == 4 && k == 2); - shm_par_queue_destroy(&q); + vms_par_queue_destroy(&q); } diff --git a/tests/queue-par-test.c b/tests/queue-par-test.c index 2dab2913..4ac2377b 100644 --- a/tests/queue-par-test.c +++ b/tests/queue-par-test.c @@ -6,10 +6,10 @@ int buffer[4]; int reader(void *data) { - shm_par_queue *q = (shm_par_queue *)data; + vms_par_queue *q = (vms_par_queue *)data; int n = 0; while (n < 3) { - if (shm_par_queue_pop(q, &buffer[n + 1])) { + if (vms_par_queue_pop(q, &buffer[n + 1])) { ++n; } } @@ -18,15 +18,15 @@ int reader(void *data) { int writer(void *data) { int num[] = {0, 1, 2, 3}; - shm_par_queue *q = (shm_par_queue *)data; - for (int i = 1; i < 4; ++i) shm_par_queue_push(q, &num[i], sizeof(int)); + vms_par_queue *q = (vms_par_queue *)data; + for (int i = 1; i < 4; ++i) vms_par_queue_push(q, &num[i], sizeof(int)); thrd_exit(0); } int main(void) { - shm_par_queue q; - shm_par_queue_init(&q, 3, sizeof(int)); + vms_par_queue q; + vms_par_queue_init(&q, 3, sizeof(int)); thrd_t r, w; thrd_create(&r, reader, &q); @@ -38,5 +38,5 @@ int main(void) { assert(buffer[i] == i); } - shm_par_queue_destroy(&q); + vms_par_queue_destroy(&q); } diff --git a/tests/spsc-ringbuf-1.c b/tests/spsc-ringbuf-1.c index 9e830d6f..1539d67f 100644 --- a/tests/spsc-ringbuf-1.c +++ b/tests/spsc-ringbuf-1.c @@ -6,143 +6,143 @@ #define CAPACITY 3 int main(void) { - shm_spsc_ringbuf r; + vms_spsc_ringbuf r; size_t n, off, len1, len2; - shm_spsc_ringbuf_init(&r, CAPACITY); - assert(shm_spsc_ringbuf_capacity(&r) == CAPACITY); - assert(shm_spsc_ringbuf_max_size(&r) == CAPACITY - 1); + vms_spsc_ringbuf_init(&r, CAPACITY); + assert(vms_spsc_ringbuf_capacity(&r) == CAPACITY); + assert(vms_spsc_ringbuf_max_size(&r) == CAPACITY - 1); /* ringbuf is empty */ - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_read_off_nowrap(&r, &n); + vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 1) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 0) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 1) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 0) == 0); /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r)); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r)); assert(off == 0); - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); /* ringbuf has 1 elem */ - assert(shm_spsc_ringbuf_size(&r) == 1); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 1); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_size(&r) == 1); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 1); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - off = shm_spsc_ringbuf_read_off_nowrap(&r, &n); + off = vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 1); assert(off == 0); - shm_spsc_ringbuf_consume_upto(&r, 1); + vms_spsc_ringbuf_consume_upto(&r, 1); /* ringbuf is empty */ - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_read_off_nowrap(&r, &n); + vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 1) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 0) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 1) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 0) == 0); /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r)); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r) - 1); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r) - 1); assert(off < CAPACITY); - assert(shm_spsc_ringbuf_size(&r) == 1); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 1); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_size(&r) == 1); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 1); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); - assert(shm_spsc_ringbuf_size(&r) == 2); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 2); - assert(shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_size(&r) == 2); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 2); + assert(vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); assert(len1 == 2); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); assert(len1 == 2); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); /* write 1 elem -- should fail */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r) - 2); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r) - 2); assert(n == 0); /* consume 1 */ - shm_spsc_ringbuf_consume(&r, 1); + vms_spsc_ringbuf_consume(&r, 1); /* write 1 to wrap around */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(n == shm_spsc_ringbuf_max_size(&r) - 1); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(n == vms_spsc_ringbuf_max_size(&r) - 1); assert(n == 1); assert(off < CAPACITY); - assert(shm_spsc_ringbuf_size(&r) == 1); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 1); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_size(&r) == 1); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 1); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 1); assert(len1 == 1); assert(len2 == 0); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 1); - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); - assert(shm_spsc_ringbuf_size(&r) == 2); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r) - 2); - assert(shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_size(&r) == 2); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r) - 2); + assert(vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == 2); assert(len1 == 1); assert(len2 == 1); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, CAPACITY + 2, &off, &len1, &len2) == 2); assert(len1 == 1); assert(len2 == 1); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 2); } diff --git a/tests/spsc-ringbuf-cbmc-1.c b/tests/spsc-ringbuf-cbmc-1.c index 5e1e7f8a..21c874be 100644 --- a/tests/spsc-ringbuf-cbmc-1.c +++ b/tests/spsc-ringbuf-cbmc-1.c @@ -6,94 +6,94 @@ extern int nondet(void); #define CAPACITY 3 int main(void) { - shm_spsc_ringbuf r; + vms_spsc_ringbuf r; size_t n, off, len1, len2; - shm_spsc_ringbuf_init(&r, CAPACITY); - assert(shm_spsc_ringbuf_capacity(&r) == CAPACITY); - assert(shm_spsc_ringbuf_max_size(&r) == CAPACITY - 1); + vms_spsc_ringbuf_init(&r, CAPACITY); + assert(vms_spsc_ringbuf_capacity(&r) == CAPACITY); + assert(vms_spsc_ringbuf_max_size(&r) == CAPACITY - 1); /* ringbuf is empty */ - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == shm_spsc_ringbuf_max_size(&r)); - assert(!shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == vms_spsc_ringbuf_max_size(&r)); + assert(!vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == 0); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == 0); - shm_spsc_ringbuf_read_off_nowrap(&r, &n); + vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(n == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 1) == 0); - assert(shm_spsc_ringbuf_consume_upto(&r, 0) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, CAPACITY) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 1) == 0); + assert(vms_spsc_ringbuf_consume_upto(&r, 0) == 0); size_t elem_num = 0; while (1) { - assert(shm_spsc_ringbuf_size(&r) == elem_num); + assert(vms_spsc_ringbuf_size(&r) == elem_num); if (nondet()) { /* CONSUME */ - assert(shm_spsc_ringbuf_free_num(&r) == - shm_spsc_ringbuf_max_size(&r) - elem_num); - assert(!shm_spsc_ringbuf_full(&r) || - shm_spsc_ringbuf_free_num(&r) == 0); - assert(shm_spsc_ringbuf_full(&r) || - shm_spsc_ringbuf_free_num(&r) > 0); + assert(vms_spsc_ringbuf_free_num(&r) == + vms_spsc_ringbuf_max_size(&r) - elem_num); + assert(!vms_spsc_ringbuf_full(&r) || + vms_spsc_ringbuf_free_num(&r) == 0); + assert(vms_spsc_ringbuf_full(&r) || + vms_spsc_ringbuf_free_num(&r) > 0); - assert(shm_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, CAPACITY, &off, &len1, &len2) == elem_num); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == elem_num); - off = shm_spsc_ringbuf_read_off_nowrap(&r, &n); + off = vms_spsc_ringbuf_read_off_nowrap(&r, &n); assert(elem_num == 0 || n > 0); - assert(shm_spsc_ringbuf_size(&r) == 0 || n > 0); - assert(n == 0 || shm_spsc_ringbuf_size(&r) > 0); + assert(vms_spsc_ringbuf_size(&r) == 0 || n > 0); + assert(n == 0 || vms_spsc_ringbuf_size(&r) > 0); if (n > 0) { assert(elem_num > 0); --elem_num; - assert(shm_spsc_ringbuf_size(&r) > 0); - assert(shm_spsc_ringbuf_size(&r) <= - shm_spsc_ringbuf_max_size(&r)); - shm_spsc_ringbuf_consume(&r, 1); - assert(shm_spsc_ringbuf_size(&r) < - shm_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_size(&r) > 0); + assert(vms_spsc_ringbuf_size(&r) <= + vms_spsc_ringbuf_max_size(&r)); + vms_spsc_ringbuf_consume(&r, 1); + assert(vms_spsc_ringbuf_size(&r) < + vms_spsc_ringbuf_max_size(&r)); } else { assert(elem_num == 0); - assert(shm_spsc_ringbuf_size(&r) == 0); - assert(shm_spsc_ringbuf_free_num(&r) == - shm_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_size(&r) == 0); + assert(vms_spsc_ringbuf_free_num(&r) == + vms_spsc_ringbuf_max_size(&r)); } } else { /* write 1 elem */ - off = shm_spsc_ringbuf_write_off_nowrap(&r, &n); - assert(shm_spsc_ringbuf_free_num(&r) <= - shm_spsc_ringbuf_max_size(&r)); + off = vms_spsc_ringbuf_write_off_nowrap(&r, &n); + assert(vms_spsc_ringbuf_free_num(&r) <= + vms_spsc_ringbuf_max_size(&r)); /* PUSH */ if (n > 0) { - assert(shm_spsc_ringbuf_size(&r) == elem_num); - assert(!shm_spsc_ringbuf_full(&r)); - assert(elem_num <= shm_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_size(&r) == elem_num); + assert(!vms_spsc_ringbuf_full(&r)); + assert(elem_num <= vms_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == elem_num); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == elem_num); ++elem_num; - shm_spsc_ringbuf_write_finish(&r, 1); + vms_spsc_ringbuf_write_finish(&r, 1); - assert(elem_num <= shm_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == + assert(elem_num <= vms_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_peek(&r, 3, &off, &len1, &len2) == elem_num); - assert(shm_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == + assert(vms_spsc_ringbuf_peek(&r, 0, &off, &len1, &len2) == elem_num); } else { - assert(elem_num == shm_spsc_ringbuf_max_size(&r)); - assert(shm_spsc_ringbuf_full(&r)); - assert(shm_spsc_ringbuf_size(&r) == - shm_spsc_ringbuf_max_size(&r)); + assert(elem_num == vms_spsc_ringbuf_max_size(&r)); + assert(vms_spsc_ringbuf_full(&r)); + assert(vms_spsc_ringbuf_size(&r) == + vms_spsc_ringbuf_max_size(&r)); } - assert(shm_spsc_ringbuf_size(&r) > 0); + assert(vms_spsc_ringbuf_size(&r) > 0); } } }