trace_selftest.c 13.6 KB
Newer Older
S
Steven Rostedt 已提交
1 2 3
/* Include in trace.c */

#include <linux/kthread.h>
I
Ingo Molnar 已提交
4
#include <linux/delay.h>
S
Steven Rostedt 已提交
5

I
Ingo Molnar 已提交
6
static inline int trace_valid_entry(struct trace_entry *entry)
S
Steven Rostedt 已提交
7 8 9 10
{
	switch (entry->type) {
	case TRACE_FN:
	case TRACE_CTX:
11
	case TRACE_WAKE:
12
	case TRACE_STACK:
13
	case TRACE_PRINT:
14
	case TRACE_SPECIAL:
S
Steven Rostedt 已提交
15
	case TRACE_BRANCH:
16 17
	case TRACE_GRAPH_ENT:
	case TRACE_GRAPH_RET:
S
Steven Rostedt 已提交
18 19 20 21 22
		return 1;
	}
	return 0;
}

23
static int trace_test_buffer_cpu(struct trace_array *tr, int cpu)
S
Steven Rostedt 已提交
24
{
25 26
	struct ring_buffer_event *event;
	struct trace_entry *entry;
S
Steven Rostedt 已提交
27

28 29
	while ((event = ring_buffer_consume(tr->buffer, cpu, NULL))) {
		entry = ring_buffer_event_data(event);
S
Steven Rostedt 已提交
30

31
		if (!trace_valid_entry(entry)) {
I
Ingo Molnar 已提交
32
			printk(KERN_CONT ".. invalid entry %d ",
33
				entry->type);
S
Steven Rostedt 已提交
34 35 36 37 38 39
			goto failed;
		}
	}
	return 0;

 failed:
40 41
	/* disable tracing */
	tracing_disabled = 1;
S
Steven Rostedt 已提交
42 43 44 45 46 47 48 49 50 51
	printk(KERN_CONT ".. corrupted trace buffer .. ");
	return -1;
}

/*
 * Test the trace buffer to see if all the elements
 * are still sane.
 */
static int trace_test_buffer(struct trace_array *tr, unsigned long *count)
{
52 53
	unsigned long flags, cnt = 0;
	int cpu, ret = 0;
S
Steven Rostedt 已提交
54

55
	/* Don't allow flipping of max traces now */
56
	local_irq_save(flags);
57
	__raw_spin_lock(&ftrace_max_lock);
S
Steven Rostedt 已提交
58

59
	cnt = ring_buffer_entries(tr->buffer);
S
Steven Rostedt 已提交
60

61 62
	for_each_possible_cpu(cpu) {
		ret = trace_test_buffer_cpu(tr, cpu);
S
Steven Rostedt 已提交
63 64 65
		if (ret)
			break;
	}
66
	__raw_spin_unlock(&ftrace_max_lock);
67
	local_irq_restore(flags);
S
Steven Rostedt 已提交
68 69 70 71 72 73 74

	if (count)
		*count = cnt;

	return ret;
}

75 76 77 78 79
static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
{
	printk(KERN_WARNING "Failed to init %s tracer, init returned %d\n",
		trace->name, init_ret);
}
80
#ifdef CONFIG_FUNCTION_TRACER
81 82 83 84 85 86 87 88 89 90 91 92 93

#ifdef CONFIG_DYNAMIC_FTRACE

#define __STR(x) #x
#define STR(x) __STR(x)

/* Test dynamic code modification and ftrace filters */
int trace_selftest_startup_dynamic_tracing(struct tracer *trace,
					   struct trace_array *tr,
					   int (*func)(void))
{
	int save_ftrace_enabled = ftrace_enabled;
	int save_tracer_enabled = tracer_enabled;
94
	unsigned long count;
S
Steven Rostedt 已提交
95
	char *func_name;
96
	int ret;
97 98 99 100 101 102 103 104 105 106 107 108

	/* The ftrace test PASSED */
	printk(KERN_CONT "PASSED\n");
	pr_info("Testing dynamic ftrace: ");

	/* enable tracing, and record the filter function */
	ftrace_enabled = 1;
	tracer_enabled = 1;

	/* passed in by parameter to fool gcc from optimizing */
	func();

S
Steven Rostedt 已提交
109 110 111 112 113 114 115
	/*
	 * Some archs *cough*PowerPC*cough* add charachters to the
	 * start of the function names. We simply put a '*' to
	 * accomodate them.
	 */
	func_name = "*" STR(DYN_FTRACE_TEST_NAME);

116
	/* filter only on our function */
S
Steven Rostedt 已提交
117
	ftrace_set_filter(func_name, strlen(func_name), 1);
118 119

	/* enable tracing */
120
	ret = tracer_init(trace, tr);
121 122 123 124
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		goto out;
	}
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	/* Sleep for a 1/10 of a second */
	msleep(100);

	/* we should have nothing in the buffer */
	ret = trace_test_buffer(tr, &count);
	if (ret)
		goto out;

	if (count) {
		ret = -1;
		printk(KERN_CONT ".. filter did not filter .. ");
		goto out;
	}

	/* call our function again */
	func();

	/* sleep again */
	msleep(100);

	/* stop the tracing. */
