| 1 | /* |
| 2 | * ltt-ring-buffer-client.h |
| 3 | * |
| 4 | * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * LTTng lib ring buffer client template. |
| 7 | * |
| 8 | * Dual LGPL v2.1/GPL v2 license. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/trace-clock.h> |
| 14 | #include "ltt-events.h" |
| 15 | #include "ltt-tracer.h" |
| 16 | |
| 17 | static const struct lib_ring_buffer_config client_config; |
| 18 | |
| 19 | static u64 client_ring_buffer_clock_read(struct channel *chan) |
| 20 | { |
| 21 | return lib_ring_buffer_clock_read(chan); |
| 22 | } |
| 23 | |
| 24 | static |
| 25 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, |
| 26 | struct channel *chan, size_t offset, |
| 27 | size_t data_size, |
| 28 | size_t *pre_header_padding, |
| 29 | unsigned int rflags, |
| 30 | struct lib_ring_buffer_ctx *ctx) |
| 31 | { |
| 32 | return record_header_size(config, chan, offset, data_size, |
| 33 | pre_header_padding, rflags, ctx); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
| 38 | * |
| 39 | * Return header size without padding after the structure. Don't use packed |
| 40 | * structure because gcc generates inefficient code on some architectures |
| 41 | * (powerpc, mips..) |
| 42 | */ |
| 43 | static size_t client_packet_header_size(void) |
| 44 | { |
| 45 | return offsetof(struct packet_header, header_end); |
| 46 | } |
| 47 | |
| 48 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, |
| 49 | unsigned int subbuf_idx) |
| 50 | { |
| 51 | struct channel *chan = buf->backend.chan; |
| 52 | struct packet_header *header = |
| 53 | (struct packet_header *) |
| 54 | lib_ring_buffer_offset_address(&buf->backend, |
| 55 | subbuf_idx * chan->backend.subbuf_size); |
| 56 | |
| 57 | header->timestamp_begin = tsc; |
| 58 | header->content_size = 0xFFFFFFFF; /* for debugging */ |
| 59 | write_trace_header(&client_config, header); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * offset is assumed to never be 0 here : never deliver a completely empty |
| 64 | * subbuffer. data_size is between 1 and subbuf_size. |
| 65 | */ |
| 66 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, |
| 67 | unsigned int subbuf_idx, unsigned long data_size) |
| 68 | { |
| 69 | struct channel *chan = buf->backend.chan; |
| 70 | struct packet_header *header = |
| 71 | (struct packet_header *) |
| 72 | lib_ring_buffer_offset_address(&buf->backend, |
| 73 | subbuf_idx * chan->backend.subbuf_size); |
| 74 | unsigned long records_lost = 0; |
| 75 | |
| 76 | header->content_size = data_size; |
| 77 | header->packet_size = PAGE_ALIGN(data_size); |
| 78 | header->timestamp_end = tsc; |
| 79 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
| 80 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
| 81 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); |
| 82 | header->events_lost = records_lost; |
| 83 | } |
| 84 | |
| 85 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, |
| 86 | int cpu, const char *name) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | static const struct lib_ring_buffer_config client_config = { |
| 96 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
| 97 | .cb.record_header_size = client_record_header_size, |
| 98 | .cb.subbuffer_header_size = client_packet_header_size, |
| 99 | .cb.buffer_begin = client_buffer_begin, |
| 100 | .cb.buffer_end = client_buffer_end, |
| 101 | .cb.buffer_create = client_buffer_create, |
| 102 | .cb.buffer_finalize = client_buffer_finalize, |
| 103 | |
| 104 | .tsc_bits = 32, |
| 105 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
| 106 | .sync = RING_BUFFER_SYNC_PER_CPU, |
| 107 | .mode = RING_BUFFER_MODE_TEMPLATE, |
| 108 | .backend = RING_BUFFER_PAGE, |
| 109 | .output = RING_BUFFER_SPLICE, |
| 110 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
| 111 | .ipi = RING_BUFFER_IPI_BARRIER, |
| 112 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, |
| 113 | }; |
| 114 | |
| 115 | static |
| 116 | struct channel *_channel_create(const char *name, |
| 117 | struct ltt_session *session, void *buf_addr, |
| 118 | size_t subbuf_size, size_t num_subbuf, |
| 119 | unsigned int switch_timer_interval, |
| 120 | unsigned int read_timer_interval) |
| 121 | { |
| 122 | return channel_create(&client_config, name, session, buf_addr, |
| 123 | subbuf_size, num_subbuf, switch_timer_interval, |
| 124 | read_timer_interval); |
| 125 | } |
| 126 | |
| 127 | static |
| 128 | void ltt_channel_destroy(struct channel *chan) |
| 129 | { |
| 130 | channel_destroy(chan); |
| 131 | } |
| 132 | |
| 133 | static |
| 134 | struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan) |
| 135 | { |
| 136 | struct lib_ring_buffer *buf; |
| 137 | int cpu; |
| 138 | |
| 139 | for_each_channel_cpu(cpu, chan) { |
| 140 | buf = channel_get_ring_buffer(&client_config, chan, cpu); |
| 141 | if (!lib_ring_buffer_open_read(buf)) |
| 142 | return buf; |
| 143 | } |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | static |
| 148 | void ltt_buffer_read_close(struct lib_ring_buffer *buf) |
| 149 | { |
| 150 | lib_ring_buffer_release_read(buf); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx) |
| 155 | { |
| 156 | int ret, cpu; |
| 157 | |
| 158 | cpu = lib_ring_buffer_get_cpu(&client_config); |
| 159 | if (cpu < 0) |
| 160 | return -EPERM; |
| 161 | ctx->cpu = cpu; |
| 162 | |
| 163 | ret = lib_ring_buffer_reserve(&client_config, ctx); |
| 164 | if (ret) |
| 165 | goto put; |
| 166 | return ret; |
| 167 | |
| 168 | put: |
| 169 | lib_ring_buffer_put_cpu(&client_config); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | void ltt_event_commit(struct lib_ring_buffer_ctx *ctx) |
| 174 | { |
| 175 | lib_ring_buffer_commit(&client_config, ctx); |
| 176 | lib_ring_buffer_put_cpu(&client_config); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static struct ltt_transport ltt_relay_transport = { |
| 181 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
| 182 | .owner = THIS_MODULE, |
| 183 | .ops = { |
| 184 | .channel_create = _channel_create, |
| 185 | .channel_destroy = ltt_channel_destroy, |
| 186 | .buffer_read_open = ltt_buffer_read_open, |
| 187 | .buffer_read_close = ltt_buffer_read_close, |
| 188 | .event_reserve = ltt_event_reserve, |
| 189 | .event_commit = ltt_event_commit, |
| 190 | }, |
| 191 | }; |
| 192 | |
| 193 | static int __init ltt_ring_buffer_client_init(void) |
| 194 | { |
| 195 | printk(KERN_INFO "LTT : ltt ring buffer client init\n"); |
| 196 | ltt_transport_register(<t_relay_transport); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | module_init(ltt_ring_buffer_client_init); |
| 201 | |
| 202 | static void __exit ltt_ring_buffer_client_exit(void) |
| 203 | { |
| 204 | printk(KERN_INFO "LTT : ltt ring buffer client exit\n"); |
| 205 | ltt_transport_unregister(<t_relay_transport); |
| 206 | } |
| 207 | |
| 208 | module_exit(ltt_ring_buffer_client_exit); |
| 209 | |
| 210 | MODULE_LICENSE("GPL and additional rights"); |
| 211 | MODULE_AUTHOR("Mathieu Desnoyers"); |
| 212 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
| 213 | " client"); |