冬のlock free祭り safe

184
冬冬 Lock-Free 冬冬 DSIRNLP @kumagi

Upload: kumazaki-hiroki

Post on 24-May-2015

14.864 views

Category:

Technology


6 download

TRANSCRIPT

  • 1. DSIRNLP@kumagi

2. Lock-freeLock-free@ 3. Lock-free! 4. CPUCPUPentiumAthlonX2CoreAMD 5. CPUCPU24intel4 6. 1/4 4 = 21/2 1/21/2 1/2 7. 1/31/993 8. Intel Many Core48125WCPU3 9. 10. We are the Bottleneck!()() 11. 12. 13. Blocking 14. What is Blocking?CPU 15. What is Blocking? OK! 16. What is Blocking? 17. What is Blocking? 18. What is Blocking? 19. Lock-free 20. LockCPU 1.x ++x;2. +1 3.x 21. Lockx==1 A B1.x(1)1.x(2)2. +1 2. +13.x(1)3.x(3) OK!x==3 22. Lockx==1 AB1.x(1)1.x(1)2. +1 2. +13.x(2)3.x(2) x==2 23. targetCAS bool compare_and_swap(int* target, int expected, int newval){ if(*target == expected){ *target = newval; return true; } return false; } 24. CASLock-freeint x;void add1(){int old,new;do{old = x;new = old+1;}while(!cas(&x, old, new));} 25. CASCASx==1 A B1. x(1)1. x(1)2. +13. 12CAS 2. +14. 5. x(2)3. 12CAS6. +17. 23CASx==3 26. Lock-freeReady Lock-free Stack Lock-free Queue Lock-free List Lock-free Hashmap Lock-free SkipList Lock-free Btree Dynamic STM(Obstruction-free STM) 27. Lock-free StackCompare And Swap 28. Lock-free Stack A Head CASHead CAS 29. Lock-free Stack CAS CASCAS Head A BC D 30. Lock-free Stack A CAS HeadCAS BCD 31. Lock-free Stack A Head CASC B D 32. Lock-free Stackpop A Head CAS C D B 33. Lock-freeABA 34. Lock-free QUEUE 35. Lock-free Queue DequeTailHead 36. Lock-free Queue DequeDequeLock-free Stack 37. Lock-free Queue DequeTail Head CAS CAS 38. Lock-free Queue EnqueEnque1. Enquee2. eCAS3. TaileCAS2 39. Lock-free Queue EnqueTailHead CASCAS 40. Lock-free Queue Enque Tail Head CAS CASCASCPU1 CPU2 1. e1. e 2. CAS2. CAS 3. TailCAS3. TailCAS 41. 42. CPU CPU1CPU2CPU3CPU4 43. CPU1CPU2CPU3CPU4 44. CPU1CPU2CPU3CPU4 45. void queue::enque(const T& v){node* const new_node = new node(v);node* const tail = que_tail_;tail->next = new_node;que_tail = new_node;} 46. Lock-free StackCASCPU1 CAS CASCPU2 CAS CASCPU3 CASCPU4 CAS CAS 47. Lock-free Queue2CASCPU1 CAS CAS CAS CASCPU2 CAS CASCASCASCPU3 CAS CASCPU4 CAS CASCASCAS 48. Lock-free QueueHeadTailTail HeadTailTail 49. TailTail HeadTail Head 50. TailTail TailHead CASCAS CPU1CPU21. e 1. e2. Tail 2. Tail3. CAS 3. CAS4. TailCAS( 4. TailCAS) 51. Lock-free Queue EnqueDequeT deque(){while(1){const Node* const first = mHead;void enque(const T& v){ const Node* const last = mTail;const Node* const node = new Node(v); const Node* const next = first->mNext;while(1){ if(first != mHead){ continue;} const Node* const last = mTail;if(first == last){ const Node* const next = last->mNext;if(next == NULL){ if(last != mTail){ continue; } while(mHead->mNext == if(next == NULL){NULL){ usleep(1); } if(compare_and_set(&last-continue;>mNext, next, node)){ } compare_and_set(&mTail, last, node); compare_and_set(&mTail,last,next); return;}else{ }T result = next->mValue; }else{ if(compare_and_set(&mHead, first, next)){ compare_and_set(&mTail, last, next); delete first; }return result;} }} }} 52. Lock-free Queue EnqueDequeT deque(){while(1){const Node* const first = mHead;void enque(const T& v){ const Node* const last = mTail;const Node* const node = new Node(v); const Node* const next = first->mNext;while(1){ if(first != mHead){ continue;} const Node* const last = mTail;if(first == last){ const Node* const next = last->mNext;if(next == NULL){ if(last != mTail){ continue; } while(mHead->mNext == if(next == NULL){NULL){ usleep(1); } if(compare_and_set(&last->mNext, next, continue;node)){ } compare_and_set(&mTail, last, node); compare_and_set(&mTail,last,next); return;}else{ }T result = next->mValue; }else{ if(compare_and_set(&mHead, first, next)){ compare_and_set(&mTail, last, next); delete first; }return result;} }} }} 53. Lock-freeMessage-Passing 54. MessagePassing Deq Enq Deq Deq Enq Queue 55. Lock-freeDeqEnqDeqQueueDeqEnq 56. FASTERSingle-lockLock-free 57. Lock-free! 58. boost.lockfreequeueIntelTBBFastFlowlock-free queueMS-Queue 59. Lock-free List 60. ConcurrentListList 61. Head 62. Head 63. Head 64. 2 HeadSegmentation Fault 65. 2 Head 66. Lazy 67. Lock-free 68. CAS CAS 69. CASCASCASCAS 70. CAS CASdelete 71. 72. CAS CAS Segmentation Fault 73. CAS CASMemory Leak 74. 22 75. 1bit 4Align 1bit0 CAS CASwordAtomic 76. 2CAS CAS CAS 77. CASCAS CASCASCAS CAS 78. CAS CASCASCAS 79. 80. Lock-free Hashmap 81. HashmapO(1) 82. Lock Hashmap01234567 83. Striped Lock Hashmap01234567LockmoduloStriped 84. java.util.concurrent.ConcurrentHashMap01234567 volatile finalStriped Lockwait-freeRCU 85. Lock free? 86. Lock 0 1 2 3 4 5 6 7Lock-free List 87. 012CAS0 31 42 53 6 4 756789 10 11 88. 89. 90. Lock-free List000000100000000100000011 010000001000000011000000 00 02 40 486d 7f 80 8a c0 74 0 1Hashmap 2 3 91. 0 02 148 6d7f 2 8a 374 00021List 040 48 6d7f 1 2 80 8a 3c0 74 92. 1. 22. 3. 4. Hash 5. 00 02 40 48 6d 7f 80 8a c0 74 0 169 2 3 93. immutable 00 02 40 48 6d 7f 80 8a c0 7401234567 94. 1.(CAS ) 2. 3. 4. 5. 6.0002 40 48 60 6d 69 7f 80 8a c0 74 0 1 2 3 4 562 6 7 95. 96. FAST 97. Lock-freejava.util.concurrent.ConcurrentHashMapFAST 98. Lock-free Hashmap 99. Lock-free SkipList 100. SkipList 101. SkipListO(log n) 2 2 LevelDB 102. SkipList11/2-33 - 12 33 - 12 3364 - 3 12 33 51 64 - 3 8 12 1633 51 64 - 3 8 12 16 29 33 51 64 103. SkipList 29- 33 - 1233 - 123364 - 3 1233 51 64 - 3 8 12 16 33 51 64 - 3 8 12 16 29 33 51 64 104. Lock-freeLock-basedSkip List 105. Lock-based Skip List 106. Lock-based Skip List1. 2. 3. 4. 25. 107. Lock-based Skip ListFullyLinked github.com/kumagi/sl 108. Lock-free 109. Lock-free ListbitSkipList 110. Lock-free SkipList1. 2. 3. 111. Lock-free SkipList1. 2. 3. 112. Lock-free SkipList1. 2. 3. 113. 114. Lock-free List 115. OK 116. Lock-free ListCAS 117. Java.util.concurrentDougLea 118. Lock-free Btree 119. Btree18 16 28 5 713 1622 25 33HashMapO(log n) 120. Btree 2618 1625 28 28 5 713 16 2222 25 26 33 SplitSplitSplitmerge 121. Lock-free! 122. Lock-free RootCAS 123. Lock-freeRoot SplitSplitMerge 124. Lock-freeRootCAS SplitMergeRootCAS 125. 126. BtreeLockFreeBtreeDB 127. Dynamic Software TransactionalMemoryCAS 128. Lock-freeCAS 129. Lock-freeLock-free! 130. 131. Composability 132. SoftwareTransactionalMemorySerializable1CAS 133. SoftwareTransactionalMemory A B 134. SoftwareTransactionalMemory AOld New Owner Commit BOld New OwnerAbort 135. SoftwareTransactionalMemory AOld New Owner Commit BOld New OwnerAbort 136. SoftwareTransactionalMemoryCommitAbortActive Commit CommitNew AbortOld )!AbortActive 137. SoftwareTransactionalMemory AOld New Owner Commit BOld New OwnerAbort 138. SoftwareTransactionalMemoryA,B ACommit BAbort 139. SoftwareTransactionalMemory Active ACommit BAbort 140. SoftwareTransactionalMemory ActiveCASACommitCASBAbort 141. SoftwareTransactionalMemory CAS Commit ActiveAB 142. ActiveA B ActiveB 143. Active AbortA CAS ActiveBB 144. AB CAS1CCommit ActiveAbortDE 145. STM 146. The Art ofMultiprocessorProgrammingTAoMP 147. Transaction on Key Value Store () 148. Webmemcached, flare, cassandra 149. CASuniq1. Getsuniq2. CASuniq3. uniq 12 150. CAS KV 151. lock(a); a lock(b);set(a, 1);set(b, 2); bunlock(a);unlock(b); 152. lock(a); a lock(b);set(a, 1); set(b, 2); bunlock(a);unlock(b); 153. KVS 154. Dynamic STM 155. A BB CA B C 156. A ab|cd|ef abvalue1 cdvalue2 efvalue3 157. Aab cd ef value1value2value3 158. Dynamic STM 159. (TKVP) (KVP)key value oMel1dSde93A 8Yu4naplE2key old new statusu40F...iLdsa... saFASFD...activvalue valuee 160. status commited:New abort:Old active:() keyold new status activ value value commited abort e 161. (TKVP) Sde93Au40F... key oMel1diLdsa...8Yu4naplE2saFASFD Sde93A u40F... value oldoMel1diLdsa...valuenew 8Yu4naplE2activ status saFASFD... e 162. keyold new status value value commited commited 163. Transaction{set(a, 1); // a=1set(b, 2); // b=2set(c, 3); // c=3 } 164. TKVP no yes 165. Active Transaction{ set(a, 1); // a=1 set(b, 2); // b=2 set(c, 3); // c=3} set(hogeactive)activhoge 166. TKVP no yes 167. TKVP() Status acommited 48 commiteda8 aabort 48 aborta4 168. TKVP Transaction{old set(a, 1); // a=1new set(b, 2); // b=2 set(c, 3); // c=3 } commitedCAS commiteda old new aaaahoge set(fEe09d, 1); cas(a48 1 [old,fEe09d,hoge]) active abort CASaborta old new aaaahoge set(fEe09d, 1); cas(a48 1 [old,fEe09d,hoge]) active 169. TKVPTKVP Transaction{ set(a, 1); // a=1 set(b, 2); // b=21set(c, 3); // c=3a}hogeCAS 2bactive hogeCAS 3c hoge 170. TKVP no yes 171. activecommited Transaction{set(a, 1); // a=1set(b, 2); // b=2set(c, 3); // c=3 }cas(hoge, commited);ActivhogecommitedCASe 172. new1 ahoge CAS2 Activ commited behoge3 c hoge 173. KVPno yes 174. abortaborthogeabortcommited 175. commited:New abort:Old active: key old new status activ value value e 176. hogeTransaction{ set(a, 1); set(b, 2); fugaTransaction{ set(b, 999); 177. TKVPactivestatusabortTKVPfugaTransaction{ a hogeset(b, 999); 8 1 aCAScas(hoge,abort) b fuga hoge active abort CASbb 7 2999active 178. abort CAS activeabort 179. 180. DynamicSTMGCactivecommited aactivecommitedactivecommitedbactivec 181. GCgithub.com/kumagi/kvtx2 activecommiteda activecommitedactivecommited bactive c 182. Future WorkKVSBtreeSQLCassandraEventual Consistent 183. Lock-free Stack, Queue, List, Hashmap,SkipList, BtreeDynamicSTM Github