aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/slow-work-debugfs.c
blob: e45c436452983fcb44a82925b1ac3574532cf405 (plain) (blame)
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
/* Slow work debugging
 *
 * Copyright (C) 2009 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/slow-work.h>
#include <linux/fs.h>
#include <linux/time.h>
#include <linux/seq_file.h>
#include "slow-work.h"

#define ITERATOR_SHIFT		(BITS_PER_LONG - 4)
#define ITERATOR_SELECTOR	(0xfUL << ITERATOR_SHIFT)
#define ITERATOR_COUNTER	(~ITERATOR_SELECTOR)

void slow_work_new_thread_desc(struct slow_work *work, struct seq_file *m)
{
	seq_puts(m, "Slow-work: New thread");
}

/*
 * Render the time mark field on a work item into a 5-char time with units plus
 * a space
 */
static void slow_work_print_mark(struct seq_file *m, struct slow_work *work)
{
	struct timespec now, diff;

	now = CURRENT_TIME;
	diff = timespec_sub(now, work->mark);

	if (diff.tv_sec < 0)
		seq_puts(m, "  -ve ");
	else if (diff.tv_sec == 0 && diff.tv_nsec < 1000)
		seq_printf(m, "%3luns ", diff.tv_nsec);
	else if (diff.tv_sec == 0 && diff.tv_nsec < 1000000)
		seq_printf(m, "%3luus ", diff.tv_nsec / 1000);
	else if (diff.tv_sec == 0 && diff.tv_nsec < 1000000000)
		seq_printf(m, "%3lums ", diff.tv_nsec / 1000000);
	else if (diff.tv_sec <= 1)
		seq_puts(m, "   1s ");
	else if (diff.tv_sec < 60)
		seq_printf(m, "%4lus ", diff.tv_sec);
	else if (diff.tv_sec < 60 * 60)
		seq_printf(m, "%4lum ", diff.tv_sec / 60);
	else if (diff.tv_sec < 60 * 60 * 24)
		seq_printf(m, "%4luh ", diff.tv_sec / 3600);
	else
		seq_puts(m, "exces ");
}

/*
 * Describe a slow work item for debugfs
 */
static int slow_work_runqueue_show(struct seq_file *m, void *v)
{
	struct slow_work *work;
	struct list_head *p = v;
	unsigned long id;

	switch ((unsigned long) v) {
	case 1:
		seq_puts(m, "THR PID   ITEM ADDR        FL MARK  DESC\n");
		return 0;
	case 2:
		seq_puts(m, "=== ===== ================ == ===== ==========\n");
		return 0;

	case 3 ... 3 + SLOW_WORK_THREAD_LIMIT - 1:
		id = (unsigned long) v - 3;

		read_lock(&slow_work_execs_lock);
		work = slow_work_execs[id];
		if (work) {
			smp_read_barrier_depends();

			seq_printf(m, "%3lu %5d %16p %2lx ",
				   id, slow_work_pids[id], work, work->flags);
			slow_work_print_mark(m, work);

			if (work->ops->desc)
				work->ops->desc(work, m);
			seq_putc(m, '\n');
		}
		read_unlock(&slow_work_execs_lock);
		return 0;

	default:
		work = list_entry(p, struct slow_work, link);
		seq_printf(m, "%3s     - %16p %2lx ",
			   work->flags & SLOW_WORK_VERY_SLOW ? "vsq" : "sq",
			   work, work->flags);
		slow_work_print_mark(m, work);

		if (work->ops->desc)
			work->ops->desc(work, m);
		seq_putc(m, '\n');
		return 0;
	}
}

/*
 * map the iterator to a work item
 */
static void *slow_work_runqueue_index(struct seq_file *m, loff_t *_pos)
{
	struct list_head *p;
	unsigned long count, id;

	switch (*_pos >> ITERATOR_SHIFT) {
	case 0x0:
		if (*_pos == 0)
			*_pos = 1;
		if (*_pos < 3)
			return (void *)(unsigned long) *_pos;
		if (*_pos < 3 + SLOW_WORK_THREAD_LIMIT)
			for (id = *_pos - 3;
			     id < SLOW_WORK_THREAD_LIMIT;
			     id++, (*_pos)++)
				if (slow_work_execs[id])
					return (void *)(unsigned long) *_pos;
		*_pos = 0x1UL << ITERATOR_SHIFT;

	case 0x1:
		count = *_pos & ITERATOR_COUNTER;
		list_for_each(p, &slow_work_queue) {
			if (count == 0)
				return p;
			count--;
		}
		*_pos = 0x2UL << ITERATOR_SHIFT;

	case 0x2:
		count = *_pos & ITERATOR_COUNTER;
		list_for_each(p, &vslow_work_queue) {
			if (count == 0)
				return p;
			count--;
		}
		*_pos = 0x3UL << ITERATOR_SHIFT;

	default:
		return NULL;
	}
}

/*
 * set up the iterator to start reading from the first line
 */
static void *slow_work_runqueue_start(struct seq_file *m, loff_t *_pos)
{
	spin_lock_irq(&slow_work_queue_lock);
	return slow_work_runqueue_index(m, _pos);
}

/*
 * move to the next line
 */
static void *slow_work_runqueue_next(struct seq_file *m, void *v, loff_t *_pos)
{
	struct list_head *p = v;
	unsigned long selector = *_pos >> ITERATOR_SHIFT;

	(*_pos)++;
	switch (selector) {
	case 0x0:
		return slow_work_runqueue_index(m, _pos);

	case 0x1:
		if (*_pos >> ITERATOR_SHIFT == 0x1) {
			p = p->next;
			if (p != &slow_work_queue)
				return p;
		}
		*_pos = 0x2UL << ITERATOR_SHIFT;
		p = &vslow_work_queue;

	case 0x2:
		if (*_pos >> ITERATOR_SHIFT == 0x2) {
			p = p->next;
			if (p != &vslow_work_queue)
				return p;
		}
		*_pos = 0x3UL << ITERATOR_SHIFT;

	default:
		return NULL;
	}
}

/*
 * clean up after reading
 */
static void slow_work_runqueue_stop(struct seq_file *m, void *v)
{
	spin_unlock_irq(&slow_work_queue_lock);
}

static const struct seq_operations slow_work_runqueue_ops = {
	.start		= slow_work_runqueue_start,
	.stop		= slow_work_runqueue_stop,
	.next		= slow_work_runqueue_next,
	.show		= slow_work_runqueue_show,
};

/*
 * open "/sys/kernel/debug/slow_work/runqueue" to list queue contents
 */
static int slow_work_runqueue_open(struct inode *inode, struct file *file)
{
	return seq_open(file, &slow_work_runqueue_ops);
}

const struct file_operations slow_work_runqueue_fops = {
	.owner		= THIS_MODULE,
	.open		= slow_work_runqueue_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= seq_release,
};