DTrace的变量类型


要想写好DTrace脚本,了解何时选用何种类型变量是非常重要的。在这里,小编推荐DTrace大神Brendan Gregg的文章《DTrace variable types》(http://dtrace.org/blogs/brendan/2011/11/25/dtrace-variable-types/)。文章很短,值得每个人一读。小编总结了一下,就是选用变量类型的优先级是:

aggregation > clause local > thread local > scalar > associative array。

也就是说,能用aggregation存储变量最好,不行就考虑clause local,再不行就考虑thread local......以此类推。另外要注意,scalar和associative array不是multi-CPU safe的。


此外,小编还要补充以下2点:

(1)clause local变量的有效期就在probe的一次触发过程内,如果这个probe有多个clause{},那么clause local变量在这些个clause{}内都可以被访问,而且这些个clause{}是顺序执行的。大家可以参考下面这个例子(http://dtrace.org/guide/chapter3.html):

int me;			/* an integer global variable */
this int foo;		/* an integer clause-local variable */

tick-1sec
{
	/*
	 * Set foo to be 10 if and only if this is the first clause executed.
	 */
	this->foo = (me % 3 == 0) ? 10 : this->foo;
	printf("Clause 1 is number %d; foo is %d\n", me++ % 3, this->foo++);
}

tick-1sec
{
	/*
	 * Set foo to be 20 if and only if this is the first clause executed. 
	 */
	this->foo = (me % 3 == 0) ? 20 : this->foo;
	printf("Clause 2 is number %d; foo is %d\n", me++ % 3, this->foo++);
}

tick-1sec
{
	/*
	 * Set foo to be 30 if and only if this is the first clause executed.
	 */
	this->foo = (me % 3 == 0) ? 30 : this->foo;
	printf("Clause 3 is number %d; foo is %d\n", me++ % 3, this->foo++);
}

此外,如果clause local变量没有被初始化,它的值是随机的,取决于当时clause local变量所在的内存的值。关于这一点可以参考一篇帖子:http://thr3ads.net/dtrace-discuss/2007/05/381363-Clause-local-Variables-and-two-different-probes-is-this-variable-shared

(2)在多个CPU同时访问associative array同一个key/value元组时,associative array不是multi-CPU safe的,但是如果可以保证不会出现多个CPU同时访问associative array同一个key/value元组时,associative arraymulti-CPU safe的。这一点得到DTrace作者Adam的确认:http://www.listbox.com/member/archive/184261/2014/04/sort/time_rev/page/1/entry/3:15/20140410014249:EB3CCA90-C072-11E3-8189-9F9034AA57AC/


如果你对DTrace感兴趣,欢迎关注DTrace公众号(微信号:chinadtrace),介绍关于DTrace的使用技巧,经验分享,话题讨论等等。也非常欢迎你转发给其它对DTrace感兴趣的朋友。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值