# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import atexit
import logging
import re

from devil.android import device_errors

logger = logging.getLogger(__name__)
_atexit_messages = set()

# Defines how to switch between the default performance configuration
# ('default_mode') and the mode for use when benchmarking ('high_perf_mode').
# For devices not in the list the defaults are to set up the scaling governor to
# 'performance' and reset it back to 'ondemand' when benchmarking is finished.
#
# The 'default_mode_governor' is mandatory to define, while
# 'high_perf_mode_governor' is not taken into account. The latter is because the
# governor 'performance' is currently used for all benchmarking on all devices.
#
# TODO(crbug.com/383566): Add definitions for all devices used in the perf
# waterfall.
_PERFORMANCE_MODE_DEFINITIONS = {
    # Fire TV Edition - 4K
    'AFTKMST12': {
        'default_mode_governor': 'interactive',
    },
    'Pixel 5': {
        'big_cores': ['cpu6', 'cpu7'],
        'default_mode_governor': 'schedutil',
    },
    'Pixel 4a': {
        'big_cores': ['cpu6', 'cpu7'],
        'default_mode_governor': 'schedutil',
    },
    'Pixel 4': {
        'big_cores': ['cpu6', 'cpu7'],
        'default_mode_governor': 'schedutil',
    },
    # Pixel 3
    'blueline': {
        'high_perf_mode': {
            'bring_cpu_cores_online': True,
            # The SoC is Arm big.LITTLE. The cores 0..3 are LITTLE,
            # the 4..7 are big.
            'cpu_max_freq': {
                '0..3': 1228800,
                '4..7': 1536000
            },
            'gpu_max_freq': 520000000,
        },
        'default_mode': {
            'cpu_max_freq': {
                '0..3': 1766400,
                '4..7': 2649600
            },
            'gpu_max_freq': 710000000,
        },
        'big_cores': ['cpu4', 'cpu5', 'cpu6', 'cpu7'],
        'default_mode_governor': 'schedutil',
    },
    'Pixel 2': {
        'high_perf_mode': {