| 1 | #ifndef _TRACEFILE_ARRAY_H |
| 2 | #define _TRACEFILE_ARRAY_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0-only |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <inttypes.h> |
| 12 | #include <limits.h> |
| 13 | #include <pthread.h> |
| 14 | #include <stdbool.h> |
| 15 | |
| 16 | struct tracefile { |
| 17 | /* Per-tracefile head/tail seq. */ |
| 18 | uint64_t seq_head; /* Newest seqcount. Inclusive. */ |
| 19 | uint64_t seq_tail; /* Oldest seqcount. Inclusive. */ |
| 20 | }; |
| 21 | |
| 22 | enum tracefile_rotate_type { |
| 23 | TRACEFILE_ROTATE_READ, |
| 24 | TRACEFILE_ROTATE_WRITE, |
| 25 | }; |
| 26 | |
| 27 | /* |
| 28 | * Represents an array of trace files in a stream. |
| 29 | * head is the most recent file/trace packet. |
| 30 | * tail is the oldest file/trace packet. |
| 31 | * |
| 32 | * There are two heads: a "read" head and a "write" head. The "write" head is |
| 33 | * the position of the newest data file. The "read" head position is only moved |
| 34 | * forward when the index is received. |
| 35 | * |
| 36 | * The viewer uses the "read" head position as upper bound, which |
| 37 | * ensures it never attempts to open a non-existing index file. |
| 38 | */ |
| 39 | struct tracefile_array { |
| 40 | struct tracefile *tf; |
| 41 | size_t count; |
| 42 | |
| 43 | /* Current head/tail files. */ |
| 44 | uint64_t file_head_read; |
| 45 | uint64_t file_head_write; |
| 46 | uint64_t file_tail; |
| 47 | |
| 48 | /* Overall head/tail seq for the entire array. Inclusive. */ |
| 49 | uint64_t seq_head; |
| 50 | uint64_t seq_tail; |
| 51 | }; |
| 52 | |
| 53 | struct tracefile_array *tracefile_array_create(size_t count); |
| 54 | void tracefile_array_destroy(struct tracefile_array *tfa); |
| 55 | |
| 56 | void tracefile_array_file_rotate(struct tracefile_array *tfa, enum tracefile_rotate_type type); |
| 57 | void tracefile_array_commit_seq(struct tracefile_array *tfa, uint64_t new_seq_head); |
| 58 | void tracefile_array_reset(struct tracefile_array *tfa); |
| 59 | |
| 60 | uint64_t tracefile_array_get_read_file_index_head(struct tracefile_array *tfa); |
| 61 | /* May return -1ULL in the case where we have not received any indexes yet. */ |
| 62 | uint64_t tracefile_array_get_seq_head(struct tracefile_array *tfa); |
| 63 | |
| 64 | uint64_t tracefile_array_get_file_index_tail(struct tracefile_array *tfa); |
| 65 | /* May return -1ULL in the case where we have not received any indexes yet. */ |
| 66 | uint64_t tracefile_array_get_seq_tail(struct tracefile_array *tfa); |
| 67 | |
| 68 | bool tracefile_array_seq_in_file(struct tracefile_array *tfa, uint64_t file_index, uint64_t seq); |
| 69 | |
| 70 | #endif /* _STREAM_H */ |