Get CPU preemption count on Linux - linux

Is it possible to get this counter via procfs/sysfs? That is how many times CPU preempted any active task. Per process value is also OK.
Linux pve-index 4.15.18-5-pve #1 SMP PVE 4.15.18-24 (Thu, 13 Sep 2018 09:15:10 +0200) x86_64 GNU/Linux

If you mean non-voluntary context switches, then there is at least couple files under /proc:
# cat /proc/61115/status | grep ctxt_switches
voluntary_ctxt_switches: 6511
nonvoluntary_ctxt_switches: 143
# cat /proc/61115/sched
bash (61115, #threads: 1)
-------------------------------------------------------------------
...
nr_switches : 6594
nr_voluntary_switches : 6451
nr_involuntary_switches : 143
Just replace number 61115 with the wanted pid.

Related

Different uname -a output

I am looking for different output of the uname -a command.
The one that I have is
Linux dogeserv.net 5.4.0-89-generic #100-Ubuntu SMP Fri Sep 24 14:50:10 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
I am looking for different outputs of Processor (other then x86_64 and aarch64), OS , -m output.
As per the time i am writing the answer, these options are available in uname command.
-a, --all print all information, in the following order, except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type (non-portable)
-i, --hardware-platform print the hardware platform (non-portable)
-o, --operating-system print the operating system
For some reason, it's not showing my processor, but you can use uname -p for processor information.
Incase you want detailed information about your processor, you can use cat /proc/cpuinfo, and this will show you information about the processor of your computer with information of every single core and this is what your output should look like
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 142
model name : Intel(R) Core(TM) i3-8130U CPU # 2.20GHz
stepping : 10
microcode : 0xea
cpu MHz : 1279.215
cache size : 4096 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 2
.....
TO be more specific you can use these commands for their respective outputs
cat /proc/cpuinfo | grep 'vendor' | uniq #view vendor name
cat /proc/cpuinfo | grep 'model name' | uniq #display model name
cat /proc/cpuinfo | grep processor | wc -l #count the number of processing units
cat /proc/cpuinfo | grep 'core id' #show individual cores
A alternative to cat /proc/cpuinfo is lscpu, it will also give you information about your processor, but it will be more about the whole processor instead of every single core.
this is what output should look like:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Vendor ID: GenuineIntel
BIOS Vendor ID: Intel(R) Corporation
Model name: Intel(R) Core(TM) i3-8130U CPU # 2.20GHz
BIOS Model name: Intel(R) Core(TM) i3-8130U CPU # 2.20GHz
CPU family: 6
Model: 142
Thread(s) per core: 2
Core(s) per socket: 2
.....
Lastly stackoverflow is for mostly programming related questions, you can see all the sites Stack Exchange offers for different type of questions to be asked by clicking this Link

Hung process and Linux syscall 248?

I'm troubleshooting a hung self test for the upcoming OpenSSL 1.1.0 on a particular machine. I've tried to debug this issue twice, and it resulted in two unresponsive GDB's and two hung processes:
$ ps -A | grep afalgtest
1030 pts/0 00:00:00 afalgtest
1196 pts/0 00:00:00 afalgtest
I was able to kill GDB, but I was not able to kill the hung processes.
According to /proc/<pid>/syscall, both are in syscall 248:
via:test$ sudo cat /proc/1030/syscall
248 0xb7fd6000 0x1 0xbfff98d4 0xb7fb9270 0xbfff98e0 0xb7ec45f7 0xbfff986c 0xb7fdbbe8
via:test$ sudo cat /proc/1196/syscall
248 0xb7fd6000 0x1 0xbfff98d4 0xb7fb9270 0xbfff98e0 0xb7ec45f7 0xbfff986c 0xb7fdbbe8
I'm running a machine with the VIA C7-D processor, and it needs Lubuntu 15 because its one of the few distros to support VIA's PM400 graphics chipset. Lubuntu 15 uses the 4.2 kernel:
$ uname -a
Linux via 4.2.0-30-generic #36-Ubuntu SMP Fri Feb 26 00:57:19 UTC 2016 i686 i686 i686 GNU/Linux
The next step is:
$ cat /usr/include/asm-generic/unistd.h | grep 248
$
Which returns nothing. Next, cat'ing unistd.h:
...
/*
* Architectures may provide up to 16 syscalls of their own
* starting with this value.
*/
#define __NR_arch_specific_syscall 244
#define __NR_wait4 260
__SC_COMP(__NR_wait4, sys_wait4, compat_sys_wait4)
#define __NR_prlimit64 261
__SYSCALL(__NR_prlimit64, sys_prlimit64)
#define __NR_fanotify_init 262
__SYSCALL(__NR_fanotify_init, sys_fanotify_init)
#define __NR_fanotify_mark 263
...
Just in case 248 is 0x248:
$ cat /usr/include/asm-generic/unistd.h | grep 584
$
So my question is, how can I determine which syscall the process is hung in?
It appears I have the latest kernel headers:
$ sudo apt-get install linux-headers-$(uname -r)
Reading package lists... Done
Building dependency tree
Reading state information... Done
linux-headers-4.2.0-30-generic is already the newest version.
linux-headers-4.2.0-30-generic set to manually installed.
The syscall number is decimal. Syscall table you are looking for can be found in arch/x86/entry/syscalls/syscall_32.tbl. Regardless, you are inspecting the wrong file. What you want to see is the entire backtrace provided in /stack file. The kernel is nice enough to not only unwind the stack but also resolve all symbols. Alternatively you can use a debugger (named crash) to inspect the live kernel.

background process log shows big gaps in message timestamps

I started a long running job under nohup in the background over a weekend. When looking at the output after it finished, I noticed that there were large gaps between the timestamps of some log messages. Some gaps were as long as 10 hrs. I had no way of finding out what was going on with my job at that time.
I ran it on a standard Red hat linux server machine at work.
Is this behavior caused by nohup command ? If not what could be possible causes ?
One such long running job was as the script below -
#!/bin/bash
while true
do
echo "`date` `top -n 1 -b | grep progname`"
done
And here one such gap from the log -
Mon May 26 04:29:42 PDT 2014 27685 user 18 0 2883m 2.8g 1732 S 0.0 3.9 29:05.54 progname
Tue May 27 03:20:35 PDT 2014 27685 user 18 0 3371m 3.3g 1732 S 0.0 4.6 34:23.21 progname
Ok.
pid is a variable that is the pid of the process you want to monitor--
Try this for starters (Courtesy of S Chazelas):
export pid=$(ps -ef | grep progname | awk '{$print $2}')
while rss=$(ps -o rss= -p "${pid}")
do
printf '%d %s\n' "$rss" "$(date)"
sleep 60;
done > t.lis
#
echo "Done $(date)" >> t.lis
rss is the resident set size (memory allocated to the process) in pages.
getconf PAGESIZE
will show how many bytes are in a page of memory.

CPU User time and System time on AIX

How can I get CPU user time and system time for each cpu on AIX.
I know I can get this value from cat /proc/stat on a linux machine, and from pstat_getprocessor() on an HP-UX machine. Is there a way to get this same metric on an AIX machine.
$ cat /proc/stat
...
cpu 23697394 7969 2744135 4505191649 2958605 190 17883 0 0
cpu0 12511394 4575 1520243 2251753159 1480624 137 10580 0 0
cpu1 11186000 3394 1223891 2253438490 1477980 53 7302 0 0
...
mpstat is providing these metrics, either parse its output or figure out how/where does it find them.

Getting CPU utilization information

How could I get the CPU utilization with time info of a process in linux? Basically I want to let my application run overnight. At the same time, I would like to monitor the CPU utilization during the period the application is run.
I tried top | grep appName >& log, it does not seem to return me anything in the log. Could someone help me with this?
Thanks.
vmstat and iostat can both give you periodic information of this nature; I would suggest either setting up the number of times manually, or putting a single poll into a cron job, and then redirecting the output to a file:
vmstat 20 4230 >> cpu_log_file
This would give you a snapshot of usage every 20 seconds for 24 hours.
install sysstat package and run sar
nohup sar -o output.file 12 8 >/dev/null 2>&1 &
use the top or watch command
PID COMMAND %CPU TIME #TH #WQ #PORT #MREG RPRVT RSHRD RSIZE VPRVT VSIZE PGRP PPID STATE UID FAULTS COW MSGSENT MSGRECV SYSBSD SYSMACH CSW PAGEINS USER
10764 top 8.4 00:01.04 1/1 0 24 33 2000K 244K 2576K 17M 2378M 10764 10719 running 0 9908+ 54 564790+ 282365+ 3381+ 283412+ 838+ 27 root
10763 taskgated 0.0 00:00.00 2 0 25 27 432K 244K 1004K 27M 2387M 10763 1 sleeping 0 376 60 140 60 160 109 11 0 root
Write a program that invokes your process and then calls getrusage(2) and reports statistics for its children.
You can monitor the time used by your program with top while it is running.
Alternatively, you can launch your application with the time command, which will print the total amount of CPU time used by your program at the end of its execution. Just type time ./my_app instead of just ./my_app
For more info, man 1 time

Resources