source: test/test-format-parallel-hasher.c @ 6b98325

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivelibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 6b98325 was 6b98325, checked in by Richard Sanger <rsangerarj@…>, 6 years ago

Removes pconfig_input() and replaces trace_config() with trace_set_<option>

  • Removes trace_pconfig completely - only trace_set_hasher is left now in trace_config
  • Rework user configuration so this is no longer modified directly. Now we just apply it directly to a trace.
  • Many minor documentation fixes and additions
  • Property mode set to 100644
File size: 7.0 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_resumed_message;
97        bool seen_pausing_message;
98        int count;
99};
100
101static int totalpkts = 0;
102static int expected;
103static void report_result(libtrace_t *trace UNUSED, int mesg,
104                          libtrace_generic_t data,
105                          libtrace_thread_t *sender UNUSED) {
106        static int totalthreads = 0;
107        switch (mesg) {
108        case MESSAGE_RESULT:
109                assert(data.res->key == 0);
110                printf("%d,", data.res->value.sint);
111                totalthreads++;
112                totalpkts += data.res->value.sint;
113                assert(data.res->value.sint == 25 ||
114                       data.res->value.sint == expected - 25);
115                break;
116        case MESSAGE_STARTING:
117                // Should have two threads here
118                assert(libtrace_get_perpkt_count(trace) == 2);
119                printf("\tLooks like %d threads are being used!\n\tcounts(", libtrace_get_perpkt_count(trace));
120                break;
121        case MESSAGE_STOPPING:
122                printf(")\n");
123                assert(totalthreads == libtrace_get_perpkt_count(trace));
124                break;
125        }
126}
127
128static int x;
129static void* per_packet(libtrace_t *trace, libtrace_thread_t *t,
130                        int mesg, libtrace_generic_t data,
131                        libtrace_thread_t *sender UNUSED) {
132        struct TLS *tls;
133        void* ret;
134        tls = trace_get_tls(t);
135        int a,*b,c=0;
136
137        switch (mesg) {
138        case MESSAGE_PACKET:
139                assert(tls != NULL);
140                assert(!(tls->seen_stop_message));
141                tls->count++;
142                if (tls->count>100) {
143                        fprintf(stderr, "Too many packets someone should stop me!!\n");
144                        kill(getpid(), SIGTERM);
145                }
146                // Do some work to even out the load on cores
147                b = &c;
148                for (a = 0; a < 10000000; a++) {
149                        c += a**b;
150                }
151                x = c;
152                return data.pkt;
153        case MESSAGE_STARTING:
154                assert(tls == NULL);
155                tls = calloc(sizeof(struct TLS), 1);
156                ret = trace_set_tls(t, tls);
157                assert(ret == NULL);
158                tls->seen_start_message = true;
159                break;
160        case MESSAGE_STOPPING:
161                assert(tls->seen_start_message);
162                assert(tls != NULL);
163                tls->seen_stop_message = true;
164                trace_set_tls(t, NULL);
165
166                // All threads publish to verify the thread count
167                assert(tls->count == 25 || tls->count == 75);
168                trace_publish_result(trace, t, (uint64_t) 0, (libtrace_generic_t){.sint=tls->count}, RESULT_USER);
169                trace_post_reporter(trace);
170                free(tls);
171                break;
172        case MESSAGE_TICK_INTERVAL:
173        case MESSAGE_TICK_COUNT:
174                assert(tls->seen_start_message );
175                fprintf(stderr, "Not expecting a tick packet\n");
176                kill(getpid(), SIGTERM);
177                break;
178        case MESSAGE_PAUSING:
179                assert(tls->seen_start_message);
180                tls->seen_pausing_message = true;
181                break;
182        case MESSAGE_RESUMING:
183                assert(tls->seen_pausing_message  || tls->seen_start_message);
184                tls->seen_resumed_message = true;
185                break;
186        }
187        return NULL;
188}
189
190
191/**
192 * Sends the first 25 packets to thread 0, the next 75 to thread 1
193 * This is based on a few internal workings assumptions, which
194 * might change and still be valid even if this test fails!!.
195 */
196uint64_t hash25_75(const libtrace_packet_t* packet UNUSED, void *data) {
197        int *count = (int *) data;
198        *count += 1;
199        if (*count <= 25)
200                return 0;
201        return 1;
202}
203
204/**
205 * Test that the hasher function works
206 */
207int test_hasher(const char *tracename) {
208        libtrace_t *trace;
209        int error = 0;
210        int hashercount = 0;
211        printf("Testing hasher function\n");
212
213        // Create the trace
214        trace = trace_create(tracename);
215        iferr(trace,tracename);
216
217        // Always use 2 threads for simplicity
218        trace_set_perpkt_threads(trace, 2);
219        trace_set_hasher(trace, HASHER_CUSTOM, &hash25_75, &hashercount);
220
221        // Start it
222        trace_pstart(trace, NULL, per_packet, report_result);
223        iferr(trace,tracename);
224        /* Make sure traces survive a pause and restart */
225        trace_ppause(trace);
226        iferr(trace,tracename);
227        trace_pstart(trace, NULL, NULL, NULL);
228        iferr(trace,tracename);
229
230        /* Wait for all threads to stop */
231        trace_join(trace);
232
233        /* Now check we have all received all the packets */
234        if (error == 0) {
235                if (totalpkts == expected) {
236                        printf("success: %d packets read\n",expected);
237                } else {
238                        printf("failure: %d packets expected, %d seen\n",expected,totalpkts);
239                        error = 1;
240                }
241        } else {
242                iferr(trace,tracename);
243        }
244    trace_destroy(trace);
245    return error;
246}
247
248
249
250int main(int argc, char *argv[]) {
251        int error = 0;
252        const char *tracename;
253        expected = 100;
254
255        if (argc<2) {
256                fprintf(stderr,"usage: %s type\n",argv[0]);
257                return 1;
258        }
259
260        tracename = lookup_uri(argv[1]);
261
262        if (strcmp(argv[1],"rtclient")==0) expected=101;
263
264        error = test_hasher(tracename);
265
266    return error;
267}
Note: See TracBrowser for help on using the repository browser.