8dca63c150e5efe831f2b5b35d5e175a32aac9f9
[cdsspec-compiler.git] / notes / nondeterm-spec.txt
1 Modification to the current specifications.
2
3 I. Order
4 -- Sequential order (SO): Some total order that is consistent with the union of
5 happens-before and SC relation.
6
7 II. State
8 1. Global state: We allow users to specify a single global state so that when we
9 want to execute the sequential replay (execute the sequential order), the whole
10 process is similar to executing an sequential program. Such a global state is
11 similiar to the internal state of a sequential data structure. We also have this
12 in our old version (the rejection of PLDI'16). As an analogy to the cache-memory
13 model, the global state we define here is similar to the main memory in the
14 sense that there does not exist a real total order to all memory accesses, but
15 to some degree (with the non-deterministic specifications) we can have an
16 illution that there is some total order.
17
18 2. Local State: Beside of having one global state (the one that we have in the
19 old approach), we also allow each method call to have a local state.  This local
20 state is the accumulative side effects of the subset of method calls that happen
21 before the current method call. As an analogy to the cache-memory model, the
22 local state we define here is similar to cache, a local state is local to the
23 sense that the current method call must have seen those effects. The main goal
24 of having this is to support tighter non-deterministic specifications.
25
26 To evaluate the local state of each method call, an obvious approach is to
27 execute the subset of methods that happen before the current method in the
28 sequential order from the initial state. A optimization we can make is that we
29 start to evaluate the state from the most recent deviding node which every other
30 node in that subset is either hb before or after. Also, since local states are
31 not required in specifications all the time, it is only evaluated when needed.
32
33 III. Specifications
34 Our specification language supports using the following primitives to access
35 both global state and local state so that users can use those to write
36 specifications with different level of tightness.
37
38 To support tighter specifications, we introduce the concept of concurrent set of
39 method calls, meaning that for a specific method call, it can basically see the
40 effect of two different categories of method calls --- one that happens before
41 it, and one that concurrently execute with it. It is worth noting that when two
42 two method calls execute concurrently, in general there can be the following two
43 effects: 1) those concurrent methods can happen in either order, and the final
44 result remains the same. A concurrent FIFO is an example, in which concurrent
45 enqueue and dequeue methods can happen in a random order; and 2) the order of
46 those concurrent methods will affect the final result. The C/C++11 atomics is an
47 example, in which when concurrent stores to the same location execute in
48 different order, a later store will have different result.
49
50 1. CONCURRENT: This primitive extracts all the methods that executes
51 "concurrently" with the current method --- neither hb/SC before nor after the
52 current method --- and returns as a set. It is worth noting that the concurrent
53 method calls cannot be accessed for calculating local state but only for
54 assertions.
55
56 2. PREV: This primitive extracts all the methods that execute right before the
57 current method in the execution graph --- most recent method calls that are
58 hb/SC before the current method call --- and returns as a set. For each method
59 in this set, the current method's specification can access their local state.
60
61 3. NEXT: This primitive extracts all the methods that execute right after the
62 current method in the execution graph, and returns as a set. For each method in
63 this set, the current method's specification cannot access their local state.
64
65 4. LOCAL: This primitive allows users to access the local state of a method
66 call. It is worth noting that in order to calculate the local state of a method
67 call, one can only access the set of method calls that happen before the current
68 method call.
69
70 Our specifications allow two ways of calculating the local states, a default way
71 and a user-customized way. The default approach is to execute the set of method
72 calls that happen before the current method call in the sequential order, and a
73 the user-customized approach supports users to calculate the local state by
74 using the PREV primitive to access the local state of previous method calls.
75
76 5. COPY: This is the function that users provide to deep-copy the state. We
77 require users to provide such a primitive function because each local state
78 should be have its own copy.
79
80 6. FINALLY: This is the function that allows users to specify some final check
81 on the state. Initially, this check will only access the global state. However,
82 for the concern of expressiveness, we allow users to access the initial state,
83 meaning that users can basically access the whole graph and the local state of
84 each method call. This will enable to users to use the graph model (the relaxed
85 atomics can be specified) although the complxity of using that can get quite
86 complex.
87
88 IV. Examples
89
90 // Global specification
91 @DeclareState: // Declare the state structure
92 @InitState: // How do we initialize the state
93 @CopyState: // A function on how to copy an existing state
94 @Commutativity: Method1 <-> Method2 (Guard) // Guard can only access the return
95                 // value and the arguments of the two method calls
96
97 // Interface specification
98 @Interface: InterfaceName // Required; a label to represent the interface
99 @LocalState: // Optional; to calculate the accumulative local state before this
100                          // method call in a customized fashion. If not specified here, the
101                          // local state would be default, which is the result of the
102                          // execution on the subset of method calls in the sequential order
103 @PreCondition: // Optional; checking code
104 @LocalSideEffect: // Optional; to calculate the side effect this method call
105                                   // have on the local state in a customized fashion. If this
106                                   // field is not stated, it means we don't care about it.
107 @SideEffect: // Optional; to calculate the side effect on the global state. When
108                 // the "@LocalSideEffect" specification is ommitted, we also impose the
109                 // same side effect on the set of method calls that happen before this
110                 // method call in the sequential order.
111 @PostCondition: // Optional; checking code
112
113 // Ordering point specification
114 @OPDefine: condition    // If the specified condition satisfied, the atomic
115                                                 // operation right before is an ordering point
116
117 @PotentialOP(Label): condition  // If the specified condition satisfied, the
118                                                                 // atomic operation right before is a potential
119                                                                 // ordering point, and we label it with a tag
120
121 @OPCheck(Label): condition      // If the specified condition satisfied, the
122                                                         // potential ordering point defined earlier with the
123                                                         // same tag becomes an ordering point
124
125 @OPClear: condition             // If the specified condition satisfied, all the
126                                                 // ordering points and potential ordering points will be
127                                                 // cleared
128
129 @OPClearDefine: condition       // If the specified condition satisfied, all the
130                                                         // ordering points and potential ordering points will
131                                                         // be cleared, and the atomic operation right before
132                                                         // becomes an ordering point. This is a syntax sugar
133                                                         // as the combination of an "OPClear" and "OPDefine"
134                                                         // statement
135
136
137 1. The register examples: Basically, we can think of registers as the cache on a
138 memory system. The effect of a load or store basically tells us what the current
139 value in the cache line is, and a load can read from values that can be
140 potentially in the cache --- either one of the concurrent store update the cache
141 or it inherites one of the the previous local state in the execution graph.
142
143 ----------   Interfae   ----------
144 void init(atomic_int &loc, int initial);
145 int load(atomic_int &loc);
146 void store(atomic_int &loc, int val);
147 ----------   Interfae   ----------
148
149 a. The SC atomics --- the classic linearizability approach
150
151 b. The RA (release/acquire) C/C++ atomics
152 // For RA atomics, a load must read its value from a store that happens before
153 // it.
154 ----------   Specification   ----------
155 @DeclareVar: int x;
156 @InitVar: x = 0;
157
158 @Interface: Store
159 @SideEffect: LOCAL(x) = val;
160 void store(int *loc, int val);
161
162 @Interface: Load
163 @PreCondition:
164         Size(Subset(PREV, LOCAL(x) == RET)) > 0;
165 @SideEffect: LOCAL(x) = RET;
166 int load(int *loc);
167
168 c. The C/C++ atomics (a slightly loose specification)
169 // Actually, any concurrent data structures that rely modification-order to be
170 // correct would not have a precicely tight specification under our model, and
171 // C/C++ relaxed atomics is an example. See the following read-read coherence
172 // example.
173
174 // T1                           // T2
175 x = 1;                          x = 2;
176
177 // T3
178 r1 = x; // r1 == 1
179 r2 = x; // r2 == 2
180 r3 = x; // r3 == 1
181
182 Our model cannot prevent such a case from happening. However, we can still have
183 a slightly loose specification which basically states that a load can read from
184 any store that either immediately happens before it or concurrently executes.
185
186
187
188 // We define a struct called MethodCall to represent the data we would collect
189 // and communicate between the real execution and the checking process
190 typedef struct MethodCall {
191         string interfaceName; // The interface label name
192         void *value; // The pointer that points to the struct that have the return
193                                  // value and the arguments
194         void *localState; // The pointer that points to the struct that represents
195                                           // the (local) state
196         vector<MethodCall*> *prev; // Method calls that are hb right before me
197         vector<MethodCall*> *next; // Method calls that are hb right after me
198         vector<MethodCall*> *concurrent; // Method calls that are concurrent with me
199 } MethodCall;
200
201 We will automatically generate two types of struct. One is the state struct, and
202 we will call it StateStruct. This state is shared by all the global and local
203 state. The other one is a per interface struct, and it wraps the return value
204 (RET) and the arguments as its field. We will name those struct with the
205 interface label name.
206
207 // Some very nice C/C++ macro definition to make specifications a lot easier
208 // 1. ForEach  --> to iterate all 
209 #define ForEach(item, list) \
210         for (iterator<MethodCall*> _mIter = list->begin(), \
211                 MethodCall *item = *_mIter; _mIter != list->end(); item = (++iter != \
212                 list->end()) ? *_mIter : NULL)
213
214
215 *********************************************
216 // 1.1 Subset(set, guard)  --> to get a subset of method calls by a boolean
217 // expression; This takes advantage of C++11 std::function features and C macros.
218
219 // 1.2 Size(set) --> to get the size of a method set
220 #define Size(set) set->size()
221
222 // 1.3 Belong(set, method) --> whether method belongs to set
223 #define Belong(set, method) std::find(set->begin(), set->end(), method) != set->end()
224
225 // 1.4 Intersect(set1, set2) --> the intersection of two method sets
226 inline MethodSet Intersect(MethodSet set1, MethodSet set2) {
227         MethodSet res = NewSet;
228         ForEach (m, set1) {
229                 if (Belong(set2, m))
230                         res->push_back(m);
231         }
232         return res;
233 }
234
235 // 1.5 Union(set1, set2) --> the union of two method sets
236 inline MethodSet Union(MethodSet set1, MethodSet set2) {
237         MethodSet res = NewSet(set1);
238         ForEach (m, set2) {
239                 if (!Belong(set1, m))
240                         res->push_back(m);
241         }
242         return res;
243 }
244
245 // 1.6 Insert(set, method) --> add a method to the set
246 inline bool Insert(MethodSet set, Method m) {
247         if (Belong(set, m))
248                 return false;
249         else {
250                 set->push_back(m);
251                 return true;
252         }
253 }
254
255 // 1.7 Subtract(set1, set2) --> subtract set2 from set1 
256 inline MethodSet Subtract(MethodSet set1, MethodSet set2) {
257         MethodSet res = NewSet;
258         ForEach (m, set1) {
259                 if (!Belong(set2, m))
260                         res->push_back(m);
261         }
262         return res;
263 }
264
265 // 1.8 MakeSet(count, ...) --> Make a set from the arguments
266
267         
268 // 2. Local(method, field)
269 #define Local(method, field) ((StateStruct*) method->localState)->field
270
271 // 3. Value(method, type, field)
272 #define Value(method, type, field) ((type*) method->value)->field
273 3.1 Return
274 #define Ret(method, type) Value(method, type, RET)
275 3.2 Arguments
276 #define Arg(method, type, arg) Value(method, type, arg)
277
278
279 // 4. Name(mehtod)
280 #defien Lable(method) method->interfaceName
281
282 // 5. Prev(method)
283 #define Prev(method) mehtod->prev
284
285 // 6. Next(method)
286 #define Next(method) mehtod->next
287
288 // 7. Concurrent(method)
289 #define Concurrent(method) mehtod->concurrent
290
291
292 ----------   Specification   ----------
293 @DeclareVar: int x;
294 @InitVar: x = 0;
295
296 @Interface: Store
297 @SideEffect: LOCAL(x) = val;
298 void store(int *loc, int val);
299
300
301 @Interface: Load
302 @PreCondition:
303         // Auto generated code
304         // MethodCall *ME = ((SomeTypeConversion) info)->method;
305         
306         int count = Size(Subset(Prev, LOCAL(x) == RET)) 
307                 + Size(Subset(CONCURRENT, NAME == "Store" && ARG(Store, val) == RET))
308         return count > 0;
309 @SideEffect: LOCAL(x) = RET;
310 int load(int *loc);
311
312 d. The C/C++ normal memory accesses
313 - Use the admissibility requirement, then the classic linearizability approach
314 on the admissible executions
315
316 2. The FIFO queue example.
317 ----------   Specification   ----------
318 // A FIFO queue should have the following properties held:
319 // 1. The enq() methods should conflict
320 // 2. The deq() methods that succeed should conflict
321 // 3. Corresponding enq() and deq() methods should conflict
322 // 4. An enqueued item can be dequeued by at most once
323 // 5. A dequeued item must have a corresponding enqueued item
324 // 6. When a queue is NOT "empty" (users can tightly or loosely define
325 // emptiness), and there comes a deq() method, the deq() method should succeed
326
327
328 @DeclareVar: vector<int> *q;
329 @InitVar: q = new voctor<int>;
330 @Copy: New.q = new vector<int>(Old.q);
331 // Fails to dequeue
332 @Commutativity: Deq <-> Deq (!_M1.RET || !_M2.RET)
333 // The dequeuer doesn't dequeue from that enqueuer
334 @Commutativity: Enq <-> Deq (!_M2.RET || (_M2.RET && Enq.val != *Deq.res))
335
336 @Interface: Enq
337 @SideEffect: q->push_back(val);
338 void enq(queue_t *q, int val);
339
340 @Interface: Deq
341 @PreCondition:
342         // Check whether the queue is really empty
343         // Either the local state is an empty queue, or for all those remaining
344         // elements in the local queue, there should be some concurrent dequeuers to
345         // dequeue them
346         if (!RET) {
347                 // Local state is empty
348                 if (Local(q)->size() == 0) return true;
349                 // Otherwise check there must be other concurrent dequeuers
350                 ForEach (item, Local(q)) {
351                         // Check there's some concurrent dequeuer for this item
352                         if (Size(Subset(CONCURRENT, NAME == "Deq" && RET(Deq) &&
353                                 *ARG(Deq, res) == item)) == 0) return false;
354                 }
355                 return true;
356         } else { // Check the global queue state
357                 return q->back() == *res;
358         }
359 @SideEffect:
360         if (RET) q->pop_back();
361 bool deq(queue_t *q, int *res);
362
363
364
365 A good example to simulate a queue data structure is as follows.
366 Suppose we have a special structure
367 typedef struct Q {
368         atomic_int x;
369         atomic_int y;
370 } Q;
371
372 , and we have two interface on Q, read() and write(), where the write and read
373 method calls are synchronized by themselves, and they have to read and write the
374 x and y fields in turn.
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394 ----------------------------------------------------------------------------------------
395 We also need to think about the non-ordered queue.
396
397 ####
398 Combiming Data Structures --- 
399 For example, a queue, a -hb->c, b -hb-> e.
400
401 // T1
402 enq(1) -> {1} - {$}    // a
403 enq(2) -> {1, 2} - {$}   // b
404
405 // T2
406 deq(1) -> {$} - {1}   // c
407 deq($) -> {$} - {1}   // d
408
409 // State before this method  
410 JOIN ({1, 2} - {$}, {$} - {1}) = {2} - {1}
411 deq(2) -> {$} - {1, 2}
412
413
414
415
416