Re: 请教一下内核态下如何进行时间测量?

88 views
Skip to first unread message

helight.xu

unread,
Oct 9, 2008, 8:41:05 AM10/9/08
to Eveson, linux-...@zh-kernel.org
Eveson wrote:
> 我写了一个驱动,想在内核里测量一下这个驱动某段代码运行了多长时间,请问如何在内核态下测量时间?
>
>
使用jiffies应该是最基本的方法,其它的方法应该在最终还是使用jiffies的。

--
---------------------------------
Zhenwen Xu - Open and Free
Home Page: http://dim4.cn
Gtalk: helig...@gmail.com

_______________________________________________
Linux 内核开发中文邮件列表
Linux-...@zh-kernel.org
http://zh-kernel.org/mailman/listinfo/linux-kernel
Linux 内核开发中文社区: http://zh-kernel.org

PGP__GH

unread,
Oct 9, 2008, 7:43:26 PM10/9/08
to helight.xu, linux-...@zh-kernel.org
2008/10/9 helight.xu <helig...@gmail.com>:

> Eveson wrote:
>> 我写了一个驱动,想在内核里测量一下这个驱动某段代码运行了多长时间,请问如何在内核态下测量时间?
>>
>>
> 使用jiffies应该是最基本的方法,其它的方法应该在最终还是使用jiffies的。

同意采用jiffies。

--
The best way to predict the future is to create it!
As long as you work hard,the miracles will happen sooner or later.

Eveson

unread,
Oct 10, 2008, 6:06:01 AM10/10/08
to hank. peng, linux-...@zh-kernel.org
现在已经放弃如下两种方法:
1、get_cycles()在arm平台上没有数据,在x86上可以获得数据。
2、jiffies精度不够,而且是用的系统定义的频率,不是cpu的时钟频率。
现在用的是do_gettimeofday().还不知道效果怎么样,还有没有其他方法?像有几位老大谈到的实时钟,rtc,我都没有接触过,能否给详细说一下?多谢啦~~~

在08-10-10,Eveson <huyo...@gmail.com> 写道:
>
>
> 代码是执行很多遍的,然后取平均值,关键是我执行一次,需要计时的函数要调用很多次,每一次执行时间很短,如果测试方法的精度不够,每一次的时间测量不出来,那么也无法累计起来执行一次的总时间,多执行几遍也无济于事啊。所以需要用一个精确度很高的方法才行。
>
> 在08-10-10,hank. peng <peng...@gmail.com> 写道:
>>
>> 你的代码多执行几遍,然后取平均不行吗?
>>
>> 2008/10/10 Eveson <huyo...@gmail.com>:
>> >
>> 如果是按照系统的时钟频率的话,jiffies就不行了,系统定义的100,就是说0.01秒jiffies就增加一次,我的算法被调用一次可能都少于0.01秒,因此时间累计不起来啊,只能用别的办法了。
>> >
>> > 在08-10-10,ye janboe <janb...@gmail.com> 写道:
>> >>
>> >> jffies 是以系统时钟来定,跟CPU的频率没有关系
>> >>
>> >> 2008/10/10 Eveson <huyo...@gmail.com>:
>> >> > RTC如何用,我现在的环境是linux+arm平台。
>> >> > 另外我现在得到jiffies了,如何换算成时间秒,我的ARM平台的频率是200MHZ,但是linux系统的在param.h中定义的hz
>> >> > 是100,按哪个换算,换算公式是什么?
>> >> >
>> >> >
>> >> > 在08-10-10,Wang Jian <la...@linux.net.cn> 写道:
>> >> >>
>> >> >> http://sources.redhat.com/ml/systemtap/2006-q3/msg00250.html
>> >> >>
>> >> >> ARM 应该不支持这种计数器,所以返回 0。你可以用别的,比如 RTC。
>> >> >>
>> >> >> Eveson 写道:
>> >> >>
>> >> >>>
>> >> >>>
>> >>
>> 我用了get_cycles()的方法,结果在x86平台上可以统计出来,把程序放到ARM平台上,结果都是0,没有数值,是不是ARM平台不支持get_cycles()?
>> >> >>>
>> >> >>> 在08-10-8,dongas <dong...@gmail.com> 写道:
>> >> >>>
>> >> >>>> 对精度有要求的话也可以用get_cycles。
>> >> >>>>
>> >> >>>> 2008/10/8 Li Yu <raise...@gmail.com>:
>> >> >>>>
>> >> >>>>> 代码的执行时间不太容易测量,我先试过读msr的方法,这种方法够准确,但结果
>> >> >>>>> 却变化非常大,我估计是CPU缓存的因素。
>> >> >>>>>
>> >> >>>>> 你可以使用sampling的方式,这样的话,Oprofile是个不错的工具。
>> >> >>>>>
>> >> >>>>> Eveson 写道:
>> >> >>>>>
>> >> >>>>>> 多谢,我现在用的是do_timesofday(),这个应该也可以把。
>> >> >>>>>>
>> >> >>>>>> 2008/10/8 Ming Lei <tom.l...@gmail.com>
>> >> >>>>>>
>> >> >>>>>> 也可以尝试下ftrace, 参考Documentation/ftrace.txt
>> >> >>>>>>>
>> >> >>>>>>> 2008/10/8 Eveson <huyo...@gmail.com>:
>> >> >>>>>>>
>> >> >>>>>>>> 我写了一个驱动,想在内核里测量一下这个驱动某段代码运行了多长时间,请问如何在内核态下测量时间?
>> >> >>>>>>>>
>> >> >>>>>>>> --
>> >> >>>>>>>> 你能够想到多远就可以走出多远http://eveson.cublog.cn


