Using dtrace on Solaris, I am able to accumulate on-cpu time for a given process (or execname) for the interval from start until control-C with the following script:

!/usr/sbin/dtrace -qs
dtrace:::BEGIN {
        total = 0;
}
sched:::on-cpu
/execname == $$1/
{
        self->start = vtimestamp;
}
sched:::off-cpu
/self->start/
{
        this->time = vtimestamp - self->start;
        total += this->time;
        self->start = 0;
}
dtrace:::END {
        printf("Total Time on CPU: %d us\n",total/1000);
}

(Accumulated time has a fine-grain granularity allowing nano/microsecond accumulation.)

Over the same timeframe, I would like to accumulate all or many processes in an array and report on all accumulated cpu time at break (^C).

What is the best way to do this?

有帮助吗?

解决方案

Okay, with a bit more work I've solved my problem.

Here is the way to get microseconds for all processes (but display per process) over the interval.

#!/usr/sbin/dtrace -qs

dtrace:::BEGIN {
        total = 0;
        starttimestamp=timestamp;
        printf("Starting...\n");
}

sched:::on-cpu
/pid!=0/
{
        self->start = vtimestamp;
}

sched:::off-cpu
/self->start && pid!=0/
{
        this->time = vtimestamp - self->start;
        total += this->time;

        @proctime[pid,uid,execname,curpsinfo->pr_psargs] = sum( this->time/1000 );

        self->start = 0;
}
dtrace:::END {
        printf("Elapsed time %d usec\n",(timestamp-starttimestamp)/1000);
        printf("Total Time on CPU: %d us\n",total/1000);
        printa(@proctime);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top