benchgen.py revision 4e378be81e7775a7084983cb5dcc69189f7e1b11
1#!/usr/bin/env python
2
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the 'License');
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an 'AS IS' BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Generates storage benchmark from captured strace output.
19
20Currently assumes that all mmap'ed regions are resource accesses, and emulates as pread().
21
22Usage:
23$ adb shell strace -p `pid zygote` -o /data/local/tmp/trace -f -ff -y -ttt -e trace=file,desc,munmap
24$ adb pull /data/local/tmp/trace*
25$ python benchgen.py trace.*
26
27"""
28
29import re, sys, collections, traceback, argparse
30
31from operator import itemgetter
32from collections import defaultdict
33
34class Event:
35    def __init__(self, thread, time, call, args, ret):
36        self.thread = thread
37        self.time = time
38        self.call = call
39        self.args = args
40        self.ret = ret
41
42    def __repr__(self):
43        return "%s(%s)=%s" % (self.call, repr(self.args), self.ret)
44
45
46class File:
47    def __init__(self, name, ident):
48        self.name = name
49        self.ident = ident
50        self.size = 0
51
52    def __repr__(self):
53        return self.name
54
55
56events = []
57files = {}
58
59def find_file(name):
60    name = name.strip('<>"')
61    if name not in files:
62        files[name] = File(name, len(files))
63    return files[name]
64
65def extract_file(e, arg):
66    if "<" in arg:
67        fd, path = arg.split("<")
68        path = path.strip(">")
69        handle = "t%sf%s" % (e.thread, fd)
70        return (fd, find_file(path), handle)
71    else:
72        return (None, None, None)
73
74def parse_args(s):
75    args = []
76    arg = ""
77    esc = False
78    quot = False
79    for c in s:
80        if esc:
81            esc = False
82            arg += c
83            continue
84
85        if c == '"':
86            if quot:
87                quot = False
88                continue
89            else:
90                quot = True
91                continue
92
93        if c == '\\':
94            esc = True
95            continue
96
97        if c == ',' and not quot:
98            args.append(arg.strip())
99            arg = ""
100        else:
101            arg += c
102
103    args.append(arg.strip())
104    return args
105
106
107bufsize = 1048576
108interesting = ["mmap2","read","write","pread64","pwrite64","fsync","fdatasync","openat","close","lseek","_llseek"]
109
110re_event = re.compile(r"^([\d\.]+) (.+?)\((.+?)\) = (.+?)$")
111re_arg = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
112for fn in sys.argv[1:]:
113    with open(fn) as f:
114        thread = int(fn.split(".")[-1])
115        for line in f:
116            line = re_event.match(line)
117            if not line: continue
118
119            time, call, args, ret = line.groups()
120            if call not in interesting: continue
121            if "/data/" not in args: continue
122
123            time = float(time)
124            args = parse_args(args)
125            events.append(Event(thread, time, call, args, ret))
126
127
128with open("BenchmarkGen.h", 'w') as bench:
129    print >>bench, """/*
130 * Copyright (C) 2015 The Android Open Source Project
131 *
132 * Licensed under the Apache License, Version 2.0 (the "License");
133 * you may not use this file except in compliance with the License.
134 * You may obtain a copy of the License at
135 *
136 *      http://www.apache.org/licenses/LICENSE-2.0
137 *
138 * Unless required by applicable law or agreed to in writing, software
139 * distributed under the License is distributed on an "AS IS" BASIS,
140 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141 * See the License for the specific language governing permissions and
142 * limitations under the License.
143 */
144
145
146/******************************************************************
147 * THIS CODE WAS GENERATED BY benchgen.py, DO NOT MODIFY DIRECTLY *
148 ******************************************************************/
149
150
151#include <base/logging.h>
152
153#include <stdlib.h>
154#include <sys/types.h>
155#include <sys/stat.h>
156#include <sys/sendfile.h>
157#include <fcntl.h>
158
159#include <algorithm>
160#include <string>
161
162namespace android {
163namespace vold {
164
165static status_t BenchmarkRun() {
166"""
167
168    print >>bench, "char* buf = (char*) malloc(%d);" % (bufsize)
169
170    nread = 0
171    nwrite = 0
172    nsync = 0
173    events = sorted(events, key=lambda e: e.time)
174    active = set()
175    defined = set()
176    for e in events:
177        if e.call == "openat":
178            fd, f, handle = extract_file(e, e.ret)
179            if f:
180                active.add(handle)
181                if handle not in defined:
182                    print >>bench, "int ",
183                    defined.add(handle)
184                print >>bench, '%s = TEMP_FAILURE_RETRY(open("file%s", %s));' % (handle, f.ident, e.args[2])
185
186        elif e.call == "close":
187            fd, f, handle = extract_file(e, e.args[0])
188            if handle in active:
189                active.remove(handle)
190                print >>bench, 'close(%s);' % (handle)
191
192        elif e.call == "lseek":
193            fd, f, handle = extract_file(e, e.args[0])
194            if handle in active:
195                print >>bench, 'TEMP_FAILURE_RETRY(lseek(%s, %s, %s));' % (handle, e.args[1], e.args[2])
196
197        elif e.call == "_llseek":
198            fd, f, handle = extract_file(e, e.args[0])
199            if handle in active:
200                print >>bench, 'TEMP_FAILURE_RETRY(lseek(%s, %s, %s));' % (handle, e.args[1], e.args[3])
201
202        elif e.call == "read":
203            fd, f, handle = extract_file(e, e.args[0])
204            if handle in active:
205                # TODO: track actual file size instead of guessing
206                count = min(int(e.args[2]), bufsize)
207                f.size += count
208                print >>bench, 'TEMP_FAILURE_RETRY(read(%s, buf, %d));' % (handle, count)
209                nread += 1
210
211        elif e.call == "write":
212            fd, f, handle = extract_file(e, e.args[0])
213            if handle in active:
214                # TODO: track actual file size instead of guessing
215                count = min(int(e.args[2]), bufsize)
216                f.size += count
217                print >>bench, 'TEMP_FAILURE_RETRY(read(%s, buf, %d));' % (handle, count)
218                nwrite += 1
219
220        elif e.call == "pread64":
221            fd, f, handle = extract_file(e, e.args[0])
222            if handle in active:
223                f.size = max(f.size, int(e.args[2]) + int(e.args[3]))
224                count = min(int(e.args[2]), bufsize)
225                print >>bench, 'TEMP_FAILURE_RETRY(pread(%s, buf, %d, %s));' % (handle, count, e.args[3])
226                nread += 1
227
228        elif e.call == "pwrite64":
229            fd, f, handle = extract_file(e, e.args[0])
230            if handle in active:
231                f.size = max(f.size, int(e.args[2]) + int(e.args[3]))
232                count = min(int(e.args[2]), bufsize)
233                print >>bench, 'TEMP_FAILURE_RETRY(pwrite(%s, buf, %d, %s));' % (handle, count, e.args[3])
234                nwrite += 1
235
236        elif e.call == "fsync":
237            fd, f, handle = extract_file(e, e.args[0])
238            if handle in active:
239                print >>bench, 'TEMP_FAILURE_RETRY(fsync(%s));' % (handle)
240                nsync += 1
241
242        elif e.call == "fdatasync":
243            fd, f, handle = extract_file(e, e.args[0])
244            if handle in active:
245                print >>bench, 'TEMP_FAILURE_RETRY(fdatasync(%s));' % (handle)
246                nsync += 1
247
248        elif e.call == "mmap2":
249            fd, f, handle = extract_file(e, e.args[4])
250            if handle in active:
251                count = min(int(e.args[1]), bufsize)
252                offset = int(e.args[5], 0)
253                f.size = max(f.size, count + offset)
254                print >>bench, 'TEMP_FAILURE_RETRY(pread(%s, buf, %s, %s)); // mmap2' % (handle, count, offset)
255                nread += 1
256
257    for handle in active:
258        print >>bench, 'close(%s);' % (handle)
259
260    print >>bench, """
261free(buf);
262return 0;
263}
264
265static status_t CreateFile(const char* name, size_t len) {
266    status_t res = -1;
267    int in = -1;
268    int out = -1;
269
270    if ((in = TEMP_FAILURE_RETRY(open("/dev/zero", O_RDONLY))) < 0) {
271        PLOG(ERROR) << "Failed to open";
272        goto done;
273    }
274    if ((out = TEMP_FAILURE_RETRY(open(name, O_WRONLY|O_CREAT|O_TRUNC))) < 0) {
275        PLOG(ERROR) << "Failed to open " << name;
276        goto done;
277    }
278
279    char buf[65536];
280    while (len > 0) {
281        int n = read(in, buf, std::min(len, sizeof(buf)));
282        if (write(out, buf, n) != n) {
283            PLOG(ERROR) << "Failed to write";
284            goto done;
285        }
286        len -= n;
287    }
288
289    res = 0;
290done:
291    close(in);
292    close(out);
293    return res;
294}
295
296static status_t BenchmarkCreate() {
297status_t res = 0;
298res |= CreateFile("stub", 0);
299"""
300    for f in files.values():
301        print >>bench, 'res |= CreateFile("file%s", %d);' % (f.ident, f.size)
302
303    print >>bench, """
304return res;
305}
306
307static status_t BenchmarkDestroy() {
308status_t res = 0;
309res |= unlink("stub");
310"""
311    for f in files.values():
312        print >>bench, 'res |= unlink("file%s");' % (f.ident)
313
314    print >>bench, """
315return res;
316}
317
318static std::string BenchmarkIdent() {"""
319    print >>bench, """return "r%d:w%d:s%d";""" % (nread, nwrite, nsync)
320    print >>bench, """}
321
322}  // namespace vold
323}  // namespace android
324"""
325
326
327size = sum([ f.size for f in files.values() ])
328print "Found", len(files), "data files accessed, total size", (size/1024), "kB"
329
330types = defaultdict(int)
331for e in events:
332    types[e.call] += 1
333
334print "Found syscalls:"
335for t, n in types.iteritems():
336    print str(n).rjust(8), t
337
338print
339