VishwanathB
VishwanathB

Reputation: 149

Thread Stack to file via MAT OQL

I know there is way we can get Thread details and more from the MAT UI, but is there a way one can get those stack(s)(similar to thread dump) redirected to output or run any OQL query to get the same?

We have pretty huge HeapDump (around 16G) which shows around 500 Active threads and going through each one of the via UI is a tedious process. was wondering if there is a way to get a simplifed thread dump from Heap Dump via MAT OQL.

Thanks

Vishwanath

Upvotes: 0

Views: 781

Answers (1)

user13762112
user13762112

Reputation: 441

With a recent level of MAT this should work:

SELECT u.Thread AS Thread, u.Frame.@text AS Frame FROM OBJECTS ( SELECT t AS Thread, ${snapshot}.getThreadStack(t.@objectId).@stackFrames AS Frame FROM java.lang.Thread t  ) u 

The inner select

SELECT t AS Thread, ${snapshot}.getThreadStack(t.@objectId).@stackFrames AS Frame FROM java.lang.Thread t

extracts each thread and an array of stack frames. The outer select then flattens that array with the same thread reference for each of its stack frames.

Thread                           |Frame
--------------------------------------------------------------------------------------------------------------------------------------------------
java.lang.Thread [id=0x7b6a1e7f0]|at java.lang.Object.wait(J)V (Native Method)
java.lang.Thread [id=0x7b6a1e7f0]|at java.lang.Object.wait(JI)V (Unknown Source)
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.performCleanup()Z (ConnectionPool.java:305)
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.runCleanupUntilPoolIsEmpty()V (ConnectionPool.java:242)
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.access$000(Lcom/squareup/okhttp/ConnectionPool;)V (ConnectionPool.java:54)
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool$1.run()V (ConnectionPool.java:97)
--------------------------------------------------------------------------------------------------------------------------------------------------

You can even extract each local from each frame using another select.

SELECT v.Thread AS Thread, v.Frame AS Frame, ${snapshot}.getObject(v.Objs) AS Local 
  FROM OBJECTS ( 
    SELECT u.Thread AS Thread, u.Frame.@text AS Frame, u.Frame.@localObjectsIds AS Objs 
      FROM OBJECTS ( 
        SELECT t AS Thread, ${snapshot}.getThreadStack(t.@objectId).@stackFrames AS Frame 
          FROM java.lang.Thread t
      ) u
  ) v 
  WHERE (v.Objs != null)
Thread                           |Frame                                                                                                           |Local
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.performCleanup()Z (ConnectionPool.java:305)                               |java.util.ArrayList [id=0x7bb402eb8]
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.performCleanup()Z (ConnectionPool.java:305)                               |com.squareup.okhttp.ConnectionPool [id=0x6c556ccb0]
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.runCleanupUntilPoolIsEmpty()V (ConnectionPool.java:242)                   |com.squareup.okhttp.ConnectionPool [id=0x6c556ccb0]
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool.access$000(Lcom/squareup/okhttp/ConnectionPool;)V (ConnectionPool.java:54)|com.squareup.okhttp.ConnectionPool [id=0x6c556ccb0]
java.lang.Thread [id=0x7b6a1e7f0]|at com.squareup.okhttp.ConnectionPool$1.run()V (ConnectionPool.java:97)                                         |com.squareup.okhttp.ConnectionPool$1 [id=0x6c556ccd8]
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Upvotes: 5

Related Questions