4 * (C) Copyright 2005-2008 -
5 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 * Karim Yaghmour (karim@opersys.com)
23 * Tom Zanussi (zanussi@us.ibm.com)
24 * Bob Wisniewski (bob@watson.ibm.com)
26 * Bob Wisniewski (bob@watson.ibm.com)
29 * 22/09/06, Move to the marker/probes mechanism.
30 * 19/10/05, Complete lockless mechanism.
31 * 27/05/05, Modular redesign and rewrite.
35 #include <urcu/rculist.h>
37 #include <ust/clock.h>
39 #include "tracercore.h"
43 //ust// static void async_wakeup(unsigned long data);
45 //ust// static DEFINE_TIMER(ltt_async_wakeup_timer, async_wakeup, 0, 0);
47 /* Default callbacks for modules */
48 notrace
int ltt_filter_control_default(enum ltt_filter_control_msg msg
,
49 struct ust_trace
*trace
)
54 int ltt_statedump_default(struct ust_trace
*trace
)
59 /* Callbacks for registered modules */
61 int (*ltt_filter_control_functor
)
62 (enum ltt_filter_control_msg msg
, struct ust_trace
*trace
) =
63 ltt_filter_control_default
;
64 struct module
*ltt_filter_control_owner
;
66 /* These function pointers are protected by a trace activation check */
67 struct module
*ltt_run_filter_owner
;
68 int (*ltt_statedump_functor
)(struct ust_trace
*trace
) =
69 ltt_statedump_default
;
70 struct module
*ltt_statedump_owner
;
72 struct chan_info_struct chan_infos
[] = {
73 [LTT_CHANNEL_METADATA
] = {
75 LTT_DEFAULT_SUBBUF_SIZE_LOW
,
76 LTT_DEFAULT_N_SUBBUFS_LOW
,
80 LTT_DEFAULT_SUBBUF_SIZE_HIGH
,
81 LTT_DEFAULT_N_SUBBUFS_HIGH
,
85 static enum ltt_channels
get_channel_type_from_name(const char *name
)
90 return LTT_CHANNEL_UST
;
92 for (i
= 0; i
< ARRAY_SIZE(chan_infos
); i
++)
93 if (chan_infos
[i
].name
&& !strcmp(name
, chan_infos
[i
].name
))
94 return (enum ltt_channels
)i
;
96 return LTT_CHANNEL_UST
;
100 * ltt_module_register - LTT module registration
102 * @function: callback to register
103 * @owner: module which owns the callback
105 * The module calling this registration function must ensure that no
106 * trap-inducing code will be executed by "function". E.g. vmalloc_sync_all()
107 * must be called between a vmalloc and the moment the memory is made visible to
108 * "function". This registration acts as a vmalloc_sync_all. Therefore, only if
109 * the module allocates virtual memory after its registration must it
110 * synchronize the TLBs.
112 //ust// int ltt_module_register(enum ltt_module_function name, void *function,
113 //ust// struct module *owner)
118 //ust// * Make sure no page fault can be triggered by the module about to be
119 //ust// * registered. We deal with this here so we don't have to call
120 //ust// * vmalloc_sync_all() in each module's init.
122 //ust// vmalloc_sync_all();
124 //ust// switch (name) {
125 //ust// case LTT_FUNCTION_RUN_FILTER:
126 //ust// if (ltt_run_filter_owner != NULL) {
127 //ust// ret = -EEXIST;
130 //ust// ltt_filter_register((ltt_run_filter_functor)function);
131 //ust// ltt_run_filter_owner = owner;
133 //ust// case LTT_FUNCTION_FILTER_CONTROL:
134 //ust// if (ltt_filter_control_owner != NULL) {
135 //ust// ret = -EEXIST;
138 //ust// ltt_filter_control_functor =
139 //ust// (int (*)(enum ltt_filter_control_msg,
140 //ust// struct ust_trace *))function;
141 //ust// ltt_filter_control_owner = owner;
143 //ust// case LTT_FUNCTION_STATEDUMP:
144 //ust// if (ltt_statedump_owner != NULL) {
145 //ust// ret = -EEXIST;
148 //ust// ltt_statedump_functor =
149 //ust// (int (*)(struct ust_trace *))function;
150 //ust// ltt_statedump_owner = owner;
160 * ltt_module_unregister - LTT module unregistration
163 //ust// void ltt_module_unregister(enum ltt_module_function name)
165 //ust// switch (name) {
166 //ust// case LTT_FUNCTION_RUN_FILTER:
167 //ust// ltt_filter_unregister();
168 //ust// ltt_run_filter_owner = NULL;
169 //ust// /* Wait for preempt sections to finish */
170 //ust// synchronize_sched();
172 //ust// case LTT_FUNCTION_FILTER_CONTROL:
173 //ust// ltt_filter_control_functor = ltt_filter_control_default;
174 //ust// ltt_filter_control_owner = NULL;
176 //ust// case LTT_FUNCTION_STATEDUMP:
177 //ust// ltt_statedump_functor = ltt_statedump_default;
178 //ust// ltt_statedump_owner = NULL;
184 static LIST_HEAD(ltt_transport_list
);
187 * ltt_transport_register - LTT transport registration
188 * @transport: transport structure
190 * Registers a transport which can be used as output to extract the data out of
191 * LTTng. The module calling this registration function must ensure that no
192 * trap-inducing code will be executed by the transport functions. E.g.
193 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
194 * is made visible to the transport function. This registration acts as a
195 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
196 * after its registration must it synchronize the TLBs.
198 void ltt_transport_register(struct ltt_transport
*transport
)
201 * Make sure no page fault can be triggered by the module about to be
202 * registered. We deal with this here so we don't have to call
203 * vmalloc_sync_all() in each module's init.
205 //ust// vmalloc_sync_all();
208 list_add_tail(&transport
->node
, <t_transport_list
);
213 * ltt_transport_unregister - LTT transport unregistration
214 * @transport: transport structure
216 void ltt_transport_unregister(struct ltt_transport
*transport
)
219 list_del(&transport
->node
);
223 static inline int is_channel_overwrite(enum ltt_channels chan
,
224 enum trace_mode mode
)
227 case LTT_TRACE_NORMAL
:
229 case LTT_TRACE_FLIGHT
:
231 case LTT_CHANNEL_METADATA
:
236 case LTT_TRACE_HYBRID
:
238 case LTT_CHANNEL_METADATA
:
248 static void trace_async_wakeup(struct ust_trace
*trace
)
251 struct ust_channel
*chan
;
253 /* Must check each channel for pending read wakeup */
254 for (i
= 0; i
< trace
->nr_channels
; i
++) {
255 chan
= &trace
->channels
[i
];
257 trace
->ops
->wakeup_channel(chan
);
261 //ust// /* Timer to send async wakeups to the readers */
262 //ust// static void async_wakeup(unsigned long data)
264 //ust// struct ust_trace *trace;
267 //ust// * PREEMPT_RT does not allow spinlocks to be taken within preempt
268 //ust// * disable sections (spinlock taken in wake_up). However, mainline won't
269 //ust// * allow mutex to be taken in interrupt context. Ugly.
270 //ust// * A proper way to do this would be to turn the timer into a
271 //ust// * periodically woken up thread, but it adds to the footprint.
273 //ust// #ifndef CONFIG_PREEMPT_RT
274 //ust// rcu_read_lock_sched();
276 //ust// ltt_lock_traces();
278 //ust// list_for_each_entry_rcu(trace, <t_traces.head, list) {
279 //ust// trace_async_wakeup(trace);
281 //ust// #ifndef CONFIG_PREEMPT_RT
282 //ust// rcu_read_unlock_sched();
284 //ust// ltt_unlock_traces();
287 //ust// mod_timer(<t_async_wakeup_timer, jiffies + LTT_PERCPU_TIMER_INTERVAL);
291 * _ltt_trace_find - find a trace by given name.
292 * trace_name: trace name
294 * Returns a pointer to the trace structure, NULL if not found.
296 struct ust_trace
*_ltt_trace_find(const char *trace_name
)
298 struct ust_trace
*trace
;
300 list_for_each_entry(trace
, <t_traces
.head
, list
)
301 if (!strncmp(trace
->trace_name
, trace_name
, NAME_MAX
))
307 /* _ltt_trace_find_setup :
308 * find a trace in setup list by given name.
310 * Returns a pointer to the trace structure, NULL if not found.
312 struct ust_trace
*_ltt_trace_find_setup(const char *trace_name
)
314 struct ust_trace
*trace
;
316 list_for_each_entry(trace
, <t_traces
.setup_head
, list
)
317 if (!strncmp(trace
->trace_name
, trace_name
, NAME_MAX
))
324 * ltt_release_transport - Release an LTT transport
325 * @kref : reference count on the transport
327 void ltt_release_transport(struct kref
*kref
)
329 //ust// struct ust_trace *trace = container_of(kref,
330 //ust// struct ust_trace, ltt_transport_kref);
331 //ust// trace->ops->remove_dirs(trace);
335 * ltt_release_trace - Release a LTT trace
336 * @kref : reference count on the trace
338 void ltt_release_trace(struct kref
*kref
)
340 struct ust_trace
*trace
= _ust_container_of(kref
,
341 struct ust_trace
, kref
);
342 ltt_channels_trace_free(trace
->channels
);
346 static inline void prepare_chan_size_num(unsigned int *subbuf_size
,
347 unsigned int *n_subbufs
)
349 /* Make sure the subbuffer size is larger than a page */
350 *subbuf_size
= max_t(unsigned int, *subbuf_size
, PAGE_SIZE
);
352 /* round to next power of 2 */
353 *subbuf_size
= 1 << get_count_order(*subbuf_size
);
354 *n_subbufs
= 1 << get_count_order(*n_subbufs
);
356 /* Subbuf size and number must both be power of two */
357 WARN_ON(hweight32(*subbuf_size
) != 1);
358 WARN_ON(hweight32(*n_subbufs
) != 1);
361 int _ltt_trace_setup(const char *trace_name
)
364 struct ust_trace
*new_trace
= NULL
;
367 enum ltt_channels chantype
;
369 if (_ltt_trace_find_setup(trace_name
)) {
370 ERR("Trace name %s already used", trace_name
);
375 if (_ltt_trace_find(trace_name
)) {
376 ERR("Trace name %s already used", trace_name
);
381 new_trace
= zmalloc(sizeof(struct ust_trace
));
383 ERR("Unable to allocate memory for trace %s", trace_name
);
387 strncpy(new_trace
->trace_name
, trace_name
, NAME_MAX
);
388 new_trace
->channels
= ltt_channels_trace_alloc(&new_trace
->nr_channels
,
389 ust_channels_overwrite_by_default
,
390 ust_channels_request_collection_by_default
, 1);
391 if (!new_trace
->channels
) {
392 ERR("Unable to allocate memory for chaninfo %s\n", trace_name
);
398 * Force metadata channel to active, no overwrite.
400 metadata_index
= ltt_channels_get_index_from_name("metadata");
401 WARN_ON(metadata_index
< 0);
402 new_trace
->channels
[metadata_index
].overwrite
= 0;
403 new_trace
->channels
[metadata_index
].active
= 1;
406 * Set hardcoded tracer defaults for some channels
408 for (chan
= 0; chan
< new_trace
->nr_channels
; chan
++) {
409 if (!(new_trace
->channels
[chan
].active
))
412 chantype
= get_channel_type_from_name(
413 ltt_channels_get_name_from_index(chan
));
414 new_trace
->channels
[chan
].subbuf_size
=
415 chan_infos
[chantype
].def_subbufsize
;
416 new_trace
->channels
[chan
].subbuf_cnt
=
417 chan_infos
[chantype
].def_subbufcount
;
420 list_add(&new_trace
->list
, <t_traces
.setup_head
);
430 int ltt_trace_setup(const char *trace_name
)
434 ret
= _ltt_trace_setup(trace_name
);
439 /* must be called from within a traces lock. */
440 static void _ltt_trace_free(struct ust_trace
*trace
)
442 list_del(&trace
->list
);
446 int ltt_trace_set_type(const char *trace_name
, const char *trace_type
)
449 struct ust_trace
*trace
;
450 struct ltt_transport
*tran_iter
, *transport
= NULL
;
454 trace
= _ltt_trace_find_setup(trace_name
);
456 ERR("Trace not found %s", trace_name
);
461 list_for_each_entry(tran_iter
, <t_transport_list
, node
) {
462 if (!strcmp(tran_iter
->name
, trace_type
)) {
463 transport
= tran_iter
;
468 ERR("Transport %s is not present", trace_type
);
473 trace
->transport
= transport
;
480 int ltt_trace_set_channel_subbufsize(const char *trace_name
,
481 const char *channel_name
, unsigned int size
)
484 struct ust_trace
*trace
;
489 trace
= _ltt_trace_find_setup(trace_name
);
491 ERR("Trace not found %s", trace_name
);
496 index
= ltt_channels_get_index_from_name(channel_name
);
498 ERR("Channel %s not found", channel_name
);
502 trace
->channels
[index
].subbuf_size
= size
;
509 int ltt_trace_set_channel_subbufcount(const char *trace_name
,
510 const char *channel_name
, unsigned int cnt
)
513 struct ust_trace
*trace
;
518 trace
= _ltt_trace_find_setup(trace_name
);
520 ERR("Trace not found %s", trace_name
);
525 index
= ltt_channels_get_index_from_name(channel_name
);
527 ERR("Channel %s not found", channel_name
);
531 trace
->channels
[index
].subbuf_cnt
= cnt
;
538 int ltt_trace_set_channel_enable(const char *trace_name
,
539 const char *channel_name
, unsigned int enable
)
542 struct ust_trace
*trace
;
547 trace
= _ltt_trace_find_setup(trace_name
);
549 ERR("Trace not found %s", trace_name
);
555 * Datas in metadata channel(marker info) is necessary to be able to
556 * read the trace, we always enable this channel.
558 if (!enable
&& !strcmp(channel_name
, "metadata")) {
559 ERR("Trying to disable metadata channel");
564 index
= ltt_channels_get_index_from_name(channel_name
);
566 ERR("Channel %s not found", channel_name
);
571 trace
->channels
[index
].active
= enable
;
578 int ltt_trace_set_channel_overwrite(const char *trace_name
,
579 const char *channel_name
, unsigned int overwrite
)
582 struct ust_trace
*trace
;
587 trace
= _ltt_trace_find_setup(trace_name
);
589 ERR("Trace not found %s", trace_name
);
595 * Always put the metadata channel in non-overwrite mode :
596 * This is a very low traffic channel and it can't afford to have its
597 * data overwritten : this data (marker info) is necessary to be
598 * able to read the trace.
600 if (overwrite
&& !strcmp(channel_name
, "metadata")) {
601 ERR("Trying to set metadata channel to overwrite mode");
606 index
= ltt_channels_get_index_from_name(channel_name
);
608 ERR("Channel %s not found", channel_name
);
613 trace
->channels
[index
].overwrite
= overwrite
;
620 int ltt_trace_alloc(const char *trace_name
)
623 struct ust_trace
*trace
;
624 unsigned int subbuf_size
, subbuf_cnt
;
625 //ust// unsigned long flags;
627 const char *channel_name
;
631 if (_ltt_trace_find(trace_name
)) { /* Trace already allocated */
636 trace
= _ltt_trace_find_setup(trace_name
);
638 ERR("Trace not found %s", trace_name
);
643 kref_init(&trace
->kref
);
644 kref_init(&trace
->ltt_transport_kref
);
645 //ust// init_waitqueue_head(&trace->kref_wq);
647 //ust// get_trace_clock();
648 trace
->freq_scale
= trace_clock_freq_scale();
650 if (!trace
->transport
) {
651 ERR("Transport is not set");
653 goto transport_error
;
655 //ust// if (!try_module_get(trace->transport->owner)) {
656 //ust// ERR("Can't lock transport module");
657 //ust// err = -ENODEV;
658 //ust// goto transport_error;
660 trace
->ops
= &trace
->transport
->ops
;
662 //ust// err = trace->ops->create_dirs(trace);
664 //ust// ERR("Can't create dir for trace %s", trace_name);
665 //ust// goto dirs_error;
668 //ust// local_irq_save(flags);
669 trace
->start_freq
= trace_clock_frequency();
670 trace
->start_tsc
= trace_clock_read64();
671 gettimeofday(&trace
->start_time
, NULL
); //ust// changed /* FIXME: is this ok? */
672 //ust// local_irq_restore(flags);
674 for (chan
= 0; chan
< trace
->nr_channels
; chan
++) {
675 if (!(trace
->channels
[chan
].active
))
678 channel_name
= ltt_channels_get_name_from_index(chan
);
679 WARN_ON(!channel_name
);
680 subbuf_size
= trace
->channels
[chan
].subbuf_size
;
681 subbuf_cnt
= trace
->channels
[chan
].subbuf_cnt
;
682 prepare_chan_size_num(&subbuf_size
, &subbuf_cnt
);
683 err
= trace
->ops
->create_channel(trace_name
, trace
,
685 &trace
->channels
[chan
],
688 trace
->channels
[chan
].overwrite
);
690 ERR("Cannot create channel %s", channel_name
);
691 goto create_channel_error
;
695 list_del(&trace
->list
);
696 //ust// if (list_empty(<t_traces.head)) {
697 //ust// mod_timer(<t_async_wakeup_timer,
698 //ust// jiffies + LTT_PERCPU_TIMER_INTERVAL);
699 //ust// set_kernel_trace_flag_all_tasks();
701 list_add_rcu(&trace
->list
, <t_traces
.head
);
702 //ust// synchronize_sched();
708 create_channel_error
:
709 for (chan
--; chan
>= 0; chan
--)
710 if (trace
->channels
[chan
].active
)
711 trace
->ops
->remove_channel(&trace
->channels
[chan
]);
714 //ust// module_put(trace->transport->owner);
716 //ust// put_trace_clock();
723 * It is worked as a wrapper for current version of ltt_control.ko.
724 * We will make a new ltt_control based on debugfs, and control each channel's
727 //ust// static int ltt_trace_create(const char *trace_name, const char *trace_type,
728 //ust// enum trace_mode mode,
729 //ust// unsigned int subbuf_size_low, unsigned int n_subbufs_low,
730 //ust// unsigned int subbuf_size_med, unsigned int n_subbufs_med,
731 //ust// unsigned int subbuf_size_high, unsigned int n_subbufs_high)
735 //ust// err = ltt_trace_setup(trace_name);
736 //ust// if (IS_ERR_VALUE(err))
739 //ust// err = ltt_trace_set_type(trace_name, trace_type);
740 //ust// if (IS_ERR_VALUE(err))
743 //ust// err = ltt_trace_alloc(trace_name);
744 //ust// if (IS_ERR_VALUE(err))
750 /* Must be called while sure that trace is in the list. */
751 static int _ltt_trace_destroy(struct ust_trace
*trace
)
760 ERR("Can't destroy trace %s : tracer is active", trace
->trace_name
);
764 /* Everything went fine */
765 list_del_rcu(&trace
->list
);
767 if (list_empty(<t_traces
.head
)) {
768 //ust// clear_kernel_trace_flag_all_tasks();
770 * We stop the asynchronous delivery of reader wakeup, but
771 * we must make one last check for reader wakeups pending
772 * later in __ltt_trace_destroy.
774 //ust// del_timer_sync(<t_async_wakeup_timer);
784 /* Sleepable part of the destroy */
785 static void __ltt_trace_destroy(struct ust_trace
*trace
, int drop
)
788 struct ust_channel
*chan
;
791 for (i
= 0; i
< trace
->nr_channels
; i
++) {
792 chan
= &trace
->channels
[i
];
794 trace
->ops
->finish_channel(chan
);
798 return; /* FIXME: temporary for ust */
799 //ust// flush_scheduled_work();
802 * The currently destroyed trace is not in the trace list anymore,
803 * so it's safe to call the async wakeup ourself. It will deliver
804 * the last subbuffers.
806 trace_async_wakeup(trace
);
808 for (i
= 0; i
< trace
->nr_channels
; i
++) {
809 chan
= &trace
->channels
[i
];
811 trace
->ops
->remove_channel(chan
);
814 kref_put(&trace
->ltt_transport_kref
, ltt_release_transport
);
816 //ust// module_put(trace->transport->owner);
819 * Wait for lttd readers to release the files, therefore making sure
820 * the last subbuffers have been read.
822 //ust// if (atomic_read(&trace->kref.refcount) > 1) {
824 //ust// __wait_event_interruptible(trace->kref_wq,
825 //ust// (atomic_read(&trace->kref.refcount) == 1), ret);
827 kref_put(&trace
->kref
, ltt_release_trace
);
830 int ltt_trace_destroy(const char *trace_name
, int drop
)
833 struct ust_trace
*trace
;
837 trace
= _ltt_trace_find(trace_name
);
839 err
= _ltt_trace_destroy(trace
);
845 __ltt_trace_destroy(trace
, drop
);
846 //ust// put_trace_clock();
851 trace
= _ltt_trace_find_setup(trace_name
);
853 _ltt_trace_free(trace
);
866 /* must be called from within a traces lock. */
867 static int _ltt_trace_start(struct ust_trace
*trace
)
876 DBG("Tracing already active for trace %s", trace
->trace_name
);
877 //ust// if (!try_module_get(ltt_run_filter_owner)) {
878 //ust// err = -ENODEV;
879 //ust// ERR("Cannot lock filter module");
880 //ust// goto get_ltt_run_filter_error;
883 /* Read by trace points without protection : be careful */
884 ltt_traces
.num_active_traces
++;
888 //ust// get_ltt_run_filter_error:
893 int ltt_trace_start(const char *trace_name
)
896 struct ust_trace
*trace
;
900 trace
= _ltt_trace_find(trace_name
);
901 err
= _ltt_trace_start(trace
);
908 * Call the kernel state dump.
909 * Events will be mixed with real kernel events, it's ok.
910 * Notice that there is no protection on the trace : that's exactly
911 * why we iterate on the list and check for trace equality instead of
912 * directly using this trace handle inside the logging function.
915 ltt_dump_marker_state(trace
);
917 //ust// if (!try_module_get(ltt_statedump_owner)) {
918 //ust// err = -ENODEV;
919 //ust// ERR("Cannot lock state dump module");
921 ltt_statedump_functor(trace
);
922 //ust// module_put(ltt_statedump_owner);
933 /* must be called from within traces lock */
934 static int _ltt_trace_stop(struct ust_trace
*trace
)
943 DBG("LTT : Tracing not active for trace %s", trace
->trace_name
);
946 ltt_traces
.num_active_traces
--;
947 //ust// synchronize_sched(); /* Wait for each tracing to be finished */
949 //ust// module_put(ltt_run_filter_owner);
950 /* Everything went fine */
958 int ltt_trace_stop(const char *trace_name
)
961 struct ust_trace
*trace
;
964 trace
= _ltt_trace_find(trace_name
);
965 err
= _ltt_trace_stop(trace
);
971 * ltt_filter_control - Trace filter control in-kernel API
972 * @msg: Action to perform on the filter
973 * @trace_name: Trace on which the action must be done
975 int ltt_filter_control(enum ltt_filter_control_msg msg
, const char *trace_name
)
978 struct ust_trace
*trace
;
980 DBG("ltt_filter_control : trace %s", trace_name
);
982 trace
= _ltt_trace_find(trace_name
);
984 ERR("Trace does not exist. Cannot proxy control request");
988 //ust// if (!try_module_get(ltt_filter_control_owner)) {
989 //ust// err = -ENODEV;
990 //ust// goto get_module_error;
993 case LTT_FILTER_DEFAULT_ACCEPT
:
994 DBG("Proxy filter default accept %s", trace_name
);
995 err
= (*ltt_filter_control_functor
)(msg
, trace
);
997 case LTT_FILTER_DEFAULT_REJECT
:
998 DBG("Proxy filter default reject %s", trace_name
);
999 err
= (*ltt_filter_control_functor
)(msg
, trace
);
1004 //ust// module_put(ltt_filter_control_owner);
1006 //ust// get_module_error:
1008 ltt_unlock_traces();