Blame view

samples/trace_events/trace-events-sample.c 2.87 KB
09c434b8a   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
9cfe06f8c   Steven Rostedt   tracing/events: a...
2
3
4
5
6
7
8
9
10
11
12
  #include <linux/module.h>
  #include <linux/kthread.h>
  
  /*
   * Any file that uses trace points, must include the header.
   * But only one file, must include the header by defining
   * CREATE_TRACE_POINTS first.  This will make the C code that
   * creates the handles for the trace points.
   */
  #define CREATE_TRACE_POINTS
  #include "trace-events-sample.h"
4e20e3a60   Steven Rostedt (Red Hat)   tracing: Update t...
13
14
15
16
17
18
19
  static const char *random_strings[] = {
  	"Mother Goose",
  	"Snoopy",
  	"Gandalf",
  	"Frodo",
  	"One ring to rule them all"
  };
9cfe06f8c   Steven Rostedt   tracing/events: a...
20
21
22
  
  static void simple_thread_func(int cnt)
  {
4e20e3a60   Steven Rostedt (Red Hat)   tracing: Update t...
23
24
25
  	int array[6];
  	int len = cnt % 5;
  	int i;
9cfe06f8c   Steven Rostedt   tracing/events: a...
26
27
  	set_current_state(TASK_INTERRUPTIBLE);
  	schedule_timeout(HZ);
4e20e3a60   Steven Rostedt (Red Hat)   tracing: Update t...
28
29
30
31
  
  	for (i = 0; i < len; i++)
  		array[i] = i + 1;
  	array[i] = 0;
c4c7eb293   Steven Rostedt (Red Hat)   tracing: Add TRAC...
32
  	/* Silly tracepoints */
4e20e3a60   Steven Rostedt (Red Hat)   tracing: Update t...
33
  	trace_foo_bar("hello", cnt, array, random_strings[len],
3bd370625   Sebastian Andrzej Siewior   sched/core: Provi...
34
  		      current->cpus_ptr);
c4c7eb293   Steven Rostedt (Red Hat)   tracing: Add TRAC...
35

7496946a8   Steven Rostedt (Red Hat)   tracing: Add samp...
36
  	trace_foo_with_template_simple("HELLO", cnt);
c4c7eb293   Steven Rostedt (Red Hat)   tracing: Add TRAC...
37
  	trace_foo_bar_with_cond("Some times print", cnt);
7496946a8   Steven Rostedt (Red Hat)   tracing: Add samp...
38
39
40
41
  
  	trace_foo_with_template_cond("prints other times", cnt);
  
  	trace_foo_with_template_print("I have to be different", cnt);
9cfe06f8c   Steven Rostedt   tracing/events: a...
42
43
44
45
46
47
48
49
50
51
52
53
54
  }
  
  static int simple_thread(void *arg)
  {
  	int cnt = 0;
  
  	while (!kthread_should_stop())
  		simple_thread_func(cnt++);
  
  	return 0;
  }
  
  static struct task_struct *simple_tsk;
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
55
56
57
58
59
60
61
62
63
  static struct task_struct *simple_tsk_fn;
  
  static void simple_thread_func_fn(int cnt)
  {
  	set_current_state(TASK_INTERRUPTIBLE);
  	schedule_timeout(HZ);
  
  	/* More silly tracepoints */
  	trace_foo_bar_with_fn("Look at me", cnt);
7496946a8   Steven Rostedt (Red Hat)   tracing: Add samp...
64
  	trace_foo_with_template_fn("Look at me too", cnt);
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
65
66
67
68
69
70
71
72
73
74
75
76
77
  }
  
  static int simple_thread_fn(void *arg)
  {
  	int cnt = 0;
  
  	while (!kthread_should_stop())
  		simple_thread_func_fn(cnt++);
  
  	return 0;
  }
  
  static DEFINE_MUTEX(thread_mutex);
a0cb2b5c3   Linus Torvalds   Fix tracing sampl...
78
  static int simple_thread_cnt;
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
79

8cf868aff   Steven Rostedt (Red Hat)   tracing: Have the...
80
  int foo_bar_reg(void)
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
81
  {
6575257c6   Steven Rostedt (VMware)   tracing/samples: ...
82
83
84
  	mutex_lock(&thread_mutex);
  	if (simple_thread_cnt++)
  		goto out;
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
85
86
87
88
89
90
91
  	pr_info("Starting thread for foo_bar_fn
  ");
  	/*
  	 * We shouldn't be able to start a trace when the module is
  	 * unloading (there's other locks to prevent that). But
  	 * for consistency sake, we still take the thread_mutex.
  	 */
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
92
  	simple_tsk_fn = kthread_run(simple_thread_fn, NULL, "event-sample-fn");
6575257c6   Steven Rostedt (VMware)   tracing/samples: ...
93
   out:
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
94
  	mutex_unlock(&thread_mutex);
8cf868aff   Steven Rostedt (Red Hat)   tracing: Have the...
95
  	return 0;
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
96
97
98
99
  }
  
  void foo_bar_unreg(void)
  {
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
100
  	mutex_lock(&thread_mutex);
6575257c6   Steven Rostedt (VMware)   tracing/samples: ...
101
102
103
104
105
  	if (--simple_thread_cnt)
  		goto out;
  
  	pr_info("Killing thread for foo_bar_fn
  ");
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
106
107
108
  	if (simple_tsk_fn)
  		kthread_stop(simple_tsk_fn);
  	simple_tsk_fn = NULL;
6575257c6   Steven Rostedt (VMware)   tracing/samples: ...
109
   out:
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
110
111
  	mutex_unlock(&thread_mutex);
  }
9cfe06f8c   Steven Rostedt   tracing/events: a...
112
113
114
115
116
117
118
119
120
121
122
123
124
  
  static int __init trace_event_init(void)
  {
  	simple_tsk = kthread_run(simple_thread, NULL, "event-sample");
  	if (IS_ERR(simple_tsk))
  		return -1;
  
  	return 0;
  }
  
  static void __exit trace_event_exit(void)
  {
  	kthread_stop(simple_tsk);
6adc13f8c   Steven Rostedt (Red Hat)   tracing: Add TRAC...
125
126
127
128
129
  	mutex_lock(&thread_mutex);
  	if (simple_tsk_fn)
  		kthread_stop(simple_tsk_fn);
  	simple_tsk_fn = NULL;
  	mutex_unlock(&thread_mutex);
9cfe06f8c   Steven Rostedt   tracing/events: a...
130
131
132
133
134
135
136
137
  }
  
  module_init(trace_event_init);
  module_exit(trace_event_exit);
  
  MODULE_AUTHOR("Steven Rostedt");
  MODULE_DESCRIPTION("trace-events-sample");
  MODULE_LICENSE("GPL");