aboutsummaryrefslogtreecommitdiff
path: root/workqueue.c
blob: a92377b7ec3693355d366d670cc6becba8a74d68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
 * Workqueue Analyser
 *
 * Copyright (C) Linaro 2012. All rights reserved.
 *
 * Viresh Kumar <viresh.kumar@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

/*
 * This program create one work per processor, these will run on specific
 * processors only. These have wq_per_cpu() as there fn.
 *
 * These work items per processor will create total_works_per_timer number of
 * works. These can be processed by any CPU, depending on scheduling decisions.
 * These have wq_function() as there fn(). This function simply counts which
 * works got scheduled to which cpu.
 */

#define pr_fmt(fmt) "Workqueue Analyser: " fmt

#include <linux/cpumask.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/percpu.h>
#include <linux/printk.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/workqueue.h>

static unsigned int migrate = 1;
module_param(migrate, uint, S_IRUGO);
MODULE_PARM_DESC(migrate, "wq migration is enabled");
extern int wq_migration;

struct mywork {
	struct work_struct _work;
	int cpu;
} *_myworks;

struct mytimer {
	struct timer_list _timer;
	unsigned int timer_count_left;
	unsigned int timer_start_count;
	unsigned int timer_fn_count;
	unsigned int timer_migration_count;
	int cpu;
};

spinlock_t lock, tlock;
static struct workqueue_struct *gwq;
static struct work_struct *gwork;

static DEFINE_PER_CPU(unsigned int, cpu_work_fn_count);
static DEFINE_PER_CPU(unsigned int, cpu_own_work_count);
static DEFINE_PER_CPU(unsigned int, cpu_migrated_work_count);
static DEFINE_PER_CPU(unsigned int, cpu_work_queued_count);
static DEFINE_PER_CPU(struct workqueue_struct *, cpu_wq);

static int total_works_per_timer = 1000, total_works_finished;

#define TIMER_DELAY 5
static DEFINE_PER_CPU(struct mytimer, _cpu_mytimer);
DECLARE_COMPLETION(wq_complete);

void timer_fn(unsigned long data)
{
	int cpu = raw_smp_processor_id();
	struct mytimer *_mytimer = (struct mytimer *)data;
	struct mytimer *cpu_mytimer = &per_cpu(_cpu_mytimer, cpu);
	struct workqueue_struct **wq = &per_cpu(cpu_wq, cpu);
	unsigned int *work_queued_count = &per_cpu(cpu_work_queued_count, cpu);

	cpu_mytimer->timer_fn_count++;

	if (cpu != _mytimer->cpu)
		cpu_mytimer->timer_migration_count++;

	/* queue work on any cpu */
	queue_work_on_any_cpu(*wq, &_myworks[_mytimer->cpu]._work);
	++(*work_queued_count);
}

extern int not_idle[5], all_idle[5], non_idle_found[5][5];
static void wq_function(struct work_struct *_work)
{
	int cpu = raw_smp_processor_id(), i;
	int cpus = num_present_cpus();
	struct mywork *_mywork = container_of(_work, struct mywork, _work);
	struct mytimer *_mytimer = &per_cpu(_cpu_mytimer, _mywork->cpu);

	/* Find current cpu id */
	per_cpu(cpu_work_fn_count, cpu)++;

	mdelay(20);

	/* work is queued and processed by same cpu */
	if (_mywork->cpu == cpu)
		per_cpu(cpu_own_work_count, cpu)++;
	else
		per_cpu(cpu_migrated_work_count, cpu)++;

	spin_lock(&lock);
	total_works_finished++;
	spin_unlock(&lock);

	if (total_works_finished == cpus * total_works_per_timer) {
		complete(&wq_complete);
		pr_info("\n\nTotal number of works per cpu are: %d\n",
				total_works_per_timer);

		for_each_present_cpu(cpu) {
			pr_info("works processsed by CPU%d, Total: %d, Own: %d, migrated: %d\n", cpu,
					per_cpu(cpu_work_fn_count, cpu),
					per_cpu(cpu_own_work_count, cpu),
					per_cpu(cpu_migrated_work_count, cpu));
		}

		for_each_present_cpu(cpu) {
			pr_info("Timers processed by cpu%d, Started: %d, fn: %d, migrated to it: %d\n",
					cpu, per_cpu(_cpu_mytimer, cpu).timer_start_count,
					per_cpu(_cpu_mytimer, cpu).timer_fn_count,
					per_cpu(_cpu_mytimer, cpu).timer_migration_count);
		}

		for_each_present_cpu(cpu) {
			pr_info("Not idle: %d, all_idle: %d\n", not_idle[cpu],
					all_idle[cpu]);
			for_each_present_cpu(i)
				pr_info("found non idle: src: %d, dest: %d, count: %d\n",
						cpu, i, non_idle_found[cpu][i]);
		}
		return;
	}

	_mytimer->_timer.expires = jiffies + TIMER_DELAY;
	if (--(_mytimer->timer_count_left)) {
		_mytimer->timer_start_count++;
		add_timer_on(&_mytimer->_timer, _mytimer->cpu);
	}
}

