1 /* LTTng ltt-tracer.c atomic lockless buffering scheme Promela model v2
2 * Created for the Spin validator.
3 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
6 * TODO : create test cases that will generate an overflow on the offset and
7 * counter type. Counter types smaller than a byte should be used.
9 * Promela only has unsigned char, no signed char.
10 * Because detection of difference < 0 depends on a signed type, but we want
11 * compactness, check also for the values being higher than half of the unsigned
12 * char range (and consider them negative). The model, by design, does not use
13 * offsets or counts higher than 127 because we would then have to use a larger
14 * type (short or int).
16 #define HALF_UCHAR (255/2)
18 /* NUMPROCS 4 : causes event loss with some reader timings.
19 * e.g. 3 events, 1 switch, 1 event (lost, buffer full), read 1 subbuffer
23 /* NUMPROCS 3 : does not cause event loss because buffers are big enough.
25 * e.g. 3 events, 1 switch, read 1 subbuffer
31 #define SUBBUF_SIZE (BUFSIZE / NR_SUBBUFS)
36 byte commit_count[NR_SUBBUFS];
52 /* buffer slot in-use bit. Detects racy use (more than a single process
53 * accessing a slot at any given step).
55 bool buffer_use[BUFSIZE];
58 /* Proceed to a sub-subber switch is needed.
59 * Used in a periodical timer interrupt to fill and ship the current subbuffer
60 * to the reader so we can guarantee a steady flow. If a subbuffer is
61 * completely empty, do not switch.
62 * Also used as "finalize" operation to complete the last subbuffer after
63 * all writers have finished so the last subbuffer can be read by the reader.
67 byte prev_off, new_off, tmp_commit;
73 size = SUBBUF_SIZE - (prev_off % SUBBUF_SIZE);
74 new_off = prev_off + size;
76 :: (new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR)
77 || size == SUBBUF_SIZE ->
78 refcount = refcount - 1;
85 :: prev_off != write_off -> goto cmpxchg_loop
86 :: else -> write_off = new_off;
91 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
93 _commit_sum = _commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
96 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
98 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
104 refcount = refcount - 1;
111 * Writes 1 byte of information in the buffer at the current
112 * "write_off" position and then increment the commit_count of the sub-buffer
113 * the information has been written to.
118 byte prev_off, new_off, tmp_commit;
123 prev_off = write_off;
124 new_off = prev_off + size;
128 :: new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR ->
135 :: prev_off != write_off -> goto cmpxchg_loop
136 :: else -> write_off = new_off;
141 assert(buffer_use[(prev_off + i) % BUFSIZE] == 0);
142 buffer_use[(prev_off + i) % BUFSIZE] = 1;
144 :: i >= size -> break
148 /* writing to buffer...
155 buffer_use[(prev_off + i) % BUFSIZE] = 0;
157 :: i >= size -> break
159 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
161 _commit_sum = _commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
163 /* </formal_verif> */
164 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
166 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
178 refcount = refcount - 1;
183 * Read the information sub-buffer per sub-buffer when available.
185 * Reads the information as soon as it is ready, or may be delayed by
186 * an asynchronous delivery. Being modeled as a process insures all cases
187 * (scheduled very quickly or very late, causing event loss) are covered.
188 * Only one reader per buffer (normally ensured by a mutex). This is modeled
189 * by using a single reader process.
196 :: (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) > 0
197 && (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) < HALF_UCHAR
198 && (commit_count[(read_off % BUFSIZE) / SUBBUF_SIZE]
199 - SUBBUF_SIZE - (((read_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS)
204 :: i < SUBBUF_SIZE ->
205 assert(buffer_use[(read_off + i) % BUFSIZE] == 0);
206 buffer_use[(read_off + i) % BUFSIZE] = 1;
208 :: i >= SUBBUF_SIZE -> break
212 /* reading from buffer...
218 :: i < SUBBUF_SIZE ->
219 buffer_use[(read_off + i) % BUFSIZE] = 0;
221 :: i >= SUBBUF_SIZE -> break
223 read_off = read_off + SUBBUF_SIZE;
225 :: read_off >= (NUMPROCS - events_lost) -> break;
229 /* Waits for all tracer and switcher processes to finish before finalizing
230 * the buffer. Only after that will the reader be allowed to read the
238 refcount = refcount + 1;
239 run switcher(); /* Finalize the last sub-buffer so it can be read. */
257 :: i >= NR_SUBBUFS -> break
261 /* </formal_verif> */
267 :: i >= BUFSIZE -> break
274 refcount = refcount + 1;
277 :: i >= NUMPROCS -> break
282 refcount = refcount + 1;
285 :: i >= NUMSWITCH -> break