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