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