1235368Sgnn#!/usr/sbin/dtrace -Zs
2235368Sgnn/*
3235368Sgnn * rb_malloc.d - Ruby operations and libc malloc statistics.
4235368Sgnn *               Written for the Ruby DTrace provider.
5235368Sgnn *
6235368Sgnn * $Id: rb_malloc.d 20 2007-09-12 09:28:22Z brendan $
7235368Sgnn *
8235368Sgnn * WARNING: This script is not 100% accurate; This prints libc malloc() byte
9235368Sgnn * distributions by "recent" Ruby operation, which we hope will be usually
10235368Sgnn * relevant. This is an experimental script that may be improved over time.
11235368Sgnn *
12235368Sgnn * USAGE: rb_malloc.d { -p PID | -c cmd }	# hit Ctrl-C to end
13235368Sgnn *
14235368Sgnn * FIELDS:
15235368Sgnn *		1		Filename of the Ruby program
16235368Sgnn *		2		Type of operation (method/objnew/startup)
17235368Sgnn *		3		Name of operation
18235368Sgnn *
19235368Sgnn * Filename and method names are printed if available.
20235368Sgnn *
21235368Sgnn * COPYRIGHT: Copyright (c) 2007 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 * 09-Sep-2007	Brendan Gregg	Created this.
38235368Sgnn */
39235368Sgnn
40235368Sgnn#pragma D option quiet
41235368Sgnn
42235368Sgnnself string filename;
43235368Sgnn
44235368Sgnndtrace:::BEGIN
45235368Sgnn{
46235368Sgnn	printf("Tracing... Hit Ctrl-C to end.\n");
47235368Sgnn}
48235368Sgnn
49235368Sgnnruby$target:::function-entry
50235368Sgnn{
51235368Sgnn	self->file = basename(copyinstr(arg2));
52235368Sgnn	self->type = "method";
53235368Sgnn	self->name = strjoin(strjoin(copyinstr(arg0), "::"), copyinstr(arg1));
54235368Sgnn}
55235368Sgnn
56235368Sgnnruby$target:::object-create-start
57235368Sgnn{
58235368Sgnn	self->file = basename(copyinstr(arg1));
59235368Sgnn	self->type = "objnew";
60235368Sgnn	self->name = copyinstr(arg0);
61235368Sgnn}
62235368Sgnn
63235368Sgnnpid$target:libc:malloc:entry
64235368Sgnn/self->file != NULL/
65235368Sgnn{
66235368Sgnn	@mallocs[self->file, self->type, self->name] = quantize(arg0);
67235368Sgnn}
68235368Sgnn
69235368Sgnnpid$target:libc:malloc:entry
70235368Sgnn/self->file == NULL/
71235368Sgnn{
72235368Sgnn	@mallocs["ruby", "startup", "-"] = quantize(arg0);
73235368Sgnn}
74235368Sgnn
75235368Sgnn
76235368Sgnndtrace:::END
77235368Sgnn{
78235368Sgnn	printf("Ruby malloc byte distributions by recent Ruby operation,\n");
79235368Sgnn	printa("   %s, %s, %s %@d\n", @mallocs);
80235368Sgnn}
81