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)
40 byte commit_count[NR_SUBBUFS];
51 /* buffer slot in-use bit. Detects racy use (more than a single process
52 * accessing a slot at any given step).
54 bool buffer_use[BUFSIZE];
56 /* Proceed to a sub-subber switch is needed.
57 * Used in a periodical timer interrupt to fill and ship the current subbuffer
58 * to the reader so we can guarantee a steady flow. If a subbuffer is
59 * completely empty, do not switch.
60 * Also used as "finalize" operation to complete the last subbuffer after
61 * all writers have finished so the last subbuffer can be read by the reader.
65 byte prev_off, new_off, tmp_commit;
71 size = SUBBUF_SIZE - (prev_off % SUBBUF_SIZE);
72 new_off = prev_off + size;
74 :: (new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR)
75 || size == SUBBUF_SIZE ->
76 refcount = refcount - 1;
83 :: prev_off != write_off -> goto cmpxchg_loop
84 :: else -> write_off = new_off;
89 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
90 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
92 commit_sum = commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
96 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
102 refcount = refcount - 1;
109 * Writes 1 byte of information in the buffer at the current
110 * "write_off" position and then increment the commit_count of the sub-buffer
111 * the information has been written to.
116 byte prev_off, new_off, tmp_commit;
121 prev_off = write_off;
122 new_off = prev_off + size;
126 :: new_off - read_off > BUFSIZE && new_off - read_off < HALF_UCHAR ->
133 :: prev_off != write_off -> goto cmpxchg_loop
134 :: else -> write_off = new_off;
139 assert(buffer_use[(prev_off + i) % BUFSIZE] == 0);
140 buffer_use[(prev_off + i) % BUFSIZE] = 1;
142 :: i >= size -> break
146 /* writing to buffer...
153 buffer_use[(prev_off + i) % BUFSIZE] = 0;
155 :: i >= size -> break
157 tmp_commit = commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] + size;
158 commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE] = tmp_commit;
160 commit_sum = commit_sum - commit_count[(prev_off % BUFSIZE) / SUBBUF_SIZE]
162 /* </formal_verif> */
164 :: (((prev_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS) + SUBBUF_SIZE -
176 refcount = refcount - 1;
181 * Read the information sub-buffer per sub-buffer when available.
183 * Reads the information as soon as it is ready, or may be delayed by
184 * an asynchronous delivery. Being modeled as a process insures all cases
185 * (scheduled very quickly or very late, causing event loss) are covered.
186 * Only one reader per buffer (normally ensured by a mutex). This is modeled
187 * by using a single reader process.
194 :: (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) > 0
195 && (write_off / SUBBUF_SIZE) - (read_off / SUBBUF_SIZE) < HALF_UCHAR
196 && (commit_count[(read_off % BUFSIZE) / SUBBUF_SIZE]
197 - SUBBUF_SIZE - (((read_off / BUFSIZE) * BUFSIZE) / NR_SUBBUFS)
202 :: i < SUBBUF_SIZE ->
203 assert(buffer_use[(read_off + i) % BUFSIZE] == 0);
204 buffer_use[(read_off + i) % BUFSIZE] = 1;
206 :: i >= SUBBUF_SIZE -> break
210 /* reading from buffer...
216 :: i < SUBBUF_SIZE ->
217 buffer_use[(read_off + i) % BUFSIZE] = 0;
219 :: i >= SUBBUF_SIZE -> break
221 read_off = read_off + SUBBUF_SIZE;
223 :: read_off >= (NUMPROCS - events_lost) -> break;
227 /* Waits for all tracer and switcher processes to finish before finalizing
228 * the buffer. Only after that will the reader be allowed to read the
236 refcount = refcount + 1;
237 run switcher(); /* Finalize the last sub-buffer so it can be read. */
254 :: i >= NR_SUBBUFS -> break
261 :: i >= BUFSIZE -> break
268 refcount = refcount + 1;
271 :: i >= NUMPROCS -> break
276 refcount = refcount + 1;
279 :: i >= NUMSWITCH -> break
286 /* The writer head must always be superior or equal to the reader head.
289 /* assert(write_off - read_off >= 0 && write_off - read_off < HALF_UCHAR);
297 commit_sum = commit_sum + commit_count[j];
299 :: j >= NR_SUBBUFS -> break
303 /* The sum of all subbuffer commit counts must always be lower or equal
304 * to the writer head, because space must be reserved before it is
305 * written to and then committed.
308 assert(write_off - commit_sum >= 0 && write_off - commit_sum < HALF_UCHAR);
310 /* If we have less writers than the buffer space available, we should
313 assert(NUMPROCS + NUMSWITCH > BUFSIZE || events_lost == 0);