Standard logging for trimmer/copy.c
[babeltrace.git] / plugins / utils / trimmer / copy.c
CommitLineData
19ce87a4
JD
1/*
2 * copy.c
3 *
4 * Babeltrace Copy Trace Structure
5 *
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
7 *
8 * Author: Julien Desfossez <jdesfossez@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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
c59fc0d5
JD
29#define BT_LOG_TAG "PLUGIN-UTILS-TRIMMER-FLT-COPY"
30#include "logging.h"
31
32#include <assert.h>
19ce87a4
JD
33#include <babeltrace/ctf-ir/event.h>
34#include <babeltrace/ctf-ir/packet.h>
35#include <babeltrace/ctf-ir/event-class.h>
36#include <babeltrace/ctf-ir/stream.h>
37#include <babeltrace/ctf-ir/stream-class.h>
38#include <babeltrace/ctf-ir/clock-class.h>
39#include <babeltrace/ctf-ir/fields.h>
40#include <babeltrace/ctf-writer/stream-class.h>
41#include <babeltrace/ctf-writer/stream.h>
42
43#include <ctfcopytrace.h>
44#include "iterator.h"
45
46static
47struct bt_ctf_packet *lookup_packet(struct trimmer_iterator *trim_it,
48 struct bt_ctf_packet *packet)
49{
50 return (struct bt_ctf_packet *) g_hash_table_lookup(
51 trim_it->packet_map,
52 (gpointer) packet);
53}
54
55static
56struct bt_ctf_packet *insert_new_packet(struct trimmer_iterator *trim_it,
57 struct bt_ctf_packet *packet,
58 struct bt_ctf_stream *stream)
59{
9ae49d3d 60 struct bt_ctf_packet *writer_packet = NULL;
387483fc 61 int ret;
19ce87a4 62
c59fc0d5 63 BT_LOGD_STR("Inserting a new packet.");
19ce87a4
JD
64 writer_packet = bt_ctf_packet_create(stream);
65 if (!writer_packet) {
c59fc0d5 66 BT_LOGE_STR("Failed to create a new packet.");
387483fc 67 goto error;
19ce87a4 68 }
19ce87a4 69
387483fc
JD
70 ret = ctf_packet_copy_header(trim_it->err, packet, writer_packet);
71 if (ret) {
c59fc0d5 72 BT_LOGE_STR("Failed to copy packet header.");
387483fc
JD
73 goto error;
74 }
75
76 g_hash_table_insert(trim_it->packet_map, (gpointer) packet,
77 writer_packet);
78 goto end;
79
80error:
81 BT_PUT(writer_packet);
19ce87a4
JD
82end:
83 return writer_packet;
84}
85
86BT_HIDDEN
87enum bt_component_status update_packet_context_field(FILE *err,
88 struct bt_ctf_packet *writer_packet,
89 const char *name, int64_t value)
90{
91 enum bt_component_status ret;
9ae49d3d
JD
92 struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
93 struct bt_ctf_field_type *struct_type = NULL, *field_type = NULL;
94 struct bt_ctf_field *field = NULL, *writer_field = NULL;
19ce87a4
JD
95 int nr_fields, i, int_ret;
96
c59fc0d5 97 BT_LOGD("Updating packet context field: name=%s", name);
19ce87a4 98 packet_context = bt_ctf_packet_get_context(writer_packet);
c59fc0d5 99 assert(packet_context);
19ce87a4
JD
100
101 struct_type = bt_ctf_field_get_type(packet_context);
c59fc0d5 102 assert(struct_type);
19ce87a4
JD
103
104 writer_packet_context = bt_ctf_packet_get_context(writer_packet);
c59fc0d5 105 assert(writer_packet_context);
19ce87a4
JD
106
107 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
108 for (i = 0; i < nr_fields; i++) {
19ce87a4
JD
109 const char *field_name;
110
111 field = bt_ctf_field_structure_get_field_by_index(
112 packet_context, i);
113 if (!field) {
c59fc0d5
JD
114 BT_LOGE("Failed to get field in packet-context: field-name=\"%s\"",
115 name);
9ae49d3d 116 goto error;
19ce87a4
JD
117 }
118 if (bt_ctf_field_type_structure_get_field(struct_type,
119 &field_name, &field_type, i) < 0) {
c59fc0d5
JD
120 BT_LOGE("Failed to get field: field-name=\"%s\"",
121 field_name);
9ae49d3d 122 goto error;
19ce87a4
JD
123 }
124 if (strcmp(field_name, name)) {
9ae49d3d
JD
125 BT_PUT(field_type);
126 BT_PUT(field);
19ce87a4
JD
127 continue;
128 }
c59fc0d5
JD
129 if (bt_ctf_field_type_get_type_id(field_type) !=
130 BT_CTF_FIELD_TYPE_ID_INTEGER) {
131 BT_LOGE("Expecting an integer for this field: field-name=\"%s\"",
132 name);
9ae49d3d 133 goto error;
19ce87a4 134 }
c59fc0d5 135
19ce87a4
JD
136 writer_field = bt_ctf_field_structure_get_field(writer_packet_context,
137 field_name);
c59fc0d5 138 assert(writer_field);
19ce87a4
JD
139
140 int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value);
c59fc0d5
JD
141 assert(int_ret == 0);
142
9ae49d3d
JD
143 BT_PUT(writer_field);
144 BT_PUT(field_type);
145 BT_PUT(field);
19ce87a4
JD
146 }
147
148 ret = BT_COMPONENT_STATUS_OK;
9ae49d3d 149 goto end;
19ce87a4 150
9ae49d3d
JD
151error:
152 bt_put(writer_field);
153 bt_put(field_type);
154 bt_put(field);
155 ret = BT_COMPONENT_STATUS_ERROR;
156end:
19ce87a4 157 bt_put(struct_type);
19ce87a4 158 bt_put(packet_context);
19ce87a4
JD
159 return ret;
160}
161
162BT_HIDDEN
163struct bt_ctf_packet *trimmer_new_packet(
164 struct trimmer_iterator *trim_it,
165 struct bt_ctf_packet *packet)
166{
9ae49d3d 167 struct bt_ctf_stream *stream = NULL;
19ce87a4
JD
168 struct bt_ctf_packet *writer_packet = NULL;
169 int int_ret;
170
171 stream = bt_ctf_packet_get_stream(packet);
c59fc0d5 172 assert(stream);
19ce87a4
JD
173
174 /*
175 * If a packet was already opened, close it and remove it from
176 * the HT.
177 */
178 writer_packet = lookup_packet(trim_it, packet);
179 if (writer_packet) {
180 g_hash_table_remove(trim_it->packet_map, packet);
9ae49d3d 181 BT_PUT(writer_packet);
19ce87a4
JD
182 }
183
184 writer_packet = insert_new_packet(trim_it, packet, stream);
185 if (!writer_packet) {
c59fc0d5 186 BT_LOGE_STR("Failed to insert new packet.");
9ae49d3d 187 goto error;
19ce87a4
JD
188 }
189 bt_get(writer_packet);
190
9877e1aa
JD
191 int_ret = ctf_packet_copy_context(trim_it->err, packet,
192 stream, writer_packet);
193 if (int_ret < 0) {
c59fc0d5 194 BT_LOGE_STR("Failed to copy packet context.");
9ae49d3d 195 goto error;
19ce87a4
JD
196 }
197
9ae49d3d
JD
198 goto end;
199
200error:
201 BT_PUT(writer_packet);
202end:
19ce87a4 203 bt_put(stream);
19ce87a4
JD
204 return writer_packet;
205}
206
207BT_HIDDEN
208struct bt_ctf_packet *trimmer_close_packet(
209 struct trimmer_iterator *trim_it,
210 struct bt_ctf_packet *packet)
211{
9ae49d3d 212 struct bt_ctf_packet *writer_packet = NULL;
19ce87a4
JD
213
214 writer_packet = lookup_packet(trim_it, packet);
215 if (!writer_packet) {
c59fc0d5 216 BT_LOGE_STR("Failed to find existing packet.");
19ce87a4
JD
217 goto end;
218 }
219
220 g_hash_table_remove(trim_it->packet_map, packet);
221
222end:
223 return writer_packet;
224}
225
226BT_HIDDEN
227struct bt_ctf_event *trimmer_output_event(
228 struct trimmer_iterator *trim_it,
229 struct bt_ctf_event *event)
230{
9ae49d3d 231 struct bt_ctf_event_class *event_class = NULL;
19ce87a4 232 struct bt_ctf_event *writer_event = NULL;
9ae49d3d 233 struct bt_ctf_packet *packet = NULL, *writer_packet = NULL;
19ce87a4
JD
234 const char *event_name;
235 int int_ret;
236
237 event_class = bt_ctf_event_get_class(event);
c59fc0d5 238 assert(event_class);
19ce87a4
JD
239
240 event_name = bt_ctf_event_class_get_name(event_class);
19ce87a4
JD
241
242 writer_event = ctf_copy_event(trim_it->err, event, event_class, false);
243 if (!writer_event) {
c59fc0d5
JD
244 BT_LOGE("Failed to copy event: event-class-name=\"%s\", event-name=\"%s\"",
245 bt_ctf_event_class_get_name(event_class),
246 event_name);
9ae49d3d 247 goto error;
19ce87a4
JD
248 }
249
250 packet = bt_ctf_event_get_packet(event);
c59fc0d5 251 assert(packet);
19ce87a4
JD
252
253 writer_packet = lookup_packet(trim_it, packet);
254 if (!writer_packet) {
c59fc0d5 255 BT_LOGE_STR("Failed to find existing packet.");
9ae49d3d 256 goto error;
19ce87a4
JD
257 }
258 bt_get(writer_packet);
259
260 int_ret = bt_ctf_event_set_packet(writer_event, writer_packet);
261 if (int_ret < 0) {
c59fc0d5
JD
262 BT_LOGE("Failed to append event: event-class-name=\"%s\", event-name=\"%s\"",
263 bt_ctf_event_class_get_name(event_class),
264 event_name);
9ae49d3d 265 goto error;
19ce87a4
JD
266 }
267
9ae49d3d
JD
268 /* We keep the reference on the writer_event to create a notification. */
269 goto end;
19ce87a4 270
9ae49d3d
JD
271error:
272 BT_PUT(writer_event);
273end:
19ce87a4 274 bt_put(writer_packet);
19ce87a4 275 bt_put(packet);
19ce87a4 276 bt_put(event_class);
19ce87a4
JD
277 return writer_event;
278}
This page took 0.039738 seconds and 4 git commands to generate.