summaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-vdo/dump.c
blob: 00e575d7d7739af34a858e73d461673f4162fda0 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright 2023 Red Hat
 */

#include "dump.h"

#include <linux/module.h>

#include "memory-alloc.h"
#include "string-utils.h"

#include "constants.h"
#include "data-vio.h"
#include "dedupe.h"
#include "funnel-workqueue.h"
#include "io-submitter.h"
#include "logger.h"
#include "types.h"
#include "vdo.h"

enum dump_options {
	/* Work queues */
	SHOW_QUEUES,
	/* Memory pools */
	SHOW_VIO_POOL,
	/* Others */
	SHOW_VDO_STATUS,
	/* This one means an option overrides the "default" choices, instead of altering them. */
	SKIP_DEFAULT
};

enum dump_option_flags {
	/* Work queues */
	FLAG_SHOW_QUEUES = (1 << SHOW_QUEUES),
	/* Memory pools */
	FLAG_SHOW_VIO_POOL = (1 << SHOW_VIO_POOL),
	/* Others */
	FLAG_SHOW_VDO_STATUS = (1 << SHOW_VDO_STATUS),
	/* Special */
	FLAG_SKIP_DEFAULT = (1 << SKIP_DEFAULT)
};

#define FLAGS_ALL_POOLS (FLAG_SHOW_VIO_POOL)
#define DEFAULT_DUMP_FLAGS (FLAG_SHOW_QUEUES | FLAG_SHOW_VDO_STATUS)
/* Another static buffer... log10(256) = 2.408+, round up: */
#define DIGITS_PER_U64 (1 + sizeof(u64) * 2409 / 1000)

static inline bool is_arg_string(const char *arg, const char *this_option)
{
	/* convention seems to be case-independent options */
	return strncasecmp(arg, this_option, strlen(this_option)) == 0;
}

static void do_dump(struct vdo *vdo, unsigned int dump_options_requested,
		    const char *why)
{
	u32 active, maximum;
	s64 outstanding;

	vdo_log_info("%s dump triggered via %s", VDO_LOGGING_MODULE_NAME, why);
	active = get_data_vio_pool_active_requests(vdo->data_vio_pool);
	maximum = get_data_vio_pool_maximum_requests(vdo->data_vio_pool);
	outstanding = (atomic64_read(&vdo->stats.bios_submitted) -
		       atomic64_read(&vdo->stats.bios_completed));
	vdo_log_info("%u device requests outstanding (max %u), %lld bio requests outstanding, device '%s'",
		     active, maximum, outstanding,
		     vdo_get_device_name(vdo->device_config->owning_target));
	if (((dump_options_requested & FLAG_SHOW_QUEUES) != 0) && (vdo->threads != NULL)) {
		thread_id_t id;

		for (id = 0; id < vdo->thread_config.thread_count; id++)
			vdo_dump_work_queue(vdo->threads[id].queue);
	}

	vdo_dump_hash_zones(vdo->hash_zones);
	dump_data_vio_pool(vdo->data_vio_pool,
			   (dump_options_requested & FLAG_SHOW_VIO_POOL) != 0);
	if ((dump_options_requested & FLAG_SHOW_VDO_STATUS) != 0)
		vdo_dump_status(vdo);

	vdo_report_memory_usage();
	vdo_log_info("end of %s dump", VDO_LOGGING_MODULE_NAME);
}

static int parse_dump_options(unsigned int argc, char *const *argv,
			      unsigned int *dump_options_requested_ptr)
{
	unsigned int dump_options_requested = 0;

	static const struct {
		const char *name;
		unsigned int flags;
	} option_names[] = {
		{ "viopool", FLAG_SKIP_DEFAULT | FLAG_SHOW_VIO_POOL },
		{ "vdo", FLAG_SKIP_DEFAULT | FLAG_SHOW_VDO_STATUS },
		{ "pools", FLAG_SKIP_DEFAULT | FLAGS_ALL_POOLS },
		{ "queues", FLAG_SKIP_DEFAULT | FLAG_SHOW_QUEUES },
		{ "threads", FLAG_SKIP_DEFAULT | FLAG_SHOW_QUEUES },
		{ "default", FLAG_SKIP_DEFAULT | DEFAULT_DUMP_FLAGS },
		{ "all", ~0 },
	};

	bool options_okay = true;
	unsigned int i;

	for (i = 1; i < argc; i++) {
		unsigned int j;

		for (j = 0; j < ARRAY_SIZE(option_names); j++) {
			if (is_arg_string(argv[i], option_names[j].name)) {
				dump_options_requested |= option_names[j].flags;
				break;
			}
		}
		if (j == ARRAY_SIZE(option_names)) {
			vdo_log_warning("dump option name '%s' unknown", argv[i]);
			options_okay = false;
		}
	}
	if (!options_okay)
		return -EINVAL;
	if ((dump_options_requested & FLAG_SKIP_DEFAULT) == 0)
		dump_options_requested |= DEFAULT_DUMP_FLAGS;
	*dump_options_requested_ptr = dump_options_requested;
	return 0;
}

/* Dump as specified by zero or more string arguments. */
int vdo_dump(struct vdo *vdo, unsigned int argc, char *const *argv, const char *why)
{
	unsigned int dump_options_requested = 0;
	int result = parse_dump_options(argc, argv, &dump_options_requested);

	if (result != 0)
		return result;

	do_dump(vdo, dump_options_requested, why);
	return 0;
}

