jmaps

jmaps · 浏览次数 : 170

小编点评

**Summary:** The script uses the `perf-map-agent` tool to generate symbol maps for all running `java` processes and writes them to `/tmp/perf-PID.map` files. These maps are later read by the `perf_events` command, which uses them to generate system profiles. **Steps:** 1. **Find running `java` processes:** - `pgrep` is used to search for all processes with the `java` suffix. - The `pid` of each process is stored in `mapfile` names. 2. **Execute `perf-map-agent` on each process:** - `perf-map-agent` is a tool for creating symbol maps from the `java` process's memory. - `$AGENT_JAR` contains the Java runtime binary. - `$AGENT_OUT` stores the path to the generated symbol map file. 3. **Remove temporary map file:** - After `perf-map-agent` finishes, the temporary map file is deleted. 4. **Output map file information:** - The script prints the PID, username, and size of each map file. **Requirements:** - `perf-map-agent` tool - `pgrep` - `perf_events` (optional, for reading symbol maps) - Java runtime environment with `perf-map-agent` included **Usage:** 1. Save the script to a file, e.g., `jmaps.sh`. 2. Make the script executable: `chmod +x jmaps.sh`. 3. Run the script: `./jmaps.sh`. **Additional Notes:** - `-u` option for `jmaps` allows for unfoldall analysis, including inline symbols. - `-F 99` option for `perf record` sets the sampling frequency to 99%. - `-a` option for `perf record` collects all available data points. - `-g` option for `perf record` includes graphical output. - `--sleep 30` option pauses the script execution for 30 seconds.

正文

#!/bin/bash
#
# jmaps - creates java /tmp/perf-PID.map symbol maps for all java processes.
#
# This is a helper script that finds all running "java" processes, then executes
# perf-map-agent on them all, creating symbol map files in /tmp. These map files
# are read by perf_events (aka "perf") when doing system profiles (specifically,
# the "report" and "script" subcommands).
#
# USAGE: jmaps [-u]
#		-u	# unfoldall: include inlined symbols
#
# My typical workflow is this:
#
# perf record -F 99 -a -g -- sleep 30; jmaps
# perf script > out.stacks
# ./stackcollapse-perf.pl out.stacks | ./flamegraph.pl --color=java --hash > out.stacks.svg
#
# The stackcollapse-perf.pl and flamegraph.pl programs come from:
# https://github.com/brendangregg/FlameGraph
#
# REQUIREMENTS:
# Tune two environment settings below.
#
# 13-Feb-2015	Brendan Gregg	Created this.
# 20-Feb-2017      "      "     Added -u for unfoldall.

JAVA_HOME=/usr/lib/jvm/java-8-oracle
AGENT_HOME=/usr/lib/jvm/perf-map-agent  # from https://github.com/jrudolph/perf-map-agent
debug=0

if [[ "$USER" != root ]]; then
	echo "ERROR: not root user? exiting..."
	exit
fi

if [[ ! -x $JAVA_HOME ]]; then
	echo "ERROR: JAVA_HOME not set correctly; edit $0 and fix"
	exit
fi

if [[ ! -x $AGENT_HOME ]]; then
	echo "ERROR: AGENT_HOME not set correctly; edit $0 and fix"
	exit
fi

if [[ "$1" == "-u" ]]; then
	opts=unfoldall
fi

# figure out where the agent files are:
AGENT_OUT=""
AGENT_JAR=""
if [[ -e $AGENT_HOME/out/attach-main.jar ]]; then
	AGENT_JAR=$AGENT_HOME/out/attach-main.jar
elif [[ -e $AGENT_HOME/attach-main.jar ]]; then
	AGENT_JAR=$AGENT_HOME/attach-main.jar
fi
if [[ -e $AGENT_HOME/out/libperfmap.so ]]; then
	AGENT_OUT=$AGENT_HOME/out
elif [[ -e $AGENT_HOME/libperfmap.so ]]; then
	AGENT_OUT=$AGENT_HOME
fi
if [[ "$AGENT_OUT" == "" || "$AGENT_JAR" == "" ]]; then
	echo "ERROR: Missing perf-map-agent files in $AGENT_HOME. Check installation."
	exit
fi

