Home | History | Annotate | Line # | Download | only in libperfuse
debug.c revision 1.6
      1  1.6  manu /*  $NetBSD: debug.c,v 1.6 2011/12/28 17:33:52 manu Exp $ */
      2  1.1  manu 
      3  1.1  manu /*-
      4  1.1  manu  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
      5  1.1  manu  *
      6  1.1  manu  *  Redistribution and use in source and binary forms, with or without
      7  1.1  manu  *  modification, are permitted provided that the following conditions
      8  1.1  manu  *  are met:
      9  1.1  manu  *  1. Redistributions of source code must retain the above copyright
     10  1.1  manu  *     notice, this list of conditions and the following disclaimer.
     11  1.1  manu  *  2. Redistributions in binary form must reproduce the above copyright
     12  1.1  manu  *     notice, this list of conditions and the following disclaimer in the
     13  1.1  manu  *     documentation and/or other materials provided with the distribution.
     14  1.1  manu  *
     15  1.1  manu  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  1.1  manu  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  1.1  manu  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  1.1  manu  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  1.1  manu  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  1.1  manu  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  1.1  manu  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  1.1  manu  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  1.1  manu  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  1.1  manu  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  manu  *  POSSIBILITY OF SUCH DAMAGE.
     26  1.1  manu  */
     27  1.1  manu 
     28  1.1  manu #include <puffs.h>
     29  1.6  manu #include <stdio.h>
     30  1.1  manu #include <sys/types.h>
     31  1.1  manu 
     32  1.1  manu #include "perfuse_if.h"
     33  1.6  manu #include "perfuse_priv.h"
     34  1.1  manu #include "fuse.h"
     35  1.1  manu 
     36  1.1  manu struct perfuse_opcode {
     37  1.1  manu 	int  opcode;
     38  1.1  manu 	const char *opname;
     39  1.1  manu };
     40  1.1  manu 
     41  1.1  manu const struct perfuse_opcode perfuse_opcode[] = {
     42  1.1  manu 	{ FUSE_LOOKUP, "LOOKUP" },
     43  1.1  manu 	{ FUSE_FORGET, "FORGET" },
     44  1.1  manu 	{ FUSE_GETATTR, "GETATTR" },
     45  1.1  manu 	{ FUSE_SETATTR, "SETATTR" },
     46  1.1  manu 	{ FUSE_READLINK, "READLINK" },
     47  1.1  manu 	{ FUSE_SYMLINK, "SYMLINK" },
     48  1.1  manu 	{ FUSE_MKNOD, "MKNOD" },
     49  1.1  manu 	{ FUSE_MKDIR, "MKDIR" },
     50  1.1  manu 	{ FUSE_UNLINK, "UNLINK" },
     51  1.1  manu 	{ FUSE_RMDIR, "RMDIR" },
     52  1.1  manu 	{ FUSE_RENAME, "RENAME" },
     53  1.1  manu 	{ FUSE_LINK, "LINK" },
     54  1.1  manu 	{ FUSE_OPEN, "OPEN" },
     55  1.1  manu 	{ FUSE_READ, "READ" },
     56  1.1  manu 	{ FUSE_WRITE, "WRITE" },
     57  1.1  manu 	{ FUSE_STATFS, "STATFS" },
     58  1.1  manu 	{ FUSE_RELEASE, "RELEASE" },
     59  1.1  manu 	{ FUSE_FSYNC, "FSYNC" },
     60  1.1  manu 	{ FUSE_SETXATTR, "SETXATTR" },
     61  1.1  manu 	{ FUSE_GETXATTR, "GETXATTR" },
     62  1.1  manu 	{ FUSE_LISTXATTR, "LISTXATTR" },
     63  1.1  manu 	{ FUSE_REMOVEXATTR, "REMOVEXATTR" },
     64  1.1  manu 	{ FUSE_FLUSH, "FLUSH" },
     65  1.1  manu 	{ FUSE_INIT, "INIT" },
     66  1.1  manu 	{ FUSE_OPENDIR, "OPENDIR" },
     67  1.1  manu 	{ FUSE_READDIR, "READDIR" },
     68  1.1  manu 	{ FUSE_RELEASEDIR, "RELEASEDIR" },
     69  1.1  manu 	{ FUSE_FSYNCDIR, "FSYNCDIR" },
     70  1.1  manu 	{ FUSE_GETLK, "GETLK" },
     71  1.1  manu 	{ FUSE_SETLK, "SETLK" },
     72  1.1  manu 	{ FUSE_SETLKW, "SETLKW" },
     73  1.1  manu 	{ FUSE_ACCESS, "ACCESS" },
     74  1.1  manu 	{ FUSE_CREATE, "CREATE" },
     75  1.1  manu 	{ FUSE_INTERRUPT, "INTERRUPT" },
     76  1.1  manu 	{ FUSE_BMAP, "BMAP" },
     77  1.1  manu 	{ FUSE_DESTROY, "DESTROY" },
     78  1.1  manu 	{ FUSE_IOCTL, "IOCTL" },
     79  1.1  manu 	{ FUSE_POLL, "POLL" },
     80  1.1  manu 	{ FUSE_CUSE_INIT, "CUSE_INIT" },
     81  1.1  manu 	{ 0, "UNKNOWN" },
     82  1.1  manu };
     83  1.1  manu 
     84  1.3  manu const char *perfuse_qtypestr[] = {
     85  1.3  manu 	"READDIR",
     86  1.3  manu 	"READ",
     87  1.3  manu 	"WRITE",
     88  1.3  manu 	"AFTERWRITE",
     89  1.5  manu 	"OPEN",
     90  1.4  manu 	"AFTERXCHG"
     91  1.3  manu };
     92  1.2  manu 
     93  1.1  manu const char *
     94  1.1  manu perfuse_opname(opcode)
     95  1.1  manu 	int opcode;
     96  1.1  manu {
     97  1.1  manu 	const struct perfuse_opcode *po;
     98  1.1  manu 
     99  1.1  manu 	for (po = perfuse_opcode; po->opcode; po++) {
    100  1.1  manu 		if (po->opcode == opcode)
    101  1.1  manu 			return po->opname;
    102  1.1  manu 	}
    103  1.1  manu 
    104  1.1  manu 	return po->opname; /* "UNKNOWN" */
    105  1.1  manu }
    106  1.6  manu 
    107  1.6  manu char *
    108  1.6  manu perfuse_opdump_in(ps, pm)
    109  1.6  manu 	struct perfuse_state *ps;
    110  1.6  manu 	perfuse_msg_t *pm;
    111  1.6  manu {
    112  1.6  manu 	struct fuse_in_header *fih;
    113  1.6  manu 	static char buf[BUFSIZ] = "";
    114  1.6  manu 
    115  1.6  manu 	fih = GET_INHDR(ps, pm);
    116  1.6  manu 
    117  1.6  manu 	switch(fih->opcode) {
    118  1.6  manu 	case FUSE_LOOKUP:
    119  1.6  manu 		(void)snprintf(buf, sizeof(buf), "path = \"%s\"",
    120  1.6  manu 			       _GET_INPAYLOAD(ps, pm, const char *));
    121  1.6  manu 		break;
    122  1.6  manu 	default:
    123  1.6  manu 		buf[0] = '\0';
    124  1.6  manu 		break;
    125  1.6  manu 	}
    126  1.6  manu 
    127  1.6  manu 	return buf;
    128  1.6  manu }
    129  1.6  manu 
    130  1.6  manu void
    131  1.6  manu perfuse_trace_dump(pu, fp)
    132  1.6  manu 	struct puffs_usermount *pu;
    133  1.6  manu 	FILE *fp;
    134  1.6  manu {
    135  1.6  manu 	struct perfuse_state *ps;
    136  1.6  manu 	struct perfuse_trace *pt;
    137  1.6  manu 	struct timespec ts_min[FUSE_OPCODE_MAX];
    138  1.6  manu 	struct timespec ts_max[FUSE_OPCODE_MAX];
    139  1.6  manu 	struct timespec ts_total[FUSE_OPCODE_MAX];
    140  1.6  manu 	int count[FUSE_OPCODE_MAX];
    141  1.6  manu 	uint64_t avg;
    142  1.6  manu 	int i;
    143  1.6  manu 
    144  1.6  manu 	if (!(perfuse_diagflags & PDF_TRACE))
    145  1.6  manu 		return;
    146  1.6  manu 
    147  1.6  manu 	ps = puffs_getspecific(pu);
    148  1.6  manu 
    149  1.6  manu 	(void)ftruncate(fileno(fp), 0);
    150  1.6  manu 	(void)fseek(fp, 0, SEEK_SET);
    151  1.6  manu 
    152  1.6  manu 	(void)memset(&ts_min, 0, sizeof(ts_min));
    153  1.6  manu 	(void)memset(&ts_max, 0, sizeof(ts_max));
    154  1.6  manu 	(void)memset(&ts_total, 0, sizeof(ts_total));
    155  1.6  manu 	(void)memset(&count, 0, sizeof(count));
    156  1.6  manu 
    157  1.6  manu 	fprintf(fp, "Last %"PRId64" operations\n", ps->ps_tracecount);
    158  1.6  manu 
    159  1.6  manu 	TAILQ_FOREACH(pt, &ps->ps_trace, pt_list) {
    160  1.6  manu 		const char *quote = pt->pt_path[0] != '\0' ? "\"" : "";
    161  1.6  manu 
    162  1.6  manu 		fprintf(fp, "%lu.%09ld %s %s%s%s %s ",
    163  1.6  manu 			pt->pt_start.tv_sec, pt->pt_start.tv_nsec,
    164  1.6  manu 			perfuse_opname(pt->pt_opcode),
    165  1.6  manu 			quote, pt->pt_path, quote,
    166  1.6  manu 			pt->pt_extra);
    167  1.6  manu 
    168  1.6  manu 		if (pt->pt_status == done) {
    169  1.6  manu 			struct timespec ts;
    170  1.6  manu 
    171  1.6  manu 			ts.tv_sec = 0;	/* delint */
    172  1.6  manu 			ts.tv_nsec = 0;	/* delint */
    173  1.6  manu 			timespecsub(&pt->pt_end, &pt->pt_start, &ts);
    174  1.6  manu 
    175  1.6  manu 			fprintf(fp, "error = %d elapsed = %lu.%09lu ",
    176  1.6  manu 				pt->pt_error, ts.tv_sec, ts.tv_nsec);
    177  1.6  manu 
    178  1.6  manu 			count[pt->pt_opcode]++;
    179  1.6  manu 			timespecadd(&ts_total[pt->pt_opcode],
    180  1.6  manu 				    &ts,
    181  1.6  manu 				    &ts_total[pt->pt_opcode]);
    182  1.6  manu 
    183  1.6  manu 			if (timespeccmp(&ts, &ts_min[pt->pt_opcode], <) ||
    184  1.6  manu 			    (count[pt->pt_opcode] == 1))
    185  1.6  manu 				ts_min[pt->pt_opcode] = ts;
    186  1.6  manu 
    187  1.6  manu 			if (timespeccmp(&ts, &ts_max[pt->pt_opcode], >))
    188  1.6  manu 				ts_max[pt->pt_opcode] = ts;
    189  1.6  manu 		} else {
    190  1.6  manu 			fprintf(fp, "ongoing ");
    191  1.6  manu 		}
    192  1.6  manu 
    193  1.6  manu 		fprintf(fp, "\n");
    194  1.6  manu 	}
    195  1.6  manu 
    196  1.6  manu 	fprintf(fp, "\nStatistics by operation\n");
    197  1.6  manu 	fprintf(fp, "operation\tcount\tmin\tavg\tmax\n");
    198  1.6  manu 	for (i = 0; i < FUSE_OPCODE_MAX; i++) {
    199  1.6  manu 		time_t min;
    200  1.6  manu 
    201  1.6  manu 		if (count[i] != 0) {
    202  1.6  manu 			avg = timespec2ns(&ts_total[i]) / count[i];
    203  1.6  manu 			min = ts_min[i].tv_sec;
    204  1.6  manu 		} else {
    205  1.6  manu 			avg = 0;
    206  1.6  manu 			min = 0;
    207  1.6  manu 		}
    208  1.6  manu 
    209  1.6  manu 		fprintf(fp, "%s\t%d\t%ld.%09ld\t%ld.%09ld\t%ld.%09ld\t\n",
    210  1.6  manu 			perfuse_opname(i), count[i],
    211  1.6  manu 			min, ts_min[i].tv_nsec,
    212  1.6  manu 			(time_t)(avg / 1000000000L), (long)(avg % 1000000000L),
    213  1.6  manu 			ts_max[i].tv_sec, ts_max[i].tv_nsec);
    214  1.6  manu 	}
    215  1.6  manu 
    216  1.6  manu 	(void)fflush(fp);
    217  1.6  manu 	return;
    218  1.6  manu }
    219