summaryrefslogtreecommitdiffstats
path: root/bitbake/contrib/bbparse-torture.py
blob: c25d547bb87188f2ec22f79da26d894302abdf84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env python3
#
# Copyright (C) 2020 Joshua Watt <JPEWhacker@gmail.com>
#
# SPDX-License-Identifier: MIT

import argparse
import os
import random
import shutil
import signal
import subprocess
import sys
import time


def try_unlink(path):
    try:
        os.unlink(path)
    except:
        pass


def main():
    def cleanup():
        shutil.rmtree("tmp/cache", ignore_errors=True)
        try_unlink("bitbake-cookerdaemon.log")
        try_unlink("bitbake.sock")
        try_unlink("bitbake.lock")

    parser = argparse.ArgumentParser(
        description="Bitbake parser torture test",
        epilog="""
        A torture test for bitbake's parser. Repeatedly interrupts parsing until
        bitbake decides to deadlock.
        """,
    )

    args = parser.parse_args()

    if not "BUILDDIR" in os.environ:
        print(
            "'BUILDDIR' not found in the environment. Did you initialize the build environment?"
        )
        return 1

    os.chdir(os.environ["BUILDDIR"])

    run_num = 0
    while True:
        if run_num % 100 == 0:
            print("Calibrating wait time...")
            cleanup()

            start_time = time.monotonic()
            r = subprocess.run(["bitbake", "-p"])
            max_wait_time = time.monotonic() - start_time

            if r.returncode != 0:
                print("Calibration run exited with %d" % r.returncode)
                return 1

            print("Maximum wait time is %f seconds" % max_wait_time)

        run_num += 1
        wait_time = random.random() * max_wait_time

        print("Run #%d" % run_num)
        print("Will sleep for %f seconds" % wait_time)

        cleanup()
        with subprocess.Popen(["bitbake", "-p"]) as proc:
            time.sleep(wait_time)
            proc.send_signal(signal.SIGINT)
            try:
                proc.wait(45)
            except subprocess.TimeoutExpired:
                print("Run #%d: Waited too long. Possible deadlock!" % run_num)
                proc.wait()
                return 1

            if proc.returncode == 0:
                print("Exited successfully. Timeout too long?")
            else:
                print("Exited with %d" % proc.returncode)


if __name__ == "__main__":
    sys.exit(main())