Skip to content

Commit ba9372a

Browse files
markus-metzgerIngo Molnar
authored andcommitted
x86, hw-branch-tracer: keep resources on stop
Distinguish init/reset and start/stop: init/reset will allocate and release bts tracing resources stop/start will suspend and resume bts tracing Return an error on init() if no cpu can be traced. Signed-off-by: Markus Metzger <markus.t.metzger@intel.com> LKML-Reference: <20090313104852.A30168@sedona.ch.intel.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent b8e4719 commit ba9372a

1 file changed

Lines changed: 85 additions & 34 deletions

File tree

‎kernel/trace/trace_hw_branches.c‎

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "trace_output.h"
2020

2121

22-
#define SIZEOF_BTS (1 << 13)
22+
#define BTS_BUFFER_SIZE (1 << 13)
2323

2424
/*
2525
* The tracer lock protects the below per-cpu tracer array.
@@ -33,66 +33,126 @@
3333
*/
3434
static DEFINE_SPINLOCK(bts_tracer_lock);
3535
static DEFINE_PER_CPU(struct bts_tracer *, tracer);
36-
static DEFINE_PER_CPU(unsigned char[SIZEOF_BTS], buffer);
36+
static DEFINE_PER_CPU(unsigned char[BTS_BUFFER_SIZE], buffer);
3737

3838
#define this_tracer per_cpu(tracer, smp_processor_id())
3939
#define this_buffer per_cpu(buffer, smp_processor_id())
4040

41-
static int __read_mostly trace_hw_branches_enabled;
41+
static int trace_hw_branches_enabled __read_mostly;
42+
static int trace_hw_branches_suspended __read_mostly;
4243
static struct trace_array *hw_branch_trace __read_mostly;
4344

4445

4546
/*
46-
* Start tracing on the current cpu.
47+
* Initialize the tracer for the current cpu.
4748
* The argument is ignored.
4849
*
4950
* pre: bts_tracer_lock must be locked.
5051
*/
51-
static void bts_trace_start_cpu(void *arg)
52+
static void bts_trace_init_cpu(void *arg)
5253
{
5354
if (this_tracer)
5455
ds_release_bts(this_tracer);
5556

56-
this_tracer =
57-
ds_request_bts(/* task = */ NULL, this_buffer, SIZEOF_BTS,
58-
/* ovfl = */ NULL, /* th = */ (size_t)-1,
59-
BTS_KERNEL);
57+
this_tracer = ds_request_bts(NULL, this_buffer, BTS_BUFFER_SIZE,
58+
NULL, (size_t)-1, BTS_KERNEL);
6059
if (IS_ERR(this_tracer)) {
6160
this_tracer = NULL;
6261
return;
6362
}
6463
}
6564

66-
static void bts_trace_start(struct trace_array *tr)
65+
static int bts_trace_init(struct trace_array *tr)
6766
{
67+
int cpu, avail;
68+
6869
spin_lock(&bts_tracer_lock);
6970

70-
on_each_cpu(bts_trace_start_cpu, NULL, 1);
71-
trace_hw_branches_enabled = 1;
71+
hw_branch_trace = tr;
72+
73+
on_each_cpu(bts_trace_init_cpu, NULL, 1);
74+
75+
/* Check on how many cpus we could enable tracing */
76+
avail = 0;
77+
for_each_online_cpu(cpu)
78+
if (per_cpu(tracer, cpu))
79+
avail++;
80+
81+
trace_hw_branches_enabled = (avail ? 1 : 0);
82+
trace_hw_branches_suspended = 0;
7283

7384
spin_unlock(&bts_tracer_lock);
85+
86+
87+
/* If we could not enable tracing on a single cpu, we fail. */
88+
return avail ? 0 : -EOPNOTSUPP;
7489
}
7590

7691
/*
77-
* Stop tracing on the current cpu.
92+
* Release the tracer for the current cpu.
7893
* The argument is ignored.
7994
*
8095
* pre: bts_tracer_lock must be locked.
8196
*/
82-
static void bts_trace_stop_cpu(void *arg)
97+
static void bts_trace_release_cpu(void *arg)
8398
{
8499
if (this_tracer) {
85100
ds_release_bts(this_tracer);
86101
this_tracer = NULL;
87102
}
88103
}
89104

