dtrace topics java

Upload: sumanth

Post on 23-Feb-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 Dtrace Topics Java

    1/34

    1

    # ./jflow.d

    Greeting.greet

    -> java/io/PrintStream.println

    -> java/io/PrintStream.print

    -> java/io/PrintStream.write

    -> java/io/PrintStream.ensureOpen java/io/riter.write

    -> java/io/!ufferedriter.write

    -> java/io/!ufferedriter.ensureO

    java/io/!ufferedriter.min

    java/lang/String.get"hars

    -> java/lang/Sstem.arra$op

  • 7/24/2019 Dtrace Topics Java

    2/34

    2

    DTrace Topics: Java

    This presentation is about DTrace and Java, and ispart of the DTrace Topics coection!

    These sides cover"

    > DTrace #ecap> Java and DTrace

    > The hotspot $rovider

    > hotspot %&ampes

    > #esources

  • 7/24/2019 Dtrace Topics Java

    3/34

    '

    DTrace Recap

    ( )enera understandin) of DTrace is assumed*no+ed)e for this presentation!

    f you are ne+ to DTrace, try startin) +ith the

    presentation caed DTrace Topics" ntro! The ne&t t+o sides are a short summary of DTrace,

    if needed!

  • 7/24/2019 Dtrace Topics Java

    4/34

    -

    What is DTrace

    DTrace is a dynamic troubeshootin) and anaysistoo first introduced in the Soaris 10 and.penSoaris operatin) systems!

    DTrace is many thin)s, in particuar"> ( tool, /usr/sbin/dtrace

    > ( programming languageinterpreter, the D an)ua)e

    > (n instrumentationframe+or*

    DTrace operates +ith o+ overhead +hen in use,and ero overhead +hen not!

    DTrace is desi)ned to be safe for production use!

  • 7/24/2019 Dtrace Topics Java

    5/34

    What is DTrace

    DTrace can observe the entire soft+are stac* fromone too! t is i*e a combination of,> truss, sotruss, apptrace, mdb, oc*stat, pre&/tnf, 3, a+*

    root and users +ith DTrace privie)es can use it! DTrace traces events at dynamic instrumentation

    points caed probes! There are thousands of them!

    providersare ibraries of reated probes! 4hen probes fire, arbitry actionscan be performed,

    > %)" print functions and ar)uments, measure atencies,process data, +a* process and *erne memory, !!!

  • 7/24/2019 Dtrace Topics Java

    6/34

    5

    Java and DTrace

    n the first reease of Soaris 10, DTrace provided ajstack()action to read Java stac* traces!

    6or JD 1!-!2 and !0, prototype DTrace Java

    providers +ere reeased as oadabe 8M a)entibraries, first named djvm then dvm!

    The hotspotDTrace Java provider +as inte)ratedin JD 5!0, ready for immediate use!

  • 7/24/2019 Dtrace Topics Java

    7/347

    jstack()

    This can be used +ith"> The profie provider, to sampe fre9uent stac* traces!

    This can identify Java code hot spots!

    >

    The pid provider, to sho+ ho+ Java interacts +ith useribraries!

    > The sysca provider, to determine +hat Java causes thesystem to do!

  • 7/24/2019 Dtrace Topics Java

    8/34:

    jstack() example

    The foo+in) sho+s whyJava caused a read;

  • 7/24/2019 Dtrace Topics Java

    9/34=

    djvm/dvm Provider

    f possibe, move the appication to JD 5!0 and usethe inte)rated hotspot provider!

    f you are stuc* on JD 1!-!2 or !0, you can try the

    d>vm/dvm prototype provider! They re9uire command ine confi)uration and the

    appication to be restarted!

    The provider can be do+noaded from,> https"//soaris10?dtrace?vm?a)ents!dev!>ava!net

    Some e&ampes of its usa)e are here,> http"//bo)s!sun!com/ah/entry/dtracin)@>ava

    https://solaris10-dtrace-vm-agents.dev.java.net/http://blogs.sun.com/ahl/entry/dtracing_javahttp://blogs.sun.com/ahl/entry/dtracing_javahttps://solaris10-dtrace-vm-agents.dev.java.net/
  • 7/24/2019 Dtrace Topics Java

    10/3410

    hotspot Provider Probes

    hotspot provides numerous probes, incudin)"> 3ass events,class-loaded ( cass oadedclass-unloaded ( cass unoaded

    > Method invocation,method-entry ( method be)insmethod-return ( method competed

    > .b>ect events,object-alloc (n ob>ect +as aocated

    > Aarba)e coect,gc-begin System +ide A3 be)insgc-end System +ide A3 ended

  • 7/24/2019 Dtrace Topics Java

    11/3411

    hotspot Provider Probes

    > Thread events,thread-start ( thread has startedthread-stop ( thread competed

    > Monitor events

    > 8M events

    The method-*probes can de)rade performance,and are ony enabed +ith the 8M fa)

    ExtendedDTraceProbes!

    The fu reference for probes and their ar)uments is"http"//>ava!sun!com/>avase/5/docs/technotes/)uides/vm/dtrace!htm

    http://java.sun.com/javase/6/docs/technotes/guides/vm/dtrace.htmlhttp://java.sun.com/javase/6/docs/technotes/guides/vm/dtrace.html
  • 7/24/2019 Dtrace Topics Java

    12/3412

    hotspot Example !

    The hotspot provider +i be demonstrated bytracin) a simpe Java pro)ram!

    The foo+in) Areetin)!>ava code may oo* famiiar,

    many Java tutorias be)in +ith somethin) simiar"

    J $at Greeting.java

    pu9li$ $lass Greeting +

    pu9li$ void greet + Sstem.out.println*Kello Tra$eL*

    0

    0

    Greeting class

  • 7/24/2019 Dtrace Topics Java

    13/341'

    hotspot Example !

    Bo+ the test harness"

    J $at TestGreeting.java

    pu9li$ $lass TestGreeting +

    pu9li$ stati$ void mainString12 args +

    Greeting hello ) new Greeting

    while true +

    hello.greet

    tr +

    Thread.$urrentThread.sleep:333

    0 $at$h Bnterrupted%($eption e + 0

    0

    0

    0

    TestGreeting class

    call greet method

    everysecond

  • 7/24/2019 Dtrace Topics Java

    14/341-

    hotspot Example !

    3ompiin), e&ecutin)"

    This simpe pro)ram produces some *no+n eventsthat +e can trace!

    J java$ TestGreeting.java

    J java TestGreeting

    Kello Tra$eL

    Kello Tra$eLKello Tra$eL

    Kello Tra$eL

    M"

    slowly scrolling output

  • 7/24/2019 Dtrace Topics Java

    15/341

    Example !: jstack()

    4e +i start +ith >stac*;< ;not hotspotC

    # dtra$e -n &ss$all''write'entr /e(e$name )) *java*/ + jsta$, 0&

    3 45675 write'entr

    li9$.so.:;write=3(4 li9jvm.so;:$hpiCwriteDCip,vBB=3(a3

    li9jvm.so;EFNrite=3(8D

    li9java.so;write!tes=3(:57

    li9java.so;EavajavaioCileOutputStreamwrite!tes=3(8f

    java/io/CileOutputStream.write!tes1!BBF

    java/io/CileOutputStream.write1!BBF java/io/!ufferedOutputStream.flush!ufferF

    java/io/!ufferedOutputStream.flushF

    java/io/PrintStream.write1!BBF

    sun/nio/$s/Stream%n$oder.write!tesF

    sun/nio/$s/Stream%n$oder.implClush!ufferF

    1...$ontinued...2

  • 7/24/2019 Dtrace Topics Java

    16/3415

    Example !: jstack()

    > #ead the stac* trace above Areetin)!)reet carefuy!

    o+ many Java casses do you reco)nieE

    sun/nio/$s/Stream%n$oder.flush!ufferF

    java/io/OutputStreamriter.flush!ufferF

    java/io/PrintStream.newHineF

    java/io/PrintStream.printlnHjava/lang/StringF

    Greeting.greetF

    TestGreeting.main1Hjava/lang/StringF

    Stu9?out

    li9jvm.so;:$EEava"allsH$allhelperDCpnEEavaFaluepnNmethodKan

    li9jvm.so;:$"osose($eptionwrapperDCpCpnEEavaFaluepnNmetho

    li9jvm.so;:$EEava"alls%$allDCpnEEavaFaluenNmethodKandlepn?E

    li9jvm.so;:$?jniinvo,estati$DCpnKEB%nvpnEEavaFaluepnBj

    li9jvm.so;jni"allStati$FoidNethod=3(:5d

    java;EavaNain=3(d83 li9$.so.:;thrsetup=3(5@

    li9$.so.:;lwpstart

    Ah-ha

  • 7/24/2019 Dtrace Topics Java

    17/3417

    Example !: "istin# Probes

    Bo+ to see +hat the hotspotprovider can do"

    > 101- probes, -=: uni9ue probe names FF deep visibiityC

    # dtra$e -ln &hotspotQ'''&

    B P?OFB%? NOH% C"TBO IN%

    5@:38 hotspotjni@4A88A li9jvm.so jniGetO9je$t?efTpe GetO9

    5@:37 hotspotjni@4A88A li9jvm.so jniGetO9je$t?efTpe GetO95@:35 hotspotjni@4A88A li9jvm.so jniGetPrimitiveIrra"riti$al GetPr

    5@:3D hotspotjni@4A88A li9jvm.so jniGetPrimitiveIrra"riti$al GetPr

    5@:34 hotspotjni@4A88A li9jvm.so jniGetShortIrra%lements GetSh

    1...2

    # dtra$e -ln &hotspotQ'''& R w$ -l

    :3:5# dtra$e -ln &hotspotQ'''& R aw, &+print J50& R sort -u R w$ -l

    766

  • 7/24/2019 Dtrace Topics Java

    18/341:

    Example !: Tracin# Probes

    4atchin) a enabed hotspot events"

    .utput scros rather fast!!!

    # dtra$e -n &hotspotQ'''&

    dtra$e' des$ription &hotspotQ'''& mat$hed :3:7 pro9es

    "P B C"TBO'IN%

    3 DD7D5 jniGetIrraHength'GetIrraHength-entr

    3 DD7DD jniGetIrraHength'GetIrraHength-return

    3 DD5@6 jniGetO9je$tCield'GetO9je$tCield-entr

    3 DD583 jniGetO9je$tCield'GetO9je$tCield-return

    3 DD5@6 jniGetO9je$tCield'GetO9je$tCield-entr

    3 DD583 jniGetO9je$tCield'GetO9je$tCield-return

    3 DD745 jniGet!teIrra?egion'Get!teIrra?egion-entr

    3 DD74D jniGet!teIrra?egion'Get!teIrra?egion-return

    3 DD75D jni%($eptionO$$urred'%($eptionO$$urred-entr

    3 DD754 jni%($eptionO$$urred'%($eptionO$$urred-return

    1...2

  • 7/24/2019 Dtrace Topics Java

    19/341=

    Example !: $##re#atin# Probes

    > nterestin), but some e&pected probes are missin) ;e),method?entry

    # dtra$e -n &hotspotQ''' + 1pro9ename2 ) $ount 0&

    dtra$e' des$ription &hotspotQ''' & mat$hed :3:7 pro9es

    M"

    %($eptionO$$urred-entr A

    %($eptionO$$urred-return A

    GetIrraHength-entr A

    GetIrraHength-return A

    Get!teIrra?egion-entr A

    Get!teIrra?egion-return A

    GetO9je$tCield-entr :D

    GetO9je$tCield-return :D

  • 7/24/2019 Dtrace Topics Java

    20/34

    20

    Example !: Extended Probes

    %&tended probes are not activated by defaut asthey may de)rade performance on busy apps!

    They can be enabed usin),

    > (n option to >ava" ?GG"H%&tendedDTrace$robes> (n option to >info" ?fa) H%&tendedDTrace$robes

    # java -'=%(tendedTra$ePro9es TestGreeting

    Kello Tra$eL

    Kello Tra$eL

    Kello Tra$eL

    1...2

  • 7/24/2019 Dtrace Topics Java

    21/34

    21

    Example !: Extended Probes

    > Bo+ +e see method?entry and method?return, +hichoccurred -=5 times +hie this +as tracin) ;- seconds

    # dtra$e -n &hotspotQ''' + 1pro9ename2 ) $ount 0&dtra$e' des$ription &hotspotQ''' & mat$hed :3:7 pro9es

    M"

    %($eptionO$$urred-entr A

    %($eptionO$$urred-return A

    GetIrraHength-entr A GetIrraHength-return A

    Get!teIrra?egion-entr A

    Get!teIrra?egion-return A

    o9je$t-allo$ A

    GetO9je$tCield-entr :D

    GetO9je$tCield-return :D

    method-entr 76D

    method-return 76D

  • 7/24/2019 Dtrace Topics Java

    22/34

    22

    Example !: Tracin# %ethods

    The cass and method name can be fetched fromthe probe ar)uments"

    # dtra$e -n &hotspotQ'''method-entr + printf*-> U7s.UsVn*W

    stringof$opinarg:W arg@W stringof$opinarg8W arg7 0&

    -> Greeting.greet-> java/io/PrintStream.println

    -> java/io/PrintStream.print

    -> java/io/PrintStream.write

    -> java/io/PrintStream.ensureOpen

    -> java/io/riter.write

    -> java/io/!ufferedriter.write-> java/io/!ufferedriter.ensureOpen

    -> java/io/!ufferedriter.min

    -> java/lang/String.get"hars

    -> java/lang/Sstem.arra$op

    -> java/io/!ufferedriter.flush!uffer

    1...2

    !ive class"method calls

  • 7/24/2019 Dtrace Topics Java

    23/34

    2'

    Example !: $##re#atin# %ethods

    > dentify most fre9uenty caed methods!

    # dtra$e -n &hotspotQ'''method-entr + $alls1stringof$opinarg:W arg@W

    stringof$opinarg8Warg72 ) $ount 0

    % + printa*U7As.U-@7s U7dVn*W $alls 0&

    M"

    Greeting.greet @

    java/io/!ufferedriter.ensureOpen @

    java/io/!ufferedriter.minureOpen @ java/io/!ufferedriter.writeeOpen @

    java/io/!ufferedriteropflush!uffer.ensureOpenr @

    java/io/!ufferedriteropinureOpen.flush!uffer @

    java/io/OutputStreamritersureOpenr.write @

    java/io/PrintStream.ensureOpen @

    1...trun$ated...2java/nio/"har!ufferJ%n$oarraOffsetodeIrraHoop.arraOffsetodeIrraHoop @3

    java/nio/!te!ufferJ%n$oarraOffsetodeIrraHoop.arraOffsetodeIrraHoop 83

  • 7/24/2019 Dtrace Topics Java

    24/34

    2-

    Example !: $##re#atin# %ethods

    4ait a sec,

    These donIt oo* ri)ht the strin)s have been corrupted!

    f you see such output, the end of the copyin strin)needs to be manuay terminated!

    1...2

    java/nio/"har!ufferJ%n$oarraOffsetodeIrraHoop.arraOffsetodeIrraHoop @3

    java/nio/!te!ufferJ%n$oarraOffsetodeIrraHoop.arraOffsetodeIrraHoop 83

  • 7/24/2019 Dtrace Topics Java

    25/34

    2

    Example !: $##re#atin# %ethods

    Bo+ a script is +ritten to a))re)ate methods +ithstrin) termination"

    # ./jagg.d

    Tra$ing... Kit "trl-" to end.

    M"

    Greeting.greet 8

    java/io/!ufferedriter.newHine 8

    java/io/PrintStream.newHine 8

    java/io/PrintStream.print 8

    java/io/PrintStream.println 8

    java/lang/Thread.$urrentThread 8

    1...2

    java/nio/$harset/"oder?esult.isnderflow :@

    java/nio/!uffer.position :A

    java/nio/"har!uffer.arraOffset :A

    java/nio/!te!uffer.arraOffset @7

  • 7/24/2019 Dtrace Topics Java

    26/34

    25

    # $at jagg.d

    #L/usr/s9in/dtra$e -s

    dtra$e'''!%GB

    +

    printf*Tra$ing... Kit "trl-" to end.Vn*

    0

    hotspotQ'''method-entr

    +

    this->$lass ) $har Q $opinarg:W arg@ = :

    this->$lass1arg@2 ) &V3&

    this->method ) $har Q $opinarg8W arg7 = :

    this->method1arg72 ) &V3&

    $alls1stringofthis->$lassW stringofthis->method2 ) $ount

    0

    dtra$e'''%

    +

    printa*U7As.U-@7s U7dVn*W $alls

    0

  • 7/24/2019 Dtrace Topics Java

    27/34

    27

    Example !: %ethod &lo'

    4ith some more scriptin), fo+ indented methodcas can be printed"

    # ./jflow.d

    Greeting.greet -> java/io/PrintStream.println

    -> java/io/PrintStream.print

    -> java/io/PrintStream.write

    -> java/io/PrintStream.ensureOpen

    java/io/riter.write -> java/io/!ufferedriter.write

    -> java/io/!ufferedriter.ensureOpen

    java/io/!ufferedriter.min

  • 7/24/2019 Dtrace Topics Java

    28/34

    2:

    Example !: jlo'd

    # $at jflow.d

    #L/usr/s9in/dtra$e -s

    #pragma option uiet

    hotspotQ'''method-entr

    +

    self->indent==

    printf*UQs Us Us.UsVn*W self->indentW **W *->*W

    stringof$opinarg:W arg@W stringof$opinarg8Warg7

    0

    hotspotQ'''method-return+

    printf*UQs Us Us.UsVn*W self->indentW **W *indent--

    0

  • 7/24/2019 Dtrace Topics Java

    29/34

    2=

    Example !: *tack &lo'

    DTrace can observe a ayers of the soft+are stac*!

    The foo+in) e&ampe demonstrates this capabiity,and +as actuay from the prototype provider,

    http"//bo)s!sun!com/ah/entry/dtracin)@>ava -> java/io/BnputStream?eader'read

    -> sun/nio/$s/Streame$oder'read

    -> sun/nio/$s/Streame$oder'read3

    -> li9$.so.:'mallo$

    -> li9$.so.:'smallo$ sun/nio/$s/Streame$oder'read

    -> sun/nio/$s/Streame$oder'ensureOpen

  • 7/24/2019 Dtrace Topics Java

    30/34

    '0

    Example !: +bject $llocation

    The creation of a ne+ ob>ect can be traced +ithob>ect?aoc"

    # dtra$e -n &hotspotQ'''o9je$t-allo$ + printf*new UsVn*W

    stringof$opinarg:W arg@ 0&

    new java/nio/Keap"har!uffer

    new java/nio/Keap"har!uffer

    new java/nio/Keap"har!uffer

    new java/nio/Keap"har!uffer

    1...2

  • 7/24/2019 Dtrace Topics Java

    31/34

    '1

    Example !: %ethod Times

    4ith method?entry and method?return probes, andDTraceIs abiity to measure nanosecondtimestamps, the time to e&ecute methods can bemeasured!

    This can hep identify bottenec*s in Java code!

    Thin)s start to )et a itte harder! Ke a+are of,> overappin) methods

    > mutipe Java threads e&ecutin) concurrenty

    > Java threads conte&t s+itchin) off the 3$Ls

    > DTrace overheads at nanosecond resoutions

    >

    recursive methodsE

  • 7/24/2019 Dtrace Topics Java

    32/34

    '2

    Example !: What,s next

    6urther anaysis can be performed usin)"> .ther hotspot probes

    > .ther DTrace providers

    >

    Scriptin) to identify events of interest The possibiities for anaysis are near?endess! This

    is )reat if you are troubeshootin) a *no+n probem as DTrace shoud have the po+er to find it!

  • 7/24/2019 Dtrace Topics Java

    33/34

    ''

    Reso-rces

    To earn more about DTrace and Java,http"//>ava!sun!com/>avase/5/docs/technotes/)uides/vm/dtrace!htm

    Search the nternet for DTrace Java, in particuar

    are artices +ritten by">(dam eventha> ey .Iair

    6ind %&ampes of DTracin) Java in/usr/>d*/instances/>d*1!5!0/sampe/dtrace/hotspot

    3hec* my bo)" http"//bo)s!sun!com/brendan

    http://java.sun.com/javase/6/docs/technotes/guides/vm/dtrace.htmlhttp://java.sun.com/javase/6/docs/technotes/guides/vm/dtrace.html
  • 7/24/2019 Dtrace Topics Java

    34/34

    dtrace:::E.D

    Brendan GreggbrendanNsun!com