-
Notifications
You must be signed in to change notification settings - Fork 6
/
measureproc
executable file
·1502 lines (1362 loc) · 53.1 KB
/
measureproc
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
: # use perl -*-Perl-*-
eval 'exec perl -S "$0" ${1+"$@"}'
if 0;
# -*cperl-*-
use strict;
use Getopt::Long;
use File::Basename;
# File::Temp is a library for creating temporary files.
use File::Temp qw/ tempfile tempdir /;
use File::Copy;
# FindBin is a library to find the directory in which the script is
# installed. This can be useful for making it easy to install other
# auxiliary files needed by the script all in one place, and no matter
# where someone installs 'the whole package' of files, the script can
# find them, without needing hand-editing.
use FindBin;
use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
use Math::BigInt;
use XML::LibXML;
use Cwd;
my $verbose = 0;
my $full_progname = $0;
my $progname = fileparse($full_progname);
my $save_dir_name;
my $os = `uname -o 2>/dev/null`;
if ($? != 0) {
# Then likely we are running on a Mac OS X system with the default
# uname installation, which accepts -s but not -o option.
$os = `uname -s 2>/dev/null`;
}
chomp $os;
my $os_full = `uname -a`;
chomp $os_full;
my $cwd = cwd();
my $install_dir = $FindBin::Bin;
my $timemem_cmd = time_cmd_location($os, $install_dir);
my $sumgclog_cmd = sumgclog_location($os, $install_dir);
sub usage {
print STDERR
"usage: $progname [ --help ] [ --verbose ]
[ --xml ]
[ --log-file <log_file> ]
[ --input-file <input_file> ]
[ --output-file <output_file> ]
[ --check-output-cmd <check_output_cmd> ]
[ --os <os_name> ]
[ --time-cmd <time_cmd> ]
[ --language <language_implementation_description_string> ]
[ --benchmark <benchmark_problem_name> ]
[ --source-file-name <source_file_name> ]
[ --jvm-info { client | server } ]
[ --jvm-cmd <jvm_command> ]
[ --jvm-gc-stats <jvm_type> ]
[ --jruby-gc-stats <jvm_type> ]
[ --save-temp-files ]
[ --never-save-temp-files ]
<measured_cmd> [ arg1 ... ]
--help Show this help.
--verbose Enable debug messages.
--xml Print measurement output in XML format.
--log-file <log_file>
Append measurement output to file <log_file>,
creating <log_file> if it does not exist already.
If --xml is specified, the XML structure of the
existing measurements in that file is preserved, and
the new measurement is appended to the end. If
--xml is not specified, the results are simply
appended to the end of the file.
--input-file <input_file>
Input file for <measured_cmd> (optional). If
provided, <measured_cmd> will be invoked with its
standard input redirected from <input_file>.
--output-file <output_file>
Similar to --input-file, except it is a file name
where <measure_cmd>'s standard output will be
redirected into, if provided.
--check-output-cmd <check_output_cmd>
A command string used to check whether the output
file's contents are good. The command's exit status
should be 0 (i.e. no error) if the output is good,
or any non-zero value if the output is not good.
This command string may have occurences of %o in it.
If so, $progname will replace all such occurrences
with the output file name. This option should only
be given if --output-file is also given.
--os <os_name>
Can be used to specify one of the supported OSs:
Cygwin
Darwin
GNU/Linux
This command line option need only be used to
override the default of '$os', which is detected via
the output of the 'uname -o' command.
--time-cmd <time_cmd>
Specify the command to use to measure the running
time and memory usage of the process. This is only
needed if you wish to override the default command,
which for platform '$os' is: $timemem_cmd
--jvm-info { client | server }
--jvm-cmd <jvm_command>
When this option is used, $progname will run a small
compiled Java program JVMInfo.class that should be
installed in the same directory as $progname, before
running <measured_cmd>. This program will print
some information such as the host OS, JVM version,
and number of available processors, among other
things, and $progname will collect this and include
it in the output.
Likely most of this information will be the same
regardless of whether a -client or -server command
line option is used, but in case they are different,
you must specify which to use by the string that
appears after --jvm-info on the command line.
If the --jvm-cmd option is specified, $progname will
use that as the command to use when invoking the JVM
to execute JVMInfo.class. If no --jvm-cmd option is
specified, $progname wil use the first 'word' of
<measured_cmd> as the name of the command to invoke
the JVM.
(TBD: This should be tested with path names that
have spaces in them to see whether it works in that
case or not).
--jvm-gc-stats <jvm_type>
When this option is used, $progname will assume that
<measured_cmd> is an invocation of a Java virtual
machine that takes an additional command line option
in order to report information about garbage
collection (GC) invocation. What the option is
depends upon what kind of JVM is in use. The
following are supported by $progname:
--jvm-gc-stats hotspot
-Xloggc:<gc_log_file>
--jvm-gc-stats jrockit
-Xverbose:memory -Xverboselog:<gc_log_file>
$progname will modify the command line given to add
options like the ones above, with the name of an
auto-generated temporary file name, and then parse
the contents of this file when <measured_cmd> exits.
Some summary statistics about the contents will be
reported.
--jruby-gc-stats <jvm_type>
This is the same as --jvm-gc-stats, except that the
command line option added to the command line will
have a '-J' prepended to it, as required by the
jruby command in order to pass the options through
to the JVM invocation.
--save-temp-files
--never-save-temp-files
Without either of these options, $progname will
generate several temporary files that are usually
deleted upon completion.
If there are errors in parsing the GC log file, it
will be saved in a file that is not deleted. The
name of this file will be included in the output.
If the --check-output-cmd is given, and has a
non-zero exit status, indicating the output file is
not good, then a copy of the output file will be
made in a new temp directory, and not deleted, even
if many consecutive runs of measureproc are made
with the same output file name. The name of this
new file will be included in the output.
With the option --save-temp-files, these files will
be saved even if there are no errors / the output
file is checked as good.
With the option --never-save-temp-files, these files
will not be saved, even if there are parse errors,
or the output file is checked as bad.
The options below do not affect how the command is run or its
performance measured. They are simply printed to the output for
inclusion in whatever results the user wishes to collect. They
could be useful later for keeping track of which results are
which.
--language <language_implementation_description_string>
--benchmark <benchmark_problem_name>
--source-file-name <source_file_name>
Note that command line options can be abbreviated to the shortest
prefix that uniquely identifies them.
Example of use on Linux or Mac OS X:
% $progname --input input/medium-input.txt --output output/medium-clj-1.2-output.txt java -version
[ ... output removed ... ]
% $progname --input input/medium-input.txt --output output/medium-clj-1.2-output.txt java -server -Xmx1536m -classpath ~/lein/swank-clj-1.2.0/lib/clojure-1.2.0.jar:./obj/clj-1.2 knucleotide
[ ... output removed ... ]
% $progname --input input/quick-input.txt --output output/quick-clj-1.2-output.txt --check 'diff --strip-trailing-cr --brief output/quick-expected-output.txt %o' java -server -Xmx1536m -classpath ~/lein/swank-clj-1.2.0/lib/clojure-1.2.0.jar:./obj/clj-1.2 knucleotide
Command measured : java -server -Xmx1536m -classpath /Users/andy/lein/swank-clj-1.2.0/lib/clojure-1.2.0.jar:./obj/clj-1.2 knucleotide
Elapsed time (sec) : 2.857
User CPU time (sec) : 3.107
System CPU time (sec) : 0.302
Max resident set size (kb): 80992
Start time : Wed Nov 10 18:55:06 2010
End time : Wed Nov 10 18:55:08 2010
Per core CPU usage (2 cores): 92% 90%
Exit status : 0
Command to check output : diff --strip-trailing-cr --brief output/quick-expected-output.txt output/quick-clj-1.2-output.txt
Exit status of check cmd : 0
OS description : Darwin andys-mbp.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 i386 MacBookPro4,1 Darwin
Examples of use on Windows XP + Cygwin:
% $progname --input input\\\\medium-input.txt --output output\\\\medium-clj-1.2-output.txt \\\\Program\\ Files\\\\Java\\\\jrmc-4.0.1-1.6.0\\\\bin\\\\java -version
[ ... output removed ... ]
% $progname --verbose --input input\\\\quick-input.txt --output output\\\\quick-clj-1.2-output.txt \\\\Program\\ Files\\\\Java\\\\jrmc-4.0.1-1.6.0\\\\bin\\\\java -server -Xmx1536m -classpath \"\\\\cygwin\\\\home\\\\Admin\\\\lein\\\\swank-clj-1.2.0\\\\lib\\\\clojure-1.2.0.jar;.\\\\obj\\\\clj-1.2\" knucleotide
[ ... output removed ... ]
";
}
######################################################################
# Get command line options
######################################################################
my $help;
my $output_format_xml;
my $log_file;
my $input_file;
my $output_file;
my $check_output_cmd;
my $os_from_cmd_line;
my $time_cmd_from_cmd_line;
my $jvm_info;
my $jvm_cmd;
my $jvm_gc_stats_from_cmd_line;
my $jruby_gc_stats_from_cmd_line;
my $save_temp_files;
my $never_save_temp_files;
my $language_implementation_desc_str = '';
my $benchmark_name = '';
my $source_file_name = '';
Getopt::Long::Configure('require_order');
if (!GetOptions('help' => \$help,
'verbose' => \$verbose,
'xml' => \$output_format_xml,
'log-file=s' => \$log_file,
'input-file=s' => \$input_file,
'output-file=s' => \$output_file,
'check-output-cmd=s' => \$check_output_cmd,
'os=s' => \$os_from_cmd_line,
'time-cmd=s' => \$time_cmd_from_cmd_line,
'jvm-info=s' => \$jvm_info,
'jvm-cmd=s' => \$jvm_cmd,
'jvm-gc-stats=s' => \$jvm_gc_stats_from_cmd_line,
'jruby-gc-stats=s' => \$jruby_gc_stats_from_cmd_line,
'save-temp-files' => \$save_temp_files,
'never-save-temp-files' => \$never_save_temp_files,
'language=s' => \$language_implementation_desc_str,
'benchmark=s' => \$benchmark_name,
'source-file-name=s' => \$source_file_name
))
{
usage();
exit(1);
}
$os = $os_from_cmd_line if (defined($os_from_cmd_line));
if ($verbose) {
printf STDERR "\$os='%s'\n", $os;
printf STDERR "install dir='%s'\n", $FindBin::Bin;
}
if ($help) {
usage();
exit(0);
}
if (defined($save_temp_files) && defined($never_save_temp_files)) {
printf STDERR
"At most one of --save-temp-files and --never-save-temp-files should
be specified.
",;
exit 1;
}
if (defined($jvm_info)) {
if (! (($jvm_info eq 'client') || ($jvm_info eq 'server')) ) {
printf STDERR
"The value after --jvm-info must be 'client' or 'server'. Found '%s'.
", $jvm_info;
exit 1;
}
}
my $gc_stats_type;
my $gc_stats_jruby;
if (defined($jvm_gc_stats_from_cmd_line) &&
defined($jruby_gc_stats_from_cmd_line))
{
printf STDERR
"At most one of --jvm-gc-stats and --jruby-gc-stats should be
specified, depending on whether the measured command is a jruby
invocation or a java invocation.
";
exit 1;
}
if (defined($jruby_gc_stats_from_cmd_line)) {
$gc_stats_type = $jruby_gc_stats_from_cmd_line;
$gc_stats_jruby = 1;
} elsif (defined($jvm_gc_stats_from_cmd_line)) {
$gc_stats_type = $jvm_gc_stats_from_cmd_line;
$gc_stats_jruby = 0;
}
if (defined($gc_stats_type)) {
if ($gc_stats_type eq 'hotspot') {
# OK
} elsif ($gc_stats_type eq 'jrockit') {
# OK
} else {
printf STDERR
"Unrecognized value '%s' given after option %s
The following are supported: hotspot jrockit
",
$gc_stats_type,
($gc_stats_jruby ? '--jruby-gc-stats' : '--jvm-gc-stats');
exit 1;
}
}
$timemem_cmd = (defined($time_cmd_from_cmd_line) ? $time_cmd_from_cmd_line
: time_cmd_location($os, $install_dir));
if ($timemem_cmd =~ /^(\S+)/) {
my $cmd = $1;
if (! -x $cmd) {
die sprintf "There is no command '$cmd' to measure performance for your command\n";
}
}
if ($#ARGV < 0) {
printf STDERR "No command given.\n";
usage();
exit 1;
}
my $tempdir;
if (($os eq 'Cygwin') || defined($gc_stats_type)) {
# We will later use a temporary directory for creating one or more
# temporary files.
$tempdir = tempdir( CLEANUP => 1 );
}
######################################################################
# Construct command to run (and measure its performance)
######################################################################
my $gc_log_filename;
my $cmd_first_word = shift @ARGV;
if ($os eq 'Cygwin') {
# timemem.exe expects a DOS/Windows file name for the command, but
# when writing shell scripts that we want to work on both
# Windows+Cygwin and Linux/Mac OS X, it is sometimes inconvenient
# to make a separate case for such things.
# Let cygpath -w convert it to DOS/Windows if it is not. If it
# already is, cygpath -w seems to recognize this and leave it
# alone.
$cmd_first_word = `cygpath -w "$cmd_first_word"`;
chomp $cmd_first_word;
}
my $cmd_to_time = $cmd_first_word;
if (defined($gc_stats_type)) {
my $tempfh;
($tempfh, $gc_log_filename) = tempfile( DIR => $tempdir,
SUFFIX => '.txt' );
my $log_fname = $gc_log_filename;
if ($os eq 'Cygwin') {
$log_fname = `cygpath -w $gc_log_filename`;
chomp $log_fname;
}
my $prefix = ($gc_stats_jruby ? '-J' : '');
if ($gc_stats_type eq 'hotspot') {
$cmd_to_time .= (sprintf ' %s-Xloggc:%s', $prefix, $log_fname);
} elsif ($gc_stats_type eq 'jrockit') {
$cmd_to_time .= (sprintf ' %s-Xverbose:memory %s-Xverboselog:%s',
$prefix, $prefix, $log_fname);
}
}
$cmd_to_time .= (' ' . join(' ', @ARGV));
if ($verbose) {
printf STDERR "\$cmd_to_time='%s'\n", $cmd_to_time;
}
my $cmd_str = '';
my $cmd_begin = '';
my $cmd_end = '';
if ($os eq 'Cygwin') {
$cmd_begin = '"';
$cmd_end = '"';
}
$cmd_str .= sprintf "%s %s%s%s", $timemem_cmd, $cmd_begin, $cmd_to_time, $cmd_end;
if (defined($input_file)) {
$cmd_str .= sprintf " < %s", $input_file;
}
if (defined($output_file)) {
$cmd_str .= sprintf " > %s", $output_file;
}
if ($verbose) {
printf STDERR "\$cmd_str='%s'\n", $cmd_str;
}
my $cmd_to_run;
if ($os eq 'Cygwin') {
# On Cygwin, put the command into a BAT file for execution.
my ($fh, $batchfile) = tempfile( DIR => $tempdir, SUFFIX => '.bat' );
if ($verbose) {
printf STDERR "\$tempdir='%s'\n", $tempdir;
}
printf $fh "%s\n", $cmd_str;
close $fh;
chmod 0755, $batchfile;
$cmd_to_run = $batchfile;
if ($save_temp_files) {
my ($copy_succeeded, $copy_info, $batch_copy_filename);
($save_dir_name, $copy_succeeded, $copy_info, $batch_copy_filename) =
save_copy_of_file($save_dir_name, $batchfile,
'batch file', 'bat-file');
printf STDERR "%s", $copy_info;
}
} else {
$cmd_to_run = $cmd_str;
}
if ($verbose) {
printf STDERR "\$cmd_to_run='%s'\n", $cmd_to_run;
}
######################################################################
# If requested, run the JVM to get some information about it and the
# host machine and OS.
######################################################################
my $jvm_property_names = [
'os.arch',
'availableProcessors',
'os.name',
'os.version',
'java.version',
'java.specification.version',
'java.vm.name',
'java.vm.vendor',
'java.vm.version',
'java.runtime.version',
'sun.arch.data.model' ];
my $jvm_property_xml_tags = {
'os.arch' => 'jvm_os_arch',
'availableProcessors' => 'jvm_availableProcessors',
'os.name' => 'jvm_os_name',
'os.version' => 'jvm_os_version',
'java.version' => 'jvm_java_version',
'java.specification.version' => 'jvm_java_specification_version',
'java.vm.name' => 'jvm_java_vm_name',
'java.vm.vendor' => 'jvm_java_vm_vendor',
'java.vm.version' => 'jvm_java_vm_version',
'java.runtime.version' => 'jvm_java_runtime_version',
'sun.arch.data.model' => 'jvm_sun_arch_data_model'
};
my $jvm_info_hash;
if (defined($jvm_info)) {
if (!defined($jvm_cmd)) {
$jvm_cmd = $cmd_first_word;
}
my $jvm_cmd_to_use = $jvm_cmd;
if ($os eq 'Cygwin') {
$jvm_cmd_to_use = `cygpath -u "$jvm_cmd"`;
chomp $jvm_cmd_to_use;
}
my $jvm_cmd_list = [ $jvm_cmd_to_use ];
my $install_dir_to_use = $install_dir;
if ($os eq 'Cygwin') {
$install_dir_to_use = `cygpath -w "$install_dir"`;
chomp $install_dir_to_use;
}
push @{$jvm_cmd_list}, ('-' . $jvm_info),
'-classpath', $install_dir_to_use, 'JVMInfo';
my $jvm_cmd_string = join(' ', @{$jvm_cmd_list});
open (JVM_OUT, '-|', @{$jvm_cmd_list})
or die sprintf "Could not execute command '%s' and read its output.\n",
$jvm_cmd_string;
my $line;
$jvm_info_hash->{'jvm_info_full_cmd'} = $jvm_cmd_string;
while ($line = <JVM_OUT>) {
$line =~ s/\r$//;
if ($line =~ /^([^=]+)=(.*)$/) {
my ($property, $val) = ($1, $2);
if (defined($jvm_property_xml_tags->{$property})) {
$jvm_info_hash->{$property} = $val;
} else {
die sprintf
"Unrecognized property name '%s' in output line from command '%s':\n%s",
$property, $jvm_cmd_string, $line;
}
} else {
die sprintf "Unrecognized output line from command '%s':\n%s",
$jvm_cmd_string, $line;
}
}
close(JVM_OUT);
}
######################################################################
# Run the command, and read performance stats from its standard error
# output.
######################################################################
my ($elapsed_sec, $user_sec, $sys_sec, $max_rss_kbytes);
my $num_cpus;
my $usage_per_cpu = 'not calculated';
my ($per_cpu_stats_start, $total_cpu_stats_start);
my ($per_cpu_stats_end, $total_cpu_stats_end);
# I want to run the program and read its stderr output. I'll assume
# that stdout is already being saved to a file, and ignore anything
# that goes to stdout.
# This is a slight modification of some sample code I found at:
# http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command?
local *CATCHERR = IO::File->new_tmpfile;
if (($os eq 'GNU/Linux') || ($os eq 'Cygwin')) {
($per_cpu_stats_start, $total_cpu_stats_start) = linux_get_cpu_usage();
}
my $start_time = localtime();
my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", $cmd_to_run);
while (<CATCHOUT>) {
}
waitpid($pid, 0);
my $child_exit_status = $? >> 8;
my $end_time = localtime();
if (($os eq 'GNU/Linux') || ($os eq 'Cygwin')) {
($per_cpu_stats_end, $total_cpu_stats_end) = linux_get_cpu_usage();
}
seek CATCHERR, 0, 0;
my $cmd_exit_status;
my $unrecognized_cmd_stderr_output = '';
if ($os eq 'Cygwin') {
my $found_pid = 0;
while (<CATCHERR>) {
chomp;
s/\r$//;
if (/^Process ID/) {
$found_pid = 1;
} elsif (/^\s*Dumping CPU usage by sampling running threads \.\.\. done\.\s*$/) {
# Ignore this line if JVM profiling via sampling was
# enabled.
} elsif ($found_pid) {
if (/^\s+exit code: (.*)\s*$/) {
$cmd_exit_status = $1;
} elsif (/^\s+elapsed time \(seconds\): (.*)\s*$/) {
$elapsed_sec = $1;
} elsif (/^\s+user time \(seconds\): (.*)\s*$/) {
$user_sec = $1;
} elsif (/^\s+kernel time \(seconds\): (.*)\s*$/) {
$sys_sec = $1;
} elsif (/^\s+Peak Working Set Size \(kbytes\): (\d+)\s*$/) {
$max_rss_kbytes = $1;
} elsif (/^\s+Page Fault Count: (\d+)\s*$/) {
# Ignore this and the ones immediately below
} elsif (/^\s+Quota Peak Paged Pool Usage: (\d+)\s*$/) {
} elsif (/^\s+Quota Peak Non Paged Pool Usage: (\d+)\s*$/) {
} elsif (/^\s+Peak Pagefile Usage: (\d+)\s*$/) {
} else {
if (! /^\s*$/) {
$unrecognized_cmd_stderr_output .= ($_ . "\n");
}
}
} else {
# This is a line of output from the program, most likely
# one printed to stderr if stdout has been redirected.
if (! /^\s*$/) {
$unrecognized_cmd_stderr_output .= ($_ . "\n");
}
}
}
} elsif ($os eq 'Darwin') {
$cmd_exit_status = $child_exit_status;
my $found_real = 0;
if ($verbose) {
printf STDERR "Reading stderr output of command: %s\n", $cmd_to_run;
}
while (<CATCHERR>) {
chomp;
if ($verbose) {
printf STDERR "stderr line: %s\n", $_;
}
if (/^\s*real\s+(.*)\s*$/) {
$elapsed_sec = $1;
$found_real = 1;
} elsif (/^\s*Dumping CPU usage by sampling running threads \.\.\. done\.\s*$/) {
# Ignore this line if JVM profiling via sampling was
# enabled.
} elsif (/^\s*Shared archive: sharing disabled for server vm\s*$/) {
# This line is often printed on my PowerPC-based Mac
# by the JVM.
} elsif ($found_real) {
if (/^\s*user\s+(.*)\s*$/) {
$user_sec = $1;
} elsif (/^\s*sys\s+(.*)\s*$/) {
$sys_sec = $1;
} elsif (/^\s*(\d+)\s+maximum resident set size from (getrusage|polling).*\s*$/)
{
# Mac OS X 10.5.* reports max RSS from getrusage in
# units of bytes. Convert to kbytes. Use BigInt
# arithmetic, in case the integer is very large.
# Mac OS X 10.6.* always reports 0 for max RSS from
# getrusage, so the measurement obtained from periodic
# polling of the current resident set size should be
# used.
my $max_rss_kbytes_bi = Math::BigInt->new($1);
my $measurement_type = $2;
# Add 1023 before dividing (with truncation) by 1024,
# so that the net effect is to round the number of
# bytes up to the next whole number of kbytes.
$max_rss_kbytes_bi->badd(1023);
$max_rss_kbytes_bi->bdiv(1024);
$max_rss_kbytes = sprintf "%s", $max_rss_kbytes_bi;
} elsif (/^\s*Per core CPU utilization \((\d+) cores\): (.*)$/) {
($num_cpus, $usage_per_cpu) = ($1, $2);
} elsif (
/^\s*number of times rss polled=\d+, avg of \d+(\.\d+)? times per second\s*$/ ||
/^\s*time between consecutive polls \(msec\): min=\d+(\.\d+)? max=\d+(\.\d+)?\s*$/ ||
/^\s*Max RSS observed \d+(\.\d+)? sec after start time\s*$/ ||
/^\s*A call to task_info\(\) returned an error\. error_time - end_time = -?\d+(\.\d+)? sec\. This may mean the maximum resident set size measurement above is too low\.\s*$/)
{
# Ignore these lines, but don't treat them as
# unrecognized lines.
} else {
if (! /^\s*$/) {
$unrecognized_cmd_stderr_output .= ($_ . "\n");
}
}
} else {
# This is a line of output from the program, most likely
# one printed to stderr if stdout has been redirected.
if (! /^\s*$/) {
$unrecognized_cmd_stderr_output .= ($_ . "\n");
}
}
}
} elsif ($os eq 'GNU/Linux') {
$cmd_exit_status = $child_exit_status;
my $found_user = 0;
while (<CATCHERR>) {
chomp;
if (/^\s*User time \(seconds\):\s*(.*)\s*$/) {
$user_sec = $1;
$found_user = 1;
} elsif (/^\s*Command being timed:.*$/ ||
/^\s*Percent of CPU this job got: \d+%\s*$/ ||
/^\s*Average shared text size \(kbytes\): \d+\s*$/ ||
/^\s*Average unshared data size \(kbytes\): \d+\s*$/ ||
/^\s*Average stack size \(kbytes\): \d+\s*$/ ||
/^\s*Average total size \(kbytes\): \d+\s*$/ ||
/^\s*Average resident set size \(kbytes\): \d+\s*$/ ||
/^\s*Major \(requiring I\/O\) page faults: \d+\s*$/ ||
/^\s*Minor \(reclaiming a frame\) page faults: \d+\s*$/ ||
/^\s*Voluntary context switches: \d+\s*$/ ||
/^\s*Involuntary context switches: \d+\s*$/ ||
/^\s*Swaps: \d+\s*$/ ||
/^\s*File system inputs: \d+\s*$/ ||
/^\s*File system outputs: \d+\s*$/ ||
/^\s*Socket messages sent: \d+\s*$/ ||
/^\s*Socket messages received: \d+\s*$/ ||
/^\s*Signals delivered: \d+\s*$/ ||
/^\s*Page size \(bytes\): \d+\s*$/ ||
/^\s*Exit status: \d+\s*$/
)
{
# Ignore these lines, but don't treat them as unrecognized
# lines.
} elsif (/^\s*Dumping CPU usage by sampling running threads \.\.\. done\.\s*$/) {
# Ignore this line if JVM profiling via sampling was
# enabled.
} elsif ($found_user) {
if (/^\s*System time \(seconds\):\s*(.*)\s*$/) {
$sys_sec = $1;
} elsif (/^\s*Elapsed \(wall clock\) time \(h:mm:ss or m:ss\):\s*(.*)\s*$/) {
my $tmp = $1;
if ($tmp =~ /^(\d+):(\d+):(\d+(\.\d+)?)$/) {
my ($h, $m, $s) = ($1, $2, $3);
$elapsed_sec = $h * 3600 + $m * 60 + $s;
} elsif ($tmp =~ /^(\d+):(\d+(\.\d+)?)$/) {
my ($m, $s) = ($1, $2);
$elapsed_sec = $m * 60 + $s;
} else {
die sprintf "Unrecognized Elapsed (wall clock) time format seen. Aborting:\n%s", $tmp;
}
} elsif (/^\s*Maximum resident set size \(kbytes\):\s*(\d+)\s*$/) {
# There is a bug in GNU time version 1.7 that causes
# the maximum resident set size reported to be 4 times
# too large, at least when running on Linux. See:
#
# http://www.mail-archive.com/help-gnu-utils@gnu.org/msg01371.html
#
# You can find out which version of GNU time you have
# using this command:
#
# /usr/bin/time -V
# Use BigInt arithmetic, in case the integer is very
# large.
my $max_rss_kbytes_bi = Math::BigInt->new($1);
$max_rss_kbytes_bi->bdiv(4);
$max_rss_kbytes = sprintf "%s", $max_rss_kbytes_bi;
} else {
if (! /^\s*$/) {
$unrecognized_cmd_stderr_output .= ($_ . "\n");
}
}
} else {
# This is a line of output from the program, most likely
# one printed to stderr if stdout has been redirected.
if (! /^\s*$/) {
$unrecognized_cmd_stderr_output .= ($_ . "\n");
}
}
}
}
close(F);
if (($os eq 'GNU/Linux') || ($os eq 'Cygwin')) {
$num_cpus = $#{$per_cpu_stats_start} + 1;
if ($verbose) {
printf STDERR "\$num_cpus=%s\n", $num_cpus;
}
$usage_per_cpu = '';
my $i;
for ($i = 0; $i < $num_cpus; $i++) {
my $cpu_busy_percent =
cpu_busy_percent($per_cpu_stats_start->[$i]{idle},
$per_cpu_stats_end->[$i]{idle},
$per_cpu_stats_start->[$i]{total},
$per_cpu_stats_end->[$i]{total});
my $cpu_busy_percent_without_bigints =
cpu_busy_percent_without_bigints($per_cpu_stats_start->[$i]{idle},
$per_cpu_stats_end->[$i]{idle},
$per_cpu_stats_start->[$i]{total},
$per_cpu_stats_end->[$i]{total});
if ($cpu_busy_percent ne $cpu_busy_percent_without_bigints) {
die sprintf "For idle times %s %s and total times %s %s cpu_busy_percent() returned %s but cpu_busy_percent_without_bigints() returned %s\n",
$per_cpu_stats_start->[$i]{idle},
$per_cpu_stats_end->[$i]{idle},
$per_cpu_stats_start->[$i]{total},
$per_cpu_stats_end->[$i]{total},
$cpu_busy_percent,
$cpu_busy_percent_without_bigints;
}
$usage_per_cpu .= sprintf " %d%%", $cpu_busy_percent;
}
# Remove leading space.
$usage_per_cpu = substr($usage_per_cpu, 1);
}
if ($unrecognized_cmd_stderr_output ne '') {
printf STDERR "
The following lines were printed to stderr by the program while
being measured, and were not recognized as part of the output of the
program that is used for measurement:
%s
%s
",
$timemem_cmd, $unrecognized_cmd_stderr_output;
}
if ($verbose) {
printf STDERR "\$elapsed_sec='%s'\n", $elapsed_sec;
printf STDERR "\$user_sec='%s'\n", $user_sec;
printf STDERR "\$sys_sec='%s'\n", $sys_sec;
printf STDERR "\$max_rss_kbytes='%s'\n", $max_rss_kbytes;
}
if (!(defined($elapsed_sec) && defined($user_sec) &&
defined($sys_sec) && defined($max_rss_kbytes) &&
defined($num_cpus) && defined($usage_per_cpu) &&
defined($cmd_exit_status)))
{
printf STDERR "
One or more of the following values were not found in the output:
elapsed, user, or kernel/system times
maximum resident/working set size
number of CPU cores, or their utilization
command exit status
This is likely due to a change in the output format of the command
$timemem_cmd, and requires a corresponding change to $progname
The command line used was:
$cmd_to_run
";
if ($os eq 'Cygwin') {
printf STDERR "
Contents of batch file %s:
----------------------------------------
", $cmd_to_run;
open(F,"$cmd_to_run") or die sprintf "Could not open file '%s' for reading.\n", $cmd_to_run;
while (<F>) {
print STDERR $_;
}
close(F);
printf STDERR
"----------------------------------------\n";
}
exit 1;
}
######################################################################
# If a check output command was given, run it to see if the output
# file's contents are good.
######################################################################
my $check_output_cmd_exit_status;
my $check_output_cmd_fail_info = '';
if (defined($output_file) && defined($check_output_cmd)) {
$check_output_cmd =~ s/%o/$output_file/g;
# TBD: Don't let anything sent to stdout of $check_output_cmd get
# to our stdout, because it could mess up our XML file output.
system($check_output_cmd);
$check_output_cmd_exit_status = $?;
if ($check_output_cmd_exit_status == -1) {
printf STDERR "failed to execute cmd '%s': %s\n", $check_output_cmd, $!;
exit 1;
} elsif (($check_output_cmd_exit_status & 127) != 0) {
$check_output_cmd_fail_info =
sprintf "check output cmd '%s' died with signal %d, %s coredump",
$check_output_cmd,
($check_output_cmd_exit_status & 127),
($check_output_cmd_exit_status & 128) ? 'with' : 'without';
} else {
my $child_exit_status = $check_output_cmd_exit_status >> 8;
if ($child_exit_status != 0) {
$check_output_cmd_fail_info =
sprintf "check output cmd '%s' exited with error status %d",
$check_output_cmd, $child_exit_status;
}
}
}
my $output_copy_filename;
my $save_output_copy = 0;
if ($save_temp_files) {
$save_output_copy = 1;
} elsif ($never_save_temp_files) {
$save_output_copy = 0;
} elsif ($check_output_cmd_fail_info ne '') {
$save_output_copy = 1;
}
if ($save_output_copy) {
my ($copy_succeeded, $copy_info);
($save_dir_name, $copy_succeeded, $copy_info, $output_copy_filename) =
save_copy_of_file($save_dir_name, $output_file,
'output', 'cmd-output');
$check_output_cmd_fail_info .= ("\n" . $copy_info);
printf STDERR "%s\n", $check_output_cmd_fail_info;
}
# Note: If $check_output_cmd_exit_status is 0, then everything is fine.
# If it is not 0, then either it exited with a non-0 exit status, or
# it was stopped without exiting normally, e.g. via a signal. No
# matter what non-0 value it is, we should conclude that we have not
# verified that the output is good.
######################################################################
# If --jvm-gc-stats option was given, read the GC log file and
# calculate summary GC performance stats.
######################################################################
my $gc_stats;
my $gc_log_copy_filename;
if (defined($gc_stats_type)) {
$gc_stats = parse_jvm_gc_log($gc_stats_type, $gc_log_filename);
my $save_gc_log_copy = 0;
if ($save_temp_files) {
$save_gc_log_copy = 1;
} elsif ($never_save_temp_files) {
$save_gc_log_copy = 0;
} elsif ($gc_stats->findvalue('num_gc_log_parse_errors') != 0) {
$save_gc_log_copy = 1;
}
if ($save_gc_log_copy) {
my ($copy_succeeded, $copy_info);
($save_dir_name, $copy_succeeded, $copy_info, $gc_log_copy_filename) =
save_copy_of_file($save_dir_name, $gc_log_filename,
'GC log', 'gc-log');
printf STDERR "%s", $copy_info;
}
}
######################################################################
# Print the performance stats in the desired format.
######################################################################
# TBD: (First part DONE) Make changes so that if a run has bad output,
# the bad output file is copied to a unique temporary file name that
# is reported on stderr and in the XML/stdout, so that the user
# can track down what the bad output was if they wish. This temporary
# file should be created in the current working directory, or in a new
# temporary directory created that is *not* automatically erased when
# measureproc is done. (Not done yet) Make an option to disable this
# copying action.
# TBD: (First part DONE) Similarly, if a GC log file cannot be parsed,
# copy it to a temporary file that will not be erased when measureproc
# is done, and report the name of the copy on stderr and in the
# XML/stdout output. (Not done yet) Make an option that disables
# this copying action.
# TBD: percent of cpu this job got while it ran
# TBD: Is there a way to find out how much total CPU time *all other
# processes* got while this process was running? It would be nice to
# have some kind of automated verification that the overall system
# load due to other processes was very low while running benchmark
# programs. For an OS running in a virtual machine like VMware
# Fusion, I don't think the "guest OS" can determine how busy the CPU
# is in the "host OS", but it should at least be able to find out
# whether other processes in the "guest OS" are using much CPU time.
# TBD:
# description string for result summary:
# normal completion, output correct
# normal completion, output incorrect
# completed with exception <name>
# unknown type of completion, output incorrect
# killed after taking too long
# TBD: Details about the virtual machine being used, e.g. VMware
# Fusion or Parallels Desktop on a Mac, or other similar things. This
# can make a significant difference for performance, and should be
# recorded. This will probably require an explicit command line
# argument from the user to specify it, unless there is some kind of
# relatively simple method I can use to get the info automatically.
# TBD: Description of hardware, e.g. # of CPU cores, type, and clock
# speed of each.
# For Ubuntu Linux, some info is available in /proc/cpuinfo
# Mac OS X command to get CPU details:
# /usr/sbin/system_profiler -detailLevel full SPHardwareDataType
# Command to get OS details in a format more like in "About This Mac"
# menu item: sw_vers
# Other similar commands mentioned at:
# http://serverfault.com/questions/14981/getting-cpu-information-from-command-line-in-mac-os-x-server
my $gc_xml_keys_format_strings = [
{ key => 'number_of_gcs',
fmt_string => " Number of GCs : %s\n"
},
{ key => 'total_memory_gced_kb',
fmt_string => " Total memory GCed (KB) : %s\n"
},
{ key => 'total_gc_time_sec',
fmt_string => " Total GC time (sec) : %s\n"
},
{ key => 'maximum_total_available_memory_kb',
fmt_string => " Maximum total available memory (KB): %s\n"
},
{ key => 'maximum_total_allocated_memory_kb',
fmt_string => " Maximum total allocated memory (KB): %s\n"
},
{ key => 'maximum_total_live_memory_kb',
fmt_string => " Maximum total live memory (KB) : %s\n"
},
{ key => 'num_gc_log_parse_warnings',
fmt_string => " GC log parse warnings : %s\n"
},