ring_buffer_benchmark.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * ring buffer tester and benchmark
 *
 * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com>
 */
#include <linux/ring_buffer.h>
#include <linux/completion.h>
#include <linux/kthread.h>
9
#include <uapi/linux/sched/types.h>
10
#include <linux/module.h>
T
Tina Ruchandani 已提交
11
#include <linux/ktime.h>
12
#include <asm/local.h>
13 14 15 16 17 18 19 20

struct rb_page {
	u64		ts;
	local_t		commit;
	char		data[4080];
};

/* run time and sleep time in seconds */
T
Tina Ruchandani 已提交
21
#define RUN_TIME	10ULL
22 23 24 25 26 27
#define SLEEP_TIME	10

/* number of events for writer to wake up the reader */
static int wakeup_interval = 100;

static int reader_finish;
28 29
static DECLARE_COMPLETION(read_start);
static DECLARE_COMPLETION(read_done);
30 31 32 33 34 35

static struct ring_buffer *buffer;
static struct task_struct *producer;
static struct task_struct *consumer;
static unsigned long read;

36
static unsigned int disable_reader;
37 38 39
module_param(disable_reader, uint, 0644);
MODULE_PARM_DESC(disable_reader, "only run producer");

40
static unsigned int write_iteration = 50;
41 42 43
module_param(write_iteration, uint, 0644);
MODULE_PARM_DESC(write_iteration, "# of writes between timestamp readings");

44 45
static int producer_nice = MAX_NICE;
static int consumer_nice = MAX_NICE;
46 47 48 49

static int producer_fifo = -1;
static int consumer_fifo = -1;

50
module_param(producer_nice, int, 0644);
51 52
MODULE_PARM_DESC(producer_nice, "nice prio for producer");

53
module_param(consumer_nice, int, 0644);
54 55
MODULE_PARM_DESC(consumer_nice, "nice prio for consumer");

56
module_param(producer_fifo, int, 0644);
57 58
MODULE_PARM_DESC(producer_fifo, "fifo prio for producer");

59
module_param(consumer_fifo, int, 0644);
60 61
MODULE_PARM_DESC(consumer_fifo, "fifo prio for consumer");

62 63
static int read_events;

64
static int test_error;
65

66
#define TEST_ERROR()				\
67
	do {					\
68 69
		if (!test_error) {		\
			test_error = 1;		\
70 71 72 73 74 75 76 77 78
			WARN_ON(1);		\
		}				\
	} while (0)

enum event_status {
	EVENT_FOUND,
	EVENT_DROPPED,
};

79 80 81 82 83
static bool break_test(void)
{
	return test_error || kthread_should_stop();
}

84 85 86 87 88 89
static enum event_status read_event(int cpu)
{
	struct ring_buffer_event *event;
	int *entry;
	u64 ts;

90
	event = ring_buffer_consume(buffer, cpu, &ts, NULL);
91 92 93 94 95
	if (!event)
		return EVENT_DROPPED;

	entry = ring_buffer_event_data(event);
	if (*entry != cpu) {
96
		TEST_ERROR();
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		return EVENT_DROPPED;
	}

	read++;
	return EVENT_FOUND;
}

static enum event_status read_page(int cpu)
{
	struct ring_buffer_event *event;
	struct rb_page *rpage;
	unsigned long commit;
	void *bpage;
	int *entry;
	int ret;
	int inc;
	int i;

115
	bpage = ring_buffer_alloc_read_page(buffer, cpu);
116 117 118
	if (!bpage)
		return EVENT_DROPPED;

119 120 121
	ret = ring_buffer_read_page(buffer, &bpage, PAGE_SIZE, cpu, 1);
	if (ret >= 0) {
		rpage = bpage;
122 123
		/* The commit may have missed event flags set, clear them */
		commit = local_read(&rpage->commit) & 0xfffff;
124
		for (i = 0; i < commit && !test_error ; i += inc) {
125 126

			if (i >= (PAGE_SIZE - offsetof(struct rb_page, data))) {
127
				TEST_ERROR();
128 129 130 131 132 133 134
				break;
			}

			inc = -1;
			event = (void *)&rpage->data[i];
			switch (event->type_len) {
			case RINGBUF_TYPE_PADDING:
135 136
				/* failed writes may be discarded events */
				if (!event->time_delta)
137
					TEST_ERROR();
138
				inc = event->array[0] + 4;
139 140 141 142 143 144 145
				break;
			case RINGBUF_TYPE_TIME_EXTEND:
				inc = 8;
				break;
			case 0:
				entry = ring_buffer_event_data(event);
				if (*entry != cpu) {
146
					TEST_ERROR();
147 148 149 150
					break;
				}
				read++;
				if (!event->array[0]) {
151
					TEST_ERROR();
152 153
					break;
				}
154
				inc = event->array[0] + 4;
155 156 157 158
				break;
			default:
				entry = ring_buffer_event_data(event);
				if (*entry != cpu) {
159
					TEST_ERROR();
160 161 162 163 164
					break;
				}
				read++;
				inc = ((event->type_len + 1) * 4);
			}
165
			if (test_error)
166 167 168
				break;

			if (inc <= 0) {
169
				TEST_ERROR();
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
				break;
			}
		}
	}
	ring_buffer_free_read_page(buffer, bpage);

