1235368Sgnn#!/usr/sbin/dtrace -s
2235368Sgnn/*
3235368Sgnn * dnlcsnoop.d - snoop DNLC activity.
4235368Sgnn *               Written in DTrace (Solaris 10 3/05).
5235368Sgnn *
6235368Sgnn * The DNLC is the Directory Name Lookup Cache. Filename lookups often
7235368Sgnn * return a hit from here, before needing to traverse the regular file
8235368Sgnn * system cache or go to disk.
9235368Sgnn *
10235368Sgnn * $Id: dnlcsnoop.d 3 2007-08-01 10:50:08Z brendan $
11235368Sgnn *
12235368Sgnn * USAGE:	dnlcsnoop.d     # wait several seconds, then hit Ctrl-C
13235368Sgnn *
14235368Sgnn * FIELDS:
15235368Sgnn *		PID             Process ID
16235368Sgnn *		CMD          	Command name
17235368Sgnn *		TIME         	Elapsed time for lookup, us
18235368Sgnn *		HIT          	DNLC hit Y/N
19235368Sgnn *		PATH         	Path for DNLC lookup
20235368Sgnn *
21235368Sgnn * COPYRIGHT: Copyright (c) 2005, 2006 Brendan Gregg.
22235368Sgnn *
23235368Sgnn * CDDL HEADER START
24235368Sgnn *
25235368Sgnn *  The contents of this file are subject to the terms of the
26235368Sgnn *  Common Development and Distribution License, Version 1.0 only
27235368Sgnn *  (the "License").  You may not use this file except in compliance
28235368Sgnn *  with the License.
29235368Sgnn *
30235368Sgnn *  You can obtain a copy of the license at Docs/cddl1.txt
31235368Sgnn *  or http://www.opensolaris.org/os/licensing.
32235368Sgnn *  See the License for the specific language governing permissions
33235368Sgnn *  and limitations under the License.
34235368Sgnn *
35235368Sgnn * CDDL HEADER END
36235368Sgnn *
37235368Sgnn * 27-Mar-2004	Brendan Gregg	Created this.
38235368Sgnn * 14-Jun-2005	   "      "	Rewrote this a lot.
39235368Sgnn * 20-Apr-2006	   "      "	Last update.
40235368Sgnn */
41235368Sgnn
42235368Sgnn#pragma D option quiet
43235368Sgnn
44235368Sgnn/*
45235368Sgnn * Print header
46235368Sgnn */
47235368Sgnndtrace:::BEGIN
48235368Sgnn{
49235368Sgnn	printf("%6s %-12s %5s %3s %s\n",
50235368Sgnn	    "PID", "CMD", "TIME", "HIT", "PATH");
51235368Sgnn}
52235368Sgnn
53235368Sgnn/*
54235368Sgnn * DNLC lookup
55235368Sgnn */
56235368Sgnnfbt:genunix:dnlc_lookup:entry
57235368Sgnn{
58235368Sgnn	/* store path */
59235368Sgnn	self->path = stringof(args[0]->v_path);
60235368Sgnn
61235368Sgnn	/* check for trailing "/" */
62235368Sgnn	this->len = strlen(self->path);
63235368Sgnn	self->join = *(char *)(args[0]->v_path + this->len - 1) == '/' ?
64235368Sgnn	    "" : "/";
65235368Sgnn
66235368Sgnn	/* store lookup name */
67235368Sgnn	self->name = stringof(arg1);
68235368Sgnn
69235368Sgnn	/* store start time */
70235368Sgnn	self->start = timestamp;
71235368Sgnn}
72235368Sgnn
73235368Sgnn/*
74235368Sgnn * DNLC return
75235368Sgnn */
76235368Sgnnfbt:genunix:dnlc_lookup:return
77235368Sgnn/self->start/
78235368Sgnn{
79235368Sgnn	/* calculate elapsed time */
80235368Sgnn	this->elapsed = (timestamp - self->start) / 1000;
81235368Sgnn
82235368Sgnn	/* print output */
83235368Sgnn	printf("%6d %-12.12s %5d %3s %s%s%s\n",
84235368Sgnn	    pid, execname, this->elapsed, arg1 == 0 ? "N" : "Y",
85235368Sgnn	    self->path, self->join, self->name);
86235368Sgnn
87235368Sgnn	/* clear variables */
88235368Sgnn	self->path = 0;
89235368Sgnn	self->join = 0;
90235368Sgnn	self->name = 0;
91235368Sgnn	self->start = 0;
92235368Sgnn}
93