linux cpu占有率居高不下 调试

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 今天调试程序,使用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.

今天调试程序,使用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 <http://gnu.org/licenses/gpl.html>
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:
<http://bugs.launchpad.net/gdb-linaro/>...
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),问题解决

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
15天前
|
Linux
Linux rsyslog占用内存CPU过高解决办法
该文档描述了`rsyslog`占用内存过高的问题及其解决方案。
40 4
|
19天前
|
Linux
Linux(5)WIFI/BT调试笔记
Linux(5)WIFI/BT调试笔记
36 0
|
1月前
|
移动开发 运维 监控
掌握Linux运维利器:查看CPU和内存占用,轻松解决性能问题!
掌握Linux运维利器:查看CPU和内存占用,轻松解决性能问题!
|
1月前
|
Linux
如何在linux中查看cpu信息、机器硬件型号
如何在linux中查看cpu信息、机器硬件型号
|
1月前
|
移动开发 Linux
Linux下如何查看哪些进程占用的CPU内存资源最多
Linux下如何查看哪些进程占用的CPU内存资源最多
|
21天前
|
机器学习/深度学习 缓存 监控
linux查看CPU、内存、网络、磁盘IO命令
`Linux`系统中,使用`top`命令查看CPU状态,要查看CPU详细信息,可利用`cat /proc/cpuinfo`相关命令。`free`命令用于查看内存使用情况。网络相关命令包括`ifconfig`(查看网卡状态)、`ifdown/ifup`(禁用/启用网卡)、`netstat`(列出网络连接,如`-tuln`组合)以及`nslookup`、`ping`、`telnet`、`traceroute`等。磁盘IO方面,`iostat`(如`-k -p ALL`)显示磁盘IO统计,`iotop`(如`-o -d 1`)则用于查看磁盘IO瓶颈。
|
14天前
|
网络协议 算法 Linux
【Linux】深入探索:Linux网络调试、追踪与优化
【Linux】深入探索:Linux网络调试、追踪与优化
|
3天前
|
缓存 监控 前端开发
如何在 Linux 命令行中检查 CPU 使用率
【5月更文挑战第8天】
13 0
|
19天前
|
Linux Android开发
Linux(6)CH9434 SPI调试笔记
Linux(6)CH9434 SPI调试笔记
18 0
|
1月前
|
Linux
如何在Linux系统上查看CPU使用率?
以上命令可以帮助你监视和分析Linux系统中的CPU使用率,可以根据需要选择合适的命令进行查看。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
17 0