SlideShare ist ein Scribd-Unternehmen logo
1 von 44
8th Central and Eastern European
Software Engineering Conference
in Russia - CEE-SECR 2012
November 1 - 2, Moscow



     Dynamic data race detection in
       concurrent Java programs

                  Vitaly Trifanov, Dmitry Tsitelov
                           Devexperts LLC
Agenda
What are data races and why they are dangerous


Automatic races detection
  approaches, pros & cons


Happens-before race detection algorithm
  Vector clocks


Our dynamic race detector
  implementation
  solved problems
Data Race Example
public class Account {
    private int amount = 0;
    public void deposit(int x) {amount += x;}
    public int getAmount() {return amount;}
}


public class TestRace {
        public static void main (String[] args) {
         final Account a = new Account();
         Thread t1 = depositAccountInNewThread(a, 5);
         Thread t2 = depositAccountInNewThread(a, 6);
         t1.join();
         t2.join();
         System.out.println(account.getAmount()); //may print 5, 6, 11.
    }
}
Expected Execution
Racy Execution
Data Races

Data race occurs when many threads access the same
  shared data concurrently; at least one writes




Usually it’s a bug
Data Races Are Dangerous

Hard to detect if occurred
  no immediate effects
  program continues to work
  damage global data structures


Hard to find manually
  Not reproducible - depends on threads timing
  Dev & QA platforms are not so multicore
Automatic Race Detection
20+ years of research


Static
  analyze program code offline
  data races prevention (extend type system, annotations)


Dynamic: analyze real program executions
  On-the-fly
  Post-mortem
Dynamic Detectors vs Static
Static Approach
Pros
  Doesn’t require program execution
  Analyzes all code
  Doesn’t depend on program input, environment, etc.


Cons
  Unsolvable in common case
  Has to reduce depth of analysis


A lot of existing tools for Java
  FindBugs, jChord, etc
Dynamic Approach
Pros
  Complete information about program flow
  Lower level of false alarms


Cons
  Very large overhead


No existing stable dynamic detectors for Java
Static vs Dynamic: What To Do?
Use both approaches 


Static (FindBugs/Sonar, jChord, …)
  Eliminate provable synchronization inconsistencies
   on the early stage

Dynamic
  Try existing tools, but they are unstable
     IBM MSDK, Thread Sanitizer for Java
  That’s why we’ve developed our own!
Data Race Detector Concept
Application uses libraries and frameworks via API
  At least JRE


API is well documented
  “Class XXX is thread-safe”
  “Class YYY is not thread-safe”
  “XXX.get() is synchronized with preceding call of XXX.set()”


Describe behavior of API and exclude library from
 analysis
DRD: How It’s Organized
What Operations to Intercept?

Synchronization operations
  thread start/join/interrupt
  synchronized
  volatile read/write
  java.util.concurrent


Accesses to shared data
  fields
  objects
How It Works


                             Instrumented
Application
                 DRD agent    app classes
 classes
                              interceptor




                                Race
                  Config      detection
                               module
JLS: Publishing Data



  Publish changes




                    Receive changes
JLS: Synchronized-With Relation

“Synchronized-with” relation
  unlock monitor M ↦ all subsequent locks on M
  volatile write ↦ all subsequent volatile reads
  …




Notation: send ↦ receive
JLS: Happens-Before & Data Races

X happens-before Y, when
  X, Y - in same thread, X before Y in program order
  X is synchronized-with Y
  Transitivity: exists Z: hb(X, Z) && hb(Z, Y)


Data race: 2 conflicting accesses, not ordered by
  happens-before relation
Happens-Before Example




No data race
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock


        Not ordered!
          A: 3 > 2
          B: 3 < 4
How It Works. No Data Race Example
How It Works. Data Race Example
Code Instrumentation
Check everything => huge overhead


Race detection scope
  Accesses to our fields
  Foreign calls (treat them as read or write)


Sync scope
  Detect sync events in our code
  Describe contracts of excluded classes
  Treat these contracts as synchronization events
