WatchDog源码分析

本文分析了WatchDog的源码,其基本原理是通过HandlerChecker在系统服务Loop队列前端post监测任务,若30s内未执行,视为卡顿,60s未完成则重启system_server。WatchDog主要检测死锁,例如ActivityManagerService和MediaProjectionManagerService的monitor()方法。然而,postAtFrontOfQueue可能导致WatchDog的run方法无法执行,引发误判和性能影响。
摘要由CSDN通过智能技术生成
  1. 看了一下watchdog的源码总结一下
  2. 基本原理
    1. HandlerChecker 是基本的检测类,scheduleCheckLocked里面会记录开始时间并将minitor()检测方法postAtFrontOfQueue到Loop的队列前端,如果30s内没有执行到这个方法说明系统已经卡顿了(正常Loop队列前端的方法应该很快执行),
      打印一次ActivityManagerService.dumpStackTraces。如果60s没有执行完 重启 并打印 堆栈信息,重启的目的是重新初始化system_server避免一直卡下去
    2. 其中monitor方法作为执行的检测办法被各个检测对象实现,大都是一些获取锁的操作,用来检测死锁情况
    3. 下面是几个类重写的monitor()方法
      1. ActivityManagerService 只调用了synchronized 用来检测死锁没有其他动作
        1. /** In this method we try to acquire our lock to make sure that we have not deadlocked */
          public void monitor() {
          synchronized (this) { }
          }
      2. MediaProjectionManagerService 也是检测死锁

        1. @Override
          publicvoid

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redisson是一个基于Redis的Java驻留库,提供了分布式和线程安全的Java数据结构。Redisson的分布式锁实现是基于Redis的setnx命令和Lua脚本实现的。下面是Redisson分布式锁的源码分析: 1.获取锁 Redisson的分布式锁获取方法是tryAcquire方法,该方法首先会尝试使用setnx命令在Redis中创建一个key,如果创建成功则表示获取锁成功,否则会进入自旋等待。在自旋等待期间,Redisson会使用watchDog机制来监控锁的状态,如果锁被其他线程释放,则会重新尝试获取锁。 2.释放锁 Redisson的分布式锁释放方法是release方法,该方法会使用Lua脚本来判断当前线程是否持有锁,如果持有锁则会使用del命令删除锁的key。 3.watchDog机制 Redisson的watchDog机制是用来监控锁的状态的,该机制会在获取锁时启动一个定时任务,定时任务会检查锁的状态,如果锁被其他线程释放,则会重新尝试获取锁。 ```java // 获取锁 public boolean tryAcquire(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException { long time = unit.toMillis(waitTime); long current = System.currentTimeMillis(); final long threadId = Thread.currentThread().getId(); final long leaseTimeInMillis = unit.toMillis(leaseTime); while (true) { if (tryAcquire()) { scheduleExpirationRenewal(threadId, leaseTimeInMillis); return true; } time -= (System.currentTimeMillis() - current); if (time <= 0) { return false; } current = System.currentTimeMillis(); if (Thread.interrupted()) { throw new InterruptedException(); } // watchDog机制 RFuture<RedissonLockEntry> future = subscribe(threadId); if (!future.await(time, TimeUnit.MILLISECONDS)) { return false; } } } // 释放锁 public void unlock() { if (isHeldByCurrentThread()) { unlockInner(); } } private void unlockInner() { Long ttl = commandExecutor.evalWriteAsync(getName(), LongCodec.INSTANCE, RedisCommands.EVAL_LONG, "if (redis.call('hexists', KEYS[1], ARGV[2]) == 0) then return nil end; " + "local counter = redis.call('hincrby', KEYS[1], ARGV[2], -1); " + "if (counter > 0) then return 0 end; " + "redis.call('del', KEYS[1]); " + "redis.call('publish', KEYS[2], ARGV[1]); " + "return 1;", Arrays.<Object>asList(getName(), getChannelName()), encode(new UnlockMessage(getName(), getLockName())), id); cancelExpirationRenewal(); if (ttl == null) { throw new IllegalMonitorStateException("attempt to unlock lock, not locked by current thread by node id: " + id + " thread-id: " + Thread.currentThread().getId()); } if (ttl == -1) { get(lockName).deleteAsync(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值