Skip to contentSkip to conversation

dm statistics

PATCH New Mikulas Patocka 1 participant 2 messages Jan 24, 2013

Subsystems (3)
lore
Share & tools
lore

Mikulas Patocka

author maintainer
#0 lore raw

PATCH 1/1 dm statistics

Hi

Here I'm sending the patch for dm statistics.

(it depends on the RCU patch, but it could be trivially fixed to apply without the RCU patch - see functions dm_internal_suspend and dm_internal_resume).

---
 Documentation/device-mapper/dm-statistics.txt |   44 ++
 drivers/md/Makefile                           |    2 
 drivers/md/dm-ioctl.c                         |  144 ++++++++
 drivers/md/dm-stats.c                         |  430 ++++++++++++++++++++++++++
 drivers/md/dm-stats.h                         |   38 ++
 drivers/md/dm.c                               |   48 ++
 drivers/md/dm.h                               |    8 
 include/uapi/linux/dm-ioctl.h                 |    5 
 8 files changed, 716 insertions(+), 3 deletions(-)
drivers/md/dm-ioctl.c +143 -1
Index: linux-3.8-rc4-fast/drivers/md/dm-ioctl.c
===================================================================
@@ -1451,6 +1451,141 @@ static int table_status(struct dm_ioctl 
 	return 0;
 }
 
+struct dm_message_output_callback {
+	struct dm_ioctl *param;
+	size_t param_size;
+};
+
+static int dm_output_message_string(struct dm_message_output_callback *c,
+				    const char *string)
+{
+	size_t len;
+	char *p;
+	if (c->param->flags & DM_BUFFER_FULL_FLAG)
+		return -1;
+	if (!(c->param->flags & DM_MESSAGE_OUT_FLAG)) {
+		p = get_result_buffer(c->param, c->param_size, &len);
+		if (!len) {
+			c->param->flags |= DM_BUFFER_FULL_FLAG;
+			return -1;
+		}
+		*p = 0;
+		c->param->data_size = c->param->data_start + 1;
+		c->param->flags |= DM_MESSAGE_OUT_FLAG;
+	}
+	p = (char *)c->param + c->param->data_size - 1;
+	len = strlen(string);
+	if (c->param->data_size + len > c->param_size) {
+		c->param->flags |= DM_BUFFER_FULL_FLAG;
+		c->param->flags &= ~DM_MESSAGE_OUT_FLAG;
+		return -1;
+	}
+	c->param->data_size += len;
+	strcpy(p, string);
+	return 0;
+}
+
+/*
+ * Process device-mapper dependent messages.
+ * Returns a number <= 0 if message was processed by device mapper.
+ * Returns 1 if message should be delivered to the target.
+ */
+static int message_for_md(struct mapped_device *md,
+			  struct dm_message_output_callback *c,
+			  unsigned argc, char **argv)
+{
+	int id;
+	char dummy;
+	if (!strcasecmp(argv[0], "@stats_create")) {
+		unsigned long long start, end, step;
+		unsigned div;
+		char id_string[11];
+
+		if (dm_request_based(md))
+			goto no_rq_based_stats;
+
+		if (argc != 3)
+			goto invalid_message;
+
+		if (!strcmp(argv[1], "-")) {
+			start = 0;
+			end = dm_get_size(md);
+			if (!end)
+				end = 1;
+		} else if (sscanf(argv[1], "%llu-%llu%c", &start, &end, &dummy) != 2 ||
+			   start != (sector_t)start || end != (sector_t)end)
+			goto invalid_message;
+
+		if (start >= end)
+			goto invalid_message;
+
+		if (sscanf(argv[2], "/%u%c", &div, &dummy) == 1) {
+			step = end - start;
+			if (do_div(step, div))
+				step++;
+			if (!step)
+				step = 1;
+		} else if (sscanf(argv[2], "%llu%c", &step, &dummy) != 1 || step != (sector_t)step || !step)
+			goto invalid_message;
+
+		/*
+		 * Suspend/resume to make sure there is no i/o in flight, so that newly
+		 * created statistics will be exact.
+		 */
+		dm_internal_suspend(md);
+		id = dm_stats_create(dm_get_stats(md), start, end, step);
+		dm_internal_resume(md);
+
+		if (id < 0)
+			return id;
+
+		snprintf(id_string, sizeof id_string, "%d", id);
+		dm_output_message_string(c, id_string);
+
+		return 0;
+	} else if (!strcasecmp(argv[0], "@stats_delete")) {
+		if (dm_request_based(md))
+			goto no_rq_based_stats;
+
+		if (argc != 2)
+			goto invalid_message;
+
+		if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
+			goto invalid_message;
+
+		return dm_stats_delete(dm_get_stats(md), id);
+	} else if (!strcasecmp(argv[0], "@stats_print")) {
+		if (dm_request_based(md))
+			goto no_rq_based_stats;
+
+		if (argc != 2)
+			goto invalid_message;
+		if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
+			goto invalid_message;
+		return dm_stats_print(dm_get_stats(md), id, false, c,
+				      dm_output_message_string);
+	} else if (!strcasecmp(argv[0], "@stats_print_clear")) {
+		if (dm_request_based(md))
+			goto no_rq_based_stats;
+
+		if (argc != 2)
+			goto invalid_message;
+		if (sscanf(argv[1], "%d%c", &id, &dummy) != 1 || id < 0)
+			goto invalid_message;
+		return dm_stats_print(dm_get_stats(md), id, true, c,
+				      dm_output_message_string);
+	}
+	return 1;
+
+no_rq_based_stats:
+	DMWARN("Statistics are only supported for bio based devices");
+	return -EOPNOTSUPP;
+
+invalid_message:
+	DMWARN("Invalid parameters for message %s", argv[0]);
+	return -EINVAL;
+}
+
 /*
  * Pass a message to the target that's at the supplied device offset.
  */
