1N/A#!/usr/sbin/dtrace -Zs
1N/A/*
1N/A * sh_wasted.d - measure Bourne shell elapsed times for "wasted" commands.
1N/A * Written for the sh DTrace provider.
1N/A *
1N/A * $Id: sh_wasted.d 25 2007-09-12 09:51:58Z brendan $
1N/A *
1N/A * USAGE: sh_wasted.d { -p PID | -c cmd } # hit Ctrl-C to end
1N/A *
1N/A * This script measures "wasted" commands - those which are called externally
1N/A * but are in fact builtins to the shell. Ever seen a script which calls
1N/A * /usr/bin/echo needlessly? This script measures that cost.
1N/A *
1N/A * FIELDS:
1N/A * FILE Filename of the shell or shellscript
1N/A * NAME Name of call
1N/A * TIME Total elapsed time for calls (us)
1N/A *
1N/A * IDEA: Mike Shapiro
1N/A *
1N/A * Filename and call names are printed if available.
1N/A *
1N/A * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
1N/A *
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License, Version 1.0 only
1N/A * (the "License"). You may not use this file except in compliance
1N/A * with the License.
1N/A *
1N/A * You can obtain a copy of the license at Docs/cddl1.txt
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * CDDL HEADER END
1N/A *
1N/A * 09-Sep-2007 Brendan Gregg Created this.
1N/A */
1N/A
1N/A#pragma D option quiet
1N/A
1N/Adtrace:::BEGIN
1N/A{
1N/A isbuiltin["echo"] = 1;
1N/A isbuiltin["test"] = 1;
1N/A /* add builtins here */
1N/A
1N/A printf("Tracing... Hit Ctrl-C to end.\n");
1N/A self->start = timestamp;
1N/A}
1N/A
1N/Ash$target:::command-entry
1N/A{
1N/A self->command = timestamp;
1N/A}
1N/A
1N/Ash$target:::command-return
1N/A{
1N/A this->elapsed = timestamp - self->command;
1N/A this->path = copyinstr(arg1);
1N/A this->cmd = basename(this->path);
1N/A}
1N/A
1N/Ash$target:::command-return
1N/A/self->command && !isbuiltin[this->cmd]/
1N/A{
1N/A @types_cmd[basename(copyinstr(arg0)), this->path] = sum(this->elapsed);
1N/A self->command = 0;
1N/A}
1N/A
1N/Ash$target:::command-return
1N/A/self->command/
1N/A{
1N/A @types_wasted[basename(copyinstr(arg0)), this->path] =
1N/A sum(this->elapsed);
1N/A self->command = 0;
1N/A}
1N/A
1N/Aproc:::exit
1N/A/pid == $target/
1N/A{
1N/A exit(0);
1N/A}
1N/A
1N/Adtrace:::END
1N/A{
1N/A this->elapsed = (timestamp - self->start) / 1000;
1N/A printf("Script duration: %d us\n", this->elapsed);
1N/A
1N/A normalize(@types_cmd, 1000);
1N/A printf("\nExternal command elapsed times,\n");
1N/A printf(" %-30s %-22s %8s\n", "FILE", "NAME", "TIME(us)");
1N/A printa(" %-30s %-22s %@8d\n", @types_cmd);
1N/A
1N/A normalize(@types_wasted, 1000);
1N/A printf("\nWasted command elapsed times,\n");
1N/A printf(" %-30s %-22s %8s\n", "FILE", "NAME", "TIME(us)");
1N/A printa(" %-30s %-22s %@8d\n", @types_wasted);
1N/A}