今天调试程序,使用top命令后,发现程序的cpu占有率很高,一直在99,这很可怕,所以来调试。

使用top命令,得如下结果

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  1997 root 20 0 358m 71m 3208 S 99.1 7.2 81:53.50 test 1 root 20 0 24332 2044 1176 S 0.0 0.2 0:01.36 init  2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd  3 root 20 0 0 0 0 S 0.0 0.0 0:46.51 ksoftirqd/0  4 root 20 0 0 0 0 S 0.0 0.0 0:38.53 kworker/0:0  6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0

可知test的PID为1997

然后具体查看test里线程的cpu使用情况

使用 top -H -p 1997 命令

root@slk:~# top -H -p 1997

top - 17:19:47 up 15 days, 34 min, 4 users, load average: 1.02, 1.06, 1.06 Tasks: 8 total, 1 running, 7 sleeping, 0 stopped, 0 zombie Cpu(s): 99.3%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.3%si, 0.3%st Mem: 1017924k total, 887500k used, 130424k free, 85928k buffers Swap: 0k total, 0k used, 0k free, 351280k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND  2005 root 20 0 358m 71m 3208 R 99.3 7.2 83:44.47 test  2001 root 20 0 358m 71m 3208 S 0.3 7.2 0:08.66 test  2004 root 20 0 358m 71m 3208 S 0.3 7.2 0:19.61 test  1997 root 20 0 358m 71m 3208 S 0.0 7.2 0:26.24 test  1999 root 20 0 358m 71m 3208 S 0.0 7.2 0:00.06 test  2000 root 20 0 358m 71m 3208 S 0.0 7.2 0:00.00 test 2002 root 20 0 358m 71m 3208 S 0.0 7.2 0:05.86 Server Listen(2  2003 root 20 0 358m 71m 3208 S 0.0 7.2 0:00.00 Server Accept(2

可得线程中CPU占有率最高的线程的PID是2005

使用gdb icdn 2005 命令

root@slk:~# gdb icdn 2005 GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04 Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". For bug reporting instructions, please see: ... icdn: No such file or directory. Attaching to process 2005

warning: process 2005 is a cloned process Reading symbols from /home/slk/test/snmptrapd...done. Reading symbols from /usr/lib/libnetsnmp.so.30...done. Loaded symbols for /usr/lib/libnetsnmp.so.30 Reading symbols from /lib/x86_64-linux-gnu/libpthread.so.0...(no debugging symbols found)...done. [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1". Loaded symbols for /lib/x86_64-linux-gnu/libpthread.so.0 Reading symbols from /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18...(no debugging symbols found)...done. Loaded symbols for /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18 Reading symbols from /usr/local/lib/libstd.so...(no debugging symbols found)...done. Loaded symbols for /usr/local/lib/libstd.so Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6 Reading symbols from /lib/x86_64-linux-gnu/librt.so.1...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/librt.so.1 Reading symbols from /lib/x86_64-linux-gnu/libcrypto.so.1.0.0...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done. Loaded symbols for /lib64/ld-linux-x86-64.so.2 Reading symbols from /lib/x86_64-linux-gnu/libz.so.1...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libz.so.1 Reading symbols from /lib/x86_64-linux-gnu/libdl.so.2...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libdl.so.2 Reading symbols from /lib/x86_64-linux-gnu/libm.so.6...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libm.so.6 Reading symbols from /usr/local/lib/libudt.so...(no debugging symbols found)...done. Loaded symbols for /usr/local/lib/libudt.so Reading symbols from /usr/lib/x86_64-linux-gnu/libstdc++.so.6...(no debugging symbols found)...done. Loaded symbols for /usr/lib/x86_64-linux-gnu/libstdc++.so.6 Reading symbols from /lib/x86_64-linux-gnu/libgcc_s.so.1...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libgcc_s.so.1 Reading symbols from /lib/x86_64-linux-gnu/libnss_files.so.2...(no debugging symbols found)...done. Loaded symbols for /lib/x86_64-linux-gnu/libnss_files.so.2 execute_search () at test.c:310

从最后一行可知,线程2005运行的是execute_search这个函数,然后查看该函数,发现有这样一段

while(g_search_state == 1)  {    tmp = g_data_list->phead->next;    if (tmp)    {      pthread_mutex_lock(&g_mysql_mutex);      res.result = search_mysql(tmp->sn, &(res.machine));      pthread_mutex_unlock(&g_mysql_mutex);      snprintf(res.sn, sizeof(res.sn), "%s", tmp->sn);    }  }

由于tmp长期为NULL,所以这变成了一个while的死循环,而死循环极为占用内存,于是再下面加上usleep(50000),问题解决

cpu居高不下 linux,linux cpu居高不下 调试相关推荐

  1. linux 使cpu使用率升高_Linux系统中CPU占用率较高问题排查思路与解决方法

    Linux服务器上出现CPU负载达到100%居高不下的情况,如果CPU 持续跑高,则会影响业务系统的正常运行: CPU利用率.根据经验来看,用户空间进程占用CPU比例在 65-70%之间,内核(系统) ...

  2. linux系统cpu性能测试工具

    这里写自定义目录标题 uptime 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你 ...

  3. arm linux cpu优化,ARM CPU与Intel x86 CPU性能比较

    原标题:ARM CPU与Intel x86 CPU性能比较 随着移动互联网时代的到来,Qualcomm(高通).Texas Instruments(德州仪器)等基于 架构的 受到越来越多人的关注,而昔 ...

  4. Linux 系统 CPU 使用率简单分析

    CPU 使用率是单位时间内 CPU 使用情况的统计,以百分比的方式展示. CPU 使用率 为了维护 CPU 时间,Linux 通过事先定义的节拍率(内核中表示为 HZ),触发时间中断,并使用全局变量 ...

  5. Linux CPU性能优化 —— CPU使用率及高CPU使用率排查

    文章目录 CPU使用率的定义 查看CPU使用率:top和pidstat 排查高CPU使用率:pref 参考文献 写在前面: 由于之前在开发分布式系统中由于云服务器性能原因,导致系统总是断连等错误.但是 ...

  6. linux 【CPU性能】CPU使用率达到100%怎么办?

    文章目录 1. 简介 2. CPU 使用率 3. 怎么查看 CPU 使用率 4. CPU 使用率过高怎么办? 5. 案例 6. 总结 1. 简介 你最常用什么指标来描述系统的 CPU 性能呢?我想你的 ...

  7. linux top cpu核数查看,Linux怎么查看CPU核数?

    在Linux中,可以使用"cat /proc/cpuinfo| grep "cpu cores"| uniq"来查看CPU核数,即每个物理CPU中core的个数 ...

  8. Linux查看CPU和内存使用情况详解

    在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中, 可以通过 top 命令来查看 CPU 使用状况.运行 top 命令后,CPU 使用状态 ...

  9. linux cpu 超频,Linux 调整 cstate 实现cpu超频

    Ubuntu 设置 与开机项有关的参数设置在 /etc/default/grub,可以对其进行调整 cat /etc/default/grub # If you change this file, r ...

  10. php多线程多核,Linux查看CPU个数/多核/多线程的查看

    在Linux系统中,如何详细了解CPU的信息呢? 当然是通过cat /proc/cpuinfo来检查了,但是比如几个物理CPU/几核/几线程,这些问题怎么确定呢? 过查看,我的开发机器是1个物理CPU ...

最新文章

  1. 如何卸载挂载的共享文件夹
  2. QT的QBuffer类的使用
  3. svn提交报e200007错误
  4. js的字符串和变量拼接
  5. leetcode155. 最小栈
  6. 面试进阶题集锦-持续更新
  7. linux物理内存地址与iomem,一种Linux系统物理内存镜像文件分析方法_4
  8. 小程序 | 微信小程序实现图片是上传、预览功能
  9. java用volatile或AtomicBoolean实现高效并发处理 (只初始化一次的功能要求)
  10. [Java] 蓝桥杯ALGO-13 算法训练 拦截导弹
  11. opencv _rect
  12. Vue指令之v-bind
  13. Android Theme.AppCompat 中,你应该熟悉的颜色属性
  14. css如何放大网页字体大小怎么调整,CSS网页设计时关于字体大小的设计
  15. 关于软件逆向工程的一些不错博客
  16. 通过证书管理解决无法连接 Citrix XenApp SSL 61 您还未选择信任证书颁发者的问题
  17. c语言 自动计时的秒表,c语言实现的简单秒表计时器
  18. stm32开发3D打印机(五)——TF卡spi协议与FATFS文件系统(已完成)
  19. c语言length函数,length_length什么意思
  20. De Bruijn 序列生成的本原多项式方法

热门文章

  1. linux 指定库名 登录mysql_数据库学习笔记之MySQL(01)
  2. 什么是线程?与进程又有什么区别,为什么要使用它,等对线程进行详细介绍
  3. 打印 Python 的一切 —— pprint beeprint
  4. 【python3】通过hashlib 和base64 对字符串进行加密
  5. python函数中把列表(list)当参数时的入坑与出坑
  6. linux进程VSZ(虚拟内存)
  7. ubuntu 交叉编译 armv7_32 ffmpeg x264(已完成)
  8. python 计算 IOU
  9. activemq启动wrapper stopped
  10. 「后端小伙伴来学前端了」Vue中学会使用Echarts生成各种各样的图表,得学学了,必须要会的基本操作了