Skip to contentSkip to conversation

iowatcher improvements

PATCH New Jan Kara 1 participant 4 messages May 20, 2016 Block layer

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover iowatcher improvements

Hi,

this patches implement some improvements in iowatcher I was looking for for some time already. When analyzing more complex block traces, it is sometimes useful to plot IO from different processes by different color. However in some cases there are too many processes involved and you cannot distinguish much anyway. In those cases you need more refined coloring of the IO graph. Since the exact needs usually differ on case by case basis, I have decided to implement a way in iowatcher to use an external program for marking IO and then display the result (differently marked IO in different colors). The idea is that the external program can be written e.g. in Python where you can write a quick & dirty script to mark the IO as you need.

In the series is also a sample python script that tags IO by originating process (not distinguishing reads and writes) and allows grouping of processes into groups displayed by the same color. It is pretty easy to add your own scripts to color IO based on different metrics (e.g. completion time).

Honza

author
#1 lore raw

PATCH 1/3 iowatcher: Do not distinguish between reads and writes in IO graphs

Currently names of struct members distinguish between IO graphs for reads and writes. However with more involved IO graph coloring this doesn't necessarily make sense and also it leads to a duplication where we have to work with two arrays instead of one. There are only a few places which care about read vs write distinction and those can be implemented differently. So just replace two IO graph arrays with a single one.

---
 iowatcher/blkparse.c | 136 +++++++++++++++++++++++++++++++++------------------
 iowatcher/blkparse.h |   7 ++-
 iowatcher/main.c     | 116 +++++++++++++------------------------------
 iowatcher/plot.h     |   3 +-
 4 files changed, 125 insertions(+), 137 deletions(-)
iowatcher/blkparse.c +88 -48
index c7d1d652c78e..6f48079a2982 100644
@@ -174,7 +174,7 @@ struct pending_io {
 struct pid_map {
 	struct list_head hash_list;
 	u32 pid;
-	int index;
+	int index[2];
 	char name[0];
 };
 
@@ -329,11 +329,25 @@ static struct pid_map *process_hash_search(u32 pid)
 	return NULL;
 }
 
-static struct pid_map *process_hash_insert(u32 pid, char *name)
+static struct pid_map *process_hash_new(u32 pid, char *name)
 {
+	struct pid_map *pm;
 	int slot = hash_pid(pid);
+
+	pm = malloc(sizeof(struct pid_map) + strlen(name) + 1);
+	pm->pid = pid;
+	pm->index[0] = 0;
+	pm->index[1] = 0;
+	strcpy(pm->name, name);
+	list_add_tail(&pm->hash_list, process_hash_table + slot);
+
+	return pm;
+}
+
+static struct pid_map *process_hash_insert(u32 pid, char *name)
+{
 	struct pid_map *pm;
-	int old_index = 0;
+	int old_index[2] = {};
 	char buf[16];
 
 	pm = process_hash_search(pid);
@@ -342,18 +356,17 @@ static struct pid_map *process_hash_insert(u32 pid, char *name)
 		if (!name || !strcmp(name, pm->name))
 			return pm;
 		list_del(&pm->hash_list);
-		old_index = pm->index;
+		old_index[0] = pm->index[0];
+		old_index[1] = pm->index[1];
 		free(pm);
 	}
 	if (!name) {
 		sprintf(buf, "[%u]", pid);
 		name = buf;
 	}
-	pm = malloc(sizeof(struct pid_map) + strlen(name) + 1);
-	pm->pid = pid;
-	pm->index = old_index;
-	strcpy(pm->name, name);
-	list_add_tail(&pm->hash_list, process_hash_table + slot);
+	pm = process_hash_new(pid, name);
+	pm->index[0] = old_index[0];
+	pm->index[1] = old_index[1];
 
 	return pm;
 }
@@ -1014,39 +1027,53 @@ void add_tput(struct trace *trace, struct graph_line_data *writes_gld,
 		gld->max = gld->data[seconds].sum;
 }
 
-#define GDD_PTR_ALLOC_STEP 16
+#define GDD_PTR_ALLOC_STEP 32
 
