Principe 1727d6416a Add Timer
Issue: #IALBFJ

Signed-off-by: Principe <zhouyuanzhu@h-partners.com>
Change-Id: I195cf9b2dd53801f18404c698119dc9e2f406343
2024-09-23 11:32:32 +08:00

118 lines
4.7 KiB
C++

/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIBPANDABASE_UTILS_TIMERS_H
#define LIBPANDABASE_UTILS_TIMERS_H
#include <chrono>
#include <mutex>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>
namespace panda {
// Event list
constexpr std::string_view EVENT_TOTAL = "Total";
constexpr std::string_view EVENT_COMPILE = "Compile";
constexpr std::string_view EVENT_RESOLVE_DEPS = "Resolve file dep relations";
constexpr std::string_view EVENT_EMIT_ABC = "Emit abc";
constexpr std::string_view EVENT_READ_INPUT_AND_CACHE = "Read input and cache";
constexpr std::string_view EVENT_COMPILE_FILE = "Compile file";
constexpr std::string_view EVENT_COMPILE_ABC_FILE = "Compile abc file";
constexpr std::string_view EVENT_COMPILE_ABC_FILE_RECORD = "Compile abc file record";
constexpr std::string_view EVENT_UPDATE_ABC_PKG_VERSION = "Update abc package version";
constexpr std::string_view EVENT_UPDATE_ABC_PROGRAM_STRING = "Update abc program string";
constexpr std::string_view EVENT_UPDATE_ABC_PROG_CACHE = "Update abc program cache";
constexpr std::string_view EVENT_OPTIMIZE_PROGRAM = "Optimize program";
constexpr std::string_view EVENT_PARSE = "Parse";
constexpr std::string_view EVENT_COMPILE_TO_PROGRAM = "Compile to program";
constexpr std::string_view EVENT_EMIT_SINGLE_PROGRAM = "Emit single program";
constexpr std::string_view EVENT_EMIT_MERGED_PROGRAM = "Emit merged program";
constexpr std::string_view EVENT_EMIT_CACHE_FILE = "Emit cache file";
// Event level, indicating the level of an event, where each event is a sub-event of the nearest preceding level event.
// The top-level event's level is 0
enum EventLevel {
ZEROTH = 0,
FIRST,
SECOND,
THIRD,
FORTH,
FIFTH,
SIXTH
};
// Holds pairs of {event, level} according to the order during compilation process.
// When adding new events later, please add them in the compilation order for ease of reading and maintenance.
const std::unordered_map<std::string_view, EventLevel> eventMap = {
{ EVENT_TOTAL, EventLevel::ZEROTH },
{ EVENT_COMPILE, EventLevel::FIRST },
{ EVENT_READ_INPUT_AND_CACHE, EventLevel::SECOND },
{ EVENT_COMPILE_ABC_FILE, EventLevel::SECOND },
{ EVENT_COMPILE_ABC_FILE_RECORD, EventLevel::FORTH },
{ EVENT_UPDATE_ABC_PKG_VERSION, EventLevel::THIRD },
{ EVENT_UPDATE_ABC_PROGRAM_STRING, EventLevel::THIRD },
{ EVENT_UPDATE_ABC_PROG_CACHE, EventLevel::THIRD },
{ EVENT_COMPILE_FILE, EventLevel::SECOND },
{ EVENT_PARSE, EventLevel::THIRD },
{ EVENT_COMPILE_TO_PROGRAM, EventLevel::THIRD },
{ EVENT_OPTIMIZE_PROGRAM, EventLevel::SECOND },
{ EVENT_RESOLVE_DEPS, EventLevel::FIRST },
{ EVENT_EMIT_ABC, EventLevel::FIRST },
{ EVENT_EMIT_SINGLE_PROGRAM, EventLevel::SECOND },
{ EVENT_EMIT_MERGED_PROGRAM, EventLevel::SECOND },
{ EVENT_EMIT_CACHE_FILE, EventLevel::SECOND },
};
typedef void (*TimeStartFunc)(const std::string_view event, std::string fileName);
typedef void (*TimeEndFunc)(const std::string_view event, std::string fileName);
typedef std::chrono::time_point<std::chrono::steady_clock> TimePoint;
struct TimePointRecord {
TimePoint startTime;
TimePoint endTime;
};
struct TimeRecord {
std::unordered_map<std::string, TimePointRecord> timePoints; // pair of {fileName, TimePointRecord}
std::string event;
int level;
};
class Timer {
public:
static void InitializeTimer(std::string &perfFile);
static void PrintTimers();
static TimeStartFunc timerStart;
static TimeEndFunc timerEnd;
private:
static void TimerStartImpl(const std::string_view event, std::string fileName = "");
static void TimerEndImpl(const std::string_view event, std::string fileName = "");
static void TimerStartDoNothing(const std::string_view event, std::string fileName = "") {}
static void TimerEndDoNothing(const std::string_view event, std::string fileName = "") {}
static std::unordered_map<std::string_view, TimeRecord> timers_; // pair of {event, TimeRecord}
static std::vector<std::string_view> events_;
static std::mutex mutex_;
static std::string perfFile_;
};
} // namespace panda
#endif // LIBPANDABASE_UTILS_TIMERS_H