1import re, pickle, os, logging
2from autotest_lib.client.bin import utils, test
3
4
5class kernbench(test.test):
6    version = 4
7
8    def initialize(self):
9        self.job.require_gcc()
10        self.job.drop_caches_between_iterations = False
11
12
13    def __init_tree(self, version=None):
14        #
15        # If we have a local copy of the 2.6.14 tarball use that
16        # else let the kernel object use the defined mirrors
17        # to obtain it.
18        #
19        # http://kernel.org/pub/linux/kernel/v2.6/linux-2.6.14.tar.bz2
20        #
21        # On ia64, we default to 2.6.20, as it can't compile 2.6.14.
22        if version:
23            default_ver = version
24        elif utils.get_current_kernel_arch() == 'ia64':
25            default_ver = '2.6.20'
26        else:
27            default_ver = '2.6.14'
28
29        tarball = None
30        for dir in (self.bindir, '/usr/local/src'):
31            tar = 'linux-%s.tar.bz2' % default_ver
32            path = os.path.join(dir, tar)
33            if os.path.exists(path):
34                tarball = path
35                break
36        if not tarball:
37            tarball = default_ver
38
39        # Do the extraction of the kernel tree
40        kernel = self.job.kernel(tarball, self.outputdir, self.tmpdir)
41        kernel.config(defconfig=True, logged=False)
42        return kernel
43
44
45    def warmup(self, threads=None, version=None):
46        if threads:
47            self.threads = threads
48        else:
49            self.threads = self.job.cpu_count()*2
50
51        self.kernel = self.__init_tree(version)
52        logging.info("Warmup run ...")
53        logfile = os.path.join(self.debugdir, 'build_log')
54        try:
55            self.kernel.build_timed(self.threads, output=logfile)  # warmup run
56        finally:
57            if os.path.exists(logfile):
58                utils.system("gzip -9 '%s'" % logfile, ignore_status=True)
59
60
61    def run_once(self):
62        logging.info("Performance run, iteration %d,"
63                     " %d threads" % (self.iteration, self.threads))
64        if self.iteration:
65            timefile = 'time.%d' % self.iteration
66        else:
67            timefile = 'time.profile'
68        self.timefile = os.path.join(self.resultsdir, timefile)
69        self.kernel.build_timed(self.threads, self.timefile)
70
71
72    def cleanup(self):
73        self.kernel.clean(logged=False)    # Don't leave litter lying around
74
75
76    def postprocess_iteration(self):
77        os.chdir(self.resultsdir)
78        utils.system("grep -h elapsed %s >> time" % self.timefile)
79
80        results = open(self.timefile).read()
81        (user, system, elapsed) = utils.extract_all_time_results(results)[0]
82        self.write_perf_keyval({'user':user,
83                                'system':system,
84                                'elapsed':elapsed
85                               })
86