S
Steven Rostedt 已提交
147
	tracing_stop();
148 149 150 151 152
	ftrace_enabled = 0;

	/* check the trace buffer */
	ret = trace_test_buffer(tr, &count);
	trace->reset(tr);
S
Steven Rostedt 已提交
153
	tracing_start();
154 155 156

	/* we should only have one item */
	if (!ret && count != 1) {
157
		printk(KERN_CONT ".. filter failed count=%ld ..", count);
158 159 160
		ret = -1;
		goto out;
	}
S
Steven Rostedt 已提交
161

162 163 164 165 166 167 168 169 170 171 172 173
 out:
	ftrace_enabled = save_ftrace_enabled;
	tracer_enabled = save_tracer_enabled;

	/* Enable tracing on all functions again */
	ftrace_set_filter(NULL, 0, 1);

	return ret;
}
#else
# define trace_selftest_startup_dynamic_tracing(trace, tr, func) ({ 0; })
#endif /* CONFIG_DYNAMIC_FTRACE */
S
Steven Rostedt 已提交
174 175 176 177 178 179 180 181
/*
 * Simple verification test of ftrace function tracer.
 * Enable ftrace, sleep 1/10 second, and then read the trace
 * buffer to see if all is in order.
 */
int
trace_selftest_startup_function(struct tracer *trace, struct trace_array *tr)
{
182 183
	int save_ftrace_enabled = ftrace_enabled;
	int save_tracer_enabled = tracer_enabled;
184 185
	unsigned long count;
	int ret;
S
Steven Rostedt 已提交
186

187 188 189
	/* make sure msleep has been recorded */
	msleep(1);

S
Steven Rostedt 已提交
190
	/* start the tracing */
I
Ingo Molnar 已提交
191
	ftrace_enabled = 1;
192
	tracer_enabled = 1;
I
Ingo Molnar 已提交
193

194
	ret = tracer_init(trace, tr);
195 196 197 198 199
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		goto out;
	}

S
Steven Rostedt 已提交
200 201 202
	/* Sleep for a 1/10 of a second */
	msleep(100);
	/* stop the tracing. */
S
Steven Rostedt 已提交
203
	tracing_stop();
I
Ingo Molnar 已提交
204 205
	ftrace_enabled = 0;

S
Steven Rostedt 已提交
206 207 208
	/* check the trace buffer */
	ret = trace_test_buffer(tr, &count);
	trace->reset(tr);
S
Steven Rostedt 已提交
209
	tracing_start();
S
Steven Rostedt 已提交
210 211 212 213

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
214
		goto out;
S
Steven Rostedt 已提交
215 216
	}

217 218 219 220 221 222 223
	ret = trace_selftest_startup_dynamic_tracing(trace, tr,
						     DYN_FTRACE_TEST_NAME);

 out:
	ftrace_enabled = save_ftrace_enabled;
	tracer_enabled = save_tracer_enabled;

224 225 226 227
	/* kill ftrace totally if we failed */
	if (ret)
		ftrace_kill();

S
Steven Rostedt 已提交
228 229
	return ret;
}
230
#endif /* CONFIG_FUNCTION_TRACER */
S
Steven Rostedt 已提交
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

#ifdef CONFIG_FUNCTION_GRAPH_TRACER
/*
 * Pretty much the same than for the function tracer from which the selftest
 * has been borrowed.
 */
