Best way to track CPU% of one process over time - performance

I want to print the cpu% of one running process every seconds in the terminal.
In addition to the cpu%, I would like to print the corresponding time stamps as well.
Example:
$
13:00:00 34,0%
13:00:01 35,2%
13:00:02 36,1%
What would be best approach for this?
Remark:
top -pid PID -s 1
Executing this command is close to what I want to achieve, except that:
It will update the output on one single line, and won't print new lines every second.
It will not print system time stamps.
I am using zsh shells in macOS.
Computer: MacBook Pro (16-inch, 2019)
Processor: 2,6 GHz 6-Core Intel Core i7
Memory: 16 GB 2667 MHz DDR4

Related

See/Count how many subshells a script has opened

Im having a conundrum with a script (or possible wsl2 memory leak).
I'm running a large script (that takes 0.67 seconds to loop)
My issue is that the loop time is slowly increasing, and so is the memory usage, so from 0.67 seconds / 0.9gig memory to 1.20 seconds / 1.7gig after a few hours.
If I restart (stop/start), the speed goes up again and the memory usage goes down to 0.9 again..
I'm suspecting that my script is leaving running subshells, and I'm wondering if there's anyway to see how many subshells that's currently running?
oh, I'm running this on win10 Wsl2 Ubuntu
Run ps and show only parent process ids and process ids. Pipe the output to awk, setting a variable pid to a given parent process id. Where the first space delimited field (parent process id) is equal to the passed pid, print the process id (field 2)
ps -eo ppid,pid | awk -v pid=<pid> '$1==pid { print $2 }'

How to compare if the cpu serial is correct

I have a question. I would like to check when the linux system starts or if the serial cpu is correct.
If not, he would be rebooting. So he would be doing reboot loops all the time.
I found the command to check the serial cpu command:
cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
How to compare the result of this command to the value of eg 000000ddd0d0d??
And I do not know how to look like such a check script and where to put it in the Ubuntu system (/etc/init.d/rc.local ??).
It is correctly??:
#!/bin/bash
STR=cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
if $STR != '000000ddd0d0d'; then
reboot
Thank you for your help
Sorry for my English.
You probably should do the whole thing in one grep command:
grep -q '^Serial.*000000ddd0d0d' /proc/cpuinfo || reboot
This will reboot unless a line with Serial and 000000ddd0d0d in it is found in /etc/cpuinfo.
BUT this is questionable for several reasons.
What should a reboot be good for in such a case? As this is supposed to be done during machine startup, you will then enter an infinite loop of reboots which can only be stopped by switching the computer off. This is horrible! You probably never encountered such a problem (as admin) or you wouldn't produce it voluntarily. The only way to fix such a system is to boot it via some other medium (USB thumb drive or similar).
Not all Linux distributions or kernel versions offer a CPU serial number in /proc/cpuinfo. My current system, for instance, doesn't. So on my computer this would not work at all.
The whole idea of reacting on a CPU serial number is highly questionable as a CPU might break (not likely but possible) and then probably is replaced and the new CPU, while surely having a different serial number, should not pose any trouble.
So, I think you might want to reconsider.

What is causing Xorg high CPU usage?

I am running feh image viewer on Debian and after some hours of normal CPU usage (3% aprox.) , xorg suddenly starts using much more CPU (80% aprox.) and everything runs very slowly. I am not running anything else so the bug should be either on feh or on the xserver...
I am using the command "feh -z -q -D20 -R 1" (-z for random image, -q for quiet, -D20 to change the picture every 20 seconds and -R 1 to refresh the directory every second, as I erase and insert pictures pretty often)
When I use the command "free -m" before the high CPU usage and feh running, I get
total used free shared buff/cache available
Mem: 923 117 474 19 331 735
Swap: 99 0 99
And after several hours I get the same for "mem" but the used amount of "swap" is 99.
The fact that your memory usage goes up (swap is full) points directly to memory leak in some program in your system. Considering that feh is not probably designed for such an use case I'd bet it's the cause for going out of memory.
The "everything runs slowly" is caused by kernel going out of memory and it's doing its best to keep the system running. If you insist on runnin feh your choices are
Triage the memory leak bug in feh and create a fix for it.
Try to get somebody else do the same for you.
Periodically kill feh and rerun it again. Basically you can do (in bash)
while true; do timeout 120m feh -z -q -D20 -R 1; sleep 2s; done
which will kill every 120 min and restart it after 2 second delay (which allows you to kill the while loop if needed). Another choice would be to use ulimit to set maximum amount of memory you want to allow for feh and the process probably simply dies once it's using too much.
I solved this problem, but I don't know why, too.
You can try run this code kill this process:
ps -a | grep Xorg | awk '{print $1}' | xargs kill 9

Measure the shell script execution time in milliseconds on Mac OS

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.

How to check CPU Utilization of system calls used in a shell script while script is executing?

I Have a shell script which uses couple of system calls (grep,ps etc). I need to find CPU utilization for each system call used inside a script. I am using AIX unix version 5.1.Please help.
I have already tried Topas, vmstat , iostat commands, but they display overall cpu utilization of processes.
use below commnad
ps -aef | grep "process_name"
there would be a column 'C' in ouptut, which display cpu utilization for that process.
Thanks,
Gopal
I'm not sure if it's available on AIX, but on Linux the time command is what you would use
time wc /etc/hosts
9 26 235 /etc/hosts
real 0m0.075s
user 0m0.002s
sys 0m0.004s
sys is the amount of system call time, user is not system call time used by the process

Resources