linux cpu usage - linux

I am working on unix.
I want to knwo the current cpu usage of a process.
I understood that ps give the average of cpu used till the process is up - it is not the current usage.
Is there a way to print only the cpu from the top command without 10 more parameters and
headers? I know how to do it with awk - this is not the way i want to do it.
top -p 20705 -bc -n 1 | tail -n 2 | awk '{ print $9}' | head -n 1
If there is another simple way to do it, not reading /proc/stat...
If there is a simple way doing it from c++, it is also ok.

Most likely, you will need to read /proc/stat, However, here is an interesting article with C code that may help you out. To understand and use the output from the program you should do man 5 proc. And here is the source code.
The bottom line is that you will need to read from /proc/stat to do what you want.

to see cpu usage of a proccess whose pid is 24556
ps -p 24556 -o \%cpu=
to see mem usage of a process named syslogd
ps -C syslogd -o \%mem=

Related

Where to look at the highest cpu usage in linux

I know about VmHWM usage in linux in /proc/[pid]/status where I can look at high water mark. Is there something similar for cpu usage? I mean top -p [pid] shows the current cpu usage. But I would like to know about the highest... Is there something for that in linux
You can use top -o cpu -n 1 | grep 'PID' -A1.
The command does the following:
top -o cpu returns sorts by cpu usage
-n 1 shows only the first line
grep 'PID' -A1 greps for PID and returns the following line
This will give you a constantly updating output of two lines. The first one are the labels and the second one the process.
Depending on what you want to use it for you can do some string processing with awk or sed

How to trace the list of PIDs running on a specific core?

I'm trying to run a program on a dedicated core in Linux. (I know Jailhouse is a good way to do so, but I have to use off-the-shelf Linux. :-( )
Other processes, such as interrupt handlers, kernel threads, service progresses, may also run on the dedicated core occasionally. I want to disable as many such processes as possible. To do that, I need first pin down the list of processes that may run on the dedicated core.
My question is:
Is there any existing tools that I can use to trace the list of PIDs or processes that run on a specific core over a time interval?
Thank you very much for your time and help in this question!
TL;DR Dirty hacky solution.
DISCLAIMER: At some point stops working "column: line too long" :-/
Copy this to: core-pids.sh
#!/bin/bash
TARGET_CPU=0
touch lastPIDs
touch CPU_PIDs
while true; do
ps ax -o cpuid,pid | tail -n +2 | sort | xargs -n 2 | grep -E "^$TARGET_CPU" | awk '{print $2}' > lastPIDs
for i in {1..100}; do printf "#\n" >> lastPIDs; done
cp CPU_PIDs aux
paste lastPIDs aux > CPU_PIDs
column -t CPU_PIDs > CPU_PIDs.humanfriendly.tsv
sleep 1
done
Then
chmod +x core-pids.sh
./core-pids.sh
Then open CPU_PIDs.humanfriendly.tsv with your favorite editor, and ¡inspect!
The key is in the "ps -o cpuid,pid" bit, for more detailed info, please comment. :D
Explanation
Infinite loop with
ps -o cpuid,pid | tail -n +2 | sort | xargs -n 2 | grep -E "^$TARGET_CPU" | awk '{print $2}' > lastPIDs
ps ax -o cpuid,pid
Show pid's associated to CPU
tail -n +2
remove headers
sort
sort by cpuid
xargs -n 2
remove white spaces at begging
grep -E "^$TARGET_CPU"
filter by CPU id
awk '{print $2}'
get pid column
> lastPIDs
output to file those las pid's for the target CPU id
for i in {1..10}; do printf "#\n" >> lastPIDs; done
hack for pretty .tsv print with the "columns -t" command
cp CPU_PIDs aux
CPU_PIDs holds the whole timeline, we copy it to aux file to allow the next command to use it as input and output
paste lastPIDs aux > CPU_PIDs
Append lastPIDs columns to the whole timeline file CPU_PIDs
column -t CPU_PIDs > CPU_PIDs.humanfriendly.tsv
pretty print whole timeline CPU_PIDs file
Attribution
stackoverflow answer to: ps utility in linux (procps), how to check which CPU is used
by Mikel
stackoverflow answer to: Echo newline in Bash prints literal \n
by sth
stackoverflow answer to: shell variable in a grep regex
by David W.
superuser answer to: Aligning columns in output from a UNIX command
Janne Pikkarainen
nixCraft article: HowTo: Unix For Loop 1 to 100 Numbers
The best way to obtain what you want is to operate as follows:
Use the isolcpus= Linux kernel boot parameter to "free" one core from the Linux scheduler
Disable the irqbalance daemon (in case it is executing)
Set the IRQs affinities to the other cores by manually writing the CPU mask on /proc/irq/<irq_number>/smp_affinity
Finally, run your program setting the affinity to the dedicated core through the taskset command.
In this case, such core will only execute your program. For checking, you can type ps -eLF and look at the PSR column (which specifies the CPU number).
Not a direct answer to the question, but I am usually using perf context-switches software event to identify the perturbation of the system or other processes on my benchmarks

