DeadLockDetector.java 3.4 KB

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