Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / spec2k / run_all.sh
1 #!/bin/bash
2
3 # Copyright (c) 2012 The Native Client Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 set -o nounset
8 set -o errexit
9
10 # The script is located in "native_client/tests/spec2k"
11 # Set pwd to spec2k/
12 cd "$(dirname "$0")"
13 if [[ $(basename "$(pwd)") != "spec2k" ]] ; then
14   echo "ERROR: cannot find the spec2k/ directory"
15   exit -1
16 fi
17
18 # TODO(pdox): Remove this dependency.
19 source ../../pnacl/scripts/common-tools.sh
20 readonly NACL_ROOT="$(GetAbsolutePath "../../")"
21 SetScriptPath "$(pwd)/run_all.sh"
22 SetLogDirectory "${NACL_ROOT}/toolchain/test-log"
23 readonly TESTS_ARCHIVE=arm-spec.tar.gz
24
25 ######################################################################
26 # CONFIGURATION
27 ######################################################################
28 # TODO(robertm): make this configurable from the commandline
29
30 readonly LIST_INT_C="164.gzip 175.vpr 176.gcc 181.mcf 186.crafty 197.parser \
31 253.perlbmk 254.gap 255.vortex 256.bzip2 300.twolf"
32
33 readonly LIST_FP_C="177.mesa 179.art 183.equake 188.ammp"
34
35 readonly LIST_INT_CPP="252.eon"
36
37 SPEC2K_BENCHMARKS="${LIST_FP_C} ${LIST_INT_C} ${LIST_INT_CPP}"
38
39 # One of {./run.train.sh, ./run.ref.sh}
40 SPEC2K_SCRIPT="./run.train.sh"
41
42 # Uncomment this to disable verification of test results.
43 # Otherwise, verification time is part of overall benchmarking time
44 # (should probably separate that out).
45 # export VERIFY=no
46 export VERIFY=${VERIFY:-yes}
47 export MAKEOPTS=${MAKEOPTS:-}
48
49 # Helper script to process timing / other perf data.
50 # Export these paths to Makefile.common, which will be included by other
51 # Makefiles in random sub-directories (where pwd will be different).
52 export PERF_LOGGER="$(pwd)/emit_perf_log.sh"
53 export COMPILE_REPEATER="$(pwd)/compile_repeater.sh"
54 # Number of times to repeat a timed step.
55 export SPEC_RUN_REPETITIONS=${SPEC_RUN_REPETITIONS:-1}
56 export SPEC_COMPILE_REPETITIONS=${SPEC_COMPILE_REPETITIONS:-1}
57
58 export DASHDASH=""
59
60 ######################################################################
61 # Helper
62 ######################################################################
63
64 readonly SCONS_OUT="${NACL_ROOT}/scons-out"
65 readonly TC_ROOT="${NACL_ROOT}/toolchain"
66 readonly TC_BASE="${TC_ROOT}/${SCONS_BUILD_PLATFORM}_${BUILD_ARCH_SHORT}"
67
68 readonly ARM_TRUSTED_TC="${TC_BASE}/arm_trusted"
69 readonly QEMU_TOOL="${ARM_TRUSTED_TC}/run_under_qemu_arm"
70
71 readonly ARM_LLC_NEXE="${TC_BASE}/pnacl_translator/translator/arm/bin/pnacl-llc.nexe"
72
73 readonly NNACL_TC="${TC_BASE}/nacl_${BUILD_ARCH_SHORT}_glibc"
74 readonly RUNNABLE_LD_X8632="${NNACL_TC}/x86_64-nacl/lib32/runnable-ld.so"
75 readonly RUNNABLE_LD_X8664="${NNACL_TC}/x86_64-nacl/lib/runnable-ld.so"
76
77 echo_file_size() {
78   local file=$1
79   echo "Uncompressed size of ${file} is $(cat ${file} | wc -c)"
80   echo "Gzipped size of ${file} is $(gzip ${file} -c | wc -c)"
81 }
82
83 ######################################################################
84 # Various Setups
85 ######################################################################
86 #@ invocation
87 #@    run_all.sh <mode> <mode-arg>*
88
89 #@ ------------------------------------------------------------
90 #@ Available Setups:
91 #@ ------------------------------------------------------------
92
93 # Setups for building (but not running) Pexes only. Used for arm-hw translator
94 # testing where pexes are build on x86 and translated on arm
95 SetupPnaclPexeOpt() {
96   SUFFIX=opt.stripped.pexe
97 }
98
99 SetupPnaclPexe() {
100   SUFFIX=unopt.stripped.pexe
101 }
102
103 #@
104 #@ SetupGccX8632
105 #@   use system compiler for x86-32
106 SetupGccX8632() {
107   PREFIX=
108   SUFFIX=gcc.x8632
109 }
110
111 #@
112 #@ SetupGccX8632Opt
113 #@   use system compiler for x86-32 with optimization
114 SetupGccX8632Opt() {
115   PREFIX=
116   SUFFIX=gcc.opt.x8632
117 }
118
119 #@
120 #@ SetupGccX8664
121 #@   use system compiler for x86-64
122 SetupGccX8664() {
123   PREFIX=
124   SUFFIX=gcc.x8664
125 }
126
127 #@
128 #@ SetupGccX8664Opt
129 #@   use system compiler for x86-64 with optimization
130 SetupGccX8664Opt() {
131   PREFIX=
132   SUFFIX=gcc.opt.x8664
133 }
134
135 #@
136 #@ SetupEmcc
137 #@   use Emscripten emcc compiler for Asm.js JavaScript generation
138 SetupEmcc() {
139   PREFIX=../run_asmjs.sh
140   SUFFIX=emcc.html
141   VERIFY=no
142 }
143
144 #@
145 #@ SetupLlvmX8632
146 #@   use system compiler for x86-32
147 SetupLlvmX8632() {
148   PREFIX=
149   SUFFIX=llvm.x8632
150 }
151
152 #@
153 #@ SetupLlvmX8632Opt
154 #@   use system compiler for x86-32 with optimization
155 SetupLlvmX8632Opt() {
156   PREFIX=
157   SUFFIX=llvm.opt.x8632
158 }
159
160 #@
161 #@ SetupLlvmX8664
162 #@   use system compiler for x86-64
163 SetupLlvmX8664() {
164   PREFIX=
165   SUFFIX=llvm.x8664
166 }
167
168 #@
169 #@ SetupLlvmX8664Opt
170 #@   use system compiler for x86-64 with optimization
171 SetupLlvmX8664Opt() {
172   PREFIX=
173   SUFFIX=llvm.opt.x8664
174 }
175
176 #@
177 #@ SetupLlvmArm
178 #@   use system compiler for ARM
179 SetupLlvmArm() {
180   PREFIX=
181   SUFFIX=llvm.hw.arm
182 }
183
184 #@
185 #@ SetupLlvmArmOpt
186 #@   use system compiler for ARM with optimization
187 SetupLlvmArmOpt() {
188   PREFIX=
189   SUFFIX=llvm.opt.hw.arm
190 }
191
192 ######################################################################
193
194 SetupNaclX8632Common() {
195   SetupSelLdr x86-32
196 }
197
198 #@
199 #@ SetupNaclX8632
200 #@   use nacl-gcc compiler
201 SetupNaclX8632() {
202   SetupNaclX8632Common
203   SUFFIX=nacl.x8632
204 }
205
206 #@
207 #@ SetupNaclX8632Opt
208 #@   use nacl-gcc compiler with optimizations
209 SetupNaclX8632Opt() {
210   SetupNaclX8632Common
211   SUFFIX=nacl.opt.x8632
212 }
213
214 SetupNaclX8664Common() {
215   SetupSelLdr x86-64
216 }
217
218 #@
219 #@ SetupNaclX8664
220 #@   use nacl-gcc64 compiler
221 SetupNaclX8664() {
222   SetupNaclX8664Common
223   SUFFIX=nacl.x8664
224 }
225
226 #@
227 #@ SetupNaclX8664Opt
228 #@   use nacl-gcc64 compiler with optimizations
229 SetupNaclX8664Opt() {
230   SetupNaclX8664Common
231   SUFFIX=nacl.opt.x8664
232 }
233
234 SetupNaclDynX8632Common() {
235   SetupSelLdr x86-32 "" "-s" "${RUNNABLE_LD_X8632}"
236 }
237
238 #@
239 #@ SetupNaclDynX8632
240 #@   use nacl-gcc compiler with glibc toolchain and dynamic linking
241 SetupNaclDynX8632() {
242   SetupNaclDynX8632Common
243   SUFFIX=nacl.dyn.x8632
244 }
245
246 #@
247 #@ SetupNaclDynX8632Opt
248 #@   use nacl-gcc compiler with glibc toolchain and dynamic linking
249 SetupNaclDynX8632Opt() {
250   SetupNaclDynX8632Common
251   SUFFIX=nacl.dyn.opt.x8632
252 }
253
254 SetupNaclDynX8664Common() {
255   SetupSelLdr x86-64 "" "-s" "${RUNNABLE_LD_X8664}"
256 }
257
258 #@
259 #@ SetupNaclDynX8664
260 #@   use nacl64-gcc compiler with glibc toolchain and dynamic linking
261 SetupNaclDynX8664() {
262   SetupNaclDynX8664Common
263   SUFFIX=nacl.dyn.x8664
264 }
265
266 #@
267 #@ SetupNaclDynX8664Opt
268 #@   use nacl64-gcc compiler with glibc toolchain and dynamic linking
269 SetupNaclDynX8664Opt() {
270   SetupNaclDynX8664Common
271   SUFFIX=nacl.dyn.opt.x8664
272 }
273
274 ######################################################################
275
276 SetupPnaclX8664Common() {
277   SetupSelLdr x86-64
278 }
279
280 #@
281 #@ SetupPnaclX8664
282 #@    use pnacl x86-64 compiler (no lto)
283 SetupPnaclX8664() {
284   SetupPnaclX8664Common
285   SUFFIX=pnacl.x8664
286 }
287
288 #@
289 #@ SetupPnaclX8664Opt
290 #@    use pnacl x86-64 compiler (with lto)
291 SetupPnaclX8664Opt() {
292   SetupPnaclX8664Common
293   SUFFIX=pnacl.opt.x8664
294 }
295
296 #@
297 #@ SetupPnaclX8664ZBSOpt
298 #@    use pnacl x86-64 compiler (with lto)
299 #@    use x86-64 zero-based sandbox
300 SetupPnaclX8664ZBSOpt() {
301   SetupSelLdr x86-64 "" "-c"
302   # TODO(arbenson): Give this a different suffix to differentitate
303   # from the existing x86-64 build, and make the corresponding
304   # changes to the build process.
305   SUFFIX=pnacl.opt.x8664
306 }
307
308 #@
309 #@ SetupPnaclTranslatorX8664
310 #@    use pnacl x8664 translator (no lto)
311 SetupPnaclTranslatorX8664() {
312   SetupPnaclX8664Common
313   SUFFIX=pnacl_translator.x8664
314 }
315
316 #@
317 #@ SetupPnaclTranslatorX8664Opt
318 #@    use pnacl x8664 translator (with lto)
319 SetupPnaclTranslatorX8664Opt() {
320   SetupPnaclX8664Common
321   SUFFIX=pnacl_translator.opt.x8664
322 }
323
324 #@
325 #@ SetupPnaclTranslatorFastX8664Opt
326 #@    use pnacl x8664 translator fast mode (with lto)
327 SetupPnaclTranslatorFastX8664Opt() {
328   SetupPnaclX8664Common
329   SUFFIX=pnacl_translator_fast.opt.x8664
330 }
331
332
333 #@
334 #@ SetupPnaclTranslator1ThreadX8664Opt
335 #@    use pnacl x8664 translator (with lto). Compile w/ 1 thread.
336 SetupPnaclTranslator1ThreadX8664Opt() {
337   SetupPnaclX8664Common
338   SUFFIX=pnacl_translator_1thread.opt.x8664
339 }
340
341 #@
342 #@ SetupPnaclTranslatorFast1ThreadX8664Opt
343 #@    use pnacl x8664 translator fast mode (with lto). Compile w/ 1 thread.
344 SetupPnaclTranslatorFast1ThreadX8664Opt() {
345   SetupPnaclX8664Common
346   SUFFIX=pnacl_translator_fast_1thread.opt.x8664
347 }
348
349
350 SetupPnaclX8632Common() {
351   SetupSelLdr x86-32
352 }
353
354 #@
355 #@ SetupPnaclX8632
356 #@    use pnacl x86-32 compiler (no lto)
357 SetupPnaclX8632() {
358   SetupPnaclX8632Common
359   SUFFIX=pnacl.x8632
360 }
361
362 #@
363 #@ SetupPnaclX8632Opt
364 #@    use pnacl x86-32 compiler (with lto)
365 SetupPnaclX8632Opt() {
366   SetupPnaclX8632Common
367   SUFFIX=pnacl.opt.x8632
368 }
369
370
371 #@
372 #@ SetupPnaclTranslatorX8632
373 #@    use pnacl x8632 translator (no lto)
374 SetupPnaclTranslatorX8632() {
375   SetupPnaclX8632Common
376   SUFFIX=pnacl_translator.x8632
377 }
378
379 #@
380 #@ SetupPnaclTranslatorX8632Opt
381 #@    use pnacl x8632 translator (with lto)
382 SetupPnaclTranslatorX8632Opt() {
383   SetupPnaclX8632Common
384   SUFFIX=pnacl_translator.opt.x8632
385 }
386
387 #@
388 #@ SetupPnaclTranslatorFastX8632Opt
389 #@    use pnacl x8632 translator fast mode (with lto)
390 SetupPnaclTranslatorFastX8632Opt() {
391   SetupPnaclX8632Common
392   SUFFIX=pnacl_translator_fast.opt.x8632
393 }
394
395 #@
396 #@ SetupPnaclTranslator1ThreadX8632Opt
397 #@    use pnacl x8632 translator (with lto). Compile w/ 1 thread.
398 SetupPnaclTranslator1ThreadX8632Opt() {
399   SetupPnaclX8632Common
400   SUFFIX=pnacl_translator_1thread.opt.x8632
401 }
402
403 #@
404 #@ SetupPnaclTranslatorFast1ThreadX8632Opt
405 #@    use pnacl x8632 translator fast mode (with lto). Compile w/ 1 thread.
406 SetupPnaclTranslatorFast1ThreadX8632Opt() {
407   SetupPnaclX8632Common
408   SUFFIX=pnacl_translator_fast_1thread.opt.x8632
409 }
410
411
412 #@
413 #@ SetupGccArm
414 #@   use gcc cross compiler
415 SetupGccArm() {
416   PREFIX="${QEMU_TOOL}"
417   SUFFIX=gcc.arm
418 }
419
420 #@
421 #@ SetupGccArmOpt
422 #@   use gcc cross compiler
423 SetupGccArmOpt() {
424   PREFIX="${QEMU_TOOL}"
425   SUFFIX=gcc.opt.arm
426 }
427
428
429 SetupPnaclArmCommon() {
430   SetupSelLdr arm "${QEMU_TOOL}" "-Q"
431 }
432
433 #@
434 #@ SetupPnaclArmOpt
435 #@    use pnacl arm compiler (with lto)  -- run with QEMU
436 SetupPnaclArmOpt() {
437   SetupPnaclArmCommon
438   SUFFIX=pnacl.opt.arm
439 }
440
441 #@
442 #@ SetupPnaclArm
443 #@    use pnacl arm compiler (no lto)  -- run with QEMU
444 SetupPnaclArm() {
445   SetupPnaclArmCommon
446   SUFFIX=pnacl.arm
447 }
448
449 #@
450 #@ SetupPnaclTranslatorArm
451 #@    use pnacl arm translator (no lto)
452 SetupPnaclTranslatorArm() {
453   SetupPnaclArmCommon
454   SUFFIX=pnacl_translator.arm
455 }
456
457 #@
458 #@ SetupPnaclTranslatorArmOpt
459 #@    use pnacl arm translator (with lto)
460 SetupPnaclTranslatorArmOpt() {
461   SetupPnaclArmCommon
462   SUFFIX=pnacl_translator.opt.arm
463 }
464
465 #@
466 #@ SetupPnaclTranslatorFastArmOpt
467 #@    use pnacl arm translator fast mode (with lto)
468 SetupPnaclTranslatorFastArmOpt() {
469   SetupPnaclArmCommon
470   SUFFIX=pnacl_translator_fast.opt.arm
471 }
472
473
474
475 SetupPnaclArmCommonHW() {
476   SetupSelLdr arm
477 }
478
479 #@
480 #@ SetupPnaclArmOptHW
481 #@    use pnacl arm compiler (with lto) -- run on ARM hardware
482 SetupPnaclArmOptHW() {
483   SetupPnaclArmCommonHW
484   SUFFIX=pnacl.opt.arm
485 }
486
487 #@
488 #@ SetupPnaclArmHW
489 #@    use pnacl arm compiler (no lto) -- run on ARM hardware
490 SetupPnaclArmHW() {
491   SetupPnaclArmCommonHW
492   SUFFIX=pnacl.arm
493 }
494
495 #@
496 #@ SetupPnaclTranslatorArmHW
497 #@    use pnacl arm translator (no lto) -- run on ARM hardware
498 SetupPnaclTranslatorArmHW() {
499   SetupPnaclArmCommonHW
500   SUFFIX=pnacl_translator.hw.arm
501 }
502
503 #@
504 #@ SetupPnaclTranslatorArmOptHW
505 #@    use pnacl arm translator (with lto) -- run on ARM hardware
506 SetupPnaclTranslatorArmOptHW() {
507   SetupPnaclArmCommonHW
508   SUFFIX=pnacl_translator.opt.hw.arm
509 }
510
511 #@
512 #@ SetupPnaclTranslatorFastArmOptHW
513 #@    use pnacl arm translator fast mode (with lto) -- run on ARM hardware
514 SetupPnaclTranslatorFastArmOptHW() {
515   SetupPnaclArmCommonHW
516   SUFFIX=pnacl_translator_fast.opt.hw.arm
517 }
518
519 #@
520 #@ SetupPnaclTranslator1ThreadArmOptHW
521 #@    use pnacl arm translator (with lto) -- run on ARM hardware.
522 #@    compile with 1 thread.
523 SetupPnaclTranslator1ThreadArmOptHW() {
524   SetupPnaclArmCommonHW
525   SUFFIX=pnacl_translator_1thread.opt.hw.arm
526 }
527
528 #@
529 #@ SetupPnaclTranslatorFast1ThreadArmOptHW
530 #@    use pnacl arm translator fast mode (with lto) -- run on ARM hardware
531 #@    compile with 1 thread.
532 SetupPnaclTranslatorFast1ThreadArmOptHW() {
533   SetupPnaclArmCommonHW
534   SUFFIX=pnacl_translator_fast_1thread.opt.hw.arm
535 }
536
537
538
539 ConfigInfo() {
540   SubBanner "Config Info"
541   echo "benchmarks: $(GetBenchmarkList "$@")"
542   echo "script:     $(GetInputSize "$@")"
543   echo "suffix      ${SUFFIX}"
544   echo "verify      ${VERIFY}"
545   echo "prefix     ${PREFIX}"
546
547 }
548
549 ######################################################################
550 # Functions intended to be called
551 ######################################################################
552 #@
553 #@ ------------------------------------------------------------
554 #@ Available Modes:
555 #@ ------------------------------------------------------------
556
557 #@
558 #@ GetBenchmarkList
559 #@
560 #@   Show available benchmarks
561 GetBenchmarkList() {
562   if [[ $# -ge 1 ]]; then
563       if [[ ($1 == "ref") || ($1 == "train") ]]; then
564           shift
565       fi
566   fi
567
568   if [[ ($# == 0) || ($1 == "all") ]] ; then
569       echo "${SPEC2K_BENCHMARKS[@]}"
570   else
571       echo "$@"
572   fi
573 }
574
575 #+
576 #+ GetInputSize [train|ref]
577 #+
578 #+  Picks input size for spec runs (train or ref)
579 GetInputSize() {
580   if [[ $# -ge 1 ]]; then
581     case $1 in
582       train)
583         echo "./run.train.sh"
584         return
585         ;;
586       ref)
587         echo "./run.ref.sh"
588         return
589         ;;
590     esac
591   fi
592   echo ${SPEC2K_SCRIPT}
593 }
594
595 #+
596 #+ CheckFileBuilt <depname> <file> -
597 #+
598 #+   Check that a dependency is actually built.
599 CheckFileBuilt() {
600   local depname="$1"
601   local filename="$2"
602   if [[ ! -f "${filename}" ]] ; then
603     echo "You have not built ${depname} yet (${filename})!" 1>&2
604     exit -1
605   fi
606 }
607
608 #+
609 #+ SetupSelLdr <arch> <prefix> <extra_flags> <preload>
610 #+
611 #+   Set up PREFIX to run sel_ldr on <arch>.
612 #+   <prefix> precedes sel_ldr in the command.
613 #+   <extra_flags> are additional flags to sel_ldr.
614 #+   <preload> is used as the actual nexe to load, making the real nexe an arg.
615 SetupSelLdr() {
616   local arch="$1"
617   local prefix="${2-}"
618   local extra_flags="${3-}"
619   local preload="${4-}"
620
621   local staging="${SCONS_OUT}/opt-${SCONS_BUILD_PLATFORM}-${arch}/staging"
622   SEL_LDR="${staging}/sel_ldr"
623   CheckFileBuilt "sel_ldr" "${SEL_LDR}"
624   if [[ ${SCONS_BUILD_PLATFORM} = "linux" ]]; then
625     SEL_LDR_BOOTSTRAP="${staging}/nacl_helper_bootstrap"
626     CheckFileBuilt "bootstrap" "${SEL_LDR_BOOTSTRAP}"
627   fi
628   IRT_IMAGE="${SCONS_OUT}/nacl_irt-${arch}/staging/irt_core.nexe"
629   CheckFileBuilt "IRT image" "${IRT_IMAGE}"
630
631   local validator_bin="ncval"
632   if [[ ${arch} = "arm" ]]; then
633     validator_bin="arm-ncval-core"
634   fi
635   VALIDATOR="${staging}/${validator_bin}"
636   # We don't CheckFileBuilt for VALIDATOR because we currently don't build
637   # or use it on x86
638
639   if [[ ${SCONS_BUILD_PLATFORM} = "linux" ]]; then
640     TEMPLATE_DIGITS="XXXXXXXXXXXXXXXX"
641     PREFIX="${prefix} ${SEL_LDR_BOOTSTRAP} \
642 ${SEL_LDR} --r_debug=0x${TEMPLATE_DIGITS} \
643 --reserved_at_zero=0x${TEMPLATE_DIGITS} -B ${IRT_IMAGE} \
644 -a ${extra_flags} -f ${preload}"
645   else
646     PREFIX="${prefix} ${SEL_LDR} -B ${IRT_IMAGE} -a ${extra_flags} \
647 -f ${preload}"
648   fi
649   DASHDASH="--"
650 }
651
652 SCONS_COMMON="./scons --mode=opt-host,nacl -j8 --verbose"
653
654 EnableX8664ZeroBasedSandbox() {
655   export NACL_ENABLE_INSECURE_ZERO_BASED_SANDBOX=1
656 }
657
658 build-runtime() {
659   local platforms=$1
660   local runtime_pieces=$2
661   local extra_flags="${3-}"
662   for platform in ${platforms} ; do
663     echo "build-runtime: scons ${runtime_pieces} [${platform}]"
664     (cd ${NACL_ROOT};
665       ${SCONS_COMMON} ${extra_flags} platform=${platform} ${runtime_pieces})
666   done
667 }
668
669 build-libs-nacl() {
670   local platforms=$1
671   shift 1
672   for platform in ${platforms} ; do
673     echo "build-libs-nacl: scons build_lib [${platform}] $*"
674     (cd ${NACL_ROOT};
675       ${SCONS_COMMON} platform=${platform} build_lib "$@")
676   done
677 }
678
679 build-libs-pnacl() {
680   pushd "${NACL_ROOT}"
681   # TODO(dschuff/jvoung): fix this circular dependency with the toolchain build
682   TOOLCHAIN_LABEL=pnacl_linux_x86 pnacl/build.sh sdk newlib
683   popd
684 }
685
686 #@
687 #@ CleanBenchmarks <benchmark>*
688 #@
689 #@   this is a deep clean and you have to rerun PopulateFromSpecHarness
690 CleanBenchmarks() {
691   local list=$(GetBenchmarkList "$@")
692   rm -rf bin/
693   for i in ${list} ; do
694     SubBanner "Cleaning: $i"
695     cd $i
696     make clean
697     rm -rf src/ data/
698     cd ..
699   done
700 }
701
702 #@
703 #@ BuildBenchmarks <do_timing> <setup> <benchmark>*
704 #@
705 #@  Build all benchmarks according to the setup
706 #@  First arg should be either 0 (no timing) or 1 (run timing measure).
707 #@  Results are delivered to {execname}.compile_time
708 BuildBenchmarks() {
709   export PREFIX=
710   local timeit=$1
711   local setup_func=$2
712   "${setup_func}"
713   shift 2
714
715   local list=$(GetBenchmarkList "$@")
716   ConfigInfo "$@"
717   for i in ${list} ; do
718     SubBanner "Building: $i"
719     cd $i
720     # SPEC_COMPONENT is used for Asm.js builds in Makefile.common.
721     export SPEC_COMPONENT="${i}"
722
723     make ${MAKEOPTS} measureit=${timeit} \
724          PERF_LOGGER="${PERF_LOGGER}" \
725          REPETITIONS="${SPEC_COMPILE_REPETITIONS}" \
726          COMPILE_REPEATER="${COMPILE_REPEATER}" \
727          SCONS_BUILD_PLATFORM=${SCONS_BUILD_PLATFORM} \
728          BUILD_ARCH_SHORT=${BUILD_ARCH_SHORT} \
729          ${i#*.}.${SUFFIX}
730     cd ..
731   done
732 }
733
734
735 #@ TimedRunCmd <time_result_file> {actual_cmd }
736 #@
737 #@  Run the command under time and dump time data to file.
738 TimedRunCmd() {
739   target="$1"
740   shift
741   echo "Running: $@"
742   /usr/bin/time -f "%U %S %e %C" --append -o "${target}" "$@"
743 }
744
745 #@
746 #@ RunBenchmarks <setup> [ref|train] <benchmark>*
747 #@
748 #@  Run all benchmarks according to the setup.
749 RunBenchmarks() {
750   export PREFIX=
751   local setup_func=$1
752   "${setup_func}"
753   shift
754   local list=$(GetBenchmarkList "$@")
755   local script=$(GetInputSize "$@")
756   ConfigInfo "$@"
757   for i in ${list} ; do
758     SubBanner "Benchmarking: $i"
759     pushd $i
760     target_file=./${i#*.}.${SUFFIX}
761     echo_file_size ${target_file}
762     # SCRIPTNAME is needed by run_asmjs.sh so that it knows
763     # which version of the prepackaged files to use.
764     export SCRIPTNAME="${script}"
765     echo "Running: ${script} ${target_file}"
766     ${script} ${target_file}
767     popd
768   done
769 }
770
771
772 #@
773 #@ RunTimedBenchmarks <setup> [ref|train] <benchmark>*
774 #@
775 #@  Run all benchmarks according to the setup.
776 #@  All timing related files are stored in {execname}.run_time
777 #@  Note that the VERIFY variable effects the timing!
778 RunTimedBenchmarks() {
779   export PREFIX=
780   "$1"
781   shift
782   local list=$(GetBenchmarkList "$@")
783   local script=$(GetInputSize "$@")
784
785   ConfigInfo "$@"
786   for i in ${list} ; do
787     SubBanner "Benchmarking: $i"
788     pushd $i
789     local benchname=${i#*.}
790     local target_file=./${benchname}.${SUFFIX}
791     local time_file=${target_file}.run_time
792     echo_file_size  ${target_file}
793     # SCRIPTNAME is needed by run_asmjs.sh so that it knows
794     # which version of the prepackaged files to use.
795     export SCRIPTNAME="${script}"
796     # Clear out the previous times.
797     rm -f "${time_file}"
798     echo "Running benchmark ${SPEC_RUN_REPETITIONS} times"
799     for ((i=0; i<${SPEC_RUN_REPETITIONS}; i++))
800     do
801       TimedRunCmd ${time_file} ${script} ${target_file}
802     done
803     # TODO(jvoung): split runtimes by arch as well
804     # i.e., pull "arch" out of SUFFIX and add to the "runtime" label.
805     "${PERF_LOGGER}" LogRealTime "${time_file}" "runtime" \
806       ${benchname} ${SUFFIX}
807     popd
808   done
809 }
810
811 TimeValidation() {
812   local setup_func=$1
813   "${setup_func}"
814   shift
815   local list=$(GetBenchmarkList "$@")
816   for i in ${list}; do
817     SubBanner "Validating: $i"
818     pushd $i
819     local benchname=${i#*.}
820     local target_file=./${benchname}.${SUFFIX}
821     local time_file=${target_file}.validation_time
822     rm -f "${time_file}"
823     for ((i=0; i<${SPEC_RUN_REPETITIONS}; i++))
824     do
825       TimedRunCmd ${time_file} "${VALIDATOR}" ${target_file}
826     done
827     "${PERF_LOGGER}" LogRealTime "${time_file}" "validationtime" \
828       ${benchname} ${SUFFIX}
829     popd
830   done
831   if [[ ${setup_func} =~ "Arm" ]]; then
832     TimedRunCmd llc.validation_time "${VALIDATOR}" "${ARM_LLC_NEXE}"
833     "${PERF_LOGGER}" LogRealTime llc.validation_time "validationtime" \
834       "llc" ${SUFFIX}
835   fi
836 }
837
838 #@
839 #@ BuildAndRunBenchmarks <setup> [ref|train] <benchmark>*
840 #@
841 #@   Builds and run all benchmarks according to the setup
842 BuildAndRunBenchmarks() {
843   setup=$1
844   shift
845   BuildBenchmarks 0 ${setup} "$@"
846   RunBenchmarks ${setup} "$@"
847 }
848
849 #@
850 #@ TimedBuildAndRunBenchmarks <setup> [ref|train] <benchmark>*
851 #@
852 #@   Builds and run all benchmarks according to the setup, using
853 #@   and records the time spent at each task..
854 #@   Results are saved in {execname}.compile_time and
855 #@   {execname}.run_time for each benchmark executable
856 #@   Note that the VERIFY variable effects the timing!
857 TimedBuildAndRunBenchmarks() {
858   setup=$1
859   shift
860   BuildBenchmarks 1 ${setup} "$@"
861   RunTimedBenchmarks ${setup} "$@"
862 }
863
864 #@
865 #@ PackageArmBinaries [usual var-args for RunBenchmarks]
866 #@
867 #@   Archives ARM binaries built from a local QEMU run of spec
868 #@
869 #@   Note: <setup> should be the QEMU setup (e.g., SetupPnaclArmOpt)
870 #@   Note: As with the other modes in this script, this script should be
871 #@   run from the directory of the script (not the native_client directory).
872 #@
873 PackageArmBinaries() {
874   local BENCH_LIST=$(GetBenchmarkList "$@")
875
876   local UNZIPPED_TAR=$(basename ${TESTS_ARCHIVE} .gz)
877
878   # Switch to native_client directory (from tests/spec2k) so that
879   # when we extract, the builder will have a more natural directory layout.
880   pushd "../.."
881   # Carefully tar only the parts of the spec harness that we need.
882   # First prune
883   find tests/spec2k -name '*.bc' -delete
884   find tests/spec2k -maxdepth 1 -type f -print |
885     xargs tar --no-recursion -cvf ${UNZIPPED_TAR}
886   tar -rvf ${UNZIPPED_TAR} tests/spec2k/bin
887   for i in ${BENCH_LIST} ; do
888     tar -rvf ${UNZIPPED_TAR} tests/spec2k/$i
889   done
890   gzip -f ${UNZIPPED_TAR}
891   popd
892 }
893
894 #@ UnpackArmBinaries
895 #@ Unpack a packaged archive of ARM SPEC binaries. The archive is
896 #@ located in the nacl root directory, but the script is run from the spec dir.
897 UnpackArmBinaries() {
898   (cd ${NACL_ROOT};
899     tar xvzf ${TESTS_ARCHIVE})
900 }
901
902 GetTestArchiveName() {
903   echo ${TESTS_ARCHIVE}
904 }
905
906 #@
907 #@ PopulateFromSpecHarness <path> <benchmark>*
908 #@
909 #@   populate a few essential directories (src, date) from
910 #@   the given spec2k harness
911 PopulateFromSpecHarness() {
912   harness=$1
913   shift
914   cp -r ${harness}/bin .
915   local list=$(GetBenchmarkList "$@")
916   echo ${list}
917   for i in ${list} ; do
918     SubBanner "Populating: $i"
919     # fix the dir with the same name inside spec harness
920     src=$(find -H ${harness} -name $i)
921     # copy relevant dirs over
922     echo "COPY"
923     rm -rf src/ data/
924     cp -r ${src}/data ${src}/src $i
925     # patch if necessary
926     if [[ -e $i/diff ]] ; then
927       echo "PATCH"
928       patch -d $i --verbose -p0 < $i/diff
929     fi
930
931     echo "COMPLETE"
932   done
933 }
934
935 #@
936 #@ BuildPrerequisites <platform> <bitcode>
937 #@
938 #@   Invoke scons to build some potentially missing  components, e.g.
939 #@   sel_ldr, sel_universal, irt, some untrusted libraries.
940 #@   Those compoents should be present in the SDK but are not in the
941 #@   standard toolchain tarballs.
942 BuildPrerequisites() {
943   local platforms=$1
944   local bitcode=$2
945   local extrabuild="${3-}"
946   local extra_flags="${4-}"
947   # Sel universal is only used for the pnacl sandboxed translator,
948   # but prepare it just in case.
949   # IRT is used both to run the tests and to run the pnacl sandboxed translator.
950   build-runtime "${platforms}" "sel_ldr sel_universal irt_core ${extrabuild}" \
951 ${extra_flags}
952   if [ ${bitcode} == "bitcode" ] ; then
953      build-libs-pnacl
954   else
955     # libs may be unnecessary for the glibc build, but build it just in case.
956     build-libs-nacl "${platforms}"
957   fi
958 }
959
960 #@
961 #@ BuildPrerequisitesSetupBased <setup>
962 #@
963 #@   Convenience wrapper for BuildPrerequisites
964 BuildPrerequisitesSetupBased() {
965   local platforms=""
966   local bitcode=""
967   if [[ "$1" == SetupPnacl* ]] ; then
968     bitcode="bitcode"
969   fi
970   if [[ "$1" == Setup*Arm* ]] ; then
971     platforms="arm"
972   elif [[ "$1" == Setup*X8632* ]] ; then
973     platforms="x86-32"
974   elif [[ "$1" == Setup*X8664* ]] ; then
975     platforms="x86-64"
976   else
977     echo "Bad setup [$1]"
978     exit -1
979   fi
980   BuildPrerequisites "${platforms}" "${bitcode}"
981 }
982 ######################################################################
983 # Main
984 ######################################################################
985
986 [ $# = 0 ] && set -- help  # Avoid reference to undefined $1.
987
988 if [ "$(type -t $1)" != "function" ]; then
989   Usage
990   echo "ERROR: unknown mode '$1'." >&2
991   exit 1
992 fi
993
994 "$@"