日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ms查约束具体代码_记 Arthas 实现一次 CPU 排查与代码热更新

發(fā)布時間:2024/9/27 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ms查约束具体代码_记 Arthas 实现一次 CPU 排查与代码热更新 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
簡介:線上代碼經(jīng)常會出現(xiàn) CPU 占用過高的情況,按以往經(jīng)驗我會使用 top 指令,進一步借助于 jstack 去查看具體信息從而進行問題排查,但基本上都逃不過需要重新發(fā)包的局面,及時是一個增量包,應用也需要短暫停啟。后來運維大兄弟讓我試一下 Arthas,說是可以進行代碼的熱更新操作,正好來試一下。

1.前言

背景

線上代碼經(jīng)常會出現(xiàn) CPU 占用過高的情況,按以往經(jīng)驗我會使用 top 指令,進一步借助于 jstack 去查看具體信息從而進行問題排查,但基本上都逃不過需要重新發(fā)包的局面,及時是一個增量包,應用也需要短暫停啟。后來運維大兄弟讓我試一下 Arthas,說是可以進行代碼的熱更新操作,正好來試一下。

環(huán)境

JDK1.8
SPringBoot 2.2.2
Arthas
Linux

測試代碼:

@RequestMapping(value = "/bigThread") @ResponseBody public String bigThread(int id) {ArthasService.test();while (true) {Thread t2 = new Thread();t2.start();id ++;if(100000 == id) {return String.valueOf(id);}} }

思路

2.thread -b 查看是否有阻塞線程

thread -b, 找出當前阻塞其他線程的線程,執(zhí)行完之后并未發(fā)現(xiàn),說明該線程并非一直阻塞、一直執(zhí)行的。

3.thread 查看占用最高的線程

當 thread 之后不跟參數(shù)時,顯示當前全部線程信息,我覺得 thread -n 10,展示前 10 應該就夠用,可根據(jù)實際需要自己決定。

下圖可以很直觀的看出,我們的應用瞬間占用了 77% 的 CPU(這里我是發(fā)起請求瞬間,通過 thread 查看的,所以比較直觀,生產(chǎn)環(huán)境應該只有阻塞,死鎖這種狀態(tài)才會比較直觀)。

4.thread id 查看具體信息

在上一步基礎(chǔ)上,我們進一步查看,thread 15(因為上面的 ID=15)。

他的大致意思就是:線程在等待一個條件從而繼續(xù)執(zhí)行,可以看到方法是在執(zhí)行 LinkedBlockingQueue.take 方法時候,查看這個方法的 API 提示如下:

public E take() throws InterruptedException {E x;int c = -1;final AtomicInteger count = this.count;final ReentrantLock takeLock = this.takeLock;takeLock.lockInterruptibly();try {while (count.get() == 0) {notEmpty.await();}x = dequeue();c = count.getAndDecrement();if (c > 1)notEmpty.signal();} finally {takeLock.unlock();}if (c == capacity)signalNotFull();return x; }

其中:AtomicInteger 是保證高并發(fā)情況下的原子性,ReentrantLock 標識可重入鎖,都是 JUC 包下需要了解的這里不贅述,需要的百度了解下。

這段代碼關(guān)鍵點就在于:notEmpty.await(),從隊列中消費數(shù)據(jù),當隊列為空是,線程阻塞,所以我們大致知道現(xiàn)在出現(xiàn)的問題是線程阻塞,但是還是不知道具體哪行代碼的問題。

如果能夠明確知道這次更改了哪些代碼,可以直接執(zhí)行步驟 6,不知道的話可以通過步驟 5 來定位問題。

5.watch 查看哪個 Controller 執(zhí)行了代碼

watch org.springframework.web.servlet.DispatcherServlet getHandler returnObj

這個腳本可以檢測一切通過 DispatcherServlet 匹配 Handler 的方法,也就是進入 Controller 的請求,如下:

找到了對應的代碼之后,我們來進一步觀察異常信息,這里可能會有一個問題:就是我明明能通過日志去查看錯誤信息,為什么還需要這么繁瑣的去操作。我的業(yè)務(wù)場景是:日志還是非常大的,剛撈到就被刷過去了,這時候定位日志不是很好操作,當然想撈下來日志肯定也是可以的,也很直觀,我一般也都是去查看日志進行問題定位,這里也是提供一個思路。

6.watch 該方法異常信息

watch 類全路徑 方法名 "{params[0],throwExp}" -e -x 2

如上,錯誤很直觀的提示了出來,下面就可以修復解決了,這里我們也可以通過 trace 指令,查看執(zhí)行時長:

trace 類全路徑 方法名 "{params[0],throwExp}" -e -x 2

返回信息如下,也可以看到錯誤信息,和每個方法執(zhí)行的時長。