/* Dump everything we know how to dump */
void vdo_dump_all(struct vdo *vdo, const char *why)
{
	do_dump(vdo, ~0, why);
}

/*
 * Dump out the data_vio waiters on a waitq.
 * wait_on should be the label to print for queue (e.g. logical or physical)
 */
static void dump_vio_waiters(struct vdo_wait_queue *waitq, char *wait_on)
{
	struct vdo_waiter *waiter, *first = vdo_waitq_get_first_waiter(waitq);
	struct data_vio *data_vio;

	if (first == NULL)
		return;

	data_vio = vdo_waiter_as_data_vio(first);

	vdo_log_info("      %s is locked. Waited on by: vio %px pbn %llu lbn %llu d-pbn %llu lastOp %s",
		     wait_on, data_vio, data_vio->allocation.pbn, data_vio->logical.lbn,
		     data_vio->duplicate.pbn, get_data_vio_operation_name(data_vio));

	for (waiter = first->next_waiter; waiter != first; waiter = waiter->next_waiter) {
		data_vio = vdo_waiter_as_data_vio(waiter);
		vdo_log_info("     ... and : vio %px pbn %llu lbn %llu d-pbn %llu lastOp %s",
			     data_vio, data_vio->allocation.pbn, data_vio->logical.lbn,
			     data_vio->duplicate.pbn,
			     get_data_vio_operation_name(data_vio));
	}
}

/*
 * Encode various attributes of a data_vio as a string of one-character flags. This encoding is for
 * logging brevity:
 *
 * R => vio completion result not VDO_SUCCESS
 * W => vio is on a waitq
 * D => vio is a duplicate
 * p => vio is a partial block operation
 * z => vio is a zero block
 * d => vio is a discard
 *
 * The common case of no flags set will result in an empty, null-terminated buffer. If any flags
 * are encoded, the first character in the string will be a space character.
 */
static void encode_vio_dump_flags(struct data_vio *data_vio, char buffer[8])
{
	char *p_flag = buffer;
	*p_flag++ = ' ';
	if (data_vio->vio.completion.result != VDO_SUCCESS)
		*p_flag++ = 'R';
	if (data_vio->waiter.next_waiter != NULL)
		*p_flag++ = 'W';
	if (data_vio->is_duplicate)
		*p_flag++ = 'D';
	if (data_vio->is_partial)
		*p_flag++ = 'p';
	if (data_vio->is_zero)
		*p_flag++ = 'z';
	if (data_vio->remaining_discard > 0)
		*p_flag++ = 'd';
	if (p_flag == &buffer[1]) {
		/* No flags, so remove the blank space. */
		p_flag = buffer;
	}
	*p_flag = '\0';
}

/* Implements buffer_dump_function. */
void dump_data_vio(void *data)
{
	struct data_vio *data_vio = data;

	/*
	 * This just needs to be big enough to hold a queue (thread) name and a function name (plus
	 * a separator character and NUL). The latter is limited only by taste.
	 *
	 * In making this static, we're assuming only one "dump" will run at a time. If more than
	 * one does run, the log output will be garbled anyway.
	 */
	static char vio_completion_dump_buffer[100 + MAX_VDO_WORK_QUEUE_NAME_LEN];
	static char vio_block_number_dump_buffer[sizeof("P L D") + 3 * DIGITS_PER_U64];
	static char vio_flush_generation_buffer[sizeof(" FG") + DIGITS_PER_U64];
	static char flags_dump_buffer[8];

	/*
	 * We're likely to be logging a couple thousand of these lines, and in some circumstances
	 * syslogd may have trouble keeping up, so keep it BRIEF rather than user-friendly.
	 */
	vdo_dump_completion_to_buffer(&data_vio->vio.completion,
				      vio_completion_dump_buffer,
				      sizeof(vio_completion_dump_buffer));
	if (data_vio->is_duplicate) {
		snprintf(vio_block_number_dump_buffer,
			 sizeof(vio_block_number_dump_buffer), "P%llu L%llu D%llu",
			 data_vio->allocation.pbn, data_vio->logical.lbn,
			 data_vio->duplicate.pbn);
	} else if (data_vio_has_allocation(data_vio)) {
		snprintf(vio_block_number_dump_buffer,
			 sizeof(vio_block_number_dump_buffer), "P%llu L%llu",
			 data_vio->allocation.pbn, data_vio->logical.lbn);
	} else {
		snprintf(vio_block_number_dump_buffer,
			 sizeof(vio_block_number_dump_buffer), "L%llu",
			 data_vio->logical.lbn);
	}

	if (data_vio->flush_generation != 0) {
		snprintf(vio_flush_generation_buffer,
			 sizeof(vio_flush_generation_buffer), " FG%llu",
			 data_vio->flush_generation);
	} else {
		vio_flush_generation_buffer[0] = 0;
	}

	encode_vio_dump_flags(data_vio, flags_dump_buffer);

	vdo_log_info("	vio %px %s%s %s %s%s", data_vio,
		     vio_block_number_dump_buffer,
		     vio_flush_generation_buffer,
		     get_data_vio_operation_name(data_vio),
		     vio_completion_dump_buffer,
		     flags_dump_buffer);
	/*
	 * might want info on: wantUDSAnswer / operation / status
	 * might want info on: bio / bios_merged
	 */

	dump_vio_waiters(&data_vio->logical.waiters, "lbn");

	/* might want to dump more info from vio here */
}