我在SO上看到这篇文章,其中包含C代码以获取最新的CPU周期数:
基于CPU周期计算的C/C++ Linux x86_64中的分析
有没有办法在C++中使用这个代码(欢迎使用windows和linux解决方案)?虽然用C语言编写(而C是C++的一个子集)但我不太确定这段代码是否适用于C++项目,如果没有,如何翻译呢?
我使用的是x86-64
EDIT2:
找到此功能但无法让VS2010识别汇编程序.我需要包含任何内容吗?(我相信我必须换uint64_t到long long窗户......?)
static inline uint64_t get_cycles()
{
uint64_t t;
__asm volatile ("rdtsc" : "=A"(t));
return t;
}
Run Code Online (Sandbox Code Playgroud)
EDIT3:
从上面的代码我得到错误:
"错误C2400:'操作码'中的内联汇编语法错误;找到'数据类型'"
有人可以帮忙吗?
使用Visual Studio,我可以从处理器读取时钟周期计数,如下所示.我如何与GCC做同样的事情?
#ifdef _MSC_VER // Compiler: Microsoft Visual Studio
#ifdef _M_IX86 // Processor: x86
inline uint64_t clockCycleCount()
{
uint64_t c;
__asm {
cpuid // serialize processor
rdtsc // read time stamp counter
mov dword ptr [c + 0], eax
mov dword ptr [c + 4], edx
}
return c;
}
#elif defined(_M_X64) // Processor: x64
extern "C" unsigned __int64 __rdtsc();
#pragma intrinsic(__rdtsc)
inline uint64_t clockCycleCount()
{
return __rdtsc();
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud) 我正在比较一些系统调用,我从/向内存读/写.是否有任何API定义来测量页面错误(页面输入/输出)C?
我找到了这个库 libperfstat.a,但它是为了AIX,我找不到任何关于linux的东西.
编辑:
我知道linux中的time&perf-stat命令,只是探索我是否可以在C程序中使用.
我正在perfUbuntu 20.04 上进行分析(尽管我可以使用任何其他免费工具)。它允许在 CLI 中传递延迟,以便事件收集在程序启动后的某个时间后开始。然而,这个时间变化很大(1000 秒中有 20 秒),并且有一些我也不感兴趣的尾部计算。
因此,最好从我的程序中调用一些 API 来启动perf我感兴趣的代码片段的事件收集,然后在代码完成后停止收集。
在循环中运行代码并不是真正的选择,因为有大约 30 秒的初始化阶段和 10 秒的测量阶段,我只对后者感兴趣。
我尝试使用perf statwithLD_PRELOAD作为可执行文件的前缀,例如:
perf stat LD_PRELOAD=$PWD/../user/preload.so ./write 1
Run Code Online (Sandbox Code Playgroud)
似乎对 perf 不起作用,有什么办法可以实现吗?
我正在尝试使用 linux perf 工具在特定功能期间监视性能统计信息。
我正在按照https://perf.wiki.kernel.org/index.php/Jolsa_Features_Togle_Event#Example_-_using_u.28ret.29probes给出的说明进行操作
我试图获得一个简单的 C 程序的指令数。(如下所示)
1) 我的简单 C 代码
#include<stdio.h>
int sum=0;
int i=0;
void func(void)
{
for(i=0;i<100;i++)
{
sum=sum+i;
}
}
int main(void)
{
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
2) 编译和添加探针
root@sunimal-laptop:/home/sunimal/temp# gcc -o ex source.c
root@sunimal-laptop:/home/sunimal/temp# perf probe -x ./ex entry=func
Added new event:
probe_ex:entry (on 0x4ed)
You can now use it in all perf tools, such as:
perf record -e probe_ex:entry -aR sleep 1
root@sunimal-laptop:/home/sunimal/temp# perf probe -x ./ex exit=func%return
Added new event: …Run Code Online (Sandbox Code Playgroud) 我有一个复杂的应用程序,分多个阶段执行。我只想介绍其中一个阶段。
C++ 应用程序在 Linux x86-64 上运行。
该程序需要几分钟才能运行。如果我使用 perf 来分析整个事情,则生成的数据集太大,无法处理 perf 报告。然而,此时我只对分析程序的一个阶段的执行感兴趣,该阶段可能花费总时间的 1/3。也许这个数据集更容易让性能报告。
理想情况下,我想要类似“向自己发送 SIGUSR1 来开始分析,并发送 SIGUSR2 来停止它”。那时,我可以轻松地描述我想要的配置信息的执行阶段。
我总是可以使用 SIGPROF 编写自己的(尽管是基本的)分析器,但是有没有办法可以使用现有的工具(例如 perf)来做到这一点?
c++ ×4
linux ×4
perf ×4
c ×3
profiling ×2
rdtsc ×2
x86 ×2
api ×1
gcc ×1
ld-preload ×1
linux-kernel ×1
page-fault ×1
performance ×1