jruby-async-profiler 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.gitmodules +3 -0
  4. data/Gemfile +4 -0
  5. data/README.md +35 -0
  6. data/Rakefile +2 -0
  7. data/bin/console +14 -0
  8. data/bin/setup +8 -0
  9. data/ext/Rakefile +6 -0
  10. data/ext/async-profiler/.gitattributes +1 -0
  11. data/ext/async-profiler/.gitignore +6 -0
  12. data/ext/async-profiler/.travis.yml +11 -0
  13. data/ext/async-profiler/CHANGELOG.md +107 -0
  14. data/ext/async-profiler/JavaHome.class +0 -0
  15. data/ext/async-profiler/LICENSE +201 -0
  16. data/ext/async-profiler/Makefile +66 -0
  17. data/ext/async-profiler/README.md +487 -0
  18. data/ext/async-profiler/demo/SwingSet2.svg +2247 -0
  19. data/ext/async-profiler/docs/cddl1.txt +358 -0
  20. data/ext/async-profiler/profiler.sh +240 -0
  21. data/ext/async-profiler/src/allocTracer.cpp +155 -0
  22. data/ext/async-profiler/src/allocTracer.h +74 -0
  23. data/ext/async-profiler/src/arch.h +69 -0
  24. data/ext/async-profiler/src/arguments.cpp +265 -0
  25. data/ext/async-profiler/src/arguments.h +152 -0
  26. data/ext/async-profiler/src/codeCache.cpp +128 -0
  27. data/ext/async-profiler/src/codeCache.h +99 -0
  28. data/ext/async-profiler/src/engine.cpp +50 -0
  29. data/ext/async-profiler/src/engine.h +38 -0
  30. data/ext/async-profiler/src/flameGraph.cpp +770 -0
  31. data/ext/async-profiler/src/flameGraph.h +118 -0
  32. data/ext/async-profiler/src/flightRecorder.cpp +727 -0
  33. data/ext/async-profiler/src/flightRecorder.h +39 -0
  34. data/ext/async-profiler/src/frameName.cpp +189 -0
  35. data/ext/async-profiler/src/frameName.h +56 -0
  36. data/ext/async-profiler/src/itimer.cpp +49 -0
  37. data/ext/async-profiler/src/itimer.h +43 -0
  38. data/ext/async-profiler/src/jattach/jattach.c +437 -0
  39. data/ext/async-profiler/src/java/one/profiler/AsyncProfiler.java +160 -0
  40. data/ext/async-profiler/src/java/one/profiler/AsyncProfilerMXBean.java +43 -0
  41. data/ext/async-profiler/src/java/one/profiler/Counter.java +25 -0
  42. data/ext/async-profiler/src/java/one/profiler/Events.java +28 -0
  43. data/ext/async-profiler/src/javaApi.cpp +124 -0
  44. data/ext/async-profiler/src/lockTracer.cpp +161 -0
  45. data/ext/async-profiler/src/lockTracer.h +55 -0
  46. data/ext/async-profiler/src/mutex.cpp +33 -0
  47. data/ext/async-profiler/src/mutex.h +49 -0
  48. data/ext/async-profiler/src/os.h +45 -0
  49. data/ext/async-profiler/src/os_linux.cpp +129 -0
  50. data/ext/async-profiler/src/os_macos.cpp +115 -0
  51. data/ext/async-profiler/src/perfEvents.h +60 -0
  52. data/ext/async-profiler/src/perfEvents_linux.cpp +550 -0
  53. data/ext/async-profiler/src/perfEvents_macos.cpp +64 -0
  54. data/ext/async-profiler/src/profiler.cpp +952 -0
  55. data/ext/async-profiler/src/profiler.h +238 -0
  56. data/ext/async-profiler/src/spinLock.h +66 -0
  57. data/ext/async-profiler/src/stackFrame.h +57 -0
  58. data/ext/async-profiler/src/stackFrame_aarch64.cpp +75 -0
  59. data/ext/async-profiler/src/stackFrame_arm.cpp +58 -0
  60. data/ext/async-profiler/src/stackFrame_i386.cpp +82 -0
  61. data/ext/async-profiler/src/stackFrame_x64.cpp +113 -0
  62. data/ext/async-profiler/src/symbols.h +37 -0
  63. data/ext/async-profiler/src/symbols_linux.cpp +354 -0
  64. data/ext/async-profiler/src/symbols_macos.cpp +156 -0
  65. data/ext/async-profiler/src/vmEntry.cpp +173 -0
  66. data/ext/async-profiler/src/vmEntry.h +105 -0
  67. data/ext/async-profiler/src/vmStructs.cpp +104 -0
  68. data/ext/async-profiler/src/vmStructs.h +112 -0
  69. data/ext/async-profiler/src/wallClock.cpp +96 -0
  70. data/ext/async-profiler/src/wallClock.h +56 -0
  71. data/ext/async-profiler/test/AllocatingTarget.java +26 -0
  72. data/ext/async-profiler/test/LoadLibraryTest.java +21 -0
  73. data/ext/async-profiler/test/Target.java +31 -0
  74. data/ext/async-profiler/test/ThreadsTarget.java +35 -0
  75. data/ext/async-profiler/test/alloc-smoke-test.sh +36 -0
  76. data/ext/async-profiler/test/load-library-test.sh +35 -0
  77. data/ext/async-profiler/test/smoke-test.sh +37 -0
  78. data/ext/async-profiler/test/thread-smoke-test.sh +32 -0
  79. data/jruby-async-profiler.gemspec +32 -0
  80. data/lib/jruby/async/profiler.rb +10 -0
  81. data/lib/jruby/async/profiler/version.rb +7 -0
  82. metadata +155 -0
