Module Name:    src
Committed By:   manu
Date:           Sun Jan 29 06:22:02 UTC 2012

Modified Files:
        src/lib/libperfuse: debug.c ops.c perfuse_priv.h subr.c
        src/usr.sbin/perfused: perfused.8 perfused.c perfused.h

Log Message:
Improve FUSE trace facility


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/lib/libperfuse/debug.c
cvs rdiff -u -r1.49 -r1.50 src/lib/libperfuse/ops.c
cvs rdiff -u -r1.24 -r1.25 src/lib/libperfuse/perfuse_priv.h
cvs rdiff -u -r1.14 -r1.15 src/lib/libperfuse/subr.c
cvs rdiff -u -r1.9 -r1.10 src/usr.sbin/perfused/perfused.8
cvs rdiff -u -r1.18 -r1.19 src/usr.sbin/perfused/perfused.c
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/perfused/perfused.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libperfuse/debug.c
diff -u src/lib/libperfuse/debug.c:1.8 src/lib/libperfuse/debug.c:1.9
--- src/lib/libperfuse/debug.c:1.8	Thu Dec 29 04:25:49 2011
+++ src/lib/libperfuse/debug.c	Sun Jan 29 06:22:01 2012
@@ -1,4 +1,4 @@
-/*  $NetBSD: debug.c,v 1.8 2011/12/29 04:25:49 riz Exp $ */
+/*  $NetBSD: debug.c,v 1.9 2012/01/29 06:22:01 manu Exp $ */
 
 /*-
  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
@@ -27,6 +27,9 @@
 
 #include <puffs.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#include <errno.h>
 #include <sys/types.h>
 
 #include "perfuse_if.h"
@@ -127,6 +130,64 @@ perfuse_opdump_in(ps, pm)
 	return buf;
 }
 
+struct perfuse_trace *
+perfuse_trace_begin(ps, opc, pm)
+	struct perfuse_state *ps;
+	puffs_cookie_t opc;
+	perfuse_msg_t *pm;
+{
+	struct perfuse_trace *pt;
+
+	if ((pt = malloc(sizeof(*pt))) == NULL)
+		DERR(EX_OSERR, "malloc failed");
+
+	pt->pt_opcode = ps->ps_get_inhdr(pm)->opcode;
+	pt->pt_status = inxchg;
+
+	if (clock_gettime(CLOCK_REALTIME, &pt->pt_start) != 0)
+		DERR(EX_OSERR, "clock_gettime failed");
+
+	if (opc == 0)
+		(void)strcpy(pt->pt_path, "");
+	else
+		(void)strlcpy(pt->pt_path, 
+			      perfuse_node_path(opc),
+			      sizeof(pt->pt_path));
+
+	(void)strlcpy(pt->pt_extra,
+		      perfuse_opdump_in(ps, pm),
+		      sizeof(pt->pt_extra));
+
+	TAILQ_INSERT_TAIL(&ps->ps_trace, pt, pt_list);
+	ps->ps_tracecount++;
+
+	return pt;
+}
+
+void
+perfuse_trace_end(ps, pt, error)
+	struct perfuse_state *ps;
+	struct perfuse_trace *pt;
+	int error;
+{
+	if (clock_gettime(CLOCK_REALTIME, &pt->pt_end) != 0)
+		DERR(EX_OSERR, "clock_gettime failed");
+
+	pt->pt_status = done;
+	pt->pt_error = error;
+
+	while (ps->ps_tracecount > PERFUSE_TRACECOUNT_MAX) {
+		struct perfuse_trace *fpt = TAILQ_FIRST(&ps->ps_trace);
+
+		if (fpt == NULL || fpt->pt_status != done)
+			break;
+
+		TAILQ_REMOVE(&ps->ps_trace, fpt, pt_list);
+		free(fpt);
+		ps->ps_tracecount--;
+	}
+}
+
 void
 perfuse_trace_dump(pu, fp)
 	struct puffs_usermount *pu;
@@ -159,7 +220,7 @@ perfuse_trace_dump(pu, fp)
 	TAILQ_FOREACH(pt, &ps->ps_trace, pt_list) {
 		const char *quote = pt->pt_path[0] != '\0' ? "\"" : "";
 
-		fprintf(fp, "%" PRIu64 ".%09ld %s %s%s%s %s ",  
+		fprintf(fp, "%ld.%09ld %s %s%s%s %s ",  
 			pt->pt_start.tv_sec, pt->pt_start.tv_nsec,
 			perfuse_opname(pt->pt_opcode),
 			quote, pt->pt_path, quote,
@@ -172,7 +233,7 @@ perfuse_trace_dump(pu, fp)
 			ts.tv_nsec = 0;	/* delint */
 			timespecsub(&pt->pt_end, &pt->pt_start, &ts);
 
