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
33 #define SUBBUF_SIZE (BUFSIZE / NR_SUBBUFS)
38 byte commit_count[NR_SUBBUFS];
54 /* buffer slot in-use bit. Detects racy use (more than a single process
55 * accessing a slot at any given step).
57 bool buffer_use[BUFSIZE];
60 /* Proceed to a sub-subber switch is needed.
61 * Used in a periodical timer interrupt to fill and ship the current subbuffer
62 * to the reader so we can guarantee a steady flow. If a subbuffer is
63 * completely empty, do not switch.
64 * Also used as "finalize" operation to complete the last subbuffer after
65 * all writers have finished so the last subbuffer can be read by the reader.
69 byte prev_off, new_off, tmp_commit;
75 size = SUBBUF_SIZE - (prev_off % SUBBUF_SIZE);
76 new_off = prev_off + size;
78 :: (new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR)
79 || size == SUBBUF_SIZE ->
80 refcount = refcount - 1;
87 :: prev_off != write_off -> goto cmpxchg_loop
88 :: else -> write_off = new_off;
93 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
95 _commit_sum = _commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
98 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
100 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
106 refcount = refcount - 1;
113 * Writes 1 byte of information in the buffer at the current
114 * "write_off" position and then increment the commit_count of the sub-buffer
115 * the information has been written to.
120 byte prev_off, new_off, tmp_commit;
125 prev_off = write_off;
126 new_off = prev_off + size;
130 :: new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR ->
137 :: prev_off != write_off -> goto cmpxchg_loop
138 :: else -> write_off = new_off;
143 assert(buffer_use[(prev_off + i) % BUFSIZE] == 0);
144 buffer_use[(prev_off + i) % BUFSIZE] = 1;
146 :: i >= size -> break
150 /* writing to buffer...
157 buffer_use[(prev_off + i) % BUFSIZE] = 0;
159 :: i >= size -> break
161 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
163 _commit_sum = _commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
165 /* </formal_verif> */
166 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
168 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
180 refcount = refcount - 1;
185 * Read the information sub-buffer per sub-buffer when available.
187 * Reads the information as soon as it is ready, or may be delayed by
188 * an asynchronous delivery. Being modeled as a process insures all cases
189 * (scheduled very quickly or very late, causing event loss) are covered.
190 * Only one reader per buffer (normally ensured by a mutex). This is modeled
191 * by using a single reader process.
198 :: (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) > 0
199 && (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) < HALF_UCHAR
200 && (commit_count[(read_off % BUFSIZE) / SUBBUF_SIZE]
201 - SUBBUF_SIZE - (((read_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS)
206 :: i < SUBBUF_SIZE ->
207 assert(buffer_use[(read_off + i) % BUFSIZE] == 0);
208 buffer_use[(read_off + i) % BUFSIZE] = 1;
210 :: i >= SUBBUF_SIZE -> break
214 /* reading from buffer...
220 :: i < SUBBUF_SIZE ->
221 buffer_use[(read_off + i) % BUFSIZE] = 0;
223 :: i >= SUBBUF_SIZE -> break
225 read_off = read_off + SUBBUF_SIZE;
227 :: read_off >= (NUMPROCS - events_lost) -> break;
231 /* Waits for all tracer and switcher processes to finish before finalizing
232 * the buffer. Only after that will the reader be allowed to read the
240 refcount = refcount + 1;
241 run switcher(); /* Finalize the last sub-buffer so it can be read. */
259 :: i >= NR_SUBBUFS -> break
263 /* </formal_verif> */
269 :: i >= BUFSIZE -> break
276 refcount = refcount + 1;
279 :: i >= NUMPROCS -> break
284 refcount = refcount + 1;
287 :: i >= NUMSWITCH -> break