Framework for performance benchmarking of functions
[platform/upstream/glibc.git] / scripts / bench.pl
1 #! /usr/bin/perl -w
2 # Copyright (C) 2013 Free Software Foundation, Inc.
3 # This file is part of the GNU C Library.
4
5 # The GNU C Library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9
10 # The GNU C Library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with the GNU C Library; if not, see
17 # <http://www.gnu.org/licenses/>.
18
19
20 use strict;
21 use warnings;
22 # Generate a benchmark source file for a given input.
23
24 if (@ARGV < 2) {
25   die "Usage: bench.pl <function> <iterations> [parameter types] [return type]"
26 }
27
28 my $arg;
29 my $func = $ARGV[0];
30 my $iters = $ARGV[1];
31 my @args;
32 my $ret = "void";
33 my $getret = "";
34 my $retval = "";
35
36 if (@ARGV >= 3) {
37   @args = split(':', $ARGV[2]);
38 }
39
40 if (@ARGV == 4) {
41   $ret = $ARGV[3];
42 }
43
44 my $decl = "extern $ret $func (";
45
46 if (@args == 0 || $args[0] eq "void") {
47   print "$decl void);\n";
48   print "#define CALL_BENCH_FUNC(j) $func();\n";
49   print "#define NUM_SAMPLES (1)\n";
50 }
51 else {
52   my $num = 0;
53   my $bench_func = "#define CALL_BENCH_FUNC(j) $func (";
54   my $struct = "struct args {";
55
56   foreach $arg (@args) {
57     if ($num > 0) {
58       $bench_func = "$bench_func,";
59       $decl = "$decl,";
60     }
61
62     $struct = "$struct $arg arg$num;";
63     $bench_func = "$bench_func in[j].arg$num";
64     $decl = "$decl $arg";
65     $num = $num + 1;
66   }
67
68   print "$decl);\n";
69   print "$bench_func);\n";
70   print "$struct } in[] = {";
71
72   open INPUTS, "<$func-inputs" or die $!;
73
74   while (<INPUTS>) {
75     chomp;
76     print "{$_},\n";
77   }
78   print "};\n";
79   print "#define NUM_SAMPLES (sizeof (in) / sizeof (struct args))\n"
80 }
81
82 # In some cases not storing a return value seems to result in the function call
83 # being optimized out.
84 if ($ret ne "void") {
85   print "static volatile $ret ret = 0.0;\n";
86   $getret = "ret = ";
87 }
88
89 print "#define BENCH_FUNC(j) ({$getret CALL_BENCH_FUNC (j);})\n";
90
91 print "#define ITER $iters\n";
92 print "#define FUNCNAME \"$func\"\n";
93 print "#include \"bench-skeleton.c\"\n";