Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2 | """Zephyr Sanity Tests |
| 3 | |
| 4 | This script scans for the set of unit test applications in the git |
| 5 | repository and attempts to execute them. By default, it tries to |
| 6 | build each test case on one platform per architecture, using a precedence |
| 7 | list defined in an archtecture configuration file, and if possible |
| 8 | run the tests in the QEMU emulator. |
| 9 | |
| 10 | Test cases are detected by the presence of a 'testcase.ini' file in |
| 11 | the application's project directory. This file may contain one or |
| 12 | more blocks, each identifying a test scenario. The title of the block |
| 13 | is a name for the test case, which only needs to be unique for the |
| 14 | test cases specified in that testcase.ini file. The full canonical |
| 15 | name for each test case is <path to test case under samples/>/<block>. |
| 16 | |
| 17 | Each testcase.ini block can define the following key/value pairs: |
| 18 | |
| 19 | tags = <list of tags> (required) |
| 20 | A set of string tags for the testcase. Usually pertains to |
| 21 | functional domains but can be anything. Command line invocations |
| 22 | of this script can filter the set of tests to run based on tag. |
| 23 | |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 24 | skip = <True|False> (default False) |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 25 | skip testcase unconditionally. This can be used for broken tests. |
| 26 | |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 27 | slow = <True|False> (default False) |
| 28 | Don't run this test case unless --enable-slow was passed in on the |
| 29 | command line. Intended for time-consuming test cases that are only |
| 30 | run under certain circumstances, like daily builds. These test cases |
| 31 | are still compiled. |
| 32 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 33 | extra_args = <list of extra arguments> |
| 34 | Extra arguments to pass to Make when building or running the |
| 35 | test case. |
| 36 | |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 37 | build_only = <True|False> (default False) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 38 | If true, don't try to run the test under QEMU even if the |
| 39 | selected platform supports it. |
| 40 | |
| 41 | timeout = <number of seconds> |
| 42 | Length of time to run test in QEMU before automatically killing it. |
| 43 | Default to 60 seconds. |
| 44 | |
| 45 | arch_whitelist = <list of arches, such as x86, arm, arc> |
| 46 | Set of architectures that this test case should only be run for. |
| 47 | |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 48 | arch_exclude = <list of arches, such as x86, arm, arc> |
| 49 | Set of architectures that this test case should not run on. |
| 50 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 51 | platform_whitelist = <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 52 | Set of platforms that this test case should only be run for. |
| 53 | |
| 54 | platform_exclude = <list of platforms> |
| 55 | Set of platforms that this test case should not run on. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 56 | |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 57 | extra_sections = <list of extra binary sections> |
| 58 | When computing sizes, sanitycheck will report errors if it finds |
| 59 | extra, unexpected sections in the Zephyr binary unless they are named |
| 60 | here. They will not be included in the size calculation. |
| 61 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 62 | filter = <expression> |
| 63 | Filter whether the testcase should be run by evaluating an expression |
| 64 | against an environment containing the following values: |
| 65 | |
| 66 | { ARCH : <architecture>, |
| 67 | PLATFORM : <platform>, |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 68 | <all CONFIG_* key/value pairs in the test's generated defconfig>, |
| 69 | *<env>: any environment variable available |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | The grammar for the expression language is as follows: |
| 73 | |
| 74 | expression ::= expression "and" expression |
| 75 | | expression "or" expression |
| 76 | | "not" expression |
| 77 | | "(" expression ")" |
| 78 | | symbol "==" constant |
| 79 | | symbol "!=" constant |
| 80 | | symbol "<" number |
| 81 | | symbol ">" number |
| 82 | | symbol ">=" number |
| 83 | | symbol "<=" number |
| 84 | | symbol "in" list |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 85 | | symbol ":" string |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 86 | | symbol |
| 87 | |
| 88 | list ::= "[" list_contents "]" |
| 89 | |
| 90 | list_contents ::= constant |
| 91 | | list_contents "," constant |
| 92 | |
| 93 | constant ::= number |
| 94 | | string |
| 95 | |
| 96 | |
| 97 | For the case where expression ::= symbol, it evaluates to true |
| 98 | if the symbol is defined to a non-empty string. |
| 99 | |
| 100 | Operator precedence, starting from lowest to highest: |
| 101 | |
| 102 | or (left associative) |
| 103 | and (left associative) |
| 104 | not (right associative) |
| 105 | all comparison operators (non-associative) |
| 106 | |
| 107 | arch_whitelist, arch_exclude, platform_whitelist, platform_exclude |
| 108 | are all syntactic sugar for these expressions. For instance |
| 109 | |
| 110 | arch_exclude = x86 arc |
| 111 | |
| 112 | Is the same as: |
| 113 | |
| 114 | filter = not ARCH in ["x86", "arc"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 115 | |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 116 | The ':' operator compiles the string argument as a regular expression, |
| 117 | and then returns a true value only if the symbol's value in the environment |
| 118 | matches. For example, if CONFIG_SOC="quark_se" then |
| 119 | |
| 120 | filter = CONFIG_SOC : "quark.*" |
| 121 | |
| 122 | Would match it. |
| 123 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 124 | Architectures and platforms are defined in an archtecture configuration |
| 125 | file which are stored by default in scripts/sanity_chk/arches/. These |
| 126 | each define an [arch] block with the following key/value pairs: |
| 127 | |
| 128 | name = <arch name> |
| 129 | The name of the arch. Example: x86 |
| 130 | |
| 131 | platforms = <list of supported platforms in order of precedence> |
| 132 | List of supported platforms for this arch. The ordering here |
| 133 | is used to select a default platform to build for that arch. |
| 134 | |
| 135 | For every platform defined, there must be a corresponding block for it |
| 136 | in the arch configuration file. This block can be empty if there are |
| 137 | no special definitions for that arch. Options are: |
| 138 | |
| 139 | qemu_support = <True|False> (default False) |
| 140 | Indicates whether binaries for this platform can run under QEMU |
| 141 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 142 | The set of test cases that actually run depends on directives in the |
Paul Sokolovsky | cb2ae52 | 2017-03-13 18:05:11 +0300 | [diff] [blame] | 143 | testcase and architecture .ini file and options passed in on the command |
| 144 | line. If there is any confusion, running with -v or --discard-report |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 145 | can help show why particular test cases were skipped. |
| 146 | |
| 147 | Metrics (such as pass/fail state and binary size) for the last code |
| 148 | release are stored in scripts/sanity_chk/sanity_last_release.csv. |
| 149 | To update this, pass the --all --release options. |
| 150 | |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 151 | To load arguments from a file, write '+' before the file name, e.g., |
| 152 | +file_name. File content must be one or more valid arguments separated by |
| 153 | line break instead of white spaces. |
| 154 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 155 | Most everyday users will run with no arguments. |
| 156 | """ |
| 157 | |
| 158 | import argparse |
| 159 | import os |
| 160 | import sys |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 161 | import configparser |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 162 | import re |
| 163 | import tempfile |
| 164 | import subprocess |
| 165 | import multiprocessing |
| 166 | import select |
| 167 | import shutil |
| 168 | import signal |
| 169 | import threading |
| 170 | import time |
| 171 | import csv |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 172 | import glob |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 173 | import concurrent |
| 174 | import concurrent.futures |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 175 | |
| 176 | if "ZEPHYR_BASE" not in os.environ: |
Anas Nashif | 427cdd3 | 2015-08-06 07:25:42 -0400 | [diff] [blame] | 177 | sys.stderr.write("$ZEPHYR_BASE environment variable undefined.\n") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 178 | exit(1) |
| 179 | ZEPHYR_BASE = os.environ["ZEPHYR_BASE"] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 180 | |
| 181 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/")) |
| 182 | |
| 183 | import expr_parser |
| 184 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 185 | VERBOSE = 0 |
| 186 | LAST_SANITY = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 187 | "last_sanity.csv") |
| 188 | RELEASE_DATA = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 189 | "sanity_last_release.csv") |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 190 | CPU_COUNTS = multiprocessing.cpu_count() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 191 | |
| 192 | if os.isatty(sys.stdout.fileno()): |
| 193 | TERMINAL = True |
| 194 | COLOR_NORMAL = '\033[0m' |
| 195 | COLOR_RED = '\033[91m' |
| 196 | COLOR_GREEN = '\033[92m' |
| 197 | COLOR_YELLOW = '\033[93m' |
| 198 | else: |
| 199 | TERMINAL = False |
| 200 | COLOR_NORMAL = "" |
| 201 | COLOR_RED = "" |
| 202 | COLOR_GREEN = "" |
| 203 | COLOR_YELLOW = "" |
| 204 | |
| 205 | class SanityCheckException(Exception): |
| 206 | pass |
| 207 | |
| 208 | class SanityRuntimeError(SanityCheckException): |
| 209 | pass |
| 210 | |
| 211 | class ConfigurationError(SanityCheckException): |
| 212 | def __init__(self, cfile, message): |
| 213 | self.cfile = cfile |
| 214 | self.message = message |
| 215 | |
| 216 | def __str__(self): |
| 217 | return repr(self.cfile + ": " + self.message) |
| 218 | |
| 219 | class MakeError(SanityCheckException): |
| 220 | pass |
| 221 | |
| 222 | class BuildError(MakeError): |
| 223 | pass |
| 224 | |
| 225 | class ExecutionError(MakeError): |
| 226 | pass |
| 227 | |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 228 | log_file = None |
| 229 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 230 | # Debug Functions |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 231 | def info(what): |
| 232 | sys.stdout.write(what + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 233 | if log_file: |
| 234 | log_file.write(what + "\n") |
| 235 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 236 | |
| 237 | def error(what): |
| 238 | sys.stderr.write(COLOR_RED + what + COLOR_NORMAL + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 239 | if log_file: |
| 240 | log_file(what + "\n") |
| 241 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 242 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 243 | def debug(what): |
| 244 | if VERBOSE >= 1: |
| 245 | info(what) |
| 246 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 247 | def verbose(what): |
| 248 | if VERBOSE >= 2: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 249 | info(what) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 250 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 251 | class Handler: |
| 252 | RUN_PASSED = "PROJECT EXECUTION SUCCESSFUL" |
| 253 | RUN_FAILED = "PROJECT EXECUTION FAILED" |
| 254 | def __init__(self, name, outdir, log_fn, timeout, unit=False): |
| 255 | """Constructor |
| 256 | |
| 257 | @param name Arbitrary name of the created thread |
| 258 | @param outdir Working directory, should be where qemu.pid gets created |
| 259 | by kbuild |
| 260 | @param log_fn Absolute path to write out QEMU's log data |
| 261 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 262 | the given number of seconds |
| 263 | """ |
| 264 | self.lock = threading.Lock() |
| 265 | self.state = "waiting" |
| 266 | self.metrics = {} |
| 267 | self.metrics["qemu_time"] = 0 |
| 268 | self.metrics["ram_size"] = 0 |
| 269 | self.metrics["rom_size"] = 0 |
| 270 | self.unit = unit |
| 271 | |
| 272 | def set_state(self, state, metrics): |
| 273 | self.lock.acquire() |
| 274 | self.state = state |
| 275 | self.metrics.update(metrics) |
| 276 | self.lock.release() |
| 277 | |
| 278 | def get_state(self): |
| 279 | self.lock.acquire() |
| 280 | ret = (self.state, self.metrics) |
| 281 | self.lock.release() |
| 282 | return ret |
| 283 | |
| 284 | class UnitHandler(Handler): |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 285 | def __init__(self, name, sourcedir, outdir, run_log, valgrind_log, timeout): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 286 | """Constructor |
| 287 | |
| 288 | @param name Arbitrary name of the created thread |
| 289 | @param outdir Working directory containing the test binary |
| 290 | @param run_log Absolute path to runtime logs |
| 291 | @param valgrind Absolute path to valgrind's log |
| 292 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 293 | the given number of seconds |
| 294 | """ |
| 295 | super().__init__(name, outdir, run_log, timeout, True) |
| 296 | |
| 297 | self.timeout = timeout |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 298 | self.sourcedir = sourcedir |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 299 | self.outdir = outdir |
| 300 | self.run_log = run_log |
| 301 | self.valgrind_log = valgrind_log |
| 302 | self.returncode = 0 |
| 303 | self.set_state("running", {}) |
| 304 | |
| 305 | def handle(self): |
| 306 | out_state = "failed" |
| 307 | |
| 308 | with open(self.run_log, "wt") as rl, open(self.valgrind_log, "wt") as vl: |
| 309 | try: |
| 310 | binary = os.path.join(self.outdir, "testbinary") |
| 311 | command = [binary] |
| 312 | if shutil.which("valgrind"): |
| 313 | command = ["valgrind", "--error-exitcode=2", |
| 314 | "--leak-check=full"] + command |
| 315 | returncode = subprocess.call(command, timeout=self.timeout, |
| 316 | stdout=rl, stderr=vl) |
| 317 | self.returncode = returncode |
| 318 | if returncode != 0: |
| 319 | if self.returncode == 1: |
| 320 | out_state = "failed" |
| 321 | else: |
| 322 | out_state = "failed valgrind" |
| 323 | else: |
| 324 | out_state = "passed" |
| 325 | except subprocess.TimeoutExpired: |
| 326 | out_state = "timeout" |
| 327 | self.returncode = 1 |
| 328 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 329 | returncode = subprocess.call(["GCOV_PREFIX=" + self.outdir, "gcov", self.sourcedir, "-s", self.outdir], shell=True) |
| 330 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 331 | self.set_state(out_state, {}) |
| 332 | |
| 333 | class QEMUHandler(Handler): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 334 | """Spawns a thread to monitor QEMU output from pipes |
| 335 | |
| 336 | We pass QEMU_PIPE to 'make qemu' and monitor the pipes for output. |
| 337 | We need to do this as once qemu starts, it runs forever until killed. |
| 338 | Test cases emit special messages to the console as they run, we check |
| 339 | for these to collect whether the test passed or failed. |
| 340 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 341 | |
| 342 | @staticmethod |
| 343 | def _thread(handler, timeout, outdir, logfile, fifo_fn, pid_fn, results): |
| 344 | fifo_in = fifo_fn + ".in" |
| 345 | fifo_out = fifo_fn + ".out" |
| 346 | |
| 347 | # These in/out nodes are named from QEMU's perspective, not ours |
| 348 | if os.path.exists(fifo_in): |
| 349 | os.unlink(fifo_in) |
| 350 | os.mkfifo(fifo_in) |
| 351 | if os.path.exists(fifo_out): |
| 352 | os.unlink(fifo_out) |
| 353 | os.mkfifo(fifo_out) |
| 354 | |
| 355 | # We don't do anything with out_fp but we need to open it for |
| 356 | # writing so that QEMU doesn't block, due to the way pipes work |
| 357 | out_fp = open(fifo_in, "wb") |
| 358 | # Disable internal buffering, we don't |
| 359 | # want read() or poll() to ever block if there is data in there |
| 360 | in_fp = open(fifo_out, "rb", buffering=0) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 361 | log_out_fp = open(logfile, "wt") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 362 | |
| 363 | start_time = time.time() |
| 364 | timeout_time = start_time + timeout |
| 365 | p = select.poll() |
| 366 | p.register(in_fp, select.POLLIN) |
| 367 | |
| 368 | metrics = {} |
| 369 | line = "" |
| 370 | while True: |
| 371 | this_timeout = int((timeout_time - time.time()) * 1000) |
| 372 | if this_timeout < 0 or not p.poll(this_timeout): |
| 373 | out_state = "timeout" |
| 374 | break |
| 375 | |
Genaro Saucedo Tejada | 2968b36 | 2016-08-09 15:11:53 -0500 | [diff] [blame] | 376 | try: |
| 377 | c = in_fp.read(1).decode("utf-8") |
| 378 | except UnicodeDecodeError: |
| 379 | # Test is writing something weird, fail |
| 380 | out_state = "unexpected byte" |
| 381 | break |
| 382 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 383 | if c == "": |
| 384 | # EOF, this shouldn't happen unless QEMU crashes |
| 385 | out_state = "unexpected eof" |
| 386 | break |
| 387 | line = line + c |
| 388 | if c != "\n": |
| 389 | continue |
| 390 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 391 | # line contains a full line of data output from QEMU |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 392 | log_out_fp.write(line) |
| 393 | log_out_fp.flush() |
| 394 | line = line.strip() |
| 395 | verbose("QEMU: %s" % line) |
| 396 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 397 | if line == handler.RUN_PASSED: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 398 | out_state = "passed" |
| 399 | break |
| 400 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 401 | if line == handler.RUN_FAILED: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 402 | out_state = "failed" |
| 403 | break |
| 404 | |
| 405 | # TODO: Add support for getting numerical performance data |
| 406 | # from test cases. Will involve extending test case reporting |
| 407 | # APIs. Add whatever gets reported to the metrics dictionary |
| 408 | line = "" |
| 409 | |
| 410 | metrics["qemu_time"] = time.time() - start_time |
| 411 | verbose("QEMU complete (%s) after %f seconds" % |
| 412 | (out_state, metrics["qemu_time"])) |
| 413 | handler.set_state(out_state, metrics) |
| 414 | |
| 415 | log_out_fp.close() |
| 416 | out_fp.close() |
| 417 | in_fp.close() |
| 418 | |
| 419 | pid = int(open(pid_fn).read()) |
| 420 | os.unlink(pid_fn) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 421 | try: |
| 422 | os.kill(pid, signal.SIGTERM) |
| 423 | except ProcessLookupError: |
| 424 | # Oh well, as long as it's dead! User probably sent Ctrl-C |
| 425 | pass |
| 426 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 427 | os.unlink(fifo_in) |
| 428 | os.unlink(fifo_out) |
| 429 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 430 | def __init__(self, name, outdir, log_fn, timeout): |
| 431 | """Constructor |
| 432 | |
| 433 | @param name Arbitrary name of the created thread |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 434 | @param outdir Working directory, should be where qemu.pid gets created |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 435 | by kbuild |
| 436 | @param log_fn Absolute path to write out QEMU's log data |
| 437 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 438 | the given number of seconds |
| 439 | """ |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 440 | super().__init__(name, outdir, log_fn, timeout) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 441 | self.results = {} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 442 | |
| 443 | # We pass this to QEMU which looks for fifos with .in and .out |
| 444 | # suffixes. |
| 445 | self.fifo_fn = os.path.join(outdir, "qemu-fifo") |
| 446 | |
| 447 | self.pid_fn = os.path.join(outdir, "qemu.pid") |
| 448 | if os.path.exists(self.pid_fn): |
| 449 | os.unlink(self.pid_fn) |
| 450 | |
| 451 | self.log_fn = log_fn |
| 452 | self.thread = threading.Thread(name=name, target=QEMUHandler._thread, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 453 | args=(self, timeout, outdir, |
| 454 | self.log_fn, self.fifo_fn, |
| 455 | self.pid_fn, self.results)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 456 | self.thread.daemon = True |
| 457 | verbose("Spawning QEMU process for %s" % name) |
| 458 | self.thread.start() |
| 459 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 460 | def get_fifo(self): |
| 461 | return self.fifo_fn |
| 462 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 463 | class SizeCalculator: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 464 | |
| 465 | alloc_sections = ["bss", "noinit"] |
Andrew Boie | 18ba153 | 2017-01-17 13:47:06 -0800 | [diff] [blame] | 466 | rw_sections = ["datas", "initlevel", "_k_task_list", "_k_event_list", |
Allan Stephens | 3f5c74c | 2016-11-01 14:37:15 -0500 | [diff] [blame] | 467 | "_k_memory_pool", "exceptions", "initshell", |
| 468 | "_static_thread_area", "_k_timer_area", |
| 469 | "_k_mem_slab_area", "_k_mem_pool_area", |
| 470 | "_k_sem_area", "_k_mutex_area", "_k_alert_area", |
| 471 | "_k_fifo_area", "_k_lifo_area", "_k_stack_area", |
Tomasz Bursztyka | b56c4df | 2016-08-18 14:07:22 +0200 | [diff] [blame] | 472 | "_k_msgq_area", "_k_mbox_area", "_k_pipe_area", |
Tomasz Bursztyka | e38a9e8 | 2017-03-08 09:30:03 +0100 | [diff] [blame] | 473 | "net_if", "net_if_event", "net_stack", "net_l2_data", |
Anas Nashif | 6d72e63 | 2017-02-27 19:49:03 -0500 | [diff] [blame] | 474 | "_k_queue_area"] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 475 | # These get copied into RAM only on non-XIP |
Andrew Boie | 3b93021 | 2016-06-07 13:11:45 -0700 | [diff] [blame] | 476 | ro_sections = ["text", "ctors", "init_array", "reset", |
Anas Nashif | bfcdfaf | 2016-12-15 10:02:14 -0500 | [diff] [blame] | 477 | "rodata", "devconfig", "net_l2", "vector"] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 478 | |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 479 | def __init__(self, filename, extra_sections): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 480 | """Constructor |
| 481 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 482 | @param filename Path to the output binary |
| 483 | The <filename> is parsed by objdump to determine section sizes |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 484 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 485 | # Make sure this is an ELF binary |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 486 | with open(filename, "rb") as f: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 487 | magic = f.read(4) |
| 488 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 489 | if (magic != b'\x7fELF'): |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 490 | raise SanityRuntimeError("%s is not an ELF binary" % filename) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 491 | |
| 492 | # Search for CONFIG_XIP in the ELF's list of symbols using NM and AWK. |
| 493 | # GREP can not be used as it returns an error if the symbol is not found. |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 494 | is_xip_command = "nm " + filename + " | awk '/CONFIG_XIP/ { print $3 }'" |
Andrew Boie | 8f0211d | 2016-03-02 20:40:29 -0800 | [diff] [blame] | 495 | is_xip_output = subprocess.check_output(is_xip_command, shell=True, |
| 496 | stderr=subprocess.STDOUT).decode("utf-8").strip() |
| 497 | if is_xip_output.endswith("no symbols"): |
| 498 | raise SanityRuntimeError("%s has no symbol information" % filename) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 499 | self.is_xip = (len(is_xip_output) != 0) |
| 500 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 501 | self.filename = filename |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 502 | self.sections = [] |
| 503 | self.rom_size = 0 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 504 | self.ram_size = 0 |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 505 | self.extra_sections = extra_sections |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 506 | |
| 507 | self._calculate_sizes() |
| 508 | |
| 509 | def get_ram_size(self): |
| 510 | """Get the amount of RAM the application will use up on the device |
| 511 | |
| 512 | @return amount of RAM, in bytes |
| 513 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 514 | return self.ram_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 515 | |
| 516 | def get_rom_size(self): |
| 517 | """Get the size of the data that this application uses on device's flash |
| 518 | |
| 519 | @return amount of ROM, in bytes |
| 520 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 521 | return self.rom_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 522 | |
| 523 | def unrecognized_sections(self): |
| 524 | """Get a list of sections inside the binary that weren't recognized |
| 525 | |
| 526 | @return list of unrecogized section names |
| 527 | """ |
| 528 | slist = [] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 529 | for v in self.sections: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 530 | if not v["recognized"]: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 531 | slist.append(v["name"]) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 532 | return slist |
| 533 | |
| 534 | def _calculate_sizes(self): |
| 535 | """ Calculate RAM and ROM usage by section """ |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 536 | objdump_command = "objdump -h " + self.filename |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 537 | objdump_output = subprocess.check_output(objdump_command, |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 538 | shell=True).decode("utf-8").splitlines() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 539 | |
| 540 | for line in objdump_output: |
| 541 | words = line.split() |
| 542 | |
| 543 | if (len(words) == 0): # Skip lines that are too short |
| 544 | continue |
| 545 | |
| 546 | index = words[0] |
| 547 | if (not index[0].isdigit()): # Skip lines that do not start |
| 548 | continue # with a digit |
| 549 | |
| 550 | name = words[1] # Skip lines with section names |
| 551 | if (name[0] == '.'): # starting with '.' |
| 552 | continue |
| 553 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 554 | # TODO this doesn't actually reflect the size in flash or RAM as |
| 555 | # it doesn't include linker-imposed padding between sections. |
| 556 | # It is close though. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 557 | size = int(words[2], 16) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 558 | if size == 0: |
| 559 | continue |
| 560 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 561 | load_addr = int(words[4], 16) |
| 562 | virt_addr = int(words[3], 16) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 563 | |
| 564 | # Add section to memory use totals (for both non-XIP and XIP scenarios) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 565 | # Unrecognized section names are not included in the calculations. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 566 | recognized = True |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 567 | if name in SizeCalculator.alloc_sections: |
| 568 | self.ram_size += size |
| 569 | stype = "alloc" |
| 570 | elif name in SizeCalculator.rw_sections: |
| 571 | self.ram_size += size |
| 572 | self.rom_size += size |
| 573 | stype = "rw" |
| 574 | elif name in SizeCalculator.ro_sections: |
| 575 | self.rom_size += size |
| 576 | if not self.is_xip: |
| 577 | self.ram_size += size |
| 578 | stype = "ro" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 579 | else: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 580 | stype = "unknown" |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 581 | if name not in self.extra_sections: |
| 582 | recognized = False |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 583 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 584 | self.sections.append({"name" : name, "load_addr" : load_addr, |
| 585 | "size" : size, "virt_addr" : virt_addr, |
Andy Ross | 8d801a5 | 2016-10-04 11:48:21 -0700 | [diff] [blame] | 586 | "type" : stype, "recognized" : recognized}) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 587 | |
| 588 | |
| 589 | class MakeGoal: |
| 590 | """Metadata class representing one of the sub-makes called by MakeGenerator |
| 591 | |
| 592 | MakeGenerator returns a dictionary of these which can then be associdated |
| 593 | with TestInstances to get a complete picture of what happened during a test. |
| 594 | MakeGenerator is used for tasks outside of building tests (such as |
| 595 | defconfigs) which is why MakeGoal is a separate class from TestInstance. |
| 596 | """ |
| 597 | def __init__(self, name, text, qemu, make_log, build_log, run_log, |
| 598 | qemu_log): |
| 599 | self.name = name |
| 600 | self.text = text |
| 601 | self.qemu = qemu |
| 602 | self.make_log = make_log |
| 603 | self.build_log = build_log |
| 604 | self.run_log = run_log |
| 605 | self.qemu_log = qemu_log |
| 606 | self.make_state = "waiting" |
| 607 | self.failed = False |
| 608 | self.finished = False |
| 609 | self.reason = None |
| 610 | self.metrics = {} |
| 611 | |
| 612 | def get_error_log(self): |
| 613 | if self.make_state == "waiting": |
| 614 | # Shouldn't ever see this; breakage in the main Makefile itself. |
| 615 | return self.make_log |
| 616 | elif self.make_state == "building": |
| 617 | # Failure when calling the sub-make to build the code |
| 618 | return self.build_log |
| 619 | elif self.make_state == "running": |
| 620 | # Failure in sub-make for "make qemu", qemu probably failed to start |
| 621 | return self.run_log |
| 622 | elif self.make_state == "finished": |
| 623 | # QEMU finished, but timed out or otherwise wasn't successful |
| 624 | return self.qemu_log |
| 625 | |
| 626 | def fail(self, reason): |
| 627 | self.failed = True |
| 628 | self.finished = True |
| 629 | self.reason = reason |
| 630 | |
| 631 | def success(self): |
| 632 | self.finished = True |
| 633 | |
| 634 | def __str__(self): |
| 635 | if self.finished: |
| 636 | if self.failed: |
| 637 | return "[%s] failed (%s: see %s)" % (self.name, self.reason, |
| 638 | self.get_error_log()) |
| 639 | else: |
| 640 | return "[%s] passed" % self.name |
| 641 | else: |
| 642 | return "[%s] in progress (%s)" % (self.name, self.make_state) |
| 643 | |
| 644 | |
| 645 | class MakeGenerator: |
| 646 | """Generates a Makefile which just calls a bunch of sub-make sessions |
| 647 | |
| 648 | In any given test suite we may need to build dozens if not hundreds of |
| 649 | test cases. The cleanest way to parallelize this is to just let Make |
| 650 | do the parallelization, sharing the jobserver among all the different |
| 651 | sub-make targets. |
| 652 | """ |
| 653 | |
| 654 | GOAL_HEADER_TMPL = """.PHONY: {goal} |
| 655 | {goal}: |
| 656 | """ |
| 657 | |
| 658 | MAKE_RULE_TMPL = """\t@echo sanity_test_{phase} {goal} >&2 |
Andrew Boie | f7a6e28 | 2017-02-02 13:04:57 -0800 | [diff] [blame] | 659 | \t$(MAKE) -C {directory} O={outdir} V={verb} EXTRA_CFLAGS="-Werror {cflags}" EXTRA_ASMFLAGS=-Wa,--fatal-warnings EXTRA_LDFLAGS=--fatal-warnings {args} >{logfile} 2>&1 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 660 | """ |
| 661 | |
| 662 | GOAL_FOOTER_TMPL = "\t@echo sanity_test_finished {goal} >&2\n\n" |
| 663 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 664 | re_make = re.compile("sanity_test_([A-Za-z0-9]+) (.+)|$|make[:] \*\*\* \[(.+:.+: )?(.+)\] Error.+$") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 665 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 666 | def __init__(self, base_outdir, asserts=False, deprecations=False, ccache=0): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 667 | """MakeGenerator constructor |
| 668 | |
| 669 | @param base_outdir Intended to be the base out directory. A make.log |
| 670 | file will be created here which contains the output of the |
| 671 | top-level Make session, as well as the dynamic control Makefile |
| 672 | @param verbose If true, pass V=1 to all the sub-makes which greatly |
| 673 | increases their verbosity |
| 674 | """ |
| 675 | self.goals = {} |
| 676 | if not os.path.exists(base_outdir): |
| 677 | os.makedirs(base_outdir) |
| 678 | self.logfile = os.path.join(base_outdir, "make.log") |
| 679 | self.makefile = os.path.join(base_outdir, "Makefile") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 680 | self.asserts = asserts |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 681 | self.deprecations = deprecations |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 682 | self.ccache = ccache |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 683 | |
| 684 | def _get_rule_header(self, name): |
| 685 | return MakeGenerator.GOAL_HEADER_TMPL.format(goal=name) |
| 686 | |
| 687 | def _get_sub_make(self, name, phase, workdir, outdir, logfile, args): |
| 688 | verb = "1" if VERBOSE else "0" |
| 689 | args = " ".join(args) |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 690 | |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 691 | if self.asserts: |
| 692 | cflags="-DCONFIG_ASSERT=1 -D__ASSERT_ON=2" |
| 693 | else: |
| 694 | cflags="" |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 695 | |
| 696 | if self.deprecations: |
| 697 | cflags = cflags + " -Wno-deprecated-declarations" |
Andrew Boie | f7a6e28 | 2017-02-02 13:04:57 -0800 | [diff] [blame] | 698 | |
| 699 | if self.ccache: |
| 700 | args = args + " USE_CCACHE=1" |
| 701 | |
| 702 | return MakeGenerator.MAKE_RULE_TMPL.format(phase=phase, goal=name, |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 703 | outdir=outdir, cflags=cflags, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 704 | directory=workdir, verb=verb, |
| 705 | args=args, logfile=logfile) |
| 706 | |
| 707 | def _get_rule_footer(self, name): |
| 708 | return MakeGenerator.GOAL_FOOTER_TMPL.format(goal=name) |
| 709 | |
| 710 | def _add_goal(self, outdir): |
| 711 | if not os.path.exists(outdir): |
| 712 | os.makedirs(outdir) |
| 713 | |
| 714 | def add_build_goal(self, name, directory, outdir, args): |
| 715 | """Add a goal to invoke a Kbuild session |
| 716 | |
| 717 | @param name A unique string name for this build goal. The results |
| 718 | dictionary returned by execute() will be keyed by this name. |
| 719 | @param directory Absolute path to working directory, will be passed |
| 720 | to make -C |
| 721 | @param outdir Absolute path to output directory, will be passed to |
| 722 | Kbuild via -O=<path> |
| 723 | @param args Extra command line arguments to pass to 'make', typically |
| 724 | environment variables or specific Make goals |
| 725 | """ |
| 726 | self._add_goal(outdir) |
| 727 | build_logfile = os.path.join(outdir, "build.log") |
| 728 | text = (self._get_rule_header(name) + |
| 729 | self._get_sub_make(name, "building", directory, |
| 730 | outdir, build_logfile, args) + |
| 731 | self._get_rule_footer(name)) |
| 732 | self.goals[name] = MakeGoal(name, text, None, self.logfile, build_logfile, |
| 733 | None, None) |
| 734 | |
| 735 | def add_qemu_goal(self, name, directory, outdir, args, timeout=30): |
| 736 | """Add a goal to build a Zephyr project and then run it under QEMU |
| 737 | |
| 738 | The generated make goal invokes Make twice, the first time it will |
| 739 | build the default goal, and the second will invoke the 'qemu' goal. |
| 740 | The output of the QEMU session will be monitored, and terminated |
| 741 | either upon pass/fail result of the test program, or the timeout |
| 742 | is reached. |
| 743 | |
| 744 | @param name A unique string name for this build goal. The results |
| 745 | dictionary returned by execute() will be keyed by this name. |
| 746 | @param directory Absolute path to working directory, will be passed |
| 747 | to make -C |
| 748 | @param outdir Absolute path to output directory, will be passed to |
| 749 | Kbuild via -O=<path> |
| 750 | @param args Extra command line arguments to pass to 'make', typically |
| 751 | environment variables. Do not pass specific Make goals here. |
| 752 | @param timeout Maximum length of time QEMU session should be allowed |
| 753 | to run before automatically killing it. Default is 30 seconds. |
| 754 | """ |
| 755 | |
| 756 | self._add_goal(outdir) |
| 757 | build_logfile = os.path.join(outdir, "build.log") |
| 758 | run_logfile = os.path.join(outdir, "run.log") |
| 759 | qemu_logfile = os.path.join(outdir, "qemu.log") |
| 760 | |
| 761 | q = QEMUHandler(name, outdir, qemu_logfile, timeout) |
| 762 | args.append("QEMU_PIPE=%s" % q.get_fifo()) |
| 763 | text = (self._get_rule_header(name) + |
| 764 | self._get_sub_make(name, "building", directory, |
| 765 | outdir, build_logfile, args) + |
| 766 | self._get_sub_make(name, "running", directory, |
| 767 | outdir, run_logfile, |
Mazen NEIFER | 7f04637 | 2017-01-31 12:41:33 +0100 | [diff] [blame] | 768 | args + ["run"]) + |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 769 | self._get_rule_footer(name)) |
| 770 | self.goals[name] = MakeGoal(name, text, q, self.logfile, build_logfile, |
| 771 | run_logfile, qemu_logfile) |
| 772 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 773 | def add_unit_goal(self, name, directory, outdir, args, timeout=30, coverage=False): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 774 | self._add_goal(outdir) |
| 775 | build_logfile = os.path.join(outdir, "build.log") |
| 776 | run_logfile = os.path.join(outdir, "run.log") |
| 777 | qemu_logfile = os.path.join(outdir, "qemu.log") |
| 778 | valgrind_logfile = os.path.join(outdir, "valgrind.log") |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 779 | if coverage: |
| 780 | args += ["COVERAGE=1"] |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 781 | |
| 782 | # we handle running in the UnitHandler class |
| 783 | text = (self._get_rule_header(name) + |
| 784 | self._get_sub_make(name, "building", directory, |
| 785 | outdir, build_logfile, args) + |
| 786 | self._get_rule_footer(name)) |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 787 | q = UnitHandler(name, directory, outdir, run_logfile, valgrind_logfile, timeout) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 788 | self.goals[name] = MakeGoal(name, text, q, self.logfile, build_logfile, |
| 789 | run_logfile, valgrind_logfile) |
| 790 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 791 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 792 | def add_test_instance(self, ti, build_only=False, enable_slow=False, coverage=False, |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 793 | extra_args=[]): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 794 | """Add a goal to build/test a TestInstance object |
| 795 | |
| 796 | @param ti TestInstance object to build. The status dictionary returned |
| 797 | by execute() will be keyed by its .name field. |
| 798 | """ |
| 799 | args = ti.test.extra_args[:] |
| 800 | args.extend(["ARCH=%s" % ti.platform.arch.name, |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 801 | "BOARD=%s" % ti.platform.name]) |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 802 | args.extend(extra_args) |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 803 | if (ti.platform.qemu_support and (not ti.build_only) and |
| 804 | (not build_only) and (enable_slow or not ti.test.slow)): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 805 | self.add_qemu_goal(ti.name, ti.test.code_location, ti.outdir, |
| 806 | args, ti.test.timeout) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 807 | elif ti.test.type == "unit": |
| 808 | self.add_unit_goal(ti.name, ti.test.code_location, ti.outdir, |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 809 | args, ti.test.timeout, coverage) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 810 | else: |
| 811 | self.add_build_goal(ti.name, ti.test.code_location, ti.outdir, args) |
| 812 | |
| 813 | def execute(self, callback_fn=None, context=None): |
| 814 | """Execute all the registered build goals |
| 815 | |
| 816 | @param callback_fn If not None, a callback function will be called |
| 817 | as individual goals transition between states. This function |
| 818 | should accept two parameters: a string state and an arbitrary |
| 819 | context object, supplied here |
| 820 | @param context Context object to pass to the callback function. |
| 821 | Type and semantics are specific to that callback function. |
| 822 | @return A dictionary mapping goal names to final status. |
| 823 | """ |
| 824 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 825 | with open(self.makefile, "wt") as tf, \ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 826 | open(os.devnull, "wb") as devnull, \ |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 827 | open(self.logfile, "wt") as make_log: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 828 | # Create our dynamic Makefile and execute it. |
| 829 | # Watch stderr output which is where we will keep |
| 830 | # track of build state |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 831 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 832 | tf.write(goal.text) |
| 833 | tf.write("all: %s\n" % (" ".join(self.goals.keys()))) |
| 834 | tf.flush() |
| 835 | |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 836 | cmd = ["make", "-k", "-j", str(CPU_COUNTS * 2), "-f", tf.name, "all"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 837 | p = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
| 838 | stdout=devnull) |
| 839 | |
| 840 | for line in iter(p.stderr.readline, b''): |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 841 | line = line.decode("utf-8") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 842 | make_log.write(line) |
| 843 | verbose("MAKE: " + repr(line.strip())) |
| 844 | m = MakeGenerator.re_make.match(line) |
| 845 | if not m: |
| 846 | continue |
| 847 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 848 | state, name, _, error = m.groups() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 849 | if error: |
| 850 | goal = self.goals[error] |
| 851 | else: |
| 852 | goal = self.goals[name] |
| 853 | goal.make_state = state |
| 854 | |
| 855 | |
| 856 | if error: |
Andrew Boie | 822b087 | 2017-01-10 13:32:40 -0800 | [diff] [blame] | 857 | # Sometimes QEMU will run an image and then crash out, which |
| 858 | # will cause the 'make qemu' invocation to exit with |
| 859 | # nonzero status. |
| 860 | # Need to distinguish this case from a compilation failure. |
| 861 | if goal.qemu: |
| 862 | goal.fail("qemu_crash") |
| 863 | else: |
| 864 | goal.fail("build_error") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 865 | else: |
| 866 | if state == "finished": |
| 867 | if goal.qemu: |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 868 | if goal.qemu.unit: |
| 869 | # We can't run unit tests with Make |
| 870 | goal.qemu.handle() |
| 871 | if goal.qemu.returncode == 2: |
| 872 | goal.qemu_log = goal.qemu.valgrind_log |
| 873 | elif goal.qemu.returncode: |
| 874 | goal.qemu_log = goal.qemu.run_log |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 875 | thread_status, metrics = goal.qemu.get_state() |
| 876 | goal.metrics.update(metrics) |
| 877 | if thread_status == "passed": |
| 878 | goal.success() |
| 879 | else: |
| 880 | goal.fail(thread_status) |
| 881 | else: |
| 882 | goal.success() |
| 883 | |
| 884 | if callback_fn: |
| 885 | callback_fn(context, self.goals, goal) |
| 886 | |
| 887 | p.wait() |
| 888 | return self.goals |
| 889 | |
| 890 | |
| 891 | # "list" - List of strings |
| 892 | # "list:<type>" - List of <type> |
| 893 | # "set" - Set of unordered, unique strings |
| 894 | # "set:<type>" - Set of <type> |
| 895 | # "float" - Floating point |
| 896 | # "int" - Integer |
| 897 | # "bool" - Boolean |
| 898 | # "str" - String |
| 899 | |
| 900 | # XXX Be sure to update __doc__ if you change any of this!! |
| 901 | |
| 902 | arch_valid_keys = {"name" : {"type" : "str", "required" : True}, |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 903 | "platforms" : {"type" : "list", "required" : True}, |
| 904 | "supported_toolchains" : {"type" : "list", "required" : True}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 905 | |
| 906 | platform_valid_keys = {"qemu_support" : {"type" : "bool", "default" : False}, |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 907 | "supported_toolchains" : {"type" : "list", "default" : []}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 908 | |
| 909 | testcase_valid_keys = {"tags" : {"type" : "set", "required" : True}, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 910 | "type" : {"type" : "str", "default": "integration"}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 911 | "extra_args" : {"type" : "list"}, |
| 912 | "build_only" : {"type" : "bool", "default" : False}, |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 913 | "skip" : {"type" : "bool", "default" : False}, |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 914 | "slow" : {"type" : "bool", "default" : False}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 915 | "timeout" : {"type" : "int", "default" : 60}, |
| 916 | "arch_whitelist" : {"type" : "set"}, |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 917 | "arch_exclude" : {"type" : "set"}, |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 918 | "extra_sections" : {"type" : "list", "default" : []}, |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 919 | "platform_exclude" : {"type" : "set"}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 920 | "platform_whitelist" : {"type" : "set"}, |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 921 | "filter" : {"type" : "str"}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 922 | |
| 923 | |
| 924 | class SanityConfigParser: |
| 925 | """Class to read architecture and test case .ini files with semantic checking |
| 926 | """ |
| 927 | def __init__(self, filename): |
| 928 | """Instantiate a new SanityConfigParser object |
| 929 | |
| 930 | @param filename Source .ini file to read |
| 931 | """ |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 932 | cp = configparser.SafeConfigParser() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 933 | cp.readfp(open(filename)) |
| 934 | self.filename = filename |
| 935 | self.cp = cp |
| 936 | |
| 937 | def _cast_value(self, value, typestr): |
| 938 | v = value.strip() |
| 939 | if typestr == "str": |
| 940 | return v |
| 941 | |
| 942 | elif typestr == "float": |
| 943 | return float(v) |
| 944 | |
| 945 | elif typestr == "int": |
| 946 | return int(v) |
| 947 | |
| 948 | elif typestr == "bool": |
| 949 | v = v.lower() |
| 950 | if v == "true" or v == "1": |
| 951 | return True |
| 952 | elif v == "" or v == "false" or v == "0": |
| 953 | return False |
| 954 | raise ConfigurationError(self.filename, |
| 955 | "bad value for boolean: '%s'" % value) |
| 956 | |
| 957 | elif typestr.startswith("list"): |
| 958 | vs = v.split() |
| 959 | if len(typestr) > 4 and typestr[4] == ":": |
| 960 | return [self._cast_value(vsi, typestr[5:]) for vsi in vs] |
| 961 | else: |
| 962 | return vs |
| 963 | |
| 964 | elif typestr.startswith("set"): |
| 965 | vs = v.split() |
| 966 | if len(typestr) > 3 and typestr[3] == ":": |
| 967 | return set([self._cast_value(vsi, typestr[4:]) for vsi in vs]) |
| 968 | else: |
| 969 | return set(vs) |
| 970 | |
| 971 | else: |
| 972 | raise ConfigurationError(self.filename, "unknown type '%s'" % value) |
| 973 | |
| 974 | |
| 975 | def sections(self): |
| 976 | """Get the set of sections within the .ini file |
| 977 | |
| 978 | @return a list of string section names""" |
| 979 | return self.cp.sections() |
| 980 | |
| 981 | def get_section(self, section, valid_keys): |
| 982 | """Get a dictionary representing the keys/values within a section |
| 983 | |
| 984 | @param section The section in the .ini file to retrieve data from |
| 985 | @param valid_keys A dictionary representing the intended semantics |
| 986 | for this section. Each key in this dictionary is a key that could |
| 987 | be specified, if a key is given in the .ini file which isn't in |
| 988 | here, it will generate an error. Each value in this dictionary |
| 989 | is another dictionary containing metadata: |
| 990 | |
| 991 | "default" - Default value if not given |
| 992 | "type" - Data type to convert the text value to. Simple types |
| 993 | supported are "str", "float", "int", "bool" which will get |
| 994 | converted to respective Python data types. "set" and "list" |
| 995 | may also be specified which will split the value by |
| 996 | whitespace (but keep the elements as strings). finally, |
| 997 | "list:<type>" and "set:<type>" may be given which will |
| 998 | perform a type conversion after splitting the value up. |
| 999 | "required" - If true, raise an error if not defined. If false |
| 1000 | and "default" isn't specified, a type conversion will be |
| 1001 | done on an empty string |
| 1002 | @return A dictionary containing the section key-value pairs with |
| 1003 | type conversion and default values filled in per valid_keys |
| 1004 | """ |
| 1005 | |
| 1006 | d = {} |
| 1007 | cp = self.cp |
| 1008 | |
| 1009 | if not cp.has_section(section): |
Andrew Boie | e57a1e5 | 2016-03-22 10:29:14 -0700 | [diff] [blame] | 1010 | # Just fill it with defaults |
| 1011 | cp.add_section(section) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1012 | |
| 1013 | for k, v in cp.items(section): |
| 1014 | if k not in valid_keys: |
| 1015 | raise ConfigurationError(self.filename, |
| 1016 | "Unknown config key '%s' in defintiion for '%s'" |
| 1017 | % (k, section)) |
| 1018 | d[k] = v |
| 1019 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1020 | for k, kinfo in valid_keys.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1021 | if k not in d: |
| 1022 | if "required" in kinfo: |
| 1023 | required = kinfo["required"] |
| 1024 | else: |
| 1025 | required = False |
| 1026 | |
| 1027 | if required: |
| 1028 | raise ConfigurationError(self.filename, |
| 1029 | "missing required value for '%s' in section '%s'" |
| 1030 | % (k, section)) |
| 1031 | else: |
| 1032 | if "default" in kinfo: |
| 1033 | default = kinfo["default"] |
| 1034 | else: |
| 1035 | default = self._cast_value("", kinfo["type"]) |
| 1036 | d[k] = default |
| 1037 | else: |
| 1038 | try: |
| 1039 | d[k] = self._cast_value(d[k], kinfo["type"]) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1040 | except ValueError as ve: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1041 | raise ConfigurationError(self.filename, |
| 1042 | "bad %s value '%s' for key '%s' in section '%s'" |
| 1043 | % (kinfo["type"], d[k], k, section)) |
| 1044 | |
| 1045 | return d |
| 1046 | |
| 1047 | |
| 1048 | class Platform: |
| 1049 | """Class representing metadata for a particular platform |
| 1050 | |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1051 | Maps directly to BOARD when building""" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1052 | def __init__(self, arch, name, plat_dict): |
| 1053 | """Constructor. |
| 1054 | |
| 1055 | @param arch Architecture object for this platform |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1056 | @param name String name for this platform, same as BOARD |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1057 | @param plat_dict SanityConfigParser output on the relevant section |
| 1058 | in the architecture configuration file which has lots of metadata. |
| 1059 | See the Architecture class. |
| 1060 | """ |
| 1061 | self.name = name |
| 1062 | self.qemu_support = plat_dict["qemu_support"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1063 | self.arch = arch |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1064 | self.supported_toolchains = arch.supported_toolchains |
| 1065 | if plat_dict["supported_toolchains"]: |
| 1066 | self.supported_toolchains = plat_dict["supported_toolchains"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1067 | # Gets populated in a separate step |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1068 | self.defconfig = None |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1069 | pass |
| 1070 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1071 | def __repr__(self): |
| 1072 | return "<%s on %s>" % (self.name, self.arch.name) |
| 1073 | |
| 1074 | |
| 1075 | class Architecture: |
| 1076 | """Class representing metadata for a particular architecture |
| 1077 | """ |
| 1078 | def __init__(self, cfile): |
| 1079 | """Architecture constructor |
| 1080 | |
| 1081 | @param cfile Path to Architecture configuration file, which gives |
| 1082 | info about the arch and all the platforms for it |
| 1083 | """ |
| 1084 | cp = SanityConfigParser(cfile) |
| 1085 | self.platforms = [] |
| 1086 | |
| 1087 | arch = cp.get_section("arch", arch_valid_keys) |
| 1088 | |
| 1089 | self.name = arch["name"] |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1090 | self.supported_toolchains = arch["supported_toolchains"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1091 | |
| 1092 | for plat_name in arch["platforms"]: |
| 1093 | verbose("Platform: %s" % plat_name) |
| 1094 | plat_dict = cp.get_section(plat_name, platform_valid_keys) |
| 1095 | self.platforms.append(Platform(self, plat_name, plat_dict)) |
| 1096 | |
| 1097 | def __repr__(self): |
| 1098 | return "<arch %s>" % self.name |
| 1099 | |
| 1100 | |
| 1101 | class TestCase: |
| 1102 | """Class representing a test application |
| 1103 | """ |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1104 | def __init__(self, testcase_root, workdir, name, tc_dict, inifile): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1105 | """TestCase constructor. |
| 1106 | |
| 1107 | This gets called by TestSuite as it finds and reads testcase.ini files. |
| 1108 | Multiple TestCase instances may be generated from a single testcase.ini, |
| 1109 | each one corresponds to a section within that file. |
| 1110 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1111 | We need to have a unique name for every single test case. Since |
| 1112 | a testcase.ini can define multiple tests, the canonical name for |
| 1113 | the test case is <workdir>/<name>. |
| 1114 | |
| 1115 | @param testcase_root Absolute path to the root directory where |
| 1116 | all the test cases live |
| 1117 | @param workdir Relative path to the project directory for this |
| 1118 | test application from the test_case root. |
| 1119 | @param name Name of this test case, corresponding to the section name |
| 1120 | in the test case configuration file. For many test cases that just |
| 1121 | define one test, can be anything and is usually "test". This is |
| 1122 | really only used to distinguish between different cases when |
| 1123 | the testcase.ini defines multiple tests |
| 1124 | @param tc_dict Dictionary with section values for this test case |
| 1125 | from the testcase.ini file |
| 1126 | """ |
| 1127 | self.code_location = os.path.join(testcase_root, workdir) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1128 | self.type = tc_dict["type"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1129 | self.tags = tc_dict["tags"] |
| 1130 | self.extra_args = tc_dict["extra_args"] |
| 1131 | self.arch_whitelist = tc_dict["arch_whitelist"] |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1132 | self.arch_exclude = tc_dict["arch_exclude"] |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1133 | self.skip = tc_dict["skip"] |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1134 | self.platform_exclude = tc_dict["platform_exclude"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1135 | self.platform_whitelist = tc_dict["platform_whitelist"] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1136 | self.tc_filter = tc_dict["filter"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1137 | self.timeout = tc_dict["timeout"] |
| 1138 | self.build_only = tc_dict["build_only"] |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1139 | self.slow = tc_dict["slow"] |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1140 | self.extra_sections = tc_dict["extra_sections"] |
Andrew Boie | 14ac902 | 2016-04-08 13:31:53 -0700 | [diff] [blame] | 1141 | self.path = os.path.join(os.path.basename(os.path.abspath(testcase_root)), |
| 1142 | workdir, name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1143 | self.name = self.path # for now |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1144 | self.defconfig = {} |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1145 | self.inifile = inifile |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1146 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1147 | def __repr__(self): |
| 1148 | return self.name |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | class TestInstance: |
| 1153 | """Class representing the execution of a particular TestCase on a platform |
| 1154 | |
| 1155 | @param test The TestCase object we want to build/execute |
| 1156 | @param platform Platform object that we want to build and run against |
| 1157 | @param base_outdir Base directory for all test results. The actual |
| 1158 | out directory used is <outdir>/<platform>/<test case name> |
| 1159 | """ |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1160 | def __init__(self, test, platform, base_outdir, build_only=False, |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1161 | slow=False, coverage=False): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1162 | self.test = test |
| 1163 | self.platform = platform |
| 1164 | self.name = os.path.join(platform.name, test.path) |
| 1165 | self.outdir = os.path.join(base_outdir, platform.name, test.path) |
| 1166 | self.build_only = build_only or test.build_only |
| 1167 | |
| 1168 | def calculate_sizes(self): |
| 1169 | """Get the RAM/ROM sizes of a test case. |
| 1170 | |
| 1171 | This can only be run after the instance has been executed by |
| 1172 | MakeGenerator, otherwise there won't be any binaries to measure. |
| 1173 | |
| 1174 | @return A SizeCalculator object |
| 1175 | """ |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1176 | fns = glob.glob(os.path.join(self.outdir, "*.elf")) |
Anas Nashif | f8dcad4 | 2016-10-27 18:10:08 -0400 | [diff] [blame] | 1177 | fns = [x for x in fns if not x.endswith('_prebuilt.elf')] |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1178 | if (len(fns) != 1): |
| 1179 | raise BuildError("Missing/multiple output ELF binary") |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1180 | return SizeCalculator(fns[0], self.test.extra_sections) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1181 | |
| 1182 | def __repr__(self): |
| 1183 | return "<TestCase %s on %s>" % (self.test.name, self.platform.name) |
| 1184 | |
| 1185 | |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1186 | def defconfig_cb(context, goals, goal): |
| 1187 | if not goal.failed: |
| 1188 | return |
| 1189 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1190 | |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1191 | info("%sCould not build defconfig for %s%s" % |
| 1192 | (COLOR_RED, goal.name, COLOR_NORMAL)); |
| 1193 | if INLINE_LOGS: |
| 1194 | with open(goal.get_error_log()) as fp: |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1195 | data = fp.read() |
| 1196 | sys.stdout.write(data) |
| 1197 | if log_file: |
| 1198 | log_file.write(data) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1199 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1200 | info("\tsee: " + COLOR_YELLOW + goal.get_error_log() + COLOR_NORMAL) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1201 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1202 | |
| 1203 | class TestSuite: |
Andrew Boie | 60823c2 | 2017-02-10 09:43:07 -0800 | [diff] [blame] | 1204 | config_re = re.compile('(CONFIG_[A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1205 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1206 | def __init__(self, arch_root, testcase_roots, outdir, coverage): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1207 | # Keep track of which test cases we've filtered out and why |
| 1208 | discards = {} |
| 1209 | self.arches = {} |
| 1210 | self.testcases = {} |
| 1211 | self.platforms = [] |
Andrew Boie | b391e66 | 2015-08-31 15:25:45 -0700 | [diff] [blame] | 1212 | self.outdir = os.path.abspath(outdir) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1213 | self.instances = {} |
| 1214 | self.goals = None |
| 1215 | self.discards = None |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1216 | self.coverage = coverage |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1217 | |
| 1218 | arch_root = os.path.abspath(arch_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1219 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1220 | for testcase_root in testcase_roots: |
| 1221 | testcase_root = os.path.abspath(testcase_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1222 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1223 | debug("Reading test case configuration files under %s..." % |
| 1224 | testcase_root) |
| 1225 | for dirpath, dirnames, filenames in os.walk(testcase_root, |
| 1226 | topdown=True): |
| 1227 | verbose("scanning %s" % dirpath) |
| 1228 | if "testcase.ini" in filenames: |
| 1229 | verbose("Found test case in " + dirpath) |
| 1230 | dirnames[:] = [] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1231 | ini_path = os.path.join(dirpath, "testcase.ini") |
| 1232 | cp = SanityConfigParser(ini_path) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1233 | workdir = os.path.relpath(dirpath, testcase_root) |
| 1234 | |
| 1235 | for section in cp.sections(): |
| 1236 | tc_dict = cp.get_section(section, testcase_valid_keys) |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1237 | tc = TestCase(testcase_root, workdir, section, tc_dict, |
| 1238 | ini_path) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1239 | self.testcases[tc.name] = tc |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1240 | |
| 1241 | debug("Reading architecture configuration files under %s..." % arch_root) |
| 1242 | for dirpath, dirnames, filenames in os.walk(arch_root): |
| 1243 | for filename in filenames: |
| 1244 | if filename.endswith(".ini"): |
| 1245 | fn = os.path.join(dirpath, filename) |
| 1246 | verbose("Found arch configuration " + fn) |
| 1247 | arch = Architecture(fn) |
| 1248 | self.arches[arch.name] = arch |
| 1249 | self.platforms.extend(arch.platforms) |
| 1250 | |
Andrew Boie | 05e3d7f | 2016-08-09 11:29:34 -0700 | [diff] [blame] | 1251 | # Build up a list of boards based on the presence of |
| 1252 | # boards/*/*_defconfig files. We want to make sure that the arch.ini |
| 1253 | # files are not missing any boards |
| 1254 | all_plats = [plat.name for plat in self.platforms] |
| 1255 | for dirpath, dirnames, filenames in os.walk(os.path.join(ZEPHYR_BASE, |
| 1256 | "boards")): |
| 1257 | for filename in filenames: |
| 1258 | if filename.endswith("_defconfig"): |
| 1259 | board_name = filename.replace("_defconfig", "") |
| 1260 | if board_name not in all_plats: |
| 1261 | error("Platform '%s' not specified in any arch .ini file and will not be tested" |
| 1262 | % board_name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1263 | self.instances = {} |
| 1264 | |
| 1265 | def get_last_failed(self): |
| 1266 | if not os.path.exists(LAST_SANITY): |
Anas Nashif | d9616b9 | 2016-11-29 13:34:53 -0500 | [diff] [blame] | 1267 | raise SanityRuntimeError("Couldn't find last sanity run.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1268 | result = [] |
| 1269 | with open(LAST_SANITY, "r") as fp: |
| 1270 | cr = csv.DictReader(fp) |
| 1271 | for row in cr: |
| 1272 | if row["passed"] == "True": |
| 1273 | continue |
| 1274 | test = row["test"] |
| 1275 | platform = row["platform"] |
| 1276 | result.append((test, platform)) |
| 1277 | return result |
| 1278 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1279 | def apply_filters(self, platform_filter, arch_filter, tag_filter, exclude_tag, |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1280 | config_filter, testcase_filter, last_failed, all_plats, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1281 | platform_limit, toolchain, extra_args, enable_ccache): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1282 | instances = [] |
| 1283 | discards = {} |
| 1284 | verbose("platform filter: " + str(platform_filter)) |
| 1285 | verbose(" arch_filter: " + str(arch_filter)) |
| 1286 | verbose(" tag_filter: " + str(tag_filter)) |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1287 | verbose(" exclude_tag: " + str(exclude_tag)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1288 | verbose(" config_filter: " + str(config_filter)) |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1289 | verbose(" enable_ccache: " + str(enable_ccache)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1290 | |
| 1291 | if last_failed: |
| 1292 | failed_tests = self.get_last_failed() |
| 1293 | |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1294 | default_platforms = False |
| 1295 | |
| 1296 | if all_plats: |
| 1297 | info("Selecting all possible platforms per test case") |
| 1298 | # When --all used, any --platform arguments ignored |
| 1299 | platform_filter = [] |
| 1300 | elif not platform_filter: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1301 | info("Selecting default platforms per test case") |
| 1302 | default_platforms = True |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1303 | |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1304 | mg = MakeGenerator(self.outdir, ccache=enable_ccache) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1305 | dlist = {} |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1306 | for tc_name, tc in self.testcases.items(): |
| 1307 | for arch_name, arch in self.arches.items(): |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1308 | instance_list = [] |
| 1309 | for plat in arch.platforms: |
| 1310 | instance = TestInstance(tc, plat, self.outdir) |
| 1311 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1312 | if (arch_name == "unit") != (tc.type == "unit"): |
| 1313 | continue |
| 1314 | |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1315 | if tc.skip: |
| 1316 | continue |
| 1317 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1318 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 1319 | continue |
| 1320 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1321 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 1322 | continue |
| 1323 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1324 | if testcase_filter and tc_name not in testcase_filter: |
| 1325 | continue |
| 1326 | |
| 1327 | if last_failed and (tc.name, plat.name) not in failed_tests: |
| 1328 | continue |
| 1329 | |
| 1330 | if arch_filter and arch_name not in arch_filter: |
| 1331 | continue |
| 1332 | |
| 1333 | if tc.arch_whitelist and arch.name not in tc.arch_whitelist: |
| 1334 | continue |
| 1335 | |
| 1336 | if tc.arch_exclude and arch.name in tc.arch_exclude: |
| 1337 | continue |
| 1338 | |
| 1339 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 1340 | continue |
| 1341 | |
| 1342 | if platform_filter and plat.name not in platform_filter: |
| 1343 | continue |
| 1344 | |
| 1345 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 1346 | continue |
| 1347 | |
Anas Nashif | 6d4ff23 | 2016-12-21 13:05:28 -0500 | [diff] [blame] | 1348 | if tc.tc_filter and (plat in arch.platforms[:platform_limit] or all_plats or platform_filter): |
Anas Nashif | 8ea9d02 | 2015-11-10 12:24:20 -0500 | [diff] [blame] | 1349 | args = tc.extra_args[:] |
| 1350 | args.extend(["ARCH=" + plat.arch.name, |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1351 | "BOARD=" + plat.name, "initconfig"]) |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 1352 | args.extend(extra_args) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1353 | # FIXME would be nice to use a common outdir for this so that |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1354 | # conf, gen_idt, etc aren't rebuilt for every combination, |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1355 | # need a way to avoid different Make processe from clobbering |
| 1356 | # each other since they all try to build them simultaneously |
| 1357 | |
| 1358 | o = os.path.join(self.outdir, plat.name, tc.path) |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1359 | dlist[tc, plat, tc.name.split("/")[-1]] = os.path.join(o,".config") |
| 1360 | goal = "_".join([plat.name, "_".join(tc.name.split("/")), "initconfig"]) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1361 | mg.add_build_goal(goal, os.path.join(ZEPHYR_BASE, tc.code_location), o, args) |
| 1362 | |
| 1363 | info("Building testcase defconfigs...") |
| 1364 | results = mg.execute(defconfig_cb) |
| 1365 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1366 | for name, goal in results.items(): |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1367 | if goal.failed: |
| 1368 | raise SanityRuntimeError("Couldn't build some defconfigs") |
| 1369 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1370 | for k, out_config in dlist.items(): |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1371 | test, plat, name = k |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1372 | defconfig = {} |
| 1373 | with open(out_config, "r") as fp: |
| 1374 | for line in fp.readlines(): |
| 1375 | m = TestSuite.config_re.match(line) |
| 1376 | if not m: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1377 | if line.strip() and not line.startswith("#"): |
| 1378 | sys.stderr.write("Unrecognized line %s\n" % line) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1379 | continue |
| 1380 | defconfig[m.group(1)] = m.group(2).strip() |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1381 | test.defconfig[plat] = defconfig |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1382 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1383 | for tc_name, tc in self.testcases.items(): |
| 1384 | for arch_name, arch in self.arches.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1385 | instance_list = [] |
| 1386 | for plat in arch.platforms: |
| 1387 | instance = TestInstance(tc, plat, self.outdir) |
| 1388 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1389 | if (arch_name == "unit") != (tc.type == "unit"): |
| 1390 | # Discard silently |
| 1391 | continue |
| 1392 | |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1393 | if tc.skip: |
| 1394 | discards[instance] = "Skip filter" |
| 1395 | continue |
| 1396 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1397 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 1398 | discards[instance] = "Command line testcase tag filter" |
| 1399 | continue |
| 1400 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1401 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 1402 | discards[instance] = "Command line testcase exclude filter" |
| 1403 | continue |
| 1404 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1405 | if testcase_filter and tc_name not in testcase_filter: |
| 1406 | discards[instance] = "Testcase name filter" |
| 1407 | continue |
| 1408 | |
| 1409 | if last_failed and (tc.name, plat.name) not in failed_tests: |
| 1410 | discards[instance] = "Passed or skipped during last run" |
| 1411 | continue |
| 1412 | |
| 1413 | if arch_filter and arch_name not in arch_filter: |
| 1414 | discards[instance] = "Command line testcase arch filter" |
| 1415 | continue |
| 1416 | |
| 1417 | if tc.arch_whitelist and arch.name not in tc.arch_whitelist: |
| 1418 | discards[instance] = "Not in test case arch whitelist" |
| 1419 | continue |
| 1420 | |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1421 | if tc.arch_exclude and arch.name in tc.arch_exclude: |
| 1422 | discards[instance] = "In test case arch exclude" |
| 1423 | continue |
| 1424 | |
| 1425 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 1426 | discards[instance] = "In test case platform exclude" |
| 1427 | continue |
| 1428 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1429 | if platform_filter and plat.name not in platform_filter: |
| 1430 | discards[instance] = "Command line platform filter" |
| 1431 | continue |
| 1432 | |
| 1433 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 1434 | discards[instance] = "Not in testcase platform whitelist" |
| 1435 | continue |
| 1436 | |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1437 | if toolchain and toolchain not in plat.supported_toolchains: |
| 1438 | discards[instance] = "Not supported by the toolchain" |
| 1439 | continue |
| 1440 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1441 | defconfig = {"ARCH" : arch.name, "PLATFORM" : plat.name} |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 1442 | defconfig.update(os.environ) |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1443 | for p, tdefconfig in tc.defconfig.items(): |
| 1444 | if p == plat: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1445 | defconfig.update(tdefconfig) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1446 | break |
| 1447 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1448 | if tc.tc_filter: |
| 1449 | try: |
| 1450 | res = expr_parser.parse(tc.tc_filter, defconfig) |
| 1451 | except SyntaxError as se: |
| 1452 | sys.stderr.write("Failed processing %s\n" % tc.inifile) |
| 1453 | raise se |
| 1454 | if not res: |
| 1455 | discards[instance] = ("defconfig doesn't satisfy expression '%s'" % |
| 1456 | tc.tc_filter) |
| 1457 | continue |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1458 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1459 | instance_list.append(instance) |
| 1460 | |
| 1461 | if not instance_list: |
| 1462 | # Every platform in this arch was rejected already |
| 1463 | continue |
| 1464 | |
| 1465 | if default_platforms: |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1466 | self.add_instances(instance_list[:platform_limit]) |
| 1467 | for instance in instance_list[platform_limit:]: |
| 1468 | discards[instance] = "Not in first %d platform(s) for arch" % platform_limit |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1469 | else: |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1470 | self.add_instances(instance_list) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1471 | self.discards = discards |
| 1472 | return discards |
| 1473 | |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1474 | def add_instances(self, ti_list): |
| 1475 | for ti in ti_list: |
| 1476 | self.instances[ti.name] = ti |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1477 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1478 | def execute(self, cb, cb_context, build_only, enable_slow, enable_asserts, enable_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1479 | extra_args, enable_ccache): |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1480 | |
| 1481 | def calc_one_elf_size(name, goal): |
| 1482 | if not goal.failed: |
| 1483 | i = self.instances[name] |
| 1484 | sc = i.calculate_sizes() |
| 1485 | goal.metrics["ram_size"] = sc.get_ram_size() |
| 1486 | goal.metrics["rom_size"] = sc.get_rom_size() |
| 1487 | goal.metrics["unrecognized"] = sc.unrecognized_sections() |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1488 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1489 | mg = MakeGenerator(self.outdir, asserts=enable_asserts, deprecations=enable_deprecations, |
| 1490 | ccache=enable_ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1491 | for i in self.instances.values(): |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1492 | mg.add_test_instance(i, build_only, enable_slow, self.coverage, extra_args) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1493 | self.goals = mg.execute(cb, cb_context) |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1494 | |
| 1495 | # Parallelize size calculation |
| 1496 | executor = concurrent.futures.ThreadPoolExecutor(CPU_COUNTS) |
| 1497 | futures = [executor.submit(calc_one_elf_size, name, goal) \ |
| 1498 | for name, goal in self.goals.items()] |
| 1499 | concurrent.futures.wait(futures) |
| 1500 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1501 | return self.goals |
| 1502 | |
| 1503 | def discard_report(self, filename): |
| 1504 | if self.discards == None: |
| 1505 | raise SanityRuntimeException("apply_filters() hasn't been run!") |
| 1506 | |
| 1507 | with open(filename, "wb") as csvfile: |
| 1508 | fieldnames = ["test", "arch", "platform", "reason"] |
| 1509 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 1510 | cw.writeheader() |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1511 | for instance, reason in self.discards.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1512 | rowdict = {"test" : i.test.name, |
| 1513 | "arch" : i.platform.arch.name, |
| 1514 | "platform" : i.platform.name, |
| 1515 | "reason" : reason} |
| 1516 | cw.writerow(rowdict) |
| 1517 | |
| 1518 | def compare_metrics(self, filename): |
| 1519 | # name, datatype, lower results better |
| 1520 | interesting_metrics = [("ram_size", int, True), |
| 1521 | ("rom_size", int, True)] |
| 1522 | |
| 1523 | if self.goals == None: |
| 1524 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1525 | |
| 1526 | if not os.path.exists(filename): |
| 1527 | info("Cannot compare metrics, %s not found" % filename) |
| 1528 | return [] |
| 1529 | |
| 1530 | results = [] |
| 1531 | saved_metrics = {} |
| 1532 | with open(filename) as fp: |
| 1533 | cr = csv.DictReader(fp) |
| 1534 | for row in cr: |
| 1535 | d = {} |
| 1536 | for m, _, _ in interesting_metrics: |
| 1537 | d[m] = row[m] |
| 1538 | saved_metrics[(row["test"], row["platform"])] = d |
| 1539 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1540 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1541 | i = self.instances[name] |
| 1542 | mkey = (i.test.name, i.platform.name) |
| 1543 | if mkey not in saved_metrics: |
| 1544 | continue |
| 1545 | sm = saved_metrics[mkey] |
| 1546 | for metric, mtype, lower_better in interesting_metrics: |
| 1547 | if metric not in goal.metrics: |
| 1548 | continue |
| 1549 | if sm[metric] == "": |
| 1550 | continue |
| 1551 | delta = goal.metrics[metric] - mtype(sm[metric]) |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1552 | if delta == 0: |
| 1553 | continue |
| 1554 | results.append((i, metric, goal.metrics[metric], delta, |
| 1555 | lower_better)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1556 | return results |
| 1557 | |
| 1558 | def testcase_report(self, filename): |
| 1559 | if self.goals == None: |
| 1560 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1561 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1562 | with open(filename, "wt") as csvfile: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1563 | fieldnames = ["test", "arch", "platform", "passed", "status", |
| 1564 | "extra_args", "qemu", "qemu_time", "ram_size", |
| 1565 | "rom_size"] |
| 1566 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 1567 | cw.writeheader() |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1568 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1569 | i = self.instances[name] |
| 1570 | rowdict = {"test" : i.test.name, |
| 1571 | "arch" : i.platform.arch.name, |
| 1572 | "platform" : i.platform.name, |
| 1573 | "extra_args" : " ".join(i.test.extra_args), |
| 1574 | "qemu" : i.platform.qemu_support} |
| 1575 | if goal.failed: |
| 1576 | rowdict["passed"] = False |
| 1577 | rowdict["status"] = goal.reason |
| 1578 | else: |
| 1579 | rowdict["passed"] = True |
| 1580 | if goal.qemu: |
| 1581 | rowdict["qemu_time"] = goal.metrics["qemu_time"] |
| 1582 | rowdict["ram_size"] = goal.metrics["ram_size"] |
| 1583 | rowdict["rom_size"] = goal.metrics["rom_size"] |
| 1584 | cw.writerow(rowdict) |
| 1585 | |
| 1586 | |
| 1587 | def parse_arguments(): |
| 1588 | |
| 1589 | parser = argparse.ArgumentParser(description = __doc__, |
| 1590 | formatter_class = argparse.RawDescriptionHelpFormatter) |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 1591 | parser.fromfile_prefix_chars = "+" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1592 | |
| 1593 | parser.add_argument("-p", "--platform", action="append", |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1594 | help="Platform filter for testing. This option may be used multiple " |
| 1595 | "times. Testcases will only be built/run on the platforms " |
| 1596 | "specified. If this option is not used, then N platforms will " |
| 1597 | "automatically be chosen from each arch to build and test, " |
| 1598 | "where N is provided by the --platform-limit option.") |
| 1599 | parser.add_argument("-L", "--platform-limit", action="store", type=int, |
| 1600 | metavar="N", default=1, |
| 1601 | help="Controls what platforms are tested if --platform or " |
| 1602 | "--all are not used. For each architecture specified by " |
| 1603 | "--arch (defaults to all of them), choose the first " |
| 1604 | "N platforms to test in the arch-specific .ini file " |
| 1605 | "'platforms' list. Defaults to 1.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1606 | parser.add_argument("-a", "--arch", action="append", |
| 1607 | help="Arch filter for testing. Takes precedence over --platform. " |
| 1608 | "If unspecified, test all arches. Multiple invocations " |
| 1609 | "are treated as a logical 'or' relationship") |
| 1610 | parser.add_argument("-t", "--tag", action="append", |
| 1611 | help="Specify tags to restrict which tests to run by tag value. " |
| 1612 | "Default is to not do any tag filtering. Multiple invocations " |
| 1613 | "are treated as a logical 'or' relationship") |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1614 | parser.add_argument("-e", "--exclude-tag", action="append", |
| 1615 | help="Specify tags of tests that should not run." |
| 1616 | "Default is to run all tests with all tags.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1617 | parser.add_argument("-f", "--only-failed", action="store_true", |
| 1618 | help="Run only those tests that failed the previous sanity check " |
| 1619 | "invocation.") |
| 1620 | parser.add_argument("-c", "--config", action="append", |
| 1621 | help="Specify platform configuration values filtering. This can be " |
| 1622 | "specified two ways: <config>=<value> or just <config>. The " |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1623 | "defconfig for all platforms will be " |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1624 | "checked. For the <config>=<value> case, only match defconfig " |
| 1625 | "that have that value defined. For the <config> case, match " |
| 1626 | "defconfig that have that value assigned to any value. " |
| 1627 | "Prepend a '!' to invert the match.") |
| 1628 | parser.add_argument("-s", "--test", action="append", |
| 1629 | help="Run only the specified test cases. These are named by " |
| 1630 | "<path to test project relative to " |
| 1631 | "--testcase-root>/<testcase.ini section name>") |
| 1632 | parser.add_argument("-l", "--all", action="store_true", |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1633 | help="Build/test on all platforms. Any --platform arguments " |
| 1634 | "ignored.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1635 | |
| 1636 | parser.add_argument("-o", "--testcase-report", |
| 1637 | help="Output a CSV spreadsheet containing results of the test run") |
| 1638 | parser.add_argument("-d", "--discard-report", |
| 1639 | help="Output a CSV spreadhseet showing tests that were skipped " |
| 1640 | "and why") |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 1641 | parser.add_argument("--compare-report", |
| 1642 | help="Use this report file for size comparision") |
| 1643 | |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1644 | parser.add_argument("--ccache", action="store_const", const=1, default=0, |
| 1645 | help="Enable the use of ccache when building") |
| 1646 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1647 | parser.add_argument("-y", "--dry-run", action="store_true", |
| 1648 | help="Create the filtered list of test cases, but don't actually " |
| 1649 | "run them. Useful if you're just interested in " |
| 1650 | "--discard-report") |
| 1651 | |
| 1652 | parser.add_argument("-r", "--release", action="store_true", |
| 1653 | help="Update the benchmark database with the results of this test " |
| 1654 | "run. Intended to be run by CI when tagging an official " |
| 1655 | "release. This database is used as a basis for comparison " |
| 1656 | "when looking for deltas in metrics such as footprint") |
| 1657 | parser.add_argument("-w", "--warnings-as-errors", action="store_true", |
| 1658 | help="Treat warning conditions as errors") |
| 1659 | parser.add_argument("-v", "--verbose", action="count", default=0, |
| 1660 | help="Emit debugging information, call multiple times to increase " |
| 1661 | "verbosity") |
| 1662 | parser.add_argument("-i", "--inline-logs", action="store_true", |
| 1663 | help="Upon test failure, print relevant log data to stdout " |
| 1664 | "instead of just a path to it") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1665 | parser.add_argument("--log-file", metavar="FILENAME", action="store", |
| 1666 | help="log also to file") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1667 | parser.add_argument("-m", "--last-metrics", action="store_true", |
| 1668 | help="Instead of comparing metrics from the last --release, " |
| 1669 | "compare with the results of the previous sanity check " |
| 1670 | "invocation") |
| 1671 | parser.add_argument("-u", "--no-update", action="store_true", |
| 1672 | help="do not update the results of the last run of the sanity " |
| 1673 | "checks") |
| 1674 | parser.add_argument("-b", "--build-only", action="store_true", |
| 1675 | help="Only build the code, do not execute any of it in QEMU") |
| 1676 | parser.add_argument("-j", "--jobs", type=int, |
| 1677 | help="Number of cores to use when building, defaults to " |
| 1678 | "number of CPUs * 2") |
| 1679 | parser.add_argument("-H", "--footprint-threshold", type=float, default=5, |
| 1680 | help="When checking test case footprint sizes, warn the user if " |
| 1681 | "the new app size is greater then the specified percentage " |
| 1682 | "from the last release. Default is 5. 0 to warn on any " |
| 1683 | "increase on app size") |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1684 | parser.add_argument("-D", "--all-deltas", action="store_true", |
| 1685 | help="Show all footprint deltas, positive or negative. Implies " |
| 1686 | "--footprint-threshold=0") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1687 | parser.add_argument("-O", "--outdir", |
| 1688 | default="%s/sanity-out" % ZEPHYR_BASE, |
| 1689 | help="Output directory for logs and binaries.") |
Andrew Boie | ae9e7f7b | 2015-07-31 12:26:12 -0700 | [diff] [blame] | 1690 | parser.add_argument("-n", "--no-clean", action="store_true", |
| 1691 | help="Do not delete the outdir before building. Will result in " |
| 1692 | "faster compilation since builds will be incremental") |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1693 | parser.add_argument("-T", "--testcase-root", action="append", default=[], |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1694 | help="Base directory to recursively search for test cases. All " |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1695 | "testcase.ini files under here will be processed. May be " |
| 1696 | "called multiple times. Defaults to the 'samples' and " |
| 1697 | "'tests' directories in the Zephyr tree.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1698 | parser.add_argument("-A", "--arch-root", |
| 1699 | default="%s/scripts/sanity_chk/arches" % ZEPHYR_BASE, |
| 1700 | help="Directory to search for arch configuration files. All .ini " |
| 1701 | "files in the directory will be processed.") |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1702 | parser.add_argument("-z", "--size", action="append", |
| 1703 | help="Don't run sanity checks. Instead, produce a report to " |
| 1704 | "stdout detailing RAM/ROM sizes on the specified filenames. " |
| 1705 | "All other command line arguments ignored.") |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1706 | parser.add_argument("-S", "--enable-slow", action="store_true", |
| 1707 | help="Execute time-consuming test cases that have been marked " |
| 1708 | "as 'slow' in testcase.ini. Normally these are only built.") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 1709 | parser.add_argument("-R", "--enable-asserts", action="store_true", |
| 1710 | help="Build all test cases with assertions enabled.") |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1711 | parser.add_argument("-Q", "--error-on-deprecations", action="store_false", |
| 1712 | help="Error on deprecation warnings.") |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 1713 | parser.add_argument("-x", "--extra-args", action="append", default=[], |
| 1714 | help="Extra arguments to pass to the build when compiling test " |
| 1715 | "cases. May be called multiple times. These will be passed " |
| 1716 | "in after any sanitycheck-supplied options.") |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1717 | parser.add_argument("-C", "--coverage", action="store_true", |
| 1718 | help="Scan for unit test coverage with gcov + lcov.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1719 | |
| 1720 | return parser.parse_args() |
| 1721 | |
| 1722 | def log_info(filename): |
Mazen NEIFER | 8f1dd3a | 2017-02-04 14:32:04 +0100 | [diff] [blame] | 1723 | filename = os.path.relpath(os.path.realpath(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1724 | if INLINE_LOGS: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1725 | info("{:-^100}".format(filename)) |
Andrew Boie | d01535c | 2017-01-10 13:15:02 -0800 | [diff] [blame] | 1726 | |
| 1727 | try: |
| 1728 | with open(filename) as fp: |
| 1729 | data = fp.read() |
| 1730 | except Exception as e: |
| 1731 | data = "Unable to read log data (%s)\n" % (str(e)) |
| 1732 | |
| 1733 | sys.stdout.write(data) |
| 1734 | if log_file: |
| 1735 | log_file.write(data) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1736 | info("{:-^100}".format(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1737 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1738 | info("\tsee: " + COLOR_YELLOW + filename + COLOR_NORMAL) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1739 | |
| 1740 | def terse_test_cb(instances, goals, goal): |
| 1741 | total_tests = len(goals) |
| 1742 | total_done = 0 |
| 1743 | total_failed = 0 |
| 1744 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1745 | for k, g in goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1746 | if g.finished: |
| 1747 | total_done += 1 |
| 1748 | if g.failed: |
| 1749 | total_failed += 1 |
| 1750 | |
| 1751 | if goal.failed: |
| 1752 | i = instances[goal.name] |
| 1753 | info("\n\n{:<25} {:<50} {}FAILED{}: {}".format(i.platform.name, |
| 1754 | i.test.name, COLOR_RED, COLOR_NORMAL, goal.reason)) |
| 1755 | log_info(goal.get_error_log()) |
| 1756 | info("") |
| 1757 | |
Andrew Boie | 7a992ae | 2017-01-17 10:40:02 -0800 | [diff] [blame] | 1758 | sys.stdout.write("\rtotal complete: %s%4d/%4d%s %2d%% failed: %s%4d%s" % ( |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1759 | COLOR_GREEN, total_done, total_tests, COLOR_NORMAL, |
Andrew Boie | 7a992ae | 2017-01-17 10:40:02 -0800 | [diff] [blame] | 1760 | int((float(total_done) / total_tests) * 100), |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1761 | COLOR_RED if total_failed > 0 else COLOR_NORMAL, |
| 1762 | total_failed, COLOR_NORMAL)) |
| 1763 | sys.stdout.flush() |
| 1764 | |
| 1765 | def chatty_test_cb(instances, goals, goal): |
| 1766 | i = instances[goal.name] |
| 1767 | |
| 1768 | if VERBOSE < 2 and not goal.finished: |
| 1769 | return |
| 1770 | |
| 1771 | if goal.failed: |
| 1772 | status = COLOR_RED + "FAILED" + COLOR_NORMAL + ": " + goal.reason |
| 1773 | elif goal.finished: |
| 1774 | status = COLOR_GREEN + "PASSED" + COLOR_NORMAL |
| 1775 | else: |
| 1776 | status = goal.make_state |
| 1777 | |
| 1778 | info("{:<25} {:<50} {}".format(i.platform.name, i.test.name, status)) |
| 1779 | if goal.failed: |
| 1780 | log_info(goal.get_error_log()) |
| 1781 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1782 | |
| 1783 | def size_report(sc): |
| 1784 | info(sc.filename) |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1785 | info("SECTION NAME VMA LMA SIZE HEX SZ TYPE") |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1786 | for i in range(len(sc.sections)): |
| 1787 | v = sc.sections[i] |
| 1788 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1789 | info("%-17s 0x%08x 0x%08x %8d 0x%05x %-7s" % |
| 1790 | (v["name"], v["virt_addr"], v["load_addr"], v["size"], v["size"], |
| 1791 | v["type"])) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1792 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1793 | info("Totals: %d bytes (ROM), %d bytes (RAM)" % |
| 1794 | (sc.rom_size, sc.ram_size)) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1795 | info("") |
| 1796 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1797 | def generate_coverage(outdir, ignores): |
| 1798 | with open(os.path.join(outdir, "coverage.log"), "a") as coveragelog: |
| 1799 | coveragefile = os.path.join(outdir, "coverage.info") |
| 1800 | ztestfile = os.path.join(outdir, "ztest.info") |
| 1801 | subprocess.call(["lcov", "--capture", "--directory", outdir, |
| 1802 | "--output-file", coveragefile], stdout=coveragelog) |
| 1803 | # We want to remove tests/* and tests/ztest/test/* but save tests/ztest |
| 1804 | subprocess.call(["lcov", "--extract", coveragefile, |
| 1805 | os.path.join(ZEPHYR_BASE, "tests", "ztest", "*"), |
| 1806 | "--output-file", ztestfile], stdout=coveragelog) |
| 1807 | subprocess.call(["lcov", "--remove", ztestfile, |
| 1808 | os.path.join(ZEPHYR_BASE, "tests/ztest/test/*"), |
| 1809 | "--output-file", ztestfile], stdout=coveragelog) |
| 1810 | for i in ignores: |
| 1811 | subprocess.call(["lcov", "--remove", coveragefile, i, |
| 1812 | "--output-file", coveragefile], stdout=coveragelog) |
| 1813 | subprocess.call(["genhtml", "-output-directory", |
| 1814 | os.path.join(outdir, "coverage"), |
| 1815 | coveragefile, ztestfile], stdout=coveragelog) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1816 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1817 | def main(): |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1818 | start_time = time.time() |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1819 | global VERBOSE, INLINE_LOGS, CPU_COUNTS, log_file |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1820 | args = parse_arguments() |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1821 | toolchain = os.environ.get("ZEPHYR_GCC_VARIANT", None) |
Andrew Boie | 1e4e68b | 2017-02-10 11:40:54 -0800 | [diff] [blame] | 1822 | if toolchain == "zephyr": |
| 1823 | os.environ["DISABLE_TRYRUN"] = "1" |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1824 | |
| 1825 | if args.size: |
| 1826 | for fn in args.size: |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1827 | size_report(SizeCalculator(fn, [])) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1828 | sys.exit(0) |
| 1829 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1830 | VERBOSE += args.verbose |
| 1831 | INLINE_LOGS = args.inline_logs |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1832 | if args.log_file: |
| 1833 | log_file = open(args.log_file, "w") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1834 | if args.jobs: |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1835 | CPU_COUNTS = args.jobs |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1836 | |
Andrew Boie | ae9e7f7b | 2015-07-31 12:26:12 -0700 | [diff] [blame] | 1837 | if os.path.exists(args.outdir) and not args.no_clean: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1838 | info("Cleaning output directory " + args.outdir) |
| 1839 | shutil.rmtree(args.outdir) |
| 1840 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1841 | if not args.testcase_root: |
| 1842 | args.testcase_root = [os.path.join(ZEPHYR_BASE, "tests"), |
| 1843 | os.path.join(ZEPHYR_BASE, "samples")] |
| 1844 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1845 | ts = TestSuite(args.arch_root, args.testcase_root, args.outdir, args.coverage) |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1846 | discards = ts.apply_filters(args.platform, args.arch, args.tag, args.exclude_tag, args.config, |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1847 | args.test, args.only_failed, args.all, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1848 | args.platform_limit, toolchain, args.extra_args, args.ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1849 | |
| 1850 | if args.discard_report: |
| 1851 | ts.discard_report(args.discard_report) |
| 1852 | |
| 1853 | if VERBOSE: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1854 | for i, reason in discards.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1855 | debug("{:<25} {:<50} {}SKIPPED{}: {}".format(i.platform.name, |
| 1856 | i.test.name, COLOR_YELLOW, COLOR_NORMAL, reason)) |
| 1857 | |
| 1858 | info("%d tests selected, %d tests discarded due to filters" % |
| 1859 | (len(ts.instances), len(discards))) |
| 1860 | |
| 1861 | if args.dry_run: |
| 1862 | return |
| 1863 | |
| 1864 | if VERBOSE or not TERMINAL: |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1865 | goals = ts.execute(chatty_test_cb, ts.instances, args.build_only, |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1866 | args.enable_slow, args.enable_asserts, args.error_on_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1867 | args.extra_args, args.ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1868 | else: |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1869 | goals = ts.execute(terse_test_cb, ts.instances, args.build_only, |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1870 | args.enable_slow, args.enable_asserts, args.error_on_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1871 | args.extra_args, args.ccache) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1872 | info("") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1873 | |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 1874 | # figure out which report to use for size comparison |
| 1875 | if args.compare_report: |
| 1876 | report_to_use = args.compare_report |
| 1877 | elif args.last_metrics: |
| 1878 | report_to_use = LAST_SANITY |
| 1879 | else: |
| 1880 | report_to_use = RELEASE_DATA |
| 1881 | |
| 1882 | deltas = ts.compare_metrics(report_to_use) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1883 | warnings = 0 |
| 1884 | if deltas: |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1885 | for i, metric, value, delta, lower_better in deltas: |
| 1886 | if not args.all_deltas and ((delta < 0 and lower_better) or |
| 1887 | (delta > 0 and not lower_better)): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1888 | continue |
| 1889 | |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1890 | percentage = (float(delta) / float(value - delta)) |
| 1891 | if not args.all_deltas and (percentage < |
| 1892 | (args.footprint_threshold / 100.0)): |
| 1893 | continue |
| 1894 | |
Daniel Leung | 00525c2 | 2016-04-11 10:27:56 -0700 | [diff] [blame] | 1895 | info("{:<25} {:<60} {}{}{}: {} {:<+4}, is now {:6} {:+.2%}".format( |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1896 | i.platform.name, i.test.name, COLOR_YELLOW, |
| 1897 | "INFO" if args.all_deltas else "WARNING", COLOR_NORMAL, |
Andrew Boie | 829c056 | 2015-10-13 09:44:19 -0700 | [diff] [blame] | 1898 | metric, delta, value, percentage)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1899 | warnings += 1 |
| 1900 | |
| 1901 | if warnings: |
| 1902 | info("Deltas based on metrics from last %s" % |
| 1903 | ("release" if not args.last_metrics else "run")) |
| 1904 | |
| 1905 | failed = 0 |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1906 | for name, goal in goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1907 | if goal.failed: |
| 1908 | failed += 1 |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1909 | elif goal.metrics.get("unrecognized"): |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1910 | info("%sFAILED%s: %s has unrecognized binary sections: %s" % |
| 1911 | (COLOR_RED, COLOR_NORMAL, goal.name, |
| 1912 | str(goal.metrics["unrecognized"]))) |
| 1913 | failed += 1 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1914 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1915 | if args.coverage: |
| 1916 | info("Generating coverage files...") |
| 1917 | generate_coverage(args.outdir, ["tests/*", "samples/*"]) |
| 1918 | |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1919 | info("%s%d of %d%s tests passed with %s%d%s warnings in %d seconds" % |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1920 | (COLOR_RED if failed else COLOR_GREEN, len(goals) - failed, |
| 1921 | len(goals), COLOR_NORMAL, COLOR_YELLOW if warnings else COLOR_NORMAL, |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1922 | warnings, COLOR_NORMAL, time.time() - start_time)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1923 | |
| 1924 | if args.testcase_report: |
| 1925 | ts.testcase_report(args.testcase_report) |
| 1926 | if not args.no_update: |
| 1927 | ts.testcase_report(LAST_SANITY) |
| 1928 | if args.release: |
| 1929 | ts.testcase_report(RELEASE_DATA) |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1930 | if log_file: |
| 1931 | log_file.close() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1932 | if failed or (warnings and args.warnings_as_errors): |
| 1933 | sys.exit(1) |
| 1934 | |
| 1935 | if __name__ == "__main__": |
| 1936 | main() |