int
trace_selftest_startup_function_graph(struct tracer *trace,
					struct trace_array *tr)
{
	int ret;
	unsigned long count;

	ret = tracer_init(trace, tr);
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		goto out;
	}

	/* Sleep for a 1/10 of a second */
	msleep(100);

	tracing_stop();

	/* check the trace buffer */
	ret = trace_test_buffer(tr, &count);

	trace->reset(tr);
	tracing_start();

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
		goto out;
	}

	/* Don't test dynamic tracing, the function tracer already did */

out:
	/* Stop it if we failed */
	if (ret)
		ftrace_graph_stop();

	return ret;
}
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */


S
Steven Rostedt 已提交
280 281 282 283 284 285 286 287 288
#ifdef CONFIG_IRQSOFF_TRACER
int
trace_selftest_startup_irqsoff(struct tracer *trace, struct trace_array *tr)
{
	unsigned long save_max = tracing_max_latency;
	unsigned long count;
	int ret;

	/* start the tracing */
289
	ret = tracer_init(trace, tr);
290 291 292 293 294
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		return ret;
	}

S
Steven Rostedt 已提交
295 296 297 298 299 300 301
	/* reset the max latency */
	tracing_max_latency = 0;
	/* disable interrupts for a bit */
	local_irq_disable();
	udelay(100);
	local_irq_enable();
	/* stop the tracing. */
S
Steven Rostedt 已提交
302
	tracing_stop();
S
Steven Rostedt 已提交
303 304 305 306 307
	/* check both trace buffers */
	ret = trace_test_buffer(tr, NULL);
	if (!ret)
		ret = trace_test_buffer(&max_tr, &count);
	trace->reset(tr);
S
Steven Rostedt 已提交
308
	tracing_start();
S
Steven Rostedt 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
	}

	tracing_max_latency = save_max;

	return ret;
}
#endif /* CONFIG_IRQSOFF_TRACER */

#ifdef CONFIG_PREEMPT_TRACER
int
trace_selftest_startup_preemptoff(struct tracer *trace, struct trace_array *tr)
{
	unsigned long save_max = tracing_max_latency;
	unsigned long count;
	int ret;

329 330 331 332 333 334 335 336 337 338 339 340 341
	/*
	 * Now that the big kernel lock is no longer preemptable,
	 * and this is called with the BKL held, it will always
	 * fail. If preemption is already disabled, simply
	 * pass the test. When the BKL is removed, or becomes
	 * preemptible again, we will once again test this,
	 * so keep it in.
	 */
	if (preempt_count()) {
		printk(KERN_CONT "can not test ... force ");
		return 0;
	}

S
Steven Rostedt 已提交
342
	/* start the tracing */
343
	ret = tracer_init(trace, tr);
344 345 346 347 348
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		return ret;
	}

S
Steven Rostedt 已提交
349 350 351 352 353 354 355
	/* reset the max latency */
	tracing_max_latency = 0;
	/* disable preemption for a bit */
	preempt_disable();
	udelay(100);
	preempt_enable();
	/* stop the tracing. */
S
Steven Rostedt 已提交
356
	tracing_stop();
S
Steven Rostedt 已提交
357 358 359 360 361
	/* check both trace buffers */
	ret = trace_test_buffer(tr, NULL);
	if (!ret)
		ret = trace_test_buffer(&max_tr, &count);
	trace->reset(tr);
S
Steven Rostedt 已提交
362
	tracing_start();
S
Steven Rostedt 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
	}

	tracing_max_latency = save_max;

	return ret;
}
#endif /* CONFIG_PREEMPT_TRACER */

#if defined(CONFIG_IRQSOFF_TRACER) && defined(CONFIG_PREEMPT_TRACER)
int
trace_selftest_startup_preemptirqsoff(struct tracer *trace, struct trace_array *tr)
{
	unsigned long save_max = tracing_max_latency;
	unsigned long count;
	int ret;

383 384 385 386 387 388 389 390 391 392 393 394 395
	/*
	 * Now that the big kernel lock is no longer preemptable,
	 * and this is called with the BKL held, it will always
	 * fail. If preemption is already disabled, simply
	 * pass the test. When the BKL is removed, or becomes
	 * preemptible again, we will once again test this,
	 * so keep it in.
	 */
	if (preempt_count()) {
		printk(KERN_CONT "can not test ... force ");
		return 0;
	}

S
Steven Rostedt 已提交
396
	/* start the tracing */
397
	ret = tracer_init(trace, tr);
398 399 400 401
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		goto out;
	}