How do I tail top -c?

I have a couple ruby scripts running on my machine and some other ruby processes. The only way I can differentiate them with top is by doing top -c (so I can see the command, otherwise everything is just 'ruby').
I want to be able to watch how many scripts are running so I can restart them if one fails.
I am thinking I can do this with top -c -n 1 | grep "script-name" but I can't figure out how to tail -f that or if that command is the best way to do it in the first place.
I think that top it's not the best choice here, because it's an interactive command and you can't really pipe its whole output (probably there is a way). One of the fair enough ways to do it would be using ps:
ps -e -o pid,cmd | grep "script-name"
If you want to periodically investigate this, you can also use watch:
watch 'ps -e -o pid,cmd | grep "script-name"'
In general, it's a bad practice to grep ps output, but I suppose in your case will work. If you only want the number of running processes that match against a pattern or you just want their PIDs, you'd better go with pgrep.
pgrep "script-name"

How to get first record of top command in linux?

How to get first record of top command in linux by using below line of code
$ top -b|tee aorpprkd004.out| grep 'Cpu(s): | head -1'
Above is not working
This:
grep 'Cpu(s): | head -1'
Should probably be this:
grep 'Cpu(s):' | head -1
Note the quotes.
First up, you need to move the quotes since you don't want to be searching for the head command in the output. The text you're looking for is simply Cpu(s): with the output filtered through head.
Secondly, batch mode by default runs forever. If you're only going to be getting the first one anyway (as per your head -1 filter), you may as well explicitly limit it with the -n option so that it exits immediately it's done that:
$ top -b -n1 | tee aorpprkd004.out | grep 'Cpu(s):'
Cpu(s): 2.0% user, 2.5% system, 0.0% nice, 95.5% idle
Here with little change you can do it,
top -b|tee aorpprkd004.out| grep 'Cpu'

get apache total cpu usage in (linux)

I want to write a script (in bash or Perl on linux) which monitors the Apache and restarts the Apache in case it exceeds X% CPU.
I understand that I need to get the total CPU usage of Apache since it opens child process.
How can I get the total CPU usage of Apache?
Try the following, but make sure to update the Apache-process name with your actual one (mine is httpd):
ps u -C httpd | awk '{sum += $3} END {print sum}'
This will get a list of all apache processes running and sum the %CPU column of ps's output using awk.
this will return sum load of parent apache process and all child processes, in percents, without any additional info, so that you can easily use this script in any way:
ps --no-heading -o pcpu -C httpd | awk '{s+=$1} END {print s}'
This will list you the total CPU usage of each apache2 process:
ps u -C apache2 | awk '{print $3}' | grep -v "%CPU"
Note, however, that the total (=average) CPU usage might be rather low even if the current CPU usage is high, especially for long running processes.

Resources