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