2121
2222package com .microsoft .applicationinsights .agent ;
2323
24+ import static java .util .concurrent .TimeUnit .MINUTES ;
25+
2426import java .io .File ;
2527import java .io .IOException ;
2628import java .io .PrintWriter ;
3032import java .lang .management .ThreadMXBean ;
3133import java .nio .charset .Charset ;
3234import java .nio .file .Files ;
33- import java .util .concurrent .Executors ;
34- import java .util .concurrent .TimeUnit ;
3535
3636final class StartupProfiler {
3737
@@ -41,21 +41,19 @@ final class StartupProfiler {
4141 public static void start () {
4242 String tempDirectory = System .getProperty ("java.io.tmpdir" );
4343 File folder = new File (tempDirectory , "applicationinsights" );
44- if (!folder .exists ()) {
45- folder .mkdirs ();
44+ if (!folder .exists () && !folder .mkdirs ()) {
45+ System .out .println ("Failed to create directory: " + tempDirectory );
46+ return ;
4647 }
4748
4849 File dumpFile = new File (folder , STACKTRACES );
4950 System .out .println ("Writing startup profiler to '" + dumpFile .getPath () + "'" );
5051
51- PrintWriter printWriter = null ;
52- try ( PrintWriter out =
53- new PrintWriter ( Files . newBufferedWriter ( dumpFile . toPath (), Charset . defaultCharset ()))) {
54- printWriter = new PrintWriter (out );
52+ PrintWriter printWriter ;
53+ try {
54+ printWriter =
55+ new PrintWriter (Files . newBufferedWriter ( dumpFile . toPath (), Charset . defaultCharset ()) );
5556 } catch (IOException e ) {
56- if (printWriter != null ) {
57- printWriter .close ();
58- }
5957 System .out .println ("Error occurred when writing dump to " + dumpFile .getPath ());
6058 e .printStackTrace ();
6159 return ;
@@ -65,12 +63,11 @@ public static void start() {
6563 }
6664
6765 private static void start (PrintWriter out ) {
68- Executors .newSingleThreadScheduledExecutor ()
69- .scheduleAtFixedRate (new ThreadDump (out ), 50 , 50 , TimeUnit .MILLISECONDS );
66+ Thread thread = new Thread (new ThreadDump (out ), "StartupProfiler" );
67+ thread .setDaemon (true );
68+ thread .start ();
7069 }
7170
72- private StartupProfiler () {}
73-
7471 private static class ThreadDump implements Runnable {
7572
7673 private final PrintWriter out ;
@@ -80,9 +77,24 @@ private ThreadDump(PrintWriter out) {
8077 }
8178
8279 @ Override
80+ @ SuppressWarnings ("SystemOut" )
8381 public void run () {
82+ long start = System .currentTimeMillis ();
83+ while (System .currentTimeMillis () - start < MINUTES .toMillis (10 )) {
84+ try {
85+ Thread .sleep (50 );
86+ } catch (InterruptedException e ) {
87+ System .out .println ("Startup profiler interrupted" );
88+ return ;
89+ }
90+ captureThreadDump ();
91+ }
92+ }
93+
94+ private void captureThreadDump () {
8495 out .println ("========================================" );
8596 RuntimeMXBean runtimeBean = ManagementFactory .getRuntimeMXBean ();
97+ out .print ("uptime: " );
8698 out .println (runtimeBean .getUptime ());
8799 out .println ();
88100 ThreadMXBean threadBean = ManagementFactory .getThreadMXBean ();
@@ -107,4 +119,6 @@ private void write(ThreadInfo threadInfo) {
107119 out .println ();
108120 }
109121 }
122+
123+ private StartupProfiler () {}
110124}
0 commit comments