2 * lttng-context-preemptible.c
4 * LTTng preemptible context.
6 * Copyright (C) 2009-2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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; only
11 * version 2.1 of the License.
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.
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
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/irqflags.h>
27 #include <lttng-events.h>
28 #include <wrapper/ringbuffer/frontend_types.h>
29 #include <wrapper/vmalloc.h>
30 #include <lttng-tracer.h>
33 * We nest twice in preempt disabling within LTTng: one nesting is done
34 * by the instrumentation (tracepoint, kprobes, kretprobes, syscall
35 * tracepoint), and the second is within the lib ring buffer
36 * lib_ring_buffer_get_cpu().
38 #define LTTNG_PREEMPT_DISABLE_NESTING 2
41 size_t preemptible_get_size(size_t offset
)
45 size
+= lib_ring_buffer_align(offset
, lttng_alignof(uint8_t));
46 size
+= sizeof(uint8_t);
51 void preemptible_record(struct lttng_ctx_field
*field
,
52 struct lib_ring_buffer_ctx
*ctx
,
53 struct lttng_channel
*chan
)
55 int pc
= preempt_count();
56 uint8_t preemptible
= 0;
58 WARN_ON_ONCE(pc
< LTTNG_PREEMPT_DISABLE_NESTING
);
59 if (pc
== LTTNG_PREEMPT_DISABLE_NESTING
)
61 lib_ring_buffer_align_ctx(ctx
, lttng_alignof(preemptible
));
62 chan
->ops
->event_write(ctx
, &preemptible
, sizeof(preemptible
));
66 void preemptible_get_value(struct lttng_ctx_field
*field
,
67 struct lttng_probe_ctx
*lttng_probe_ctx
,
68 union lttng_ctx_value
*value
)
70 int pc
= preempt_count();
72 WARN_ON_ONCE(pc
< LTTNG_PREEMPT_DISABLE_NESTING
);
73 if (pc
== LTTNG_PREEMPT_DISABLE_NESTING
)
79 int lttng_add_preemptible_to_ctx(struct lttng_ctx
**ctx
)
81 struct lttng_ctx_field
*field
;
83 field
= lttng_append_context(ctx
);
86 if (lttng_find_context(*ctx
, "preemptible")) {
87 lttng_remove_context_field(ctx
, field
);
90 field
->event_field
.name
= "preemptible";
91 field
->event_field
.type
.atype
= atype_integer
;
92 field
->event_field
.type
.u
.basic
.integer
.size
= sizeof(uint8_t) * CHAR_BIT
;
93 field
->event_field
.type
.u
.basic
.integer
.alignment
= lttng_alignof(uint8_t) * CHAR_BIT
;
94 field
->event_field
.type
.u
.basic
.integer
.signedness
= lttng_is_signed_type(uint8_t);
95 field
->event_field
.type
.u
.basic
.integer
.reverse_byte_order
= 0;
96 field
->event_field
.type
.u
.basic
.integer
.base
= 10;
97 field
->event_field
.type
.u
.basic
.integer
.encoding
= lttng_encode_none
;
98 field
->get_size
= preemptible_get_size
;
99 field
->record
= preemptible_record
;
100 field
->get_value
= preemptible_get_value
;
101 lttng_context_update(*ctx
);
102 wrapper_vmalloc_sync_all();
105 EXPORT_SYMBOL_GPL(lttng_add_preemptible_to_ctx
);