Add vtid (gettid()) context
[lttng-ust.git] / libust / lttng-context-vtid.c
1 /*
2 * (C) Copyright 2009-2011 -
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * LTTng UST vtid context.
6 *
7 * Dual LGPL v2.1/GPL v2 license.
8 */
9
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <ust/lttng-events.h>
13 #include <ust/lttng-tracer.h>
14 #include <ust/ringbuffer-config.h>
15
16 #ifdef __linux__
17 #include <syscall.h>
18 #endif
19
20 #if defined(_syscall0)
21 _syscall0(pid_t, gettid)
22 #elif defined(__NR_gettid)
23 static inline pid_t gettid(void)
24 {
25 return syscall(__NR_gettid);
26 }
27 #else
28 #warning "use pid as tid"
29 static inline pid_t gettid(void)
30 {
31 return getpid();
32 }
33 #endif
34
35 /*
36 * We cache the result to ensure we don't trigger a system call for
37 * each event.
38 */
39 static __thread pid_t cached_vtid;
40
41 static
42 size_t vtid_get_size(size_t offset)
43 {
44 size_t size = 0;
45
46 size += lib_ring_buffer_align(offset, lttng_alignof(pid_t));
47 size += sizeof(pid_t);
48 return size;
49 }
50
51 static
52 void vtid_record(struct lttng_ctx_field *field,
53 struct lib_ring_buffer_ctx *ctx,
54 struct ltt_channel *chan)
55 {
56 if (unlikely(!cached_vtid))
57 cached_vtid = gettid();
58 lib_ring_buffer_align_ctx(ctx, lttng_alignof(cached_vtid));
59 chan->ops->event_write(ctx, &cached_vtid, sizeof(cached_vtid));
60 }
61
62 int lttng_add_vtid_to_ctx(struct lttng_ctx **ctx)
63 {
64 struct lttng_ctx_field *field;
65
66 field = lttng_append_context(ctx);
67 if (!field)
68 return -ENOMEM;
69 if (lttng_find_context(*ctx, "vtid")) {
70 lttng_remove_context_field(ctx, field);
71 return -EEXIST;
72 }
73 field->event_field.name = "vtid";
74 field->event_field.type.atype = atype_integer;
75 field->event_field.type.u.basic.integer.size = sizeof(pid_t) * CHAR_BIT;
76 field->event_field.type.u.basic.integer.alignment = lttng_alignof(pid_t) * CHAR_BIT;
77 field->event_field.type.u.basic.integer.signedness = lttng_is_signed_type(pid_t);
78 field->event_field.type.u.basic.integer.reverse_byte_order = 0;
79 field->event_field.type.u.basic.integer.base = 10;
80 field->event_field.type.u.basic.integer.encoding = lttng_encode_none;
81 field->get_size = vtid_get_size;
82 field->record = vtid_record;
83 return 0;
84 }
This page took 0.030012 seconds and 4 git commands to generate.