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

Diagnose the startup performance #68

Merged
merged 3 commits into from Jul 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/main/java/com/cloudbees/jenkins/support/SupportPlugin.java
Expand Up @@ -30,6 +30,7 @@
import com.cloudbees.jenkins.support.api.StringContent;
import com.cloudbees.jenkins.support.api.SupportProvider;
import com.cloudbees.jenkins.support.api.SupportProviderDescriptor;
import com.cloudbees.jenkins.support.impl.ThreadDumps;
import com.cloudbees.jenkins.support.util.Helper;
import com.codahale.metrics.Histogram;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand Down Expand Up @@ -62,6 +63,7 @@
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
Expand All @@ -70,17 +72,21 @@

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;
Expand Down Expand Up @@ -396,6 +402,59 @@ public static void loadConfig() throws IOException {
instance.load();
}

private static final boolean logStartupPerformanceIssues = Boolean.getBoolean(SupportPlugin.class.getCanonicalName() + ".threadDumpStartup");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephenc would it be easier if we piggybacked off of the environment variable: -Djenkins.model.Jenkins.logStartupPerformance=true instead of creating a custom one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever you want, but if this causes issues, may need to turn it off separately to my mind

private static final int secondsPerThreadDump = Integer.getInteger(SupportPlugin.class.getCanonicalName() + ".secondsPerTD", 60);
private static boolean milestonesCompleted = false;

@Initializer(after = InitMilestone.COMPLETED)
public static void completedMilestones() throws IOException {
milestonesCompleted = true;
}

@Initializer(after = InitMilestone.STARTED)
public static void threadDumpStartup() throws Exception {
if (!logStartupPerformanceIssues) return;
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss");
final File f = new File(getRootDirectory(), "/startup-threadDump" + dateFormat.format(new Date())+ ".txt");
if (!f.exists()) {
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

Thread t = new Thread("Support core plugin startup diagnostics") {
@Override
public void run() {
while (!milestonesCompleted) {
PrintStream ps = null;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(f, true);
ps = new PrintStream(fileOutputStream, false, "UTF-8");
ps.println("=== Thread dump at " + new Date() + " ===");
ThreadDumps.threadDumpModern(fileOutputStream);
// Generate a thread dump every few seconds/minutes
ps.flush();
Thread.sleep(TimeUnit.SECONDS.toMillis(secondsPerThreadDump));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
org.apache.commons.io.IOUtils.closeQuietly(ps);
org.apache.commons.io.IOUtils.closeQuietly(fileOutputStream);
}
}
}
};
t.start();
}


@Override
public synchronized void start() throws Exception {
super.start();
Expand Down