CPU Load Monitor

来源:互联网 发布:linux mv 文件 编辑:程序博客网 时间:2024/05/18 03:05

统计CPU Load,有这几种方法。

1. IDL task

需要一个专门的task来完成这个工作,这里我们举例将这个task称为“IDL”。

IDL task中是一个循环,循环中只做一件事,就是对一个全局计数器累加。

在系统启动时,context是xx task?此时的优先级是0?
在xx task启动系统的正常功能之前,将task IDL的优先级设为1,然后xx task用taskDelay()让出CPU,让IDL task跑一段时间,用sysTimestampLock()记录开始和结束的时间。这样就得到在IDL task完全占有CPU的情况下,计数的效率,这里我们将这个值记为“A”。

然后将IDL task的优先级设为254。(为什么不是255?

在系统正常功能启动之后,每隔一段时间查询IDL task维护的计数器,计算这段时间IDL task计数的效率,我们将此时的效率记为“B”。当然B肯定是小于A的。
所以可以得到IDL task在这段时间内占用CPU的比率是“B/A”,从而系统正常功能部分占用CPU的比率是“1-B/A”。
这里可以是在周期固定的中断函数中,进行IDL task计数器的查询。

From VxWorks FAQ, for reference:

Q: How can I monitor the processor-load?

A: The code we use for CPU monitoring is here - I've probably missed the odd definition from earlier in the file but I think they should be pretty self-explanatory. It creates a task at priority 255 = idle task level, which eats CPU and times itself. The routine getCpu() returns a double which is CPU loading as a %. You have to run the initialize function at a high priority so it gets its auto-calibration done properly.
(From: Andrew Johnson, anj@aps.anl.gov)

Another example.
Based on a discussion about this subject that I read here a few weeks ago, I wrotethis idle time monitor that I have been testing. It seems to work very well.
You can add it to you build, then start it with nothing running

-> startCPUIdleMonitor

then spawn the task that prints the CPU Idle Time

-> period 10, getCPUIdlefloat

You will need to adjust the ZERO_LOAD_IDLE_COUNTER for your CPU. For my 50Mhz 860T it is around 377000.

2. Spylib in VxWorks

这个功能依赖于auxiliary clock,必须有auxiliary clock才可以使用这个library。
因为这个模块使用了auxiliary clock的中断,每个tick都进入中断,在中断中查看当前的context,并记录在对应的结果。
我理解auxiliary clock应该是给CPU的一个时钟输入,也就是需要硬件的支持才行。

TCB中的taskTicks和taskIncTicks就是专门用于spylib模块中的,taskTicks是记录这个task总的运行时间,taskIncTicks是记录这个task在上一个slice中运行的时间。

这个模块可以统计中断、kernel和各个task的运行时间。

3. Switch Hook

这个方法的原理是在每次switch时,进入hook的函数进行记录和计算。这里我们举例将这个模称为“Monitor”。

monitorTaskSwitchHook()函数中,计算被打断task在上一个slice中运行的时间,在一个专属于被打断task的变量中保存这个task总的运行时间;
记录当前时间(用于之后时间差的计算),将记录保存到专属于换到的task的变量中。一般这个“专属于某个task的变量”可以是一个结构体,并且可以是动态申请的,而它的地址可以保存在这个task的TCB(Task Control Block )中。

所以可以hook这样的两个函数,monitorTaskCreateHook()用来在creat task时申请专属于这个task的变量的空间,在monitorTaskDeleteHook()时释放对应的空间。
Monitor Task中每隔一段时间(可以用taskDelay()来实现),获取所有task的统计数据(可以用taskIdListGet()来实现),然后进行排序,最后输出结果。