1# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import logging
6import multiprocessing
7
8import common
9from autotest_lib.server import test
10from autotest_lib.server import hosts
11from autotest_lib.server import crashcollect
12
13
14def log_collector_dut_worker(dut, job):
15    """Worker function to collect logs from each DUT in the pool.
16
17    The method called by multiprocessing worker pool for collecting DUT
18    logs. This function is the function which is repeatedly scheduled for each
19    DUT through the multiprocessing worker. This has to be defined outside
20    the class because it needs to be pickleable.
21
22    @param dut: DUTObject representing the DUT.
23    @param job: Autotest job object.
24    """
25    host = dut.host
26    # Set the job on the host object for log collection.
27    host.job = job
28    logging.info("Collecting logs from: %s", host.hostname)
29    crashcollect.get_crashinfo(host, 0)
30
31
32class CliqueDUTLogCollector(object):
33    """CliqueDUTLogCollector is responsible for collecting the relevant logs
34    from all the DUT's in the DUT pool after the test is executed.
35    """
36
37    def collect_logs(self, dut_objects, job):
38        """Collects logs from all tall the DUT's in the pool to a provided
39        folder.
40
41        @param dut_objects: An array of DUTObjects corresponding to all the
42                            DUT's in the DUT pool.
43        @param job: Autotest job object.
44        """
45        tasks = []
46        for dut in dut_objects:
47            # Schedule the log collection for this DUT to the log process
48            # pool.
49            task = multiprocessing.Process(
50                    target=log_collector_dut_worker,
51                    args=(dut, job))
52            tasks.append(task)
53        # Run the log collections in parallel.
54        for task in tasks:
55            task.start()
56        for task in tasks:
57            task.join()
58