17ec681f3Smrg#!/usr/bin/env python3
27ec681f3Smrg#
37ec681f3Smrg# Copyright © 2020 Google LLC
47ec681f3Smrg#
57ec681f3Smrg# Permission is hereby granted, free of charge, to any person obtaining a
67ec681f3Smrg# copy of this software and associated documentation files (the "Software"),
77ec681f3Smrg# to deal in the Software without restriction, including without limitation
87ec681f3Smrg# the rights to use, copy, modify, merge, publish, distribute, sublicense,
97ec681f3Smrg# and/or sell copies of the Software, and to permit persons to whom the
107ec681f3Smrg# Software is furnished to do so, subject to the following conditions:
117ec681f3Smrg#
127ec681f3Smrg# The above copyright notice and this permission notice (including the next
137ec681f3Smrg# paragraph) shall be included in all copies or substantial portions of the
147ec681f3Smrg# Software.
157ec681f3Smrg#
167ec681f3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177ec681f3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187ec681f3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
197ec681f3Smrg# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
207ec681f3Smrg# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
217ec681f3Smrg# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
227ec681f3Smrg# IN THE SOFTWARE.
237ec681f3Smrg
247ec681f3Smrgimport argparse
257ec681f3Smrgimport os
267ec681f3Smrgimport re
277ec681f3Smrgfrom serial_buffer import SerialBuffer
287ec681f3Smrgimport sys
297ec681f3Smrgimport threading
307ec681f3Smrg
317ec681f3Smrgclass FastbootRun:
327ec681f3Smrg    def __init__(self, args):
337ec681f3Smrg        self.powerup = args.powerup
347ec681f3Smrg        # We would like something like a 1 minute timeout, but the piglit traces
357ec681f3Smrg        # jobs stall out for long periods of time.
367ec681f3Smrg        self.ser = SerialBuffer(args.dev, "results/serial-output.txt", "R SERIAL> ", timeout=600)
377ec681f3Smrg        self.fastboot="fastboot boot -s {ser} artifacts/fastboot.img".format(ser=args.fbserial)
387ec681f3Smrg
397ec681f3Smrg    def print_error(self, message):
407ec681f3Smrg        RED = '\033[0;31m'
417ec681f3Smrg        NO_COLOR = '\033[0m'
427ec681f3Smrg        print(RED + message + NO_COLOR)
437ec681f3Smrg
447ec681f3Smrg    def logged_system(self, cmd):
457ec681f3Smrg        print("Running '{}'".format(cmd))
467ec681f3Smrg        return os.system(cmd)
477ec681f3Smrg
487ec681f3Smrg    def run(self):
497ec681f3Smrg        if self.logged_system(self.powerup) != 0:
507ec681f3Smrg            return 1
517ec681f3Smrg
527ec681f3Smrg        fastboot_ready = False
537ec681f3Smrg        for line in self.ser.lines():
547ec681f3Smrg            if re.search("fastboot: processing commands", line) or \
557ec681f3Smrg                re.search("Listening for fastboot command on", line):
567ec681f3Smrg                fastboot_ready = True
577ec681f3Smrg                break
587ec681f3Smrg
597ec681f3Smrg            if re.search("data abort", line):
607ec681f3Smrg                self.print_error("Detected crash during boot, restarting run...")
617ec681f3Smrg                return 2
627ec681f3Smrg
637ec681f3Smrg        if not fastboot_ready:
647ec681f3Smrg            self.print_error("Failed to get to fastboot prompt, restarting run...")
657ec681f3Smrg            return 2
667ec681f3Smrg
677ec681f3Smrg        if self.logged_system(self.fastboot) != 0:
687ec681f3Smrg            return 1
697ec681f3Smrg
707ec681f3Smrg        for line in self.ser.lines():
717ec681f3Smrg            if re.search("---. end Kernel panic", line):
727ec681f3Smrg                return 1
737ec681f3Smrg
747ec681f3Smrg            # The db820c boards intermittently reboot.  Just restart the run
757ec681f3Smrg            # when if we see a reboot after we got past fastboot.
767ec681f3Smrg            if re.search("PON REASON", line):
777ec681f3Smrg                self.print_error("Detected spontaneous reboot, restarting run...")
787ec681f3Smrg                return 2
797ec681f3Smrg
807ec681f3Smrg            # db820c sometimes wedges around iommu fault recovery
817ec681f3Smrg            if re.search("watchdog: BUG: soft lockup - CPU.* stuck", line):
827ec681f3Smrg                self.print_error(
837ec681f3Smrg                    "Detected kernel soft lockup, restarting run...")
847ec681f3Smrg                return 2
857ec681f3Smrg
867ec681f3Smrg            # If the network device dies, it's probably not graphics's fault, just try again.
877ec681f3Smrg            if re.search("NETDEV WATCHDOG", line):
887ec681f3Smrg                self.print_error(
897ec681f3Smrg                    "Detected network device failure, restarting run...")
907ec681f3Smrg                return 2
917ec681f3Smrg
927ec681f3Smrg            result = re.search("hwci: mesa: (\S*)", line)
937ec681f3Smrg            if result:
947ec681f3Smrg                if result.group(1) == "pass":
957ec681f3Smrg                    return 0
967ec681f3Smrg                else:
977ec681f3Smrg                    return 1
987ec681f3Smrg
997ec681f3Smrg        self.print_error("Reached the end of the CPU serial log without finding a result, restarting run...")
1007ec681f3Smrg        return 2
1017ec681f3Smrg
1027ec681f3Smrgdef main():
1037ec681f3Smrg    parser = argparse.ArgumentParser()
1047ec681f3Smrg    parser.add_argument('--dev', type=str, help='Serial device (otherwise reading from serial-output.txt)')
1057ec681f3Smrg    parser.add_argument('--powerup', type=str, help='shell command for rebooting', required=True)
1067ec681f3Smrg    parser.add_argument('--powerdown', type=str, help='shell command for powering off', required=True)
1077ec681f3Smrg    parser.add_argument('--fbserial', type=str, help='fastboot serial number of the board', required=True)
1087ec681f3Smrg    args = parser.parse_args()
1097ec681f3Smrg
1107ec681f3Smrg    fastboot = FastbootRun(args)
1117ec681f3Smrg
1127ec681f3Smrg    while True:
1137ec681f3Smrg        retval = fastboot.run()
1147ec681f3Smrg        if retval != 2:
1157ec681f3Smrg            break
1167ec681f3Smrg
1177ec681f3Smrg        fastboot = FastbootRun(args)
1187ec681f3Smrg
1197ec681f3Smrg    fastboot.logged_system(args.powerdown)
1207ec681f3Smrg
1217ec681f3Smrg    sys.exit(retval)
1227ec681f3Smrg
1237ec681f3Smrgif __name__ == '__main__':
1247ec681f3Smrg    main()
125