Avoid unnecessary inclusions in public headers
[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>
9d408fca 33#include <babeltrace/babeltrace.h>
19ce87a4
JD
34
35#include <ctfcopytrace.h>
36#include "iterator.h"
37
38static
39struct bt_ctf_packet *lookup_packet(struct trimmer_iterator *trim_it,
40 struct bt_ctf_packet *packet)
41{
42 return (struct bt_ctf_packet *) g_hash_table_lookup(
43 trim_it->packet_map,
44 (gpointer) packet);
45}
46
47static
48struct bt_ctf_packet *insert_new_packet(struct trimmer_iterator *trim_it,
49 struct bt_ctf_packet *packet,
50 struct bt_ctf_stream *stream)
51{
9ae49d3d 52 struct bt_ctf_packet *writer_packet = NULL;
387483fc 53 int ret;
19ce87a4 54
c59fc0d5 55 BT_LOGD_STR("Inserting a new packet.");
19ce87a4
JD
56 writer_packet = bt_ctf_packet_create(stream);
57 if (!writer_packet) {
c59fc0d5 58 BT_LOGE_STR("Failed to create a new packet.");
387483fc 59 goto error;
19ce87a4 60 }
19ce87a4 61
387483fc
JD
62 ret = ctf_packet_copy_header(trim_it->err, packet, writer_packet);
63 if (ret) {
c59fc0d5 64 BT_LOGE_STR("Failed to copy packet header.");
387483fc
JD
65 goto error;
66 }
67
68 g_hash_table_insert(trim_it->packet_map, (gpointer) packet,
69 writer_packet);
70 goto end;
71
72error:
73 BT_PUT(writer_packet);
19ce87a4
JD
74end:
75 return writer_packet;
76}
77
78BT_HIDDEN
79enum bt_component_status update_packet_context_field(FILE *err,
80 struct bt_ctf_packet *writer_packet,
81 const char *name, int64_t value)
82{
83 enum bt_component_status ret;
9ae49d3d
JD
84 struct bt_ctf_field *packet_context = NULL, *writer_packet_context = NULL;
85 struct bt_ctf_field_type *struct_type = NULL, *field_type = NULL;
86 struct bt_ctf_field *field = NULL, *writer_field = NULL;
19ce87a4
JD
87 int nr_fields, i, int_ret;
88
c59fc0d5 89 BT_LOGD("Updating packet context field: name=%s", name);
19ce87a4 90 packet_context = bt_ctf_packet_get_context(writer_packet);
c59fc0d5 91 assert(packet_context);
19ce87a4
JD
92
93 struct_type = bt_ctf_field_get_type(packet_context);
c59fc0d5 94 assert(struct_type);
19ce87a4
JD
95
96 writer_packet_context = bt_ctf_packet_get_context(writer_packet);
c59fc0d5 97 assert(writer_packet_context);
19ce87a4
JD
98
99 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
100 for (i = 0; i < nr_fields; i++) {
19ce87a4
JD
101 const char *field_name;
102
103 field = bt_ctf_field_structure_get_field_by_index(
104 packet_context, i);
105 if (!field) {
c59fc0d5
JD
106 BT_LOGE("Failed to get field in packet-context: field-name=\"%s\"",
107 name);
9ae49d3d 108 goto error;
19ce87a4
JD
109 }
110 if (bt_ctf_field_type_structure_get_field(struct_type,
111 &field_name, &field_type, i) < 0) {
c59fc0d5
JD
112 BT_LOGE("Failed to get field: field-name=\"%s\"",
113 field_name);
9ae49d3d 114 goto error;
19ce87a4
JD
115 }
116 if (strcmp(field_name, name)) {
9ae49d3d
JD
117 BT_PUT(field_type);
118 BT_PUT(field);
19ce87a4
JD
119 continue;
120 }
c59fc0d5
JD
121 if (bt_ctf_field_type_get_type_id(field_type) !=
122 BT_CTF_FIELD_TYPE_ID_INTEGER) {
123 BT_LOGE("Expecting an integer for this field: field-name=\"%s\"",
124 name);
9ae49d3d 125 goto error;
19ce87a4 126 }
c59fc0d5 127
19ce87a4
JD
128 writer_field = bt_ctf_field_structure_get_field(writer_packet_context,
129 field_name);
c59fc0d5 130 assert(writer_field);
19ce87a4
JD
131
132 int_ret = bt_ctf_field_unsigned_integer_set_value(writer_field, value);
c59fc0d5
JD
133 assert(int_ret == 0);
134
9ae49d3d
JD
135 BT_PUT(writer_field);
136 BT_PUT(field_type);
137 BT_PUT(field);
19ce87a4
JD
138 }
139
140 ret = BT_COMPONENT_STATUS_OK;
9ae49d3d 141 goto end;
19ce87a4 142
9ae49d3d
JD
143error:
144 bt_put(writer_field);
145 bt_put(field_type);
146 bt_put(field);
147 ret = BT_COMPONENT_STATUS_ERROR;
148end:
19ce87a4 149 bt_put(struct_type);
19ce87a4 150 bt_put(packet_context);
19ce87a4
JD
151 return ret;
152}
153
154BT_HIDDEN
155struct bt_ctf_packet *trimmer_new_packet(
156 struct trimmer_iterator *trim_it,
157 struct bt_ctf_packet *packet)
158{
9ae49d3d 159 struct bt_ctf_stream *stream = NULL;
19ce87a4
JD
160 struct bt_ctf_packet *writer_packet = NULL;
161 int int_ret;
162
163 stream = bt_ctf_packet_get_stream(packet);
c59fc0d5 164 assert(stream);
19ce87a4
JD
165
166 /*
167 * If a packet was already opened, close it and remove it from
168 * the HT.
169 */
170 writer_packet = lookup_packet(trim_it, packet);
171 if (writer_packet) {
172 g_hash_table_remove(trim_it->packet_map, packet);
9ae49d3d 173 BT_PUT(writer_packet);
19ce87a4
JD
174 }
175
176 writer_packet = insert_new_packet(trim_it, packet, stream);
177 if (!writer_packet) {
c59fc0d5 178 BT_LOGE_STR("Failed to insert new packet.");
9ae49d3d 179 goto error;
19ce87a4
JD
180 }
181 bt_get(writer_packet);
182
9877e1aa
JD
183 int_ret = ctf_packet_copy_context(trim_it->err, packet,
184 stream, writer_packet);
185 if (int_ret < 0) {
c59fc0d5 186 BT_LOGE_STR("Failed to copy packet context.");
9ae49d3d 187 goto error;
19ce87a4
JD
188 }
189
9ae49d3d
JD
190 goto end;
191
192error:
193 BT_PUT(writer_packet);
194end:
19ce87a4 195 bt_put(stream);
19ce87a4
JD
196 return writer_packet;
197}
198
199BT_HIDDEN
200struct bt_ctf_packet *trimmer_close_packet(
201 struct trimmer_iterator *trim_it,
202 struct bt_ctf_packet *packet)
203{
9ae49d3d 204 struct bt_ctf_packet *writer_packet = NULL;
19ce87a4
JD
205
206 writer_packet = lookup_packet(trim_it, packet);
207 if (!writer_packet) {
c59fc0d5 208 BT_LOGE_STR("Failed to find existing packet.");
19ce87a4
JD
209 goto end;
210 }
211
212 g_hash_table_remove(trim_it->packet_map, packet);
213
214end:
215 return writer_packet;
216}
217
218BT_HIDDEN
219struct bt_ctf_event *trimmer_output_event(
220 struct trimmer_iterator *trim_it,
221 struct bt_ctf_event *event)
222{
9ae49d3d 223 struct bt_ctf_event_class *event_class = NULL;
19ce87a4 224 struct bt_ctf_event *writer_event = NULL;
9ae49d3d 225 struct bt_ctf_packet *packet = NULL, *writer_packet = NULL;
19ce87a4
JD
226 const char *event_name;
227 int int_ret;
228
229 event_class = bt_ctf_event_get_class(event);
c59fc0d5 230 assert(event_class);
19ce87a4
JD
231
232 event_name = bt_ctf_event_class_get_name(event_class);
19ce87a4
JD
233
234 writer_event = ctf_copy_event(trim_it->err, event, event_class, false);
235 if (!writer_event) {
c59fc0d5
JD
236 BT_LOGE("Failed to copy event: event-class-name=\"%s\", event-name=\"%s\"",
237 bt_ctf_event_class_get_name(event_class),
238 event_name);
9ae49d3d 239 goto error;
19ce87a4
JD
240 }
241
242 packet = bt_ctf_event_get_packet(event);
c59fc0d5 243 assert(packet);
19ce87a4
JD
244
245 writer_packet = lookup_packet(trim_it, packet);
246 if (!writer_packet) {
c59fc0d5 247 BT_LOGE_STR("Failed to find existing packet.");
9ae49d3d 248 goto error;
19ce87a4
JD
249 }
250 bt_get(writer_packet);
251
252 int_ret = bt_ctf_event_set_packet(writer_event, writer_packet);
253 if (int_ret < 0) {
c59fc0d5
JD
254 BT_LOGE("Failed to append event: event-class-name=\"%s\", event-name=\"%s\"",
255 bt_ctf_event_class_get_name(event_class),
256 event_name);
9ae49d3d 257 goto error;
19ce87a4
JD
258 }
259
9ae49d3d
JD
260 /* We keep the reference on the writer_event to create a notification. */
261 goto end;
19ce87a4 262
9ae49d3d
JD
263error:
264 BT_PUT(writer_event);
265end:
19ce87a4 266 bt_put(writer_packet);
19ce87a4 267 bt_put(packet);
19ce87a4 268 bt_put(event_class);
19ce87a4
JD
269 return writer_event;
270}
This page took 0.043501 seconds and 4 git commands to generate.