Top Banner
1 Multi-Core for Real-Time and Safety-Critical Software: Avoid the Pitfalls Dr. Fridtjof Siebert, CTO, aicas GmbH Dr. James J. Hunt, CEO, aicas GmbH aicas Technology Multicore for Real-Time and Safety-Critical Software: Avoid the Pitfalls Dr. Fridtjof Siebert, CTO, aicas Dr. James J. Hunt, CEO aicas MultiCoreExpo 2011, 5 th May 2011
112

Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

Aug 13, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

1

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

aicas Technology

Multicore for Real-Timeand Safety-Critical Software:

Avoid the Pitfal ls

Dr. Fridtjof Siebert, CTO, aicasDr. James J. Hunt, CEO aicas

MultiCoreExpo 2011, 5th May 2011

Page 2: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

2

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Agenda

Race conditions

Synchronization

Atomic operations

Memory model

Values out of thin air

Reaching peak performance

CPU affinities

Multicore scheduling

Lock free algorithms

Compare and Swap

Page 3: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

3

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

typical code sequence (C/C++ or Java)

int counter; 

void increment(){  counter++; }

Page 4: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

4

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

typical code sequence (C/C++ or Java)

int counter; 

void increment(){  counter++; } r1 = counter; 

r2 = r1 + 1; counter = r2; 

Page 5: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

5

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

typical code sequence (C/C++ or Java)

int counter; 

void increment(){  counter++; } r1 = counter; 

r2 = r1 + 1; counter = r2; 

r1 = counter; r2 = r1 + 1; counter = r2; 

Thread 1 Thread 2

Page 6: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

6

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

typical code sequence (C/C++ or Java)

int counter; 

void increment(){  counter++; } r1 = counter; 

r2 = r1 + 1; counter = r2; 

r1 = counter; r2 = r1 + 1; counter = r2; 

Thread 1 Thread 2

An increment can get lost!

Page 7: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

7

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

typical code sequence (C/C++ or Java)

int counter; 

void increment(){  counter++; }

Page 8: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

8

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

typical code sequence (C/C++ or Java)

int counter; 

void increment(){  counter++; }

code lacks synchronization

but on a single core, it practically always works!

on a multicore, chances for failure explode!

Page 9: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

9

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Synchronization

solution: synchronize

int counter; 

synchronized void increment(){  counter++; }

Easy, problem solved.

Right? See later.

Page 10: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

10

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Atomic Operations

What is the result of

int a, b;  /* 32 bit, initially 0 */

Thread 1 Thread 2 b = a;         a = ­1; 

?

Page 11: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

11

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Atomic Operations

What is the result of

int a, b;  /* 32 bit, initially 0 */

Thread 1 Thread 2 b = a;         a = ­1; 

?

b == 0b == ­1

Page 12: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

12

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Atomic Operations

What is the result of

long a, b;  /* 64 bit, initially 0 */

Thread 1 Thread 2 b = a;         a = ­1; 

?

Page 13: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

13

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Atomic Operations

What is the result of

long a, b;  /* 64 bit, initially 0 */

Thread 1 Thread 2 b = a;         a = ­1; 

?

b == 0b == ­1b == ­4294967296b == 4294967295

Page 14: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

14

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Cache Structure

CPUs may have local caches for performance

CPU0 CPU1CPU0 CPU1CPU0 CPU1CPU0 CPU1 CPU0 CPU1CPU0 CPU1CPU0 CPU1CPU2 CPU3

CPU0 CPU1CPU0 CPU1L1 Cache L1 CacheL1 Cache L1 Cache

L2 Cache L2 Cache

Main Memory

Page 15: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

15

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Cache Structure

Modifications do not become visible immediately

Modifications may be re-ordered

Reads may refer to outdated (cached) data

Reads may be re-ordered

CPU0 CPU1 CPU2 CPU3

L1 Cache L1 CacheL1 Cache L1 Cache