# Fetch map for all "java" processes
echo "Fetching maps for all java processes..."
for pid in $(pgrep -x java); do
	mapfile=/tmp/perf-$pid.map
	[[ -e $mapfile ]] && rm $mapfile

	cmd="cd $AGENT_OUT; $JAVA_HOME/bin/java -Xms32m -Xmx128m -cp $AGENT_JAR:$JAVA_HOME/lib/tools.jar net.virtualvoid.perf.AttachOnce $pid $opts"
	(( debug )) && echo $cmd

	user=$(ps ho user -p $pid)
	if [[ "$user" != root ]]; then
		# make $user the username if it is a UID:
		if [[ "$user" == [0-9]* ]]; then user=$(awk -F: '$3 == '$user' { print $1 }' /etc/passwd); fi
		cmd="sudo -u $user sh -c '$cmd'"
	fi

	echo "Mapping PID $pid (user $user):"
	if (( debug )); then
		time eval $cmd
	else
		eval $cmd
	fi
	if [[ -e "$mapfile" ]]; then
		chown root $mapfile
		chmod 666 $mapfile
	else
		echo "ERROR: $mapfile not created."
	fi

	echo "wc(1): $(wc $mapfile)"
	echo
done

与jmaps相似的内容:

jmaps

#!/bin/bash # # jmaps - creates java /tmp/perf-PID.map symbol maps for all java processes. # # This is a helper script that finds all running "java" p

[转帖]jmap执行失败了,怎么获取heapdump?

https://www.jianshu.com/p/f4bfd169b4ca 在之前的OOM问题复盘中,我们添加了jmap脚本来自动dump内存现场,方便排查OOM问题。 但当我反复模拟OOM场景测试时,发现jmap有时可以dump成功,有时会报错,如下: 经过网上一顿搜索,发现两种原因可能导致这个

[转帖]JVM监控和诊断工具

目录 jps jstat jmap jinfo jstack 总结 作者:@dwtfukgv本文为作者原创,转载请注明出处:https://www.cnblogs.com/dwtfukgv/p/15126148.html JVM监控和诊断工具 jps 它将打印所有正在运行的Java进程的相关信息。

[转帖]JVM调优常用命令(jstat、jmap、jstack)

原文:https://www.cnblogs.com/ityouknow/p/5714703.html 一、jstat jstat(JVM statistics Monitoring)是用于监视虚拟机运行时状态信息的命令,它可以显示出虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据。 命令

[转帖]查看堆内对象的工具:jmap

文章目录 用途命令格式示例一,no option二,heap三,histo[:live]四,clstats五,finalizerinfo六,dump: 用途 jmap 可以查看堆内对象的信息,生成 java 程序的 dump 文件,甚至可以查看 ClassLoader 的

gcore的学习

gcore的学习-解决jmap无法生成dump文件的一种方法 背景 周末在跆拳道馆看孩子练跆拳道. 开着笔记本翻到了 扣钉日记 公众号里面的讲解 想着自己也遇到过无法保存dump文件的情况. 所以想学习一下. 进行联系 其实之前也验证过宕机时的dump文件. 感觉是比较类似的. gcore NAME

[转帖]java并发-一些系统分析工具

https://segmentfault.com/a/1190000041541631 内存分析 java内存分析一般采用Java jmap dump下系统内存文件,使用 jmat 进行分析。 线程分析 线程栈一般通过 jstack 得到。 cpu分析 火焰图arthas 火焰图做为cpu调试的神器

一次Java服务内存过高的分析过程

现象 年前,收到了短信报警,显示A服务的某台机器内存过高,超过80% 如上图所示,内存会阶段性增加。奇怪的是,十多台机器中只有这一台有这个问题 堆内内存分析 最先怀疑是内存泄漏的问题,所以首先使用jmap命令把堆dump下来 jmap -dump:format=b,file=service.hpro

[转帖]JVM性能调优监控工具

原文 https://www.cnblogs.com/haiyang1985/p/7654654.html 摘要: JDK自己提供了不少方便的JVM性能调优监控工具,除了集成式的VisualVM和jConsole外,还有jps、jstack、jmap、jhat、jstat、hprof等小巧的工具,本

[转帖] JVM诊断命令jcmd介绍

https://www.cnblogs.com/codelogs/p/16535451.html 简介# 从JDK7开始,jdk提供了一个方便扩展的诊断命令jcmd,用来取代之前比较分散的jdk基础命令,如jps、jstack、jmap、jinfo等,并且jdk添加新的诊断功能,也会通过jcmd提供