2016-06-05 16:08:55 +02:00
|
|
|
#include "HyperionConfig.h"
|
2016-09-03 15:54:33 +02:00
|
|
|
#include <utils/Profiler.h>
|
|
|
|
#include <utils/FileUtils.h>
|
2016-06-05 16:08:55 +02:00
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
struct StopWatchItem {
|
|
|
|
const char* sourceFile;
|
|
|
|
const char* func;
|
|
|
|
unsigned int line;
|
|
|
|
clock_t startTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
static unsigned int blockCounter = 0;
|
2017-03-04 22:17:42 +01:00
|
|
|
static std::map<QString,StopWatchItem> GlobalProfilerMap;
|
2016-06-05 16:08:55 +02:00
|
|
|
Logger* Profiler::_logger = nullptr;
|
|
|
|
|
|
|
|
double getClockDelta(clock_t start)
|
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
return ((double)(clock() - start) / CLOCKS_PER_SEC);
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
|
2016-11-26 22:34:46 +01:00
|
|
|
Profiler::Profiler(const char* sourceFile, const char* func, unsigned int line)
|
|
|
|
: _file(sourceFile)
|
|
|
|
, _func(func)
|
|
|
|
, _line(line)
|
|
|
|
, _blockId(blockCounter++)
|
|
|
|
, _startTime(clock())
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
|
|
|
Profiler::initLogger();
|
|
|
|
_logger->Message(Logger::DEBUG,_file,_func,_line,">>> enter block %d", _blockId);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Profiler::~Profiler()
|
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
_logger->Message( Logger::DEBUG, _file,_func, _line, "<<< exit block %d, executed for %f s", _blockId, getClockDelta(_startTime));
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profiler::initLogger()
|
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
if (_logger == nullptr)
|
2016-06-05 16:08:55 +02:00
|
|
|
_logger = Logger::getInstance("PROFILER", Logger::DEBUG);
|
|
|
|
}
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
void Profiler::TimerStart(const QString timerName, const char* sourceFile, const char* func, unsigned int line)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
std::pair<std::map<QString,StopWatchItem>::iterator,bool> ret;
|
2016-06-05 16:08:55 +02:00
|
|
|
Profiler::initLogger();
|
|
|
|
|
|
|
|
StopWatchItem item = {sourceFile, func, line};
|
|
|
|
|
|
|
|
ret = GlobalProfilerMap.emplace(timerName, item);
|
2020-06-28 23:12:22 +02:00
|
|
|
if (!ret.second)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
if (ret.first->second.sourceFile == sourceFile && ret.first->second.func == func && ret.first->second.line == line)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
_logger->Message(Logger::DEBUG, sourceFile, func, line, "restart timer '%s'", QSTRING_CSTR(timerName));
|
2016-06-05 16:08:55 +02:00
|
|
|
ret.first->second.startTime = clock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' started in multiple locations. First occurence %s:%d:%s()",
|
2020-06-28 23:12:22 +02:00
|
|
|
QSTRING_CSTR(timerName), FileUtils::getBaseName(ret.first->second.sourceFile).toLocal8Bit().constData(), ret.first->second.line, ret.first->second.func);
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
_logger->Message(Logger::DEBUG, sourceFile, func, line, "start timer '%s'", QSTRING_CSTR(timerName));
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-04 22:17:42 +01:00
|
|
|
void Profiler::TimerGetTime(const QString timerName, const char* sourceFile, const char* func, unsigned int line)
|
2016-06-05 16:08:55 +02:00
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
std::map<QString,StopWatchItem>::iterator ret = GlobalProfilerMap.find(timerName);
|
2016-06-05 16:08:55 +02:00
|
|
|
Profiler::initLogger();
|
|
|
|
if (ret != GlobalProfilerMap.end())
|
|
|
|
{
|
2017-03-04 22:17:42 +01:00
|
|
|
_logger->Message(Logger::DEBUG, sourceFile, func, line, "timer '%s' started at %s:%d:%s() took %f s execution time until here", QSTRING_CSTR(timerName),
|
2020-06-28 23:12:22 +02:00
|
|
|
FileUtils::getBaseName(ret->second.sourceFile).toLocal8Bit().constData(), ret->second.line, ret->second.func, getClockDelta(ret->second.startTime));
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-28 23:12:22 +02:00
|
|
|
_logger->Message(Logger::DEBUG, sourceFile, func, line, "ERROR timer '%s' not started", QSTRING_CSTR(timerName));
|
2016-06-05 16:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|