L2 Cache L2 Cache

Main Memory

Page 16: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

16

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

polling update

long counter;[..]do   {    doSomething();   }while (counter < MAX);

counter is incremented by parallel thread

on a Multicore, changes to counter may not become visible!

Page 17: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

17

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Typical Problems on Mult icore

polling update

long counter;[..]do   {    doSomething();   }while (counter < MAX);

counter is incremented by parallel thread

on a Multicore, changes to counter may not become visible!

Page 18: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

18

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Solution: volati le?

polling update

volatile long counter;[..]do   {    doSomething();   }while (counter < MAX);

works for Java

does not work for C!

Page 19: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

19

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Solution: volati le?

polling update

volatile long counter;[..]do   {    doSomething();   }while (counter < MAX);

works for Java

does not work for C!

Page 20: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

20

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Understanding the Memory Model

Memory model specifies what optimizations are permitted by the compiler or underlying hardware

C/C++ programs have undefined semantics in case of race conditions

Java defines a strict memory model

Page 21: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

21

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Java's Memory Model

ordering operations aresynchronized block

accessing a volatile variable

The presence of an ordering operation determines the visible state in shared memory

Page 22: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

22

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Java's Memory Model: Defined Order

all reads are completed before entering synchronized block, or

reading a volatile variable

read fence

all writes are completed before exiting a synchronized block, or

writing a volatile variable

write fence

Page 23: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

23

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Java's Memory Model: Data Races

data races are not forbidden in Java you can use shared memory variables

your code has to tolerate optimizations

examples collecting debugging / profiling information

useful if occasional errors due to data races are tolerable

Page 24: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

24

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communicationPtr     p;boolean p_valid;

Thread 1

p = new Ptr();        p_valid = true;                                    

Page 25: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

25

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communicationPtr     p;boolean p_valid;

Thread 1 Thread 2

p = new Ptr();          p_valid = true;                                                                  

Page 26: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

26

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communicationPtr     p;boolean p_valid;

Thread 1 Thread 2

p = new Ptr();          if (p_valid)p_valid = true;           p.call();                           

Page 27: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

27

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();                           

Page 28: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

28

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

What may happen:

Page 29: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

29

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

What may happen: t1 = new Ptr();         t2 = true;                p_valid = t2;               p = t1;                   

Page 30: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

30

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

What may happen: t1 = new Ptr();         t2 = true;                p_valid = t2;               p = t1;                   

Writes reordered!

Page 31: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

31

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

What may happen: t1 = new Ptr();         t2 = true;                p_valid = t2;              p = t1;                                    

Writes reordered!

Page 32: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

32

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

What may happen: t1 = new Ptr();         t3 = p;t2 = true;              if (p_valid)  p_valid = t2;             t3.call();  p = t1;                                    

Writes reordered! Reads reordered!

Page 33: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

33

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Memory Model Example

Shared memory communication

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

What may happen: t1 = new Ptr();         t3 = p;t2 = true;              if (p_valid)  p_valid = t2;             t3.call();  p = t1;                                    

Writes reordered! Reads reordered!

Page 34: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

34

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Ptr     p;volatile boolean p_valid;

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

in Java

Example Use of Java's Memory Model

Page 35: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

35

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Ptr     p;volatile boolean p_valid;

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

in Java

Example Use of Java's Memory Model

Page 36: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

36

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Ptr     p;volatile boolean p_valid;

Thread 1 Thread 2 p = new Ptr();          if (p_valid)p_valid = true;           p.call();

in Java

Example Use of Java's Memory Model

Page 37: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

37

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)p_valid = TRUE;           p­>f = ..;();

in C?

Example Use of C's Memory Model

Page 38: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

38

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)p_valid = TRUE;           p­>f = ..;();

in C? CPU may reorder memory accesses! 

Example Use of C's Memory Model

Page 39: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

39

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)p_valid = TRUE;           p­>f = ..;();

