mirror of
https://gitee.com/huoyo/ko-time.git
synced 2025-12-06 16:58:26 +08:00
add:export heapdump
This commit is contained in:
parent
dead0a981c
commit
abca82ac7c
@ -3,11 +3,10 @@ package cn.langpy.kotime.controller;
|
|||||||
import cn.langpy.kotime.annotation.Auth;
|
import cn.langpy.kotime.annotation.Auth;
|
||||||
import cn.langpy.kotime.config.DefaultConfig;
|
import cn.langpy.kotime.config.DefaultConfig;
|
||||||
import cn.langpy.kotime.model.*;
|
import cn.langpy.kotime.model.*;
|
||||||
import cn.langpy.kotime.service.ClassService;
|
import cn.langpy.kotime.service.*;
|
||||||
import cn.langpy.kotime.service.GraphService;
|
|
||||||
import cn.langpy.kotime.service.SysUsageService;
|
|
||||||
import cn.langpy.kotime.service.ThreadUsageService;
|
|
||||||
import cn.langpy.kotime.util.Context;
|
import cn.langpy.kotime.util.Context;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
@ -99,6 +98,7 @@ public class KoTimeController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/getParamGraph")
|
@GetMapping("/getParamGraph")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@Auth
|
@Auth
|
||||||
@ -259,6 +259,37 @@ public class KoTimeController {
|
|||||||
return heapMemoryInfo;
|
return heapMemoryInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/dumpHeap")
|
||||||
|
@ResponseBody
|
||||||
|
// @Auth
|
||||||
|
public void dumpHeap(Boolean live, HttpServletResponse response) {
|
||||||
|
live = live==null?false:live;
|
||||||
|
HeapDumpService heapDumpService = HeapDumpService.newInstance();
|
||||||
|
String heapDumpFile = heapDumpService.getHeapDumpFile(live);
|
||||||
|
if (heapDumpFile == null) {
|
||||||
|
throw new RuntimeException("Can not dumpheap file!");
|
||||||
|
}
|
||||||
|
log.info(heapDumpFile);
|
||||||
|
response.setCharacterEncoding("utf-8");
|
||||||
|
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||||
|
response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+heapDumpService.getHeapDumpFileName(live));
|
||||||
|
try( OutputStream out = response.getOutputStream();
|
||||||
|
BufferedOutputStream bufferedOut = new BufferedOutputStream(out);
|
||||||
|
FileInputStream fileInputStream = new FileInputStream(heapDumpFile);
|
||||||
|
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)){
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int bytesRead;
|
||||||
|
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
|
||||||
|
bufferedOut.write(buffer,0,bytesRead);
|
||||||
|
}
|
||||||
|
bufferedOut.flush();
|
||||||
|
|
||||||
|
}catch (Exception e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/getPhysicalMemoryInfo")
|
@GetMapping("/getPhysicalMemoryInfo")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@Auth
|
@Auth
|
||||||
|
|||||||
45
src/main/java/cn/langpy/kotime/service/HeapDumpService.java
Normal file
45
src/main/java/cn/langpy/kotime/service/HeapDumpService.java
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
package cn.langpy.kotime.service;
|
||||||
|
|
||||||
|
import com.sun.management.HotSpotDiagnosticMXBean;
|
||||||
|
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.management.ManagementFactory;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class HeapDumpService {
|
||||||
|
private static final String STANDARD_DUMP_NAME = "kotime-heapdump-%s.hprof";
|
||||||
|
private static Logger log = Logger.getLogger(HeapDumpService.class.toString());
|
||||||
|
private static final String HotSpotDiagnosticName = "com.sun.management:type=HotSpotDiagnostic";
|
||||||
|
|
||||||
|
public static HeapDumpService newInstance() {
|
||||||
|
return new HeapDumpService();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHeapDumpFile(boolean live) {
|
||||||
|
String targetFile = System.getProperty("java.io.tmpdir")+File.separator+getHeapDumpFileName(live);
|
||||||
|
try {
|
||||||
|
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
HotSpotDiagnosticMXBean hotSpotDiagnostic = ManagementFactory.newPlatformMXBeanProxy(server, HotSpotDiagnosticName, HotSpotDiagnosticMXBean.class);
|
||||||
|
if (Files.exists(Paths.get(targetFile))) {
|
||||||
|
new File(targetFile).delete();
|
||||||
|
}
|
||||||
|
hotSpotDiagnostic.dumpHeap(targetFile, live);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
log.severe("Can not dump heap file!");
|
||||||
|
}
|
||||||
|
return targetFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHeapDumpFileName(boolean live) {
|
||||||
|
if (live) {
|
||||||
|
return String.format(STANDARD_DUMP_NAME, "live");
|
||||||
|
}
|
||||||
|
return String.format(STANDARD_DUMP_NAME, "all");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user