| 1 | /* |
| 2 | * Copyright (C) 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License, version 2 only, as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <assert.h> |
| 21 | #include <common/common.h> |
| 22 | #include <common/utils.h> |
| 23 | #include <common/defaults.h> |
| 24 | |
| 25 | #include "tracefile-array.h" |
| 26 | |
| 27 | struct tracefile_array *tracefile_array_create(size_t count) |
| 28 | { |
| 29 | struct tracefile_array *tfa = NULL; |
| 30 | int i; |
| 31 | |
| 32 | tfa = zmalloc(sizeof(*tfa)); |
| 33 | if (!tfa) { |
| 34 | goto error; |
| 35 | } |
| 36 | tfa->tf = zmalloc(sizeof(*tfa->tf) * count); |
| 37 | if (!tfa->tf) { |
| 38 | goto error; |
| 39 | } |
| 40 | tfa->count = count; |
| 41 | for (i = 0; i < count; i++) { |
| 42 | tfa->tf[i].seq_head = -1ULL; |
| 43 | tfa->tf[i].seq_tail = -1ULL; |
| 44 | } |
| 45 | tfa->seq_head = -1ULL; |
| 46 | tfa->seq_tail = -1ULL; |
| 47 | return tfa; |
| 48 | |
| 49 | error: |
| 50 | if (tfa) { |
| 51 | free(tfa->tf); |
| 52 | } |
| 53 | free(tfa); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | void tracefile_array_destroy(struct tracefile_array *tfa) |
| 58 | { |
| 59 | if (!tfa) { |
| 60 | return; |
| 61 | } |
| 62 | free(tfa->tf); |
| 63 | free(tfa); |
| 64 | } |
| 65 | |
| 66 | void tracefile_array_file_rotate(struct tracefile_array *tfa) |
| 67 | { |
| 68 | uint64_t *headp, *tailp; |
| 69 | |
| 70 | if (!tfa->count) { |
| 71 | /* Not in tracefile rotation mode. */ |
| 72 | return; |
| 73 | } |
| 74 | /* Rotate to next file. */ |
| 75 | tfa->file_head = (tfa->file_head + 1) % tfa->count; |
| 76 | if (tfa->file_head == tfa->file_tail) { |
| 77 | /* Move tail. */ |
| 78 | tfa->file_tail = (tfa->file_tail + 1) % tfa->count; |
| 79 | } |
| 80 | headp = &tfa->tf[tfa->file_head].seq_head; |
| 81 | tailp = &tfa->tf[tfa->file_head].seq_tail; |
| 82 | /* |
| 83 | * If we overwrite a file with content, we need to push the tail |
| 84 | * to the position following the content we are overwriting. |
| 85 | */ |
| 86 | if (*headp != -1ULL) { |
| 87 | tfa->seq_tail = tfa->tf[tfa->file_tail].seq_tail; |
| 88 | } |
| 89 | /* Reset this file head/tail (overwrite). */ |
| 90 | *headp = -1ULL; |
| 91 | *tailp = -1ULL; |
| 92 | } |
| 93 | |
| 94 | void tracefile_array_commit_seq(struct tracefile_array *tfa) |
| 95 | { |
| 96 | uint64_t *headp, *tailp; |
| 97 | |
| 98 | /* Increment overall head. */ |
| 99 | tfa->seq_head++; |
| 100 | /* If we are committing our first index overall, set tail to 0. */ |
| 101 | if (tfa->seq_tail == -1ULL) { |
| 102 | tfa->seq_tail = 0; |
| 103 | } |
| 104 | if (!tfa->count) { |
| 105 | /* Not in tracefile rotation mode. */ |
| 106 | return; |
| 107 | } |
| 108 | headp = &tfa->tf[tfa->file_head].seq_head; |
| 109 | tailp = &tfa->tf[tfa->file_head].seq_tail; |
| 110 | /* Update head tracefile seq_head. */ |
| 111 | *headp = tfa->seq_head; |
| 112 | /* |
| 113 | * If we are committing our first index in this packet, set tail |
| 114 | * to this index seq count. |
| 115 | */ |
| 116 | if (*tailp == -1ULL) { |
| 117 | *tailp = tfa->seq_head; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | uint64_t tracefile_array_get_file_index_head(struct tracefile_array *tfa) |
| 122 | { |
| 123 | return tfa->file_head; |
| 124 | } |
| 125 | |
| 126 | uint64_t tracefile_array_get_seq_head(struct tracefile_array *tfa) |
| 127 | { |
| 128 | return tfa->seq_head; |
| 129 | } |
| 130 | |
| 131 | uint64_t tracefile_array_get_file_index_tail(struct tracefile_array *tfa) |
| 132 | { |
| 133 | return tfa->file_tail; |
| 134 | } |
| 135 | |
| 136 | uint64_t tracefile_array_get_seq_tail(struct tracefile_array *tfa) |
| 137 | { |
| 138 | return tfa->seq_tail; |
| 139 | } |
| 140 | |
| 141 | bool tracefile_array_seq_in_file(struct tracefile_array *tfa, |
| 142 | uint64_t file_index, uint64_t seq) |
| 143 | { |
| 144 | if (!tfa->count) { |
| 145 | /* |
| 146 | * Not in tracefile rotation mode; we are guaranteed to have the |
| 147 | * index in this file. |
| 148 | */ |
| 149 | return true; |
| 150 | } |
| 151 | assert(file_index < tfa->count); |
| 152 | if (seq == -1ULL) { |
| 153 | return false; |
| 154 | } |
| 155 | if (seq >= tfa->tf[file_index].seq_tail |
| 156 | && seq <= tfa->tf[file_index].seq_head) { |
| 157 | return true; |
| 158 | } else { |
| 159 | return false; |
| 160 | } |
| 161 | } |