Race Detection
private class Storage {
   private Map<Integer, Item> items = new HashMap<Integer, Item> ();

    public void store(Item item) {
        items.put(item.getId(), item);
    }

    public void saveToDisk() {                 On each access of “items” field we check
                                               On each access of “items” field we check
        for (Item item : items.values()) {
               //serialize and save                      race on this field
                                                          race on this field
               saveItem(item);
               //...
        }
    }
                                               On each call of “items” method we check
                                               On each call of “items” method we check
    public Item getItem(int id) {                       race on this object
                                                         race on this object
        return items.get(id);
    }

    public void reload() {
       items = deserealizeFromFile();            Each field of class Item is protected the
                                                 Each field of class Item is protected the
    }                                          same way as field “items” of class Storage
                                                same way as field “items” of class Storage
}
Synchronization Contract Example
Clocks Storing
Thread clock
  ThreadLocal<VectorClock >


Field XXX
  volatile transient VectorClock XXX_vc;


Foreign objects, monitors
   WeakIdentityConcurrentHashMap<Object,VectorClock>


 Volatiles, synchronization contracts
   ConcurrentHashMap <???, VectorClock>
Composite Keys
 AtomicLongFieldUpdater.CAS(Object o, long offset, long v)
   param 0 + param 1


 Volatile field “abc” of object o
   object + field name


 AtomicInteger.set() & AtomicInteger.get()
   object


 ConcurrentMap.put(key, value) & ConcurrentMap.get(key)
   object + param 0
Solved Problems
Composite keys for contracts and volatiles
  Generate them on-the-fly


Avoid unnecessary keys creation
  ThreadLocal<MutableKeyXXX> for each CompositeKeyXXX


Loading of classes, generated on-the-fly
  Instrument ClassLoader.loadClass()
Solved Problems
Don’t break serialization
  compute serialVersiodUid before instrumentation


Caching components of dead clocks
  when thread dies, its time frames doesn’t grow anymore
  cache frames of dead threads to avoid memory leaks
  local last-known generation & global generation
DRD in Real Life: QD


                ✔ 6 races found


  QD




QD + DRD
DRD in Real Life: MARS UI


  MARS

                 ✔ 5 races found




MARS + DRD
DRD Race Report Example
WRITE_READ data race between current thread Thread-12(id = 33) and thread
  Thread-11(id = 32)


Race target : field my/app/DataServiceImpl.stopped


Thread 32 accessed it in my/app/DataServiceImpl.access$400(line : 29)


--------------------------------Stack trace for racing thread (id = 32) is not available.------------


-------------------------------------Current thread's stack trace (id = 33) : ---------------------------
   at my.app.DataServiceImpl.stop(DataServiceImpl.java:155)
   at my.app.DataManager.close(DataManager.java:201)
   ...
DRD Advantages
Doesn’t break serialization


No memory leaks


Few garbage


No JVM modification


Synchronization contracts
  very important: Unsafe, AbstractQueuedSynchronizer
Links
http://code.devexperts.com/display/DRD/:
 documentation, links, etc
Contact us: drd-support@devexperts.com


Useful links: see also on product page
  IBM MSDK
  ThreadSanitizer for Java
  jChord
  FindBugs


  JLS «Threads and locks» chapter
Q&A

Weitere ähnliche Inhalte

Was ist angesagt?

Java I/O and Object Serialization
Java I/O and Object SerializationJava I/O and Object Serialization
Java I/O and Object SerializationNavneet Prakash
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESNikunj Parekh
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NETAbhi Arya
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesAndrey Karpov
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java conceptsChikugehlot
 
Let's talk about jni
Let's talk about jniLet's talk about jni
Let's talk about jniYongqiang Li
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructorHaresh Jaiswal
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...GeeksLab Odessa
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Marcos García
 
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
A Runtime Monitoring Framework for Event Streams with Non-Primitive ArgumentsA Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
A Runtime Monitoring Framework for Event Streams with Non-Primitive ArgumentsSylvain Hallé
 