-static struct pid_map *get_pid_map(struct trace_file *tf, u32 pid)
+static int per_process_get_io_index(struct trace_file *tf,
+				    struct blk_io_trace *io,
+				    char **label, char **suffix)
 {
 	struct pid_map *pm;
+	int rw = !(BLK_DATADIR(io->action) & BLK_TC_READ);
 
-	if (!io_per_process) {
-		if (!tf->io_plots)
-			tf->io_plots = 1;
-		return NULL;
-	}
-
-	pm = process_hash_insert(pid, NULL);
+	pm = process_hash_insert(io->pid, NULL);
+	if (rw)
+		*suffix = " Writes";
+	else
+		*suffix = " Reads";
+	*label = pm->name;
 	/* New entry? */
-	if (!pm->index) {
+	if (!pm->index[rw]) {
 		if (tf->io_plots == tf->io_plots_allocated) {
 			tf->io_plots_allocated += GDD_PTR_ALLOC_STEP;
-			tf->gdd_reads = realloc(tf->gdd_reads, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
-			if (!tf->gdd_reads)
-				abort();
-			tf->gdd_writes = realloc(tf->gdd_writes, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
-			if (!tf->gdd_writes)
+			tf->gdd_io = realloc(tf->gdd_io, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
+			if (!tf->gdd_io)
 				abort();
-			memset(tf->gdd_reads + tf->io_plots_allocated - GDD_PTR_ALLOC_STEP,
-			       0, GDD_PTR_ALLOC_STEP * sizeof(struct graph_dot_data *));
-			memset(tf->gdd_writes + tf->io_plots_allocated - GDD_PTR_ALLOC_STEP,
+			memset(tf->gdd_io + tf->io_plots_allocated - GDD_PTR_ALLOC_STEP,
 			       0, GDD_PTR_ALLOC_STEP * sizeof(struct graph_dot_data *));
 		}
-		pm->index = tf->io_plots++;
+		pm->index[rw] = tf->io_plots++;
 
-		return pm;
+		return pm->index[rw];
 	}
-	return pm;
+	return pm->index[rw];
+}
+
+static int default_get_io_index(struct trace_file *tf,
+				struct blk_io_trace *io,
+				char **label, char **suffix)
+{
+	int rw = !(BLK_DATADIR(io->action) & BLK_TC_READ);
+
+	*label = "";
+	if (rw)
+		*suffix = "Writes";
+	else
+		*suffix = "Reads";
+	/* We assume we have space for at least two plot pointers in gdd_io... */
+	if (tf->io_plots <= rw)
+		tf->io_plots = rw + 1;
+	return rw;
 }
 
 void add_io(struct trace *trace, struct trace_file *tf)
@@ -1055,8 +1082,7 @@ void add_io(struct trace *trace, struct trace_file *tf)
 	int action = io->action & BLK_TA_MASK;
 	u64 offset;
 	int index;
-	char *label;
-	struct pid_map *pm;
+	char *label, *suffix;
 
 	if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY))
 		return;
@@ -1064,25 +1090,39 @@ void add_io(struct trace *trace, struct trace_file *tf)
 	if (action != io_event(trace))
 		return;
 
+	if (!(BLK_DATADIR(io->action) & BLK_TC_READ) &&
+	    !(BLK_DATADIR(io->action) & BLK_TC_WRITE))
+		return;
+
 	offset = map_io(trace, io);
 
-	pm = get_pid_map(tf, io->pid);
-	if (!pm) {
-		index = 0;
-		label = "";
-	} else {
-		index = pm->index;
-		label = pm->name;
-	}
-	if (BLK_DATADIR(io->action) & BLK_TC_READ) {
-		if (!tf->gdd_reads[index])
-			tf->gdd_reads[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, pick_color(), strdup(label));
-		set_gdd_bit(tf->gdd_reads[index], offset, io->bytes, io->time);
-	} else if (BLK_DATADIR(io->action) & BLK_TC_WRITE) {
-		if (!tf->gdd_writes[index])
-			tf->gdd_writes[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, pick_color(), strdup(label));
-		set_gdd_bit(tf->gdd_writes[index], offset, io->bytes, io->time);
+	if (!io_per_process)
+		index = default_get_io_index(tf, io, &label, &suffix);
+	else
+		index = per_process_get_io_index(tf, io, &label, &suffix);
+
+	if (!tf->gdd_io[index]) {
+		int len = strlen(label) + strlen(suffix) + 1;
+		char *concat = malloc(len);
+		char *color = pick_color();
+		int rw = !(BLK_DATADIR(io->action) & BLK_TC_READ);
+
+		if (!concat)
+			abort();
+		strcpy(concat, label);
+		strcat(concat, suffix);
+		tf->gdd_io[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, color, concat);
+		if (!io_per_process) {
+			/* Use the color also for corresponding line graphs. */
+			if (!tf->line_color)
+				tf->line_color = color;
+			if (!rw && !tf->reads_color)
+				tf->reads_color = color;
+			if (rw && !tf->writes_color)
+				tf->writes_color = color;
+		}
 	}
+	set_gdd_bit(tf->gdd_io[index], offset, io->bytes, io->time);
 }
 
 void add_pending_io(struct trace *trace, struct graph_line_data *gld)
iowatcher/blkparse.h +3 -4
index fce9d01ed2c6..0448b70bbb09 100644
@@ -91,13 +91,12 @@ struct trace_file {
 	int fio_trace;
 	struct graph_line_data *fio_gld;
 
-	/* Number of entries in gdd_writes / gdd_reads */
+	/* Number of entries in gdd_io */
 	int io_plots;
 
-	/* Allocated array size for gdd_writes / gdd_reads */
+	/* Allocated array size for gdd_io */
 	int io_plots_allocated;
-	struct graph_dot_data **gdd_writes;
-	struct graph_dot_data **gdd_reads;
+	struct graph_dot_data **gdd_io;
 
 	unsigned int mpstat_min_seconds;
 	unsigned int mpstat_max_seconds;
iowatcher/main.c +33 -83

197 lines not on this page · raw message

iowatcher/plot.h +1 -2
index d65bbcf3396e..48e17e8bd81b 100644
@@ -128,8 +128,7 @@ struct plot_history {
 	struct list_head list;
 	int pid_history_count;
 	int col;
-	struct pid_plot_history **read_pid_history;
-	struct pid_plot_history **write_pid_history;
+	struct pid_plot_history **io_pid_history;
 };
 
 char *pick_color(void);
Signature · 1 line
2.6.6
author
#2 lore raw

PATCH 2/3 iowatcher: Allow external program to be used for IO tagging

In reply to Jan Kara
---
 iowatcher/blkparse.c | 225 ++++++++++++++++++++++++++++++++++++++-------------
 iowatcher/blkparse.h |   4 +-
 iowatcher/main.c     |  46 ++++++++++-
 iowatcher/tracers.c  |  53 +++++++++---
 iowatcher/tracers.h  |   1 +
 5 files changed, 259 insertions(+), 70 deletions(-)
iowatcher/blkparse.c +168 -57
index 6f48079a2982..32fc614ea479 100644
@@ -377,12 +377,6 @@ static void handle_notify(struct trace *trace)
 	void *payload = (char *)io + sizeof(*io);
 	u32 two32[2];
 
-	if (io->action == BLK_TN_PROCESS) {
-		if (io_per_process)
-			process_hash_insert(io->pid, payload);
-		return;
-	}
-
 	if (io->action != BLK_TN_TIMESTAMP)
 		return;
 
@@ -1029,50 +1023,93 @@ void add_tput(struct trace *trace, struct graph_line_data *writes_gld,
 
 #define GDD_PTR_ALLOC_STEP 32
 
-static int per_process_get_io_index(struct trace_file *tf,
-				    struct blk_io_trace *io,
-				    char **label, char **suffix)
+static void io_graph_new_index(struct trace_file *tf, int index, char *label,
+			       char *suffix)
+{
+	if (index >= tf->io_plots_allocated) {
+		int old_alloc = tf->io_plots_allocated;
+
+		tf->io_plots_allocated += GDD_PTR_ALLOC_STEP;
+		if (index >= tf->io_plots_allocated)
+			tf->io_plots_allocated = index + 1;
+		tf->gdd_io = realloc(tf->gdd_io, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
+		if (!tf->gdd_io)
+			abort();
+		memset(tf->gdd_io + old_alloc, 0,
+		       (tf->io_plots_allocated - old_alloc) * sizeof(struct graph_dot_data *));
+	}
+	if (tf->io_plots < index + 1)
+		tf->io_plots = index + 1;
+
+	if (!tf->gdd_io[index]) {
+		int len = strlen(label) + strlen(suffix) + 1;
+		char *concat = malloc(len);
+		char *color = pick_color();
+
+		if (!concat)
+			abort();
+		strcpy(concat, label);
+		strcat(concat, suffix);
+		tf->gdd_io[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, color, concat);
+	}
+}
+
+static int per_process_get_io_index(struct trace *trace, struct trace_file *tf)
 {
 	struct pid_map *pm;
+	struct blk_io_trace *io = trace->io;
 	int rw = !(BLK_DATADIR(io->action) & BLK_TC_READ);
+	int action = io->action & BLK_TA_MASK;
+	char *suffix;
+
+	if (io->action == BLK_TN_PROCESS) {
+		void *payload = (char *)io + sizeof(*io);
+
+		process_hash_insert(io->pid, payload);
+		return -1;
+	}
+
+	if (action != io_event(trace))
+		return -1;
+
+	if (!(BLK_DATADIR(io->action) & BLK_TC_READ) &&
+	    !(BLK_DATADIR(io->action) & BLK_TC_WRITE))
+		return -1;
 
 	pm = process_hash_insert(io->pid, NULL);
 	if (rw)
-		*suffix = " Writes";
+		suffix = " Writes";
 	else
-		*suffix = " Reads";
-	*label = pm->name;
+		suffix = " Reads";
 	/* New entry? */
 	if (!pm->index[rw]) {
-		if (tf->io_plots == tf->io_plots_allocated) {
-			tf->io_plots_allocated += GDD_PTR_ALLOC_STEP;
-			tf->gdd_io = realloc(tf->gdd_io, tf->io_plots_allocated * sizeof(struct graph_dot_data *));
-			if (!tf->gdd_io)
-				abort();
-			memset(tf->gdd_io + tf->io_plots_allocated - GDD_PTR_ALLOC_STEP,
-			       0, GDD_PTR_ALLOC_STEP * sizeof(struct graph_dot_data *));
-		}
-		pm->index[rw] = tf->io_plots++;
-
+		pm->index[rw] = tf->io_plots;
+		io_graph_new_index(tf, tf->io_plots, pm->name, suffix);
 		return pm->index[rw];
 	}
 	return pm->index[rw];
 }
 
-static int default_get_io_index(struct trace_file *tf,
-				struct blk_io_trace *io,
-				char **label, char **suffix)
+static int default_get_io_index(struct trace *trace, struct trace_file *tf)
 {
+	struct blk_io_trace *io = trace->io;
 	int rw = !(BLK_DATADIR(io->action) & BLK_TC_READ);
+	int action = io->action & BLK_TA_MASK;
+	char *suffix;
+
+	if (action != io_event(trace))
+		return -1;
+
+	if (!(BLK_DATADIR(io->action) & BLK_TC_READ) &&
+	    !(BLK_DATADIR(io->action) & BLK_TC_WRITE))
+		return -1;
 
-	*label = "";
 	if (rw)
-		*suffix = "Writes";
+		suffix = "Writes";
 	else
-		*suffix = "Reads";
-	/* We assume we have space for at least two plot pointers in gdd_io... */
-	if (tf->io_plots <= rw)
-		tf->io_plots = rw + 1;
+		suffix = "Reads";
+	if (!tf->gdd_io[rw])
+		io_graph_new_index(tf, rw, "", suffix);
 	return rw;
 }
 
@@ -1081,8 +1118,7 @@ void add_io(struct trace *trace, struct trace_file *tf)
 	struct blk_io_trace *io = trace->io;
 	int action = io->action & BLK_TA_MASK;
 	u64 offset;
-	int index;
-	char *label, *suffix;
+	int index = trace->io_tag;
 
 	if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY))
 		return;
@@ -1096,33 +1132,19 @@ void add_io(struct trace *trace, struct trace_file *tf)
 
 	offset = map_io(trace, io);
 
-	if (!io_per_process)
-		index = default_get_io_index(tf, io, &label, &suffix);
-	else
-		index = per_process_get_io_index(tf, io, &label, &suffix);
+	set_gdd_bit(tf->gdd_io[index], offset, io->bytes, io->time);
 
-	if (!tf->gdd_io[index]) {
-		int len = strlen(label) + strlen(suffix) + 1;
-		char *concat = malloc(len);
-		char *color = pick_color();
+	if (!io_per_process) {
 		int rw = !(BLK_DATADIR(io->action) & BLK_TC_READ);
 
-		if (!concat)
-			abort();
-		strcpy(concat, label);
-		strcat(concat, suffix);
-		tf->gdd_io[index] = alloc_dot_data(tf->min_seconds, tf->max_seconds, tf->min_offset, tf->max_offset, tf->stop_seconds, color, concat);
-		if (!io_per_process) {
-			/* Use the color also for corresponding line graphs. */
-			if (!tf->line_color)
-				tf->line_color = color;
-			if (!rw && !tf->reads_color)
-				tf->reads_color = color;
-			if (rw && !tf->writes_color)
-				tf->writes_color = color;
-		}
+		/* Use the color also for corresponding line graphs. */
+		if (!tf->line_color)
+			tf->line_color = tf->gdd_io[index]->color;
+		if (!rw && !tf->reads_color)
+			tf->reads_color = tf->gdd_io[index]->color;
+		if (rw && !tf->writes_color)
+			tf->writes_color = tf->gdd_io[index]->color;
 	}
-	set_gdd_bit(tf->gdd_io[index], offset, io->bytes, io->time);
 }
 
 void add_pending_io(struct trace *trace, struct graph_line_data *gld)
@@ -1242,7 +1264,96 @@ void add_iop(struct trace *trace, struct graph_line_data *gld)
 		gld->max = gld->data[seconds].sum;
 }
 
-void check_record(struct trace *trace)
+enum {
+	TAG_RESP_END,		/* No more responses for given IO event */
+	TAG_RESP_NEW_TAG,	/* Create new tag */
+	TAG_RESP_TAG_IO,	/* Tag IO with given tag */
+};
+
+extern int tag_io_fd;
+
+static void read_safe(void *buf, int len)
+{
+	ssize_t ret;
+
+	while (len > 0) {
+		ret = read(tag_io_fd, buf, len);
+		if (ret < 0) {
+			fprintf(stderr, "Failed to get IO tag. Read error: %s\n", strerror(errno));
+			exit(1);
+		}
+		if (ret == 0) {
+			fprintf(stderr, "Failed to get IO tag. Premature EOF.\n");
+			exit(1);
+		}
+		buf += ret;
+		len -= ret;
+	}
+}
+
+#define BUFSIZE 65536
+
+static int tag_prog_get_io_index(struct trace *trace, struct trace_file *tf)
+{
+	struct blk_io_trace *io = trace->io;
+	ssize_t ret;
+	char buf[BUFSIZE];
+	u32 response;
+	int index = -1;
+
+	ret = write(tag_io_fd, io, sizeof(*io) + io->pdu_len);
+	if (ret != (int)sizeof(*io) + io->pdu_len) {
+		fprintf(stderr, "Failed to get IO tag. Write returned %d\n", (int)ret);
+		exit(1);
+	}
+	while (1) {
+		read_safe(&response, sizeof(response));
+		if (response == TAG_RESP_END)
+			break;
+		else if (response == TAG_RESP_TAG_IO) {
+			u32 tag;
+
+			read_safe(&tag, sizeof(tag));
+			index = tag;
+		}
+		else if (response == TAG_RESP_NEW_TAG) {
+			u32 tag;
+			u32 len;
+
+			read_safe(&tag, sizeof(tag));
+			read_safe(&len, sizeof(len));
+			if (len > BUFSIZE) {
+				fprintf(stderr, "Failed to get IO tag. Too long tag label: %u\n", (unsigned)len);
+				exit(1);
+			}
+			read_safe(buf, len);
+			/* We expect \0 terminated string in buf as a label */
+			io_graph_new_index(tf, tag, buf, "");
+		}
+		else {
+			fprintf(stderr, "Failed to get IO tag. Unknown response %u\n", response);
+			exit(1);
+		}
+	}
+
+	return index;
+}
+
+static void handle_io_tags(struct trace *trace, struct trace_file *tf)
+{
+	int index;
+
+	if (tag_io_fd)
+		index = tag_prog_get_io_index(trace, tf);
+	else if (io_per_process)
+		index = per_process_get_io_index(trace, tf);
+	else
+		index = default_get_io_index(trace, tf);
+	trace->io_tag = index;
+}
+
+void check_record(struct trace *trace, struct trace_file *tf)
 {
 	handle_notify(trace);
+	handle_io_tags(trace, tf);
 }
iowatcher/blkparse.h +3 -1
index 0448b70bbb09..3dabe3675566 100644
@@ -40,6 +40,7 @@ struct trace {
 	char *start;
 	char *cur;
 	struct blk_io_trace *io;
+	int io_tag;
 	u64 start_timestamp;
 	struct timespec abs_start_time;
 
@@ -124,7 +125,8 @@ int filter_outliers(struct trace *trace, u64 min_offset, u64 max_offset,
 		    u64 *yzoom_min, u64 *yzoom_max);
 int action_char_to_num(char action);
 void add_iop(struct trace *trace, struct graph_line_data *gld);
-void check_record(struct trace *trace);
+void check_record(struct trace *trace, struct trace_file *tf);
+void get_io_tag(struct trace *trace);
 void add_completed_io(struct trace *trace,
 		      struct graph_line_data *latency_gld);
 void add_io(struct trace *trace, struct trace_file *tf);
iowatcher/main.c +44 -2

115 lines not on this page · raw message

iowatcher/tracers.c +43 -10

81 lines not on this page · raw message

iowatcher/tracers.h +1 -0
index 9b4f6a5480e4..68a972701104 100644
@@ -18,6 +18,7 @@
 #ifndef __IOWATCH_TRACERS
 #define __IOWATCH_TRACERS
 int run_program(int argc, char **argv, int wait, pid_t *pid, char *stdoutpath);
+int run_program_piped(int argc, char **argv, pid_t *pid, int *fd);
 int wait_program(pid_t pid, const char *pname, int signal);
 int stop_blktrace(void);
 int start_blktrace(char **devices, int num_devices, char *trace_name, char *dest);
Signature · 1 line
2.6.6
author
#3 lore raw

PATCH 3/3 iowatcher: Add sample script to tag IO by process and allow grouping

In reply to Jan Kara

Add a python module iowatcher.py that implements communication with the main iowatcher program. Also add a simple python script that tags IO by process ID and also allows grouping of processes into groups to demonstrate the use of the script.

---
 iowatcher/iowatcher.py  | 141 ++++++++++++++++++++++++++++++++++++++++++++++++
 iowatcher/tag-by-pid.py |  64 ++++++++++++++++++++++
 2 files changed, 205 insertions(+)
 create mode 100755 iowatcher/iowatcher.py
 create mode 100755 iowatcher/tag-by-pid.py
iowatcher/iowatcher.py new file +141 -0
new file mode 100755
index 000000000000..bfc6e01f8744
@@ -0,0 +1,141 @@
+import struct
+import os
+import sys
+
+"""
+We read input in the following form:
+struct blk_io_trace {
+        __u32 magic;            /* MAGIC << 8 | version */
+        __u32 sequence;         /* event number */
+        __u64 time;             /* in nanoseconds */
+        __u64 sector;           /* disk offset */
+        __u32 bytes;            /* transfer length */
+        __u32 action;           /* what happened */
+        __u32 pid;              /* who did it */
+        __u32 device;           /* device identifier (dev_t) */
+        __u32 cpu;              /* on what cpu did it happen */
+        __u16 error;            /* completion error */
+        __u16 pdu_len;          /* length of data after this trace */
+}; => size 48 bytes
+"""
+
+# Basic trace action constants
+__BLK_TA_QUEUE = 1
+__BLK_TA_BACKMERGE = 2
+__BLK_TA_FRONTMERGE = 3
+__BLK_TA_GETRQ = 4
+__BLK_TA_SLEEPRQ = 5
+__BLK_TA_REQUEUE = 6
+__BLK_TA_ISSUE = 7
+__BLK_TA_COMPLETE = 8
+__BLK_TA_PLUG = 9
+__BLK_TA_UNPLUG_IO = 10
+__BLK_TA_UNPLUG_TIMER = 11
+__BLK_TA_INSERT = 12
+__BLK_TA_SPLIT = 13
+__BLK_TA_BOUNCE = 14
+__BLK_TA_REMAP = 15
+__BLK_TA_ABORT = 16
+__BLK_TA_DRV_DATA = 17
+
+# Notification events constants
+__BLK_TN_PROCESS = 0
+__BLK_TN_TIMESTAMP = 1
+__BLK_TN_MESSAGE = 2
+
+# Trace categories
+BLK_TC_READ     = 1 << 0
+BLK_TC_WRITE    = 1 << 1
+BLK_TC_FLUSH    = 1 << 2
+BLK_TC_SYNC     = 1 << 3
+BLK_TC_QUEUE    = 1 << 4
+BLK_TC_REQUEUE  = 1 << 5
+BLK_TC_ISSUE    = 1 << 6
+BLK_TC_COMPLETE = 1 << 7
+BLK_TC_FS       = 1 << 8
+BLK_TC_PC       = 1 << 9
+BLK_TC_NOTIFY   = 1 << 10
+BLK_TC_AHEAD    = 1 << 11
+BLK_TC_META     = 1 << 12
+BLK_TC_DISCARD  = 1 << 13
+BLK_TC_DRV_DATA = 1 << 14
+BLK_TC_FUA      = 1 << 15
+
+BLK_TC_SHIFT = 16
+BLK_TA_MASK = (1 << BLK_TC_SHIFT) - 1
+
+def BLK_TC_ACT(act):
+	return act << BLK_TC_SHIFT
+
+def BLK_DATADIR(action):
+	return (action >> BLK_TC_SHIFT) & (BLK_TC_READ | BLK_TC_WRITE)
+
+# Trace actions in full
+BLK_TA_QUEUE =		__BLK_TA_QUEUE | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_BACKMERGE =	__BLK_TA_BACKMERGE | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_FRONTMERGE =	__BLK_TA_FRONTMERGE | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_GETRQ =		__BLK_TA_GETRQ | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_SLEEPRQ =	__BLK_TA_SLEEPRQ | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_REQUEUE =	__BLK_TA_REQUEUE | BLK_TC_ACT(BLK_TC_REQUEUE)
+BLK_TA_ISSUE =		__BLK_TA_ISSUE | BLK_TC_ACT(BLK_TC_ISSUE)
+BLK_TA_COMPLETE =	__BLK_TA_COMPLETE| BLK_TC_ACT(BLK_TC_COMPLETE)
+BLK_TA_PLUG =		__BLK_TA_PLUG | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_UNPLUG_IO =	__BLK_TA_UNPLUG_IO | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_UNPLUG_TIMER =	__BLK_TA_UNPLUG_TIMER | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_INSERT =		__BLK_TA_INSERT | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_SPLIT =		__BLK_TA_SPLIT
+BLK_TA_BOUNCE =		__BLK_TA_BOUNCE
+BLK_TA_REMAP =		__BLK_TA_REMAP | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_ABORT =		__BLK_TA_ABORT | BLK_TC_ACT(BLK_TC_QUEUE)
+BLK_TA_DRV_DATA =	__BLK_TA_DRV_DATA | BLK_TC_ACT(BLK_TC_DRV_DATA)
+
+BLK_TN_PROCESS =	__BLK_TN_PROCESS | BLK_TC_ACT(BLK_TC_NOTIFY)
+BLK_TN_TIMESTAMP =	__BLK_TN_TIMESTAMP | BLK_TC_ACT(BLK_TC_NOTIFY)
+BLK_TN_MESSAGE =	__BLK_TN_MESSAGE | BLK_TC_ACT(BLK_TC_NOTIFY)
+
+# Responses to iowatcher process
+TAG_RESP_END = 0
+TAG_RESP_NEW_TAG = 1
+TAG_RESP_TAG_IO = 2
+
+def safe_read(count):
+	buf = ''
+	while count > 0:
+		newbuf = os.read(0, count)
+		if len(newbuf) == 0:
+			return ''
+		count -= len(newbuf)
+		buf += newbuf
+	return buf
+
+
+def read_event():
+	buf = safe_read(48)
+	# EOF?
+	if len(buf) == 0:
+		return ()
+	fields = struct.unpack('=IIQQIIIIIHH', buf)
+	pdu_len = fields[10]
+	if pdu_len > 0:
+		buf = safe_read(pdu_len)
+		if len(buf) == 0:
+			return ()
+		fields += (buf,)
+	else:
+		fields += ('',)
+	return fields
+
+def write_response_end():
+	buf = struct.pack('=I', TAG_RESP_END)
+	os.write(1, buf)
+
+def write_response_iotag(tag):
+	buf = struct.pack('=II', TAG_RESP_TAG_IO, tag)
+	os.write(1, buf)
+
+def write_response_newtag(tag, label):
+	label += '\0'
+	plen = len(label)
+	buf = struct.pack('=III', TAG_RESP_NEW_TAG, tag, plen)
+	os.write(1, buf)
+	os.write(1, label)
iowatcher/tag-by-pid.py new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.6.6