X-Git-Url: http://git.lttng.org/?p=lttng-modules.git;a=blobdiff_plain;f=lttng-events.c;h=27a8f8644477be0bcf41c3732622e0e8c0f9fa57;hp=0ef87a8c992a9224b613b218ac1e324b95440a43;hb=b3c40230ad700b12c39640d8593aece428a2702b;hpb=a90917c3f8c4ed79117f1caa333b29a2108084ec diff --git a/lttng-events.c b/lttng-events.c index 0ef87a8c..27a8f864 100644 --- a/lttng-events.c +++ b/lttng-events.c @@ -1,11 +1,23 @@ /* * lttng-events.c * - * Copyright 2010-2011 (c) - Mathieu Desnoyers - * * Holds LTTng per-session event registry. * - * Dual LGPL v2.1/GPL v2 license. + * Copyright (C) 2010-2012 Mathieu Desnoyers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; only + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include @@ -14,13 +26,22 @@ #include #include #include +#include #include "wrapper/uuid.h" #include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */ +#include "wrapper/random.h" +#include "wrapper/tracepoint.h" #include "lttng-events.h" #include "lttng-tracer.h" +#include "lttng-abi-old.h" + +#define METADATA_CACHE_DEFAULT_SIZE 4096 static LIST_HEAD(sessions); static LIST_HEAD(lttng_transport_list); +/* + * Protect the sessions and metadata caches. + */ static DEFINE_MUTEX(sessions_mutex); static struct kmem_cache *event_cache; @@ -33,6 +54,8 @@ int _lttng_event_metadata_statedump(struct lttng_session *session, struct lttng_event *event); static int _lttng_session_metadata_statedump(struct lttng_session *session); +static +void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream); void synchronize_trace(void) { @@ -45,23 +68,54 @@ void synchronize_trace(void) struct lttng_session *lttng_session_create(void) { struct lttng_session *session; + struct lttng_metadata_cache *metadata_cache; mutex_lock(&sessions_mutex); session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL); if (!session) - return NULL; + goto err; INIT_LIST_HEAD(&session->chan); INIT_LIST_HEAD(&session->events); uuid_le_gen(&session->uuid); + + metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache), + GFP_KERNEL); + if (!metadata_cache) + goto err_free_session; + metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE, + GFP_KERNEL); + if (!metadata_cache->data) + goto err_free_cache; + metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE; + kref_init(&metadata_cache->refcount); + session->metadata_cache = metadata_cache; + INIT_LIST_HEAD(&metadata_cache->metadata_stream); list_add(&session->list, &sessions); mutex_unlock(&sessions_mutex); return session; + +err_free_cache: + kfree(metadata_cache); +err_free_session: + kfree(session); +err: + mutex_unlock(&sessions_mutex); + return NULL; +} + +void metadata_cache_destroy(struct kref *kref) +{ + struct lttng_metadata_cache *cache = + container_of(kref, struct lttng_metadata_cache, refcount); + kfree(cache->data); + kfree(cache); } void lttng_session_destroy(struct lttng_session *session) { struct lttng_channel *chan, *tmpchan; struct lttng_event *event, *tmpevent; + struct lttng_metadata_stream *metadata_stream; int ret; mutex_lock(&sessions_mutex); @@ -77,8 +131,13 @@ void lttng_session_destroy(struct lttng_session *session) synchronize_trace(); /* Wait for in-flight events to complete */ list_for_each_entry_safe(event, tmpevent, &session->events, list) _lttng_event_destroy(event); - list_for_each_entry_safe(chan, tmpchan, &session->chan, list) + list_for_each_entry_safe(chan, tmpchan, &session->chan, list) { + BUG_ON(chan->channel_type == METADATA_CHANNEL); _lttng_channel_destroy(chan); + } + list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list) + _lttng_metadata_channel_hangup(metadata_stream); + kref_put(&session->metadata_cache->refcount, metadata_cache_destroy); list_del(&session->list); mutex_unlock(&sessions_mutex); kfree(session); @@ -111,6 +170,11 @@ int lttng_session_enable(struct lttng_session *session) ACCESS_ONCE(session->active) = 1; ACCESS_ONCE(session->been_active) = 1; ret = _lttng_session_metadata_statedump(session); + if (ret) { + ACCESS_ONCE(session->active) = 0; + goto end; + } + ret = lttng_statedump_start(session); if (ret) ACCESS_ONCE(session->active) = 0; end: @@ -137,7 +201,7 @@ int lttng_channel_enable(struct lttng_channel *channel) { int old; - if (channel == channel->session->metadata) + if (channel->channel_type == METADATA_CHANNEL) return -EPERM; old = xchg(&channel->enabled, 1); if (old) @@ -149,7 +213,7 @@ int lttng_channel_disable(struct lttng_channel *channel) { int old; - if (channel == channel->session->metadata) + if (channel->channel_type == METADATA_CHANNEL) return -EPERM; old = xchg(&channel->enabled, 0); if (!old) @@ -161,7 +225,7 @@ int lttng_event_enable(struct lttng_event *event) { int old; - if (event->chan == event->chan->session->metadata) + if (event->chan->channel_type == METADATA_CHANNEL) return -EPERM; old = xchg(&event->enabled, 1); if (old) @@ -173,7 +237,7 @@ int lttng_event_disable(struct lttng_event *event) { int old; - if (event->chan == event->chan->session->metadata) + if (event->chan->channel_type == METADATA_CHANNEL) return -EPERM; old = xchg(&event->enabled, 0); if (!old) @@ -197,13 +261,14 @@ struct lttng_channel *lttng_channel_create(struct lttng_session *session, void *buf_addr, size_t subbuf_size, size_t num_subbuf, unsigned int switch_timer_interval, - unsigned int read_timer_interval) + unsigned int read_timer_interval, + enum channel_type channel_type) { struct lttng_channel *chan; struct lttng_transport *transport = NULL; mutex_lock(&sessions_mutex); - if (session->been_active) + if (session->been_active && channel_type != METADATA_CHANNEL) goto active; /* Refuse to add channel to active session */ transport = lttng_transport_find(transport_name); if (!transport) { @@ -225,14 +290,15 @@ struct lttng_channel *lttng_channel_create(struct lttng_session *session, * headers. Therefore the "chan" information used as input * should be already accessible. */ - chan->chan = transport->ops.channel_create("[lttng]", chan, buf_addr, - subbuf_size, num_subbuf, switch_timer_interval, - read_timer_interval); + chan->chan = transport->ops.channel_create(transport_name, + chan, buf_addr, subbuf_size, num_subbuf, + switch_timer_interval, read_timer_interval); if (!chan->chan) goto create_error; chan->enabled = 1; chan->ops = &transport->ops; chan->transport = transport; + chan->channel_type = channel_type; list_add(&chan->list, &session->chan); mutex_unlock(&sessions_mutex); return chan; @@ -249,7 +315,9 @@ active: } /* - * Only used internally at session destruction. + * Only used internally at session destruction for per-cpu channels, and + * when metadata channel is released. + * Needs to be called with sessions mutex held. */ static void _lttng_channel_destroy(struct lttng_channel *chan) @@ -261,6 +329,24 @@ void _lttng_channel_destroy(struct lttng_channel *chan) kfree(chan); } +void lttng_metadata_channel_destroy(struct lttng_channel *chan) +{ + BUG_ON(chan->channel_type != METADATA_CHANNEL); + + /* Protect the metadata cache with the sessions_mutex. */ + mutex_lock(&sessions_mutex); + _lttng_channel_destroy(chan); + mutex_unlock(&sessions_mutex); +} +EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy); + +static +void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream) +{ + stream->finalized = 1; + wake_up_interruptible(&stream->read_wait); +} + /* * Supports event creation while tracing session is active. */ @@ -273,7 +359,7 @@ struct lttng_event *lttng_event_create(struct lttng_channel *chan, int ret; mutex_lock(&sessions_mutex); - if (chan->free_event_id == -1UL) + if (chan->free_event_id == -1U) goto full; /* * This is O(n^2) (for each event, the loop is called at event @@ -297,7 +383,7 @@ struct lttng_event *lttng_event_create(struct lttng_channel *chan, event->desc = lttng_event_get(event_param->name); if (!event->desc) goto register_error; - ret = tracepoint_probe_register(event_param->name, + ret = kabi_2635_tracepoint_probe_register(event_param->name, event->desc->probe_callback, event); if (ret) @@ -401,7 +487,7 @@ int _lttng_event_unregister(struct lttng_event *event) switch (event->instrumentation) { case LTTNG_KERNEL_TRACEPOINT: - ret = tracepoint_probe_unregister(event->desc->name, + ret = kabi_2635_tracepoint_probe_unregister(event->desc->name, event->desc->probe_callback, event); if (ret) @@ -461,20 +547,59 @@ void _lttng_event_destroy(struct lttng_event *event) } /* + * Serialize at most one packet worth of metadata into a metadata + * channel. * We have exclusive access to our metadata buffer (protected by the * sessions_mutex), so we can do racy operations such as looking for * remaining space left in packet and write, since mutual exclusion * protects us from concurrent writes. */ +int lttng_metadata_output_channel(struct lttng_channel *chan, + struct lttng_metadata_stream *stream) +{ + struct lib_ring_buffer_ctx ctx; + int ret = 0; + size_t len, reserve_len; + + len = stream->metadata_cache->metadata_written - + stream->metadata_cache_read; + if (!len) + return 0; + reserve_len = min_t(size_t, + chan->ops->packet_avail_size(chan->chan), + len); + lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, + sizeof(char), -1); + /* + * If reservation failed, return an error to the caller. + */ + ret = chan->ops->event_reserve(&ctx, 0); + if (ret != 0) { + printk(KERN_WARNING "LTTng: Metadata event reservation failed\n"); + goto end; + } + chan->ops->event_write(&ctx, + stream->metadata_cache->data + stream->metadata_cache_read, + reserve_len); + chan->ops->event_commit(&ctx); + stream->metadata_cache_read += reserve_len; + ret = reserve_len; + +end: + return ret; +} + +/* + * Write the metadata to the metadata cache. + * Must be called with sessions_mutex held. + */ int lttng_metadata_printf(struct lttng_session *session, const char *fmt, ...) { - struct lib_ring_buffer_ctx ctx; - struct lttng_channel *chan = session->metadata; char *str; - int ret = 0, waitret; - size_t len, reserve_len, pos; + size_t len; va_list ap; + struct lttng_metadata_stream *stream; WARN_ON_ONCE(!ACCESS_ONCE(session->active)); @@ -485,42 +610,40 @@ int lttng_metadata_printf(struct lttng_session *session, return -ENOMEM; len = strlen(str); - pos = 0; - - for (pos = 0; pos < len; pos += reserve_len) { - reserve_len = min_t(size_t, - chan->ops->packet_avail_size(chan->chan), - len - pos); - lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len, - sizeof(char), -1); - /* - * We don't care about metadata buffer's records lost - * count, because we always retry here. Report error if - * we need to bail out after timeout or being - * interrupted. - */ - waitret = wait_event_interruptible_timeout(*chan->ops->get_writer_buf_wait_queue(chan->chan, -1), - ({ - ret = chan->ops->event_reserve(&ctx, 0); - ret != -ENOBUFS || !ret; - }), - msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC)); - if (!waitret || waitret == -ERESTARTSYS || ret) { - printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n", - waitret == -ERESTARTSYS ? "interrupted" : - (ret == -ENOBUFS ? "timeout" : "I/O error")); - if (waitret == -ERESTARTSYS) - ret = waitret; - goto end; - } - chan->ops->event_write(&ctx, &str[pos], reserve_len); - chan->ops->event_commit(&ctx); + if (session->metadata_cache->metadata_written + len > + session->metadata_cache->cache_alloc) { + char *tmp_cache_realloc; + unsigned int tmp_cache_alloc_size; + + tmp_cache_alloc_size = max_t(unsigned int, + session->metadata_cache->cache_alloc + len, + session->metadata_cache->cache_alloc << 1); + tmp_cache_realloc = krealloc(session->metadata_cache->data, + tmp_cache_alloc_size, GFP_KERNEL); + if (!tmp_cache_realloc) + goto err; + session->metadata_cache->cache_alloc = tmp_cache_alloc_size; + session->metadata_cache->data = tmp_cache_realloc; } -end: + memcpy(session->metadata_cache->data + + session->metadata_cache->metadata_written, + str, len); + session->metadata_cache->metadata_written += len; kfree(str); - return ret; + + list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) + wake_up_interruptible(&stream->read_wait); + + return 0; + +err: + kfree(str); + return -ENOMEM; } +/* + * Must be called with sessions_mutex held. + */ static int _lttng_field_statedump(struct lttng_session *session, const struct lttng_event_field *field) @@ -677,6 +800,9 @@ int _lttng_fields_metadata_statedump(struct lttng_session *session, return ret; } +/* + * Must be called with sessions_mutex held. + */ static int _lttng_event_metadata_statedump(struct lttng_session *session, struct lttng_channel *chan, @@ -686,7 +812,7 @@ int _lttng_event_metadata_statedump(struct lttng_session *session, if (event->metadata_dumped || !ACCESS_ONCE(session->active)) return 0; - if (chan == session->metadata) + if (chan->channel_type == METADATA_CHANNEL) return 0; ret = lttng_metadata_printf(session, @@ -742,6 +868,9 @@ end: } +/* + * Must be called with sessions_mutex held. + */ static int _lttng_channel_metadata_statedump(struct lttng_session *session, struct lttng_channel *chan) @@ -750,7 +879,8 @@ int _lttng_channel_metadata_statedump(struct lttng_session *session, if (chan->metadata_dumped || !ACCESS_ONCE(session->active)) return 0; - if (chan == session->metadata) + + if (chan->channel_type == METADATA_CHANNEL) return 0; WARN_ON_ONCE(!chan->header_type); @@ -789,16 +919,19 @@ end: return ret; } +/* + * Must be called with sessions_mutex held. + */ static int _lttng_stream_packet_context_declare(struct lttng_session *session) { return lttng_metadata_printf(session, "struct packet_context {\n" - " uint64_t timestamp_begin;\n" - " uint64_t timestamp_end;\n" - " uint32_t events_discarded;\n" - " uint32_t content_size;\n" - " uint32_t packet_size;\n" + " uint64_clock_monotonic_t timestamp_begin;\n" + " uint64_clock_monotonic_t timestamp_end;\n" + " uint64_t content_size;\n" + " uint64_t packet_size;\n" + " unsigned long events_discarded;\n" " uint32_t cpu_id;\n" "};\n\n" ); @@ -812,6 +945,8 @@ int _lttng_stream_packet_context_declare(struct lttng_session *session) * Large header: * id: range: 0 - 65534. * id 65535 is reserved to indicate an extended header. + * + * Must be called with sessions_mutex held. */ static int _lttng_event_header_declare(struct lttng_session *session) @@ -821,11 +956,11 @@ int _lttng_event_header_declare(struct lttng_session *session) " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n" " variant {\n" " struct {\n" - " uint27_t timestamp;\n" + " uint27_clock_monotonic_t timestamp;\n" " } compact;\n" " struct {\n" " uint32_t id;\n" - " uint64_t timestamp;\n" + " uint64_clock_monotonic_t timestamp;\n" " } extended;\n" " } v;\n" "} align(%u);\n" @@ -834,11 +969,11 @@ int _lttng_event_header_declare(struct lttng_session *session) " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n" " variant {\n" " struct {\n" - " uint32_t timestamp;\n" + " uint32_clock_monotonic_t timestamp;\n" " } compact;\n" " struct {\n" " uint32_t id;\n" - " uint64_t timestamp;\n" + " uint64_clock_monotonic_t timestamp;\n" " } extended;\n" " } v;\n" "} align(%u);\n\n", @@ -847,14 +982,42 @@ int _lttng_event_header_declare(struct lttng_session *session) ); } + /* + * Approximation of NTP time of day to clock monotonic correlation, + * taken at start of trace. + * Yes, this is only an approximation. Yes, we can (and will) do better + * in future versions. + */ +static +uint64_t measure_clock_offset(void) +{ + uint64_t offset, monotonic[2], realtime; + struct timespec rts = { 0, 0 }; + unsigned long flags; + + /* Disable interrupts to increase correlation precision. */ + local_irq_save(flags); + monotonic[0] = trace_clock_read64(); + getnstimeofday(&rts); + monotonic[1] = trace_clock_read64(); + local_irq_restore(flags); + + offset = (monotonic[0] + monotonic[1]) >> 1; + realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC; + realtime += rts.tv_nsec; + offset = realtime - offset; + return offset; +} + /* * Output metadata into this session's metadata buffers. + * Must be called with sessions_mutex held. */ static int _lttng_session_metadata_statedump(struct lttng_session *session) { unsigned char *uuid_c = session->uuid.b; - unsigned char uuid_s[37]; + unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN]; struct lttng_channel *chan; struct lttng_event *event; int ret = 0; @@ -863,10 +1026,6 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) return 0; if (session->metadata_dumped) goto skip_session; - if (!session->metadata) { - printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n"); - return -EPERM; - } snprintf(uuid_s, sizeof(uuid_s), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", @@ -880,6 +1039,7 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n" "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n" + "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n" "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n" "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n" "\n" @@ -898,8 +1058,10 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) lttng_alignof(uint16_t) * CHAR_BIT, lttng_alignof(uint32_t) * CHAR_BIT, lttng_alignof(uint64_t) * CHAR_BIT, - CTF_VERSION_MAJOR, - CTF_VERSION_MINOR, + sizeof(unsigned long) * CHAR_BIT, + lttng_alignof(unsigned long) * CHAR_BIT, + CTF_SPEC_MAJOR, + CTF_SPEC_MINOR, uuid_s, #ifdef __BIG_ENDIAN "be" @@ -910,6 +1072,79 @@ int _lttng_session_metadata_statedump(struct lttng_session *session) if (ret) goto end; + ret = lttng_metadata_printf(session, + "env {\n" + " hostname = \"%s\";\n" + " domain = \"kernel\";\n" + " sysname = \"%s\";\n" + " kernel_release = \"%s\";\n" + " kernel_version = \"%s\";\n" + " tracer_name = \"lttng-modules\";\n" + " tracer_major = %d;\n" + " tracer_minor = %d;\n" + " tracer_patchlevel = %d;\n" + "};\n\n", + current->nsproxy->uts_ns->name.nodename, + utsname()->sysname, + utsname()->release, + utsname()->version, + LTTNG_MODULES_MAJOR_VERSION, + LTTNG_MODULES_MINOR_VERSION, + LTTNG_MODULES_PATCHLEVEL_VERSION + ); + if (ret) + goto end; + + ret = lttng_metadata_printf(session, + "clock {\n" + " name = %s;\n", + "monotonic" + ); + if (ret) + goto end; + + if (!trace_clock_uuid(clock_uuid_s)) { + ret = lttng_metadata_printf(session, + " uuid = \"%s\";\n", + clock_uuid_s + ); + if (ret) + goto end; + } + + ret = lttng_metadata_printf(session, + " description = \"Monotonic Clock\";\n" + " freq = %llu; /* Frequency, in Hz */\n" + " /* clock value offset from Epoch is: offset * (1/freq) */\n" + " offset = %llu;\n" + "};\n\n", + (unsigned long long) trace_clock_freq(), + (unsigned long long) measure_clock_offset() + ); + if (ret) + goto end; + + ret = lttng_metadata_printf(session, + "typealias integer {\n" + " size = 27; align = 1; signed = false;\n" + " map = clock.monotonic.value;\n" + "} := uint27_clock_monotonic_t;\n" + "\n" + "typealias integer {\n" + " size = 32; align = %u; signed = false;\n" + " map = clock.monotonic.value;\n" + "} := uint32_clock_monotonic_t;\n" + "\n" + "typealias integer {\n" + " size = 64; align = %u; signed = false;\n" + " map = clock.monotonic.value;\n" + "} := uint64_clock_monotonic_t;\n\n", + lttng_alignof(uint32_t) * CHAR_BIT, + lttng_alignof(uint64_t) * CHAR_BIT + ); + if (ret) + goto end; + ret = _lttng_stream_packet_context_declare(session); if (ret) goto end;