1 | /* |
---|
2 | * |
---|
3 | * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand. |
---|
4 | * All rights reserved. |
---|
5 | * |
---|
6 | * This file is part of libtrace. |
---|
7 | * |
---|
8 | * This code has been developed by the University of Waikato WAND |
---|
9 | * research group. For further information please see http://www.wand.net.nz/ |
---|
10 | * |
---|
11 | * libtrace is free software; you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Lesser General Public License as published by |
---|
13 | * the Free Software Foundation; either version 3 of the License, or |
---|
14 | * (at your option) any later version. |
---|
15 | * |
---|
16 | * libtrace is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | * GNU Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | * |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | /** @file |
---|
29 | * |
---|
30 | * @brief Header file containing definitions for structures and functions |
---|
31 | * related to the parallel framework |
---|
32 | * |
---|
33 | * @author Richard Sanger |
---|
34 | * |
---|
35 | * @version 4.0.0 |
---|
36 | * |
---|
37 | * The parallel libtrace framework is a replacement to the libtrace framework |
---|
38 | * that allows packet processing workload to be spread over multiple threads. |
---|
39 | * It can also take advantage of native parallelism in the packet capture |
---|
40 | * source. |
---|
41 | */ |
---|
42 | |
---|
43 | #ifndef LIBTRACE_PARALLEL_H |
---|
44 | #define LIBTRACE_PARALLEL_H |
---|
45 | |
---|
46 | #include "libtrace.h" |
---|
47 | #include <stdio.h> |
---|
48 | |
---|
49 | #ifdef __cplusplus |
---|
50 | extern "C" { |
---|
51 | #endif |
---|
52 | |
---|
53 | typedef struct libtrace_result_t libtrace_result_t; |
---|
54 | |
---|
55 | /** |
---|
56 | * A collection of types for convenience used in place of a |
---|
57 | * simple void* to allow any type of data to be stored and passed |
---|
58 | * around easily. |
---|
59 | * |
---|
60 | * This is expected to be 8 bytes in length. |
---|
61 | */ |
---|
62 | typedef union { |
---|
63 | /* Pointers */ |
---|
64 | void *ptr; |
---|
65 | libtrace_packet_t *pkt; |
---|
66 | libtrace_result_t *res; |
---|
67 | |
---|
68 | /* C99 Integer types */ |
---|
69 | /* NOTE: Standard doesn't require 64-bit |
---|
70 | * but x32 and x64 gcc does */ |
---|
71 | int64_t sint64; |
---|
72 | uint64_t uint64; |
---|
73 | |
---|
74 | uint32_t uint32s[2]; |
---|
75 | int32_t sint32s[2]; |
---|
76 | uint32_t uint32; |
---|
77 | int32_t sint32; |
---|
78 | |
---|
79 | uint16_t uint16s[4]; |
---|
80 | int16_t sint16s[4]; |
---|
81 | uint16_t uint16; |
---|
82 | int16_t sint16; |
---|
83 | |
---|
84 | uint8_t uint8s[8]; |
---|
85 | int8_t sint8s[8]; |
---|
86 | uint8_t uint8; |
---|
87 | int8_t sint8; |
---|
88 | |
---|
89 | size_t size; |
---|
90 | |
---|
91 | /* C basic types - we cannot be certain of the size */ |
---|
92 | int sint; |
---|
93 | unsigned int uint; |
---|
94 | |
---|
95 | signed char schars[8]; |
---|
96 | unsigned char uchars[8]; |
---|
97 | signed char schar; |
---|
98 | unsigned char uchar; |
---|
99 | |
---|
100 | /* Real numbers */ |
---|
101 | float rfloat; |
---|
102 | double rdouble; |
---|
103 | } libtrace_generic_t; |
---|
104 | ct_assert(sizeof(libtrace_generic_t) == 8); |
---|
105 | |
---|
106 | /** |
---|
107 | * Structure describing a message that can be sent to a libtrace thread. |
---|
108 | */ |
---|
109 | typedef struct libtrace_message_t { |
---|
110 | int code; /**< The message code, as defined in enum libtrace_messages */ |
---|
111 | libtrace_generic_t data; /**< Additional data related to the message */ |
---|
112 | libtrace_thread_t *sender; /**< The thread that sent the message */ |
---|
113 | } libtrace_message_t; |
---|
114 | |
---|
115 | /** Structure holding information about a result */ |
---|
116 | struct libtrace_result_t { |
---|
117 | uint64_t key; /**< The unique key for the result */ |
---|
118 | libtrace_generic_t value; /**< The result value itself */ |
---|
119 | int type; /**< Describes the type of result, see enum result_types */ |
---|
120 | }; |
---|
121 | |
---|
122 | /** The libtrace_messages enum |
---|
123 | * All libtrace messages are defined and documented here. |
---|
124 | * |
---|
125 | * Some messages can be sent to control the internal behaviour of the library |
---|
126 | * while others are used to trigger the user-defined callback functions. |
---|
127 | * If a user wishes to send their own custom messages, they should use |
---|
128 | * numbers greater than MESSAGE_USER (1000). |
---|
129 | * |
---|
130 | * @note Some messages are for internal use only |
---|
131 | */ |
---|
132 | enum libtrace_messages { |
---|
133 | /** A libtrace packet is ready, this will trigger the packet callback |
---|
134 | * for the processing threads. |
---|
135 | */ |
---|
136 | MESSAGE_PACKET, |
---|
137 | |
---|
138 | /** A libtrace meta packet is ready, this will trigger the meta packet |
---|
139 | * callback for the processing threads. |
---|
140 | */ |
---|
141 | MESSAGE_META_PACKET, |
---|
142 | |
---|
143 | /** A libtrace result is ready, this will trigger the result callback |
---|
144 | * for the reporter thread. |
---|
145 | */ |
---|
146 | MESSAGE_RESULT, |
---|
147 | |
---|
148 | /** This message is sent to each thread when it first starts and will |
---|
149 | * trigger the starting callback for the processing and reporter |
---|
150 | * threads. A starting message is sent when trace_pstart is called |
---|
151 | * for the first time on a trace. |
---|
152 | */ |
---|
153 | MESSAGE_STARTING, |
---|
154 | |
---|
155 | /** This message is sent to each thread when the thread ends and will |
---|
156 | * trigger the stopping callback for the processing and reporter |
---|
157 | * threads. |
---|
158 | */ |
---|
159 | MESSAGE_STOPPING, |
---|
160 | |
---|
161 | /** This message is sent to each thread when the thread transitions |
---|
162 | * from a paused state to a running state. It will trigger the |
---|
163 | * resuming callback for the processing and reporter threads. |
---|
164 | * |
---|
165 | * A resuming message is sent whenever trace_pstart is called on a |
---|
166 | * trace (including the first time the trace is started). |
---|
167 | */ |
---|
168 | MESSAGE_RESUMING, |
---|
169 | |
---|
170 | /** This message is sent to each thread when the thread transitions |
---|
171 | * into a paused state from a running state. It will trigger the |
---|
172 | * pausing callback for the processing and reporter threads. |
---|
173 | * |
---|
174 | * A pausing message is sent whenever trace_ppause is called on a |
---|
175 | * trace. It will also be sent when a trace is stopped, as all traces |
---|
176 | * are implicitly paused before they stop. |
---|
177 | */ |
---|
178 | MESSAGE_PAUSING, |
---|
179 | |
---|
180 | /** An internal message for forcing another thread to pause. Do not |
---|
181 | * use this in user-defined callbacks! |
---|
182 | */ |
---|
183 | MESSAGE_DO_PAUSE, |
---|
184 | |
---|
185 | /** An internal message for forcing another thread to stop. Do not |
---|
186 | * use this in user-defined callbacks! |
---|
187 | */ |
---|
188 | MESSAGE_DO_STOP, |
---|
189 | |
---|
190 | /** This message is sent to each processing thread as soon as the first |
---|
191 | * packet has been seen by any of the processing threads. This will |
---|
192 | * trigger the first_packet callback for the processing threads, |
---|
193 | * allowing the threads to perform any initialisation required based |
---|
194 | * on the properties of the first packet (e.g. the timestamp). |
---|
195 | * |
---|
196 | * Threads should use trace_get_first_packet() to access the packet |
---|
197 | * that triggered this message. |
---|
198 | * |
---|
199 | * @note Upon pausing and restarting a trace, this message will be |
---|
200 | * sent again when the first new packet is encountered. |
---|
201 | */ |
---|
202 | MESSAGE_FIRST_PACKET, |
---|
203 | |
---|
204 | /** An internal message for notifying the reporter thread that more |
---|
205 | * results are available. |
---|
206 | * |
---|
207 | * Do not use this in user-defined callbacks -- call |
---|
208 | * trace_post_reporter() instead. |
---|
209 | */ |
---|
210 | MESSAGE_POST_REPORTER, |
---|
211 | |
---|
212 | /** Sent to per-packet threads periodically after the configured time |
---|
213 | * interval has passed. |
---|
214 | * |
---|
215 | * This is sent out-of-band with respect to packets and as a result |
---|
216 | * can appear after a packet with an later time-stamp, or before one |
---|
217 | * with an earlier time-stamp. |
---|
218 | * |
---|
219 | * @param data data.uint64 holds the system time-stamp in the |
---|
220 | * erf format |
---|
221 | * @param sender should be ignored |
---|
222 | */ |
---|
223 | |
---|
224 | /** This message is sent to the processing threads periodically, after |
---|
225 | * the configured time interval has passed. This message will |
---|
226 | * trigger the tick_interval callback function for the processing |
---|
227 | * threads. |
---|
228 | * |
---|
229 | * This message is sent out-of-band relative to packet messages and |
---|
230 | * therefore can appear after a packet with a later timestamp or |
---|
231 | * before a packet with an earlier timestamp. |
---|
232 | */ |
---|
233 | MESSAGE_TICK_INTERVAL, |
---|
234 | |
---|
235 | /** Sent to per-packet threads once the configured number of packets |
---|
236 | * are read from a trace. |
---|
237 | * |
---|
238 | * This are sent in-band with respect to packets such that all |
---|
239 | * threads will see it between the same packets. |
---|
240 | * |
---|
241 | * @param data data.uint64 holds the number of packets seen so far across all threads |
---|
242 | * @param sender Set to the current per-packet thread |
---|
243 | */ |
---|
244 | /** This message is sent to the processing threads periodically, after |
---|
245 | * the configured number of packets have been read from the input |
---|
246 | * trace. This message will trigger the tick_count callback function |
---|
247 | * for the processing threads. |
---|
248 | * |
---|
249 | * This message is sent in-band relative to packet messages and |
---|
250 | * will always appear in the right place relative to the other packets |
---|
251 | * observed by the thread. |
---|
252 | */ |
---|
253 | MESSAGE_TICK_COUNT, |
---|
254 | |
---|
255 | /** All message codes at or above this value represent custom |
---|
256 | * user-defined messages and will trigger the usermessage callback |
---|
257 | * for the processing threads. |
---|
258 | */ |
---|
259 | MESSAGE_USER = 1000 |
---|
260 | }; |
---|
261 | |
---|
262 | /** The hasher types that are available to libtrace applications. |
---|
263 | * These can be selected using trace_set_hasher(). |
---|
264 | */ |
---|
265 | enum hasher_types { |
---|
266 | /** Balance load across per-packet threads as best as possible, i.e |
---|
267 | * the program does not care which thread sees a given packet. This |
---|
268 | * will be implemented using a hash or round robin, depending on the |
---|
269 | * format and libtrace configuration. |
---|
270 | */ |
---|
271 | HASHER_BALANCE, |
---|
272 | |
---|
273 | /** Use a hash which is bi-directional for TCP and UDP flows, such that |
---|
274 | * packets with the same 5-tuple are sent to the same processing thread. |
---|
275 | * All non TCP/UDP packets will be sent to the same thread. |
---|
276 | * |
---|
277 | * @note it is possible that UDP packets may not be spread across |
---|
278 | * processing threads, depending upon the format support. In this case |
---|
279 | * they would be directed to a single thread. |
---|
280 | */ |
---|
281 | HASHER_BIDIRECTIONAL, |
---|
282 | |
---|
283 | /** Use a hash which is uni-directional across TCP and UDP flows, such |
---|
284 | * that the opposing directions of the same 5-tuple may end up on |
---|
285 | * different processing threads. |
---|
286 | * Otherwise this is identical to HASHER_BIDIRECTIONAL. |
---|
287 | */ |
---|
288 | HASHER_UNIDIRECTIONAL, |
---|
289 | |
---|
290 | /** |
---|
291 | * This value indicates that the hasher is a custom user-defined |
---|
292 | * function. |
---|
293 | */ |
---|
294 | HASHER_CUSTOM |
---|
295 | }; |
---|
296 | |
---|
297 | typedef struct libtrace_info_t { |
---|
298 | /** |
---|
299 | * True if a live format (i.e. packets have to be trace-time). |
---|
300 | * Otherwise false, indicating packets can be read as fast |
---|
301 | * as possible from the format. |
---|
302 | */ |
---|
303 | bool live; |
---|
304 | |
---|
305 | /** |
---|
306 | * The maximum number of threads supported by a parallel trace. 1 |
---|
307 | * if parallel support is not native (in this case libtrace will |
---|
308 | * simulate an unlimited number of threads), -1 means unlimited and 0 |
---|
309 | * unknown. |
---|
310 | */ |
---|
311 | int max_threads; |
---|
312 | |
---|
313 | /* TODO hash fn supported list */ |
---|
314 | |
---|
315 | /* TODO consider time/clock details?? */ |
---|
316 | } libtrace_info_t; |
---|
317 | |
---|
318 | typedef struct libtrace_combine libtrace_combine_t; |
---|
319 | /** |
---|
320 | * The methods we use to combine the results from multiple processing |
---|
321 | * threads into a single output. Users can write their own combiners, but |
---|
322 | * we strongly recommend that you use one of the provided combiners. |
---|
323 | * |
---|
324 | */ |
---|
325 | struct libtrace_combine { |
---|
326 | |
---|
327 | /** |
---|
328 | * Called at the start of the trace to allow data-structures |
---|
329 | * to be initialised and allow functions to be swapped if appropriate. |
---|
330 | * |
---|
331 | * Also factors such as whether the trace is live or not can |
---|
332 | * be used to determine the functions used. |
---|
333 | * @return 0 if successful, -1 if an error occurs |
---|
334 | */ |
---|
335 | int (*initialise)(libtrace_t *,libtrace_combine_t *); |
---|
336 | |
---|
337 | /** |
---|
338 | * Called when the trace ends, clean up any memory allocated |
---|
339 | * by the initialise function. |
---|
340 | */ |
---|
341 | void (*destroy)(libtrace_t *, libtrace_combine_t *); |
---|
342 | |
---|
343 | /** |
---|
344 | * Receive a result from a processing thread. Most implementations |
---|
345 | * of this function will push the result into an appropriate |
---|
346 | * queue. If this is NULL, the result will automatically be pushed |
---|
347 | * to the reporter thread. |
---|
348 | */ |
---|
349 | void (*publish)(libtrace_t *, int thread_id, libtrace_combine_t *, libtrace_result_t *); |
---|
350 | |
---|
351 | /** |
---|
352 | * Read as many results as possible from the trace. Each result |
---|
353 | * that is read should cause a MESSAGE_RESULT to be sent to the |
---|
354 | * reporter thread. |
---|
355 | * |
---|
356 | * THIS SHOULD BE NON-BLOCKING AND READ AS MANY AS POSSIBLE! |
---|
357 | * If publish is NULL, this probably should be NULL as it will not be |
---|
358 | * called in that case. |
---|
359 | */ |
---|
360 | void (*read)(libtrace_t *, libtrace_combine_t *); |
---|
361 | |
---|
362 | /** |
---|
363 | * Called when the trace is finished to flush the final |
---|
364 | * results to the reporter thread. Any leftover results should |
---|
365 | * cause a MESSAGE_RESULT to be sent to the reporter thread. |
---|
366 | * |
---|
367 | * There may be no results, in which case this function should |
---|
368 | * just return. |
---|
369 | * |
---|
370 | * Libtrace state: |
---|
371 | * This function will be called from the reporter thread. |
---|
372 | * No processing threads will be running, i.e. you can assume that |
---|
373 | * publish will not be called again. |
---|
374 | * |
---|
375 | * If publish is NULL, this probably should be NULL as it will not be |
---|
376 | * called in that case. |
---|
377 | */ |
---|
378 | void (*read_final)(libtrace_t *, libtrace_combine_t *); |
---|
379 | |
---|
380 | /** |
---|
381 | * Pause must make sure any queued results that contain packets are |
---|
382 | * safe. See libtrace_make_result_safe() for more details on what it |
---|
383 | * means for a result to be safe. |
---|
384 | * This function should be NULL if publish is NULL. |
---|
385 | */ |
---|
386 | void (*pause)(libtrace_t *, libtrace_combine_t *); |
---|
387 | |
---|
388 | /** |
---|
389 | * Data storage for all the combiner threads |
---|
390 | */ |
---|
391 | void *queues; |
---|
392 | |
---|
393 | /** The last counter tick that we saw, so we can avoid duplicating |
---|
394 | * any ticks that are published. |
---|
395 | */ |
---|
396 | uint64_t last_count_tick; |
---|
397 | |
---|
398 | /** The last timestamp tick that we saw, so we can avoid duplicating |
---|
399 | * any ticks that are published. |
---|
400 | */ |
---|
401 | uint64_t last_ts_tick; |
---|
402 | |
---|
403 | /** |
---|
404 | * Configuration options, what this does is up to the combiner |
---|
405 | * chosen. |
---|
406 | */ |
---|
407 | libtrace_generic_t configuration; |
---|
408 | }; |
---|
409 | |
---|
410 | /** |
---|
411 | * The definition for a hasher function, allowing matching packets to be |
---|
412 | * directed to the correct thread for processing. |
---|
413 | * |
---|
414 | * @param packet The packet to be hashed. |
---|
415 | * @param data A void pointer which can contain additional information, |
---|
416 | * such as configuration for the hasher function. |
---|
417 | * |
---|
418 | * @return The id of the thread that should receive this packet. |
---|
419 | */ |
---|
420 | typedef uint64_t (*fn_hasher)(const libtrace_packet_t* packet, void *data); |
---|
421 | |
---|
422 | |
---|
423 | /** Start or restart an input trace in the parallel libtrace framework. |
---|
424 | * |
---|
425 | * @param libtrace The input trace to start |
---|
426 | * @param global_blob Global data related to this trace. This may be NULL if |
---|
427 | * no global data is required. |
---|
428 | * @param per_packet_cbs A set of user supplied functions to be called in |
---|
429 | * response to messages that are observed by the processing threads. |
---|
430 | * @param reporter_cbs A set of user supplied functions to be called in |
---|
431 | * response to messages being seen by the reporter thread. |
---|
432 | * Optional if NULL, the reporter thread will not be started. |
---|
433 | * @return 0 on success, otherwise -1 to indicate an error has occurred |
---|
434 | * |
---|
435 | * This can also be used to restart an existing parallel trace, |
---|
436 | * that has previously been paused using trace_ppause(). |
---|
437 | * In this case global_blob, per_packet_cbs and reporter_cbs will only be |
---|
438 | * updated if they are non-null. Otherwise their previous values will be |
---|
439 | * maintained. |
---|
440 | * |
---|
441 | */ |
---|
442 | DLLEXPORT int trace_pstart(libtrace_t *libtrace, void* global_blob, |
---|
443 | libtrace_callback_set_t *per_packet_cbs, |
---|
444 | libtrace_callback_set_t *reporter_cbs); |
---|
445 | |
---|
446 | /** |
---|
447 | * The starting callback for a processing or reporting thread. Use this |
---|
448 | * callback to allocate and initialise any thread-local storage that you |
---|
449 | * would like to be available in other callbacks. |
---|
450 | * |
---|
451 | * @param libtrace The parallel trace. |
---|
452 | * @param t The thread that has just started. |
---|
453 | * @param global The global storage for the trace. |
---|
454 | * |
---|
455 | * @return The returned value is stored against the thread's local storage. |
---|
456 | * This is typically passed as the 'tls' argument to other callbacks. |
---|
457 | */ |
---|
458 | typedef void* (*fn_cb_starting)(libtrace_t *libtrace, |
---|
459 | libtrace_thread_t *t, |
---|
460 | void *global); |
---|
461 | |
---|
462 | /** |
---|
463 | * A callback function for any message that does not require any specific |
---|
464 | * data, e.g. stopping, pausing, or resuming callbacks. |
---|
465 | * |
---|
466 | * @param libtrace The parallel trace. |
---|
467 | * @param t The thread that is running. |
---|
468 | * @param global The global storage. |
---|
469 | * @param tls The thread local storage. |
---|
470 | */ |
---|
471 | typedef void (*fn_cb_dataless)(libtrace_t *libtrace, |
---|
472 | libtrace_thread_t *t, |
---|
473 | void *global, |
---|
474 | void *tls); |
---|
475 | |
---|
476 | /** |
---|
477 | * A callback function for a first packet message seen by a processing thread. |
---|
478 | * @param libtrace The parallel trace. |
---|
479 | * @param t The thread that is running. |
---|
480 | * @param global The global storage. |
---|
481 | * @param tls The thread local storage. |
---|
482 | * @param sender The thread that saw the first packet. |
---|
483 | */ |
---|
484 | typedef void (*fn_cb_first_packet)(libtrace_t *libtrace, |
---|
485 | libtrace_thread_t *t, |
---|
486 | void *global, |
---|
487 | void *tls, |
---|
488 | libtrace_thread_t *sender); |
---|
489 | |
---|
490 | /** |
---|
491 | * A callback function for handling a tick message within a processing thread. |
---|
492 | * |
---|
493 | * @param libtrace The parallel trace. |
---|
494 | * @param t The thread that is running. |
---|
495 | * @param global The global storage. |
---|
496 | * @param tls The thread local storage. |
---|
497 | * @param uint64_t The value of the tick; either a timestamp or packet count |
---|
498 | * depending on the type of tick. |
---|
499 | */ |
---|
500 | typedef void (*fn_cb_tick)(libtrace_t *libtrace, |
---|
501 | libtrace_thread_t *t, |
---|
502 | void *global, |
---|
503 | void *tls, |
---|
504 | uint64_t order); |
---|
505 | |
---|
506 | /** |
---|
507 | * A callback function triggered when a processing thread receives a packet. |
---|
508 | * |
---|
509 | * @param libtrace The parallel trace. |
---|
510 | * @param t The thread that is running |
---|
511 | * @param global The global storage. |
---|
512 | * @param tls The thread local storage. |
---|
513 | * @param packet The packet to be processed. |
---|
514 | * |
---|
515 | * @return either the packet itself if it is not being published as a result |
---|
516 | * or NULL otherwise. If returning NULL, it is the user's responsibility |
---|
517 | * to ensure the packet is freed when the reporter thread is finished with it. |
---|
518 | */ |
---|
519 | typedef libtrace_packet_t* (*fn_cb_packet)(libtrace_t *libtrace, |
---|
520 | libtrace_thread_t *t, |
---|
521 | void *global, |
---|
522 | void *tls, |
---|
523 | libtrace_packet_t *packet); |
---|
524 | |
---|
525 | /** |
---|
526 | * A callback function triggered when a processing thread receives a meta packet. |
---|
527 | * |
---|
528 | * @param libtrace The parallel trace. |
---|
529 | * @param t The thread that is running |
---|
530 | * @param global The global storage. |
---|
531 | * @param tls The thread local storage. |
---|
532 | * @param packet The packet to be processed. |
---|
533 | * |
---|
534 | * @return either the packet itself if it is not being published as a result |
---|
535 | * or NULL otherwise. If returning NULL, it is the user's responsibility |
---|
536 | * to ensure the packet is freed when the reporter thread is finished with it. |
---|
537 | */ |
---|
538 | typedef libtrace_packet_t* (*fn_cb_meta_packet)(libtrace_t *libtrace, |
---|
539 | libtrace_thread_t *t, |
---|
540 | void *global, |
---|
541 | void *tls, |
---|
542 | libtrace_packet_t *packet); |
---|
543 | |
---|
544 | /** |
---|
545 | * Callback for handling a result message. Should only be required by the |
---|
546 | * reporter thread. |
---|
547 | * |
---|
548 | * @param libtrace The parallel trace. |
---|
549 | * @param sender The thread that generated this result. |
---|
550 | * @param global The global storage. |
---|
551 | * @param tls The thread local storage. |
---|
552 | * @param result The result associated with the message. |
---|
553 | * |
---|
554 | */ |
---|
555 | typedef void (*fn_cb_result)(libtrace_t *libtrace, libtrace_thread_t *sender, |
---|
556 | void *global, void *tls, libtrace_result_t *result); |
---|
557 | |
---|
558 | |
---|
559 | /** |
---|
560 | * Callback for handling any user-defined message types. This will handle |
---|
561 | * any messages with a type >= MESSAGE_USER. |
---|
562 | * |
---|
563 | * @param libtrace The parallel trace. |
---|
564 | * @param t The thread. |
---|
565 | * @param global The global storage. |
---|
566 | * @param tls The thread local storage. |
---|
567 | * @param mesg The code identifying the message type. |
---|
568 | * @param data The data associated with the message. |
---|
569 | * |
---|
570 | */ |
---|
571 | typedef void (*fn_cb_usermessage) (libtrace_t *libtrace, libtrace_thread_t *t, |
---|
572 | void *global, void *tls, int mesg, libtrace_generic_t data, |
---|
573 | libtrace_thread_t *sender); |
---|
574 | |
---|
575 | |
---|
576 | /** |
---|
577 | * Registers a starting callback against a callback set. |
---|
578 | * |
---|
579 | * @param cbset The callback set. |
---|
580 | * @param handler The starting callback function. |
---|
581 | * @return 0 if successful, -1 otherwise. |
---|
582 | */ |
---|
583 | DLLEXPORT int trace_set_starting_cb(libtrace_callback_set_t *cbset, |
---|
584 | fn_cb_starting handler); |
---|
585 | |
---|
586 | /** |
---|
587 | * Registers a stopping callback against a callback set. |
---|
588 | * |
---|
589 | * @param cbset The callback set. |
---|
590 | * @param handler The stopping callback function. |
---|
591 | * @return 0 if successful, -1 otherwise. |
---|
592 | */ |
---|
593 | DLLEXPORT int trace_set_stopping_cb(libtrace_callback_set_t *cbset, |
---|
594 | fn_cb_dataless handler); |
---|
595 | |
---|
596 | /** |
---|
597 | * Registers a resuming callback against a callback set. |
---|
598 | * |
---|
599 | * @param cbset The callback set. |
---|
600 | * @param handler The resuming callback function. |
---|
601 | * @return 0 if successful, -1 otherwise. |
---|
602 | */ |
---|
603 | DLLEXPORT int trace_set_resuming_cb(libtrace_callback_set_t *cbset, |
---|
604 | fn_cb_dataless handler); |
---|
605 | |
---|
606 | /** |
---|
607 | * Registers a pausing callback against a callback set. |
---|
608 | * |
---|
609 | * @param cbset The callback set. |
---|
610 | * @param handler The pausing callback function. |
---|
611 | * @return 0 if successful, -1 otherwise. |
---|
612 | */ |
---|
613 | DLLEXPORT int trace_set_pausing_cb(libtrace_callback_set_t *cbset, |
---|
614 | fn_cb_dataless handler); |
---|
615 | |
---|
616 | /** |
---|
617 | * Registers a packet callback against a callback set. |
---|
618 | * |
---|
619 | * @param cbset The callback set. |
---|
620 | * @param handler The packet callback function. |
---|
621 | * @return 0 if successful, -1 otherwise. |
---|
622 | */ |
---|
623 | DLLEXPORT int trace_set_packet_cb(libtrace_callback_set_t *cbset, |
---|
624 | fn_cb_packet handler); |
---|
625 | |
---|
626 | /** |
---|
627 | * Registers a meta packet callback against a callback set. |
---|
628 | * |
---|
629 | * @param cbset The callback set. |
---|
630 | * @param handler The meta packet callback funtion. |
---|
631 | * @return 0 if successful, -1 otherwise. |
---|
632 | */ |
---|
633 | DLLEXPORT int trace_set_meta_packet_cb(libtrace_callback_set_t *cbset, |
---|
634 | fn_cb_meta_packet handler); |
---|
635 | |
---|
636 | /** |
---|
637 | * Registers a first packet callback against a callback set. |
---|
638 | * |
---|
639 | * @param cbset The callback set. |
---|
640 | * @param handler The first packet callback function. |
---|
641 | * @return 0 if successful, -1 otherwise. |
---|
642 | */ |
---|
643 | DLLEXPORT int trace_set_first_packet_cb(libtrace_callback_set_t *cbset, |
---|
644 | fn_cb_first_packet handler); |
---|
645 | |
---|
646 | /** |
---|
647 | * Registers a result callback against a callback set. |
---|
648 | * |
---|
649 | * @param cbset The callback set. |
---|
650 | * @param handler The result callback function. |
---|
651 | * @return 0 if successful, -1 otherwise. |
---|
652 | */ |
---|
653 | DLLEXPORT int trace_set_result_cb(libtrace_callback_set_t *cbset, |
---|
654 | fn_cb_result handler); |
---|
655 | |
---|
656 | /** |
---|
657 | * Registers a tick counter callback against a callback set. |
---|
658 | * |
---|
659 | * @param cbset The callback set. |
---|
660 | * @param handler The tick callback function. |
---|
661 | * @return 0 if successful, -1 otherwise. |
---|
662 | */ |
---|
663 | DLLEXPORT int trace_set_tick_count_cb(libtrace_callback_set_t *cbset, |
---|
664 | fn_cb_tick handler); |
---|
665 | |
---|
666 | /** |
---|
667 | * Registers a tick interval callback against a callback set. |
---|
668 | * |
---|
669 | * @param cbset The callback set. |
---|
670 | * @param handler The tick callback function. |
---|
671 | * @return 0 if successful, -1 otherwise. |
---|
672 | */ |
---|
673 | DLLEXPORT int trace_set_tick_interval_cb(libtrace_callback_set_t *cbset, |
---|
674 | fn_cb_tick handler); |
---|
675 | |
---|
676 | /** |
---|
677 | * Registers a callback for custom user messages against a callback set. |
---|
678 | * |
---|
679 | * @param cbset The callback set. |
---|
680 | * @param handler The user message callback function. |
---|
681 | * @return 0 if successful, -1 otherwise. |
---|
682 | */ |
---|
683 | DLLEXPORT int trace_set_user_message_cb(libtrace_callback_set_t *cbset, |
---|
684 | fn_cb_usermessage handler); |
---|
685 | |
---|
686 | /** Create a callback set that can be used to define callbacks for parallel |
---|
687 | * libtrace threads. |
---|
688 | * |
---|
689 | * @return A pointer to a freshly allocated callback set. |
---|
690 | */ |
---|
691 | DLLEXPORT libtrace_callback_set_t *trace_create_callback_set(void); |
---|
692 | |
---|
693 | /** Destroys a callback set, freeing up any resources it was using. |
---|
694 | * |
---|
695 | * @param cbset The callback set to be destroyed. |
---|
696 | */ |
---|
697 | DLLEXPORT void trace_destroy_callback_set(libtrace_callback_set_t *cbset); |
---|
698 | |
---|
699 | |
---|
700 | /** Pauses a trace previously started with trace_pstart() |
---|
701 | * |
---|
702 | * @param libtrace The parallel trace to be paused |
---|
703 | * @return 0 on success, otherwise -1 to indicate an error has occurred |
---|
704 | * |
---|
705 | */ |
---|
706 | DLLEXPORT int trace_ppause(libtrace_t *libtrace); |
---|
707 | |
---|
708 | /** Stops a parallel trace, causing all threads to exit as if an EOF |
---|
709 | * has occurred. This replaces trace_interrupt(), allowing |
---|
710 | * a specified trace to be stopped. |
---|
711 | * |
---|
712 | * @param libtrace The parallel trace to be stopped |
---|
713 | * @return 0 on success, otherwise -1 to indicate an error has occurred |
---|
714 | * |
---|
715 | * Ideally, this should only be called by the main thread (i.e. from a signal |
---|
716 | * handler) but it can be called from within a reporter thread reasonably |
---|
717 | * safely. |
---|
718 | * |
---|
719 | */ |
---|
720 | DLLEXPORT int trace_pstop(libtrace_t *libtrace); |
---|
721 | |
---|
722 | /** Waits for a trace to finish and all threads to join. |
---|
723 | * |
---|
724 | * @param trace The parallel trace |
---|
725 | * |
---|
726 | * Waits for a trace to finish, whether this be due to |
---|
727 | * an error occurring, an EOF or trace_pstop. |
---|
728 | * |
---|
729 | */ |
---|
730 | DLLEXPORT void trace_join(libtrace_t * trace); |
---|
731 | |
---|
732 | |
---|
733 | /** |
---|
734 | * @name Parallel Configuration |
---|
735 | * |
---|
736 | * These methods provide a way to configure the parallel libtrace library. |
---|
737 | * |
---|
738 | * Many of these options are typically unneeded by most applications as they |
---|
739 | * control tuning aspects of the library and are more useful to the |
---|
740 | * end user. |
---|
741 | * |
---|
742 | * To allow the end user to change this configuration libtrace will search for |
---|
743 | * three environment variables and apply them to the configuration in the |
---|
744 | * following order. Such that the first has the lowest priority. |
---|
745 | * |
---|
746 | * 1. LIBTRACE_CONF, The global environment configuration |
---|
747 | * 2. LIBTRACE_CONF_<FORMAT>, Applied to a given format |
---|
748 | * 3. LIBTRACE_CONF_<FORMAT_URI>, Applied the specified trace |
---|
749 | * |
---|
750 | * E.g. |
---|
751 | * - int:eth0 would match LIBTRACE_CONF, LIBTRACE_CONF_INT, |
---|
752 | * LIBTRACE_CONF_INT_ETH0 |
---|
753 | * - dag:/dev/dag0,0 would match LIBTRACE_CONF, LIBTRACE_CONF_DAG, |
---|
754 | * LIBTRACE_CONF_DAG__DEV_DAG0_0 |
---|
755 | * - test.erf would match LIBTRACE_CONF, LIBTRACE_CONF_ERF, |
---|
756 | * LIBTRACE_CONF_ERF_TEST_ERF |
---|
757 | * |
---|
758 | * @note All environment variables names MUST only contain |
---|
759 | * [A-Z], [0-9] and [_] (underscore). Any characters |
---|
760 | * outside of this range should be capitalised if possible or replaced with an |
---|
761 | * underscore. |
---|
762 | * @{ |
---|
763 | */ |
---|
764 | |
---|
765 | /** Set the maximum number of perpkt threads to use in a trace. |
---|
766 | * |
---|
767 | * Only valid on a new trace, that has not be started. Once started |
---|
768 | * the number of threads cannot be changed without destroying the trace. |
---|
769 | * |
---|
770 | * @param[in] trace The parallel input trace |
---|
771 | * @param[in] nb The number of threads to use. If set to 0, libtrace will |
---|
772 | * try to auto-detect how many threads it can use. |
---|
773 | * @return 0 if successful otherwise -1 |
---|
774 | */ |
---|
775 | DLLEXPORT int trace_set_perpkt_threads(libtrace_t *trace, int nb); |
---|
776 | |
---|
777 | /** Set the interval between tick messages in milliseconds. |
---|
778 | * |
---|
779 | * @param[in] trace The parallel input trace |
---|
780 | * @param[in] millisec The interval in milliseconds. If 0 this is disabled |
---|
781 | * [default]. |
---|
782 | * @return 0 if successful, otherwise -1. |
---|
783 | * |
---|
784 | * When enabled, MESSAGE_TICK_INTERVAL will be sent every tick interval to all |
---|
785 | * processing threads. This allows results to be published even in cases where |
---|
786 | * new packets are not being directed to a processing thread, while still |
---|
787 | * maintaining order etc. |
---|
788 | * |
---|
789 | * @see MESSAGE_TICK_INTERVAL, trace_set_tick_count() |
---|
790 | */ |
---|
791 | DLLEXPORT int trace_set_tick_interval(libtrace_t *trace, size_t millisec); |
---|
792 | |
---|
793 | /** Set the number of packets to be read between tick messages. |
---|
794 | * |
---|
795 | * @param[in] trace The parallel input trace |
---|
796 | * @param[in] count The tick count. If 0 this is disabled [default]. |
---|
797 | * @return 0 if successful otherwise -1 |
---|
798 | * |
---|
799 | * When enabled, MESSAGE_TICK_COUNT will be sent to all processing threads |
---|
800 | * after every 'count' packets have been read from the trace. This allows |
---|
801 | * results to be published even in cases where new packets are not being |
---|
802 | * directed to a processing thread, while still maintaining order etc. |
---|
803 | * |
---|
804 | * @see MESSAGE_TICK_COUNT, trace_set_tick_interval() |
---|
805 | */ |
---|
806 | DLLEXPORT int trace_set_tick_count(libtrace_t *trace, size_t count); |
---|
807 | |
---|
808 | /** |
---|
809 | * Delays packets so they are played back in trace-time rather than as fast |
---|
810 | * as possible (real-time). |
---|
811 | * |
---|
812 | * @param trace A parallel input trace |
---|
813 | * @param tracetime If true packets are released with time spacing that matches |
---|
814 | * the original trace. Otherwise packets are read as fast as possible. |
---|
815 | * @return 0 if successful otherwise -1 |
---|
816 | */ |
---|
817 | DLLEXPORT int trace_set_tracetime(libtrace_t *trace, bool tracetime); |
---|
818 | |
---|
819 | /** Sets the maximum size of the freelist used to store empty packets |
---|
820 | * and their memory buffers. |
---|
821 | * |
---|
822 | * @param trace A parallel input trace |
---|
823 | * @param size The number of empty packets to cache in memory. Set to the |
---|
824 | * default, 0, to autoconfigure this value. |
---|
825 | * @return 0 if successful otherwise -1 |
---|
826 | * |
---|
827 | * Internally libtrace maintains a buffer of packet structures which |
---|
828 | * includes a cache per thread and a shared main pool. This option configures |
---|
829 | * the size of the main pool. If an application is not passing packets |
---|
830 | * through to the reporter thread, i.e. the packet callback always returns |
---|
831 | * the packet, then the main pool is not used. |
---|
832 | * |
---|
833 | * @note Setting this too low could cause performance issues or a deadlock. An |
---|
834 | * unblockable warning will be printed. |
---|
835 | * |
---|
836 | * @see trace_set_thread_cache_size(), trace_set_fixed_count() |
---|
837 | */ |
---|
838 | DLLEXPORT int trace_set_cache_size(libtrace_t *trace, size_t size); |
---|
839 | |
---|
840 | /** This sets the maximum size of the freelist cache owned by each thread |
---|
841 | * used to provide faster access to empty packets than the main shared pool. |
---|
842 | * |
---|
843 | * @param trace A parallel input trace |
---|
844 | * @param size The number of empty packets to cache in memory. Set to the |
---|
845 | * default, 0, to autoconfigure this value. |
---|
846 | * @return 0 if successful otherwise -1 |
---|
847 | * |
---|
848 | * @see trace_set_cache_size(), trace_set_fixed_count() |
---|
849 | */ |
---|
850 | DLLEXPORT int trace_set_thread_cache_size(libtrace_t *trace, size_t size); |
---|
851 | |
---|
852 | /** Determines whether a trace is allowed to create additional packets |
---|
853 | * beyond the cache size. |
---|
854 | * |
---|
855 | * If set to true, libtrace will cease reading packets once the cache is used |
---|
856 | * up until the other threads release some packets back to the cache. |
---|
857 | * |
---|
858 | * If set to false (the default), libtrace will use malloc and free to create |
---|
859 | * additional packets when the cache is exhausted. This will be slower than |
---|
860 | * getting a packet from the cache and will eventually run the machine out |
---|
861 | * of memory if packets are allocated faster than they are released. |
---|
862 | * |
---|
863 | * @param trace A parallel input trace |
---|
864 | * @param fixed If true the total number of packets is limited, otherwise |
---|
865 | * it is not. Defaults to false. |
---|
866 | * @return 0 if successful otherwise -1 |
---|
867 | * |
---|
868 | * @see trace_set_thread_cache_size(), trace_set_cache_size() |
---|
869 | */ |
---|
870 | DLLEXPORT int trace_set_fixed_count(libtrace_t *trace, bool fixed); |
---|
871 | |
---|
872 | /** The number of packets to batch together for processing internally |
---|
873 | * by libtrace. |
---|
874 | * |
---|
875 | * @param trace A parallel input trace |
---|
876 | * @param size The total number of packets to batch together. Set to the |
---|
877 | * default, 0, to autoconfigure this value. |
---|
878 | * @return 0 if successful otherwise -1 |
---|
879 | * |
---|
880 | * Internally libtrace will attempt to read up to this number of packets from |
---|
881 | * a format at a time. Typically, values of 10 will get good performance and |
---|
882 | * increasing beyond that will should little difference. |
---|
883 | * |
---|
884 | * @note We still pass a single packet at a time to the packet callback |
---|
885 | * function. |
---|
886 | */ |
---|
887 | DLLEXPORT int trace_set_burst_size(libtrace_t *trace, size_t size); |
---|
888 | |
---|
889 | /** |
---|
890 | * Sets the maximum size of the buffer used between the single hasher thread |
---|
891 | * and the packet processing thread. |
---|
892 | * |
---|
893 | * Setting this to less than recommend could cause a deadlock for an input |
---|
894 | * trace that manages its own packets. |
---|
895 | * A unblockable warning message will be printed to stderr in this case. |
---|
896 | */ |
---|
897 | DLLEXPORT int trace_set_hasher_queue_size(libtrace_t *trace, size_t size); |
---|
898 | |
---|
899 | /** |
---|
900 | * Enables or disables polling of the hasher queue. |
---|
901 | * |
---|
902 | * If enabled, the processing threads will poll on the hasher queue, yielding |
---|
903 | * if no data is available. |
---|
904 | * |
---|
905 | * If disabled, the processing threads will block on a condition variable |
---|
906 | * if there is no data available from the hasher. |
---|
907 | * |
---|
908 | * @param trace A parallel input trace |
---|
909 | * @param polling If true the hasher will poll waiting for data, otherwise |
---|
910 | * it will use a condition variable. Defaults to false. |
---|
911 | * |
---|
912 | * We note polling is likely to waste many CPU cycles and could even decrease |
---|
913 | * performance. |
---|
914 | * |
---|
915 | * @return 0 if successful otherwise -1 |
---|
916 | */ |
---|
917 | DLLEXPORT int trace_set_hasher_polling(libtrace_t *trace, bool polling); |
---|
918 | |
---|
919 | /** |
---|
920 | * Enables or disables polling of the reporter result queue. |
---|
921 | * |
---|
922 | * If enabled, the reporter thread will continuously poll for results. |
---|
923 | * If disabled, the reporter will only check for results if it receives a |
---|
924 | * MESSAGE_POST_REPORTER. |
---|
925 | * |
---|
926 | * @param trace A parallel input trace |
---|
927 | * @param polling If true the reporter will poll waiting for data, otherwise |
---|
928 | * it will wait for a MESSAGE_POST_REPORTER. Defaults to false. |
---|
929 | * @return 0 if successful otherwise -1 |
---|
930 | * |
---|
931 | * We note polling is likely to waste many CPU cycles and could even decrease |
---|
932 | * performance. |
---|
933 | * |
---|
934 | * @note This setting could be ignored by some reporters. |
---|
935 | */ |
---|
936 | DLLEXPORT int trace_set_reporter_polling(libtrace_t *trace, bool polling); |
---|
937 | |
---|
938 | /** |
---|
939 | * Set the number of results that are required in the result queue before |
---|
940 | * a MESSAGE_POST_REPORTER is sent to the reporter so that it can read the |
---|
941 | * results. |
---|
942 | * |
---|
943 | * Set this to 1 to ensure if you require your results to reach the reporter |
---|
944 | * as soon as possible. |
---|
945 | * |
---|
946 | * @param trace A parallel input trace |
---|
947 | * @param thold The threshold on the number of results to enqueue before |
---|
948 | * notifying the reporter thread to read them. |
---|
949 | * @return 0 if successful otherwise -1 |
---|
950 | * |
---|
951 | * |
---|
952 | * @note This setting is generally ignored if the reporter is polling. However, |
---|
953 | * some combiner functions might ignore the polling behaviour and still |
---|
954 | * require this to be set. |
---|
955 | * @see trace_publish_result(), trace_post_reporter() |
---|
956 | */ |
---|
957 | DLLEXPORT int trace_set_reporter_thold(libtrace_t *trace, size_t thold); |
---|
958 | |
---|
959 | /** |
---|
960 | * Enable or disable debug output for parallel libtrace. |
---|
961 | |
---|
962 | * If enabled, libtrace will print a line to standard error for every |
---|
963 | * state change observed by both the trace as a whole and by each thread. |
---|
964 | * |
---|
965 | * You really shouldn't need to enable this.... |
---|
966 | * |
---|
967 | * @param trace A parallel input trace |
---|
968 | * @param debug_state If true debug is printed. Defaults false. |
---|
969 | * @return 0 if successful otherwise -1. |
---|
970 | * |
---|
971 | */ |
---|
972 | DLLEXPORT int trace_set_debug_state(libtrace_t *trace, bool debug_state); |
---|
973 | |
---|
974 | /** Set the hasher function for a parallel trace. |
---|
975 | * |
---|
976 | * @param[in] trace The parallel trace to apply the hasher to |
---|
977 | * @param[in] type The type of hashing to apply, see enum hasher_types |
---|
978 | * @param[in] hasher A hasher function to use [Optional] |
---|
979 | * @param[in] data Data passed to the hasher function [Optional] |
---|
980 | * |
---|
981 | * @return 0 if successful otherwise -1 on error |
---|
982 | * |
---|
983 | * The hasher function in a parallel trace can be used to control which |
---|
984 | * processing thread will receive each packet. |
---|
985 | * |
---|
986 | * See hasher_types for a list of hashers supported natively by libtrace. |
---|
987 | * |
---|
988 | * HASHER_BALANCE is the default and will dispatch packets as fast as possible |
---|
989 | * to all threads arbitrarily. |
---|
990 | * |
---|
991 | * HASHER_CUSTOM will force the libtrace to use the user defined function. In |
---|
992 | * this case, the hasher parameter must be supplied. |
---|
993 | * |
---|
994 | * With other defined hasher types libtrace will try to push the hashing into |
---|
995 | * the capture format wherever possible. In this case, the hasher parameter is |
---|
996 | * optional; if a hasher is provided then it will be preferred over the |
---|
997 | * libtrace implementation. |
---|
998 | * |
---|
999 | * @note When supplying a hasher function it should be thread-safe so it can |
---|
1000 | * be run in parallel by libtrace. Ideally this should rely upon no state, other |
---|
1001 | * than some form of seed value supplied in data. |
---|
1002 | */ |
---|
1003 | DLLEXPORT int trace_set_hasher(libtrace_t *trace, enum hasher_types type, |
---|
1004 | fn_hasher hasher, void *data); |
---|
1005 | |
---|
1006 | /// @} |
---|
1007 | |
---|
1008 | |
---|
1009 | /** Types of results. |
---|
1010 | * |
---|
1011 | * Custom result types users should be defined as RESULT_USER(1000) or greater. |
---|
1012 | * |
---|
1013 | */ |
---|
1014 | enum result_types { |
---|
1015 | /** |
---|
1016 | * The result contains a pointer to a libtrace_packet_t. This |
---|
1017 | * packet should be freed using trace_free_packet() once the result |
---|
1018 | * is processed by the reporter thread. |
---|
1019 | * |
---|
1020 | * The key for a RESULT_PACKET is the packet order (see |
---|
1021 | * trace_get_packet_order() for more about ordering). |
---|
1022 | * |
---|
1023 | */ |
---|
1024 | RESULT_PACKET, |
---|
1025 | |
---|
1026 | /** |
---|
1027 | * The result is a tick timestamp. The key is an ERF timestamp. |
---|
1028 | */ |
---|
1029 | RESULT_TICK_INTERVAL, |
---|
1030 | |
---|
1031 | /** |
---|
1032 | * The result is a tick counter. The key is the sequence number of |
---|
1033 | * the tick, relative to the packets read so far. |
---|
1034 | */ |
---|
1035 | RESULT_TICK_COUNT, |
---|
1036 | |
---|
1037 | /** |
---|
1038 | * Any user-defined result codes should be at or above this value. |
---|
1039 | */ |
---|
1040 | RESULT_USER = 1000 |
---|
1041 | |
---|
1042 | }; |
---|
1043 | |
---|
1044 | /** Publish a result to the reporter thread (via the combiner) |
---|
1045 | * |
---|
1046 | * @param[in] libtrace The parallel input trace |
---|
1047 | * @param[in] t The current per-packet thread |
---|
1048 | * @param[in] key The key of the result (used for sorting by the combiner) |
---|
1049 | * @param[in] value The value of the result |
---|
1050 | * @param[in] type The type of result (see result_types) |
---|
1051 | */ |
---|
1052 | DLLEXPORT void trace_publish_result(libtrace_t *libtrace, |
---|
1053 | libtrace_thread_t *t, |
---|
1054 | uint64_t key, |
---|
1055 | libtrace_generic_t value, |
---|
1056 | int type); |
---|
1057 | |
---|
1058 | /** Check if a dedicated hasher thread is being used. |
---|
1059 | * |
---|
1060 | * @param[in] libtrace The parallel input trace |
---|
1061 | * @return true if the trace has dedicated hasher thread otherwise false. |
---|
1062 | * |
---|
1063 | * This should only be called after the trace has been started with |
---|
1064 | * trace_pstart(). |
---|
1065 | */ |
---|
1066 | DLLEXPORT bool trace_has_dedicated_hasher(libtrace_t * libtrace); |
---|
1067 | |
---|
1068 | /** Checks if a trace is using a reporter thread. |
---|
1069 | * |
---|
1070 | * @param[in] libtrace The parallel input trace |
---|
1071 | * @return True if the trace is using a reporter otherwise false |
---|
1072 | */ |
---|
1073 | DLLEXPORT bool trace_has_reporter(libtrace_t * libtrace); |
---|
1074 | |
---|
1075 | /** Post a message to the reporter thread requesting that it check for more |
---|
1076 | * results. |
---|
1077 | * |
---|
1078 | * @param[in] The parallel input trace |
---|
1079 | * @return -1 upon error indicating the message has not been sent otherwise a |
---|
1080 | * backlog indicator (the number of messages the reporter has not yet read). |
---|
1081 | */ |
---|
1082 | DLLEXPORT int trace_post_reporter(libtrace_t *libtrace); |
---|
1083 | |
---|
1084 | /** Check the number of messages waiting in a thread's message queue |
---|
1085 | * |
---|
1086 | * @param[in] libtrace The input trace |
---|
1087 | * @param[in] t The thread to check; if NULL the current thread will be used. |
---|
1088 | * |
---|
1089 | * @return packets in the queue otherwise -1 upon error. |
---|
1090 | * |
---|
1091 | * @note For best performance it is recommended to supply the thread argument |
---|
1092 | * even if it is the current thread. |
---|
1093 | */ |
---|
1094 | DLLEXPORT int libtrace_thread_get_message_count(libtrace_t * libtrace, |
---|
1095 | libtrace_thread_t *t); |
---|
1096 | |
---|
1097 | /** Read a message from a thread in a blocking fashion. |
---|
1098 | * |
---|
1099 | * @param[in] libtrace The input trace |
---|
1100 | * @param[in] t The thread to check, if NULL the current thread will be used. |
---|
1101 | * @param[out] message A pointer to a libtrace_message_t structure which will |
---|
1102 | * be filled with the retrieved message. |
---|
1103 | * |
---|
1104 | * @return The number of messages remaining otherwise -1 upon error. |
---|
1105 | * |
---|
1106 | * @note For best performance it is recommended to supply the thread argument |
---|
1107 | * even if it is the current thread. |
---|
1108 | */ |
---|
1109 | DLLEXPORT int libtrace_thread_get_message(libtrace_t * libtrace, |
---|
1110 | libtrace_thread_t *t, |
---|
1111 | libtrace_message_t * message); |
---|
1112 | |
---|
1113 | /** Read a message from a thread in a non-blocking fashion. |
---|
1114 | * |
---|
1115 | * @param[in] libtrace The input trace |
---|
1116 | * @param[in] t The thread to check, if NULL the current thread will be used. |
---|
1117 | * @param[out] message A pointer to a libtrace_message_t structure which will |
---|
1118 | * be filled with the retrieved message. |
---|
1119 | * |
---|
1120 | * @return 0 if successful otherwise -1 upon error or if no message were |
---|
1121 | * available. |
---|
1122 | * |
---|
1123 | * @note For best performance it is recommended to supply the thread argument |
---|
1124 | * even if it is the current thread. |
---|
1125 | */ |
---|
1126 | DLLEXPORT int libtrace_thread_try_get_message(libtrace_t * libtrace, |
---|
1127 | libtrace_thread_t *t, |
---|
1128 | libtrace_message_t * message); |
---|
1129 | |
---|
1130 | /** Send a message to the reporter thread. |
---|
1131 | * |
---|
1132 | * @param[in] libtrace The parallel trace |
---|
1133 | * @param[in] message The message to be sent. If the sender field is NULL, |
---|
1134 | * libtrace will attempt to fill this in. It is faster to assign this if it is |
---|
1135 | * known. |
---|
1136 | * |
---|
1137 | * @return -1 upon error indicating the message has not been sent. Otherwise, |
---|
1138 | * will return the number of messages the reporter has not yet read. |
---|
1139 | */ |
---|
1140 | DLLEXPORT int trace_message_reporter(libtrace_t * libtrace, |
---|
1141 | libtrace_message_t * message); |
---|
1142 | |
---|
1143 | /** Send a message to all processing threads. |
---|
1144 | * |
---|
1145 | * @param[in] libtrace The parallel trace |
---|
1146 | * @param[in] message The message to be sent. If the sender field is NULL, |
---|
1147 | * libtrace will attempt to fill this in. It is faster to assign this if it is |
---|
1148 | * known. |
---|
1149 | * |
---|
1150 | * @return 0 if successful. Otherwise, a negative number is returned that |
---|
1151 | * indicates the number of processing threads that the message was not sent |
---|
1152 | * to (i.e. -1 means one thread could not be sent the message). |
---|
1153 | */ |
---|
1154 | DLLEXPORT int trace_message_perpkts(libtrace_t * libtrace, |
---|
1155 | libtrace_message_t * message); |
---|
1156 | |
---|
1157 | /** Send a message to a specific thread. |
---|
1158 | * |
---|
1159 | * @param[in] libtrace The parallel trace |
---|
1160 | * @param[in] t The thread to message |
---|
1161 | * @param[in] message The message to be sent. If the sender field is NULL, |
---|
1162 | * libtrace will attempt to fill this in. It is faster to assign this if it is |
---|
1163 | * known. |
---|
1164 | * |
---|
1165 | * @return -1 upon error indicating the message has not been sent. Otherwise, |
---|
1166 | * will return the number of messages the recipient has not yet read. |
---|
1167 | */ |
---|
1168 | DLLEXPORT int trace_message_thread(libtrace_t * libtrace, |
---|
1169 | libtrace_thread_t *t, |
---|
1170 | libtrace_message_t * message); |
---|
1171 | |
---|
1172 | /** Checks if a parallel trace has finished reading packets. |
---|
1173 | * |
---|
1174 | * @return true if the trace has finished reading packets (even if all results |
---|
1175 | * have not yet been processed). Otherwise false. |
---|
1176 | * |
---|
1177 | * @note This returns true even if all results have not yet been processed by |
---|
1178 | * the reporter thread. |
---|
1179 | */ |
---|
1180 | DLLEXPORT bool trace_has_finished(libtrace_t * libtrace); |
---|
1181 | |
---|
1182 | |
---|
1183 | /** Check if libtrace is directly reading from multiple queues |
---|
1184 | * from within the capture format (such as a NICs hardware queues). |
---|
1185 | * |
---|
1186 | * A trace is considered to be parallel if the input format for the trace |
---|
1187 | * allows the packets to be read in a natively parallel fashion, i.e. packets |
---|
1188 | * can be read using multiple pipelines. If this function returns false, the |
---|
1189 | * packets are instead being read from a single input source and then |
---|
1190 | * distributed amongst the processing threads. |
---|
1191 | * |
---|
1192 | * Factors that may cause this function to return false despite the format |
---|
1193 | * normally supporting native parallel reads include: the choice of hasher |
---|
1194 | * function, the number of threads choosen (such as 1 or more than the trace |
---|
1195 | * supports) or another error when trying to start the parallel format. |
---|
1196 | * |
---|
1197 | * If called before the trace is started, i.e. before trace_pstart(), this |
---|
1198 | * function returns an indication whether the trace has the possiblity to |
---|
1199 | * support native parallel reads. After trace_pstart() is called this should be |
---|
1200 | * checked again to confirm that this has happened. |
---|
1201 | * |
---|
1202 | * @return true if the trace is parallel or false if the library is splitting |
---|
1203 | * the trace into multiple threads. |
---|
1204 | */ |
---|
1205 | DLLEXPORT bool trace_is_parallel(libtrace_t * libtrace); |
---|
1206 | |
---|
1207 | /** Returns either the sequence number or erf timestamp of a packet. |
---|
1208 | * |
---|
1209 | * @param[in] packet |
---|
1210 | * @return A 64bit sequence number or erf timestamp. |
---|
1211 | * |
---|
1212 | * The returned value can be used to compare the relative ordering of packets. |
---|
1213 | * Formats that are not natively parallel will typically return a sequence |
---|
1214 | * number. Natively parallel formats will return a timestamp. |
---|
1215 | */ |
---|
1216 | DLLEXPORT uint64_t trace_packet_get_order(libtrace_packet_t * packet); |
---|
1217 | |
---|
1218 | /** Returns the hash of a packet. |
---|
1219 | * |
---|
1220 | * @param[in] packet |
---|
1221 | * @return A 64-bit hash |
---|
1222 | * |
---|
1223 | * @note This function will only work in situations where |
---|
1224 | * a custom hash is being used. You can use trace_has_dedicated_hasher() |
---|
1225 | * to check if this is the case. |
---|
1226 | */ |
---|
1227 | DLLEXPORT uint64_t trace_packet_get_hash(libtrace_packet_t * packet); |
---|
1228 | |
---|
1229 | /** Sets the order of a packet. |
---|
1230 | * |
---|
1231 | * @param[in] packet |
---|
1232 | * @param[in] order the new order of a packet |
---|
1233 | * |
---|
1234 | * @note Many combiners rely on this value, so please ensure that changing this |
---|
1235 | * conforms to the expectations of the combiner. |
---|
1236 | * |
---|
1237 | * Generally speaking, you probably shouldn't be changing the order of packets! |
---|
1238 | */ |
---|
1239 | DLLEXPORT void trace_packet_set_order(libtrace_packet_t * packet, uint64_t order); |
---|
1240 | |
---|
1241 | /** Sets the hash of a packet. |
---|
1242 | * |
---|
1243 | * @param[in] packet |
---|
1244 | * @param[in] hash the new hash |
---|
1245 | * |
---|
1246 | * Once a packet reaches the processing thread, the libtrace library has |
---|
1247 | * little use for this field and as such this can essentially be used for any |
---|
1248 | * storage that the user requires. |
---|
1249 | */ |
---|
1250 | DLLEXPORT void trace_packet_set_hash(libtrace_packet_t * packet, uint64_t hash); |
---|
1251 | |
---|
1252 | |
---|
1253 | /** Returns the first packet read by a processing thread since the source |
---|
1254 | * trace was last started or restarted. |
---|
1255 | * |
---|
1256 | * @param[in] libtrace the parallel input trace. |
---|
1257 | * @param[in] t Either a per packet thread or NULL to retrieve the earliest |
---|
1258 | * packet across all per packet threads. |
---|
1259 | * @param[out] packet A pointer to the requested packet. [Optional] |
---|
1260 | * @param[out] tv The system time-stamp when the packet was received. [Optional] |
---|
1261 | * @return 1 if we are confident this is the first packet. Otherwise 0 if this |
---|
1262 | * is a best guess (this is only possible int the case t=NULL) in which case |
---|
1263 | * we recommend trying again at a later time. |
---|
1264 | * -1 is returned if an error occurs, such as when this function is supplied |
---|
1265 | * an invalid thread. |
---|
1266 | * |
---|
1267 | * The packet and timeval returned by this function is shared by all threads |
---|
1268 | * and remain valid until MESSAGE_PAUSING is received. |
---|
1269 | */ |
---|
1270 | DLLEXPORT int trace_get_first_packet(libtrace_t *libtrace, |
---|
1271 | libtrace_thread_t *t, |
---|
1272 | const libtrace_packet_t **packet, |
---|
1273 | const struct timeval **tv); |
---|
1274 | |
---|
1275 | /** Makes a packet safe, preventing the packet from becoming invalid after a |
---|
1276 | * pausing a trace. |
---|
1277 | * |
---|
1278 | * @param[in,out] pkt The packet to make safe |
---|
1279 | * |
---|
1280 | * This copies a packet in such a way that it will be able to survive a pause. |
---|
1281 | * However this will not allow the packet to be used after the format is |
---|
1282 | * destroyed. |
---|
1283 | */ |
---|
1284 | DLLEXPORT void libtrace_make_packet_safe(libtrace_packet_t *pkt); |
---|
1285 | |
---|
1286 | /** Makes a result safe, preventing the result from becoming invalid after |
---|
1287 | * pausing a trace. |
---|
1288 | * |
---|
1289 | * @param[in,out] res The result to make safe. |
---|
1290 | * |
---|
1291 | * This ensures the internal content of a result is safe to survive a pause. |
---|
1292 | * Note that this is only an issue if the result contains a packet. |
---|
1293 | * See libtrace_make_packet_safe(). |
---|
1294 | */ |
---|
1295 | DLLEXPORT void libtrace_make_result_safe(libtrace_result_t *res); |
---|
1296 | |
---|
1297 | /** In a parallel trace, free a packet back to libtrace. |
---|
1298 | * |
---|
1299 | * @param[in] libtrace A parallel input trace |
---|
1300 | * @param[in] packet The packet to be released back to libtrace |
---|
1301 | * |
---|
1302 | * The packet should not be used after calling this function. |
---|
1303 | * |
---|
1304 | * @note Don't use this inside a packet callback function -- just return |
---|
1305 | * the packet instead, as this will be faster. |
---|
1306 | * |
---|
1307 | * @note All packets should be free'd before a trace is destroyed. |
---|
1308 | */ |
---|
1309 | DLLEXPORT void trace_free_packet(libtrace_t * libtrace, libtrace_packet_t * packet); |
---|
1310 | |
---|
1311 | /** Increments the internal reference counter for a packet. |
---|
1312 | * @param packet The packet opaque pointer |
---|
1313 | * |
---|
1314 | * You may wish to use this function (and its decrementing counterpart) |
---|
1315 | * in situations where you are retaining multiple references to a packet |
---|
1316 | * outside of the core packet processing function. This will ensure that |
---|
1317 | * the packet is not released until there are no more outstanding references |
---|
1318 | * to the packet anywhere in your program. |
---|
1319 | */ |
---|
1320 | DLLEXPORT void trace_increment_packet_refcount(libtrace_packet_t *packet); |
---|
1321 | |
---|
1322 | /** Decrements the internal reference counter for a packet. |
---|
1323 | * @param packet The packet opaque pointer |
---|
1324 | * |
---|
1325 | * If the reference counter goes below one, trace_fin_packet() will be |
---|
1326 | * called on the packet. |
---|
1327 | * |
---|
1328 | * You may wish to use this function (and its incrementing counterpart) |
---|
1329 | * in situations where you are retaining multiple references to a packet |
---|
1330 | * outside of the core packet processing function. This will ensure that |
---|
1331 | * the packet is not released until there are no more outstanding references |
---|
1332 | * to the packet anywhere in your program. |
---|
1333 | */ |
---|
1334 | DLLEXPORT void trace_decrement_packet_refcount(libtrace_packet_t *packet); |
---|
1335 | |
---|
1336 | |
---|
1337 | /** Provides some basic information about a trace based on its input format. |
---|
1338 | * |
---|
1339 | * @param libtrace The trace that is being inquired about. |
---|
1340 | * @return a libtrace_info_t structure that contains information about the |
---|
1341 | * trace format, i.e. is it live or not, how many threads it supports. |
---|
1342 | * |
---|
1343 | * See trace_is_parallel(), trace_get_perpkt_threads(). |
---|
1344 | */ |
---|
1345 | DLLEXPORT libtrace_info_t *trace_get_information(libtrace_t * libtrace); |
---|
1346 | |
---|
1347 | /** Sets the configuration of a trace based upon a comma separated list of |
---|
1348 | * key value pairs. |
---|
1349 | * |
---|
1350 | * @param trace A parallel trace which is not running or destroyed. |
---|
1351 | * @param str A comma separated list of key=value pairs: |
---|
1352 | * e.g. \em "burst_size=20,perpkt_threads=2,fixed_count=true" |
---|
1353 | * @return 0 if successful otherwise -1. If bad options are passed we will |
---|
1354 | * print the error to stderr but still return successful. |
---|
1355 | * |
---|
1356 | * List of keys: |
---|
1357 | * * \b cache_size,\b cs see trace_set_cache_size() [size_t] |
---|
1358 | * * \b thread_cache_size,\b tcs see trace_set_thread_cache_size() [size_t] |
---|
1359 | * * \b fixed_count,\b fc see trace_set_fixed_count() [bool] |
---|
1360 | * * \b burst_size,\b bs see trace_set_burst_size() [size_t] |
---|
1361 | * * \b tick_interval,\b ti see trace_set_tick_interval() [size_t] |
---|
1362 | * * \b tick_count,\b tc see trace_set_tick_count() [size_t] |
---|
1363 | * * \b perpkt_threads,\b pt see trace_set_perpkt_threads() [XXX TBA XXX] |
---|
1364 | * * \b hasher_queue_size,\b hqs see trace_set_hasher_queue_size() [size_t] |
---|
1365 | * * \b hasher_polling,\b hp see trace_set_hasher_polling() [bool] |
---|
1366 | * * \b reporter_polling,\b rp see trace_set_reporter_polling() [bool] |
---|
1367 | * * \b reporter_thold,\b rt see trace_set_reporter_thold() [size_t] |
---|
1368 | * * \b debug_state,\b ds see trace_set_debug_state() [bool] |
---|
1369 | * |
---|
1370 | * Booleans can be set as 0/1 or false/true. |
---|
1371 | * |
---|
1372 | * @note a environment variable interface is provided by default to users via |
---|
1373 | * LIBTRACE_CONF, see Parallel Configuration for more information. |
---|
1374 | * |
---|
1375 | * @note This interface is provided to allow a user to quickly configure an |
---|
1376 | * application using a single API call. A nicer programatic method for |
---|
1377 | * configuration would be to use the appropriate trace_set_*() function for |
---|
1378 | * each option. |
---|
1379 | */ |
---|
1380 | DLLEXPORT int trace_set_configuration(libtrace_t *trace, const char * str); |
---|
1381 | |
---|
1382 | /** Sets configuration from a file. This reads every line from the file and |
---|
1383 | * interprets each line with trace_set_configuration(). |
---|
1384 | * |
---|
1385 | * @param trace A parallel trace which is not running or destroyed |
---|
1386 | * @param file A file pointer which we read each line from |
---|
1387 | * @return 0 if successful otherwise -1. If bad options are passed we will |
---|
1388 | * print the error to stderr but still return successful. |
---|
1389 | * |
---|
1390 | * @note We do not close the file pointer upon completion |
---|
1391 | */ |
---|
1392 | DLLEXPORT int trace_set_configuration_file(libtrace_t *trace, FILE *file); |
---|
1393 | |
---|
1394 | /** Returns the number of processing threads that have been created for |
---|
1395 | * a given trace. |
---|
1396 | * |
---|
1397 | * @param t A parallel trace. |
---|
1398 | * @return The number of processing threads owned by that trace. |
---|
1399 | */ |
---|
1400 | DLLEXPORT int trace_get_perpkt_threads(libtrace_t* t); |
---|
1401 | |
---|
1402 | /** Returns the internal unique ID for a packet processing thread. |
---|
1403 | * |
---|
1404 | * @param thread The thread being queried. |
---|
1405 | * @return The ID number of the thread or -1 if the thread is not a processing |
---|
1406 | * thread or is otherwise invalid. |
---|
1407 | */ |
---|
1408 | DLLEXPORT int trace_get_perpkt_thread_id(libtrace_thread_t *thread); |
---|
1409 | |
---|
1410 | /** |
---|
1411 | * Sets a combiner function for an input trace. |
---|
1412 | * |
---|
1413 | * @param trace The input trace |
---|
1414 | * @param combiner The combiner to use |
---|
1415 | * @param config Configuration information. Dependent upon the combiner. |
---|
1416 | * |
---|
1417 | * Sets a combiner against a trace, this should only be called on a |
---|
1418 | * non-started or paused trace. By default, combiner_unordered |
---|
1419 | * will be used if this function is not called before starting the trace. |
---|
1420 | */ |
---|
1421 | DLLEXPORT void trace_set_combiner(libtrace_t *trace, const libtrace_combine_t *combiner, libtrace_generic_t config); |
---|
1422 | |
---|
1423 | /** |
---|
1424 | * Takes unordered (or ordered) input and produces unordered output. |
---|
1425 | * This is the fastest combiner but makes no attempt to ensure you get |
---|
1426 | * results in a particular order. |
---|
1427 | */ |
---|
1428 | extern const libtrace_combine_t combiner_unordered; |
---|
1429 | |
---|
1430 | /** |
---|
1431 | * Takes ordered input and produces ordered output. Each processing thread |
---|
1432 | * must produce results that are strictly ordered for this combiner to |
---|
1433 | * work correctly. |
---|
1434 | * |
---|
1435 | * For example, a thread may publish a series of results with the keys |
---|
1436 | * (in order) of 1,4,10,11,15,20 as the keys are all in order. It must not |
---|
1437 | * publish the results in the order 1,4,11,10,15,20 -- 10 comes after 11, |
---|
1438 | * which is out-of-order. |
---|
1439 | */ |
---|
1440 | extern const libtrace_combine_t combiner_ordered; |
---|
1441 | |
---|
1442 | /** |
---|
1443 | * Like classic Google Map/Reduce, the results are sorted |
---|
1444 | * in ascending order based on their key. The sorting is only done when the |
---|
1445 | * trace finishes and all results are stored internally until then. |
---|
1446 | * |
---|
1447 | * This only works with a very limited number of results, otherwise |
---|
1448 | * libtrace will just run out of memory and crash. You should always |
---|
1449 | * use combiner_ordered if you can. |
---|
1450 | */ |
---|
1451 | extern const libtrace_combine_t combiner_sorted; |
---|
1452 | |
---|
1453 | #ifdef __cplusplus |
---|
1454 | } |
---|
1455 | #endif |
---|
1456 | |
---|
1457 | #endif // LIBTRACE_PARALLEL_H |
---|