-			fprintf(fp, "error = %d elapsed = %" PRIu64 ".%09lu ",
+			fprintf(fp, "error = %d elapsed = %ld.%09lu ",
 				pt->pt_error, ts.tv_sec, ts.tv_nsec);
 
 			count[pt->pt_opcode]++;
@@ -206,8 +267,7 @@ perfuse_trace_dump(pu, fp)
 			min = 0;
 		}
 			
-		fprintf(fp, "%s\t%d\t%" PRId64 ".%09ld\t%" PRId64 
-		    ".%09ld\t%" PRId64 ".%09ld\t\n",
+		fprintf(fp, "%s\t%d\t%ld.%09ld\t%ld.%09ld\t%ld.%09ld\t\n",
 			perfuse_opname(i), count[i],
 			min, ts_min[i].tv_nsec,
 			(time_t)(avg / 1000000000L), (long)(avg % 1000000000L),

Index: src/lib/libperfuse/ops.c
diff -u src/lib/libperfuse/ops.c:1.49 src/lib/libperfuse/ops.c:1.50
--- src/lib/libperfuse/ops.c:1.49	Wed Dec 28 17:33:53 2011
+++ src/lib/libperfuse/ops.c	Sun Jan 29 06:22:02 2012
@@ -1,4 +1,4 @@
-/*  $NetBSD: ops.c,v 1.49 2011/12/28 17:33:53 manu Exp $ */
+/*  $NetBSD: ops.c,v 1.50 2012/01/29 06:22:02 manu Exp $ */
 
 /*-
  *  Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved.
@@ -207,7 +207,6 @@ xchg_msg(pu, opc, pm, len, wait)
 	struct perfuse_state *ps;
 	struct perfuse_node_data *pnd;
 	struct perfuse_trace *pt = NULL;
-	int opcode;
 	int error;
 
 	ps = puffs_getspecific(pu);
@@ -228,31 +227,8 @@ xchg_msg(pu, opc, pm, len, wait)
 	/*
 	 * Record FUSE call start if requested
 	 */
-	opcode = ps->ps_get_inhdr(pm)->opcode;
-	if (perfuse_diagflags & PDF_TRACE) {
-		if ((pt = malloc(sizeof(*pt))) == NULL)
-			DERR(EX_OSERR, "malloc failed");
-
-		pt->pt_opcode = opcode;
-		pt->pt_status = inxchg;
-
-		if (opc == 0)
-			(void)strcpy(pt->pt_path, "");
-		else
-			(void)strlcpy(pt->pt_path, 
-				      perfuse_node_path(opc),
-				      sizeof(pt->pt_path));
-
-		(void)strlcpy(pt->pt_extra,
-			      perfuse_opdump_in(ps, pm),
-			      sizeof(pt->pt_extra));
-
-		if (clock_gettime(CLOCK_REALTIME, &pt->pt_start) != 0)
-			DERR(EX_OSERR, "clock_gettime failed");
-
-		TAILQ_INSERT_TAIL(&ps->ps_trace, pt, pt_list);
-		ps->ps_tracecount++;
-	}
+	if (perfuse_diagflags & PDF_TRACE)
+		pt = perfuse_trace_begin(ps, opc, pm);
 
 	/*
 	 * Do actual FUSE exchange
@@ -263,23 +239,8 @@ xchg_msg(pu, opc, pm, len, wait)
 	/*
 	 * Record FUSE call end if requested
 	 */
-	if (perfuse_diagflags & PDF_TRACE) {
-		if (clock_gettime(CLOCK_REALTIME, &pt->pt_end) != 0)
-			DERR(EX_OSERR, "clock_gettime failed");
-
-		pt->pt_status = done;
-		pt->pt_error = error;
-		while (ps->ps_tracecount > PERFUSE_TRACECOUNT_MAX) {
-			struct perfuse_trace *fpt = TAILQ_FIRST(&ps->ps_trace);
-
-			if (fpt->pt_status != done)
-				break;
-
-			TAILQ_REMOVE(&ps->ps_trace, fpt, pt_list);
-			ps->ps_tracecount--;
-			free(fpt);
-		}
-	}
+	if (pt != NULL)
+		perfuse_trace_end(ps, pt, error);
 
 	if (pnd) {
 		pnd->pnd_flags &= ~PND_INXCHG;
@@ -434,23 +395,30 @@ static int
 attr_expired(opc)
 	puffs_cookie_t opc;
 {
-	struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
+	struct perfuse_node_data *pnd;
+	struct timespec expire;
 	struct timespec now;
 
+	pnd = PERFUSE_NODE_DATA(opc);
+	expire = pnd->pnd_attr_expire;
+
 	if (clock_gettime(CLOCK_REALTIME, &now) != 0)
 		DERR(EX_OSERR, "clock_gettime failed");
 
-	return timespeccmp(&pnd->pnd_attr_expire, &now, <);
+	return timespeccmp(&expire, &now, <);
 }
 
 static int
 entry_expired(opc)
 	puffs_cookie_t opc;
 {
-	struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc);
-	struct timespec expire = pnd->pnd_entry_expire;
+	struct perfuse_node_data *pnd;
+	struct timespec expire;
 	struct timespec now;
 
+	pnd = PERFUSE_NODE_DATA(opc);
+	expire = pnd->pnd_entry_expire;
+
 	if (clock_gettime(CLOCK_REALTIME, &now) != 0)
 		DERR(EX_OSERR, "clock_gettime failed");
 

Index: src/lib/libperfuse/perfuse_priv.h
diff -u src/lib/libperfuse/perfuse_priv.h:1.24 src/lib/libperfuse/perfuse_priv.h:1.25
--- src/lib/libperfuse/perfuse_priv.h:1.24	Wed Dec 28 17:33:53 2011
+++ src/lib/libperfuse/perfuse_priv.h	Sun Jan 29 06:22:02 2012
@@ -1,4 +1,4 @@
-/*  $NetBSD: perfuse_priv.h,v 1.24 2011/12/28 17:33:53 manu Exp $ */
+/*  $NetBSD: perfuse_priv.h,v 1.25 2012/01/29 06:22:02 manu Exp $ */
 
 /*-
  *  Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved.
@@ -252,6 +252,9 @@ int perfuse_node_listextattr(struct puff
 int perfuse_node_deleteextattr(struct puffs_usermount *, puffs_cookie_t,
     int, const char *, const struct puffs_cred *);
 
+struct perfuse_trace *perfuse_trace_begin(struct perfuse_state *, 
+    puffs_cookie_t, perfuse_msg_t *);
+void perfuse_trace_end(struct perfuse_state *, struct perfuse_trace *, int);
 char *perfuse_opdump_in(struct perfuse_state *, perfuse_msg_t *);
 
 __END_DECLS

Index: src/lib/libperfuse/subr.c
diff -u src/lib/libperfuse/subr.c:1.14 src/lib/libperfuse/subr.c:1.15
--- src/lib/libperfuse/subr.c:1.14	Sun Oct 30 05:11:37 2011
+++ src/lib/libperfuse/subr.c	Sun Jan 29 06:22:02 2012
@@ -1,4 +1,4 @@
-/*  $NetBSD: subr.c,v 1.14 2011/10/30 05:11:37 manu Exp $ */
+/*  $NetBSD: subr.c,v 1.15 2012/01/29 06:22:02 manu Exp $ */
 
 /*-
  *  Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved.
@@ -116,6 +116,9 @@ perfuse_destroy_pn(pn)
 
 		if (!TAILQ_EMPTY(&pnd->pnd_pcq))
 			DERRX(EX_SOFTWARE, "%s: non empty pnd_pcq", __func__);
+
+		if (pnd == NULL)
+			DERRX(EX_SOFTWARE, "%s: pnd == NULL ???", __func__);
 #endif /* PERFUSE_DEBUG */
 
 		free(pnd);

Index: src/usr.sbin/perfused/perfused.8
diff -u src/usr.sbin/perfused/perfused.8:1.9 src/usr.sbin/perfused/perfused.8:1.10
--- src/usr.sbin/perfused/perfused.8:1.9	Wed Dec 28 18:56:38 2011
+++ src/usr.sbin/perfused/perfused.8	Sun Jan 29 06:22:02 2012
@@ -1,4 +1,4 @@
-.\" $NetBSD: perfused.8,v 1.9 2011/12/28 18:56:38 wiz Exp $
+.\" $NetBSD: perfused.8,v 1.10 2012/01/29 06:22:02 manu Exp $
 .\"
 .\" Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
 .\"
@@ -126,8 +126,10 @@ If the
 flag was used, toggle debug output.
 Do nothing otherwise.
 .It Dv SIGUSR1
-Dump FUSE operation trace to
-.Pa /var/run/perfuse.trace .
+Toggle FUSE operation dump on and off. When toggling off, the trace
+is is stored in 
+.Pa /var/run/perfuse-xxx.trace
+(xxx is the filesystem mount point).
 .El
 .Sh ERRORS
 The program logs to the syslog daemon as facility

Index: src/usr.sbin/perfused/perfused.c
diff -u src/usr.sbin/perfused/perfused.c:1.18 src/usr.sbin/perfused/perfused.c:1.19
--- src/usr.sbin/perfused/perfused.c:1.18	Tue Jan 17 17:58:36 2012
+++ src/usr.sbin/perfused/perfused.c	Sun Jan 29 06:22:02 2012
@@ -1,4 +1,4 @@
-/*  $NetBSD: perfused.c,v 1.18 2012/01/17 17:58:36 joerg Exp $ */
+/*  $NetBSD: perfused.c,v 1.19 2012/01/29 06:22:02 manu Exp $ */
 
 /*-
  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
@@ -55,8 +55,8 @@
  * we ever mount multiple filesystems in a single perfused, 
  * but it is not sure we will ever want to do that.
  */
-static struct puffs_usermount *my_perfuse_mount = NULL;
-static FILE *perfuse_trace = NULL;
+struct puffs_usermount *perfuse_mount = NULL;
+FILE *perfuse_trace = NULL;
 
 static int access_mount(const char *, uid_t, int);
 static void new_mount(int, int);
@@ -210,6 +210,10 @@ new_mount(int fd, int pmnt_flags)
 	pid_t pid;
 	int flags;
 	int sock_type;
+	char trace_file[MAXPATHLEN + 1];
+	char trace_name[MAXPATHLEN + 1];
+	ssize_t trace_namelen;
+	int i;
 
 	pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
 	switch(pid) {
@@ -278,9 +282,17 @@ new_mount(int fd, int pmnt_flags)
 	/*
 	 * Setup trace file facility
 	 */
-	my_perfuse_mount = pu;
+	perfuse_mount = pu;
 
-	if ((perfuse_trace = fopen(_PATH_VAR_RUN_PERFUSE_TRACE, "w")) == NULL)
+	trace_namelen = strlcpy(trace_name, pmi.pmi_target, MAXPATHLEN);
+	for (i = 0; i < trace_namelen; i++)
+		if (trace_name[i] == '/')
+			trace_name[i] = '-';
+
+	(void)snprintf(trace_file, MAXPATHLEN, _PATH_VAR_RUN_PERFUSE_TRACE,
+		       trace_name);
+
+	if ((perfuse_trace = fopen(trace_file, "w")) == NULL)
 		DERR(EX_OSFILE, 
 		     "could not open \"%s\"",
 		     _PATH_VAR_RUN_PERFUSE_TRACE);
@@ -359,7 +371,16 @@ siginfo_handler(int sig)
 static void
 sigusr1_handler(int sig)
 {
-	return perfuse_trace_dump(my_perfuse_mount, perfuse_trace);
+	if (perfuse_diagflags & PDF_TRACE) {
+		perfuse_trace_dump(perfuse_mount, perfuse_trace);
+		perfuse_diagflags &= ~PDF_TRACE;
+		DPRINTF("trace dumped, trace disabled");
+	} else {
+		perfuse_diagflags |= PDF_TRACE;
+		DPRINTF("trace enabled");
+	}
+
+	return;
 }
 
 static int
@@ -370,9 +391,6 @@ parse_options(int argc, char **argv)
 	int retval = -1;
 
 	perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
-#ifdef PERFUSE_DEBUG
-	perfuse_diagflags |= PDF_TRACE;
-#endif
 
 	while ((ch = getopt(argc, argv, "d:fsi:")) != -1) {
 		switch (ch) {

Index: src/usr.sbin/perfused/perfused.h
diff -u src/usr.sbin/perfused/perfused.h:1.6 src/usr.sbin/perfused/perfused.h:1.7
--- src/usr.sbin/perfused/perfused.h:1.6	Wed Dec 28 17:33:53 2011
+++ src/usr.sbin/perfused/perfused.h	Sun Jan 29 06:22:02 2012
@@ -1,4 +1,4 @@
-/*  $NetBSD: perfused.h,v 1.6 2011/12/28 17:33:53 manu Exp $ */
+/*  $NetBSD: perfused.h,v 1.7 2012/01/29 06:22:02 manu Exp $ */
 
 /*-
  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
@@ -34,7 +34,7 @@
 
 #define PERFUSE_MSG_T struct puffs_framebuf
 
-#define _PATH_VAR_RUN_PERFUSE_TRACE "/var/run/perfuse.trace"
+#define _PATH_VAR_RUN_PERFUSE_TRACE "/var/run/perfuse%s.trace"
 
 __BEGIN_DECLS
 

Reply via email to