- Timestamp:
- Jun 12, 2023, 12:05:58 PM (3 years ago)
- Branches:
- master, stuck-waitfor-destruct
- Children:
- fec8bd1
- Parents:
- 2b78949 (diff), 38e266ca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- doc
- Files:
-
- 24 added
- 1 deleted
- 22 edited
-
bibliography/pl.bib (modified) (8 diffs)
-
papers/llheap/Paper.tex (modified) (47 diffs)
-
papers/llheap/figures/AllocatorComponents.fig (modified) (3 diffs)
-
papers/llheap/figures/AllocatorComponents.fig.bak (deleted)
-
theses/colby_parsons_MMAth/Makefile (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/actors/cfa/balance.cfa (modified) (3 diffs)
-
theses/colby_parsons_MMAth/benchmarks/actors/cfa/dynamic.cfa (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/actors/cfa/executor.cfa (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/actors/cfa/matrix.cfa (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/actors/cfa/repeat.cfa (modified) (4 diffs)
-
theses/colby_parsons_MMAth/benchmarks/actors/cfa/static.cfa (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/actors/plotData.py (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/channels/plotData.py (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/mutex_stmt/plotData.py (modified) (1 diff)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/cfa/contend.cfa (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/cfa/future.cfa (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/cfa/sidechan.cfa (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/cfa/spin.cfa (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend/contend.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend2/contend.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend2/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend4/contend.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend4/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend8/contend.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/contend8/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/sidechan/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/sidechan/sidechan.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin/spin.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin2/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin2/spin.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin4/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin4/spin.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin8/go.mod (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/go/spin8/spin.go (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/run (added)
-
theses/colby_parsons_MMAth/benchmarks/waituntil/ucpp/future.cc (added)
-
theses/colby_parsons_MMAth/code/basic_actor_example.cfa (modified) (1 diff)
-
theses/colby_parsons_MMAth/glossary.tex (modified) (1 diff)
-
theses/colby_parsons_MMAth/local.bib (modified) (1 diff)
-
theses/colby_parsons_MMAth/style/style.tex (modified) (1 diff)
-
theses/colby_parsons_MMAth/text/channels.tex (modified) (8 diffs)
-
theses/colby_parsons_MMAth/text/waituntil.tex (modified) (5 diffs)
-
theses/colby_parsons_MMAth/thesis.tex (modified) (2 diffs)
-
user/figures/EHMHierarchy.fig (modified) (1 diff)
-
user/user.tex (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/bibliography/pl.bib
r2b78949 r8a930c03 1209 1209 year = 2018, 1210 1210 pages = {2111-2146}, 1211 note = {\href{http://dx.doi.org/10.1002/spe.2624}{http://\-dx.doi.org/\-10.1002/\-spe.2624}},1211 optnote = {\href{http://dx.doi.org/10.1002/spe.2624}{http://\-dx.doi.org/\-10.1002/\-spe.2624}}, 1212 1212 } 1213 1213 … … 1870 1870 month = sep, 1871 1871 year = 2020, 1872 note = {\ href{https://plg.uwaterloo.ca/~usystem/pub/uSystem/uC++.pdf}{https://\-plg.uwaterloo.ca/\-$\sim$usystem/\-pub/\-uSystem/uC++.pdf}},1872 note = {\url{https://plg.uwaterloo.ca/~usystem/pub/uSystem/uC++.pdf}}, 1873 1873 } 1874 1874 … … 2004 2004 number = 5, 2005 2005 pages = {1005-1042}, 2006 note = {\href{https://onlinelibrary.wiley.com/doi/10.1002/spe.2925}{https://\-onlinelibrary.wiley.com/\-doi/\-10.1002/\-spe.2925}},2006 optnote = {\href{https://onlinelibrary.wiley.com/doi/10.1002/spe.2925}{https://\-onlinelibrary.wiley.com/\-doi/\-10.1002/\-spe.2925}}, 2007 2007 } 2008 2008 … … 4223 4223 title = {Implementing Lock-Free Queues}, 4224 4224 booktitle = {Seventh International Conference on Parallel and Distributed Computing Systems}, 4225 organization= {International Society for Computers and Their Applications}, 4225 4226 address = {Las Vegas, Nevada, U.S.A.}, 4226 4227 year = {1994}, … … 5086 5087 } 5087 5088 5088 @m anual{MMTk,5089 @misc{MMTk, 5089 5090 keywords = {Java memory management}, 5090 5091 contributer = {pabuhr@plg}, … … 5093 5094 month = sep, 5094 5095 year = 2006, 5095 note = {\href{http://cs.anu.edu.au/~Robin.Garner/mmtk-guide.pdf} 5096 {http://cs.anu.edu.au/\-$\sim$Robin.Garner/\-mmtk-guide.pdf}}, 5096 howpublished= {\url{http://cs.anu.edu.au/~Robin.Garner/mmtk-guide.pdf}}, 5097 5097 } 5098 5098 … … 7402 7402 } 7403 7403 7404 @misc{rpmalloc, 7405 author = {Mattias Jansson}, 7406 title = {rpmalloc version 1.4.1}, 7407 month = apr, 7408 year = 2022, 7409 howpublished= {\href{https://github.com/mjansson/rpmalloc}{https://\-github.com/\-mjansson/\-rpmalloc}}, 7410 } 7411 7404 7412 @manual{Rust, 7405 7413 keywords = {Rust programming language}, … … 7456 7464 booktitle = {PLDI '04: Proceedings of the ACM SIGPLAN 2004 Conference on Programming Language Design and Implementation}, 7457 7465 location = {Washington DC, USA}, 7458 publisher= {ACM},7466 organization= {ACM}, 7459 7467 address = {New York, NY, USA}, 7460 7468 volume = 39, -
doc/papers/llheap/Paper.tex
r2b78949 r8a930c03 252 252 Dynamic code/data memory is managed by the dynamic loader for libraries loaded at runtime, which is complex especially in a multi-threaded program~\cite{Huang06}. 253 253 However, changes to the dynamic code/data space are typically infrequent, many occurring at program startup, and are largely outside of a program's control. 254 Stack memory is managed by the program call/return-mechanism using a simpleLIFO technique, which works well for sequential programs.255 For stackful coroutines and user threads, a new stack is commonly created in dynamic-allocation memory.254 Stack memory is managed by the program call/return-mechanism using a LIFO technique, which works well for sequential programs. 255 For stackful coroutines and user threads, a new stack is commonly created in the dynamic-allocation memory. 256 256 This work focuses solely on management of the dynamic-allocation memory. 257 257 … … 293 293 \begin{enumerate}[leftmargin=*,itemsep=0pt] 294 294 \item 295 Implementation of a new stand-alone concurrent low-latency memory-allocator ($\approx$1,200 lines of code) for C/\CC programs using kernel threads (1:1 threading), and specialized versions of the allocator for the programming languages \uC and \CFAusing user-level threads running on multiple kernel threads (M:N threading).296 297 \item 298 Extend the standard C heap functionality by preserving with each allocation: its request size plus the amount allocated, whether an allocation is zero fill , andallocation alignment.295 Implementation of a new stand-alone concurrent low-latency memory-allocator ($\approx$1,200 lines of code) for C/\CC programs using kernel threads (1:1 threading), and specialized versions of the allocator for the programming languages \uC~\cite{uC++} and \CFA~\cite{Moss18,Delisle21} using user-level threads running on multiple kernel threads (M:N threading). 296 297 \item 298 Extend the standard C heap functionality by preserving with each allocation: its request size plus the amount allocated, whether an allocation is zero fill and/or allocation alignment. 299 299 300 300 \item … … 365 365 366 366 The following discussion is a quick overview of the moving-pieces that affect the design of a memory allocator and its performance. 367 It is assumed that dynamic allocates and deallocates acquire storage for a program variable, referred to asan \newterm{object}, through calls such as @malloc@ and @free@ in C, and @new@ and @delete@ in \CC.367 Dynamic acquires and releases obtain storage for a program variable, called an \newterm{object}, through calls such as @malloc@ and @free@ in C, and @new@ and @delete@ in \CC. 368 368 Space for each allocated object comes from the dynamic-allocation zone. 369 369 … … 378 378 379 379 Figure~\ref{f:AllocatorComponents} shows the two important data components for a memory allocator, management and storage, collectively called the \newterm{heap}. 380 The \newterm{management data} is a data structure located at a known memory address and contains all information necessary to manage the storage data. 381 The management data starts with fixed-sized information in the static-data memory that references components in the dynamic-allocation memory. 380 The \newterm{management data} is a data structure located at a known memory address and contains fixed-sized information in the static-data memory that references components in the dynamic-allocation memory. 382 381 For multi-threaded programs, additional management data may exist in \newterm{thread-local storage} (TLS) for each kernel thread executing the program. 383 382 The \newterm{storage data} is composed of allocated and freed objects, and \newterm{reserved memory}. … … 385 384 \ie only the program knows the location of allocated storage not the memory allocator. 386 385 Freed objects (white) represent memory deallocated by the program, which are linked into one or more lists facilitating easy location of new allocations. 387 Reserved memory (dark grey) is one or more blocks of memory obtained from the operating systembut not yet allocated to the program;388 if there are multiple reserved blocks, they are also chained together , usually internally.386 Reserved memory (dark grey) is one or more blocks of memory obtained from the \newterm{operating system} (OS) but not yet allocated to the program; 387 if there are multiple reserved blocks, they are also chained together. 389 388 390 389 \begin{figure} … … 395 394 \end{figure} 396 395 397 In m ost allocator designs, allocated objects have management data embedded within them.396 In many allocator designs, allocated objects and reserved blocks have management data embedded within them (see also Section~\ref{s:ObjectContainers}). 398 397 Figure~\ref{f:AllocatedObject} shows an allocated object with a header, trailer, and optional spacing around the object. 399 398 The header contains information about the object, \eg size, type, etc. … … 404 403 When padding and spacing are necessary, neither can be used to satisfy a future allocation request while the current allocation exists. 405 404 406 A free object alsocontains management data, \eg size, pointers, etc.405 A free object often contains management data, \eg size, pointers, etc. 407 406 Often the free list is chained internally so it does not consume additional storage, \ie the link fields are placed at known locations in the unused memory blocks. 408 407 For internal chaining, the amount of management data for a free node defines the minimum allocation size, \eg if 16 bytes are needed for a free-list node, allocation requests less than 16 bytes are rounded up. 409 The information in an allocated or freed object is overwritten when it transitions from allocated to freed and vice-versa by new management information and/or program data.408 The information in an allocated or freed object is overwritten when it transitions from allocated to freed and vice-versa by new program data and/or management information. 410 409 411 410 \begin{figure} … … 428 427 \label{s:Fragmentation} 429 428 430 Fragmentation is memory requested from the operating systembut not used by the program;429 Fragmentation is memory requested from the OS but not used by the program; 431 430 hence, allocated objects are not fragmentation. 432 431 Figure~\ref{f:InternalExternalFragmentation} shows fragmentation is divided into two forms: internal or external. … … 443 442 An allocator should strive to keep internal management information to a minimum. 444 443 445 \newterm{External fragmentation} is all memory space reserved from the operating systembut not allocated to the program~\cite{Wilson95,Lim98,Siebert00}, which includes all external management data, freed objects, and reserved memory.444 \newterm{External fragmentation} is all memory space reserved from the OS but not allocated to the program~\cite{Wilson95,Lim98,Siebert00}, which includes all external management data, freed objects, and reserved memory. 446 445 This memory is problematic in two ways: heap blowup and highly fragmented memory. 447 446 \newterm{Heap blowup} occurs when freed memory cannot be reused for future allocations leading to potentially unbounded external fragmentation growth~\cite{Berger00}. 448 Memory can become \newterm{highly fragmented} after multiple allocations and deallocations of objects, resulting in a checkerboard of adjacent allocated and free areas, where the free blocks have become very small.447 Memory can become \newterm{highly fragmented} after multiple allocations and deallocations of objects, resulting in a checkerboard of adjacent allocated and free areas, where the free blocks have become to small to service requests. 449 448 % Figure~\ref{f:MemoryFragmentation} shows an example of how a small block of memory fragments as objects are allocated and deallocated over time. 450 449 Heap blowup can occur due to allocator policies that are too restrictive in reusing freed memory (the allocated size cannot use a larger free block) and/or no coalescing of free storage. … … 452 451 % Memory is highly fragmented when most free blocks are unusable because of their sizes. 453 452 % For example, Figure~\ref{f:Contiguous} and Figure~\ref{f:HighlyFragmented} have the same quantity of external fragmentation, but Figure~\ref{f:HighlyFragmented} is highly fragmented. 454 % If there is a request to allocate a large object, Figure~\ref{f:Contiguous} is more likely to be able to satisfy it with existing free memory, while Figure~\ref{f:HighlyFragmented} likely has to request more memory from the operating system.453 % If there is a request to allocate a large object, Figure~\ref{f:Contiguous} is more likely to be able to satisfy it with existing free memory, while Figure~\ref{f:HighlyFragmented} likely has to request more memory from the OS. 455 454 456 455 % \begin{figure} … … 475 474 The first approach is a \newterm{sequential-fit algorithm} with one list of free objects that is searched for a block large enough to fit a requested object size. 476 475 Different search policies determine the free object selected, \eg the first free object large enough or closest to the requested size. 477 Any storage larger than the request can become spacing after the object or besplit into a smaller free object.476 Any storage larger than the request can become spacing after the object or split into a smaller free object. 478 477 % The cost of the search depends on the shape and quality of the free list, \eg a linear versus a binary-tree free-list, a sorted versus unsorted free-list. 479 478 … … 489 488 490 489 The third approach is \newterm{splitting} and \newterm{coalescing algorithms}. 491 When an object is allocated, if there are no free objects of the requested size, a larger free object may be split into two smaller objects to satisfy the allocation request without obtaining more memory from the operating system.492 For example, in the \newterm{buddy system}, a block of free memory is split into two equal chunks, one of those chunks is again split into two equal chunks, and so on until a block just large enough to fit the requested object is created.493 When an object is deallocated it is coalesced with the objects immediately before and after it in memory, if they are free, turning them into one larger object.490 When an object is allocated, if there are no free objects of the requested size, a larger free object is split into two smaller objects to satisfy the allocation request rather than obtaining more memory from the OS. 491 For example, in the \newterm{buddy system}, a block of free memory is split into equal chunks, one of those chunks is again split, and so on until a minimal block is created that fits the requested object. 492 When an object is deallocated, it is coalesced with the objects immediately before and after it in memory, if they are free, turning them into one larger block. 494 493 Coalescing can be done eagerly at each deallocation or lazily when an allocation cannot be fulfilled. 495 In all cases, coalescing increases allocation latency, hence some allocations can cause unbounded delays during coalescing.494 In all cases, coalescing increases allocation latency, hence some allocations can cause unbounded delays. 496 495 While coalescing does not reduce external fragmentation, the coalesced blocks improve fragmentation quality so future allocations are less likely to cause heap blowup. 497 496 % Splitting and coalescing can be used with other algorithms to avoid highly fragmented memory. … … 501 500 \label{s:Locality} 502 501 503 The principle of locality recognizes that programs tend to reference a small set of data, called a working set, for a certain period of time, where a working set iscomposed of temporal and spatial accesses~\cite{Denning05}.502 The principle of locality recognizes that programs tend to reference a small set of data, called a \newterm{working set}, for a certain period of time, composed of temporal and spatial accesses~\cite{Denning05}. 504 503 % Temporal clustering implies a group of objects are accessed repeatedly within a short time period, while spatial clustering implies a group of objects physically close together (nearby addresses) are accessed repeatedly within a short time period. 505 504 % Temporal locality commonly occurs during an iterative computation with a fixed set of disjoint variables, while spatial locality commonly occurs when traversing an array. 506 Hardware takes advantage of t emporal and spatial localitythrough multiple levels of caching, \ie memory hierarchy.505 Hardware takes advantage of the working set through multiple levels of caching, \ie memory hierarchy. 507 506 % When an object is accessed, the memory physically located around the object is also cached with the expectation that the current and nearby objects will be referenced within a short period of time. 508 For example, entire cache lines are transferred between memory and cache and entire virtual-memory pages are transferred between disk and memory.507 For example, entire cache lines are transferred between cache and memory, and entire virtual-memory pages are transferred between memory and disk. 509 508 % A program exhibiting good locality has better performance due to fewer cache misses and page faults\footnote{With the advent of large RAM memory, paging is becoming less of an issue in modern programming.}. 510 509 … … 532 531 \label{s:MutualExclusion} 533 532 534 \newterm{Mutual exclusion} provides sequential access to the shared management data of the heap.533 \newterm{Mutual exclusion} provides sequential access to the shared-management data of the heap. 535 534 There are two performance issues for mutual exclusion. 536 535 First is the overhead necessary to perform (at least) a hardware atomic operation every time a shared resource is accessed. 537 536 Second is when multiple threads contend for a shared resource simultaneously, and hence, some threads must wait until the resource is released. 538 537 Contention can be reduced in a number of ways: 539 1) Using multiple fine-grained locks versus a single lock , spreadingthe contention across a number of locks.538 1) Using multiple fine-grained locks versus a single lock to spread the contention across a number of locks. 540 539 2) Using trylock and generating new storage if the lock is busy, yielding a classic space versus time tradeoff. 541 540 3) Using one of the many lock-free approaches for reducing contention on basic data-structure operations~\cite{Oyama99}. … … 551 550 a memory allocator can only affect the latter two. 552 551 553 Assume two objects, object$_1$ and object$_2$, share a cache line.554 \newterm{Program-induced false-sharing} occurs when thread$_1$ passes a reference to object$_2$ to thread$_2$, and then threads$_1$ modifies object$_1$ while thread$_2$ modifies object$_2$.552 Specifically, assume two objects, O$_1$ and O$_2$, share a cache line, with threads, T$_1$ and T$_2$. 553 \newterm{Program-induced false-sharing} occurs when T$_1$ passes a reference to O$_2$ to T$_2$, and then T$_1$ modifies O$_1$ while T$_2$ modifies O$_2$. 555 554 % Figure~\ref{f:ProgramInducedFalseSharing} shows when Thread$_1$ passes Object$_2$ to Thread$_2$, a false-sharing situation forms when Thread$_1$ modifies Object$_1$ and Thread$_2$ modifies Object$_2$. 556 555 % Changes to Object$_1$ invalidate CPU$_2$'s cache line, and changes to Object$_2$ invalidate CPU$_1$'s cache line. … … 574 573 % \label{f:FalseSharing} 575 574 % \end{figure} 576 \newterm{Allocator-induced active false-sharing}\label{s:AllocatorInducedActiveFalseSharing} occurs when object$_1$ and object$_2$ are heap allocated and their references are passed to thread$_1$ and thread$_2$, which modify the objects.575 \newterm{Allocator-induced active false-sharing}\label{s:AllocatorInducedActiveFalseSharing} occurs when O$_1$ and O$_2$ are heap allocated and their references are passed to T$_1$ and T$_2$, which modify the objects. 577 576 % For example, in Figure~\ref{f:AllocatorInducedActiveFalseSharing}, each thread allocates an object and loads a cache-line of memory into its associated cache. 578 577 % Again, changes to Object$_1$ invalidate CPU$_2$'s cache line, and changes to Object$_2$ invalidate CPU$_1$'s cache line. … … 580 579 % is another form of allocator-induced false-sharing caused by program-induced false-sharing. 581 580 % When an object in a program-induced false-sharing situation is deallocated, a future allocation of that object may cause passive false-sharing. 582 when thread$_1$ passes object$_2$ to thread$_2$, and thread$_2$ subsequently deallocates object$_2$, and then object$_2$ is reallocated to thread$_2$ while thread$_1$ is still using object$_1$.581 when T$_1$ passes O$_2$ to T$_2$, and T$_2$ subsequently deallocates O$_2$, and then O$_2$ is reallocated to T$_2$ while T$_1$ is still using O$_1$. 583 582 584 583 … … 593 592 \label{s:MultiThreadedMemoryAllocatorFeatures} 594 593 595 The following features are used in the construction of multi-threaded memory-allocators: 596 \begin{enumerate}[itemsep=0pt] 597 \item multiple heaps: with or without a global heap, or with or without heap ownership. 598 \item object containers: with or without ownership, fixed or variable sized, global or local free-lists. 599 \item hybrid private/public heap 600 \item allocation buffer 601 \item lock-free operations 602 \end{enumerate} 594 The following features are used in the construction of multi-threaded memory-allocators: multiple heaps, user-level threading, ownership, object containers, allocation buffer, lock-free operations. 603 595 The first feature, multiple heaps, pertains to different kinds of heaps. 604 596 The second feature, object containers, pertains to the organization of objects within the storage area. … … 606 598 607 599 608 \subs ection{Multiple Heaps}600 \subsubsection{Multiple Heaps} 609 601 \label{s:MultipleHeaps} 610 602 611 603 A multi-threaded allocator has potentially multiple threads and heaps. 612 604 The multiple threads cause complexity, and multiple heaps are a mechanism for dealing with the complexity. 613 The spectrum ranges from multiple threads using a single heap, denoted as T:1 (see Figure~\ref{f:SingleHeap}), to multiple threads sharing multiple heaps, denoted as T:H (see Figure~\ref{f:SharedHeaps}), to one thread per heap, denoted as 1:1 (see Figure~\ref{f:PerThreadHeap}), which is almost back to a single-threaded allocator.605 The spectrum ranges from multiple threads using a single heap, denoted as T:1, to multiple threads sharing multiple heaps, denoted as T:H, to one thread per heap, denoted as 1:1, which is almost back to a single-threaded allocator. 614 606 615 607 \begin{figure} … … 635 627 \end{figure} 636 628 637 \paragraph{T:1 model } where all threads allocate and deallocate objects from one heap.638 Memory is obtained from the freed objects, or reserved memory in the heap, or from the operating system (OS);639 the heap may also return freed memory to the operating system.629 \paragraph{T:1 model (see Figure~\ref{f:SingleHeap})} where all threads allocate and deallocate objects from one heap. 630 Memory is obtained from the freed objects, or reserved memory in the heap, or from the OS; 631 the heap may also return freed memory to the OS. 640 632 The arrows indicate the direction memory conceptually moves for each kind of operation: allocation moves memory along the path from the heap/operating-system to the user application, while deallocation moves memory along the path from the application back to the heap/operating-system. 641 633 To safely handle concurrency, a single lock may be used for all heap operations or fine-grained locking for different operations. 642 634 Regardless, a single heap may be a significant source of contention for programs with a large amount of memory allocation. 643 635 644 \paragraph{T:H model } where each thread allocates storage from several heaps depending on certain criteria, with the goal of reducing contention by spreading allocations/deallocations across the heaps.636 \paragraph{T:H model (see Figure~\ref{f:SharedHeaps})} where each thread allocates storage from several heaps depending on certain criteria, with the goal of reducing contention by spreading allocations/deallocations across the heaps. 645 637 The decision on when to create a new heap and which heap a thread allocates from depends on the allocator design. 646 638 To determine which heap to access, each thread must point to its associated heap in some way. … … 673 665 An alternative implementation is for all heaps to share one reserved memory, which requires a separate lock for the reserved storage to ensure mutual exclusion when acquiring new memory. 674 666 Because multiple threads can allocate/free/reallocate adjacent storage, all forms of false sharing may occur. 675 Other storage-management options are to use @mmap@ to set aside (large) areas of virtual memory for each heap and suballocate each heap's storage within that area, pushing part of the storage management complexity back to the operating system.667 Other storage-management options are to use @mmap@ to set aside (large) areas of virtual memory for each heap and suballocate each heap's storage within that area, pushing part of the storage management complexity back to the OS. 676 668 677 669 % \begin{figure} … … 684 676 Multiple heaps increase external fragmentation as the ratio of heaps to threads increases, which can lead to heap blowup. 685 677 The external fragmentation experienced by a program with a single heap is now multiplied by the number of heaps, since each heap manages its own free storage and allocates its own reserved memory. 686 Additionally, objects freed by one heap cannot be reused by other threads without increasing the cost of the memory operations, except indirectly by returning free memory to the operating system, which can be expensive.687 Depending on how the operating system provides dynamic storage to an application, returning storagemay be difficult or impossible, \eg the contiguous @sbrk@ area in Unix.688 In the worst case, a program in which objects are allocated from one heap but deallocated to another heap means these freed objects are never reused.678 Additionally, objects freed by one heap cannot be reused by other threads without increasing the cost of the memory operations, except indirectly by returning free memory to the OS (see Section~\ref{s:Ownership}). 679 Returning storage to the OS may be difficult or impossible, \eg the contiguous @sbrk@ area in Unix. 680 % In the worst case, a program in which objects are allocated from one heap but deallocated to another heap means these freed objects are never reused. 689 681 690 682 Adding a \newterm{global heap} (G) attempts to reduce the cost of obtaining/returning memory among heaps (sharing) by buffering storage within the application address-space. 691 Now, each heap obtains and returns storage to/from the global heap rather than the operating system.683 Now, each heap obtains and returns storage to/from the global heap rather than the OS. 692 684 Storage is obtained from the global heap only when a heap allocation cannot be fulfilled, and returned to the global heap when a heap's free memory exceeds some threshold. 693 Similarly, the global heap buffers this memory, obtaining and returning storage to/from the operating systemas necessary.685 Similarly, the global heap buffers this memory, obtaining and returning storage to/from the OS as necessary. 694 686 The global heap does not have its own thread and makes no internal allocation requests; 695 687 instead, it uses the application thread, which called one of the multiple heaps and then the global heap, to perform operations. 696 688 Hence, the worst-case cost of a memory operation includes all these steps. 697 With respect to heap blowup, the global heap provides an indirect mechanism to move free memory among heaps, which usually has a much lower cost than interacting with the operating system to achieve the same goal and is independent of the mechanism used by the operating system to present dynamic memory to an address space. 698 689 With respect to heap blowup, the global heap provides an indirect mechanism to move free memory among heaps, which usually has a much lower cost than interacting with the OS to achieve the same goal and is independent of the mechanism used by the OS to present dynamic memory to an address space. 699 690 However, since any thread may indirectly perform a memory operation on the global heap, it is a shared resource that requires locking. 700 691 A single lock can be used to protect the global heap or fine-grained locking can be used to reduce contention. 701 692 In general, the cost is minimal since the majority of memory operations are completed without the use of the global heap. 702 693 703 704 \paragraph{1:1 model (thread heaps)} where each thread has its own heap eliminating most contention and locking because threads seldom access another thread's heap (see ownership in Section~\ref{s:Ownership}). 694 \paragraph{1:1 model (see Figure~\ref{f:PerThreadHeap})} where each thread has its own heap eliminating most contention and locking because threads seldom access another thread's heap (see Section~\ref{s:Ownership}). 705 695 An additional benefit of thread heaps is improved locality due to better memory layout. 706 696 As each thread only allocates from its heap, all objects are consolidated in the storage area for that heap, better utilizing each CPUs cache and accessing fewer pages. … … 708 698 Thread heaps can also eliminate allocator-induced active false-sharing, if memory is acquired so it does not overlap at crucial boundaries with memory for another thread's heap. 709 699 For example, assume page boundaries coincide with cache line boundaries, if a thread heap always acquires pages of memory then no two threads share a page or cache line unless pointers are passed among them. 710 Hence, allocator-induced active false-sharing cannot occur because the memory for thread heaps never overlaps.700 % Hence, allocator-induced active false-sharing cannot occur because the memory for thread heaps never overlaps. 711 701 712 702 When a thread terminates, there are two options for handling its thread heap. … … 720 710 721 711 It is possible to use any of the heap models with user-level (M:N) threading. 722 However, an important goal of user-level threading is for fast operations (creation/termination/context-switching) by not interacting with the operating system, which allows the ability to create large numbers of high-performance interacting threads ($>$ 10,000).712 However, an important goal of user-level threading is for fast operations (creation/termination/context-switching) by not interacting with the OS, which allows the ability to create large numbers of high-performance interacting threads ($>$ 10,000). 723 713 It is difficult to retain this goal, if the user-threading model is directly involved with the heap model. 724 714 Figure~\ref{f:UserLevelKernelHeaps} shows that virtually all user-level threading systems use whatever kernel-level heap-model is provided by the language runtime. … … 732 722 \end{figure} 733 723 734 Adopting this modelresults in a subtle problem with shared heaps.735 With kernel threading, an operation that isstarted by a kernel thread is always completed by that thread.736 For example, if a kernel thread starts an allocation/deallocation on a shared heap, it always completes that operation with that heap even if preempted, \ie any locking correctness associated with the shared heap is preserved across preemption.724 Adopting user threading results in a subtle problem with shared heaps. 725 With kernel threading, an operation started by a kernel thread is always completed by that thread. 726 For example, if a kernel thread starts an allocation/deallocation on a shared heap, it always completes that operation with that heap, even if preempted, \ie any locking correctness associated with the shared heap is preserved across preemption. 737 727 However, this correctness property is not preserved for user-level threading. 738 728 A user thread can start an allocation/deallocation on one kernel thread, be preempted (time slice), and continue running on a different kernel thread to complete the operation~\cite{Dice02}. 739 729 When the user thread continues on the new kernel thread, it may have pointers into the previous kernel-thread's heap and hold locks associated with it. 740 730 To get the same kernel-thread safety, time slicing must be disabled/\-enabled around these operations, so the user thread cannot jump to another kernel thread. 741 However, eagerly disabling/enabling time-slicing on the allocation/deallocation fast path is expensive, because preemption does not happen that frequently.731 However, eagerly disabling/enabling time-slicing on the allocation/deallocation fast path is expensive, because preemption is infrequent (milliseconds). 742 732 Instead, techniques exist to lazily detect this case in the interrupt handler, abort the preemption, and return to the operation so it can complete atomically. 743 Occasional ly ignoring a preemption should be benign, but a persistent lack of preemption can result in both short and long termstarvation;744 techniques like roll forward can be used to force an eventual preemption.733 Occasional ignoring of a preemption should be benign, but a persistent lack of preemption can result in starvation; 734 techniques like rolling forward the preemption to the next context switch can be used. 745 735 746 736 … … 800 790 % For example, in Figure~\ref{f:AllocatorInducedPassiveFalseSharing}, Object$_2$ may be deallocated to Thread$_2$'s heap initially. 801 791 % If Thread$_2$ reallocates Object$_2$ before it is returned to its owner heap, then passive false-sharing may occur. 792 793 For thread heaps with ownership, it is possible to combine these approaches into a hybrid approach with both private and public heaps.% (see~Figure~\ref{f:HybridPrivatePublicHeap}). 794 The main goal of the hybrid approach is to eliminate locking on thread-local allocation/deallocation, while providing ownership to prevent heap blowup. 795 In the hybrid approach, a thread first allocates from its private heap and second from its public heap if no free memory exists in the private heap. 796 Similarly, a thread first deallocates an object to its private heap, and second to the public heap. 797 Both private and public heaps can allocate/deallocate to/from the global heap if there is no free memory or excess free memory, although an implementation may choose to funnel all interaction with the global heap through one of the heaps. 798 % Note, deallocation from the private to the public (dashed line) is unlikely because there is no obvious advantages unless the public heap provides the only interface to the global heap. 799 Finally, when a thread frees an object it does not own, the object is either freed immediately to its owner's public heap or put in the freeing thread's private heap for delayed ownership, which does allows the freeing thread to temporarily reuse an object before returning it to its owner or batch objects for an owner heap into a single return. 800 801 % \begin{figure} 802 % \centering 803 % \input{PrivatePublicHeaps.pstex_t} 804 % \caption{Hybrid Private/Public Heap for Per-thread Heaps} 805 % \label{f:HybridPrivatePublicHeap} 806 % \vspace{10pt} 807 % \input{RemoteFreeList.pstex_t} 808 % \caption{Remote Free-List} 809 % \label{f:RemoteFreeList} 810 % \end{figure} 811 812 % As mentioned, an implementation may have only one heap interact with the global heap, so the other heap can be simplified. 813 % For example, if only the private heap interacts with the global heap, the public heap can be reduced to a lock-protected free-list of objects deallocated by other threads due to ownership, called a \newterm{remote free-list}. 814 % To avoid heap blowup, the private heap allocates from the remote free-list when it reaches some threshold or it has no free storage. 815 % Since the remote free-list is occasionally cleared during an allocation, this adds to that cost. 816 % Clearing the remote free-list is $O(1)$ if the list can simply be added to the end of the private-heap's free-list, or $O(N)$ if some action must be performed for each freed object. 817 818 % If only the public heap interacts with other threads and the global heap, the private heap can handle thread-local allocations and deallocations without locking. 819 % In this scenario, the private heap must deallocate storage after reaching a certain threshold to the public heap (and then eventually to the global heap from the public heap) or heap blowup can occur. 820 % If the public heap does the major management, the private heap can be simplified to provide high-performance thread-local allocations and deallocations. 821 822 % The main disadvantage of each thread having both a private and public heap is the complexity of managing two heaps and their interactions in an allocator. 823 % Interestingly, heap implementations often focus on either a private or public heap, giving the impression a single versus a hybrid approach is being used. 824 % In many case, the hybrid approach is actually being used, but the simpler heap is just folded into the complex heap, even though the operations logically belong in separate heaps. 825 % For example, a remote free-list is actually a simple public-heap, but may be implemented as an integral component of the complex private-heap in an allocator, masking the presence of a hybrid approach. 802 826 803 827 … … 817 841 818 842 819 \subs ection{Object Containers}843 \subsubsection{Object Containers} 820 844 \label{s:ObjectContainers} 821 845 … … 827 851 \eg an object is accessed by the program after it is allocated, while the header is accessed by the allocator after it is free. 828 852 829 The alternative factors common header data to a separate location in memory and organizes associated free storage into blocks called \newterm{object containers} (\newterm{superblocks} in~\cite{Berger00}), as in Figure~\ref{f:ObjectContainer}.853 An alternative approach factors common header data to a separate location in memory and organizes associated free storage into blocks called \newterm{object containers} (\newterm{superblocks}~\cite{Berger00}), as in Figure~\ref{f:ObjectContainer}. 830 854 The header for the container holds information necessary for all objects in the container; 831 855 a trailer may also be used at the end of the container. … … 862 886 863 887 864 \ subsubsection{Container Ownership}888 \paragraph{Container Ownership} 865 889 \label{s:ContainerOwnership} 866 890 … … 894 918 895 919 Additional restrictions may be applied to the movement of containers to prevent active false-sharing. 896 For example, if a container changes ownership through the global heap, then when a thread allocates an object from the newly acquired container itis actively false-sharing even though no objects are passed among threads.920 For example, if a container changes ownership through the global heap, then a thread allocating from the newly acquired container is actively false-sharing even though no objects are passed among threads. 897 921 Note, once the thread frees the object, no more false sharing can occur until the container changes ownership again. 898 922 To prevent this form of false sharing, container movement may be restricted to when all objects in the container are free. 899 One implementation approach that increases the freedom to return a free container to the operating system involves allocating containers using a call like @mmap@, which allows memory at an arbitrary address to be returned versus only storage at the end of the contiguous @sbrk@ area, again pushing storage management complexity back to the operating system.923 One implementation approach that increases the freedom to return a free container to the OS involves allocating containers using a call like @mmap@, which allows memory at an arbitrary address to be returned versus only storage at the end of the contiguous @sbrk@ area, again pushing storage management complexity back to the OS. 900 924 901 925 % \begin{figure} … … 930 954 931 955 932 \ subsubsection{Container Size}956 \paragraph{Container Size} 933 957 \label{s:ContainerSize} 934 958 … … 941 965 However, with more objects in a container, there may be more objects that are unallocated, increasing external fragmentation. 942 966 With smaller containers, not only are there more containers, but a second new problem arises where objects are larger than the container. 943 In general, large objects, \eg greater than 64\,KB, are allocated directly from the operating system and are returned immediately to the operating systemto reduce long-term external fragmentation.967 In general, large objects, \eg greater than 64\,KB, are allocated directly from the OS and are returned immediately to the OS to reduce long-term external fragmentation. 944 968 If the container size is small, \eg 1\,KB, then a 1.5\,KB object is treated as a large object, which is likely to be inappropriate. 945 969 Ideally, it is best to use smaller containers for smaller objects, and larger containers for medium objects, which leads to the issue of locating the container header. … … 970 994 971 995 972 \ subsubsection{Container Free-Lists}996 \paragraph{Container Free-Lists} 973 997 \label{s:containersfreelists} 974 998 … … 1005 1029 1006 1030 1007 \subsubsection{Hybrid Private/Public Heap} 1008 \label{s:HybridPrivatePublicHeap} 1009 1010 Section~\ref{s:Ownership} discusses advantages and disadvantages of public heaps (T:H model and with ownership) and private heaps (thread heaps with ownership). 1011 For thread heaps with ownership, it is possible to combine these approaches into a hybrid approach with both private and public heaps (see~Figure~\ref{f:HybridPrivatePublicHeap}). 1012 The main goal of the hybrid approach is to eliminate locking on thread-local allocation/deallocation, while providing ownership to prevent heap blowup. 1013 In the hybrid approach, a thread first allocates from its private heap and second from its public heap if no free memory exists in the private heap. 1014 Similarly, a thread first deallocates an object to its private heap, and second to the public heap. 1015 Both private and public heaps can allocate/deallocate to/from the global heap if there is no free memory or excess free memory, although an implementation may choose to funnel all interaction with the global heap through one of the heaps. 1016 Note, deallocation from the private to the public (dashed line) is unlikely because there is no obvious advantages unless the public heap provides the only interface to the global heap. 1017 Finally, when a thread frees an object it does not own, the object is either freed immediately to its owner's public heap or put in the freeing thread's private heap for delayed ownership, which allows the freeing thread to temporarily reuse an object before returning it to its owner or batch objects for an owner heap into a single return. 1018 1019 \begin{figure} 1020 \centering 1021 \input{PrivatePublicHeaps.pstex_t} 1022 \caption{Hybrid Private/Public Heap for Per-thread Heaps} 1023 \label{f:HybridPrivatePublicHeap} 1024 % \vspace{10pt} 1025 % \input{RemoteFreeList.pstex_t} 1026 % \caption{Remote Free-List} 1027 % \label{f:RemoteFreeList} 1028 \end{figure} 1029 1030 As mentioned, an implementation may have only one heap interact with the global heap, so the other heap can be simplified. 1031 For example, if only the private heap interacts with the global heap, the public heap can be reduced to a lock-protected free-list of objects deallocated by other threads due to ownership, called a \newterm{remote free-list}. 1032 To avoid heap blowup, the private heap allocates from the remote free-list when it reaches some threshold or it has no free storage. 1033 Since the remote free-list is occasionally cleared during an allocation, this adds to that cost. 1034 Clearing the remote free-list is $O(1)$ if the list can simply be added to the end of the private-heap's free-list, or $O(N)$ if some action must be performed for each freed object. 1035 1036 If only the public heap interacts with other threads and the global heap, the private heap can handle thread-local allocations and deallocations without locking. 1037 In this scenario, the private heap must deallocate storage after reaching a certain threshold to the public heap (and then eventually to the global heap from the public heap) or heap blowup can occur. 1038 If the public heap does the major management, the private heap can be simplified to provide high-performance thread-local allocations and deallocations. 1039 1040 The main disadvantage of each thread having both a private and public heap is the complexity of managing two heaps and their interactions in an allocator. 1041 Interestingly, heap implementations often focus on either a private or public heap, giving the impression a single versus a hybrid approach is being used. 1042 In many case, the hybrid approach is actually being used, but the simpler heap is just folded into the complex heap, even though the operations logically belong in separate heaps. 1043 For example, a remote free-list is actually a simple public-heap, but may be implemented as an integral component of the complex private-heap in an allocator, masking the presence of a hybrid approach. 1044 1045 1046 \subsection{Allocation Buffer} 1031 \subsubsection{Allocation Buffer} 1047 1032 \label{s:AllocationBuffer} 1048 1033 1049 1034 An allocation buffer is reserved memory (see Section~\ref{s:AllocatorComponents}) not yet allocated to the program, and is used for allocating objects when the free list is empty. 1050 1035 That is, rather than requesting new storage for a single object, an entire buffer is requested from which multiple objects are allocated later. 1051 Any heap may use an allocation buffer, resulting in allocation from the buffer before requesting objects (containers) from the global heap or operating system, respectively.1036 Any heap may use an allocation buffer, resulting in allocation from the buffer before requesting objects (containers) from the global heap or OS, respectively. 1052 1037 The allocation buffer reduces contention and the number of global/operating-system calls. 1053 1038 For coalescing, a buffer is split into smaller objects by allocations, and recomposed into larger buffer areas during deallocations. … … 1062 1047 1063 1048 Allocation buffers may increase external fragmentation, since some memory in the allocation buffer may never be allocated. 1064 A smaller allocation buffer reduces the amount of external fragmentation, but increases the number of calls to the global heap or operating system.1049 A smaller allocation buffer reduces the amount of external fragmentation, but increases the number of calls to the global heap or OS. 1065 1050 The allocation buffer also slightly increases internal fragmentation, since a pointer is necessary to locate the next free object in the buffer. 1066 1051 … … 1068 1053 For example, when a container is created, rather than placing all objects within the container on the free list, the objects form an allocation buffer and are allocated from the buffer as allocation requests are made. 1069 1054 This lazy method of constructing objects is beneficial in terms of paging and caching. 1070 For example, although an entire container, possibly spanning several pages, is allocated from the operating system, only a small part of the container is used in the working set of the allocator, reducing the number of pages and cache lines that are brought into higher levels of cache.1071 1072 1073 \subs ection{Lock-Free Operations}1055 For example, although an entire container, possibly spanning several pages, is allocated from the OS, only a small part of the container is used in the working set of the allocator, reducing the number of pages and cache lines that are brought into higher levels of cache. 1056 1057 1058 \subsubsection{Lock-Free Operations} 1074 1059 \label{s:LockFreeOperations} 1075 1060 … … 1194 1179 % A sequence of code that is guaranteed to run to completion before being invoked to accept another input is called serially-reusable code.~\cite{SeriallyReusable}\label{p:SeriallyReusable} 1195 1180 % \end{quote} 1196 % If a KT is preempted during an allocation operation, the operating systemcan schedule another KT on the same CPU, which can begin an allocation operation before the previous operation associated with this CPU has completed, invalidating heap correctness.1181 % If a KT is preempted during an allocation operation, the OS can schedule another KT on the same CPU, which can begin an allocation operation before the previous operation associated with this CPU has completed, invalidating heap correctness. 1197 1182 % Note, the serially-reusable problem can occur in sequential programs with preemption, if the signal handler calls the preempted function, unless the function is serially reusable. 1198 % Essentially, the serially-reusable problem is a race condition on an unprotected critical subsection, where the operating systemis providing the second thread via the signal handler.1183 % Essentially, the serially-reusable problem is a race condition on an unprotected critical subsection, where the OS is providing the second thread via the signal handler. 1199 1184 % 1200 1185 % Library @librseq@~\cite{librseq} was used to perform a fast determination of the CPU and to ensure all memory operations complete on one CPU using @librseq@'s restartable sequences, which restart the critical subsection after undoing its writes, if the critical subsection is preempted. … … 1256 1241 A sequence of code that is guaranteed to run to completion before being invoked to accept another input is called serially-reusable code.~\cite{SeriallyReusable}\label{p:SeriallyReusable} 1257 1242 \end{quote} 1258 If a KT is preempted during an allocation operation, the operating systemcan schedule another KT on the same CPU, which can begin an allocation operation before the previous operation associated with this CPU has completed, invalidating heap correctness.1243 If a KT is preempted during an allocation operation, the OS can schedule another KT on the same CPU, which can begin an allocation operation before the previous operation associated with this CPU has completed, invalidating heap correctness. 1259 1244 Note, the serially-reusable problem can occur in sequential programs with preemption, if the signal handler calls the preempted function, unless the function is serially reusable. 1260 Essentially, the serially-reusable problem is a race condition on an unprotected critical subsection, where the operating systemis providing the second thread via the signal handler.1245 Essentially, the serially-reusable problem is a race condition on an unprotected critical subsection, where the OS is providing the second thread via the signal handler. 1261 1246 1262 1247 Library @librseq@~\cite{librseq} was used to perform a fast determination of the CPU and to ensure all memory operations complete on one CPU using @librseq@'s restartable sequences, which restart the critical subsection after undoing its writes, if the critical subsection is preempted. … … 1273 1258 For the T:H=CPU and 1:1 models, locking is eliminated along the allocation fastpath. 1274 1259 However, T:H=CPU has poor operating-system support to determine the CPU id (heap id) and prevent the serially-reusable problem for KTs. 1275 More operating systemsupport is required to make this model viable, but there is still the serially-reusable problem with user-level threading.1260 More OS support is required to make this model viable, but there is still the serially-reusable problem with user-level threading. 1276 1261 So the 1:1 model had no atomic actions along the fastpath and no special operating-system support requirements. 1277 1262 The 1:1 model still has the serially-reusable problem with user-level threading, which is addressed in Section~\ref{s:UserlevelThreadingSupport}, and the greatest potential for heap blowup for certain allocation patterns. … … 1308 1293 A primary goal of llheap is low latency, hence the name low-latency heap (llheap). 1309 1294 Two forms of latency are internal and external. 1310 Internal latency is the time to perform an allocation, while external latency is time to obtain/return storage from/to the operating system.1295 Internal latency is the time to perform an allocation, while external latency is time to obtain/return storage from/to the OS. 1311 1296 Ideally latency is $O(1)$ with a small constant. 1312 1297 … … 1314 1299 The mitigating factor is that most programs have well behaved allocation patterns, where the majority of allocation operations can be $O(1)$, and heap blowup does not occur without coalescing (although the allocation footprint may be slightly larger). 1315 1300 1316 To obtain $O(1)$ external latency means obtaining one large storage area from the operating systemand subdividing it across all program allocations, which requires a good guess at the program storage high-watermark and potential large external fragmentation.1301 To obtain $O(1)$ external latency means obtaining one large storage area from the OS and subdividing it across all program allocations, which requires a good guess at the program storage high-watermark and potential large external fragmentation. 1317 1302 Excluding real-time operating-systems, operating-system operations are unbounded, and hence some external latency is unavoidable. 1318 1303 The mitigating factor is that operating-system calls can often be reduced if a programmer has a sense of the storage high-watermark and the allocator is capable of using this information (see @malloc_expansion@ \pageref{p:malloc_expansion}). … … 1329 1314 headers per allocation versus containers, 1330 1315 no coalescing to minimize latency, 1331 global heap memory (pool) obtained from the operating systemusing @mmap@ to create and reuse heaps needed by threads,1316 global heap memory (pool) obtained from the OS using @mmap@ to create and reuse heaps needed by threads, 1332 1317 local reserved memory (pool) per heap obtained from global pool, 1333 global reserved memory (pool) obtained from the operating systemusing @sbrk@ call,1318 global reserved memory (pool) obtained from the OS using @sbrk@ call, 1334 1319 optional fast-lookup table for converting allocation requests into bucket sizes, 1335 1320 optional statistic-counters table for accumulating counts of allocation operations. … … 1358 1343 Each heap uses segregated free-buckets that have free objects distributed across 91 different sizes from 16 to 4M. 1359 1344 All objects in a bucket are of the same size. 1360 The number of buckets used is determined dynamically depending on the crossover point from @sbrk@ to @mmap@ allocation using @mallopt( M_MMAP_THRESHOLD )@, \ie small objects managed by the program and large objects managed by the operating system.1345 The number of buckets used is determined dynamically depending on the crossover point from @sbrk@ to @mmap@ allocation using @mallopt( M_MMAP_THRESHOLD )@, \ie small objects managed by the program and large objects managed by the OS. 1361 1346 Each free bucket of a specific size has two lists. 1362 1347 1) A free stack used solely by the KT heap-owner, so push/pop operations do not require locking. … … 1367 1352 Algorithm~\ref{alg:heapObjectAlloc} shows the allocation outline for an object of size $S$. 1368 1353 First, the allocation is divided into small (@sbrk@) or large (@mmap@). 1369 For large allocations, the storage is mapped directly from the operating system.1354 For large allocations, the storage is mapped directly from the OS. 1370 1355 For small allocations, $S$ is quantized into a bucket size. 1371 1356 Quantizing is performed using a binary search over the ordered bucket array. … … 1378 1363 heap's local pool, 1379 1364 global pool, 1380 operating system(@sbrk@).1365 OS (@sbrk@). 1381 1366 1382 1367 \begin{algorithm} … … 1443 1428 Algorithm~\ref{alg:heapObjectFreeOwn} shows the de-allocation (free) outline for an object at address $A$ with ownership. 1444 1429 First, the address is divided into small (@sbrk@) or large (@mmap@). 1445 For large allocations, the storage is unmapped back to the operating system.1430 For large allocations, the storage is unmapped back to the OS. 1446 1431 For small allocations, the bucket associated with the request size is retrieved. 1447 1432 If the bucket is local to the thread, the allocation is pushed onto the thread's associated bucket. … … 3044 3029 3045 3030 \textsf{pt3} is the only memory allocator where the total dynamic memory goes down in the second half of the program lifetime when the memory is freed by the benchmark program. 3046 It makes pt3 the only memory allocator that gives memory back to the operating systemas it is freed by the program.3031 It makes pt3 the only memory allocator that gives memory back to the OS as it is freed by the program. 3047 3032 3048 3033 % FOR 1 THREAD -
doc/papers/llheap/figures/AllocatorComponents.fig
r2b78949 r8a930c03 8 8 -2 9 9 1200 2 10 6 1275 2025 2700 262511 10 6 2400 2025 2700 2625 12 11 2 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5 … … 14 13 2 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5 15 14 2700 2025 2700 2325 2400 2325 2400 2025 2700 2025 16 -617 4 2 0 50 -1 2 11 0.0000 2 165 1005 2325 2400 Management\00118 15 -6 19 16 2 2 0 1 0 7 50 -1 -1 0.000 0 0 -1 0 0 5 … … 61 58 2 2 0 1 0 7 60 -1 13 0.000 0 0 -1 0 0 5 62 59 3300 2700 6300 2700 6300 3000 3300 3000 3300 2700 63 4 0 0 50 -1 2 11 0.0000 2 165 585 3300 1725 Storage\00160 4 0 0 50 -1 2 11 0.0000 2 165 1005 3300 1725 Storage Data\001 64 61 4 2 0 50 -1 0 11 0.0000 2 165 810 3000 1875 free objects\001 65 62 4 2 0 50 -1 0 11 0.0000 2 135 1140 3000 2850 reserve memory\001 66 63 4 1 0 50 -1 0 11 0.0000 2 120 795 2325 1500 Static Zone\001 67 64 4 1 0 50 -1 0 11 0.0000 2 165 1845 4800 1500 Dynamic-Allocation Zone\001 65 4 2 0 50 -1 2 11 0.0000 2 165 1005 2325 2325 Management\001 66 4 2 0 50 -1 2 11 0.0000 2 135 375 2325 2525 Data\001 -
doc/theses/colby_parsons_MMAth/Makefile
r2b78949 r8a930c03 98 98 99 99 ${BASE}.dvi : Makefile ${GRAPHS} ${PROGRAMS} ${PICTURES} ${FIGURES} ${SOURCES} ${DATA} \ 100 style/style.tex ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build}100 glossary.tex style/style.tex ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build} 101 101 # Must have *.aux file containing citations for bibtex 102 102 if [ ! -r ${basename $@}.aux ] ; then ${LaTeX} ${basename $@}.tex ; fi -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/balance.cfa
r2b78949 r8a930c03 31 31 32 32 d_actor ** actor_arr; 33 Allocation receive( d_actor & this, start_msg & msg ) with( this ) {33 allocation receive( d_actor & this, start_msg & msg ) with( this ) { 34 34 for ( i; Set ) { 35 35 *actor_arr[i + gstart] << shared_msg; … … 38 38 } 39 39 40 Allocation receive( d_actor & this, d_msg & msg ) with( this ) {40 allocation receive( d_actor & this, d_msg & msg ) with( this ) { 41 41 if ( recs == rounds ) return Delete; 42 42 if ( recs % Batch == 0 ) { … … 50 50 } 51 51 52 Allocation receive( filler & this, d_msg & msg ) { return Delete; }52 allocation receive( filler & this, d_msg & msg ) { return Delete; } 53 53 54 54 int main( int argc, char * argv[] ) { -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/dynamic.cfa
r2b78949 r8a930c03 24 24 25 25 uint64_t start_time; 26 Allocation receive( derived_actor & receiver, derived_msg & msg ) {26 allocation receive( derived_actor & receiver, derived_msg & msg ) { 27 27 if ( msg.cnt >= Times ) { 28 28 printf("%.2f\n", ((double)(bench_time() - start_time)) / ((double)Times) ); // ns -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/executor.cfa
r2b78949 r8a930c03 25 25 struct d_msg { inline message; } shared_msg; 26 26 27 Allocation receive( d_actor & this, d_msg & msg ) with( this ) {27 allocation receive( d_actor & this, d_msg & msg ) with( this ) { 28 28 if ( recs == rounds ) return Finished; 29 29 if ( recs % Batch == 0 ) { -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/matrix.cfa
r2b78949 r8a930c03 24 24 } 25 25 26 Allocation receive( derived_actor & receiver, derived_msg & msg ) {26 allocation receive( derived_actor & receiver, derived_msg & msg ) { 27 27 for ( unsigned int i = 0; i < yc; i += 1 ) { // multiply X_row by Y_col and sum products 28 28 msg.Z[i] = 0; -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/repeat.cfa
r2b78949 r8a930c03 46 46 47 47 Client * cl; 48 Allocation receive( Server & this, IntMsg & msg ) { msg.val = 7; *cl << msg; return Nodelete; }49 Allocation receive( Server & this, CharMsg & msg ) { msg.val = 'x'; *cl << msg; return Nodelete; }50 Allocation receive( Server & this, StateMsg & msg ) { return Finished; }48 allocation receive( Server & this, IntMsg & msg ) { msg.val = 7; *cl << msg; return Nodelete; } 49 allocation receive( Server & this, CharMsg & msg ) { msg.val = 'x'; *cl << msg; return Nodelete; } 50 allocation receive( Server & this, StateMsg & msg ) { return Finished; } 51 51 52 52 void terminateServers( Client & this ) with(this) { … … 56 56 } 57 57 58 Allocation reset( Client & this ) with(this) {58 allocation reset( Client & this ) with(this) { 59 59 times += 1; 60 60 if ( times == Times ) { terminateServers( this ); return Finished; } … … 64 64 } 65 65 66 Allocation process( Client & this ) with(this) {66 allocation process( Client & this ) with(this) { 67 67 this.results++; 68 68 if ( results == 2 * Messages ) { return reset( this ); } … … 70 70 } 71 71 72 Allocation receive( Client & this, IntMsg & msg ) { return process( this ); }73 Allocation receive( Client & this, CharMsg & msg ) { return process( this ); }74 Allocation receive( Client & this, StateMsg & msg ) with(this) {72 allocation receive( Client & this, IntMsg & msg ) { return process( this ); } 73 allocation receive( Client & this, CharMsg & msg ) { return process( this ); } 74 allocation receive( Client & this, StateMsg & msg ) with(this) { 75 75 for ( i; Messages ) { 76 76 servers[i] << intmsg[i]; -
doc/theses/colby_parsons_MMAth/benchmarks/actors/cfa/static.cfa
r2b78949 r8a930c03 23 23 24 24 uint64_t start_time; 25 Allocation receive( derived_actor & receiver, derived_msg & msg ) {25 allocation receive( derived_actor & receiver, derived_msg & msg ) { 26 26 if ( msg.cnt >= Times ) { 27 27 printf("%.2f\n", ((double)(bench_time() - start_time)) / ((double)Times) ); // ns -
doc/theses/colby_parsons_MMAth/benchmarks/actors/plotData.py
r2b78949 r8a930c03 160 160 161 161 if currVariant == numVariants: 162 fig, ax = plt.subplots( )162 fig, ax = plt.subplots(layout='constrained') 163 163 plt.title(name + " Benchmark") 164 164 plt.ylabel("Runtime (seconds)") -
doc/theses/colby_parsons_MMAth/benchmarks/channels/plotData.py
r2b78949 r8a930c03 124 124 125 125 if currVariant == numVariants: 126 fig, ax = plt.subplots( )126 fig, ax = plt.subplots(layout='constrained') 127 127 plt.title(name + " Benchmark") 128 128 plt.ylabel("Throughput (channel operations)") -
doc/theses/colby_parsons_MMAth/benchmarks/mutex_stmt/plotData.py
r2b78949 r8a930c03 97 97 98 98 if currVariant == numVariants: 99 fig, ax = plt.subplots( )99 fig, ax = plt.subplots(layout='constrained') 100 100 plt.title(name + " Benchmark: " + str(currLocks) + " Locks") 101 101 plt.ylabel("Throughput (entries)") -
doc/theses/colby_parsons_MMAth/code/basic_actor_example.cfa
r2b78949 r8a930c03 19 19 } 20 20 21 Allocation receive( derived_actor & receiver, derived_msg & msg ) {21 allocation receive( derived_actor & receiver, derived_msg & msg ) { 22 22 printf("The message contained the string: %s\n", msg.word); 23 23 return Finished; // Return allocation status of Finished now that the actor is done work -
doc/theses/colby_parsons_MMAth/glossary.tex
r2b78949 r8a930c03 32 32 % Examples from template above 33 33 34 \newabbreviation{raii}{RAII}{ Resource Acquisition Is Initialization}35 \newabbreviation{rtti}{RTTI}{ Run-Time Type Information}36 \newabbreviation{fcfs}{FCFS}{ First Come First Served}37 \newabbreviation{toctou}{TOCTOU}{ time-of-check to time-of-use}34 \newabbreviation{raii}{RAII}{\Newterm{resource acquisition is initialization}} 35 \newabbreviation{rtti}{RTTI}{\Newterm{run-time type information}} 36 \newabbreviation{fcfs}{FCFS}{\Newterm{first-come first-served}} 37 \newabbreviation{toctou}{TOCTOU}{\Newterm{time-of-check to time-of-use}} 38 38 39 39 \newglossaryentry{actor} -
doc/theses/colby_parsons_MMAth/local.bib
r2b78949 r8a930c03 95 95 @misc{go:select, 96 96 author = "The Go Programming Language", 97 title = "src/runtime/ chan.go",97 title = "src/runtime/select.go", 98 98 howpublished = {\href{https://go.dev/src/runtime/select.go}}, 99 99 note = "[Online; accessed 23-May-2023]" 100 100 } 101 101 102 @misc{go:selectref, 103 author = "The Go Programming Language Specification", 104 title = "Select statements", 105 howpublished = {\href{https://go.dev/ref/spec#Select\_statements}}, 106 note = "[Online; accessed 23-May-2023]" 107 } 108 109 @misc{boost:channel, 110 author = "Boost C++ Libraries", 111 title = "experimental::basic\_concurrent\_channel", 112 howpublished = {\href{https://www.boost.org/doc/libs/master/doc/html/boost\_asio/reference/experimental\__basic\_concurrent\_channel.html}}, 113 note = "[Online; accessed 23-May-2023]" 114 } 115 116 @misc{rust:channel, 117 author = "The Rust Standard Library", 118 title = "std::sync::mpsc::sync\_channel", 119 howpublished = {\href{https://doc.rust-lang.org/std/sync/mpsc/fn.sync\_channel.html}}, 120 note = "[Online; accessed 23-May-2023]" 121 } 122 123 @misc{rust:select, 124 author = "The Rust Standard Library", 125 title = "Macro futures::select", 126 howpublished = {\href{https://docs.rs/futures/latest/futures/macro.select.html}}, 127 note = "[Online; accessed 23-May-2023]" 128 } 129 130 @misc{ocaml:channel, 131 author = "The OCaml Manual", 132 title = "OCaml library : Event", 133 howpublished = {\href{https://v2.ocaml.org/api/Event.html}}, 134 note = "[Online; accessed 23-May-2023]" 135 } 136 137 @misc{haskell:channel, 138 author = "The Haskell Package Repository", 139 title = "Control.Concurrent.Chan", 140 howpublished = {\href{https://hackage.haskell.org/package/base-4.18.0.0/docs/Control-Concurrent-Chan.html}}, 141 note = "[Online; accessed 23-May-2023]" 142 } 143 144 @misc{linux:select, 145 author = "Linux man pages", 146 title = "select(2) — Linux manual page", 147 howpublished = {\href{https://man7.org/linux/man-pages/man2/select.2.html}}, 148 note = "[Online; accessed 23-May-2023]" 149 } 150 151 @misc{linux:poll, 152 author = "Linux man pages", 153 title = "poll(2) — Linux manual page", 154 howpublished = {\href{https://man7.org/linux/man-pages/man2/poll.2.html}}, 155 note = "[Online; accessed 23-May-2023]" 156 } 157 158 @misc{linux:epoll, 159 author = "Linux man pages", 160 title = "epoll(7) — Linux manual page", 161 howpublished = {\href{https://man7.org/linux/man-pages/man7/epoll.7.html}}, 162 note = "[Online; accessed 23-May-2023]" 163 } 164 165 @article{Ichbiah79, 166 title={Preliminary Ada reference manual}, 167 author={Ichbiah, Jean D}, 168 journal={ACM Sigplan Notices}, 169 volume={14}, 170 number={6a}, 171 pages={1--145}, 172 year={1979}, 173 publisher={ACM New York, NY, USA} 174 } 175 176 @misc{cpp:whenany, 177 author = "C++ reference", 178 title = "std::experimental::when\_any", 179 howpublished = {\href{https://en.cppreference.com/w/cpp/experimental/when\_any}}, 180 note = "[Online; accessed 23-May-2023]" 181 } 182 183 184 -
doc/theses/colby_parsons_MMAth/style/style.tex
r2b78949 r8a930c03 15 15 \newsavebox{\myboxB} 16 16 17 \lstnewenvironment{Golang}[1][] 18 {\lstset{language=Go,literate={<-}{\makebox[2ex][c]{\textless\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}}2, 19 moredelim=**[is][\protect\color{red}]{@}{@}}\lstset{#1}} 20 {} 21 17 22 \lstnewenvironment{java}[1][] 18 23 {\lstset{language=java,moredelim=**[is][\protect\color{red}]{@}{@}}\lstset{#1}} -
doc/theses/colby_parsons_MMAth/text/channels.tex
r2b78949 r8a930c03 17 17 Additionally all channel operations in CSP are synchronous (no buffering). 18 18 Advanced channels as a programming language feature has been popularized in recent years by the language Go~\cite{Go}, which encourages the use of channels as its fundamental concurrent feature. 19 It was the popularity of Go channels that lead to their implement ion in \CFA.19 It was the popularity of Go channels that lead to their implementation in \CFA. 20 20 Neither Go nor \CFA channels have the restrictions of the early channel-based concurrent systems. 21 22 Other popular languages and libraries that provide channels include C++ Boost~\cite{boost:channel}, Rust~\cite{rust:channel}, Haskell~\cite{haskell:channel}, and OCaml~\cite{ocaml:channel}. 23 Boost channels only support asynchronous (non-blocking) operations, and Rust channels are limited to only having one consumer per channel. 24 Haskell channels are unbounded in size, and OCaml channels are zero-size. 25 These restrictions in Haskell and OCaml are likely due to their functional approach, which results in them both using a list as the underlying data structure for their channel. 26 These languages and libraries are not discussed further, as their channel implementation is not comparable to the bounded-buffer style channels present in Go and \CFA. 21 27 22 28 \section{Producer-Consumer Problem} … … 61 67 \section{Channel Implementation} 62 68 Currently, only the Go programming language provides user-level threading where the primary communication mechanism is channels. 63 Experiments were conducted that varied the producer-consumer problemalgorithm and lock type used inside the channel.69 Experiments were conducted that varied the producer-consumer algorithm and lock type used inside the channel. 64 70 With the exception of non-\gls{fcfs} or non-FIFO algorithms, no algorithm or lock usage in the channel implementation was found to be consistently more performant that Go's choice of algorithm and lock implementation. 65 71 Performance of channels can be improved by sharding the underlying buffer \cite{Dice11}. 66 In doing so the FIFO property is lost, which is undesireable for user-facing channels.72 However, the FIFO property is lost, which is undesirable for user-facing channels. 67 73 Therefore, the low-level channel implementation in \CFA is largely copied from the Go implementation, but adapted to the \CFA type and runtime systems. 68 74 As such the research contributions added by \CFA's channel implementation lie in the realm of safety and productivity features. 69 75 70 The Go channel implementation utilitizes cooperation between threads to achieve good performance~\cite{go:chan}. 71 The cooperation between threads only occurs when producers or consumers need to block due to the buffer being full or empty. 72 In these cases the blocking thread stores their relevant data in a shared location and the signalling thread will complete their operation before waking them. 73 This helps improve performance in a few ways. 74 First, each thread interacting with the channel with only acquire and release the internal channel lock exactly once. 75 This decreases contention on the internal lock, as only entering threads will compete for the lock since signalled threads never reacquire the lock. 76 The other advantage of the cooperation approach is that it eliminates the potential bottleneck of waiting for signalled threads. 77 The property of acquiring/releasing the lock only once can be achieved without cooperation by \Newterm{baton passing} the lock. 78 Baton passing is when one thread acquires a lock but does not release it, and instead signals a thread inside the critical section conceptually "passing" the mutual exclusion to the signalled thread. 79 While baton passing is useful in some algorithms, it results in worse performance than the cooperation approach in channel implementations since all entering threads then need to wait for the blocked thread to reach the front of the ready queue and run before other operations on the channel can proceed. 76 The Go channel implementation utilizes cooperation among threads to achieve good performance~\cite{go:chan}. 77 This cooperation only occurs when producers or consumers need to block due to the buffer being full or empty. 78 In these cases, a blocking thread stores their relevant data in a shared location and the signalling thread completes the blocking thread's operation before waking them; 79 \ie the blocking thread has no work to perform after it unblocks because the signalling threads has done this work. 80 This approach is similar to wait morphing for locks~\cite[p.~82]{Butenhof97} and improves performance in a few ways. 81 First, each thread interacting with the channel only acquires and releases the internal channel lock once. 82 As a result, contention on the internal lock is decreased, as only entering threads compete for the lock as unblocking threads do not reacquire the lock. 83 The other advantage of Go's wait-morphing approach is that it eliminates the bottleneck of waiting for signalled threads to run. 84 Note, the property of acquiring/releasing the lock only once can also be achieved with a different form of cooperation, called \Newterm{baton passing}. 85 Baton passing occurs when one thread acquires a lock but does not release it, and instead signals a thread inside the critical section, conceptually ``passing'' the mutual exclusion from the signalling thread to the signalled thread. 86 The baton-passing approach has threads cooperate to pass mutual exclusion without additional lock acquires or releases; 87 the wait-morphing approach has threads cooperate by completing the signalled thread's operation, thus removing a signalled thread's need for mutual exclusion after unblocking. 88 While baton passing is useful in some algorithms, it results in worse channel performance than the Go approach. 89 In the baton-passing approach, all threads need to wait for the signalled thread to reach the front of the ready queue, context switch, and run before other operations on the channel can proceed, since the signalled thread holds mutual exclusion; 90 in the wait-morphing approach, since the operation is completed before the signal, other threads can continue to operate on the channel without waiting for the signalled thread to run. 80 91 81 92 In this work, all channel sizes \see{Sections~\ref{s:ChannelSize}} are implemented with bounded buffers. … … 100 111 \subsection{Toggle-able Statistics} 101 112 As discussed, a channel is a concurrent layer over a bounded buffer. 102 To achieve efficient buffering users should aim for as few blocking operations on a channel as possible.103 Often to achieve this users maychange the buffer size, shard a channel into multiple channels, or tweak the number of producer and consumer threads.104 Fo users to be able to make informed decisions when tuning channel usage, toggle-able channel statistics are provided.105 The statistics are toggled at compile time via the @CHAN_STATS@ macro to ensure that they are entirely elided when not used.106 When statistics are turned on, four counters are maintained per channel, two for producers and two for consumers.113 To achieve efficient buffering, users should aim for as few blocking operations on a channel as possible. 114 Mechanisms to reduce blocking are: change the buffer size, shard a channel into multiple channels, or tweak the number of producer and consumer threads. 115 For users to be able to make informed decisions when tuning channel usage, toggle-able channel statistics are provided. 116 The statistics are toggled on during the \CFA build by defining the @CHAN_STATS@ macro, which guarantees zero cost when not using this feature. 117 When statistics are turned on, four counters are maintained per channel, two for inserting (producers) and two for removing (consumers). 107 118 The two counters per type of operation track the number of blocking operations and total operations. 108 In the channel destructor the counters are printed out aggregated and also per type of operation. 109 An example use case of the counters follows. 110 A user is buffering information between producer and consumer threads and wants to analyze channel performance. 111 Via the statistics they see that producers block for a large percentage of their operations while consumers do not block often. 112 They then can use this information to adjust their number of producers/consumers or channel size to achieve a larger percentage of non-blocking producer operations, thus increasing their channel throughput. 119 In the channel destructor, the counters are printed out aggregated and also per type of operation. 120 An example use case is noting that producer inserts are blocking often while consumer removes do not block often. 121 This information can be used to increase the number of consumers to decrease the blocking producer operations, thus increasing the channel throughput. 122 Whereas, increasing the channel size in this scenario is unlikely to produce a benefit because the consumers can never keep up with the producers. 113 123 114 124 \subsection{Deadlock Detection} 115 The deadlock detection in the \CFA channels is fairly basic. 116 It only detects the case where threads are blocked on the channel during deallocation. 117 This case is guaranteed to deadlock since the list holding the blocked thread is internal to the channel and will be deallocated. 118 If a user maintained a separate reference to a thread and unparked it outside the channel they could avoid the deadlock, but would run into other runtime errors since the thread would access channel data after waking that is now deallocated. 119 More robust deadlock detection surrounding channel usage would have to be implemented separate from the channel implementation since it would require knowledge about the threading system and other channel/thread state. 125 The deadlock detection in the \CFA channels is fairly basic but detects a very common channel mistake during termination. 126 That is, it detects the case where threads are blocked on the channel during channel deallocation. 127 This case is guaranteed to deadlock since there are no other threads to supply or consume values needed by the waiting threads. 128 Only if a user maintained a separate reference to the blocked threads and manually unblocks them outside the channel could the deadlock be avoid. 129 However, without special semantics, this unblocking would generate other runtime errors where the unblocked thread attempts to access non-existing channel data or even a deallocated channel. 130 More robust deadlock detection needs to be implemented separate from channels since it requires knowledge about the threading system and other channel/thread state. 120 131 121 132 \subsection{Program Shutdown} 122 133 Terminating concurrent programs is often one of the most difficult parts of writing concurrent code, particularly if graceful termination is needed. 123 The difficulty of graceful termination often arises from the usage ofsynchronization primitives that need to be handled carefully during shutdown.134 Graceful termination can be difficult to achieve with synchronization primitives that need to be handled carefully during shutdown. 124 135 It is easy to deadlock during termination if threads are left behind on synchronization primitives. 125 136 Additionally, most synchronization primitives are prone to \gls{toctou} issues where there is race between one thread checking the state of a concurrent object and another thread changing the state. 126 137 \gls{toctou} issues with synchronization primitives often involve a race between one thread checking the primitive for blocked threads and another thread blocking on it. 127 138 Channels are a particularly hard synchronization primitive to terminate since both sending and receiving to/from a channel can block. 128 Thus, improperly handled \gls{toctou} issues with channels often result in deadlocks as threads trying to perform the termination may end up unexpectedly blocking in their attempt to help other threads exit the system. 129 130 \paragraph{Go channels} provide a set of tools to help with concurrent shutdown~\cite{go:chan}. 131 Channels in Go have a @close@ operation and a \Go{select} statement that both can be used to help threads terminate. 139 Thus, improperly handled \gls{toctou} issues with channels often result in deadlocks as threads performing the termination may end up unexpectedly blocking in their attempt to help other threads exit the system. 140 141 \paragraph{Go channels} provide a set of tools to help with concurrent shutdown~\cite{go:chan} using a @close@ operation in conjunction with the \Go{select} statement. 132 142 The \Go{select} statement is discussed in \ref{s:waituntil}, where \CFA's @waituntil@ statement is compared with the Go \Go{select} statement. 133 143 … … 143 153 Note, panics in Go can be caught, but it is not the idiomatic way to write Go programs. 144 154 145 While Go's channel closing semantics are powerful enough to perform any concurrent termination needed by a program, their lack of ease of use leaves much to be desired.155 While Go's channel-closing semantics are powerful enough to perform any concurrent termination needed by a program, their lack of ease of use leaves much to be desired. 146 156 Since both closing and sending panic once a channel is closed, a user often has to synchronize the senders (producers) before the channel can be closed to avoid panics. 147 157 However, in doing so it renders the @close@ operation nearly useless, as the only utilities it provides are the ability to ensure receivers no longer block on the channel and receive zero-valued elements. 148 158 This functionality is only useful if the zero-typed element is recognized as a sentinel value, but if another sentinel value is necessary, then @close@ only provides the non-blocking feature. 149 159 To avoid \gls{toctou} issues during shutdown, a busy wait with a \Go{select} statement is often used to add or remove elements from a channel. 150 Due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown.160 Hence, due to Go's asymmetric approach to channel shutdown, separate synchronization between producers and consumers of a channel has to occur during shutdown. 151 161 152 162 \paragraph{\CFA channels} have access to an extensive exception handling mechanism~\cite{Beach21}. … … 161 171 When a channel in \CFA is closed, all subsequent calls to the channel raise a resumption exception at the caller. 162 172 If the resumption is handled, the caller attempts to complete the channel operation. 163 However, if channel operation would block, a termination exception is thrown.173 However, if the channel operation would block, a termination exception is thrown. 164 174 If the resumption is not handled, the exception is rethrown as a termination. 165 175 These termination exceptions allow for non-local transfer that is used to great effect to eagerly and gracefully shut down a thread. 166 176 When a channel is closed, if there are any blocked producers or consumers inside the channel, they are woken up and also have a resumption thrown at them. 167 The resumption exception, @channel_closed@, has a couple fields to aid in handling the exception. 168 The exception contains a pointer to the channel it was thrown from, and a pointer to an element. 169 In exceptions thrown from remove the element pointer will be null. 170 In the case of insert the element pointer points to the element that the thread attempted to insert. 177 The resumption exception, @channel_closed@, has internal fields to aid in handling the exception. 178 The exception contains a pointer to the channel it is thrown from and a pointer to a buffer element. 179 For exceptions thrown from @remove@, the buffer element pointer is null. 180 For exceptions thrown from @insert@, the element pointer points to the buffer element that the thread attempted to insert. 181 Utility routines @bool is_insert( channel_closed & e );@ and @bool is_remove( channel_closed & e );@ are provided for convenient checking of the element pointer. 171 182 This element pointer allows the handler to know which operation failed and also allows the element to not be lost on a failed insert since it can be moved elsewhere in the handler. 172 Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based which channel and operation failed. 173 Exception handlers in \CFA have an optional predicate after the exception type which can be used to optionally trigger or skip handlers based on the content of an exception. 174 It is worth mentioning that the approach of exceptions for termination may incur a larger performance cost during termination that the approach used in Go. 175 This should not be an issue, since termination is rarely an fast-path of an application and ensuring that termination can be implemented correctly with ease is the aim of the exception approach. 183 Furthermore, due to \CFA's powerful exception system, this data can be used to choose handlers based on which channel and operation failed. 184 For example, exception handlers in \CFA have an optional predicate which can be used to trigger or skip handlers based on the content of the matching exception. 185 It is worth mentioning that using exceptions for termination may incur a larger performance cost than the Go approach. 186 However, this should not be an issue, since termination is rarely on the fast-path of an application. 187 In contrast, ensuring termination can be easily implemented correctly is the aim of the exception approach. 176 188 177 189 \section{\CFA / Go channel Examples} 178 To highlight the differences between \CFA's and Go's close semantics, three examples will be presented.190 To highlight the differences between \CFA's and Go's close semantics, three examples are presented. 179 191 The first example is a simple shutdown case, where there are producer threads and consumer threads operating on a channel for a fixed duration. 180 Once the duration ends, producers and consumers terminate without worrying about any leftover values in the channel.181 The second example extends the first example by requiring the channel to be empty uponshutdown.192 Once the duration ends, producers and consumers terminate immediately leaving unprocessed elements in the channel. 193 The second example extends the first by requiring the channel to be empty after shutdown. 182 194 Both the first and second example are shown in Figure~\ref{f:ChannelTermination}. 183 184 185 First the Go solutions to these examples shown in Figure~\ref{l:go_chan_term} are discussed.186 Since some of the elements being passed through the channel are zero-valued, closing the channel in Go does not aid in communicating shutdown.187 Instead, a different mechanism to communicate with the consumers and producers needs to be used.188 This use of an additional flag or communication method is common in Go channel shutdown code, since to avoid panics on a channel, the shutdown of a channel often has to be communicated with threads before it occurs.189 In this example, a flag is used to communicate with producers and another flag is used for consumers.190 Producers and consumers need separate avenues of communication both so that producers terminate before the channel is closed to avoid panicking, and to avoid the case where all the consumers terminate first, which can result in a deadlock for producers if the channel is full.191 The producer flag is set first, then after producers terminate the consumer flag is set and the channel is closed.192 In the second example where all values need to be consumed, the main thread iterates over the closed channel to process any remaining values.193 194 195 In the \CFA solutions in Figure~\ref{l:cfa_chan_term}, shutdown is communicated directly to both producers and consumers via the @close@ call.196 In the first example where all values do not need to be consumed, both producers and consumers do not handle the resumption and finish once they receive the termination exception.197 The second \CFA example where all values must be consumed highlights how resumption is used with channel shutdown.198 The @Producer@ thread-main knows to stop producing when the @insert@ call on a closed channel raises exception @channel_closed@.199 The @Consumer@ thread-main knows to stop consuming after all elements of a closed channel are removed and the call to @remove@ would block.200 Hence, the consumer knows the moment the channel closes because a resumption exception is raised, caught, and ignored, and then control returns to @remove@ to return another item from the buffer.201 Only when the buffer is drained and the call to @remove@ would block, a termination exception is raised to stop consuming.202 The \CFA semantics allow users to communicate channel shutdown directly through the channel, without having to share extra state between threads.203 Additionally, when the channel needs to be drained, \CFA provides users with easy options for processing the leftover channel values in the main thread or in the consumer threads.204 If one wishes to consume the leftover values in the consumer threads in Go, extra synchronization between the main thread and the consumer threads is needed.205 195 206 196 \begin{figure} … … 208 198 209 199 \begin{lrbox}{\myboxA} 200 \begin{Golang}[aboveskip=0pt,belowskip=0pt] 201 var channel chan int = make( chan int, 128 ) 202 var prodJoin chan int = make( chan int, 4 ) 203 var consJoin chan int = make( chan int, 4 ) 204 var cons_done, prod_done bool = false, false; 205 func producer() { 206 for { 207 if prod_done { break } 208 channel <- 5 209 } 210 prodJoin <- 0 // synch with main thd 211 } 212 213 func consumer() { 214 for { 215 if cons_done { break } 216 <- channel 217 } 218 consJoin <- 0 // synch with main thd 219 } 220 221 222 func main() { 223 for j := 0; j < 4; j++ { go consumer() } 224 for j := 0; j < 4; j++ { go producer() } 225 time.Sleep( time.Second * 10 ) 226 prod_done = true 227 for j := 0; j < 4 ; j++ { <- prodJoin } 228 cons_done = true 229 close(channel) // ensure no cons deadlock 230 @for elem := range channel {@ 231 // process leftover values 232 @}@ 233 for j := 0; j < 4; j++ { <- consJoin } 234 } 235 \end{Golang} 236 \end{lrbox} 237 238 \begin{lrbox}{\myboxB} 210 239 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 211 channel( size_t ) Channel{ ChannelSize }; 212 240 channel( size_t ) chan{ 128 }; 213 241 thread Consumer {}; 242 thread Producer {}; 243 244 void main( Producer & this ) { 245 try { 246 for () 247 insert( chan, 5 ); 248 } catch( channel_closed * ) { 249 // unhandled resume or full 250 } 251 } 214 252 void main( Consumer & this ) { 215 try { 216 for ( ;; ) 217 remove( Channel ); 218 @} catchResume( channel_closed * ) { @ 219 // handled resume => consume from chan 220 } catch( channel_closed * ) { 221 // empty or unhandled resume 222 } 223 } 224 225 thread Producer {}; 226 void main( Producer & this ) { 227 size_t count = 0; 228 try { 229 for ( ;; ) 230 insert( Channel, count++ ); 231 } catch ( channel_closed * ) { 232 // unhandled resume or full 233 } 234 } 235 236 int main( int argc, char * argv[] ) { 237 Consumer c[Consumers]; 238 Producer p[Producers]; 239 sleep(Duration`s); 240 close( Channel ); 241 return 0; 242 } 253 try { 254 for () { int i = remove( chan ); } 255 @} catchResume( channel_closed * ) {@ 256 // handled resume => consume from chan 257 } catch( channel_closed * ) { 258 // empty or unhandled resume 259 } 260 } 261 int main() { 262 Consumer c[4]; 263 Producer p[4]; 264 sleep( 10`s ); 265 close( chan ); 266 } 267 268 269 270 271 272 273 243 274 \end{cfa} 244 275 \end{lrbox} 245 276 246 \begin{lrbox}{\myboxB} 247 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 248 var cons_done, prod_done bool = false, false; 249 var prodJoin chan int = make(chan int, Producers) 250 var consJoin chan int = make(chan int, Consumers) 251 252 func consumer( channel chan uint64 ) { 253 for { 254 if cons_done { break } 255 <-channel 256 } 257 consJoin <- 0 // synch with main thd 258 } 259 260 func producer( channel chan uint64 ) { 261 var count uint64 = 0 262 for { 263 if prod_done { break } 264 channel <- count++ 265 } 266 prodJoin <- 0 // synch with main thd 267 } 268 269 func main() { 270 channel = make(chan uint64, ChannelSize) 271 for j := 0; j < Consumers; j++ { 272 go consumer( channel ) 273 } 274 for j := 0; j < Producers; j++ { 275 go producer( channel ) 276 } 277 time.Sleep(time.Second * Duration) 278 prod_done = true 279 for j := 0; j < Producers ; j++ { 280 <-prodJoin // wait for prods 281 } 282 cons_done = true 283 close(channel) // ensure no cons deadlock 284 @for elem := range channel { @ 285 // process leftover values 286 @}@ 287 for j := 0; j < Consumers; j++{ 288 <-consJoin // wait for cons 289 } 290 } 291 \end{cfa} 292 \end{lrbox} 293 294 \subfloat[\CFA style]{\label{l:cfa_chan_term}\usebox\myboxA} 277 \subfloat[Go style]{\label{l:go_chan_term}\usebox\myboxA} 295 278 \hspace*{3pt} 296 279 \vrule 297 280 \hspace*{3pt} 298 \subfloat[ Go style]{\label{l:go_chan_term}\usebox\myboxB}281 \subfloat[\CFA style]{\label{l:cfa_chan_term}\usebox\myboxB} 299 282 \caption{Channel Termination Examples 1 and 2. Code specific to example 2 is highlighted.} 300 283 \label{f:ChannelTermination} 301 284 \end{figure} 302 285 303 The final shutdown example uses channels to implement a barrier. 304 It is shown in Figure~\ref{f:ChannelBarrierTermination}. 305 The problem of implementing a barrier is chosen since threads are both producers and consumers on the barrier-internal channels, which removes the ability to easily synchronize producers before consumers during shutdown. 306 As such, while the shutdown details will be discussed with this problem in mind, they are also applicable to other problems taht have individual threads both producing and consuming from channels. 307 Both of these examples are implemented using \CFA syntax so that they can be easily compared. 308 Figure~\ref{l:cfa_chan_bar} uses \CFA-style channel close semantics and Figure~\ref{l:go_chan_bar} uses Go-style close semantics. 309 In this example it is infeasible to use the Go @close@ call since all threads are both potentially producers and consumers, causing panics on close to be unavoidable without complex synchronization. 310 As such in Figure~\ref{l:go_chan_bar} to implement a flush routine for the buffer, a sentinel value of @-1@ has to be used to indicate to threads that they need to leave the barrier. 311 This sentinel value has to be checked at two points. 286 Figure~\ref{l:go_chan_term} shows the Go solution. 287 Since some of the elements being passed through the channel are zero-valued, closing the channel in Go does not aid in communicating shutdown. 288 Instead, a different mechanism to communicate with the consumers and producers needs to be used. 289 Flag variables are common in Go-channel shutdown-code to avoid panics on a channel, meaning the channel shutdown has to be communicated with threads before it occurs. 290 Hence, the two flags @cons_done@ and @prod_done@ are used to communicate with the producers and consumers, respectively. 291 Furthermore, producers and consumers need to shutdown separately to ensure that producers terminate before the channel is closed to avoid panicking, and to avoid the case where all the consumers terminate first, which can result in a deadlock for producers if the channel is full. 292 The producer flag is set first; 293 then after all producers terminate, the consumer flag is set and the channel is closed leaving elements in the buffer. 294 To purge the buffer, a loop is added (red) that iterates over the closed channel to process any remaining values. 295 296 Figure~\ref{l:cfa_chan_term} shows the \CFA solution. 297 Here, shutdown is communicated directly to both producers and consumers via the @close@ call. 298 A @Producer@ thread knows to stop producing when the @insert@ call on a closed channel raises exception @channel_closed@. 299 If a @Consumer@ thread ignores the first resumption exception from the @close@, the exception is reraised as a termination exception and elements are left in the buffer. 300 If a @Consumer@ thread handles the resumptions exceptions (red), control returns to complete the remove. 301 A @Consumer@ thread knows to stop consuming after all elements of a closed channel are removed and the consumer would block, which causes a termination raise of @channel_closed@. 302 The \CFA semantics allow users to communicate channel shutdown directly through the channel, without having to share extra state between threads. 303 Additionally, when the channel needs to be drained, \CFA provides users with easy options for processing the leftover channel values in the main thread or in the consumer threads. 304 305 Figure~\ref{f:ChannelBarrierTermination} shows a final shutdown example using channels to implement a barrier. 306 A Go and \CFA style solution are presented but both are implemented using \CFA syntax so they can be easily compared. 307 Implementing a barrier is interesting because threads are both producers and consumers on the barrier-internal channels, @entryWait@ and @barWait@. 308 The outline for the barrier implementation starts by initially filling the @entryWait@ channel with $N$ tickets in the barrier constructor, allowing $N$ arriving threads to remove these values and enter the barrier. 309 After @entryWait@ is empty, arriving threads block when removing. 310 However, the arriving threads that entered the barrier cannot leave the barrier until $N$ threads have arrived. 311 Hence, the entering threads block on the empty @barWait@ channel until the $N$th arriving thread inserts $N-1$ elements into @barWait@ to unblock the $N-1$ threads calling @remove@. 312 The race between these arriving threads blocking on @barWait@ and the $N$th thread inserting values into @barWait@ does not affect correctness; 313 \ie an arriving thread may or may not block on channel @barWait@ to get its value. 314 Finally, the last thread to remove from @barWait@ with ticket $N-2$, refills channel @entryWait@ with $N$ values to start the next group into the barrier. 315 316 Now, the two channels makes termination synchronization between producers and consumers difficult. 317 Interestingly, the shutdown details for this problem are also applicable to other problems with threads producing and consuming from the same channel. 318 The Go-style solution cannot use the Go @close@ call since all threads are both potentially producers and consumers, causing panics on close to be unavoidable without complex synchronization. 319 As such in Figure \ref{l:go_chan_bar}, a flush routine is needed to insert a sentinel value, @-1@, to inform threads waiting in the buffer they need to leave the barrier. 320 This sentinel value has to be checked at two points along the fast-path and sentinel values daisy-chained into the buffers. 312 321 Furthermore, an additional flag @done@ is needed to communicate to threads once they have left the barrier that they are done. 313 314 In the \CFA version~\ref{l:cfa_chan_bar}, the barrier shutdown results in an exception being thrown at threads operating on it, which informs the threads that they must terminate.322 Also note that in the Go version~\ref{l:go_chan_bar}, the size of the barrier channels has to be larger than in the \CFA version to ensure that the main thread does not block when attempting to clear the barrier. 323 For The \CFA solution~\ref{l:cfa_chan_bar}, the barrier shutdown results in an exception being thrown at threads operating on it, to inform waiting threads they must leave the barrier. 315 324 This avoids the need to use a separate communication method other than the barrier, and avoids extra conditional checks on the fast path of the barrier implementation. 316 Also note that in the Go version~\ref{l:go_chan_bar}, the size of the barrier channels has to be larger than in the \CFA version to ensure that the main thread does not block when attempting to clear the barrier.317 325 318 326 \begin{figure} … … 320 328 321 329 \begin{lrbox}{\myboxA} 330 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 331 struct barrier { 332 channel( int ) barWait, entryWait; 333 int size; 334 }; 335 void ?{}( barrier & this, int size ) with(this) { 336 barWait{size + 1}; entryWait{size + 1}; 337 this.size = size; 338 for ( i; size ) 339 insert( entryWait, i ); 340 } 341 void wait( barrier & this ) with(this) { 342 int ticket = remove( entryWait ); 343 @if ( ticket == -1 ) { insert( entryWait, -1 ); return; }@ 344 if ( ticket == size - 1 ) { 345 for ( i; size - 1 ) 346 insert( barWait, i ); 347 return; 348 } 349 ticket = remove( barWait ); 350 @if ( ticket == -1 ) { insert( barWait, -1 ); return; }@ 351 if ( size == 1 || ticket == size - 2 ) { // last ? 352 for ( i; size ) 353 insert( entryWait, i ); 354 } 355 } 356 void flush(barrier & this) with(this) { 357 @insert( entryWait, -1 ); insert( barWait, -1 );@ 358 } 359 enum { Threads = 4 }; 360 barrier b{Threads}; 361 @bool done = false;@ 362 thread Thread {}; 363 void main( Thread & this ) { 364 for () { 365 @if ( done ) break;@ 366 wait( b ); 367 } 368 } 369 int main() { 370 Thread t[Threads]; 371 sleep(10`s); 372 done = true; 373 flush( b ); 374 } // wait for threads to terminate 375 \end{cfa} 376 \end{lrbox} 377 378 \begin{lrbox}{\myboxB} 322 379 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 323 380 struct barrier { … … 368 425 \end{lrbox} 369 426 370 \begin{lrbox}{\myboxB} 371 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 372 struct barrier { 373 channel( int ) barWait, entryWait; 374 int size; 375 }; 376 void ?{}( barrier & this, int size ) with(this) { 377 barWait{size + 1}; entryWait{size + 1}; 378 this.size = size; 379 for ( i; size ) 380 insert( entryWait, i ); 381 } 382 void wait( barrier & this ) with(this) { 383 int ticket = remove( entryWait ); 384 @if ( ticket == -1 ) { insert( entryWait, -1 ); return; }@ 385 if ( ticket == size - 1 ) { 386 for ( i; size - 1 ) 387 insert( barWait, i ); 388 return; 389 } 390 ticket = remove( barWait ); 391 @if ( ticket == -1 ) { insert( barWait, -1 ); return; }@ 392 if ( size == 1 || ticket == size - 2 ) { // last ? 393 for ( i; size ) 394 insert( entryWait, i ); 395 } 396 } 397 void flush(barrier & this) with(this) { 398 @insert( entryWait, -1 ); insert( barWait, -1 );@ 399 } 400 enum { Threads = 4 }; 401 barrier b{Threads}; 402 @bool done = false;@ 403 thread Thread {}; 404 void main( Thread & this ) { 405 for () { 406 @if ( done ) break;@ 407 wait( b ); 408 } 409 } 410 int main() { 411 Thread t[Threads]; 412 sleep(10`s); 413 done = true; 414 flush( b ); 415 } // wait for threads to terminate 416 \end{cfa} 417 \end{lrbox} 418 419 \subfloat[\CFA style]{\label{l:cfa_chan_bar}\usebox\myboxA} 427 \subfloat[Go style]{\label{l:go_chan_bar}\usebox\myboxA} 420 428 \hspace*{3pt} 421 429 \vrule 422 430 \hspace*{3pt} 423 \subfloat[ Go style]{\label{l:go_chan_bar}\usebox\myboxB}431 \subfloat[\CFA style]{\label{l:cfa_chan_bar}\usebox\myboxB} 424 432 \caption{Channel Barrier Termination} 425 433 \label{f:ChannelBarrierTermination} -
doc/theses/colby_parsons_MMAth/text/waituntil.tex
r2b78949 r8a930c03 14 14 The ability to wait for the first stall available without spinning can be done with concurrent tools that provide \gls{synch_multiplex}, the ability to wait synchronously for a resource or set of resources. 15 15 16 % C_TODO: fill in citations in following section17 16 \section{History of Synchronous Multiplexing} 18 17 There is a history of tools that provide \gls{synch_multiplex}. 19 Some of the most well known include the set o r unix system utilities signal(2)\cite{}, poll(2)\cite{}, and epoll(7)\cite{}, and the select statement provided by Go\cite{}.18 Some of the most well known include the set of unix system utilities: select(2)\cite{linux:select}, poll(2)\cite{linux:poll}, and epoll(7)\cite{linux:epoll}, and the select statement provided by Go\cite{go:selectref}. 20 19 21 20 Before one can examine the history of \gls{synch_multiplex} implementations in detail, the preceding theory must be discussed. … … 27 26 If a guard is false then the resource it guards is considered to not be in the set of resources being waited on. 28 27 Guards can be simulated using if statements, but to do so requires \[2^N\] if cases, where @N@ is the number of guards. 29 This transformation from guards to if statements will be discussed further in Section~\ref{}. % C_TODO: fill ref when writing semantics section later 28 The equivalence between guards and exponential if statements comes from an Occam ALT statement rule~\cite{Roscoe88}, which is presented in \CFA syntax in Figure~\ref{f:wu_if}. 29 Providing guards allows for easy toggling of waituntil clauses without introducing repeated code. 30 31 \begin{figure} 32 \begin{cfa} 33 when( predicate ) waituntil( A ) {} 34 or waituntil( B ) {} 35 // === 36 if ( predicate ) { 37 waituntil( A ) {} 38 or waituntil( B ) {} 39 } else { 40 waituntil( B ) {} 41 } 42 \end{cfa} 43 \caption{Occam's guard to if statement equivalence shown in \CFA syntax.} 44 \label{f:wu_if} 45 \end{figure} 30 46 31 47 Switching to implementations, it is important to discuss the resources being multiplexed. … … 44 60 It is worth noting these \gls{synch_multiplex} tools mentioned so far interact directly with the operating system and are often used to communicate between processes. 45 61 Later \gls{synch_multiplex} started to appear in user-space to support fast multiplexed concurrent communication between threads. 46 An early example of \gls{synch_multiplex} is the select statement in Ada .62 An early example of \gls{synch_multiplex} is the select statement in Ada~\cite[\S~9.7]{Ichbiah79}. 47 63 The select statement in Ada allows a task to multiplex over some subset of its own methods that it would like to @accept@ calls to. 48 64 Tasks in Ada can be thought of as threads which are an object of a specific class, and as such have methods, fields, etc. … … 53 69 The @else@ changes the synchronous multiplexing to asynchronous multiplexing. 54 70 If an @else@ clause is in a select statement and no calls to the @accept@ed methods are immediately available the code block associated with the @else@ is run and the task does not block. 55 The most popular example of user-space \gls{synch_multiplex} is Go with their select statement. 71 72 A popular example of user-space \gls{synch_multiplex} is Go with their select statement~\cite{go:selectref}. 56 73 Go's select statement operates on channels and has the same exclusive-or semantics as the ALT primitive from Occam, and has associated code blocks for each clause like ALT and Ada. 57 74 However, unlike Ada and ALT, Go does not provide any guards for their select statement cases. 58 75 Go provides a timeout utility and also provides a @default@ clause which has the same semantics as Ada's @else@ clause. 76 77 \uC provides \gls{synch_multiplex} over futures with their @_Select@ statement and Ada-style \gls{synch_multiplex} over monitor methods with their @_Accept@ statement~\cite{uC++}. 78 Their @_Accept@ statement builds upon the select statement offered by Ada, by offering both @and@ and @or@ semantics, which can be used together in the same statement. 79 These semantics are also supported for \uC's @_Select@ statement. 80 This enables fully expressive \gls{synch_multiplex} predicates. 81 82 There are many other languages that provide \gls{synch_multiplex}, including Rust's @select!@ over futures~\cite{rust:select}, OCaml's @select@ over channels~\cite{ocaml:channe}, and C++14's @when_any@ over futures~\cite{cpp:whenany}. 83 Note that while C++14 and Rust provide \gls{synch_multiplex}, their implemetations leave much to be desired as they both rely on busy-waiting polling to wait on multiple resources. 59 84 60 85 \section{Other Approaches to Synchronous Multiplexing} … … 69 94 If the requests for the other resources need to be retracted, the burden falls on the programmer to determine how to synchronize appropriately to ensure that only one resource is delivered. 70 95 71 72 96 \section{\CFA's Waituntil Statement} 73 74 75 97 The new \CFA \gls{synch_multiplex} utility introduced in this work is the @waituntil@ statement. 98 There is a @waitfor@ statement in \CFA that supports Ada-style \gls{synch_multiplex} over monitor methods, so this @waituntil@ focuses on synchronizing over other resources. 99 All of the \gls{synch_multiplex} features mentioned so far are monomorphic, only supporting one resource to wait on, select(2) supports file descriptors, Go's select supports channel operations, \uC's select supports futures, and Ada's select supports monitor method calls. 100 The waituntil statement in \CFA is polymorphic and provides \gls{synch_multiplex} over any objects that satisfy the trait in Figure~\ref{f:wu_trait}. 101 102 \begin{figure} 103 \begin{cfa} 104 forall(T & | sized(T)) 105 trait is_selectable { 106 // For registering a waituntil stmt on a selectable type 107 bool register_select( T &, select_node & ); 108 109 // For unregistering a waituntil stmt from a selectable type 110 bool unregister_select( T &, select_node & ); 111 112 // on_selected is run on the selecting thread prior to executing the statement associated with the select_node 113 void on_selected( T &, select_node & ); 114 }; 115 \end{cfa} 116 \caption{Trait for types that can be passed into \CFA's waituntil statement.} 117 \label{f:wu_trait} 118 \end{figure} 119 120 Currently locks, channels, futures and timeouts are supported by the waituntil statement, but this will be expanded as other use cases arise. 121 The waituntil statement supports guarded clauses, like Ada, and Occam, supports both @or@, and @and@ semantics, like \uC, and provides an @else@ for asynchronous multiplexing. An example of \CFA waituntil usage is shown in Figure~\ref{f:wu_example}. In Figure~\ref{f:wu_example} the waituntil statement is waiting for either @Lock@ to be available or for a value to be read from @Channel@ into @i@ and for @Future@ to be fulfilled. The semantics of the waituntil statement will be discussed in detail in the next section. 122 123 \begin{figure} 124 \begin{cfa} 125 future(int) Future; 126 channel(int) Channel; 127 owner_lock Lock; 128 int i = 0; 129 130 waituntil( Lock ) { ... } 131 or when( i == 0 ) waituntil( i << Channel ) { ... } 132 and waituntil( Future ) { ... } 133 \end{cfa} 134 \caption{Example of \CFA's waituntil statement} 135 \label{f:wu_example} 136 \end{figure} 137 138 \section{Waituntil Semantics} 139 There are two parts of the waituntil semantics to discuss, the semantics of the statement itself, \ie @and@, @or@, @when@ guards, and @else@ semantics, and the semantics of how the waituntil interacts with types like channels, locks and futures. 140 To start, the semantics of the statement itself will be discussed. 141 142 \subsection{Waituntil Statement Semantics} 143 The @or@ semantics are the most straightforward and nearly match those laid out in the ALT statement from Occam, the clauses have an exclusive-or relationship where the first one to be available will be run and only one clause is run. 144 \CFA's @or@ semantics differ from ALT semantics in one respect, instead of randomly picking a clause when multiple are available, the clause that appears first in the order of clauses will be picked. 145 \eg in the following example, if @foo@ and @bar@ are both available, @foo@ will always be selected since it comes first in the order of waituntil clauses. 146 \begin{cfa} 147 future(int) bar; 148 future(int) foo; 149 waituntil( foo ) { ... } 150 or waituntil( bar ) { ... } 151 \end{cfa} 152 153 The @and@ semantics match the @and@ semantics used by \uC. 154 When multiple clauses are joined by @and@, the waituntil will make a thread wait for all to be available, but will run the corresponding code blocks \emph{as they become available}. 155 As @and@ clauses are made available, the thread will be woken to run those clauses' code blocks and then the thread will wait again until all clauses have been run. 156 This allows work to be done in parallel while synchronizing over a set of resources, and furthermore gives a good reason to use the @and@ operator. 157 If the @and@ operator waited for all clauses to be available before running, it would not provide much more use that just acquiring those resources one by one in subsequent lines of code. 158 The @and@ operator binds more tightly than the @or@ operator. 159 To give an @or@ operator higher precedence brackets can be used. 160 \eg the following waituntil unconditionally waits for @C@ and one of either @A@ or @B@, since the @or@ is given higher precendence via brackets. 161 \begin{cfa} 162 (waituntil( A ) { ... } 163 or waituntil( B ) { ... } ) 164 and waituntil( C ) { ... } 165 \end{cfa} 166 167 The guards in the waituntil statement are called @when@ clauses. 168 The @when@ clause is passed a boolean expression. 169 All the @when@ boolean expressions are evaluated before the waituntil statement is run. 170 The guards in Occam's ALT effectively toggle clauses on and off, where a clause will only be evaluated and waited on if the corresponding guard is @true@. 171 The guards in the waituntil statement operate the same way, but require some nuance since both @and@ and @or@ operators are supported. 172 When a guard is false and a clause is removed, it can be thought of as removing that clause and its preceding operator from the statement. 173 \eg in the following example the two waituntil statements are semantically the same. 174 \begin{cfa} 175 when(true) waituntil( A ) { ... } 176 or when(false) waituntil( B ) { ... } 177 and waituntil( C ) { ... } 178 // === 179 waituntil( A ) { ... } 180 and waituntil( C ) { ... } 181 \end{cfa} 182 183 The @else@ clause on the waituntil has identical semantics to the @else@ clause in Ada. 184 If all resources are not immediately available and there is an @else@ clause, the @else@ clause is run and the thread will not block. 185 186 \subsection{Waituntil Type Semantics} 187 As described earlier, to support interaction with the waituntil statement a type must support the trait shown in Figure~\ref{f:wu_trait}. 188 The waituntil statement expects types to register and unregister themselves via calls to @register_select@ and @unregister_select@ respectively. 189 When a resource becomes available, @on_selected@ is run. 190 Many types may not need @on_selected@, but it is provided since some types may need to check and set things before the resource can be accessed in the code block. 191 The register/unregister routines in the trait return booleans. 192 The return value of @register_select@ is @true@ if the resource is immediately available, and @false@ otherwise. 193 The return value of @unregister_select@ is @true@ if the corresponding code block should be run after unregistration and @false@ otherwise. 194 The routine @on_selected@, and the return value of @unregister_select@ were needed to support channels as a resource. 195 More detail on channels and their interaction with waituntil will be discussed in Section~\ref{s:wu_chans}. 196 197 \section{Waituntil Implementation} 198 The waituntil statement is not inherently complex, and can be described as a few steps. 199 The complexity of the statement comes from the consideration of race conditions and synchronization needed when supporting various primitives. 200 The basic steps that the waituntil statement follows are the following. 201 202 First the waituntil statement creates a @select_node@ per resource that is being waited on. 203 The @select_node@ is an object that stores the waituntil data pertaining to one of the resources. 204 Then, each @select_node@ is then registered with the corresponding resource. 205 The thread executing the waituntil then enters a loop that will loop until the entire waituntil statement being satisfied. 206 In each iteration of the loop the thread attempts to block. 207 If any clauses are satified the block will fail and the thread will proceed, otherwise the block succeeds. 208 After proceeding past the block all clauses are checked for completion and the completed clauses have their code blocks run. 209 Once the thread escapes the loop, the @select_nodes@ are unregistered from the resources. 210 In the case where the block suceeds, the thread will be woken by the thread that marks one of the resources as available. 211 Pseudocode detailing these steps is presented in the following code block. 212 213 \begin{cfa} 214 select_nodes s[N]; // N select nodes 215 for ( node in s ) 216 register_select( resource, node ); 217 while( statement not satisfied ) { 218 // try to block 219 for ( resource in waituntil statement ) 220 if ( resource is avail ) run code block 221 } 222 for ( node in s ) 223 unregister_select( resource, node ); 224 \end{cfa} 225 226 These steps give a basic, but mildly inaccurate overview of how the statement works. 227 Digging into some parts of the implementation will shed light on more of the specifics and provide some accuracy. 228 229 \subsection{Locks} 230 Locks are one of the resources supported in the waituntil statement. 231 When a thread waits on multiple locks via a waituntil, it enqueues a @select_node@ in each of the lock's waiting queues. 232 When a @select_node@ reaches the front of the queue and gains ownership of a lock, the blocked thread is notified. 233 The lock will be held until the node is unregistered. 234 To prevent the waiting thread from holding many locks at once and potentially introducing a deadlock, the node is unregistered right after the corresponding code block is executed. 235 This prevents deadlocks since the waiting thread will never hold a lock while waiting on another resource. 236 As such the only nodes unregistered at the end are the ones that have not run. 237 238 \subsection{Timeouts} 239 Timeouts in the waituntil take the form of a duration being passed to a @sleep@ or @timeout@ call. 240 An example is shown in the following code. 241 242 \begin{cfa} 243 waituntil( sleep( 1`ms ) ) {} 244 waituntil( timeout( 1`s ) ) {} or waituntil( timeout( 2`s ) ) {} 245 waituntil( timeout( 1`ns ) ) {} and waituntil( timeout( 2`s ) ) {} 246 \end{cfa} 247 248 The timeout implementation highlights a key part of the waituntil semantics, the expression is evaluated before the waituntil runs. 249 As such calls to @sleep@ and @timeout@ do not block, but instead return a type that supports the @is_selectable@ trait. 250 This mechanism is needed for types that want to support multiple operations such as channels that support reading and writing. 251 252 \subsection{Channels}\label{s:wu_chans} 253 To support both waiting on both reading and writing to channels, the opperators @?<<?@ and @?>>?@ are used to show reading and writing to a channel respectively, where the lefthand operand is the value and the righthand operand is the channel. 254 Channels require significant complexity to wait on for a few reasons. 255 The first reason is that reading or writing to a channel is a mutating operation. 256 What this means is that if a read or write to a channel occurs, the state of the channel has changed. 257 In comparison, for standard locks and futures, if a lock is acquired then released or a future is ready but not accessed, the states of the lock and the future are not modified. 258 In this way if a waituntil over locks or futures have some resources available that were not consumed, it is not an issue. 259 However, if a thread modifies a channel on behalf of a thread blocked on a waituntil statement, it is important that the corresponding waituntil code block is run, otherwise there is a potentially erroneous mismatch between the channel state and associated side effects. 260 As such, the @unregister_select@ routine has a boolean return that is used by channels to indicate when the operation was completed but the block was not run yet. 261 As such some channel code blocks may be run as part of the unregister. 262 Furthermore if there are both @and@ and @or@ operators, the @or@ operators stop behaving like exclusive-or semantics since this race between operations and unregisters exists. 263 264 It was deemed important that exclusive-or semantics were maintained when only @or@ operators were used, so this situation has been special-cased, and is handled by having all clauses race to set a value \emph{before} operating on the channel. 265 This approach is infeasible in the case where @and@ and @or@ operators are used. 266 To show this consider the following waituntil statement. 267 268 \begin{cfa} 269 waituntil( i >> A ) {} and waituntil( i >> B ) {} 270 or waituntil( i >> C ) {} and waituntil( i >> D ) {} 271 \end{cfa} 272 273 If exclusive-or semantics were followed, this waituntil would only run the code blocks for @A@ and @B@, or the code blocks for @C@ and @D@. 274 However, to race before operation completion in this case introduces a race whose complexity increases with the size of the waituntil statement. 275 In the example above, for @i@ to be inserted into @C@, to ensure the exclusive-or it must be ensured that @i@ can also be inserted into @D@. 276 Furthermore, the race for the @or@ would also need to be won. 277 However, due to TOCTOU issues, one cannot know that all resources are available without acquiring all the internal locks of channels in the subtree. 278 This is not a good solution for two reasons. 279 It is possible that once all the locks are acquired that the subtree is not satisfied and they must all be released. 280 This would incur high cost for signalling threads and also heavily increase contention on internal channel locks. 281 Furthermore, the waituntil statement is polymorphic and can support resources that do not have internal locks, which also makes this approach infeasible. 282 As such, the exclusive-or semantics are lost when using both @and@ and @or@ operators since they can not be supported without significant complexity and hits to waituntil statement performance. 283 284 The mechanism by which the predicate of the waituntil is checked is discussed in more detail in Section~\ref{s:wu_guards}. 285 286 Another consideration introduced by channels is that supporting both reading and writing to a channel in a waituntil means that one waituntil clause may be the notifier for another waituntil clause. 287 This becomes a problem when dealing with the special-cased @or@ where the clauses need to win a race to operate on a channel. 288 When you have both a special-case @or@ inserting on one thread and another special-case @or@ consuming is blocked on another thread there is not one but two races that need to be consolidated by the inserting thread. 289 (The race can occur in the opposite case with a blocked producer and signalling consumer too.) 290 For them to know that the insert succeeded, they need to win the race for their own waituntil and win the race for the other waituntil. 291 Go solves this problem in their select statement by acquiring the internal locks of all channels before registering the select on the channels. 292 This eliminates the race since no other threads can operate on the blocked channel since its lock will be held. 293 294 This approach is not used in \CFA since the waituntil is polymorphic. 295 Not all types in a waituntil have an internal lock, and when using non-channel types acquiring all the locks incurs extra uneeded overhead. 296 Instead this race is consolidated in \CFA in two phases by having an intermediate pending status value for the race. 297 This case is detectable, and if detected the thread attempting to signal will first race to set the race flag to be pending. 298 If it succeeds, it then attempts to set the consumer's race flag to its success value. 299 If the producer successfully sets the consumer race flag, then the operation can proceed, if not the signalling thread will set its own race flag back to the initial value. 300 If any other threads attempt to set the producer's flag and see a pending value, they will wait until the value changes before proceeding to ensure that in the case that the producer fails, the signal will not be lost. 301 This protocol ensures that signals will not be lost and that the two races can be resolved in a safe manner. 302 303 Channels in \CFA have exception based shutdown mechanisms that the waituntil statement needs to support. 304 These exception mechanisms were what brought in the @on_selected@ routine. 305 This routine is needed by channels to detect if they are closed upon waking from a waituntil statement, to ensure that the appropriate behaviour is taken. 306 307 \subsection{Guards and Statement Predicate}\label{s:wu_guards} 308 Checking for when a synchronous multiplexing utility is done is trivial when it has an or/xor relationship, since any resource becoming available means that the blocked thread can proceed. 309 In \uC and \CFA, their \gls{synch_multiplex} utilities involve both an @and@ and @or@ operator, which make the problem of checking for completion of the statement more difficult. 310 311 In the \uC @_Select@ statement, they solve this problem by constructing a tree of the resources, where the internal nodes are operators and the leafs are the resources. 312 The internal nodes also store the status of each of the subtrees beneath them. 313 When resources become available, their status is modified and the status of the leaf nodes percolate into the internal nodes update the state of the statement. 314 Once the root of the tree has both subtrees marked as @true@ then the statement is complete. 315 As an optimization, when the internal nodes are updated, their subtrees marked as @true@ are effectively pruned and are not touched again. 316 To support \uC's @_Select@ statement guards, the tree prunes the branch if the guard is false. 317 318 The \CFA waituntil statement blocks a thread until a set of resources have become available that satisfy the underlying predicate. 319 The waiting condition of the waituntil statement can be represented as a predicate over the resources, joined by the waituntil operators, where a resource is @true@ if it is available, and @false@ otherwise. 320 In \CFA, this representation is used as the mechanism to check if a thread is done waiting on the waituntil. 321 Leveraging the compiler, a routine is generated per waituntil that is passed the statuses of the resources and returns a boolean that is @true@ when the waituntil is done, and false otherwise. 322 To support guards on the \CFA waituntil statement, the status of a resource disabled by a guard is set to ensure that the predicate function behaves as if that resource is no longer part of the predicate. 323 324 In \uC's @_Select@, it supports operators both inside and outside the clauses of their statement. 325 \eg in the following example the code blocks will run once their corresponding predicate inside the round braces is satisfied. 326 327 % C_TODO put this is uC++ code style not cfa-style 328 \begin{cfa} 329 Future_ISM<int> A, B, C, D; 330 _Select( A || B && C ) { ... } 331 and _Select( D && E ) { ... } 332 \end{cfa} 333 334 This is more expressive that the waituntil statement in \CFA. 335 In \CFA, since the waituntil statement supports more resources than just futures, implmenting operators inside clauses was avoided for a few reasons. 336 As an example, suppose \CFA supported operators inside clauses and consider the code snippet in Figure~\ref{f:wu_inside_op}. 337 338 \begin{figure} 339 \begin{cfa} 340 owner_lock A, B, C, D; 341 waituntil( A && B ) { ... } 342 or waituntil( C && D ) { ... } 343 \end{cfa} 344 \caption{Example of unsupported operators inside clauses in \CFA.} 345 \label{f:wu_inside_op} 346 \end{figure} 347 348 If the waituntil in Figure~\ref{f:wu_inside_op} works with the same semantics as described and acquires each lock as it becomes available, it opens itself up to possible deadlocks since it is now holding locks and waiting on other resources. 349 As such other semantics would be needed to ensure that this operation is safe. 350 One possibility is to use \CC's @scoped_lock@ approach that was described in Section~\ref{s:DeadlockAvoidance}, however the potential for livelock leaves much to be desired. 351 Another possibility would be to use resource ordering similar to \CFA's @mutex@ statement, but that alone is not sufficient if the resource ordering is not used everywhere. 352 Additionally, using resource ordering could conflict with other semantics of the waituntil statement. 353 To show this conflict, consider if the locks in Figure~\ref{f:wu_inside_op} were ordered @D@, @B@, @C@, @A@. 354 If all the locks are available, it becomes complex to both respect the ordering of the waituntil in Figure~\ref{f:wu_inside_op} when choosing which code block to run and also respect the lock ordering of @D@, @B@, @C@, @A@ at the same time. 355 One other way this could be implemented is to wait until all resources for a given clause are available before proceeding to acquire them, but this also quickly becomes a poor approach. 356 This approach won't work due to TOCTOU issues, as it is not possible to ensure that the full set resources are available without holding them all first. 357 Operators inside clauses in \CFA could potentially be implemented with careful circumvention of the problems involved, but it was not deemed an important feature when taking into account the runtime cost that would need to be paid to handle these situations. 358 The problem of operators inside clauses also becomes a difficult issue to handle when supporting channels. 359 If internal operators were supported, it would require some way to ensure that channels with internal operators are modified on if and only if the corresponding code block is run, but that is not feasible due to reasons described in the exclusive-or portion of Section~\ref{s:wu_chans}. 360 361 \section{Waituntil Performance} 362 The two \gls{synch_multiplex} utilities that are in the realm of comparability with the \CFA waituntil statement are the Go @select@ statement and the \uC @_Select@ statement. 363 As such, two microbenchmarks are presented, one for Go and one for \uC to contrast the systems. 364 The similar utilities discussed at the start of this chapter in C, Ada, Rust, \CC, and OCaml are either not meaningful or feasible to benchmark against. 365 The select(2) and related utilities in C are not comparable since they are system calls that go into the kernel and operate on file descriptors, whereas the waituntil exists solely in userspace. 366 Ada's @select@ only operates on methods, which is done in \CFA via the @waitfor@ utility so it is not feasible to benchmark against the @waituntil@, which cannot wait on the same resource. 367 Rust and \CC only offer a busy-wait based approach which is not meaningly comparable to a blocking approach. 368 OCaml's @select@ waits on channels that are not comparable with \CFA and Go channels, which makes the OCaml @select@ infeasible to compare it with Go's @select@ and \CFA's @waituntil@. 369 Given the differences in features, polymorphism, and expressibility between the waituntil and @select@, and @_Select@, the aim of the microbenchmarking in this chapter is to show that these implementations lie in the same realm of performance, not to pick a winner. 370 371 \subsection{Channel Benchmark} 372 The channel microbenchmark compares \CFA's waituntil and Go's select, where the resource being waited on is a set of channels. 373 374 %C_TODO explain benchmark 375 376 %C_TODO show results 377 378 %C_TODO discuss results 379 380 \subsection{Future Benchmark} 381 The future benchmark compares \CFA's waituntil with \uC's @_Select@, with both utilities waiting on futures. 382 383 %C_TODO explain benchmark 384 385 %C_TODO show results 386 387 %C_TODO discuss results -
doc/theses/colby_parsons_MMAth/thesis.tex
r2b78949 r8a930c03 111 111 colorlinks=true, % false: boxed links; true: colored links 112 112 linkcolor=blue, % color of internal links 113 citecolor=blue, % color of links to bibliography113 citecolor=blue, % color of links to bibliography 114 114 filecolor=magenta, % color of file links 115 urlcolor=cyan % color of external links 115 urlcolor=cyan, % color of external links 116 breaklinks=true 116 117 } 117 118 \ifthenelse{\boolean{PrintVersion}}{ % for improved print quality, change some hyperref options … … 126 127 % \usepackage[acronym]{glossaries} 127 128 \usepackage[automake,toc,abbreviations]{glossaries-extra} % Exception to the rule of hyperref being the last add-on package 129 \renewcommand*{\glstextformat}[1]{\textcolor{black}{#1}} 128 130 % If glossaries-extra is not in your LaTeX distribution, get it from CTAN (http://ctan.org/pkg/glossaries-extra), 129 131 % although it's supposed to be in both the TeX Live and MikTeX distributions. There are also documentation and -
doc/user/figures/EHMHierarchy.fig
r2b78949 r8a930c03 29 29 1 1 1.00 60.00 90.00 30 30 4950 1950 4950 1725 31 4 1 0 50 -1 0 1 30.0000 2 135 225 1950 1650 IO\00132 4 1 0 50 -1 0 1 30.0000 2 135 915 4950 1650 Arithmetic\00133 4 1 0 50 -1 0 1 30.0000 2 150 330 1350 2100 File\00134 4 1 0 50 -1 0 1 30.0000 2 135 735 2550 2100 Network\00135 4 1 0 50 -1 0 1 30.0000 2 180 1215 3750 2100 DivideByZero\00136 4 1 0 50 -1 0 1 30.0000 2 150 810 4950 2100 Overflow\00137 4 1 0 50 -1 0 1 30.0000 2 150 915 6000 2100 Underflow\00138 4 1 0 50 -1 0 1 30.0000 2 180 855 3450 1200 Exception\00131 4 1 0 50 -1 0 12 0.0000 2 135 225 1950 1650 IO\001 32 4 1 0 50 -1 0 12 0.0000 2 135 915 4950 1650 Arithmetic\001 33 4 1 0 50 -1 0 12 0.0000 2 150 330 1350 2100 File\001 34 4 1 0 50 -1 0 12 0.0000 2 135 735 2550 2100 Network\001 35 4 1 0 50 -1 0 12 0.0000 2 180 1215 3750 2100 DivideByZero\001 36 4 1 0 50 -1 0 12 0.0000 2 150 810 4950 2100 Overflow\001 37 4 1 0 50 -1 0 12 0.0000 2 150 915 6000 2100 Underflow\001 38 4 1 0 50 -1 0 12 0.0000 2 180 855 3450 1200 Exception\001 -
doc/user/user.tex
r2b78949 r8a930c03 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Aug 22 23:43:30 202214 %% Update Count : 55 0313 %% Last Modified On : Mon Jun 5 21:18:29 2023 14 %% Update Count : 5521 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 108 108 \huge \CFA Team (past and present) \medskip \\ 109 109 \Large Andrew Beach, Richard Bilson, Michael Brooks, Peter A. Buhr, Thierry Delisle, \smallskip \\ 110 \Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Colby Parsons, Rob Schluntz,\smallskip \\111 \Large Fangren Yu, Mubeen Zulfiqar110 \Large Glen Ditchfield, Rodolfo G. Esteves, Jiada Liang, Aaron Moss, Colby Parsons \smallskip \\ 111 \Large Rob Schluntz, Fangren Yu, Mubeen Zulfiqar 112 112 }% author 113 113 … … 169 169 Like \Index*[C++]{\CC{}}, there may be both old and new ways to achieve the same effect. 170 170 For example, the following programs compare the C, \CFA, and \CC I/O mechanisms, where the programs output the same result. 171 \begin{ flushleft}172 \begin{tabular}{@{}l @{\hspace{1em}}l@{\hspace{1em}}l@{}}173 \multicolumn{1}{@{}c @{\hspace{1em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{\CC}} \\171 \begin{center} 172 \begin{tabular}{@{}lll@{}} 173 \multicolumn{1}{@{}c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c@{}}{\textbf{\CC}} \\ 174 174 \begin{cfa}[tabsize=3] 175 175 #include <stdio.h>$\indexc{stdio.h}$ … … 199 199 \end{cfa} 200 200 \end{tabular} 201 \end{ flushleft}201 \end{center} 202 202 While \CFA I/O \see{\VRef{s:StreamIOLibrary}} looks similar to \Index*[C++]{\CC{}}, there are important differences, such as automatic spacing between variables and an implicit newline at the end of the expression list, similar to \Index*{Python}~\cite{Python}. 203 203 … … 856 856 still works. 857 857 Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shell arguments. 858 Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called \Indexc{choose}, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword \Indexc{fallthrough}/\ Indexc{fallthru}, \eg:858 Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called \Indexc{choose}, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword \Indexc{fallthrough}/\-\Indexc{fallthru}, \eg: 859 859 \begin{cfa} 860 860 ®choose® ( i ) { … … 1167 1167 \end{cfa} 1168 1168 \end{itemize} 1169 \R{Warning}: specifying the down-to range maybe unex cepted because the loop control \emph{implicitly} switches the L and H values (and toggles the increment/decrement for I):1169 \R{Warning}: specifying the down-to range maybe unexpected because the loop control \emph{implicitly} switches the L and H values (and toggles the increment/decrement for I): 1170 1170 \begin{cfa} 1171 1171 for ( i; 1 ~ 10 ) ${\C[1.5in]{// up range}$ … … 1173 1173 for ( i; ®10 -~ 1® ) ${\C{// \R{WRONG down range!}}\CRT}$ 1174 1174 \end{cfa} 1175 The reason for this sema tics is that the range direction can be toggled by adding/removing the minus, ©'-'©, versus interchanging the L and H expressions, which has a greater chance of introducing errors.1175 The reason for this semantics is that the range direction can be toggled by adding/removing the minus, ©'-'©, versus interchanging the L and H expressions, which has a greater chance of introducing errors. 1176 1176 1177 1177 … … 2256 2256 Days days = Mon; // enumeration type declaration and initialization 2257 2257 \end{cfa} 2258 The set of enums are injected into the variable namespace at the definition scope. 2259 Hence, enums may be overloaded with enum/variable/function names. 2260 \begin{cfa} 2258 The set of enums is injected into the variable namespace at the definition scope. 2259 Hence, enums may be overloaded with variable, enum, and function names. 2260 \begin{cfa} 2261 int Foo; $\C{// type/variable separate namespaces}$ 2261 2262 enum Foo { Bar }; 2262 2263 enum Goo { Bar }; $\C[1.75in]{// overload Foo.Bar}$ 2263 int Foo; $\C{// type/variable separate namespace}$2264 2264 double Bar; $\C{// overload Foo.Bar, Goo.Bar}\CRT$ 2265 2265 \end{cfa} … … 2301 2301 Hence, the value of enum ©Mon© is 0, ©Tue© is 1, ...\,, ©Sun© is 6. 2302 2302 If an enum value is specified, numbering continues by one from that value for subsequent unnumbered enums. 2303 If an enum value is a nexpression, the compiler performs constant-folding to obtain a constant value.2303 If an enum value is a \emph{constant} expression, the compiler performs constant-folding to obtain a constant value. 2304 2304 2305 2305 \CFA allows other integral types with associated values. … … 2313 2313 \begin{cfa} 2314 2314 // non-integral numeric 2315 enum( ®double® ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 }2315 enum( ®double® ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 } 2316 2316 // pointer 2317 enum( ®char *® ) Name { Fred = "Fred", Mary = "Mary", Jane = "Jane" };2317 enum( ®char *® ) Name { Fred = "Fred", Mary = "Mary", Jane = "Jane" }; 2318 2318 int i, j, k; 2319 2319 enum( ®int *® ) ptr { I = &i, J = &j, K = &k }; 2320 enum( ®int &® ) ref { I = i, J = j,K = k };2320 enum( ®int &® ) ref { I = i, J = j, K = k }; 2321 2321 // tuple 2322 2322 enum( ®[int, int]® ) { T = [ 1, 2 ] }; … … 2361 2361 \begin{cfa} 2362 2362 enum( char * ) Name2 { ®inline Name®, Jack = "Jack", Jill = "Jill" }; 2363 enum ®/* inferred */® Name3 { ®inline Name2®, Sue = "Sue", Tom = "Tom" };2363 enum ®/* inferred */® Name3 { ®inline Name2®, Sue = "Sue", Tom = "Tom" }; 2364 2364 \end{cfa} 2365 2365 Enumeration ©Name2© inherits all the enums and their values from enumeration ©Name© by containment, and a ©Name© enumeration is a subtype of enumeration ©Name2©. … … 3818 3818 "[ output-file (default stdout) ] ]"; 3819 3819 } // choose 3820 } catch( ® Open_Failure® * ex; ex->istream == &in ) {3820 } catch( ®open_failure® * ex; ex->istream == &in ) { $\C{// input file errors}$ 3821 3821 ®exit® | "Unable to open input file" | argv[1]; 3822 } catch( ® Open_Failure® * ex; ex->ostream == &out ) {3822 } catch( ®open_failure® * ex; ex->ostream == &out ) { $\C{// output file errors}$ 3823 3823 ®close®( in ); $\C{// optional}$ 3824 3824 ®exit® | "Unable to open output file" | argv[2]; … … 4038 4038 4039 4039 \item 4040 \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} toggle printing the separator.4040 \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} globally toggle printing the separator. 4041 4041 \begin{cfa}[belowskip=0pt] 4042 4042 sout | sepDisable | 1 | 2 | 3; $\C{// turn off implicit separator}$ … … 4053 4053 4054 4054 \item 4055 \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} toggle printing the separator with respect to the next printed item, and then return to the global separator setting.4055 \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} locally toggle printing the separator with respect to the next printed item, and then return to the global separator setting. 4056 4056 \begin{cfa}[belowskip=0pt] 4057 4057 sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$ … … 4129 4129 6 4130 4130 \end{cfa} 4131 Note, a terminating ©nl© is merged (overrides) with the implicit newline at the end of the ©sout© expression, otherwise it is impossible to toprint a single newline4131 Note, a terminating ©nl© is merged (overrides) with the implicit newline at the end of the ©sout© expression, otherwise it is impossible to print a single newline 4132 4132 \item 4133 4133 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} implicitly prints a newline at the end of each output expression.
Note:
See TracChangeset
for help on using the changeset viewer.