Ps Utility in Linux (Procps), How to Check Which CPU Is Used

ps utility in linux (procps), how to check which CPU is used

which of multiple processors? it does not offer an option for that according to the manpage. but on my Debian stable system it accepts the undocumented -o cpu



after looking at the source, and the output of ps L, I believe your answer is either the cpuid or sgi_p output options, column IDs CPUID and P, respectively.


And 'cpu' should work according to this note in output.c, but it's currently tied to the 'nop' output pr_nop():

{"cpu", "CPU", pr_nop, sr_nop, 3, 0, BSD, AN|RIGHT}, /* FIXME ... HP-UX wants this as the CPU number for SMP? */

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

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

Bash script checking cpu usage of specific process

The problem is that bash can't handle decimals. You can just multiply them by 100 and work with plain integers instead:

#!/bin/bash
declare -i app_pid
declare -i app_cpu
declare -i cpu_limit
app_name="top"
cpu_limit="5000"
app_pid=`ps aux | grep $app_name | grep -v grep | awk {'print $2'}`
app_cpu=`ps aux | grep $app_name | grep -v grep | awk {'print $3*100'}`
if [[ $app_cpu -gt $cpu_limit ]]; then
echo "crap"
else
echo "we're good"
fi

Keep in mind that CPU percentage is a suboptimal measurement of application health. If you have two processes running infinite loops on a single core system, no other application of the same priority will ever go over 33%, even if they're trashing around.

how to redirect ps command in linux to look into other folder than /proc

the /proc path is hardcoded in the source tree of the /bin/ps binary file.
Thus, you need to recompile /bin/ps

Follow these steps to recompile /bin/ps inside a container that mounts the host /proc and use this new ps to display the process list of the docker host from the container:

In this example, I do not use /host/proc but /prod, to avoid modifying the path length to the mount point of the procfs filesystem (increasing the path length could trigger runtime errors in some situations). I also use a container based on OpenSUSE Leap 42.1, since you have not described the base image you are using.

1- on the docker host, first download the /bin/ps sources:

fenyo@myhost# mkdir /root/git
fenyo@myhost# cd /root/git
fenyo@myhost# git clone https://gitlab.com/procps-ng/procps.git
fenyo@myhost# cd procps

2- replace any occurence of "/proc by "/prod in **/*.c

3- run your docker container, mounting procfs on /prod in the container

fenyo@myhost# docker run -v /proc:/prod -v /root/git/procps:/root/git/procps -t -i --rm opensuse:42.1 bash

4- inside the docker container, recompile /bin/ps

bash-4.2# cd /root/git/procps
bash-4.2# zypper install -y gettext gettext-tools autoconf libtool pkg-config gcc make
bash-4.2# ./autogen.sh
bash-4.2# ./configure --without-ncurses
bash-4.2# make

Now, you can use /root/git/procps/ps/pscommand as a replacement for /bin/ps, in order to use /prod instead of /proc. With pscommand, you will get a process list from the host, not from the container:

bash-4.2# ps -auxgww
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 20064 3076 ? Ss 23:20 0:00 bash
root 5199 0.0 0.0 33340 2996 ? R+ 23:24 0:00 ps -auxgww

bash-4.2# /root/git/procps/ps/pscommand -auxgww
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 33620 3956 ? Ss Jul03 1:13 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root 2 0.0 0.0 0 0 ? S Jul03 0:01 [kthreadd]
root 3 0.0 0.0 0 0 ? R Jul03 4:44 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Jul03 0:01 [kworker/0:0H]
root 7 0.0 0.0 0 0 ? S Jul03 15:27 [rcu_preempt]
root 8 0.0 0.0 0 0 ? S Jul03 0:00 [rcu_sched]
root 9 0.0 0.0 0 0 ? S Jul03 0:00 [rcu_bh]
root 10 0.0 0.0 0 0 ? S Jul03 10:02 [rcuop/0]
root 11 0.0 0.0 0 0 ? S Jul03 0:00 [rcuos/0]
root 12 0.0 0.0 0 0 ? S Jul03 0:00 [rcuob/0]
root 13 0.0 0.0 0 0 ? S Jul03 0:06 [migration/0]
root 14 0.0 0.0 0 0 ? S Jul03 0:07 [watchdog/0]
root 15 0.0 0.0 0 0 ? S Jul03 0:06 [watchdog/1]
root 16 0.0 0.0 0 0 ? S Jul03 0:04 [migration/1]
root 17 0.0 0.0 0 0 ? S Jul03 1:07 [ksoftirqd/1]
root 19 0.0 0.0 0 0 ? S< Jul03 0:00 [kworker/1:0H]
root 20 0.0 0.0 0 0 ? S Jul03 2:42 [rcuop/1]
root 21 0.0 0.0 0 0 ? S Jul03 0:00 [rcuos/1]
root 22 0.0 0.0 0 0 ? S Jul03 0:00 [rcuob/1]
root 23 0.0 0.0 0 0 ? S Jul03 0:05 [watchdog/2]
root 24 0.0 0.0 0 0 ? S Jul03 0:06 [migration/2]
root 25 0.0 0.0 0 0 ? S Jul03 0:48 [ksoftirqd/2]
root 27 0.0 0.0 0 0 ? S< Jul03 0:00 [kworker/2:0H]
root 28 0.0 0.0 0 0 ? S Jul03 7:32 [rcuop/2]
root 29 0.0 0.0 0 0 ? S Jul03 0:00 [rcuos/2]
root 30 0.0 0.0 0 0 ? S Jul03 0:00 [rcuob/2]
root 31 0.0 0.0 0 0 ? S Jul03 0:05 [watchdog/3]
root 32 0.0 0.0 0 0 ? S Jul03 0:05 [migration/3]
root 33 0.0 0.0 0 0 ? S Jul03 0:37 [ksoftirqd/3]
[...]

How to store ps command output in csv format?

You could try something like the following code I wrote:

ps -e -o %p, -o lstart -o ,%C, -o %mem -o ,%c > output.csv

Brief explanation:

The -o option can be used multiple times in a ps command to specify the format.
In order to control which separator is used we can use AIX format descriptors. We can specify our needed separators like, e.g. %p,. Since AIX format descriptors are not available for every piece of data, but only for some of the data (for example in our case there are no AIX format descriptors for %mem and for lstart), we plant %mem and lstart around the available AIX format descriptors to achieve the comma separation. For example this site provides information about the ps command for further readings.

output.csv example:

  PID,                 STARTED,%CPU,%MEM,COMMAND
1,Mon Feb 25 00:00:01 2019, 0.0, 0.1,examplecommand1
2,Mon Feb 25 00:00:01 2019, 0.0, 0.0,examplecommand2
(...)


Related Topics



Leave a reply



Submit