Procedure Typing for Scala
Procedure Typing for ScalaProcedure Typing for Scala
Procedure Typing for Scalaakuklev
 

Was ist angesagt? (20)

Java I/O and Object Serialization
Java I/O and Object SerializationJava I/O and Object Serialization
Java I/O and Object Serialization
 
Csharp_Chap03
Csharp_Chap03Csharp_Chap03
Csharp_Chap03
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Kotlin
KotlinKotlin
Kotlin
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Jist of Java
Jist of JavaJist of Java
Jist of Java
 
JAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICESJAVA CONCEPTS AND PRACTICES
JAVA CONCEPTS AND PRACTICES
 
Serialization in .NET
Serialization in .NETSerialization in .NET
Serialization in .NET
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
 
Core java concepts
Core    java  conceptsCore    java  concepts
Core java concepts
 
Let's talk about jni
Let's talk about jniLet's talk about jni
Let's talk about jni
 
04. constructor & destructor
04. constructor & destructor04. constructor & destructor
04. constructor & destructor
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
 
Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)Quick introduction to Java Garbage Collector (JVM GC)
Quick introduction to Java Garbage Collector (JVM GC)
 
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
A Runtime Monitoring Framework for Event Streams with Non-Primitive ArgumentsA Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
 
Collections
CollectionsCollections
Collections
 
Scala test
Scala testScala test
Scala test
 
Procedure Typing for Scala
Procedure Typing for ScalaProcedure Typing for Scala
Procedure Typing for Scala
 

Andere mochten auch

Browsers. Magic is inside.
Browsers. Magic is inside.Browsers. Magic is inside.
Browsers. Magic is inside.Devexperts
 
Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...Devexperts
 
20130420 bitbyte
20130420 bitbyte20130420 bitbyte
20130420 bitbyteDevexperts
 
Effective websites development
Effective websites developmentEffective websites development
Effective websites developmentDevexperts
 
How to improve java performance
How to improve java performanceHow to improve java performance
How to improve java performanceDevexperts
 
Codefreeze rus
Codefreeze rusCodefreeze rus
Codefreeze rusDevexperts
 
Codefreeze eng
Codefreeze engCodefreeze eng
Codefreeze engDevexperts
 
Dynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programsDynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programsDevexperts
 
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems ReviewRoman Elizarov
 

Andere mochten auch (9)

Browsers. Magic is inside.
Browsers. Magic is inside.Browsers. Magic is inside.
Browsers. Magic is inside.
 
Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...
 
20130420 bitbyte
20130420 bitbyte20130420 bitbyte
20130420 bitbyte
 
Effective websites development
Effective websites developmentEffective websites development
Effective websites development
 
How to improve java performance
How to improve java performanceHow to improve java performance
How to improve java performance
 
Codefreeze rus
Codefreeze rusCodefreeze rus
Codefreeze rus
 
Codefreeze eng
Codefreeze engCodefreeze eng
Codefreeze eng
 
Dynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programsDynamic data race detection in concurrent Java programs
Dynamic data race detection in concurrent Java programs
 
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems ReviewACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
 

Ähnlich wie Drd secr final1_3

Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionAlex Su
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerAlex Matrosov
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindAndreas Czakaj
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidIntel® Software
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxpetabridge
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugketan_patel25
 
Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft
 
Automated Discovery of Deserialization Gadget Chains
 Automated Discovery of Deserialization Gadget Chains Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsPriyanka Aash
 
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
SNMP Project: SNMP-based Network Anomaly Detection Using ClusteringSNMP Project: SNMP-based Network Anomaly Detection Using Clustering
SNMP Project: SNMP-based Network Anomaly Detection Using ClusteringLaili Aidi
 
findbugs Bernhard Merkle
findbugs Bernhard Merklefindbugs Bernhard Merkle
findbugs Bernhard Merklebmerkle
 

Ähnlich wie Drd secr final1_3 (20)

Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
.NET Reflection
.NET Reflection.NET Reflection
.NET Reflection
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorer
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mind
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
JavaSecure
JavaSecureJavaSecure
JavaSecure
 
04 Data Access
04 Data Access04 Data Access
04 Data Access
 
Java14
Java14Java14
Java14
 
Java
JavaJava
Java
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in Android
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptx
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
 
Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015
 
Automated Discovery of Deserialization Gadget Chains
 Automated Discovery of Deserialization Gadget Chains Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget Chains
 
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
SNMP Project: SNMP-based Network Anomaly Detection Using ClusteringSNMP Project: SNMP-based Network Anomaly Detection Using Clustering
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
 
Reversing JavaScript
Reversing JavaScriptReversing JavaScript
Reversing JavaScript
 
findbugs Bernhard Merkle
findbugs Bernhard Merklefindbugs Bernhard Merkle
findbugs Bernhard Merkle
 

Drd secr final1_3

  • 1. 8th Central and Eastern European Software Engineering Conference in Russia - CEE-SECR 2012 November 1 - 2, Moscow Dynamic data race detection in concurrent Java programs Vitaly Trifanov, Dmitry Tsitelov Devexperts LLC
  • 2. Agenda What are data races and why they are dangerous Automatic races detection approaches, pros & cons Happens-before race detection algorithm Vector clocks Our dynamic race detector implementation solved problems
  • 3. Data Race Example public class Account { private int amount = 0; public void deposit(int x) {amount += x;} public int getAmount() {return amount;} } public class TestRace { public static void main (String[] args) { final Account a = new Account(); Thread t1 = depositAccountInNewThread(a, 5); Thread t2 = depositAccountInNewThread(a, 6); t1.join(); t2.join(); System.out.println(account.getAmount()); //may print 5, 6, 11. } }
  • 6. Data Races Data race occurs when many threads access the same shared data concurrently; at least one writes Usually it’s a bug
  • 7. Data Races Are Dangerous Hard to detect if occurred no immediate effects program continues to work damage global data structures Hard to find manually Not reproducible - depends on threads timing Dev & QA platforms are not so multicore
  • 8. Automatic Race Detection 20+ years of research Static analyze program code offline data races prevention (extend type system, annotations) Dynamic: analyze real program executions On-the-fly Post-mortem
  • 10. Static Approach Pros Doesn’t require program execution Analyzes all code Doesn’t depend on program input, environment, etc. Cons Unsolvable in common case Has to reduce depth of analysis A lot of existing tools for Java FindBugs, jChord, etc
  • 11. Dynamic Approach Pros Complete information about program flow Lower level of false alarms Cons Very large overhead No existing stable dynamic detectors for Java
  • 12. Static vs Dynamic: What To Do? Use both approaches  Static (FindBugs/Sonar, jChord, …) Eliminate provable synchronization inconsistencies on the early stage Dynamic Try existing tools, but they are unstable  IBM MSDK, Thread Sanitizer for Java That’s why we’ve developed our own!
  • 13. Data Race Detector Concept Application uses libraries and frameworks via API At least JRE API is well documented “Class XXX is thread-safe” “Class YYY is not thread-safe” “XXX.get() is synchronized with preceding call of XXX.set()” Describe behavior of API and exclude library from analysis
  • 14. DRD: How It’s Organized
  • 15. What Operations to Intercept? Synchronization operations thread start/join/interrupt synchronized volatile read/write java.util.concurrent Accesses to shared data fields objects
  • 16. How It Works Instrumented Application DRD agent app classes classes interceptor Race Config detection module
  • 17. JLS: Publishing Data Publish changes Receive changes
  • 18. JLS: Synchronized-With Relation “Synchronized-with” relation unlock monitor M ↦ all subsequent locks on M volatile write ↦ all subsequent volatile reads … Notation: send ↦ receive
  • 19. JLS: Happens-Before & Data Races X happens-before Y, when X, Y - in same thread, X before Y in program order X is synchronized-with Y Transitivity: exists Z: hb(X, Z) && hb(Z, Y) Data race: 2 conflicting accesses, not ordered by happens-before relation
  • 29. Vector Clock Not ordered! A: 3 > 2 B: 3 < 4
  • 30. How It Works. No Data Race Example
  • 31. How It Works. Data Race Example
  • 32. Code Instrumentation Check everything => huge overhead Race detection scope Accesses to our fields Foreign calls (treat them as read or write) Sync scope Detect sync events in our code Describe contracts of excluded classes Treat these contracts as synchronization events
  • 33. Race Detection private class Storage { private Map<Integer, Item> items = new HashMap<Integer, Item> (); public void store(Item item) { items.put(item.getId(), item); } public void saveToDisk() { On each access of “items” field we check On each access of “items” field we check for (Item item : items.values()) { //serialize and save race on this field race on this field saveItem(item); //... } } On each call of “items” method we check On each call of “items” method we check public Item getItem(int id) { race on this object race on this object return items.get(id); } public void reload() { items = deserealizeFromFile(); Each field of class Item is protected the Each field of class Item is protected the } same way as field “items” of class Storage same way as field “items” of class Storage }
  • 35. Clocks Storing Thread clock ThreadLocal<VectorClock > Field XXX volatile transient VectorClock XXX_vc; Foreign objects, monitors  WeakIdentityConcurrentHashMap<Object,VectorClock>  Volatiles, synchronization contracts  ConcurrentHashMap <???, VectorClock>
  • 36. Composite Keys  AtomicLongFieldUpdater.CAS(Object o, long offset, long v)  param 0 + param 1  Volatile field “abc” of object o  object + field name  AtomicInteger.set() & AtomicInteger.get()  object  ConcurrentMap.put(key, value) & ConcurrentMap.get(key)  object + param 0
  • 37. Solved Problems Composite keys for contracts and volatiles Generate them on-the-fly Avoid unnecessary keys creation ThreadLocal<MutableKeyXXX> for each CompositeKeyXXX Loading of classes, generated on-the-fly Instrument ClassLoader.loadClass()
  • 38. Solved Problems Don’t break serialization compute serialVersiodUid before instrumentation Caching components of dead clocks when thread dies, its time frames doesn’t grow anymore cache frames of dead threads to avoid memory leaks local last-known generation & global generation
  • 39. DRD in Real Life: QD ✔ 6 races found QD QD + DRD
  • 40. DRD in Real Life: MARS UI MARS ✔ 5 races found MARS + DRD
  • 41. DRD Race Report Example WRITE_READ data race between current thread Thread-12(id = 33) and thread Thread-11(id = 32) Race target : field my/app/DataServiceImpl.stopped Thread 32 accessed it in my/app/DataServiceImpl.access$400(line : 29) --------------------------------Stack trace for racing thread (id = 32) is not available.------------ -------------------------------------Current thread's stack trace (id = 33) : --------------------------- at my.app.DataServiceImpl.stop(DataServiceImpl.java:155) at my.app.DataManager.close(DataManager.java:201) ...
  • 42. DRD Advantages Doesn’t break serialization No memory leaks Few garbage No JVM modification Synchronization contracts very important: Unsafe, AbstractQueuedSynchronizer
  • 43. Links http://code.devexperts.com/display/DRD/: documentation, links, etc Contact us: drd-support@devexperts.com Useful links: see also on product page IBM MSDK ThreadSanitizer for Java jChord FindBugs JLS «Threads and locks» chapter
  • 44. Q&A

Hinweis der Redaktion

  1. Hard to find at development/review stage Control of program execution flow is frequently delegated to frameworks Hard to detect manually during testing
  2. safe/read/write contracts synchronization contracts
  3. ThreadLocal&lt;MutableInteger&gt; : increment my counter, push it ’ s value to global AtiomicInteger each N-th hit. When thread dies, increment global generation. Each clock stores last known generation. When it becomes less than global, check who have died and cache corresponding frames.
  4. Module testing – check module having contracts of others Lightweight mode : instrument little, most important parts of code Support for synchronization on array cells