static void wq_per_cpu(struct work_struct *_work)
{
	int cpu = raw_smp_processor_id();
	struct workqueue_struct **wq = &per_cpu(cpu_wq, cpu);
	struct mytimer *_mytimer;
	struct timer_list *timer;

	pr_info("%s: cpu%d\n", __func__, cpu);
	*wq = alloc_workqueue("wq-cpu-%d", 0, 0, cpu);
	if (!*wq) {
		pr_err("Failed to allocate workqueue for cpu: %d\n", cpu);
		return;
	}

	_mytimer = &per_cpu(_cpu_mytimer, cpu);
	timer = &_mytimer->_timer;
	init_timer(timer);
	timer->expires = jiffies + TIMER_DELAY;
	timer->data = (unsigned long)_mytimer;
	timer->function = timer_fn;
	_mytimer->timer_count_left = total_works_per_timer;
	_mytimer->timer_start_count = 1;
	_mytimer->timer_fn_count = 0;
	_mytimer->timer_migration_count = 0;
	_mytimer->cpu = cpu;
	add_timer_on(timer, cpu);
}

static void init_globals(void)
{
	int i, j;

	for (i=0; i<5; i++) {
		not_idle[i] = 0;
		all_idle[i] = 0;
		for (j=0; j<5; j++)
			non_idle_found[i][j] = 0;
	}
}

static int __init wq_module_init(void)
{
	int i, cpu, cpus = num_present_cpus();

	spin_lock_init(&lock);
	spin_lock_init(&tlock);

	wq_migration = migrate;
	gwq = alloc_workqueue("wq-init", 0, 0);
	if (!gwq) {
		pr_err("Failed to allocate workqueue\n");
		return 0;
	}

	gwork = kmalloc(cpus * sizeof(*gwork), GFP_KERNEL);
	if (!gwork) {
		pr_err("Failed to allocate work\n");
		return 0;
	}

	_myworks = kmalloc(cpus * sizeof(*_myworks),
			GFP_KERNEL);
	if (!_myworks) {
		pr_err("Failed to allocate work\n");
		return 0;
	}

	for (i = 0; i < cpus; i++) {
		INIT_WORK(&_myworks[i]._work, wq_function);
		_myworks[i].cpu = i;
	}

	init_globals();
	for_each_present_cpu(cpu) {
		INIT_WORK(&gwork[cpu], wq_per_cpu);
		queue_work_on(cpu, gwq, &gwork[cpu]);
	}

	wait_for_completion_interruptible(&wq_complete);
	return 0;
}

static void __exit wq_module_exit(void)
{
	int cpu;
	struct workqueue_struct *wq;
	struct mytimer *_mytimer;

	for_each_present_cpu(cpu) {
		_mytimer = &per_cpu(_cpu_mytimer, cpu);
		del_timer(&_mytimer->_timer);
		wq = per_cpu(cpu_wq, cpu);
		destroy_workqueue(wq);
	}

	kfree(_myworks);
	kfree(gwork);
	destroy_workqueue(gwq);
}

module_init(wq_module_init);
module_exit(wq_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
MODULE_DESCRIPTION("Workqueue Analyser");