in C? CPU may reorder memory accesses! 

Example Use of C's Memory Model

Page 40: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

40

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)p_valid = TRUE;           p­>f = ..;();

in C? CPU may reorder memory accesses!  

Example Use of C's Memory Model

Page 41: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

41

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)p_valid = TRUE;           p­>f = ..;

How to fix it?

Example Use of C's Memory Model

Page 42: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

42

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)asm volatile(             p­>f = ..;  "sfence":::"memory");     p_valid = TRUE;           

How to fix it? Add memory fences!

Example Use of C's Memory Model

Page 43: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

43

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)asm volatile(             {  "sfence":::"memory");     asm volatile(p_valid = TRUE;             "lfence":::"memory");                            p­>f = ..;                          }

How to fix it? Add memory fences!

Example Use of C's Memory Model

Page 44: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

44

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Example Use of C's Memory Model

Shared memory communicationvolatile Obj    *p;volatile boolean p_valid;

Thread 1 Thread 2 p = malloc(..);         if (p_valid)asm volatile(             {  "sfence":::"memory");     asm volatile(p_valid = TRUE;             "lfence":::"memory");                            p­>f = ..;                          }

How to fix it? Add memory fences!

Page 45: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

45

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air

imagine this codeint x = 0, n = 0;

Thread 1 Thread 2 for (i=0; i<n; i++)     x = 42;  x += f(i);            print(x);

Page 46: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

46

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air

imagine this codeint x = 0, n = 0;

Thread 1 Thread 2 for (i=0; i<n; i++)     x = 42;  x += f(i);            print(x);

can only print 42 in Java

Page 47: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

47

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air: Introduction of Writes

loop optimization in C/C++int x = 0, n = 0;

Thread 1 Thread 2 tmp = x;                for (i=0; i<n; i++)     x = 42;  tmp += f(i);x = tmp;                                        print(x);

Page 48: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

48

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air: Introduction of Writes

loop optimization in C/C++int x = 0, n = 0;

Thread 1 Thread 2 tmp = x;                for (i=0; i<n; i++)     x = 42;  tmp += f(i);x = tmp;                                        print(x);

can print 0 in C/C++

Page 49: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

49

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air

imagine this codeint x = 0, y = 0;

Thread 1 Thread 2 r1 = x;                 r2 = y;y = r1;                 x = r2;

Page 50: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

50

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air

imagine this codeint x = 0, y = 0;

Thread 1 Thread 2 r1 = x;                 r2 = y;y = r1;                 x = r2;

Expected result  x == 0; y == 0;

Only possible result in Java

Page 51: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

51

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Out of Thin Air: Optimization in C/C++

imagine this codeint x = 0, y = 0;

Thread 1 Thread 2 y = 42;                 r2 = y;r1 = x;                 x = r2;if (r1 != 42)  y = r1;

Possible results in upcoming C++ MM  x == 42; y == 42;

Page 52: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

52

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Performance on Mult icore: Example

Single core application, 3 threads

All threads synchronize frequently on same lock

Page 53: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

53

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Performance on Mult icore: Example

Single core application, 3 threads

All threads synchronize frequently on same lock

while (true)  {    synchronized (lock)      {        counter++;       }    doSomething();   }

Page 54: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

54

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Single core application, 3 threads

Performance on Mult icore: Example

Page 55: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

55

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Single core application, 3 threads

On a multicore

Performance on Mult icore: Example

Page 56: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

56

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Performance on a Mult icore

Frequent synchronization can kill the performance

Typical non-RTOS will use heuristics to improve average performance

spin-lock for a short time

blocking for longer periods

Page 57: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

57

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Performance on a Mult icore

These heuristics introduce priority-inversion and generally destroy predictability

A typical semaphore implementationdoes not take thread priority into account

does not limit worst-case-execution-time

Page 58: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

58

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies

