[sheepdog] [PATCH v3 10/10] collie: rename debug to trace

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Aug 9 11:09:07 CEST 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

This renames 'collie debug trace' to 'collie trace', and cleanups sub
commands.  Available trace commands are:

  trace enable <tracer>   enable the specified tracer
  trace disable <tracer>  disable the spcified tracer
  trace status            show tracer statuses
  trace cat               cat the output of tracers if any

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/Makefile.am          |    2 +-
 collie/collie.c             |    2 +-
 collie/collie.h             |    4 +-
 collie/{debug.c => trace.c} |  109 +++++++++++++++++++++++++++++--------------
 include/internal_proto.h    |    4 +-
 sheep/ops.c                 |   42 +++++++++++++----
 sheep/trace/trace.c         |   29 ++++++++++++
 sheep/trace/trace.h         |    2 +
 8 files changed, 143 insertions(+), 51 deletions(-)
 rename collie/{debug.c => trace.c} (63%)

diff --git a/collie/Makefile.am b/collie/Makefile.am
index a336f11..2b57f5a 100644
--- a/collie/Makefile.am
+++ b/collie/Makefile.am
@@ -28,7 +28,7 @@ collie_SOURCES		= farm/object_tree.c farm/sha1_file.c farm/snap.c \
 			  collie.c common.c treeview.c vdi.c node.c cluster.c
 
 if BUILD_TRACE
-collie_SOURCES          += debug.c
+collie_SOURCES          += trace.c
 override CFLAGS         := $(subst -pg,,$(CFLAGS))
 endif
 
diff --git a/collie/collie.c b/collie/collie.c
index 166a84c..55ed087 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -136,7 +136,7 @@ static void init_commands(const struct command **commands)
 		vdi_command,
 		node_command,
 		cluster_command,
-		debug_command,
+		trace_command,
 		{NULL,}
 	};
 
diff --git a/collie/collie.h b/collie/collie.h
index 7f5c128..fc06ab8 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -89,9 +89,9 @@ extern struct command node_command;
 extern struct command cluster_command;
 
 #ifdef HAVE_TRACE
-  extern struct command debug_command;
+  extern struct command trace_command;
 #else
-  #define debug_command {}
+  #define trace_command {}
 #endif /* HAVE_TRACE */
 
 #endif
diff --git a/collie/debug.c b/collie/trace.c
similarity index 63%
rename from collie/debug.c
rename to collie/trace.c
index 34f2a69..87ccecb 100644
--- a/collie/debug.c
+++ b/collie/trace.c
@@ -113,44 +113,90 @@ read_buffer:
 	return EXIT_SUCCESS;
 }
 
