source: test/test-format-parallel.c @ 43b773a

cachetimestampsdevelopetsiliverc-4.0.3rc-4.0.4ringdecrementfixringperformance
Last change on this file since 43b773a was 43b773a, checked in by Shane Alcock <salcock@…>, 3 years ago

Add basic tests for reading ERF provenance records

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id: test-rtclient.c,v 1.2 2006/02/27 03:41:12 perry Exp $
28 *
29 */
30#ifndef WIN32
31#  include <sys/time.h>
32#  include <netinet/in.h>
33#  include <netinet/in_systm.h>
34#  include <netinet/tcp.h>
35#  include <netinet/ip.h>
36#  include <netinet/ip_icmp.h>
37#  include <arpa/inet.h>
38#  include <sys/socket.h>
39#endif
40#include <stdio.h>
41#include <stdlib.h>
42#include <assert.h>
43#include <string.h>
44#include <sys/types.h>
45#include <time.h>
46#include <string.h>
47#include <signal.h>
48#include <unistd.h>
49
50#include "dagformat.h"
51#include "libtrace_parallel.h"
52#include "data-struct/vector.h"
53
54void iferr(libtrace_t *trace,const char *msg)
55{
56        libtrace_err_t err = trace_get_err(trace);
57        if (err.err_num==0)
58                return;
59        printf("Error: %s: %s\n", msg, err.problem);
60        exit(1);
61}
62
63const char *lookup_uri(const char *type) {
64        if (strchr(type,':'))
65                return type;
66        if (!strcmp(type,"erf"))
67                return "erf:traces/100_packets.erf";
68        if (!strcmp(type,"erfprov"))
69                return "erf:traces/provenance.erf";
70        if (!strcmp(type,"rawerf"))
71                return "rawerf:traces/100_packets.erf";
72        if (!strcmp(type,"pcap"))
73                return "pcap:traces/100_packets.pcap";
74        if (!strcmp(type,"pcapng"))
75                return "pcap:traces/100_packets.pcapng";
76        if (!strcmp(type,"wtf"))
77                return "wtf:traces/wed.wtf";
78        if (!strcmp(type,"rtclient"))
79                return "rtclient:chasm";
80        if (!strcmp(type,"pcapfile"))
81                return "pcapfile:traces/100_packets.pcap";
82        if (!strcmp(type,"pcapfilens"))
83                return "pcapfile:traces/100_packetsns.pcap";
84        if (!strcmp(type, "duck"))
85                return "duck:traces/100_packets.duck";
86        if (!strcmp(type, "legacyatm"))
87                return "legacyatm:traces/legacyatm.gz";
88        if (!strcmp(type, "legacypos"))
89                return "legacypos:traces/legacypos.gz";
90        if (!strcmp(type, "legacyeth"))
91                return "legacyeth:traces/legacyeth.gz";
92        if (!strcmp(type, "tsh"))
93                return "tsh:traces/10_packets.tsh.gz";
94        return type;
95}
96
97struct TLS {
98        bool seen_start_message;
99        bool seen_stop_message;
100        bool seen_resuming_message;
101        bool seen_pausing_message;
102        int count;
103};
104
105struct final {
106        int threads;
107        int packets;
108};
109
110static void *report_start(libtrace_t *trace UNUSED,
111                libtrace_thread_t *t UNUSED,
112                void *global) {
113        uint32_t *magic = (uint32_t *)global;
114        struct final *threadcounter =
115                        (struct final *)malloc(sizeof(struct final));
116
117        assert(*magic == 0xabcdef);
118
119        threadcounter->threads = 0;
120        threadcounter->packets = 0;
121        return threadcounter;
122}
123
124static void report_cb(libtrace_t *trace UNUSED,
125                libtrace_thread_t *sender UNUSED,
126                void *global, void *tls, libtrace_result_t *res) {
127
128        uint32_t *magic = (uint32_t *)global;
129        struct final *threadcounter = (struct final *)tls;
130
131        assert(*magic == 0xabcdef);
132        assert(res->key == 0);
133
134        threadcounter->threads ++;
135        threadcounter->packets += res->value.sint;
136        printf("%d\n", res->value.sint);
137}
138
139static void report_end(libtrace_t *trace, libtrace_thread_t *t UNUSED,
140                void *global, void *tls) {
141
142        uint32_t *magic = (uint32_t *)global;
143        struct final *threadcounter = (struct final *)tls;
144
145        assert(*magic == 0xabcdef);
146        assert(threadcounter->threads == trace_get_perpkt_threads(trace));
147        assert(threadcounter->packets == 100);
148
149        free(threadcounter);
150}
151
152static libtrace_packet_t *per_packet(libtrace_t *trace UNUSED,
153                libtrace_thread_t *t UNUSED,
154                void *global, void *tls, libtrace_packet_t *packet) {
155        struct TLS *storage = (struct TLS *)tls;
156        uint32_t *magic = (uint32_t *)global;
157        static __thread int count = 0;
158        int a,*b,c=0;
159
160        assert(storage != NULL);
161        assert(!storage->seen_stop_message);
162
163        if (storage->seen_pausing_message)
164                assert(storage->seen_resuming_message);
165
166        assert(*magic == 0xabcdef);
167
168        if (storage->count == 0)
169                usleep(100000);
170        storage->count ++;
171        count ++;
172
173        assert(count == storage->count);
174
175        if (count > 100) {
176                fprintf(stderr, "Too many packets -- someone should stop me!\n");
177                kill(getpid(), SIGTERM);
178        }
179
180        // Do some work to even out the load on cores
181        b = &c;
182        for (a = 0; a < 10000000; a++) {
183                c += a**b;
184        }
185
186        return packet;
187}
188
189static void *start_processing(libtrace_t *trace, libtrace_thread_t *t UNUSED,
190                void *global) {
191
192        static __thread bool seen_start_message = false;
193        uint32_t *magic = (uint32_t *)global;
194        struct TLS *storage = NULL;
195        assert(*magic == 0xabcdef);
196
197        assert(!seen_start_message);
198        assert(trace);
199
200        storage = (struct TLS *)malloc(sizeof(struct TLS));
201        storage->seen_start_message = true;
202        storage->seen_stop_message = false;
203        storage->seen_resuming_message = false;
204        storage->seen_pausing_message = false;
205        storage->count = 0;
206
207        seen_start_message = true;
208
209        return storage;
210}
211
212static void stop_processing(libtrace_t *trace, libtrace_thread_t *t,
213                void *global, void *tls) {
214
215        static __thread bool seen_stop_message = false;
216        struct TLS *storage = (struct TLS *)tls;
217        uint32_t *magic = (uint32_t *)global;
218
219        assert(storage != NULL);
220        assert(!storage->seen_stop_message);
221        assert(!seen_stop_message);
222        assert(storage->seen_start_message);
223        assert(*magic == 0xabcdef);
224
225        seen_stop_message = true;
226        storage->seen_stop_message = true;
227
228        trace_publish_result(trace, t, (uint64_t) 0, (libtrace_generic_t){.sint = storage->count}, RESULT_USER);
229        trace_post_reporter(trace);
230        free(storage);
231}
232
233static void process_tick(libtrace_t *trace UNUSED, libtrace_thread_t *t UNUSED,
234                void *global UNUSED, void *tls UNUSED, uint64_t tick UNUSED) {
235
236        fprintf(stderr, "Not expecting a tick packet\n");
237        kill(getpid(), SIGTERM);
238}
239
240static void pause_processing(libtrace_t *trace UNUSED,
241                libtrace_thread_t *t UNUSED,
242                void *global, void *tls) {
243
244        static __thread bool seen_pause_message = false;
245        struct TLS *storage = (struct TLS *)tls;
246        uint32_t *magic = (uint32_t *)global;
247
248        assert(storage != NULL);
249        assert(!storage->seen_stop_message);
250        assert(storage->seen_start_message);
251        assert(*magic == 0xabcdef);
252
253        assert(seen_pause_message == storage->seen_pausing_message);
254
255        seen_pause_message = true;
256        storage->seen_pausing_message = true;
257}
258
259static void resume_processing(libtrace_t *trace UNUSED,
260                libtrace_thread_t *t UNUSED,
261                void *global, void *tls) {
262
263        static __thread bool seen_resume_message = false;
264        struct TLS *storage = (struct TLS *)tls;
265        uint32_t *magic = (uint32_t *)global;
266
267        assert(storage != NULL);
268        assert(!storage->seen_stop_message);
269        assert(storage->seen_start_message);
270        assert(*magic == 0xabcdef);
271
272        assert(seen_resume_message == storage->seen_resuming_message);
273
274        seen_resume_message = true;
275        storage->seen_resuming_message = true;
276}
277
278int main(int argc, char *argv[]) {
279        int error = 0;
280        const char *tracename;
281        libtrace_t *trace;
282        libtrace_callback_set_t *processing = NULL;
283        libtrace_callback_set_t *reporter = NULL;
284        uint32_t global = 0xabcdef;
285
286        if (argc<2) {
287                fprintf(stderr,"usage: %s type\n",argv[0]);
288                return 1;
289        }
290
291        tracename = lookup_uri(argv[1]);
292
293        trace = trace_create(tracename);
294        iferr(trace,tracename);
295
296        processing = trace_create_callback_set();
297        trace_set_starting_cb(processing, start_processing);
298        trace_set_stopping_cb(processing, stop_processing);
299        trace_set_packet_cb(processing, per_packet);
300        trace_set_pausing_cb(processing, pause_processing);
301        trace_set_resuming_cb(processing, resume_processing);
302        trace_set_tick_count_cb(processing, process_tick);
303        trace_set_tick_interval_cb(processing, process_tick);
304
305        reporter = trace_create_callback_set();
306        trace_set_starting_cb(reporter, report_start);
307        trace_set_stopping_cb(reporter, report_end);
308        trace_set_result_cb(reporter, report_cb);
309
310
311        trace_set_perpkt_threads(trace, 4);
312
313        trace_pstart(trace, &global, processing, reporter);
314        iferr(trace,tracename);
315
316        /* Make sure traces survive a pause */
317        trace_ppause(trace);
318        iferr(trace,tracename);
319        trace_pstart(trace, NULL, NULL, NULL);
320        iferr(trace,tracename);
321
322        /* Wait for all threads to stop */
323        trace_join(trace);
324
325        global = 0xffffffff;
326
327        /* Now check we have all received all the packets */
328        if (error != 0) {
329                iferr(trace,tracename);
330        }
331
332        trace_destroy(trace);
333        trace_destroy_callback_set(processing);
334        trace_destroy_callback_set(reporter);
335        return error;
336}
Note: See TracBrowser for help on using the repository browser.