2

I want to get the realtime CPU frequency which is using by a process in Linux. I think I should use top command. But I want to know the '%CPU' column in top is the percent of CPU usage for a process per 1 core or per all CPU cores? for example, in the following output of top, how many CPU frequency is used by firefox?

Tasks: 279 total,   1 running, 277 sleeping,   0 stopped,   1 zombie
%Cpu(s):  5.0 us,  1.3 sy,  0.1 ni, 88.8 id,  4.8 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8059824 total,  1540120 free,  3931444 used,  2588260 buff/cache
KiB Swap:  3998716 total,  3998716 free,        0 used.  3626336 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                           
29941 sina      20   0 2345056 304040 144348 S   7.7  3.8   2:02.08 firefox                                                                           
 1222 root      20   0  447092 107272  70064 S   4.3  1.3  53:31.25 Xorg                                                                              
 2857 sina      20   0 1671448 362580  74016 S   4.3  4.5  82:19.20 compiz                                                                            
30582 sina      20   0 1619480 164448 120608 S   3.7  2.0   0:09.28 Web Content

Here's the ouptput of lscpu in my machine:

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    2
Core(s) per socket:    2
Socket(s):             1
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 60
Model name:            Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz
Stepping:              3
CPU MHz:               3400.265
CPU max MHz:           3400.0000
CPU min MHz:           800.0000
BogoMIPS:              6784.33
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              3072K
NUMA node0 CPU(s):     0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm invpcid_single ssbd ibrs ibpb stibp kaiser tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm arat pln pts flush_l1d
Sinai
  • 203
  • 1
  • 3
  • 17

2 Answers2

1

From top command press '1', it will give you cores usage on system now specify processes id to limit the output.

asktyagi
  • 2,860
  • 2
  • 8
  • 25
  • I want to get the frequncy which is using by the process on demand (execute a shell script) and make a log file e.g. 'firefox 500Mhz of CPU on $(date)'. From your answer how can I get this!!! should I devide the column %CPU to the frequency of the CPU (3.4GHz) for a process? or divide %CPU column to sum of all cores frequencies (3.4GHz * 4)? – Sinai May 04 '19 at 08:16
  • Yeah, just run top in batch mode with your required frequency and pid and calculate. you can check each core frequency from /proc/cpuinfo since you want core based calculation use /proc data to calculate – asktyagi May 04 '19 at 08:34
  • I can get the working frequency of each core by `/proc/cpuinfo|grep "cpu MHz"`. My question is that for example top shows 50% for process 'firefox'. So I should do (50%)/3.4GHz or I should do (50%)/(3.4GHz*4) or I should divide 50% to sum of the working cpu core frequencies? (the top output on the next comment) – Sinai May 04 '19 at 09:29
  • `cat /proc/cpuinfo |grep "cpu MHz" cpu MHz : 1599.992 cpu MHz : 2927.718 cpu MHz : 1601.453 cpu MHz : 3381.671 ` – Sinai May 04 '19 at 09:30
  • /proc/cpuinfo should show the real capacity of you core, not the used one, % used core you can get it from top with batch mode with pid and real capacity from /proc/cpuinfo or use dmidecode or lshw which ever is available. So avg = (sum of top command output filtered core only )/(sum of each core frequency) – asktyagi May 04 '19 at 09:59
  • Imagine I want to calculate the CPU usage of a php process (with username). I use the following to get sum of cpu usage: `ps -e -o uname,pmem,pcpu|grep "myphpuser"|awk '{sum+=$3} END {print sum}'` the output is: 45.3 . and my server has 8 cpu threads `cat /proc/cpuinfo |grep "cpu MHz"` the output: `cpu MHz : 3600.026 cpu MHz : 3600.026 cpu MHz : 3600.026 cpu MHz : 3600.026 cpu MHz : 3600.026 cpu MHz : 3600.026 cpu MHz : 3600.026 cpu MHz : 3600.026 ` now how should I calculate the average cpu usage in MHz for the process? – Sinai May 04 '19 at 15:08
  • Why do you need this in MHz? – ewwhite May 05 '19 at 11:50
  • I want to create a panel for my customers and show their real time resource usage. Because we are selling a special hosting service for high traffic websites and want our customers to pay for the resources they consume (pay as you go) :D – Sinai May 05 '19 at 14:27
  • @asktyagi: no, /procu/cpuinfo shows the current speed of each Cpu core (not the maximum). It changes each time you look at cpuinfo (unless your cpu has been configured to have a static frequency). – CpnCrunch Apr 07 '22 at 04:07
1

Seems like pidstat is the right option for what you're seeking.

For example, pidstat -l 1 -p 52411

You could also get comprehensive graphs on a per-application level from Netdata.

ewwhite
  • 197,159
  • 92
  • 443
  • 809
  • `pidstat` seems to be a good tool. I want to have the CPU usage in frequency like (450MHz, 500MHz, ...). I can get CPU from top or ps for a process, my issue is that the sum of CPU times in top, ps or pidstat should be divided to one cpu core or sum of cpu cores? I think it is spmething related to Irix mode. I found a potential solution to my question in this thred: `https://unix.stackexchange.com/questions/15733/why-process-cpu-usage-larger-than-total-cpu-time`. – Sinai May 05 '19 at 04:48
  • (continue from above comment): Let say `N` is the total number of CPUs. Then In Irix mode, %CPU is the CPU usage out of `N * 100%`. In Solaris mode,%CPU is the `%CPU in Irix mode / N`. – Sinai May 05 '19 at 04:48
  • So with the above explanation if I have 8 cores (output of `cat /pro/cpuinfo`) with 3600MHz each core, and the sum of %CPU is 26.9 (output of `ps -e -o pcpu,user|grep process_name|awk '{sum+=$1} END {print sum}'`). the used frequency in an Irix mode system (which is enable by default in linux) should be 26.9/800*3600=121MHz. – Sinai May 05 '19 at 05:04