Sun.com Dtrace for Web 2.0, JavaScript, PHP, and the rest

  • 4,218 views
Uploaded on

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • thanks for sharing..
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
4,218
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
25
Comments
1
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Dtrace Web2.0 Java, AJAX, PHP and the rest Peter Karlsson Sun Microsystems 1
  • 2. Agenda • Why should you care?? • Introduction to Dtrace • DTrace and AJAX > DTrace and JavaScript > Dtrace on the BackEnd – Java, PHP, Phyton, Ruby etc. > DTrace and Postgres • Putting it all together • Summary and Resources 2
  • 3. Why should you care • Isn't DTrace a Solaris thing, for kernel nerds and such • Nope, Dtrace now have providers for a large number of languages used in AJAX, such as: > JavaScript > Java > PHP > Phyton > Ruby • As well as for some databases such as Postgess 3
  • 4. Introduction to DTrace • DTrace was introduced in Solaris 10 • Allows for dynamic instrumentation of the OS and applications > A typical Solaris 10 system has around 35K probe points plus any probes that can be dynamically inserted in to ANY application running on the system • Available on any Solaris 10 system • Comes with a new dynamic language, D > D is used to scrip instrumentation wether it's for your application or you are looking at the system it self 4
  • 5. Introduction to DTrace, (Cont.) • Designed explicitly for use on production systems • Zero performance impact when not in use • Desgined with safty in mind, includes safegardes against panics, crashes, data corruption or pathological performance degradation • Powerful data management primitives eliminate need for most postprocessing, agaregations • Unwanted data is pruned as close to the source as possible 5
  • 6. The DTrace Revolution • DTrace tightens the diagnosis loop: hypothesis → instrumentation → data gathering → analysis → hypothesis ........ • Tightened loop effects a revolution in the way we diagnose transient failure • Focus can shift from instrumentation stage to hypothesis stage: > Much less labor intensive, less error prone > Much more brain intensive > Much more effective! (And a lot more fun) 6
  • 7. In a nutshell : DTrace architecture DTrace consumers script.d lockstat(1M) DTrace(1M) plockstat(1M) libDTrace(3LIB) userland DTrace(7D) kernel DTrace DTrace sysinfo vminfo fasttrap providers proc syscall sdt fbt 7
  • 8. The D Language • D is a C-like language specific to DTrace with some constructs similar to awk(1) • Global, thread-local and probe-local variables • Built-in variables like execname and timestamp • Predicates can use arbitrary expressions to select which data is traced and which is discarded • Actions to trace data, record stack backtraces, stop processes at points of interest, etc. 8
  • 9. Providers • A provider allows for instrumentation of a particular area of the system • Providers make probes available to the framework • Providers transfer control to the DTrace framework when an enabled probe is hit • DTrace has several providers, e.g.: > The pid provider for C and C++ applications > The hotspot and dvm provider for Java applications > The syscall provider for system calls > The io provider for system I/O > The profile provider for cyclical events 9
  • 10. D Language - Format. • When a probe fires then action is executed if predicate evaluates true • Example, “Print all the system calls executed by bash” #!/usr/sbin/dtrace -s probe description syscall:::entry / predicate / /execname==”bash”/ { { action statements printf(“%s calledn”,probefunc); } } 10
  • 11. Aggregations • Often the patterns are more interesting than each individual datum • Want to aggregate data to look for larger trends • DTrace supports the aggregation of data as a first class operation • An aggregation is the result of an aggregating function > count(), min(), max(), avg(), quantize() • May be keyed by an arbitrary tuple 11
  • 12. Aggregation - Format • @name[keys] = aggfunc(args); • '@' - key to show that name is an aggregation. • keys – comma separated list of D expressions. • aggfunc could be one of... > sum(expr) – total value of specified expression > count() – number of times called. > avg(expr) – average of expression > min(expr)/max(expr) – min and max of expressions > quantize()/lquantize() - power of two & linear distribution 12
  • 13. Aggregation Example #!/usr/sbin/dtrace -s pid$target:libc:malloc:entry { @["Malloc Distribution"]=quantize(arg0); } $ aggr_malloc.d -c who dtrace: script './aggr2.d' matched 1 probe ... dtrace: pid 1401 has exited Malloc Distribution value ------------- Distribution --------------------------------------------------count 1| 0 2 |@@@@@@@@@@@@@@@@@ 3 4| 0 8 |@@@@@@ 1 16 |@@@@@@ 1 32 | 0 64 | 0 128 | 0 256 | 0 512 | 0 1024 | 0 2048 | 0 4096 | 0 8192 |@@@@@@@@@@@ 2 16384 | 0 13
  • 14. Calculating time spent • One of the most common request is to find time spent in a given function • Here is how this can be done #!/usr/sbin/dtrace -s syscall::open*:entry, syscall::close*:entry { ts=timestamp; } syscall::open*:return, Whats wrong with this?? syscall::close*:return { timespent = timestamp - ts; printf("ThreadID %d spent %d nsecs in %s", tid, timespent, probefunc); ts=0; /*allow DTrace to reclaim the storage */ timespent = 0; } 14
  • 15. Thread Local Variable • self->variable = expression; > self – keyword to indicate that the variable is thread local > A boon to multi-threaded debugging > As name indicates this is specific to the thread. – See code re-written #!/usr/sbin/dtrace -s syscall::open*:entry, syscall::close*:entry { self->ts=timestamp; } syscall::open*:return, syscall::close*:return { timespent = timestamp - self->ts; printf("ThreadID %d spent %d nsecs in %s", tid, timespent, probefunc); self->ts=0; /*allow DTrace to reclaim the storage */ timespent = 0; } 15
  • 16. Built-in Variable • Here are a few built-in variables. arg0 ... arg9 – Arguments represented in int64_t format args[ ] - Arguments represented in correct type based on function. cpu – current cpu id. cwd – current working directory errno – error code from last system call gid, uid – real group id, user id pid, ppid, tid – process id, parent proc id & thread id probeprov, probemod, probefunc, probename - probe info. timestamp, walltimestamp, vtimestamp – time stamp nano sec from an arbitary point and nano sec from epoc. 16
  • 17. DTrace for scripting languages Dynamic Tracing for Dynamic development 17
  • 18. DTrace and Scripting Language • DTrace has been integrated into many scripting languages. • We will see two samples. > PHP > JavaScript • For this exercise we will use Coolstack version of PHP. > Get coolstack from > http://cooltools.sunsource.net/coolstack/ • Please note the DTrace providers are experimental and not official 18
  • 19. DTrace for PHP D tracing for P Hyper Processor 19
  • 20. DTrace and PHP • DTrace provider for PHP is a PECL/PEAR module • To get the provider # pear install dtrace or # pecl install dtrace then add “extension=dtrace.so” to the php.ini file then restart PHP 20
  • 21. DTrace and PHP • Couple links for more information • Wez Furlong who created the provider has details in http://netevil.org/node.php?uuid=42f82a8b-09b7-5013-1667-2f82a8b24ead • Bryan Cantrill has a nice demo script in his blog. http://blogs.sun.com/bmc/entry/dtrace_and_php_demonstrated 21
  • 22. DTrace and PHP • There are two probes in the provider. > function-return > function-entry • You can use the 5 args in the action. > arg0 = the function name > arg1 = the filename > arg2 = the line number > arg3 = classname (or an empty string) > arg4 = object/class operator (::, ->, or an empty string) 22
  • 23. DTrace and PHP • Here is a PHP example prime.php <?php $numprimes=10000; $pArray[0]=3; $test=5; $num=0; function doesDevide($x,$y) { return($x % $y); } function isprime($x) { global $pArray; global $num; $index=0; $check=1; while($check==1 && $index <= $num && $x >= ($pArray[$index] * $pArray[$index]) ) { if( doesDevide($x , $pArray[$index]) == 0) { $check=0; } else $index++; } return($check); } 23
  • 24. DTrace and PHP • Here is a PHP example(cont) while($num<$numprimes) { if(isPrime($test)==1){ $num++; $pArray[$num]=$test; if($num%1000==0){ printf("Progress done ...%dn",$num); } } $test+=2; } ?> 24
  • 25. DTrace and PHP • Here is a simple D-Script #!/usr/sbin/dtrace -Zqs php*:::function-entry { @[copyinstr(arg0)]=count(); } • Note: -Z will allow probes that have zero match. • Run this script in one window while you run the php script in another window. 25
  • 26. DTrace and PHP • Example run: ./php.d ^C printf 10 isprime 52378 doesDevide 684216 26
  • 27. DTrace for Javascript Get look in to the fire, Firefox that is....... 27
  • 28. DTrace and Javascript • DTrace probes have been added to Mozilla to help observe Javascript application. • Here is the list of the probes > js_execute-start and js_execute-done > js_function-entry & js_function-return > js_object-create, js_object-create-start, js_object-create-done & js_object-finalize > layout-start & layout-end 28
  • 29. DTrace and Javascript • Here is an example that prints java script function flow. #!/usr/sbin/dtrace -ZFs trace_mozilla*:::js_function-entry { trace(copyinstr(arg2)); } trace_mozilla*:::js_function-return { trace(copyinstr(arg2)); } 29
  • 30. Lets look at some JavaScript with DTrace 30
  • 31. DTrace & Postgres Observe post deploy of PostgreSQL 31
  • 32. DTrace probes in Postgres • Postgres 8.2 and later has following embedded D probes. > probe transaction__start(int); > probe transaction__commit(int); > probe transaction__abort(int); > probe lwlock__acquire(int, int); > probe lwlock__release(int); > probe lwlock__startwait(int, int); > probe lwlock__endwait(int, int); > probe lwlock__condacquire(int, int); > probe lwlock__condacquire__fail(int, int); > probe lock__startwait(int, int); > probe lock__endwait(int, int); 32
  • 33. How to get Postgres with DTrace • Get Solaris Express Developer Edition 9/07 • OR • Download the source > For 32-bit version – ./configure –enable-dtrace > For 64 bit: – $ configure CC='gcc -m64' –enable-dtrace DTRACEFLAGS='-64' – $ configure CC='/opt/SUNWspro/bin/cc -xtarget=native64' --enable-dtrace DTRACEFLAGS='-64' > Run make or gmake. 33
  • 34. Get started with Postgres • Here's how you'd run Postgres 8.2: • 1) As root, su to postgres # su – postgres • 2) Create Postgres DB cluster $ /usr/postgres/8.2/bin/initdb -D /var/postgres/8.2/data • 3) As root, use the SMF's svadm command to start Postgres # /usr/sbin/svcadm enable postgresql:version_82 34
  • 35. DTrace and PostgreSQL • Create the bench db $ createdb bench • Populate the data in db $ pgbench -i -s 5 bench • Run the benchmark $ pgbench -c 2 -t 400000 bench • Find the pids of the postgres process. $ pgrep -l postgres 35
  • 36. DTrace and PostgreSQL • Here are a few example D-scripts. #!/usr/sbin/dtrace -Zqs postgresql*:::transaction-start { self->ts=timestamp; postgres_avg_query_time.d @cnt[pid]=count(); } postgresql*:::transaction-commit { @avg[pid]=avg(timestamp - self->ts); } tick-5sec { normalize(@avg, 1000000); printf("%15s %30s %30sn","PID","Total queries","Avegrage time (ms)"); printf("t======================================================================n"); printa("%15d %@30d %@30dn",@cnt,@avg); printf("t======================================================================nn"); clear(@cnt); clear(@avg); } 36
  • 37. DTrace and PostgreSQL • Example output # ./postgres_avg_query_time.d PID Total queries Avegrage time (ms) ================================================================== 23814 46 57 23817 58 34 23816 59 32 23815 59 33 23818 75 26 ================================================================== 37
  • 38. DTrace and PostgreSQL • Here is a simple script to print all the SQL statements executed. #!/usr/sbin/dtrace -Zwqs postgres_queries.d BEGIN { freopen(“sql.output”); } pid$1::pg_parse_query:entry { printf("%sn",copyinstr(arg0)); } • This script will send its output to a file. sql.output 38
  • 39. DTrace and PostgreSQL • Sample output: BEGIN; UPDATE accounts SET abalance = abalance + 344 WHERE aid = 212898; SELECT abalance FROM accounts WHERE aid = 212898; UPDATE tellers SET tbalance = tbalance + 344 WHERE tid = 22; UPDATE branches SET bbalance = bbalance + 344 WHERE bid = 3; INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (22, 3, 212898, 344, CURRENT_TIMESTAMP); END; BEGIN; UPDATE accounts SET abalance = abalance + 15549 WHERE aid = 474266; SELECT abalance FROM accounts WHERE aid = 474266; UPDATE tellers SET tbalance = tbalance + 15549 WHERE tid = 19; UPDATE branches SET bbalance = bbalance + 15549 WHERE bid = 5; INSERT INTO history (tid, bid, aid, delta, mtime) VALUES (19, 5, 474266, 15549, CURRENT_TIMESTAMP); END; 39
  • 40. DTrace and PostgreSQL • More info on PostgreSQL and DTrace • Look at Robert Lor's Blog http://blogs.sun.com/robertlor/ • For more examples see http://pgfoundry.org/projects/dtrace/ • Documentation http://www.postgresql.org/docs/8.2/interactive/dynamic-trace.html 40
  • 41. The END Peter Karlsson Technology Evangelist Sun Microsystems 41