Home | History | Annotate | Line # | Download | only in libperfuse
debug.c revision 1.5.6.2
      1  1.5.6.2  yamt /*  $NetBSD: debug.c,v 1.5.6.2 2012/10/30 18:59:14 yamt 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.5.6.1  yamt #include <stdio.h>
     30  1.5.6.1  yamt #include <stdlib.h>
     31  1.5.6.1  yamt #include <err.h>
     32  1.5.6.1  yamt #include <errno.h>
     33      1.1  manu #include <sys/types.h>
     34      1.1  manu 
     35      1.1  manu #include "perfuse_if.h"
     36  1.5.6.1  yamt #include "perfuse_priv.h"
     37      1.1  manu #include "fuse.h"
     38      1.1  manu 
     39      1.1  manu struct perfuse_opcode {
     40      1.1  manu 	int  opcode;
     41      1.1  manu 	const char *opname;
     42      1.1  manu };
     43      1.1  manu 
     44      1.1  manu const struct perfuse_opcode perfuse_opcode[] = {
     45      1.1  manu 	{ FUSE_LOOKUP, "LOOKUP" },
     46      1.1  manu 	{ FUSE_FORGET, "FORGET" },
     47      1.1  manu 	{ FUSE_GETATTR, "GETATTR" },
     48      1.1  manu 	{ FUSE_SETATTR, "SETATTR" },
     49      1.1  manu 	{ FUSE_READLINK, "READLINK" },
     50      1.1  manu 	{ FUSE_SYMLINK, "SYMLINK" },
     51      1.1  manu 	{ FUSE_MKNOD, "MKNOD" },
     52      1.1  manu 	{ FUSE_MKDIR, "MKDIR" },
     53      1.1  manu 	{ FUSE_UNLINK, "UNLINK" },
     54      1.1  manu 	{ FUSE_RMDIR, "RMDIR" },
     55      1.1  manu 	{ FUSE_RENAME, "RENAME" },
     56      1.1  manu 	{ FUSE_LINK, "LINK" },
     57      1.1  manu 	{ FUSE_OPEN, "OPEN" },
     58      1.1  manu 	{ FUSE_READ, "READ" },
     59      1.1  manu 	{ FUSE_WRITE, "WRITE" },
     60      1.1  manu 	{ FUSE_STATFS, "STATFS" },
     61      1.1  manu 	{ FUSE_RELEASE, "RELEASE" },
     62      1.1  manu 	{ FUSE_FSYNC, "FSYNC" },
     63      1.1  manu 	{ FUSE_SETXATTR, "SETXATTR" },
     64      1.1  manu 	{ FUSE_GETXATTR, "GETXATTR" },
     65      1.1  manu 	{ FUSE_LISTXATTR, "LISTXATTR" },
     66      1.1  manu 	{ FUSE_REMOVEXATTR, "REMOVEXATTR" },
     67      1.1  manu 	{ FUSE_FLUSH, "FLUSH" },
     68      1.1  manu 	{ FUSE_INIT, "INIT" },
     69      1.1  manu 	{ FUSE_OPENDIR, "OPENDIR" },
     70      1.1  manu 	{ FUSE_READDIR, "READDIR" },
     71      1.1  manu 	{ FUSE_RELEASEDIR, "RELEASEDIR" },
     72      1.1  manu 	{ FUSE_FSYNCDIR, "FSYNCDIR" },
     73      1.1  manu 	{ FUSE_GETLK, "GETLK" },
     74      1.1  manu 	{ FUSE_SETLK, "SETLK" },
     75      1.1  manu 	{ FUSE_SETLKW, "SETLKW" },
     76      1.1  manu 	{ FUSE_ACCESS, "ACCESS" },
     77      1.1  manu 	{ FUSE_CREATE, "CREATE" },
     78      1.1  manu 	{ FUSE_INTERRUPT, "INTERRUPT" },
     79      1.1  manu 	{ FUSE_BMAP, "BMAP" },
     80      1.1  manu 	{ FUSE_DESTROY, "DESTROY" },
     81      1.1  manu 	{ FUSE_IOCTL, "IOCTL" },
     82      1.1  manu 	{ FUSE_POLL, "POLL" },
     83      1.1  manu 	{ FUSE_CUSE_INIT, "CUSE_INIT" },
     84      1.1  manu 	{ 0, "UNKNOWN" },
     85      1.1  manu };
     86      1.1  manu 
     87  1.5.6.1  yamt const char * const perfuse_qtypestr[] = {
     88      1.3  manu 	"READDIR",
     89      1.3  manu 	"READ",
     90      1.3  manu 	"WRITE",
     91      1.3  manu 	"AFTERWRITE",
     92      1.5  manu 	"OPEN",
     93  1.5.6.2  yamt 	"AFTERXCHG",
     94  1.5.6.2  yamt 	"REF"
     95      1.3  manu };
     96      1.2  manu 
     97      1.1  manu const char *
     98  1.5.6.1  yamt perfuse_opname(int opcode)
     99      1.1  manu {
    100      1.1  manu 	const struct perfuse_opcode *po;
    101      1.1  manu 
    102      1.1  manu 	for (po = perfuse_opcode; po->opcode; po++) {
    103      1.1  manu 		if (po->opcode == opcode)
    104      1.1  manu 			return po->opname;
    105      1.1  manu 	}
    106      1.1  manu 
    107      1.1  manu 	return po->opname; /* "UNKNOWN" */
    108      1.1  manu }
    109  1.5.6.1  yamt 
    110  1.5.6.1  yamt char *
    111  1.5.6.1  yamt perfuse_opdump_in(struct perfuse_state *ps, perfuse_msg_t *pm)
    112  1.5.6.1  yamt {
    113  1.5.6.1  yamt 	struct fuse_in_header *fih;
    114  1.5.6.1  yamt 	static char buf[BUFSIZ] = "";
    115  1.5.6.1  yamt 
    116  1.5.6.1  yamt 	fih = GET_INHDR(ps, pm);
    117  1.5.6.1  yamt 
    118  1.5.6.1  yamt 	switch(fih->opcode) {
    119  1.5.6.1  yamt 	case FUSE_LOOKUP:
    120  1.5.6.1  yamt 		(void)snprintf(buf, sizeof(buf), "path = \"%s\"",
    121  1.5.6.1  yamt 			       _GET_INPAYLOAD(ps, pm, const char *));
    122  1.5.6.1  yamt 		break;
    123  1.5.6.1  yamt 	default:
    124  1.5.6.1  yamt 		buf[0] = '\0';
    125  1.5.6.1  yamt 		break;
    126  1.5.6.1  yamt 	}
    127  1.5.6.1  yamt 
    128  1.5.6.1  yamt 	return buf;
    129  1.5.6.1  yamt }
    130  1.5.6.1  yamt 
    131  1.5.6.1  yamt struct perfuse_trace *
    132  1.5.6.1  yamt perfuse_trace_begin(struct perfuse_state *ps, puffs_cookie_t opc,
    133  1.5.6.1  yamt 	perfuse_msg_t *pm)
    134  1.5.6.1  yamt {
    135  1.5.6.1  yamt 	struct perfuse_trace *pt;
    136  1.5.6.1  yamt 
    137  1.5.6.1  yamt 	if ((pt = malloc(sizeof(*pt))) == NULL)
    138  1.5.6.1  yamt 		DERR(EX_OSERR, "malloc failed");
    139  1.5.6.1  yamt 
    140  1.5.6.1  yamt 	pt->pt_opcode = ps->ps_get_inhdr(pm)->opcode;
    141  1.5.6.1  yamt 	pt->pt_status = inxchg;
    142  1.5.6.1  yamt 
    143  1.5.6.1  yamt 	if (clock_gettime(CLOCK_REALTIME, &pt->pt_start) != 0)
    144  1.5.6.1  yamt 		DERR(EX_OSERR, "clock_gettime failed");
    145  1.5.6.1  yamt 
    146  1.5.6.1  yamt 	if (opc == 0)
    147  1.5.6.1  yamt 		(void)strcpy(pt->pt_path, "");
    148  1.5.6.1  yamt 	else
    149  1.5.6.1  yamt 		(void)strlcpy(pt->pt_path,
    150  1.5.6.2  yamt 			      perfuse_node_path(ps, opc),
    151  1.5.6.1  yamt 			      sizeof(pt->pt_path));
    152  1.5.6.1  yamt 
    153  1.5.6.1  yamt 	(void)strlcpy(pt->pt_extra,
    154  1.5.6.1  yamt 		      perfuse_opdump_in(ps, pm),
    155  1.5.6.1  yamt 		      sizeof(pt->pt_extra));
    156  1.5.6.1  yamt 
    157  1.5.6.1  yamt 	TAILQ_INSERT_TAIL(&ps->ps_trace, pt, pt_list);
    158  1.5.6.1  yamt 	ps->ps_tracecount++;
    159  1.5.6.1  yamt 
    160  1.5.6.1  yamt 	return pt;
    161  1.5.6.1  yamt }
    162  1.5.6.1  yamt 
    163  1.5.6.1  yamt void
    164  1.5.6.1  yamt perfuse_trace_end(struct perfuse_state *ps, struct perfuse_trace *pt, int error)
    165  1.5.6.1  yamt {
    166  1.5.6.1  yamt 	if (clock_gettime(CLOCK_REALTIME, &pt->pt_end) != 0)
    167  1.5.6.1  yamt 		DERR(EX_OSERR, "clock_gettime failed");
    168  1.5.6.1  yamt 
    169  1.5.6.1  yamt 	pt->pt_status = done;
    170  1.5.6.1  yamt 	pt->pt_error = error;
    171  1.5.6.1  yamt 
    172  1.5.6.1  yamt 	while (ps->ps_tracecount > PERFUSE_TRACECOUNT_MAX) {
    173  1.5.6.1  yamt 		struct perfuse_trace *fpt = TAILQ_FIRST(&ps->ps_trace);
    174  1.5.6.1  yamt 
    175  1.5.6.1  yamt 		if (fpt == NULL || fpt->pt_status != done)
    176  1.5.6.1  yamt 			break;
    177  1.5.6.1  yamt 
    178  1.5.6.1  yamt 		TAILQ_REMOVE(&ps->ps_trace, fpt, pt_list);
    179  1.5.6.1  yamt 		free(fpt);
    180  1.5.6.1  yamt 		ps->ps_tracecount--;
    181  1.5.6.1  yamt 	}
    182  1.5.6.1  yamt }
    183  1.5.6.1  yamt 
    184  1.5.6.1  yamt void
    185  1.5.6.1  yamt perfuse_trace_dump(struct puffs_usermount *pu, FILE *fp)
    186  1.5.6.1  yamt {
    187  1.5.6.1  yamt 	struct perfuse_state *ps;
    188  1.5.6.1  yamt 	struct perfuse_trace *pt;
    189  1.5.6.1  yamt 	struct timespec ts_min[FUSE_OPCODE_MAX];
    190  1.5.6.1  yamt 	struct timespec ts_max[FUSE_OPCODE_MAX];
    191  1.5.6.1  yamt 	struct timespec ts_total[FUSE_OPCODE_MAX];
    192  1.5.6.1  yamt 	int count[FUSE_OPCODE_MAX];
    193  1.5.6.1  yamt 	uint64_t avg;
    194  1.5.6.1  yamt 	int i;
    195  1.5.6.1  yamt 
    196  1.5.6.1  yamt 	if (!(perfuse_diagflags & PDF_TRACE))
    197  1.5.6.1  yamt 		return;
    198  1.5.6.1  yamt 
    199  1.5.6.1  yamt 	ps = puffs_getspecific(pu);
    200  1.5.6.1  yamt 
    201  1.5.6.1  yamt 	(void)ftruncate(fileno(fp), 0);
    202  1.5.6.2  yamt 	(void)fseek(fp, 0UL, SEEK_SET);
    203  1.5.6.1  yamt 
    204  1.5.6.1  yamt 	(void)memset(&ts_min, 0, sizeof(ts_min));
    205  1.5.6.1  yamt 	(void)memset(&ts_max, 0, sizeof(ts_max));
    206  1.5.6.1  yamt 	(void)memset(&ts_total, 0, sizeof(ts_total));
    207  1.5.6.1  yamt 	(void)memset(&count, 0, sizeof(count));
    208  1.5.6.1  yamt 
    209  1.5.6.1  yamt 	fprintf(fp, "Last %"PRId64" operations\n", ps->ps_tracecount);
    210  1.5.6.1  yamt 
    211  1.5.6.1  yamt 	TAILQ_FOREACH(pt, &ps->ps_trace, pt_list) {
    212  1.5.6.1  yamt 		const char *quote = pt->pt_path[0] != '\0' ? "\"" : "";
    213  1.5.6.1  yamt 
    214  1.5.6.1  yamt 		fprintf(fp, "%lld.%09ld %s %s%s%s %s ",
    215  1.5.6.1  yamt 			(long long)pt->pt_start.tv_sec, pt->pt_start.tv_nsec,
    216  1.5.6.1  yamt 			perfuse_opname(pt->pt_opcode),
    217  1.5.6.1  yamt 			quote, pt->pt_path, quote,
    218  1.5.6.1  yamt 			pt->pt_extra);
    219  1.5.6.1  yamt 
    220  1.5.6.1  yamt 		if (pt->pt_status == done) {
    221  1.5.6.1  yamt 			struct timespec ts;
    222  1.5.6.1  yamt 
    223  1.5.6.1  yamt 			ts.tv_sec = 0;	/* delint */
    224  1.5.6.1  yamt 			ts.tv_nsec = 0;	/* delint */
    225  1.5.6.1  yamt 			timespecsub(&pt->pt_end, &pt->pt_start, &ts);
    226  1.5.6.1  yamt 
    227  1.5.6.1  yamt 			fprintf(fp, "error = %d elapsed = %lld.%09lu ",
    228  1.5.6.1  yamt 				pt->pt_error, (long long)ts.tv_sec,
    229  1.5.6.1  yamt 				ts.tv_nsec);
    230  1.5.6.1  yamt 
    231  1.5.6.1  yamt 			count[pt->pt_opcode]++;
    232  1.5.6.1  yamt 			timespecadd(&ts_total[pt->pt_opcode],
    233  1.5.6.1  yamt 				    &ts,
    234  1.5.6.1  yamt 				    &ts_total[pt->pt_opcode]);
    235  1.5.6.1  yamt 
    236  1.5.6.1  yamt 			if (timespeccmp(&ts, &ts_min[pt->pt_opcode], <) ||
    237  1.5.6.1  yamt 			    (count[pt->pt_opcode] == 1))
    238  1.5.6.1  yamt 				ts_min[pt->pt_opcode] = ts;
    239  1.5.6.1  yamt 
    240  1.5.6.1  yamt 			if (timespeccmp(&ts, &ts_max[pt->pt_opcode], >))
    241  1.5.6.1  yamt 				ts_max[pt->pt_opcode] = ts;
    242  1.5.6.1  yamt 		} else {
    243  1.5.6.1  yamt 			fprintf(fp, "ongoing ");
    244  1.5.6.1  yamt 		}
    245  1.5.6.1  yamt 
    246  1.5.6.1  yamt 		fprintf(fp, "\n");
    247  1.5.6.1  yamt 	}
    248  1.5.6.1  yamt 
    249  1.5.6.1  yamt 	fprintf(fp, "\nStatistics by operation\n");
    250  1.5.6.1  yamt 	fprintf(fp, "operation\tcount\tmin\tavg\tmax\n");
    251  1.5.6.1  yamt 	for (i = 0; i < FUSE_OPCODE_MAX; i++) {
    252  1.5.6.1  yamt 		time_t min;
    253  1.5.6.1  yamt 
    254  1.5.6.1  yamt 		if (count[i] != 0) {
    255  1.5.6.1  yamt 			avg = timespec2ns(&ts_total[i]) / count[i];
    256  1.5.6.1  yamt 			min = ts_min[i].tv_sec;
    257  1.5.6.1  yamt 		} else {
    258  1.5.6.1  yamt 			avg = 0;
    259  1.5.6.1  yamt 			min = 0;
    260  1.5.6.1  yamt 		}
    261  1.5.6.1  yamt 
    262  1.5.6.1  yamt 		fprintf(fp, "%s\t%d\t%lld.%09ld\t%lld.%09ld\t%lld.%09ld\t\n",
    263  1.5.6.1  yamt 			perfuse_opname(i), count[i],
    264  1.5.6.1  yamt 			(long long)min, ts_min[i].tv_nsec,
    265  1.5.6.1  yamt 			(long long)(time_t)(avg / 1000000000L),
    266  1.5.6.1  yamt 			(long)(avg % 1000000000L),
    267  1.5.6.1  yamt 			(long long)ts_max[i].tv_sec, ts_max[i].tv_nsec);
    268  1.5.6.1  yamt 	}
    269  1.5.6.2  yamt 
    270  1.5.6.2  yamt 	fprintf(fp, "\n\nGlobal statistics\n");
    271  1.5.6.2  yamt 	fprintf(fp, "Nodes: %d\n", ps->ps_nodecount);
    272  1.5.6.2  yamt 	fprintf(fp, "Exchanges: %d\n", ps->ps_xchgcount);
    273  1.5.6.2  yamt 	fprintf(fp, "Nodes possibly leaked: %d\n", ps->ps_nodeleakcount);
    274  1.5.6.1  yamt 
    275  1.5.6.1  yamt 	(void)fflush(fp);
    276  1.5.6.1  yamt 	return;
    277  1.5.6.1  yamt }
    278