内存和cpu调试

buddyinfo 小内存越多,碎片越多

perf

可以使用perf进行cpu占用率进行分析

如下代码中,函数AA死循环,预期会占用大量CPU资源

#include<stdio.h>
#include<stdlib.h>

void AA(){
    int i=0;
    while(1){
        i++;
    }
}

void BB(){

    printf("BB\n");
}

int main(){
    BB();
    AA();

}
  • 首先使用top命令查看cpu占用率,可以看出用户空间cpu占用率达到了50%,而内核空间很低,可以看出cpu占用率主要在用户态,涉及系统调用比较少
%Cpu(s): 50.0 us,  8.3 sy,  0.0 ni, 41.7 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
  • 使用perf top,部分结果如下,可以看到一个名为test的进程,同时可以看到一个名为AA的符号,该符号就是名为AA的函数
Samples: 699K of event 'cpu-clock', Event count (approx.): 8217702565                                                                                                                     
Overhead  Shared Object                  Symbol                                                                                                                                           
  99.68%  test                           [.] AA                                                                                                                                           
   0.12%  [kernel]                       [k] _raw_spin_unlock_irqrestore                                                                                                                  
   0.06%  [kernel]                       [k] __do_softirq                                                                                                                                 
   0.02%  [kernel]                       [k] e1000_xmit_frame                                                                                                                             
   0.01%  libc-2.17.so                   [.] _int_malloc                                                                                                                                  
   0.00%  [kernel]                       [k] clear_page                                                                                                                                   
   0.00%  libvmtools.so.0.0.0            [.] Backdoor_InOut                                                                                                                               
   0.00%  [kernel]                       [k] kstat_irqs                                                                                                                                   
  • 使用perf record记录下10s以内的cpu 处理器时钟使用情况,通过perf report可以看到占用率高的进程的调用栈
perf record -a -e cycles -o cycle.perf -g sleep 10
[root@localhost ~]# perf report -i cycle.perf|more
# To display the perf.data header info, please use --header/--header-only options.
#
#
# Total Lost Samples: 0
#
# Samples: 22K of event 'cpu-clock'
# Event count (approx.): 5736750000
#
# Children      Self  Command          Shared Object        Symbol                                                                    
# ........  ........  ...............  ...................  ..........................................................................
#
    88.97%     0.00%  test             libc-2.17.so         [.] __libc_start_main
            |
            ---__libc_start_main
               main
               AA

    88.97%     0.00%  test             test                 [.] main
            |
            ---main
               AA

    88.97%    88.88%  test             test                 [.] AA
            |          
             --88.88%--__libc_start_main
                       main
                       AA

猜你喜欢

转载自www.cnblogs.com/charlieroro/p/10064022.html