2 use strict;
3 use warnings;
4
5 use Socket;
6 use Switch;
7 use POSIX ":sys_wait_h";
8
9
10 #==================================================
11 # main routine
12 #==================================================
13 print_banner_begin();
14
15 my $command = "sudo ./az_mmap1";
16 #------------
17 # vmsysqa5
18 #-------------
19 #my $target_machine = "10.10.213.174"; #"conchiglie";
20 #my $target_directory = "/home/gzhang";
21 #-------------
22 # conchiglie
23 #-------------
24 my $target_machine = "conchiglie";
25 my $target_directory = "/home/gzhang/P4/qa/x86/projects/tests/AVX/Syscalls";
26
27
28 my $task = "task";
29 my $user = "gzhang";
30 my $port = "1000";
31 my $child_wait = 10;
32 my $parent_wait = 15;
33
34 my $timeout_value = 60;
35 my $finish = 0; #timeout flag
36
37
38 my $remote_pidfile = "/var/tmp/syscalls.pid";
39 my $file = "syscalls.txt";
40 my $DIR = "/var/tmp";
41 my $filew = 0;
42 my $filelog = 0;
43 my $totalno = 0;
44 my @epermarr = ("az_pgroup_create_eperm",
45 "az_pgroup_destroy_eperm",
"az_pgroup_set_data_eperm",
47 "az_pgroup_set_rlimit_eperm",
48 "az_pgroup_uncommit_all_eperm",
49 "az_tok_delete_all_eperm",
50 "az_tok_delete_eperm"
51 );
52
53
54 if ($#ARGV > 0) {
55 $filew = "$DIR/$ARGV[0]";
56 $filelog = "$DIR/$ARGV[1]";
57 } else {
58 $filew = "$DIR/syscalls.result";
59 $filelog = "$DIR/syscalls.log";
60 }
61
62
63 open(FH, $file) || die("Could not open $file. $!\n");
64 ##open FHW, ">$filew" or die(" $!\n");
65 #
66
67 # cleanup remote
68 `ssh $target_machine /bin/rm -rf $remote_pidfile` ;
69
70 LINE: while (
71 my ($line) = $_;
72 chomp($line);
73 next LINE if /^$/;
74 next LINE if (/^\s*#/);
75 my $cmd = $line;
76 my @cmd0 = split(/ /, $cmd);
77 switch ($cmd0[0]) {
78 case (\@epermarr) {
79 $command = "./$cmd";
80 }
81 else {
82 $command = "sudo ./$cmd";
83 }
84 }
85 print "TestID: $totalno $command\n";
86 $totalno++;
87
88 start_task($command,
$target_machine,
90 $target_directory,
91 $task,
92 $user,
93 $port );
94 }
95
96 if ($totalno > 3) {
97 exit 0;
98 }
99 } #while loop
100
101
102
103 print_banner_end();
104 exit 0;
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 #==================================================
123 # sub routine
124 #==================================================
125 sub is_remote_process_alive {
126 my ($remote_machine, $pid) = @_;
127 #open fiel to read pid or others
128 #
129 my $remote_str = `ssh $remote_machine tail -n 1 $remote_pidfile `;
130 chomp($remote_str);
131 my ($remote_key, $remote_pid) = split(/:/, $remote_str);
chomp($remote_pid);
133
134 # `ssh $remote_machine "/bin/ps -p $remote_pid 2>&1 /dev/null\n" `;
135 `ssh $remote_machine "/bin/ps -p $remote_pid 1> /dev/null 2> /dev/null\n" `;
136
137 if (($? >> 8) == 0) {
138 return 1;
139 } else {
140 return 0;
141 }
142 #remove others
143 # ` ssh $remote_machine /bin/rm -f $remote_dir/$remote_file_name `;
144 }
145
146
147 sub kill_remote_pid {
148 my ($pid, $remote_machine, $msg) = @_;
149 print "--";
150 # ` ssh $remote_machine kill -9 $pid 2> /dev/null 1> /dev/null ' ;
151
152 return;
153 }
154
155
156 sub get_remote_ip {
157 my ($hostname) = @_;
158
159 my $ip = gethostbyname($hostname);
160 my $ipaddr = 0;
161
162 if (defined $ip) {
163 $ipaddr = inet_ntoa($ip);
164 };
165 return $ipaddr;
166 }
167
168 sub get_remote_hostname {
169 my ($ip) = @_; # "1.2.3.4" format
170 my $ipaddr = inet_aton($ip);
171 my $name = gethostbbyaddr($ipaddr, AF_INET);
172 #or use $straddr = inet_ntoa($ip);
173 return $name;
174 }
177 ##############################################################################
178 # Fire off the command on the specified machine and return the pid
179 sub start_task {
180 my ( $command, $target_machine, $target_directory, $task, $user, $port ) = @_;
181
182 my @cmd0 = split(/\//, $command);
183 my @cmd1 = split(/ /, $cmd0[1]);
184 my $local_stdout = "rbatch_local_stdout_$cmd1[0]";
185 my $local_stderr = "rbatch_local_stderr_$cmd1[0]";
186
187
188 my $task_pid = 0;
189 my $task_status = 0 ;
190
191 # file where we'll be dumping server output :
192 my $id_string = sprintf ( "%s_%s_%s", $task, $user, $port ) ;
193 my $batch_name = sprintf ( "rbatch_%s.sh", $id_string ) ;
194 my $task_stderr = sprintf ( "%s_stderr", $id_string ) ;
195 my $task_stdout = sprintf ( "%s_stdout", $id_string ) ;
196 # my $task_stderr = sprintf ( "%s/%s_stderr", $target_directory, $id_string ) ;
197 # my $task_stdout = sprintf ( "%s/%s_stdout", $target_directory, $id_string ) ;
198
199 # build a little batch that we can launch over on the target machine that
200 # sets all relevant environmental variables, launches the task, tells us
201 # the pid, and whether or not after 12 seconds if the thing is still up,
202 # making the assumption that if it's still alive after 10 seconds that it
203 # didn't die for whatever reason
204 open ( REMOTE_BATCH, ">$batch_name" ) || die "Unable to open *$batch_name*\n";
205 print REMOTE_BATCH "#!/bin/sh\n" ;
206 print REMOTE_BATCH "cd $target_directory; $command 1> ./$task_stderr 2> ./$task_stdo ut &\n" ;
207 print REMOTE_BATCH "task_pid=\$\!\n" ;
208 # this print output goes to rbatch_stderr
209 print REMOTE_BATCH "echo pid of launched task is \$\{task_pid\}\n" ;
210 print REMOTE_BATCH "echo $cmd0[1] : \$\{task_pid\} : RUNNING >> $remote_pidfile \n";
211 print REMOTE_BATCH "sleep $child_wait\n" ; # Might gone: 10seconds
212 #4 print REMOTE_BATCH "/bin/ps -p \$\{task_pid\} 1> /dev/null 2> /dev/null\n" ;
213 #4 print REMOTE_BATCH "pid_status=\$\?\n" ; #0 still there, 1 no more
214 #4 print REMOTE_BATCH "echo pid status is \$\{pid_status\}\n" ;
215 #3 print REMOTE_BATCH "if [ \$\{pid_status\} -ne 0 ]\n" ;
216 #3 print REMOTE_BATCH "then\n" ;
# need wait for a while to flush beffer into err/out since background job.
218 # we need partial of this file for debug:
219 #9 print REMOTE_BATCH "cat $task_stderr\n" ;
220 #9 print REMOTE_BATCH "cat $task_stdout\n" ;
221 #3 print REMOTE_BATCH "fi\n" ;
222
223 close REMOTE_BATCH ;
224
225 # copy the batch file over to the directory over on host
226
227 `scp $batch_name $target_machine:$target_directory 1> $local_stderr 2> $local_stdout ` ;
228
229 my $scp_status = $? >> 8 ;
230 if ( $scp_status != 0 ) {
231 print "Unable to copy $task batch file to $target_machine:$target_directory\n" ;
232 open ( RBATCH, "rbatch_stderr" ) ;
233 while (
234 close RBATCH ;
235 open ( RBATCH, "$local_stdout" ) ;
236 while (
237 close RBATCH ;
238 $task_status = 1 ;
239 } else {
240
241 `ssh $target_machine /bin/chmod 777 $target_directory/$batch_name` ;
242
243 ################################################
244 # Fork off a process and let it start the task
245 FORK: {
246 my $pid = fork();
247 if ( $pid ) {
248 # parent task : child process is in $pid ( note : this is the pid on
249 # the machine we are running on, and not the pid over on the target.... )
250 # print "parent pid on calling host : $pid\n" ;
251 # print "===parent ==\n";
252 sleep 1; # wait for remote to launch
253 eval {
254 local $SIG{ALRM} = sub {
255 $finish = 0; #no timeout
256 };
257
258 alarm $timeout_value;
my $parentdone = 0;
260 while (1) {
261 # task_pid might now ready.
262 my $alive = is_remote_process_alive($target_machine, $task_pid);
263
264 my $ret = waitpid($pid, &WNOHANG);
265 # print "parent is waiting.. $pid, ret=$ret\n";
266 if ($ret == $pid) {
267 my $exit_value = $? >> 8;
268 #print "parent: ssh returned: $exit_value\n";
269 $parentdone = 1;
270 #last;
271 }
272 if ($finish) {
273 timeout($pid, $command);
274 last;
275 }
276
277 if ($parentdone == 1 && $alive == 0) {
278 last;
279 }
280 sleep (1);
281 };
282 }; #eval
283 alarm 0;
284 #9 if ($@) {
285 #9 print "what is this =====>\n";
286 #9 warn unless $@ eq "alarm\n";
287 #9 } else {
288 #9 print "what is this===>not timeout. \n";
289 #9 }
290 } elsif ( defined $pid) {
291 # child task here : it kicks off the task
292 #
293 # Here we send remote output to rbatch_stderr
294 #
295
296 `ssh -n -f $target_machine $target_directory/$batch_name 1> $local_stderr 2> $local_stdout` ;
297
298 exit(0) ;
299 # }
300 # elsif ( $! == EAGAIN ) {
# # recoverable fork error : sleep and try again
302 # sleep 5 ;
303 # redo FORK ;
304 } else {
305 $task_status = 1 ;
306 print "Unable to fork task on $target_machine : $!\n" ;
307 } ;
308 } ;
309 ################################################
310
311 if ( $task_status == 0 ) {
312 # did it start correctly ? sleep until the remote batch has had its
313 # chance to start running
314 sleep $parent_wait; ##20 ;
315 #6 open ( RESULTS, "rbatch_stderr" ) ;
316 #6 $task_pid =
317 #6 $task_pid =~ s/[\n\r]// ;
318 #6 $task_pid =~ s/pid of launched task is // ;
319 #6 $task_status =
320 #6 $task_status =~ s/[\n\r]// ;
321
322 # print "task status : *$task_status*\n" ;
323 #6 if ( $task_status ne "pid status is 0" ) {
324 #2 print "\n" ;
325 # 2 print "command on $target_machine :\n" ;
326 # 2 print "$command\n" ;
327 # 2 while (
328 #6 } ;
329 #6 close RESULTS ;
330
331 #6 print_file("rbatch_stderr");
332 #6 $task_status =~ s/pid status is // ;
333
334 # remove the remote batch file
335 `ssh $target_machine /bin/rm -f $target_directory/$batch_name ` ;
336 } ; #parent ok
337 ################################################
338 } ; #scp ok
339
340 if ($task_status != 0) {
341 print "Error. task start failed. $task_status: *task_status*\n";
342 }
344 # print "start_task done........\n";
345 return ( $task_pid, $task_status, $task_stderr, $task_stdout ) ;
346 }
347
348
349
350 sub print_file {
351 my ($filename) = @_;
352 open(FH, "<$filename") || die "Can not open $filename: $!\n";
353
354 while ( my $line =
355 chomp ($line);
356 print "==> $line\n";
357 }
358 return;
359 }
360
361
362
363 sub timeout {
364 my ($pid, $command) = @_;
365 print "Timeout $pid. Cmd: $command\n";
366 local $SIG{INT} = 'IGNORE';
367 local $SIG{TERM} = 'IGNORE';
368 ########kill local
369 #kill 'INT' => -$$; ##$pid; C-c that ssh to remote box
370
371 #kill 'KILL' => $pid;
372 #die 'alarm';
373 #$task_status = 2; #0: ok; 1: failed; 2 killed?
374 ########kill remote background process
375 #kill_remote_pid($pid, $remote_machine, $msg);
376 }
377
378
379
380
381
382 sub print_banner_begin {
383 my $datetime = localtime();
384
385 print "==============test begin============\n";
386 print " $datetime\n";
print "====================================\n";
388
389
390 }
391
392
393 sub print_banner_end {
394 my $datetime = localtime();
395
396 print "==============test end============\n";
397 print " $datetime\n";
398 print "====================================\n";
399
400
401 }
~
~
No comments:
Post a Comment