1# Copyright (C) 2020 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Sorts event names according to a predefined order. 15 16Attributes: 17 EVENT_SORT_ORDER (List[str]): A list of event names sorted as they should 18 appear in the output CFG file. 19 EVENT_SORT_MAP (Dict[str, int]): A mapping of event names to their index in 20 the event sort order list. 21""" 22 23from typing import Iterable, List 24 25EVENT_SORT_ORDER = [ 26 'cpu-cycles', 27 'stalled-cycles-frontend', 28 'stalled-cycles-backend', 29 'instructions', 30 'branch-instructions', 31 'branch-misses', 32 'cache-references', 33 'cache-misses', 34 'task-clock', 35 'context-switches', 36 'page-faults', 37] 38 39EVENT_SORT_MAP = {name: i for i, name in enumerate(EVENT_SORT_ORDER)} 40 41 42def sort_event_names(event_names: Iterable[str]) -> List[str]: 43 """Sorts event names according to a predefined order. 44 45 Args: 46 event_names (Iterable[str]): An iterable of event names. 47 48 Returns: 49 List[str]: A list of sorted event names. 50 """ 51 default_index = len(EVENT_SORT_MAP) 52 return sorted(event_names, 53 key=lambda name: EVENT_SORT_MAP.get(name, default_index)) 54