简体   繁体   English

测量进程消耗的CPU时钟

[英]Measuring CPU clocks consumed by a process

I have written a program in C. Its a program created as result of a research. 我用C编写了一个程序。它是一个由研究创建的程序。 I want to compute exact CPU cycles which program consumes. 我想计算程序消耗的精确CPU周期。 Exact number of cycles. 确切的周期数。 Any idea how can I find that? 知道我怎么能找到它?

The valgrind tool cachegrind ( valgrind --tool=cachegrind ) will give you a detailed output including the number of instructions executed, cache misses and branch prediction misses. valgrind工具cachegrindvalgrind --tool=cachegrind )将为您提供详细的输出,包括执行的指令数,缓存未命中和分支预测未命中。 These can be accounted down to individual lines of assembler, so in principle (with knowledge of your exact architecture) you could derive precise cycle counts from this output. 这些可以归结为汇编程序的各个行,因此原则上(了解您的确切架构)您可以从此输出中获得精确的循环计数。

Know that it will change from execution to execution, due to cache effects. 知道由于缓存效应,它会从执行变为执行。

The documentation for the cachegrind tool is here . cachegrind工具的文档在这里

No you can't. 不,你不能。 The concept of a 'CPU cycle' is not well defined. “CPU周期”的概念没有明确定义。 Modern chips can run at multiple clock rates, and different parts of them can be doing different things at different times. 现代芯片可以以多种时钟速率运行,并且它们的不同部分可以在不同时间执行不同的操作。

The question of 'how many total pipeline steps' might in some cases be meaningful, but there is not likely to be a way to get it. 在某些情况下,“有多少总管道步骤”的问题可能是有意义的,但是不太可能有办法实现它。

Try OProfile . 尝试OProfile It use various hardware counters on the CPU to measure the number of instructions executed and how many cycles have passed. 它使用CPU上的各种硬件计数器来测量执行的指令数和已经过的循环数。 You can see an example of it's use in the article, Memory part 7: Memory performance tools . 您可以在文章Memory Memory 7:Memory performance tools中看到它的一个例子。

I am not entirely sure that I know exactly what you're trying to do, but what can be done on modern x86 processors is to read the time stamp counter (TSC) before and after the block of code you're interested in. On the assembly level, this is done using the RDTSC instruction, which gives you the value of the TSC in the edx:eax register pair. 我并不完全确定我确切知道你要做什么,但现代x86处理器可以做的是在你感兴趣的代码块之前和之后读取时间戳计数器 (TSC)。汇编级别,这是使用RDTSC指令完成的,该指令为您提供edx:eax寄存器对中的TSC值。

Note however that there are certain caveats to this approach, eg if your process starts out on CPU0 and ends up on CPU1, the result you get from RDTSC will refer to the specific processor core that executed the instruction and hence may not be comparable. 但是请注意,这种方法有一些注意事项,例如,如果您的进程从CPU0开始并最终在CPU1上,您从RDTSC获得的结果将指向执行该指令的特定处理器内核,因此可能无法比较。 (There's also the lack of instruction serialisation with RDTSC , but in this context here, I don't think that's so much of an issue.) (还有缺乏RDTSC的指令序列化,但在这种情况下,我不认为这是一个很大的问题。)

Sorry, but no, at least not for most practical purposes -- it's simply not possible with most normal OSes. 对不起,但不,至少不是出于大多数实际目的 - 大多数普通操作系统根本不可能。 Just for example, quite a few OSes don't do a full context switch to handle an interrupt, so the time spent servicing a interrupt can and often will appear to be time spent in whatever process was executing when the interrupt occurred. 例如,相当多的操作系统不执行完整的上下文切换来处理中断,因此服务中断所花费的时间通常似乎是在中断发生时执行的任何进程中花费的时间。

The "not for practical purposes" would indicate the possibility of running your program under a cycle accurate simulator. “不是出于实际目的”表示可以在循环精确模拟器下运行程序。 These are available, but mostly for CPUs used primarily in real-time embedded systems, NOT for anything like a full-blown PC. 这些都是可用的,但主要用于主要用于实时嵌入式系统的CPU,而不是像完整的PC那样。 Worse, they (generally) aren't for running anything like a full-blown OS, but for code that runs on the "bare metal." 更糟糕的是,它们(通常)不是用于运行完整的操作系统,而是用于运行在“裸机”上的代码。

In theory, you might be able to do something with a virtual machine running something like Windows or Linux -- but I don't know of any existing virtual machine that attempts to, and it would be decidedly non-trivial and probably have pretty serious consequences in performance as well (to put it mildly). 从理论上讲,您可能可以使用运行Windows或Linux等功能的虚拟机执行某些操作 - 但我不知道任何现有的虚拟机是否会尝试,而且这些虚拟机肯定是非常重要的,并且可能非常严重绩效的后果(温和地说)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM