JVM Profiler StacktraceCollectorProfiler
开篇
StacktraceCollectorProfiler主要用来采集线程的调用栈,原理是通过ManagementFactory.getThreadMXBean()返回的ThreadMXBean对象来实现。
源码分析
public void profile() { // 获取此刻所有线程的dump信息 ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false); if (threadInfos == null) { return; } // 遍历线程信息并处理每个线程 for (ThreadInfo threadInfo : threadInfos) { String threadName = threadInfo.getThreadName(); if (threadName == null) { threadName = ""; } if (!ignoreThreadNamePrefix.isEmpty() && threadName.startsWith(ignoreThreadNamePrefix)) { continue; } // 获取线程的调用栈 StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); // 创建调用栈对象 Stacktrace stacktrace = new Stacktrace(); stacktrace.setThreadName(threadName); stacktrace.setThreadState(String.valueOf(threadInfo.getThreadState())); // 创建保存调用栈的list int totalLength = 0; List<ClassAndMethod> stack = new ArrayList<>(stackTraceElements.length); // 按照倒序保存调用链,最底层的放在最前面,把最根本的原因发在前面 for (int i = stackTraceElements.length - 1; i >= 0; i--) { StackTraceElement stackTraceElement = stackTraceElements[i]; String className = String.valueOf(stackTraceElement.getClassName()); String methodName = String.valueOf(stackTraceElement.getMethodName()); stack.add(new ClassAndMethod(className, methodName)); totalLength += className.length() + methodName.length(); // 如果长度超出限制,那么就截断 if (totalLength >= Constants.MAX_STRING_LENGTH) { stack.add(new ClassAndMethod("_stack_", "_trimmed_")); break; } } // 反转调用链,把最底层的放在最前面。 ClassAndMethod[] classAndMethodArray = new ClassAndMethod[stack.size()]; for (int i = 0; i < stack.size(); i++) { classAndMethodArray[classAndMethodArray.length - 1 - i] = stack.get(i); } stacktrace.setStack(classAndMethodArray); buffer.appendValue(stacktrace); } }
采集结果
调用链的反向序列为:
- java.lang.Thread.sleep
- com.uber.profiling.examples.HelloWorldApplication.privateSleepMethod
- com.uber.profiling.examples.HelloWorldApplication.main
{ "stacktrace": ["java.lang.Thread.sleep", "com.uber.profiling.examples.HelloWorldApplication.privateSleepMethod", "com.uber.profiling.examples.HelloWorldApplication.main"], "endEpoch": 1536072801080, "appId": null, "host": "xiaozhideMacBook-Pro.local", "name": "2203@xiaozhideMacBook-Pro.local", "processUuid": "1e580f6e-0493-4e5b-bee2-a61c5f7b097d", "threadState": "TIMED_WAITING", "count": 24, "tag": "mytag", "startEpoch": 1536072796084, "threadName": "main" }

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
amazeUI 地区选择器三级联动问题解决,带地区数据
2018年5月25号 直接上代码 <div id="area_box"> <select data-am-selected="{maxHeight:200,btnWidth:'120px'}" placeholder="请选择省" id="province"> <option value=""></option> </select> <select data-am-selected="{maxHeight:200,btnWidth:'120px'}" placeholder="请选择市" id="city"> <option value=""></option> </select> <select data-am-selected="{maxHeight:200,btnWidth:'120px'}" placeholder="请选择区/县" id="area"> <option value=""></option> </select>...
- 下一篇
Echarts 象形图
2018年5月15日 var walk = 'image://../images/health_walk.png'; var dom = document.getElementById("recoveryChart"); var myChart = echarts.init(dom); var app = {}; option = null; option = { tooltip: { trigger: 'axis', formatter: '{b}<br />{a1}:{c1}' }, legend: { show: true, data: ['改善目标'], textStyle: { color: '#000' }, left: 38 }, grid: { top: 60, left: 8, containLabel: true, }, color: ['#0f0', '#c23531', '#2f4554', '#61a0a8', '#d48265', '#91c7ae', '#749f83', '#ca8622', '#bda29a', '#6e7074', '#5...
相关文章
文章评论
共有0条评论来说两句吧...