File: brutal

package info (click to toggle)
emcast 0.3.2-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,532 kB
  • ctags: 898
  • sloc: sh: 9,351; ansic: 7,234; perl: 731; makefile: 168
file content (366 lines) | stat: -rwxr-xr-x 7,017 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/perl -w

# Usage: brutal <url>
#
# brutal creates the emcast group <url> and then joins it multiple
# times.  The joiners are randomly killed and restarted.  If a 
# core file is created, 
#
# Internally, it uses the emcast program to launch the programs.

use IPC::Open2;

my $emcast = "../src/emcast -l";  # Location of emcast program
#my $emcast = "../btp/btpcat -l";  # Location of emcast program

my $max_joiners = 0;           # Number of clients who join
my $max_joins   = 1;           # Number of times each joiner will join
my $max_sends   = 1;           # Number of sends per join
my $send_rate   = 1.0;         # Time between sends

my $create_sleep_time = 1;     # Seconds to sleep after creating group
my $interjoin_sleep_time = 0;  # Seconds to sleep between joins
my $join_sleep_time   = 5;     # Seconds to sleep after joining group
my $linger_time       = 5;     # Seconds to wait after sending last message

my $creator_sends = 0;         # Creator sends
my $joiners_killed = 0;        # Kill every other joiner?

my $debug = 0;
my $log = 0;    # Stderr process log 
my $log2 = 1;   # Brutal log

my $id = 0;

my $options = "";
#$options = "-Obtp_debug_flags 0xffffffff";
#$options = "-d 0xc5cc";


####################
# Parse command line

