path | title |
---|---|
/learnings/ops_java |
Learnings: Ops: Java |
How / what the JVM uses to allow itself to be diagnosed:
-
binary files dumped to tmp dirs
-
JVM reacts to QUIT signal and looks in CWD for
.attach_pid$PID
. If exists will create a thread that: a) creates a UNIX domain socket in the CWD called.jaa_pid$PID
b) listens to commands on that socket^^^^^ How jattach works !!! (jmap, jstack, jcmd work)!!!
-
JVMTI: allows an external .so to be loaded, attached and register for interesting events. ^^^^ This is how
agentpath
command line arguments work!!!
See also:
- DZone: Java Zone: JVM Diagnostic Tools and Containers <-- this is actually written by someone on the Israelly Azure team at Microsoft
- Learning_Ops_Java_Docker
"Hmmm, I should get an alert when my Java process is taking up too much RAM / growing out of control"
How to take a thread dump from a JVM ^^^^ this includes a script that will take N heapdumps, waiting M seconds between dumps
uses jattach under covers
Can get statistics on old, new generation of GC, gcutil
See also:
Java memory map printer
uses jattach under covers
See also:
- Oracle: jMap
- A Test Developer's Blog: jmap
- Interpretation of jstat heap memory to debug suspected java memory leak
<<Learning_Ops_SRE_Java_Memory_Leak_Debugging>>
See also:
- 8 Options for Capturing thread dump data
- How to analyze a thread dump
- https://spring.io/blog/2015/12/10/spring-boot-memory-performance <-- using these tools to see memory etc of a Spring Boot application in action
Need to explicitly set heap size, memory because Java 8 can't understand limits placed upon it by Docker schedulers, Docker, cgroups, or CPU constraints.
See also:
- Deploying JVM in tiny containers
- Running Java in a Container - Mesosphere <-- goes into cgroups stuff, etc
Introspection / Debugging <<Learning_Ops_SRE_Java_Debugging>> . Or: looking for signs of epidemic in your herd of cattle
"WHY is my Java app leaking memory / needing to be restarted every 4 hours?"
Oracle: Deep Monitoring with JMX intro article on this
Problems:
- need to mount directories for heapdumps, and other binary files dumped to /tmp by the JVM diagnostic tools
- JVM attach interface communicates and passes around PID (recent versions of this account for cgroups namespaces)
- Serviability API requires EXACT match between JDK/JRE version on the container and the host (this is hard to get exact)
-
Firewalls / opening broad RMI range of ports
-
Multiple Java processes on same OS could port conflict their JMX / RMI ports
-
JMX also sends hostname back to client ????????
-
If really in Docker containers than:
- port mappings host -> container problems
- Container may not know hostname
-
If Docker containers managed via a scheduler:
-
how to get at a specific instance of that container - see also:
Pets_Not_Cattle_Service_Debugging_Questions_Strategy
Set following -D options (must be -D options, can not be Spring application properties):
-Dcom.sun.management.jmxremote.rmi.port=1234
-Dcom.sun.management.jmxremote.port=23454
May also need to set:
-Djava.rmi.server.hostname=<your public hostname>
-Dcom.sun.management.jmxremote.authenticate=false
Blog article on how RMI hostname is determined by default
"high dynamic range": or "long tail distributions": where yes the average is Nms, but 5% of consumers had a response time at ( (8*N)ms ) or some other waaaaayyyy outside the range metric.
Maven Coordinates:
groupId : org.hdrhistogram artifact : HdrHistogram
TL;DR: separate out distribution of data into buckets, display those buckets + their values.
- Learning_Java_Memory_Performace_Debuggin_Pause_Time
- -XX:+PrintCompilation — prints information about what HotSpot is JIT-ing
- -XX:+LogCompilation with -XX:+UnlockDiagnosticVMOptions (will generate 100s of MB of XML, use JITWatch)
JAVA 9 intros new unified JVM internals log format
New -Xlog:THING
gives several abilities:
- namespacing only select topics or all
Xlog:THING,THING+subthing
orXlog:THING*
<-- for all - destination (
java -Xlog:THING:stdout
), can also send to stderr or a specific file - field format
Command format: -Xlog:SELECTOR:OUTPUT:DECORATORS:OUTPUT-OPTIONS
See avail tags, etc: java -Xlog:help
- Deoptimization
- Thread dump
- Heap inspection
- Class Redefinition
- ... more
- Source: https://stackoverflow.com/a/29673564/224334 , http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/fc3cd1db10e2/src/share/vm/runtime/vm_operations.hpp#l39
To do STW operations requires threads to be at safe point.
Activities requiring a Safepoint:
- creating heap dump
- method deoptimization
- revoking biased lock
- class redefinition
BUT Safepoints happen when:
- method return
- loop back branch
Loop unrolling etc may mean Safepoints are further away than normal! - source: Optimized Java
You can print information about mean time to Safepoint.
See also:
- Learning_Java_Operational_Information_Flags
JDK 8 and earlier add -XX:+PrintGCApplicationStoppedTime JVM option; starting from JDK 9 add -Xlog:safepoint.
Can use thread dumps to get information about all running threads.
Will contain name, priority, ID, status and current callstack.
- A single thread:
Thread t = new Thread("myName!")
- In ThreadPools / ExecutorServices: can specify own
ThreadFactory
. See: the simple implementation and the more complex one that auto increments thread numbers - In
Runnable
s: just grab the name ofThread.currentThread()
, stash it somewhere, then use a getter in some monitoring code
- https://www.javaworld.com/article/2074769/core-java/detecting-java-threads-in-deadlock-with-groovy-and-jmx.html
- https://dzone.com/articles/how-analyze-java-thread-dumps
- Optimizing Java !!!