Saturday, October 18, 2008

Adding D-Trace to Code Base 0.1 Release

For those who need a reminder of what I am doing: My Project Page

This post recognizes that I have released my 0.1 to the public. I have released:


  • Mac OS X 10.5 (Leopard) and OpenSolaris only
  • Non-extension based
  • Instructions to compile Mozilla for D-Tracing
  • Test script to interact with existing probes


Basic Instructions to compiling this are as follows (more advanced and well-explained instructions are on my 0.1 Release - link is at the bottom of the page:




  1. Download the Mozilla source tree. The version must be >= 3.0.3
  2. Extract the tarball at your home directory.
  3. Within your .mozconfig file. Add this line: ac_add_options --enable-dtrace along with your other options
  4. Make sure your $PATH variable has at least:
    export PATH=/usr/include/sys:/usr/include:/opt/local/bin:/opt/local/sbin:$PATH
  5. Compile the source tree.
  6. Go to your proper executable and run firefox-bin (mozilla/dist/bin is OpenSolaris, mozilla/dist/Minefield.app/Contents/MacOS/
  7. On a second terminal, create a file called js_callcount.d and add this code to it (use a 2nd terminal than the one handling Minefield):

    #pragma D option quiet

    dtrace:::BEGIN
    {
    printf("Tracing... Hit Ctrl-C to end.\n");
    }

    javascript*:::function-entry
    {
    @funcs[basename(copyinstr(arg0)), copyinstr(arg2)] = count();
    }

    dtrace:::END
    {
    printf(" %-32s %-36s %8s\n", "FILE", "FUNC", "CALLS");
    printa(" %-32s %-36s %@8d\n", @funcs);
    }

  8. Compile and run this script with the second terminal using:
    [sudo] dtrace -s js_callcount.d
  9. Perform some actions like go to a different web page, hit some buttons, do a Google search etc. without closing Minefield.
  10. When you think you are done, go to the 2nd terminal and kill the script using Ctrl+C. The output will tell you how many functions were called in each file the D-Trace script picked up on.


You may optinally use the command
[sudo] dtrace -n 'javascript*' -l

to display any currently active probes. This will only work while Mozilla is running.

I did a test run of it myself, doing the following scenario:
  1. Ran Minefield on first terminal. Created a profile called DTrace and used it
  2. Ran my D-Script on my second terminal.
  3. On Minefield - typed [http://www.wikipedia.org/wiki/Santino_Marella http://www.wikipedia.org/wiki/Santino_Marella] and hit Enter.
  4. Clicked on the first instance of RAW (should be in basic description field on top of wiki).
  5. Ctrl+C'd out of my D-Script on second terminal.
  6. Closed Minefield and Ctrl+C'd out of my first terminal.


And got these results:
//DTrace::BEGIN
Tracing... Hit Ctrl-C to end.

//Started picking up on my Minefield interactions

//DTrace::END
FILE FUNC CALLS
Santino_Marella writeln 2
WWE_Raw writeln 2
autocomplete.xml QueryInterface 2
autocomplete.xml attachController 2
autocomplete.xml getBoundingClientRect 2
autocomplete.xml getComplexValue 2
autocomplete.xml getIntPref 2
autocomplete.xml handleEnter 2
autocomplete.xml setConsumeRollupEvent 2
autocomplete.xml stopSearch 2
..continues on
autocomplete.xml hasChildNodes 334
autocomplete.xml substr 338
autocomplete.xml setAttribute 344
autocomplete.xml parseInt 508
autocomplete.xml min 592
autocomplete.xml push 610
javascript&smaxage=21600&maxage=86400 substr 712
autocomplete.xml getAttribute 1506
autocomplete.xml charCodeAt 3876


The probe that handles function-entry (basically when a function is called) seems to handle more on browser.js (was cut off, but appeared) and the xml files than the other locations. A person would need this information to make sure that thier functions are being called the right amount of times. Which helps if a user needs to write more functions, cut off ones that are least used, or access more that can be used to make sure they are "earning thier keep".

To improve on 0.1 so that I can work with 0.2, I only have one thing. There was a patch released on Bugzilla Bug 370906 in Post 20/23 that renames the Probe conventions from javascript* to trace_mozilla*. This is helpful if you have 2 programs running that use the same naming conventions, and maybe the same ID (the * is a wildcard). I was able to run it on Solaris, but not on Mac. I didn't post the instructions that way because I wanted to make sure that I was cross-platform compliant. I want to try to figure out how to get it running on Mac. But I fear that Sayrer (the man who suggested that this technology would be useful for Mozilla) may not like it, or he knows how to solve my problem.

For 0.2, I plan on actually writing custom probes for other sections of the Mozilla Code base. Hopefully, where Sayrer wants them to go. Then I am gonna remodify the above script to check for these "custom probes".

I have written better instructions and easier to copy code on this 0.1 Release wiki. I apologize if my pre tags cut off the code on the screen. It is the way this page is rendered at 1024x768 that bugs me.

No comments: