2 * iostats - Gather file system information
9 #define DEBUGFILE "iostats.out"
10 #define DONESTR "done"
13 Maxfile = 1000, /* number of Files we'll log */
16 typedef struct File File;
17 typedef struct Fid Fid;
18 typedef struct Req Req;
19 typedef struct Rpc Rpc;
20 typedef struct Stats Stats;
22 /* per file statistics */
43 File *file; /* set on open/create */
57 /* per rpc statistics */
69 /* all the statistics */
92 catcher(void *a, char *msg)
96 if(strcmp(msg, DONESTR) == 0) {
100 if(strcmp(msg, "exit") == 0)
107 update(Rpc *rpc, vlong t)
126 return &fidtab[fid % nelem(fidtab)];
130 getfid(int fid, int new)
135 for(f = *ff; f != nil; f = f->next){
140 f = mallocz(sizeof(*f), 1);
149 setfid(Fid *f, char *path, Qid qid)
160 rattach(Fcall *fin, Fcall *fout)
162 setfid(getfid(fin->fid, 1), strdup("/"), fout->qid);
166 rwalk(Fcall *fin, Fcall *fout)
171 if((of = getfid(fin->fid, 0)) == nil)
173 f = getfid(fin->newfid, 1);
175 setfid(f, strdup(of->path), of->qid);
176 for(i=0; i<fout->nwqid; i++)
177 setfid(f, cleanname(smprint("%s/%s", f->path, fin->wname[i])), fout->wqid[i]);
181 ropen(Fcall *fin, Fcall *fout)
186 if((f = getfid(fin->fid, 0)) == nil)
188 if(fin->type == Tcreate)
189 setfid(f, cleanname(smprint("%s/%s", f->path, fin->name)), fout->qid);
191 setfid(f, f->path, fout->qid);
192 for(fs = stats->file; fs < &stats->file[Maxfile]; fs++){
194 fs->path = strdup(f->path);
199 if(fs->qid.path == f->qid.path && strcmp(fs->path, f->path) == 0){
213 for(ff = fidhash(fin->fid); (f = *ff) != nil; ff = &f->next){
214 if(f->fid == fin->fid){
224 rio(Fcall *fin, Fcall *fout)
230 if((f = getfid(fin->fid, 0)) == nil)
236 f->file->bread += count;
238 stats->totread += count;
243 f->file->bwrite += count;
245 stats->totwrite += count;
253 fprint(2, "usage: iostats [-d] [-f debugfile] cmds [args ...]\n");
258 main(int argc, char **argv)
264 float brpsec, bwpsec, bppsec;
265 int cpid, fspid, rspid, dbg, n, mflag;
295 switch(cpid = fork()) {
300 if(getwd(buf, sizeof(buf)) == 0)
301 sysfatal("no working directory");
303 rfork(RFENVG|RFNAMEG|RFNOTEG);
305 if(mount(pfd[0], -1, "/", mflag, "") < 0)
308 bind("#c/pid", "/dev/pid", MREPL);
309 bind("#c/ppid", "/dev/ppid", MREPL);
310 bind("#e", "/env", MREPL|MCREATE);
314 open("/fd/0", OREAD);
315 open("/fd/1", OWRITE);
316 open("/fd/2", OWRITE);
320 exec(smprint("/bin/%s", argv[0]), argv);
321 sysfatal("exec: %r");
326 switch(fspid = fork()) {
328 while(cpid != waitpid())
330 postnote(PNPROC, fspid, DONESTR);
331 while(fspid != waitpid())
356 execl("/bin/exportfs", "exportfs", "-df", dbfile, "-r", "/", nil);
358 execl("/bin/exportfs", "exportfs", "-r", "/", nil);
363 fmtinstall('F', fcallfmt);
365 stats->rpc[Tversion].name = "version";
366 stats->rpc[Tauth].name = "auth";
367 stats->rpc[Tflush].name = "flush";
368 stats->rpc[Tattach].name = "attach";
369 stats->rpc[Twalk].name = "walk";
370 stats->rpc[Topen].name = "open";
371 stats->rpc[Tcreate].name = "create";
372 stats->rpc[Tclunk].name = "clunk";
373 stats->rpc[Tread].name = "read";
374 stats->rpc[Twrite].name = "write";
375 stats->rpc[Tremove].name = "remove";
376 stats->rpc[Tstat].name = "stat";
377 stats->rpc[Twstat].name = "wstat";
379 for(n = 0; n < nelem(stats->rpc); n++)
380 stats->rpc[n].lo = 10000000000LL;
382 switch(rspid = rfork(RFPROC|RFMEM)) {
384 /* read response from exportfs and pass to mount */
386 uchar tmp[sizeof(buf)];
389 n = read(efd[1], buf, sizeof(buf));
395 /* convert response */
396 memset(&f, 0, sizeof(f));
397 memmove(tmp, buf, n);
398 if(convM2S(tmp, n, &f) != n)
399 sysfatal("convM2S: %r");
401 /* find request to this response */
403 for(rr = &rqhead; (r = *rr) != nil; rr = &r->next){
404 if(r->f.tag == f.tag){
434 rpc = &stats->rpc[r->f.type];
439 if(write(pfd[1], buf, n) != n)
444 /* read request from mount and pass to exportfs */
446 n = read(pfd[1], buf, sizeof(buf));
452 r = mallocz(sizeof(*r) + n, 1);
453 memmove(r->buf, buf, n);
454 if(convM2S(r->buf, n, &r->f) != n)
455 sysfatal("convM2S: %r");
457 rpc = &stats->rpc[r->f.type];
470 if(write(efd[1], buf, n) != n)
477 postnote(PNPROC, rspid, DONESTR);
481 /* dump statistics */
482 rpc = &stats->rpc[Tread];
483 brpsec = (double)stats->totread / (((float)rpc->time/1e9)+.000001);
485 rpc = &stats->rpc[Twrite];
486 bwpsec = (double)stats->totwrite / (((float)rpc->time/1e9)+.000001);
489 for(n = 0; n < nelem(stats->rpc); n++) {
490 rpc = &stats->rpc[n];
496 bppsec = (double)stats->nproto / ((ttime/1e9)+.000001);
498 fprint(2, "\nread %llud bytes, %g Kb/sec\n", stats->totread, brpsec/1024.0);
499 fprint(2, "write %llud bytes, %g Kb/sec\n", stats->totwrite, bwpsec/1024.0);
500 fprint(2, "protocol %llud bytes, %g Kb/sec\n", stats->nproto, bppsec/1024.0);
501 fprint(2, "rpc %lud count\n\n", stats->nrpc);
503 fprint(2, "%-10s %5s %5s %5s %5s %5s T R\n",
504 "Message", "Count", "Low", "High", "Time", "Averg");
506 for(n = 0; n < nelem(stats->rpc); n++) {
507 rpc = &stats->rpc[n];
510 fprint(2, "%-10s %5lud %5llud %5llud %5llud %5llud ms %8llud %8llud bytes\n",
516 rpc->time/1000000/rpc->count,
521 fprint(2, "\nOpens Reads (bytes) Writes (bytes) File\n");
522 for(fs = stats->file; fs < &stats->file[Maxfile]; fs++){
525 fprint(2, "%5lud %8lud %8llud %8lud %8llud %s\n",
527 fs->nread, fs->bread,
528 fs->nwrite, fs->bwrite,