2 /* LTTng ltt-tracer.c atomic lockless buffering scheme Promela model v2
3 * Created for the Spin validator.
4 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * TODO : create test cases that will generate an overflow on the offset and
8 * counter type. Counter types smaller than a byte should be used.
10 * Promela only has unsigned char, no signed char.
11 * Because detection of difference < 0 depends on a signed type, but we want
12 * compactness, check also for the values being higher than half of the unsigned
13 * char range (and consider them negative). The model, by design, does not use
14 * offsets or counts higher than 127 because we would then have to use a larger
15 * type (short or int).
17 #define HALF_UCHAR (255/2)
19 /* NUMPROCS 4 : causes event loss with some reader timings.
20 * e.g. 3 events, 1 switch, 1 event (lost, buffer full), read 1 subbuffer
24 /* NUMPROCS 3 : does not cause event loss because buffers are big enough.
26 * e.g. 3 events, 1 switch, read 1 subbuffer
34 #define SUBBUF_SIZE (BUFSIZE / NR_SUBBUFS)
39 byte commit_count[NR_SUBBUFS];
55 /* buffer slot in-use bit. Detects racy use (more than a single process
56 * accessing a slot at any given step).
58 bool buffer_use[BUFSIZE];
61 /* Proceed to a sub-subber switch is needed.
62 * Used in a periodical timer interrupt to fill and ship the current subbuffer
63 * to the reader so we can guarantee a steady flow. If a subbuffer is
64 * completely empty, do not switch.
65 * Also used as "finalize" operation to complete the last subbuffer after
66 * all writers have finished so the last subbuffer can be read by the reader.
70 byte prev_off, new_off, tmp_commit;
76 size = SUBBUF_SIZE - (prev_off % SUBBUF_SIZE);
77 new_off = prev_off + size;
79 :: (new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR)
80 || size == SUBBUF_SIZE ->
81 refcount = refcount - 1;
88 :: prev_off != write_off -> goto cmpxchg_loop
89 :: else -> write_off = new_off;
94 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
96 _commit_sum = _commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
99 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
101 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
107 refcount = refcount - 1;
114 * Writes 1 byte of information in the buffer at the current
115 * "write_off" position and then increment the commit_count of the sub-buffer
116 * the information has been written to.
121 byte prev_off, new_off, tmp_commit;
126 prev_off = write_off;
127 new_off = prev_off + size;
131 :: new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR ->
138 :: prev_off != write_off -> goto cmpxchg_loop
139 :: else -> write_off = new_off;
144 assert(buffer_use[(prev_off + i) % BUFSIZE] == 0);
145 buffer_use[(prev_off + i) % BUFSIZE] = 1;
147 :: i >= size -> break
151 /* writing to buffer...
158 buffer_use[(prev_off + i) % BUFSIZE] = 0;
160 :: i >= size -> break
162 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
164 _commit_sum = _commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
166 /* </formal_verif> */
167 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
169 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
181 refcount = refcount - 1;
186 * Read the information sub-buffer per sub-buffer when available.
188 * Reads the information as soon as it is ready, or may be delayed by
189 * an asynchronous delivery. Being modeled as a process insures all cases
190 * (scheduled very quickly or very late, causing event loss) are covered.
191 * Only one reader per buffer (normally ensured by a mutex). This is modeled
192 * by using a single reader process.
199 :: (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) > 0
200 && (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) < HALF_UCHAR
201 && (commit_count[(read_off % BUFSIZE) / SUBBUF_SIZE]
202 - SUBBUF_SIZE - (((read_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS)
207 :: i < SUBBUF_SIZE ->
208 assert(buffer_use[(read_off + i) % BUFSIZE] == 0);
209 buffer_use[(read_off + i) % BUFSIZE] = 1;
211 :: i >= SUBBUF_SIZE -> break
215 /* reading from buffer...
221 :: i < SUBBUF_SIZE ->
222 buffer_use[(read_off + i) % BUFSIZE] = 0;
224 :: i >= SUBBUF_SIZE -> break
226 read_off = read_off + SUBBUF_SIZE;
228 :: read_off >= (NUMPROCS - events_lost) -> break;
232 /* Waits for all tracer and switcher processes to finish before finalizing
233 * the buffer. Only after that will the reader be allowed to read the
241 refcount = refcount + 1;
242 run switcher(); /* Finalize the last sub-buffer so it can be read. */
260 :: i >= NR_SUBBUFS -> break
264 /* </formal_verif> */
270 :: i >= BUFSIZE -> break
277 refcount = refcount + 1;
280 :: i >= NUMPROCS -> break
285 refcount = refcount + 1;
288 :: i >= NUMSWITCH -> break