Commit | Line | Data |
---|---|---|
b5b073e2 PMF |
1 | /* |
2 | * buffers.c | |
3 | * LTTng userspace tracer buffering system | |
4 | * | |
5 | * Copyright (C) 2009 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca) | |
6 | * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca) | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
204141ee | 23 | #include <unistd.h> |
b5b073e2 PMF |
24 | #include <sys/mman.h> |
25 | #include <sys/ipc.h> | |
26 | #include <sys/shm.h> | |
27 | #include <fcntl.h> | |
909bc43f | 28 | #include <stdlib.h> |
518d7abb PMF |
29 | |
30 | #include <ust/clock.h> | |
31 | ||
b5b073e2 PMF |
32 | #include "buffers.h" |
33 | #include "channels.h" | |
34 | #include "tracer.h" | |
35 | #include "tracercore.h" | |
36 | #include "usterr.h" | |
37 | ||
b73a4c47 PMF |
38 | struct ltt_reserve_switch_offsets { |
39 | long begin, end, old; | |
40 | long begin_switch, end_switch_current, end_switch_old; | |
41 | size_t before_hdr_pad, size; | |
42 | }; | |
43 | ||
44 | ||
b5b073e2 | 45 | static DEFINE_MUTEX(ust_buffers_channels_mutex); |
0222e121 | 46 | static CDS_LIST_HEAD(ust_buffers_channels); |
b5b073e2 | 47 | |
204141ee PMF |
48 | static int get_n_cpus(void) |
49 | { | |
50 | int result; | |
51 | static int n_cpus = 0; | |
52 | ||
c7dc133c PMF |
53 | if(!n_cpus) { |
54 | /* On Linux, when some processors are offline | |
55 | * _SC_NPROCESSORS_CONF counts the offline | |
56 | * processors, whereas _SC_NPROCESSORS_ONLN | |
57 | * does not. If we used _SC_NPROCESSORS_ONLN, | |
58 | * getcpu() could return a value greater than | |
59 | * this sysconf, in which case the arrays | |
60 | * indexed by processor would overflow. | |
61 | */ | |
62 | result = sysconf(_SC_NPROCESSORS_CONF); | |
63 | if(result == -1) { | |
64 | return -1; | |
65 | } | |
66 | ||
67 | n_cpus = result; | |
204141ee PMF |
68 | } |
69 | ||
c7dc133c | 70 | return n_cpus; |
204141ee PMF |
71 | } |
72 | ||
bb3132c8 MD |
73 | /** |
74 | * _ust_buffers_strncpy_fixup - Fix an incomplete string in a ltt_relay buffer. | |
75 | * @buf : buffer | |
76 | * @offset : offset within the buffer | |
77 | * @len : length to write | |
78 | * @copied: string actually copied | |
79 | * @terminated: does string end with \0 | |
b73a4c47 | 80 | * |
bb3132c8 | 81 | * Fills string with "X" if incomplete. |
b73a4c47 | 82 | */ |
bb3132c8 MD |
83 | void _ust_buffers_strncpy_fixup(struct ust_buffer *buf, size_t offset, |
84 | size_t len, size_t copied, int terminated) | |
b73a4c47 | 85 | { |
bb3132c8 MD |
86 | size_t buf_offset, cpy; |
87 | ||
88 | if (copied == len) { | |
89 | /* | |
90 | * Deal with non-terminated string. | |
91 | */ | |
92 | assert(!terminated); | |
93 | offset += copied - 1; | |
94 | buf_offset = BUFFER_OFFSET(offset, buf->chan); | |
95 | /* | |
96 | * Underlying layer should never ask for writes across | |
97 | * subbuffers. | |
98 | */ | |
99 | assert(buf_offset | |
100 | < buf->chan->subbuf_size*buf->chan->subbuf_cnt); | |
101 | ust_buffers_do_memset(buf->buf_data + buf_offset, '\0', 1); | |
102 | return; | |
103 | } | |
104 | ||
105 | /* | |
106 | * Deal with incomplete string. | |
107 | * Overwrite string's \0 with X too. | |
108 | */ | |
109 | cpy = copied - 1; | |
110 | assert(terminated); | |
111 | len -= cpy; | |
112 | offset += cpy; | |
113 | buf_offset = BUFFER_OFFSET(offset, buf->chan); | |
114 | ||
115 | /* | |
116 | * Underlying layer should never ask for writes across subbuffers. | |
117 | */ | |
118 | assert(buf_offset | |
119 | < buf->chan->subbuf_size*buf->chan->subbuf_cnt); | |
b73a4c47 | 120 | |
bb3132c8 MD |
121 | ust_buffers_do_memset(buf->buf_data + buf_offset, |
122 | 'X', len); | |
b73a4c47 | 123 | |
bb3132c8 MD |
124 | /* |
125 | * Overwrite last 'X' with '\0'. | |
126 | */ | |
127 | offset += len - 1; | |
128 | buf_offset = BUFFER_OFFSET(offset, buf->chan); | |
129 | /* | |
130 | * Underlying layer should never ask for writes across subbuffers. | |
131 | */ | |
132 | assert(buf_offset | |
133 | < buf->chan->subbuf_size*buf->chan->subbuf_cnt); | |
134 | ust_buffers_do_memset(buf->buf_data + buf_offset, '\0', 1); | |
b73a4c47 PMF |
135 | } |
136 | ||
ef15e552 NC |
137 | static void ltt_buffer_begin(struct ust_buffer *buf, |
138 | u64 tsc, unsigned int subbuf_idx) | |
139 | { | |
140 | struct ust_channel *channel = buf->chan; | |
141 | struct ltt_subbuffer_header *header = | |
142 | (struct ltt_subbuffer_header *) | |
143 | ust_buffers_offset_address(buf, | |
144 | subbuf_idx * buf->chan->subbuf_size); | |
b5b073e2 | 145 | |
ef15e552 NC |
146 | header->cycle_count_begin = tsc; |
147 | header->data_size = 0xFFFFFFFF; /* for recognizing crashed buffers */ | |
148 | header->sb_size = 0xFFFFFFFF; /* for recognizing crashed buffers */ | |
149 | /* FIXME: add memory barrier? */ | |
150 | ltt_write_trace_header(channel->trace, header); | |
151 | } | |
152 | ||
153 | static int map_buf_data(struct ust_buffer *buf, size_t *size) | |
b5b073e2 PMF |
154 | { |
155 | void *ptr; | |
156 | int result; | |
157 | ||
158 | *size = PAGE_ALIGN(*size); | |
159 | ||
160 | result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700); | |
ef15e552 | 161 | if (result < 0 && errno == EINVAL) { |
b5b073e2 PMF |
162 | ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased."); |
163 | return -1; | |
ef15e552 | 164 | } else if (result < 0) { |
b5b073e2 PMF |
165 | PERROR("shmget"); |
166 | return -1; | |
167 | } | |
168 | ||
169 | ptr = shmat(buf->shmid, NULL, 0); | |
ef15e552 | 170 | if (ptr == (void *) -1) { |
b5b073e2 PMF |
171 | perror("shmat"); |
172 | goto destroy_shmem; | |
173 | } | |
174 | ||
175 | /* Already mark the shared memory for destruction. This will occur only | |
176 | * when all users have detached. | |
177 | */ | |
178 | result = shmctl(buf->shmid, IPC_RMID, NULL); | |
179 | if(result == -1) { | |
180 | perror("shmctl"); | |
181 | return -1; | |
182 | } | |
183 | ||
184 | buf->buf_data = ptr; | |
185 | buf->buf_size = *size; | |
186 | ||
187 | return 0; | |
188 | ||
ef15e552 | 189 | destroy_shmem: |
b5b073e2 PMF |
190 | result = shmctl(buf->shmid, IPC_RMID, NULL); |
191 | if(result == -1) { | |
192 | perror("shmctl"); | |
193 | } | |
194 | ||
195 | return -1; | |
196 | } | |
197 | ||
ef15e552 | 198 | static int open_buf(struct ust_channel *chan, int cpu) |
b5b073e2 | 199 | { |
ef15e552 NC |
200 | int result, fds[2]; |
201 | unsigned int j; | |
202 | struct ust_trace *trace = chan->trace; | |
203 | struct ust_buffer *buf = chan->buf[cpu]; | |
204 | unsigned int n_subbufs = chan->subbuf_cnt; | |
b5b073e2 | 205 | |
ef15e552 NC |
206 | |
207 | result = map_buf_data(buf, &chan->alloc_size); | |
208 | if (result < 0) | |
204141ee | 209 | return -1; |
b5b073e2 | 210 | |
ef15e552 NC |
211 | buf->commit_count = |
212 | zmalloc(sizeof(*buf->commit_count) * n_subbufs); | |
213 | if (!buf->commit_count) | |
214 | goto unmap_buf; | |
b5b073e2 | 215 | |
ef15e552 NC |
216 | result = pipe(fds); |
217 | if (result < 0) { | |
218 | PERROR("pipe"); | |
219 | goto free_commit_count; | |
220 | } | |
221 | buf->data_ready_fd_read = fds[0]; | |
222 | buf->data_ready_fd_write = fds[1]; | |
b5b073e2 | 223 | |
ef15e552 NC |
224 | buf->cpu = cpu; |
225 | buf->chan = chan; | |
b5b073e2 | 226 | |
ef15e552 NC |
227 | uatomic_set(&buf->offset, ltt_subbuffer_header_size()); |
228 | uatomic_set(&buf->consumed, 0); | |
229 | uatomic_set(&buf->active_readers, 0); | |
230 | for (j = 0; j < n_subbufs; j++) { | |
231 | uatomic_set(&buf->commit_count[j].cc, 0); | |
232 | uatomic_set(&buf->commit_count[j].cc_sb, 0); | |
b5b073e2 PMF |
233 | } |
234 | ||
ef15e552 | 235 | ltt_buffer_begin(buf, trace->start_tsc, 0); |
b5b073e2 | 236 | |
ef15e552 NC |
237 | uatomic_add(&buf->commit_count[0].cc, ltt_subbuffer_header_size()); |
238 | ||
239 | uatomic_set(&buf->events_lost, 0); | |
240 | uatomic_set(&buf->corrupted_subbuffers, 0); | |
241 | ||
242 | memset(buf->commit_seq, 0, sizeof(buf->commit_seq[0]) * n_subbufs); | |
243 | ||
244 | return 0; | |
245 | ||
246 | free_commit_count: | |
247 | free(buf->commit_count); | |
248 | ||
249 | unmap_buf: | |
250 | if (shmdt(buf->buf_data) < 0) { | |
251 | PERROR("shmdt failed"); | |
252 | } | |
253 | ||
254 | return -1; | |
b5b073e2 PMF |
255 | } |
256 | ||
ef15e552 | 257 | static void close_buf(struct ust_buffer *buf) |
b5b073e2 | 258 | { |
204141ee | 259 | int result; |
b5b073e2 | 260 | |
ef15e552 NC |
261 | result = shmdt(buf->buf_data); |
262 | if (result < 0) { | |
263 | PERROR("shmdt"); | |
264 | } | |
b5b073e2 | 265 | |
ef15e552 NC |
266 | result = close(buf->data_ready_fd_read); |
267 | if (result < 0) { | |
268 | PERROR("close"); | |
269 | } | |
b5b073e2 | 270 | |
ef15e552 NC |
271 | result = close(buf->data_ready_fd_write); |
272 | if (result < 0 && errno != EBADF) { | |
273 | PERROR("close"); | |
274 | } | |
b5b073e2 PMF |
275 | } |
276 | ||
b5b073e2 | 277 | |
ef15e552 NC |
278 | static int open_channel(struct ust_channel *chan, size_t subbuf_size, |
279 | size_t subbuf_cnt) | |
b5b073e2 | 280 | { |
204141ee PMF |
281 | int i; |
282 | int result; | |
283 | ||
b5b073e2 PMF |
284 | if(subbuf_size == 0 || subbuf_cnt == 0) |
285 | return -1; | |
286 | ||
b73a4c47 PMF |
287 | /* Check that the subbuffer size is larger than a page. */ |
288 | WARN_ON_ONCE(subbuf_size < PAGE_SIZE); | |
289 | ||
290 | /* | |
291 | * Make sure the number of subbuffers and subbuffer size are power of 2. | |
292 | */ | |
293 | WARN_ON_ONCE(hweight32(subbuf_size) != 1); | |
294 | WARN_ON(hweight32(subbuf_cnt) != 1); | |
295 | ||
b5b073e2 PMF |
296 | chan->version = UST_CHANNEL_VERSION; |
297 | chan->subbuf_cnt = subbuf_cnt; | |
298 | chan->subbuf_size = subbuf_size; | |
299 | chan->subbuf_size_order = get_count_order(subbuf_size); | |
b73a4c47 | 300 | chan->alloc_size = subbuf_size * subbuf_cnt; |
204141ee | 301 | |
f7b16408 | 302 | pthread_mutex_lock(&ust_buffers_channels_mutex); |
ef15e552 NC |
303 | for (i=0; i < chan->n_cpus; i++) { |
304 | result = open_buf(chan, i); | |
204141ee PMF |
305 | if (result == -1) |
306 | goto error; | |
307 | } | |
0222e121 | 308 | cds_list_add(&chan->list, &ust_buffers_channels); |
f7b16408 | 309 | pthread_mutex_unlock(&ust_buffers_channels_mutex); |
b5b073e2 PMF |
310 | |
311 | return 0; | |
312 | ||
b2ccc231 | 313 | /* Error handling */ |
204141ee | 314 | error: |
b2ccc231 MD |
315 | for(i--; i >= 0; i--) |
316 | close_buf(chan->buf[i]); | |
204141ee | 317 | |
f7b16408 | 318 | pthread_mutex_unlock(&ust_buffers_channels_mutex); |
b5b073e2 PMF |
319 | return -1; |
320 | } | |
321 | ||
ef15e552 | 322 | static void close_channel(struct ust_channel *chan) |
b5b073e2 | 323 | { |
204141ee PMF |
324 | int i; |
325 | if(!chan) | |
b5b073e2 PMF |
326 | return; |
327 | ||
f7b16408 | 328 | pthread_mutex_lock(&ust_buffers_channels_mutex); |
204141ee PMF |
329 | for(i=0; i<chan->n_cpus; i++) { |
330 | /* FIXME: if we make it here, then all buffers were necessarily allocated. Moreover, we don't | |
331 | * initialize to NULL so we cannot use this check. Should we? */ | |
332 | //ust// if (chan->buf[i]) | |
ef15e552 | 333 | close_buf(chan->buf[i]); |
204141ee | 334 | } |
b5b073e2 | 335 | |
0222e121 | 336 | cds_list_del(&chan->list); |
ef15e552 | 337 | |
f7b16408 | 338 | pthread_mutex_unlock(&ust_buffers_channels_mutex); |
b5b073e2 PMF |
339 | } |
340 | ||
b5b073e2 PMF |
341 | static void ltt_force_switch(struct ust_buffer *buf, |
342 | enum force_switch_mode mode); | |
343 | ||
b5b073e2 | 344 | |
b5b073e2 PMF |
345 | |
346 | /* | |
347 | * offset is assumed to never be 0 here : never deliver a completely empty | |
348 | * subbuffer. The lost size is between 0 and subbuf_size-1. | |
349 | */ | |
b73a4c47 | 350 | static notrace void ltt_buffer_end(struct ust_buffer *buf, |
b5b073e2 PMF |
351 | u64 tsc, unsigned int offset, unsigned int subbuf_idx) |
352 | { | |
353 | struct ltt_subbuffer_header *header = | |
354 | (struct ltt_subbuffer_header *) | |
b73a4c47 | 355 | ust_buffers_offset_address(buf, |
b5b073e2 | 356 | subbuf_idx * buf->chan->subbuf_size); |
8c36d1ee | 357 | u32 data_size = SUBBUF_OFFSET(offset - 1, buf->chan) + 1; |
b5b073e2 | 358 | |
8c36d1ee PMF |
359 | header->data_size = data_size; |
360 | header->sb_size = PAGE_ALIGN(data_size); | |
b5b073e2 | 361 | header->cycle_count_end = tsc; |
b102c2b0 PMF |
362 | header->events_lost = uatomic_read(&buf->events_lost); |
363 | header->subbuf_corrupt = uatomic_read(&buf->corrupted_subbuffers); | |
719569e4 PMF |
364 | if(unlikely(header->events_lost > 0)) { |
365 | DBG("Some events (%d) were lost in %s_%d", header->events_lost, buf->chan->channel_name, buf->cpu); | |
366 | } | |
b5b073e2 PMF |
367 | } |
368 | ||
369 | /* | |
370 | * This function should not be called from NMI interrupt context | |
371 | */ | |
372 | static notrace void ltt_buf_unfull(struct ust_buffer *buf, | |
373 | unsigned int subbuf_idx, | |
374 | long offset) | |
375 | { | |
b5b073e2 PMF |
376 | } |
377 | ||
b73a4c47 | 378 | /* |
0222e121 | 379 | * Promote compiler cmm_barrier to a smp_mb(). |
b73a4c47 PMF |
380 | * For the specific LTTng case, this IPI call should be removed if the |
381 | * architecture does not reorder writes. This should eventually be provided by | |
382 | * a separate architecture-specific infrastructure. | |
383 | */ | |
e17571a5 PMF |
384 | //ust// static void remote_mb(void *info) |
385 | //ust// { | |
386 | //ust// smp_mb(); | |
387 | //ust// } | |
b73a4c47 PMF |
388 | |
389 | int ust_buffers_get_subbuf(struct ust_buffer *buf, long *consumed) | |
b5b073e2 PMF |
390 | { |
391 | struct ust_channel *channel = buf->chan; | |
392 | long consumed_old, consumed_idx, commit_count, write_offset; | |
b73a4c47 PMF |
393 | //ust// int retval; |
394 | ||
b102c2b0 | 395 | consumed_old = uatomic_read(&buf->consumed); |
b5b073e2 | 396 | consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan); |
b102c2b0 | 397 | commit_count = uatomic_read(&buf->commit_count[consumed_idx].cc_sb); |
b5b073e2 PMF |
398 | /* |
399 | * Make sure we read the commit count before reading the buffer | |
400 | * data and the write offset. Correct consumed offset ordering | |
401 | * wrt commit count is insured by the use of cmpxchg to update | |
402 | * the consumed offset. | |
b73a4c47 PMF |
403 | * smp_call_function_single can fail if the remote CPU is offline, |
404 | * this is OK because then there is no wmb to execute there. | |
405 | * If our thread is executing on the same CPU as the on the buffers | |
406 | * belongs to, we don't have to synchronize it at all. If we are | |
0222e121 | 407 | * migrated, the scheduler will take care of the memory cmm_barriers. |
b73a4c47 PMF |
408 | * Normally, smp_call_function_single() should ensure program order when |
409 | * executing the remote function, which implies that it surrounds the | |
410 | * function execution with : | |
411 | * smp_mb() | |
412 | * send IPI | |
413 | * csd_lock_wait | |
414 | * recv IPI | |
415 | * smp_mb() | |
416 | * exec. function | |
417 | * smp_mb() | |
418 | * csd unlock | |
419 | * smp_mb() | |
420 | * | |
421 | * However, smp_call_function_single() does not seem to clearly execute | |
ef15e552 | 422 | * such barriers. It depends on spinlock semantic to provide the barrier |
b73a4c47 PMF |
423 | * before executing the IPI and, when busy-looping, csd_lock_wait only |
424 | * executes smp_mb() when it has to wait for the other CPU. | |
425 | * | |
426 | * I don't trust this code. Therefore, let's add the smp_mb() sequence | |
427 | * required ourself, even if duplicated. It has no performance impact | |
428 | * anyway. | |
429 | * | |
0222e121 | 430 | * smp_mb() is needed because cmm_smp_rmb() and cmm_smp_wmb() only order read vs |
b73a4c47 | 431 | * read and write vs write. They do not ensure core synchronization. We |
0222e121 | 432 | * really have to ensure total order between the 3 cmm_barriers running on |
b73a4c47 PMF |
433 | * the 2 CPUs. |
434 | */ | |
435 | //ust// #ifdef LTT_NO_IPI_BARRIER | |
436 | /* | |
437 | * Local rmb to match the remote wmb to read the commit count before the | |
438 | * buffer data and the write offset. | |
b5b073e2 | 439 | */ |
0222e121 | 440 | cmm_smp_rmb(); |
b73a4c47 PMF |
441 | //ust// #else |
442 | //ust// if (raw_smp_processor_id() != buf->cpu) { | |
443 | //ust// smp_mb(); /* Total order with IPI handler smp_mb() */ | |
444 | //ust// smp_call_function_single(buf->cpu, remote_mb, NULL, 1); | |
445 | //ust// smp_mb(); /* Total order with IPI handler smp_mb() */ | |
446 | //ust// } | |
447 | //ust// #endif | |
448 | ||
b102c2b0 | 449 | write_offset = uatomic_read(&buf->offset); |
b5b073e2 PMF |
450 | /* |
451 | * Check that the subbuffer we are trying to consume has been | |
452 | * already fully committed. | |
453 | */ | |
454 | if (((commit_count - buf->chan->subbuf_size) | |
455 | & channel->commit_count_mask) | |
456 | - (BUFFER_TRUNC(consumed_old, buf->chan) | |
457 | >> channel->n_subbufs_order) | |
458 | != 0) { | |
459 | return -EAGAIN; | |
460 | } | |
461 | /* | |
462 | * Check that we are not about to read the same subbuffer in | |
463 | * which the writer head is. | |
464 | */ | |
465 | if ((SUBBUF_TRUNC(write_offset, buf->chan) | |
466 | - SUBBUF_TRUNC(consumed_old, buf->chan)) | |
467 | == 0) { | |
468 | return -EAGAIN; | |
469 | } | |
470 | ||
b73a4c47 PMF |
471 | /* FIXME: is this ok to disable the reading feature? */ |
472 | //ust// retval = update_read_sb_index(buf, consumed_idx); | |
473 | //ust// if (retval) | |
474 | //ust// return retval; | |
475 | ||
476 | *consumed = consumed_old; | |
477 | ||
b5b073e2 PMF |
478 | return 0; |
479 | } | |
480 | ||
b73a4c47 | 481 | int ust_buffers_put_subbuf(struct ust_buffer *buf, unsigned long uconsumed_old) |
b5b073e2 PMF |
482 | { |
483 | long consumed_new, consumed_old; | |
484 | ||
b102c2b0 | 485 | consumed_old = uatomic_read(&buf->consumed); |
b5b073e2 PMF |
486 | consumed_old = consumed_old & (~0xFFFFFFFFL); |
487 | consumed_old = consumed_old | uconsumed_old; | |
488 | consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan); | |
489 | ||
490 | //ust// spin_lock(<t_buf->full_lock); | |
b102c2b0 | 491 | if (uatomic_cmpxchg(&buf->consumed, consumed_old, |
b5b073e2 PMF |
492 | consumed_new) |
493 | != consumed_old) { | |
494 | /* We have been pushed by the writer : the last | |
495 | * buffer read _is_ corrupted! It can also | |
496 | * happen if this is a buffer we never got. */ | |
497 | //ust// spin_unlock(<t_buf->full_lock); | |
498 | return -EIO; | |
499 | } else { | |
500 | /* tell the client that buffer is now unfull */ | |
501 | int index; | |
502 | long data; | |
503 | index = SUBBUF_INDEX(consumed_old, buf->chan); | |
504 | data = BUFFER_OFFSET(consumed_old, buf->chan); | |
505 | ltt_buf_unfull(buf, index, data); | |
506 | //ust// spin_unlock(<t_buf->full_lock); | |
507 | } | |
508 | return 0; | |
509 | } | |
510 | ||
ef15e552 | 511 | static int map_buf_structs(struct ust_channel *chan) |
b5b073e2 PMF |
512 | { |
513 | void *ptr; | |
514 | int result; | |
204141ee PMF |
515 | size_t size; |
516 | int i; | |
b5b073e2 | 517 | |
204141ee | 518 | size = PAGE_ALIGN(1); |
b5b073e2 | 519 | |
204141ee | 520 | for(i=0; i<chan->n_cpus; i++) { |
b5b073e2 | 521 | |
204141ee PMF |
522 | result = chan->buf_struct_shmids[i] = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700); |
523 | if(result == -1) { | |
524 | PERROR("shmget"); | |
525 | goto destroy_previous; | |
526 | } | |
b5b073e2 | 527 | |
204141ee PMF |
528 | ptr = shmat(chan->buf_struct_shmids[i], NULL, 0); |
529 | if(ptr == (void *) -1) { | |
530 | perror("shmat"); | |
531 | goto destroy_shm; | |
532 | } | |
533 | ||
534 | /* Already mark the shared memory for destruction. This will occur only | |
535 | * when all users have detached. | |
536 | */ | |
537 | result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL); | |
538 | if(result == -1) { | |
539 | perror("shmctl"); | |
540 | goto destroy_previous; | |
541 | } | |
542 | ||
543 | chan->buf[i] = ptr; | |
b5b073e2 PMF |
544 | } |
545 | ||
204141ee | 546 | return 0; |
b5b073e2 | 547 | |
204141ee PMF |
548 | /* Jumping inside this loop occurs from within the other loop above with i as |
549 | * counter, so it unallocates the structures for the cpu = current_i down to | |
550 | * zero. */ | |
551 | for(; i>=0; i--) { | |
552 | destroy_shm: | |
553 | result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL); | |
554 | if(result == -1) { | |
555 | perror("shmctl"); | |
556 | } | |
b5b073e2 | 557 | |
204141ee PMF |
558 | destroy_previous: |
559 | continue; | |
b5b073e2 PMF |
560 | } |
561 | ||
204141ee | 562 | return -1; |
b5b073e2 PMF |
563 | } |
564 | ||
ef15e552 NC |
565 | static int unmap_buf_structs(struct ust_channel *chan) |
566 | { | |
567 | int i; | |
568 | ||
569 | for (i=0; i < chan->n_cpus; i++) { | |
570 | if (shmdt(chan->buf[i]) < 0) { | |
571 | PERROR("shmdt"); | |
572 | } | |
573 | } | |
fbae86d6 | 574 | return 0; |
ef15e552 NC |
575 | } |
576 | ||
b5b073e2 PMF |
577 | /* |
578 | * Create channel. | |
579 | */ | |
ef15e552 NC |
580 | static int create_channel(const char *trace_name, struct ust_trace *trace, |
581 | const char *channel_name, struct ust_channel *chan, | |
b5b073e2 PMF |
582 | unsigned int subbuf_size, unsigned int n_subbufs, int overwrite) |
583 | { | |
ef15e552 | 584 | int i, result; |
b5b073e2 | 585 | |
ef15e552 NC |
586 | chan->trace = trace; |
587 | chan->overwrite = overwrite; | |
588 | chan->n_subbufs_order = get_count_order(n_subbufs); | |
589 | chan->commit_count_mask = (~0UL >> chan->n_subbufs_order); | |
590 | chan->n_cpus = get_n_cpus(); | |
b5b073e2 | 591 | |
ef15e552 NC |
592 | /* These mappings should ideall be per-cpu, if somebody can do that |
593 | * from userspace, that would be cool! | |
594 | */ | |
595 | chan->buf = (void *) zmalloc(chan->n_cpus * sizeof(void *)); | |
596 | if(chan->buf == NULL) { | |
204141ee PMF |
597 | goto error; |
598 | } | |
ef15e552 NC |
599 | chan->buf_struct_shmids = (int *) zmalloc(chan->n_cpus * sizeof(int)); |
600 | if(chan->buf_struct_shmids == NULL) | |
204141ee | 601 | goto free_buf; |
b5b073e2 | 602 | |
ef15e552 | 603 | result = map_buf_structs(chan); |
204141ee PMF |
604 | if(result != 0) { |
605 | goto free_buf_struct_shmids; | |
606 | } | |
b5b073e2 | 607 | |
ef15e552 | 608 | result = open_channel(chan, subbuf_size, n_subbufs); |
204141ee | 609 | if (result != 0) { |
c1f20530 | 610 | ERR("Cannot open channel for trace %s", trace_name); |
ef15e552 | 611 | goto unmap_buf_structs; |
b5b073e2 PMF |
612 | } |
613 | ||
204141ee PMF |
614 | return 0; |
615 | ||
ef15e552 NC |
616 | unmap_buf_structs: |
617 | for (i=0; i < chan->n_cpus; i++) { | |
618 | if (shmdt(chan->buf[i]) < 0) { | |
619 | PERROR("shmdt bufstruct"); | |
620 | } | |
621 | } | |
204141ee PMF |
622 | |
623 | free_buf_struct_shmids: | |
ef15e552 | 624 | free(chan->buf_struct_shmids); |
b5b073e2 | 625 | |
204141ee | 626 | free_buf: |
ef15e552 | 627 | free(chan->buf); |
204141ee PMF |
628 | |
629 | error: | |
630 | return -1; | |
b5b073e2 PMF |
631 | } |
632 | ||
ef15e552 NC |
633 | |
634 | static void remove_channel(struct ust_channel *chan) | |
635 | { | |
636 | close_channel(chan); | |
637 | ||
638 | unmap_buf_structs(chan); | |
639 | ||
640 | free(chan->buf_struct_shmids); | |
641 | ||
642 | free(chan->buf); | |
ef15e552 NC |
643 | } |
644 | ||
b5b073e2 PMF |
645 | static void ltt_relay_async_wakeup_chan(struct ust_channel *ltt_channel) |
646 | { | |
647 | //ust// unsigned int i; | |
648 | //ust// struct rchan *rchan = ltt_channel->trans_channel_data; | |
649 | //ust// | |
650 | //ust// for_each_possible_cpu(i) { | |
651 | //ust// struct ltt_channel_buf_struct *ltt_buf = | |
652 | //ust// percpu_ptr(ltt_channel->buf, i); | |
653 | //ust// | |
b102c2b0 PMF |
654 | //ust// if (uatomic_read(<t_buf->wakeup_readers) == 1) { |
655 | //ust// uatomic_set(<t_buf->wakeup_readers, 0); | |
b5b073e2 PMF |
656 | //ust// wake_up_interruptible(&rchan->buf[i]->read_wait); |
657 | //ust// } | |
658 | //ust// } | |
659 | } | |
660 | ||
204141ee | 661 | static void ltt_relay_finish_buffer(struct ust_channel *channel, unsigned int cpu) |
b5b073e2 PMF |
662 | { |
663 | // int result; | |
664 | ||
204141ee PMF |
665 | if (channel->buf[cpu]) { |
666 | struct ust_buffer *buf = channel->buf[cpu]; | |
97c10252 | 667 | ltt_force_switch(buf, FORCE_FLUSH); |
ef15e552 | 668 | |
b5b073e2 | 669 | /* closing the pipe tells the consumer the buffer is finished */ |
b5b073e2 PMF |
670 | close(buf->data_ready_fd_write); |
671 | } | |
672 | } | |
673 | ||
674 | ||
ef15e552 | 675 | static void finish_channel(struct ust_channel *channel) |
b5b073e2 | 676 | { |
204141ee | 677 | unsigned int i; |
b5b073e2 | 678 | |
204141ee PMF |
679 | for(i=0; i<channel->n_cpus; i++) { |
680 | ltt_relay_finish_buffer(channel, i); | |
681 | } | |
b5b073e2 PMF |
682 | } |
683 | ||
b5b073e2 | 684 | |
b5b073e2 | 685 | /* |
b73a4c47 PMF |
686 | * ltt_reserve_switch_old_subbuf: switch old subbuffer |
687 | * | |
688 | * Concurrency safe because we are the last and only thread to alter this | |
689 | * sub-buffer. As long as it is not delivered and read, no other thread can | |
690 | * alter the offset, alter the reserve_count or call the | |
691 | * client_buffer_end_callback on this sub-buffer. | |
692 | * | |
693 | * The only remaining threads could be the ones with pending commits. They will | |
694 | * have to do the deliver themselves. Not concurrency safe in overwrite mode. | |
695 | * We detect corrupted subbuffers with commit and reserve counts. We keep a | |
696 | * corrupted sub-buffers count and push the readers across these sub-buffers. | |
697 | * | |
698 | * Not concurrency safe if a writer is stalled in a subbuffer and another writer | |
699 | * switches in, finding out it's corrupted. The result will be than the old | |
700 | * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer | |
701 | * will be declared corrupted too because of the commit count adjustment. | |
702 | * | |
703 | * Note : offset_old should never be 0 here. | |
b5b073e2 | 704 | */ |
b73a4c47 PMF |
705 | static void ltt_reserve_switch_old_subbuf( |
706 | struct ust_channel *chan, struct ust_buffer *buf, | |
707 | struct ltt_reserve_switch_offsets *offsets, u64 *tsc) | |
b5b073e2 | 708 | { |
b73a4c47 PMF |
709 | long oldidx = SUBBUF_INDEX(offsets->old - 1, chan); |
710 | long commit_count, padding_size; | |
b5b073e2 | 711 | |
b73a4c47 PMF |
712 | padding_size = chan->subbuf_size |
713 | - (SUBBUF_OFFSET(offsets->old - 1, chan) + 1); | |
714 | ltt_buffer_end(buf, *tsc, offsets->old, oldidx); | |
b5b073e2 | 715 | |
b73a4c47 PMF |
716 | /* |
717 | * Must write slot data before incrementing commit count. | |
ef15e552 | 718 | * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI |
0222e121 | 719 | * sent by get_subbuf() when it does its cmm_smp_rmb(). |
b73a4c47 | 720 | */ |
0222e121 | 721 | cmm_smp_wmb(); |
b102c2b0 PMF |
722 | uatomic_add(&buf->commit_count[oldidx].cc, padding_size); |
723 | commit_count = uatomic_read(&buf->commit_count[oldidx].cc); | |
b73a4c47 | 724 | ltt_check_deliver(chan, buf, offsets->old - 1, commit_count, oldidx); |
1e8c9e7b | 725 | ltt_write_commit_counter(chan, buf, oldidx, |
b73a4c47 PMF |
726 | offsets->old, commit_count, padding_size); |
727 | } | |
b5b073e2 | 728 | |
b73a4c47 PMF |
729 | /* |
730 | * ltt_reserve_switch_new_subbuf: Populate new subbuffer. | |
731 | * | |
732 | * This code can be executed unordered : writers may already have written to the | |
733 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
734 | * that this code is executed before the deliver of this sub-buffer. | |
735 | */ | |
736 | static void ltt_reserve_switch_new_subbuf( | |
737 | struct ust_channel *chan, struct ust_buffer *buf, | |
738 | struct ltt_reserve_switch_offsets *offsets, u64 *tsc) | |
739 | { | |
740 | long beginidx = SUBBUF_INDEX(offsets->begin, chan); | |
741 | long commit_count; | |
b5b073e2 | 742 | |
b73a4c47 | 743 | ltt_buffer_begin(buf, *tsc, beginidx); |
b5b073e2 | 744 | |
b73a4c47 PMF |
745 | /* |
746 | * Must write slot data before incrementing commit count. | |
ef15e552 | 747 | * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI |
0222e121 | 748 | * sent by get_subbuf() when it does its cmm_smp_rmb(). |
b73a4c47 | 749 | */ |
0222e121 | 750 | cmm_smp_wmb(); |
b102c2b0 PMF |
751 | uatomic_add(&buf->commit_count[beginidx].cc, ltt_subbuffer_header_size()); |
752 | commit_count = uatomic_read(&buf->commit_count[beginidx].cc); | |
b73a4c47 PMF |
753 | /* Check if the written buffer has to be delivered */ |
754 | ltt_check_deliver(chan, buf, offsets->begin, commit_count, beginidx); | |
1e8c9e7b | 755 | ltt_write_commit_counter(chan, buf, beginidx, |
b73a4c47 PMF |
756 | offsets->begin, commit_count, ltt_subbuffer_header_size()); |
757 | } | |
b5b073e2 | 758 | |
b73a4c47 PMF |
759 | /* |
760 | * ltt_reserve_end_switch_current: finish switching current subbuffer | |
761 | * | |
762 | * Concurrency safe because we are the last and only thread to alter this | |
763 | * sub-buffer. As long as it is not delivered and read, no other thread can | |
764 | * alter the offset, alter the reserve_count or call the | |
765 | * client_buffer_end_callback on this sub-buffer. | |
766 | * | |
767 | * The only remaining threads could be the ones with pending commits. They will | |
768 | * have to do the deliver themselves. Not concurrency safe in overwrite mode. | |
769 | * We detect corrupted subbuffers with commit and reserve counts. We keep a | |
770 | * corrupted sub-buffers count and push the readers across these sub-buffers. | |
771 | * | |
772 | * Not concurrency safe if a writer is stalled in a subbuffer and another writer | |
773 | * switches in, finding out it's corrupted. The result will be than the old | |
774 | * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer | |
775 | * will be declared corrupted too because of the commit count adjustment. | |
776 | */ | |
777 | static void ltt_reserve_end_switch_current( | |
778 | struct ust_channel *chan, | |
779 | struct ust_buffer *buf, | |
780 | struct ltt_reserve_switch_offsets *offsets, u64 *tsc) | |
781 | { | |
782 | long endidx = SUBBUF_INDEX(offsets->end - 1, chan); | |
783 | long commit_count, padding_size; | |
784 | ||
785 | padding_size = chan->subbuf_size | |
786 | - (SUBBUF_OFFSET(offsets->end - 1, chan) + 1); | |
787 | ||
788 | ltt_buffer_end(buf, *tsc, offsets->end, endidx); | |
789 | ||
790 | /* | |
791 | * Must write slot data before incrementing commit count. | |
ef15e552 | 792 | * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI |
0222e121 | 793 | * sent by get_subbuf() when it does its cmm_smp_rmb(). |
b73a4c47 | 794 | */ |
0222e121 | 795 | cmm_smp_wmb(); |
b102c2b0 PMF |
796 | uatomic_add(&buf->commit_count[endidx].cc, padding_size); |
797 | commit_count = uatomic_read(&buf->commit_count[endidx].cc); | |
b73a4c47 PMF |
798 | ltt_check_deliver(chan, buf, |
799 | offsets->end - 1, commit_count, endidx); | |
1e8c9e7b | 800 | ltt_write_commit_counter(chan, buf, endidx, |
b73a4c47 | 801 | offsets->end, commit_count, padding_size); |
b5b073e2 PMF |
802 | } |
803 | ||
804 | /* | |
805 | * Returns : | |
806 | * 0 if ok | |
807 | * !0 if execution must be aborted. | |
808 | */ | |
b73a4c47 | 809 | static int ltt_relay_try_switch_slow( |
b5b073e2 | 810 | enum force_switch_mode mode, |
b73a4c47 | 811 | struct ust_channel *chan, |
b5b073e2 PMF |
812 | struct ust_buffer *buf, |
813 | struct ltt_reserve_switch_offsets *offsets, | |
814 | u64 *tsc) | |
815 | { | |
816 | long subbuf_index; | |
b73a4c47 | 817 | long reserve_commit_diff; |
b5b073e2 | 818 | |
b102c2b0 | 819 | offsets->begin = uatomic_read(&buf->offset); |
b5b073e2 PMF |
820 | offsets->old = offsets->begin; |
821 | offsets->begin_switch = 0; | |
822 | offsets->end_switch_old = 0; | |
823 | ||
824 | *tsc = trace_clock_read64(); | |
825 | ||
826 | if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) { | |
827 | offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan); | |
828 | offsets->end_switch_old = 1; | |
829 | } else { | |
830 | /* we do not have to switch : buffer is empty */ | |
831 | return -1; | |
832 | } | |
833 | if (mode == FORCE_ACTIVE) | |
834 | offsets->begin += ltt_subbuffer_header_size(); | |
835 | /* | |
836 | * Always begin_switch in FORCE_ACTIVE mode. | |
837 | * Test new buffer integrity | |
838 | */ | |
839 | subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan); | |
b73a4c47 | 840 | reserve_commit_diff = |
b5b073e2 | 841 | (BUFFER_TRUNC(offsets->begin, buf->chan) |
b73a4c47 | 842 | >> chan->n_subbufs_order) |
b102c2b0 | 843 | - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb) |
b73a4c47 PMF |
844 | & chan->commit_count_mask); |
845 | if (reserve_commit_diff == 0) { | |
b5b073e2 PMF |
846 | /* Next buffer not corrupted. */ |
847 | if (mode == FORCE_ACTIVE | |
b73a4c47 | 848 | && !chan->overwrite |
b102c2b0 | 849 | && offsets->begin - uatomic_read(&buf->consumed) |
b73a4c47 | 850 | >= chan->alloc_size) { |
b5b073e2 PMF |
851 | /* |
852 | * We do not overwrite non consumed buffers and we are | |
853 | * full : ignore switch while tracing is active. | |
854 | */ | |
855 | return -1; | |
856 | } | |
857 | } else { | |
858 | /* | |
859 | * Next subbuffer corrupted. Force pushing reader even in normal | |
860 | * mode | |
861 | */ | |
862 | } | |
863 | offsets->end = offsets->begin; | |
864 | return 0; | |
865 | } | |
866 | ||
b5b073e2 | 867 | /* |
b73a4c47 PMF |
868 | * Force a sub-buffer switch for a per-cpu buffer. This operation is |
869 | * completely reentrant : can be called while tracing is active with | |
870 | * absolutely no lock held. | |
b5b073e2 | 871 | */ |
b73a4c47 PMF |
872 | void ltt_force_switch_lockless_slow(struct ust_buffer *buf, |
873 | enum force_switch_mode mode) | |
b5b073e2 | 874 | { |
b73a4c47 | 875 | struct ust_channel *chan = buf->chan; |
b5b073e2 | 876 | struct ltt_reserve_switch_offsets offsets; |
b73a4c47 | 877 | u64 tsc; |
b5b073e2 | 878 | |
b5b073e2 PMF |
879 | offsets.size = 0; |
880 | ||
10dd3941 | 881 | DBG("Switching (forced) %s_%d", chan->channel_name, buf->cpu); |
b5b073e2 PMF |
882 | /* |
883 | * Perform retryable operations. | |
884 | */ | |
b5b073e2 | 885 | do { |
b73a4c47 PMF |
886 | if (ltt_relay_try_switch_slow(mode, chan, buf, |
887 | &offsets, &tsc)) | |
888 | return; | |
b102c2b0 | 889 | } while (uatomic_cmpxchg(&buf->offset, offsets.old, |
b5b073e2 PMF |
890 | offsets.end) != offsets.old); |
891 | ||
892 | /* | |
893 | * Atomically update last_tsc. This update races against concurrent | |
894 | * atomic updates, but the race will always cause supplementary full TSC | |
895 | * events, never the opposite (missing a full TSC event when it would be | |
896 | * needed). | |
897 | */ | |
b73a4c47 | 898 | save_last_tsc(buf, tsc); |
b5b073e2 PMF |
899 | |
900 | /* | |
901 | * Push the reader if necessary | |
902 | */ | |
b73a4c47 PMF |
903 | if (mode == FORCE_ACTIVE) { |
904 | ltt_reserve_push_reader(chan, buf, offsets.end - 1); | |
905 | //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan)); | |
906 | } | |
b5b073e2 PMF |
907 | |
908 | /* | |
909 | * Switch old subbuffer if needed. | |
910 | */ | |
b73a4c47 PMF |
911 | if (offsets.end_switch_old) { |
912 | //ust// ltt_clear_noref_flag(rchan, buf, SUBBUF_INDEX(offsets.old - 1, rchan)); | |
913 | ltt_reserve_switch_old_subbuf(chan, buf, &offsets, &tsc); | |
914 | } | |
b5b073e2 PMF |
915 | |
916 | /* | |
917 | * Populate new subbuffer. | |
918 | */ | |
b73a4c47 PMF |
919 | if (mode == FORCE_ACTIVE) |
920 | ltt_reserve_switch_new_subbuf(chan, buf, &offsets, &tsc); | |
921 | } | |
b5b073e2 | 922 | |
b73a4c47 PMF |
923 | /* |
924 | * Returns : | |
925 | * 0 if ok | |
926 | * !0 if execution must be aborted. | |
927 | */ | |
928 | static int ltt_relay_try_reserve_slow(struct ust_channel *chan, struct ust_buffer *buf, | |
929 | struct ltt_reserve_switch_offsets *offsets, size_t data_size, | |
930 | u64 *tsc, unsigned int *rflags, int largest_align) | |
931 | { | |
932 | long reserve_commit_diff; | |
b5b073e2 | 933 | |
b102c2b0 | 934 | offsets->begin = uatomic_read(&buf->offset); |
b73a4c47 PMF |
935 | offsets->old = offsets->begin; |
936 | offsets->begin_switch = 0; | |
937 | offsets->end_switch_current = 0; | |
938 | offsets->end_switch_old = 0; | |
939 | ||
940 | *tsc = trace_clock_read64(); | |
941 | if (last_tsc_overflow(buf, *tsc)) | |
942 | *rflags = LTT_RFLAG_ID_SIZE_TSC; | |
943 | ||
944 | if (unlikely(SUBBUF_OFFSET(offsets->begin, buf->chan) == 0)) { | |
945 | offsets->begin_switch = 1; /* For offsets->begin */ | |
946 | } else { | |
947 | offsets->size = ust_get_header_size(chan, | |
948 | offsets->begin, data_size, | |
949 | &offsets->before_hdr_pad, *rflags); | |
950 | offsets->size += ltt_align(offsets->begin + offsets->size, | |
951 | largest_align) | |
952 | + data_size; | |
953 | if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) + | |
954 | offsets->size) > buf->chan->subbuf_size)) { | |
955 | offsets->end_switch_old = 1; /* For offsets->old */ | |
956 | offsets->begin_switch = 1; /* For offsets->begin */ | |
957 | } | |
958 | } | |
959 | if (unlikely(offsets->begin_switch)) { | |
960 | long subbuf_index; | |
961 | ||
962 | /* | |
963 | * We are typically not filling the previous buffer completely. | |
964 | */ | |
965 | if (likely(offsets->end_switch_old)) | |
966 | offsets->begin = SUBBUF_ALIGN(offsets->begin, | |
967 | buf->chan); | |
968 | offsets->begin = offsets->begin + ltt_subbuffer_header_size(); | |
969 | /* Test new buffer integrity */ | |
970 | subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan); | |
971 | reserve_commit_diff = | |
972 | (BUFFER_TRUNC(offsets->begin, buf->chan) | |
973 | >> chan->n_subbufs_order) | |
b102c2b0 | 974 | - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb) |
b73a4c47 PMF |
975 | & chan->commit_count_mask); |
976 | if (likely(reserve_commit_diff == 0)) { | |
977 | /* Next buffer not corrupted. */ | |
978 | if (unlikely(!chan->overwrite && | |
979 | (SUBBUF_TRUNC(offsets->begin, buf->chan) | |
b102c2b0 | 980 | - SUBBUF_TRUNC(uatomic_read( |
b73a4c47 PMF |
981 | &buf->consumed), |
982 | buf->chan)) | |
983 | >= chan->alloc_size)) { | |
984 | /* | |
985 | * We do not overwrite non consumed buffers | |
986 | * and we are full : event is lost. | |
987 | */ | |
b102c2b0 | 988 | uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
989 | return -1; |
990 | } else { | |
991 | /* | |
992 | * next buffer not corrupted, we are either in | |
993 | * overwrite mode or the buffer is not full. | |
994 | * It's safe to write in this new subbuffer. | |
995 | */ | |
996 | } | |
997 | } else { | |
998 | /* | |
999 | * Next subbuffer corrupted. Drop event in normal and | |
1000 | * overwrite mode. Caused by either a writer OOPS or | |
1001 | * too many nested writes over a reserve/commit pair. | |
1002 | */ | |
b102c2b0 | 1003 | uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1004 | return -1; |
1005 | } | |
1006 | offsets->size = ust_get_header_size(chan, | |
1007 | offsets->begin, data_size, | |
1008 | &offsets->before_hdr_pad, *rflags); | |
1009 | offsets->size += ltt_align(offsets->begin + offsets->size, | |
1010 | largest_align) | |
1011 | + data_size; | |
1012 | if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) | |
1013 | + offsets->size) > buf->chan->subbuf_size)) { | |
1014 | /* | |
1015 | * Event too big for subbuffers, report error, don't | |
1016 | * complete the sub-buffer switch. | |
1017 | */ | |
b102c2b0 | 1018 | uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1019 | return -1; |
1020 | } else { | |
1021 | /* | |
1022 | * We just made a successful buffer switch and the event | |
1023 | * fits in the new subbuffer. Let's write. | |
1024 | */ | |
1025 | } | |
1026 | } else { | |
1027 | /* | |
1028 | * Event fits in the current buffer and we are not on a switch | |
1029 | * boundary. It's safe to write. | |
1030 | */ | |
1031 | } | |
1032 | offsets->end = offsets->begin + offsets->size; | |
1033 | ||
1034 | if (unlikely((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0)) { | |
1035 | /* | |
1036 | * The offset_end will fall at the very beginning of the next | |
1037 | * subbuffer. | |
1038 | */ | |
1039 | offsets->end_switch_current = 1; /* For offsets->begin */ | |
1040 | } | |
b5b073e2 PMF |
1041 | return 0; |
1042 | } | |
1043 | ||
b73a4c47 PMF |
1044 | /** |
1045 | * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer. | |
1046 | * @trace: the trace structure to log to. | |
1047 | * @ltt_channel: channel structure | |
1048 | * @transport_data: data structure specific to ltt relay | |
1049 | * @data_size: size of the variable length data to log. | |
1050 | * @slot_size: pointer to total size of the slot (out) | |
1051 | * @buf_offset : pointer to reserved buffer offset (out) | |
1052 | * @tsc: pointer to the tsc at the slot reservation (out) | |
1053 | * @cpu: cpuid | |
b5b073e2 | 1054 | * |
b73a4c47 PMF |
1055 | * Return : -ENOSPC if not enough space, else returns 0. |
1056 | * It will take care of sub-buffer switching. | |
b5b073e2 | 1057 | */ |
12e81b07 PMF |
1058 | int ltt_reserve_slot_lockless_slow(struct ust_channel *chan, |
1059 | struct ust_trace *trace, size_t data_size, | |
1060 | int largest_align, int cpu, | |
1061 | struct ust_buffer **ret_buf, | |
1062 | size_t *slot_size, long *buf_offset, | |
1063 | u64 *tsc, unsigned int *rflags) | |
b5b073e2 | 1064 | { |
12e81b07 | 1065 | struct ust_buffer *buf = *ret_buf = chan->buf[cpu]; |
b5b073e2 | 1066 | struct ltt_reserve_switch_offsets offsets; |
b5b073e2 | 1067 | |
b5b073e2 PMF |
1068 | offsets.size = 0; |
1069 | ||
b5b073e2 | 1070 | do { |
b73a4c47 PMF |
1071 | if (unlikely(ltt_relay_try_reserve_slow(chan, buf, &offsets, |
1072 | data_size, tsc, rflags, largest_align))) | |
1073 | return -ENOSPC; | |
b102c2b0 | 1074 | } while (unlikely(uatomic_cmpxchg(&buf->offset, offsets.old, |
b73a4c47 | 1075 | offsets.end) != offsets.old)); |
b5b073e2 PMF |
1076 | |
1077 | /* | |
1078 | * Atomically update last_tsc. This update races against concurrent | |
1079 | * atomic updates, but the race will always cause supplementary full TSC | |
1080 | * events, never the opposite (missing a full TSC event when it would be | |
1081 | * needed). | |
1082 | */ | |
b73a4c47 | 1083 | save_last_tsc(buf, *tsc); |
b5b073e2 PMF |
1084 | |
1085 | /* | |
1086 | * Push the reader if necessary | |
1087 | */ | |
b73a4c47 PMF |
1088 | ltt_reserve_push_reader(chan, buf, offsets.end - 1); |
1089 | ||
1090 | /* | |
1091 | * Clear noref flag for this subbuffer. | |
1092 | */ | |
1093 | //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan)); | |
b5b073e2 PMF |
1094 | |
1095 | /* | |
1096 | * Switch old subbuffer if needed. | |
1097 | */ | |
b73a4c47 PMF |
1098 | if (unlikely(offsets.end_switch_old)) { |
1099 | //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.old - 1, chan)); | |
1100 | ltt_reserve_switch_old_subbuf(chan, buf, &offsets, tsc); | |
10dd3941 | 1101 | DBG("Switching %s_%d", chan->channel_name, cpu); |
b73a4c47 | 1102 | } |
b5b073e2 PMF |
1103 | |
1104 | /* | |
1105 | * Populate new subbuffer. | |
1106 | */ | |
b73a4c47 PMF |
1107 | if (unlikely(offsets.begin_switch)) |
1108 | ltt_reserve_switch_new_subbuf(chan, buf, &offsets, tsc); | |
1109 | ||
1110 | if (unlikely(offsets.end_switch_current)) | |
1111 | ltt_reserve_end_switch_current(chan, buf, &offsets, tsc); | |
1112 | ||
1113 | *slot_size = offsets.size; | |
1114 | *buf_offset = offsets.begin + offsets.before_hdr_pad; | |
1115 | return 0; | |
b5b073e2 PMF |
1116 | } |
1117 | ||
b5b073e2 PMF |
1118 | static struct ltt_transport ust_relay_transport = { |
1119 | .name = "ustrelay", | |
1120 | .ops = { | |
ef15e552 NC |
1121 | .create_channel = create_channel, |
1122 | .finish_channel = finish_channel, | |
1123 | .remove_channel = remove_channel, | |
b5b073e2 | 1124 | .wakeup_channel = ltt_relay_async_wakeup_chan, |
b5b073e2 PMF |
1125 | }, |
1126 | }; | |
1127 | ||
b5b073e2 PMF |
1128 | static char initialized = 0; |
1129 | ||
1130 | void __attribute__((constructor)) init_ustrelay_transport(void) | |
1131 | { | |
1132 | if(!initialized) { | |
1133 | ltt_transport_register(&ust_relay_transport); | |
1134 | initialized = 1; | |
1135 | } | |
1136 | } | |
1137 | ||
b73a4c47 | 1138 | static void __attribute__((destructor)) ust_buffers_exit(void) |
b5b073e2 PMF |
1139 | { |
1140 | ltt_transport_unregister(&ust_relay_transport); | |
1141 | } | |
b73a4c47 | 1142 | |
12e81b07 | 1143 | size_t ltt_write_event_header_slow(struct ust_channel *channel, |
b73a4c47 PMF |
1144 | struct ust_buffer *buf, long buf_offset, |
1145 | u16 eID, u32 event_size, | |
1146 | u64 tsc, unsigned int rflags) | |
1147 | { | |
1148 | struct ltt_event_header header; | |
1149 | u16 small_size; | |
1150 | ||
1151 | switch (rflags) { | |
1152 | case LTT_RFLAG_ID_SIZE_TSC: | |
1153 | header.id_time = 29 << LTT_TSC_BITS; | |
1154 | break; | |
1155 | case LTT_RFLAG_ID_SIZE: | |
1156 | header.id_time = 30 << LTT_TSC_BITS; | |
1157 | break; | |
1158 | case LTT_RFLAG_ID: | |
1159 | header.id_time = 31 << LTT_TSC_BITS; | |
1160 | break; | |
e2b46575 DG |
1161 | default: |
1162 | WARN_ON_ONCE(1); | |
1163 | header.id_time = 0; | |
1164 | break; | |
b73a4c47 PMF |
1165 | } |
1166 | ||
1167 | header.id_time |= (u32)tsc & LTT_TSC_MASK; | |
1168 | ust_buffers_write(buf, buf_offset, &header, sizeof(header)); | |
1169 | buf_offset += sizeof(header); | |
1170 | ||
1171 | switch (rflags) { | |
1172 | case LTT_RFLAG_ID_SIZE_TSC: | |
1173 | small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE); | |
1174 | ust_buffers_write(buf, buf_offset, | |
1175 | &eID, sizeof(u16)); | |
1176 | buf_offset += sizeof(u16); | |
1177 | ust_buffers_write(buf, buf_offset, | |
1178 | &small_size, sizeof(u16)); | |
1179 | buf_offset += sizeof(u16); | |
1180 | if (small_size == LTT_MAX_SMALL_SIZE) { | |
1181 | ust_buffers_write(buf, buf_offset, | |
1182 | &event_size, sizeof(u32)); | |
1183 | buf_offset += sizeof(u32); | |
1184 | } | |
1185 | buf_offset += ltt_align(buf_offset, sizeof(u64)); | |
1186 | ust_buffers_write(buf, buf_offset, | |
1187 | &tsc, sizeof(u64)); | |
1188 | buf_offset += sizeof(u64); | |
1189 | break; | |
1190 | case LTT_RFLAG_ID_SIZE: | |
1191 | small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE); | |
1192 | ust_buffers_write(buf, buf_offset, | |
1193 | &eID, sizeof(u16)); | |
1194 | buf_offset += sizeof(u16); | |
1195 | ust_buffers_write(buf, buf_offset, | |
1196 | &small_size, sizeof(u16)); | |
1197 | buf_offset += sizeof(u16); | |
1198 | if (small_size == LTT_MAX_SMALL_SIZE) { | |
1199 | ust_buffers_write(buf, buf_offset, | |
1200 | &event_size, sizeof(u32)); | |
1201 | buf_offset += sizeof(u32); | |
1202 | } | |
1203 | break; | |
1204 | case LTT_RFLAG_ID: | |
1205 | ust_buffers_write(buf, buf_offset, | |
1206 | &eID, sizeof(u16)); | |
1207 | buf_offset += sizeof(u16); | |
1208 | break; | |
1209 | } | |
1210 | ||
1211 | return buf_offset; | |
1212 | } |