DeadLockDetector.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. String info = "DeadLock Found!\n";
  53. for(ThreadInfo ti : tis)
  54. {
  55. info += ti.toString();
  56. }
  57. for(ThreadInfo ti : tis)
  58. {
  59. LockInfo[] locks = ti.getLockedSynchronizers();
  60. MonitorInfo[] monitors = ti.getLockedMonitors();
  61. if(locks.length == 0 && monitors.length == 0)
  62. {
  63. continue;
  64. }
  65. ThreadInfo dl = ti;
  66. info += "Java-level deadlock:\n";
  67. info += "\t"+dl.getThreadName()+" is waiting to lock "+dl.getLockInfo().toString()+" which is held by "+dl.getLockOwnerName()+"\n";
  68. while((dl = tmx.getThreadInfo(new long[]{dl.getLockOwnerId()},true,true)[0]).getThreadId() != ti.getThreadId())
  69. {
  70. info += "\t"+dl.getThreadName()+" is waiting to lock "+dl.getLockInfo().toString()+" which is held by "+dl.getLockOwnerName()+"\n";
  71. }
  72. }
  73. _log.warning(info);
  74. if(Config.RESTART_ON_DEADLOCK)
  75. {
  76. Announcements an = Announcements.getInstance();
  77. an.announceToAll("Server has stability issues - restarting now.");
  78. Shutdown.getInstance().startTelnetShutdown("DeadLockDetector - Auto Restart",60,true);
  79. }
  80. }
  81. Thread.sleep(_sleepTime);
  82. }
  83. catch(Exception e)
  84. {
  85. _log.log(Level.WARNING,"DeadLockDetector: ",e);
  86. }
  87. }
  88. }
  89. }