90-
static void bts_trace_stop(struct trace_array *tr)
105+
static void bts_trace_reset(struct trace_array *tr)
91106
{
92107
spin_lock(&bts_tracer_lock);
93108

109+
on_each_cpu(bts_trace_release_cpu, NULL, 1);
94110
trace_hw_branches_enabled = 0;
95-
on_each_cpu(bts_trace_stop_cpu, NULL, 1);
111+
trace_hw_branches_suspended = 0;
112+
113+
spin_unlock(&bts_tracer_lock);
114+
}
115+
116+
/*
117+
* Resume tracing on the current cpu.
118+
* The argument is ignored.
119+
*
120+
* pre: bts_tracer_lock must be locked.
121+
*/
122+
static void bts_trace_resume_cpu(void *arg)
123+
{
124+
if (this_tracer)
125+
ds_resume_bts(this_tracer);
126+
}
127+
128+
static void bts_trace_start(struct trace_array *tr)
129+
{
130+
spin_lock(&bts_tracer_lock);
131+
132+
on_each_cpu(bts_trace_resume_cpu, NULL, 1);
133+
trace_hw_branches_suspended = 0;
134+
135+
spin_unlock(&bts_tracer_lock);
136+
}
137+
138+
/*
139+
* Suspend tracing on the current cpu.
140+
* The argument is ignored.
141+
*
142+
* pre: bts_tracer_lock must be locked.
143+
*/
144+
static void bts_trace_suspend_cpu(void *arg)
145+
{
146+
if (this_tracer)
147+
ds_suspend_bts(this_tracer);
148+
}
149+
150+
static void bts_trace_stop(struct trace_array *tr)
151+
{
152+
spin_lock(&bts_tracer_lock);
153+
154+
on_each_cpu(bts_trace_suspend_cpu, NULL, 1);
155+
trace_hw_branches_suspended = 1;
96156

97157
spin_unlock(&bts_tracer_lock);
98158
}
@@ -110,10 +170,14 @@ static int __cpuinit bts_hotcpu_handler(struct notifier_block *nfb,
110170
switch (action) {
111171
case CPU_ONLINE:
112172
case CPU_DOWN_FAILED:
113-
smp_call_function_single(cpu, bts_trace_start_cpu, NULL, 1);
173+
smp_call_function_single(cpu, bts_trace_init_cpu, NULL, 1);
174+
175+
if (trace_hw_branches_suspended)
176+
smp_call_function_single(cpu, bts_trace_suspend_cpu,
177+
NULL, 1);
114178
break;
115179
case CPU_DOWN_PREPARE:
116-
smp_call_function_single(cpu, bts_trace_stop_cpu, NULL, 1);
180+
smp_call_function_single(cpu, bts_trace_release_cpu, NULL, 1);
117181
break;
118182
}
119183

@@ -126,20 +190,6 @@ static struct notifier_block bts_hotcpu_notifier __cpuinitdata = {
126190
.notifier_call = bts_hotcpu_handler
127191
};
128192

129-
static int bts_trace_init(struct trace_array *tr)
130-
{
131-
hw_branch_trace = tr;
132-
133-
bts_trace_start(tr);
134-
135-
return 0;
136-
}
137-
138-
static void bts_trace_reset(struct trace_array *tr)
139-
{
140-
bts_trace_stop(tr);
141-
}
142-
143193
static void bts_trace_print_header(struct seq_file *m)
144194
{
145195
seq_puts(m, "# CPU# TO <- FROM\n");
@@ -228,7 +278,7 @@ static void trace_bts_at(const struct bts_trace *trace, void *at)
228278
*/
229279
static void trace_bts_cpu(void *arg)
230280
{
231-
struct trace_array *tr = (struct trace_array *) arg;
281+
struct trace_array *tr = (struct trace_array *)arg;
232282
const struct bts_trace *trace;
233283
unsigned char *at;
234284

@@ -276,7 +326,8 @@ void trace_hw_branch_oops(void)
276326
{
277327
spin_lock(&bts_tracer_lock);
278328

279-
trace_bts_cpu(hw_branch_trace);
329+
if (trace_hw_branches_enabled)
330+
trace_bts_cpu(hw_branch_trace);
280331

281332
spin_unlock(&bts_tracer_lock);
282333
}

0 commit comments

Comments
 (0)