OSes provide APIs to lock threads certain CPUs

This limits the decision space of the RTOSa global scheduler for n CPUs would always run the n highest priority threads

with affinities, this may not be possible, e.g., if the two highest priority threads are locked to the same CPU

CPU affinities can introduce priority inversion!

So what are CPU affinities good for?

Page 59: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

59

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: No More interrupts

Locking interrupts to a dedicated CPUprotects all other CPUs from interrupts, and

invalidated cashes

WCETA is simplified considerably

Page 60: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

60

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: No More interrupts

Locking interrupts to a dedicated CPUprotects all other CPUs from interrupts, and

invalidated cashes

WCETA is simplified considerably

non-RT Task

Interrupt

CPU0

CPU1 A A A A A A A A A A A RT Task

Page 61: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

61

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: Separating Threads

Locking thread A to one CPU and threads B, C, ... to other CPUs may increase A's performance.

A will not be preempted by B, C, ..

A will not see its caches invalidated by B, C, ...

Page 62: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

62

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: Separating Threads

Locking thread A to one CPU and threads B, C, ... to other CPUs may increase A's performance.

A will not be preempted by B, C, ..

A will not see its caches invalidated by B, C, ...

non-RT Task

RT Task

CPU0

CPU1

CPU2

A AA A A A

B

B B

B

B

A A A A A

B

B

B

C C C

Page 63: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

63

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: Grouping Threads

Locking threads A and B to the same CPUwill increase shared memory communication between A and B

will avoid performance degradation on locking

will enable simple scheduling analysis (RMA)

Page 64: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

64

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: Grouping Threads

Locking threads A and B to the same CPUwill increase shared memory communication between A and B

will avoid performance degradation on locking

will enable simple schedule feasibility analysis (RMA)

RT Task ACPU0

CPU1

CPU2

BAA B A RT Task B

Other Task

C

C

DD D D

E E E E

E

E

EC C

GG

G

BA

Page 65: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

65

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CPU Affinit ies: Diff icult Decisions

what if A and B both computation intensive and both access the same shared memory?

How can we use idle time?

...

Page 66: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

66

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Multicore Scheduling for Realt ime

A pure priority based scheduler is not sufficient:

Imagine three tasks A, B, C on 2 CPUS

A

B

C

Release Deadline

Page 67: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

67

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Multicore Scheduling for Realt ime

Priorities will cause deadline miss

A pri=10

B pri=10

CPU0

CPU1

C pri=9

Release Deadline

Page 68: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

68

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Multicore Scheduling for Realt ime

Priorities will cause deadline miss

A pri=10

B pri=10

CPU0

CPU1

C pri=9Release Deadline

Page 69: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

69

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Multicore Scheduling for Realt ime

CPU affinities do not help

A pri=10, {CPU0}

B pri=10, {CPU1}

CPU0

CPU1

C pri=9

Release Deadline

Page 70: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

70

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Multicore Scheduling for Realt ime

Round robin could help

ACPU0

CPU1

C B

B A C B A C

A C B

Release Deadline

Page 71: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

71

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

A C B

B A C B A C

A C B

Multicore Scheduling for Realt ime

Round robin could help

Release Deadline

CPU0

CPU1

Page 72: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

72

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Performance on Multicore

Synchronization is expensive

Can synchronization be avoided?

Can lock free algorithms be used?Use compare and swap (CAS) instructions instead

Page 73: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

73

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Lock Free Algorithms

Typical code sequence

do  {    x = counter;    result = CAS(counter,x,x+1);  }while (result != x);

Page 74: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

74

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Compare-and-Swap Issues

Typical code sequence

do  {    x = counter;    result = CAS(counter,x,x+1);  }while (result != x);

What is the WCET? ∞?

Page 75: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

75

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

On dual core:

# iterations

fre

qu

en

cy

Typical code sequence

do  {    x = counter;    result = CAS(counter,x,x+1);  }while (result != x);

