{"action":"created","comment":{"url":"https://api.github.com/repos/jenkinsci/remoting/pulls/comments/53630005","id":53630005,"diff_hunk":"@@ -0,0 +1,225 @@\n+package hudson.remoting;\n+\n+import org.jvnet.animal_sniffer.IgnoreJRERequirement;\n+\n+import java.io.File;\n+import java.io.FileNotFoundException;\n+import java.io.PrintWriter;\n+import java.io.UnsupportedEncodingException;\n+import java.lang.management.LockInfo;\n+import java.lang.management.ManagementFactory;\n+import java.lang.management.MonitorInfo;\n+import java.lang.management.ThreadInfo;\n+import java.lang.management.ThreadMXBean;\n+import java.text.SimpleDateFormat;\n+import java.util.Date;\n+\n+/**\n+ * Generates diagnosis information (threaddump + stacktrace) when the pingThread fails\n+ *\n+ * <p>\n+ * Useful to troubleshoot the slave disconnection failures on the agent side.\n+ *\n+ * @since 2.55\n+ */\n+public class PingFailureAnalyzer {\n+\n+    /**\n+     * Channel we are going to study\n+     */\n+    private Channel c;\n+\n+    /**\n+     * The cause of the ping failure\n+     */\n+    private Throwable cause;\n+\n+\n+    /**\n+     * Maximum of files to store inside pingFailureAnalyzer/thread-dumps and pingFailureAnalyzer/stacktraces.\n+     * When the limit is reached out it starts overriding.\n+     */\n+    private final static int MAX_DIAGNOSIS_FILES = 5;\n+\n+    /**\n+     * Folder to store the thread dumps. Maintains most recent N files in a directory in cooperation with the writer.\n+     */\n+    private final PingThreadFileListCap threadDumps = new PingThreadFileListCap(new File(\"pingFailureAnalyzer/thread-dumps\"), MAX_DIAGNOSIS_FILES);\n+\n+    /**\n+     * Folder to store the stacktraces. Maintains most recent N files in a directory in cooperation with the writer.\n+     */\n+    private final PingThreadFileListCap stacktraces = new PingThreadFileListCap(new File(\"pingFailureAnalyzer/stacktraces\"), MAX_DIAGNOSIS_FILES);\n+\n+    public PingFailureAnalyzer(Channel c, Throwable cause) {\n+        this.c = c;\n+        this.cause = cause;\n+    }\n+\n+\n+    /**\n+     * Save the stacktrace which produces the ping failure into pingFailureAnalyzer/stacktraces\n+     *\n+     * @param cause\n+     *      The Throwable\n+     */\n+    public void saveStackTrace(Throwable cause) {\n+        long iota = System.currentTimeMillis();\n+        final SimpleDateFormat format = new SimpleDateFormat(\"yyyyMMdd-HHmmss.SSS\");\n+        File file = stacktraces.file(format.format(new Date(iota++)) + \".txt\");\n+        stacktraces.add(file);\n+        PrintWriter writer = null;\n+        try {\n+            writer = new PrintWriter(file, \"UTF-8\");\n+        } catch (FileNotFoundException e) {\n+            e.printStackTrace();\n+        } catch (UnsupportedEncodingException e) {\n+            e.printStackTrace();\n+        }\n+        cause.printStackTrace(writer);\n+        writer.close();\n+    }\n+\n+    /**\n+     * Dumps all of the threads' current information to an output stream.\n+     *\n+     */\n+    @IgnoreJRERequirement\n+    @edu.umd.cs.findbugs.annotations.SuppressWarnings(\n+            value = {\"VA_FORMAT_STRING_USES_NEWLINE\"},\n+            justification = \"We don't want platform specific\"\n+    )\n+    public void takeThreadDump() {\n+        long iota = System.currentTimeMillis();\n+        final SimpleDateFormat format = new SimpleDateFormat(\"yyyyMMdd-HHmmss.SSS\");\n+        File file = threadDumps.file(format.format(new Date(iota++)) + \".txt\");\n+        threadDumps.add(file);\n+\n+        PrintWriter writer = null;\n+        try {\n+            writer = new PrintWriter(file, \"UTF-8\");\n+        } catch (FileNotFoundException e) {\n+            e.printStackTrace();\n+        } catch (UnsupportedEncodingException e) {\n+            e.printStackTrace();\n+        }\n+\n+        ThreadMXBean mbean = ManagementFactory.getThreadMXBean();\n+        ThreadInfo[] threads;\n+        try {\n+            threads = mbean.dumpAllThreads(mbean.isObjectMonitorUsageSupported(), mbean.isSynchronizerUsageSupported());\n+        } catch (UnsupportedOperationException x) {\n+            x.printStackTrace(writer);\n+            threads = new ThreadInfo[0];\n+        }\n+\n+        for (int ti = threads.length - 1; ti >= 0; ti--) {\n+            printThreadInfo(writer, threads[ti], mbean);\n+        }\n+\n+        // Print any information about deadlocks.\n+        long[] deadLocks;\n+        try {\n+            deadLocks = mbean.findDeadlockedThreads();\n+        } catch (UnsupportedOperationException x) {\n+            x.printStackTrace(writer);\n+            deadLocks = null;\n+        }\n+        if (deadLocks != null && deadLocks.length != 0) {\n+            writer.println(\" Deadlock Found \");\n+            ThreadInfo[] deadLockThreads = mbean.getThreadInfo(deadLocks);\n+            for (ThreadInfo threadInfo : deadLockThreads) {\n+                StackTraceElement[] elements = threadInfo.getStackTrace();\n+                for (StackTraceElement element : elements) {\n+                    writer.println(element.toString());\n+                }\n+            }\n+        }\n+\n+        writer.println();\n+        writer.flush();\n+    }\n+\n+    /**\n+     * Prints the {@link ThreadInfo} (because {@link ThreadInfo#toString()} caps out the stack trace at 8 frames)\n+     *\n+     * @param writer the writer to print to.\n+     * @param t      the thread to print\n+     * @param mbean  the {@link ThreadMXBean} to use.\n+     */\n+    @edu.umd.cs.findbugs.annotations.SuppressWarnings(\n+            value = {\"VA_FORMAT_STRING_USES_NEWLINE\"},\n+            justification = \"We don't want platform specific\"\n+    )\n+    public static void printThreadInfo(PrintWriter writer, ThreadInfo t, ThreadMXBean mbean) {\n+        long cpuPercentage;\n+        try {\n+            long cpuTime = mbean.getThreadCpuTime(t.getThreadId());\n+            long threadUserTime = mbean.getThreadUserTime(t.getThreadId());\n+            cpuPercentage = (cpuTime == 0) ? 0: 100 * threadUserTime / cpuTime;","path":"src/main/java/hudson/remoting/PingFailureAnalyzer.java","position":159,"original_position":159,"commit_id":"f2ed6a55259423db289d7003dffc0daf50006d1d","original_commit_id":"f2ed6a55259423db289d7003dffc0daf50006d1d","user":{"login":"jtnord","id":494726,"avatar_url":"https://avatars.githubusercontent.com/u/494726?v=3","gravatar_id":"","url":"https://api.github.com/users/jtnord","html_url":"https://github.com/jtnord","followers_url":"https://api.github.com/users/jtnord/followers","following_url":"https://api.github.com/users/jtnord/following{/other_user}","gists_url":"https://api.github.com/users/jtnord/gists{/gist_id}","starred_url":"https://api.github.com/users/jtnord/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jtnord/subscriptions","organizations_url":"https://api.github.com/users/jtnord/orgs","repos_url":"https://api.github.com/users/jtnord/repos","events_url":"https://api.github.com/users/jtnord/events{/privacy}","received_events_url":"https://api.github.com/users/jtnord/received_events","type":"User","site_admin":false},"body":"this is a bug copied from the support core plugin - please don't duplicate it :-)\r\n\r\n100% cpu time is for things that have been running without stopping since Jenkins started.\r\na thread that wakes up every day and runs for 1ms should not be listed at 100%\r\n","created_at":"2016-02-22T14:19:14Z","updated_at":"2016-02-22T14:19:14Z","html_url":"https://github.com/jenkinsci/remoting/pull/74#discussion_r53630005","pull_request_url":"https://api.github.com/repos/jenkinsci/remoting/pulls/74","_links":{"self":{"href":"https://api.github.com/repos/jenkinsci/remoting/pulls/comments/53630005"},"html":{"href":"https://github.com/jenkinsci/remoting/pull/74#discussion_r53630005"},"pull_request":{"href":"https://api.github.com/repos/jenkinsci/remoting/pulls/74"}}},"pull_request":{"url":"https://api.github.com/repos/jenkinsci/remoting/pulls/74","id":60175350,"html_url":"https://github.com/jenkinsci/remoting/pull/74","diff_url":"https://github.com/jenkinsci/remoting/pull/74.diff","patch_url":"https://github.com/jenkinsci/remoting/pull/74.patch","issue_url":"https://api.github.com/repos/jenkinsci/remoting/issues/74","number":74,"state":"open","locked":false,"title":"[WiP][FIXED JENKINS-33082] Record PingThread diagnosis information on agent pingthread failure","user":{"login":"fbelzunc","id":3898648,"avatar_url":"https://avatars.githubusercontent.com/u/3898648?v=3","gravatar_id":"","url":"https://api.github.com/users/fbelzunc","html_url":"https://github.com/fbelzunc","followers_url":"https://api.github.com/users/fbelzunc/followers","following_url":"https://api.github.com/users/fbelzunc/following{/other_user}","gists_url":"https://api.github.com/users/fbelzunc/gists{/gist_id}","starred_url":"https://api.github.com/users/fbelzunc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fbelzunc/subscriptions","organizations_url":"https://api.github.com/users/fbelzunc/orgs","repos_url":"https://api.github.com/users/fbelzunc/repos","events_url":"https://api.github.com/users/fbelzunc/events{/privacy}","received_events_url":"https://api.github.com/users/fbelzunc/received_events","type":"User","site_admin":false},"body":"This is just a work in progress...\r\n\r\nFor diagnosis purposes could be nice to record what is happening on the slave side once the pingThread has failed.\r\n\r\n@andresrc @oleg-nenashev @jtnord WDYT about this? Might be useful?\r\n","created_at":"2016-02-22T13:50:08Z","updated_at":"2016-02-22T14:19:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3eca295a441a5135fcfbf49a5f4cb80b8007509b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/jenkinsci/remoting/pulls/74/commits","review_comments_url":"https://api.github.com/repos/jenkinsci/remoting/pulls/74/comments","review_comment_url":"https://api.github.com/repos/jenkinsci/remoting/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jenkinsci/remoting/issues/74/comments","statuses_url":"https://api.github.com/repos/jenkinsci/remoting/statuses/f2ed6a55259423db289d7003dffc0daf50006d1d","head":{"label":"fbelzunc:pingthread","ref":"pingthread","sha":"f2ed6a55259423db289d7003dffc0daf50006d1d","user":{"login":"fbelzunc","id":3898648,"avatar_url":"https://avatars.githubusercontent.com/u/3898648?v=3","gravatar_id":"","url":"https://api.github.com/users/fbelzunc","html_url":"https://github.com/fbelzunc","followers_url":"https://api.github.com/users/fbelzunc/followers","following_url":"https://api.github.com/users/fbelzunc/following{/other_user}","gists_url":"https://api.github.com/users/fbelzunc/gists{/gist_id}","starred_url":"https://api.github.com/users/fbelzunc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fbelzunc/subscriptions","organizations_url":"https://api.github.com/users/fbelzunc/orgs","repos_url":"https://api.github.com/users/fbelzunc/repos","events_url":"https://api.github.com/users/fbelzunc/events{/privacy}","received_events_url":"https://api.github.com/users/fbelzunc/received_events","type":"User","site_admin":false},"repo":{"id":51997440,"name":"remoting","full_name":"fbelzunc/remoting","owner":{"login":"fbelzunc","id":3898648,"avatar_url":"https://avatars.githubusercontent.com/u/3898648?v=3","gravatar_id":"","url":"https://api.github.com/users/fbelzunc","html_url":"https://github.com/fbelzunc","followers_url":"https://api.github.com/users/fbelzunc/followers","following_url":"https://api.github.com/users/fbelzunc/following{/other_user}","gists_url":"https://api.github.com/users/fbelzunc/gists{/gist_id}","starred_url":"https://api.github.com/users/fbelzunc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fbelzunc/subscriptions","organizations_url":"https://api.github.com/users/fbelzunc/orgs","repos_url":"https://api.github.com/users/fbelzunc/repos","events_url":"https://api.github.com/users/fbelzunc/events{/privacy}","received_events_url":"https://api.github.com/users/fbelzunc/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/fbelzunc/remoting","description":"Jenkins remoting module","fork":true,"url":"https://api.github.com/repos/fbelzunc/remoting","forks_url":"https://api.github.com/repos/fbelzunc/remoting/forks","keys_url":"https://api.github.com/repos/fbelzunc/remoting/keys{/key_id}","collaborators_url":"https://api.github.com/repos/fbelzunc/remoting/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/fbelzunc/remoting/teams","hooks_url":"https://api.github.com/repos/fbelzunc/remoting/hooks","issue_events_url":"https://api.github.com/repos/fbelzunc/remoting/issues/events{/number}","events_url":"https://api.github.com/repos/fbelzunc/remoting/events","assignees_url":"https://api.github.com/repos/fbelzunc/remoting/assignees{/user}","branches_url":"https://api.github.com/repos/fbelzunc/remoting/branches{/branch}","tags_url":"https://api.github.com/repos/fbelzunc/remoting/tags","blobs_url":"https://api.github.com/repos/fbelzunc/remoting/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/fbelzunc/remoting/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/fbelzunc/remoting/git/refs{/sha}","trees_url":"https://api.github.com/repos/fbelzunc/remoting/git/trees{/sha}","statuses_url":"https://api.github.com/repos/fbelzunc/remoting/statuses/{sha}","languages_url":"https://api.github.com/repos/fbelzunc/remoting/languages","stargazers_url":"https://api.github.com/repos/fbelzunc/remoting/stargazers","contributors_url":"https://api.github.com/repos/fbelzunc/remoting/contributors","subscribers_url":"https://api.github.com/repos/fbelzunc/remoting/subscribers","subscription_url":"https://api.github.com/repos/fbelzunc/remoting/subscription","commits_url":"https://api.github.com/repos/fbelzunc/remoting/commits{/sha}","git_commits_url":"https://api.github.com/repos/fbelzunc/remoting/git/commits{/sha}","comments_url":"https://api.github.com/repos/fbelzunc/remoting/comments{/number}","issue_comment_url":"https://api.github.com/repos/fbelzunc/remoting/issues/comments{/number}","contents_url":"https://api.github.com/repos/fbelzunc/remoting/contents/{+path}","compare_url":"https://api.github.com/repos/fbelzunc/remoting/compare/{base}...{head}","merges_url":"https://api.github.com/repos/fbelzunc/remoting/merges","archive_url":"https://api.github.com/repos/fbelzunc/remoting/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/fbelzunc/remoting/downloads","issues_url":"https://api.github.com/repos/fbelzunc/remoting/issues{/number}","pulls_url":"https://api.github.com/repos/fbelzunc/remoting/pulls{/number}","milestones_url":"https://api.github.com/repos/fbelzunc/remoting/milestones{/number}","notifications_url":"https://api.github.com/repos/fbelzunc/remoting/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/fbelzunc/remoting/labels{/name}","releases_url":"https://api.github.com/repos/fbelzunc/remoting/releases{/id}","deployments_url":"https://api.github.com/repos/fbelzunc/remoting/deployments","created_at":"2016-02-18T09:52:18Z","updated_at":"2016-02-18T09:52:21Z","pushed_at":"2016-02-22T13:42:08Z","git_url":"git://github.com/fbelzunc/remoting.git","ssh_url":"git@github.com:fbelzunc/remoting.git","clone_url":"https://github.com/fbelzunc/remoting.git","svn_url":"https://github.com/fbelzunc/remoting","homepage":"http://jenkins-ci.org/","size":2384,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"jenkinsci:master","ref":"master","sha":"de883efad4c1be53a58f41641eadbb5628f9bac5","user":{"login":"jenkinsci","id":107424,"avatar_url":"https://avatars.githubusercontent.com/u/107424?v=3","gravatar_id":"","url":"https://api.github.com/users/jenkinsci","html_url":"https://github.com/jenkinsci","followers_url":"https://api.github.com/users/jenkinsci/followers","following_url":"https://api.github.com/users/jenkinsci/following{/other_user}","gists_url":"https://api.github.com/users/jenkinsci/gists{/gist_id}","starred_url":"https://api.github.com/users/jenkinsci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenkinsci/subscriptions","organizations_url":"https://api.github.com/users/jenkinsci/orgs","repos_url":"https://api.github.com/users/jenkinsci/repos","events_url":"https://api.github.com/users/jenkinsci/events{/privacy}","received_events_url":"https://api.github.com/users/jenkinsci/received_events","type":"Organization","site_admin":false},"repo":{"id":2040168,"name":"remoting","full_name":"jenkinsci/remoting","owner":{"login":"jenkinsci","id":107424,"avatar_url":"https://avatars.githubusercontent.com/u/107424?v=3","gravatar_id":"","url":"https://api.github.com/users/jenkinsci","html_url":"https://github.com/jenkinsci","followers_url":"https://api.github.com/users/jenkinsci/followers","following_url":"https://api.github.com/users/jenkinsci/following{/other_user}","gists_url":"https://api.github.com/users/jenkinsci/gists{/gist_id}","starred_url":"https://api.github.com/users/jenkinsci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jenkinsci/subscriptions","organizations_url":"https://api.github.com/users/jenkinsci/orgs","repos_url":"https://api.github.com/users/jenkinsci/repos","events_url":"https://api.github.com/users/jenkinsci/events{/privacy}","received_events_url":"https://api.github.com/users/jenkinsci/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/jenkinsci/remoting","description":"Jenkins remoting module","fork":false,"url":"https://api.github.com/repos/jenkinsci/remoting","forks_url":"https://api.github.com/repos/jenkinsci/remoting/forks","keys_url":"https://api.github.com/repos/jenkinsci/remoting/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jenkinsci/remoting/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jenkinsci/remoting/teams","hooks_url":"https://api.github.com/repos/jenkinsci/remoting/hooks","issue_events_url":"https://api.github.com/repos/jenkinsci/remoting/issues/events{/number}","events_url":"https://api.github.com/repos/jenkinsci/remoting/events","assignees_url":"https://api.github.com/repos/jenkinsci/remoting/assignees{/user}","branches_url":"https://api.github.com/repos/jenkinsci/remoting/branches{/branch}","tags_url":"https://api.github.com/repos/jenkinsci/remoting/tags","blobs_url":"https://api.github.com/repos/jenkinsci/remoting/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jenkinsci/remoting/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jenkinsci/remoting/git/refs{/sha}","trees_url":"https://api.github.com/repos/jenkinsci/remoting/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jenkinsci/remoting/statuses/{sha}","languages_url":"https://api.github.com/repos/jenkinsci/remoting/languages","stargazers_url":"https://api.github.com/repos/jenkinsci/remoting/stargazers","contributors_url":"https://api.github.com/repos/jenkinsci/remoting/contributors","subscribers_url":"https://api.github.com/repos/jenkinsci/remoting/subscribers","subscription_url":"https://api.github.com/repos/jenkinsci/remoting/subscription","commits_url":"https://api.github.com/repos/jenkinsci/remoting/commits{/sha}","git_commits_url":"https://api.github.com/repos/jenkinsci/remoting/git/commits{/sha}","comments_url":"https://api.github.com/repos/jenkinsci/remoting/comments{/number}","issue_comment_url":"https://api.github.com/repos/jenkinsci/remoting/issues/comments{/number}","contents_url":"https://api.github.com/repos/jenkinsci/remoting/contents/{+path}","compare_url":"https://api.github.com/repos/jenkinsci/remoting/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jenkinsci/remoting/merges","archive_url":"https://api.github.com/repos/jenkinsci/remoting/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jenkinsci/remoting/downloads","issues_url":"https://api.github.com/repos/jenkinsci/remoting/issues{/number}","pulls_url":"https://api.github.com/repos/jenkinsci/remoting/pulls{/number}","milestones_url":"https://api.github.com/repos/jenkinsci/remoting/milestones{/number}","notifications_url":"https://api.github.com/repos/jenkinsci/remoting/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jenkinsci/remoting/labels{/name}","releases_url":"https://api.github.com/repos/jenkinsci/remoting/releases{/id}","deployments_url":"https://api.github.com/repos/jenkinsci/remoting/deployments","created_at":"2011-07-13T05:06:16Z","updated_at":"2016-01-24T18:28:02Z","pushed_at":"2016-02-22T13:50:09Z","git_url":"git://github.com/jenkinsci/remoting.git","ssh_url":"git@github.com:jenkinsci/remoting.git","clone_url":"https://github.com/jenkinsci/remoting.git","svn_url":"https://github.com/jenkinsci/remoting","homepage":"http://jenkins-ci.org/","size":3037,"stargazers_count":35,"watchers_count":35,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":64,"mirror_url":null,"open_issues_count":5,"forks":64,"open_issues":5,"watchers":35,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jenkinsci/remoting/pulls/74"},"html":{"href":"https://github.com/jenkinsci/remoting/pull/74"},"issue":{"href":"https://api.github.com/repos/jenkinsci/remoting/issues/74"},"comments":{"href":"https://api.github.com/repos/jenkinsci/remoting/issues/74/comments"},"review_comments":{"href":"https://api.github.com/repos/jenkinsci/remoting/pulls/74/comments"},"review_comment":{"href":"https://api.github.com/repos/jenkinsci/remoting/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jenkinsci/remoting/pulls/74/commits"},"statuses":{"href":"https://api.github.com/repos/jenkinsci/remoting/statuses/f2ed6a55259423db289d7003dffc0daf50006d1d"}}}}