[6.2.0][技术优化] 优化各线程cpu使用的锁相关逻辑
This commit is contained in:
@@ -5,32 +5,19 @@ import com.zhjt.service.chain.*
|
||||
|
||||
internal class MainBlockLinkedLog {
|
||||
|
||||
fun record(extra: Map<String, List<String>>) {
|
||||
fun record(json: String) {
|
||||
try {
|
||||
recordInternal(extra)
|
||||
recordInternal(json)
|
||||
} catch (t: Throwable) {
|
||||
t.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
fun recordCpuUsage(extra: Map<String, String>) {
|
||||
recordCpuInternal(extra)
|
||||
}
|
||||
|
||||
@ChainLog(
|
||||
linkChainLog = ChainConstant.CHAIN_TYPE_ANR_LEAK,
|
||||
linkCode = ChainConstant.CHAIN_SOURCE_HMI,
|
||||
nodeAliasCode = ChainConstant.CHAIN_CODE_MAIN_BLOCK,
|
||||
paramIndexes = [0]
|
||||
)
|
||||
private fun recordInternal(extra: Map<String, List<String>>) {}
|
||||
|
||||
|
||||
@ChainLog(
|
||||
linkChainLog = ChainConstant.CHAIN_TYPE_ANR_LEAK,
|
||||
linkCode = ChainConstant.CHAIN_SOURCE_HMI,
|
||||
nodeAliasCode = ChainConstant.CHAIN_CODE_MAIN_BLOCK,
|
||||
paramIndexes = [0]
|
||||
)
|
||||
private fun recordCpuInternal(extra: Map<String, String>) {}
|
||||
private fun recordInternal(json: String) {}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import com.mogo.eagle.core.block.runtime.listener.*
|
||||
import com.mogo.eagle.core.block.runtime.report.*
|
||||
import com.mogo.eagle.core.function.api.devatools.block.*
|
||||
import com.mogo.eagle.core.function.call.devatools.CallerDevaToolsManager
|
||||
import com.mogo.eagle.core.utilcode.util.GsonUtils
|
||||
import java.util.concurrent.TimeUnit.SECONDS
|
||||
|
||||
internal class MoGoBlockProviderImpl: IMoGoBlockProvider, IBlockListener {
|
||||
@@ -55,15 +56,15 @@ internal class MoGoBlockProviderImpl: IMoGoBlockProvider, IBlockListener {
|
||||
val mainThreadUsage = cpu.remove("MainThreadUsage")
|
||||
val processUsage = cpu.remove("ProcessUsage")
|
||||
if (mainThreadUsage != null && processUsage != null) {
|
||||
linkedLog.recordCpuUsage(LinkedHashMap<String, String>().also { itx ->
|
||||
itx["MainThread"] = "${ "%.2f".format(mainThreadUsage * 1.0f * 100 / processUsage) }% ($mainThreadUsage, $processUsage)"
|
||||
cpu.entries.sortedByDescending { it.value }.forEach { sorted ->
|
||||
itx[sorted.key] = "${ "%.2f".format(sorted.value * 1.0f * 100 / processUsage) }% (${sorted.value}, $processUsage)"
|
||||
map["cpu"] = ArrayList<String>().also {
|
||||
it.add("MainThread: ${ "%.2f".format(mainThreadUsage * 1.0f * 100 / processUsage) }% ($mainThreadUsage, $processUsage)")
|
||||
cpu.entries.sortedByDescending { e ->
|
||||
it.add("${e.key}: ${ "%.2f".format(e.value * 1.0f * 100 / processUsage) }% (${e.value}, $processUsage)")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
linkedLog.record(map)
|
||||
linkedLog.record(GsonUtils.toJson(map))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -50,13 +50,20 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
override fun updateOtherThreadTime() {
|
||||
val last = otherThreadLaunchedTime.get()
|
||||
val now = Debug.threadCpuTimeNanos()
|
||||
var flag = true
|
||||
try {
|
||||
if (last == null) {
|
||||
flag = false
|
||||
otherThreadLaunchedTime.set(now)
|
||||
return
|
||||
}
|
||||
val duration = now - last
|
||||
if (duration <= 10000) {
|
||||
if (duration < 1_000_000) {
|
||||
//每1毫秒更新线程时间
|
||||
flag = false
|
||||
return
|
||||
}
|
||||
if (lock.isLocked) {
|
||||
return
|
||||
}
|
||||
var builder = builder.get()
|
||||
@@ -68,16 +75,24 @@ internal class MoGoCpuUsageProviderImpl: IMoGoCpuUsageProvider {
|
||||
builder.setLength(0)
|
||||
}
|
||||
builder.append(Thread.currentThread().name)
|
||||
lock.withLock {
|
||||
val isGetLock = lock.tryLock()
|
||||
if (!isGetLock) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
while (map.size >= maxSize) {
|
||||
val toEvict = map.entries.iterator().next()
|
||||
map.remove(toEvict.key)
|
||||
}
|
||||
val key = builder.toString()
|
||||
map[key] = map.getOrPut(key) { 0L } + duration
|
||||
} finally {
|
||||
lock.unlock()
|
||||
}
|
||||
} finally {
|
||||
otherThreadLaunchedTime.set(now)
|
||||
if (flag) {
|
||||
otherThreadLaunchedTime.set(now)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user