What is the WCET? ∞?

Compare-and-Swap Issues

Page 76: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

76

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Lock Free Library Code

Using libraries helpsAtomicInteger counter = new AtomicInteger();void increment(){  (void)counter.incrementAndGet();}

Code is easier and safer

Hand made lock free algorithms are not for normal application development

Page 77: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

77

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Compare-and-Swap Solutions

One way state changes, without retry

Bounded number of retries

Page 78: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

78

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

One Way State Changes Using CAS

Example codefor (i=0; i<N; i++)  {    new Thread()       {        public void run()          {            CAS(state,INIT,STARTING);             [..]          }      }.start();   }

Page 79: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

79

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Bounding Retries for CAS

introduce long enough code sections in between 2 compare-and-swap loops

then, if a retry is required, one other CPU was successful

after n-1 conflicts, one can be sure that all other CPUs are outside the CAS loop

Page 80: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

80

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Bounding Retries for CAS: Example

     AtomicInteger counter = new AtomicInteger();      static final int GRANULARITY = 64;     [..]     new Thread(){       int local_counter;        public void incCounter() {         local_counter++;         if (local_counter >= GRANULARITY) {           local_counter = 0;           counter.addAndSet(GRANULARITY);         }       }     }.start(); 

Page 81: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

81

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Measurements

alloc free sweep available Memory1E-1

1E+0

1E+1

1E+2

1E+3

1E+4

1E+5

1E+6

1E+7

1E+8

1E+9

1E+10

1E+11

123456789

Number of CAS tries is bounded:

On 8-CPU x86 system (Linux)

Page 82: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

82

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

List modifaction using CAS, single linked list

next

data

head next

data

next

data

A B C

Page 83: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

83

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Add

next

data

head next

data

next

data

next

data

X

B CA B C

Page 84: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

84

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Add

next

data

head next

data

next

data

next

data

X

B CA B C

Page 85: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

85

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Add: CAS(head,A,X)

next

data

head next

data

next

data

next

data

X

B CA B C

Page 86: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

86

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Add: CAS(head,A,X)

next

data

head next

data

next

data

next

data

A B C

X

Page 87: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

87

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Add

next

data

head next

data

next

data

next

data

A B C

X

Page 88: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

88

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Remove

head next

data

next

data

next

data

A B C

Page 89: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

89

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Remove: CAS(head,A,B)

head next

data

next

data

next

data

A B C

Page 90: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

90

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Remove: CAS(head,A,B)

head next

data

next

data

next

data

A B C

Page 91: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

91

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists

Remove

head next

data

next

data

B C

Page 92: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

92

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

Now, consider concurrent modifications:

next

data

head next

data

A B

next

data

C Thread 2Thread 1

Page 93: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

93

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

next

data

C Thread 2Thread 1remove:CAS(head,A,B)

Page 94: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

94

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

next

data

C Thread 2Thread 1remove:

CAS(head,A,B)

preempted

Page 95: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

95

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

next

data

C Thread 2remove:CAS(head,A,B)

Thread 1remove:

CAS(head,A,B)

preempted

Page 96: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

96

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

Thread 2remove:CAS(head,A,B)

Thread 1remove:

CAS(head,A,B)

preempted

Page 97: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

97

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

Thread 2remove:CAS(head,A,B)

remove:CAS(head,B,C)

Thread 1remove:

CAS(head,A,B)

preempted

Page 98: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

98

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

head next

data

C

Thread 2remove:CAS(head,A,B)

remove:CAS(head,B,C)

Thread 1remove:

CAS(head,A,B)

preempted

Page 99: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

99

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

head next

data

C

Thread 2remove:CAS(head,A,B)

remove:CAS(head,B,C)

add A:CAS(head,C,A)

Thread 1remove:

CAS(head,A,B)

preempted

Page 100: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

100

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

head next

data

C

head next

data

C

next

