2017-12-21 07:45:38 +08:00
|
|
|
/*
|
2024-01-22 15:28:27 +08:00
|
|
|
* Copyright (C) 2018-2024 Intel Corporation
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
2019-02-27 18:39:32 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2017-12-21 07:45:38 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2020-02-24 05:44:01 +08:00
|
|
|
#include "shared/source/utilities/timer_util.h"
|
2019-02-27 18:39:32 +08:00
|
|
|
|
2017-12-21 07:45:38 +08:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2017-12-21 07:45:38 +08:00
|
|
|
class PerfProfiler {
|
|
|
|
|
|
|
|
struct SystemLog {
|
|
|
|
unsigned int id;
|
|
|
|
long long start;
|
|
|
|
unsigned long long time;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
struct LogBuilder {
|
|
|
|
static void write(std::ostream &str, long long start, long long end, long long span, unsigned long long totalSystem, const char *function);
|
|
|
|
static void read(std::istream &str, long long &start, long long &end, long long &span, unsigned long long &totalSystem, std::string &function);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SysLogBuilder {
|
|
|
|
static void write(std::ostream &str, long long start, unsigned long long time, unsigned int id);
|
|
|
|
static void read(std::istream &str, long long &start, unsigned long long &time, unsigned int &id);
|
|
|
|
};
|
|
|
|
|
|
|
|
static void readAndVerify(std::istream &stream, const std::string &token);
|
|
|
|
|
2021-10-21 20:37:31 +08:00
|
|
|
PerfProfiler(int id, std::unique_ptr<std::ostream> &&logOut = {nullptr},
|
|
|
|
std::unique_ptr<std::ostream> &&sysLogOut = {nullptr});
|
2017-12-21 07:45:38 +08:00
|
|
|
~PerfProfiler();
|
|
|
|
|
|
|
|
void apiEnter() {
|
|
|
|
totalSystemTime = 0;
|
|
|
|
systemLogs.clear();
|
|
|
|
systemLogs.reserve(20);
|
2023-04-27 17:50:55 +08:00
|
|
|
apiTimer.start();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void apiLeave(const char *func) {
|
2023-04-27 17:50:55 +08:00
|
|
|
apiTimer.end();
|
|
|
|
logTimes(apiTimer.getStart(), apiTimer.getEnd(), apiTimer.get(), totalSystemTime, func);
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void logTimes(long long start, long long end, long long span, unsigned long long totalSystem, const char *function);
|
|
|
|
void logSysTimes(long long start, unsigned long long time, unsigned int id);
|
|
|
|
|
|
|
|
void systemEnter() {
|
2023-04-27 17:50:55 +08:00
|
|
|
systemTimer.start();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void systemLeave(unsigned int id) {
|
2023-04-27 17:50:55 +08:00
|
|
|
systemTimer.end();
|
|
|
|
logSysTimes(systemTimer.getStart(), systemTimer.get(), id);
|
|
|
|
totalSystemTime += systemTimer.get();
|
2017-12-21 07:45:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream *getLogStream() {
|
|
|
|
return logFile.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream *getSystemLogStream() {
|
|
|
|
return sysLogFile.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static PerfProfiler *create(bool dumpToFile = true);
|
|
|
|
static void destroyAll();
|
|
|
|
|
|
|
|
static int getCurrentCounter() {
|
|
|
|
return counter.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
static PerfProfiler *getObject(unsigned int id) {
|
|
|
|
return objects[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const unsigned int objectsNumber = 4096;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static std::atomic<int> counter;
|
|
|
|
static PerfProfiler *objects[PerfProfiler::objectsNumber];
|
2023-04-27 17:50:55 +08:00
|
|
|
Timer apiTimer;
|
|
|
|
Timer systemTimer;
|
2022-05-10 01:40:30 +08:00
|
|
|
unsigned long long totalSystemTime = 0;
|
2017-12-21 07:45:38 +08:00
|
|
|
std::unique_ptr<std::ostream> logFile;
|
|
|
|
std::unique_ptr<std::ostream> sysLogFile;
|
|
|
|
std::vector<SystemLog> systemLogs;
|
|
|
|
};
|
2019-03-26 18:59:46 +08:00
|
|
|
}; // namespace NEO
|