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. *
  28. * @author -Nemesiss- L2M
  29. */
  30. public class DeadLockDetector extends Thread
  31. {
  32. private static Logger _log = Logger.getLogger(DeadLockDetector.class.getName());
  33. /** Interval to check for deadlocked threads */
  34. private static final int _sleepTime = Config.DEADLOCK_CHECK_INTERVAL*1000;
  35. private final ThreadMXBean tmx;
  36. public DeadLockDetector()
  37. {
  38. super("DeadLockDetector");
  39. tmx = ManagementFactory.getThreadMXBean();
  40. }
  41. @Override
  42. public final void run()
  43. {
  44. boolean deadlock = false;
  45. while(!deadlock)
  46. {
  47. try
  48. {
  49. long[] ids = tmx.findDeadlockedThreads();
  50. if(ids != null)
  51. {
  52. deadlock = true;
  53. ThreadInfo[] tis = tmx.getThreadInfo(ids,true,true);
  54. StringBuilder info = new StringBuilder();
  55. info.append("DeadLock Found!\n");
  56. for(ThreadInfo ti : tis)
  57. {
  58. info.append(ti.toString());
  59. }
  60. for(ThreadInfo ti : tis)
  61. {
  62. LockInfo[] locks = ti.getLockedSynchronizers();
  63. MonitorInfo[] monitors = ti.getLockedMonitors();
  64. if(locks.length == 0 && monitors.length == 0)
  65. {
  66. continue;
  67. }
  68. ThreadInfo dl = ti;
  69. info.append("Java-level deadlock:\n");
  70. info.append("\t");
  71. info.append(dl.getThreadName());
  72. info.append(" is waiting to lock ");
  73. info.append(dl.getLockInfo().toString());
  74. info.append(" which is held by ");
  75. info.append(dl.getLockOwnerName());
  76. info.append("\n");
  77. while((dl = tmx.getThreadInfo(new long[]{dl.getLockOwnerId()},true,true)[0]).getThreadId() != ti.getThreadId())
  78. {
  79. info.append("\t");
  80. info.append(dl.getThreadName());
  81. info.append(" is waiting to lock ");
  82. info.append(dl.getLockInfo().toString());
  83. info.append(" which is held by ");
  84. info.append(dl.getLockOwnerName());
  85. info.append("\n");
  86. }
  87. }
  88. _log.warning(info.toString());
  89. if(Config.RESTART_ON_DEADLOCK)
  90. {
  91. Announcements an = Announcements.getInstance();
  92. an.announceToAll("Server has stability issues - restarting now.");
  93. Shutdown.getInstance().startTelnetShutdown("DeadLockDetector - Auto Restart",60,true);
  94. }
  95. }
  96. Thread.sleep(_sleepTime);
  97. }
  98. catch(Exception e)
  99. {
  100. _log.log(Level.WARNING,"DeadLockDetector: ",e);
  101. }
  102. }
  103. }
  104. }