summaryrefslogtreecommitdiffstats
path: root/cddl/contrib/dtracetoolkit/Misc
diff options
context:
space:
mode:
Diffstat (limited to 'cddl/contrib/dtracetoolkit/Misc')
-rw-r--r--cddl/contrib/dtracetoolkit/Misc/Readme5
-rwxr-xr-xcddl/contrib/dtracetoolkit/Misc/guess.d118
-rwxr-xr-xcddl/contrib/dtracetoolkit/Misc/woof.d63
-rwxr-xr-xcddl/contrib/dtracetoolkit/Misc/wpm.d143
4 files changed, 329 insertions, 0 deletions
diff --git a/cddl/contrib/dtracetoolkit/Misc/Readme b/cddl/contrib/dtracetoolkit/Misc/Readme
new file mode 100644
index 0000000..2b77f60
--- /dev/null
+++ b/cddl/contrib/dtracetoolkit/Misc/Readme
@@ -0,0 +1,5 @@
+Extra - Extra DTrace scripts
+
+ These are scripts that fall into no other category. They probably aren't
+ very useful, and are here as a particular coding example rather than
+ a useful tool.
diff --git a/cddl/contrib/dtracetoolkit/Misc/guess.d b/cddl/contrib/dtracetoolkit/Misc/guess.d
new file mode 100755
index 0000000..3ebcd39
--- /dev/null
+++ b/cddl/contrib/dtracetoolkit/Misc/guess.d
@@ -0,0 +1,118 @@
+#!/usr/sbin/dtrace -wqs
+/*
+ * guess.d - guessing game in D (DTrace)
+ *
+ * $Id: guess.d 32 2007-09-15 05:08:49Z brendan $
+ *
+ * USAGE: guess.d
+ *
+ * SEE: http://www.brendangregg.com/guessinggame.html
+ *
+ * This is written to demonstrate this language versus the same program
+ * written in other languages.
+ *
+ * 11-May-2005 Brendan Gregg Created this.
+ */
+
+inline string scorefile = "highscores_d";
+
+dtrace:::BEGIN
+{
+ printf("guess.d - Guess a number between 1 and 100\n\n");
+ num = 1;
+ state = 1;
+
+ /* Generate random number */
+ answer = (rand() % 100) + 1;
+ answer = answer > 0 ? answer : - answer;
+}
+
+syscall::write:entry
+/state == 1 && pid == $pid/
+{
+ state = 2;
+ printf("Enter guess %d: ", num);
+ system("read guess");
+ pos = 0;
+}
+
+syscall::read:entry
+/state == 2 && ppid == $pid && arg0 == 3/
+{
+ self->inguess = 1;
+ self->buf = arg1;
+}
+
+syscall::read:return
+/self->inguess/
+{
+ key = copyin(self->buf, arg0);
+ keys[pos] = *(char *)key;
+ self->buf = 0;
+ pos++;
+}
+
+syscall::read:return
+/self->inguess && keys[pos-1] == '\n'/
+{
+ pos -= 2;
+ fac = 1;
+ guess = fac * (keys[pos] - '0');
+ pos--;
+ fac *= 10;
+ guess = pos >= 0 ? guess + fac * (keys[pos] - '0') : guess;
+ pos--;
+ fac *= 10;
+ guess = pos >= 0 ? guess + fac * (keys[pos] - '0') : guess;
+ self->doneguess = 1;
+}
+
+syscall::read:return
+/self->inguess/
+{
+ self->inguess = 0;
+}
+
+/* Play game */
+syscall::read:return
+/self->doneguess && guess == answer/
+{
+ printf("Correct! That took %d guesses.\n\n", num);
+ self->doneguess = 0;
+ state = 3;
+ printf("Please enter your name: ");
+ system("/usr/bin/read name");
+}
+
+syscall::read:return
+/self->doneguess && guess != answer/
+{
+ num++;
+
+ printf("%s...\n", guess < answer ? "Higher" : "Lower");
+
+ printf("Enter guess %d: ", num);
+ system("read line");
+ pos = 0;
+}
+
+syscall::read:entry
+/state == 3 && curthread->t_procp->p_parent->p_ppid == $pid && arg0 == 0/
+{
+ self->inname = 1;
+ self->buf = arg1;
+}
+
+/* Save high score */
+syscall::read:return
+/self->inname/
+{
+ self->inname = 0;
+ name = stringof(copyin(self->buf, arg0 - 1));
+ system("echo %s %d >> %s", name, num, scorefile);
+
+ /* Print high scores */
+ printf("\nPrevious high scores,\n");
+ system("cat %s", scorefile);
+ exit(0);
+}
diff --git a/cddl/contrib/dtracetoolkit/Misc/woof.d b/cddl/contrib/dtracetoolkit/Misc/woof.d
new file mode 100755
index 0000000..d856a09
--- /dev/null
+++ b/cddl/contrib/dtracetoolkit/Misc/woof.d
@@ -0,0 +1,63 @@
+#!/usr/sbin/dtrace -s
+/*
+ * woof.d - Bark whenever new processes appear. Needs /dev/audio.
+ * Written in DTrace (Solaris 10 3/05).
+ *
+ * $Id: woof.d 3 2007-08-01 10:50:08Z brendan $
+ *
+ * USAGE: woof.d &
+ *
+ * SEE ALSO: /usr/dt/bin/sdtaudiocontrol # to set volume
+ *
+ * COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
+ *
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at Docs/cddl1.txt
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * CDDL HEADER END
+ *
+ * 14-Aug-2006 Brendan Gregg Created this.
+ * 14-Aug-2006 " " Last update.
+ */
+
+#pragma D option quiet
+#pragma D option destructive
+#pragma D option switchrate=10hz
+
+inline int SCREEN_OUTPUT = 0; /* Set to 1 for screen output */
+
+/* barks prevents woof.d from barking too much (up to 20 barks/second) */
+int barks;
+
+dtrace:::BEGIN
+{
+ SCREEN_OUTPUT ? trace("Beware of the dog!\n") : 1;
+}
+
+/*
+ * Call the shell to run a background audioplay command (cat > /dev/audio
+ * doesn't always work). One problem this creates is a feedback loop,
+ * where we bark at our own barks, or at other dogs barks; entertaining
+ * as this is, it can really slog the system and has been avoided by
+ * checking our ancestory.
+ */
+proc:::exec-success
+/!progenyof($pid) && barks++ < 2/
+{
+ SCREEN_OUTPUT ? trace("Woof! ") : 1;
+ system("audioplay /usr/share/audio/samples/au/bark.au &");
+}
+
+profile:::tick-10hz
+{
+ barks = 0;
+}
diff --git a/cddl/contrib/dtracetoolkit/Misc/wpm.d b/cddl/contrib/dtracetoolkit/Misc/wpm.d
new file mode 100755
index 0000000..7f3bff2
--- /dev/null
+++ b/cddl/contrib/dtracetoolkit/Misc/wpm.d
@@ -0,0 +1,143 @@
+#!/usr/sbin/dtrace -s
+/*
+ * wpm.d - Measure words per minute of typing.
+ * Written in DTrace (Solaris 10 3/05).
+ *
+ * $Id: wpm.d 52 2007-09-24 04:28:01Z brendan $
+ *
+ * USAGE: wpm.d commandname
+ * eg,
+ * wpm.d bash
+ * wpm.d vim
+ *
+ * This script assumes that keystrokes arrive one at a time on STDIN. This
+ * isn't the case for all processes that read keyboard input (eg, sh).
+ *
+ * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
+ *
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at Docs/cddl1.txt
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * CDDL HEADER END
+ *
+ * 05-Aug-2007 Brendan Gregg Created this.
+ */
+
+#pragma D option quiet
+#pragma D option switchrate=10
+#pragma D option defaultargs
+
+inline int STDIN = 0;
+
+enum tracing_state {
+ BEGIN,
+ TRACING
+};
+
+dtrace:::BEGIN
+/$$1 == ""/
+{
+ trace("USAGE: wpm.d commandname\n");
+ trace(" eg,\n");
+ trace(" wpm.d bash\n");
+ trace(" wpm.d vim\n");
+ exit(1);
+}
+
+dtrace:::BEGIN
+{
+ state = BEGIN;
+ keys = 0;
+ words = 0;
+ wordsize = 0;
+ countdown = 5;
+ last = 0;
+ printf("Measuring will start in : %2d seconds", countdown);
+}
+
+profile:::tick-1sec
+/--countdown >= 0/
+{
+ printf("\b\b\b\b\b\b\b\b\b\b%2d seconds", countdown);
+}
+
+profile:::tick-1sec
+/state == BEGIN && countdown == -1/
+{
+ state = TRACING;
+ countdown = 60;
+ printf("\nMeasuring will stop in : %2d seconds", countdown);
+}
+
+syscall::read:entry
+/state == TRACING && execname == $$1 && arg0 == STDIN/
+{
+ self->buf = arg1;
+}
+
+syscall::read:return
+/self->buf && last/
+{
+ this->elapsed = (timestamp - last) / 1000000;
+ @dist = quantize(this->elapsed);
+ @avg = avg(this->elapsed);
+ @min = min(this->elapsed);
+ @max = max(this->elapsed);
+}
+
+syscall::read:return
+/self->buf/
+{
+ keys++;
+ wordsize++;
+ this->key = stringof(copyin(self->buf, arg0));
+ last = timestamp;
+}
+
+syscall::read:return
+/self->buf && (this->key == " " || this->key == "\n" || this->key == "\r") &&
+ wordsize == 1/
+{
+ /* recurring space */
+ wordsize = 0;
+ self->buf = 0;
+}
+
+syscall::read:return
+/self->buf && (this->key == " " || this->key == "\n" || this->key == "\r")/
+{
+ words++;
+ @sizes = lquantize(wordsize - 1, 0, 32, 1);
+ wordsize = 0;
+}
+
+syscall::read:return
+/self->buf/
+{
+ self->buf = 0;
+}
+
+profile:::tick-1sec
+/state == TRACING && countdown == -1/
+{
+ printf("\n\nCharacters typed : %d\n", keys);
+ printf("Words per minute : %d\n\n", words);
+
+ printa("Minimum keystroke latency : %@d ms\n", @min);
+ printa("Average keystroke latency : %@d ms\n", @avg);
+ printa("Maximum keystroke latency : %@d ms\n\n", @max);
+
+ printa("Word size distribution (letters),\n%@d\n", @sizes);
+ printa("Keystroke latency distribution (ms),\n%@d\n", @dist);
+
+ exit(0);
+}
OpenPOWER on IntegriCloud