• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 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
5"""This throttler reduces result size by deleting files permanently."""
6
7import os
8
9try:
10    from autotest_lib.client.bin.result_tools import throttler_lib
11    from autotest_lib.client.bin.result_tools import utils_lib
12except ImportError:
13    import throttler_lib
14    import utils_lib
15
16
17# Default threshold of file size in KB for a file to be qualified for deletion.
18DEFAULT_FILE_SIZE_THRESHOLD_BYTE = 1024 * 1024
19
20# Regex for file path that should not be deleted.
21# Note; it is OK to try to compress these.
22NON_DELETABLE_FILE_PATH_PATTERNS = [
23        '.*perf.data$',  # Performance test data.
24        '.*DEBUG.*',  # Any autotest debug file needs to be preserved
25        '.*full.*',  # tast logs...
26        '.*net.log',  # net logs.
27        '.*android*',  # several android debugging logs are important.
28        '.*screenshot*',  # screenshots are needed for debugging.
29        '.*logcat*',
30        '.*/faillog/*',  # full tast fail logs.
31]
32
33
34def _delete_file(file_info):
35    """Delete the given file and update the summary.
36
37    @param file_info: A ResultInfo object containing summary for the file to be
38            shrunk.
39    """
40    utils_lib.LOG('Deleting file %s.' % file_info.path)
41    try:
42        os.remove(file_info.path)
43    except OSError as e:
44        utils_lib.LOG('Failed to delete file %s Error: %s' %
45                      (file_info.path, e))
46
47    # Update the trimmed_size in ResultInfo.
48    file_info.trimmed_size = 0
49
50
51def throttle(summary, max_result_size_KB,
52             file_size_threshold_byte=DEFAULT_FILE_SIZE_THRESHOLD_BYTE,
53             exclude_file_patterns=[]):
54    """Throttle the files in summary by trimming file content.
55
56    Stop throttling until all files are processed or the result size is already
57    reduced to be under the given max_result_size_KB.
58
59    @param summary: A ResultInfo object containing result summary.
60    @param max_result_size_KB: Maximum test result size in KB.
61    @param file_size_threshold_byte: Threshold of file size in byte for a file
62            to be qualified for deletion. All qualified files will be deleted,
63            until all files are processed or the result size is under the given
64            max_result_size_KB.
65    @param exclude_file_patterns: A list of regex pattern for files not to be
66            throttled. Default is an empty list.
67    """
68    file_infos, _ = throttler_lib.sort_result_files(summary)
69    file_infos = throttler_lib.get_throttleable_files(
70            file_infos,
71            exclude_file_patterns + NON_DELETABLE_FILE_PATH_PATTERNS)
72
73    for info in file_infos:
74        if info.trimmed_size > file_size_threshold_byte:
75            _delete_file(info)
76            if throttler_lib.check_throttle_limit(summary, max_result_size_KB):
77                return
78