| 1 | /* |
| 2 | * babeltrace-lib.c |
| 3 | * |
| 4 | * Babeltrace Trace Converter Library |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | */ |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <errno.h> |
| 23 | #include <stdio.h> |
| 24 | #include <inttypes.h> |
| 25 | #include <babeltrace/babeltrace.h> |
| 26 | #include <babeltrace/format.h> |
| 27 | #include <babeltrace/ctf/types.h> |
| 28 | #include <babeltrace/ctf/metadata.h> |
| 29 | #include <babeltrace/ctf-text/types.h> |
| 30 | #include <babeltrace/prio_heap.h> |
| 31 | |
| 32 | static int read_event(struct ctf_file_stream *sin) |
| 33 | { |
| 34 | int ret; |
| 35 | |
| 36 | ret = sin->pos.parent.event_cb(&sin->pos.parent, &sin->parent); |
| 37 | if (ret == EOF) |
| 38 | return EOF; |
| 39 | else if (ret) { |
| 40 | fprintf(stdout, "[error] Reading event failed.\n"); |
| 41 | return ret; |
| 42 | } |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * returns true if a < b, false otherwise. |
| 48 | */ |
| 49 | int stream_compare(void *a, void *b) |
| 50 | { |
| 51 | struct ctf_file_stream *s_a = a, *s_b = b; |
| 52 | |
| 53 | if (s_a->parent.timestamp < s_b->parent.timestamp) |
| 54 | return 1; |
| 55 | else |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | int convert_trace(struct trace_descriptor *td_write, |
| 60 | struct trace_collection *trace_collection_read) |
| 61 | { |
| 62 | struct ptr_heap *stream_heap; |
| 63 | struct ctf_text_stream_pos *sout; |
| 64 | int i, stream_id; |
| 65 | int ret = 0; |
| 66 | |
| 67 | stream_heap = g_new(struct ptr_heap, 1); |
| 68 | heap_init(stream_heap, 0, stream_compare); |
| 69 | sout = container_of(td_write, struct ctf_text_stream_pos, |
| 70 | trace_descriptor); |
| 71 | |
| 72 | for (i = 0; i < trace_collection_read->array->len; i++) { |
| 73 | struct ctf_trace *tin; |
| 74 | struct trace_descriptor *td_read; |
| 75 | |
| 76 | td_read = g_ptr_array_index(trace_collection_read->array, i); |
| 77 | tin = container_of(td_read, struct ctf_trace, parent); |
| 78 | |
| 79 | /* Populate heap with each stream */ |
| 80 | for (stream_id = 0; stream_id < tin->streams->len; |
| 81 | stream_id++) { |
| 82 | struct ctf_stream_class *stream; |
| 83 | int filenr; |
| 84 | |
| 85 | stream = g_ptr_array_index(tin->streams, stream_id); |
| 86 | if (!stream) |
| 87 | continue; |
| 88 | for (filenr = 0; filenr < stream->streams->len; |
| 89 | filenr++) { |
| 90 | struct ctf_file_stream *file_stream; |
| 91 | |
| 92 | file_stream = g_ptr_array_index(stream->streams, |
| 93 | filenr); |
| 94 | |
| 95 | ret = read_event(file_stream); |
| 96 | if (ret == EOF) { |
| 97 | ret = 0; |
| 98 | continue; |
| 99 | } else if (ret) { |
| 100 | goto end; |
| 101 | } |
| 102 | /* Add to heap */ |
| 103 | ret = heap_insert(stream_heap, file_stream); |
| 104 | if (ret) { |
| 105 | fprintf(stdout, |
| 106 | "[error] Out of memory.\n"); |
| 107 | goto end; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Replace heap entries until EOF for each stream (heap empty) */ |
| 114 | for (;;) { |
| 115 | struct ctf_file_stream *file_stream, *removed; |
| 116 | |
| 117 | file_stream = heap_maximum(stream_heap); |
| 118 | if (!file_stream) { |
| 119 | /* end of file for all streams */ |
| 120 | ret = 0; |
| 121 | break; |
| 122 | } |
| 123 | ret = sout->parent.event_cb(&sout->parent, &file_stream->parent); |
| 124 | if (ret) { |
| 125 | fprintf(stdout, "[error] Writing event failed.\n"); |
| 126 | goto end; |
| 127 | } |
| 128 | ret = read_event(file_stream); |
| 129 | if (ret == EOF) { |
| 130 | removed = heap_remove(stream_heap); |
| 131 | assert(removed == file_stream); |
| 132 | ret = 0; |
| 133 | continue; |
| 134 | } else if (ret) |
| 135 | goto end; |
| 136 | /* Reinsert the file stream into the heap, and rebalance. */ |
| 137 | removed = heap_replace_max(stream_heap, file_stream); |
| 138 | assert(removed == file_stream); |
| 139 | } |
| 140 | |
| 141 | end: |
| 142 | heap_free(stream_heap); |
| 143 | g_free(stream_heap); |
| 144 | return ret; |
| 145 | } |