简体   繁体   English

如何获取程序执行的指令数?

[英]How can I get the number of instructions executed by a program?

I have written and cross compiled a small c++ program, and I could run it in an ARM or a PC. 我编写并交叉编译了一个小型的c ++程序,我可以在ARM或PC上运行它。 Since ARM and a PC have different instruction set architectures, I wanna to compare them. 由于ARM和PC具有不同的指令集架构,我想比较它们。 Is that possible for me to get the number of executed instructions in this c++ program for both ISAs? 我可以在这个c ++程序中为两个ISA获取执行指令的数量吗?

What you need is a profiler. 你需要的是一个分析器。 perf would be one easy to use. perf将是一个易于使用。 It will give you the number of instructions that executed , which is the best metric if you want to compare ISA efficiency. 它将为您提供执行指令数,如果您想比较ISA效率,这是最佳指标。

Check the tutorial here . 这里查看教程。

You need to use: perf stat ./your binary 你需要使用: perf stat ./your binary

Look for instructions metric. 查找指令指标。 This approach uses a register in your CPU's performance monitoring unit - PMU - that counts the number of instructions. 这种方法在CPU的性能监视单元PMU中使用一个寄存器来计算指令数。

Are you trying to get the number of static instructions or dynamic instructions? 您是否尝试获取静态指令或动态指令的数量? So, for instance, if you have the following loop (pseudocode): 因此,例如,如果您有以下循环(伪代码):

for (i 0 to N):
 a[i] = b[i] + c[i]

Static instruction count will be just under 10 instructions, give or take based on your ISA, but the dynamic count would depend on N, on the branch prediction implementation and so on. 静态指令计数将在10个指令之下,根据您的ISA给出或取得,但动态计数将取决于N,在分支预测实现上等等。

So for static count I would recommend using objdump, as per recommendations in the comments. 因此,对于静态计数,我建议使用objdump,根据评论中的建议。 You can find the entry and exit labels of your subroutine and count the number of instructions in between. 您可以找到子例程的入口和出口标签,并计算两者之间的指令数。

For dynamic instruction count, I would recommend one of two things: 对于动态指令计数,我建议使用以下两种方法之一:

  • You can simulate running that code using an instruction set simulator (there are open source ISA simulators for both ARM and x86 out there - Gem5 for instance implements both of them, there are others out there that support one or the other. 您可以使用指令集模拟器模拟运行该代码(有ARM和x86的开源ISA模拟器 - 例如Gem5实现它们两者,还有其他支持一个或另一个。
  • Your second option is to run this natively on the target system and setup performance counters in the CPU to report dynamic instruction count. 您的第二个选择是在目标系统上本机运行并在CPU中设置性能计数器以报告动态指令计数。 You would reset before executing your code, and read it afterwards (there might be some noise here associated with calling your subroutine and exiting, but you should be able to isolate that out) 您可以在执行代码之前重置,然后再读取它(此处可能存在一些与调用子程序和退出相关的噪声,但您应该能够将其隔离出来)

Hope this helps :) 希望这可以帮助 :)

objdump -dw mybinary | wc -l

On Linux and friends, this gives a good approximation of the number of instructions in an executable, library or object file. 在Linux和朋友中,这给出了可执行文件,库或目标文件中指令数量的良好近似值。 This is a static count, which is of course completely different than runtime behavior. 这是一个静态计数,当然与运行时行为完全不同。

Linux: valgrind --tool=callgrind ./program 1 > /dev/null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复我的 C++ 程序? 该程序应该使用 function 获取字符串中的空格数 - How can I fix my C++ program? The program is supposed to get the number of spaces in a string with a function 如何在程序中查找非法指令? - How to find illegal instructions in a program? 如何计算或查看编译时生成的指令数量? - How do I calculate or view the number of instructions generated when compiled? 我怎样才能获得程序的状态? - How can I get the state of a program? 如果我使用 ./program_name 执行我的程序,如何在 C++ 中获取 file_name - How to get the file_name in C++ if I executed my program with ./program_name<file_name 如何从程序中设置 OpenMP 线程数? - How can I set the number of OpenMP threads from within the program? 如何使这个并行求和函数使用向量指令? - How can I make this parallel sum function use vector instructions? 如何记录或重播在崩溃之前立即执行的行或指令 - How to log or replay lines or instructions executed immediately before a crash 可以仅通过使用加减运算来检查数字是奇数还是偶数? - can I check if a number is odd or even by using add and subract instructions only? 如何获得在C ++中执行函数的确切次数? - How to get the exact number of times that a function is executed in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM