testing concurrent programs program testing and...
TRANSCRIPT
![Page 1: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/1.jpg)
1
Program Testing and Analysis:
Testing Concurrent Programs
Dr. Michael Pradel
Software Lab, TU Darmstadt
![Page 2: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/2.jpg)
2
Warm-up Quiz
var a = (0.1 + 0.2) + 0.3;var b = 0.1 + (0.2 + 0.3);console.log(a === b);
What does the following code print?
Something elsefalsetrue
![Page 3: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/3.jpg)
2
Warm-up Quiz
var a = (0.1 + 0.2) + 0.3;var b = 0.1 + (0.2 + 0.3);console.log(a === b);
What does the following code print?
Something elsefalsetrue
![Page 4: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/4.jpg)
2
Warm-up Quiz
var a = (0.1 + 0.2) + 0.3;var b = 0.1 + (0.2 + 0.3);console.log(a === b);
What does the following code print?
Something elsefalsetrue
Floating point numbers are representedwith finite precision(not only in JavaScript)
![Page 5: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/5.jpg)
2
Warm-up Quiz
var a = (0.1 + 0.2) + 0.3;var b = 0.1 + (0.2 + 0.3);console.log(a === b);
What does the following code print?
Something elsefalsetrue
0.30000000000000004(due to rounding)
![Page 6: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/6.jpg)
3
Mid-term
� Difficulty ≈What to expect for finalexam
� Exam and results will be madeavailable
� Results will be send out this week� Some students: Very good results� Other students: Have some work to do
![Page 7: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/7.jpg)
3
Mid-term
![Page 8: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/8.jpg)
4
Outline
1. Introduction
2. Dynamic Data Race Detection
3. Testing Thread-Safe Classes
4. Exploring Interleavings
Mostly based on these papers:
� Eraser: A Dynamic Data Race Detector for MultithreadedPrograms, Savage et al., ACM TOCS, 1997
� Fully Automatic and Precise Detection of Thread SafetyViolations, Pradel and Gross, PLDI 2012
� Finding and Reproducing Heisenbugs in ConcurrentPrograms, Musuvathi et al., USENIX 2008
![Page 9: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/9.jpg)
5
Why Bother with Concurrency?
� The free lunch provided by Moore’s law is over� CPU clock speeds stopped to increase around 2005� Instead, multi-core processors became mainstream� Need concurrent programs to make full use of the
hardware
� Many real-world problems are inherentlyconcurrent, e.g.,� Servers must handle multiple concurrent requests� Computations done on huge data often are
”embarrasingly parallel”
![Page 10: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/10.jpg)
5
Why Bother with Concurrency?
� The free lunch provided by Moore’s law is over� CPU clock speeds stopped to increase around 2005� Instead, multi-core processors became mainstream� Need concurrent programs to make full use of the
hardware
� Many real-world problems are inherentlyconcurrent, e.g.,� Servers must handle multiple concurrent requests� Computations done on huge data often are
”embarrasingly parallel”
![Page 11: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/11.jpg)
5
Why Bother with Concurrency?
� The free lunch provided by Moore’s law is over� CPU clock speeds stopped to increase around 2005� Instead, multi-core processors became mainstream� Need concurrent programs to make full use of the
hardware
� Many real-world problems are inherentlyconcurrent, e.g.,� Servers must handle multiple concurrent requests� Computations done on huge data often are
”embarrasingly parallel”
![Page 12: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/12.jpg)
6
Concurrency Styles
� Message-passing� Popular for large-scale scientific computing, e.g.,
MPI (message-passing interface)� Used in actor concurrency model, e.g., popular in
Erlang and Scala� No shared memory (ideally), all communication via
messages
� Thread-based, shared memory� Multiple concurrently executing threads� All threads access the same shared memory� Synchronize via locks and barriers
![Page 13: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/13.jpg)
6
Concurrency Styles
� Message-passing� Popular for large-scale scientific computing, e.g.,
MPI (message-passing interface)� Used in actor concurrency model, e.g., popular in
Erlang and Scala� No shared memory (ideally), all communication via
messages
� Thread-based, shared memory� Multiple concurrently executing threads� All threads access the same shared memory� Synchronize via locks and barriers
Focus of this lecture
![Page 14: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/14.jpg)
7
Example
int a = 0, b = 0;
boolean r = false, t = false;
a = 1;
r = true;
t = r;
b = a;
Thread 1 Thread 2
What does this program mean?
→ Behavior depends on threadinterleaving
![Page 15: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/15.jpg)
1
![Page 16: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/16.jpg)
9
Sequential Consistency
Assumption made here:Programs execute under sequential consistency
� Program order is preserved: Each thread’sinstructions execute in the specified order
� Shared memory behaves like a global array:Reads and writes are done immediately
� We assume sequential consistency for the rest ofthe lecture
� Many real-world platforms provide more complexsemantics (”memory models”)
![Page 17: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/17.jpg)
10
What Can Go Wrong?
Common source of errors: Data races
� Two accesses to the same shared memorylocation
� At least one is a write
� Ordering of accesses is non-deterministic
![Page 18: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/18.jpg)
11
Example
// bank account
int balance = 10;
// deposit money
int tmp1 = balance;
balance = tmp1 + 5;
// withdraw money
int tmp2 = balance;
balance = tmp2 - 7;
Thread 1 Thread 2
![Page 19: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/19.jpg)
11
Example
// bank account
int balance = 10;
// deposit money
int tmp1 = balance;
balance = tmp1 + 5;
// withdraw money
int tmp2 = balance;
balance = tmp2 - 7;
Thread 1 Thread 2
Sharedmemorylocation
Read
Write
![Page 20: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/20.jpg)
11
Example
// bank account
int balance = 10;
// deposit money
int tmp1 = balance;
balance = tmp1 + 5;
// withdraw money
int tmp2 = balance;
balance = tmp2 - 7;
Thread 1 Thread 2
3 races
![Page 21: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/21.jpg)
11
Example
// bank account
int balance = 10;
// deposit money
int tmp1 = balance;
balance = tmp1 + 5;
// withdraw money
int tmp2 = balance;
balance = tmp2 - 7;
Thread 1 Thread 2
Quiz: What values can balance
have after executing this code?
![Page 22: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/22.jpg)
11
Example
// bank account
int balance = 10;
// deposit money
int tmp1 = balance;
balance = tmp1 + 5;
// withdraw money
int tmp2 = balance;
balance = tmp2 - 7;
Thread 1 Thread 2
Possible outcomes:balance may be 3, 8, and 15
But: Only 8 is correct
![Page 23: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/23.jpg)
12
Avoiding Data Races
Use locks to ensure that accesses toshared memory do not interfere
int balance = 10;
acquire(L);
int tmp1 = balance;
balance = tmp1 + 5;
release(L);
acquire(L);
int tmp2 = balance;
balance = tmp2 - 7;
release(L);
Thread 1 Thread 2
![Page 24: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/24.jpg)
12
Avoiding Data Races
Use locks to ensure that accesses toshared memory do not interfere
int balance = 10;
acquire(L);
int tmp1 = balance;
balance = tmp1 + 5;
release(L);
acquire(L);
int tmp2 = balance;
balance = tmp2 - 7;
release(L);
Thread 1 Thread 2
Same lock⇒ Mutuallyexclusive critical sections
![Page 25: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/25.jpg)
12
Avoiding Data Races
Use locks to ensure that accesses toshared memory do not interfere
int balance = 10;Thread 1 Thread 2
synchronized (L) {
int tmp1 = balance;
balance = tmp1 + 5;
}
synchronized (L) {
int tmp2 = balance;
balance = tmp2 - 7;
}
(Java syntax)
![Page 26: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/26.jpg)
13
Outline
1. Introduction
2. Dynamic Data Race Detection
3. Testing Thread-Safe Classes
4. Exploring Interleavings
Mostly based on these papers:
� Eraser: A Dynamic Data Race Detector for MultithreadedPrograms, Savage et al., ACM TOCS, 1997
� Fully Automatic and Precise Detection of Thread SafetyViolations, Pradel and Gross, PLDI 2012
� Finding and Reproducing Heisenbugs in ConcurrentPrograms, Musuvathi et al., USENIX 2008
![Page 27: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/27.jpg)
14
Eraser: Data Race Detection
� Basic idea: Look for ”unprotected” accesses toshared memory
� Assumption: All accesses to a shared memorylocation v should happen while holding the samelock L
→ Consistent locking discipline
� Dynamic analysis that monitors all lockacquisitions, lock releases, and accesses foshared memory locations
![Page 28: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/28.jpg)
15
Lockset Algorithm (Simple Form)
� Let locksHeld(t) be the set of locksheld by thread t
� For each shared memory location v,initialize C(v) to the set of all locks
� On each access to v by thread t� Set C(v) := C(v) ∩ locksHeld(t)
� If C(v) = ∅, issue a warning
![Page 29: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/29.jpg)
15
Lockset Algorithm (Simple Form)
� Let locksHeld(t) be the set of locksheld by thread t
� For each shared memory location v,initialize C(v) to the set of all locks
� On each access to v by thread t� Set C(v) := C(v) ∩ locksHeld(t)
� If C(v) = ∅, issue a warning
Lockset
Lockset refinement
![Page 30: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/30.jpg)
2
![Page 31: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/31.jpg)
17
Simple Lockset is Too Strict
Simple lockset algorithm produces falsepositives for� variables initialized without locks held
� read-shared data read without locks held
� read-write locking mechanisms(producer-consumer style)
![Page 32: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/32.jpg)
18
Refining the Lockset Algorithm
Virgin
ExclusiveShared-modified
Shared
� Keep state of each shared memory location� Issue warnings only in the Shared-modified
state
wrrd/wr by1st thread
wr by 2nd thread
rd by 2ndthread
rd
wr
![Page 33: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/33.jpg)
19
Summary: Eraser
� Dynamic analysis to detect data races
� Assumes consistent locking discipline
� Limitations� May report false positives when locks are
acquired inconsistently but correctly
� May miss data races because it does notconsider all possible interleavings
![Page 34: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/34.jpg)
20
Outline
1. Introduction
2. Dynamic Data Race Detection
3. Testing Thread-Safe Classes
4. Exploring Interleavings
Mostly based on these papers:
� Eraser: A Dynamic Data Race Detector for MultithreadedPrograms, Savage et al., ACM TOCS, 1997
� Fully Automatic and Precise Detection of Thread SafetyViolations, Pradel and Gross, PLDI 2012
� Finding and Reproducing Heisenbugs in ConcurrentPrograms, Musuvathi et al., USENIX 2008
![Page 35: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/35.jpg)
21
Thread Safety
� Popular way to encapsulate the challenges ofconcurrent programming: Thread-safe classes
� Class ensures correct synchronization
� Clients can use instances as if they were alone
� Rest of program can treat implementation ofthread-safe class as a blackbox
![Page 36: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/36.jpg)
22
Thread Safety (2)
“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code” page 18
![Page 37: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/37.jpg)
22
Thread Safety (2)
“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code” page 18
![Page 38: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/38.jpg)
22
Thread Safety (2)
“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code” page 18
![Page 39: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/39.jpg)
22
Thread Safety (2)
“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code”
“operations ... behave as if they occurin some serial order that is consistentwith the order of the method callsmade by each of the individualthreads”
page 18
StringBuffer API documentation, JDK 6
![Page 40: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/40.jpg)
22
Thread Safety (2)
“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code”
“operations ... behave as if they occurin some serial order that is consistentwith the order of the method callsmade by each of the individualthreads”
page 18
StringBuffer API documentation, JDK 6
![Page 41: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/41.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
![Page 42: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/42.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
Quiz: What can be the content of b ifStringBuffer is thread-safe?
![Page 43: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/43.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
"abc" 3 "cab" 3 "acb" 3 "ac" 7 "bac" 7
![Page 44: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/44.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
"abc" 3 "cab" 3 "acb" 3 "ac" 7 "bac" 7
![Page 45: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/45.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
"abc" 3 "cab" 3 "acb" 3 "ac" 7 "bac" 7
![Page 46: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/46.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
"abc" 3 "cab" 3 "acb" 3 "ac" 7 "bac" 7
![Page 47: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/47.jpg)
23
Example from JDK
StringBuffer b = new StringBuffer()
b.append("a")
b.append("b")
b.append("c")
Thread 1 Thread 2
"abc" 3 "cab" 3 "acb" 3 "ac" 7 "bac" 7
![Page 48: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/48.jpg)
24
Testing Thread-Safe Classes
� Correctness of program relies on thread safety ofspecific classes
� But: What if the class is actually not thread-safe?
� ConTeGe = Concurrent Test Generator
� Creates multi-threaded unit tests
� Detects thread safety violations by comparingconcurrent behavior against linearizations
![Page 49: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/49.jpg)
25
Example Bug from JDK
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
![Page 50: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/50.jpg)
25
Example Bug from JDK
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
IndexOutOfBoundsException
Confirmed as bug: Issue #7100996
!
![Page 51: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/51.jpg)
26
ConTeGe
Bug
Classundertest(CUT)
Execute
Thread safetyoracle
Generate aconcurrent test
![Page 52: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/52.jpg)
26
ConTeGe
Bug
Classundertest(CUT)
Execute
Thread safetyoracle
Generate aconcurrent test
![Page 53: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/53.jpg)
26
ConTeGe
Bug
Classundertest(CUT)
Execute
Thread safetyoracle
Generate aconcurrent test
![Page 54: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/54.jpg)
27
Generating Concurrent Tests
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Example:
![Page 55: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/55.jpg)
27
Generating Concurrent Tests
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Sequential prefix:
Create and set upCUT instance
Example:
![Page 56: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/56.jpg)
27
Generating Concurrent Tests
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Concurrent suffixes:
Use shared CUTinstance
Example:
![Page 57: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/57.jpg)
28
Test Generation Algorithm
1. Create prefix� Instantiate CUT
� Call methods
2. Create suffixes for prefix� Call methods on shared CUT instance
3. Prefix + two suffixes = test
Selection of methods similar tofeedback-directed test generation
![Page 58: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/58.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
![Page 59: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/59.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Randomlyselect aconstructor
![Page 60: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/60.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Randomlyselect aconstructor
![Page 61: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/61.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
After adding a call:Execute
![Page 62: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/62.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
After adding a call:Execute
3
![Page 63: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/63.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Randomlyselect amethod
![Page 64: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/64.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Randomlyselect amethod
b.append(/* String */)
![Page 65: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/65.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
b.append(/* String */)
Arguments:a) Take available objectb) Call method returning
required typec) Random value
![Page 66: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/66.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Arguments:a) Take available objectb) Call method returning
required typec) Random value
![Page 67: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/67.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
After adding a call:Execute
![Page 68: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/68.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
After adding a call:Execute
3
![Page 69: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/69.jpg)
29
Creating a Prefix
1. Create prefix� Instantiate CUT
� Call methods
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
![Page 70: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/70.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
![Page 71: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/71.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
![Page 72: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/72.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
Randomlyselect amethod
![Page 73: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/73.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
Randomlyselect amethod
b.insert(/* int */, /* CharSequence */)
![Page 74: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/74.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)b.insert(/* int */, /* CharSequence */)
Arguments:a) Take available objectb) Call method returning
required typec) Random value
![Page 75: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/75.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)b.insert(-5, b)
Arguments:a) Take available objectb) Call method returning
required typec) Random value
![Page 76: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/76.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)b.insert(-5, b)
After adding a call:Execute
![Page 77: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/77.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)b.insert(-5, b)
After adding a call:Execute
!
![Page 78: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/78.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)b.insert(/* int */, /* CharSequence */)
Arguments:a) Take available objectb) Call method returning
required typec) Random value
![Page 79: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/79.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
Arguments:a) Take available objectb) Call method returning
required typec) Random value
![Page 80: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/80.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
After adding a call:Execute
![Page 81: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/81.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
After adding a call:Execute
3
![Page 82: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/82.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b)
![Page 83: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/83.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
![Page 84: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/84.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
After adding a call:Execute
![Page 85: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/85.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
After adding a call:Execute
3
![Page 86: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/86.jpg)
30
Creating Suffixes
2. Create suffixesfor prefix
� Call methods onshared CUT instance
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
![Page 87: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/87.jpg)
31
Creating a Test
3. Prefix + two suffixes = test
![Page 88: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/88.jpg)
31
Creating a Test
3. Prefix + two suffixes = test
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
![Page 89: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/89.jpg)
31
Creating a Test
3. Prefix + two suffixes = test
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
Spawn new threadfor each suffix
![Page 90: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/90.jpg)
32
Approach
Bug
Classundertest(CUT)
Execute
Thread safetyoracle
Generate aconcurrent test
![Page 91: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/91.jpg)
32
Approach
Bug
Classundertest(CUT)
Execute
Thread safetyoracle
Generate aconcurrent test
![Page 92: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/92.jpg)
33
Thread Safety Oracle
Does the test executionexpose a thread safetyviolation?
� Focus on exceptionsand deadlocks
� Compare concurrentexecution tolinearizations
![Page 93: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/93.jpg)
34
Assumptions
Concurrency-only crashes are undesired
� Matches definition of thread safety
Control over all input to tests
� Sequential execution: Deterministic
![Page 94: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/94.jpg)
35
Linearizations
� Put all calls into one thread� Preserve order of calls within a thread
![Page 95: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/95.jpg)
35
Linearizations
� Put all calls into one thread� Preserve order of calls within a thread
21 3
![Page 96: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/96.jpg)
35
Linearizations
213
2
13
21
3
� Put all calls into one thread� Preserve order of calls within a thread
21 3
![Page 97: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/97.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
3
3
![Page 98: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/98.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Execute concurrently
3
3
![Page 99: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/99.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Exception ordeadlock? 3
3
![Page 100: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/100.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Execute linearization
3
3
![Page 101: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/101.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Samefailure?
3
3
![Page 102: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/102.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Execute linearization
3
3
![Page 103: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/103.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Samefailure?
3
3
![Page 104: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/104.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
3
3
All linearizationschecked
![Page 105: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/105.jpg)
36
The Oracle
Exception ordeadlock?
Execute concurrently
No
Yes
Yes
No Thread safetyviolation
Samefailure?
Execute linearization All linearizationschecked
Thread safetyviolation
3
3
![Page 106: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/106.jpg)
37
Example
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
![Page 107: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/107.jpg)
37
Example
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
Thread 1 Thread 2
!
![Page 108: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/108.jpg)
37
Example
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
StringBuffer b = ..
b.append("abc")
b.insert(1, b)
b.deleteCharAt(1) 3
Thread 1 Thread 2
!
![Page 109: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/109.jpg)
37
Example
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
StringBuffer b = ..
b.append("abc")
b.insert(1, b)
b.deleteCharAt(1) 3
Thread 1 Thread 2
StringBuffer b = ..
b.append("abc")
b.deleteCharAt(1)
b.insert(1, b) 3
!
![Page 110: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/110.jpg)
37
Example
StringBuffer b = new StringBuffer()
b.append("abc")
b.insert(1, b) b.deleteCharAt(1)
StringBuffer b = ..
b.append("abc")
b.insert(1, b)
b.deleteCharAt(1) 3
Thread 1 Thread 2
StringBuffer b = ..
b.append("abc")
b.deleteCharAt(1)
b.insert(1, b) 3
!Thread safety violation
![Page 111: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/111.jpg)
38
Properties of the Oracle
Sound but incomplete *
� All reported violations are real� Cannot guarantee thread safety
Independent of bug type� Data races� Atomicity violations� Deadlocks
* with respect to incorrectness
![Page 112: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/112.jpg)
39
Implementation & Results
� Implemented for Java classes
� Applied to popular thread-safe classesfrom JDK, Apache libraries, etc.
� Found 15 concurrency bugs, includingpreviously unknown problems in JDK
� Takes between several seconds andseveral hours (worst case: 19 hours)
![Page 113: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/113.jpg)
40
Open Challenges
� How to generate tests that are likely to triggerbugs? (Currently: random decisions)
� Static analysis to find potential bugs; focus on thoseduring test generation
� Use feedback from test execution to steer testgeneration towards not yet explored behavior
� How to generate tests for larger pieces ofconcurrent software, e.g., entire libraries orprograms?
![Page 114: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/114.jpg)
40
Open Challenges
� How to generate tests that are likely to triggerbugs? (Currently: random decisions)
� Static analysis to find potential bugs; focus on thoseduring test generation
� Use feedback from test execution to steer testgeneration towards not yet explored behavior
� How to generate tests for larger pieces ofconcurrent software, e.g., entire libraries orprograms?
Hint: Opportunities for master theses
![Page 115: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/115.jpg)
41
Outline
1. Introduction
2. Dynamic Data Race Detection
3. Testing Thread-Safe Classes
4. Exploring Interleavings
Mostly based on these papers:
� Eraser: A Dynamic Data Race Detector for MultithreadedPrograms, Savage et al., ACM TOCS, 1997
� Fully Automatic and Precise Detection of Thread SafetyViolations, Pradel and Gross, PLDI 2012
� Finding and Reproducing Heisenbugs in ConcurrentPrograms, Musuvathi et al., USENIX 2008
![Page 116: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/116.jpg)
42
Scheduling Non-Determinism
� A single program executed with a single inputmay have many different interleavings
� Scheduler decides interleavingsnon-deterministically
� Some interleavings may expose bugs, othersexecute correctly (”Heisenbugs”)
� Challenge: How to explore different interleavings?How to detect buggy interleavings?
![Page 117: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/117.jpg)
43
CHESS in a Nutshell
� A user mode scheduler that controlsall scheduling non-determinism
� Guarantees:� Every program run takes a new thread
interleaving� Can reproduce the interleaving for every run
� Systematic but non-exhaustiveexploration of the set of possibleinterleavings
![Page 118: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/118.jpg)
44
Tree of Interleavings
� Search space of possibleinterleavings: Represent as a tree
� Node = points of scheduling decision
� Edge = decisions taken
� Each path = one possible schedule
![Page 119: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/119.jpg)
45
Example
// bank account
int balance = 10;
// deposit money
int tmp1 = balance;
balance = tmp1 + 5;
// withdraw money
int tmp2 = balance;
balance = tmp2 - 7;
Thread 1 Thread 2
![Page 120: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/120.jpg)
3
![Page 121: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/121.jpg)
47
State Space Explosion
Thread 1:
instr. 1instr. 2...instr. k
n threads
k instructions
� Number ofinterleavings: O(nn·k)
� Exponential in both n
and k
� Typically: n < 10,k > 100
� Exploring allinterleavings does notscale to largeprograms (i.e., large k)
Thread 2:
instr. 1instr. 2...instr. k
![Page 122: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/122.jpg)
48
Preemption Bounding
� Limit exploration to schedules with a smallnumber c of preemptions� Preemption = Context switches forced by the
scheduler
� Number of schedules: O((n2 · k)c · n!)� Exponential in c and n, but not in k
� Based on empirical observation: Mostconcurrency bugs can be triggered with few (< 2)interleavings
![Page 123: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/123.jpg)
49
Implementation and Results
� Implemented via binary instrumentation
� Applied to eight mid-size and large systems (upto 175K lines of code),
� Found a total of 27 bugs
� Major benefit over stress testing: Once a failure isdetected, can easily reproduce and debug it
![Page 124: Testing Concurrent Programs Program Testing and Analysissoftware-lab.org/.../Teaching/Winter_2015/PTA/lecture08.pdf · 2015-12-07 · 5 Why Bother with Concurrency? The free lunch](https://reader034.vdocuments.mx/reader034/viewer/2022042321/5f0ba9357e708231d4319943/html5/thumbnails/124.jpg)
50
Summary
� Concurrent programming is inevitable
� Writing correct concurrent programsis hard
� Techniques to detect concurrencybugs� Dynamic data race detection� Test generation and thread safety checking� Systematic exploration of interleavings