data

A

Thread 2remove:CAS(head,A,B)

remove:CAS(head,A,B)

add A:CAS(head,C,A)

Thread 1remove:

CAS(head,A,B)

preempted

Page 101: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

101

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

head next

data

C

head next

data

C

next

data

A

head next

data

C

next

data

A

next

data

B

Thread 2remove:CAS(head,A,B)

remove:CAS(head,A,B)

add A:CAS(head,C,A)

Thread 1remove:

CAS(head,A,B)

preempted

Page 102: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

102

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

CAS Lists, ABA Problem

next

data

head next

data

A B

head next

data

B

next

data

C

next

data

C

head next

data

C

head next

data

C

next

data

A

head next

data

C

next

data

A

next

data

B

Thread 2remove:CAS(head,A,B)

remove:CAS(head,A,B)

add A:CAS(head,C,A)

Thread 1remove:

CAS(head,A,B)

preempted

B was re-introduced!

Page 103: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

103

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Problem: Solutions

Non solution: In C: free() after remove

Reason: newly allocated block may be sameSolution: In Java: only add new references

Reason: GC ensures old values no longer visibleSolution: Sync all threads before reuse

Example: Phase 1 moves elements from List1 to List2, Phase 2 moves elements back

Solution: Use 64-/128-bit CAS and mod. counter

Page 104: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

104

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

next

data

C Thread 2Thread 1remove:

preempted

Page 105: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

105

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

next

data

C Thread 2remove:CAS(head,A:42,

B:43)

Thread 1remove:

preempted

Page 106: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

106

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

head: 43 next

data

B

next

data

C

next

data

C

Thread 2remove:CAS(head,A:42,

B:43)

Thread 1remove:

Page 107: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

107

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

head: 43 next

data

B

next

data

C

next

data

C

head: 44 next

data

C

Thread 2remove:CAS(head,A:42,

B:43)

remove:CAS(head,B:43,

C:44)

Thread 1remove:

Page 108: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

108

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

head: 43 next

data

B

next

data

C

next

data

C

head: 44 next

data

C

head: 45 next

data

C

next

data

A

Thread 2remove:CAS(head,A:42,

B:43)

remove:CAS(head,B:43,

C:44)

add A:CAS(head,C:44,

A:45)

Thread 1remove:

Page 109: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

109

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

head: 43 next

data

B

next

data

C

next

data

C

head: 44 next

data

C

head: 45 next

data

C

next

data

A

Thread 2remove:CAS(head,A:42,

B:43)

remove:CAS(head,B:43,

C:44)

add A:CAS(head,C:44,

A:45)

Thread 1remove:

CAS(head,A:42,B:43)

retry!

Page 110: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

110

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ABA Solved via Modification Counter

next

data

head: 42 next

data

A B

head: 43 next

data

B

next

data

C

next

data

C

head: 44 next

data

C

head: 45 next

data

C

next

data

A

head: 46 next

data

C

Thread 2remove:CAS(head,A:42,

B:43)

remove:CAS(head,B:43,

C:44)

add A:CAS(head,C:44,

A:45)

Thread 1remove:

CAS(head,A:42,B:43)

retry!CAS(head,A:45,

C:46)

Page 111: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

111

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

ConclusionCode that runs well on single CPU may fail on multicore

Clear semantics of concurrent code is required for functional correctness

Cost of locking may be prohibitive

CPU affinities may help, but it is difficult to make the application more efficient

Lock free code is very hard to get right

A reliable memory model and good concurrent libraries are basis to avoid pitfalls.

Page 112: Multicoreexpo2011 05-05fridiusingtemplate-110518193827-phpapp02

112

Multi-Core for Real-Time and Safety-Critical Software: Avoid the PitfallsDr. Fridtjof Siebert, CTO, aicas GmbHDr. James J. Hunt, CEO, aicas GmbH

Thank you.

Please come see us at booth 2306