1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
#! /usr/bin/env python3
#
# Copyright (C) 2010-2025 Joel Rosdahl and other contributors
#
# See doc/AUTHORS.adoc for a complete list of contributors.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
from optparse import OptionParser
from os import access, environ, mkdir, getpid, X_OK
from os.path import (
abspath,
basename,
exists,
isabs,
isfile,
join as joinpath,
realpath,
splitext,
)
from shutil import rmtree
from subprocess import call
from statistics import median
from time import time
import sys
USAGE = """%prog [options] <compiler> [compiler options] <source code file>"""
DESCRIPTION = """\
This program compiles a C/C++ file with/without ccache a number of times to get
some idea of ccache speedup and overhead in the preprocessor and direct modes.
The arguments to the program should be the compiler, optionally followed by
compiler options, and finally the source file to compile. The compiler options
must not contain -c or -o as these options will be added later. Example:
misc/performance gcc -g -O2 -Idir file.c
"""
DEFAULT_CCACHE = "./ccache"
DEFAULT_DIRECTORY = "."
DEFAULT_HIT_FACTOR = 1
DEFAULT_TIMES = 30
PHASES = [
"without ccache",
"with ccache, preprocessor mode, cache miss",
"with ccache, preprocessor mode, cache hit",
"with ccache, direct mode, cache miss",
"with ccache, direct mode, cache hit",
"with ccache, depend mode, cache miss",
"with ccache, depend mode, cache hit",
]
verbose = False
def progress(msg):
if verbose:
sys.stderr.write(msg)
sys.stderr.flush()
def recreate_dir(x):
if exists(x):
rmtree(x)
mkdir(x)
def test(tmp_dir, options, compiler_args, source_file):
src_dir = "%s/src" % tmp_dir
obj_dir = "%s/obj" % tmp_dir
ccache_dir = "%s/ccache" % tmp_dir
mkdir(src_dir)
mkdir(obj_dir)
compiler_args += ["-c", "-o"]
extension = splitext(source_file)[1]
hit_factor = options.hit_factor
times = options.times
progress("Creating source code\n")
for i in range(times):
with open("%s/%d%s" % (src_dir, i, extension), "w") as fp:
with open(source_file) as fp2:
content = fp2.read()
fp.write(content)
fp.write("\nint ccache_perf_test_%d;\n" % i)
environment = {"CCACHE_DIR": ccache_dir, "PATH": environ["PATH"]}
environment["CCACHE_COMPILERCHECK"] = options.compilercheck
if options.compression_level:
environment["CCACHE_COMPRESSLEVEL"] = str(options.compression_level)
if options.file_clone:
environment["CCACHE_FILECLONE"] = "1"
if options.hardlink:
environment["CCACHE_HARDLINK"] = "1"
if options.no_compression:
environment["CCACHE_NOCOMPRESS"] = "1"
if options.no_stats:
environment["CCACHE_NOSTATS"] = "1"
results = []
def run(
times, *, use_direct, use_depend, use_ccache=True, print_progress=True
):
timings = []
for i in range(times):
obj = "%s/%d.o" % (obj_dir, i)
src = "%s/%d%s" % (src_dir, i, extension)
if use_ccache:
args = [options.ccache]
else:
args = []
args += compiler_args + [obj, src]
env = environment.copy()
if not use_direct:
env["CCACHE_NODIRECT"] = "1"
if use_depend:
env["CCACHE_DEPEND"] = "1"
if print_progress:
progress(".")
t0 = time()
if call(args, env=env) != 0:
sys.stderr.write(
'Error running "%s"; please correct\n' % " ".join(args)
)
sys.exit(1)
timings.append(time() - t0)
return timings
# Warm up the disk cache.
recreate_dir(ccache_dir)
recreate_dir(obj_dir)
run(1, use_direct=True, use_depend=False, print_progress=False)
###########################################################################
# Without ccache
recreate_dir(ccache_dir)
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[0])
results.append(
run(times, use_direct=False, use_depend=False, use_ccache=False)
)
progress("\n")
###########################################################################
# Preprocessor mode
recreate_dir(ccache_dir)
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[1])
results.append(run(times, use_direct=False, use_depend=False))
progress("\n")
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[2])
res = []
for j in range(hit_factor):
res += run(times, use_direct=False, use_depend=False)
results.append(res)
progress("\n")
###########################################################################
# Direct mode
recreate_dir(ccache_dir)
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[3])
results.append(run(times, use_direct=True, use_depend=False))
progress("\n")
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[4])
res = []
for j in range(hit_factor):
res += run(times, use_direct=True, use_depend=False)
results.append(res)
progress("\n")
###########################################################################
# Direct+depend mode
recreate_dir(ccache_dir)
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[5])
results.append(run(times, use_direct=True, use_depend=True))
progress("\n")
recreate_dir(obj_dir)
progress("Compiling %s\n" % PHASES[6])
res = []
for j in range(hit_factor):
res += run(times, use_direct=True, use_depend=True)
results.append(res)
progress("\n")
for i, x in enumerate(results):
results[i] = median(x)
return results
def print_result_as_text(results):
for i, x in enumerate(PHASES):
print(
"%-43s %6.4f s (%8.4f %%) (%8.4f x)"
% (
x.capitalize() + ":",
results[i],
100 * (results[i] / results[0]),
results[0] / results[i],
)
)
def print_result_as_xml(results):
print('<?xml version="1.0" encoding="UTF-8"?>')
print("<ccache-perf>")
for i, x in enumerate(PHASES):
print("<measurement>")
print("<name>%s</name>" % x.capitalize())
print("<seconds>%.4f</seconds>" % results[i])
print("<percent>%.4f</percent>" % (100 * (results[i] / results[0])))
print("<times>%.4f</times>" % (results[0] / results[i]))
print("</measurement>")
print("</ccache-perf>")
def on_off(x):
return "on" if x else "off"
def find_in_path(cmd):
if isabs(cmd):
return cmd
else:
for path in environ["PATH"].split(":"):
p = joinpath(path, cmd)
if isfile(p) and access(p, X_OK):
return p
return None
def main(argv):
op = OptionParser(usage=USAGE, description=DESCRIPTION)
op.disable_interspersed_args()
op.add_option(
"--ccache", help="location of ccache (default: %s)" % DEFAULT_CCACHE
)
op.add_option(
"--compilercheck", help="specify compilercheck (default: mtime)"
)
op.add_option(
"--no-compression", help="disable compression", action="store_true"
)
op.add_option("--compression-level", help="set compression level", type=int)
op.add_option(
"-d",
"--directory",
help=(
"where to create the temporary directory with the cache and other"
" files (default: %s)" % DEFAULT_DIRECTORY
),
)
op.add_option("--file-clone", help="use file cloning", action="store_true")
op.add_option("--hardlink", help="use hard links", action="store_true")
op.add_option(
"--hit-factor",
help=(
"how many times more to compile the file for cache hits (default:"
" %d)" % DEFAULT_HIT_FACTOR
),
type="int",
)
op.add_option(
"--no-stats", help="don't write statistics", action="store_true"
)
op.add_option(
"-n",
"--times",
help=(
"number of times to compile the file (default: %d)" % DEFAULT_TIMES
),
type="int",
)
op.add_option(
"-v", "--verbose", help="print progress messages", action="store_true"
)
op.add_option("--xml", help="print results as XML", action="store_true")
op.set_defaults(
ccache=DEFAULT_CCACHE,
compilercheck="mtime",
directory=DEFAULT_DIRECTORY,
hit_factor=DEFAULT_HIT_FACTOR,
times=DEFAULT_TIMES,
)
options, args = op.parse_args(argv[1:])
if len(args) < 2:
op.error("Missing arguments; pass -h/--help for help")
global verbose
verbose = options.verbose
options.ccache = abspath(options.ccache)
compiler = find_in_path(args[0])
if compiler is None:
op.error("Could not find %s in PATH" % args[0])
if "ccache" in basename(realpath(compiler)):
op.error(
"%s seems to be a symlink to ccache; please specify the path to"
" the real compiler instead" % compiler
)
if not options.xml:
print(
"Compilation command: %s -c -o %s.o"
% (" ".join(args), splitext(argv[-1])[0])
)
print("Compilercheck:", options.compilercheck)
print("Compression:", on_off(not options.no_compression))
print("Compression level:", options.compression_level or "default")
print("File cloning:", on_off(options.file_clone))
print("Hard linking:", on_off(options.hardlink))
print("No stats:", on_off(options.no_stats))
tmp_dir = "%s/perfdir.%d" % (abspath(options.directory), getpid())
recreate_dir(tmp_dir)
results = test(tmp_dir, options, args[:-1], args[-1])
rmtree(tmp_dir)
if options.xml:
print_result_as_xml(results)
else:
print_result_as_text(results)
main(sys.argv)
|