SlideShare a Scribd company logo
Advanced Production Debugging
About Me
Co-founder – Takipi, JVM Production Debugging.
Director, AutoCAD Web & Mobile.
Software Architect at IAI Aerospace.
Coding for the past 16 years - C++, Delphi, .NET, Java.
Focus on real-time, scalable systems.
Blogs at blog.takipi.com
Overview
Dev-stage debugging is forward-tracing.
Production debugging is focused on backtracing.
Modern production debugging poses two challenges:
• State isolation.
• Data distribution.
Agenda
1. Logging at scale.
2. Preemptive jstacks
1. Extracting state with Btrace
1. Extracting state with custom Java agents.
Best Logging Practices
1.  Code context.
2. Time + duration.
3. Transactional data (for async & distributed debugging).
A primary new consumer is a log analyzer. Context trumps content.
Transactional IDs
• Modern logging is done over a multi–threads / processes.

• Generate a UUID at every thread entry point into your app – the transaction ID.
• Append the ID into each log entry.

• Try to maintain it across machines – critical for debugging Reactive and microservice
apps.
[20-07 07:32:51][BRT -1473 -S4247] ERROR - Unable to retrieve data for
Job J141531. {CodeAnalysisUtil TID: Uu7XoelHfCTUUlvol6d2a9pU}
[SQS-prod_taskforce1_BRT-Executor-1-thread-2]
1. Don’t catch exceptions within loops and log them (implicit and explicit).
For long running loops this will flood the log, impede performance and bring a server down.
void readData {
while (hasNext()) {
try {
readData();
}
catch {Exception e) {
logger.errror(“error reading “ X + “ from “ Y, e);
}
}
2. Do not log Object.toString(), especially collections.
Can create an implicit loop. If needed – make sure length is limited.
Logging Performance
Thread Names
• Thread name is a mutable property.
• Can be set to hold transaction specific state.
• Some frameworks (e.g. EJB) don’t like that.
• Can be super helpful when debugging in tandem with jstack.
Thread Names (2)
For example:

Thread.currentThread().setName(

Context + TID + Params + current Time, ...);
Before:

“pool-1-thread-1″ #17 prio=5 os_prio=31
tid=0x00007f9d620c9800 nid=0x6d03 in Object.wait()
[0x000000013ebcc000
After:
”Queue Processing Thread, MessageID: AB5CAD, type:
AnalyzeGraph, queue: ACTIVE_PROD, Transaction_ID: 5678956,
Start Time: 10/8/2014 18:34″ #17 prio=5 os_prio=31
tid=0x00007f9d620c9800 nid=0x6d03 in Object.wait()
[0x000000013ebcc000]
Advanced Production Debugging
Modern Stacks - Java 8
Modern Stacks - Scala
Advanced Production Debugging
Preemptive jstack
github.com/takipi/jstack
Preemptive jstack
• A production debugging foundation.
• Presents two issues –
– Activated only in retrospect.
– No state: does not provide any variable state.
• Let’s see how we can overcome these with preemptive jstacks.
Advanced Production Debugging
Advanced Production Debugging
”MsgID: AB5CAD, type: Analyze, queue: ACTIVE_PROD, TID:
5678956, TS: 11/8/20014 18:34 "
#17 prio=5 os_prio=31 tid=0x00007f9d620c9800 nid=0x6d03
in Object.wait() [0x000000013ebcc000]
Jstack Triggers
• A queue exceeds capacity.
• Throughput exceeds or drops below a threshold.
• CPU usage passes a threshold.
• Locking failures / Deadlock.
Integrate as a first class citizen with your logging infrastructure.
Advanced Production Debugging
BTrace
• An advanced open-source tool for extracting state from a live JVM.
• Uses a Java agent and a meta-scripting language to capture state.
• Pros: Lets you probe variable state without modifying / restarting the JVM.
• Cons: read-only querying using a custom syntax and libraries.
Usage
• No JVM restart needed. Works remotely.
• btrace [-I <include-path>] [-p <port>] [-cp <classpath>] <pid> <btrace-script> [<args>]
• Example: Btrace 9550 myScript.java
• Available at: kenai.com/projects/btrace
BTrace - Restrictions
• Can not create new objects.
• Can not create new arrays.
• Can not throw exceptions.
• Can not catch exceptions.
• Can not make arbitrary instance or static method calls - only the public static
methods of com.sun.btrace.BTraceUtils class may be called from a BTrace
program.
• Can not assign to static or instance fields of target program's classes and
objects. But, BTrace class can assign to it's own static fields ("trace state" can be
mutated).
• Can not have instance fields and methods. Only static public void returning
methods are allowed for a BTrace class. And all fields have to be static.
• Can not have outer, inner, nested or local classes.
• Can not have synchronized blocks or synchronized methods.
• can not have loops (for, while, do..while)
• Can not extend arbitrary class (super class has to be java.lang.Object)
• Can not implement interfaces.
• Can not contains assert statements.
Advanced Production Debugging
Advanced Production Debugging
Advanced Production Debugging
Java Agents
• An advanced technique for instrumenting code dynamically.
• The foundation of modern profiling / debugging tools.
• Two types of agents: Java and Native.
• Pros: extremely powerful technique to collect state from a live app.
• Cons: requires knowledge of creating verifiable bytecode.
Agent Types
• Java agents are written in Java. Have access to the Instrumentation BCI API.
• Native agents – written in C++.
• Have access to JVMTI – the JVM’s low-level set of APIs and capabilities.
– JIT compilation, Garbage Collection, Monitor acquisition, Exception callbacks, ..
• More complex to write.
• Platform dependent.
Java Agents
github.com/takipi/debugAgent
com.sun.tools.attach.VirtualMachine
Attach at startup: java -Xmx2G -agentlib:myAgent -jar myapp.jar
start
To a live JVM using: com.sun.tools.attach.VirtualMachine Attach API.
Advanced Production Debugging
Advanced Production Debugging
Advanced Production Debugging
ASMifying
ASM Bytecode Outline plug-in
takipi.com
blog.takipi.com
Questions?

More Related Content

What's hot (20)

PDF
Scalax
Martin Odersky
 
ODP
Introduction to Java 8
Knoldus Inc.
 
PDF
Debugging Your Production JVM
kensipe
 
PDF
Java SE 8 library design
Stephen Colebourne
 
PDF
Java collections the force awakens
RichardWarburton
 
PDF
Introduction to Scala
Saleem Ansari
 
PDF
Java 8 features
Oleg Tsal-Tsalko
 
PPTX
55 New Features in Java SE 8
Simon Ritter
 
PPTX
Java SE 8 - New Features
Naveen Hegde
 
PDF
Scala : language of the future
AnsviaLab
 
PDF
Build Cloud Applications with Akka and Heroku
Salesforce Developers
 
PDF
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
PPTX
Introduction to Scala
Mohammad Hossein Rimaz
 
PDF
Java 8 - Project Lambda
Rahman USTA
 
PPTX
Java 201 Intro to Test Driven Development in Java
agorolabs
 
PDF
Collections forceawakens
RichardWarburton
 
PDF
Generics Past, Present and Future (Latest)
RichardWarburton
 
PPTX
Java 8 Features
Trung Nguyen
 
PDF
The Evolution of Scala / Scala進化論
scalaconfjp
 
Introduction to Java 8
Knoldus Inc.
 
Debugging Your Production JVM
kensipe
 
Java SE 8 library design
Stephen Colebourne
 
Java collections the force awakens
RichardWarburton
 
Introduction to Scala
Saleem Ansari
 
Java 8 features
Oleg Tsal-Tsalko
 
55 New Features in Java SE 8
Simon Ritter
 
Java SE 8 - New Features
Naveen Hegde
 
Scala : language of the future
AnsviaLab
 
Build Cloud Applications with Akka and Heroku
Salesforce Developers
 
A Brief, but Dense, Intro to Scala
Derek Chen-Becker
 
Introduction to Scala
Mohammad Hossein Rimaz
 
Java 8 - Project Lambda
Rahman USTA
 
Java 201 Intro to Test Driven Development in Java
agorolabs
 
Collections forceawakens
RichardWarburton
 
Generics Past, Present and Future (Latest)
RichardWarburton
 
Java 8 Features
Trung Nguyen
 
The Evolution of Scala / Scala進化論
scalaconfjp
 

Viewers also liked (6)

PDF
In Search of Segmentation
Adrian Cockcroft
 
PDF
10 SQL Tricks that You Didn't Think Were Possible
Lukas Eder
 
PDF
Project Jigsaw in JDK 9: Modularity Comes To Java
C4Media
 
PDF
Scala Days NYC 2016
Martin Odersky
 
PDF
Java SE 8 best practices
Stephen Colebourne
 
PPTX
Microservices + Oracle: A Bright Future
Kelly Goetsch
 
In Search of Segmentation
Adrian Cockcroft
 
10 SQL Tricks that You Didn't Think Were Possible
Lukas Eder
 
Project Jigsaw in JDK 9: Modularity Comes To Java
C4Media
 
Scala Days NYC 2016
Martin Odersky
 
Java SE 8 best practices
Stephen Colebourne
 
Microservices + Oracle: A Bright Future
Kelly Goetsch
 
Ad

Similar to Advanced Production Debugging (20)

PDF
Surge2012
davidapacheco
 
PDF
Java 23 and Beyond - A Roadmap Of Innovations
Ana-Maria Mihalceanu
 
PPTX
Byte code manipulation and instrumentalization in Java
Alex Moskvin
 
PDF
Rakuten openstack
Rakuten Group, Inc.
 
PPT
Bag it Tag It Put it : Project Tracking One Click away
Abhishek Bakshi
 
PDF
JDK Tools For Performance Diagnostics
Baruch Sadogursky
 
PDF
The Diabolical Developers Guide to Performance Tuning
jClarity
 
PPTX
Hadoop cluster performance profiler
Ihor Bobak
 
PDF
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
JiandSon
 
PPTX
Loom promises: be there!
Jean-Francois James
 
PDF
Exciting Features and Enhancements in Java 23 and 24
Ana-Maria Mihalceanu
 
PPTX
Typesafe spark- Zalando meetup
Stavros Kontopoulos
 
PDF
Understand the Trade-offs Using Compilers for Java Applications
C4Media
 
PPTX
Spring batch for large enterprises operations
Ignasi González
 
PPTX
DIY Java Profiling
Roman Elizarov
 
PDF
Build Java Web Application Using Apache Struts
weili_at_slideshare
 
PDF
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JSFestUA
 
PPTX
Nguyen Phuong Truong Anh - Some new vulnerabilities in modern web application
Security Bootcamp
 
PPT
JavaOne_2010
Tadaya Tsuyukubo
 
PDF
Java 8 selected updates
Vinay H G
 
Surge2012
davidapacheco
 
Java 23 and Beyond - A Roadmap Of Innovations
Ana-Maria Mihalceanu
 
Byte code manipulation and instrumentalization in Java
Alex Moskvin
 
Rakuten openstack
Rakuten Group, Inc.
 
Bag it Tag It Put it : Project Tracking One Click away
Abhishek Bakshi
 
JDK Tools For Performance Diagnostics
Baruch Sadogursky
 
The Diabolical Developers Guide to Performance Tuning
jClarity
 
Hadoop cluster performance profiler
Ihor Bobak
 
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
JiandSon
 
Loom promises: be there!
Jean-Francois James
 
Exciting Features and Enhancements in Java 23 and 24
Ana-Maria Mihalceanu
 
Typesafe spark- Zalando meetup
Stavros Kontopoulos
 
Understand the Trade-offs Using Compilers for Java Applications
C4Media
 
Spring batch for large enterprises operations
Ignasi González
 
DIY Java Profiling
Roman Elizarov
 
Build Java Web Application Using Apache Struts
weili_at_slideshare
 
JS Fest 2018. Никита Галкин. Микросервисная архитектура с переиспользуемыми к...
JSFestUA
 
Nguyen Phuong Truong Anh - Some new vulnerabilities in modern web application
Security Bootcamp
 
JavaOne_2010
Tadaya Tsuyukubo
 
Java 8 selected updates
Vinay H G
 
Ad

More from Takipi (7)

PPTX
AppDynamics VS New Relic – The Complete Guide
Takipi
 
PPTX
The Dark Side Of Lambda Expressions in Java 8
Takipi
 
PDF
Java 9 – The Ultimate Feature List
Takipi
 
PDF
7 New Tools Java Developers Should Know
Takipi
 
PDF
5 Coding Hacks to Reduce GC Overhead
Takipi
 
PDF
JVM Performance Magic Tricks
Takipi
 
PPTX
JVM bytecode - The secret language behind Java and Scala
Takipi
 
AppDynamics VS New Relic – The Complete Guide
Takipi
 
The Dark Side Of Lambda Expressions in Java 8
Takipi
 
Java 9 – The Ultimate Feature List
Takipi
 
7 New Tools Java Developers Should Know
Takipi
 
5 Coding Hacks to Reduce GC Overhead
Takipi
 
JVM Performance Magic Tricks
Takipi
 
JVM bytecode - The secret language behind Java and Scala
Takipi
 

Recently uploaded (20)

PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PPTX
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Final Major project a b c d e f g h i j k l m
bharathpsnab
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
MODULE 04 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 

Advanced Production Debugging

  • 2. About Me Co-founder – Takipi, JVM Production Debugging. Director, AutoCAD Web & Mobile. Software Architect at IAI Aerospace. Coding for the past 16 years - C++, Delphi, .NET, Java. Focus on real-time, scalable systems. Blogs at blog.takipi.com
  • 3. Overview Dev-stage debugging is forward-tracing. Production debugging is focused on backtracing. Modern production debugging poses two challenges: • State isolation. • Data distribution.
  • 4. Agenda 1. Logging at scale. 2. Preemptive jstacks 1. Extracting state with Btrace 1. Extracting state with custom Java agents.
  • 5. Best Logging Practices 1.  Code context. 2. Time + duration. 3. Transactional data (for async & distributed debugging). A primary new consumer is a log analyzer. Context trumps content.
  • 6. Transactional IDs • Modern logging is done over a multi–threads / processes.
 • Generate a UUID at every thread entry point into your app – the transaction ID. • Append the ID into each log entry.
 • Try to maintain it across machines – critical for debugging Reactive and microservice apps. [20-07 07:32:51][BRT -1473 -S4247] ERROR - Unable to retrieve data for Job J141531. {CodeAnalysisUtil TID: Uu7XoelHfCTUUlvol6d2a9pU} [SQS-prod_taskforce1_BRT-Executor-1-thread-2]
  • 7. 1. Don’t catch exceptions within loops and log them (implicit and explicit). For long running loops this will flood the log, impede performance and bring a server down. void readData { while (hasNext()) { try { readData(); } catch {Exception e) { logger.errror(“error reading “ X + “ from “ Y, e); } } 2. Do not log Object.toString(), especially collections. Can create an implicit loop. If needed – make sure length is limited. Logging Performance
  • 8. Thread Names • Thread name is a mutable property. • Can be set to hold transaction specific state. • Some frameworks (e.g. EJB) don’t like that. • Can be super helpful when debugging in tandem with jstack.
  • 9. Thread Names (2) For example:
 Thread.currentThread().setName(
 Context + TID + Params + current Time, ...); Before:
 “pool-1-thread-1″ #17 prio=5 os_prio=31 tid=0x00007f9d620c9800 nid=0x6d03 in Object.wait() [0x000000013ebcc000 After: ”Queue Processing Thread, MessageID: AB5CAD, type: AnalyzeGraph, queue: ACTIVE_PROD, Transaction_ID: 5678956, Start Time: 10/8/2014 18:34″ #17 prio=5 os_prio=31 tid=0x00007f9d620c9800 nid=0x6d03 in Object.wait() [0x000000013ebcc000]
  • 11. Modern Stacks - Java 8
  • 15. Preemptive jstack • A production debugging foundation. • Presents two issues – – Activated only in retrospect. – No state: does not provide any variable state. • Let’s see how we can overcome these with preemptive jstacks.
  • 18. ”MsgID: AB5CAD, type: Analyze, queue: ACTIVE_PROD, TID: 5678956, TS: 11/8/20014 18:34 " #17 prio=5 os_prio=31 tid=0x00007f9d620c9800 nid=0x6d03 in Object.wait() [0x000000013ebcc000]
  • 19. Jstack Triggers • A queue exceeds capacity. • Throughput exceeds or drops below a threshold. • CPU usage passes a threshold. • Locking failures / Deadlock. Integrate as a first class citizen with your logging infrastructure.
  • 21. BTrace • An advanced open-source tool for extracting state from a live JVM. • Uses a Java agent and a meta-scripting language to capture state. • Pros: Lets you probe variable state without modifying / restarting the JVM. • Cons: read-only querying using a custom syntax and libraries.
  • 22. Usage • No JVM restart needed. Works remotely. • btrace [-I <include-path>] [-p <port>] [-cp <classpath>] <pid> <btrace-script> [<args>] • Example: Btrace 9550 myScript.java • Available at: kenai.com/projects/btrace
  • 23. BTrace - Restrictions • Can not create new objects. • Can not create new arrays. • Can not throw exceptions. • Can not catch exceptions. • Can not make arbitrary instance or static method calls - only the public static methods of com.sun.btrace.BTraceUtils class may be called from a BTrace program. • Can not assign to static or instance fields of target program's classes and objects. But, BTrace class can assign to it's own static fields ("trace state" can be mutated). • Can not have instance fields and methods. Only static public void returning methods are allowed for a BTrace class. And all fields have to be static. • Can not have outer, inner, nested or local classes. • Can not have synchronized blocks or synchronized methods. • can not have loops (for, while, do..while) • Can not extend arbitrary class (super class has to be java.lang.Object) • Can not implement interfaces. • Can not contains assert statements.
  • 27. Java Agents • An advanced technique for instrumenting code dynamically. • The foundation of modern profiling / debugging tools. • Two types of agents: Java and Native. • Pros: extremely powerful technique to collect state from a live app. • Cons: requires knowledge of creating verifiable bytecode.
  • 28. Agent Types • Java agents are written in Java. Have access to the Instrumentation BCI API. • Native agents – written in C++. • Have access to JVMTI – the JVM’s low-level set of APIs and capabilities. – JIT compilation, Garbage Collection, Monitor acquisition, Exception callbacks, .. • More complex to write. • Platform dependent.
  • 30. com.sun.tools.attach.VirtualMachine Attach at startup: java -Xmx2G -agentlib:myAgent -jar myapp.jar start To a live JVM using: com.sun.tools.attach.VirtualMachine Attach API.