Skip to content

Instantly share code, notes, and snippets.

@Yoshyn
Last active March 14, 2019 15:15
Show Gist options
  • Save Yoshyn/241aa3d183f79bad62177482e42a2736 to your computer and use it in GitHub Desktop.
Save Yoshyn/241aa3d183f79bad62177482e42a2736 to your computer and use it in GitHub Desktop.
Script & help reminded for Dtrace & ruby
# Contains :
# * ruby_glob_analysis.d
Amazing quicksheet : http://www.tablespace.net/quicksheet/dtrace-quickstart.html
Amasome one-liner : https://wiki.freebsd.org/DTrace/One-Liners
Adding Probe dynamicly for ruby : https://github.com/kevinykchan/ruby-usdt
IP tracking : https://docs.oracle.com/cd/E36784_01/html/E36846/glhhr.html
# Main exemple :
ruby*:::method-entry
//execname == "ruby" && substr(copyinstr(arg2),0,38 ) == "/Users/sylvestre/codes/my_project/"/
// strstr(copyinstr(arg0), "/Users") != NULL -> Search for the first occurence ptr of '/user' into args0
{
printf("-pid(%d)-------------------cwd(%s)------------------\n", pid, cwd);
printf("execname(%s), probeprov(%s), probemod(%s), probefunc(%s), probename(%s)\n", execname, probeprov, probemod, probefunc, probename);
printf("-> %s::%s (%s:%d)\n", copyinstr(arg0), copyinstr(arg1), copyinstr(arg2), arg3);
printf("---------------------------------------\n");
printf("%s\n", substr(copyinstr(arg2),0,38 ));
}
// rm -f dtrace.log && sudo dtrace -q -s ruby_glob_analysis.d -p $( ps aux | grep 'rails s'| grep -v grep | awk '{print $2}') > dtrace.log
#!/usr/sbin/dtrace -s
#pragma D option quiet
BEGIN
{
start = timestamp;
}
ruby*:::method-entry
{
self->time = timestamp;
@rbclasses[this->class = copyinstr(arg0)] = count();
this->sep = strjoin(this->class, "#");
self->class_method = strjoin(this->sep, copyinstr(arg1));
@rbmethods_count[self->class_method] = count();
}
ruby*:::method-return
{
this->took = (timestamp - self->time) / 1000000;
@rbmethods_time[self->class_method] = sum(this->took);
}
ruby*:::object-create
{
@created_objects[copyinstr(arg0), copyinstr(arg1)] = count();
}
syscall:::entry
/execname=="ruby"/
{
@syscall_entries[probefunc, ustack(3)] = count();
}
END
{
normalize(@rbclasses, (timestamp - start) / 1000000000);
trunc(@rbclasses, 15);
printf("\nClasses called (top 15 normalized) :\n");
printf("---------------------------------------");
printf("--------------------------------+------\n");
printa(" %-68s | %@d\n", @rbclasses);
normalize(@rbmethods_count, (timestamp - start) / 1000000000);
trunc(@rbmethods_count, 15);
printf("\nMethods called (top 15 normalized) :\n");
printf("---------------------------------------");
printf("--------------------------------+------\n");
printa(" %-68s | %@d\n", @rbmethods_count);
normalize(@created_objects, (timestamp - start) / 1000000000);
trunc(@created_objects, 15);
printf("\nObject created (top 15 normalized) :\n");
printf("---------------------------------------");
printf("--------------------------------+------\n");
printa(" %-68s | %@d\n", @created_objects);
//printa(" %-68s | %-68s | %@d\n", @created_objects); See also file associated
normalize(@rbmethods_time, (timestamp - start) / 1000000000);
trunc(@rbmethods_time, 15);
printf("\nTime per method (top 15 normalized) :\n");
printf("---------------------------------------");
printf("--------------------------------+------\n");
printa(" %-68s | %@d\n", @rbmethods_time);
normalize(@syscall_entries, (timestamp - start) / 1000000000);
trunc(@syscall_entries, 15);
printf("\nSyscall method (top 15 normalized) :\n");
printf("---------------------------------------");
printf("--------------------------------+------\n");
printa(" %-68s | %@d\n", @syscall_entries);
//printa(@syscall_entries); See also the trace
}
// If concern relative with system call :
// 8thlight.com/blog/colin-jones/2015/12/01/ask-dtrace-why-are-my-tests-so-slow.html
// 1-Ensure : sudo dtrace -n 'syscall:::entry /execname=="ruby"/ { @[probefunc] = count(); } tick-1s { trunc(@,5); printa(@); trunc(@);}'
// 2-Find Why : sudo dtrace -n 'syscall::open:entry /execname=="ruby"/ { @[ustack()] = count(); } tick-1s { trunc(@,3); printa(@); trunc(@);}'
// 3-Find Where : sudo dtrace -n 'syscall::open:entry /execname=="ruby"/ { @[copyinstr(arg0)] = count(); } tick-1s { trunc(@,10); printa(@); trunc(@);}'
@Yoshyn
Copy link
Author

Yoshyn commented Mar 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment