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