S
Steven Rostedt 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414

	/* reset the max latency */
	tracing_max_latency = 0;

	/* disable preemption and interrupts for a bit */
	preempt_disable();
	local_irq_disable();
	udelay(100);
	preempt_enable();
	/* reverse the order of preempt vs irqs */
	local_irq_enable();

	/* stop the tracing. */
S
Steven Rostedt 已提交
415
	tracing_stop();
S
Steven Rostedt 已提交
416 417
	/* check both trace buffers */
	ret = trace_test_buffer(tr, NULL);
S
Steven Rostedt 已提交
418 419
	if (ret) {
		tracing_start();
S
Steven Rostedt 已提交
420
		goto out;
S
Steven Rostedt 已提交
421
	}
S
Steven Rostedt 已提交
422 423

	ret = trace_test_buffer(&max_tr, &count);
S
Steven Rostedt 已提交
424 425
	if (ret) {
		tracing_start();
S
Steven Rostedt 已提交
426
		goto out;
S
Steven Rostedt 已提交
427
	}
S
Steven Rostedt 已提交
428 429 430 431

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
S
Steven Rostedt 已提交
432
		tracing_start();
S
Steven Rostedt 已提交
433 434 435 436 437
		goto out;
	}

	/* do the test by disabling interrupts first this time */
	tracing_max_latency = 0;
S
Steven Rostedt 已提交
438
	tracing_start();
S
Steven Rostedt 已提交
439 440 441 442 443 444 445 446
	preempt_disable();
	local_irq_disable();
	udelay(100);
	preempt_enable();
	/* reverse the order of preempt vs irqs */
	local_irq_enable();

	/* stop the tracing. */
S
Steven Rostedt 已提交
447
	tracing_stop();
S
Steven Rostedt 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	/* check both trace buffers */
	ret = trace_test_buffer(tr, NULL);
	if (ret)
		goto out;

	ret = trace_test_buffer(&max_tr, &count);

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
		goto out;
	}

 out:
	trace->reset(tr);
S
Steven Rostedt 已提交
463
	tracing_start();
S
Steven Rostedt 已提交
464 465 466 467 468 469
	tracing_max_latency = save_max;

	return ret;
}
#endif /* CONFIG_IRQSOFF_TRACER && CONFIG_PREEMPT_TRACER */

S
Steven Noonan 已提交
470 471 472 473 474 475 476 477 478
#ifdef CONFIG_NOP_TRACER
int
trace_selftest_startup_nop(struct tracer *trace, struct trace_array *tr)
{
	/* What could possibly go wrong? */
	return 0;
}
#endif

S
Steven Rostedt 已提交
479 480 481 482
#ifdef CONFIG_SCHED_TRACER
static int trace_wakeup_test_thread(void *data)
{
	/* Make this a RT thread, doesn't need to be too high */
483 484
	struct sched_param param = { .sched_priority = 5 };
	struct completion *x = data;
S
Steven Rostedt 已提交
485

486
	sched_setscheduler(current, SCHED_FIFO, &param);
S
Steven Rostedt 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

	/* Make it know we have a new prio */
	complete(x);

	/* now go to sleep and let the test wake us up */
	set_current_state(TASK_INTERRUPTIBLE);
	schedule();

	/* we are awake, now wait to disappear */
	while (!kthread_should_stop()) {
		/*
		 * This is an RT task, do short sleeps to let
		 * others run.
		 */
		msleep(100);
	}

	return 0;
}

int
trace_selftest_startup_wakeup(struct tracer *trace, struct trace_array *tr)
{
	unsigned long save_max = tracing_max_latency;
	struct task_struct *p;
	struct completion isrt;
	unsigned long count;
	int ret;

	init_completion(&isrt);

	/* create a high prio thread */
	p = kthread_run(trace_wakeup_test_thread, &isrt, "ftrace-test");
I
Ingo Molnar 已提交
520
	if (IS_ERR(p)) {
S
Steven Rostedt 已提交
521 522 523 524 525 526 527 528
		printk(KERN_CONT "Failed to create ftrace wakeup test thread ");
		return -1;
	}

	/* make sure the thread is running at an RT prio */
	wait_for_completion(&isrt);

	/* start the tracing */
529
	ret = tracer_init(trace, tr);
530 531 532 533 534
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		return ret;
	}

