1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 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 */ 15 16 #ifndef LIBPANDABASE_UTILS_TIMERS_H 17 #define LIBPANDABASE_UTILS_TIMERS_H 18 19 #include <chrono> 20 #include <mutex> 21 #include <string> 22 #include <string_view> 23 #include <unordered_map> 24 #include <vector> 25 26 namespace panda { 27 // Event list 28 constexpr std::string_view EVENT_GENERATE_ABC = "generate merged abc by es2abc (async)"; 29 constexpr std::string_view EVENT_TOTAL = "Total"; 30 constexpr std::string_view EVENT_COMPILE = "Compile"; 31 constexpr std::string_view EVENT_RESOLVE_DEPS = "Resolve file dep relations"; 32 constexpr std::string_view EVENT_EMIT_ABC = "Emit abc"; 33 constexpr std::string_view EVENT_READ_INPUT_AND_CACHE = "Read input and cache (async)"; 34 constexpr std::string_view EVENT_COMPILE_FILE = "Compile file (async)"; 35 constexpr std::string_view EVENT_COMPILE_ABC_FILE = "Compile abc file (async)"; 36 constexpr std::string_view EVENT_COMPILE_ABC_FILE_RECORD = "Compile abc file record (async)"; 37 constexpr std::string_view EVENT_UPDATE_ABC_PKG_VERSION = "Update abc package version (async)"; 38 constexpr std::string_view EVENT_UPDATE_ABC_PROGRAM_STRING = "Update abc program string (async)"; 39 constexpr std::string_view EVENT_UPDATE_ABC_PROG_CACHE = "Update abc program cache (async)"; 40 constexpr std::string_view EVENT_OPTIMIZE_PROGRAM = "Optimize program (async)"; 41 constexpr std::string_view EVENT_PARSE = "Parse (async)"; 42 constexpr std::string_view EVENT_COMPILE_TO_PROGRAM = "Compile to program (async)"; 43 constexpr std::string_view EVENT_EMIT_SINGLE_PROGRAM = "Emit single program"; 44 constexpr std::string_view EVENT_EMIT_MERGED_PROGRAM = "Emit merged program"; 45 constexpr std::string_view EVENT_EMIT_CACHE_FILE = "Emit cache file"; 46 47 // Event level, indicating the level of an event, where each event is a sub-event of the nearest preceding level event. 48 // The top-level event's level is 0 49 enum EventLevel { 50 ZEROTH = 0, 51 FIRST, 52 SECOND, 53 THIRD, 54 FORTH, 55 FIFTH, 56 SIXTH 57 }; 58 59 // Holds pairs of {event, level} according to the order during compilation process. 60 // When adding new events later, please add them in the compilation order for ease of reading and maintenance. 61 const std::unordered_map<std::string_view, EventLevel> eventMap = { 62 { EVENT_TOTAL, EventLevel::ZEROTH }, 63 { EVENT_COMPILE, EventLevel::FIRST }, 64 { EVENT_READ_INPUT_AND_CACHE, EventLevel::SECOND }, 65 { EVENT_COMPILE_ABC_FILE, EventLevel::SECOND }, 66 { EVENT_COMPILE_ABC_FILE_RECORD, EventLevel::FORTH }, 67 { EVENT_UPDATE_ABC_PKG_VERSION, EventLevel::THIRD }, 68 { EVENT_UPDATE_ABC_PROGRAM_STRING, EventLevel::THIRD }, 69 { EVENT_UPDATE_ABC_PROG_CACHE, EventLevel::THIRD }, 70 { EVENT_COMPILE_FILE, EventLevel::SECOND }, 71 { EVENT_PARSE, EventLevel::THIRD }, 72 { EVENT_COMPILE_TO_PROGRAM, EventLevel::THIRD }, 73 { EVENT_OPTIMIZE_PROGRAM, EventLevel::SECOND }, 74 { EVENT_RESOLVE_DEPS, EventLevel::FIRST }, 75 { EVENT_EMIT_ABC, EventLevel::FIRST }, 76 { EVENT_EMIT_SINGLE_PROGRAM, EventLevel::SECOND }, 77 { EVENT_EMIT_MERGED_PROGRAM, EventLevel::SECOND }, 78 { EVENT_EMIT_CACHE_FILE, EventLevel::SECOND }, 79 }; 80 81 const std::unordered_map<std::string_view, std::string_view> eventParent = { 82 { EVENT_TOTAL, EVENT_GENERATE_ABC }, 83 { EVENT_COMPILE, EVENT_TOTAL }, 84 { EVENT_READ_INPUT_AND_CACHE, EVENT_COMPILE }, 85 { EVENT_COMPILE_ABC_FILE, EVENT_COMPILE }, 86 { EVENT_COMPILE_ABC_FILE_RECORD, EVENT_COMPILE_ABC_FILE }, 87 { EVENT_UPDATE_ABC_PKG_VERSION, EVENT_COMPILE_ABC_FILE }, 88 { EVENT_UPDATE_ABC_PROGRAM_STRING, EVENT_COMPILE_ABC_FILE }, 89 { EVENT_UPDATE_ABC_PROG_CACHE, EVENT_COMPILE_ABC_FILE }, 90 { EVENT_COMPILE_FILE, EVENT_COMPILE }, 91 { EVENT_PARSE, EVENT_COMPILE_FILE }, 92 { EVENT_COMPILE_TO_PROGRAM, EVENT_COMPILE_FILE }, 93 { EVENT_OPTIMIZE_PROGRAM, EVENT_COMPILE }, 94 { EVENT_RESOLVE_DEPS, EVENT_TOTAL }, 95 { EVENT_EMIT_ABC, EVENT_TOTAL }, 96 { EVENT_EMIT_SINGLE_PROGRAM, EVENT_EMIT_ABC }, 97 { EVENT_EMIT_MERGED_PROGRAM, EVENT_EMIT_ABC }, 98 { EVENT_EMIT_CACHE_FILE, EVENT_EMIT_ABC }, 99 }; 100 101 typedef void (*TimeStartFunc)(const std::string_view event, std::string fileName); 102 typedef void (*TimeEndFunc)(const std::string_view event, std::string fileName); 103 typedef std::chrono::time_point<std::chrono::steady_clock> TimePoint; 104 105 struct TimePointRecord { 106 TimePoint startTime; 107 TimePoint endTime; 108 }; 109 110 struct TimeRecord { 111 std::unordered_map<std::string, TimePointRecord> timePoints; // pair of {fileName, TimePointRecord} 112 std::string event; 113 int level; 114 }; 115 116 class Timer { 117 public: 118 static void InitializeTimer(std::string &perfFile); 119 static void PrintTimers(); 120 121 static TimeStartFunc timerStart; 122 static TimeEndFunc timerEnd; 123 124 private: 125 static void TimerStartImpl(const std::string_view event, std::string fileName = ""); 126 static void TimerEndImpl(const std::string_view event, std::string fileName = ""); 127 static void TimerStartDoNothing(const std::string_view event, std::string fileName = "") {} 128 static void TimerEndDoNothing(const std::string_view event, std::string fileName = "") {} 129 static void PrintJson(); 130 static void PrintTxt(); 131 132 static std::unordered_map<std::string_view, TimeRecord> timers_; // pair of {event, TimeRecord} 133 static std::vector<std::string_view> events_; 134 static std::mutex mutex_; 135 static std::string perfFile_; 136 }; 137 138 } // namespace panda 139 140 #endif // LIBPANDABASE_UTILS_TIMERS_H 141