Principles of Computer Systems 1999



1. Course Information

Staff

Faculty

|Butler Lampson |32-G924 |425-703-5925 |blampson@ |

|Daniel Jackson |32-G704 |8-8471 |dnj@mit.edu |

Teaching Assistant

|David Shin | | |dshin@mit.edu |

Course Secretary

|Maria Rebelo |32-G715 |3-5895 |mr@csail.mit.edu |

Office Hours

Messrs. Lampson and Jackson will arrange individual appointments. David Shin will hold scheduled office hours in the 7th floor lounge of the Gates tower in building, Monday 4-6. In addition to holding regularly scheduled office hours, he will also be available by appointment.

Lectures and handouts

Lectures are held on Monday and Wednesday from 1:00 to 2:30PM in room 32-155. Messrs. Lampson and Jackson will split the lectures. The tentative schedule is at the end of this handout.

The source material for this course is an extensive set of handouts. There are about 400 pages of topic handouts that take the place of a textbook; you will need to study them to do well in the course. Since we don’t want to simply repeat the written handouts in class, we will hand out the material for each lecture one week in advance. We expect you to read the day’s handouts before the class and come prepared to ask questions, discuss the material, or follow extensions of it or different ways of approaching the topic.

Seven research papers supplement the topic handouts. In addition there are 5 problem sets, and the project described below. Solutions for each problem set will be available shortly after the due date.

There is a course Web page, at web.mit.edu/6.826/ Last year’s handouts can be found from this page. Current handouts will be placed on the Web as they are produced.

Current handouts will generally be available in lecture. If you miss any in lecture, you can obtain them afterwards from the course secretary. She keeps them in a file cabinet outside her office.

Problem sets

There is a problem set approximately once a week for the first half of the course. Problem sets are handed out on Wednesdays and are due by noon the following Wednesday in the tray on the course secretary's desk. They normally cover the material discussed in class during the week they are handed out. Delayed submission of the solutions will be penalized, and no solutions will be accepted after Thursday 5:00PM.

Students in the class will be asked to help grade the problem sets. Each week a team of students will work with the TA to grade the week’s problems. This takes about 3-4 hours. Each student will probably only have to do it once during the term.

We will try to return the graded problem sets, with solutions, within a week after their due date.

Policy on collaboration

We encourage discussion of the issues in the lectures, readings, and problem sets. However, if you collaborate on problem sets, you must tell us who your collaborators are. And in any case, you must write up all solutions on your own.

Project

During the last half of the course there is a project in which students will work in groups of three or so to apply the methods of the course to their own research projects. Each group will pick a real system, preferably one that some member of the group is actually working on but possibly one from a published paper or from someone else’s research, and write:

A specification for it.

High-level code that captures the novel or tricky aspects of the actual implementation.

The abstraction function and key invariants for the correctness of the code. This is not optional; if you can’t write these things down, you don’t understand what you are doing.

Depending on the difficulty of the specification and code, the group may also write a correctness proof for the code.

Projects may range in style from fairly formal, like handout 18 on consensus, in which the ‘real system’ is a simple one, to fairly informal (at least by the standards of this course), like the section on copying file systems in handout 7. These two handouts, along with the ones on naming, sequential transactions, concurrent transactions, and caching, are examples of the appropriate size and possible styles of a project.

The result of the project should be a write-up, in the style of one of these handouts. During the last two weeks of the course, each group will give a 25-minute presentation of its results. We have allocated four class periods for these presentations, which means that there will be twelve or fewer groups.

The projects will have five milestones. The purpose of these milestones is not to assign grades, but to make it possible for the instructors to keep track of how the projects are going and give everyone the best possible chance of a successful project

1. We will form the groups around March 2, to give most of the people that will drop the course a chance to do so.

2. Each group will write up a 2-3 page project proposal, present it to one of the instructors around spring break, and get feedback about how appropriate the project is and suggestions on how to carry it out. Any project that seems to be seriously off the rails will have a second proposal meeting a week later.

3. Each group will submit a 5-10 page interim report in the middle of the project period.

4. Each group will give a presentation to the class during the last two weeks of classes.

5. Each group will submit a final report, which is due on Friday, May 14, the last day allowed by MIT regulations. Of course you are free to submit it early.

Half the groups will be ‘early’ ones; the other half will be ‘late’ ones that give their presentations one week later. The due dates of proposals and interim reports will be spread out over two weeks in the same way. See the schedule later in this handout for precise dates.

Grades

There are no exams. Grades are based 30% on the problem sets, 50% on the project, and 20% on class participation and quality and promptness of grading.

Course mailing list

A mailing list for course announcements—6.826-students@mit.edu—has been set up to include all students and the TA. If you do not receive any email from this mailing list within the first week, check with the TA. Another mailing list, 6.826-staff@mit.edu, sends email to the entire 6.826 staff.

Course Schedule

|Date |No |By |HO |Topic |PS |PS |

| | | | | |out |due |

|Wed., Feb. 8 |1 |L | |Overview. The Spec language. State machine semantics. Examples of |1 | |

| | | | |specifications and code. | | |

| | | |1 |Course information | | |

| | | |2 |Background | | |

| | | |3 |Introduction to Spec | | |

| | | |4 |Spec reference manual | | |

| | | |5 |Examples of specs and code | | |

|Mon., Feb. 13 |2 |J | |Spec and code for sequential programs. Correctness notions and proofs. Proof | | |

| | | | |methods: abstraction functions and invariants. | | |

| | | |6 |Abstraction functions | | |

|Wed., Feb. 15 |3 |L | |File systems 1: Disks, simple sequential file system, caching, logs for crash |2 |1 |

| | | | |recovery. | | |

| | | |7 |Disks and file systems | | |

|Tues., Feb. 21 |4 |L | |File systems 2: Copying file system. | | |

|Wed., Feb.22 |5 |L | |Proof methods: History and prophecy variables; abstraction relations. |3 |2 |

| | | |8 |History variables | | |

|Mon., Feb. 27 |6 |S | |Semantics and proofs: Formal sequential semantics of Spec. | | |

| | | |9 |Atomic semantics of Spec | | |

|Wed., Mar. 1 |7 |J | |Naming: Specs, variations, and examples of hierarchical naming. |Form groups |

| | | |12 |Naming | | |

| | | |13 |Paper: David Gifford et al, Semantic file systems, Proc.13th ACM Symposium on | | |

| | | | |Operating System Principles, October 1991, pp 16-25. | | |

|Mon., Mar. 6 |8 |L | |Performance: How to get it, how to analyze it. |4 |3 |

| | | |10 |Performance | | |

| | | |11 |Paper: Michael Schroeder and Michael Burrows, Performance of Firefly RPC, ACM | | |

| | | | |Transactions on Computer Systems 8, 1, February 1990, pp 1-17. | | |

|Wed., Mar. 8 |9 |J | |Concurrency 1: Practical concurrency, easy and hard. Easy concurrency using |5 |4 |

| | | | |locks and condition variables. Problems with it: scheduling, deadlock. | | |

| | | |14 |Practical concurrency | | |

| | | |15 |Concurrent disks | | |

| | | |16 |Paper: Andrew Birrell, An Introduction to Programming with C# Threads, | | |

| | | | |Microsoft Research Technical Report MSR-TR-2005-68, May 2005. | | |

|Mon., Mar. 13 |10 |S | |Concurrency 2: Concurrency in Spec: threads and non-atomic semantics. Big | | |

| | | | |atomic actions. Safety and liveness. Examples of concurrency. | | |

| | | |17 |Formal concurrency | | |

|Wed., Mar. 15 |11 |S | |Concurrency 3: Proving correctness of concurrent programs: assertional proofs,| |5 |

| | | | |model checking | | |

|Mon., Mar. 20 |12 |L | |Distributed consensus 1. Paxos algorithm for asynchronous consensus in the | | |

| | | | |presence of faults. | | |

| | | |18 |Consensus | | |

|Wed., Mar. 22 |13 |L | |Distributed consensus 2. |Early proposals |

|Mar. 27-31 | | | |Spring Break | | |

|Mon., Apr. 3 |14 |J | |Sequential transactions with caching. | | |

| | | |19 |Sequential transactions | | |

|Wed., Apr. 5 |15 |J | |Concurrent transactions: Specs for serializability. Ways to code the specs. |Late |

| | | | | |proposals |

| | | |20 |Concurrent transactions | | |

|Mon., Apr. 10 |16 |J | |Distributed transactions: Commit as a consensus problem. Two-phase commit. | |

| | | | |Optimizations. | |

| | | |27 |Distributed transactions | | |

|Wed., Apr. 12 |17 |L | |Introduction to distributed systems: Characteristics of distributed systems. | | |

| | | | |Physical, data link, and network layers. Design principles. | | |

| | | | |Networks 1: Links. Point-to-point and broadcast networks. | | |

| | | |21 |Distributed systems | | |

| | | |22 |Paper: Michael Schroeder et al, Autonet: A high-speed, self-configuring local | | |

| | | | |area network, IEEE Journal on Selected Areas in Communications 9, 8, October | | |

| | | | |1991, pp 1318-1335. | | |

| | | |23 |Networks: Links and switches | | |

|Mon., Apr. 17 | | | |Patriot’s Day, no class | |

|Wed., Apr. 19 |18 |L | |Networks 2: Links cont’d: Ethernet. Token Rings. |Early interim |

| | | | |Switches. Coding switches. Routing. Learning topologies and establishing |reports |

| | | | |routes. | |

|Mon., Apr. 24 |19 |J | |Networks 3: Network objects and remote procedure call (RPC). | |

| | | |24 |Network objects | | |

| | | |25 |Paper: Andrew Birrell et al., Network objects, Proc.14th ACM Symposium on | | |

| | | | |Operating Systems Principles, Asheville, NC, December 1993. | | |

|Wed., Apr. 26 |20 |L | |Networks 4: Reliable messages. 3-way handshake and clock code. TCP as a form |Late interim |

| | | | |of reliable messages. |reports |

| | | |26 |Paper: Butler Lampson, Reliable messages and connection establishment. In | | |

| | | | |Distributed Systems, ed. S. Mullender, Addison-Wesley, 1993, pp 251-281. | | |

|Mon., May 1 |21 |J | |Replication and availability: Coding replicated state machines using | | |

| | | | |consensus. Applications to replicated storage. | | |

| | | |28 |Replication | | |

| | | |29 |Paper: Jim Gray and Andreas Reuter, Fault tolerance, in Transaction | | |

| | | | |Processing: Concepts and Techniques, Morgan Kaufmann, 1993, pp 93-156. | | |

|Wed., May 3 |22 |J | |Caching: Maintaining coherent memory. Broadcast (snoopy) and directory | | |

| | | | |protocols. Examples: multiprocessors, distributed shared memory, distributed | | |

| | | | |file systems. | | |

| | | |30 |Concurrent caching | | |

|Mon., May 8 |23 | | |Early project presentations | | |

|Wed., May 10 |24 | | |Early project presentations | | |

|Mon., May 15 |25 | | |Late project presentations | | |

|Wed., May 17 |26 | | |Late project presentations | | |

|Fri., May 19 | | | |Final reports due | |

|May 22-26 | | | |Finals week. There is no final for 6.826. | | |

2. Overview and Background

This is a course for computer system designers and builders, and for people who want to really understand how systems work, especially concurrent, distributed, and fault-tolerant systems.

The course teaches you

how to write precise specifications for any kind of computer system,

what it means for code to satisfy a specification, and

how to prove that it does.

It also shows you how to use the same methods less formally, and gives you some suggestions for deciding how much formality is appropriate (less formality means less work, and often a more understandable spec, but also more chance to overlook an important detail).

The course also teaches you a lot about the topics in computer systems that we think are the most important: persistent storage, concurrency, naming, networks, distributed systems, transactions, fault tolerance, and caching. The emphasis is on

careful specifications of subtle and sometimes complicated things,

the important ideas behind good code, and

how to understand what makes them actually work.

We spend most of our time on specific topics, but we use the general techniques throughout. We emphasize the ideas that different kinds of computer system have in common, even when they have different names.

The course uses a formal language called Spec for writing specs and code; you can think of it as a very high level programming language. There is a good deal of written introductory material on Spec (explanations and finger exercises) as well as a reference manual and a formal semantics. We introduce Spec ideas in class as we use them, but we do not devote class time to teaching Spec per se; we expect you to learn it on your own from the handouts. The one to concentrate on is handout 3, which has an informal introduction to the main features and lots of examples. Section 9 of handout 4, the reference manual, should also be useful. The rest of the reference manual is for reference, not for learning. Don’t overlook the one page summary at the end of handout 3.

Because we write specs and do proofs, you need to know something about logic. Since many people don’t, there is a concise treatment of the logic you will need at the end of this handout.

This is not a course in computer architecture, networks, operating systems, or databases. We will not talk in detail about how to code pipelines, memory interconnects, multiprocessors, routers, data link protocols, network management, virtual memory, scheduling, resource allocation, SQL, relational integrity, or TP monitors, although we will deal with many of the ideas that underlie these mechanisms.

Topics

General

Specifications as state machines.

The Spec language for describing state machines (writing specs and code).

What it means to implement a spec.

Using abstraction functions and invariants to prove that a program implements a spec.

What it means to have a crash.

What every system builder needs to know about performance.

Specific

Disks and file systems.

Practical concurrency using mutexes (locks) and condition variables; deadlock.

Hard concurrency (without locking): models, specs, proofs, and examples.

Transactions: simple, cached, concurrent, distributed.

Naming: principles, specs, and examples.

Distributed systems: communication, fault-tolerance, and autonomy.

Networking: links, switches, reliable messages and connections.

Remote procedure call and network objects.

Fault-tolerance, availability, consensus and replication.

Caching and distributed shared memory.

Previous editions of the course have also covered security (authentication, authorization, encryption, trust) and system management, but this year we are omitting these topics in order to spend more time on concurrency and semantics and to leave room for project presentations.

Prerequisites

There are no formal prerequisites for the course. However, we assume some knowledge both of computer systems and of mathematics. If you have taken 6.033 and 6.042, you should be in good shape. If you are missing some of this knowledge you can pick it up as we go, but if you are missing a lot of it you can expect to have serious trouble. It’s also important to have a certain amount of maturity: enough experience with systems and mathematics to feel comfortable with the basic notions and to have some reliable intuition.

If you know the meaning of the following words, you have the necessary background. If a lot of them are unfamiliar, this course is probably not for you.

Systems

Cache, virtual memory, page table, pipeline

Process, scheduler, address space, priority

Thread, mutual exclusion (locking), semaphore, producer-consumer, deadlock

Transaction, commit, availability, relational data base, query, join

File system, directory, path name, striping, RAID

LAN, switch, routing, connection, flow control, congestion

Capability, access control list, principal (subject)

If you have not already studied Lampson’s paper on hints for system design, you should do so as background for this course. It is Butler Lampson, Hints for computer system design, Proceedings of the Ninth ACM Symposium on Operating Systems Principles, October 1983, pp 33-48. There is a pointer to it on the course Web page.

Programming

Invariant, precondition, weakest precondition, fixed point

Procedure, recursion, stack

Data type, sub-type, type-checking, abstraction, representation

Object, method, inheritance

Data structures: list, hash table, binary search, B-tree, graph

Mathematics

Function, relation, set, transitive closure

Logic: proof, induction, de Morgan’s laws, implication, predicate, quantifier

Probability: independent events, sampling, Poisson distribution

State machine, context-free grammar

Computational complexity, unsolvable problem

If you haven’t been exposed to formal logic, you should study the summary at the end of this handout.

References

These are places to look when you want more information about some topic covered or alluded to in the course, or when you want to follow current research. You might also wish to consult Prof. Saltzer’s bibliography for 6.033, which you can find on the course web page.

Books

Some of these are fat books better suited for reference than for reading cover to cover, especially Cormen, Leiserson, and Rivest, Jain, Mullender, Hennessy and Patterson, and Gray and Reuter. But the last two are pretty easy to read in spite of their encyclopedic character.

Specification: Leslie Lamport, Specifying Systems: The TLA+ Language and Tools for Hardware and Software Engineers, Addison-Wesley, 2002. TLA+ is superficially quite different from Spec, but the same underneath. Lamport’s approach is somewhat more mathematical than ours, but in the same spirit. You can find this book at .

Systems programming: Greg Nelson, ed., Systems Programming with Modula-3, Prentice-Hall, 1991. Describes the language, which has all the useful features of C++ but is much simpler and less error-prone, and also shows how to use it for concurrency (a version of chapter 4 is a handout in this course), an efficiently customizable I/O streams package, and a window system.

Performance: Jon Bentley, Writing Efficient Programs, Prentice-Hall, 1982. Short, concrete, and practical. Raj Jain, The Art of Computer Systems Performance Analysis, Wiley, 1991. Tells you much more than you need to know about this subject, but does have a lot of realistic examples.

Algorithms and data structures: Robert Sedgwick, Algorithms, Addison-Wesley, 1983. Short, and usually tells you what you need to know. Tom Cormen, Charles Leiserson, and Ron Rivest, Introduction to Algorithms, McGraw-Hill, 1989. Comprehensive, and sometimes valuable for that reason, but usually tells you a lot more than you need to know.

Distributed algorithms: Nancy Lynch, Distributed Algorithms, Morgan Kaufmann, 1996. The bible for distributed algorithms. Comprehensive, but a much more formal treatment than in this course. The topic is algorithms, not systems.

Computer architecture: John Hennessy and David Patterson, Computer Architecture: A Quantitative Approach, 2nd edition, Morgan Kaufmann, 1995. The bible for computer architecture. The second edition has lots of interesting new material, especially on multiprocessor memory systems and interconnection networks. There’s also a good appendix on computer arithmetic; it’s useful to know where to find this information, though it has nothing to do with this course.

Transactions, data bases, and fault-tolerance: Jim Gray and Andreas Reuter, Transaction Processing: Concepts and Techniques, Morgan Kaufmann, 1993. The bible for transaction processing, with much good material on data bases as well; it includes a lot of practical information that doesn’t appear elsewhere in the literature.

Networks: Radia Perlman, Interconnections: Bridges and Routers, Addison-Wesley, 1992. Not exactly the bible for networking, but tells you nearly everything you might want to know about how packets are actually switched in computer networks.

Distributed systems: Sape Mullender, ed., Distributed Systems, 2nd ed., Addison-Wesley, 1993. A compendium by many authors that covers the field fairly well. Some chapters are much more theoretical than this course. Chapters 10 and 11 are handouts in this course. Chapters 1, 2, 8, and 12 are also recommended. Chapters 16 and 17 are the best you can do to learn about real-time computing; unfortunately, that is not saying much.

User interfaces: Alan Cooper, About Face, IDG Books, 1995. Principles, lots of examples, and opinionated advice, much of it good, from the original designer of Visual Basic.

Journals

You can find all of these in the CSAIL reading room in 32-G882. The cryptic strings in brackets are call numbers there. You can also find the ACM publications in the ACM digital library at .

For the current literature, the best sources are the proceedings of the following conferences. ‘Sig’ is short for “Special Interest Group”, a subdivision of the ACM that deals with one field of computing. The relevant ones for systems are SigArch for computer architecture, SigPlan for programming languages, SigOps for operating systems, SigComm for communications, SigMod for data bases, and SigMetrics for performance measurement and analysis.

Symposium on Operating Systems Principles (SOSP; published as special issues of ACM SigOps Operating Systems Review; fall of odd-numbered years) [P4.35.06]

