【问题标题】:How do you print an associative array in DTrace?如何在 DTrace 中打印关联数组?
【发布时间】:2011-01-20 06:03:40
【问题描述】:

这个问题总结得差不多了。 “dtrace 'print an associative array'” 谷歌搜索结果只有一次,类似的搜索同样没用。

编辑:

如果我使用聚合,我不知道我仍然能够删除条目。我的应用程序要求我能够执行以下操作:

file_descriptors[0] = "stdin"
file_descriptors[3] = "service.log"

...
...


file_descriptors[3] = 0

...
...

# should print only those entries that have not been cleared.
print_array(file_descriptors)

我知道你可以清除整个聚合,但是单个条目呢?

更新:

由于我在 OS X 中执行此操作,并且我的应用程序要跟踪已由特定进程打开的所有文件描述符,因此我能够拥有一个包含 256 个路径名的数组,因此:

syscall::open*:entry
/execname == $1/
{
    self->path = copyinstr(arg0);
}

syscall::open*:return
/execname == $1/
{    
    opened[arg0] = self->path;
}

syscall::close*:entry
/execname == $1/
{
    opened[arg0] = 0;
}

tick-10sec
{
    printf("  0:  %s\n", opened[0]);
}

The above probe repeated 255 more times...

这很糟糕。我真的很想拥有更好的东西。

【问题讨论】:

    标签: arrays printing associative dtrace


    【解决方案1】:

    this 是 Google 找到的链接吗?因为这个建议看起来很合理:

    我认为您正在寻找的效果应该通过使用 聚合而不是数组。所以你实际上会做这样的事情:

    @requests[remote_ip,request] = count();
    

    ...然后:

    profile:::tick-10sec
    {
        /* print all of the requests */
        printa(@requests);
    
        /* Nuke the requests aggregation */
        trunc(@requests);
    }
    

    【讨论】:

    • 哦 - 我现在明白你在做什么了。除非您在运行期间为该键收集大量数据,否则聚合不是您想要的。对不起;我误会了。
    • 没问题。这个问题有点难以解决,我本来可以在我的帖子中更清楚......
    【解决方案2】:

    使用关联数组和sum(1)sum(-1) 而不是count()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-03
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 2019-05-28
      • 2018-02-28
      相关资源
      最近更新 更多