@@ -0,0 +1,358 @@
1
+ COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
2
+
3
+ 1. Definitions.
4
+
5
+ 1.1. "Contributor" means each individual or entity that
6
+ creates or contributes to the creation of Modifications.
7
+
8
+ 1.2. "Contributor Version" means the combination of the
9
+ Original Software, prior Modifications used by a
10
+ Contributor (if any), and the Modifications made by that
11
+ particular Contributor.
12
+
13
+ 1.3. "Covered Software" means (a) the Original Software, or
14
+ (b) Modifications, or (c) the combination of files
15
+ containing Original Software with files containing
16
+ Modifications, in each case including portions thereof.
17
+
18
+ 1.4. "Executable" means the Covered Software in any form
19
+ other than Source Code.
20
+
21
+ 1.5. "Initial Developer" means the individual or entity
22
+ that first makes Original Software available under this
23
+ License.
24
+
25
+ 1.6. "Larger Work" means a work which combines Covered
26
+ Software or portions thereof with code not governed by the
27
+ terms of this License.
28
+
29
+ 1.7. "License" means this document.
30
+
31
+ 1.8. "Licensable" means having the right to grant, to the
32
+ maximum extent possible, whether at the time of the initial
33
+ grant or subsequently acquired, any and all of the rights
34
+ conveyed herein.
35
+
36
+ 1.9. "Modifications" means the Source Code and Executable
37
+ form of any of the following:
38
+ A. Any file that results from an addition to,
39
+ deletion from or modification of the contents of a
40
+ file containing Original Software or previous
41
+ Modifications;
42
+ B. Any new file that contains any part of the
43
+ Original Software or previous Modification; or
44
+ C. Any new file that is contributed or otherwise made
45
+ available under the terms of this License.
46
+
47
+ 1.10. "Original Software" means the Source Code and
48
+ Executable form of computer software code that is
49
+ originally released under this License.
50
+
51
+ 1.11. "Patent Claims" means any patent claim(s), now owned
52
+ or hereafter acquired, including without limitation,
53
+ method, process, and apparatus claims, in any patent
54
+ Licensable by grantor.
55
+
56
+ 1.12. "Source Code" means (a) the common form of computer
57
+ software code in which modifications are made and (b)
58
+ associated documentation included in or with such code.
59
+
60
+ 1.13. "You" (or "Your") means an individual or a legal
61
+ entity exercising rights under, and complying with all of
62
+ the terms of, this License. For legal entities, "You"
63
+ includes any entity which controls, is controlled by, or is
64
+ under common control with You. For purposes of this
65
+ definition, "control" means (a) the power, direct or
66
+ indirect, to cause the direction or management of such
67
+ entity, whether by contract or otherwise, or (b) ownership
68
+ of more than fifty percent (50%) of the outstanding shares
69
+ or beneficial ownership of such entity.
70
+
71
+ 2. License Grants.
72
+
73
+ 2.1. The Initial Developer Grant.
74
+ Conditioned upon Your compliance with Section 3.1 below and
75
+ subject to third party intellectual property claims, the
76
+ Initial Developer hereby grants You a world-wide,
77
+ royalty-free, non-exclusive license:
78
+ (a) under intellectual property rights (other than
79
+ patent or trademark) Licensable by Initial Developer,
80
+ to use, reproduce, modify, display, perform,
81
+ sublicense and distribute the Original Software (or
82
+ portions thereof), with or without Modifications,
83
+ and/or as part of a Larger Work; and
84
+ (b) under Patent Claims infringed by the making,
85
+ using or selling of Original Software, to make, have
86
+ made, use, practice, sell, and offer for sale, and/or
87
+ otherwise dispose of the Original Software (or
88
+ portions thereof).
89
+ (c) The licenses granted in Sections 2.1(a) and (b)
90
+ are effective on the date Initial Developer first
91
+ distributes or otherwise makes the Original Software
92
+ available to a third party under the terms of this
93
+ License.
94
+ (d) Notwithstanding Section 2.1(b) above, no patent
95
+ license is granted: (1) for code that You delete from
96
+ the Original Software, or (2) for infringements
97
+ caused by: (i) the modification of the Original
98
+ Software, or (ii) the combination of the Original
99
+ Software with other software or devices.
100
+
101
+ 2.2. Contributor Grant.
102
+ Conditioned upon Your compliance with Section 3.1 below and
103
+ subject to third party intellectual property claims, each
104
+ Contributor hereby grants You a world-wide, royalty-free,
105
+ non-exclusive license:
106
+ (a) under intellectual property rights (other than
107
+ patent or trademark) Licensable by Contributor to
108
+ use, reproduce, modify, display, perform, sublicense
109
+ and distribute the Modifications created by such
110
+ Contributor (or portions thereof), either on an
111
+ unmodified basis, with other Modifications, as
112
+ Covered Software and/or as part of a Larger Work; and
113
+ (b) under Patent Claims infringed by the making,
114
+ using, or selling of Modifications made by that
115
+ Contributor either alone and/or in combination with
116
+ its Contributor Version (or portions of such
117
+ combination), to make, use, sell, offer for sale,
118
+ have made, and/or otherwise dispose of: (1)
119
+ Modifications made by that Contributor (or portions
120
+ thereof); and (2) the combination of Modifications
121
+ made by that Contributor with its Contributor Version
122
+ (or portions of such combination).
123
+ (c) The licenses granted in Sections 2.2(a) and
124
+ 2.2(b) are effective on the date Contributor first
125
+ distributes or otherwise makes the Modifications
126
+ available to a third party.
127
+ (d) Notwithstanding Section 2.2(b) above, no patent
128
+ license is granted: (1) for any code that Contributor
129
+ has deleted from the Contributor Version; (2) for
130
+ infringements caused by: (i) third party
131
+ modifications of Contributor Version, or (ii) the
132
+ combination of Modifications made by that Contributor
133
+ with other software (except as part of the
134
+ Contributor Version) or other devices; or (3) under
135
+ Patent Claims infringed by Covered Software in the
136
+ absence of Modifications made by that Contributor.
137
+
138
+ 3. Distribution Obligations.
139
+
140
+ 3.1. Availability of Source Code.
141
+ Any Covered Software that You distribute or otherwise make
142
+ available in Executable form must also be made available in
143
+ Source Code form and that Source Code form must be
144
+ distributed only under the terms of this License. You must
145
+ include a copy of this License with every copy of the
146
+ Source Code form of the Covered Software You distribute or
147
+ otherwise make available. You must inform recipients of any
148
+ such Covered Software in Executable form as to how they can
149
+ obtain such Covered Software in Source Code form in a
150
+ reasonable manner on or through a medium customarily used
151
+ for software exchange.
152
+
153
+ 3.2. Modifications.
154
+ The Modifications that You create or to which You
155
+ contribute are governed by the terms of this License. You
156
+ represent that You believe Your Modifications are Your
157
+ original creation(s) and/or You have sufficient rights to
158
+ grant the rights conveyed by this License.
159
+
160
+ 3.3. Required Notices.
161
+ You must include a notice in each of Your Modifications
162
+ that identifies You as the Contributor of the Modification.
163
+ You may not remove or alter any copyright, patent or
164
+ trademark notices contained within the Covered Software, or
165
+ any notices of licensing or any descriptive text giving
166
+ attribution to any Contributor or the Initial Developer.
167
+
168
+ 3.4. Application of Additional Terms.
169
+ You may not offer or impose any terms on any Covered
170
+ Software in Source Code form that alters or restricts the
171
+ applicable version of this License or the recipients'
172
+ rights hereunder. You may choose to offer, and to charge a
173
+ fee for, warranty, support, indemnity or liability
174
+ obligations to one or more recipients of Covered Software.
175
+ However, you may do so only on Your own behalf, and not on
176
+ behalf of the Initial Developer or any Contributor. You
177
+ must make it absolutely clear that any such warranty,
178
+ support, indemnity or liability obligation is offered by
179
+ You alone, and You hereby agree to indemnify the Initial
180
+ Developer and every Contributor for any liability incurred
181
+ by the Initial Developer or such Contributor as a result of
182
+ warranty, support, indemnity or liability terms You offer.
183
+
184
+ 3.5. Distribution of Executable Versions.
185
+ You may distribute the Executable form of the Covered
186
+ Software under the terms of this License or under the terms
187
+ of a license of Your choice, which may contain terms
188
+ different from this License, provided that You are in
189
+ compliance with the terms of this License and that the
190
+ license for the Executable form does not attempt to limit
191
+ or alter the recipient's rights in the Source Code form
192
+ from the rights set forth in this License. If You
193
+ distribute the Covered Software in Executable form under a
194
+ different license, You must make it absolutely clear that
195
+ any terms which differ from this License are offered by You
196
+ alone, not by the Initial Developer or Contributor. You
197
+ hereby agree to indemnify the Initial Developer and every
198
+ Contributor for any liability incurred by the Initial
199
+ Developer or such Contributor as a result of any such terms
200
+ You offer.
201
+
202
+ 3.6. Larger Works.
203
+ You may create a Larger Work by combining Covered Software
204
+ with other code not governed by the terms of this License
205
+ and distribute the Larger Work as a single product. In such
206
+ a case, You must make sure the requirements of this License
207
+ are fulfilled for the Covered Software.
208
+
209
+ 4. Versions of the License.
210
+
211
+ 4.1. New Versions.
212
+ Sun Microsystems, Inc. is the initial license steward and
213
+ may publish revised and/or new versions of this License
214
+ from time to time. Each version will be given a
215
+ distinguishing version number. Except as provided in
216
+ Section 4.3, no one other than the license steward has the
217
+ right to modify this License.
218
+
219
+ 4.2. Effect of New Versions.
220
+ You may always continue to use, distribute or otherwise
221
+ make the Covered Software available under the terms of the
222
+ version of the License under which You originally received
223
+ the Covered Software. If the Initial Developer includes a
224
+ notice in the Original Software prohibiting it from being
225
+ distributed or otherwise made available under any
226
+ subsequent version of the License, You must distribute and
227
+ make the Covered Software available under the terms of the
228
+ version of the License under which You originally received
229
+ the Covered Software. Otherwise, You may also choose to
230
+ use, distribute or otherwise make the Covered Software
231
+ available under the terms of any subsequent version of the
232
+ License published by the license steward.
233
+
234
+ 4.3. Modified Versions.
235
+ When You are an Initial Developer and You want to create a
236
+ new license for Your Original Software, You may create and
237
+ use a modified version of this License if You: (a) rename
238
+ the license and remove any references to the name of the
239
+ license steward (except to note that the license differs
240
+ from this License); and (b) otherwise make it clear that
241
+ the license contains terms which differ from this License.
242
+
243
+ 5. DISCLAIMER OF WARRANTY.
244
+
245
+ COVERED SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS"
246
+ BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
247
+ INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED
248
+ SOFTWARE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR
249
+ PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND
250
+ PERFORMANCE OF THE COVERED SOFTWARE IS WITH YOU. SHOULD ANY
251
+ COVERED SOFTWARE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE
252
+ INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF
253
+ ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF
254
+ WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF
255
+ ANY COVERED SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS
256
+ DISCLAIMER.
257
+
258
+ 6. TERMINATION.
259
+
260
+ 6.1. This License and the rights granted hereunder will
261
+ terminate automatically if You fail to comply with terms
262
+ herein and fail to cure such breach within 30 days of
263
+ becoming aware of the breach. Provisions which, by their
264
+ nature, must remain in effect beyond the termination of
265
+ this License shall survive.
266
+
267
+ 6.2. If You assert a patent infringement claim (excluding
268
+ declaratory judgment actions) against Initial Developer or
269
+ a Contributor (the Initial Developer or Contributor against
270
+ whom You assert such claim is referred to as "Participant")
271
+ alleging that the Participant Software (meaning the
272
+ Contributor Version where the Participant is a Contributor
273
+ or the Original Software where the Participant is the
274
+ Initial Developer) directly or indirectly infringes any
275
+ patent, then any and all rights granted directly or
276
+ indirectly to You by such Participant, the Initial
277
+ Developer (if the Initial Developer is not the Participant)
278
+ and all Contributors under Sections 2.1 and/or 2.2 of this
279
+ License shall, upon 60 days notice from Participant
280
+ terminate prospectively and automatically at the expiration
281
+ of such 60 day notice period, unless if within such 60 day
282
+ period You withdraw Your claim with respect to the
283
+ Participant Software against such Participant either
284
+ unilaterally or pursuant to a written agreement with
285
+ Participant.
286
+
287
+ 6.3. In the event of termination under Sections 6.1 or 6.2
288
+ above, all end user licenses that have been validly granted
289
+ by You or any distributor hereunder prior to termination
290
+ (excluding licenses granted to You by any distributor)
291
+ shall survive termination.
292
+
293
+ 7. LIMITATION OF LIABILITY.
294
+
295
+ UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT
296
+ (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE, SHALL YOU, THE
297
+ INITIAL DEVELOPER, ANY OTHER CONTRIBUTOR, OR ANY DISTRIBUTOR OF
298
+ COVERED SOFTWARE, OR ANY SUPPLIER OF ANY OF SUCH PARTIES, BE
299
+ LIABLE TO ANY PERSON FOR ANY INDIRECT, SPECIAL, INCIDENTAL, OR
300
+ CONSEQUENTIAL DAMAGES OF ANY CHARACTER INCLUDING, WITHOUT
301
+ LIMITATION, DAMAGES FOR LOST PROFITS, LOSS OF GOODWILL, WORK
302
+ STOPPAGE, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL OTHER
303
+ COMMERCIAL DAMAGES OR LOSSES, EVEN IF SUCH PARTY SHALL HAVE BEEN
304
+ INFORMED OF THE POSSIBILITY OF SUCH DAMAGES. THIS LIMITATION OF
305
+ LIABILITY SHALL NOT APPLY TO LIABILITY FOR DEATH OR PERSONAL
306
+ INJURY RESULTING FROM SUCH PARTY'S NEGLIGENCE TO THE EXTENT
307
+ APPLICABLE LAW PROHIBITS SUCH LIMITATION. SOME JURISDICTIONS DO
308
+ NOT ALLOW THE EXCLUSION OR LIMITATION OF INCIDENTAL OR
309
+ CONSEQUENTIAL DAMAGES, SO THIS EXCLUSION AND LIMITATION MAY NOT
310
+ APPLY TO YOU.
311
+
312
+ 8. U.S. GOVERNMENT END USERS.
313
+
314
+ The Covered Software is a "commercial item," as that term is
315
+ defined in 48 C.F.R. 2.101 (Oct. 1995), consisting of "commercial
316
+ computer software" (as that term is defined at 48 C.F.R. ¤
317
+ 252.227-7014(a)(1)) and "commercial computer software
318
+ documentation" as such terms are used in 48 C.F.R. 12.212 (Sept.
319
+ 1995). Consistent with 48 C.F.R. 12.212 and 48 C.F.R. 227.7202-1
320
+ through 227.7202-4 (June 1995), all U.S. Government End Users
321
+ acquire Covered Software with only those rights set forth herein.
322
+ This U.S. Government Rights clause is in lieu of, and supersedes,
323
+ any other FAR, DFAR, or other clause or provision that addresses
324
+ Government rights in computer software under this License.
325
+
326
+ 9. MISCELLANEOUS.
327
+
328
+ This License represents the complete agreement concerning subject
329
+ matter hereof. If any provision of this License is held to be
330
+ unenforceable, such provision shall be reformed only to the
331
+ extent necessary to make it enforceable. This License shall be
332
+ governed by the law of the jurisdiction specified in a notice
333
+ contained within the Original Software (except to the extent
334
+ applicable law, if any, provides otherwise), excluding such
335
+ jurisdiction's conflict-of-law provisions. Any litigation
336
+ relating to this License shall be subject to the jurisdiction of
337
+ the courts located in the jurisdiction and venue specified in a
338
+ notice contained within the Original Software, with the losing
339
+ party responsible for costs, including, without limitation, court
340
+ costs and reasonable attorneys' fees and expenses. The
341
+ application of the United Nations Convention on Contracts for the
342
+ International Sale of Goods is expressly excluded. Any law or
343
+ regulation which provides that the language of a contract shall
344
+ be construed against the drafter shall not apply to this License.
345
+ You agree that You alone are responsible for compliance with the
346
+ United States export administration regulations (and the export
347
+ control laws and regulation of any other countries) when You use,
348
+ distribute or otherwise make available any Covered Software.
349
+
350
+ 10. RESPONSIBILITY FOR CLAIMS.
351
+
352
+ As between Initial Developer and the Contributors, each party is
353
+ responsible for claims and damages arising, directly or
354
+ indirectly, out of its utilization of rights under this License
355
+ and You agree to work with Initial Developer and Contributors to
356
+ distribute such responsibility on an equitable basis. Nothing
357
+ herein is intended or shall be deemed to constitute any admission
358
+ of liability.
@@ -0,0 +1,240 @@
1
+ #!/bin/bash
2
+
3
+ usage() {
4
+ echo "Usage: $0 [action] [options] <pid>"
5
+ echo "Actions:"
6
+ echo " start start profiling and return immediately"
7
+ echo " resume resume profiling without resetting collected data"
8
+ echo " stop stop profiling"
9
+ echo " status print profiling status"
10
+ echo " list list profiling events supported by the target JVM"
11
+ echo " collect collect profile for the specified period of time"
12
+ echo " and then stop (default action)"
13
+ echo "Options:"
14
+ echo " -e event profiling event: cpu|alloc|lock|cache-misses etc."
15
+ echo " -d duration run profiling for <duration> seconds"
16
+ echo " -f filename dump output to <filename>"
17
+ echo " -i interval sampling interval in nanoseconds"
18
+ echo " -j jstackdepth maximum Java stack depth"
19
+ echo " -b bufsize frame buffer size"
20
+ echo " -t profile different threads separately"
21
+ echo " -s simple class names instead of FQN"
22
+ echo " -g print method signatures"
23
+ echo " -a annotate Java method names"
24
+ echo " -o fmt output format: summary|traces|flat|collapsed|svg|tree|jfr"
25
+ echo " -v, --version display version string"
26
+ echo ""
27
+ echo " --title string SVG title"
28
+ echo " --width px SVG width"
29
+ echo " --height px SVG frame height"
30
+ echo " --minwidth px skip frames smaller than px"
31
+ echo " --reverse generate stack-reversed FlameGraph / Call tree"
32
+ echo ""
33
+ echo " --all-kernel only include kernel-mode events"
34
+ echo " --all-user only include user-mode events"
35
+ echo " --sync-walk use synchronous JVMTI stack walker (dangerous!)"
36
+ echo ""
37
+ echo "<pid> is a numeric process ID of the target JVM"
38
+ echo " or 'jps' keyword to find running JVM automatically"
39
+ echo ""
40
+ echo "Example: $0 -d 30 -f profile.svg 3456"
41
+ echo " $0 start -i 999000 jps"
42
+ echo " $0 stop -o summary,flat jps"
43
+ exit 1
44
+ }
45
+
46
+ mirror_output() {
47
+ # Mirror output from temporary file to local terminal
48
+ if [[ $USE_TMP ]]; then
49
+ if [[ -f $FILE ]]; then
50
+ cat "$FILE"
51
+ rm "$FILE"
52
+ fi
53
+ fi
54
+ }
55
+
56
+ check_if_terminated() {
57
+ if ! kill -0 $PID 2> /dev/null; then
58
+ mirror_output
59
+ exit 0
60
+ fi
61
+ }
62
+
63
+ jattach() {
64
+ "$JATTACH" $PID load "$PROFILER" true "$1" > /dev/null
65
+ RET=$?
66
+
67
+ # Check if jattach failed
68
+ if [ $RET -ne 0 ]; then
69
+ if [ $RET -eq 255 ]; then
70
+ echo "Failed to inject profiler into $PID"
71
+ if [ "$UNAME_S" == "Darwin" ]; then
72
+ otool -L "$PROFILER"
73
+ else
74
+ ldd "$PROFILER"
75
+ fi
76
+ fi
77
+ exit $RET
78
+ fi
79
+
80
+ mirror_output
81
+ }
82
+
83
+ function abspath() {
84
+ if [ "$UNAME_S" == "Darwin" ]; then
85
+ perl -MCwd -e 'print Cwd::abs_path shift' "$1"
86
+ else
87
+ readlink -f "$1"
88
+ fi
89
+ }
90
+
91
+
92
+ OPTIND=1
93
+ UNAME_S=$(uname -s)
94
+ SCRIPT_DIR=$(dirname "$(abspath "$0")")
95
+ JATTACH=$SCRIPT_DIR/build/jattach
96
+ PROFILER=$SCRIPT_DIR/build/libasyncProfiler.so
97
+ ACTION="collect"
98
+ EVENT="cpu"
99
+ DURATION="60"
100
+ FILE=""
101
+ USE_TMP="true"
102
+ OUTPUT=""
103
+ FORMAT=""
104
+ PARAMS=""
105
+
106
+ while [[ $# -gt 0 ]]; do
107
+ case $1 in
108
+ -h|"-?")
109
+ usage
110
+ ;;
111
+ start|resume|stop|status|list|collect)
112
+ ACTION="$1"
113
+ ;;
114
+ -v|--version)
115
+ ACTION="version"
116
+ ;;
117
+ -e)
118
+ EVENT="$2"
119
+ shift
120
+ ;;
121
+ -d)
122
+ DURATION="$2"
123
+ shift
124
+ ;;
125
+ -f)
126
+ FILE="$2"
127
+ unset USE_TMP
128
+ shift
129
+ ;;
130
+ -i)
131
+ PARAMS="$PARAMS,interval=$2"
132
+ shift
133
+ ;;
134
+ -j)
135
+ PARAMS="$PARAMS,jstackdepth=$2"
136
+ shift
137
+ ;;
138
+ -b)
139
+ PARAMS="$PARAMS,framebuf=$2"
140
+ shift
141
+ ;;
142
+ -t)
143
+ PARAMS="$PARAMS,threads"
144
+ ;;
145
+ -s)
146
+ FORMAT="$FORMAT,simple"
147
+ ;;
148
+ -g)
149
+ FORMAT="$FORMAT,sig"
150
+ ;;
151
+ -a)
152
+ FORMAT="$FORMAT,ann"
153
+ ;;
154
+ -o)
155
+ OUTPUT="$2"
156
+ shift
157
+ ;;
158
+ --title)
159
+ # escape XML special characters and comma
160
+ TITLE=${2//&/&amp;}
161
+ TITLE=${TITLE//</&lt;}
162
+ TITLE=${TITLE//>/&gt;}
163
+ TITLE=${TITLE//,/&#44;}
164
+ FORMAT="$FORMAT,title=$TITLE"
165
+ shift
166
+ ;;
167
+ --width|--height|--minwidth)
168
+ FORMAT="$FORMAT,${1:2}=$2"
169
+ shift
170
+ ;;
171
+ --reverse)
172
+ FORMAT="$FORMAT,reverse"
173
+ ;;
174
+ --all-kernel)
175
+ PARAMS="$PARAMS,allkernel"
176
+ ;;
177
+ --all-user)
178
+ PARAMS="$PARAMS,alluser"
179
+ ;;
180
+ --sync-walk)
181
+ PARAMS="$PARAMS,syncwalk"
182
+ ;;
183
+ [0-9]*)
184
+ PID="$1"
185
+ ;;
186
+ jps)
187
+ # A shortcut for getting PID of a running Java application
188
+ # -XX:+PerfDisableSharedMem prevents jps from appearing in its own list
189
+ PID=$(pgrep -n java || jps -q -J-XX:+PerfDisableSharedMem)
190
+ ;;
191
+ *)
192
+ echo "Unrecognized option: $1"
193
+ usage
194
+ ;;
195
+ esac
196
+ shift
197
+ done
198
+
199
+ if [[ "$PID" == "" && "$ACTION" != "version" ]]; then
200
+ usage
201
+ fi
202
+
203
+ # If no -f argument is given, use temporary file to transfer output to caller terminal.
204
+ # Let the target process create the file in case this script is run by superuser.
205
+ if [[ $USE_TMP ]]; then
206
+ FILE=/tmp/async-profiler.$$.$PID
207
+ elif [[ $FILE != /* ]]; then
208
+ # Output file is written by the target process. Make the path absolute to avoid confusion.
209
+ FILE=$PWD/$FILE
210
+ fi
211
+
212
+ case $ACTION in
213
+ start|resume)
214
+ jattach "$ACTION,event=$EVENT,file=$FILE,$OUTPUT$FORMAT$PARAMS"
215
+ ;;
216
+ stop)
217
+ jattach "stop,file=$FILE,$OUTPUT$FORMAT"
218
+ ;;
219
+ status)
220
+ jattach "status,file=$FILE"
221
+ ;;
222
+ list)
223
+ jattach "list,file=$FILE"
224
+ ;;
225
+ collect)
226
+ jattach "start,event=$EVENT,file=$FILE,$OUTPUT$FORMAT$PARAMS"
227
+ while (( DURATION-- > 0 )); do
228
+ check_if_terminated
229
+ sleep 1
230
+ done
231
+ jattach "stop,file=$FILE,$OUTPUT$FORMAT"
232
+ ;;
233
+ version)
234
+ if [[ "$PID" == "" ]]; then
235
+ java "-agentpath:$PROFILER=version" -version 2> /dev/null
236
+ else
237
+ jattach "version,file=$FILE"
238
+ fi
239
+ ;;
240
+ esac