-
Notifications
You must be signed in to change notification settings - Fork 117
/
JvmStats.kt
81 lines (66 loc) · 2.47 KB
/
JvmStats.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package jvmmon
import com.jvmtop.monitor.VMInfo
import com.jvmtop.monitor.VMInfoState
import com.jvmtop.view.VMDetailView
import com.jvmtop.view.VMOverviewView
import org.andrejs.json.Json
class JvmStats {
private val vmOverviewView = VMOverviewView(0)
private var vmDetailView: VMDetailView? = null
init {
update()
}
fun getVmStats(vmId: Int): List<Json> {
update(vmId)
val vmInfos = vmOverviewView.vmInfoList
.filter { vm -> vm.state == VMInfoState.ATTACHED }
val vmStats = vmInfos
.map { toVmJsObject(it) }
.map { vmJs -> maybeAddThreads(vmJs, vmId, vmDetailView) }
return vmStats
}
private fun update(vmId: Int = 0) {
vmOverviewView.updateVMs(vmOverviewView.scanForNewVMs())
if (vmId != 0 && (vmDetailView == null || vmId != vmDetailView!!.vmId)) {
vmDetailView = VMDetailView(vmOverviewView.getVMInfo(vmId))
}
}
private fun maybeAddThreads(vmJson: Json, vmId: Int, view: VMDetailView?): Json {
if (view != null && vmJson.get("Id", -1) == vmId) {
val threads = view.topThreads
.map { toThreadJs(it) }
vmJson.put("threads", threads)
}
return vmJson
}
private fun toThreadJs(ts: VMDetailView.ThreadStats): Json {
val threadJs = Json()
.set("TID", ts.TID)
.set("name", ts.name)
.set("state", ts.state.toString())
.set("cpu", ts.cpu)
.set("totalCpu", ts.totalCpu)
.set("blockedBy", ts.blockedBy)
return threadJs
}
private fun toVmJsObject(vm: VMInfo): Json {
val vmJs = Json()
.set("Id", vm.id)
.set("DisplayName", displayName(vm))
.set("HeapUsed", vm.heapUsed)
.set("HeapMax", vm.heapMax)
.set("HeapSize", vm.heapSize)
.set("NonHeapUsed", vm.nonHeapUsed)
.set("NonHeapMax", vm.nonHeapMax)
.set("CpuLoad", vm.cpuLoad)
.set("GcLoad", vm.gcLoad)
.set("VMVersion", vm.vmVersion)
.set("OSUser", vm.osUser)
.set("ThreadCount", vm.threadCount)
.set("hasDeadlockThreads", vm.hasDeadlockThreads())
return vmJs
}
private fun displayName(vm: VMInfo): String {
return vm.displayName.trim().substringBefore(" ")
}
}