	if (ret < 0)
		return EVENT_DROPPED;
	return EVENT_FOUND;
}

static void ring_buffer_consumer(void)
{
	/* toggle between reading pages and events */
	read_events ^= 1;

	read = 0;
187 188 189 190 191 192
	/*
	 * Continue running until the producer specifically asks to stop
	 * and is ready for the completion.
	 */
	while (!READ_ONCE(reader_finish)) {
		int found = 1;
193

194
		while (found && !test_error) {
195 196 197 198 199 200 201 202 203 204 205
			int cpu;

			found = 0;
			for_each_online_cpu(cpu) {
				enum event_status stat;

				if (read_events)
					stat = read_event(cpu);
				else
					stat = read_page(cpu);

206
				if (test_error)
207
					break;
208

209 210
				if (stat == EVENT_FOUND)
					found = 1;
211

212
			}
213
		}
214

215 216 217
		/* Wait till the producer wakes us up when there is more data
		 * available or when the producer wants us to finish reading.
		 */
218 219 220 221 222 223
		set_current_state(TASK_INTERRUPTIBLE);
		if (reader_finish)
			break;

		schedule();
	}
224
	__set_current_state(TASK_RUNNING);
225 226 227 228 229 230
	reader_finish = 0;
	complete(&read_done);
}

static void ring_buffer_producer(void)
{
T
Tina Ruchandani 已提交
231
	ktime_t start_time, end_time, timeout;
232 233 234 235 236 237 238 239 240 241 242 243
	unsigned long long time;
	unsigned long long entries;
	unsigned long long overruns;
	unsigned long missed = 0;
	unsigned long hit = 0;
	unsigned long avg;
	int cnt = 0;

	/*
	 * Hammer the buffer for 10 secs (this may
	 * make the system stall)
	 */
244
	trace_printk("Starting ring buffer hammer\n");
T
Tina Ruchandani 已提交
245 246
	start_time = ktime_get();
	timeout = ktime_add_ns(start_time, RUN_TIME * NSEC_PER_SEC);
247 248 249
	do {
		struct ring_buffer_event *event;
		int *entry;
250 251 252 253 254 255 256 257 258 259 260 261
		int i;

		for (i = 0; i < write_iteration; i++) {
			event = ring_buffer_lock_reserve(buffer, 10);
			if (!event) {
				missed++;
			} else {
				hit++;
				entry = ring_buffer_event_data(event);
				*entry = smp_processor_id();
				ring_buffer_unlock_commit(buffer, event);
			}
262
		}
T
Tina Ruchandani 已提交
263
		end_time = ktime_get();
264

265 266
		cnt++;
		if (consumer && !(cnt % wakeup_interval))
267 268
			wake_up_process(consumer);

269
#ifndef CONFIG_PREEMPT
270 271 272 273 274
		/*
		 * If we are a non preempt kernel, the 10 second run will
		 * stop everything while it runs. Instead, we will call
		 * cond_resched and also add any time that was lost by a
		 * rescedule.
275 276 277
		 *
		 * Do a cond resched at the same frequency we would wake up
		 * the reader.
278
		 */
279 280 281
		if (cnt % wakeup_interval)
			cond_resched();
#endif
282
	} while (ktime_before(end_time, timeout) && !break_test());
283
	trace_printk("End ring buffer hammer\n");
284 285 286 287 288 289 290 291 292 293 294 295

	if (consumer) {
		/* Init both completions here to avoid races */
		init_completion(&read_start);
		init_completion(&read_done);
		/* the completions must be visible before the finish var */
		smp_wmb();
		reader_finish = 1;
		wake_up_process(consumer);
		wait_for_completion(&read_done);
	}

T
Tina Ruchandani 已提交
296
	time = ktime_us_delta(end_time, start_time);
297 298 299 300

	entries = ring_buffer_entries(buffer);
	overruns = ring_buffer_overruns(buffer);

301
	if (test_error)
302
		trace_printk("ERROR!\n");
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

	if (!disable_reader) {
		if (consumer_fifo < 0)
			trace_printk("Running Consumer at nice: %d\n",
				     consumer_nice);
		else
			trace_printk("Running Consumer at SCHED_FIFO %d\n",
				     consumer_fifo);
	}
	if (producer_fifo < 0)
		trace_printk("Running Producer at nice: %d\n",
			     producer_nice);
	else
		trace_printk("Running Producer at SCHED_FIFO %d\n",
			     producer_fifo);

	/* Let the user know that the test is running at low priority */
	if (producer_fifo < 0 && consumer_fifo < 0 &&
321
	    producer_nice == MAX_NICE && consumer_nice == MAX_NICE)
322 323
		trace_printk("WARNING!!! This test is running at lowest priority.\n");

324 325
	trace_printk("Time:     %lld (usecs)\n", time);
	trace_printk("Overruns: %lld\n", overruns);
326
	if (disable_reader)
327
		trace_printk("Read:     (reader disabled)\n");
328
	else
329
		trace_printk("Read:     %ld  (by %s)\n", read,
330
			read_events ? "events" : "pages");
331 332 333 334
	trace_printk("Entries:  %lld\n", entries);
	trace_printk("Total:    %lld\n", entries + overruns + read);
	trace_printk("Missed:   %ld\n", missed);
	trace_printk("Hit:      %ld\n", hit);
335

336 337
	/* Convert time from usecs to millisecs */
	do_div(time, USEC_PER_MSEC);
338 339 340
	if (time)
		hit /= (long)time;
	else
341
		trace_printk("TIME IS ZERO??\n");
342

343
	trace_printk("Entries per millisec: %ld\n", hit);
344 345

	if (hit) {
346 347
		/* Calculate the average time in nanosecs */
		avg = NSEC_PER_MSEC / hit;
348
		trace_printk("%ld ns per entry\n", avg);
349
	}
350 351 352 353 354

	if (missed) {
		if (time)
			missed /= (long)time;

355 356
		trace_printk("Total iterations per millisec: %ld\n",
			     hit + missed);
357

358 359
		/* it is possible that hit + missed will overflow and be zero */
		if (!(hit + missed)) {
360
			trace_printk("hit + missed overflowed and totalled zero!\n");
361 362 363
			hit--; /* make it non zero */
		}

364 365
		/* Caculate the average time in nanosecs */
		avg = NSEC_PER_MSEC / (hit + missed);
366
		trace_printk("%ld ns per entry\n", avg);
367
	}
368 369 370 371 372 373 374 375 376 377 378 379 380 381
}