-static int trace_start(int argc, char **argv)
+static int trace_enable(int argc, char **argv)
 {
+	const char *tracer = argv[optind];
 	int ret;
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 
-	sd_init_req(&hdr, SD_OP_TRACE);
-	hdr.data_length = 1;
+	sd_init_req(&hdr, SD_OP_TRACE_ENABLE);
+	hdr.flags = SD_FLAG_CMD_WRITE;
+	hdr.data_length = strlen(tracer) + 1;
 
-	ret = send_light_req(&hdr, sdhost, sdport);
-	if (ret) {
-		fprintf(stderr, "Trace start failed: %s\n",
-				sd_strerror(rsp->result));
+	ret = collie_exec_req(sdhost, sdport, &hdr, (void *)tracer);
+	if (ret < 0)
+		return EXIT_SYSFAIL;
+
+	switch (rsp->result) {
+	case SD_RES_SUCCESS:
+		break;
+	case SD_RES_NO_SUPPORT:
+		fprintf(stderr, "no such tracer %s\n", tracer);
 		return EXIT_FAILURE;
+	case SD_RES_INVALID_PARMS:
+		fprintf(stderr, "tracer %s is already enabled\n", tracer);
+		return EXIT_FAILURE;
+	default:
+		fprintf(stderr, "unknown error (%s)\n",
+			sd_strerror(rsp->result));
+		return EXIT_SYSFAIL;
 	}
 
 	return EXIT_SUCCESS;
 }
 
-static int trace_stop(int argc, char **argv)
+static int trace_disable(int argc, char **argv)
 {
+	const char *tracer = argv[optind];
 	int ret;
 	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 
-	sd_init_req(&hdr, SD_OP_TRACE);
-	hdr.data_length = 0;
+	sd_init_req(&hdr, SD_OP_TRACE_DISABLE);
+	hdr.flags = SD_FLAG_CMD_WRITE;
+	hdr.data_length = strlen(tracer) + 1;
 
-	ret = send_light_req(&hdr, sdhost, sdport);
-	if (ret) {
-		fprintf(stderr, "Trace stop failed: %s\n",
-				sd_strerror(rsp->result));
+	ret = collie_exec_req(sdhost, sdport, &hdr, (void *)tracer);
+	if (ret < 0)
+		return EXIT_SYSFAIL;
+
+	switch (rsp->result) {
+	case SD_RES_SUCCESS:
+		break;
+	case SD_RES_NO_SUPPORT:
+		fprintf(stderr, "no such tracer %s\n", tracer);
 		return EXIT_FAILURE;
+	case SD_RES_INVALID_PARMS:
+		fprintf(stderr, "tracer %s is not enabled\n", tracer);
+		return EXIT_FAILURE;
+	default:
+		fprintf(stderr, "unknown error (%s)\n",
+			sd_strerror(rsp->result));
+		return EXIT_SYSFAIL;
 	}
 
 	return trace_read_buffer();
 }
 
+static int trace_status(int argc, char **argv)
+{
+	char buf[4096]; /* must have enough space to store tracer list */
+	int ret;
+	struct sd_req hdr;
+
+	sd_init_req(&hdr, SD_OP_TRACE_STATUS);
+	hdr.data_length = sizeof(buf);
+
+	ret = collie_exec_req(sdhost, sdport, &hdr, buf);
+	if (ret < 0)
+		return EXIT_SYSFAIL;
+
+	printf("%s", buf);
+
+	return EXIT_SUCCESS;
+}
+
 static int trace_cat(int argc, char **argv)
 {
 	int fd = open(tracefile, O_RDONLY);
@@ -183,35 +229,26 @@ static int trace_cat(int argc, char **argv)
 	return EXIT_SUCCESS;
 }
 
-static int debug_parser(int ch, char *opt)
+static int trace_parser(int ch, char *opt)
 {
 	return 0;
 }
 
 /* Subcommand list of trace */
 static struct subcommand trace_cmd[] = {
-	{"start", NULL, NULL, "start the trace",
-	 NULL, 0, trace_start},
-	{"stop", NULL, NULL, "stop the trace",
-	 NULL, 0, trace_stop},
-	{"cat", NULL, NULL, "cat the trace",
-	 NULL, 0, trace_cat},
+	{"enable", "<tracer>", "aph", "enable tracer", NULL,
+	 SUBCMD_FLAG_NEED_ARG, trace_enable},
+	{"disable", "<tracer>", "aph", "disable tracer", NULL,
+	 SUBCMD_FLAG_NEED_ARG, trace_disable},
+	{"status", NULL, "aph", "show tracer statuses", NULL,
+	 0, trace_status},
+	{"cat", NULL, "aph", "cat the output of tracers if any", NULL,
+	 0, trace_cat},
 	{NULL},
 };
 
-static int debug_trace(int argc, char **argv)
-{
-	return do_generic_subcommand(trace_cmd, argc, argv);
-}
-
-static struct subcommand debug_cmd[] = {
-	{"trace", NULL, "aph", "trace the node",
-	 trace_cmd, SUBCMD_FLAG_NEED_ARG, debug_trace},
-	{NULL,},
-};
-
-struct command debug_command = {
-	"debug",
-	debug_cmd,
-	debug_parser
+struct command trace_command = {
+	"trace",
+	trace_cmd,
+	trace_parser
 };
diff --git a/include/internal_proto.h b/include/internal_proto.h
index 3ca46cf..e1c9d33 100644
--- a/include/internal_proto.h
+++ b/include/internal_proto.h
@@ -50,12 +50,14 @@
 #define SD_OP_RESTORE        0x92
 #define SD_OP_GET_SNAP_FILE  0x93
 #define SD_OP_CLEANUP        0x94
-#define SD_OP_TRACE          0x95
+#define SD_OP_TRACE_STATUS   0x95
 #define SD_OP_TRACE_READ_BUF 0x96
 #define SD_OP_STAT_RECOVERY  0x97
 #define SD_OP_FLUSH_DEL_CACHE  0x98
 #define SD_OP_NOTIFY_VDI_DEL 0x99
 #define SD_OP_KILL_NODE      0x9A
+#define SD_OP_TRACE_ENABLE   0x9B
+#define SD_OP_TRACE_DISABLE  0x9C
 #define SD_OP_GET_OBJ_LIST   0xA1
 #define SD_OP_GET_EPOCH      0xA2
 #define SD_OP_CREATE_AND_WRITE_PEER 0xA3
diff --git a/sheep/ops.c b/sheep/ops.c
index 61ce782..193bd31 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -774,16 +774,24 @@ static int local_flush_and_del(struct request *req)
 	return object_cache_flush_and_del(req);
 }
 
-static int local_trace_ops(const struct sd_req *req, struct sd_rsp *rsp, void *data)
+static int local_trace_enable(const struct sd_req *req, struct sd_rsp *rsp,
+			      void *data)
 {
-	int enable = req->data_length, ret;
+	return trace_enable(data);
+}
 
-	if (enable)
-		ret = trace_enable("graph");
-	else
-		ret = trace_disable("graph");
+static int local_trace_disable(const struct sd_req *req, struct sd_rsp *rsp,
+			       void *data)
+{
+	return trace_disable(data);
+}
 
-	return ret;
+static int local_trace_status(const struct sd_req *req, struct sd_rsp *rsp,
+			      void *data)
+{
+	rsp->data_length = trace_status(data);
+
+	return SD_RES_SUCCESS;
 }
 
 static int local_trace_read_buf(struct request *request)
@@ -1143,11 +1151,25 @@ static struct sd_op_template sd_ops[] = {
 		.process_work = local_flush_and_del,
 	},
 
-	[SD_OP_TRACE] = {
-		.name = "TRACE",
+	[SD_OP_TRACE_ENABLE] = {
+		.name = "TRACE_ENABLE",
+		.type = SD_OP_TYPE_LOCAL,
+		.force = true,
+		.process_main = local_trace_enable,
+	},
+
+	[SD_OP_TRACE_DISABLE] = {
+		.name = "TRACE_DISABLE",
+		.type = SD_OP_TYPE_LOCAL,
+		.force = true,
+		.process_main = local_trace_disable,
+	},
+
+	[SD_OP_TRACE_STATUS] = {
+		.name = "TRACE_STATUS",
 		.type = SD_OP_TYPE_LOCAL,
 		.force = true,
-		.process_main = local_trace_ops,
+		.process_main = local_trace_status,
 	},
 
 	[SD_OP_TRACE_READ_BUF] = {
diff --git a/sheep/trace/trace.c b/sheep/trace/trace.c
index 883c9ee..813986c 100644
--- a/sheep/trace/trace.c
+++ b/sheep/trace/trace.c
@@ -221,6 +221,35 @@ int trace_disable(const char *name)
 	return SD_RES_SUCCESS;
 }
 
+/*
+ * Set the current tracer status to 'buf' and return the length of the
+ * data. 'buf' must have enough space to store all the tracer list.
+ */
+size_t trace_status(char *buf)
+{
+	struct tracer *t;
+	char *p = buf;
+
+	list_for_each_entry(t, &tracers, list) {
+		strcpy(p, t->name);
+		p += strlen(p);
+
+		*p++ = '\t';
+
+		if (uatomic_is_true(&t->enabled))
+			strcpy(p, "enabled");
+		else
+			strcpy(p, "disabled");
+		p += strlen(p);
+
+		*p++ = '\n';
+	}
+
+	*p++ = '\0';
+
+	return p - buf;
+}
+
 int trace_buffer_pop(void *buf, uint32_t len)
 {
 	int readin, count = 0, requested = len;
diff --git a/sheep/trace/trace.h b/sheep/trace/trace.h
index 9912fd2..3c8a5b3 100644
--- a/sheep/trace/trace.h
+++ b/sheep/trace/trace.h
@@ -40,6 +40,7 @@ unsigned long trace_function_exit(void);
   void regist_tracer(struct tracer *tracer);
   int trace_enable(const char *name);
   int trace_disable(const char *name);
+  size_t trace_status(char *buf);
   int trace_buffer_pop(void *buf, uint32_t len);
   void trace_buffer_push(int cpuid, struct trace_graph_item *item);
 
@@ -47,6 +48,7 @@ unsigned long trace_function_exit(void);
   static inline int trace_init(void) { return 0; }
   static inline int trace_enable(const char *name) { return 0; }
   static inline int trace_disable(const char *name) { return 0; }
+  static inline size_t trace_status(char *buf) { return 0; }
   static inline int trace_buffer_pop(void *buf, uint32_t len) { return 0; }
   static inline void trace_buffer_push(
 	  int cpuid, struct trace_graph_item *item) { return; }
-- 
1.7.9.5




More information about the sheepdog mailing list