X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fust-metadata.c;h=0f060c440a0fdb13e80a607c48df8600d582f12e;hp=f8871ecaad1f460e803d9862b891acd9df937aa9;hb=3671213542e8d4953bcb036d7cc91603dbad8984;hpb=d0b96690836f4b876096f3dc14801f8e25281a77 diff --git a/src/bin/lttng-sessiond/ust-metadata.c b/src/bin/lttng-sessiond/ust-metadata.c index f8871ecaa..0f060c440 100644 --- a/src/bin/lttng-sessiond/ust-metadata.c +++ b/src/bin/lttng-sessiond/ust-metadata.c @@ -20,6 +20,7 @@ */ #define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include @@ -37,6 +38,14 @@ #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b))) #endif +#define NSEC_PER_SEC 1000000000ULL +#define NR_CLOCK_OFFSET_SAMPLES 10 + +struct offset_sample { + uint64_t offset; /* correlation offset */ + uint64_t measure_delta; /* lower is better */ +}; + static inline int fls(unsigned int x) { @@ -112,6 +121,23 @@ ssize_t metadata_reserve(struct ust_registry_session *session, size_t len) return ret; } +static +int metadata_file_append(struct ust_registry_session *session, + const char *str, size_t len) +{ + ssize_t written; + + if (session->metadata_fd < 0) { + return 0; + } + /* Write to metadata file */ + written = lttng_write(session->metadata_fd, str, len); + if (written != len) { + return -1; + } + return 0; +} + /* * We have exclusive access to our metadata buffer (protected by the * ust_lock), so we can do racy operations such as looking for @@ -141,6 +167,11 @@ int lttng_metadata_printf(struct ust_registry_session *session, goto end; } memcpy(&session->metadata[offset], str, len); + ret = metadata_file_append(session, str, len); + if (ret) { + PERROR("Error appending to metadata file"); + goto end; + } DBG3("Append to metadata: \"%s\"", str); ret = 0; @@ -328,7 +359,7 @@ int ust_metadata_event_statedump(struct ust_registry_session *session, ret = lttng_metadata_printf(session, " loglevel = %d;\n", - event->loglevel); + event->loglevel_value); if (ret) goto end; @@ -340,23 +371,6 @@ int ust_metadata_event_statedump(struct ust_registry_session *session, goto end; } -#if 0 /* context for events not supported */ - if (event->ctx) { - ret = lttng_metadata_printf(session, - " context := struct {\n"); - if (ret) - goto end; - } - ret = _lttng_context_metadata_statedump(session, event->ctx); - if (ret) - goto end; - if (event->ctx) { - ret = lttng_metadata_printf(session, - " };\n"); - if (ret) - goto end; - } -#endif ret = lttng_metadata_printf(session, " fields := struct {\n" ); @@ -372,6 +386,7 @@ int ust_metadata_event_statedump(struct ust_registry_session *session, "};\n\n"); if (ret) goto end; + event->metadata_dumped = 1; end: return ret; @@ -424,6 +439,8 @@ int ust_metadata_channel_statedump(struct ust_registry_session *session, ret = lttng_metadata_printf(session, "};\n\n"); + /* Flag success of metadata dump. */ + chan->metadata_dumped = 1; end: return ret; @@ -487,37 +504,70 @@ int _lttng_event_header_declare(struct ust_registry_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) +int measure_single_clock_offset(struct offset_sample *sample) { - uint64_t offset, monotonic[2], realtime; + uint64_t offset, monotonic[2], measure_delta, realtime; + uint64_t tcf = trace_clock_freq(); struct timespec rts = { 0, 0 }; int ret; monotonic[0] = trace_clock_read64(); ret = clock_gettime(CLOCK_REALTIME, &rts); - if (ret < 0) - return 0; + if (ret < 0) { + return ret; + } monotonic[1] = trace_clock_read64(); + measure_delta = monotonic[1] - monotonic[0]; + if (measure_delta > sample->measure_delta) { + /* + * Discard value if it took longer to read than the best + * sample so far. + */ + return 0; + } offset = (monotonic[0] + monotonic[1]) >> 1; - realtime = (uint64_t) rts.tv_sec * 1000000000ULL; - realtime += rts.tv_nsec; + realtime = (uint64_t) rts.tv_sec * tcf; + if (tcf == NSEC_PER_SEC) { + realtime += rts.tv_nsec; + } else { + realtime += (uint64_t) rts.tv_nsec * tcf / NSEC_PER_SEC; + } offset = realtime - offset; - return offset; + sample->offset = offset; + sample->measure_delta = measure_delta; + return 0; } +/* + * Approximation of NTP time of day to clock monotonic correlation, + * taken at start of trace. Keep the measurement that took the less time + * to complete, thus removing imprecision caused by preemption. + */ +static +uint64_t measure_clock_offset(void) +{ + int i; + struct offset_sample offset_best_sample = { + .offset = 0, + .measure_delta = UINT64_MAX, + }; + + for (i = 0; i < NR_CLOCK_OFFSET_SAMPLES; i++) { + if (measure_single_clock_offset(&offset_best_sample)) { + return 0; + } + } + return offset_best_sample.offset; +} /* * Should be called with session registry mutex held. */ int ust_metadata_session_statedump(struct ust_registry_session *session, - struct ust_app *app) + struct ust_app *app, + uint32_t major, + uint32_t minor) { unsigned char *uuid_c; char uuid_s[UUID_STR_LEN], @@ -525,6 +575,8 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, int ret = 0; char hostname[HOST_NAME_MAX]; + assert(session); + uuid_c = session->uuid; snprintf(uuid_s, sizeof(uuid_s), @@ -534,6 +586,15 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11], uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]); + /* For crash ABI */ + ret = lttng_metadata_printf(session, + "/* CTF %u.%u */\n\n", + CTF_SPEC_MAJOR, + CTF_SPEC_MINOR); + if (ret) { + goto end; + } + ret = lttng_metadata_printf(session, "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n" "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n" @@ -579,13 +640,10 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, " domain = \"ust\";\n" " tracer_name = \"lttng-ust\";\n" " tracer_major = %u;\n" - " tracer_minor = %u;\n" - " tracer_patchlevel = %u;\n" - "};\n\n", + " tracer_minor = %u;\n", hostname, - app->version.major, - app->version.minor, - app->version.patchlevel + major, + minor ); if (ret) goto end; @@ -596,9 +654,10 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, */ if (app) { ret = lttng_metadata_printf(session, + " tracer_patchlevel = %u;\n" " vpid = %d;\n" - " procname = \"%s\";\n" - "};\n\n", + " procname = \"%s\";\n", + app->version.patchlevel, (int) app->pid, app->name ); @@ -615,8 +674,8 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, ret = lttng_metadata_printf(session, "clock {\n" - " name = %s;\n", - "monotonic" + " name = \"%s\";\n", + trace_clock_name() ); if (ret) goto end; @@ -631,11 +690,12 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, } ret = lttng_metadata_printf(session, - " description = \"Monotonic Clock\";\n" + " description = \"%s\";\n" " freq = %" PRIu64 "; /* Frequency, in Hz */\n" " /* clock value offset from Epoch is: offset * (1/freq) */\n" " offset = %" PRIu64 ";\n" "};\n\n", + trace_clock_description(), trace_clock_freq(), measure_clock_offset() ); @@ -645,20 +705,23 @@ int ust_metadata_session_statedump(struct ust_registry_session *session, ret = lttng_metadata_printf(session, "typealias integer {\n" " size = 27; align = 1; signed = false;\n" - " map = clock.monotonic.value;\n" + " map = clock.%s.value;\n" "} := uint27_clock_monotonic_t;\n" "\n" "typealias integer {\n" " size = 32; align = %u; signed = false;\n" - " map = clock.monotonic.value;\n" + " map = clock.%s.value;\n" "} := uint32_clock_monotonic_t;\n" "\n" "typealias integer {\n" " size = 64; align = %u; signed = false;\n" - " map = clock.monotonic.value;\n" + " map = clock.%s.value;\n" "} := uint64_clock_monotonic_t;\n\n", + trace_clock_name(), session->uint32_t_alignment, - session->uint64_t_alignment + trace_clock_name(), + session->uint64_t_alignment, + trace_clock_name() ); if (ret) goto end;