Leslie, Qi Wang (qiwa)
2006-Apr-14 02:28 UTC
[dtrace-discuss] A script to calculate process running percentage in multi-cpu
Hi, Expert I made a script to calculate running percentage for one process in multiple cpu. I use sched:::on-cpu and sched:::off-cpu to collect the process running time. After some time, I divide process running time by the interval to get process running percentage in different cpu. Do you think if it works? Thanks for your comments. Attached is the script #!/usr/sbin/dtrace -s #pragma D option quiet inline int MYPID = $1; inline int INTERVAL = $2; inline int COUNTS = $3; dtrace:::BEGIN { secs = INTERVAL; counts = COUNTS; printf("Sampling %d with interval %d and counts %d...\n", MYPID, secs, counts); } /* Record the start time of a thread */ sched:::on-cpu, sched:::remain-cpu /pid == MYPID/ { cpustart[cpu, execname] = timestamp; start[execname] = timestamp; } /* Save the elapsed time of a thread */ sched:::off-cpu, sched:::remain-cpu, profile:::profile-1sec /pid == MYPID/ { /* save elapsed */ this->elapsed = timestamp - cpustart[cpu, execname]; @Procs[execname] = sum(timestamp - start[execname]); @CPUProcs[cpu, execname] = sum(this->elapsed); @PercCPUProcs[cpu, execname] = sum(this->elapsed / INTERVAL / 1000000); } profile:::tick-1sec { secs--; } /* Print time */ profile:::tick-1sec /secs == 0/ { printf("%Y,\n", walltimestamp); } /* Print report */ profile:::tick-1sec /secs == 0/ { printf("%16s %16s\n", "THREADS", "TIME (ns)"); printa("%16s %@16d\n", @Procs); trunc(@Procs); printf("\n%16s %16s %16s\n", "CPU", "THREADS", "TIME (ns)"); printa("%16d %16s %@16d\n", @CPUProcs); trunc(@CPUProcs); printf("\n%16s %16s %16s\n", "CPU", "THREADS", "%TIME"); printa("%16d %16s %@16d\n", @PercCPUProcs); trunc(@PercCPUProcs); secs = INTERVAL; counts--; } /* End of program */ profile:::tick-1sec /counts == 0/ { exit(0); } /* cleanup for Ctrl-C */ dtrace:::END { trunc(@Procs); trunc(@CPUProcs); trunc(@PercCPUProcs); } Best Regards Leslie (Wang Qi)