zoukankan      html  css  js  c++  java
  • Linux 线程占用CPU过高定位分析

    今天朋友问我一个Linux程序CPU占用涨停了,该如何分析,

    CPU占用过高,模拟CPU占用过高的情况

    先上一段代码:

     1 #include <iostream>
     2 #include <thread>
     3 #include <vector>
     4 
     5 
     6 int main(int argc, char **argv) {
     7   
     8     std::vector<std::thread> test_threads;
     9     for(int i = 0; i < 9; i++){
    10       test_threads.push_back(std::thread([]{
    11     while(1){
    12       std::this_thread::sleep_for(std::chrono::milliseconds(500));
    13     }
    14       }));
    15     }
    16     test_threads.push_back(std::thread([]{
    17       while(1){
    18       std::cout<<"cpu"<<std::endl;
    19       }
    20     }));
    21     
    22     for(auto &x : test_threads){
    23       x.join();
    24     }
    25     
    26     return 0;
    27 }

    第10个线程中没有进行睡眠,会独占进程的时间片,导致CPU利用率过高,

    现在就要定位到第10个

    第一步:top 查看程序进程id

     

    第二步:top -H -p 96263 定位CPU占用过高的线程id

     

    第三步:使用pstack 96263或者strace -f -p 96263 定位线程堆栈

    strace -f -p 96263

     

  • 相关阅读:
    旋转数组中的最小数字
    二叉树的遍历:先序,中序,后序,递归,非递归,层次遍历
    重建二叉树
    Combination Sum II
    Combination Sum
    红黑树
    python 时间模块
    docker 常用命令
    python request 和requests 的区别
    python 面试题1
  • 原文地址:https://www.cnblogs.com/Forever-Kenlen-Ja/p/8618102.html
Copyright © 2011-2022 走看看