DeadLockDetector.java 3.6 KB

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