DeadLockDetector.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* This program is free software: you can redistribute it and/or modify it under
  2. * the terms of the GNU General Public License as published by the Free Software
  3. * Foundation, either version 3 of the License, or (at your option) any later
  4. * version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  9. * details.
  10. *
  11. * You should have received a copy of the GNU General Public License along with
  12. * this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.l2jserver.util;
  15. import java.lang.management.LockInfo;
  16. import java.lang.management.ManagementFactory;
  17. import java.lang.management.MonitorInfo;
  18. import java.lang.management.ThreadInfo;
  19. import java.lang.management.ThreadMXBean;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.Announcements;
  24. import com.l2jserver.gameserver.Shutdown;
  25. /**
  26. * Thread to check for deadlocked threads.
  27. * @author -Nemesiss- L2M
  28. */
  29. public class DeadLockDetector extends Thread
  30. {
  31. private static Logger _log = Logger.getLogger(DeadLockDetector.class.getName());
  32. /** Interval to check for deadlocked threads */
  33. private static final int _sleepTime = Config.DEADLOCK_CHECK_INTERVAL * 1000;
  34. private final ThreadMXBean tmx;
  35. public DeadLockDetector()
  36. {
  37. super("DeadLockDetector");
  38. tmx = ManagementFactory.getThreadMXBean();
  39. }
  40. @Override
  41. public final void run()
  42. {
  43. boolean deadlock = false;
  44. while (!deadlock)
  45. {
  46. try
  47. {
  48. long[] ids = tmx.findDeadlockedThreads();
  49. if (ids != null)
  50. {
  51. deadlock = true;
  52. ThreadInfo[] tis = tmx.getThreadInfo(ids, true, true);
  53. StringBuilder info = new StringBuilder();
  54. info.append("DeadLock Found!\n");
  55. for (ThreadInfo ti : tis)
  56. {
  57. info.append(ti.toString());
  58. }
  59. for (ThreadInfo ti : tis)
  60. {
  61. LockInfo[] locks = ti.getLockedSynchronizers();
  62. MonitorInfo[] monitors = ti.getLockedMonitors();
  63. if ((locks.length == 0) && (monitors.length == 0))
  64. {
  65. continue;
  66. }
  67. ThreadInfo dl = ti;
  68. info.append("Java-level deadlock:\n");
  69. info.append('\t');
  70. info.append(dl.getThreadName());
  71. info.append(" is waiting to lock ");
  72. info.append(dl.getLockInfo().toString());
  73. info.append(" which is held by ");
  74. info.append(dl.getLockOwnerName());
  75. info.append('\n');
  76. while ((dl = tmx.getThreadInfo(new long[]
  77. {
  78. dl.getLockOwnerId()
  79. }, true, true)[0]).getThreadId() != ti.getThreadId())
  80. {
  81. info.append('\t');
  82. info.append(dl.getThreadName());
  83. info.append(" is waiting to lock ");
  84. info.append(dl.getLockInfo().toString());
  85. info.append(" which is held by ");
  86. info.append(dl.getLockOwnerName());
  87. info.append('\n');
  88. }
  89. }
  90. _log.warning(info.toString());
  91. if (Config.RESTART_ON_DEADLOCK)
  92. {
  93. Announcements an = Announcements.getInstance();
  94. an.announceToAll("Server has stability issues - restarting now.");
  95. Shutdown.getInstance().startTelnetShutdown("DeadLockDetector - Auto Restart", 60, true);
  96. }
  97. }
  98. Thread.sleep(_sleepTime);
  99. }
  100. catch (Exception e)
  101. {
  102. _log.log(Level.WARNING, "DeadLockDetector: ", e);
  103. }
  104. }
  105. }
  106. }