source: test/test-format-parallel-singlethreaded.c @ 8decff7

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 8decff7 was 8decff7, checked in by Shane Alcock <salcock@…>, 6 years ago

Update all parallel tests so they compile and run

  • Property mode set to 100644
File size: 9.6 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,"rawerf"))
69                return "rawerf:traces/100_packets.erf";
70        if (!strcmp(type,"pcap"))
71                return "pcap:traces/100_packets.pcap";
72        if (!strcmp(type,"wtf"))
73                return "wtf:traces/wed.wtf";
74        if (!strcmp(type,"rtclient"))
75                return "rtclient:chasm";
76        if (!strcmp(type,"pcapfile"))
77                return "pcapfile:traces/100_packets.pcap";
78        if (!strcmp(type,"pcapfilens"))
79                return "pcapfile:traces/100_packetsns.pcap";
80        if (!strcmp(type, "duck"))
81                return "duck:traces/100_packets.duck";
82        if (!strcmp(type, "legacyatm"))
83                return "legacyatm:traces/legacyatm.gz";
84        if (!strcmp(type, "legacypos"))
85                return "legacypos:traces/legacypos.gz";
86        if (!strcmp(type, "legacyeth"))
87                return "legacyeth:traces/legacyeth.gz";
88        if (!strcmp(type, "tsh"))
89                return "tsh:traces/10_packets.tsh.gz";
90        return type;
91}
92
93struct TLS {
94        bool seen_start_message;
95        bool seen_stop_message;
96        bool seen_resuming_message;
97        bool seen_pausing_message;
98        int count;
99};
100
101struct final {
102        int threads;
103        int packets;
104};
105
106static void *report_start(libtrace_t *trace UNUSED,
107                libtrace_thread_t *t UNUSED,
108                void *global) {
109        uint32_t *magic = (uint32_t *)global;
110        struct final *threadcounter =
111                        (struct final *)malloc(sizeof(struct final));
112
113        assert(*magic == 0xabcdef);
114
115        threadcounter->threads = 0;
116        threadcounter->packets = 0;
117        return threadcounter;
118}
119
120static void report_cb(libtrace_t *trace UNUSED,
121                libtrace_thread_t *sender UNUSED,
122                void *global, void *tls, libtrace_result_t *res) {
123
124        uint32_t *magic = (uint32_t *)global;
125        struct final *threadcounter = (struct final *)tls;
126
127        assert(*magic == 0xabcdef);
128        assert(res->key == 0);
129
130        threadcounter->threads ++;
131        threadcounter->packets += res->value.sint;
132        printf("%d\n", res->value.sint);
133}
134
135static void report_end(libtrace_t *trace, libtrace_thread_t *t UNUSED,
136                void *global, void *tls) {
137
138        uint32_t *magic = (uint32_t *)global;
139        struct final *threadcounter = (struct final *)tls;
140
141        assert(*magic == 0xabcdef);
142        assert(threadcounter->threads == trace_get_perpkt_threads(trace));
143        assert(threadcounter->packets == 100);
144
145        free(threadcounter);
146}
147
148static libtrace_packet_t *per_packet(libtrace_t *trace UNUSED,
149                libtrace_thread_t *t UNUSED,
150                void *global, void *tls, libtrace_packet_t *packet) {
151        struct TLS *storage = (struct TLS *)tls;
152        uint32_t *magic = (uint32_t *)global;
153        static __thread int count = 0;
154        int a,*b,c=0;
155
156        assert(storage != NULL);
157        assert(!storage->seen_stop_message);
158
159        if (storage->seen_pausing_message)
160                assert(storage->seen_resuming_message);
161
162        assert(*magic == 0xabcdef);
163
164        storage->count ++;
165        count ++;
166
167        assert(count == storage->count);
168
169        if (count > 100) {
170                fprintf(stderr, "Too many packets -- someone should stop me!\n");
171                kill(getpid(), SIGTERM);
172        }
173
174        // Do some work to even out the load on cores
175        b = &c;
176        for (a = 0; a < 10000000; a++) {
177                c += a**b;
178        }
179
180        return packet;
181}
182
183static void *start_processing(libtrace_t *trace, libtrace_thread_t *t UNUSED,
184                void *global) {
185
186        static __thread bool seen_start_message = false;
187        uint32_t *magic = (uint32_t *)global;
188        struct TLS *storage = NULL;
189        assert(*magic == 0xabcdef);
190
191        assert(!seen_start_message);
192        assert(trace);
193
194        storage = (struct TLS *)malloc(sizeof(struct TLS));
195        storage->seen_start_message = true;
196        storage->seen_stop_message = false;
197        storage->seen_resuming_message = false;
198        storage->seen_pausing_message = false;
199        storage->count = 0;
200
201        seen_start_message = true;
202
203        return storage;
204}
205
206static void stop_processing(libtrace_t *trace, libtrace_thread_t *t,
207                void *global, void *tls) {
208
209        static __thread bool seen_stop_message = false;
210        struct TLS *storage = (struct TLS *)tls;
211        uint32_t *magic = (uint32_t *)global;
212
213        assert(storage != NULL);
214        assert(!storage->seen_stop_message);
215        assert(!seen_stop_message);
216        assert(storage->seen_start_message);
217        assert(*magic == 0xabcdef);
218
219        seen_stop_message = true;
220        storage->seen_stop_message = true;
221
222        trace_publish_result(trace, t, (uint64_t) 0, (libtrace_generic_t){.sint = storage->count}, RESULT_USER);
223        trace_post_reporter(trace);
224        free(storage);
225}
226
227static void process_tick(libtrace_t *trace UNUSED, libtrace_thread_t *t UNUSED,
228                void *global UNUSED, void *tls UNUSED, uint64_t tick UNUSED) {
229
230        fprintf(stderr, "Not expecting a tick packet\n");
231        kill(getpid(), SIGTERM);
232}
233
234static void pause_processing(libtrace_t *trace UNUSED,
235                libtrace_thread_t *t UNUSED,
236                void *global, void *tls) {
237
238        static __thread bool seen_pause_message = false;
239        struct TLS *storage = (struct TLS *)tls;
240        uint32_t *magic = (uint32_t *)global;
241
242        assert(storage != NULL);
243        assert(!storage->seen_stop_message);
244        assert(storage->seen_start_message);
245        assert(*magic == 0xabcdef);
246
247        assert(seen_pause_message == storage->seen_pausing_message);
248
249        seen_pause_message = true;
250        storage->seen_pausing_message = true;
251}
252
253static void resume_processing(libtrace_t *trace UNUSED,
254                libtrace_thread_t *t UNUSED,
255                void *global, void *tls) {
256
257        static __thread bool seen_resume_message = false;
258        struct TLS *storage = (struct TLS *)tls;
259        uint32_t *magic = (uint32_t *)global;
260
261        assert(storage != NULL);
262        assert(!storage->seen_stop_message);
263        assert(storage->seen_start_message);
264        assert(*magic == 0xabcdef);
265
266        assert(seen_resume_message == storage->seen_resuming_message);
267
268        seen_resume_message = true;
269        storage->seen_resuming_message = true;
270}
271
272int main(int argc, char *argv[]) {
273        int error = 0;
274        const char *tracename;
275        libtrace_t *trace;
276        libtrace_callback_set_t *processing = NULL;
277        libtrace_callback_set_t *reporter = NULL;
278        uint32_t global = 0xabcdef;
279
280        if (argc<2) {
281                fprintf(stderr,"usage: %s type\n",argv[0]);
282                return 1;
283        }
284
285        tracename = lookup_uri(argv[1]);
286
287        trace = trace_create(tracename);
288        iferr(trace,tracename);
289
290        processing = trace_create_callback_set();
291        trace_set_starting_cb(processing, start_processing);
292        trace_set_stopping_cb(processing, stop_processing);
293        trace_set_packet_cb(processing, per_packet);
294        trace_set_pausing_cb(processing, pause_processing);
295        trace_set_resuming_cb(processing, resume_processing);
296        trace_set_tick_count_cb(processing, process_tick);
297        trace_set_tick_interval_cb(processing, process_tick);
298
299        reporter = trace_create_callback_set();
300        trace_set_starting_cb(reporter, report_start);
301        trace_set_stopping_cb(reporter, report_end);
302        trace_set_result_cb(reporter, report_cb);
303
304        /* Limit this to just one thread */
305        trace_set_perpkt_threads(trace, 1);
306
307        trace_pstart(trace, &global, processing, reporter);
308        iferr(trace,tracename);
309
310        /* Make sure traces survive a pause */
311        trace_ppause(trace);
312        iferr(trace,tracename);
313        trace_pstart(trace, NULL, NULL, NULL);
314        iferr(trace,tracename);
315
316        /* Wait for all threads to stop */
317        trace_join(trace);
318
319        global = 0xffffffff;
320
321        /* Now check we have all received all the packets */
322        if (error != 0) {
323                iferr(trace,tracename);
324        }
325
326        trace_destroy(trace);
327        trace_destroy_callback_set(processing);
328        trace_destroy_callback_set(reporter);
329        return error;
330}
Note: See TracBrowser for help on using the repository browser.