[2498008] | 1 | #include "libtrace.h" |
---|
| 2 | #include "libtrace_int.h" |
---|
| 3 | #include "data-struct/vector.h" |
---|
| 4 | #include <assert.h> |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | |
---|
| 7 | static int init_combiner(libtrace_t *t, libtrace_combine_t *c) { |
---|
| 8 | int i = 0; |
---|
| 9 | assert(libtrace_get_perpkt_count(t) > 0); |
---|
| 10 | libtrace_vector_t *queues; |
---|
| 11 | c->queues = calloc(sizeof(libtrace_vector_t), libtrace_get_perpkt_count(t)); |
---|
| 12 | queues = c->queues; |
---|
| 13 | for (i = 0; i < libtrace_get_perpkt_count(t); ++i) { |
---|
| 14 | libtrace_vector_init(&queues[i], sizeof(libtrace_result_t)); |
---|
| 15 | } |
---|
| 16 | return 0; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | static void publish(libtrace_t *trace UNUSED, int t_id, libtrace_combine_t *c, libtrace_result_t *res) { |
---|
| 20 | libtrace_vector_t *vec = &((libtrace_vector_t*)c->queues)[t_id]; |
---|
| 21 | libtrace_vector_push_back(vec, res); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | static void read(libtrace_t *trace UNUSED, libtrace_combine_t *c UNUSED){ |
---|
| 25 | return; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | static int compare_result(const void* p1, const void* p2) |
---|
| 29 | { |
---|
| 30 | if (libtrace_result_get_key((libtrace_result_t *) p1) < libtrace_result_get_key((libtrace_result_t *) p2)) |
---|
| 31 | return -1; |
---|
| 32 | if (libtrace_result_get_key((libtrace_result_t *) p1) == libtrace_result_get_key((libtrace_result_t *) p2)) |
---|
| 33 | return 0; |
---|
| 34 | else |
---|
| 35 | return 1; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | static void pause(libtrace_t *trace, libtrace_combine_t *c) { |
---|
| 39 | libtrace_vector_t *queues = c->queues; |
---|
| 40 | int i; |
---|
| 41 | for (i = 0; i < libtrace_get_perpkt_count(trace); ++i) { |
---|
[d994324] | 42 | libtrace_vector_apply_function(&queues[i], (vector_data_fn) libtrace_make_result_safe); |
---|
[2498008] | 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | static void read_final(libtrace_t *trace, libtrace_combine_t *c) { |
---|
| 47 | libtrace_vector_t *queues = c->queues; |
---|
| 48 | int i; |
---|
| 49 | size_t a; |
---|
| 50 | // Combine all results into queue 1 |
---|
| 51 | for (i = 1; i < libtrace_get_perpkt_count(trace); ++i) |
---|
| 52 | { |
---|
| 53 | libtrace_vector_append(&queues[0],&queues[i]); |
---|
| 54 | } |
---|
| 55 | // Sort them |
---|
| 56 | libtrace_vector_qsort(&queues[0], compare_result); |
---|
| 57 | |
---|
| 58 | for (a = 0; a < libtrace_vector_get_size(&queues[0]); ++a) { |
---|
| 59 | libtrace_result_t r; |
---|
| 60 | ASSERT_RET (libtrace_vector_get(&queues[0], a, (void *) &r), == 1); |
---|
| 61 | trace->reporter(trace, &r, NULL); |
---|
| 62 | } |
---|
| 63 | libtrace_vector_empty(&queues[0]); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | static void destroy(libtrace_t *trace, libtrace_combine_t *c) { |
---|
| 67 | int i; |
---|
| 68 | libtrace_vector_t *queues = c->queues; |
---|
| 69 | |
---|
| 70 | for (i = 0; i < libtrace_get_perpkt_count(trace); i++) { |
---|
| 71 | assert(libtrace_vector_get_size(&queues[i]) == 0); |
---|
| 72 | libtrace_vector_destroy(&queues[i]); |
---|
| 73 | } |
---|
| 74 | free(queues); |
---|
| 75 | queues = NULL; |
---|
| 76 | } |
---|
| 77 | |
---|
[d994324] | 78 | DLLEXPORT const libtrace_combine_t combiner_sorted = { |
---|
[2498008] | 79 | init_combiner, /* initialise */ |
---|
| 80 | destroy, /* destroy */ |
---|
| 81 | publish, /* publish */ |
---|
| 82 | read, /* read */ |
---|
| 83 | read_final, /* read_final */ |
---|
| 84 | pause, /* pause */ |
---|
| 85 | NULL, /* queues */ |
---|
[d994324] | 86 | {0} /* opts */ |
---|
[2498008] | 87 | }; |
---|