Operating Systems Design and Implementation (OSDI; Usenix Association, now published as special issues of ACM SigOps Review; fall of even-numbered years, except spring 1999 instead of fall 1998) [P4.35.U71]

Architectural Support for Programming Languages and Operating Systems (ASPLOS; published as special issues of ACM SigOps Operating Systems Review, SigArch Computer Architecture News, or SigPlan Notices; fall of even-numbered years) [P6.29.A7]

Applications, Technologies, Architecture, and Protocols for Computer Communication, (SigComm conference; published as special issues of ACM SigComm Computer Communication Review; annual) [P6.24.D31]

Principles of Distributed Computing (PODC; ACM; annual) [P4.32.D57]

Very Large Data Bases (VLDB; Morgan Kaufmann; annual) [P4.33.V4]

International Symposium on Computer Architecture (ISCA; published as special issues of ACM SigArch Computer Architecture News; annual) [P6.20.C6]

Less up to date, but more selective, are the journals. Often papers in these journals are revised versions of papers from the conferences listed above.

ACM Transactions on Computer Systems

ACM Transactions on Database Systems

ACM Transactions on Programming Languages and Systems

There are often good survey articles in the less technical IEEE journals:

IEEE Computer, Networks, Communication, Software

The Internet Requests for Comments (RFC’s) can be reached from



Rudiments of logic

Propositional logic

The basic type is Bool, which contains two elements true and false. Expressions in these operators (and the other ones introduced later) are called ‘propositions’.

Basic operators. These are ∧ (and), ∨ (or), and ~ (not).[1] The meaning of these operators can be conveniently given by a ‘truth table’ which lists the value of a op b for each possible combination of values of a and b (the operators on the right are discussed later) along with some popular names for certain expressions and their operands.

| | |negation |conjunction |disjunction | |equality | |implication |

| | |not |and |or | | | |implies |

