Contention Resistant Non-Blocking Priority Queues
Total Page:16
File Type:pdf, Size:1020Kb
Contention resistant non-blocking priority queues Lars Frydendal Bonnichsen Kongens Lyngby 2012 IMM-MSC-2012-21 Technical University of Denmark Informatics and Mathematical Modelling Building 321, DK-2800 Kongens Lyngby, Denmark Phone +45 45253351, Fax +45 45882673 [email protected] www.imm.dtu.dk 3 Table of contents 1 Abstract...........................................................................................................................4 2 Acknowledgments..........................................................................................................5 3 Introduction....................................................................................................................6 3.1 Contributions...........................................................................................................6 3.2 Outline.....................................................................................................................7 4 Background.....................................................................................................................8 4.1 Terminology............................................................................................................8 4.2 Prior work.............................................................................................................13 4.3 Summary...............................................................................................................20 5 Concurrent building blocks..........................................................................................21 5.1 Introduction...........................................................................................................21 5.2 Random number generation..................................................................................21 5.3 Avoiding context switches....................................................................................23 5.4 Interfacing to synchronization primitives.............................................................24 5.5 Truncated exponential backoff..............................................................................41 5.6 MCS locks.............................................................................................................46 5.7 Summary...............................................................................................................50 6 Static search structure based priority queues................................................................51 6.1 Introduction...........................................................................................................51 6.2 A static tree structure for priority queues..............................................................51 6.3 Combining funnels................................................................................................53 6.4 Stacks with elimination.........................................................................................59 6.5 Truncated exponential backoff with elimination..................................................64 6.6 Conclusion............................................................................................................68 7 Investigation of wide search trees.................................................................................69 7.1 Overview...............................................................................................................69 7.2 Non-blocking k-ary search tree.............................................................................69 7.3 B-trees...................................................................................................................73 7.4 Lock-free B-tree derivative...................................................................................75 7.5 Synchronization....................................................................................................78 7.6 Rebalancing...........................................................................................................79 7.7 Memory reclamation.............................................................................................82 7.8 Implementation.....................................................................................................86 7.9 Evaluation.............................................................................................................92 7.10 Conclusion..........................................................................................................95 8 Conclusions..................................................................................................................96 9 Project planning............................................................................................................97 9.1 Risk analysis.........................................................................................................97 9.2 Project process and time planning......................................................................101 10 Appendix...................................................................................................................105 10.1 Read-modify-write update loops.......................................................................105 1Abstract 4 1 Abstract This thesis primarily deals with the design and implementation of concurrent data structures, as well as related facilities. Any concurrent data structure may have strictly limited scalability, unless care is taken in their access patterns. This thesis seeks to investigate ways to reduce these issues, for the specific context of priority queues used for picking tasks in operating systems. The thesis makes improvements upon a state of the art locking mechanism, to provide up 27 times faster locking, for small data structures. This is in part achieved, by improving a leading backoff scheme, and applying it in a novel fashion. We have designed and implemented a priority queue based on a balanced search tree. The new data structure is based on a new lock-free data structure based on B-trees. To the best of our knowledge, this is the first lock-free B-tree, that does not depend on the presence of a garbage collector. 5 2Acknowledgments 2 Acknowledgments First and foremost I would like to thank Anders Handler. You have been the best possible sparring partner during the past couple of months, where we have discussed theses on an almost daily basis. I would also like to thank all the other guys down at the lab. I want to thank all of you for the good atmosphere, and the helpful, and enlightening discussions we have had during the writing of this thesis. I would like to thank my long time study partners Thoai Lam Nguyen and Nawar Al- Mubaraki, for helping me proof read. I am also very grateful to my brother Jesper Frydendal Bonnichsen, for proof reading the thesis, even though I came to you at the very last moment. I would also like to thank the rest of my family, for being very supportive during the writing of this thesis. I would like to thank my supervisor Sven Karlsson. I have really appreciated your advice, interest and enthusiasm during this project. Finally I would also like to thank Christian Probst, who has acted as my supervisor, and managed the project since Sven fell ill. 3Introduction 6 3 Introduction This thesis deals with data structures suitable for controlling the order in which tasks run, on computers that can run tasks in parallel. Specifically the thesis deals with the case where tasks are given priority levels, where tasks with the highest priority are run first. The general data structure for solving this issue is called a priority queue[CLRS09]. The priority queue is to be implemented into the AMD64 branch of FenixOS, a research operating system developed at DTU. Picking the task with the highest priority takes computation time. Most solutions tend to significantly increase the computation time, when more tasks are picked concurrently, due to contention of resources. As computer systems grow in complexity, they tend to get more concurrent. With this change, it is increasingly important to be able to deal with high contention efficiently. 3.1 Contributions This thesis presents three primary contributions: 1. Refinement of ways to keep the computation time low at high contention. 2. Refinement of contention resistant stacks and counters. 3. Introduction of a new priority queue. Managing contention The improved ways of keeping computation time low at high contention, are focused on ways of reducing the contention. We present three significant contributions: 1. We provide an efficient way of giving each task a unique access pattern. 2. We provide an improvement to truncated exponential backoff, which is a state of the art backoff scheme, ie scheme for reducing contention. On the tested setups the improved backoff scheme gets up to 15 % higher throughput, in highly contended test cases. The new scheme does have the drawback, that it has a slightly higher memory consumption. 3. We show how to apply the improved truncated exponential backoff scheme to MCS locks. MCS locks is a state of the art locking mechanism, ie a mechanism for ensuring exclusive access. On the tested setups the improved locking scheme was able to provide a shared counter up to 2700 % higher throughput. The scheme was also able to give a shared priority queue 150 % higher throughput. In general the the improved locking mechanism provides significantly better performance, when operating on contended data.