From 20d0dcf9609dcd28aebe87c167d6600ddbe668d1 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Fri, 17 Feb 2012 13:01:29 -0500 Subject: [PATCH] API fix: packet_seek should take an index rather than offset Signed-off-by: Julien Desfossez Signed-off-by: Mathieu Desnoyers --- formats/bt-dummy/bt-dummy.c | 2 +- formats/ctf-text/ctf-text.c | 4 +-- formats/ctf/ctf.c | 65 ++++++++++++++++++---------------- include/babeltrace/context.h | 2 +- include/babeltrace/ctf/types.h | 6 ++-- include/babeltrace/format.h | 4 +-- lib/context.c | 4 +-- lib/iterator.c | 11 ++---- 8 files changed, 47 insertions(+), 51 deletions(-) diff --git a/formats/bt-dummy/bt-dummy.c b/formats/bt-dummy/bt-dummy.c index 3249317a..e251ee7b 100644 --- a/formats/bt-dummy/bt-dummy.c +++ b/formats/bt-dummy/bt-dummy.c @@ -40,7 +40,7 @@ int bt_dummy_write_event(struct stream_pos *ppos, static struct trace_descriptor *bt_dummy_open_trace(const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp) { struct ctf_text_stream_pos *pos; diff --git a/formats/ctf-text/ctf-text.c b/formats/ctf-text/ctf-text.c index 62cb5310..20e18e86 100644 --- a/formats/ctf-text/ctf-text.c +++ b/formats/ctf-text/ctf-text.c @@ -76,7 +76,7 @@ enum bt_loglevel { struct trace_descriptor *ctf_text_open_trace(const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp); void ctf_text_close_trace(struct trace_descriptor *descriptor); @@ -459,7 +459,7 @@ error: struct trace_descriptor *ctf_text_open_trace(const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp) { struct ctf_text_stream_pos *pos; diff --git a/formats/ctf/ctf.c b/formats/ctf/ctf.c index 5911b19d..17f299c3 100644 --- a/formats/ctf/ctf.c +++ b/formats/ctf/ctf.c @@ -65,12 +65,14 @@ extern int yydebug; static struct trace_descriptor *ctf_open_trace(const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, - int whence), FILE *metadata_fp); + void (*packet_seek)(struct stream_pos *pos, size_t index, + int whence), + FILE *metadata_fp); static struct trace_descriptor *ctf_open_mmap_trace( struct mmap_stream_list *mmap_list, - void (*packet_seek)(struct stream_pos *pos, size_t offset, int whence), + void (*packet_seek)(struct stream_pos *pos, size_t index, + int whence), FILE *metadata_fp); static @@ -436,7 +438,11 @@ void ctf_fini_pos(struct ctf_stream_pos *pos) (void) g_array_free(pos->packet_index, TRUE); } -void ctf_packet_seek(struct stream_pos *stream_pos, size_t offset, int whence) +/* + * for SEEK_CUR: go to next packet. + * for SEEK_POS: go to packet numer (index). + */ +void ctf_packet_seek(struct stream_pos *stream_pos, size_t index, int whence) { struct ctf_stream_pos *pos = container_of(stream_pos, struct ctf_stream_pos, parent); @@ -444,7 +450,7 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t offset, int whence) container_of(pos, struct ctf_file_stream, pos); int ret; off_t off; - struct packet_index *index; + struct packet_index *packet_index; if (pos->prot == PROT_WRITE && pos->content_size_loc) *pos->content_size_loc = pos->offset; @@ -468,11 +474,10 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t offset, int whence) switch (whence) { case SEEK_CUR: /* The writer will add padding */ - assert(pos->offset + offset == pos->packet_size); pos->mmap_offset += WRITE_PACKET_LEN / CHAR_BIT; break; case SEEK_SET: - assert(offset == 0); /* only seek supported for now */ + assert(index == 0); /* only seek supported for now */ pos->cur_index = 0; break; default: @@ -495,27 +500,25 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t offset, int whence) return; } /* For printing discarded event count */ - index = &g_array_index(pos->packet_index, + packet_index = &g_array_index(pos->packet_index, struct packet_index, pos->cur_index); - events_discarded_diff = index->events_discarded; + events_discarded_diff = packet_index->events_discarded; file_stream->parent.prev_timestamp_end = - index->timestamp_end; + packet_index->timestamp_end; if (pos->cur_index > 0) { - index = &g_array_index(pos->packet_index, + packet_index = &g_array_index(pos->packet_index, struct packet_index, pos->cur_index - 1); - events_discarded_diff -= index->events_discarded; + events_discarded_diff -= packet_index->events_discarded; } file_stream->parent.events_discarded = events_discarded_diff; file_stream->parent.prev_timestamp = file_stream->parent.timestamp; /* The reader will expect us to skip padding */ - assert(pos->offset + offset == pos->content_size); ++pos->cur_index; break; } case SEEK_SET: - if (offset == 0) - pos->cur_index = 0; + pos->cur_index = index; file_stream->parent.prev_timestamp = 0; file_stream->parent.prev_timestamp_end = 0; break; @@ -546,20 +549,19 @@ void ctf_packet_seek(struct stream_pos *stream_pos, size_t offset, int whence) pos->offset = EOF; return; } - index = &g_array_index(pos->packet_index, struct packet_index, + packet_index = &g_array_index(pos->packet_index, struct packet_index, pos->cur_index); - pos->mmap_offset = index->offset; + pos->mmap_offset = packet_index->offset; /* Lookup context/packet size in index */ - file_stream->parent.timestamp = index->timestamp_begin; - pos->content_size = index->content_size; - pos->packet_size = index->packet_size; - if (index->data_offset < index->content_size) { + file_stream->parent.timestamp = packet_index->timestamp_begin; + pos->content_size = packet_index->content_size; + pos->packet_size = packet_index->packet_size; + if (packet_index->data_offset < packet_index->content_size) { pos->offset = 0; /* will read headers */ - } else if (index->data_offset == index->content_size) { + } else if (packet_index->data_offset == packet_index->content_size) { /* empty packet */ - pos->offset = index->data_offset; - offset = 0; + pos->offset = packet_index->data_offset; whence = SEEK_CUR; goto read_next_packet; } else { @@ -770,7 +772,7 @@ int ctf_open_trace_metadata_stream_read(struct ctf_trace *td, FILE **fp, static int ctf_open_trace_metadata_read(struct ctf_trace *td, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp) { struct ctf_scanner *scanner; @@ -1243,7 +1245,7 @@ error: */ static int ctf_open_file_stream_read(struct ctf_trace *td, const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence)) { int ret; @@ -1294,7 +1296,7 @@ error: static int ctf_open_trace_read(struct ctf_trace *td, const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp) { int ret; @@ -1378,7 +1380,7 @@ error: static struct trace_descriptor *ctf_open_trace(const char *path, int flags, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp) { struct ctf_trace *td; @@ -1467,7 +1469,7 @@ end: static int ctf_open_mmap_stream_read(struct ctf_trace *td, struct mmap_stream *mmap_info, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence)) { int ret; @@ -1502,7 +1504,7 @@ error_def: int ctf_open_mmap_trace_read(struct ctf_trace *td, struct mmap_stream_list *mmap_list, - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence), FILE *metadata_fp) { @@ -1535,7 +1537,8 @@ error: static struct trace_descriptor *ctf_open_mmap_trace( struct mmap_stream_list *mmap_list, - void (*packet_seek)(struct stream_pos *pos, size_t offset, int whence), + void (*packet_seek)(struct stream_pos *pos, size_t index, + int whence), FILE *metadata_fp) { struct ctf_trace *td; diff --git a/include/babeltrace/context.h b/include/babeltrace/context.h index 6190d37b..f9da534e 100644 --- a/include/babeltrace/context.h +++ b/include/babeltrace/context.h @@ -51,7 +51,7 @@ struct bt_context *bt_context_create(void); int bt_context_add_trace(struct bt_context *ctx, const char *path, const char *format, void (*packet_seek)(struct stream_pos *pos, - size_t offset, int whence)); + size_t index, int whence)); /* * bt_context_remove_trace: Remove a trace from the context. diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index 24e1aa5b..faa653ea 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -62,7 +62,7 @@ struct ctf_stream_pos { ssize_t offset; /* offset from base, in bits. EOF for end of file. */ ssize_t last_offset; /* offset before the last read_event */ size_t cur_index; /* current index in packet index */ - void (*packet_seek)(struct stream_pos *pos, size_t offset, + void (*packet_seek)(struct stream_pos *pos, size_t index, int whence); /* function called to switch packet */ int dummy; /* dummy position, for length calculation */ @@ -90,7 +90,7 @@ int ctf_array_write(struct stream_pos *pos, struct definition *definition); int ctf_sequence_read(struct stream_pos *pos, struct definition *definition); int ctf_sequence_write(struct stream_pos *pos, struct definition *definition); -void ctf_packet_seek(struct stream_pos *pos, size_t offset, int whence); +void ctf_packet_seek(struct stream_pos *pos, size_t index, int whence); void ctf_init_pos(struct ctf_stream_pos *pos, int fd, int open_flags); void ctf_fini_pos(struct ctf_stream_pos *pos); @@ -118,7 +118,7 @@ void ctf_move_pos(struct ctf_stream_pos *pos, size_t bit_offset) && (unlikely(pos->offset + bit_offset >= pos->packet_size))) { printf_debug("ctf_packet_seek (before call): %zd\n", pos->offset); - ctf_packet_seek(&pos->parent, bit_offset, SEEK_CUR); + ctf_packet_seek(&pos->parent, 0, SEEK_CUR); printf_debug("ctf_packet_seek (after call): %zd\n", pos->offset); return; diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index bba056cc..8eae42c0 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -48,12 +48,12 @@ struct format { struct trace_descriptor *(*open_trace)(const char *path, int flags, void (*packet_seek)(struct stream_pos *pos, - size_t offset, int whence), + size_t index, int whence), FILE *metadata_fp); struct trace_descriptor *(*open_mmap_trace)( struct mmap_stream_list *mmap_list, void (*packet_seek)(struct stream_pos *pos, - size_t offset, int whence), + size_t index, int whence), FILE *metadata_fp); void (*close_trace)(struct trace_descriptor *descriptor); }; diff --git a/lib/context.c b/lib/context.c index 5021d7ad..1311b5c2 100644 --- a/lib/context.c +++ b/lib/context.c @@ -58,8 +58,8 @@ struct bt_context *bt_context_create(void) int bt_context_add_trace(struct bt_context *ctx, const char *path, const char *format_name, - void (*packet_seek)(struct stream_pos *pos, - size_t offset, int whence)) + void (*packet_seek)(struct stream_pos *pos, size_t index, + int whence)) { struct trace_descriptor *td; struct format *fmt; diff --git a/lib/iterator.c b/lib/iterator.c index 2cb40cbc..398b097b 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -115,9 +115,7 @@ static int seek_file_stream_by_timestamp(struct ctf_file_stream *cfs, index->timestamp_end <= timestamp) continue; - stream_pos->cur_index = i; - stream_pos->packet_seek(&stream_pos->parent, - index->offset, SEEK_SET); + stream_pos->packet_seek(&stream_pos->parent, i, SEEK_SET); while (cfs->parent.timestamp < timestamp) { ret = stream_read_event(cfs); if (ret < 0) @@ -191,7 +189,6 @@ int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) i++) { struct stream_saved_pos *saved_pos; struct ctf_stream_pos *stream_pos; - struct packet_index *index; struct ctf_stream *stream; saved_pos = &g_array_index( @@ -199,13 +196,9 @@ int bt_iter_set_pos(struct bt_iter *iter, const struct bt_iter_pos *iter_pos) struct stream_saved_pos, i); stream = &saved_pos->file_stream->parent; stream_pos = &saved_pos->file_stream->pos; - index = &g_array_index(stream_pos->packet_index, - struct packet_index, - saved_pos->cur_index); - stream_pos->cur_index = saved_pos->cur_index; stream_pos->packet_seek(&stream_pos->parent, - index->offset, SEEK_SET); + saved_pos->cur_index, SEEK_SET); /* * the timestamp needs to be restored after -- 2.34.1