| 1 | /* |
| 2 | * test-seeks.c |
| 3 | * |
| 4 | * Lib BabelTrace - Seeks test program |
| 5 | * |
| 6 | * Copyright 2012 - Yannick Brosseau <yannick.brosseau@gmail.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; under version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | #define _GNU_SOURCE |
| 22 | #include <babeltrace/context.h> |
| 23 | #include <babeltrace/iterator.h> |
| 24 | #include <babeltrace/ctf/iterator.h> |
| 25 | #include <babeltrace/ctf/events.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | #include <limits.h> |
| 31 | |
| 32 | #include "common.h" |
| 33 | #include "tap.h" |
| 34 | |
| 35 | #define NR_TESTS 23 |
| 36 | |
| 37 | void run_seek_begin(char *path, uint64_t expected_begin) |
| 38 | { |
| 39 | struct bt_context *ctx; |
| 40 | struct bt_ctf_iter *iter; |
| 41 | struct bt_ctf_event *event; |
| 42 | struct bt_iter_pos newpos; |
| 43 | int ret; |
| 44 | uint64_t timestamp_begin; |
| 45 | uint64_t timestamp_seek_begin; |
| 46 | |
| 47 | /* Open the trace */ |
| 48 | ctx = create_context_with_path(path); |
| 49 | if (!ctx) { |
| 50 | plan_skip_all("Cannot create valid context"); |
| 51 | } |
| 52 | |
| 53 | /* Create iterator with null begin and end */ |
| 54 | iter = bt_ctf_iter_create(ctx, NULL, NULL); |
| 55 | if (!iter) { |
| 56 | plan_skip_all("Cannot create valid iterator"); |
| 57 | } |
| 58 | |
| 59 | event = bt_ctf_iter_read_event(iter); |
| 60 | |
| 61 | ok(event, "Event valid"); |
| 62 | |
| 63 | /* Validate that the first timestamp is right */ |
| 64 | timestamp_begin = bt_ctf_get_timestamp(event); |
| 65 | |
| 66 | ok1(timestamp_begin == expected_begin); |
| 67 | |
| 68 | /* Validate that we get the same value after a seek begin */ |
| 69 | newpos.type = BT_SEEK_BEGIN; |
| 70 | ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); |
| 71 | |
| 72 | ok(ret == 0, "Seek begin retval %d", ret); |
| 73 | |
| 74 | event = bt_ctf_iter_read_event(iter); |
| 75 | |
| 76 | ok(event, "Event valid"); |
| 77 | |
| 78 | timestamp_seek_begin = bt_ctf_get_timestamp(event); |
| 79 | |
| 80 | ok1(timestamp_begin == timestamp_seek_begin); |
| 81 | |
| 82 | bt_context_put(ctx); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | void run_seek_last(char *path, uint64_t expected_last) |
| 87 | { |
| 88 | struct bt_context *ctx; |
| 89 | struct bt_ctf_iter *iter; |
| 90 | struct bt_ctf_event *event; |
| 91 | struct bt_iter_pos newpos; |
| 92 | int ret; |
| 93 | uint64_t timestamp_last; |
| 94 | |
| 95 | /* Open the trace */ |
| 96 | ctx = create_context_with_path(path); |
| 97 | if (!ctx) { |
| 98 | plan_skip_all("Cannot create valid context"); |
| 99 | } |
| 100 | |
| 101 | /* Create iterator with null last and end */ |
| 102 | iter = bt_ctf_iter_create(ctx, NULL, NULL); |
| 103 | if (!iter) { |
| 104 | plan_skip_all("Cannot create valid iterator"); |
| 105 | } |
| 106 | |
| 107 | event = bt_ctf_iter_read_event(iter); |
| 108 | |
| 109 | ok(event, "Event valid at beginning"); |
| 110 | |
| 111 | /* Seek to last */ |
| 112 | newpos.type = BT_SEEK_LAST; |
| 113 | ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); |
| 114 | |
| 115 | ok(ret == 0, "Seek last retval %d", ret); |
| 116 | |
| 117 | event = bt_ctf_iter_read_event(iter); |
| 118 | |
| 119 | ok(event, "Event valid at last position"); |
| 120 | |
| 121 | timestamp_last = bt_ctf_get_timestamp(event); |
| 122 | |
| 123 | ok1(timestamp_last == expected_last); |
| 124 | |
| 125 | /* Try to read next event */ |
| 126 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
| 127 | |
| 128 | ok(ret == 0, "iter next should return an error"); |
| 129 | |
| 130 | event = bt_ctf_iter_read_event(iter); |
| 131 | |
| 132 | ok(event == 0, "Event after last should be invalid"); |
| 133 | |
| 134 | bt_context_put(ctx); |
| 135 | } |
| 136 | |
| 137 | void run_seek_cycles(char *path, |
| 138 | uint64_t expected_begin, |
| 139 | uint64_t expected_last) |
| 140 | { |
| 141 | struct bt_context *ctx; |
| 142 | struct bt_ctf_iter *iter; |
| 143 | struct bt_ctf_event *event; |
| 144 | struct bt_iter_pos newpos; |
| 145 | int ret; |
| 146 | uint64_t timestamp; |
| 147 | |
| 148 | /* Open the trace */ |
| 149 | ctx = create_context_with_path(path); |
| 150 | if (!ctx) { |
| 151 | plan_skip_all("Cannot create valid context"); |
| 152 | } |
| 153 | |
| 154 | /* Create iterator with null last and end */ |
| 155 | iter = bt_ctf_iter_create(ctx, NULL, NULL); |
| 156 | if (!iter) { |
| 157 | plan_skip_all("Cannot create valid iterator"); |
| 158 | } |
| 159 | |
| 160 | event = bt_ctf_iter_read_event(iter); |
| 161 | |
| 162 | ok(event, "Event valid at beginning"); |
| 163 | |
| 164 | /* Seek to last */ |
| 165 | newpos.type = BT_SEEK_LAST; |
| 166 | ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); |
| 167 | |
| 168 | ok(ret == 0, "Seek last retval %d", ret); |
| 169 | |
| 170 | event = bt_ctf_iter_read_event(iter); |
| 171 | |
| 172 | ok(event, "Event valid at last position"); |
| 173 | |
| 174 | timestamp = bt_ctf_get_timestamp(event); |
| 175 | |
| 176 | ok1(timestamp == expected_last); |
| 177 | |
| 178 | /* Try to read next event */ |
| 179 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
| 180 | |
| 181 | ok(ret == 0, "iter next should return an error"); |
| 182 | |
| 183 | event = bt_ctf_iter_read_event(iter); |
| 184 | |
| 185 | ok(event == 0, "Event after last should be invalid"); |
| 186 | |
| 187 | /* Seek to BEGIN */ |
| 188 | newpos.type = BT_SEEK_BEGIN; |
| 189 | ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); |
| 190 | |
| 191 | ok(ret == 0, "Seek begin retval %d", ret); |
| 192 | |
| 193 | event = bt_ctf_iter_read_event(iter); |
| 194 | |
| 195 | ok(event, "Event valid at first position"); |
| 196 | |
| 197 | timestamp = bt_ctf_get_timestamp(event); |
| 198 | |
| 199 | ok1(timestamp == expected_begin); |
| 200 | |
| 201 | /* Seek last again */ |
| 202 | newpos.type = BT_SEEK_LAST; |
| 203 | ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos); |
| 204 | |
| 205 | ok(ret == 0, "Seek last retval %d", ret); |
| 206 | |
| 207 | event = bt_ctf_iter_read_event(iter); |
| 208 | |
| 209 | ok(event, "Event valid at last position"); |
| 210 | |
| 211 | timestamp = bt_ctf_get_timestamp(event); |
| 212 | |
| 213 | ok1(timestamp == expected_last); |
| 214 | |
| 215 | bt_context_put(ctx); |
| 216 | } |
| 217 | |
| 218 | int main(int argc, char **argv) |
| 219 | { |
| 220 | char *path; |
| 221 | uint64_t expected_begin; |
| 222 | uint64_t expected_last; |
| 223 | |
| 224 | plan_tests(NR_TESTS); |
| 225 | |
| 226 | if (argc < 4) { |
| 227 | plan_skip_all("Invalid arguments: need a trace path and the start and last timestamp"); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | /* Parse arguments (Trace, begin timestamp) */ |
| 232 | path = argv[1]; |
| 233 | |
| 234 | expected_begin = strtoull(argv[2], NULL, 0); |
| 235 | if (ULLONG_MAX == expected_begin && errno == ERANGE) { |
| 236 | plan_skip_all("Invalid value for begin timestamp"); |
| 237 | } |
| 238 | |
| 239 | expected_last = strtoull(argv[3], NULL, 0); |
| 240 | if (ULLONG_MAX == expected_last && errno == ERANGE) { |
| 241 | plan_skip_all("Invalid value for last timestamp"); |
| 242 | } |
| 243 | |
| 244 | run_seek_begin(path, expected_begin); |
| 245 | run_seek_last(path, expected_last); |
| 246 | run_seek_cycles(path, expected_begin, expected_last); |
| 247 | |
| 248 | return exit_status(); |
| 249 | } |