Every once in a while I get email asking for an option to pmcstat(8) that would allow hardware events to be measured for a specified time interval.
The good news is: pmcstat(8) already supports timed measurements — using /bin/sleep.
Here is how you would do it:
Timed system sampling would be done in the following manner:
% pmcstat -S instructions -O logfile /bin/sleep 42
This invocation allocates a system-scope sampling PMC (-S) and profiles the whole system while /bin/sleep executes, i.e., for 42 seconds.
Timed measurements on groups of processes are performed in a similar fashion:
% pmcstat -d -p dc-misses -t '1234' /bin/sleep 24
This invocation would allocate a process-scope counting PMC (-p) that counts data cache misses, attach it (-t) to the process with pid 1234 and its descendants (-d), and count for 24 seconds.
Note that the '-t' option also takes regular expressions, so you don't need to know process ids beforehand. To count instructions executed by processes named 'httpd' for an hour you would use:% pmcstat -p instructions -t 'httpd' /bin/sleep 3600
The "Unix way" uses small tools, each of which does a defined task reasonably well and which are combined to perform more complex tasks. In the examples above, /bin/sleep manages time intervals and pmcstat(8) manages PMC based measurements.
When you combine them, voilĂ , you get timed PMC based measurements.