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