>> >> >>>>>>>> _______________________________________________
>> >> >>>>>>>> Linux 内核开发中文邮件列表
>> >> >>>>>>>> Linux-...@zh-kernel.org
>> >> >>>>>>>> http://zh-kernel.org/mailman/listinfo/linux-kernel
>> >> >>>>>>>> Linux 内核开发中文社区: http://zh-kernel.org
>> >> >>>>>>>>
>> >> >>>>>>>

>> >> >>>>>>> --
>> >> >>>>>>> Lei Ming


>> >> >>>>>>>
>> >> >>>>>>>
>> >> >>>>>>
>> >> >>>>>> _______________________________________________
>> >> >>>>> Linux 内核开发中文邮件列表
>> >> >>>>> Linux-...@zh-kernel.org
>> >> >>>>> http://zh-kernel.org/mailman/listinfo/linux-kernel
>> >> >>>>> Linux 内核开发中文社区: http://zh-kernel.org
>> >> >>>>>
>> >> >>>>
>> >> >>>
>> >> >>>
>> >> >>>
>> >> >>
>> >> >
>> >> >

>> >> > --
>> >> > 你能够想到多远就可以走出多远http://eveson.cublog.cn


>> >> > _______________________________________________
>> >> > Linux 内核开发中文邮件列表
>> >> > Linux-...@zh-kernel.org
>> >> > http://zh-kernel.org/mailman/listinfo/linux-kernel
>> >> > Linux 内核开发中文社区: http://zh-kernel.org
>> >>
>> >
>> >
>> >

>> > --
>> > 你能够想到多远就可以走出多远http://eveson.cublog.cn


>> > _______________________________________________
>> > Linux 内核开发中文邮件列表
>> > Linux-...@zh-kernel.org
>> > http://zh-kernel.org/mailman/listinfo/linux-kernel
>> > Linux 内核开发中文社区: http://zh-kernel.org
>>
>>
>>

>> --
>> The simplest is not all best but the best is surely the simplest!
>>
>
>
>
> --
> 你能够想到多远就可以走出多远http://eveson.cublog.cn


--
你能够想到多远就可以走出多远http://eveson.cublog.cn

Li Yu

unread,
Oct 10, 2008, 6:23:55 AM10/10/08
to Eveson, linux-...@zh-kernel.org
Eveson 写道:

> 现在已经放弃如下两种方法:
> 1、get_cycles()在arm平台上没有数据,在x86上可以获得数据。
> 2、jiffies精度不够,而且是用的系统定义的频率,不是cpu的时钟频率。
> 现在用的是do_gettimeofday().还不知道效果怎么样,还有没有其他方法?像有几位老大谈到的实时钟,rtc,我都没有接触过,能否给详细说一下?多谢啦~~~

RTC是用来保存系统wall date的,好象一般是用摩托罗拉的什么芯片。这东东的精
度不高,恐怕不能满足你的要求。ARM上的RTC,我就不太清楚了。

查查你的系统中的定时器的datasheet。如果这个精度不够,还看看你的ARM CPU上
有没有什么类似于intel CPU上的performance counter的功能。

再有,实现一个定制的简版oprofile好像不难吧?

实在不行,就计算汇编指令的时间得了。

你真的需要这么精确的结果么?

Xinyu Chen

unread,
Oct 10, 2008, 9:41:09 AM10/10/08
to Eveson, linux-...@zh-kernel.org
get_cycles 是需要你的平台支持的,利用平台的可编程计数器实现clocksource
实在不行,你就oprofile,v6的arch支持。

2008/10/10 Eveson <huyo...@gmail.com>:

--
Best Regards
Xinyu

Reply all
Reply to author
Forward
0 new messages