java cpu定位,定位cpu 100%

[TOC]

# 简介

线上cpu 100%,定位问题办法

4步

~~~

1. top oder by with P:1040// 首先按进程负载排序找到  axLoad(pid)

2. top -Hp 进程PID:1073// 找到相关负载 线程PID

3. printf “0x%x\\n”线程PID: 0x431// 将线程PID转换为 16进制,为后面查找 jstack 日志做准备

4. jstack  进程PID | vim +/十六进制线程PID -        // 例如:jstack 1040|vim +/0x431 -

~~~

mac查看线程

~~~

ps -M {pid}

~~~

在ps命令中,“-T”选项可以开启线程查看。下面的命令列出了由进程号为的进程创建的所有线程。

~~~

ps -T -p {pid}

~~~

将上面的流程封装为了一个工具:`show-busy-java-threads.sh`( https://github.com/oldratlee/useful-scripts ),可以很方便的定位线上的这类问题

快速安装

~~~

source

~~~

# 案例

## java 正则表达式回溯造成 CPU 100%

~~~

import java.util.ArrayList;

import java.util.List;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RegexLoad {

public static void main(String[] args) {

String[] patternMatch = {"([\\w\\s]+)+([+\\-/*])+([\\w\\s]+)",

"([\\w\\s]+)+([+\\-/*])+([\\w\\s]+)+([+\\-/*])+([\\w\\s]+)"};

List patternList = new ArrayList();

patternList.add("Avg Volume Units product A + Volume Units product A");

patternList.add("Avg Volume Units / Volume Units product A");

patternList.add("Avg retailer On Hand / Volume Units Plan / Store Count");

patternList.add("Avg Hand Volume Units Plan Store Count");

patternList.add("1 - Avg merchant Volume Units");

patternList.add("Total retailer shipment Count");

for (String s :patternList ){

for(int i=0;i

Pattern pattern = Pattern.compile(patternMatch[i]);

Matcher matcher = pattern.matcher(s);

System.out.println(s);

if (matcher.matches()) {

System.out.println("Passed");

}else

System.out.println("Failed;");

}

}

}

}

~~~

怎么使用呢?

~~~

show-busy-java-threads.sh

# 从 所有的 Java进程中找出最消耗CPU的线程(缺省5个),打印出其线程栈。

show-busy-java-threads.sh -c

show-busy-java-threads.sh -c -p

# -F选项:执行jstack命令时加上-F选项(强制jstack),一般情况不需要使用

show-busy-java-threads.sh -p -F

show-busy-java-threads.sh -s

# 对于sudo方式的运行,JAVA_HOME环境变量不能传递给root,

# 而root用户往往没有配置JAVA_HOME且不方便配置,

# 显式指定jstack命令的路径就反而显得更方便了

show-busy-java-threads.sh -a

show-busy-java-threads.sh -t -i

# 缺省执行一次;执行间隔缺省是3秒

##############################

# 注意:

##############################

# 如果Java进程的用户 与 执行脚本的当前用户 不同,则jstack不了这个Java进程。

# 为了能切换到Java进程的用户,需要加sudo来执行,即可以解决:

sudo show-busy-java-threads.sh

~~~

示例:

~~~

work@dev_zz_Master 10.48.186.32 23:45:50 ~/demo >

bash show-busy-java-threads.sh

[1] Busy(96.2%) thread(8577/0x2181) stack of java process(8576) under user(work):

"main" prio=10 tid=0x00007f0c64006800 nid=0x2181 runnable [0x00007f0c6a64a000]

java.lang.Thread.State: RUNNABLE

at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)

at java.util.regex.Pattern$Loop.match(Pattern.java:4295)

...

at java.util.regex.Matcher.match(Matcher.java:1127)

at java.util.regex.Matcher.matches(Matcher.java:502)

at RegexLoad.main(RegexLoad.java:27)

[2] Busy(1.5%) thread(8591/0x218f) stack of java process(8576) under user(work):

"C2 CompilerThread1" daemon prio=10 tid=0x00007f0c64095800 nid=0x218f waiting on condition [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

[3] Busy(0.8%) thread(8590/0x218e) stack of java process(8576) under user(work):

"C2 CompilerThread0" daemon prio=10 tid=0x00007f0c64093000 nid=0x218e waiting on condition [0x0000000000000000]

java.lang.Thread.State: RUNNABLE

[4] Busy(0.2%) thread(8593/0x2191) stack of java process(8576) under user(work):

"VM Periodic Task Thread" prio=10 tid=0x00007f0c640a2800 nid=0x2191 waiting on condition

[5] Busy(0.1%) thread(25159/0x6247) stack of java process(25137) under user(work):

"VM Periodic Task Thread" prio=10 tid=0x00007f13340b4000 nid=0x6247 waiting on condition

work@dev_zz_Master 10.48.186.32 23:46:04 ~/demo >

~~~

可以看到,一键直接定位异常代码行

## 线程死锁,程序卡住

~~~

import java.util.*;

public class SimpleDeadLock extends Thread {

public static Object l1 = new Object();

public static Object l2 = new Object();

private int index;

public static void main(String[] a) {

Thread t1 = new Thread1();

Thread t2 = new Thread2();

t1.start();

t2.start();

}

private static class Thread1 extends Thread {

public void run() {

synchronized (l1) {

System.out.println("Thread 1: Holding lock 1...");

try { Thread.sleep(10); }

catch (InterruptedException e) {}

System.out.println("Thread 1: Waiting for lock 2...");

synchronized (l2) {

System.out.println("Thread 2: Holding lock 1 & 2...");

}

}

}

}

private static class Thread2 extends Thread {

public void run() {

synchronized (l2) {

System.out.println("Thread 2: Holding lock 2...");

try { Thread.sleep(10); }

catch (InterruptedException e) {}

System.out.println("Thread 2: Waiting for lock 1...");

synchronized (l1) {

System.out.println("Thread 2: Holding lock 2 & 1...");

}

}

}

}

}

~~~

![](https://img.kancloud.cn/a4/e3/a4e3dffda6558ddeaa53938777a74f8f_3150x1396.png)

一键定位:可以清晰的看到线程互相锁住了对方等待的资源,导致死锁,直接定位到代码行和具体原因。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值