[arthas@10999]$ trace com.arthas.controller.OrderController bigThread Press Q or Ctrl+C to abort. Affect(class count: 1 , method count: 1) cost in 53 ms, listenerId: 10 `---ts=2020-08-19 14:45:57;thread_name=http-nio-0.0.0.0-8080-exec-10;id=16;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@1f1c7bf6`---[1452.684406ms] com.arthas.controller.OrderController:bigThread() [throws Exception]+---[0.168814ms] com.arthas.service.ArthasService:test() #20`---throw:java.lang.OutOfMemoryError #-2 [unable to create new native thread]

7.jad 反編譯熱更新

在上面知道問題之后,我們就來定位問題就好了。

命令:jad 類全路徑 方法名

[arthas@13190]$ jad com.arthas.controller.OrderControllerClassLoader: +-org.springframework.boot.loader.LaunchedURLClassLoader@17f052a3 +-sun.misc.Launcher$AppClassLoader@3d4eac69 +-sun.misc.Launcher$ExtClassLoader@45f45fa1 Location: file:/opt/software/arthas/Arthas.jar!/BOOT-INF/classes!/ /** Decompiled with CFR.* * Could not load the following classes:* com.arthas.service.ArthasService* org.springframework.stereotype.Controller* org.springframework.web.bind.annotation.RequestMapping* org.springframework.web.bind.annotation.ResponseBody*/ package com.arthas.controller;import com.arthas.service.ArthasService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class OrderController {@RequestMapping(value={"/bigThread"})@ResponseBodypublic String bigThread(int id) {ArthasService.test();do {Thread t2 = new Thread();t2.start();} while (100000 != ++id);return String.valueOf(id);} }Affect(row-cnt:1) cost in 1513 ms.

此時代碼就被反編譯了,為了能夠更改,所以我們需要輸出為 java 文件。

指令:jad com.arthas.controller.OrderController > /tmp/OrderController.java

即:jad 類全路徑 方法名 > 存儲路徑/存儲名稱

然后到 tmp 路徑下 vi 修改 java 文件即可,修改完成之后,查看對應的 classloader 為編譯做準備。

sc -d *OrderController | grep classLoaderHash mc -c 17f052a3 /tmp/OrderController.java -d /tmp

但是這里編譯出錯了,官方提示:

所以我們本地編譯好 class 文件,上傳上去是一樣的。

編譯前調(diào)用

[arthas@13190]$ trace com.arthas.controller.OrderController bigThread Press Q or Ctrl+C to abort. Affect(class count: 1 , method count: 1) cost in 77 ms, listenerId: 2 `---ts=2020-08-19 15:51:46;thread_name=http-nio-0.0.0.0-8080-exec-1;id=d;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@1f1c7bf6`---[6734.666529ms] com.arthas.controller.OrderController:bigThread() [throws Exception]+---[0.786517ms] com.arthas.service.ArthasService:test() #20`---throw:java.lang.OutOfMemoryError #-2 [unable to create new native thread]

更新前代碼

@RequestMapping(value = "/bigThread") @ResponseBody public String bigThread(int id) {ArthasService.test();while (true) {Thread t2 = new Thread();t2.start();id ++;if(100000 == id) {return String.valueOf(id);}} }

更新后代碼

@RequestMapping(value = "/bigThread") @ResponseBody public String bigThread(int id) {ArthasService.test();Thread t2 = new Thread();t2.start();return "success"; }

編譯指令

[arthas@13190]$ redefine /tmp/OrderController.class redefine success, size: 1, classes: com.arthas.controller.OrderController

編譯后調(diào)用三次

`---ts=2020-08-19 15:52:02;thread_name=http-nio-0.0.0.0-8080-exec-3;id=f;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@1f1c7bf6`---[5.609405ms] com.arthas.controller.OrderController:bigThread()`---[0.204675ms] com.arthas.service.ArthasService:test() #20`---ts=2020-08-19 15:52:04;thread_name=http-nio-0.0.0.0-8080-exec-4;id=10;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@1f1c7bf6`---[3.900149ms] com.arthas.controller.OrderController:bigThread()`---[0.14636ms] com.arthas.service.ArthasService:test() #20`---ts=2020-08-19 15:52:04;thread_name=http-nio-0.0.0.0-8080-exec-5;id=11;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@1f1c7bf6`---[1.90945ms] com.arthas.controller.OrderController:bigThread()`---[0.147353ms] com.arthas.service.ArthasService:test() #20

可以發(fā)現(xiàn)時間從 6734.666529ms 變成 3ms 左右,說明熱更新的代碼生效了。

8.profile 繪制火焰圖做后續(xù)分析

如下圖所示:

作者:何波

原文鏈接

本文為阿里云原創(chuàng)內(nèi)容,未經(jīng)允許不得轉(zhuǎn)載。

總結(jié)

以上是生活随笔為你收集整理的ms查约束具体代码_记 Arthas 实现一次 CPU 排查与代码热更新的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。