[6.2.0][技术优化] 只记录线程CPU使用时长TOP50
This commit is contained in:
@@ -228,7 +228,7 @@ ext {
|
||||
passport_secret : "com.zhidaoauto:sdk-java:1.0.5-SNAPSHOT",
|
||||
|
||||
// 主线程卡顿监测
|
||||
block_detector : "com.mogo.eagle.core.block:runtime:10.60.0",
|
||||
block_detector : "com.mogo.eagle.core.block:runtime:10.80.0",
|
||||
|
||||
//======================== google auto-service ===============
|
||||
google_auto_service : "com.google.auto.service:auto-service:1.0-rc7",
|
||||
|
||||
@@ -15,7 +15,7 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
|
||||
private var mainThreadCpuUsage: Long = 0L
|
||||
|
||||
private val maxSize = 500
|
||||
private val maxSize = 50 // 取top50的线程占用
|
||||
|
||||
private val map = LinkedHashMap<String, Long>(0, 0.75f, true)
|
||||
|
||||
@@ -23,16 +23,16 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
|
||||
private val builder = ThreadLocal<StringBuilder>()
|
||||
|
||||
private val otherThreadLastTime = ThreadLocal<Long>()
|
||||
private val otherThreadLaunchedTime = ThreadLocal<Long>()
|
||||
|
||||
private val mainLooper = Looper.getMainLooper()
|
||||
|
||||
override fun onProcessLaunched() {
|
||||
processLaunchTime = SystemClock.elapsedRealtime()
|
||||
processLaunchTime = SystemClock.elapsedRealtimeNanos()
|
||||
}
|
||||
|
||||
override fun onMainThreadLaunched() {
|
||||
mainThreadStartTime = Debug.threadCpuTimeNanos() / 1000_000
|
||||
mainThreadStartTime = Debug.threadCpuTimeNanos()
|
||||
}
|
||||
|
||||
override fun updateMainThreadTime() {
|
||||
@@ -40,20 +40,23 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
return
|
||||
}
|
||||
if (mainThreadStartTime > 0) {
|
||||
mainThreadCpuUsage = (Debug.threadCpuTimeNanos() / 1000_000 - mainThreadStartTime)
|
||||
val dur = Debug.threadCpuTimeNanos() - mainThreadStartTime
|
||||
if (dur > 0) {
|
||||
mainThreadCpuUsage = dur
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateOtherThreadTime() {
|
||||
var last = otherThreadLastTime.get()
|
||||
val now = Debug.threadCpuTimeNanos() / 1000_000
|
||||
val last = otherThreadLaunchedTime.get()
|
||||
val now = Debug.threadCpuTimeNanos()
|
||||
try {
|
||||
if (last == null) {
|
||||
last = now
|
||||
otherThreadLastTime.set(last)
|
||||
otherThreadLaunchedTime.set(now)
|
||||
return
|
||||
}
|
||||
val duration = now - last
|
||||
if (duration < 1) {
|
||||
if (duration <= 10000) {
|
||||
return
|
||||
}
|
||||
var builder = builder.get()
|
||||
@@ -66,14 +69,15 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
}
|
||||
builder.append(Thread.currentThread().name)
|
||||
lock.withLock {
|
||||
while (map.size > maxSize) {
|
||||
while (map.size >= maxSize) {
|
||||
val toEvict = map.entries.iterator().next()
|
||||
map.remove(toEvict.key)
|
||||
}
|
||||
map[builder.toString()] = map.getOrPut(builder.toString()) { 0 } + duration
|
||||
val key = builder.toString()
|
||||
map[key] = map.getOrPut(key) { 0L } + duration
|
||||
}
|
||||
} finally {
|
||||
otherThreadLastTime.set(now)
|
||||
otherThreadLaunchedTime.set(now)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +87,7 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
val iterator = map.entries.iterator()
|
||||
val rst = LinkedHashMap<String, Long>()
|
||||
rst["MainThreadUsage"] = mainThreadCpuUsage
|
||||
rst["ProcessUsage"] = SystemClock.elapsedRealtime() - processLaunchTime
|
||||
rst["ProcessUsage"] = SystemClock.elapsedRealtimeNanos() - processLaunchTime
|
||||
while (iterator.hasNext()) {
|
||||
val next = iterator.next()
|
||||
rst[next.key] = next.value
|
||||
|
||||
Reference in New Issue
Block a user