1

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
1

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

| improve this answer | |
  • 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 4 '19 at 8: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 4 '19 at 8: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 4 '19 at 9: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 4 '19 at 9: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 4 '19 at 9:59
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.

| improve this answer | |
  • 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 5 '19 at 4: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 5 '19 at 4: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 5 '19 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.