~ubuntu-branches/ubuntu/vivid/drizzle/vivid

« back to all changes in this revision

Viewing changes to tests/test_tools/randgen/combinations.pl

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-10-29 15:43:40 UTC
  • mfrom: (20.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20131029154340-j36v7gxq9tm1gi5f
Tags: 1:7.2.3-2ubuntu1
* Merge from debian, remaining changes:
  - Link against boost_system because of boost_thread.
  - Add required libs to message/include.am
  - Add upstart job and adjust init script to be upstart compatible.
  - Disable -floop-parallelize-all due to gcc-4.8/4.9 compiler ICE
    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57732

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008, 2011 Oracle and/or its affiliates. All rights reserved.
 
2
# Use is subject to license terms.
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; version 2 of the License.
 
7
#
 
8
# This program is distributed in the hope that it will be useful, but
 
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
11
# General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 
16
# USA
 
17
 
 
18
use strict;
 
19
use lib 'lib';
 
20
use lib "$ENV{RQG_HOME}/lib";
 
21
use Carp;
 
22
#use List::Util 'shuffle';
 
23
use Cwd;
 
24
use GenTest;
 
25
use GenTest::Random;
 
26
use GenTest::Constants;
 
27
use Getopt::Long;
 
28
use GenTest::BzrInfo; 
 
29
use Data::Dumper;
 
30
 
 
31
my $logger;
 
32
eval
 
33
{
 
34
    require Log::Log4perl;
 
35
    Log::Log4perl->import();
 
36
    $logger = Log::Log4perl->get_logger('randgen.gentest');
 
37
};
 
38
 
 
39
if (osWindows()) {
 
40
    croak("Sorry. $0 is not ported to Windows (yet)");
 
41
}
 
42
 
 
43
$| = 1;
 
44
my $ctrl_c = 0;
 
45
    
 
46
$SIG{INT} = sub { $ctrl_c = 1 };
 
47
$SIG{TERM} = sub { exit(0) };
 
48
$SIG{CHLD} = "IGNORE" if osWindows();
 
49
 
 
50
my ($config_file, $basedir, $vardir, $trials, $duration, $grammar, $gendata, 
 
51
    $seed, $testname, $xml_output, $report_xml_tt, $report_xml_tt_type,
 
52
    $report_xml_tt_dest, $force, $no_mask, $exhaustive, $start_combination, $debug, $noLog, 
 
53
    $threads, $new, $servers, $noshuffle, $workdir);
 
54
 
 
55
my @basedirs=('','');
 
56
my $combinations;
 
57
my %results;
 
58
my @commands;
 
59
my $max_result = 0;
 
60
my $thread_id = 0;
 
61
 
 
62
my $mtrbt = defined $ENV{MTR_BUILD_THREAD}?$ENV{MTR_BUILD_THREAD}:300;
 
63
 
 
64
my $opt_result = GetOptions(
 
65
        'config=s' => \$config_file,
 
66
        'basedir=s' => \$basedirs[0],
 
67
        'basedir1=s' => \$basedirs[0],
 
68
        'basedir2=s' => \$basedirs[1],
 
69
        'workdir=s' => \$workdir,
 
70
        'vardir=s' => \$workdir,
 
71
        'trials=i' => \$trials,
 
72
        'duration=i' => \$duration,
 
73
        'seed=s' => \$seed,
 
74
        'force' => \$force,
 
75
        'no-mask' => \$no_mask,
 
76
        'grammar=s' => \$grammar,
 
77
        'gendata=s' => \$gendata,
 
78
        'testname=s' => \$testname,
 
79
        'xml-output=s' => \$xml_output,
 
80
        'report-xml-tt' => \$report_xml_tt,
 
81
        'report-xml-tt-type=s' => \$report_xml_tt_type,
 
82
        'report-xml-tt-dest=s' => \$report_xml_tt_dest,
 
83
    'run-all-combinations-once' => \$exhaustive,
 
84
    'start-combination=i' => \$start_combination,
 
85
    'debug' => \$debug,
 
86
    'no-log' => \$noLog,
 
87
    'parallel=i' => \$threads,
 
88
    'new' => \$new,
 
89
    'servers=i' => \$servers,
 
90
    'no-shuffle' => \$noshuffle
 
91
);
 
92
 
 
93
my $prng = GenTest::Random->new(
 
94
        seed => $seed eq 'time' ? time() : $seed
 
95
);
 
96
 
 
97
open(CONF, $config_file) or croak "unable to open config file '$config_file': $!";
 
98
read(CONF, my $config_text, -s $config_file);
 
99
eval ($config_text);
 
100
croak "Unable to load $config_file: $@" if $@;
 
101
 
 
102
if (!defined $servers) {
 
103
    $servers = 1;
 
104
    $servers = 2 if $basedirs[1] ne '';
 
105
}
 
106
 
 
107
croak "--servers may only be 1 or 2" if !($servers == 1 or $servers == 2);
 
108
 
 
109
my $logToStd = !$noLog;
 
110
 
 
111
my $bzrinfo = GenTest::BzrInfo->new(
 
112
    dir => cwd()
 
113
    ); 
 
114
my $revno = $bzrinfo->bzrRevno();
 
115
my $revid = $bzrinfo->bzrRevisionId();
 
116
 
 
117
if ((defined $revno) && (defined $revid)) {
 
118
    say(cwd()." Revno: $revno");
 
119
    say(cwd()." Revision-Id: $revid");
 
120
} else {
 
121
    say(cwd().' does not look like a bzr branch, cannot get revision info.');
 
122
 
123
 
 
124
if (not defined $threads) {
 
125
    $threads=1;
 
126
} else {
 
127
    if ((not defined $trials) and (not defined $exhaustive)) {
 
128
        croak("When using --parallel, also add either or both of these options: --run-all-combinations-once (exhaustive run) and/or --trials=x (random run).\n(Both options combined gives a non-random exhaustive run, yet limited by the number of trials.)");
 
129
    }
 
130
    $logToStd = 0;
 
131
}
 
132
 
 
133
say("Using workdir=".$workdir);
 
134
 
 
135
my $comb_count = $#$combinations + 1;
 
136
 
 
137
my $total = 1;
 
138
my $thread_id;
 
139
if ($exhaustive) {
 
140
    foreach my $comb_id (0..($comb_count-1)) {
 
141
        $total *= $#{$combinations->[$comb_id]}+1;
 
142
    }
 
143
    if (defined $trials) {
 
144
        if ($trials < $total) {
 
145
            say("You specified --run-all-combinations-once, which gives $total combinations, but then limited the same with --trials=$trials");
 
146
        } else {
 
147
            $trials = $total;
 
148
        }
 
149
    } else {
 
150
        $trials = $total;
 
151
    }
 
152
}
 
153
 
 
154
my %pids;
 
155
for my $i (1..$threads) {
 
156
    my $pid = fork();
 
157
    if ($pid == 0) {
 
158
        ## Child
 
159
        $thread_id = $i;
 
160
        mkdir($workdir);
 
161
        
 
162
        if ($exhaustive) {
 
163
            doExhaustive(0);
 
164
        } else {
 
165
            doRandom();
 
166
        }
 
167
        ## Children does not continue this loop
 
168
        last;
 
169
    } else {
 
170
        ##Parent
 
171
        $thread_id = 0;
 
172
        $pids{$pid}=$i;
 
173
        say("Started thread [$i] pid=$pid");
 
174
    }
 
175
}
 
176
 
 
177
if ($thread_id > 0) {
 
178
    ## Child
 
179
    ##say("[$thread_id] Summary of various interesting strings from the logs:");
 
180
    ##say("[$thread_id] ". Dumper \%results);
 
181
    #foreach my $string ('text=', 'bugcheck', 'Error: assertion', 'mysqld got signal', 'Received signal', 'exception') {
 
182
    #    system("grep -i '$string' $workdir/trial*log");
 
183
    #} 
 
184
    
 
185
    say("[$thread_id] will exit with exit status ".status2text($max_result).
 
186
        "($max_result)");
 
187
    exit($max_result);
 
188
} else {
 
189
    ## Parent
 
190
    my $total_status = 0;
 
191
    while(1) {
 
192
        my $child = wait();
 
193
        last if $child == -1;
 
194
        my $exit_status = $? > 0 ? ($? >> 8) : 0;
 
195
        #say("Thread $pids{$child} (pid=$child) exited with $exit_status");
 
196
        $total_status = $exit_status if $exit_status > $total_status;
 
197
    }
 
198
    say("$0 will exit with exit status ".status2text($total_status).
 
199
        "($total_status)");
 
200
    exit($total_status);
 
201
}
 
202
 
 
203
 
 
204
 
 
205
## ----------------------------------------------------
 
206
 
 
207
my $trial_counter = 0;
 
208
 
 
209
sub doExhaustive {
 
210
    my ($level,@idx) = @_;
 
211
    if ($level < $comb_count) {
 
212
        my @alts;
 
213
        foreach my $i (0..$#{$combinations->[$level]}) {
 
214
            push @alts, $i;
 
215
        }
 
216
        $prng->shuffleArray(\@alts) if !$noshuffle;
 
217
        
 
218
        foreach my $alt (@alts) {
 
219
            push @idx, $alt;
 
220
            doExhaustive($level+1,@idx) if $trial_counter < $trials;
 
221
            pop @idx;
 
222
        }
 
223
    } else {
 
224
        $trial_counter++;
 
225
        my @comb;
 
226
        foreach my $i (0 .. $#idx) {
 
227
            push @comb, $combinations->[$i]->[$idx[$i]];
 
228
        }
 
229
        my $comb_str = join(' ', @comb);
 
230
        next if $trial_counter < $start_combination;
 
231
        doCombination($trial_counter,$comb_str,"combination");
 
232
    }
 
233
}
 
234
 
 
235
## ----------------------------------------------------
 
236
 
 
237
sub doRandom {
 
238
    foreach my $trial_id (1..$trials) {
 
239
        my @comb;
 
240
        foreach my $comb_id (0..($comb_count-1)) {
 
241
            my $n = $prng->uint16(0, $#{$combinations->[$comb_id]});
 
242
            $comb[$comb_id] = $combinations->[$comb_id]->[$n];
 
243
        }
 
244
        my $comb_str = join(' ', @comb);        
 
245
        doCombination($trial_id,$comb_str,"random trial");
 
246
    }
 
247
}
 
248
 
 
249
## ----------------------------------------------------
 
250
sub doCombination {
 
251
    my ($trial_id,$comb_str,$comment) = @_;
 
252
 
 
253
    return if (($trial_id -1) % $threads +1) != $thread_id;
 
254
    say("[$thread_id] Running $comment ".$trial_id."/".$trials);
 
255
        my $mask = $prng->uint16(0, 65535);
 
256
 
 
257
    my $runall = $new?"runall-new.pl":"runall.pl";
 
258
 
 
259
        my $command = "
 
260
                perl ".($Carp::Verbose?"-MCarp=verbose ":"").
 
261
        (defined $ENV{RQG_HOME} ? $ENV{RQG_HOME}."/" : "" ).
 
262
        "$runall --queries=100000000 $comb_str ";
 
263
 
 
264
    $command .= " --mtr-build-thread=".($mtrbt+($thread_id-1)*2);
 
265
        $command .= " --mask=$mask" if not defined $no_mask;
 
266
        $command .= " --duration=$duration" if $duration ne '';
 
267
    foreach my $s (1..$servers) {
 
268
        $command .= " --basedir".$s."=".$basedirs[$s-1]." " if $basedirs[$s-1] ne '';
 
269
    }
 
270
        $command .= " --gendata=$gendata " if $gendata ne '';
 
271
        $command .= " --grammar=$grammar " if $grammar ne '';
 
272
        $command .= " --seed=$seed " if $seed ne '';
 
273
        $command .= " --testname=$testname " if $testname ne '';
 
274
        $command .= " --xml-output=$xml_output " if $xml_output ne '';
 
275
        $command .= " --report-xml-tt" if defined $report_xml_tt;
 
276
        $command .= " --report-xml-tt-type=$report_xml_tt_type " if $report_xml_tt_type ne '';
 
277
        $command .= " --report-xml-tt-dest=$report_xml_tt_dest " if $report_xml_tt_dest ne '';
 
278
 
 
279
    foreach my $s (1..$servers) {
 
280
        $command .= " --vardir".$s."=$workdir/current".$s."_$thread_id " if $command !~ m{--mem}sio && $workdir ne '';
 
281
    }
 
282
        $command =~ s{[\t\r\n]}{ }sgio;
 
283
    if ($logToStd) {
 
284
        $command .= " 2>&1 | tee $workdir/trial".$trial_id.'.log';
 
285
    } else {
 
286
        $command .= " > $workdir/trial".$trial_id.'.log'. " 2>&1";
 
287
    }
 
288
 
 
289
        $commands[$trial_id] = $command;
 
290
 
 
291
        $command =~ s{"}{\\"}sgio;
 
292
        $command = 'bash -c "set -o pipefail; '.$command.'"';
 
293
 
 
294
    if ($logToStd) {
 
295
        say("[$thread_id] $command");
 
296
    }
 
297
    my $result = 0;
 
298
    $result = system($command) if not $debug;
 
299
 
 
300
        $result = $result >> 8;
 
301
        say("[$thread_id] $runall exited with exit status ".status2text($result).
 
302
        "($result), see $workdir/trial".$trial_id.'.log');
 
303
        exit($result) if (($result == STATUS_ENVIRONMENT_FAILURE) || ($result == 255)) && (not defined $force);
 
304
 
 
305
        if ($result > 0) {
 
306
        foreach my $s (1..$servers) {
 
307
            $max_result = $result if $result > $max_result;
 
308
            my $from = $workdir."/current".$s."_".$thread_id;
 
309
            my $to = $workdir."/vardir".$s."_".$trial_id;
 
310
            say("[$thread_id] Copying $from to $to") if $logToStd;
 
311
            if ($command =~ m{--mem}) {
 
312
                system("cp -r /dev/shm/var $to");
 
313
            } else {
 
314
                system("cp -r $from $to");
 
315
            }
 
316
            open(OUT, ">$to/command");
 
317
            print OUT $command;
 
318
            close(OUT);
 
319
        }
 
320
        }
 
321
        $results{$result >> 8}++;
 
322
}