Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a startup profiler for debugging #2085

Merged
merged 21 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static void premain(String agentArgs, Instrumentation inst) {
}
OpenTelemetryAgent.premain(agentArgs, inst);
alreadyLoaded = true;

StartupProfiler.start();
heyams marked this conversation as resolved.
Show resolved Hide resolved
}

// this is provided only for dynamic attach in the first line of main
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.agent;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

final class StartupProfiler {

private static final String STACKTRACES = "stacktrace.txt";

public static void start() {
if (isReadonly()) {
return;
}

String startupProfiling = System.getProperty("-Dapplicationinsights.debug.startupProfiling");
heyams marked this conversation as resolved.
Show resolved Hide resolved
if (!"true".equalsIgnoreCase(startupProfiling)) {
return;
}

String tempDirectory = System.getProperty("java.io.tmpdir");
File file = new File(tempDirectory, STACKTRACES);
try {
start(
new PrintWriter(
new PrintWriter(Files.newBufferedWriter(file.toPath(), Charset.defaultCharset()))));
} catch (IOException ignore) {
// ignore it for now
}
}

private static void start(PrintWriter out) {
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(new ThreadDump(out), 50, 50, TimeUnit.MILLISECONDS);
}

private static boolean isReadonly() {
File tempDir = new File(System.getProperty("java.io.tmpdir"));
return tempDir.canRead() && !tempDir.canWrite();
}

heyams marked this conversation as resolved.
Show resolved Hide resolved
private StartupProfiler() {}

private static class ThreadDump implements Runnable {

private final PrintWriter out;

private ThreadDump(PrintWriter out) {
this.out = out;
}

@Override
public void run() {
out.println("========================================");
RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
out.println(runtimeBean.getUptime());
out.println();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos =
threadBean.getThreadInfo(
threadBean.getAllThreadIds(), threadBean.isObjectMonitorUsageSupported(), false);
long currentThreadId = Thread.currentThread().getId();
for (ThreadInfo threadInfo : threadInfos) {
if (threadInfo.getThreadId() != currentThreadId) {
write(threadInfo);
}
}
out.flush();
}

private void write(ThreadInfo threadInfo) {
if (capture(threadInfo)) {}
out.println(threadInfo.getThreadName() + " #" + threadInfo.getThreadId());
out.println(" java.lang.Thread.State: " + threadInfo.getThreadState());
for (StackTraceElement ste : threadInfo.getStackTrace()) {
out.println(" " + ste);
}
out.println();
}

private static boolean capture(ThreadInfo threadInfo) {
if (threadInfo.getThreadName().equals("main")) {
return true;
}
// check stack trace length helps to skip "Signal Dispatcher" thread
return threadInfo.getThreadState() == Thread.State.RUNNABLE
&& threadInfo.getStackTrace().length > 0;
}
}
}