I'm aware that I can use time as
time <some command>
But this requires me to remember to type time before <some command>
I'm wondering if it's possible create some sort of hook, so that time is run for every command, but only displayed if I enter last-time or something at the cli.
An example usage might be:
$ sleep 2
$ last-time
________________________________________________________
Executed in 2.00 secs fish external
usr time 2.28 millis 912.00 micros 1.37 millis
sys time 0.30 millis 296.00 micros 0.00 millis
The $CMD_DURATION variable contains the duration of the last-run interactive command, in milliseconds.
$ sleep 2
$ echo $CMD_DURATION
2014
Docs are here.
Related
I've been trying to use perf to profile my running process, but I cannot make sense of some numbers output by perf, here is the command I used and output I got:
$ sudo perf stat -x, -v -e branch-misses,cpu-cycles,cache-misses sleep 1
Using CPUID GenuineIntel-6-55-4
branch-misses: 7751 444665 444665
cpu-cycles: 1212296 444665 444665
cache-misses: 4902 444665 444665
7751,,branch-misses,444665,100.00,,
1212296,,cpu-cycles,444665,100.00,,
4902,,cache-misses,444665,100.00,,
May I know what event does the number "444665" represent?
-x format of perf stat is described in man page of perf-stat, section CSV FORMAT. There is fragment of this man page without optional columns:
CSV FORMAT top
With -x, perf stat is able to output a not-quite-CSV format output
Commas in the output are not put into "". To make it easy to parse it
is recommended to use a different character like -x \;
The fields are in this order:
· counter value
· unit of the counter value or empty
· event name
· run time of counter
· percentage of measurement time the counter was running
Additional metrics may be printed with all earlier fields being
empty.
So, you have value of counter, empty unit of counter, event name, run time, percentage of counter being active (compared to program running time).
By comparing output of these two commands (recommended by Peter Cordes in comment)
perf stat awk 'BEGIN{for(i=0;i<10000000;i++){}}'
perf stat -x \; awk 'BEGIN{for(i=0;i<10000000;i++){}}'
I think than run time is nanoseconds for all time this counter was active. When you run perf stat with non-conflicting set of events, and there are enough hardware counters to count all required events, run time will be almost total time of profiled program being run on CPU. (Example of too large event set: perf stat -x , -e cycles,instructions,branches,branch-misses,cache-misses,cache-references,mem-loads,mem-stores awk 'BEGIN{for(i=0;i<10000000;i++){}}' - run time will be different for these events, because they were dynamically multiplexed during program execution; and sleep 1 will be too short to have multiplexing to activate.)
For sleep 1 there is very small amount of code to be active on CPU, it is just libc startup code and calling syscall nanosleep for 1 second (check strace sleep 1). So in your output 444665 is in ns or is just 444 microseconds or 0.444 milliseconds or 0.000444 seconds of libc startup for sleep 1 process.
If you want to measure whole system activity for one second, try adding -a option of perf stat (profile all processes), optionally with -A to separate events for cpu cores (or with -I 100 to have periodic printing):
perf stat -a sleep 1
perf stat -Aa sleep 1
perf stat -a -x , sleep 1
perf stat -Aa -x , sleep 1
Say I have a very simple script that does the
sleep 0.001
command in Ubuntu.
I want to know if this command effect the c state of the core or the package somehow. Does it force a c state on the hardware?
Thanks!
Code example:
/bin/sh
sleep 0.01
sleep 0.01
The sleep is purely software, the process executing it is not eligible for the CPU/cores during at least the specified time, but all your Ubuntu is not frozen and the CPU/cores are available for other processes. After if there is nothing to do during the sleep perhaps the CPU/cores save energy, but this is not directly required by the command sleep itself
I am running the timeout command of GNU Coreutils,
gtimeout 600 python myprogram.py
According to the manual,
duration is a floating point number followed by an optional unit:
‘s’ for seconds (the default) ‘m’ for minutes ‘h’ for hours ‘d’ for
days
Thus, the 'python myprogram.py part should terminate within 600 seconds (10 minutes). To my surprise, the command actually timeouts after 1 hour. Why?
It's possible that your program ignores SIGTERM, the signal which is used by gtimeout to "kindly ask the program to terminate".
You can have gtimeout use SIGKILL instead, which can't be ignored or blocked, by adding the parameter -s 9 like this:
gtimeout -s 9 python myprogram.py
Can I configure bash to report how long each command takes to execute, if it's longer than some threshold?
I thought I recalled some setting for this, but can't find it either in bash(1) or google.
The idea, in case it's not clear, would be something like this:
% SUBCMDTMOUT=30
% sleep 29 # 29 seconds elapse
% sleep 30 # 30 seconds elapse
% sleep 31 # 31 seconds elapse
bash: subcommand `sleep 31' took 31 seconds to complete.
%
Prepend time to your command and then parse the output of time through any condition to get your desired output.
Example:
$ time sleep 15
real 0m15.003s
user 0m0.000s
sys 0m0.002s
#chepner probably has it right: REPORTTIME in zsh (though that only tracks CPU time; I suspect my mystery problem is some kind of network wait). But since I'm not motivated enough to convert my login shell for this, the specific answer to my question is "nope."
I was wondering if there is the way to get time in milliseconds from shell script on Mac OS.
I need it to time how much certain query runs.
Now I can only get the time in seconds:
Start=`date +%s`
End =`date +%s`
Time=$Start-$End
You could use the benchmarking-tool hyperfine (https://github.com/sharkdp/hyperfine).
It is more elaborate than time, by default runs your command multiple times and gives you mean runtime, deviation, min, and max.
Simple usage
hyperfine your_command
Result looks like this (result of hyperfine 'sleep 0.5'):
bash-3.2$ hyperfine 'sleep 0.5'
Benchmark #1: sleep 0.5
Time (mean ± σ): 505.6 ms ± 1.5 ms [User: 0.8 ms, System: 1.2 ms]
Range (min … max): 503.1 ms … 508.8 ms 10 runs
There is one caveat, the minimum number of runs is 2 (hyperfine -r 2 'your command').
Installation
Hyperfine can be installed via Homebrew:
brew install hyperfine
For more info see: https://github.com/sharkdp/hyperfine#on-macos
just use the 'time' command:
time something
something could be a shell, or a command (find, etc)
the "real" time is the total elapsed time you want, and includes milliseconds
Since Mac OS is BSD-like system, its date does not support the %N parameter you need.
You could though consider installing the GNU Core Utils.
It will allow you to get the time in the usual Linux way.
I suppose, the time something command will also output the result with milliseconds then.