Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: tools/sanitizers/sancov_merger.py

Issue 1737263003: [coverage] Enable sanitizer coverage. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Logging + exe blacklist Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/sanitizers/sancov_formatter_test.py ('k') | tools/sanitizers/sancov_merger_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # Copyright 2016 the V8 project authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Script for merging sancov files in parallel.
7
8 The sancov files are expected
9 to be located in one directory with the file-name pattern:
10 <executable name>.test.<id>.sancov
11
12 For each executable, this script writes a new file:
13 <executable name>.result.sancov
14
15 The sancov tool is expected to be in the llvm compiler-rt third-party
16 directory. It's not checked out by default and must be added as a custom deps:
17 'v8/third_party/llvm/projects/compiler-rt':
18 'https://chromium.googlesource.com/external/llvm.org/compiler-rt.git'
19 """
20
21 import argparse
22 import logging
23 import math
24 import os
25 import re
26 import subprocess
27 import sys
28
29 from multiprocessing import Pool, cpu_count
30
31
32 logging.basicConfig(level=logging.INFO)
33
34 # V8 checkout directory.
35 BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(
36 os.path.abspath(__file__))))
37
38 # The sancov tool location.
39 SANCOV_TOOL = os.path.join(
40 BASE_DIR, 'third_party', 'llvm', 'projects', 'compiler-rt',
41 'lib', 'sanitizer_common', 'scripts', 'sancov.py')
42
43 # Number of cpus.
44 CPUS = cpu_count()
45
46 # Regexp to find sancov file as output by the v8 test runner. Also grabs the
47 # executable name in group 1.
48 SANCOV_FILE_RE = re.compile(r'^(.*)\.test\.\d+\.sancov$')
49
50
51 def merge(args):
52 """Merge several sancov files into one.
53
54 Called trough multiprocessing pool. The args are expected to unpack to:
55 keep: Option if source and intermediate sancov files should be kept.
56 coverage_dir: Folder where to find the sancov files.
57 executable: Name of the executable whose sancov files should be merged.
58 index: A number to be put into the intermediate result file name.
59 If None, this is a final result.
60 bucket: The list of sancov files to be merged.
61 Returns: A tuple with the executable name and the result file name.
62 """
63 keep, coverage_dir, executable, index, bucket = args
64 process = subprocess.Popen(
65 [SANCOV_TOOL, 'merge'] + bucket,
66 stdout=subprocess.PIPE,
67 stderr=subprocess.PIPE,
68 cwd=coverage_dir,
69 )
70 output, _ = process.communicate()
71 assert process.returncode == 0
72 if index is not None:
73 # This is an intermediate result, add the bucket index to the file name.
74 result_file_name = '%s.result.%d.sancov' % (executable, index)
75 else:
76 # This is the final result without bucket index.
77 result_file_name = '%s.result.sancov' % executable
78 with open(os.path.join(coverage_dir, result_file_name), "wb") as f:
79 f.write(output)
80 if not keep:
81 for f in bucket:
82 os.remove(os.path.join(coverage_dir, f))
83 return executable, result_file_name
84
85
86 def generate_inputs(keep, coverage_dir, file_map, cpus):
87 """Generate inputs for multiprocessed merging.
88
89 Splits the sancov files into several buckets, so that each bucket can be
90 merged in a separate process. We have only few executables in total with
91 mostly lots of associated files. In the general case, with many executables
92 we might need to avoid splitting buckets of executables with few files.
93
94 Returns: List of args as expected by merge above.
95 """
96 inputs = []
97 for executable, files in file_map.iteritems():
98 # What's the bucket size for distributing files for merging? E.g. with
99 # 2 cpus and 9 files we want bucket size 5.
100 n = max(2, int(math.ceil(len(files) / float(cpus))))
101
102 # Chop files into buckets.
103 buckets = [files[i:i+n] for i in xrange(0, len(files), n)]
104
105 # Inputs for multiprocessing. List of tuples containing:
106 # Keep-files option, base path, executable name, index of bucket,
107 # list of files.
108 inputs.extend([(keep, coverage_dir, executable, i, b)
109 for i, b in enumerate(buckets)])
110 return inputs
111
112
113 def merge_parallel(inputs):
114 """Process several merge jobs in parallel."""
115 pool = Pool(CPUS)
116 try:
117 return pool.map(merge, inputs)
118 finally:
119 pool.close()
120
121
122 def main():
123 parser = argparse.ArgumentParser()
124 parser.add_argument('--coverage-dir', required=True,
125 help='Path to the sancov output files.')
126 parser.add_argument('--keep', default=False, action='store_true',
127 help='Keep sancov output files after merging.')
128 options = parser.parse_args()
129
130 # Check if folder with coverage output exists.
131 assert (os.path.exists(options.coverage_dir) and
132 os.path.isdir(options.coverage_dir))
133
134 # Map executable names to their respective sancov files.
135 file_map = {}
136 for f in os.listdir(options.coverage_dir):
137 match = SANCOV_FILE_RE.match(f)
138 if match:
139 file_map.setdefault(match.group(1), []).append(f)
140
141 inputs = generate_inputs(
142 options.keep, options.coverage_dir, file_map, CPUS)
143
144 logging.info('Executing %d merge jobs in parallel for %d executables.' %
145 (len(inputs), len(file_map)))
146
147 results = merge_parallel(inputs)
148
149 # Map executable names to intermediate bucket result files.
150 file_map = {}
151 for executable, f in results:
152 file_map.setdefault(executable, []).append(f)
153
154 # Merge the bucket results for each executable.
155 # The final result has index None, so no index will appear in the
156 # file name.
157 inputs = [(options.keep, options.coverage_dir, executable, None, files)
158 for executable, files in file_map.iteritems()]
159
160 logging.info('Merging %d intermediate results.' % len(inputs))
161
162 merge_parallel(inputs)
163 return 0
164
165
166 if __name__ == '__main__':
167 sys.exit(main())
OLDNEW
« no previous file with comments | « tools/sanitizers/sancov_formatter_test.py ('k') | tools/sanitizers/sancov_merger_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698