11package cfig.lazybox
22
3+ import cfig.lazybox.sysinfo.SysInfo.Companion.runAndWrite
4+ import org.slf4j.LoggerFactory
5+ import java.io.ByteArrayOutputStream
6+ import java.io.File
7+ import java.io.FileOutputStream
8+
39class AMS {
10+ enum class StandbyBucket (val id : Int ) {
11+ STANDBY_BUCKET_EXEMPTED (5 ),
12+ STANDBY_BUCKET_ACTIVE (10 ),
13+ STANDBY_BUCKET_WORKING_SET (20 ),
14+ STANDBY_BUCKET_FREQUENT (30 ),
15+ STANDBY_BUCKET_RARE (40 ),
16+ STANDBY_BUCKET_RESTRICTED (45 ),
17+ STANDBY_BUCKET_NEVER (50 );
18+
19+ companion object {
20+ fun fromValue (value : Int ): StandbyBucket ? = StandbyBucket .entries.firstOrNull { it.id == value }
21+ }
22+ }
23+
24+ companion object {
25+ val log = LoggerFactory .getLogger(AMS ::class .qualifiedName)
26+ fun getProcRank (): MutableList <String > {
27+ val pkgRank: MutableList <String > = mutableListOf ()
28+ ByteArrayOutputStream ().use {
29+ runAndWrite(" adb shell procrank" , it, true )
30+ it.toString().split(" \n " ).subList(1 , 21 ).forEachIndexed { i, line ->
31+ val pkg = line.trim().split(" \\ s+" .toRegex()).last()
32+ pkgRank.add(pkg)
33+ }
34+ }
35+
36+ // print pkgRank items, with index and content
37+
38+ FileOutputStream (" proc_rank.log" ).use {
39+ pkgRank.forEachIndexed { i, s ->
40+ it.write(" $s \n " .toByteArray())
41+ }
42+ }
43+ return pkgRank
44+ }
45+
46+ fun getStandbyBucket (): HashMap <StandbyBucket , MutableList <String >> {
47+ val buckets: HashMap <StandbyBucket , MutableList <String >> = hashMapOf(
48+ StandbyBucket .STANDBY_BUCKET_EXEMPTED to mutableListOf (),
49+ StandbyBucket .STANDBY_BUCKET_ACTIVE to mutableListOf (),
50+ StandbyBucket .STANDBY_BUCKET_WORKING_SET to mutableListOf (),
51+ StandbyBucket .STANDBY_BUCKET_FREQUENT to mutableListOf (),
52+ StandbyBucket .STANDBY_BUCKET_RARE to mutableListOf (),
53+ StandbyBucket .STANDBY_BUCKET_RESTRICTED to mutableListOf (),
54+ StandbyBucket .STANDBY_BUCKET_NEVER to mutableListOf (),
55+ )
56+ ByteArrayOutputStream ().use {
57+ runAndWrite(" adb shell am get-standby-bucket" , it, true )
58+ it.toString().trim().split(" \n " ).forEachIndexed { i, line ->
59+ log.info(" #$i : $line " )
60+ if (line.split(" :" ).size == 2 ) {
61+ val b = line.split(" :" ).get(0 ).trim()
62+ val a = line.split(" :" ).get(1 ).trim()
63+ log.info(" [$a ]-[$b ]" )
64+ buckets[StandbyBucket .fromValue(a.toInt())]!! .add(b)
65+ }
66+ }
67+ }
68+ StandbyBucket .entries.forEach {
69+ log.info(it.toString() + " (${it.id} )" )
70+ buckets[it]!! .apply { sort() }.forEach {
71+ log.info(" \t $it " )
72+ }
73+ }
74+ return buckets
75+ }
76+
77+ fun getStandbyBucket2 (): HashMap <String , StandbyBucket > {
78+ val ret: HashMap <String , StandbyBucket > = HashMap <String , StandbyBucket >()
79+ ByteArrayOutputStream ().use {
80+ runAndWrite(" adb shell am get-standby-bucket" , it, true )
81+ it.toString().trim().split(" \n " ).forEachIndexed { i, line ->
82+ log.info(" #$i : $line " )
83+ if (line.split(" :" ).size == 2 ) {
84+ val b = line.split(" :" ).get(0 ).trim()
85+ val a = line.split(" :" ).get(1 ).trim()
86+ log.info(" [$a ]-[$b ]" )
87+ ret.put(b, StandbyBucket .fromValue(a.toInt())!! )
88+ }
89+ }
90+ }
91+ return ret
92+ }
93+
94+ fun getOom () {
95+ val text = ByteArrayOutputStream ().use {
96+ runAndWrite(" adb shell dumpsys activity oom" , it, true )
97+ it.toString()
98+ }
99+ log.info(text)
100+ val lines = text.trim().split(" \n " ) // Split lines
101+ val regex = Regex (""" ^ +Proc #\d+: (.*?) +oom: max=\d+ curRaw=(-?\d+) setRaw=(-?\d+) cur=(-?\d+) set=(-?\d+)""" ) // Match relevant parts
102+ val results = lines.mapNotNull { line ->
103+ val matchResult = regex.matchEntire(line) ? : return @mapNotNull null
104+ val groups = matchResult.groups
105+ // Extract data from groups
106+ val packageName = groups[1 ]?.value ? : " "
107+ val oomCurValue = groups[2 ]?.value?.toIntOrNull() ? : 0
108+ val status = groups[3 ]?.value ? : " "
109+
110+ // Create the Any array
111+ arrayOf(packageName, oomCurValue, status)
112+ log.info(" $packageName -> $oomCurValue -> $status " )
113+ }
114+ }
115+
116+
117+ fun computeRankAndBucket (rank : MutableList <String >, bkt : HashMap <String , StandbyBucket >) {
118+ val sb = StringBuilder ()
119+ rank.forEach {
120+ val bktEntry = bkt.get(it)
121+ if (bktEntry != null ) {
122+ sb.append(String .format(" %-40s %s\n " , it, bktEntry))
123+ } else {
124+ sb.append(String .format(" %-40s -\n " , it))
125+ }
126+ }
127+ log.info(" Writing to rank_bucket.log ...\n $sb " )
128+ File (" rank_bucket.log" ).writeText(sb.toString())
129+ }
130+
131+ }
4132}
0 commit comments