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 | * All rights reserved. |
---|
8 | * |
---|
9 | * This code has been developed by the University of Waikato WAND |
---|
10 | * research group. For further information please see http://www.wand.net.nz/ |
---|
11 | * |
---|
12 | * libtrace is free software; you can redistribute it and/or modify |
---|
13 | * it under the terms of the GNU General Public License as published by |
---|
14 | * the Free Software Foundation; either version 2 of the License, or |
---|
15 | * (at your option) any later version. |
---|
16 | * |
---|
17 | * libtrace is distributed in the hope that it will be useful, |
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | * GNU General Public License for more details. |
---|
21 | * |
---|
22 | * You should have received a copy of the GNU General Public License |
---|
23 | * along with libtrace; if not, write to the Free Software |
---|
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
25 | * |
---|
26 | * $Id$ |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | |
---|
31 | #define _GNU_SOURCE |
---|
32 | #include "common.h" |
---|
33 | #include "config.h" |
---|
34 | #include <assert.h> |
---|
35 | #include <errno.h> |
---|
36 | #include <fcntl.h> |
---|
37 | #include <stdio.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <string.h> |
---|
40 | #include <sys/stat.h> |
---|
41 | #include <sys/types.h> |
---|
42 | #ifndef WIN32 |
---|
43 | #include <sys/socket.h> |
---|
44 | #endif |
---|
45 | #include <stdarg.h> |
---|
46 | #include <sys/param.h> |
---|
47 | |
---|
48 | #ifdef HAVE_LIMITS_H |
---|
49 | # include <limits.h> |
---|
50 | #endif |
---|
51 | |
---|
52 | #ifdef HAVE_SYS_LIMITS_H |
---|
53 | # include <sys/limits.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | #ifdef HAVE_NET_IF_ARP_H |
---|
57 | # include <net/if_arp.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | #ifdef HAVE_NET_IF_H |
---|
61 | # include <net/if.h> |
---|
62 | #endif |
---|
63 | |
---|
64 | #ifdef HAVE_NETINET_IN_H |
---|
65 | # include <netinet/in.h> |
---|
66 | #endif |
---|
67 | |
---|
68 | #ifdef HAVE_NET_ETHERNET_H |
---|
69 | # include <net/ethernet.h> |
---|
70 | #endif |
---|
71 | |
---|
72 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
73 | # include <netinet/if_ether.h> |
---|
74 | #endif |
---|
75 | |
---|
76 | #include <time.h> |
---|
77 | #ifdef WIN32 |
---|
78 | #include <sys/timeb.h> |
---|
79 | #endif |
---|
80 | |
---|
81 | #include "libtrace.h" |
---|
82 | #include "libtrace_int.h" |
---|
83 | |
---|
84 | #ifdef HAVE_PCAP_BPF_H |
---|
85 | # include <pcap-bpf.h> |
---|
86 | #else |
---|
87 | # ifdef HAVE_NET_BPF_H |
---|
88 | # include <net/bpf.h> |
---|
89 | # endif |
---|
90 | #endif |
---|
91 | |
---|
92 | |
---|
93 | #include "libtrace_int.h" |
---|
94 | #include "format_helper.h" |
---|
95 | #include "rt_protocol.h" |
---|
96 | #include "hash_toeplitz.h" |
---|
97 | #include "combiners.h" |
---|
98 | |
---|
99 | #include <pthread.h> |
---|
100 | #include <signal.h> |
---|
101 | #include <unistd.h> |
---|
102 | #include <ctype.h> |
---|
103 | |
---|
104 | static inline int delay_tracetime(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t); |
---|
105 | extern int libtrace_parallel; |
---|
106 | |
---|
107 | struct mem_stats { |
---|
108 | struct memfail { |
---|
109 | uint64_t cache_hit; |
---|
110 | uint64_t ring_hit; |
---|
111 | uint64_t miss; |
---|
112 | uint64_t recycled; |
---|
113 | } readbulk, read, write, writebulk; |
---|
114 | }; |
---|
115 | |
---|
116 | // Grrr gcc wants this spelt out |
---|
117 | __thread struct mem_stats mem_hits = {{0},{0},{0},{0}}; |
---|
118 | |
---|
119 | static void print_memory_stats() { |
---|
120 | #if 0 |
---|
121 | char t_name[50]; |
---|
122 | uint64_t total; |
---|
123 | pthread_getname_np(pthread_self(), t_name, sizeof(t_name)); |
---|
124 | |
---|
125 | fprintf(stderr, "Thread ID#%d - %s\n", (int) pthread_self(), t_name); |
---|
126 | |
---|
127 | total = mem_hits.read.cache_hit + mem_hits.read.ring_hit + mem_hits.read.miss; |
---|
128 | if (total) { |
---|
129 | fprintf(stderr, "\tRead:\n\t---CHits=%"PRIu64"\n\t---RHits=%"PRIu64"\n\t---Misses=%"PRIu64"\n\t---Recycled=%"PRIu64"\n", |
---|
130 | mem_hits.read.cache_hit, mem_hits.read.ring_hit, mem_hits.read.miss, mem_hits.read.recycled); |
---|
131 | fprintf(stderr, "\t---Total=%"PRIu64"\n\t---Miss %%=%f\n", |
---|
132 | total, (double) mem_hits.read.miss / (double) total * 100.0); |
---|
133 | } |
---|
134 | |
---|
135 | total = mem_hits.readbulk.cache_hit + mem_hits.readbulk.ring_hit + mem_hits.readbulk.miss; |
---|
136 | if (total) { |
---|
137 | fprintf(stderr, "\tReadbulk:\n\t---CHits=%"PRIu64"\n\t---RHits=%"PRIu64"\n\t---Misses=%"PRIu64"\n\t---Recycled=%"PRIu64"\n", |
---|
138 | mem_hits.readbulk.cache_hit, mem_hits.readbulk.ring_hit, mem_hits.readbulk.miss, mem_hits.readbulk.recycled); |
---|
139 | |
---|
140 | |
---|
141 | fprintf(stderr, "\t---Total=%"PRIu64"\n\t---Miss %%=%f\n", |
---|
142 | total, (double) mem_hits.readbulk.miss / (double) total * 100.0); |
---|
143 | } |
---|
144 | |
---|
145 | total = mem_hits.write.cache_hit + mem_hits.write.ring_hit + mem_hits.write.miss; |
---|
146 | if (total) { |
---|
147 | fprintf(stderr, "\tWrite:\n\t---CHits=%"PRIu64"\n\t---RHits=%"PRIu64"\n\t---Misses=%"PRIu64"\n\t---Recycled=%"PRIu64"\n", |
---|
148 | mem_hits.write.cache_hit, mem_hits.write.ring_hit, mem_hits.write.miss, mem_hits.write.recycled); |
---|
149 | |
---|
150 | fprintf(stderr, "\t---Total=%"PRIu64"\n\t---Miss %%=%f\n", |
---|
151 | total, (double) mem_hits.write.miss / (double) total * 100.0); |
---|
152 | } |
---|
153 | |
---|
154 | total = mem_hits.writebulk.cache_hit + mem_hits.writebulk.ring_hit + mem_hits.writebulk.miss; |
---|
155 | if (total) { |
---|
156 | fprintf(stderr, "\tWritebulk:\n\t---CHits=%"PRIu64"\n\t---RHits=%"PRIu64"\n\t---Misses=%"PRIu64"\n\t---Recycled=%"PRIu64"\n", |
---|
157 | mem_hits.writebulk.cache_hit, mem_hits.writebulk.ring_hit, mem_hits.writebulk.miss, mem_hits.writebulk.recycled); |
---|
158 | |
---|
159 | fprintf(stderr, "\t---Total=%"PRIu64"\n\t---Miss %%=%f\n", |
---|
160 | total, (double) mem_hits.writebulk.miss / (double) total * 100.0); |
---|
161 | } |
---|
162 | #endif |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * This can be used once the hasher thread has been started and internally after |
---|
167 | * verfiy_configuration. |
---|
168 | * |
---|
169 | * @return true if the trace has dedicated hasher thread otherwise false. |
---|
170 | */ |
---|
171 | inline bool trace_has_dedicated_hasher(libtrace_t * libtrace) |
---|
172 | { |
---|
173 | return libtrace->hasher_thread.type == THREAD_HASHER; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * True if the trace has dedicated hasher thread otherwise false, |
---|
178 | * to be used after the trace is running |
---|
179 | */ |
---|
180 | static inline int trace_has_dedicated_reporter(libtrace_t * libtrace) |
---|
181 | { |
---|
182 | assert(libtrace->state != STATE_NEW); |
---|
183 | return libtrace->reporter_thread.type == THREAD_REPORTER && libtrace->reporter; |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * When running the number of perpkt threads in use. |
---|
188 | * TODO what if the trace is not running yet, or has finished?? |
---|
189 | * |
---|
190 | * @brief libtrace_perpkt_thread_nb |
---|
191 | * @param t The trace |
---|
192 | * @return |
---|
193 | */ |
---|
194 | DLLEXPORT int libtrace_get_perpkt_count(libtrace_t * t) { |
---|
195 | return t->perpkt_thread_count; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * Changes a thread's state and broadcasts the condition variable. This |
---|
200 | * should always be done when the lock is held. |
---|
201 | * |
---|
202 | * Additionally for perpkt threads the state counts are updated. |
---|
203 | * |
---|
204 | * @param trace A pointer to the trace |
---|
205 | * @param t A pointer to the thread to modify |
---|
206 | * @param new_state The new state of the thread |
---|
207 | * @param need_lock Set to true if libtrace_lock is not held, otherwise |
---|
208 | * false in the case the lock is currently held by this thread. |
---|
209 | */ |
---|
210 | static inline void thread_change_state(libtrace_t *trace, libtrace_thread_t *t, |
---|
211 | const enum thread_states new_state, const bool need_lock) |
---|
212 | { |
---|
213 | enum thread_states prev_state; |
---|
214 | if (need_lock) |
---|
215 | pthread_mutex_lock(&trace->libtrace_lock); |
---|
216 | prev_state = t->state; |
---|
217 | t->state = new_state; |
---|
218 | if (t->type == THREAD_PERPKT) { |
---|
219 | --trace->perpkt_thread_states[prev_state]; |
---|
220 | ++trace->perpkt_thread_states[new_state]; |
---|
221 | } |
---|
222 | |
---|
223 | if (trace->config.debug_state) |
---|
224 | fprintf(stderr, "Thread %d state changed from %d to %d\n", |
---|
225 | (int) t->tid, prev_state, t->state); |
---|
226 | |
---|
227 | pthread_cond_broadcast(&trace->perpkt_cond); |
---|
228 | if (need_lock) |
---|
229 | pthread_mutex_unlock(&trace->libtrace_lock); |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * Changes the overall traces state and signals the condition. |
---|
234 | * |
---|
235 | * @param trace A pointer to the trace |
---|
236 | * @param new_state The new state of the trace |
---|
237 | * @param need_lock Set to true if libtrace_lock is not held, otherwise |
---|
238 | * false in the case the lock is currently held by this thread. |
---|
239 | */ |
---|
240 | static inline void libtrace_change_state(libtrace_t *trace, |
---|
241 | const enum trace_state new_state, const bool need_lock) |
---|
242 | { |
---|
243 | UNUSED enum trace_state prev_state; |
---|
244 | if (need_lock) |
---|
245 | pthread_mutex_lock(&trace->libtrace_lock); |
---|
246 | prev_state = trace->state; |
---|
247 | trace->state = new_state; |
---|
248 | |
---|
249 | if (trace->config.debug_state) |
---|
250 | fprintf(stderr, "Trace(%s) state changed from %s to %s\n", |
---|
251 | trace->uridata, get_trace_state_name(prev_state), |
---|
252 | get_trace_state_name(trace->state)); |
---|
253 | |
---|
254 | pthread_cond_broadcast(&trace->perpkt_cond); |
---|
255 | if (need_lock) |
---|
256 | pthread_mutex_unlock(&trace->libtrace_lock); |
---|
257 | } |
---|
258 | |
---|
259 | /** |
---|
260 | * This is valid once a trace is initialised |
---|
261 | * |
---|
262 | * @return True if the format supports parallel threads. |
---|
263 | */ |
---|
264 | static inline bool trace_supports_parallel(libtrace_t *trace) |
---|
265 | { |
---|
266 | assert(trace); |
---|
267 | assert(trace->format); |
---|
268 | if (trace->format->pstart_input) |
---|
269 | return true; |
---|
270 | else |
---|
271 | return false; |
---|
272 | } |
---|
273 | |
---|
274 | void libtrace_zero_thread(libtrace_thread_t * t) { |
---|
275 | t->accepted_packets = 0; |
---|
276 | t->filtered_packets = 0; |
---|
277 | t->recorded_first = false; |
---|
278 | t->tracetime_offset_usec = 0; |
---|
279 | t->user_data = 0; |
---|
280 | t->format_data = 0; |
---|
281 | libtrace_zero_ringbuffer(&t->rbuffer); |
---|
282 | t->trace = NULL; |
---|
283 | t->ret = NULL; |
---|
284 | t->type = THREAD_EMPTY; |
---|
285 | t->perpkt_num = -1; |
---|
286 | } |
---|
287 | |
---|
288 | // Ints are aligned int is atomic so safe to read and write at same time |
---|
289 | // However write must be locked, read doesn't (We never try read before written to table) |
---|
290 | libtrace_thread_t * get_thread_table(libtrace_t *libtrace) { |
---|
291 | int i = 0; |
---|
292 | pthread_t tid = pthread_self(); |
---|
293 | |
---|
294 | for (;i<libtrace->perpkt_thread_count ;++i) { |
---|
295 | if (pthread_equal(tid, libtrace->perpkt_threads[i].tid)) |
---|
296 | return &libtrace->perpkt_threads[i]; |
---|
297 | } |
---|
298 | return NULL; |
---|
299 | } |
---|
300 | |
---|
301 | int get_thread_table_num(libtrace_t *libtrace) { |
---|
302 | int i = 0; |
---|
303 | pthread_t tid = pthread_self(); |
---|
304 | for (;i<libtrace->perpkt_thread_count; ++i) { |
---|
305 | if (pthread_equal(tid, libtrace->perpkt_threads[i].tid)) |
---|
306 | return i; |
---|
307 | } |
---|
308 | return -1; |
---|
309 | } |
---|
310 | |
---|
311 | static libtrace_thread_t * get_thread_descriptor(libtrace_t *libtrace) { |
---|
312 | libtrace_thread_t *ret; |
---|
313 | if (!(ret = get_thread_table(libtrace))) { |
---|
314 | pthread_t tid = pthread_self(); |
---|
315 | // Check if we are reporter or something else |
---|
316 | if (pthread_equal(tid, libtrace->reporter_thread.tid)) |
---|
317 | ret = &libtrace->reporter_thread; |
---|
318 | else if (pthread_equal(tid, libtrace->hasher_thread.tid)) |
---|
319 | ret = &libtrace->hasher_thread; |
---|
320 | else |
---|
321 | ret = NULL; |
---|
322 | } |
---|
323 | return ret; |
---|
324 | } |
---|
325 | |
---|
326 | /** Makes a packet safe, a packet may become invaild after a |
---|
327 | * pause (or stop/destroy) of a trace. This copies a packet |
---|
328 | * in such a way that it will be able to survive a pause. |
---|
329 | * |
---|
330 | * However this will not allow the packet to be used after |
---|
331 | * the format is destroyed. Or while the trace is still paused. |
---|
332 | */ |
---|
333 | DLLEXPORT void libtrace_make_packet_safe(libtrace_packet_t *pkt) { |
---|
334 | // Duplicate the packet in standard malloc'd memory and free the |
---|
335 | // original, This is a 1:1 exchange so is ocache count remains unchanged. |
---|
336 | if (pkt->buf_control != TRACE_CTRL_PACKET) { |
---|
337 | libtrace_packet_t *dup; |
---|
338 | dup = trace_copy_packet(pkt); |
---|
339 | /* Release the external buffer */ |
---|
340 | trace_fin_packet(pkt); |
---|
341 | /* Copy the duplicated packet over the existing */ |
---|
342 | memcpy(pkt, dup, sizeof(libtrace_packet_t)); |
---|
343 | } |
---|
344 | } |
---|
345 | |
---|
346 | /** |
---|
347 | * Makes a libtrace_result_t safe, used when pausing a trace. |
---|
348 | * This will call libtrace_make_packet_safe if the result is |
---|
349 | * a packet. |
---|
350 | */ |
---|
351 | DLLEXPORT void libtrace_make_result_safe(libtrace_result_t *res) { |
---|
352 | if (res->type == RESULT_PACKET) { |
---|
353 | libtrace_make_packet_safe(res->value.pkt); |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | /** |
---|
358 | * Holds threads in a paused state, until released by broadcasting |
---|
359 | * the condition mutex. |
---|
360 | */ |
---|
361 | static void trace_thread_pause(libtrace_t *trace, libtrace_thread_t *t) { |
---|
362 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
363 | thread_change_state(trace, t, THREAD_PAUSED, false); |
---|
364 | while (trace->state == STATE_PAUSED || trace->state == STATE_PAUSING) { |
---|
365 | ASSERT_RET(pthread_cond_wait(&trace->perpkt_cond, &trace->libtrace_lock), == 0); |
---|
366 | } |
---|
367 | thread_change_state(trace, t, THREAD_RUNNING, false); |
---|
368 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
369 | } |
---|
370 | |
---|
371 | /** |
---|
372 | * Sends a packet to the user, expects either a valid packet or a TICK packet. |
---|
373 | * |
---|
374 | * @param trace The trace |
---|
375 | * @param t The current thread |
---|
376 | * @param packet A pointer to the packet storage, which may be set to null upon |
---|
377 | * return, or a packet to be finished. |
---|
378 | * @return 0 is successful, otherwise if playing back in tracetime |
---|
379 | * READ_MESSAGE(-2) can be returned in which case the packet is not sent. |
---|
380 | * |
---|
381 | * @note READ_MESSAGE will only be returned if tracetime is true. |
---|
382 | */ |
---|
383 | static inline int dispatch_packet(libtrace_t *trace, |
---|
384 | libtrace_thread_t *t, |
---|
385 | libtrace_packet_t **packet, |
---|
386 | bool tracetime) { |
---|
387 | |
---|
388 | if ((*packet)->error > 0) { |
---|
389 | if (tracetime) { |
---|
390 | if (delay_tracetime(trace, packet[0], t) == READ_MESSAGE) |
---|
391 | return READ_MESSAGE; |
---|
392 | } |
---|
393 | t->accepted_packets++; |
---|
394 | libtrace_generic_t data = {.pkt = *packet}; |
---|
395 | *packet = (*trace->per_pkt)(trace, t, MESSAGE_PACKET, data, t); |
---|
396 | trace_fin_packet(*packet); |
---|
397 | } else { |
---|
398 | assert((*packet)->error == READ_TICK); |
---|
399 | libtrace_generic_t data = {.uint64 = trace_packet_get_order(*packet)}; |
---|
400 | (*trace->per_pkt)(trace, t, MESSAGE_TICK, data, t); |
---|
401 | } |
---|
402 | return 0; |
---|
403 | } |
---|
404 | |
---|
405 | /** |
---|
406 | * Sends a batch of packets to the user, expects either a valid packet or a |
---|
407 | * TICK packet. |
---|
408 | * |
---|
409 | * @param trace The trace |
---|
410 | * @param t The current thread |
---|
411 | * @param packets [in,out] An array of packets, these may be null upon return |
---|
412 | * @param nb_packets The total number of packets in the list |
---|
413 | * @param empty [in,out] A pointer to an integer storing the first empty slot, |
---|
414 | * upon return this is updated |
---|
415 | * @param offset [in,out] The offset into the array, upon return this is updated |
---|
416 | * @return 0 is successful, otherwise if playing back in tracetime |
---|
417 | * READ_MESSAGE(-2) can be returned in which case the packet is not sent. |
---|
418 | * |
---|
419 | * @note READ_MESSAGE will only be returned if tracetime is true. |
---|
420 | */ |
---|
421 | static inline int dispatch_packets(libtrace_t *trace, |
---|
422 | libtrace_thread_t *t, |
---|
423 | libtrace_packet_t *packets[], |
---|
424 | int nb_packets, int *empty, int *offset, |
---|
425 | bool tracetime) { |
---|
426 | for (;*offset < nb_packets; ++*offset) { |
---|
427 | int ret; |
---|
428 | ret = dispatch_packet(trace, t, &packets[*offset], tracetime); |
---|
429 | if (ret == 0) { |
---|
430 | /* Move full slots to front as we go */ |
---|
431 | if (packets[*offset]) { |
---|
432 | if (*empty != *offset) { |
---|
433 | packets[*empty] = packets[*offset]; |
---|
434 | packets[*offset] = NULL; |
---|
435 | } |
---|
436 | ++*empty; |
---|
437 | } |
---|
438 | } else { |
---|
439 | /* Break early */ |
---|
440 | assert(ret == READ_MESSAGE); |
---|
441 | return READ_MESSAGE; |
---|
442 | } |
---|
443 | } |
---|
444 | |
---|
445 | return 0; |
---|
446 | } |
---|
447 | |
---|
448 | /** |
---|
449 | * Pauses a per packet thread, messages will not be processed when the thread |
---|
450 | * is paused. |
---|
451 | * |
---|
452 | * This process involves reading packets if a hasher thread is used. As such |
---|
453 | * this function can fail to pause due to errors when reading in which case |
---|
454 | * the thread should be stopped instead. |
---|
455 | * |
---|
456 | * |
---|
457 | * @brief trace_perpkt_thread_pause |
---|
458 | * @return READ_ERROR(-1) or READ_EOF(0) or 1 if successfull |
---|
459 | */ |
---|
460 | static int trace_perpkt_thread_pause(libtrace_t *trace, libtrace_thread_t *t, |
---|
461 | libtrace_packet_t *packets[], |
---|
462 | int nb_packets, int *empty, int *offset) { |
---|
463 | libtrace_packet_t * packet = NULL; |
---|
464 | |
---|
465 | /* Let the user thread know we are going to pause */ |
---|
466 | (*trace->per_pkt)(trace, t, MESSAGE_PAUSING, (libtrace_generic_t){0}, t); |
---|
467 | |
---|
468 | /* Send through any remaining packets (or messages) without delay */ |
---|
469 | |
---|
470 | /* First send those packets already read, as fast as possible |
---|
471 | * This should never fail or check for messages etc. */ |
---|
472 | ASSERT_RET(dispatch_packets(trace, t, packets, nb_packets, empty, |
---|
473 | offset, false), == 0); |
---|
474 | |
---|
475 | libtrace_ocache_alloc(&trace->packet_freelist, (void **) &packet, 1, 1); |
---|
476 | /* If a hasher thread is running, empty input queues so we don't lose data */ |
---|
477 | if (trace_has_dedicated_hasher(trace)) { |
---|
478 | fprintf(stderr, "Trace is using a hasher thread emptying queues\n"); |
---|
479 | // The hasher has stopped by this point, so the queue shouldn't be filling |
---|
480 | while(!libtrace_ringbuffer_is_empty(&t->rbuffer) || t->format_data) { |
---|
481 | int ret = trace->pread(trace, t, &packet, 1); |
---|
482 | if (ret == 1) { |
---|
483 | if (packet->error > 0) { |
---|
484 | store_first_packet(trace, packet, t); |
---|
485 | } |
---|
486 | ASSERT_RET(dispatch_packet(trace, t, &packet, 1), == 0); |
---|
487 | if (packet == NULL) |
---|
488 | libtrace_ocache_alloc(&trace->packet_freelist, (void **) &packet, 1, 1); |
---|
489 | } else if (ret != READ_MESSAGE) { |
---|
490 | /* Ignore messages we pick these up next loop */ |
---|
491 | assert (ret == READ_EOF || ret == READ_ERROR); |
---|
492 | /* Verify no packets are remaining */ |
---|
493 | /* TODO refactor this sanity check out!! */ |
---|
494 | while (!libtrace_ringbuffer_is_empty(&t->rbuffer)) { |
---|
495 | ASSERT_RET(trace->pread(trace, t, &packet, 1), <= 0); |
---|
496 | // No packets after this should have any data in them |
---|
497 | assert(packet->error <= 0); |
---|
498 | } |
---|
499 | fprintf(stderr, "PREAD_FAILED %d\n", ret); |
---|
500 | libtrace_ocache_free(&trace->packet_freelist, (void **) &packet, 1, 1); |
---|
501 | return -1; |
---|
502 | } |
---|
503 | } |
---|
504 | } |
---|
505 | libtrace_ocache_free(&trace->packet_freelist, (void **) &packet, 1, 1); |
---|
506 | |
---|
507 | /* Now we do the actual pause, this returns when we resumed */ |
---|
508 | trace_thread_pause(trace, t); |
---|
509 | (*trace->per_pkt)(trace, t, MESSAGE_RESUMING, (libtrace_generic_t){0}, t); |
---|
510 | return 1; |
---|
511 | } |
---|
512 | |
---|
513 | /** |
---|
514 | * The is the entry point for our packet processing threads. |
---|
515 | */ |
---|
516 | static void* perpkt_threads_entry(void *data) { |
---|
517 | libtrace_t *trace = (libtrace_t *)data; |
---|
518 | libtrace_thread_t *t; |
---|
519 | libtrace_message_t message = {0}; |
---|
520 | libtrace_packet_t *packets[trace->config.burst_size]; |
---|
521 | size_t i; |
---|
522 | //int ret; |
---|
523 | /* The current reading position into the packets */ |
---|
524 | int offset = 0; |
---|
525 | /* The number of packets last read */ |
---|
526 | int nb_packets = 0; |
---|
527 | /* The offset to the first NULL packet upto offset */ |
---|
528 | int empty = 0; |
---|
529 | |
---|
530 | /* Wait until trace_pstart has been completed */ |
---|
531 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
532 | t = get_thread_table(trace); |
---|
533 | assert(t); |
---|
534 | if (trace->state == STATE_ERROR) { |
---|
535 | thread_change_state(trace, t, THREAD_FINISHED, false); |
---|
536 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
537 | pthread_exit(NULL); |
---|
538 | } |
---|
539 | //printf("Yay Started perpkt thread #%d\n", (int) get_thread_table_num(trace)); |
---|
540 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
541 | |
---|
542 | if (trace->format->pregister_thread) { |
---|
543 | trace->format->pregister_thread(trace, t, !trace_has_dedicated_hasher(trace)); |
---|
544 | } |
---|
545 | |
---|
546 | /* Fill our buffer with empty packets */ |
---|
547 | memset(&packets, 0, sizeof(void*) * trace->config.burst_size); |
---|
548 | libtrace_ocache_alloc(&trace->packet_freelist, (void **) packets, |
---|
549 | trace->config.burst_size, |
---|
550 | trace->config.burst_size); |
---|
551 | |
---|
552 | /* ~~~~~~~~~~~ Setup complete now we loop ~~~~~~~~~~~~~~~ */ |
---|
553 | |
---|
554 | /* Let the per_packet function know we have started */ |
---|
555 | (*trace->per_pkt)(trace, t, MESSAGE_STARTING, (libtrace_generic_t){0}, t); |
---|
556 | (*trace->per_pkt)(trace, t, MESSAGE_RESUMING, (libtrace_generic_t){0}, t); |
---|
557 | |
---|
558 | for (;;) { |
---|
559 | |
---|
560 | if (libtrace_message_queue_try_get(&t->messages, &message) != LIBTRACE_MQ_FAILED) { |
---|
561 | int ret; |
---|
562 | switch (message.code) { |
---|
563 | case MESSAGE_DO_PAUSE: // This is internal |
---|
564 | ret = trace_perpkt_thread_pause(trace, t, |
---|
565 | packets, nb_packets, &empty, &offset); |
---|
566 | if (ret == READ_EOF) { |
---|
567 | fprintf(stderr, "PAUSE stop eof!!\n"); |
---|
568 | goto eof; |
---|
569 | } else if (ret == READ_ERROR) { |
---|
570 | fprintf(stderr, "PAUSE stop error!!\n"); |
---|
571 | goto error; |
---|
572 | } |
---|
573 | assert(ret == 1); |
---|
574 | continue; |
---|
575 | case MESSAGE_DO_STOP: // This is internal |
---|
576 | fprintf(stderr, "DO_STOP stop!!\n"); |
---|
577 | goto eof; |
---|
578 | } |
---|
579 | (*trace->per_pkt)(trace, t, message.code, message.additional, message.sender); |
---|
580 | /* Continue and the empty messages out before packets */ |
---|
581 | continue; |
---|
582 | } |
---|
583 | |
---|
584 | |
---|
585 | /* Do we need to read a new set of packets MOST LIKELY we do */ |
---|
586 | if (offset == nb_packets) { |
---|
587 | /* Refill the packet buffer */ |
---|
588 | if (empty != nb_packets) { |
---|
589 | // Refill the empty packets |
---|
590 | libtrace_ocache_alloc(&trace->packet_freelist, |
---|
591 | (void **) &packets[empty], |
---|
592 | nb_packets - empty, |
---|
593 | nb_packets - empty); |
---|
594 | } |
---|
595 | if (!trace->pread) { |
---|
596 | assert(packets[0]); |
---|
597 | nb_packets = trace_read_packet(trace, packets[0]); |
---|
598 | packets[0]->error = nb_packets; |
---|
599 | if (nb_packets > 0) |
---|
600 | nb_packets = 1; |
---|
601 | } else { |
---|
602 | nb_packets = trace->pread(trace, t, packets, trace->config.burst_size); |
---|
603 | } |
---|
604 | offset = 0; |
---|
605 | empty = 0; |
---|
606 | } |
---|
607 | |
---|
608 | /* Handle error/message cases */ |
---|
609 | if (nb_packets > 0) { |
---|
610 | /* Store the first packet */ |
---|
611 | if (packets[0]->error > 0) { |
---|
612 | store_first_packet(trace, packets[0], t); |
---|
613 | } |
---|
614 | dispatch_packets(trace, t, packets, nb_packets, &empty, |
---|
615 | &offset, trace->tracetime); |
---|
616 | } else { |
---|
617 | switch (nb_packets) { |
---|
618 | case READ_EOF: |
---|
619 | fprintf(stderr, "EOF stop %d!!\n", nb_packets); |
---|
620 | goto eof; |
---|
621 | case READ_ERROR: |
---|
622 | fprintf(stderr, "ERROR stop %d!!\n", nb_packets); |
---|
623 | goto error; |
---|
624 | case READ_MESSAGE: |
---|
625 | nb_packets = 0; |
---|
626 | continue; |
---|
627 | default: |
---|
628 | fprintf(stderr, "Unexpected error %d!!\n", nb_packets); |
---|
629 | goto error; |
---|
630 | } |
---|
631 | } |
---|
632 | |
---|
633 | } |
---|
634 | |
---|
635 | error: |
---|
636 | fprintf(stderr, "An error occured in trace\n"); |
---|
637 | message.code = MESSAGE_DO_STOP; |
---|
638 | message.sender = t; |
---|
639 | message.additional.uint64 = 0; |
---|
640 | trace_send_message_to_perpkts(trace, &message); |
---|
641 | eof: |
---|
642 | fprintf(stderr, "An eof occured in trace\n"); |
---|
643 | /* ~~~~~~~~~~~~~~ Trace is finished do tear down ~~~~~~~~~~~~~~~~~~~~~ */ |
---|
644 | |
---|
645 | // Let the per_packet function know we have stopped |
---|
646 | (*trace->per_pkt)(trace, t, MESSAGE_PAUSING, (libtrace_generic_t){0}, t); |
---|
647 | (*trace->per_pkt)(trace, t, MESSAGE_STOPPING, (libtrace_generic_t){0}, t); |
---|
648 | |
---|
649 | // Free any remaining packets |
---|
650 | for (i = 0; i < trace->config.burst_size; i++) { |
---|
651 | if (packets[i]) { |
---|
652 | libtrace_ocache_free(&trace->packet_freelist, (void **) &packets[i], 1, 1); |
---|
653 | packets[i] = NULL; |
---|
654 | } |
---|
655 | } |
---|
656 | |
---|
657 | thread_change_state(trace, t, THREAD_FINISHED, true); |
---|
658 | |
---|
659 | // Notify only after we've defiantly set the state to finished |
---|
660 | message.code = MESSAGE_PERPKT_ENDED; |
---|
661 | message.additional.uint64 = 0; |
---|
662 | trace_send_message_to_reporter(trace, &message); |
---|
663 | |
---|
664 | // Release all ocache memory before unregistering with the format |
---|
665 | // because this might(it does in DPDK) unlink the formats mempool |
---|
666 | // causing destroy/finish packet to fail. |
---|
667 | libtrace_ocache_unregister_thread(&trace->packet_freelist); |
---|
668 | if (trace->format->punregister_thread) { |
---|
669 | trace->format->punregister_thread(trace, t); |
---|
670 | } |
---|
671 | print_memory_stats(); |
---|
672 | |
---|
673 | pthread_exit(NULL); |
---|
674 | }; |
---|
675 | |
---|
676 | /** |
---|
677 | * The start point for our single threaded hasher thread, this will read |
---|
678 | * and hash a packet from a data source and queue it against the correct |
---|
679 | * core to process it. |
---|
680 | */ |
---|
681 | static void* hasher_entry(void *data) { |
---|
682 | libtrace_t *trace = (libtrace_t *)data; |
---|
683 | libtrace_thread_t * t; |
---|
684 | int i; |
---|
685 | libtrace_packet_t * packet; |
---|
686 | libtrace_message_t message = {0}; |
---|
687 | int pkt_skipped = 0; |
---|
688 | |
---|
689 | assert(trace_has_dedicated_hasher(trace)); |
---|
690 | /* Wait until all threads are started and objects are initialised (ring buffers) */ |
---|
691 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
692 | t = &trace->hasher_thread; |
---|
693 | assert(t->type == THREAD_HASHER && pthread_equal(pthread_self(), t->tid)); |
---|
694 | if (trace->state == STATE_ERROR) { |
---|
695 | thread_change_state(trace, t, THREAD_FINISHED, false); |
---|
696 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
697 | pthread_exit(NULL); |
---|
698 | } |
---|
699 | |
---|
700 | printf("Hasher Thread started\n"); |
---|
701 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
702 | |
---|
703 | if (trace->format->pregister_thread) { |
---|
704 | trace->format->pregister_thread(trace, t, true); |
---|
705 | } |
---|
706 | |
---|
707 | /* Read all packets in then hash and queue against the correct thread */ |
---|
708 | while (1) { |
---|
709 | int thread; |
---|
710 | if (!pkt_skipped) |
---|
711 | libtrace_ocache_alloc(&trace->packet_freelist, (void **) &packet, 1, 1); |
---|
712 | assert(packet); |
---|
713 | |
---|
714 | if (libtrace_halt) { |
---|
715 | packet->error = 0; |
---|
716 | break; |
---|
717 | } |
---|
718 | |
---|
719 | // Check for messages that we expect MESSAGE_DO_PAUSE, (internal messages only) |
---|
720 | if (libtrace_message_queue_try_get(&t->messages, &message) != LIBTRACE_MQ_FAILED) { |
---|
721 | switch(message.code) { |
---|
722 | case MESSAGE_DO_PAUSE: |
---|
723 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
724 | thread_change_state(trace, t, THREAD_PAUSED, false); |
---|
725 | pthread_cond_broadcast(&trace->perpkt_cond); |
---|
726 | while (trace->state == STATE_PAUSED || trace->state == STATE_PAUSING) { |
---|
727 | ASSERT_RET(pthread_cond_wait(&trace->perpkt_cond, &trace->libtrace_lock), == 0); |
---|
728 | } |
---|
729 | thread_change_state(trace, t, THREAD_RUNNING, false); |
---|
730 | pthread_cond_broadcast(&trace->perpkt_cond); |
---|
731 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
732 | break; |
---|
733 | case MESSAGE_DO_STOP: |
---|
734 | assert(trace->started == false); |
---|
735 | assert(trace->state == STATE_FINSHED); |
---|
736 | /* Mark the current packet as EOF */ |
---|
737 | packet->error = 0; |
---|
738 | break; |
---|
739 | default: |
---|
740 | fprintf(stderr, "Hasher thread didn't expect message code=%d\n", message.code); |
---|
741 | } |
---|
742 | pkt_skipped = 1; |
---|
743 | continue; |
---|
744 | } |
---|
745 | |
---|
746 | if ((packet->error = trace_read_packet(trace, packet)) <1) { |
---|
747 | break; /* We are EOF or error'd either way we stop */ |
---|
748 | } |
---|
749 | |
---|
750 | /* We are guaranteed to have a hash function i.e. != NULL */ |
---|
751 | trace_packet_set_hash(packet, (*trace->hasher)(packet, trace->hasher_data)); |
---|
752 | thread = trace_packet_get_hash(packet) % trace->perpkt_thread_count; |
---|
753 | /* Blocking write to the correct queue - I'm the only writer */ |
---|
754 | if (trace->perpkt_threads[thread].state != THREAD_FINISHED) { |
---|
755 | uint64_t order = trace_packet_get_order(packet); |
---|
756 | libtrace_ringbuffer_write(&trace->perpkt_threads[thread].rbuffer, packet); |
---|
757 | if (trace->config.tick_count && order % trace->config.tick_count == 0) { |
---|
758 | // Write ticks to everyone else |
---|
759 | libtrace_packet_t * pkts[trace->perpkt_thread_count]; |
---|
760 | memset(pkts, 0, sizeof(void *) * trace->perpkt_thread_count); |
---|
761 | libtrace_ocache_alloc(&trace->packet_freelist, (void **) pkts, trace->perpkt_thread_count, trace->perpkt_thread_count); |
---|
762 | for (i = 0; i < trace->perpkt_thread_count; i++) { |
---|
763 | pkts[i]->error = READ_TICK; |
---|
764 | trace_packet_set_order(pkts[i], order); |
---|
765 | libtrace_ringbuffer_write(&trace->perpkt_threads[i].rbuffer, pkts[i]); |
---|
766 | } |
---|
767 | } |
---|
768 | pkt_skipped = 0; |
---|
769 | } else { |
---|
770 | assert(!"Dropping a packet!!"); |
---|
771 | pkt_skipped = 1; // Reuse that packet no one read it |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | /* Broadcast our last failed read to all threads */ |
---|
776 | for (i = 0; i < trace->perpkt_thread_count; i++) { |
---|
777 | libtrace_packet_t * bcast; |
---|
778 | fprintf(stderr, "Broadcasting error/EOF now the trace is over\n"); |
---|
779 | if (i == trace->perpkt_thread_count - 1) { |
---|
780 | bcast = packet; |
---|
781 | } else { |
---|
782 | libtrace_ocache_alloc(&trace->packet_freelist, (void **) &bcast, 1, 1); |
---|
783 | bcast->error = packet->error; |
---|
784 | } |
---|
785 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
786 | if (trace->perpkt_threads[i].state != THREAD_FINISHED) { |
---|
787 | // Unlock early otherwise we could deadlock |
---|
788 | libtrace_ringbuffer_write(&trace->perpkt_threads[i].rbuffer, bcast); |
---|
789 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
790 | } else { |
---|
791 | fprintf(stderr, "SKIPPING THREAD !!!%d!!!/n", (int) i); |
---|
792 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
793 | } |
---|
794 | } |
---|
795 | |
---|
796 | // We don't need to free the packet |
---|
797 | thread_change_state(trace, t, THREAD_FINISHED, true); |
---|
798 | |
---|
799 | // Notify only after we've defiantly set the state to finished |
---|
800 | message.code = MESSAGE_PERPKT_ENDED; |
---|
801 | message.additional.uint64 = 0; |
---|
802 | trace_send_message_to_reporter(trace, &message); |
---|
803 | libtrace_ocache_unregister_thread(&trace->packet_freelist); |
---|
804 | if (trace->format->punregister_thread) { |
---|
805 | trace->format->punregister_thread(trace, t); |
---|
806 | } |
---|
807 | print_memory_stats(); |
---|
808 | |
---|
809 | // TODO remove from TTABLE t sometime |
---|
810 | pthread_exit(NULL); |
---|
811 | }; |
---|
812 | |
---|
813 | /** |
---|
814 | * Moves src into dest(Complete copy) and copies the memory buffer and |
---|
815 | * its flags from dest into src ready for reuse without needing extra mallocs. |
---|
816 | */ |
---|
817 | static inline void swap_packets(libtrace_packet_t *dest, libtrace_packet_t *src) { |
---|
818 | // Save the passed in buffer status |
---|
819 | assert(dest->trace == NULL); // Must be a empty packet |
---|
820 | void * temp_buf = dest->buffer; |
---|
821 | buf_control_t temp_buf_control = dest->buf_control; |
---|
822 | // Completely copy StoredPacket into packet |
---|
823 | memcpy(dest, src, sizeof(libtrace_packet_t)); |
---|
824 | // Set the buffer settings on the returned packet |
---|
825 | src->buffer = temp_buf; |
---|
826 | src->buf_control = temp_buf_control; |
---|
827 | src->trace = NULL; |
---|
828 | } |
---|
829 | |
---|
830 | /* Our simplest case when a thread becomes ready it can obtain an exclusive |
---|
831 | * lock to read packets from the underlying trace. |
---|
832 | */ |
---|
833 | static int trace_pread_packet_first_in_first_served(libtrace_t *libtrace, |
---|
834 | libtrace_thread_t *t, |
---|
835 | libtrace_packet_t *packets[], |
---|
836 | size_t nb_packets) { |
---|
837 | size_t i = 0; |
---|
838 | //bool tick_hit = false; |
---|
839 | |
---|
840 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
841 | /* Read nb_packets */ |
---|
842 | for (i = 0; i < nb_packets; ++i) { |
---|
843 | if (libtrace_halt) { |
---|
844 | break; |
---|
845 | } |
---|
846 | packets[i]->error = trace_read_packet(libtrace, packets[i]); |
---|
847 | |
---|
848 | if (packets[i]->error <= 0) { |
---|
849 | /* We'll catch this next time if we have already got packets */ |
---|
850 | if ( i==0 ) { |
---|
851 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
852 | return packets[i]->error; |
---|
853 | } else { |
---|
854 | break; |
---|
855 | } |
---|
856 | } |
---|
857 | /* |
---|
858 | if (libtrace->config.tick_count && trace_packet_get_order(packets[i]) % libtrace->config.tick_count == 0) { |
---|
859 | tick_hit = true; |
---|
860 | }*/ |
---|
861 | } |
---|
862 | // Doing this inside the lock ensures the first packet is always |
---|
863 | // recorded first |
---|
864 | if (packets[0]->error > 0) { |
---|
865 | store_first_packet(libtrace, packets[0], t); |
---|
866 | } |
---|
867 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
868 | /* XXX TODO this needs to be inband with packets, or we don't bother in this case |
---|
869 | if (tick_hit) { |
---|
870 | libtrace_message_t tick; |
---|
871 | tick.additional.uint64 = trace_packet_get_order(packets[i]); |
---|
872 | tick.code = MESSAGE_TICK; |
---|
873 | trace_send_message_to_perpkts(libtrace, &tick); |
---|
874 | } */ |
---|
875 | return i; |
---|
876 | } |
---|
877 | |
---|
878 | /** |
---|
879 | * For the case that we have a dedicated hasher thread |
---|
880 | * 1. We read a packet from our buffer |
---|
881 | * 2. Move that into the packet provided (packet) |
---|
882 | */ |
---|
883 | inline static int trace_pread_packet_hasher_thread(libtrace_t *libtrace, |
---|
884 | libtrace_thread_t *t, |
---|
885 | libtrace_packet_t *packets[], |
---|
886 | size_t nb_packets) { |
---|
887 | size_t i; |
---|
888 | |
---|
889 | /* We store the last error message here */ |
---|
890 | if (t->format_data) { |
---|
891 | fprintf(stderr, "Hit me, ohh yeah got error %d\n", |
---|
892 | ((libtrace_packet_t *)t->format_data)->error); |
---|
893 | return ((libtrace_packet_t *)t->format_data)->error; |
---|
894 | } |
---|
895 | |
---|
896 | // Always grab at least one |
---|
897 | if (packets[0]) // Recycle the old get the new |
---|
898 | libtrace_ocache_free(&libtrace->packet_freelist, (void **) packets, 1, 1); |
---|
899 | packets[0] = libtrace_ringbuffer_read(&t->rbuffer); |
---|
900 | |
---|
901 | if (packets[0]->error <= 0 && packets[0]->error != READ_TICK) { |
---|
902 | fprintf(stderr, "Hit me, ohh yeah returning error %d\n", packets[0]->error); |
---|
903 | return packets[0]->error; |
---|
904 | } |
---|
905 | |
---|
906 | for (i = 1; i < nb_packets; i++) { |
---|
907 | if (packets[i]) // Recycle the old get the new |
---|
908 | libtrace_ocache_free(&libtrace->packet_freelist, (void **) &packets[i], 1, 1); |
---|
909 | if (!libtrace_ringbuffer_try_read(&t->rbuffer, (void **) &packets[i])) { |
---|
910 | packets[i] = NULL; |
---|
911 | break; |
---|
912 | } |
---|
913 | |
---|
914 | /* We will return an error or EOF the next time around */ |
---|
915 | if (packets[i]->error <= 0 && packets[0]->error != READ_TICK) { |
---|
916 | /* The message case will be checked automatically - |
---|
917 | However other cases like EOF and error will only be |
---|
918 | sent once*/ |
---|
919 | if (packets[i]->error != READ_MESSAGE) { |
---|
920 | assert(t->format_data == NULL); |
---|
921 | t->format_data = packets[i]; |
---|
922 | fprintf(stderr, "Hit me, ohh yeah set error %d\n", |
---|
923 | ((libtrace_packet_t *)t->format_data)->error); |
---|
924 | } |
---|
925 | break; |
---|
926 | } |
---|
927 | } |
---|
928 | |
---|
929 | return i; |
---|
930 | } |
---|
931 | |
---|
932 | /** |
---|
933 | * For the first packet of each queue we keep a copy and note the system |
---|
934 | * time it was received at. |
---|
935 | * |
---|
936 | * This is used for finding the first packet when playing back a trace |
---|
937 | * in trace time. And can be used by real time applications to print |
---|
938 | * results out every XXX seconds. |
---|
939 | */ |
---|
940 | void store_first_packet(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t) |
---|
941 | { |
---|
942 | if (!t->recorded_first) { |
---|
943 | struct timeval tv; |
---|
944 | libtrace_packet_t * dup; |
---|
945 | // For what it's worth we can call these outside of the lock |
---|
946 | gettimeofday(&tv, NULL); |
---|
947 | dup = trace_copy_packet(packet); |
---|
948 | ASSERT_RET(pthread_spin_lock(&libtrace->first_packets.lock), == 0); |
---|
949 | libtrace->first_packets.packets[t->perpkt_num].packet = dup; |
---|
950 | //printf("Stored first packet time=%f\n", trace_get_seconds(dup)); |
---|
951 | memcpy(&libtrace->first_packets.packets[t->perpkt_num].tv, &tv, sizeof(tv)); |
---|
952 | // Now update the first |
---|
953 | libtrace->first_packets.count++; |
---|
954 | if (libtrace->first_packets.count == 1) { |
---|
955 | // We the first entry hence also the first known packet |
---|
956 | libtrace->first_packets.first = t->perpkt_num; |
---|
957 | } else { |
---|
958 | // Check if we are newer than the previous 'first' packet |
---|
959 | size_t first = libtrace->first_packets.first; |
---|
960 | if (trace_get_seconds(dup) < |
---|
961 | trace_get_seconds(libtrace->first_packets.packets[first].packet)) |
---|
962 | libtrace->first_packets.first = t->perpkt_num; |
---|
963 | } |
---|
964 | ASSERT_RET(pthread_spin_unlock(&libtrace->first_packets.lock), == 0); |
---|
965 | libtrace_message_t mesg = {0}; |
---|
966 | mesg.code = MESSAGE_FIRST_PACKET; |
---|
967 | trace_send_message_to_reporter(libtrace, &mesg); |
---|
968 | t->recorded_first = true; |
---|
969 | } |
---|
970 | } |
---|
971 | |
---|
972 | /** |
---|
973 | * Returns 1 if it's certain that the first packet is truly the first packet |
---|
974 | * rather than a best guess based upon threads that have published so far. |
---|
975 | * Otherwise 0 is returned. |
---|
976 | * It's recommended that this result is stored rather than calling this |
---|
977 | * function again. |
---|
978 | */ |
---|
979 | DLLEXPORT int retrive_first_packet(libtrace_t *libtrace, libtrace_packet_t **packet, struct timeval **tv) |
---|
980 | { |
---|
981 | int ret = 0; |
---|
982 | ASSERT_RET(pthread_spin_lock(&libtrace->first_packets.lock), == 0); |
---|
983 | if (libtrace->first_packets.count) { |
---|
984 | *packet = libtrace->first_packets.packets[libtrace->first_packets.first].packet; |
---|
985 | *tv = &libtrace->first_packets.packets[libtrace->first_packets.first].tv; |
---|
986 | if (libtrace->first_packets.count == (size_t) libtrace->perpkt_thread_count) { |
---|
987 | ret = 1; |
---|
988 | } else { |
---|
989 | struct timeval curr_tv; |
---|
990 | // If a second has passed since the first entry we will assume this is the very first packet |
---|
991 | gettimeofday(&curr_tv, NULL); |
---|
992 | if (curr_tv.tv_sec > (*tv)->tv_sec) { |
---|
993 | if(curr_tv.tv_usec > (*tv)->tv_usec || curr_tv.tv_sec - (*tv)->tv_sec > 1) { |
---|
994 | ret = 1; |
---|
995 | } |
---|
996 | } |
---|
997 | } |
---|
998 | } else { |
---|
999 | *packet = NULL; |
---|
1000 | *tv = NULL; |
---|
1001 | } |
---|
1002 | ASSERT_RET(pthread_spin_unlock(&libtrace->first_packets.lock), == 0); |
---|
1003 | return ret; |
---|
1004 | } |
---|
1005 | |
---|
1006 | |
---|
1007 | DLLEXPORT uint64_t tv_to_usec(struct timeval *tv) |
---|
1008 | { |
---|
1009 | return (uint64_t) tv->tv_sec*1000000ull + (uint64_t) tv->tv_usec; |
---|
1010 | } |
---|
1011 | |
---|
1012 | inline static struct timeval usec_to_tv(uint64_t usec) |
---|
1013 | { |
---|
1014 | struct timeval tv; |
---|
1015 | tv.tv_sec = usec / 1000000; |
---|
1016 | tv.tv_usec = usec % 1000000; |
---|
1017 | return tv; |
---|
1018 | } |
---|
1019 | |
---|
1020 | /** Similar to delay_tracetime but send messages to all threads periodically */ |
---|
1021 | static void* reporter_entry(void *data) { |
---|
1022 | libtrace_message_t message = {0}; |
---|
1023 | libtrace_t *trace = (libtrace_t *)data; |
---|
1024 | libtrace_thread_t *t = &trace->reporter_thread; |
---|
1025 | |
---|
1026 | fprintf(stderr, "Reporter thread starting\n"); |
---|
1027 | |
---|
1028 | /* Wait until all threads are started */ |
---|
1029 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
1030 | if (trace->state == STATE_ERROR) { |
---|
1031 | thread_change_state(trace, t, THREAD_FINISHED, false); |
---|
1032 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
1033 | pthread_exit(NULL); |
---|
1034 | } |
---|
1035 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
1036 | |
---|
1037 | (*trace->reporter)(trace, MESSAGE_STARTING, (libtrace_generic_t) {0}, t); |
---|
1038 | (*trace->reporter)(trace, MESSAGE_RESUMING, (libtrace_generic_t) {0}, t); |
---|
1039 | |
---|
1040 | while (!trace_finished(trace)) { |
---|
1041 | if (trace->config.reporter_polling) { |
---|
1042 | if (libtrace_message_queue_try_get(&t->messages, &message) == LIBTRACE_MQ_FAILED) |
---|
1043 | message.code = MESSAGE_POST_REPORTER; |
---|
1044 | } else { |
---|
1045 | libtrace_message_queue_get(&t->messages, &message); |
---|
1046 | } |
---|
1047 | switch (message.code) { |
---|
1048 | // Check for results |
---|
1049 | case MESSAGE_POST_REPORTER: |
---|
1050 | trace->combiner.read(trace, &trace->combiner); |
---|
1051 | break; |
---|
1052 | case MESSAGE_DO_PAUSE: |
---|
1053 | assert(trace->combiner.pause); |
---|
1054 | trace->combiner.pause(trace, &trace->combiner); |
---|
1055 | (*trace->reporter)(trace, MESSAGE_PAUSING, (libtrace_generic_t) {0}, t); |
---|
1056 | trace_thread_pause(trace, t); |
---|
1057 | (*trace->reporter)(trace, MESSAGE_RESUMING, (libtrace_generic_t) {0}, t); |
---|
1058 | break; |
---|
1059 | default: |
---|
1060 | (*trace->reporter)(trace, message.code, message.additional, message.sender); |
---|
1061 | } |
---|
1062 | } |
---|
1063 | |
---|
1064 | // Flush out whats left now all our threads have finished |
---|
1065 | trace->combiner.read_final(trace, &trace->combiner); |
---|
1066 | |
---|
1067 | // GOODBYE |
---|
1068 | (*trace->reporter)(trace, MESSAGE_PAUSING, (libtrace_generic_t) {0}, t); |
---|
1069 | (*trace->reporter)(trace, MESSAGE_STOPPING, (libtrace_generic_t) {0}, t); |
---|
1070 | |
---|
1071 | thread_change_state(trace, &trace->reporter_thread, THREAD_FINISHED, true); |
---|
1072 | print_memory_stats(); |
---|
1073 | return NULL; |
---|
1074 | } |
---|
1075 | |
---|
1076 | /** Similar to delay_tracetime but send messages to all threads periodically */ |
---|
1077 | static void* keepalive_entry(void *data) { |
---|
1078 | struct timeval prev, next; |
---|
1079 | libtrace_message_t message = {0}; |
---|
1080 | libtrace_t *trace = (libtrace_t *)data; |
---|
1081 | uint64_t next_release; |
---|
1082 | fprintf(stderr, "keepalive thread is starting\n"); |
---|
1083 | |
---|
1084 | /* Wait until all threads are started */ |
---|
1085 | ASSERT_RET(pthread_mutex_lock(&trace->libtrace_lock), == 0); |
---|
1086 | if (trace->state == STATE_ERROR) { |
---|
1087 | thread_change_state(trace, &trace->keepalive_thread, THREAD_FINISHED, false); |
---|
1088 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
1089 | pthread_exit(NULL); |
---|
1090 | } |
---|
1091 | ASSERT_RET(pthread_mutex_unlock(&trace->libtrace_lock), == 0); |
---|
1092 | |
---|
1093 | gettimeofday(&prev, NULL); |
---|
1094 | message.code = MESSAGE_TICK; |
---|
1095 | while (trace->state != STATE_FINSHED) { |
---|
1096 | fd_set rfds; |
---|
1097 | next_release = tv_to_usec(&prev) + (trace->config.tick_interval * 1000); |
---|
1098 | gettimeofday(&next, NULL); |
---|
1099 | if (next_release > tv_to_usec(&next)) { |
---|
1100 | next = usec_to_tv(next_release - tv_to_usec(&next)); |
---|
1101 | // Wait for timeout or a message |
---|
1102 | FD_ZERO(&rfds); |
---|
1103 | FD_SET(libtrace_message_queue_get_fd(&trace->keepalive_thread.messages), &rfds); |
---|
1104 | if (select(libtrace_message_queue_get_fd(&trace->keepalive_thread.messages)+1, &rfds, NULL, NULL, &next) == 1) { |
---|
1105 | libtrace_message_t msg; |
---|
1106 | libtrace_message_queue_get(&trace->keepalive_thread.messages, &msg); |
---|
1107 | assert(msg.code == MESSAGE_DO_STOP); |
---|
1108 | goto done; |
---|
1109 | } |
---|
1110 | } |
---|
1111 | prev = usec_to_tv(next_release); |
---|
1112 | if (trace->state == STATE_RUNNING) { |
---|
1113 | message.additional.uint64 = tv_to_usec(&prev); |
---|
1114 | trace_send_message_to_perpkts(trace, &message); |
---|
1115 | } |
---|
1116 | } |
---|
1117 | done: |
---|
1118 | |
---|
1119 | thread_change_state(trace, &trace->keepalive_thread, THREAD_FINISHED, true); |
---|
1120 | return NULL; |
---|
1121 | } |
---|
1122 | |
---|
1123 | /** |
---|
1124 | * Delays a packets playback so the playback will be in trace time. |
---|
1125 | * This may break early if a message becomes available. |
---|
1126 | * |
---|
1127 | * Requires the first packet for this thread to be received. |
---|
1128 | * @param libtrace The trace |
---|
1129 | * @param packet The packet to delay |
---|
1130 | * @param t The current thread |
---|
1131 | * @return Either READ_MESSAGE(-2) or 0 is successful |
---|
1132 | */ |
---|
1133 | static inline int delay_tracetime(libtrace_t *libtrace, libtrace_packet_t *packet, libtrace_thread_t *t) { |
---|
1134 | struct timeval curr_tv, pkt_tv; |
---|
1135 | uint64_t next_release = t->tracetime_offset_usec; |
---|
1136 | uint64_t curr_usec; |
---|
1137 | |
---|
1138 | if (!t->tracetime_offset_usec) { |
---|
1139 | libtrace_packet_t *first_pkt; |
---|
1140 | struct timeval *sys_tv; |
---|
1141 | int64_t initial_offset; |
---|
1142 | int stable = retrive_first_packet(libtrace, &first_pkt, &sys_tv); |
---|
1143 | assert(first_pkt); |
---|
1144 | pkt_tv = trace_get_timeval(first_pkt); |
---|
1145 | initial_offset = (int64_t)tv_to_usec(sys_tv) - (int64_t)tv_to_usec(&pkt_tv); |
---|
1146 | /* In the unlikely case offset is 0, change it to 1 */ |
---|
1147 | if (stable) |
---|
1148 | t->tracetime_offset_usec = initial_offset ? initial_offset: 1; |
---|
1149 | next_release = initial_offset; |
---|
1150 | } |
---|
1151 | /* next_release == offset */ |
---|
1152 | pkt_tv = trace_get_timeval(packet); |
---|
1153 | next_release += tv_to_usec(&pkt_tv); |
---|
1154 | gettimeofday(&curr_tv, NULL); |
---|
1155 | curr_usec = tv_to_usec(&curr_tv); |
---|
1156 | if (next_release > curr_usec) { |
---|
1157 | int ret, mesg_fd = libtrace_message_queue_get_fd(&t->messages); |
---|
1158 | struct timeval delay_tv = usec_to_tv(next_release-curr_usec); |
---|
1159 | fd_set rfds; |
---|
1160 | FD_ZERO(&rfds); |
---|
1161 | FD_SET(mesg_fd, &rfds); |
---|
1162 | // We need to wait |
---|
1163 | |
---|
1164 | //printf("WAITING for %d.%d next=%"PRIu64" curr=%"PRIu64" seconds packettime %f\n", delay_tv.tv_sec, delay_tv.tv_usec, next_release, curr_usec, trace_get_seconds(packet)); |
---|
1165 | ret = select(mesg_fd+1, &rfds, NULL, NULL, &delay_tv); |
---|
1166 | if (ret == 0) { |
---|
1167 | return 0; |
---|
1168 | } else if (ret > 0) { |
---|
1169 | return READ_MESSAGE; |
---|
1170 | } else { |
---|
1171 | fprintf(stderr, "I thnik we broke select\n"); |
---|
1172 | } |
---|
1173 | } |
---|
1174 | return 0; |
---|
1175 | } |
---|
1176 | |
---|
1177 | /* Discards packets that don't match the filter. |
---|
1178 | * Discarded packets are emptied and then moved to the end of the packet list. |
---|
1179 | * |
---|
1180 | * @param trace The trace format, containing the filter |
---|
1181 | * @param packets An array of packets |
---|
1182 | * @param nb_packets The number of valid items in packets |
---|
1183 | * |
---|
1184 | * @return The number of packets that passed the filter, which are moved to |
---|
1185 | * the start of the packets array |
---|
1186 | */ |
---|
1187 | static inline size_t filter_packets(libtrace_t *trace, |
---|
1188 | libtrace_packet_t **packets, |
---|
1189 | size_t nb_packets) { |
---|
1190 | size_t offset = 0; |
---|
1191 | size_t i; |
---|
1192 | |
---|
1193 | for (i = 0; i < nb_packets; ++i) { |
---|
1194 | // The filter needs the trace attached to receive the link type |
---|
1195 | packets[i]->trace = trace; |
---|
1196 | if (trace_apply_filter(trace->filter, packets[i])) { |
---|
1197 | libtrace_packet_t *tmp; |
---|
1198 | tmp = packets[offset]; |
---|
1199 | packets[offset++] = packets[i]; |
---|
1200 | packets[i] = tmp; |
---|
1201 | } else { |
---|
1202 | trace_fin_packet(packets[i]); |
---|
1203 | } |
---|
1204 | } |
---|
1205 | |
---|
1206 | return offset; |
---|
1207 | } |
---|
1208 | |
---|
1209 | /* Read a batch of packets from the trace into a buffer. |
---|
1210 | * Note that this function will block until a packet is read (or EOF is reached) |
---|
1211 | * |
---|
1212 | * @param libtrace The trace |
---|
1213 | * @param t The thread |
---|
1214 | * @param packets An array of packets |
---|
1215 | * @param nb_packets The number of empty packets in packets |
---|
1216 | * @return The number of packets read, 0 on EOF (or an error/message -1,-2). |
---|
1217 | */ |
---|
1218 | static int trace_pread_packet_wrapper(libtrace_t *libtrace, |
---|
1219 | libtrace_thread_t *t, |
---|
1220 | libtrace_packet_t *packets[], |
---|
1221 | size_t nb_packets) { |
---|
1222 | int i; |
---|
1223 | assert(nb_packets); |
---|
1224 | assert(libtrace && "libtrace is NULL in trace_read_packet()"); |
---|
1225 | if (trace_is_err(libtrace)) |
---|
1226 | return -1; |
---|
1227 | if (!libtrace->started) { |
---|
1228 | trace_set_err(libtrace, TRACE_ERR_BAD_STATE, |
---|
1229 | "You must call libtrace_start() before trace_read_packet()\n"); |
---|
1230 | return -1; |
---|
1231 | } |
---|
1232 | |
---|
1233 | if (libtrace->format->pread_packets) { |
---|
1234 | int ret; |
---|
1235 | for (i = 0; i < (int) nb_packets; ++i) { |
---|
1236 | assert(i[packets]); |
---|
1237 | if (!(packets[i]->buf_control==TRACE_CTRL_PACKET || |
---|
1238 | packets[i]->buf_control==TRACE_CTRL_EXTERNAL)) { |
---|
1239 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, |
---|
1240 | "Packet passed to trace_read_packet() is invalid\n"); |
---|
1241 | return -1; |
---|
1242 | } |
---|
1243 | } |
---|
1244 | do { |
---|
1245 | ret=libtrace->format->pread_packets(libtrace, t, |
---|
1246 | packets, |
---|
1247 | nb_packets); |
---|
1248 | /* Error, EOF or message? */ |
---|
1249 | if (ret <= 0) { |
---|
1250 | return ret; |
---|
1251 | } |
---|
1252 | |
---|
1253 | if (libtrace->filter) { |
---|
1254 | int remaining; |
---|
1255 | remaining = filter_packets(libtrace, |
---|
1256 | packets, ret); |
---|
1257 | t->filtered_packets += ret - remaining; |
---|
1258 | ret = remaining; |
---|
1259 | } |
---|
1260 | for (i = 0; i < ret; ++i) { |
---|
1261 | /* We do not mark the packet against the trace, |
---|
1262 | * before hand or after. After breaks DAG meta |
---|
1263 | * packets and before is inefficient */ |
---|
1264 | //packets[i]->trace = libtrace; |
---|
1265 | /* TODO IN FORMAT?? Like traditional libtrace */ |
---|
1266 | if (libtrace->snaplen>0) |
---|
1267 | trace_set_capture_length(packets[i], |
---|
1268 | libtrace->snaplen); |
---|
1269 | trace_packet_set_order(packets[i], trace_get_erf_timestamp(packets[i])); |
---|
1270 | } |
---|
1271 | } while(ret == 0); |
---|
1272 | return ret; |
---|
1273 | } |
---|
1274 | trace_set_err(libtrace, TRACE_ERR_UNSUPPORTED, |
---|
1275 | "This format does not support reading packets\n"); |
---|
1276 | return ~0U; |
---|
1277 | } |
---|
1278 | |
---|
1279 | /* Restarts a parallel trace, this is called from trace_pstart. |
---|
1280 | * The libtrace lock is held upon calling this function. |
---|
1281 | * Typically with a parallel trace the threads are not |
---|
1282 | * killed rather. |
---|
1283 | */ |
---|
1284 | static int trace_prestart(libtrace_t * libtrace, void *global_blob, |
---|
1285 | fn_per_pkt per_pkt, fn_reporter reporter) { |
---|
1286 | int err = 0; |
---|
1287 | if (libtrace->state != STATE_PAUSED) { |
---|
1288 | trace_set_err(libtrace, TRACE_ERR_BAD_STATE, |
---|
1289 | "trace(%s) is not currently paused", |
---|
1290 | libtrace->uridata); |
---|
1291 | return -1; |
---|
1292 | } |
---|
1293 | |
---|
1294 | /* Update functions if requested */ |
---|
1295 | if (per_pkt) |
---|
1296 | libtrace->per_pkt = per_pkt; |
---|
1297 | if (reporter) |
---|
1298 | libtrace->reporter = reporter; |
---|
1299 | if(global_blob) |
---|
1300 | libtrace->global_blob = global_blob; |
---|
1301 | |
---|
1302 | assert(libtrace_parallel); |
---|
1303 | assert(!libtrace->perpkt_thread_states[THREAD_RUNNING]); |
---|
1304 | assert(libtrace->per_pkt); |
---|
1305 | |
---|
1306 | if (libtrace->perpkt_thread_count > 1 && |
---|
1307 | trace_supports_parallel(libtrace) && |
---|
1308 | !trace_has_dedicated_hasher(libtrace)) { |
---|
1309 | fprintf(stderr, "Restarting trace pstart_input()\n"); |
---|
1310 | err = libtrace->format->pstart_input(libtrace); |
---|
1311 | } else { |
---|
1312 | if (libtrace->format->start_input) { |
---|
1313 | fprintf(stderr, "Restarting trace start_input()\n"); |
---|
1314 | err = libtrace->format->start_input(libtrace); |
---|
1315 | } |
---|
1316 | } |
---|
1317 | |
---|
1318 | if (err == 0) { |
---|
1319 | libtrace->started = true; |
---|
1320 | libtrace_change_state(libtrace, STATE_RUNNING, false); |
---|
1321 | } |
---|
1322 | return err; |
---|
1323 | } |
---|
1324 | |
---|
1325 | /** |
---|
1326 | * Verifies the configuration and sets default values for any values not |
---|
1327 | * specified by the user. |
---|
1328 | * @return |
---|
1329 | */ |
---|
1330 | static void verify_configuration(libtrace_t *libtrace) { |
---|
1331 | bool require_hasher = false; |
---|
1332 | |
---|
1333 | /* Might we need a dedicated hasher thread? */ |
---|
1334 | if (libtrace->hasher && libtrace->hasher_type != HASHER_HARDWARE) { |
---|
1335 | require_hasher = true; |
---|
1336 | } |
---|
1337 | |
---|
1338 | if (libtrace->config.hasher_queue_size <= 0) |
---|
1339 | libtrace->config.hasher_queue_size = 1000; |
---|
1340 | |
---|
1341 | if (libtrace->config.perpkt_threads <= 0) { |
---|
1342 | // TODO add BSD support |
---|
1343 | libtrace->perpkt_thread_count = sysconf(_SC_NPROCESSORS_ONLN); |
---|
1344 | if (libtrace->perpkt_thread_count <= 0) |
---|
1345 | // Lets just use one |
---|
1346 | libtrace->perpkt_thread_count = 1; |
---|
1347 | } else { |
---|
1348 | libtrace->perpkt_thread_count = libtrace->config.perpkt_threads; |
---|
1349 | } |
---|
1350 | |
---|
1351 | if (libtrace->config.reporter_thold <= 0) |
---|
1352 | libtrace->config.reporter_thold = 100; |
---|
1353 | if (libtrace->config.burst_size <= 0) |
---|
1354 | libtrace->config.burst_size = 10; |
---|
1355 | if (libtrace->config.packet_thread_cache_size <= 0) |
---|
1356 | libtrace->config.packet_thread_cache_size = 20; |
---|
1357 | if (libtrace->config.packet_cache_size <= 0) |
---|
1358 | libtrace->config.packet_cache_size = (libtrace->config.hasher_queue_size + 1) * libtrace->perpkt_thread_count; |
---|
1359 | |
---|
1360 | if (libtrace->config.packet_cache_size < |
---|
1361 | (libtrace->config.hasher_queue_size + 1) * libtrace->perpkt_thread_count) |
---|
1362 | fprintf(stderr, "WARNING deadlocks may occur and extra memory allocating buffer sizes (packet_freelist_size) mismatched\n"); |
---|
1363 | |
---|
1364 | if (libtrace->combiner.initialise == NULL && libtrace->combiner.publish == NULL) |
---|
1365 | libtrace->combiner = combiner_unordered; |
---|
1366 | |
---|
1367 | |
---|
1368 | /* Figure out if we are using a dedicated hasher thread? */ |
---|
1369 | if (require_hasher && libtrace->perpkt_thread_count > 1) { |
---|
1370 | libtrace->hasher_thread.type = THREAD_HASHER; |
---|
1371 | } |
---|
1372 | } |
---|
1373 | |
---|
1374 | /** |
---|
1375 | * Starts a libtrace_thread, including allocating memory for messaging. |
---|
1376 | * Threads are expected to wait until the libtrace look is released. |
---|
1377 | * Hence why we don't init structures until later. |
---|
1378 | * |
---|
1379 | * @param trace The trace the thread is associated with |
---|
1380 | * @param t The thread that is filled when the thread is started |
---|
1381 | * @param type The type of thread |
---|
1382 | * @param start_routine The entry location of the thread |
---|
1383 | * @param perpkt_num The perpkt thread number (should be set -1 if not perpkt) |
---|
1384 | * @param name For debugging purposes set the threads name (Optional) |
---|
1385 | * |
---|
1386 | * @return 0 on success or -1 upon error in which case the libtrace error is set. |
---|
1387 | * In this situation the thread structure is zeroed. |
---|
1388 | */ |
---|
1389 | static int trace_start_thread(libtrace_t *trace, |
---|
1390 | libtrace_thread_t *t, |
---|
1391 | enum thread_types type, |
---|
1392 | void *(*start_routine) (void *), |
---|
1393 | int perpkt_num, |
---|
1394 | const char *name) { |
---|
1395 | int ret; |
---|
1396 | assert(t->type == THREAD_EMPTY); |
---|
1397 | t->trace = trace; |
---|
1398 | t->ret = NULL; |
---|
1399 | t->user_data = NULL; |
---|
1400 | t->type = type; |
---|
1401 | t->state = THREAD_RUNNING; |
---|
1402 | ret = pthread_create(&t->tid, NULL, start_routine, (void *) trace); |
---|
1403 | if (ret != 0) { |
---|
1404 | libtrace_zero_thread(t); |
---|
1405 | trace_set_err(trace, ret, "Failed to create a thread"); |
---|
1406 | return -1; |
---|
1407 | } |
---|
1408 | libtrace_message_queue_init(&t->messages, sizeof(libtrace_message_t)); |
---|
1409 | if (trace_has_dedicated_hasher(trace) && type == THREAD_PERPKT) { |
---|
1410 | libtrace_ringbuffer_init(&t->rbuffer, |
---|
1411 | trace->config.hasher_queue_size, |
---|
1412 | trace->config.hasher_polling? |
---|
1413 | LIBTRACE_RINGBUFFER_POLLING: |
---|
1414 | LIBTRACE_RINGBUFFER_BLOCKING); |
---|
1415 | } |
---|
1416 | if(name) |
---|
1417 | pthread_setname_np(t->tid, name); |
---|
1418 | t->perpkt_num = perpkt_num; |
---|
1419 | return 0; |
---|
1420 | } |
---|
1421 | |
---|
1422 | /** Parses the environment variable LIBTRACE_CONF into the supplied |
---|
1423 | * configuration structure. |
---|
1424 | * |
---|
1425 | * @param libtrace The trace from which we determine the URI |
---|
1426 | * @param uc A configuration structure to be configured. |
---|
1427 | * |
---|
1428 | * We search for 3 environment variables and apply them to the config in the |
---|
1429 | * following order. Such that the first has the lowest priority. |
---|
1430 | * |
---|
1431 | * 1. LIBTRACE_CONF, The global environment configuration |
---|
1432 | * 2. LIBTRACE_CONF_<FORMAT>, Applied to a given format |
---|
1433 | * 3. LIBTRACE_CONF_<FORMAT_URI>, Applied the specified trace |
---|
1434 | * |
---|
1435 | * E.g. |
---|
1436 | * - int:eth0 would match LIBTRACE_CONF, LIBTRACE_CONF_INT, LIBTRACE_CONF_INT_ETH0 |
---|
1437 | * - dag:/dev/dag0,0 would match LIBTRACE_CONF, LIBTRACE_CONF_DAG, LIBTRACE_CONF_DAG__DEV_DAG0_0 |
---|
1438 | * - test.erf would match LIBTRACE_CONF, LIBTRACE_CONF_ERF, LIBTRACE_CONF_ERF_TEST_ERF |
---|
1439 | * |
---|
1440 | * @note All enironment variables names MUST only contian |
---|
1441 | * [A-Z], [0-9] and [_] (underscore) and not start with a number. Any characters |
---|
1442 | * outside of this range should be captilised if possible or replaced with an |
---|
1443 | * underscore. |
---|
1444 | */ |
---|
1445 | static void parse_env_config (libtrace_t *libtrace, struct user_configuration* uc) { |
---|
1446 | char env_name[1024] = "LIBTRACE_CONF_"; |
---|
1447 | size_t len = strlen(env_name); |
---|
1448 | size_t mark = 0; |
---|
1449 | size_t i; |
---|
1450 | char * env; |
---|
1451 | |
---|
1452 | /* Make our compound string */ |
---|
1453 | strncpy(&env_name[len], libtrace->format->name, sizeof(env_name) - len); |
---|
1454 | len += strlen(libtrace->format->name); |
---|
1455 | strncpy(&env_name[len], ":", sizeof(env_name) - len); |
---|
1456 | len += 1; |
---|
1457 | strncpy(&env_name[len], libtrace->uridata, sizeof(env_name) - len); |
---|
1458 | |
---|
1459 | /* env names are allowed to be A-Z (CAPS) 0-9 and _ */ |
---|
1460 | for (i = 0; env_name[i] != 0; ++i) { |
---|
1461 | env_name[i] = toupper(env_name[i]); |
---|
1462 | if(env_name[i] == ':') { |
---|
1463 | mark = i; |
---|
1464 | } |
---|
1465 | if (!( (env_name[i] >= 'A' && env_name[i] <= 'Z') || |
---|
1466 | (env_name[i] >= '0' && env_name[i] <= '9') )) { |
---|
1467 | env_name[i] = '_'; |
---|
1468 | } |
---|
1469 | } |
---|
1470 | |
---|
1471 | /* First apply global env settings LIBTRACE_CONF */ |
---|
1472 | env = getenv("LIBTRACE_CONF"); |
---|
1473 | if (env) |
---|
1474 | { |
---|
1475 | printf("Got env %s", env); |
---|
1476 | parse_user_config(uc, env); |
---|
1477 | } |
---|
1478 | |
---|
1479 | /* Then format settings LIBTRACE_CONF_<FORMAT> */ |
---|
1480 | if (mark != 0) { |
---|
1481 | env_name[mark] = 0; |
---|
1482 | env = getenv(env_name); |
---|
1483 | if (env) { |
---|
1484 | printf("Got %s=%s", env_name, env); |
---|
1485 | parse_user_config(uc, env); |
---|
1486 | } |
---|
1487 | env_name[mark] = '_'; |
---|
1488 | } |
---|
1489 | |
---|
1490 | /* Finally this specific trace LIBTRACE_CONF_<FORMAT_URI> */ |
---|
1491 | env = getenv(env_name); |
---|
1492 | if (env) { |
---|
1493 | printf("Got %s=%s", env_name, env); |
---|
1494 | parse_user_config(uc, env); |
---|
1495 | } |
---|
1496 | } |
---|
1497 | |
---|
1498 | /* Start an input trace in the parallel libtrace framework. |
---|
1499 | * This can also be used to restart an existing parallel. |
---|
1500 | * |
---|
1501 | * NOTE: libtrace lock is held for the majority of this function |
---|
1502 | * |
---|
1503 | * @param libtrace the input trace to start |
---|
1504 | * @param global_blob some global data you can share with the new perpkt threads |
---|
1505 | * @returns 0 on success, otherwise -1 to indicate an error has occured |
---|
1506 | */ |
---|
1507 | DLLEXPORT int trace_pstart(libtrace_t *libtrace, void* global_blob, |
---|
1508 | fn_per_pkt per_pkt, fn_reporter reporter) { |
---|
1509 | int i; |
---|
1510 | int ret = -1; |
---|
1511 | char name[16]; |
---|
1512 | sigset_t sig_before, sig_block_all; |
---|
1513 | assert(libtrace); |
---|
1514 | |
---|
1515 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
1516 | if (trace_is_err(libtrace)) { |
---|
1517 | goto cleanup_none; |
---|
1518 | } |
---|
1519 | |
---|
1520 | if (libtrace->state == STATE_PAUSED) { |
---|
1521 | ret = trace_prestart(libtrace, global_blob, per_pkt, reporter); |
---|
1522 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1523 | return ret; |
---|
1524 | } |
---|
1525 | |
---|
1526 | if (libtrace->state != STATE_NEW) { |
---|
1527 | trace_set_err(libtrace, TRACE_ERR_BAD_STATE, "trace_pstart " |
---|
1528 | "should be called on a NEW or PAUSED trace but " |
---|
1529 | "instead was called from %s", |
---|
1530 | get_trace_state_name(libtrace->state)); |
---|
1531 | goto cleanup_none; |
---|
1532 | } |
---|
1533 | |
---|
1534 | /* Store the user defined things against the trace */ |
---|
1535 | libtrace->global_blob = global_blob; |
---|
1536 | libtrace->per_pkt = per_pkt; |
---|
1537 | libtrace->reporter = reporter; |
---|
1538 | /* And zero other fields */ |
---|
1539 | for (i = 0; i < THREAD_STATE_MAX; ++i) { |
---|
1540 | libtrace->perpkt_thread_states[i] = 0; |
---|
1541 | } |
---|
1542 | libtrace->first_packets.first = 0; |
---|
1543 | libtrace->first_packets.count = 0; |
---|
1544 | libtrace->first_packets.packets = NULL; |
---|
1545 | libtrace->perpkt_threads = NULL; |
---|
1546 | /* Set a global which says we are using a parallel trace. This is |
---|
1547 | * for backwards compatability due to changes when destroying packets */ |
---|
1548 | libtrace_parallel = 1; |
---|
1549 | |
---|
1550 | /* Parses configuration passed through environment variables */ |
---|
1551 | parse_env_config(libtrace, &libtrace->config); |
---|
1552 | verify_configuration(libtrace); |
---|
1553 | |
---|
1554 | /* Try start the format - we prefer parallel over single threaded, as |
---|
1555 | * these formats should support messages better */ |
---|
1556 | if (trace_supports_parallel(libtrace) && |
---|
1557 | !trace_has_dedicated_hasher(libtrace)) { |
---|
1558 | printf("Using the parallel trace format\n"); |
---|
1559 | ret = libtrace->format->pstart_input(libtrace); |
---|
1560 | libtrace->pread = trace_pread_packet_wrapper; |
---|
1561 | } else { |
---|
1562 | printf("Using single threaded interface\n"); |
---|
1563 | if (libtrace->format->start_input) { |
---|
1564 | ret = libtrace->format->start_input(libtrace); |
---|
1565 | } |
---|
1566 | if (libtrace->perpkt_thread_count > 1) |
---|
1567 | libtrace->pread = trace_pread_packet_first_in_first_served; |
---|
1568 | else |
---|
1569 | /* Use standard read_packet */ |
---|
1570 | libtrace->pread = NULL; |
---|
1571 | } |
---|
1572 | |
---|
1573 | if (ret != 0) { |
---|
1574 | goto cleanup_none; |
---|
1575 | } |
---|
1576 | |
---|
1577 | /* --- Start all the threads we need --- */ |
---|
1578 | /* Disable signals because it is inherited by the threads we start */ |
---|
1579 | sigemptyset(&sig_block_all); |
---|
1580 | ASSERT_RET(pthread_sigmask(SIG_SETMASK, &sig_block_all, &sig_before), == 0); |
---|
1581 | |
---|
1582 | /* If we need a hasher thread start it |
---|
1583 | * Special Case: If single threaded we don't need a hasher |
---|
1584 | */ |
---|
1585 | if (trace_has_dedicated_hasher(libtrace)) { |
---|
1586 | libtrace->hasher_thread.type = THREAD_EMPTY; |
---|
1587 | ret = trace_start_thread(libtrace, &libtrace->hasher_thread, |
---|
1588 | THREAD_HASHER, hasher_entry, -1, |
---|
1589 | "hasher-thread"); |
---|
1590 | if (ret != 0) { |
---|
1591 | trace_set_err(libtrace, errno, "trace_pstart " |
---|
1592 | "failed to start a hasher thread."); |
---|
1593 | goto cleanup_started; |
---|
1594 | } |
---|
1595 | libtrace->pread = trace_pread_packet_hasher_thread; |
---|
1596 | } else { |
---|
1597 | libtrace->hasher_thread.type = THREAD_EMPTY; |
---|
1598 | } |
---|
1599 | |
---|
1600 | /* Start up our perpkt threads */ |
---|
1601 | libtrace->perpkt_threads = calloc(sizeof(libtrace_thread_t), |
---|
1602 | libtrace->perpkt_thread_count); |
---|
1603 | if (!libtrace->perpkt_threads) { |
---|
1604 | trace_set_err(libtrace, errno, "trace_pstart " |
---|
1605 | "failed to allocate memory."); |
---|
1606 | goto cleanup_threads; |
---|
1607 | } |
---|
1608 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
1609 | snprintf(name, sizeof(name), "perpkt-%d", i); |
---|
1610 | libtrace_zero_thread(&libtrace->perpkt_threads[i]); |
---|
1611 | ret = trace_start_thread(libtrace, &libtrace->perpkt_threads[i], |
---|
1612 | THREAD_PERPKT, perpkt_threads_entry, i, |
---|
1613 | name); |
---|
1614 | if (ret != 0) { |
---|
1615 | trace_set_err(libtrace, errno, "trace_pstart " |
---|
1616 | "failed to start a perpkt thread."); |
---|
1617 | goto cleanup_threads; |
---|
1618 | } |
---|
1619 | } |
---|
1620 | |
---|
1621 | /* Start the reporter thread */ |
---|
1622 | if (reporter) { |
---|
1623 | if (libtrace->combiner.initialise) |
---|
1624 | libtrace->combiner.initialise(libtrace, &libtrace->combiner); |
---|
1625 | ret = trace_start_thread(libtrace, &libtrace->reporter_thread, |
---|
1626 | THREAD_REPORTER, reporter_entry, -1, |
---|
1627 | "reporter_thread"); |
---|
1628 | if (ret != 0) { |
---|
1629 | trace_set_err(libtrace, errno, "trace_pstart " |
---|
1630 | "failed to start reporter thread."); |
---|
1631 | goto cleanup_threads; |
---|
1632 | } |
---|
1633 | } |
---|
1634 | |
---|
1635 | /* Start the keepalive thread */ |
---|
1636 | if (libtrace->config.tick_interval > 0) { |
---|
1637 | ret = trace_start_thread(libtrace, &libtrace->keepalive_thread, |
---|
1638 | THREAD_KEEPALIVE, keepalive_entry, -1, |
---|
1639 | "keepalive_thread"); |
---|
1640 | if (ret != 0) { |
---|
1641 | trace_set_err(libtrace, errno, "trace_pstart " |
---|
1642 | "failed to start keepalive thread."); |
---|
1643 | goto cleanup_threads; |
---|
1644 | } |
---|
1645 | } |
---|
1646 | |
---|
1647 | /* Init other data structures */ |
---|
1648 | libtrace->perpkt_thread_states[THREAD_RUNNING] = libtrace->perpkt_thread_count; |
---|
1649 | ASSERT_RET(pthread_spin_init(&libtrace->first_packets.lock, 0), == 0); |
---|
1650 | libtrace->first_packets.packets = calloc(libtrace->perpkt_thread_count, |
---|
1651 | sizeof(struct __packet_storage_magic_type)); |
---|
1652 | if (libtrace->first_packets.packets == NULL) { |
---|
1653 | trace_set_err(libtrace, errno, "trace_pstart " |
---|
1654 | "failed to allocate memory."); |
---|
1655 | goto cleanup_threads; |
---|
1656 | } |
---|
1657 | |
---|
1658 | if (libtrace_ocache_init(&libtrace->packet_freelist, |
---|
1659 | (void* (*)()) trace_create_packet, |
---|
1660 | (void (*)(void *))trace_destroy_packet, |
---|
1661 | libtrace->config.packet_thread_cache_size, |
---|
1662 | libtrace->config.packet_cache_size * 4, |
---|
1663 | libtrace->config.fixed_packet_count) != 0) { |
---|
1664 | trace_set_err(libtrace, TRACE_ERR_INIT_FAILED, "trace_pstart " |
---|
1665 | "failed to allocate ocache."); |
---|
1666 | goto cleanup_threads; |
---|
1667 | } |
---|
1668 | |
---|
1669 | /* Threads don't start */ |
---|
1670 | libtrace->started = true; |
---|
1671 | libtrace_change_state(libtrace, STATE_RUNNING, false); |
---|
1672 | |
---|
1673 | ret = 0; |
---|
1674 | goto success; |
---|
1675 | cleanup_threads: |
---|
1676 | if (libtrace->first_packets.packets) { |
---|
1677 | free(libtrace->first_packets.packets); |
---|
1678 | libtrace->first_packets.packets = NULL; |
---|
1679 | } |
---|
1680 | libtrace_change_state(libtrace, STATE_ERROR, false); |
---|
1681 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1682 | if (libtrace->hasher_thread.type == THREAD_HASHER) { |
---|
1683 | pthread_join(libtrace->hasher_thread.tid, NULL); |
---|
1684 | libtrace_zero_thread(&libtrace->hasher_thread); |
---|
1685 | } |
---|
1686 | |
---|
1687 | if (libtrace->perpkt_threads) { |
---|
1688 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
1689 | if (libtrace->perpkt_threads[i].type == THREAD_PERPKT) { |
---|
1690 | pthread_join(libtrace->perpkt_threads[i].tid, NULL); |
---|
1691 | libtrace_zero_thread(&libtrace->perpkt_threads[i]); |
---|
1692 | } else break; |
---|
1693 | } |
---|
1694 | free(libtrace->perpkt_threads); |
---|
1695 | libtrace->perpkt_threads = NULL; |
---|
1696 | } |
---|
1697 | |
---|
1698 | if (libtrace->reporter_thread.type == THREAD_REPORTER) { |
---|
1699 | pthread_join(libtrace->reporter_thread.tid, NULL); |
---|
1700 | libtrace_zero_thread(&libtrace->reporter_thread); |
---|
1701 | } |
---|
1702 | |
---|
1703 | if (libtrace->keepalive_thread.type == THREAD_KEEPALIVE) { |
---|
1704 | pthread_join(libtrace->keepalive_thread.tid, NULL); |
---|
1705 | libtrace_zero_thread(&libtrace->keepalive_thread); |
---|
1706 | } |
---|
1707 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
1708 | libtrace_change_state(libtrace, STATE_NEW, false); |
---|
1709 | assert(libtrace->perpkt_thread_states[THREAD_RUNNING] == 0); |
---|
1710 | libtrace->perpkt_thread_states[THREAD_FINISHED] = 0; |
---|
1711 | cleanup_started: |
---|
1712 | if (trace_supports_parallel(libtrace) && |
---|
1713 | !trace_has_dedicated_hasher(libtrace) |
---|
1714 | && libtrace->perpkt_thread_count > 1) { |
---|
1715 | if (libtrace->format->ppause_input) |
---|
1716 | libtrace->format->ppause_input(libtrace); |
---|
1717 | } else { |
---|
1718 | if (libtrace->format->pause_input) |
---|
1719 | libtrace->format->pause_input(libtrace); |
---|
1720 | } |
---|
1721 | ret = -1; |
---|
1722 | success: |
---|
1723 | ASSERT_RET(pthread_sigmask(SIG_SETMASK, &sig_before, NULL), == 0); |
---|
1724 | cleanup_none: |
---|
1725 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1726 | return ret; |
---|
1727 | } |
---|
1728 | |
---|
1729 | /** |
---|
1730 | * Pauses a trace, this should only be called by the main thread |
---|
1731 | * 1. Set started = false |
---|
1732 | * 2. All perpkt threads are paused waiting on a condition var |
---|
1733 | * 3. Then call ppause on the underlying format if found |
---|
1734 | * 4. The traces state is paused |
---|
1735 | * |
---|
1736 | * Once done you should be able to modify the trace setup and call pstart again |
---|
1737 | * TODO handle changing thread numbers |
---|
1738 | */ |
---|
1739 | DLLEXPORT int trace_ppause(libtrace_t *libtrace) |
---|
1740 | { |
---|
1741 | libtrace_thread_t *t; |
---|
1742 | int i; |
---|
1743 | assert(libtrace); |
---|
1744 | |
---|
1745 | t = get_thread_table(libtrace); |
---|
1746 | // Check state from within the lock if we are going to change it |
---|
1747 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
1748 | if (!libtrace->started || libtrace->state != STATE_RUNNING) { |
---|
1749 | fprintf(stderr, "pause failed started=%d state=%s (%d)\n", libtrace->started, get_trace_state_name(libtrace->state), libtrace->state); |
---|
1750 | trace_set_err(libtrace,TRACE_ERR_BAD_STATE, "You must call trace_start() before calling trace_ppause()"); |
---|
1751 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1752 | return -1; |
---|
1753 | } |
---|
1754 | |
---|
1755 | libtrace_change_state(libtrace, STATE_PAUSING, false); |
---|
1756 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1757 | |
---|
1758 | // Special case handle the hasher thread case |
---|
1759 | if (trace_has_dedicated_hasher(libtrace)) { |
---|
1760 | if (libtrace->config.debug_state) |
---|
1761 | fprintf(stderr, "Hasher thread is running, asking it to pause ..."); |
---|
1762 | libtrace_message_t message = {0}; |
---|
1763 | message.code = MESSAGE_DO_PAUSE; |
---|
1764 | trace_send_message_to_thread(libtrace, &libtrace->hasher_thread, &message); |
---|
1765 | // Wait for it to pause |
---|
1766 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
1767 | while (libtrace->hasher_thread.state == THREAD_RUNNING) { |
---|
1768 | ASSERT_RET(pthread_cond_wait(&libtrace->perpkt_cond, &libtrace->libtrace_lock), == 0); |
---|
1769 | } |
---|
1770 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1771 | if (libtrace->config.debug_state) |
---|
1772 | fprintf(stderr, " DONE\n"); |
---|
1773 | } |
---|
1774 | |
---|
1775 | if (libtrace->config.debug_state) |
---|
1776 | fprintf(stderr, "Asking perpkt threads to pause ..."); |
---|
1777 | // Stop threads, skip this one if it's a perpkt |
---|
1778 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
1779 | if (&libtrace->perpkt_threads[i] != t) { |
---|
1780 | libtrace_message_t message = {0}; |
---|
1781 | message.code = MESSAGE_DO_PAUSE; |
---|
1782 | trace_send_message_to_thread(libtrace, &libtrace->perpkt_threads[i], &message); |
---|
1783 | if(trace_has_dedicated_hasher(libtrace)) { |
---|
1784 | // The hasher has stopped and other threads have messages waiting therefore |
---|
1785 | // If the queues are empty the other threads would have no data |
---|
1786 | // So send some message packets to simply ask the threads to check |
---|
1787 | // We are the only writer since hasher has paused |
---|
1788 | libtrace_packet_t *pkt; |
---|
1789 | libtrace_ocache_alloc(&libtrace->packet_freelist, (void **) &pkt, 1, 1); |
---|
1790 | pkt->error = READ_MESSAGE; |
---|
1791 | libtrace_ringbuffer_write(&libtrace->perpkt_threads[i].rbuffer, pkt); |
---|
1792 | } |
---|
1793 | } else { |
---|
1794 | fprintf(stderr, "Mapper threads should not be used to pause a trace this could cause any number of problems!!\n"); |
---|
1795 | } |
---|
1796 | } |
---|
1797 | |
---|
1798 | if (t) { |
---|
1799 | // A perpkt is doing the pausing, interesting, fake an extra thread paused |
---|
1800 | // We rely on the user to *not* return before starting the trace again |
---|
1801 | thread_change_state(libtrace, t, THREAD_PAUSED, true); |
---|
1802 | } |
---|
1803 | |
---|
1804 | // Wait for all threads to pause |
---|
1805 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
1806 | while(libtrace->perpkt_thread_states[THREAD_RUNNING]) { |
---|
1807 | ASSERT_RET(pthread_cond_wait(&libtrace->perpkt_cond, &libtrace->libtrace_lock), == 0); |
---|
1808 | } |
---|
1809 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1810 | |
---|
1811 | if (libtrace->config.debug_state) |
---|
1812 | fprintf(stderr, " DONE\n"); |
---|
1813 | |
---|
1814 | // Deal with the reporter |
---|
1815 | if (trace_has_dedicated_reporter(libtrace)) { |
---|
1816 | if (libtrace->config.debug_state) |
---|
1817 | fprintf(stderr, "Reporter thread is running, asking it to pause ..."); |
---|
1818 | libtrace_message_t message = {0}; |
---|
1819 | message.code = MESSAGE_DO_PAUSE; |
---|
1820 | trace_send_message_to_thread(libtrace, &libtrace->reporter_thread, &message); |
---|
1821 | // Wait for it to pause |
---|
1822 | ASSERT_RET(pthread_mutex_lock(&libtrace->libtrace_lock), == 0); |
---|
1823 | while (libtrace->reporter_thread.state == THREAD_RUNNING) { |
---|
1824 | ASSERT_RET(pthread_cond_wait(&libtrace->perpkt_cond, &libtrace->libtrace_lock), == 0); |
---|
1825 | } |
---|
1826 | ASSERT_RET(pthread_mutex_unlock(&libtrace->libtrace_lock), == 0); |
---|
1827 | if (libtrace->config.debug_state) |
---|
1828 | fprintf(stderr, " DONE\n"); |
---|
1829 | } |
---|
1830 | |
---|
1831 | /* Cache values before we pause */ |
---|
1832 | if (libtrace->stats == NULL) |
---|
1833 | libtrace->stats = trace_create_statistics(); |
---|
1834 | // Save the statistics against the trace |
---|
1835 | trace_get_statistics(libtrace, NULL); |
---|
1836 | if (trace_supports_parallel(libtrace) && !trace_has_dedicated_hasher(libtrace) && libtrace->perpkt_thread_count > 1) { |
---|
1837 | libtrace->started = false; |
---|
1838 | if (libtrace->format->ppause_input) |
---|
1839 | libtrace->format->ppause_input(libtrace); |
---|
1840 | // TODO What happens if we don't have pause input?? |
---|
1841 | } else { |
---|
1842 | int err; |
---|
1843 | fprintf(stderr, "Trace is not parallel so we are doing a normal pause %s\n", libtrace->uridata); |
---|
1844 | err = trace_pause(libtrace); |
---|
1845 | // We should handle this a bit better |
---|
1846 | if (err) |
---|
1847 | return err; |
---|
1848 | } |
---|
1849 | |
---|
1850 | // Only set as paused after the pause has been called on the trace |
---|
1851 | libtrace_change_state(libtrace, STATE_PAUSED, true); |
---|
1852 | return 0; |
---|
1853 | } |
---|
1854 | |
---|
1855 | /** |
---|
1856 | * Stop trace finish prematurely as though it meet an EOF |
---|
1857 | * This should only be called by the main thread |
---|
1858 | * 1. Calls ppause |
---|
1859 | * 2. Sends a message asking for threads to finish |
---|
1860 | * 3. Releases threads which will pause |
---|
1861 | */ |
---|
1862 | DLLEXPORT int trace_pstop(libtrace_t *libtrace) |
---|
1863 | { |
---|
1864 | int i, err; |
---|
1865 | libtrace_message_t message = {0}; |
---|
1866 | assert(libtrace); |
---|
1867 | |
---|
1868 | // Ensure all threads have paused and the underlying trace format has |
---|
1869 | // been closed and all packets associated are cleaned up |
---|
1870 | // Pause will do any state checks for us |
---|
1871 | err = trace_ppause(libtrace); |
---|
1872 | if (err) |
---|
1873 | return err; |
---|
1874 | |
---|
1875 | // Now send a message asking the threads to stop |
---|
1876 | // This will be retrieved before trying to read another packet |
---|
1877 | |
---|
1878 | message.code = MESSAGE_DO_STOP; |
---|
1879 | trace_send_message_to_perpkts(libtrace, &message); |
---|
1880 | if (trace_has_dedicated_hasher(libtrace)) |
---|
1881 | trace_send_message_to_thread(libtrace, &libtrace->hasher_thread, &message); |
---|
1882 | |
---|
1883 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
1884 | trace_send_message_to_thread(libtrace, &libtrace->perpkt_threads[i], &message); |
---|
1885 | } |
---|
1886 | |
---|
1887 | // Now release the threads and let them stop |
---|
1888 | libtrace_change_state(libtrace, STATE_FINSHED, true); |
---|
1889 | return 0; |
---|
1890 | } |
---|
1891 | |
---|
1892 | /** |
---|
1893 | * Set the hasher type along with a selected function, if hardware supports |
---|
1894 | * that generic type of hashing it will be used otherwise the supplied |
---|
1895 | * hasher function will be used and passed data when called. |
---|
1896 | * |
---|
1897 | * @return 0 if successful otherwise -1 on error |
---|
1898 | */ |
---|
1899 | DLLEXPORT int trace_set_hasher(libtrace_t *trace, enum hasher_types type, fn_hasher hasher, void *data) { |
---|
1900 | int ret = -1; |
---|
1901 | if (type == HASHER_HARDWARE || (type == HASHER_CUSTOM && !hasher) || (type == HASHER_BALANCE && hasher)) { |
---|
1902 | return -1; |
---|
1903 | } |
---|
1904 | |
---|
1905 | // Save the requirements |
---|
1906 | trace->hasher_type = type; |
---|
1907 | if (hasher) { |
---|
1908 | trace->hasher = hasher; |
---|
1909 | trace->hasher_data = data; |
---|
1910 | } else { |
---|
1911 | trace->hasher = NULL; |
---|
1912 | trace->hasher_data = NULL; |
---|
1913 | } |
---|
1914 | |
---|
1915 | // Try push this to hardware - NOTE hardware could do custom if |
---|
1916 | // there is a more efficient way to apply it, in this case |
---|
1917 | // it will simply grab the function out of libtrace_t |
---|
1918 | if (trace->format->pconfig_input) |
---|
1919 | ret = trace->format->pconfig_input(trace, TRACE_OPTION_SET_HASHER, &type); |
---|
1920 | |
---|
1921 | if (ret == -1) { |
---|
1922 | // We have to deal with this ourself |
---|
1923 | // This most likely means single threaded reading of the trace |
---|
1924 | if (!hasher) { |
---|
1925 | switch (type) |
---|
1926 | { |
---|
1927 | case HASHER_CUSTOM: |
---|
1928 | case HASHER_BALANCE: |
---|
1929 | return 0; |
---|
1930 | case HASHER_BIDIRECTIONAL: |
---|
1931 | trace->hasher = (fn_hasher) toeplitz_hash_packet; |
---|
1932 | trace->hasher_data = calloc(1, sizeof(toeplitz_conf_t)); |
---|
1933 | toeplitz_init_config(trace->hasher_data, 1); |
---|
1934 | return 0; |
---|
1935 | case HASHER_UNIDIRECTIONAL: |
---|
1936 | trace->hasher = (fn_hasher) toeplitz_hash_packet; |
---|
1937 | trace->hasher_data = calloc(1, sizeof(toeplitz_conf_t)); |
---|
1938 | toeplitz_init_config(trace->hasher_data, 0); |
---|
1939 | return 0; |
---|
1940 | case HASHER_HARDWARE: |
---|
1941 | return -1; |
---|
1942 | } |
---|
1943 | return -1; |
---|
1944 | } |
---|
1945 | } else { |
---|
1946 | // The hardware is dealing with this yay |
---|
1947 | trace->hasher_type = HASHER_HARDWARE; |
---|
1948 | } |
---|
1949 | |
---|
1950 | return 0; |
---|
1951 | } |
---|
1952 | |
---|
1953 | // Waits for all threads to finish |
---|
1954 | DLLEXPORT void trace_join(libtrace_t *libtrace) { |
---|
1955 | int i; |
---|
1956 | |
---|
1957 | /* Firstly wait for the perpkt threads to finish, since these are |
---|
1958 | * user controlled */ |
---|
1959 | for (i=0; i< libtrace->perpkt_thread_count; i++) { |
---|
1960 | //printf("Waiting to join with perpkt #%d\n", i); |
---|
1961 | ASSERT_RET(pthread_join(libtrace->perpkt_threads[i].tid, NULL), == 0); |
---|
1962 | //printf("Joined with perpkt #%d\n", i); |
---|
1963 | // So we must do our best effort to empty the queue - so |
---|
1964 | // the producer (or any other threads) don't block. |
---|
1965 | libtrace_packet_t * packet; |
---|
1966 | assert(libtrace->perpkt_threads[i].state == THREAD_FINISHED); |
---|
1967 | while(libtrace_ringbuffer_try_read(&libtrace->perpkt_threads[i].rbuffer, (void **) &packet)) |
---|
1968 | if (packet) // This could be NULL iff the perpkt finishes early |
---|
1969 | trace_destroy_packet(packet); |
---|
1970 | } |
---|
1971 | |
---|
1972 | /* Now the hasher */ |
---|
1973 | if (trace_has_dedicated_hasher(libtrace)) { |
---|
1974 | pthread_join(libtrace->hasher_thread.tid, NULL); |
---|
1975 | assert(libtrace->hasher_thread.state == THREAD_FINISHED); |
---|
1976 | } |
---|
1977 | |
---|
1978 | // Now that everything is finished nothing can be touching our |
---|
1979 | // buffers so clean them up |
---|
1980 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
1981 | // Its possible 1 packet got added by the reporter (or 1 per any other thread) since we cleaned up |
---|
1982 | // if they lost timeslice before-during a write |
---|
1983 | libtrace_packet_t * packet; |
---|
1984 | while(libtrace_ringbuffer_try_read(&libtrace->perpkt_threads[i].rbuffer, (void **) &packet)) |
---|
1985 | trace_destroy_packet(packet); |
---|
1986 | if (libtrace->hasher) { |
---|
1987 | assert(libtrace_ringbuffer_is_empty(&libtrace->perpkt_threads[i].rbuffer)); |
---|
1988 | libtrace_ringbuffer_destroy(&libtrace->perpkt_threads[i].rbuffer); |
---|
1989 | } |
---|
1990 | // Cannot destroy vector yet, this happens with trace_destroy |
---|
1991 | } |
---|
1992 | // TODO consider perpkt threads marking trace as finished before join is called |
---|
1993 | libtrace_change_state(libtrace, STATE_FINSHED, true); |
---|
1994 | |
---|
1995 | if (trace_has_dedicated_reporter(libtrace)) { |
---|
1996 | pthread_join(libtrace->reporter_thread.tid, NULL); |
---|
1997 | assert(libtrace->reporter_thread.state == THREAD_FINISHED); |
---|
1998 | } |
---|
1999 | |
---|
2000 | // Wait for the tick (keepalive) thread if it has been started |
---|
2001 | if (libtrace->keepalive_thread.type == THREAD_KEEPALIVE) { |
---|
2002 | libtrace_message_t msg = {0}; |
---|
2003 | msg.code = MESSAGE_DO_STOP; |
---|
2004 | trace_send_message_to_thread(libtrace, &libtrace->keepalive_thread, &msg); |
---|
2005 | pthread_join(libtrace->keepalive_thread.tid, NULL); |
---|
2006 | } |
---|
2007 | |
---|
2008 | libtrace_change_state(libtrace, STATE_JOINED, true); |
---|
2009 | print_memory_stats(); |
---|
2010 | } |
---|
2011 | |
---|
2012 | DLLEXPORT int libtrace_thread_get_message_count(libtrace_t * libtrace) |
---|
2013 | { |
---|
2014 | libtrace_thread_t * t = get_thread_descriptor(libtrace); |
---|
2015 | assert(t); |
---|
2016 | return libtrace_message_queue_count(&t->messages); |
---|
2017 | } |
---|
2018 | |
---|
2019 | DLLEXPORT int libtrace_thread_get_message(libtrace_t * libtrace, libtrace_message_t * message) |
---|
2020 | { |
---|
2021 | libtrace_thread_t * t = get_thread_descriptor(libtrace); |
---|
2022 | assert(t); |
---|
2023 | return libtrace_message_queue_get(&t->messages, message); |
---|
2024 | } |
---|
2025 | |
---|
2026 | DLLEXPORT int libtrace_thread_try_get_message(libtrace_t * libtrace, libtrace_message_t * message) |
---|
2027 | { |
---|
2028 | libtrace_thread_t * t = get_thread_descriptor(libtrace); |
---|
2029 | assert(t); |
---|
2030 | return libtrace_message_queue_try_get(&t->messages, message); |
---|
2031 | } |
---|
2032 | |
---|
2033 | /** |
---|
2034 | * Return backlog indicator |
---|
2035 | */ |
---|
2036 | DLLEXPORT int trace_post_reporter(libtrace_t *libtrace) |
---|
2037 | { |
---|
2038 | libtrace_message_t message = {0}; |
---|
2039 | message.code = MESSAGE_POST_REPORTER; |
---|
2040 | message.sender = get_thread_descriptor(libtrace); |
---|
2041 | return libtrace_message_queue_put(&libtrace->reporter_thread.messages, (void *) &message); |
---|
2042 | } |
---|
2043 | |
---|
2044 | /** |
---|
2045 | * Return backlog indicator |
---|
2046 | */ |
---|
2047 | DLLEXPORT int trace_send_message_to_reporter(libtrace_t * libtrace, libtrace_message_t * message) |
---|
2048 | { |
---|
2049 | //printf("Sending message code=%d to reporter\n", message->code); |
---|
2050 | message->sender = get_thread_descriptor(libtrace); |
---|
2051 | return libtrace_message_queue_put(&libtrace->reporter_thread.messages, message); |
---|
2052 | } |
---|
2053 | |
---|
2054 | /** |
---|
2055 | * |
---|
2056 | */ |
---|
2057 | DLLEXPORT int trace_send_message_to_thread(libtrace_t * libtrace, libtrace_thread_t *t, libtrace_message_t * message) |
---|
2058 | { |
---|
2059 | //printf("Sending message code=%d to reporter\n", message->code); |
---|
2060 | message->sender = get_thread_descriptor(libtrace); |
---|
2061 | return libtrace_message_queue_put(&t->messages, message); |
---|
2062 | } |
---|
2063 | |
---|
2064 | DLLEXPORT int trace_send_message_to_perpkts(libtrace_t * libtrace, libtrace_message_t * message) |
---|
2065 | { |
---|
2066 | int i; |
---|
2067 | message->sender = get_thread_descriptor(libtrace); |
---|
2068 | for (i = 0; i < libtrace->perpkt_thread_count; i++) { |
---|
2069 | libtrace_message_queue_put(&libtrace->perpkt_threads[i].messages, message); |
---|
2070 | } |
---|
2071 | //printf("Sending message code=%d to reporter\n", message->code); |
---|
2072 | return 0; |
---|
2073 | } |
---|
2074 | |
---|
2075 | DLLEXPORT void libtrace_result_set_key(libtrace_result_t * result, uint64_t key) { |
---|
2076 | result->key = key; |
---|
2077 | } |
---|
2078 | DLLEXPORT uint64_t libtrace_result_get_key(libtrace_result_t * result) { |
---|
2079 | return result->key; |
---|
2080 | } |
---|
2081 | DLLEXPORT void libtrace_result_set_value(libtrace_result_t * result, libtrace_generic_t value) { |
---|
2082 | result->value = value; |
---|
2083 | } |
---|
2084 | DLLEXPORT libtrace_generic_t libtrace_result_get_value(libtrace_result_t * result) { |
---|
2085 | return result->value; |
---|
2086 | } |
---|
2087 | DLLEXPORT void libtrace_result_set_key_value(libtrace_result_t * result, uint64_t key, libtrace_generic_t value) { |
---|
2088 | result->key = key; |
---|
2089 | result->value = value; |
---|
2090 | } |
---|
2091 | DLLEXPORT void trace_destroy_result(libtrace_result_t ** result) { |
---|
2092 | free(*result); |
---|
2093 | result = NULL; |
---|
2094 | // TODO automatically back with a free list!! |
---|
2095 | } |
---|
2096 | |
---|
2097 | DLLEXPORT void * trace_get_global(libtrace_t *trace) |
---|
2098 | { |
---|
2099 | return trace->global_blob; |
---|
2100 | } |
---|
2101 | |
---|
2102 | DLLEXPORT void * trace_set_global(libtrace_t *trace, void * data) |
---|
2103 | { |
---|
2104 | if (trace->global_blob && trace->global_blob != data) { |
---|
2105 | void * ret = trace->global_blob; |
---|
2106 | trace->global_blob = data; |
---|
2107 | return ret; |
---|
2108 | } else { |
---|
2109 | trace->global_blob = data; |
---|
2110 | return NULL; |
---|
2111 | } |
---|
2112 | } |
---|
2113 | |
---|
2114 | DLLEXPORT void * trace_get_tls(libtrace_thread_t *t) |
---|
2115 | { |
---|
2116 | return t->user_data; |
---|
2117 | } |
---|
2118 | |
---|
2119 | DLLEXPORT void * trace_set_tls(libtrace_thread_t *t, void * data) |
---|
2120 | { |
---|
2121 | if(t->user_data && t->user_data != data) { |
---|
2122 | void *ret = t->user_data; |
---|
2123 | t->user_data = data; |
---|
2124 | return ret; |
---|
2125 | } else { |
---|
2126 | t->user_data = data; |
---|
2127 | return NULL; |
---|
2128 | } |
---|
2129 | } |
---|
2130 | |
---|
2131 | /** |
---|
2132 | * Publishes a result to the reduce queue |
---|
2133 | * Should only be called by a perpkt thread, i.e. from a perpkt handler |
---|
2134 | */ |
---|
2135 | DLLEXPORT void trace_publish_result(libtrace_t *libtrace, libtrace_thread_t *t, uint64_t key, libtrace_generic_t value, int type) { |
---|
2136 | libtrace_result_t res; |
---|
2137 | res.type = type; |
---|
2138 | res.key = key; |
---|
2139 | res.value = value; |
---|
2140 | assert(libtrace->combiner.publish); |
---|
2141 | libtrace->combiner.publish(libtrace, t->perpkt_num, &libtrace->combiner, &res); |
---|
2142 | return; |
---|
2143 | } |
---|
2144 | |
---|
2145 | /** |
---|
2146 | * Sets a combiner function against the trace. |
---|
2147 | */ |
---|
2148 | DLLEXPORT void trace_set_combiner(libtrace_t *trace, const libtrace_combine_t *combiner, libtrace_generic_t config){ |
---|
2149 | if (combiner) { |
---|
2150 | trace->combiner = *combiner; |
---|
2151 | trace->combiner.configuration = config; |
---|
2152 | } else { |
---|
2153 | // No combiner, so don't try use it |
---|
2154 | memset(&trace->combiner, 0, sizeof(trace->combiner)); |
---|
2155 | } |
---|
2156 | } |
---|
2157 | |
---|
2158 | DLLEXPORT uint64_t trace_packet_get_order(libtrace_packet_t * packet) { |
---|
2159 | return packet->order; |
---|
2160 | } |
---|
2161 | |
---|
2162 | DLLEXPORT uint64_t trace_packet_get_hash(libtrace_packet_t * packet) { |
---|
2163 | return packet->hash; |
---|
2164 | } |
---|
2165 | |
---|
2166 | DLLEXPORT void trace_packet_set_order(libtrace_packet_t * packet, uint64_t order) { |
---|
2167 | packet->order = order; |
---|
2168 | } |
---|
2169 | |
---|
2170 | DLLEXPORT void trace_packet_set_hash(libtrace_packet_t * packet, uint64_t hash) { |
---|
2171 | packet->hash = hash; |
---|
2172 | } |
---|
2173 | |
---|
2174 | DLLEXPORT int trace_finished(libtrace_t * libtrace) { |
---|
2175 | // TODO I don't like using this so much, we could use state!!! |
---|
2176 | return libtrace->perpkt_thread_states[THREAD_FINISHED] == libtrace->perpkt_thread_count; |
---|
2177 | } |
---|
2178 | |
---|
2179 | DLLEXPORT int trace_parallel_config(libtrace_t *libtrace, trace_parallel_option_t option, void *value) |
---|
2180 | { |
---|
2181 | UNUSED int ret = -1; |
---|
2182 | switch (option) { |
---|
2183 | case TRACE_OPTION_TICK_INTERVAL: |
---|
2184 | libtrace->config.tick_interval = *((int *) value); |
---|
2185 | return 1; |
---|
2186 | case TRACE_OPTION_SET_HASHER: |
---|
2187 | return trace_set_hasher(libtrace, (enum hasher_types) *((int *) value), NULL, NULL); |
---|
2188 | case TRACE_OPTION_SET_PERPKT_THREAD_COUNT: |
---|
2189 | libtrace->config.perpkt_threads = *((int *) value); |
---|
2190 | return 1; |
---|
2191 | case TRACE_OPTION_TRACETIME: |
---|
2192 | if(*((int *) value)) |
---|
2193 | libtrace->tracetime = 1; |
---|
2194 | else |
---|
2195 | libtrace->tracetime = 0; |
---|
2196 | return 0; |
---|
2197 | case TRACE_OPTION_SET_CONFIG: |
---|
2198 | libtrace->config = *((struct user_configuration *) value); |
---|
2199 | case TRACE_OPTION_GET_CONFIG: |
---|
2200 | *((struct user_configuration *) value) = libtrace->config; |
---|
2201 | } |
---|
2202 | return 0; |
---|
2203 | } |
---|
2204 | |
---|
2205 | static bool config_bool_parse(char *value, size_t nvalue) { |
---|
2206 | if (strncmp(value, "true", nvalue) == 0) |
---|
2207 | return true; |
---|
2208 | else if (strncmp(value, "false", nvalue) == 0) |
---|
2209 | return false; |
---|
2210 | else |
---|
2211 | return strtoll(value, NULL, 10) != 0; |
---|
2212 | } |
---|
2213 | |
---|
2214 | static void config_string(struct user_configuration *uc, char *key, size_t nkey, char *value, size_t nvalue) { |
---|
2215 | assert(key); |
---|
2216 | assert(value); |
---|
2217 | assert(uc); |
---|
2218 | if (strncmp(key, "packet_cache_size", nkey) == 0 |
---|
2219 | || strncmp(key, "pcs", nkey) == 0) { |
---|
2220 | uc->packet_cache_size = strtoll(value, NULL, 10); |
---|
2221 | } else if (strncmp(key, "packet_thread_cache_size", nkey) == 0 |
---|
2222 | || strncmp(key, "ptcs", nkey) == 0) { |
---|
2223 | uc->packet_thread_cache_size = strtoll(value, NULL, 10); |
---|
2224 | } else if (strncmp(key, "fixed_packet_count", nkey) == 0 |
---|
2225 | || strncmp(key, "fpc", nkey) == 0) { |
---|
2226 | uc->fixed_packet_count = config_bool_parse(value, nvalue); |
---|
2227 | } else if (strncmp(key, "burst_size", nkey) == 0 |
---|
2228 | || strncmp(key, "bs", nkey) == 0) { |
---|
2229 | uc->burst_size = strtoll(value, NULL, 10); |
---|
2230 | } else if (strncmp(key, "tick_interval", nkey) == 0 |
---|
2231 | || strncmp(key, "ti", nkey) == 0) { |
---|
2232 | uc->tick_interval = strtoll(value, NULL, 10); |
---|
2233 | } else if (strncmp(key, "tick_count", nkey) == 0 |
---|
2234 | || strncmp(key, "tc", nkey) == 0) { |
---|
2235 | uc->tick_count = strtoll(value, NULL, 10); |
---|
2236 | } else if (strncmp(key, "perpkt_threads", nkey) == 0 |
---|
2237 | || strncmp(key, "pt", nkey) == 0) { |
---|
2238 | uc->perpkt_threads = strtoll(value, NULL, 10); |
---|
2239 | } else if (strncmp(key, "hasher_queue_size", nkey) == 0 |
---|
2240 | || strncmp(key, "hqs", nkey) == 0) { |
---|
2241 | uc->hasher_queue_size = strtoll(value, NULL, 10); |
---|
2242 | } else if (strncmp(key, "hasher_polling", nkey) == 0 |
---|
2243 | || strncmp(key, "hp", nkey) == 0) { |
---|
2244 | uc->hasher_polling = config_bool_parse(value, nvalue); |
---|
2245 | } else if (strncmp(key, "reporter_polling", nkey) == 0 |
---|
2246 | || strncmp(key, "rp", nkey) == 0) { |
---|
2247 | uc->reporter_polling = config_bool_parse(value, nvalue); |
---|
2248 | } else if (strncmp(key, "reporter_thold", nkey) == 0 |
---|
2249 | || strncmp(key, "rt", nkey) == 0) { |
---|
2250 | uc->reporter_thold = strtoll(value, NULL, 10); |
---|
2251 | } else if (strncmp(key, "debug_state", nkey) == 0 |
---|
2252 | || strncmp(key, "ds", nkey) == 0) { |
---|
2253 | uc->debug_state = config_bool_parse(value, nvalue); |
---|
2254 | } else { |
---|
2255 | fprintf(stderr, "No matching value %s(=%s)\n", key, value); |
---|
2256 | } |
---|
2257 | } |
---|
2258 | |
---|
2259 | DLLEXPORT void parse_user_config(struct user_configuration* uc, char * str) { |
---|
2260 | char *pch; |
---|
2261 | char key[100]; |
---|
2262 | char value[100]; |
---|
2263 | assert(str); |
---|
2264 | assert(uc); |
---|
2265 | pch = strtok (str," ,.-"); |
---|
2266 | while (pch != NULL) |
---|
2267 | { |
---|
2268 | if (sscanf(pch, "%99[^=]=%99s", key, value) == 2) { |
---|
2269 | config_string(uc, key, sizeof(key), value, sizeof(value)); |
---|
2270 | } else { |
---|
2271 | fprintf(stderr, "Error parsing %s\n", pch); |
---|
2272 | } |
---|
2273 | pch = strtok (NULL," ,.-"); |
---|
2274 | } |
---|
2275 | } |
---|
2276 | |
---|
2277 | DLLEXPORT void parse_user_config_file(struct user_configuration* uc, FILE *file) { |
---|
2278 | char line[1024]; |
---|
2279 | while (fgets(line, sizeof(line), file) != NULL) |
---|
2280 | { |
---|
2281 | parse_user_config(uc, line); |
---|
2282 | } |
---|
2283 | } |
---|
2284 | |
---|
2285 | DLLEXPORT libtrace_packet_t* trace_result_packet(libtrace_t * libtrace, libtrace_packet_t * packet) { |
---|
2286 | libtrace_packet_t* result; |
---|
2287 | libtrace_ocache_alloc(&libtrace->packet_freelist, (void **) &result, 1, 1); |
---|
2288 | assert(result); |
---|
2289 | swap_packets(result, packet); // Move the current packet into our copy |
---|
2290 | return result; |
---|
2291 | } |
---|
2292 | |
---|
2293 | DLLEXPORT void trace_free_result_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
2294 | // Try write back the packet |
---|
2295 | assert(packet); |
---|
2296 | // Always release any resources this might be holding such as a slot in a ringbuffer |
---|
2297 | trace_fin_packet(packet); |
---|
2298 | libtrace_ocache_free(&libtrace->packet_freelist, (void **) &packet, 1, 1); |
---|
2299 | } |
---|
2300 | |
---|
2301 | DLLEXPORT libtrace_info_t *trace_get_information(libtrace_t * libtrace) { |
---|
2302 | if (libtrace->format) |
---|
2303 | return &libtrace->format->info; |
---|
2304 | else |
---|
2305 | return NULL; |
---|
2306 | } |
---|