@@ -1463,6 +1598,7 @@ static int target_message(struct dm_ioct
 	struct dm_target *ti;
 	struct dm_target_msg *tmsg = (void *) param + param->data_start;
 	int srcu_idx;
+	struct dm_message_output_callback c = { param, param_size };
 
 	md = find_device(param);
 	if (!md)
@@ -1486,6 +1622,10 @@ static int target_message(struct dm_ioct
 		goto out_argv;
 	}
 
+	r = message_for_md(md, &c, argc, argv);
+	if (r <= 0)
+		goto out_argv;
+
 	table = dm_get_live_table(md, &srcu_idx);
 	if (!table)
 		goto out_table;
@@ -1511,7 +1651,8 @@ static int target_message(struct dm_ioct
  out_argv:
 	kfree(argv);
  out:
-	param->data_size = 0;
+	if (!(param->flags & (DM_MESSAGE_OUT_FLAG | DM_BUFFER_FULL_FLAG)))
+		param->data_size = 0;
 	dm_put(md);
 	return r;
 }
@@ -1685,6 +1826,7 @@ static int validate_params(uint cmd, str
 	param->flags &= ~DM_BUFFER_FULL_FLAG;
 	param->flags &= ~DM_UEVENT_GENERATED_FLAG;
 	param->flags &= ~DM_SECURE_DATA_FLAG;
+	param->flags &= ~DM_MESSAGE_OUT_FLAG;
 
 	/* Ignores parameters */
 	if (cmd == DM_REMOVE_ALL_CMD ||
include/uapi/linux/dm-ioctl.h +5 -0
Index: linux-3.8-rc4-fast/include/uapi/linux/dm-ioctl.h
===================================================================
@@ -336,4 +336,9 @@ enum {
  */
 #define DM_SECURE_DATA_FLAG		(1 << 15) /* In */
 
+/*
+ * If set, message generated output.
+ */
+#define DM_MESSAGE_OUT_FLAG		(1 << 16) /* Out */
+
 #endif				/* _LINUX_DM_IOCTL_H */
drivers/md/Makefile +1 -1
Index: linux-3.8-rc4-fast/drivers/md/Makefile
===================================================================
@@ -3,7 +3,7 @@
 #
 
 dm-mod-y	+= dm.o dm-table.o dm-target.o dm-linear.o dm-stripe.o \
-		   dm-ioctl.o dm-io.o dm-kcopyd.o dm-sysfs.o
+		   dm-ioctl.o dm-stats.o dm-io.o dm-kcopyd.o dm-sysfs.o
 dm-multipath-y	+= dm-path-selector.o dm-mpath.o
 dm-snapshot-y	+= dm-snap.o dm-exception-store.o dm-snap-transient.o \
 		    dm-snap-persistent.o
drivers/md/dm-stats.c new file +430 -0

430 lines not on this page · raw message

drivers/md/dm-stats.h new file +38 -0
Index: linux-3.8-rc4-fast/drivers/md/dm-stats.h
===================================================================
@@ -0,0 +1,38 @@
+#ifndef DM_STATS_H
+#define DM_STATS_H
+
+#include <linux/types.h>
+#include <linux/mutex.h>
+#include <linux/list.h>
+#include <linux/rcupdate.h>
+#include <linux/genhd.h>
+
+int dm_stats_init(void);
+void dm_stats_exit(void);
+
+struct dm_stats {
+	struct mutex mutex;
+	struct list_head list;	/* list of struct dm_stat */
+};
+
+void dm_stats_init_device(struct dm_stats *st);
+void dm_stats_exit_device(struct dm_stats *st);
+
+int dm_stats_create(struct dm_stats *st, sector_t start, sector_t end, sector_t step);
+int dm_stats_delete(struct dm_stats *st, int id);
+
+void dm_stats_bio(struct dm_stats *st, struct bio *bio, bool end,
+		  unsigned long duration);
+
+struct dm_message_output_callback;
+
+int dm_stats_print(struct dm_stats *st, int id, bool clear,
+		   struct dm_message_output_callback *c,
+		   int (*callback)(struct dm_message_output_callback *, const char *));
+
+static inline bool dm_stats_used(struct dm_stats *st)
+{
+	return !list_empty(&st->list);
+}
+
+#endif
drivers/md/dm.c +47 -1

108 lines not on this page · raw message

drivers/md/dm.h +8 -0
Index: linux-3.8-rc4-fast/drivers/md/dm.h
===================================================================
@@ -16,6 +16,8 @@
 #include <linux/blkdev.h>
 #include <linux/hdreg.h>
 
+#include "dm-stats.h"
+
 /*
  * Suspend feature flags
  */
@@ -146,10 +148,16 @@ void dm_destroy(struct mapped_device *md
 void dm_destroy_immediate(struct mapped_device *md);
 int dm_open_count(struct mapped_device *md);
 int dm_lock_for_deletion(struct mapped_device *md);
+int dm_request_based(struct mapped_device *md);
+sector_t dm_get_size(struct mapped_device *md);
+struct dm_stats *dm_get_stats(struct mapped_device *md);
 
 int dm_kobject_uevent(struct mapped_device *md, enum kobject_action action,
 		      unsigned cookie);
 
+void dm_internal_suspend(struct mapped_device *md);
+void dm_internal_resume(struct mapped_device *md);
+
 int dm_io_init(void);
 void dm_io_exit(void);
 
Documentation/device-mapper/dm-statistics.txt new file +44 -0

44 lines not on this page · raw message

author maintainer
#1 lore raw

PATCH 1/1 userspace: dm statistics

This is the userspace patch for two-way messages that are needed for statistics.

BTW. when querying large data sets, dmsetup fails with "Huge memory allocation (size 67108864) rejected - metadata corruption?"

- do you think this limit should be removed from dmsetup?

Mikulas

---
 libdm/ioctl/libdm-iface.c |   19 +++++++++++++++++++
 libdm/libdevmapper.h      |    1 +
 libdm/misc/dm-ioctl.h     |    5 +++++
 tools/dmsetup.c           |    9 +++++++++
 4 files changed, 34 insertions(+)
libdm/misc/dm-ioctl.h +5 -0
Index: LVM2.2.02.98/libdm/misc/dm-ioctl.h
===================================================================
@@ -338,4 +338,9 @@ enum {
  */
 #define DM_SECURE_DATA_FLAG		(1 << 15) /* In */
 
+/*
+ * If set, message generated output.
+ */
+#define DM_MESSAGE_OUT_FLAG		(1 << 16) /* Out */
+
 #endif				/* _LINUX_DM_IOCTL_H */
libdm/ioctl/libdm-iface.c +19 -0
Index: LVM2.2.02.98/libdm/ioctl/libdm-iface.c
===================================================================
@@ -700,6 +700,24 @@ struct dm_versions *dm_task_get_versions
 				       dmt->dmi.v4->data_start);
 }
 
+const char *dm_task_get_message_result(struct dm_task *dmt)
+{
+	const char *start, *end;
+	if (!(dmt->dmi.v4->flags & DM_MESSAGE_OUT_FLAG))
+		return NULL;
+	start = (const char *) dmt->dmi.v4 + dmt->dmi.v4->data_start;
+	end = (const char *) dmt->dmi.v4 + dmt->dmi.v4->data_size;
+	if (end < start) {
+		log_error(INTERNAL_ERROR "Corrupted message structure returned: %d > %d", (int)dmt->dmi.v4->data_start, (int)dmt->dmi.v4->data_size);
+		return NULL;
+	}
+	if (!memchr(start, 0, end - start)) {
+		log_error(INTERNAL_ERROR "Message result doesn't contain terminating nul character");
+		return NULL;
+	}
+	return start;
+}
+
 int dm_task_set_ro(struct dm_task *dmt)
 {
 	dmt->read_only = 1;
@@ -1870,6 +1888,7 @@ repeat_ioctl:
 		case DM_DEVICE_STATUS:
 		case DM_DEVICE_TABLE:
 		case DM_DEVICE_WAITEVENT:
+		case DM_DEVICE_TARGET_MSG:
 			_ioctl_buffer_double_factor++;
 			_dm_zfree_dmi(dmi);
 			goto repeat_ioctl;
libdm/libdevmapper.h +1 -0
Index: LVM2.2.02.98/libdm/libdevmapper.h
===================================================================
@@ -178,6 +178,7 @@ const char *dm_task_get_uuid(const struc
 
 struct dm_deps *dm_task_get_deps(struct dm_task *dmt);
 struct dm_versions *dm_task_get_versions(struct dm_task *dmt);
+const char *dm_task_get_message_result(struct dm_task *dmt);
 
 /*
  * These functions return device-mapper names based on the value
tools/dmsetup.c +9 -0
Index: LVM2.2.02.98/tools/dmsetup.c
===================================================================
@@ -770,6 +770,7 @@ static int _message(CMD_ARGS)
 	size_t sz = 1;
 	struct dm_task *dmt;
 	char *str;
+	const char *result;
 	uint64_t sector;
 	char *endptr;
 
@@ -833,6 +834,14 @@ static int _message(CMD_ARGS)
 	if (!dm_task_run(dmt))
 		goto out;
 
+	result = dm_task_get_message_result(dmt);
+	if (result) {
+		if (!*result || result[strlen(result) - 1] == '\n')
+			fputs(result, stdout);
+		else
+			puts(result);
+	}
+
 	r = 1;
 
       out:

--
lvm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/lvm-devel