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