1 Star2 Stars3 Stars4 Stars5 Stars (还没有评分)
Loading...

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):

clause.d

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 array是multi-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/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.