如何在现有 c++ 代码中以功能方式实现遥测
How to implement telemetrics functionally in existing c++ code
我需要在现有的 c++ 代码中以功能方式实现遥测,以分析哪些方法执行时间过长。可以在文本文件中记录时间戳。这是正确的方法还是有任何其他方法可以在 c++ 中继续。由于我是vc++的新手,请提供您的建议。
我开发了最简单的代码来C++代码自我分析。它以调用树的形式收集和显示统计信息。您可以根据需要对其进行修改。要使用,您只需要在分析块中添加TRACE
宏。
#pragma once
#include <vector>
#include <string>
#include <windows.h>
typedef double Time;
typedef unsigned int uint;
#ifndef FLATTEN_RECURSION
#define FLATTEN_RECURSION 1
#endif
class Profiler
{
static Time getCurrentTime()
{
LARGE_INTEGER time;
LARGE_INTEGER freq;
QueryPerformanceCounter(&time);
QueryPerformanceFrequency(&freq);
return 1.0 * time.QuadPart / freq.QuadPart;
}
struct Entry
{
Time fullTime_ = Time(0);
const char* name_;
uint started_ = 0;
Time startTime_ = Time(0);
Entry(const char* name) { name_ = name; }
void start()
{
if (!started_) startTime_ = getCurrentTime();
started_++;
}
void stop()
{
started_--;
if (!started_)
fullTime_ += getCurrentTime() - startTime_;
}
};
struct CallTreeNode
{
const Entry& entry_;
std::vector<CallTreeNode> childs_;
CallTreeNode(const Entry& entry) : entry_(entry)
{}
CallTreeNode& addCall(const Entry& entry)
{
for (auto& itr : childs_)
if (&itr.entry_ == &entry)
return itr;
childs_.push_back(CallTreeNode(entry));
return childs_.back();
}
};
std::vector<CallTreeNode*> callStack_;
Entry rootEntry = Entry("root");
CallTreeNode root = CallTreeNode(rootEntry);
Time duration_;
Profiler()
{
rootEntry.start();
callStack_.push_back(&root);
}
void printTreeImpl(
CallTreeNode& node,
std::string& out,
bool last = true,
const std::string& prefix = "")
{
out += prefix + (last ? 'xC0' : 'xC3') + std::string("xC4xC4")
+ node.entry_.name_ + " rate: "
+ std::to_string(node.entry_.fullTime_ / duration_ * 100)
+ "% full time: " + std::to_string(node.entry_.fullTime_) + 'n';
const int childsNum = (int)node.childs_.size();
for (int i = 0; i < childsNum; i++)
printTreeImpl(node.childs_[i], out, i == (childsNum - 1), prefix + (last ? ' ' : 'xB3') + " ");
}
void forward(const Entry& entry)
{
callStack_.push_back(&callStack_.back()->addCall(entry));
}
void backward(const Entry& entry)
{
callStack_.pop_back();
}
public:
Profiler(Profiler const&) = delete;
void operator=(Profiler const&) = delete;
static Profiler& getInstance()
{
static Profiler instance;
return instance;
}
static Entry newEntry(const char* name)
{
return Entry(name);
}
std::string printCallTree()
{
auto& inst = getInstance();
inst.duration_ = inst.getCurrentTime() - inst.rootEntry.startTime_;
inst.rootEntry.fullTime_ += duration_;
std::string out;
inst.printTreeImpl(root, out);
return out;
}
class ProfilerAutoStopper
{
Entry& entry_;
public:
ProfilerAutoStopper(Entry& entry) : entry_(entry)
{
if (!entry_.started_ || !FLATTEN_RECURSION)
getInstance().forward(entry_);
entry_.start();
}
~ProfilerAutoStopper()
{
entry_.stop();
if (!entry_.started_ || !FLATTEN_RECURSION)
getInstance().backward(entry_);
}
};
};
#define TRACE
static auto pflrEntry = Profiler::newEntry(__FUNCTION__);
Profiler::ProfilerAutoStopper autoStopper(pflrEntry);
使用的主要思想:
- 主探查器类是单例。
- 服务对象
ProfilerAutoStopper
用于在进入和退出块时自动启动和停止计时器。这消除了多个启动/停止错误并正确处理异常。 std::vector<CallTreeNode*> callStack_
堆栈和CallTreeNode
树结构用于构造调用树。
用
void foo()
{
TRACE;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
void recursive(int level)
{
TRACE;
if (--level) recursive(level);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
void bar()
{
TRACE;
foo();
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
int main()
{
{
TRACE;
for (int i = 0; i < 10; i++)
foo();
recursive(10);
bar();
}
std::cout << Profiler::getInstance().printCallTree() << std::endl;
return 0;
}
外
└──root rate: 100.000000% full time: 0.191205
└──main rate: 99.971599% full time: 0.191150
├──foo rate: 62.788339% full time: 0.120054
├──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
│ └──recursive rate: 31.276141% full time: 0.059801
└──bar rate: 11.446139% full time: 0.021886
└──foo rate: 62.788339% full time: 0.120054
使用FLATTEN_RECURSION 1
└──root rate: 100.000000% full time: 0.190720
└──main rate: 99.944164% full time: 0.190614
├──foo rate: 62.658680% full time: 0.119503
├──recursive rate: 31.347459% full time: 0.059786
└──bar rate: 11.477065% full time: 0.021889
└──foo rate: 62.658680% full time: 0.119503
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 如何在c++中为模板函数实例创建快捷方式
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 在c代码之间共享数据的最佳方式
- 类模板的成员功能的定义在单独的TU中完全专业化
- c++ Visual Studio 2015 快捷方式,用于从选择代码中生成功能
- 确定范围是访问虚拟功能的合法方式吗?
- 如何在现有 c++ 代码中以功能方式实现遥测
- 优雅地尝试以特定的方式执行各种功能
- 如何以有效的方式使用boost mpi广播功能
- C ,以并行和协作的方式运行两个功能
- 如何获取窗口的命令行"打开方式.."功能?
- 如何以编程方式启用/禁用Windows功能
- C++ ⎼ 两个相似的功能,但执行方式却大不相同
- SetupDi API:以编程方式控制设备管理器功能
- Windows 8.1使用SHBrowseForFolder功能无法展开快捷方式
- 如何以非内联方式定义模板覆盖的功能
- Qt:在QThread中实现睡眠功能的最佳方式是什么