source: lib/libtrace_parallel.h @ 4007dbb

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 4007dbb was 4007dbb, checked in by Richard Sanger <rsangerarj@…>, 6 years ago

Updates the new interface to be more complete

This should work around any issues with systems without thread support.
This still remains compatible with existing code.
Examples/tools/tests still need to be updated to make use of the new interface.
And tests also need to be updated.
Adds debug memory stats as an option to configure.

  • Property mode set to 100644
File size: 43.8 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Richard Sanger
8 *
9 * All rights reserved.
10 *
11 * This code has been developed by the University of Waikato WAND
12 * research group. For further information please see http://www.wand.net.nz/
13 *
14 * libtrace is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * libtrace is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with libtrace; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27 *
28 * $Id$
29 *
30 */
31
32/** @file
33 *
34 * @brief Header file containing definitions for structures and functions
35 * related to the parallel framework
36 *
37 * @author Richard Sanger
38 *
39 * @version $Id$
40 *
41 * The parallel libtrace framework is a replacement to the libtrace framework. XXX TODO MAKE MORE DOCS HERE.
42 */
43
44#ifndef LIBTRACE_PARALLEL_H
45#define LIBTRACE_PARALLEL_H
46
47#include "libtrace.h"
48#include <stdio.h>
49
50typedef struct libtrace_result_t libtrace_result_t;
51
52/**
53 * A collection of types for convenience used in place of a
54 * simple void* to allow a any type of data to be stored.
55 *
56 * This is expected to be 8 bytes in length.
57 */
58typedef union {
59        /* Pointers */
60        void *ptr;
61        libtrace_packet_t *pkt;
62        libtrace_result_t *res;
63
64        /* C99 Integer types */
65        /* NOTE: Standard doesn't require 64-bit
66         * but x32 and x64 gcc does */
67        int64_t sint64;
68        uint64_t uint64;
69
70        uint32_t uint32s[2];
71        int32_t sint32s[2];
72        uint32_t uint32;
73        int32_t sint32;
74
75        uint16_t uint16s[4];
76        int16_t sint16s[4];
77        uint16_t uint16;
78        int16_t sint16;
79
80        uint8_t uint8s[8];
81        int8_t sint8s[8];
82        uint8_t uint8;
83        int8_t sint8;
84
85        size_t size;
86
87        /* C basic types - we cannot be certain of the size */
88        int sint;
89        unsigned int uint;
90
91        signed char schars[8];
92        unsigned char uchars[8];
93        signed char schar;
94        unsigned char uchar;
95
96        /* Real numbers */
97        float rfloat;
98        double rdouble;
99} libtrace_generic_t;
100ct_assert(sizeof(libtrace_generic_t) == 8);
101
102typedef struct libtrace_message_t {
103        int code; /**< The message code see enum libtrace_messages */
104        libtrace_generic_t data; /**< Additional data related to the message */
105        libtrace_thread_t *sender; /**< The thread that sent the message */
106} libtrace_message_t;
107
108/** Structure holding information about a result */
109struct libtrace_result_t {
110        uint64_t key;
111        libtrace_generic_t value;
112        int type;
113};
114
115/** The libtrace_messages enum
116 * All libtrace messages are defined and documented here.
117 *
118 * Some messages can be sent to control the library while others
119 * are received by the per-packet and reporter functions to inform the libtrace
120 * application.
121 *
122 * If a user wishes to send there own custom messages they should use
123 * numbers greater than MESSAGE_USER (1000).
124 *
125 * @note Some messages are for internal use only
126 */
127enum libtrace_messages {
128        /** A libtrace packet is ready, this will only be sent to per
129         * packet threads.
130         * @param data Holds the packet in data.pkt. The packet belongs to
131         * libtrace and should either be returned from the per-packet function
132         * if no longer needed or free'd at some later time using the XXX
133         * function.
134         * @param sender The sender will be set as the current thread
135         */
136        MESSAGE_PACKET,
137        /** A libtrace result is ready, this will only be sent to the reporter
138         * thread.
139         * @param data Holds the result in data.res. The memory holding the
140         * result is allocated by libtrace and should not be free'd. However
141         * note that any data stored within the result might need to be free'd.
142         * @param sender The sender will be set as the current thread
143         */
144        MESSAGE_RESULT,
145
146        /** A message sent to each thread when it starts. This is sent
147         * to both the reporter and per-packet threads. This will be sent once
148         * after trace_pstart() (assuming no errors occurs).
149         *
150         * This can be used to allocate resources required by each thread.
151         *
152         * These can be free'd when MESSAGE_STOPPING is received.
153         *
154         * @param data unused, do not use this
155         * @param sender The sender will be set as the current thread
156         * @return When using a function callback for starting, the returned
157         * value is stored against the thread tls. Otherwise the return is ignored.
158         */
159        MESSAGE_STARTING,
160
161        /** A message sent to each thread when it stops. This is sent
162         * to both the reporter and per-packet threads. This will be sent once
163         * after MESSAGE_STARTING.
164         *
165         * This can be used to free any resources allocated with
166         * MESSAGE_STARTING.
167         *
168         * @param data unused, do not use this
169         * @param sender The sender will be set as the current thread
170         */
171        MESSAGE_STOPPING,
172
173        /** A message sent to each thread when a thread transitions between a
174         * paused (or unstarted) state to running state. This is sent
175         * to both the reporter and per-packet threads. This will be sent after
176         * MESSAGE_STARTING when a trace is first started and when a trace
177         * is started (trace_pstart()) after a pause (trace_ppause()).
178         *
179         * This can be used to allocate resources.
180         *
181         * @param data unused, do not use this
182         * @param sender The sender will be set as the current thread
183         */
184        MESSAGE_RESUMING,
185
186        /** A message sent to each thread when a thread transitions between a
187         * paused (or unstarted) state to running state. This is sent
188         * to both the reporter and per-packet threads. This will be sent after
189         * MESSAGE_STARTING when a trace is first started and when a trace
190         * is started (trace_pstart()) after a pause (trace_ppause()).
191         *
192         * This can be used to allocate resources.
193         *
194         * @param data unused, do not use this
195         * @param sender The sender will be set as the current thread
196         */
197        MESSAGE_PAUSING,
198
199        /** An internal message do not use this */
200        MESSAGE_DO_PAUSE,
201        /** An internal message do not use this */
202        MESSAGE_DO_STOP,
203
204        /** Sent to all per-packet threads (including the sender) and the
205         * reducer when the first packet is seen for a thread.
206         *
207         * @param data The first packet is stored in data.pkt. This packet is
208         * shared by all threads receiving the message and is valid until
209         * MESSAGE_PAUSING is received.
210         * @param sender The per-packet thread which received the packet
211         *
212         * @note Upon pausing and restarting a trace this will be reset and
213         * sent once a new packet is encountered
214         *
215         * @see trace_get_first_packet()
216         */
217        MESSAGE_FIRST_PACKET,
218
219        /** Notify the reporter thread more data is available.
220         *
221         * Triggers the reporter to read as many results as possible.
222         *
223         * @param data unused
224         * @param sender the sending
225         *
226         * @note This message should not be sent directly instead call
227         * trace_post_reporter()
228         *
229         */
230        MESSAGE_POST_REPORTER,
231
232        /** Sent to per-packet threads periodically after the configured time
233         * interval has passed.
234         *
235         * This is sent out-of-band with respect to packets and as a result
236         * can appear after a packet with an later time-stamp, or before one
237         * with an earlier time-stamp.
238         *
239         * @param data data.uint64 holds the system time-stamp in the
240         * erf format
241         * @param sender should be ignored
242         */
243        MESSAGE_TICK_INTERVAL,
244
245        /** Sent to per-packet threads once the configured number of packets
246         * are read from a trace.
247         *
248         * This are sent in-band with respect to packets such that all
249         * threads will see it between the same packets.
250         *
251         * @param data data.uint64 holds the number of packets seen so far across all threads
252         * @param sender Set to the current per-packet thread
253         */
254        MESSAGE_TICK_COUNT,
255
256        /** For specific user defined messages use codes of MESSAGE_USER or above. */
257        MESSAGE_USER = 1000
258};
259
260/** The hasher types available to libtrace application.
261 * These can be selected using trace_set_hasher().
262 */
263enum hasher_types {
264        /** Balance load across per-packet threads as best as possible, this is
265         * basically to say I do not care about where packets are sent. This
266         * might still might be implemented using a hash or round robin etc.
267         * depending on the format and libtrace configuration.
268         */
269        HASHER_BALANCE,
270
271        /** Use a hash which is bi-directional for TCP and UDP flows, that is
272         * packets with the same 5-tuple are sent to the same per-packet thread.
273         * All non TCP/UDP packets will be sent to the same thread.
274         *
275         * @note it is possible that UDP packets may not be spread across
276         * per-packet threads, depending upon the format support. In this case
277         * they would be directed to a single per-packet thread.
278         */
279        HASHER_BIDIRECTIONAL,
280
281        /** Use a hash which is uni-directional across TCP and UDP flows, this
282         * means the opposing directions of the same 5-tuple might end up on
283         * different per-packet threads.
284         * Otherwise this is identical to HASHER_BIDIRECTIONAL
285         */
286        HASHER_UNIDIRECTIONAL,
287
288        /**
289         * Always use the user supplied hasher, this disables native
290         * support in and is likely significantly slower.
291         */
292        HASHER_CUSTOM
293};
294
295typedef struct libtrace_info_t {
296        /**
297         * True if a live format (i.e. packets have to be trace-time).
298         * Otherwise false, indicating packets can be read as fast
299         * as possible from the format.
300         */
301        bool live;
302
303        /**
304         * The maximum number of threads supported by a parallel trace. 1
305         * if parallel support is not native (in this case libtrace will simulate
306         * an unlimited number of threads), -1 means unlimited and 0 unknown.
307         */
308        int max_threads;
309
310        /* TODO hash fn supported list */
311
312        /* TODO consider time/clock details?? */
313} libtrace_info_t;
314
315/**
316 * The methods we use to combine multiple outputs into a single output
317 * This is not considered a stable API however is public.
318 * Where possible use built in combiners.
319 *
320 * @note this structure is duplicated per trace and as such can
321 * have functions rewritten, and in fact should if possible.
322 */
323typedef struct libtrace_combine libtrace_combine_t;
324struct libtrace_combine {
325
326        /**
327         * Called at the start of the trace to allow data-structures
328         * to be initialised and allow functions to be swapped if appropriate.
329         *
330         * Also factors such as whether the trace is live or not can
331         * be used to determine the functions used.
332         * @return 0 if successful, -1 if an error occurs
333         */
334        int (*initialise)(libtrace_t *,libtrace_combine_t *);
335
336        /**
337         * Called when the trace ends, clean up any memory here
338         * from libtrace_t * init.
339         */
340        void (*destroy)(libtrace_t *, libtrace_combine_t *);
341
342        /**
343         * Publish a result against it's a threads queue.
344         * If null publish directly, expected to be used
345         * as a single threaded optimisation and can be
346         * set to NULL by init if this case is detected.
347         *
348         * TODO this is old info
349         */
350        void (*publish)(libtrace_t *, int thread_id, libtrace_combine_t *, libtrace_result_t *);
351
352        /**
353         * Read as many results as possible from the trace.
354         * Directly calls the users code to handle results from here.
355         *
356         * THIS SHOULD BE NON-BLOCKING AND READ AS MANY AS POSSIBLE
357         * If publish is NULL, this probably should be NULL also otherwise
358         * it will not be called.
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.
365         *
366         * There may be no results, in which case this should
367         * just return.
368         *
369         * Libtrace state:
370         * Called from reporter thread
371         * No perpkt threads will be running, i.e. publish will not be
372         * called again.
373         *
374         * If publish is NULL, this probably should be NULL also otherwise
375         * it will not be called.
376         */
377        void (*read_final)(libtrace_t *, libtrace_combine_t *);
378
379        /**
380         * Pause must make sure any results of the type packet are safe.
381         * That means trace_copy_packet() and destroy the original.
382         * This also should be NULL if publish is NULL.
383         */
384        void (*pause)(libtrace_t *, libtrace_combine_t *);
385
386        /**
387         * Data storage for all the combiner threads
388         */
389        void *queues;
390
391        /**
392         * Configuration options, what this does is up to the combiner
393         * chosen.
394         */
395        libtrace_generic_t configuration;
396};
397
398/**
399 * The definition for the main function that the user supplies to process
400 * messages.
401 *
402 * @param trace The trace the packet is related to.
403 * @param thread The thread identifier.
404 * @param mesg_code The type of data ready, the most important being MESSAGE_PACKET.
405 * In this case data.pkt contains the packet.
406 * @param data A generic union of types that fit into 8 bytes, containing
407 * information dependent upon the mesg_code.
408 * @param sender The thread from which the message originated.
409 * @return If the message type is MESSAGE_PACKET a packet can be returned back
410 * to the library similar to trace_free_packet() otherwise this should be NULL.
411 *
412 * The values of data and sender depend upon the mesg_code. Please see the
413 * documentation for the message as to what value these will contain.
414 */
415typedef void* (*fn_cb_msg)(libtrace_t* trace,
416                           libtrace_thread_t *thread,
417                           int mesg_code,
418                           libtrace_generic_t data,
419                           libtrace_thread_t *sender);
420
421/**
422 * The definition for the main function that the user supplies to process
423 * results from trace_publish_result().
424 *
425 * @param trace The trace the packet is related to.
426 * @param mesg_code The type of data ready, the most important being MESSAGE_RESULT.
427 * In this case data.res contains the result.
428 * @param data A generic union of types that fit into 8 bytes, containing
429 * information dependent upon the mesg_code.
430 * @param sender The thread from which the message originated.
431 *
432 * The values of data and sender depend upon the mesg_code. Please see the
433 * documentation for the message as to what value these will contain.
434 */
435typedef void (*fn_reporter)(libtrace_t* trace,
436                            int mesg_code,
437                            libtrace_generic_t data,
438                            libtrace_thread_t *sender);
439
440/**
441 * The definition for a hasher function, allowing matching packets to be
442 * directed to the same per packet thread for processing.
443 *
444 * @param packet The packet to be hashed.
445 * @param data A void pointer which can contain additional information,
446 * such as configuration of the hasher function.
447 */
448typedef uint64_t (*fn_hasher)(const libtrace_packet_t* packet, void *data);
449
450
451/** Start or restart an input trace in the parallel libtrace framework.
452 *
453 * @param libtrace The input trace to start
454 * @param global_blob Global data related to this trace accessible using trace_get_global()
455 * @param per_msg A user supplied function called when a message is ready
456 * @param reporter A user supplied function called when a result is ready.
457 * Optional if NULL the reporter thread will not be started.
458 * @return 0 on success, otherwise -1 to indicate an error has occurred
459 *
460 * This can also be used to restart an existing parallel trace,
461 * that has previously been paused using trace_ppause().
462 * In this case global_blob,per_msg and reporter will only be updated
463 * if they are non-null. Otherwise their previous values will be maintained.
464 *
465 */
466DLLEXPORT int trace_pstart(libtrace_t *libtrace, void* global_blob,
467                           fn_cb_msg per_msg, fn_reporter reporter);
468
469/**
470 *
471 * @param libtrace The parallel trace
472 * @param t The thread that is running
473 * @param global The global storage
474 * @return The returned value is stored against the threads tls.
475 *         This is typically passed as tls argument to other messages.
476 */
477typedef void* (*fn_cb_starting)(libtrace_t *libtrace,
478                                     libtrace_thread_t *t,
479                                     void *global);
480
481/**
482 * @param libtrace The parallel trace
483 * @param t The thread that is running
484 * @param global The global storage
485 * @param tls The thread local storage
486 */
487typedef void (*fn_cb_dataless)(libtrace_t *libtrace,
488                                    libtrace_thread_t *t,
489                                    void *global,
490                                    void *tls);
491
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 */
498typedef void (*fn_cb_first_packet)(libtrace_t *libtrace,
499                                   libtrace_thread_t *t,
500                                   void *global,
501                                   void *tls,
502                                   libtrace_packet_t *first_packet,
503                                   libtrace_thread_t *sender);
504
505/**
506 * @param libtrace The parallel trace
507 * @param t The thread that is running
508 * @param global The global storage
509 * @param tls The thread local storage
510 * @param uint64_t Either the timestamp or packet count depending on message type
511 */
512typedef void (*fn_cb_tick)(libtrace_t *libtrace,
513                           libtrace_thread_t *t,
514                           void *global,
515                           void *tls,
516                           uint64_t order);
517
518/**
519 * @param libtrace The parallel trace
520 * @param t The thread
521 * @param packet The packet associated with the message
522 * @param global The global storage
523 * @param tls The thread local storage
524 *
525 * @return optionally a packet which is handed back to the library,
526 *         typically this is the packet supplied. Otherwise NULL.
527 */
528typedef libtrace_packet_t* (*fn_cb_packet)(libtrace_t *libtrace,
529                                           libtrace_thread_t *t,
530                                           void *global,
531                                           void *tls,
532                                           libtrace_packet_t *packet);
533
534/** Registers a built-in message with a handler.
535 * Note we do not include the sending thread as an argument to the reporter.
536 * If set to NULL, the message will be sent to default perpkt handler.
537 *
538 * @param libtrace The input trace to start
539 * @param message The message to intercept
540 * @param handler the handler to be called when the message is received
541 * @return 0 if successful otherwise -1.
542 */
543
544DLLEXPORT int trace_cb_starting(libtrace_t *libtrace, fn_cb_starting handler);
545DLLEXPORT int trace_cb_stopping(libtrace_t *libtrace, fn_cb_dataless handler);
546DLLEXPORT int trace_cb_resuming(libtrace_t *libtrace, fn_cb_dataless handler);
547DLLEXPORT int trace_cb_pausing(libtrace_t *libtrace, fn_cb_dataless handler);
548DLLEXPORT int trace_cb_packet(libtrace_t *libtrace, fn_cb_packet handler);
549DLLEXPORT int trace_cb_first_packet(libtrace_t *libtrace, fn_cb_first_packet handler);
550DLLEXPORT int trace_cb_tick_count(libtrace_t *libtrace, fn_cb_tick handler);
551DLLEXPORT int trace_cb_tick_interval(libtrace_t *libtrace, fn_cb_tick handler);
552
553/** Pauses a trace previously started with trace_pstart()
554 *
555 * @param libtrace The parallel trace to be paused
556 * @return 0 on success, otherwise -1 to indicate an error has occurred
557 *
558 */
559DLLEXPORT int trace_ppause(libtrace_t *libtrace);
560
561/** Stops a parallel trace, causing all threads to exit as if an EOF
562 * has occurred. This replaces trace_interrupt(), allowing
563 * a specified trace to be stopped.
564 *
565 * @param libtrace The parallel trace to be stopped
566 * @return 0 on success, otherwise -1 to indicate an error has occurred
567 *
568 * This should only be called by the main thread.
569 *
570 */
571DLLEXPORT int trace_pstop(libtrace_t *libtrace);
572
573/** Waits for a trace to finish and all threads to join.
574 *
575 * @param trace The parallel trace
576 *
577 * Waits for a trace to finish, whether this be due to
578 * an error occurring, an EOF or trace_pstop.
579 *
580 */
581DLLEXPORT void trace_join(libtrace_t * trace);
582
583/**
584 * @name User Data Storage
585 *
586 * These method provide a way for users to store data against a trace or
587 * a thread.
588 *
589 * Alternatively one could use global variables and thread local
590 * storage (__thread), respectively, which in many cases could be simpler.
591 *
592 * @note We do not lock on reads, instead we rely on the
593 * processor making any writes appear atomically.
594 *
595 * @{
596 */
597
598/** Returns the data stored against a trace.
599 *
600 * @param trace The parallel trace
601 * @return The stored data.
602 */
603DLLEXPORT void * trace_get_local(libtrace_t *trace);
604
605/** Store data against a trace so that all threads can access it
606 * using trace_get_global().
607 *
608 * @param trace The parallel trace.
609 * @param data The new value to save against the trace
610 * @return The previously stored value
611 *
612 * The update to the previous value is atomic and thread-safe.
613 *
614 * @note Although this is thread-safe another thread may still be
615 * using the previous data, as such further synchronisation is needed
616 * if a thread wanted to free the existing value.
617 */
618DLLEXPORT void * trace_set_local(libtrace_t *trace, void * data);
619
620/** Returns the users data stored against a thread.
621 *
622 * @param thread The thread
623 * @return The stored data
624 */
625DLLEXPORT void * trace_get_tls(libtrace_thread_t *thread);
626
627/** Store data against a thread.
628 *
629 * @param The parallel trace.
630 * @param data The new value to save against the trace
631 * @return The previously stored value
632 *
633 * This function is not thread-safe and is intended only to be
634 * called on the currently running thread.
635 */
636DLLEXPORT void * trace_set_tls(libtrace_thread_t *thread, void * data);
637
638/// @}
639
640
641/**
642 * @name Parallel Configuration
643 *
644 * These methods provide a way to configure the parallel libtrace library.
645 *
646 * Many of these options are typically unneeded by most applications as they
647 * control tuning aspects of the library and are more useful to the
648 * end user.
649 *
650 * To allow the end user to change this configuration libtrace will search for
651 * three environment variables and apply them to the configuration in the
652 * following order. Such that the first has the lowest priority.
653 *
654 * 1. LIBTRACE_CONF, The global environment configuration
655 * 2. LIBTRACE_CONF_<FORMAT>, Applied to a given format
656 * 3. LIBTRACE_CONF_<FORMAT_URI>, Applied the specified trace
657 *
658 * E.g.
659 * - int:eth0 would match LIBTRACE_CONF, LIBTRACE_CONF_INT, LIBTRACE_CONF_INT_ETH0
660 * - dag:/dev/dag0,0 would match LIBTRACE_CONF, LIBTRACE_CONF_DAG, LIBTRACE_CONF_DAG__DEV_DAG0_0
661 * - test.erf would match LIBTRACE_CONF, LIBTRACE_CONF_ERF, LIBTRACE_CONF_ERF_TEST_ERF
662 *
663 * @note All environment variables names MUST only contain
664 * [A-Z], [0-9] and [_] (underscore). Any characters
665 * outside of this range should be capitalised if possible or replaced with an
666 * underscore.
667 * @{
668 */
669
670/** Set the maximum number of perpkt threads to use in a trace.
671 *
672 * @param[in] trace The parallel input trace
673 * @param[in] nb The number of threads to use. If 0 use default.
674 * @return 0 if successful otherwise -1
675 */
676DLLEXPORT int trace_set_perpkt_threads(libtrace_t *trace, int nb);
677
678/** Set the interval between tick messages in milliseconds.
679 *
680 * @param[in] trace The parallel input trace
681 * @param[in] millisec The interval in milliseconds. If 0 this is disabled [default].
682 * @return 0 if successful otherwise -1
683 *
684 * When a underlying parallel live trace is used MESSAGE_TICK_INTERVAL is sent
685 * every tick interval to all per-packet threads to ensure data is received.
686 * This allows results to be printed even in cases flows are not being directed
687 * to a per-packet thread, while still maintaining order etc.
688 *
689 * @note Tick count is preferred over tick interval and will be used rather
690 * than tick interval if possible.
691 * @see MESSAGE_TICK_INTERVAL, trace_set_tick_count()
692 */
693DLLEXPORT int trace_set_tick_interval(libtrace_t *trace, size_t millisec);
694
695/** Set the count between tick messages.
696 *
697 * @param[in] trace The parallel input trace
698 * @param[in] count The tick count.  If 0 this is disabled [default].
699 * @return 0 if successful otherwise -1
700 *
701 * When an underlying trace is accessed internally by libtrace in a
702 * single-threaded manner MESSAGE_TICK_COUNT is sent to all per-packet threads
703 * after every count packets have been seen in the trace. This allows results
704 * to be printed even in cases flows are not being directed to a per-packet
705 * thread, while still maintaining order etc.
706 *
707 * @see MESSAGE_TICK_COUNT, trace_set_tick_interval()
708 */
709DLLEXPORT int trace_set_tick_count(libtrace_t *trace, size_t count);
710
711/**
712 * Delays packets so they are played back in trace-time rather than as fast
713 * as possible (real-time).
714 *
715 * @param A parallel input trace
716 * @param tracetime If true packets are released with time intervals matching
717 * the original trace. Otherwise packets are read as fast as possible.
718 * @return 0 if successful otherwise -1
719 */
720DLLEXPORT int trace_set_tracetime(libtrace_t *trace, bool tracetime);
721
722/** This sets the maximum size of the freelist used to store empty packets
723 * and their memory buffers.
724 *
725 * @param trace A parallel input trace
726 * @param size The number of empty packets to cache in memory. Set to the
727 * default, 0, to autoconfigure this value.
728 * @return 0 if successful otherwise -1
729 *
730 * Internally libtrace maintains a buffer of packet structures, this buffer
731 * includes a cache per thread and a shared main pool. This configures
732 * the size of the main pool. If an application is not passing packets
733 * through to the reducer step --- that is to say returns packets from
734 * the perpkt function --- this buffer will not need to be used.
735 *
736 * @note Setting this too low could cause performance issues or a deadlock. An
737 * unblockable warning will be printed.
738 *
739 * @see trace_set_thread_cache_size(), trace_set_fixed_count()
740 */
741DLLEXPORT int trace_set_cache_size(libtrace_t *trace, size_t size);
742
743/** This sets the maximum size of the freelist thread cache's used to provide
744 * faster access than the main shared pool.
745 *
746 * @param trace A parallel input trace
747 * @param size The number of empty packets to cache in memory. Set to the
748 * default, 0, to autoconfigure this value.
749 * @return 0 if successful otherwise -1
750 *
751 * @see trace_set_cache_size(), trace_set_fixed_count()
752 */
753DLLEXPORT int trace_set_thread_cache_size(libtrace_t *trace, size_t size);
754
755/** If true the total number of packets that can be created by a trace is limited
756 * to that set by trace_set_cache_size(), otherwise once exceeded malloc
757 * and free will be used to create and free packets, this will be slower than
758 * using the freelist and could run a machine out of memory.
759 *
760 * @param trace A parallel input trace
761 * @param fixed If true the total number of packets is limited, otherwise
762 * it is not. Defaults to false.
763 * @return 0 if successful otherwise -1
764 *
765 * @see trace_set_thread_cache_size(), trace_set_cache_size()
766 */
767DLLEXPORT int trace_set_fixed_count(libtrace_t *trace, bool fixed);
768
769/** The number of packets to batch together for processing internally
770 * by libtrace.
771 *
772 * @param trace A parallel input trace
773 * @param size The total number of packets to batch together. Set to the
774 * default, 0, to autoconfigure this value.
775 * @return 0 if successful otherwise -1
776 *
777 * Internally libtrace will attempt to read up to this number of packets from
778 * a format typically values of 10 will get good performance and increasing
779 * beyond that will should little difference.
780 *
781 * @note We still pass a single packet at a time to the perpkt function
782 */
783DLLEXPORT int trace_set_burst_size(libtrace_t *trace, size_t size);
784
785/**
786 * See diagrams, this sets the maximum size of buffers used between
787 * the single hasher thread and the buffer.
788 * NOTE setting this to less than recommend could cause deadlock a
789 * trace that manages its own packets.
790 * A unblockable warning message will be printed to stderr in this case.
791 */
792/** The number of packets that can queue per thread from hasher thread */
793DLLEXPORT int trace_set_hasher_queue_size(libtrace_t *trace, size_t size);
794
795/** If true use a polling hasher queue, that means that we will spin/or yield
796 * when data is not available rather than blocking on a condition.
797 *
798 * @param trace A parallel input trace
799 * @param polling If true the hasher will poll waiting for data, otherwise
800 * it is not. Defaults to false.
801 *
802 * We note this is likely to waste many CPU cycles and could even decrease
803 * performance.
804 *
805 * @return 0 if successful otherwise -1
806 */
807DLLEXPORT int trace_set_hasher_polling(libtrace_t *trace, bool polling);
808
809/** If true the reporter thread will continuously poll waiting for results
810 * if false they are only checked when a message is received, this message
811 * is controlled by reporter_thold.
812 *
813 * @param trace A parallel input trace
814 * @param polling If true the reporter will poll waiting for data, otherwise
815 * it is not. Defaults to false.
816 * @return 0 if successful otherwise -1
817 *
818 * We note this is likely to waste many CPU cycles and could even decrease
819 * performance.
820 *
821 * @note This setting could be ignored by some reporters.
822 */
823DLLEXPORT int trace_set_reporter_polling(libtrace_t *trace, bool polling);
824
825/** Set the perpkt thread result queue size before triggering the reporter
826 * to read results.
827 *
828 * @param trace A parallel input trace
829 * @param thold The threshold on the number of results to enqueue before
830 * notifying the reporter thread to read them.
831 * @return 0 if successful otherwise -1
832 *
833 *
834 * @note This setting is generally ignored if trace_set_reporter_polling() is
835 * set however some combiner functions might ignore trace_set_reporter_polling()
836 * and still require this to be set.
837 * @see trace_publish_result(), trace_post_reporter()
838 */
839DLLEXPORT int trace_set_reporter_thold(libtrace_t *trace, size_t thold);
840
841/** Prints a line to standard error for every state change
842 * for both the trace as a whole and for each thread.
843 *
844 * @param trace A parallel input trace
845 * @param debug_state If true debug is printed. Defaults false.
846 * @return 0 if successful otherwise -1.
847 *
848 */
849DLLEXPORT int trace_set_debug_state(libtrace_t *trace, bool debug_state);
850
851/** Set the hasher function for a parallel trace.
852 *
853 * @param[in] trace The parallel trace to apply the hasher to
854 * @param[in] type The type of hashing to apply, see enum hasher_types
855 * @param[in] hasher A hasher function to use [Optional]
856 * @param[in] data Data passed to the hasher function [Optional]
857 *
858 * @return 0 if successful otherwise -1 on error
859 *
860 * The hasher function in a parallel trace can be used to control which
861 * per-packet thread a packets is processed by.
862 *
863 * HASHER_BALANCE is the default and will dispatch packets as fast as possible
864 * to all threads arbitrarily. As such when called the hasher and
865 * data parameters must be set to NULL.
866 *
867 * HASHER_CUSTOM will force the libtrace to use the user defined function. As
868 * such the hasher parameter must be supplied.
869 *
870 * With other defined hasher types we will try to push the hashing into the format
871 * by default. In this case the hasher parameter is optional and will be
872 * preferred over the default supplied by libtrace.
873 *
874 * @note When supplying a hasher function it should be thread-safe so it can
875 * be run in parallel by libtrace. Ideally this should rely upon no state, other
876 * than some form of seed value supplied in data.
877 */
878DLLEXPORT int trace_set_hasher(libtrace_t *trace, enum hasher_types type,
879                               fn_hasher hasher, void *data);
880
881/// @}
882
883
884/** Types of results.
885 * Some result types require special handling by combiners
886 * as such making use of built-in types is important.
887 *
888 * Custom result types users should be defined as RESULT_USER(1000) or greater.
889 *
890 */
891enum result_types {
892        /**
893         * The result is a packet in some circumstances special handling needs
894         * to be performed. As such packets should always be published as so.
895         *
896         * @param key (Typically) The packets order, see trace_packet_get_order()
897         * @param
898         */
899        RESULT_PACKET,
900
901        /** The result is a tick message
902         *
903         * @param key The erf time-stamp of the tick
904         */
905        RESULT_TICK_INTERVAL,
906
907        /** The result is a tick message
908         *
909         * @param key The sequence number of the tick message
910         */
911        RESULT_TICK_COUNT,
912
913        /** Any user specific codes should be above this.
914         *
915         */
916        RESULT_USER = 1000
917
918};
919
920/** Publish a result for to the combiner destined for the reporter thread
921 *
922 * @param[in] libtrace The parallel input trace
923 * @param[in] t The current per-packet thread
924 * @param[in] key The key of the result (used for sorting by the combiner)
925 * @param[in] value The value of the result
926 * @param[in] type The type of result see the documentation for the result_types enum
927 */
928DLLEXPORT void trace_publish_result(libtrace_t *libtrace,
929                                    libtrace_thread_t *t,
930                                    uint64_t key,
931                                    libtrace_generic_t value,
932                                    int type);
933
934/** Check if a dedicated hasher thread is being used.
935 *
936 * @param[in] libtrace The parallel input trace
937 * @return True if the trace has dedicated hasher thread otherwise false.
938 *
939 * This is valid once the trace is running after calling trace_pstart().
940 */
941DLLEXPORT bool trace_has_dedicated_hasher(libtrace_t * libtrace);
942
943/** Checks if a trace is using a reporter
944 *
945 * @param[in] libtrace The parallel input trace
946 * @return True if the trace is using a reporter otherwise false
947 */
948DLLEXPORT bool trace_has_reporter(libtrace_t * libtrace);
949
950/** Post a message to the reporter thread requesting it to check for more
951 * results.
952 *
953 * @param[in] The parallel input trace
954 * @return -1 upon error indicating the message has not been sent otherwise a
955 * backlog indicator (the number of messages the reporter has not yet read).
956 */
957DLLEXPORT int trace_post_reporter(libtrace_t *libtrace);
958
959/** Check the number of messages waiting in a queue
960 *
961 * @param[in] libtrace The input trace
962 * @param[in] t The thread to check, if NULL the current thread will be used [Optional]
963 *
964 * @return packets in the queue otherwise -1 upon error.
965 *
966 * @note For best performance it is recommended to supply the thread argument
967 * even if it is the current thread.
968 */
969DLLEXPORT int libtrace_thread_get_message_count(libtrace_t * libtrace,
970                                                libtrace_thread_t *t);
971
972/** Read a message from a thread in a blocking fashion
973 *
974 * @param[in] libtrace The input trace
975 * @param[in] t The thread to check, if NULL the current thread will be used [Optional]
976 * @param[out] message A pointer to libtrace_message_t structure which will be
977 * filled with the retrieved message.
978 *
979 * @return The number of messages remaining otherwise -1 upon error.
980 *
981 *
982 * @note For best performance it is recommended to supply the thread argument
983 * even if it is the current thread.
984 */
985DLLEXPORT int libtrace_thread_get_message(libtrace_t * libtrace,
986                                          libtrace_thread_t *t,
987                                          libtrace_message_t * message);
988
989/** Read a message from a thread in a blocking fashion
990 *
991 * @param[in] libtrace The input trace
992 * @param[in] t The thread to check, if NULL the current thread will be used [Optional]
993 * @param[out] message A pointer to libtrace_message_t structure which will be
994 * filled with the retrieved message.
995 *
996 * @return 0 if successful otherwise -1 upon error or if no packets were available.
997 *
998 *
999 * @note For best performance it is recommended to supply the thread argument
1000 * even if it is the current thread.
1001 */
1002DLLEXPORT int libtrace_thread_try_get_message(libtrace_t * libtrace,
1003                                              libtrace_thread_t *t,
1004                                              libtrace_message_t * message);
1005
1006/** Send a message to the reporter thread
1007 *
1008 * @param[in] libtrace The parallel trace
1009 * @param[in] message The message to be sent, if sender is NULL libtrace will
1010 * attempt to fill this in. It is faster to assign this if it is known.
1011 *
1012 * @return -1 upon error indicating the message has not been sent otherwise a
1013 * backlog indicator (the number of messages the reporter has not yet read).
1014 */
1015DLLEXPORT int trace_message_reporter(libtrace_t * libtrace,
1016                                     libtrace_message_t * message);
1017
1018/** Send a message to all per-packet threads
1019 *
1020 * @param[in] libtrace The parallel trace
1021 * @param[in] message The message to be sent, if sender is NULL libtrace will
1022 * attempt to fill this in. It is faster to assign this if it is known.
1023 *
1024 * @return 0 if successful otherwise a negative number indicating the number
1025 * of per-packet threads the message was not sent to (i.e. -1 means one thread
1026 * could not be sent the message).
1027 */
1028DLLEXPORT int trace_message_perpkts(libtrace_t * libtrace,
1029                                    libtrace_message_t * message);
1030
1031/** Send a message to a thread
1032 *
1033 * @param[in] libtrace The parallel trace
1034 * @param[in] t The thread to message
1035 * @param[in] message The message to be sent, if sender is NULL libtrace will
1036 * attempt to fill this in. It is faster to assign this if it is known.
1037 *
1038 * @return -1 upon error indicating the message has not been sent otherwise a
1039 * backlog indicator (the number of messages the thread has not yet read).
1040 */
1041DLLEXPORT int trace_message_thread(libtrace_t * libtrace,
1042                                   libtrace_thread_t *t,
1043                                   libtrace_message_t * message);
1044
1045/** Check if a parallel trace has finished reading packets
1046 *
1047 * @return True if the trace has finished reading packets (even if all results
1048 * have not yet been processed). Otherwise false.
1049 *
1050 * @note This returns true even if all results have not yet been processed.
1051 */
1052DLLEXPORT bool trace_has_finished(libtrace_t * libtrace);
1053
1054/** Returns either the sequence number or erf timestamp of a packet.
1055 *
1056 * @param[in] packet
1057 * @return A 64bit sequence number or erf timestamp.
1058 *
1059 * The returned value can be used to compare if packets come before or after
1060 * others.
1061 */
1062DLLEXPORT uint64_t trace_packet_get_order(libtrace_packet_t * packet);
1063
1064/** Returns the hash of a packet.
1065 *
1066 * @param[in] packet
1067 * @return A 64-bit hash
1068 *
1069 * @note In many cases this might not be filled in, only in cases where
1070 * a custom hash is being used. You can use trace_has_dedicated_hasher()
1071 * to check if this will be valid.
1072 */
1073DLLEXPORT uint64_t trace_packet_get_hash(libtrace_packet_t * packet);
1074
1075/** Sets the order of a packet.
1076 *
1077 * @param[in] packet
1078 * @param[in] order the new order of a packet
1079 *
1080 * @note many combiners rely on this value, ensure changing this conforms to
1081 * the combiners requirements.
1082 */
1083DLLEXPORT void trace_packet_set_order(libtrace_packet_t * packet, uint64_t order);
1084
1085/** Sets the hash of a packet.
1086 *
1087 * @param[in] packet
1088 * @param[in] hash the new hash
1089 *
1090 * Once handed to the user the libtrace library has little use for this field
1091 * and as such this can essentially be used for any storage the user requires.
1092 */
1093DLLEXPORT void trace_packet_set_hash(libtrace_packet_t * packet, uint64_t hash);
1094
1095/** TODO WHAT TO DO WITH THIS ? */
1096DLLEXPORT uint64_t tv_to_usec(const struct timeval *tv);
1097
1098
1099/** Returns the first packet of a parallel trace since it was started or
1100 * restarted.
1101 *
1102 * @param[in] libtrace the parallel input trace
1103 * @param[in] t Either a per packet thread or NULL to retrieve the first packet
1104 * of across all per packet threads.
1105 * @param[out] packet A pointer to the first packet in the trace. [Optional]
1106 * @param[out] tv The system time-stamp when this packet was received. [Optional]
1107 * @return 1 if we are confident this is the first packet. Otherwise 0 if this
1108 * is a best guess (this is only possible int the case t=NULL)
1109 * in which case we recommend calling this at a later time.
1110 * -1 is returned if an error occurs, such as supplied a invalid thread.
1111 *
1112 * The packet and timeval returned by this function is shared by all threads
1113 * and remain valid until MESSAGE_PAUSING is received.
1114 */
1115DLLEXPORT int trace_get_first_packet(libtrace_t *libtrace,
1116                                     libtrace_thread_t *t,
1117                                     const libtrace_packet_t **packet,
1118                                     const struct timeval **tv);
1119
1120/** Makes a packet safe, a packet will become invalid after a
1121 * pausing a trace.
1122 *
1123 * @param[in,out] pkt The packet to make safe
1124 *
1125 * This copies a packet in such a way that it will be able to survive a pause.
1126 * However this will not allow the packet to be used after
1127 * the format is destroyed.
1128 */
1129DLLEXPORT void libtrace_make_packet_safe(libtrace_packet_t *pkt);
1130
1131/** Makes a result safe if a result contains a packet.
1132 *
1133 * @param[in,out] res The result to make safe.
1134 *
1135 * This ensures the internal content of a result is safe to survive a pause.
1136 * See libtrace_make_packet_safe().
1137 */
1138DLLEXPORT void libtrace_make_result_safe(libtrace_result_t *res);
1139
1140/** In a parallel trace, free a packet back to libtrace.
1141 *
1142 * @param[in] libtrace A parallel input trace
1143 * @param[in] packet The packet to be released back to libtrace
1144 *
1145 * The packet should not be used after calling this function.
1146 *
1147 * @note All packets should be free'd before a trace is destroyed.
1148 */
1149DLLEXPORT void trace_free_packet(libtrace_t * libtrace, libtrace_packet_t * packet);
1150
1151
1152DLLEXPORT libtrace_info_t *trace_get_information(libtrace_t * libtrace);
1153
1154/** Sets the configuration of a trace based upon a comma separated list of
1155 * key value pairs.
1156 *
1157 * @param trace A parallel trace which is not running or destroyed
1158 * @param str A comma separated list of key=value pairs.
1159 * E.g. \em "burst_size=20,perpkt_threads=2,fixed_count=true"
1160 * @return 0 if successful otherwise -1. If bad options are passed we will
1161 * print the error to stderr but still return successful.
1162 *
1163 * List of keys:
1164 * * \b cache_size,\b cs see trace_set_cache_size() [size_t]
1165 * * \b thread_cache_size,\b tcs see trace_set_thread_cache_size() [size_t]
1166 * * \b fixed_count,\b fc see trace_set_fixed_count() [bool]
1167 * * \b burst_size,\b bs see trace_set_burst_size() [size_t]
1168 * * \b tick_interval,\b ti see trace_set_tick_interval() [size_t]
1169 * * \b tick_count,\b tc see trace_set_tick_count() [size_t]
1170 * * \b perpkt_threads,\b pt see trace_set_perpkt_threads() [XXX TBA XXX]
1171 * * \b hasher_queue_size,\b hqs see trace_set_hasher_queue_size() [size_t]
1172 * * \b hasher_polling,\b hp see trace_set_hasher_polling() [bool]
1173 * * \b reporter_polling,\b rp see trace_set_reporter_polling() [bool]
1174 * * \b reporter_thold,\b rt see trace_set_reporter_thold() [size_t]
1175 * * \b debug_state,\b ds see trace_set_debug_state() [bool]
1176 *
1177 * Booleans can be set as 0/1 or false/true.
1178 *
1179 * @note a environment variable interface is provided by default to users via
1180 * LIBTRACE_CONF, see Parallel Configuration for more information.
1181 *
1182 * @note this interface is provided to allow a user to configure an application
1183 * if a libtrace applicate wishes to configure a setting it should use a
1184 * trace_set_*() function with the same name.
1185 */
1186DLLEXPORT int trace_set_configuration(libtrace_t *trace, const char * str);
1187
1188/** Sets configuration from a file. This reads every line from the file and
1189 * interprets each line with trace_set_configuration().
1190 *
1191 * @param trace A parallel trace which is not running or destroyed
1192 * @param file A file pointer which we read each line from
1193 * @return 0 if successful otherwise -1. If bad options are passed we will
1194 * print the error to stderr but still return successful.
1195 *
1196 * @note We do not close the file pointer upon completion
1197 */
1198DLLEXPORT int trace_set_configuration_file(libtrace_t *trace, FILE *file);
1199
1200DLLEXPORT int libtrace_get_perpkt_count(libtrace_t* t); // TODO MATCH WITH THE SET_PERPKT
1201
1202/**
1203 * Sets a combiner function against the trace.
1204 *
1205 * @param trace The input trace
1206 * @combiner The combiner to use
1207 * @config config Configuration information. Dependent upon the combiner in use
1208 *
1209 * Sets a combiner against a trace, this should only be called on a
1210 * non-started or paused trace.  By default combiner_unordered
1211 * will be used.
1212 */
1213DLLEXPORT void trace_set_combiner(libtrace_t *trace, const libtrace_combine_t *combiner, libtrace_generic_t config);
1214
1215/**
1216 * Takes unordered (or ordered) input and produces unordered output.
1217 * Basically you get the result quickly but in no particular order.
1218 */
1219extern const libtrace_combine_t combiner_unordered;
1220
1221/**
1222 * Takes ordered input and produces ordered output. Perpkt threads
1223 * the output results must be ordered for this to work correctly!!
1224 */
1225extern const libtrace_combine_t combiner_ordered;
1226
1227/**
1228 * Like classic Google Map/Reduce, the results are sorted
1229 * in ascending order, this is only done when the trace finishes.
1230 *
1231 * This only works with a limited number of results, otherwise
1232 * we will just run out of memory and crash!! You should always
1233 * use combiner_ordered if you can.
1234 */
1235extern const libtrace_combine_t combiner_sorted;
1236
1237#endif // LIBTRACE_PARALLEL_H
Note: See TracBrowser for help on using the repository browser.