while ($#ARGV > 0)
{
    $_ = shift(@ARGV);

    if    (/--joiners/)  { $max_joiners = shift(@ARGV); }
    elsif (/--joins/)    { $max_joins   = shift(@ARGV); }
    elsif (/--sends/)    { $max_sends   = shift(@ARGV); }
    elsif (/--sendrate/) { $send_rate   = shift(@ARGV); }
    elsif (/--linger/)   { $linger_time = shift(@ARGV); }
    elsif (/--kills/)    { $joiners_killed = 1;         }
    elsif (/--debug/)    { $debug       = 1;            }
    elsif (/--log/)      { $log         = 1;            }
    elsif (/--log-brutal/){ $log2       = 1;            }
    else                 
    { 
	print STDERR "$0: Bad option: $_\n";
	usage();                     
    }
}

usage () if ($#ARGV < 0);
my $url = shift(@ARGV);


####################

my @pids = ();

sub main
{
    $| = 1;
    $SIG{'INT'} = 'on_sigint';

    # Launch creator
    emcreate ($url);
    sleep ($create_sleep_time);

    # Launch joiners
    my $i;
    for ($i = 0; $i < $max_joiners; $i++)
    {
	emjoin ($url, $i);
	sleep ($interjoin_sleep_time) if $interjoin_sleep_time;
    }

    # Wait for all but one (the creator) to finish
    while ($#pids > 0 && (my $kid = wait) != -1)
    {
	print STDERR "KID $kid DONE\n" if $debug;

	if (-f 'core')
	{
	    rename ('core', "core.$kid");
	    print STDERR "$0 WARNING: core file (moved to core.$kid)\n";
	}

	for ($i = 0; $i <= $#pids; $i++)
	{
	    if ($pids[$i] == $kid)
	    {
		splice (@pids, $i, 1);
		last;
	    }
	}
    }

    kill_processes();

    exit 0;

}

main();


####################


sub usage
{
    print STDERR ("Usage: $0 [OPTIONS] <URL>\n");
    exit 1;
}


sub emcreate
{
    my $url = $_[0];

    print STDERR "CREATE $url\n" if $debug;
    run ($id++, "$emcast $options $url", \&run_creator);
}


sub emjoin
{
    my ($url, $num) = @_;

    print STDERR "JOIN $url $num\n" if $debug;
    run ($id++, "$emcast $options $url", \&run_joiner);
}


sub run
{
    my ($id, $cmd, $fun) = @_;
    my $pid;

    if ($pid = fork)         # Parent
    {
	push (@pids, $pid);
    }
    elsif (defined $pid)     # Child
    {
	setpgrp (0, $$);
	$fun->($id, $cmd);
	exit (0);
    }
    else                     # Error
    { 
	die ("Fork error: $!\n"); 
    }
}


sub run_creator
{
    my ($id, $cmd) = @_;
    my $pid;
    my ($tot_sends, $tot_recvs, $tot_errs) = (0, 0, 0);

    open (STDERR, ">creator-$$.log") if $log;
    if ($pid = open2(\*RDR, \*WTR, $cmd))
    {
	$max_sends = 0 if !$creator_sends;

	while (1) 
	{
	    # Send and receive data
	    my ($sends, $recvs, $errs);
	    ($sends, $recvs, $errs) = loop ($pid);
	    $tot_sends += $sends;
	    $tot_recvs += $recvs;
	    $tot_errs  += $errs;
	}
    }
    else
    {
	die ("open2 error: $!\n");
    }

    print ("CREATOR: SEND $tot_sends RECV $tot_recvs ERROR $tot_errs\n");
}


sub run_joiner
{
    my ($id, $cmd) = @_;
    my $i;
    my $pid;
    my ($tot_sends, $tot_recvs, $tot_errs) = (0, 0, 0);

    # Do $max_runs runs
    open (STDERR, ">joiner-$$.log") if $log;
    for ($i = 0; $i < $max_joins; $i++)
    {
	# Launch emcast
	if ($pid = open2(\*RDR, \*WTR, $cmd))
	{
	    sleep ($join_sleep_time) if $join_sleep_time;

	    # Send and receive data
	    my ($sends, $recvs, $errs);
	    ($sends, $recvs, $errs) = loop ($pid);
	    $tot_sends += $sends;
	    $tot_recvs += $recvs;
	    $tot_errs  += $errs;

	    # Close or kill emcast
	    if ($joiners_killed && ($i % 2))
	    {
		print STDERR "$pid KILL\n" if $debug;
		kill (9, $pid);
	    }
	    else
	    {
		print STDERR "$pid CLOSE\n" if $debug;
		close (WTR);
	    }

	    waitpid $pid, 0;
	}
	else
	{
	    die ("open2 error: $!\n"); 
	}
    }

    print ("JOINER $id: SEND $tot_sends RECV $tot_recvs ERROR $tot_errs\n");

    exit 0;
}


sub loop
{
    my ($pid) = @_;

    my $num_sends = 0;

    if ($num_sends < $max_sends)
    {
	$num_sends++;
	print WTR "MESSAGE $pid $num_sends\n";
	print "$pid SEND: MESSAGE $pid $num_sends\n" if $log2;
    }

    my ($rin, $rout);
    my ($timeout, $timeout_time);
    my $linger = 0;

    $timeout = $send_rate;
    $timeout = undef if !$max_sends;
    $timestart = time();
    $rin = '';
    vec($rin, fileno(RDR), 1) = 1;

    my ($recvs, $errs) = (0, 0);

    while (my ($nfound) = select($rout = $rin, undef, undef, $timeout))
    {
	if (vec($rout, fileno(RDR), 1))
	{
	    $! = 0;
	    my $line = readline(*RDR);
	    if ($! != 0)
	    {
		$errs++;
		print STDERR "ERROR: readline failed in loop\n";
		last;
	    }
	    print STDERR "EOF\n" if !defined $line;
	    last if (!defined($line));
	    $recvs++;

	    print STDERR $line if $debug;
	    print STDOUT "$pid RECV: $line" if $log2;
	}

	if (defined($timeout))
	{
	    my $timepassed = time() - $timestart;

	    # Check linger timer
	    if ($linger && $timepassed >= $linger_time)
	    {
		last;
	    }
	    # Check send timer
	    elsif (!$linger && $timepassed >= $send_rate)
	    {
		$num_sends++;
		print WTR "MESSAGE $pid $num_sends\n";
		print "$pid SEND: MESSAGE $pid $num_sends\n" if $log2;
		# Check if we can quit
		if ($num_sends >= $max_sends)
		{
		    if ($linger_time)  # Linger
		    {
			$linger = 1;
			$timeout = $linger_time;
			$timestart = time();
		    }
		    else
		    {
			last;
		    }
		}
		else
		{
		    # Set next timer
		    $timeout = $send_rate;
		    $timestart = time();
		}
	    }
	    # Update timer
	    else
	    {
		if ($linger)
		{
		    $timeout = $linger_time - $timepassed;
		}
		else
		{
		    $timeout = $send_rate - $timepassed;
		}
	    }
	}

	$rin = '';
	vec($rin, fileno(RDR), 1) = 1;
    }

    return ($num_sends, $recvs, $errs);
}



sub on_sigint
{
    kill_processes ();
    exit 0;
}


sub kill_processes
{
    # Kill all children
    foreach $pid (@pids)
    {
	kill (-9, $pid);
	waitpid ($pid, 0);
    }
}