S
Steven Rostedt 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
	/* reset the max latency */
	tracing_max_latency = 0;

	/* sleep to let the RT thread sleep too */
	msleep(100);

	/*
	 * Yes this is slightly racy. It is possible that for some
	 * strange reason that the RT thread we created, did not
	 * call schedule for 100ms after doing the completion,
	 * and we do a wakeup on a task that already is awake.
	 * But that is extremely unlikely, and the worst thing that
	 * happens in such a case, is that we disable tracing.
	 * Honestly, if this race does happen something is horrible
	 * wrong with the system.
	 */

	wake_up_process(p);

554 555 556
	/* give a little time to let the thread wake up */
	msleep(100);

S
Steven Rostedt 已提交
557
	/* stop the tracing. */
S
Steven Rostedt 已提交
558
	tracing_stop();
S
Steven Rostedt 已提交
559 560 561 562 563 564 565
	/* check both trace buffers */
	ret = trace_test_buffer(tr, NULL);
	if (!ret)
		ret = trace_test_buffer(&max_tr, &count);


	trace->reset(tr);
S
Steven Rostedt 已提交
566
	tracing_start();
S
Steven Rostedt 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589

	tracing_max_latency = save_max;

	/* kill the thread */
	kthread_stop(p);

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
	}

	return ret;
}
#endif /* CONFIG_SCHED_TRACER */

#ifdef CONFIG_CONTEXT_SWITCH_TRACER
int
trace_selftest_startup_sched_switch(struct tracer *trace, struct trace_array *tr)
{
	unsigned long count;
	int ret;

	/* start the tracing */
590
	ret = tracer_init(trace, tr);
591 592 593 594 595
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		return ret;
	}

S
Steven Rostedt 已提交
596 597 598
	/* Sleep for a 1/10 of a second */
	msleep(100);
	/* stop the tracing. */
S
Steven Rostedt 已提交
599
	tracing_stop();
S
Steven Rostedt 已提交
600 601 602
	/* check the trace buffer */
	ret = trace_test_buffer(tr, &count);
	trace->reset(tr);
S
Steven Rostedt 已提交
603
	tracing_start();
S
Steven Rostedt 已提交
604 605 606 607 608 609 610 611 612

	if (!ret && !count) {
		printk(KERN_CONT ".. no entries found ..");
		ret = -1;
	}

	return ret;
}
#endif /* CONFIG_CONTEXT_SWITCH_TRACER */
613 614 615 616 617 618 619 620 621

#ifdef CONFIG_SYSPROF_TRACER
int
trace_selftest_startup_sysprof(struct tracer *trace, struct trace_array *tr)
{
	unsigned long count;
	int ret;

	/* start the tracing */
622
	ret = tracer_init(trace, tr);
623 624 625 626 627
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		return 0;
	}

628 629 630
	/* Sleep for a 1/10 of a second */
	msleep(100);
	/* stop the tracing. */
S
Steven Rostedt 已提交
631
	tracing_stop();
632 633 634
	/* check the trace buffer */
	ret = trace_test_buffer(tr, &count);
	trace->reset(tr);
S
Steven Rostedt 已提交
635
	tracing_start();
636 637 638 639

	return ret;
}
#endif /* CONFIG_SYSPROF_TRACER */
S
Steven Rostedt 已提交
640 641 642 643 644 645 646 647 648

#ifdef CONFIG_BRANCH_TRACER
int
trace_selftest_startup_branch(struct tracer *trace, struct trace_array *tr)
{
	unsigned long count;
	int ret;

	/* start the tracing */
649
	ret = tracer_init(trace, tr);
650 651 652 653 654
	if (ret) {
		warn_failed_init_tracer(trace, ret);
		return ret;
	}

S
Steven Rostedt 已提交
655 656 657 658 659 660 661 662 663 664 665 666
	/* Sleep for a 1/10 of a second */
	msleep(100);
	/* stop the tracing. */
	tracing_stop();
	/* check the trace buffer */
	ret = trace_test_buffer(tr, &count);
	trace->reset(tr);
	tracing_start();

	return ret;
}
#endif /* CONFIG_BRANCH_TRACER */