static void wait_to_die(void)
{
	set_current_state(TASK_INTERRUPTIBLE);
	while (!kthread_should_stop()) {
		schedule();
		set_current_state(TASK_INTERRUPTIBLE);
	}
	__set_current_state(TASK_RUNNING);
}

static int ring_buffer_consumer_thread(void *arg)
{
382
	while (!break_test()) {
383 384 385 386 387
		complete(&read_start);

		ring_buffer_consumer();

		set_current_state(TASK_INTERRUPTIBLE);
388
		if (break_test())
389 390 391 392 393
			break;
		schedule();
	}
	__set_current_state(TASK_RUNNING);

394
	if (!kthread_should_stop())
395 396 397 398 399 400 401
		wait_to_die();

	return 0;
}

static int ring_buffer_producer_thread(void *arg)
{
402
	while (!break_test()) {
403 404 405 406 407 408 409 410
		ring_buffer_reset(buffer);

		if (consumer) {
			wake_up_process(consumer);
			wait_for_completion(&read_start);
		}

		ring_buffer_producer();
411
		if (break_test())
412
			goto out_kill;
413

414
		trace_printk("Sleeping for 10 secs\n");
415
		set_current_state(TASK_INTERRUPTIBLE);
416 417
		if (break_test())
			goto out_kill;
418 419 420
		schedule_timeout(HZ * SLEEP_TIME);
	}

421
out_kill:
422
	__set_current_state(TASK_RUNNING);
423
	if (!kthread_should_stop())
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
		wait_to_die();

	return 0;
}

static int __init ring_buffer_benchmark_init(void)
{
	int ret;

	/* make a one meg buffer in overwite mode */
	buffer = ring_buffer_alloc(1000000, RB_FL_OVERWRITE);
	if (!buffer)
		return -ENOMEM;

	if (!disable_reader) {
		consumer = kthread_create(ring_buffer_consumer_thread,
					  NULL, "rb_consumer");
		ret = PTR_ERR(consumer);
		if (IS_ERR(consumer))
			goto out_fail;
	}

	producer = kthread_run(ring_buffer_producer_thread,
			       NULL, "rb_producer");
	ret = PTR_ERR(producer);

	if (IS_ERR(producer))
		goto out_kill;

453 454 455
	/*
	 * Run them as low-prio background tasks by default:
	 */
456 457 458 459 460 461 462 463 464 465 466 467
	if (!disable_reader) {
		if (consumer_fifo >= 0) {
			struct sched_param param = {
				.sched_priority = consumer_fifo
			};
			sched_setscheduler(consumer, SCHED_FIFO, &param);
		} else
			set_user_nice(consumer, consumer_nice);
	}

	if (producer_fifo >= 0) {
		struct sched_param param = {
468
			.sched_priority = producer_fifo
469 470 471 472
		};
		sched_setscheduler(producer, SCHED_FIFO, &param);
	} else
		set_user_nice(producer, producer_nice);
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	return 0;

 out_kill:
	if (consumer)
		kthread_stop(consumer);

 out_fail:
	ring_buffer_free(buffer);
	return ret;
}

static void __exit ring_buffer_benchmark_exit(void)
{
	kthread_stop(producer);
	if (consumer)
		kthread_stop(consumer);
	ring_buffer_free(buffer);
}

module_init(ring_buffer_benchmark_init);
module_exit(ring_buffer_benchmark_exit);

MODULE_AUTHOR("Steven Rostedt");
MODULE_DESCRIPTION("ring_buffer_benchmark");
MODULE_LICENSE("GPL");