|a |b |~a |a ∧ b |a ∨ b | |a = b |a ( b |a ⇒ b |

|T |T |F |T |T | |T |F |T |

|T |F | |F |T | |F |T |F |

|F |T |T |F |T | |F |T |T |

|F |F | |F |F | |T |F |T |

|name of a | |conjunct |disjunct | | | |antecedent |

|name of b | |conjunct |disjunct | | | |consequent |

Note: In Spec we write ==> instead of the ⇒ that mathematicians use for implication. Logicians write ⊃ for implication, which looks different but is shaped like the > part of ⇒.

Since the table has only four rows, there are only 16 Boolean operators, one for each possible arrangement of T and F in a column. Most of the ones not listed don’t have common names, though ‘not and’ is called ‘nand’ and ‘not or’ is called ‘nor’ by logic designers.

The ∧ and ∨ operators are

commutative and

associative and

distribute over each other.

That is, they are just like * (times) and + (plus) on integers, except that + doesn’t distribute over *:

a + (b * c) ( (a + b) * (a + c)

but ∨ does distribute over ∧:

a ∨ (b ∧ c) = (a ∨ b) ∧ (a ∨ c)

An operator that distributes over ∧ is called ‘conjunctive’; one that distributes over ∨ is called ‘disjunctive’. Both ∧ and ∨ are both conjunctive and disjunctive. This takes some getting used to.

The relation between these operators and ~ is given by DeMorgan’s laws (sometimes called the “bubble rule” by logic designers), which say that you can push ~ inside ∧ or ∨ (or pull it out) by flipping from one to the other:

~ (a ∧ b) = ~a ∨ ~b

~ (a ∨ b) = ~a ∧ ~b

To put a complex expression into “ disjunctive normal form” replace terms in = and ⇒ with their equivalents in ∧, ∨, and ~ (given below), use DeMorgan’s laws to push all the ~’s in past ∧ and ∨ so that they apply to variables, and then distribute ∧ over ∨ so that the result looks like

(a1 ∧ ~a2 ∧ ...) ∨ (~b1 ∧ b2 ∧ ...) ∨ ...

The disjunctive normal form is unique (up to ordering, since ∧ and ∨ are commutative). Of course, you can also distribute ∨ over ∧ to get a unique “conjunctive normal form”.

If you want to find out whether two expressions are equal, one way is to put them both into disjunctive (or conjunctive) normal form, sort the terms, and see whether they are identical. Another way is to list all the possible values of the variables (if there are n variables, there are 2n of them) and tabulate the values of the expressions for each of them; we saw this ‘truth table’ for some two-variable expressions above.

Because Bool is the result type of relations like =, you can write expressions that mix up relations with other operators in ways that are impossible for any other type. Notably

(a = b) = ((a ∧ b) ∨ (~a ∧ ~b))

Some people feel that the outer = in this expression is somehow different from the inner one, and write it ≡. Experience suggests, however, that this is often a harmful distinction to make.

Implication. We can define an ordering on Bool with false > true, that is, false is greater than true. The non-strict version of this ordering is called ‘implication’ and written ⇒ (rather than ( or >= as we do with other types; logicians write it ⊃, which also looks like an ordering symbol). So (true ⇒ false) = false (read this as: “true is greater than or equal to false” is false) but all other combinations are true. The expression a ⇒ b is pronounced “a implies b”, or “if a then b”.[2]

There are lots of rules for manipulating expressions containing ⇒; the most useful ones are given below. If you remember that ⇒ is an ordering you’ll find it easy to remember most of the rules, but if you forget the rules or get confused, you can turn the ⇒ into ∨ by the rule

(a ⇒ b) = ~a ∨ b

and then just use the simpler rules for ∧, ∨, and ~. So remember this even if you forget everything else.

The point of implication is that it tells you when one proposition is stronger than another, in the sense that if the first one is true, the second is also true (because if both a and a ⇒ b are true, then b must be true since it can’t be false).[3] So we use implication all the time when reasoning from premises to conclusions. Two more ways to pronounce a ⇒ b are “a is stronger than b” and “b follows from a”. The second pronunciation suggests that it’s sometimes useful to write the operands in the other order, as b ( a, which can also be pronounced “b is weaker than a” or “b only if a”; this should be no surprise, since we do it with other orderings.

Of course, implication has the properties we expect of an ordering:

Transitive: If a ⇒ b and b ⇒ c then a ⇒ c.[4]

Reflexive: a ⇒ a.

Anti-symmetric: If a ⇒ b and b ⇒ a then a = b.[5]

Furthermore, ~ reverses the sense of implication (this is called the ‘contrapositive’):

(a ⇒ b) = (~b ⇒ ~a)

More generally, you can move a disjunct on the right to a conjunct on the left by negating it, or vice versa. Thus

(a ⇒ b ∨ c) = (a ∧ ~b ⇒ c)

As special cases in addition to the contrapositive we have

(a ⇒ b) = (a ∧ ~b ⇒ false ) = ~ (a ∧ ~b) ∨ false = ~a ∨ b

(a ⇒ b) = (true ⇒ ~a ∨ b) = false ∨ ~a ∨ b = ~a ∨ b

since false and true are the identities for ∨ and ∧.

We say that an operator op is ‘monotonic’ in an operand if replacing that operand with a stronger (or weaker) one makes the result stronger (or weaker). Precisely, “op is monotonic in its first operand” means that if a ⇒ b then (a op c) ⇒ (b op c). Both ∧ and ∨ are monotonic; in fact, any operator that is conjunctive (distributes over ∧) is monotonic, because if a ⇒ b then a = (a ∧ b), so

a op c = (a ∧ b) op c = a op c ∧ b op c ⇒ b op c

If you know what a lattice is, you will find it useful to know that the set of propositions forms a lattice with ⇒ as its ordering and (remember, think of ⇒ as “greater than or equal”):

top = false

bottom = true

meet = ∧ least upper bound, so (a ∧ b) ⇒ a and (a ∧ b) ⇒ b

join = ∨ greatest lower bound, so a ⇒ (a ∨ b) and b ⇒ (a ∨ b)

This suggests two more expressions that are equivalent to a ⇒ b:

(a ⇒ b) = (a = (a ∧ b)) ‘and’ing a weaker term makes no difference,

because a ⇒ b iff a = least upper bound(a, b).

(a ⇒ b) = (b = (a ∨ b)) ‘or’ing a stronger term makes no difference,

because a ⇒ b iff b = greatest lower bound(a, b).

Predicate logic

Propositions that have free variables, like x < 3 or x < 3 ⇒ x < 5, demand a little more machinery. You can turn such a proposition into one without a free variable by substituting some value for the variable. Thus if P(x) is x < 3 then P(5) is 5 < 3 = false. To get rid of the free variable without substituting a value for it, you can take the ‘and’ or ‘or’ of the proposition for all the possible values of the free variable. These have special names and notation[6]:

∀ x | P(x) = P(x1) ∧ P(x2) ∧ ... for all x, P(x). In Spec,

(ALL x | P(x)) or ∧ : {x | P(x)}

∃ x | P(x) = P(x1) ∨ P(x2) ∨ ... there exists an x such that P(x). In Spec,

(EXISTS x | P(x)) or ∨ : {x | P(x)}

Here the xi range over all the possible values of the free variables.[7] The first is called ‘universal quantification’; as you can see, it corresponds to conjunction. The second is called ‘existential quantification’ and corresponds to disjunction. If you remember this you can easily figure out what the quantifiers do with respect to the other operators.

In particular, DeMorgan’s laws generalize to quantifiers:

~ (∀ x | P(x)) = (∃ x | ~P(x))

~ (∃ x | P(x)) = (∀ x | ~P(x))

Also, because ∧ and ∨ are conjunctive and therefore monotonic, ∀ and ∃ are conjunctive and therefore monotonic.

It is not true that you can reverse the order of ∀ and ∃, but it’s sometimes useful to know that having ∃ first is stronger:

∃ y | ∀ x | P(x, y) ⇒ ∀ x | ∃ y | P(x, y)

Intuitively this is clear: a y that works for every x can surely do the job for each particular x.

If we think of P as a relation, the consequent in this formula says that P is total (relates every x to some y). It doesn’t tell us anything about how to find a y that is related to x. As computer scientists, we like to be able to compute things, so we prefer to have a function that computes y, or the set of y’s, from x. This is called a ‘Skolem function’; in Spec you write P.func (or P.setF for the set). P.func is total if P is total. Or, to turn this around, if we have a total function f such that ∀ x | P(x, f(x)), then certainly ∀ x | ∃  y | P(x, y); in fact, y = f(x) will do. Amazing.

Summary of logic

The ∧ and ∨ operators are commutative and associative and distribute over each other.

DeMorgan’s laws: ~ (a ∧ b) = ~a ∨ ~b

~ (a ∨ b) = ~a ∧ ~b

Any expression has a unique (up to ordering) disjunctive normal form in which ( combines terms in which ( combines (possibly negated) variables: (a1 ∧ ~a2 ∧ ...) ∨ (~b1 ∧ b2 ∧ ...) ∨ ...

Implication: (a ⇒ b) = ~a ∨ b

Implication is the ordering in a lattice (a partially ordered set in which every subset has a least upper and a greatest lower bound) with

top = false so false ⇒ true

bottom = true

meet = ∧ least upper bound, so (a ∧ b) ⇒ a

join = ∨ greatest lower bound, so a ⇒ (a ∨ b)

For all x, P(x):

∀ x | P(x) = P(x1) ∧ P(x2) ∧ ...

There exists an x such that P(x):

∃ x | P(x) = P(x1) ∨ P(x2) ∨ ...

Index for logic

~, 6

==>, 6

ALL, 9

and, 6

antecedent, 6

Anti-symmetric, 8

associative, 6

bottom, 8

commutative, 6

conjunction, 6

conjunctive, 6

consequent, 6

contrapositive, 8

DeMorgan’s laws, 7, 9

disjunction, 6

disjunctive, 6

distribute, 6

existential quantification, 9

EXISTS, 9

follows from, 7

free variables, 8

greatest lower bound, 8

if a then b, 7

implication, 6, 7

join, 8

lattice, 8

least upper bound, 8

meet, 8

monotonic, 8

negation, 6

not, 6

only if, 7

operators, 6

or, 6

ordering on Bool, 7

predicate logic, 8

propositions, 6

quantifiers, 9

reflexive, 8

Skolem function, 9

stronger than, 7

top, 8

transitive, 8

truth table, 6

universal quantification, 9

weaker than, 7

3. Introduction to Spec

This handout explains what the Spec language is for, how to use it effectively, and how it differs from a programming language like C, Pascal, Clu, Java, or Scheme. Spec is very different from these languages, but it is also much simpler. Its meaning is clearer and Spec programs are more succinct and less burdened with trivial details. The handout also introduces the main constructs that are likely to be unfamiliar to a programmer. You will probably find it worthwhile to read it over more than once, until those constructs are familiar. Don’t miss the one-page summary of spec at the end. The handout also has an index.

Spec is a language for writing precise descriptions of digital systems, both sequential and concurrent. In Spec you can write something that differs from practical code (for instance, code written in C) only in minor details of syntax. This sort of thing is usually called a program. Or you can write a very high level description of the behavior of a system, usually called a specification. A good specification is almost always quite different from a good program. You can use Spec to write either one, but not the same style of Spec. The flexibility of the language means that you need to know the purpose of your Spec in order to write it well.

Most people know a lot more about writing programs than about writing specs, so this introduction emphasizes how Spec differs from a programming language and how to use it to write good specs. It does not attempt to be either complete or precise, but other handouts fill these needs. The Spec Reference Manual (handout 4) describes the language completely; it gives the syntax of Spec precisely and the semantics informally. Atomic Semantics of Spec (handout 9) describes precisely the meaning of an atomic command; here ‘precisely’ means that you should be able to get an unambiguous answer to any question. The section “Non-Atomic Semantics of Spec” in handout 17 on formal concurrency describes the meaning of a non-atomic command.

Spec’s notation for commands, that is, for changing the state, is derived from Edsger Dijkstra’s guarded commands (E. Dijkstra, A Discipline of Programming, Prentice-Hall, 1976) as extended by Greg Nelson (G. Nelson, A generalization of Dijkstra’s calculus, ACM TOPLAS 11, 4, Oct. 1989, pp 517-561). The notation for expressions is derived from mathematics.

This handout starts with a discussion of specifications and how to write them, with many small examples of Spec. Then there is an outline of the Spec language, followed by three extended examples of specs and code. At the end are two handy tear-out one-page summaries, one of the language and one of the official POCS strategy for writing specs and code.

In the language outline, the parts in small type describe less important features, and you can skip them on first reading.

What is a specification for?

The purpose of a specification is to communicate precisely all the essential facts about the behavior of a system. The important words in this sentence are:

communicate The spec should tell both the client and the implementer what each needs to know.

precisely We should be able to prove theorems or compile machine instructions based on the spec.

essential Unnecessary requirements in the spec may confuse the client or make it more expensive to implement the system.

behavior We need to know exactly what we mean by the behavior of the system.

Communication

Spec mediates communication between the client of the system and its implementer. One way to view the spec is as a contract between these parties:

The client agrees to depend only on the system behavior expressed in the spec; in return it only has to read the spec, and it can count on the implementer to provide a system that actually does behave as the spec says it should.

The implementer agrees to provide a system that behaves according to the spec; in return it is free to arrange the internals of the system however it likes, and it does not have to deliver anything not laid down in the spec.

Usually the implementer of a spec is a programmer, and the client is another programmer. Usually the implementer of a program is a compiler or a computer, and the client is a programmer.

Usually the system that the implementer provides is called an implementation, but in this course we will call it code for short. It doesn’t have to be C or Java code; we will give lots of examples of code in Spec which would still require a lot of work on the details of data structures, memory allocation, etc. to turn it into an executable program. You might wonder what good this kind of high-level code is. It expresses the difficult parts of the design clearly, without the straightforward details needed to actually make it run.

Behavior

What do we mean by behavior? In real life a spec defines not only the functional behavior of the system, but also its performance, cost, reliability, availability, size, weight, etc. In this course we will deal with these matters informally if at all. The Spec language doesn’t help much with them.

Spec is concerned only with the possible state transitions of the system, on the theory that the possible state transitions tell the complete story of the functional behavior of a digital system. So we make the following definitions:

A state is the values of a set of names (for instance, x=3, color=red).

A history is a sequence of states such that each pair of adjacent states is a transition of the system (for instance, x=1; x=2; x=5 is the history if the initial state is x=1 and the transitions are “if x = 1 then x := x + 1” and “if x = 2 then x := 2 * x + 1”).

A behavior is a set of histories (a non-deterministic system can have more than one history, usually at least one for every possible input).

How can we specify a behavior?

One way to do this is to just write down all the histories in the behavior. For example, if the state just consists of a single integer, we might write

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

...

1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2

....

1 2 3 4 5 1 2 3 1 2 3 4 5 6 7 8 9 10

The example reveals two problems with this approach:

The sequences are long, and there are a lot of them, so it takes a lot of space to write them down. In fact, in most cases of interest the sequences are infinite, so we can’t actually write them down.

It isn’t too clear from looking at such a set of sequences what is really going on.

Another description of this set of sequences from which these examples are drawn is “18 integers, each one either 1 or one more than the preceding one.” This is concise and understandable, but it is not formal enough either for mathematical reasoning or for directions to a computer.

Precise

In Spec the set of sequences can be described in many ways, for example, by the expression

{q: SEQ Int | q.size = 18

/\ (ALL i: Int | 0

q(i) = 1 \/ (i > 0 /\ q(i) = q(i-1) + 1)) }

Here the expression in {...} is very close to the usual mathematical notation for defining a set. Read it as “The set of all q which are sequences of integers such that q.size = 18 and ...”. Spec sequences are indexed from 0. The (ALL ...) is a universally quantified predicate, and ==> stands for implication, since Spec uses the more familiar => for ‘then’ in a guarded command. Throughout Spec the ‘|’ symbol separates a declaration of some new names and their types from the scope in which they are meaningful.

Alternatively, here is a state machine that generates the sequences we want. We specify the transitions of the machine by starting with primitive assignment commands and putting them together with a few kinds of compound commands. Each command specifies a set of possible transitions.

VAR i, j |

> ;

DO BEGIN i := 1 [] i := i+1 END; Output(i); j := j+1 >> OD

Here there is a good deal of new notation, in addition to the familiar semicolons, assignments, and plus signs.

VAR i, j | introduces the local variables i and j with arbitrary values. Because ; binds more tightly than |, the scope of the variables is the rest of the example.

The > brackets delimit the atomic actions or transitions of the state machine. All the changes inside these brackets happen as one transition of the state machine.

j < 18 => ... is a transition that can only happen when j < 18. Read it as “if j < 18 then ...”. The j < 18 is called a guard. If the guard is false, we say that the entire command fails.

i := 1 [] i := i + 1 is a non-deterministic transition which can either set i to 1 or increment it. Read [] as ‘or’.

The BEGIN ... END brackets are just brackets for commands, like { ... } in C. They are there because => binds more tightly than the [] operator inside the brackets; without them the meaning would be “either set i to 1 if j < 18 or increment i and j unconditionally”.

Finally, the DO ... OD brackets mean: repeat the ... transition as long as possible. Eventually j becomes 18 and the guard becomes false, so the command inside the DO ... OD fails and can no longer happen.

The expression approach is better when it works naturally, as this example suggests, so Spec has lots of facilities for describing values: sequences, sets, and functions as well as integers and booleans. Usually, however, the sequences we want are too complicated to be conveniently described by an expression; a state machine can describe them much more easily.

State machines can be written in many different ways. When each transition involves only simple expressions and changes only a single integer or boolean state variable, we think of the state machine as a program, since we can easily make a computer exhibit this behavior. When there are transitions that change many variables, non-deterministic transitions, big values like sequences or functions, or expressions with quantifiers, we think of the state machine as a spec, since it may be much easier to understand and reason about it, but difficult to make a computer exhibit this behavior. In other words, large atomic actions, non-determinism, and expressions that compute sequences or functions are hard to code. It may take a good deal of ingenuity to find code that has the same behavior but uses only the small, deterministic atomic actions and simple expressions that are easy for the computer.

Essential

The hardest thing for most people to learn about writing specs is that a spec is not a program. A spec defines the behavior of a system, but unlike a program it need not, and usually should not, give any practical method for producing this behavior. Furthermore, it should pin down the behavior of the system only enough to meet the client’s needs. Details in the spec that the client doesn’t need can only make trouble for the implementer.

The example we just saw is too artificial to illustrate this point. To learn more about the difference between a spec and code consider the following:

CONST eps := 10**-8

APROC SquareRoot0(x: Real) -> Real =

RET y >>

(Spec as described in the reference manual doesn’t have a Real data type, but we’ll add it for the purpose of this example.)

The combination of VAR and => is a very common Spec idiom; read it as “choose a y such that Abs(x - y*y) < eps and do RET y”. Why is this the meaning? The VAR makes a choice of any Real as the value of y, but the entire transition on the second line cannot occur unless the guard Abs(x - y*y) < eps is true. Hence the VAR must choose a value that satisfies the guard.

What can we learn from this example? First, the result of SquareRoot0(x) is not completely determined by the value of x; any result whose square is within eps of x is possible. This is why SquareRoot0 is written as a procedure rather than a function; the result of a function has to be determined by the arguments and the current state, so that the value of an expression like f(x) = f(x) will be true. In other words, SquareRoot0 is non-deterministic.

Why did we write it that way? First of all, there might not be any Real (that is, any floating-point number of the kind used to represent Real) whose square exactly equals x. We could accommodate this fact of life by specifying the closest floating-point number.[8] Second, however, we may not want to pay for code that gives the closest possible answer. Instead, we may settle for a less accurate answer in the hope of getting the answer faster.

You have to make sure you know what you are doing, though. This spec allows a negative result, which is perhaps not what we really wanted. We could have written (highlighting changes with boxes):

APROC SquareRoot1(x: Real) -> Real =

= 0 /\ Abs(x - y*y) < eps => RET y >>

to rule that out. Also, the spec produces no result if x < 0, which means that SquareRoot1(-1) will fail (see the section on commands for a discussion of failure). We might prefer a total function that raises an exception:

APROC SquareRoot2(x: Real) -> Real RAISES {undefined} =

= 0 => VAR y : Real | y >= 0 /\ Abs(x - y*y) < eps => RET y

[*] RAISE undefined >>

The [*] is ‘else’; it does its second operand iff the first one fails. Exceptions in Spec are much like exceptions in clu. An exception is contagious: once started by a RAISE it causes any containing expression or command to yield the same exception, until it runs into an exception handler (not shown here). The RAISES clause of a routine declaration must list all the exceptions that the procedure body can generate, either by RAISES or by invoking another routine.

Code for this spec would look quite different from the spec itself. Instead of the existential quantifier implied by the VAR y, it would have an algorithm for finding y, for instance, Newton’s method. In the algorithm you would only see operations that have obvious codes in terms of the load, store, arithmetic, and test instructions of a computer. Probably the code would be deterministic.

Another way to write these specs is as functions that return the set of possible answers. Thus

FUNC SquareRoots1(x: Real) -> SET Real =

RET {y : Real | y >= 0 /\ Abs(x - y*y) < eps}

Note that the form inside the {...} set constructor is the same as the guard on the RET. To get a single result you can use the set’s choose method: SquareRoots1(2).choose.[9]

In the next section we give an outline of the Spec language. Following that are three extended examples of specs and code for fairly realistic systems. At the end is a one-page summary of the language.

An outline of the Spec language

The Spec language has two main parts:

• An expression describes how to compute a result (a value or an exception) as a function of other values: either literal constants or the current values of state variables.

• A command describes possible transitions of the state variables. Another way of saying this is that a command is a relation on states: it allows a transition from s1 to s2 iff it relates s1 to s2.

Both are based on the state, which in Spec is a mapping from names to values. The names are called state variables or simply variables: in the sequence example above they are i and j. Actually a command relates states to outcomes; an outcome is either a state (a normal outcome) or a state together with an exception (an exceptional outcome).

There are two kinds of commands:

• An atomic command describes a set of possible transitions, or equivalently, a set of pairs of states, or a relation between states. For instance, the command describes the transitions i=1→i=2, i=2→i=3, etc. (Actually, many transitions are summarized by i=1→i=2, for instance, (i=1, j=1)→(i=2, j=1) and (i=1, j=15)→(i=2, j=15)). If a command allows more than one transition from a given state we say it is non-deterministic. For instance, on page 3 the command BEGIN i := 1 [] i := i + 1 END allows the transitions i=2→i=1 and i=2→i=3, with the rest of the state unchanged.

• A non-atomic command describes a set of sequences of states (by contrast with the set of pairs for an atomic command). More on this below.

A sequential program, in which we are only interested in the initial and final states, can be described by an atomic command.

The meaning of an expression, which is a function from states to values (or exceptions), is much simpler than the meaning of an atomic command, which is a relation between states, for two reasons:

• The expression yields a single value rather than an entire state.

• The expression yields at most one value, whereas a non-deterministic command can yield many final states.

An atomic command is still simple, because its meaning is just a relation between states. The relation may be partial: in some states there may be no way to execute the command. When this happens we say that the command is not enabled in those states. As we saw, the relation is not necessarily a function, since the command may be non-deterministic.

A non-atomic command is much more complicated than an atomic command, because:

• Taken in isolation, the meaning of a non-atomic command is a relation between an initial state and a history. A history is a whole sequence of states, much more complicated than a single final state. Again, many histories can stem from a single initial state.

• The meaning of the (parallel) composition of two non-atomic commands is not any simple combination of their relations, such as the union, because the commands can interact if they share any variables that change.

These considerations lead us to describe the meaning of a non-atomic command by breaking it down into its atomic subcommands and connecting these up with a new state variable called a program counter. The details are somewhat complicated; they are sketched in the discussion of atomicity below, and described in handout 17 on formal concurrency.

The moral of all this is that you should use the simpler parts of the language as much as possible: expressions rather than atomic commands, and atomic commands rather than non-atomic ones. To encourage this style, Spec has a lot of syntax and built-in types and functions that make it easy to write expressions clearly and concisely. You can write many things in a single Spec expression that would require a number of C statements, or even a loop. Of course, code with a lot of concurrency will necessarily have more non-atomic commands, but this complication should be put off as long as possible.

Organizing the program

In addition to the expressions and commands that are the core of the language, Spec has four other mechanisms that are useful for organizing your program and making it easier to understand.

• A routine is a named computation with parameters, in other words, an abstraction of the computation. Parameters are passed by value. There are four kinds of routine:

A function (defined with FUNC) is an abstraction of an expression.

An atomic procedure (defined with APROC) is an abstraction of an atomic command.

A general procedure (defined with PROC) is an abstraction of a non-atomic command.

A thread (defined with THREAD) is the way to introduce concurrency.

• A type is a highly stylized assertion about the set of values that a name or expression can assume. A type is also a convenient way to group and name a collection of routines, called its methods, that operate on values in that set.

• An exception is a way to report an unusual outcome.

• A module is a way to structure the name space into a two-level hierarchy. An identifier i declared in a module m has the name m.i throughout the program. A class is a module that can be instantiated many times to create many objects, much like a Java class.

A Spec program is some global declarations of variables, routines, types, and exceptions, plus a set of modules each of which declares some variables, routines, types, and exceptions.

The next two sections describe things about Spec’s expressions and commands that may be new to you. They should be enough for the Spec you will read and write in this course, but they don’t answer every question about Spec; for those answers, read the reference manual and the handouts on Spec semantics.

Paragraphs in small print contain material that you might want to skip on first reading.

There is a one-page summary of the Spec language at the end of this handout.

Expressions, types, and relations

Expressions ;are for computing functions of the state.

|A Spec expression is | |and its value is |

|a constant | |the constant |

|a variable | |the current value of the variable |

|an invocation of a function on an argument that is some | |the value of the function at the value of the argument |

|sub-expression | | |

There are no side-effects; those are the province of commands. There is quite a bit of syntactic sugar for function invocations. An expression may be undefined in a state; if a simple command evaluates an undefined expression, the command fails (see below).

Types

A Spec type defines two things:

A set of values; we say that a value has the type if it’s in the set. The sets are not disjoint. If T is a type, T.all is its set of values.

A set of functions called the methods of the type. There is convenient syntax v.m for invoking method m on a value v of the type. A method m of type T is lifted to a method m of a set of T’s, a function U->T, or a relation from U to T in the obvious way, by applying it to the set elements or the result of the function or relation, unless overridden by a different m in the definition of the higher type. Thus if int has a square method, {2, 3, 4}.square = {4, 9, 16}. We’ll see that this is a form of function composition.

Spec is strongly typed. This means that you are supposed to declare the types of your variables, just as you do in Java. In return the language defines a type for every expression[10] and ensures that the value of the expression always has that type. In particular, the value of a variable always has the declared type. You should think of a type declaration as a stylized comment that has a precise meaning and can be checked mechanically.

If Foo is a type, you can omit it in a declaration of the identifiers foo, foo1, foo' etc. Thus

VAR int1, bool2, char' | ...

is short for

VAR int1: Int, bool2: Bool, char': Char | ...

Note that this can be confusing in a declaration like t, u: Int, where u has type U, not type Int.

If e IN T.all then e AS T is an expression with the same value and type T; otherwise it’s undefined. You can write e IS T for e IN T.all.

Spec has the usual types:

Int, Nat (non-negative Int), Bool

sets SET T

functions T->U

relations T->>U

records or structs [f1: T1, f2: T2, ...]

tuples (T1, T2, ...)

variable-length arrays called sequences, SEQ T

A sequence is actually a function whose domain is {0, 1, ..., n-1} for some n. A record is actually a function whose domain is the field names, as strings. In addition to the usual functions like "+" and "\/", Spec also has some less usual operations on these types, which are valuable when you want to suppress code detail; they are called constructors and combinations and are described below.

You can make a type with fewer values using SUCHTHAT. For example,

TYPE T = Int SUCHTHAT 0 e1 [*] e2), which yields e1 if predicate is true and e2 otherwise. If you omit [*] e2, the result is undefined if predicate is false. Because => denotes if ... then, implication is written ==>.

Here is a list of all the built-in operators, which also gives their precedence, and a list of the built-in methods. You should read these over so that you know the vocabulary. The rest of this section explains many of these and gives examples of their use.

Note that any lattice (any partially ordered set with least upper bound or max, and greatest lower bound or min, defined on any pair of elements) has operators /\ (max) and \/ (min). Booleans, sets, and relations are examples of lattices. Any totally ordered set such as Int is a lattice.

Binary operators

|Op |Prec. |Argument/result types |Operation |

|** |8 |(Int, Int)->Int |exponentiate |

|* |7 |(Int, Int)->Int |multiply |

| | |(T->U, U->V)->(T->V) |function or relation composition: (\t | e2(e1(t)) |

|/ |7 |(Int, Int)->Int |divide |

|// |7 |(Int, Int)->Int |remainder |

|+ |6 |(Int, Int)->Int |add |

| | |(SEQ T, SEQ T)->SEQ T |concatenation |

| | |(T->U, T->U)->(T->U) |function overlay: (\t | (e2!t => e2(t) [*] e1(t)) |

|- |6 |(Int, Int)->Int |subtract |

| | |(SET T, SET T)->SET T |set difference |

| | |(SEQ T, SEQ T)->SEQ T |multiset difference |

|! |6 |(T->U, T)->Bool |function is defined at arg |

|!! |6 |(T->U, T)->Bool |function defined, no exception at arg |

|.. |5 |(Int, Int)->SEQ Int |subrange: {e1, e1+1, ..., e2} |

||| |5 |(SEQ T, SEQ U)->SEQ(T,U) |zip: pair of sequences to sequence of pairs |

|Bool |less than or equal |

| | |(SET T, SET T)->Bool |subset |

| | |(SEQ T, SEQ T)->Bool |prefix: e2.restrict(e1.dom) = e1 |

|< |4 |(T, T)->Bool, T with |4 |(T, T)->Bool, T with = |4 |(T, T)->Bool, T with Bool |can’t override by WITH |

|# |4 |(Any, Any)->Bool |not equal; can’t override by WITH |

|Bool |conditional and* |

| | |(T, T)->T |max, for any lattice; example: set/relation intersection |

|\/ |1 |(Bool, Bool)->Bool |conditional or* |

| | |(T, T)->T |min, for any lattice; example: set/relation union |

|==> |0 |(Bool, Bool)->Bool |conditional implies* |

|op |5 |(T, U)->V |op none of the above: T."op"(e1, e2) |

The “*” on the conditional Boolean operators means that, unlike all other operators, they don’t evaluate their second argument if the first one determines the result. Thus f(x) /\ g(x) is false if f(x) is false, even if g(x) is undefined.

Unary operators

|Op |Prec. |Argument/result types |Operation |

|- |6 |Int->Int |negation |

|~ |3 |Bool->Bool |complement |

| | |SET T->SET T |set complement |

| | |(T->>U)->(T->>U) |relation complement |

|op |5 |T->U |op none of the above: T."op"(e1) |

Relations

A relation r is a generalization of a function: an arbitrary set of ordered pairs, defined by a predicate, a total function from pairs to Bool. Thus r can relate an element of its domain to any number of elements of its range (including none). Like a function, r has dom, rng, and inv methods (the inverse is obtained just by flipping the ordered pairs), and you can compose relations with *. Note that in general r * r.inv is not the identity; for this reason many people prefer to call it the “transpose” or “converse”. You can also take the complement, union, and intersection of two relations that have the same type, and compare relations with .

The relation r given by the set of ordered pairs s = {("a", 1), ("b", 2), ("a", 3)}; r = s.pred.pToR; that is, turn the set into a predicate on ordered pairs and the predicate into a relation. Its inverse r.inv = {(1, "a"), (2, "b"), (3, "a")}, which is the sequence {"a", "b", "a"}. Its domain r.dom = {"a", "b"}; its range r.rng = {1, 2, 3}.

The advantage of relations is simplicity and generality; for example, there’s no notion of “undefined” for relations. The drawback is that you can’t write r(x) (although you can write {x} * r for the set of values related to x by r; see below).

A relation r has methods

r.setF to turn it into a set function: r.setF(x) is the set of elements that r relates to x. This is total. Int." typeX} % argument doesn't typecheck

) ) % end of the two lambdas

We leave the meaning of a routine with no result as an exercise.

Invocation and LAMBDA expressions

We have already given in MC the meaning of invocations in commands, so we can use MC to deal with invocations in expressions. Here is the fragment of the definition of ME that deals with an E that is an invocation e1(e2) of a function. It is written in terms of the meaning MC(C«e1(e2)») of the invocation as a command, which is defined above. The meaning of the command is an atomic transition aTr, a predicate on an initial state and an outcome of the routine. In the outcome the value of the pseudo-name $a is the value returned by the function. The definition given here discards any side-effects of the function; in fact, in a legal Spec program there can be no side-effects, since functions are not allowed to assign to non-local variables or call procedures.

FUNC ME(e) -> (S -> (V + X)) =

IF

...

[] VAR e1, e2 | e = E« e1(e2) » =>

% if E is an invocation its meaning is this function from states to values

VAR aTr := MC(C« e1(e2) ») |

RET ( LAMBDA (s) -> V =

% the command must have a unique outcome, that is, aTr must be a

% function at s. See Relation in section 9 of the reference manual

VAR o := aTr.func(s) | RET (~o.isX => o("$a") [*] o("$x")) )

...

FI

The result of the expression is the value of $a in the outcome if it is normal, the value of $x if it is exceptional. If the invocation has no outcome or more than one outcome, ME(e)(s) is undefined.

The fragment of ME for LAMBDA uses MR to get the meaning of a FUNC with the same signature and body. As we explained earlier, this meaning is a function from a state to a transition function, and it is the value of ME((LAMBDA ...)). The value of (LAMBDA ...), like the value of any expression, is the result of evaluating ME((LAMBDA ...)) on the current state. This yields a transition function as we expect, and that function captures the local state of the LAMBDA expression; this is standard static scoping. .

IF

...

[] VAR signature, c0 | e = E« (LAMBDA signature = c0) » =>

RET MR(R« FUNC id1 signature = c0 »)

...

FI

10. Performance

Overview

This is not a course about performance analysis or about writing efficient programs, although it often touches on these topics. Both are much too large to be covered, even superficially, in a single lecture devoted to performance. There are many books on performance analysis[24] and a few on efficient programs[25].

Our goal in this handout is more modest: to explain how to take a system apart and understand its performance well enough for most practical purposes. The analysis is necessarily rather rough and ready, but nearly always a rough analysis is adequate, often it’s the best you can do, and certainly it’s much better than what you usually see, which is no analysis at all. Note that performance analysis is not the same as performance measurement, which is more common.

What is performance? The critical measures are bandwidth and latency. We neglect other aspects that are sometimes important: availability (discussed later when we deal with replication), connectivity (discussed later when we deal with switched networks), and storage capacity

When should you work on performance? When it’s needed. Time spent speeding up parts of a program that are fast enough is time wasted, at least from any practical point of view. Also, the march of technology, also known as Moore’s law, means that in 18 months from March 2006 a computer will cost the same but be twice as fast[26] and have twice as much RAM and four times as much disk storage; in five years it will be ten times as fast and have 100 times as much disk storage. So it doesn’t help to make your system twice as fast if it takes two years to do it; it’s better to just wait. Of course it still might pay if you get the improvement on new machines as well, or if a 4 x speedup is needed.

How can you get performance? There are techniques for making things faster:

better algorithms,

fast paths for common cases, and

concurrency.

And there is methodology for figuring out where the time is going:

analyze and measure the system to find the bottlenecks and the critical parameters that determine its performance, and

keep doing so both as you improve it and when it’s in service.

As a rule, a rough back-of-the-envelope analysis is all you need. Putting in a lot of detail will be a lot of work, take a lot of time, and obscure the important points.

What is performance: bandwidth and latency

Bandwidth and latency are usually the important metrics. Bandwidth tells you how much work gets done per second (or per year), and latency tells you how long something takes from start to finish: to send a message, process a transaction, or referee a paper. In some contexts it’s customary to call these things by different names: throughput and response time, or capacity and delay. The ideas are exactly the same.

Here are some examples of communication bandwidth and latency on a single link. Note that all the numbers are in bytes/sec; it’s traditional to quote bandwidths for some interconnects in bits/sec, so be wary of numbers you read.

|Medium |Link |Bandwidth |Latency |Width |

|Pentium 4 chip |on-chip bus |30 |GB/s |.4 |ns |64 |

|PC board |Rambus bus |1.6 |GB/s |75 |ns |16 |

| |PCI I/O bus |533 |MB/s |200 |ns |32 |

|Wires |Serial ATA (SATA) |300 |MB/s |200 |ns |1 |

| |SCSI |40 |MB/s |500 |ns |32 |

|LAN |Gigabit Ethernet |125 |MB/s |100 + |µs |1 |

| |Fast Ethernet |12.5 |MB/s |100 + |µs |1 |

| |Ethernet |1.25 |MB/s |100 + |µs |1 |

Here are examples of communication bandwidth and latency through a switch that interconnects multiple links.

|Medium |Switch |Bandwidth |Latency |Links |

|Pentium 4 chip |register file |180 |GB/s |.4 |ns |6 |

|Wires |Cray T3E |122 |GB/s |1 |µs |2K |

|LAN |Ethernet switch |4 |GB/s |4–100 |µs |32 |

|Copper pair |Central office |80 |MB/s |125 |µs |50K |

Finally, here are some examples of other kinds of work, different from simple communication.

|Medium |Bandwidth |Latency |

|Disk |40 |MB/s |10 |ms |

|RPC on Giganet with VIA |30 |calls/ms |30 |µs |

|RPC |3 |calls/ms |1 |ms |

|Airline reservation transactions |10000 |trans/s |1 |sec |

|Published papers |20 |papers/yr |2 |years |

Specs for performance

How can we put performance into our specs? In other words, how can we specify the amount of real time or other resources that an operation consumes? For resources like disk space that are controlled by the system, it’s quite easy. Add a variable spaceInUse that records the amount of disk space in use, and to specify that an operation consumes no more than max space, write

>

This is usually what you want, rather than saying exactly how much space is consumed, which would restrict the code too much.

Doing the same thing for real time is a bit trickier, since we don’t usually think of the advance of real time as being under the control of the system. The spec, however, has to put a limit on how much time can pass before an operation is complete. Suppose we have a procedure P. We can specify TimedP that takes no more than maxPLatency to complete as follows. The variable now records the current time, and deadlines records a set of latest completion times for operations in progress. The thread Clock advances now, but not past a deadline. An operation like TimedP sets a deadline before it starts to run and clears it when it is done.

VAR now : Time

deadlines: SET Time

THREAD Clock() = DO now < deadlines.min => now + := 1 [] SKIP OD

PROC TimedP() = VAR t : Time

deadlines := deadlines + {t} >>;

P();

>

This may seem like an odd way of doing things, but it does allow exactly the sequences of transitions that we want. The alternative is to construct P so that it completes within maxPLatency, but there’s no straightforward way to do this.

Often we would like to write a probabilistic performance spec; for example, service time is drawn from a normal distribution with given mean and variance. There’s no way to do this directly in Spec, because the underlying model of non-deterministic state machines has no notion of probability. What we can do is to keep track of actual service times and declare a failure if they get too far from the desired form. Then you can interpret the spec to say: either the observed performance is a reasonably likely consequence of the desired distribution, or the system is malfunctioning.

How to get performance: Methodology

First you have to choose the right scale for looking at the system. Then you have to model or analyze the system, breaking it down into a few parts that add up to the whole, and measure the performance of the parts.

Choosing the scale

The first step in understanding the performance of a system is to find the right scale on which to analyze it. The figure shows the scales from the processor clock to an Internet access; there is a range of at least 50 million in speed and 50 million in quantity. Usually there is a scale that is the right one for understanding what’s going on. For the performance of an inner loop it might be the system clock, for a simple transaction system the number of disk references, and for a Web browser the number of IP packets.

In practice, systems are not deterministic. Even if there isn’t inherent non-determinism caused by unsynchronized clocks, the system is usually too complex to analyze in complete detail. The way to simplify it is to approximate. First find the right scale and the right primitives to count, ignoring all the fine detail. Then find the critical parameters that govern performance at that scale: number of RPC’s per transaction, cache miss rate, clock ticks per instruction, or whatever. In this way you should be able to find a simple formula that comes within 20% of the observed performance, and usually this is plenty good enough.

|[pic] |

|Scales of interconnection. Relative speed and size are in italics. |

For example, in the 1994 election DEC ran a Web server that provided data on the California election. It got about 80k hits/hour, or 20/sec, and it ran on a 200 MIPS machine. The data was probably all in memory, so there were no disk references. A hit typically returns about 2 KB of data. So the cost was about 10M instructions/hit, or 5K instructions/byte returned. Clearly this was not an optimized system.

By comparison, a simple debit-credit transaction (the TPC-A benchmark) when carefully coded does slightly more than two disk i/o’s per transaction (these are to read and write per-account data that won’t fit in memory). If carefully coded it takes about 100K instructions. So on a 2000 MIPS machine it will consume 50 μs of compute time. Since two disk i/o’s is 20 ms, it takes 400 disks to keep up with this CPU for this application. Since this is not too reasonable, engineers have responded by coding transactions less carefully, taking advantage of the fact that instructions are so cheap.

As a third example, consider sorting 10 million 64 bit numbers; the numbers start on disk and must end up there, but you have room for the whole 80 MB in memory. So there’s 160 MB of disk transfer plus the in-memory sort time, which is n log n comparisons and about half that many swaps. A single comparison and half swap might take 10 instructions with a good code for Quicksort, so this is a total of 10 * 10 M * 24 = 2.4 G instructions. Suppose the disk system can transfer 80 MB/sec and the processor runs at 200 MIPS. Then the total time is 2 sec for the disk plus 1.2 sec for the computing, or 3.2 sec, less any overlap you can get between the two phases. With considerable care this performance can be achieved. On a parallel machine you can do perhaps 30 times better.[27]

Here are some examples of parameters that might determine the performance of a system to first order: cache hit rate, fragmentation, block size, message overhead, message latency, peak message bandwidth, working set size, ratio of disk reference time to message time.

Modeling

Once you have chosen the right scale, you have to break down the work at that scale into its component parts. The reason this is useful is the following principle:

[pic]

Most people who have been to school in the last 20 years seem not to believe this. They think the ‘system effect’ is so large that knowing the cost of a and b doesn’t help at all in understanding the cost of x. But they are wrong. Your goal should be to break down the work into a small number of parts, between two and ten. Adding up the cost of the parts should give a result within 10% of the measured cost for the whole.

If it doesn’t then either you got the parts wrong (very likely), or there actually is an important system effect. This is not common, but it does happen. Such effects are always caused by contention for resources, but this takes two rather different forms:

• Thrashing in a cache, because the sum of the working sets of the parts exceeds the size of the cache. The important parameter is the cache miss rate. If this is large, then the cache miss time and the working set are the things to look at. For example, SQL server on Windows NT running on a DEC Alpha 21164 in 1997 executes .25 instructions/cycle, even though the processor chip is capable of 2 instructions/cycle. The reason turns out to be that the instruction working set is much larger than the instruction cache, so that essentially every block of 4 instructions (16 bytes or one cache line) causes a cache miss, and the miss takes 64 ns, which is 16 4 ns cycles, or 4 cycles/instruction.

• Clashing or queuing for a resource that serves one customer at a time (unlike a cache, which can take away the resource before the customer is done). The important parameter is the queue length. It’s important to realize that a resource need not be a physical object like a CPU, a memory block, a disk drive, or a printer. Any lock in the system is a resource on which queuing can occur. Typically the physical resources are instrumented so that it’s fairly easy to find the contention, but this is often not true for locks. In the Alta Vista web search engine, for example, CPU and disk utilization were fairly low but the system was saturated. It turned out that queries were acquiring a lock and then page faulting; during the page fault time lots of other queries would pile up waiting for the lock and unable to make progress.

In the section on techniques we discuss how to analyze both of these situations.

Measuring

The basic strategy for measuring is to count the number of times things happen and observe how long they take. This can be done by sampling (what most profiling tools do) or by logging significant events such as procedure entries and exits. Once you have collected the data, you can use statistics or graphs to present it, or you can formulate a model of how it should be (for example, time in this procedure is a linear function of the first parameter) and look for disagreements between the model and reality.[28] The latter technique is especially valuable for continuous monitoring of a running system. Without it, when a system starts performing badly in service it’s very difficult to find out why.

Measurement is usually not useful without a model, because you don’t know what to do with the data. Sometimes an appropriate model just jumps out at you when you look at raw profile data, but usually you have to think about it and try a few things. This is just like any branch of science: without a theory you can’t make sense of the data.

How to get performance: Techniques

There are three main ways to make your program run faster: use a better algorithm, find a common case that can be made to run fast, or use concurrency to work on several things at once.

Algorithms

There are two interesting things about an algorithm: the ‘complexity’ and the ‘constant factor’. An algorithm that works on n inputs can take roughly k (constant) time, or k log n (logarithmic), or k n (linear), or k n2 (quadratic), or k 2n (exponential). The k is the constant factor, and the function of n is the complexity. Usually these are ‘asymptotic’ results, which means that their percentage error gets smaller as n gets bigger. Often a mathematical analysis gives a worst-case complexity; if what you care about is the average case, beware. Sometimes a ‘randomized’ algorithm that flips coins internally can make the average case overwhelmingly likely.

For practical purposes the difference between k log n time and constant time is not too important, since the range over which n varies is likely to be 10 to 1M, so that log n varies only from 3 to 20. This factor of 6 may be much less than the change in k when you change algorithms. Similarly, the difference between k n and k n log n is usually not important. But the differences between constant and linear, between linear and quadratic, and between quadratic and exponential are very important. To sort a million numbers, for example, a quadratic insertion sort takes a trillion operations, while the n log n Quicksort takes only 20 million in the average case (unfortunately the worst case for Quicksort is also quadratic). On the other hand, if n is only 100, then the difference among the various complexities (except exponential) may be less important than the values of k.

Another striking example of the value of a better algorithm is ‘multi-grid’ methods for solving the n-body problem: lots of particles (atoms, molecules or asteroids) interacting according to some force law (electrostatics or gravity). By aggregating distant particles into a single virtual particle, these methods reduce the complexity from n2 to n log n, so that it is feasible to solve systems with millions of particles. This makes it practical to compute the behavior of complex chemical reactions, of currents flowing in an integrated circuit package, or of the solar system.

Fast path

If you can find a common case, you can try to do it fast. Here are some examples.

Caching is the most important: memory, disk (virtual memory, database buffer pool), web cache, memo functions (also called ‘dynamic programming’), ...

Receiving a message that is an expected ack or the next message in sequence.

Acquiring a lock when no one else holds it.

Normal arithmetic vs. overflow.

Inserting a node in a tree at a leaf, vs. splitting a node or rebalancing the tree.

Here is the basic analysis for a fast path.

1 = fast time, 1 207.46.130.149 -> {router output port, LAN address}

a/b/c/1026 -> INode/1026 -> DA/2 -> {cylinder, head, sector, byte 2}

Sometimes people talk about “descriptive names”, which are queries in a database. We will see that these are readily encompassed within the framework of path names. That is a formal relationship, however. There is an important practical difference between a designator for a single entity, such as lampson@, and a description or query such as “everyone at MIT’s CSAIL whose research involves parallel computing”. The difference is illuminated by the comparison between the name eecsfaculty@eecs.mit.edu and the query “the faculty members in MIT’s EECS department”. The name is probably maintained with some care; it’s anyone’s guess how reliable the answer to the query is. When using a name, it is wise to consider whether it is a designator or a description.

This is not to say that descriptions or queries are bad. On the contrary, they are very valuable, as any one knows who has ever used a web search engine. However, they usually work well only when a person examines the results carefully.

In the remainder of this handout we examine the specs for the two ways of describing a name space that we introduced earlier: as a memory addressed by path names, and as a tree (or more generally a graph) of directories. The two ways are closely related, but they give rise to somewhat different specs. Then we study the recursive structure of name spaces and various ways of inducing a name space on a collection of values. This leads to a more abstract analysis of how the spec for a name space can vary, depending on the properties of the underlying values. We conclude our general treatment by examining how to name a name space. Finally, we give a large number of examples of name spaces; you might want to look at these first to get some more context.

Name space as memory

We can view a name space as an example of the memory abstraction we studied earlier. Recall that a memory is a partial map M = A -> V. Here we take A = PN and replace M with D (for directory). This kind of memory differs from the byte-addressable physical memory of a computer in several ways[32]:

• The map is partial.

• The current value of the domain (that is, which names are defined) is interesting.

• The domain is changing.

• PN’s with the same prefix are related (though not as much as in the second view of name spaces).

Here are some examples of name spaces that can naturally be viewed as memories:

The Simple Network Management Protocol (SNMP) is used to manage components of the Internet. It uses path names (rooted in IP addresses) to name values, and the basic operations are to read and write a single named value.

Several file systems use a single large table to map the path name of a file to the extents that represent it.

MODULE MemNames0[V] EXPORT Read, Write, Remove, Enum, Next, Rename =

TYPE N = String % Name

PN = SEQ N WITH {" (EXISTS u' | u1 C u' /\ u’ A u2) )

This says that any result that you could get by doing A; C you could also get by doing C; A. Note that it’s OK for C; A to have more transitions, since we want to show that A; C; B implements C; >, not that they are equivalent. This is not just nit-picking; if C acquires a lock that A holds, there is no transition from A to C in the first case.

It seems reasonable to do the proof by making A simulate the empty trace and B simulate , since we know more about A than about B; every other command simulates itself.

[pic]

So we make AR the identity everywhere except at (, where it relates any state u that can be reached from s by A to s. This expresses the intention that at ( we haven’t yet done A in S, but we have done A in T. (Since A may take many states to s, this can’t just be an abstraction function.) We write u ~ s for “AR relates u to s”. Precisely, we say that u ~ s if

u("h.$pc") ( ( /\ s = u

\/ u("h.$pc") = ( /\ s A u.

Why is this an abstraction relation? It certainly relates an initial state to an initial state, and it certainly works for any transition u -> u' that stays away from (, that is, in which u("h.$pc") ≠ ( and u'("h.$pc") ≠ (, since the abstract and concrete states are the same. What about transitions that do involve (?

• If h.$pc changes to ( then we must have executed A. The picture is

[pic]

The abstract trace is empty, so the abstract state doesn’t change: s = s'. Also, s' = u because only equal states are related when h.$pc # (. But we executed A, so u A u', so s' ~ u' because of the equalities.

• If h.$pc starts at ( then the command must be either B or some C that commutes with A. If the command is B, then the picture is

[pic]

To show the top relation, we have to show that there exists an s0 such that s A s0 and s0 B s', by the meaning of semicolon. But u has exactly this property, since s' = u'.

• If the command is C, then the picture is

[pic]

But this follows from the definition of ‘commutes’: we are given s, u, and u' related as shown, and we need s' related as shown, which is just what the definition gives us, with u1 = s, u2 = u', and u' = s'.

Examples of concurrency

This section contains a number of example specs and codes that illustrate various aspects of concurrency. The specs have large atomic actions that keep them simple. The codes have smaller atomic actions that reflect the realities of the machines on which they have to run. Some of the examples of code illustrate easy concurrency (that is, that use locks): RWLockImpl and BufferImpl. Others illustrate hard concurrency: SpinLock, Mutex2Impl, ClockImpl, MutexImpl, and ConditionImpl.

Incrementing a register

The first example involves incrementing a register that has Read and Write operations. Here is the unsurprising spec of the register, which makes both operations atomic:

MODULE Register EXPORT Read, Write =

VAR x : Int := 0

APROC Read() -> Int = >

APROC Write(i: Int) = >

END Register

To increment the register, we could use the following procedure:

PROC Increment() = VAR t: Int | t := Register.Read(); t := t + 1; Register.Write(t)

Suppose that, starting from the initial state where x = 0, n threads execute Increment in parallel. Then, depending on the interleaving of the low-level steps, the final value of the register could be anything from 1 to n. This is unlikely to be what was intended. Certainly this code doesn’t implement the spec

PROC Increment() = >

Exercise: Suppose that we weaken our atomicity assumptions to say that the value of a register is represented as a sequence of bits, and that the only atomic operations are reading and writing individual bits. Now what are the possible final states if n threads execute Increment in parallel?

Alternatively, consider a new module RWInc that explicitly supports Increment operations in addition to Read and Write. This might add the following (exported) procedure to the Register module:

PROC Increment() = x := x+1

Or, more explicitly:

PROC Increment() = VAR t: Int | >; >

Because of the fine grain of atomicity, it is still true that if n threads execute Increment in parallel then, depending on the interleaving of the low-level steps, the final value of the register could be anything from 1 to n. Putting the procedure inside the Register module doesn’t help. Of course, making Increment an APROC would certainly do the trick.

Mutexes

Here is a spec of a simple Mutex module, which can be used to ensure mutually exclusive execution of critical sections; it is copied from handout 14 on practical concurrency. The state of a mutex is nil if the mutex is free, and otherwise is the thread that holds the mutex.

CLASS Mutex EXPORT acq, rel =

VAR m : (Thread + Null) := nil

% Each mutex is either nil or the thread holding the mutex.

% The variable SELF is defined to be the thread currently making a transition.

APROC acq() = m := SELF; RET >>

APROC rel() = m := nil ; RET [*] HAVOC >>

END Mutex

If a thread invokes acq when m ≠ nil, then the body fails, This means that there’s no possible transition for that thread, and the thread is blocked, waiting at this point until the guard becomes true. If many threads are blocked at this point, then when m is set to nil, one is scheduled first, and it sets m to itself atomically; the other threads are still blocked.

The spec says that if a thread that doesn’t hold m does m.rel, the result is HAVOC. As usual, this means that the code is free to do anything when this happens. As we shall see in the SpinLock code below, one possible ‘anything’ is to free the mutex anyway.

Here is a simple use of a mutex m to make the Increment procedure atomic:

PROC Increment() = VAR t: Int |

m.acq; t := Register.Read(); t := t + 1; Register.Write(t); m.rel

This keeps concurrent calls of Increment from interfering with each other. If there are other write accesses to the register, they must also use the mutex to avoid interfering with threads executing Increment.

Spin locks

A simple way to code a mutex is to use a spin lock. The name is derived from the behavior of a thread waiting to acquire the lock—it “spins”, repeatedly attempting to acquire the lock until it is finally successful.

Here is incorrect code:

CLASS BadSpinLock EXPORT acq, rel =

TYPE FH = ENUM[free, held]

VAR fh := free

PROC acq() =

DO SKIP >> OD; % wait for fh = free

> % and acquire it

PROC rel() = >

END BadSpinLock

This is wrong because two concurrent invocations of acq could both find fh = free and subsequently both set fh := held and return.

Here is correct code. It uses a more complex atomic command in the acq procedure. This command corresponds to the atomic “test-and-set” instruction provided by many real machines to code locks. It records the initial value of the lock, and then sets it to held. Then it tests the initial value; if it was free, then this thread was successful in atomically changing the state of the lock from free to held. Otherwise some other thread must hold the lock, so we “spin”, repeatedly trying to acquire it until we succeed. The important difference in SpinLock is that the guard now involves only the local variable t, instead of the global variable fh in BadSpinLock. A thread acquires the lock when it is the one that changes it from free to held, which it checks by testing the value returned by the test-and-set.

CLASS SpinLock EXPORT acq, rel =

TYPE FH = ENUM[free, held]

VAR fh := free

PROC acq() = VAR t: FH |

DO >; IF t = free => RET [*] SKIP FI OD

PROC rel() = >

END SpinLock

Of course this code is not practical in general unless each thread has its own processor; it is used, however, in the kernels of most operating systems for computers with several processors. Later, in MutexImpl, we give practical code that queues a waiting thread.

The SpinLock code differs from the Mutex spec in another important way. It “forgets” which thread owns the mutex. The following ForgetfulMutex module is useful in understanding the SpinLock code—in ForgetfulMutex, the threads get forgotten, but the atomicity is the same as in Mutex.

CLASS ForgetfulMutex EXPORT acq, rel =

TYPE FH = ENUM[free, held]

VAR fh := free

PROC acq() = fh := held >>

PROC rel() = >

END ForgetfulMutex

Note that ForgetfulMutex releases a mutex regardless of which thread acquired it, and it does a SKIP if the mutex is already free. This is one of the behaviors permitted by the Mutex spec, which allows anything under these conditions.

Later we will show that SpinLock implements ForgetfulMutex and that ForgetfulMutex implements Mutex, from which it follows that SpinLock implements Mutex. We don’t give the abstraction function here because it involves the details of program counters.

Wait-free primitives

It’s also possible to implement spin locks with a wait-free primitive rather than with test-and-set, although this rather misses the point of wait-free synchronization, which is discussed informally in handout 14.

The simplest wait-free primitive is compare-and-swap (CAS), which is illustrated in the following code for acq. It stores new into fh (which is an address parameter in real life) and returns true if the current contents of fh is free, otherwise it is SKIP. Now acq has no atomicity brackets.

VAR x : Any

PROC acq() = DO VAR t := CAS(free, held); IF t => RET [*] SKIP FI OD

APROC CAS(old: Any, new: Any)-> Bool =

x := new; RET true [*] RET false >>

A more general form of compare-and-swap allows you to do an arbitrary computation on the old contents of a variable. It is called load-locked/store-conditional. The idea is that if anyone writes the variable in between the load-locked and the store-conditional, the store fails.

VAR lock : Bool := false % a global variable; could be per variable

APROC LL() -> Any = >

APROC SC(new: Any) -> BOOL = x := new; RET true [*] RET false >>

Now we can write acq:

PROC acq() = VAR fh’, OK: Bool |

DO fh’ := LL();

IF fh’ = free => OK := SC(held); IF OK => RET [*] SKIP FI

[*] SKIP FI

OD

Of course we can program CAS using LL/SC:

PROC CAS(old: Any, new: Any)-> Bool = VAR fh’, OK: Bool |

fh’ := LL(); IF fh’ = old => OK := SC(new); RET OK [*] RET false FI

We can also program operations such as incrementing a variable, either with CAS:

VAR OK: Bool := false | DO ~OK => i := x; OK := CAS(i, i+1) OD

or with LL/SC:

VAR OK: Bool := false | DO ~OK => i := LL(); OK := SC(i+1) OD

More generally, you can update an arbitrary data structure with an arbitrary function f by replacing i+1 in the CAS implementation with f(i). The way to think about this is that f computes a new version of i, and you install it if the version hasn’t changed since you started. This is a form of optimistic concurrency control; see handout 20 for a more general discussion of this subject. Like optimistic concurrency control in general, the approach runs the danger of doing a lot of work that you then have to discard, or of starving some of the threads because they never get done before other threads sneak in and change the version out from under them. There are clever tricks for minimizing this danger; the basic idea is to queue your f for some other thread to execute along with its own.

Read/write locks

Here is a spec of a module that provides locks with two modes, read and write, rather than the single mode of a mutex. Several threads can hold a lock in read mode, but only one thread can hold a lock in write mode, and no thread can hold a lock in read mode if some thread holds it in write mode. In other words, read locks can be shared, but write locks are exclusive; hence the locks are also known as ‘shared’ and ‘exclusive’.

CLASS RWLock EXPORT rAcq, rRel, wAcq, wRel =

TYPE ST = SET Thread

VAR r : ST := {}

w : ST := {}

APROC rAcq() = % Acquires r if no current write locks

HAVOC [*] w = {} => r \/ := {SELF} >>

APROC wAcq() = % Acquires w if no current locks

HAVOC [*] (r \/ w) = {} => w := {SELF} >>

APROC rRel() = % Releases r if the thread has it

HAVOC [*] r - := {SELF} >>

APROC wRel() =

HAVOC [*] w := {} >>

END RWLock

The following simple code is similar to ForgetfulMutex. It has the same atomicity as RWLock, but uses a different data structure to represent possession of the lock. Specifically, it uses a single integer variable rw to keep track of the number of readers (positive) or the existence of a writer (-1).

CLASS ForgetfulRWL EXPORT rAcq, rRel, wAcq, wRel =

VAR rw := 0

% >0 gives number of readers, 0 means free, -1 means one writer

APROC rAcq() = = 0 => rw + := 1 >>

APROC wAcq() = rw := -1 >>

APROC rRel() = >

APROC wRel() = >

END ForgetfulRWL

We will see later how to code ForgetfulRWL using a mutex.

Condition variables

Mutexes are used to protect shared variables. Often a thread h cannot proceed until some condition is true of the shared variables, a condition produced by some other thread. Since the variables are protected by a lock, and can be changed only by the thread holding the lock, h has to release the lock. It is not efficient to repeatedly release the lock and then re-acquire it to check the condition. Instead, it’s better for h to wait on a condition variable, as we saw in handout 14. Whenever any thread changes the shared variables in such a way that the condition might become true, it signals the threads waiting on that variable. Sometimes we say that the waiting threads ‘wake up’ when they are signaled. Depending on the application, a thread may signal one or several of the waiting threads.

Here is the spec for condition variables, copied from handout 14 on practical concurrency.

CLASS Condition EXPORT wait, signal, broadcast =

TYPE M = Mutex

VAR c : SET Thread := {}

% Each condition variable is the set of waiting threads.

PROC wait(m) =

>; % m.rel=HAVOC unless SELF IN m

m.acq >>

APROC signal() = >

APROC broadcast() = >

END Condition

As we saw in handout 14, it’s not necessary to have a single condition for each set of shared variables. We want enough condition variables so that we don’t wake up too many threads whose conditions are not yet satisfied, but not so many that the cost of doing all the signals is excessive.

Coding read/write lock using condition variables

This example shows how to use easy concurrency to make more complex locks and scheduling out of basic mutexes and conditions. We use a single mutex and condition for all the read-write locks here, but we could have separate ones for each read-write lock, or we could partition the locks into groups that share a mutex and condition. The choice depends on the amount of contention for the mutex.

Compare the code with ForgetfulRWL; the differences are highlighted with boxes. The in ForgetfulRWL have become m.acq ... m.rel; this provides atomicity because shared variables are only touched while the lock is held. The other change is that each guard that could block (in this example, all of them) is replaced by a loop that tests the guard and does c.wait if it doesn’t hold. The release operations do the corresponding signal or broadcast operations.

CLASS RWLockImpl EXPORT rAcq, rRel, wAcq, wRel = % implements ForgetfulRWL

VAR rw : Int := 0

m := m.new()

c := c.new()

% ABSTRACTION FUNCTION ForgetfulRWL.rw = rw

PROC rAcq(l) = m.acq; DO ~ rw >= 0 => c.wait(m) OD; rw + := 1; m.rel

PROC wAcq(l) = m.acq; DO ~ rw = 0 => c.wait(m) OD; rw := -1; m.rel

PROC rRel(l) =

m.acq; rw - := 1; IF rw = 0 => c.signal [*] SKIP FI; m.rel

PROC wRel(l) =

m.acq; rw := 0; c.broadcast; m.rel

END RWLockImpl

This is the prototypical example for scheduling resources. There are mutexes (just m in this case) to protect the scheduling data structures, conditions (just c in this case) on which to delay threads that are waiting for a resource, and logic that figures out when it’s all right to allocate a resource (the read or write lock in this case) to a thread.

Note that this code may starve a writer: if readers come and go but there’s always at least one of them, a waiting writer will never acquire the lock. How could you fix this?

An unbounded FIFO buffer

In this section, we give a spec and code for a simple unbounded buffer that could be used as a communication channel between two threads. This is the prototypical example of a producer-consumer relation between threads. Other popular names for Produce and Consume are Put and Get.

MODULE Buffer[T] EXPORT Produce, Consume =

VAR b : SEQ T := {}

APROC Produce(t) = >

APROC Consume() -> T = VAR t | t := b.head; b := b.tail; RET t >>

END Buffer

The code is another example of easy concurrency.

MODULE BufferImpl[T] EXPORT Produce, Consume =

VAR b : SEQ T := {}

m := m.new()

c := c.new()

% ABSTRACTION FUNCTION Buffer.b = b

PROC Produce(t) = m.acq; IF b = {} => c.signal [*] SKIP FI; b + := {t}; m.rel

PROC Consume() -> T = VAR t |

m.acq; DO b = {} => c.wait(m) OD; t := b.head; b := b.tail; m.rel; RET t

END BufferImpl

Coding Mutex with memory

The usual way to code Mutex is to use an atomic test-and-set operation; we saw this in the MutexImpl module above. If such an operation is not available, however, it’s possible to code Mutex using only atomic read and write operations on memory. This requires an amount of storage linear in the number of threads, however. We give a fair algorithm due to Peterson[66] for two threads; if thread h is competing for the mutex, we write h* for its competitor.

CLASS Mutex2Impl EXPORT acq, rel =

VAR req : Thread -> Bool := {* -> false}

lastReq : Int

PROC acq() =

[a0] req(SELF) := true;

[a1] lastReq := SELF;

DO [a2] (req(SELF*) /\ lastReq = SELF) => SKIP OD [a3]

PROC rel() = req(SELF) := false

END Mutex2Impl

This is hard concurrency, and it’s tricky to show that it works. To see the idea, consider first a simpler version of acq that ensures mutual exclusion but can deadlock:

PROC acq0() =

[a0] req(SELF) := true;

DO [a2] req(SELF*) => SKIP OD [a3] % busy wait

We get mutual exclusion because once req(h) is true, h* can’t get from a2 to a3. Thus req(h) acts as a lock that keeps the predicate h*.$pc = a2 true once it becomes true. Only one of the threads can get to a3 and acquire the lock. We might call the algorithm ‘polite’ because each thread defers to the other one at a2.

Of course, acq0 is no good because it can deadlock—if both threads get to a2 then neither can progress. acq avoids this problem by making it a little easier for a thread to progress: even if req(h*), h can take (a2, a3) if lastReq # h. Intuitively this maintains mutual exclusion because:

If both threads are at a2, only the one ≠ lastReq, say h, can progress to a3 and acquire the lock. Since lastReq won’t change, h* will remain at a2 until h releases the lock.

Once h has acquired the lock with h* not at a2, h* can only reach a2 by setting lastReq := h*, and again h* will remain at a2 until h releases the lock.

It ensures progress because the DO is the only place to get stuck, and whichever thread is not in lastReq will get past it. It ensures fairness because the first thread to get to a2 is the one that will get the lock first.

Abstractly, h has the mutex if req(h) /\ h.$pc # a2, and the transition from a2 to a3 simulates the body of Mutex.acq. Precisely, the abstraction function is

Mutex.m = (Holds0.set = {} => nil [*] Holds0.set.choose)

We sketch the proof that Mutex2Impl implements Mutex later.

There is lots more to say about coding Mutex efficiently, especially in the context of shared-memory multiprocessors.[67] Even on a uniprocessor you still need an implementation that can handle pre-emption; often the most efficient implementation gets the necessary atomicity by modifying the code for pre-emption to detect when a thread is pre-empted in the middle of the mutex code and either complete the operation or back up the state.

Multi-word clock

Often it’s possible to get better performance by avoiding locking. Algorithms that do this are called ‘wait-free’; we gave a brief discussion in handout 14. Here we present a wait-free algorithm due to Lamport[68] for reading and incrementing a clock, even if clock values do not fit into a single memory location that can be read and written atomically.

We begin with the spec. It says that a Read returns some value that the clock had between the beginning and the end of the Read. As we saw in handout 8 on generalized abstraction functions, where this spec is called LateClock, it takes a prophecy variable to show that this spec is equivalent to the simpler spec that just reads the clock value.

MODULE Clock EXPORT Read =

VAR t : Int := 0 % the current time

THREAD Tick() = DO > OD % demon thread advances t

PROC Read() -> Int = VAR t1: Int |

>;

END Clock

The code below is based on the idea of doing reads and writes of the same multi-word data in opposite orders. Tick writes hi2, then lo, then hi1. Read reads hi1, then lo, then hi2; if it sees different values in hi1 and hi2, there must have been at least one carry during the read, so t must have taken on the value hi2 * base. The function T expresses this idea. The atomicity brackets in the code are the largest ones that are justified by big atomic actions.

MODULE ClockImpl EXPORT Read =

CONST base := 2**32

TYPE Word = Int SUCHTHAT word IN base.seq)

VAR lo : Word := 0

hi1 : Word := 0

hi2 : Word := 0

% ABSTRACTION FUNCTION Clock.t = T(lo, hi1, hi2), Clock.Read.t1 = Read.t1Hist,

Clock.Read.t2 = T(Read.tLo, Read.tH1, read.tH2)

THREAD Tick() = DO VAR newLo: Word, newHi: Word |

>;

IF lo := newLo >>

[*] >; >; >

FI OD

PROC Read() -> Int = VAR tLo: Word, tH1: Word, tH2: Word |

>;

>;

>

FUNC T(l: Int, h1: Int, h2: Int) -> Int = h2 * base + (h1 = h2 => l [*] 0)

END ClockImpl

Given this code for reading a two-word clock atomically starting with atomic reads of the low and high parts, it’s obvious how to apply it recursively n–1 times to read an n word clock.

User and kernel mutexes and condition variables

This section presents code for mutexes and condition variables based on the Taos operating system from DEC SRC. Instead of spinning like SpinLock, it explicitly queues threads waiting for locks or conditions. The code for mutexes has a fast path that stays out of the kernel in acq when the mutex is free, and in rel when no other thread is waiting for the mutex. There is also a fast path for signal, for the common case that there’s nobody waiting on the condition. There’s no fast path for wait, since that always requires the kernel to run in order to reschedule the processor (unless a signal sneaks in before the kernel gets around to the rescheduling).

Notes on the code for mutexes:

1. MutexImpl maintains a queue of waiting threads, blocks a waiting thread using Deschedule, and uses Schedule to hand a ready thread over to the scheduler to run.

2. SpinLock and ReleaseSpinLock acquire and release a global lock used in the kernel to protect thread queues. This is OK because code running in the kernel can’t be pre-empted.

3. The loop in acq serves much the same purpose as a loop that waits on a condition variable. If the mutex is already held, the loop calls KernelQueue to wait until it becomes free, and then tries again. rel calls KernelRelease if there’s anyone waiting, and KernelRelease allows just one thread to run. That thread returns from its call of KernelQueue, and it will acquire the mutex unless another thread has called acq and slipped in since the mutex was released (roughly).

4. There is clumsy code in KernelQueue that puts the thread on the queue and then takes it off if the mutex turns out to be free. This is not a mistake; it avoids a race with rel, which calls KernelRelease to take a thread off the queue only if it sees that the queue is not empty. KernelQueue changes q and looks at s; rel uses the opposite order to change s and look at q.

This opposite-order access pattern often works in hard concurrency, that is, when there’s not enough locking to do the job in a straightforward way. We saw another version of it in Mutex2Impl, which sets req(h) before reading req(h*). In this case req(h) acts like a lock to keep h*.$pc = a2 from changing from true to false. We also saw it in ClockImpl, where the reader and the writer of the clock touch its pieces in the opposite order.

The boxes show how the state, acq, and rel differ from the versions in SpinLock.

CLASS MutexImpl EXPORT acq, rel = % implements ForgetfulMutex

TYPE FH = Mutex.FH

VAR fh := free

q : SEQ Thread := {}

PROC acq() = VAR t: FH |

DO >; IF t#held => RET [*] SKIP FI; KernelQueue() OD

PROC rel() = fh := free; IF q # {} => KernelRelease() [*] SKIP FI

% KernelQueue and KernelRelease run in the kernel so they can hold the spin lock and call the scheduler.

PROC KernelQueue() =

% This is just a delay until there’s a chance to acquire the lock. When it returns acq will retry.

% Queuing SELF before testing fh ensures that the test in rel doesn’t miss us.

% The spin lock keeps KernelRelease from getting ahead of us.

SpinLock(); % indented code holds the lock

q + := {SELF};

IF fh = free => q := q.reml % undo previous line; will retry at acq

[*] Deschedule(SELF) % wait, then retry at acq

FI;

ReleaseSpinLock()

PROC KernelRelease() =

SpinLock(); % indented code holds the lock

IF q # {} => Schedule(q.head); q := q.tail [*] SKIP FI;

ReleaseSpinLock()

% The newly scheduled thread competes with others to acquire the mutex.

END MutexImpl

Now for conditions. Note that:

1. The ‘event count’ ecSig deals with the standard ‘wakeup-waiting’ race condition: the signal arrives after the m.rel but before the thread is queued. Note the use of the global spin lock as part of this. It looks as though signal always schedules exactly one thread if the queue is not empty, but other threads that are in wait but have not yet acquired the spin lock may keep running; in terms of the spec they are awakened by signal as well.

2. signal and broadcast test for any waiting threads without holding any locks, in order to avoid calling the kernel in this common case. The other event count ecWait ensures that this test doesn’t miss a thread that is in KernelWait but hasn’t yet blocked.

CLASS ConditionImpl EXPORT wait, signal, broadcast = % implements Condition

TYPE M = Mutex

VAR ecSig : Int := 0

ecWait : Int := 0

q : SEQ Thread := {}

PROC wait(m) = VAR i := ecSig | m.rel; KernelWait(i); m.acq

PROC signal() = VAR i := ecWait |

ecSig + := 1; IF q # 0 \/ i # ecWait => KernelSig

PROC broadcast() = VAR i := ecWait |

ecSig + := 1; IF q # 0 \/ i # ecWait => KernelBroadcast

PROC KernelWait(i: Int) = % internal kernel procedure

SpinLock(); % indented code holds the lock

ecWait + := 1;

% if ecSig changed, there must have been a Signal, so return, else queue

IF i = ecSig => q + := {SELF}; Deschedule(SELF) [*] SKIP FI;

ReleaseSpinLock()

PROC KernelSig() = % internal kernel procedure

SpinLock(); % indented code holds the lock

IF q # {} => Schedule(q.head); q := q.tail [*] SKIP FI;

ReleaseSpinLock()

PROC KernelBroadcast() =

SpinLock(); % indented code holds the lock

DO q # {} => Schedule(q.head); q := q.tail OD;

ReleaseSpinLock()

END ConditionImpl

The code for mutexes and conditions are quite similar; in fact, both are cases of a general semaphore.

Proving concurrent modules correct

This section explains how to prove the correctness of concurrent program modules. It reviews the simulation method that we have already studied, which works just as well for concurrent as for sequential modules. Then several examples illustrate how the method works in practice. Things are more complicated in the concurrent case because there are many more atomic transitions, and because the program counters of the threads are part of the state.

Before using this method in its full generality, you should first apply the theorem on big atomic actions as much as possible, in order to reduce the number of transitions that your proofs need to consider. If you are programming with easy concurrency, that is, if your code uses a standard locking discipline, this will get rid of nearly all the work. If you are doing hard concurrency, there will still be lots of transitions, and in doing the proof you will probably find bugs in your program.

The formal method

We use the same simulation technique that we used for sequential modules, as described in handouts 6 and 8 on abstraction functions. In particular, we use the most general version of this method, presented near the end of handout 8. This version does not require the transitions of the code to correspond one-for-one with the transitions of the spec. Only the external behavior (invocations and responses) must be the same—there can be any number of internal steps. The method proves that every trace (external behavior sequence) produced by the code can also be produced by the spec.

Of course, the utility of this method depends on an assumption that the external behavior of a module is all that is of interest to callers of the module. In other words, we are assuming here, as everywhere in this course, that the only interaction between the module and the rest of the program is through calls to the external routines provided by the module.

We need to show that each transition of the code simulates a sequence of transitions of the spec. An external transition must simulate a sequence that contains exactly one instance of the same external transition and no other external transitions; it can also contain any number of internal transitions. An internal transition must simulate a sequence that contains only internal transitions.

Here, once again, are the definitions:

Suppose T and S are modules with same external interface. An abstraction function F is a function from states(T) to states(S) such that:

Start: If u is any initial state of T then F(u) is an initial state of S.

Step: If u and F(u) are reachable states of T and S respectively, and (u, (, u') is a step of T, then there is an execution fragment of S from F(u) to F(u'), having the same trace.

Thus, if ( is an invocation or response, the fragment consists of a single ( step, with any number of internal steps before and/or after. If ( is internal, the fragment consists of any number (possibly 0) of internal steps.

As we saw in handout 8, we may have to add history variables to T in order to find an abstraction function to S (and perhaps prophecy variables too). The values of history variables are calculated in terms of the actual variables, but they are not allowed to affect the real steps.

An alternative to adding history variables is to define an abstraction relation instead of an abstraction function. An abstraction relation AR is a relation between states(T) and states(S) such that:

Start: If u is any initial state of T then there exists an initial state s of S such that (u, s) ∈ AR.

Step: If u and s are reachable states of T and S respectively, (u, s) ∈ AR, and (u, (, u') is a step of T, then there is an execution fragment of S from s to some s' having the same trace, and such that (u', s') ∈ AR.

Theorem: If there exists an abstraction function or relation from T to S then T implements S; that is, every trace of T is a trace of S.

Proof: By induction.

The strategy

The formal method suggests the following strategy for doing hard concurrency proofs.

1. Start with a spec, which has an abstract state.

2. Choose a concrete state for the code.

3. Choose an abstraction function, perhaps with history variables, or an abstraction relation.

4. Write code, identifying the critical actions that change the abstract state.

5. While (checking the simulation fails) do

Add an invariant, checking that all actions of the code preserve it, or

Change the abstraction function (step 3), the code (step 4), the invariant (step 5), or more than one, or

Change the spec (step 1).

This approach always works. The first four steps require creativity; step 5 is quite mechanical except when you find an error. It is somewhat laborious, but experience shows that if you are doing hard concurrency and you omit any of these steps, your program won’t work. Be warned.

Owicki-Gries proofs

Owicki and Gries invented a special case of this general method that is well known and sometimes useful.[69] Their idea is to do an ordinary sequential proof of correctness for each thread h, annotating each atomic command in the usual style with an assertion that is true at that point if h is the only thread running. This proof shows that the code of h establishes each assertion. Then you show that each of these assertions remains true after any command that any other thread can execute while h is at that point. This condition is called ‘non-interference’; meaning not that other threads don’t interfere with access to shared variables, but rather that they don’t interfere with the proof.

The Owicki-Gries method amounts to defining an invariant of the form

h.$pc = α ==> Aα /\ h.$pc = β ==> Aβ /\ ...

and showing that it’s an invariant in two steps: first, that every step of h maintains it, and then that every step of any other thread maintains it. The hope is that this decomposition will pay because the most complicated parts of the invariant have to do with private variables of h that aren’t affected by other threads.

Prospectus for proofs

The remainder of this handout contains example proofs of correctness for several of the examples above: the RWLockImpl code for a read/write lock, the BufferImpl code for a FIFO buffer, the SpinLock code for a mutex (given in two versions), the Mutex2Impl code for a mutex used by two threads, and the ClockImpl code for a multi-word clock.

The amount of detail in these proofs is uneven. The proof of the FIFO buffer code and the second proof of the Spinlock code are the most detailed. The others give the abstraction functions and key invariants, but do not discuss each simulation step.

Read/write locks

We sketch how to prove directly that the module RWLockImpl implements ForgetfulRWL.This could be done by big atomic actions, since the code uses easy concurrency, but as an easy introduction discuss how to do it directly. The two modules are based on the same data, the variable rw. The difference is that RWLockImpl uses a condition variable to prevent threads in acq from busy-waiting when they don’t see the condition they require. It also uses a mutex to restrict accesses to rw, so that a series of accesses to rw can be done atomically.

An abstraction function maps RWLockImpl to ForgetfulRWL. The interesting part of the state of ForgetfulRWL is the rw variable. We define that by the identity mapping from RWLockImpl.

The mapping for steps is mostly determined by the rw identity mapping: the steps that assign to rw in RWLockImpl are the ones that correspond to the procedure bodies in ForgetfulRWL Then the checking of the state and step correspondences is pretty routine.

There is one subtlety. It would be bad if a series of rw steps done atomically in ForgetfulRWL were interleaved in RWLockImpl. Of course, we know they aren’t, because they are always done by a thread holding the mutex. But how does this fact show up in the proof?

The answer is that we need some invariants for RWLockImpl. The first, a “dominant thread invariant”, says that only a thread whose name is in m (a ‘dominant thread’) can be in certain portions of its code (those guarded by the mutex). The dominant thread invariant is in turn used to prove other invariants called “data protection invariants”.

For example, one data protection invariant says that if a thread (in RWLockImpl) is in middle of the assignment statement rw + := 1, then in fact rw ( 0 (that is, the test is still true). We need this data protection invariant to show that the corresponding abstract step (the body of rAcq in ForgetfulRWLock) is enabled.

BufferImpl implements Buffer

The FIFO buffer is another example of easy concurrency, so again we don’t need to do a transition-by-transition proof for it. Instead, it suffices to show that a thread holds the lock m whenever it touches the shared variable b. Then we can treat the whole critical section during which the lock is held as a big atomic action, and the proof is easy. We will work out the important details of a low-level proof, however, in order to get some practice in a situation that is slightly more complicated but still straightforward, and in order to convince you that the theorem about big atomic actions can save you a lot of work.

First, we give the abstraction function; then we use it to show that the code simulates the spec. We use a slightly simplified version of Produce that always signals, and we introduce a local variable temp to make explicit the atomicity of assignment to the shared variable b.

Abstraction function

The abstraction function on the state must explain how to interpret a state of the code as a state of the spec. Remember that to prove a concurrent program correct, we need to consider the entire state of a module, including the program counters and local variables of threads. For sequential programs, we can avoid this by treating each external operation as a single atomic action.

To describe the abstraction function, we thus need to explain how to construct a state of the spec from a state of the code. So what is a state of the Buffer module above? It consists of:

• A sequence of items b (the buffer itself);

• for each thread that is active in the module, a program counter; and

• for each thread that is active in the module, values for local variables.

A state of the code is similar, except that it includes the state of the Mutex and Condition modules.

To define the mapping, we need to enumerate the possible program counters. For the spec, they are:

P1 — before the body of Produce

P2 — after the body of Produce

C1 — before the body of Consume

C2 — after the body of Consume

or as annotations to the code:

PROC Produce(t) = [P1] > [P2]

PROC Consume() -> T =

[C1] VAR t := b.head | b := b.tail; RET t >> [C2]

For the code, they are:

• For a thread in Produce:

p1 — before m.acq

in m.acq—either before or after the action

p2 — before temp := b + {t}

p3 — before b := temp

p4 — before c.signal

in c.signal—either before or after the action

p5 — before m.rel

in m.rel—either before or after the action

p6 — after m.rel

• For a thread in Consume:

c1 — before m.acq

in m.acq—either before or after action

c2 — before the test b # {}

c3 — before c.wait

in c.wait—at beginning, in middle, or at end

c4 — before t := b.head

c5 — before temp := b.tail

c6 — before b := temp

c7 — before m.rel

in m.rel—either before or after action

c8 — before RET t

c9 — after RET t

or as annotations to the code:

PROC Produce(t) = VAR temp |

[p1] m.acq;

[p2] temp = b + {t};

[p3] b := temp;

[p4] c.signal;

[p5] m.rel [p6]

PROC Consume() -> T = VAR t, temp |

[c1] m.acq;

DO [c2] b # {} => [c3] c.wait OD;

[c4] t := b.head;

[c5] temp := b.tail; [c6] b := temp;

[c7] m.rel;

[c8] RET t [c9]

Notice that we have broken the assignment statements into their constituent atomic actions, introducing a temporary variable temp to hold the result of evaluating the right hand side. Also, the PC’s in the Mutex and Condition operations are taken from the specs of those modules (not the code; we prove their correctness separately). Here for reference is the relevant code.

APROC acq() = m := SELF; RET >>

APROC rel() = m := nil ; RET [*] HAVOC >>

APROC signal() = >

Now we can define the mapping on program counters:

• If a thread h is not in Produce or Consume in the code, then it is not in either procedure in the spec.

• If a thread h is in Produce in the code, then:

If h.$pc is in {p1, p2, p3} or is in m.acq, then in the spec h.$pc = P1.

If h.$pc is in {p4, p5, p6} or is in m.rel or c.signal then in the spec h.$pc = P2.

• If a thread h is in Consume in the code, then:

If h.$pc ∈ {c1, …, c6} or is in m.acq or c.wait then in the spec h.$pc = C1.

If h.$pc is in {c7, c8, c9} or is in m.rel then in the spec h.$pc = C2.

The general strategy here is to pick, for each atomic transition in the spec, some atomic transition in the code to simulate it. Here, we have chosen the modification of b in the code to simulate the corresponding operation in the spec. Thus, program counters before that point in the code map to program counters before the body in the spec, and similarly for program counters after that point in the code.

This choice of the abstraction function for program counters determines how each transition of the code simulates transitions of the spec as follows:

• If ( is an external transition, ( simulates the singleton sequence containing just (.

• If ( takes a thread from a PC of p3 to a PC of p4, ( simulates the singleton sequence containing just the body of Produce.

• If ( takes a thread from a PC of c6 to a PC of c7, ( simulates the singleton sequence containing just the body of Consume.

• All other transitions ( simulate the empty sequence.

This example illustrates a typical situation: we usually find that a transition in the code simulates a sequence of either zero or one transitions in the spec. Transitions that have no effect on the abstract state simulate the empty sequence, while transitions that change the abstract state simulate a single transition in the spec. The proof technique used here works fine if a transition simulates a sequence with more than one transition in it, but this doesn’t show up in most examples.

In addition to defining the abstract program counters for threads that are active in the module, we also need to define the values of their local variables. For this example, the only local variables are temp and the item t. For threads active in either Produce or Consume, the abstraction function on temp and t is the identity; that is, it defines the values of temp and t in a state of the spec to be the value of the identically named variable in the corresponding operation of the code.

Finally, we need to describe how to construct the state of the buffer b from the state of the code. Given the choices above, this is simple: the abstraction function is the identity on b.

Proof sketch

To prove the code correct, we need to prove some invariants on the state. Here are some obvious ones; the others we need will become clear as we work through the rest of the proof.

First, define a thread h to be dominant if h.$pc is in Produce and h.$pc is in {p2, p3, p4, p5} or is at the end of m.acq, in c.signal, or at the beginning of m.rel, or if h.$pc is in Consume and h.$pc is in {c2, c3, c4, c5, c6, c7} or is at the end of m.acq, at the beginning or end of c.wait (but not in the middle), or at the beginning of m.rel.

Now, we claim that the following property is invariant: a thread h is dominant if and only if Mutex.m = h. This simply says that h holds the mutex if and only if its PC is at an appropriate point. This is the basic mutual exclusion property. Amazingly enough, given this property we can easily show that operations are mutually exclusive: for all threads h, h' such that h ( h', if h is dominant then h' is not dominant. In other words, at most one thread can be in the middle of one of the operations in the code at any time.

Now let’s consider what needs to be shown to prove the code correct. First, we need to show that the claimed invariants actually are invariants. We do this using the standard inductive proof technique: Show that each initial state of the code satisfies the invariants, and then show that each atomic action in the code preserves the invariants. This is left as an exercise.

Next, we need to show that the abstraction function defines a simulation of the spec by the code. Again, this is an inductive proof. The first step is to show that an initial state of the code is mapped by the abstraction function to an initial state of the spec. This should be straightforward, and is left as an exercise. The second step is to show that the effects of each transition are preserved by the abstraction function. Let’s consider a couple of examples.

• Consider a transition ( from r to r' in which an invocation of an operation occurs for thread h. Then in state r, h was not active in the module, and in r', its PC is at the beginning of the operation. This transition simulates the identical transition in the spec, which has the effect of moving the PC of this thread to the beginning of the operation. So AF(r) is taken to AF(r') by the transition.

• Consider a transition in which a thread h moves from h.$pc = p3 to h.$pc = p4, setting b to the value stored in temp. The corresponding abstract transition sets b to b + {t}. To show that this transition does the right thing, we need an additional invariant:

If h.$pc = p3, then temp = b + {t}.

To prove this, we use the fact that if h.$pc = p3, then no other thread is dominant, so no other transition can change b. We also have to show that any transition that puts h.$pc at this point establishes the consequent of the implication — but there is only one transition that does this (the one that assigns to temp), and it clearly establishes the desired property.

The transition in Consume that assigns to b relies on a similar invariant. The rest of the transitions involve straightforward case analyses. For the external transitions, it is clear that they correspond directly. For the other internal transitions, we must show that they have no abstract effect, i.e., if they take r to r', then AF(r) = AF(r'). This is left as an exercise.

SpinLock implements Mutex, first version

The proof is done in two layers. First, we show that ForgetfulMutex implements Mutex. Second, we show that SpinLock implements ForgetfulMutex. For convenience, we repeat the definitions of the two modules.

CLASS Mutex EXPORT acq, rel =

VAR m : (Thread + Null) := nil

PROC acq() = m := SELF; RET >>

PROC rel() = m := nil ; RET [*] HAVOC >>

END Mutex

CLASS ForgetfulMutex EXPORT acq, rel =

TYPE M = ENUM[free, held]

VAR m := free

PROC acq() = m := held; RET >>

PROC rel() = >

END ForgetfulMutex

Proof that ForgetfulMutex implements Mutex

These two modules have the same atomicity. The difference is that ForgetfulMutex forgets which thread owns the mutex, and so it can’t check that the “right” thread releases it. We use an abstraction relation AR. It needs to be multi-valued in order to put back the information that is forgotten in the code. Instead of using a relation, we could use a function and history variables to keep track of the owner and havoc. The single-level proof given later on that Spinlock implements Mutex uses history variables.

The main interesting relationship that AR must express is:

s.m is non-nil if and only if u.m = held.

In addition, AR must include less interesting relationships. For example, it has to relate the $pc values for the various threads. In each module, each thread is either not there at all, before the body, or after the body. Thus, AR also includes the condition:

The $pc value for each thread is the same in both modules.

Finally, there is the technicality of the special $havoc = true state that occurs in Mutex. We handle this by allowing AR to relate all states of ForgetfulMutex to any state with $havoc = true.

Having defined AR, we just show that the two conditions of the abstraction relation definition are satisfied.

The start condition is obvious. In the unique start states of both modules, no thread is in the module. Also, if u is the state of ForgetfulMutex and s is the state of Mutex, then we have u.m = free and s.m = nil. It follows that (u, s) ∈ AR, as needed.

Now we turn to the step condition. Let u and s be reachable states of ForgetfulMutex and Mutex, respectively, and suppose that (u, (, u') is a step of ForgetfulMutex and that (u, s) ∈ AR. If s.$havoc, then it is easy to show the existence of a corresponding execution fragment of Mutex, because any transition is possible. So we suppose that s.$havoc = false. Invocation and response steps are straightforward; the interesting cases are the internal steps.

So suppose that ( is an internal action of ForgetfulMutex. We argue that the given step corresponds to a single step of Mutex, with “the same” action. There are two cases:

1. ( is the body of an acq, by some thread h. Since acq is enabled in ForgetfulMutex, we have u.m = free, and h.$pc is right before the acq body in u. Since (u, s) ∈ AR, we have s.m = nil, and also h.$pc is just before the acq body in s. Therefore, the acq body for thread h is also enabled in Mutex. Let s' be the resulting state of Mutex.

By the code, u'.m = held and s'.m = h, which correspond correctly according to AR. Also, since the same thread h gets the mutex in both steps, the PC’s are changed in the same way in both steps. So (u', s') ∈ AR.

2. ( is the body of a rel, by some thread h. If u.m = free then ForgetfulMutex does something sensible, as indicated by its code. But since (u, s) ∈ AR, s.m = nil and Mutex does HAVOC. Since $havoc in Mutex is defined to correspond to everything in ForgetfulMutex, we have (u', s') ∈ AR in this case.

On the other hand, if u.m = held then ForgetfulMutex sets u'.m := free. Since (u, s) ∈ AR, we have s.m ( nil. Now there are two cases: If s.m = h, then corresponding changes occur in both modules, which allows us to conclude (u', s') ∈ AR. Otherwise, Mutex goes to $havoc = true. But as before, this is OK because $havoc = true corresponds to everything in ForgetfulMutex.

The conclusion is that every trace of ForgetfulMutex is also a trace of Mutex. Note that this proof does not imply anything about liveness, though in fact the two modules have the same liveness properties.

Proof that SpinLock implements ForgetfulMutex

We repeat the definition of SpinLock.

CLASS SpinLock EXPORT acq, rel =

TYPE M = ENUM[free, held]

VAR m := free

PROC acq() = VAR t: FH |

DO >; IF t # held => RET [*] SKIP FI OD

PROC rel() = >

END SpinLock

These two modules use the same basic data. The difference is their atomicity. Because they use the same data, an abstraction function AF will work. Indeed, the point of introducing ForgetfulMutex was to take care of the need for history variables or abstraction relations there.

The key to defining AF is to identify the exact moment in an execution of SpinLock when we want to say the abstract acq body occurs. There are two logical choices: the moment when a thread converts u.m from free to held, or the later moment when the thread discovers that it has done this. Either will work, but to be definite we consider the earlier of these two possibilities.

Then AF is defined as follows. If u is any state of SpinLock then AF(u) is the unique state s of ForgetfulMutex such that:

• s.m = u.m, and

• The PC values of all threads “correspond”.

We must define the sense in which the PC values correspond. The correspondence is straightforward for threads that aren’t there, or are engaged in a rel operation. For a thread h engaged in an acq operation, we say that

• h.$pc in ForgetfulMutex, s.h.$pc, is just before the body of acq if and only if u.h.$pc is in SpinLock either (a) at the DO, and before the test-and-set ,or (b) after the test-and-set with h’s local variable t equal to held.

• h.$pc in ForgetfulMutex, s.h.$pc, is just after the body of acq if and only if u.h.$pc is either (a) after the test-and-set with h’s local variable t equal to free or (b) after the t # held test.

The proof that this is an abstraction function is interesting. The start condition is easy. For the step condition, the invocation and response cases are easy, so consider the internal steps. The rel body corresponds exactly in both modules, so the interesting steps to consider are those that are part of the acq. acq in SpinLock has two kinds of internal steps: the atomic test-and-set and the test for t # held. We consider these two cases separately:

1) The atomic test-and-set, (u, test-and-set, u'). Say this is done by thread h. In this case, the value of m might change. It depends on whether the step of SpinLock changes m from free to held. If it does, then we map the step to the acq body. If not, then we map it to the empty sequence of steps. We consider the two cases separately:

1. The step changes m. Then in SpinLock, h.$pc moves after the test-and-set with h’s local variable t = free. We claim first that the acq body in ForgetfulMutex is enabled in state AF(u). This is true because it requires only that s.m = free, and this follows from the abstraction function since u.m = free. Then we claim that the new states in the two modules are related by AF. To see this, note that m = held in both cases. And the new PC’s correspond: in ForgetfulMutex, h.$pc moves to right after the acq body, which corresponds to the position of h.$pc in SpinLock, by the definition of the abstraction function.

2. The step does not change m. Then h.$pc in SpinLock moves to the test, with t = held. Thus, there is no change in the abstract value of h.$pc.

2) The test for t # held, (u, test, u’). Say this is done by thread h. We always map this to the empty sequence of steps in ForgetfulMutex. We must argue that this step does not change anything in the abstract state, i.e., that AF(u') = AF(u). There are two cases:

3. If t = held, then the step of SpinLock moves h.$pc to after the DO. But this does not change the abstract value of h.$pc, according to the abstraction function, because both before and after the step, the abstract h.$pc value is before the body of acq.

4. On the other hand, if t = free, then the step of SpinLock moves h.$pc to after the =>. Again, this does not change the abstract value of h.$pc because both before and after the step, the abstract h.$pc value is after the body of acq.

SpinLock implements Mutex, second version

Now we show again that SpinLock implements Mutex, this time with a direct proof that combines the work done in both levels of the proof in the previous section. For contrast, we use history variables instead of an abstraction relation.

Abstraction function

As usual, we need to be precise about what constitutes a state of the code and what constitutes a state of the spec. A state of the spec consists of:

• A value for m (either a thread or nil); and

• for each thread that is active in the module, a program counter.

There are no local variables for threads in the spec.

A state of the code is similar; it consists of:

• A value for m (either free or held);

• for each thread that is active in the module, a program counter; and

• for each thread that is active in acq, a value for the local variable t.

Now we have a problem: there is no way to define an abstraction function from a code state to a spec state. The problem here is that the code does not record which thread holds the mutex, yet the spec keeps track of this information. To solve this problem, we have to introduce a history variable or use an abstraction relation. We choose the history variable, and add it as follows:

• We augment the state of the code with two additional variables:

ms: (Thread + Null) := nil % m in the Spec

hs: Bool := false % $havoc in the Spec

• We define the effect of each atomic action in the code on the history variable; written in Spec, this results in the following modified code:

PROC acq() = VAR t: FH |

DO ; IF t # held => ; RET [*] SKIP FI OD;

PROC rel() = >

You can easily check that these additions to the code satisfy the constraints required for adding history variables.

This treatment of ms is the obvious way to keep track of the spec’s m. Unfortunately, it turns out to require a rather complicated proof, which we now proceed to give. At the end of this section we will see a less obvious ms that allows a much simpler proof; skip to there if you get worn out.

Now we can proceed to define the abstraction function. First, we enumerate the program counters. For the spec, they are:

A1 — before the body of acq

A2 — after the body of acq

R1 — before the body of rel

R2 — after the body of rel

For the code, they are:

• For a thread in acq:

a1 — before the VAR t

a2 — after the VAR t and before the DO loop

a3 — before the test-and-set in the body of the DO loop

a4 — after the test-and-set in the body of the DO loop

a5 — before the assignment to ms

a6 — after the assignment to ms

• For a thread in rel:

r1 — before the body

r2 — after the body

The transitions in acq may be a little confusing: there’s a transition from a4 to a3, as well as transitions from a4 to a5.

Here are the routines in Mutex annotated with the PC values:

APROC acq() = [A1] m := SELF >> [A2]

APROC rel() = [R1] HAVOC [*] m := nil >> [R2]

Here are the routines in SpinLock annotated with the PC values:

PROC acq() = [a1] VAR t := FH |

[a2] DO [a3] >;

[a4] IF t # held => [a5] >; [a6] RET [*] SKIP FI OD;

PROC rel() = [r1] > [r2]

Now we can define the mappings on program counters:

• If a thread is not in acq or rel in the code, then it is not in either in the spec.

• {a1, a2, a3, a4, a5} maps to A1, a6 maps to A2

• r1 maps to R1, r2 maps to R2

The part of the abstraction function dealing with the global variables of the module simply defines m in the spec to have the value of ms in the code, and $havoc in the spec to have the value of hs in the code. As in handout 8, we just throw away all but the spec part of the state.

Since there are no local variables in the spec, the mapping on program counters and the mapping on the global variables are enough to define how to construct a state of the spec from a state of the code.

Once again, the abstraction function on program counters determines how transitions in the code simulate sequences of transitions in the spec:

• If ( is an external transition, ( simulates the singleton sequence containing just (.

• If ( takes a thread from a5 to a6, ( simulates the singleton sequence containing just the transition from A1 to A2.

• If ( takes a thread from r1 to r2, ( simulates the singleton sequence containing just the transition from R1 to R2.

• All other transitions simulate the empty sequence.

Proof sketch

As in the previous example, we will need some invariants to do the proof. Rather than trying to write them down first, we will see what we need as we do the proof.

First, we show that initial states of the code map to initial states of the spec. This is easy; all the thread states correspond, and the initial state of ms in the code is nil.

Next, we show that transitions in the code and the spec correspond. All transitions from outside the module to just before a routine’s body are straightforward, as are transitions from the end a routine’s body to outside the module (i.e., when a routine returns). The transition in the body of rel is also straightforward. The hard cases are in the body of acq.

Consider all the transitions in acq before the one from a5 to a6. These all simulate the null transition, so they should leave the abstract state unchanged. And they do, because none of them changes ms.

The transition from a5 to a6 simulates the transition from A1 to A2. There are two cases: when ms = nil, and when ms ≠ nil.

1. In the first case, the transition from A1 to A2 is enabled and, when taken, changes the state so that m = SELF. This is just what the transition from a5 to a6 does.

2. Now consider the case when ms ≠ nil. We claim this case is possible only if a thread that didn’t hold the mutex has done a rel. Then hs = true, the spec has done HAVOC, and anything can happen. In the absence of havoc, if a thread is at a5, then ms = nil. But even though this invariant is what we want, it’s too weak to prove itself inductively; for that, we need the following, stronger invariant:

Either

If m = free then ms = nil, and

If a thread is at a5, or at a4 with t = free, then ms = nil, m = held, there are no other threads at a5, and for all other threads at a4, t = held

or hs is true.

Given this invariant, we are done: we have shown the appropriate correspondence for all the transitions in the code. So we must prove the invariant. We do this by induction. It’s vacuously true in the initial state, since no thread could be at a4 or a5 in the initial state. Now, for each transition, we assume that the invariant is true before the transition and prove that it still holds afterwards.

The external transitions preserve the invariant, since they change nothing relevant to it.

The transition in rel preserves the first conjunct of the invariant because afterwards both m = free and ms = nil. To prove that the transition in rel preserves the second conjunct of the invariant, there are two cases, depending on whether the spec allows HAVOC.

1. If it does, then the code sets hs true; this corresponds to the HAVOC transition in the spec, and thereafter anything can happen in the spec, so any transition of the code simulates the spec. The reason for explicitly simulating HAVOC is that the rest of the invariant may not hold after a rogue thread does rel. Because the rogue thread resets m to free, if there’s a thread at a5 or at a4 with t = free, and m = held, then after the rogue rel, m is no longer held and hence the second conjunct is false This means that it’s possible for several threads to get to a5, or to a4 with t = free. The invariant still holds, because hs is now true.

2. In the normal case ms ≠ nil, and since we’re assuming the invariant is true before the transition, this implies that no thread is at a4 with t = free or at a5. After the transition to r2 it’s still the case that no thread is at a4 with t = free or at a5, so the invariant is still true.

Now we consider the transitions in acq. The transitions from a1 to a2 and from a2 to a3 obviously preserve the invariant. The transition from a4 to a5 puts a thread at a5, but t = free in this case so the invariant is true after the transition by induction. The transition from a4 to a3 also clearly preserves the invariant.

The transition from a3 to a4 is the first interesting one. We need only consider the case hs = false, since otherwise the spec allows anything. This transition certainly preserves the first conjunct of the invariant, since it doesn’t change ms and only changes m to held. Now we assume the second conjunct of the invariant true before the transition. There are two cases:

1. Before the transition, there is a thread at a5, or at a4 with t = free. Then we have m = held by induction, so after the transition both t = held and m = held. This preserves the invariant.

2. Before the transition, there are no threads at a5 or at a4 with t = free. Then after the transition, there is still no thread at a5, but there is a new thread at a4. (Any others must have t = held.) Now, if this thread has t = held, the second part of the invariant is true vacuously; but if t = free, then we have:

ms = nil (since when the thread was at a3 m must have been free, hence the first part of the invariant applies);

m = held (as a direct result of the transition);

there are no threads at a5 (by assumption); and

there are no other threads at a4 with t = free (by assumption).

So the invariant is still true after the transition.

Finally, assume a thread h is at a5, about to transition to a6. If the invariant is true here, then h is the only thread at a5, and all threads at a4 have t = held. So after it makes the transition, the invariant is vacuously true, because there is no other thread at a5 and the threads at a4 haven’t changed their state.

We have proved the invariant. The invariant implies that if a thread is at a5, ms = nil, which is what we wanted to show.

Simplifying the proof

This proof is a good example of how to use invariants and of the subtleties associated with preconditions. It’s possible to give a considerably simpler proof, however, by handling the history variable ms in a less natural way. This version is closer to the two-stage proof we saw earlier. In particular, it uses the transition from a3 to a4 to simulate the body of Mutex.acq. We omit the hs history variable and augment the code as follows:

PROC acq() = [a1] VAR t := FH |

[a2] DO [a3] ms := SELF [*] SKIP FI >>;

[a4] IF t # held => [a6] RET [a7] [*] SKIP FI OD;

PROC rel() = [r1] > [r2]

The abstraction function maps ms to Mutex.m as before, and it maps PC’s a1- a3 to A1 and a6-a7 to A2. It maps a4 to A1 if t = held, and to A2 if t = free; thus a3 to a4 simulates Mutex.acq only if m was free, as we should expect. There is no need for an invariant; we only used it at a5 to a6, which no longer exists.

The simulation argument is the same as before except for a3 to a4, which is the only place where we changed the code. If m = held, then m and ms don’t change; hence Mutex.m doesn’t change, and neither does the abstract PC; in this case the transition simulates the empty trace. If m = free, then m becomes held, ms becomes SELF, and the abstract PC becomes A2; in this case the transition simulates A1 to A2, as promised.

The moral of this story is that it can make a big difference how you choose the abstraction function. The crucial decision is the choice of the ‘critical transition’ that models the body of Mutex.acq, that is, how to abstract the PC. It seems very natural to change ms in the code after the test of t # held that is already there, but this forces the critical transition to be after the test. Then there has to be an invariant to carry forward the relationship between the local variable t and the global variable m, which complicates things, and the HAVOC case in rel complicates them further by falsifying the natural statement of the invariant and requiring the additional hs variable to patch things up. The uglier code with a second test of t # held inside the atomic test-and-set command makes it possible to use that action, which does the real work, to simulate the body of Mutex.acq, and then everything falls out nicely.

More complicated code requires invariants even when we choose the best abstraction function, as we see in the next two examples.

Mutex2Impl implements Mutex

This is the rather subtle code that implements a mutex for two threads using only memory reads and writes. We begin with a proof in the style of the last few, and then give an entirely different proof based on model checking.

First we show that the simple, deadlocking version acq0 maintains mutual exclusion. Recall that we write h* for the thread that is the partner of thread h. Here are the spec and code again:

CLASS Mutex EXPORT acq, rel =

VAR m : (Thread + Null) := nil

PROC acq() = m := SELF; RET >>

PROC rel() = m := nil ; RET [*] HAVOC >>

END Mutex

CLASS Mutex2Impl0 EXPORT acq, rel =

VAR req : Thread -> Bool := {* -> false}

lastReq : Int

PROC acq0() =

[a1] req(SELF) := true;

DO [a2] req(SELF*) => SKIP OD [a3]

PROC rel() = req(SELF) := false

END Mutex2Impl0

Intuitively, we get mutual exclusion because once req(h) is true, h* can’t get from a2 to a3. It’s convenient to define

FUNC Holds0(h: Thread) = RET req(h) /\ h.$pc # a2

Abstractly, h has the mutex if Holds0(h), and the transition from a2 to a3 simulates the body of Mutex.acq. Precisely, the abstraction function is

Mutex.m = (Holds0.set = {} => nil [*] Holds0.set.choose)

Recall that if P is a predicate, P.set is the set of arguments for which it is true.

To make precise the idea that req(h) stops h* from getting to a3, the invariant we need is

Holds0.set.size req(h))

The first conjunct is the mutual exclusion. It holds because, given the first conjunct, only (a2, a3) can increase the size of Holds0.set, and h can take that step only if req(h*) = false, so Holds0.set goes from {} to {h}. The second conjunct holds because it can never be true ==> false, since only the step (a1, req(h) := true, a2) can make the antecedent true, this step also makes the consequent true, and no step away from a2 makes the consequent false.

This argument applies to acq0 as written, but you might think that it’s unrealistic to fetch the shared variable req(SELF*) and test it in a single atomic action; certainly this will take more than one machine instruction. We can appeal to big atomic actions, since the whole sequence from a2 to a3 has only one action that touches a shared variable (the fetch of req(SELF*)) and therefore is atomic.

This is the right thing to do in practice, but it’s instructive to see how to do it by hand. We break the last line down into two atomic actions:

VAR t | DO [a2] >; [a21] SKIP >> OD [a3]

We examine several ways to show the correctness of this; they all have the same idea, but the details differ. The most obvious one is to add the conjunct h.$pc # a21 to Holds0, and extend the mutual exclusion conjunct of the invariant so that it covers a thread that has reached a21 with t = false:

(Holds0.set \/ {h | h.$pc = a21 /\ h.t = false}).size h.t = false)

Yet another approach is to make explicit in the invariant what h knows about the global state. One purpose of an invariant is to remember things about the global state that a thread has discovered in the past; the fact that it’s an invariant means that those things stay true, even though other threads are taking steps. In this case, t = false in h means that either req(h*) = false or h* is at a2 or a21, in other words, Holds(h*) = false. We can put this into the invariant with the conjunct

h.$pc = a21 /\ h.t = false ==> Holds(h*) = false

and this is enough to ensure that the transition (a21, a3) maintains the invariant.

We return from this digression on proof methodology to study the non-deadlocking acq from Mutex2Impl:

PROC acq() =

[a0] req(SELF) := true;

[a1] lastReq := self;

DO [a2] (req(SELF*) /\ lastReq = SELF) => SKIP OD [a3]

We discussed liveness informally earlier, and we don’t attempt to prove it. To prove mutual exclusion, we need to extend Holds0 in the obvious way:

FUNC Holds(h: Thread) = req(h) /\ h.$pc # a1 /\ h.$pc # a2

and add \/ h.$pc = a1 to the antecedent of the invariant In order to have mutual exclusion, it must be true that h won’t find lastReq = h* as long as h* holds the lock. We need to add a conjunct to the invariant to express this. This leaves us with:

Holds0.set.size req(h))

/\ (Holds(h*) /\ h.$pc = a2 ==> lastReq = h)

The last conjunct holds because (a1, a2) makes it true, and the only way to make it false is for h* to do lastReq := SELF, which it can only do from a1, so that Holds(h*) is false. With this invariant it’s obvious that (a2, a3) maintains the invariant.

Proof by model checking

We have been doing all our proofs by establishing invariants; these are called assertional proofs. An alternative method is to explore the state space exhaustively; this is called model checking. It only works when the state space not too big. In this case, if the two threads are a and b, the state space is just:

a.$pc IN {a0, a1, a2, a3}

b.$pc IN {a0, a1, a2, a3}

req(a) IN {false, true}

req(b) IN {false, true}

lastReq IN {a, b}

We can write down a state concisely with one digit to represent each PC, a t or f for each req, and an a or b for lastReq. Thus 00ffa is a.$pc = a0, b.$pc = a0, req(a) = false, req(b) = false, lastReq = a. When the value of a component is unimportant we write x for it.

The figure displays the complete state machine for Mutex2Impl.acq. Note the extensive symmetries. Nominally there are 128 states, but many are not reachable:

|[pic] |

|State machine for Mutex2Impl.acq, assuming (req(SELF*) /\ lastReq = SELF)is atomic |

1. The value of req follows from the PC’s, which cuts the number of reachable states to 32.

2. 33xxx is not reachable. This is the mutual exclusion invariant, which is that both PC’s cannot be in the critical section at the end of acq.This removes 2 states.

3. At the top of the picture the value of lastReq is not important, so we have shown it as x. This removes 4 states.

4. We can’t have 20xxb or 21xxb or 30xxb or 31xxb or 32xxa, or the 5 symmetric states, because of the way lastReq is set. This removes 10 states.

In the end there are only 16 reachable states, and 7 of them are obtained from the others simply by exchanging the two threads a and b.

Since there is no non-determinism in this algorithm and a thread is never blocked from making a transition, there are two transitions from each state, one for each thread. If there were no transitions from a state, the system would deadlock in that state. It’s easy to see that the algorithm is live if both threads are scheduled fairly, since there are no non-trivial cycles that don’t reach the end of acq. It is fair because the transitions from 00ffx and 11ttx are fair.

The appeal of model-checking should be clear from the example: we don’t have to think, but can just search the state space mechanically. The drawback is that the space may be too large. This small example illustrates that symmetries can cut the size of the search dramatically, but the symmetries are often not obvious.

Unfortunately, this story is incomplete, because it assumed that we can evaluate (req(SELF*) /\ lastReq = SELF) atomically, which is not true. To fix this we have to break this evaluation down into two steps, with a new program counter value in the middle. We reproduce the whole procedure for easy reference:

PROC acq() =

[a0] req(SELF) := true;

[a1] lastReq := self;

DO [a2] req(SELF*) => [a4] IF lastReq = SELF => SKIP FI OD [a3]

PROC rel() = req(SELF) := false

This increases the number of states from 128 to 200; the state space is:

a.$pc IN {a0, a1, a2, a3, a4}

b.$pc IN {a0, a1, a2, a3, a4}

req(a) IN {false, true}

req(b) IN {false, true}

lastReq IN {a, b}

Most of the states are still unreachable, but there are 26 reachable states that we need to distinguish (using x as before when the value of a component doesn’t affect the possible transitions). Instead of drawing a new state diagram like the previous one, we present a matrix that exposes the symmetries in a different way, by using the two PCs as the x and y coordinates. Except for the x in the 22ttx, 24ttx, and 42ttx states, the PC values determine the other state components. The convention in this table is that for each state there’s a transition that advances a thread’s PC to the next non-blank row (for a) or column (for b) unless there’s an arrow going somewhere else. Like the diagram, the table is symmetric.

The new transitions are the ones that involve a PC of a4. These transitions don’t change any state variables. As the table shows, what they do is to articulate the fine structure of the 22ttx loops in the simpler state diagram. If a gets to 2xtta it loops between that state and 4xttx; similarly b loops between x2ttb and x4ttb. On the other hand, when lastReq = b, thread a can get through the sequence 2xttb, 4xttb to 3xttb (where x can be 2 or 4) and similarly b can get through the sequence x2tta, x4tta to x3tta.

These complications should suffice to convince you that model checking is for machines, not for people.

|b.$pc a.$pc |

|a0 |

|a1 |

|a2 |

|lr = a / b |

|a4 |

|lr = a / b |

|a3 |

| |

|a0 |

|00ffx |

|01ftx |

|02ftb |

|04ftb |

|03ftb |

| |

|a1 |

|10tfx |

|11ttx |

|12ttb |

|14ttb |

| |

| |

|a2 lr=a |

|20tfa |

|21tta |

|22tta |

|24tta |

|23tta |

| |

|a2 lr=b |

| |

| |

|22ttb |

|24ttb |

| |

| |

|a4 lr=a |

|40tfa |

|41tta |

|42tta |

|44tta |

|43tta |

| |

|a4 lr=b |

| |

| |

|42ttb |

|44ttb |

| |

| |

|a3 |

|30tfa |

| |

|32ttb |

|34ttb |

|MX |

| |

|State machine for Mutex2Impl.acq, (req(SELF*) /\ lastReq = SELF)not atomic |

It’s instructive to tell the same story for acq0, the implementation that can deadlock. The code is:

PROC acq0() =

[a0] req(SELF) := true;

DO [a2] req(SELF*) => SKIP OD [a3]

and there’s no lastReq variable. The picture is much simpler; too bad it doesn’t work. As you can see, there is no progress from 22tt.

|[pic] |

|State machine for Mutex2Impl.acq, which deadlocks in 22tt |

ClockImpl implements Clock

We conclude with the proof of the clock implementation. The spec says that a Read returns some value that the clock had between the beginning and the end of the Read. Here it is, with labels.

MODULE Clock EXPORT Read =

VAR t : Int := 0 % the current time

THREAD Tick() = DO > OD % demon thread advances t

PROC Read() -> Int = VAR t1: Int |

[R1] >; [R2] [R3]

END Clock

To show that ClockImpl implements this we introduce a history variable t1Hist in Read that corresponds to t1 in the spec, recording the time at the beginning of Read’s execution. The invariant that is needed is based on the idea that Read might complete before the next Tick, and therefore the value Read would return by reading the rest of the shared variables must be between t1Hist and Clock.t. We can write this most clearly by annotating the labels in Read with assertions that are true when the PC is there.

MODULE ClockImpl EXPORT Read =

CONST base := 2**32

TYPE Word = Int SUCHTHAT word IN base.seq)

VAR lo : Word := 0

hi1 : Word := 0

hi2 : Word := 0

% ABSTRACTION FUNCTION Clock.t = T(lo, hi1, hi2), Clock.Read.t1 = Read.t1Hist,

Clock.Read.t2 = T(Read.tLo, Read.tH1, read.tH2)

% The PC correspondence is R1 ( r1, R2 ( r2, r3, R3 ( r4

THREAD Tick() = DO VAR newLo: Word, newHi: Word |

>;

IF lo := newLo >>

[*] >; >; >

FI OD

PROC Read() -> Int = VAR tLo: Word, tH1: Word, tH2: Word, t1Hist: Int |

[r1] >;

[r2] % I2: T(lo , tH1, hi2) IN t1Hist .. T(lo, hi1, hi2)

>

[r3] % I3: T(tLo, tH1, hi2) IN t1Hist .. T(lo, hi1, hi2)

>

[r4] % I4: $a IN t1Hist .. T(lo, hi1, hi2)

FUNC T(l: Int, h1: Int, h2: Int) -> Int = h2 * base + (h1 = h2 => l [*] 0)

END ClockImpl

The whole invariant is thus

h.$pc = r2 ==> I2 /\ h.$pc = r3 ==> I3 /\ h.$pc = r4 ==> I4

The steps of Read clearly maintain this invariant, since they don’t change the value before IN. The steps of Tick maintain it by case analysis.

18. Consensus

Consensus (sometimes called ‘reliable broadcast’ or ‘atomic broadcast’) is a fundamental building block for distributed systems. Informally, we say that several processes achieve consensus if they all agree on some value. Three obvious applications are:

Distributed transactions, where all the processes need to agree on whether a transaction commits or aborts. Each transaction needs a new consensus on its outcome.

Membership, where a set of processes cooperating to provide a highly available service need to agree on which processes are currently functioning as members of the set. Every time a process fails or starts working again there must be a new consensus.

Electing a leader of a group of processes.

A less obvious, but much more powerful application is to replicate that state machines, which are discussed in detail below and in handout 28.

There are four important things to learn from this part of the course:

The idea of replicated state machines as a completely general method for building highly available, fault tolerant systems. In handout 28 we will discuss replicated state machines and other methods for fault tolerance in more detail.

The Paxos algorithm for distributed, fault tolerant consensus: how and why it works .

Paxos as an example of the best style for distributed, fault tolerant algorithms.

The correctness of Paxos as an example of the abstraction functions and simulation proofs applied to a very subtle algorithm.

Replicated state machines

There is a much more general way to use consensus, as the mechanism for coding a highly available state machine, which is the basic tool for building a highly available system. The way to get availability is to have either perfect components or redundancy. Perfect components are too hard, which leaves redundancy. The simplest form of redundancy is replication: have several copies or replicas of each component, and make sure that all the non-faulty components do the same thing. Since any computation can be expressed as a state machine, a replicated state machine can make any computation highly available.

Recall the basic idea of a replicated state machine:

If the transition relation is deterministic (in other words, is a function from (state, input) to (new state, output)), then several copies of the state machine that start in the same state and see the same sequence of inputs will do the same thing, that is, end up in the same state and produce the same outputs.

So if several processes are implementing the same state machine and achieve consensus on the values and order of the inputs, they will do the same thing. In this way it’s possible to replicate an arbitrary computation and thus make it highly available. Of course we can make the order a part of the value of the input by defining some total order on the set of possible inputs;[70] the easiest way to do this is simply to number them 1, 2, 3, .... We have already seen one application of this replicated state machine idea, in the code for transactions; there the replication takes the form of redoing a sequence of actions that is remembered in a log.

Suppose, for example, that we want to build a highly available file system. The transitions are read and write operations on the files (and rename, list, … as well). We make several copies of the file system and make sure that they process read and write operations in the same order. A client sends its operation to some copy, which gets consensus that it is the next operation. Then all the copies do the operation, and one of them returns the result to the client.

In many applications the inputs are requests from clients to the replicated service. Typically different clients generate their requests independently, so it’s necessary to agree not only on what the requests are, but also on the order in which to serve them. The simplest way to do this is to number them with consecutive integers, starting at 1. This is especially easy in the usual implementation, ‘primary copy’ replication, since there’s one place (the primary) to assign consecutive numbers. As we shall see, however, it’s straightforward in any consensus scheme: you get consensus on input 1, then on input 2, etc.

You might think that a read could be handled by any copy with no need for consensus, since it doesn’t change the state of the file system. Without consensus, however, a read might fail to see the result of a write that finished before the read started, since the read might be handled by a copy whose state is behind the current state of the file system. This result violates “external consistency”, which is a formal expression of the usual intuition about state machines. In some applications, however, it is acceptable to get a possibly old result from a read, and then any copy can satisfy it without consensus. Another possibility is to notice that a given copy will have done all the operations up to n, and define a read operation that returns n along with the result value, and possibly the real time of operation n as well. Then it’s up to the client to decide whether this is recent enough.

The literature is full of other schemes for achieving consensus on the order of requests when their total order is not derived from consecutive integers. These schemes label each input with some label from a totally ordered set (for instance, (client UID, timestamp) pairs) and then devise some way to be certain that you have seen all the inputs that can ever exist with labels smaller than a given value. They are complicated, and of doubtful utility.[71] People who do it for money use primary copy.[72]

Unfortunately, consensus is expensive. The section on optimizations at the end of this handout explains a variety of ways to make a replicated state machine run efficiently: leases, transactions, and batching.

Spec for consensus

Here is the spec for consensus; we have seen it already in handout 8 on history and prophecy variables. The idea is that the outcome of consensus should be one and only one of the allowed values. In the spec there is an outcome variable initialized to nil, and an action Allow(v) that can be invoked any number of times. There is also an action Outcome to read the outcome variable; it must return either nil or a v which was the argument of some Allow action, and if it doesn’t return nil it must always return the same v.

More precisely, we have two requirements:

Agreement: Every non-nil result of Outcome is the same.

Validity: A non-nil outcome equals some allowed value.

Validity means that the outcome can’t be any arbitrary value, but must be a value that was allowed. Consensus is reached by choosing some allowed value and assigning it to outcome. This spec makes the choice on the fly as the allowed values arrive.

MODULE Consensus [V] EXPORT Allow, Outcome = % data value to agree on

VAR outcome : (V + Null) := nil

APROC Allow(v) = outcome := v [] SKIP >>

APROC Outcome() -> (V + Null) = >

END Consensus

Note that Outcome is allowed to return nil even after the choice has been made. This reflects the fact that in code with several replicas, Outcome is often coded by talking to just one of the replicas, and that replica may not yet have learned about the choice.

If only one Allow action occurs, there’s no need to choose a v, and the code’s only problem is to ensure termination. An algorithm that does so is said to implement ‘reliable’ or ‘atomic’ broadcast; there is only one sender, and either everyone or no one gets the message. The single Allow might not set outcome, which corresponds to failure of the sender of the broadcast message; in this case no one gets the message.

Here is an equivalent spec, slightly more complicated but perhaps more intuitive, and certainly closer to an implementation. It accumulates the allowed values and then chooses one of them in the internal action Agree.

MODULE LateConsensus [V] EXPORT Allow, Outcome =

VAR outcome : (V + Null) := nil

allowed : SET V := {}

APROC Allow(v) = >

APROC Outcome() -> (V + Null) = >

% Only outcome is visible

APROC Decide() = outcome := v >>

END LateConsensus

It should be fairly clear that LateConsensus implements Consensus. An abstraction function to prove this, however, requires a prophecy variable, because Consensus decides on the outcome (in the Allow action) before LateConsensus does (in the Decide action). We saw these specs in handout 8 on generalized abstraction functions, where prophecy variables are explained.

In the code we have in mind, there are some processes, each with its own outcome variable initialized to nil. The outcome variables are supposed to reach consensus, that is, become equal to the argument of some Allow action. An Outcome can be directed to any process, which returns the value of its outcome variable. The tricky part is to ensure that two non-nil outcome variables are always equal, so that the agreement property is satisfied.

We would also like to have the property that eventually Outcome stops returning nil. In the code, this happens when every process’ outcome variable is non-nil. However, this could take a long time if some process is very slow (or down for a long time).

We can change Consensus to express this with an internal action Done:

MODULE TerminatingConsensus [V] EXPORT Allow, Outcome =

VAR outcome : (V + Null) := nil

done : Bool := false

APROC Allow(v) = outcome := v [] SKIP >>

APROC Outcome() -> (V + Null) = RET nil >>

THREAD Done() = done := true >>

END TermConsensus

Note that this spec does not say anything about the processes in the assumed code; the abstraction function will say that done is true when all the processes have outcome ≠ nil.

An even stronger spec returns an outcome only when it’s done:

APROC Outcome() -> (V + Null) = RET outcome [] ~ done => RET nil >>

This is usually too strong for distributed code. It means that a process may not be able to respond to an Outcome request, since it can’t return a value if it doesn’t know the outcome yet, and it can’t return nil if anyone else has already returned a value. If either the processes or the communication are asynchronous, it won’t be possible in general for one process to know whether another one no longer matters because it has failed, or is just slow.

Facts about consensus

In this section we summarize the most important facts about when consensus is possible and what it costs. You can learn more about this in Nancy Lynch’s course on distributed algorithms, 6.852J, or in her book cited in handout 2.

Fault models

To devise code for Consensus we need a precise model for the general setting of processes connected by links that can communicate messages from one process to another. In particular, the model must define what faults are possible. There are lots of ways to do this, and we have space to describe only the models that are most popular and closest to reality.

There are two broad classes of models:

• Synchronous, in which a non-faulty component makes its state transitions within a known amount of time. Usually this is coded by using a timeout, and declaring a component faulty if it fails to perform within the specified time.

• Asynchronous, in which nothing is known about the relative rate of progress of different components. In particular, a process can take an arbitrary amount of time to make a transition, and a link can take an arbitrary amount of time to deliver a message.

In general a process can send a message only to certain other processes; this “can send message” relation defines a graph whose edges are the links. The graph may be directed (it’s possible that A can talk to B but B can’t talk to A), but we will assume that communication is full-duplex so that the graph is undirected. Links are either working or faulty; a faulty link delivers no messages. Even a working link may lose messages, and in some models may lose any number of messages; it’s helpful to think of such a system as one with totally asynchronous communication.

Processes are either working or faulty. There are two models for a faulty process:

• Stopping faults: a faulty process stops making transitions and doesn’t start again. In an asynchronous model there’s no way for another process to distinguish a stopped process or link from one that is simply very slow.

• Byzantine faults: a faulty process makes arbitrary transitions; these are named after the Byzantine Empire, famous for treachery. The motivation for this model is usually not fear of treachery, but ignorance of the ways in which a process might fail. Clearly Byzantine failure is an upper bound on how bad things can be.

Is consensus possible (will it terminate)?

A consensus algorithm terminates when the outcome variables of all non-faulty processes equal some allowed value. Here are the basic facts about consensus in some of these models.

• There is no consensus algorithm that is guaranteed to terminate in an asynchronous system with perfect links and even one process that has a stopping fault. This startling result is due to Fischer, Lynch, and Paterson.[73] It holds even if the communication system provides reliable broadcast that delivers each message either to all the non-faulty processes or to none of them. Real systems get around it by using timeout to make the system synchronous, or by using randomness.

• Even in a synchronous system with perfect processes there is no consensus algorithm that is guaranteed to terminate if an unbounded number of messages can be lost (that is, if communication is effectively asynchronous). The reason is that the last message sent must be pointless, since it might be lost. So it can be dropped to get a shorter algorithm. Repeat this argument to drop all the messages. But clearly an algorithm with no messages can’t achieve consensus. The simplest case of this problem, with just two processes, is called the “two generals problem”.

• In a system with both synchronous processes and synchronous communication, terminating consensus is possible. If f faults are allowed, then:

For processes with stopping faults, consensus requires f+1 processes and an f+1-connected[74] network (that is, at least one good process and a connected subnet of good processes after all the allowed faults have happened). Even if the network is fully connected, it takes f+1 rounds to reach consensus in the worst case.

For processors with Byzantine faults, consensus requires 3f+1 processes, a 2f+1-connected network, at least f+1 rounds of communication, and 2f bits of data communicated.

For processors with Byzantine faults and digital signatures (so that a process can present unforgeable evidence that another process sent it a message), consensus requires f+1 processes. Even if the network is fully connected, it takes f+1 rounds to reach consensus in the worst case.

The amount of communication required depends on the number of faults, the complexity of the algorithm, etc. Randomized algorithms can achieve better results with arbitrarily high probability.

Warning: In many applications the model of no more than f faults may not be realistic if the system is allowed to do the wrong thing when the number of faults exceeds f. It’s often more important to do either the right thing or nothing.

The simplest consensus algorithms

There are two simple and popular algorithms for consensus. Both have the problem that they are not very fault-tolerant.

• A fixed ‘leader’, ‘master’, or ‘coordinator’ process that works like the Consensus spec: it gets all the Allow actions, chooses the outcome, and tells everyone. If it fails, you are out of luck. The abstraction function is just the identity on the leader’s state; TerminatingConsensus.done is true iff everyone has gotten the outcome (or failed permanently). Standard two-phase commit for distributed transactions works this way.

• Simple majority voting. The abstraction function for outcome is the value that has a majority, or nil if there isn’t one. This fails if you don’t get a majority, or if enough members of a majority fail that it isn’t a majority any more. In the latter case you can’t determine the outcome. Example: a votes for 11, b and c vote for 12, and b fails. Now all you can see is one vote for 11 and one for 12, so you can’t tell that 12 had a majority.

The Paxos algorithm: The idea

In the rest of this handout, we describe Lamport’s Paxos algorithm for coding asynchronous consensus; Liskov and Oki independently invented this algorithm as part of a replicated data storage system.[75] Its heart is the best asynchronous algorithm known, which is

run by a set of proposer processes that guide a set of acceptor processes to achieve consensus,

correct no matter how many simultaneous proposers there are and no matter how often proposer or acceptor processes fail and recover or how slow they are, and

guaranteed to terminate if there is a single proposer for a long enough time during which each member of a majority of the acceptor processes is up for long enough, but

possibly non-terminating if there are always too many proposers (fortunate, since we know that guaranteed termination is impossible).

To get a complete consensus algorithm we combine this with a sloppy timeout-based algorithm for choosing a single proposer. If the sloppy algorithm leaves us with no proposer or more than one proposer for a time, the consensus algorithm may not terminate during that time. But if the sloppy algorithm ever produces a single proposer for long enough the algorithm will terminate, no matter how messy things were earlier.

Paxos is the way to do consensus if you want a high degree of fault-tolerance, don’t have any real-time requirements, and can’t tightly control the time to transmit and process a message. There isn’t any simpler algorithm that has the same fault-tolerance. There is lots of code for consensus that doesn’t work.

The grand plan of the algorithm is to have a sequence of rounds, each with a single proposer. This attacks the problem with simple majority voting, which is that a single attempt to get a majority may fall victim to failure. Each Paxos round is a distinct attempt to get a majority. Each acceptor has a state variable s(a) that is a function of the round; that is, there’s a state value s(a)(n) for each round n. To reduce clutter, we write this sna. In each round the proposer:

queries the acceptors to learn their state for past rounds,

chooses a safe value v,

commands the acceptors, trying to get a majority to accept v, and

if it gets a majority, that’s a decision, and it distributes v as the outcome to everyone.

The outcome is the value accepted by a majority in some round. The tricky part of the algorithm is to ensure that there is only one such value, even though there may be lots of rounds.

Most descriptions of Paxos call the acceptors ‘voters’. This is unfortunate, because the acceptors do not make any decisions; they do whatever a proposer requests, unless they have already done something inconsistent with that. In fact, an acceptor can be coded by a memory that has a compare-and-swap operation, as we shall see later. Of course, the proposers and acceptors can run on the same machine, and even in the same process. This is usually the way it’s coded, but the algorithm with separate proposers and acceptors is easier to explain.

It takes a total of 21/2 round trips for a deciding round. If there’s only one proposer that doesn’t fail, Paxos reaches consensus in one round. If the proposer fails repeatedly, or several proposers fight it out, it may take arbitrarily many rounds to reach consensus. This may seem bad, but actually it is a good thing, since we know from Fischer-Lynch-Paterson that we can’t have an algorithm that is guaranteed to terminate.

The rounds are numbered (not necessarily consecutively) with numbers of type N, and the numbers determine a total ordering on the rounds. Each round has a single value, which starts out nil and then may change to one of the allowed values; we write vn for the value of round n. In each round an acceptor starts out neutral, and it can only change to vn or no. A vn or no state can’t change. Note that different rounds can have different values. A round is dead if a majority has state no, and decides if a majority has state vn. If a round decides, that round’s value is the outcome.

The state of Paxos that contributes to the abstraction function to LateConsensus is

MODULE Paxos[ % implements Consensus

V, % data Value to decide on

P WITH {" ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download