From cd46a2fe6caecd2af25bd2a1b087e58ae1aba2ed Mon Sep 17 00:00:00 2001 From: cfig Date: Sat, 28 Dec 2024 14:50:51 +0800 Subject: [PATCH] staging --- .gitignore | 7 + .../src/main/kotlin/packable/MiscImgParser.kt | 7 +- .../src/main/kotlin/rom/misc/MiscImage.kt | 2 + lazybox/build.gradle.kts | 1 + lazybox/src/main/kotlin/cfig/lazybox/AMS.kt | 128 ++++++++++++++++++ lazybox/src/main/kotlin/cfig/lazybox/App.kt | 8 ++ .../kotlin/cfig/lazybox/sysinfo/SysInfo.kt | 32 +++-- tools/factory_image_parser.py | 8 +- tools/{ => local/bin}/abe | 17 ++- 9 files changed, 186 insertions(+), 24 deletions(-) rename tools/{ => local/bin}/abe (65%) diff --git a/.gitignore b/.gitignore index 34f44ec..48da03c 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,10 @@ helper/build/ lazybox/build/ avbImpl/build/ bbootimg/build/ +aosp/libsparse/sparse/build/ +aosp/libsparse/simg2simg/build/ +aosp/apksigner/bin/ +aosp/boot_signer/bin/ +bbootimg/bin/ +helper/bin/ +build/ diff --git a/bbootimg/src/main/kotlin/packable/MiscImgParser.kt b/bbootimg/src/main/kotlin/packable/MiscImgParser.kt index bc0e50f..7d61fc0 100644 --- a/bbootimg/src/main/kotlin/packable/MiscImgParser.kt +++ b/bbootimg/src/main/kotlin/packable/MiscImgParser.kt @@ -20,6 +20,7 @@ import org.slf4j.LoggerFactory import rom.misc.MiscImage import java.io.File import java.io.RandomAccessFile +import kotlin.io.nameWithoutExtension import kotlin.io.path.Path import kotlin.io.path.deleteIfExists @@ -63,7 +64,7 @@ class MiscImgParser : IPackable { log.info("${out.name} is ready") } - override fun flash(fileName: String, deviceName: String) { + fun flash(fileName: String) { val stem = fileName.substring(0, fileName.indexOf(".")) super.flash("$fileName.new", stem) } @@ -72,8 +73,8 @@ class MiscImgParser : IPackable { super.`@verify`(fileName) } - override fun pull(fileName: String, deviceName: String) { - super.pull(fileName, deviceName) + fun pull(fileName: String) { + super.pull(fileName, File(fileName).nameWithoutExtension) } fun clear(fileName: String) { diff --git a/bbootimg/src/main/kotlin/rom/misc/MiscImage.kt b/bbootimg/src/main/kotlin/rom/misc/MiscImage.kt index e9cd608..ec17d84 100644 --- a/bbootimg/src/main/kotlin/rom/misc/MiscImage.kt +++ b/bbootimg/src/main/kotlin/rom/misc/MiscImage.kt @@ -26,6 +26,8 @@ data class MiscImage( ret.virtualAB = VirtualABMessage(fis) } catch (e: IllegalArgumentException) { log.info(e.toString()) + } catch (e: IllegalStateException) { + log.info(e.toString()) } } FileInputStream(fileName).use { fis -> diff --git a/lazybox/build.gradle.kts b/lazybox/build.gradle.kts index cfab236..684d10d 100644 --- a/lazybox/build.gradle.kts +++ b/lazybox/build.gradle.kts @@ -24,6 +24,7 @@ dependencies { implementation("org.slf4j:slf4j-api:2.0.9") implementation("org.apache.commons:commons-compress:1.26.0") implementation("com.github.freva:ascii-table:1.8.0") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3") // Use the Kotlin JUnit 5 integration. testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") // Use the JUnit 5 integration. diff --git a/lazybox/src/main/kotlin/cfig/lazybox/AMS.kt b/lazybox/src/main/kotlin/cfig/lazybox/AMS.kt index 4a1396c..a0c8afe 100644 --- a/lazybox/src/main/kotlin/cfig/lazybox/AMS.kt +++ b/lazybox/src/main/kotlin/cfig/lazybox/AMS.kt @@ -1,4 +1,132 @@ package cfig.lazybox +import cfig.lazybox.sysinfo.SysInfo.Companion.runAndWrite +import org.slf4j.LoggerFactory +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.FileOutputStream + class AMS { + enum class StandbyBucket(val id: Int) { + STANDBY_BUCKET_EXEMPTED(5), + STANDBY_BUCKET_ACTIVE(10), + STANDBY_BUCKET_WORKING_SET(20), + STANDBY_BUCKET_FREQUENT(30), + STANDBY_BUCKET_RARE(40), + STANDBY_BUCKET_RESTRICTED(45), + STANDBY_BUCKET_NEVER(50); + + companion object { + fun fromValue(value: Int): StandbyBucket? = StandbyBucket.entries.firstOrNull { it.id == value } + } + } + + companion object { + val log = LoggerFactory.getLogger(AMS::class.qualifiedName) + fun getProcRank(): MutableList { + val pkgRank: MutableList = mutableListOf() + ByteArrayOutputStream().use { + runAndWrite("adb shell procrank", it, true) + it.toString().split("\n").subList(1, 21).forEachIndexed { i, line -> + val pkg = line.trim().split("\\s+".toRegex()).last() + pkgRank.add(pkg) + } + } + + //print pkgRank items, with index and content + + FileOutputStream("proc_rank.log").use { + pkgRank.forEachIndexed { i, s -> + it.write("$s\n".toByteArray()) + } + } + return pkgRank + } + + fun getStandbyBucket(): HashMap> { + val buckets: HashMap> = hashMapOf( + StandbyBucket.STANDBY_BUCKET_EXEMPTED to mutableListOf(), + StandbyBucket.STANDBY_BUCKET_ACTIVE to mutableListOf(), + StandbyBucket.STANDBY_BUCKET_WORKING_SET to mutableListOf(), + StandbyBucket.STANDBY_BUCKET_FREQUENT to mutableListOf(), + StandbyBucket.STANDBY_BUCKET_RARE to mutableListOf(), + StandbyBucket.STANDBY_BUCKET_RESTRICTED to mutableListOf(), + StandbyBucket.STANDBY_BUCKET_NEVER to mutableListOf(), + ) + ByteArrayOutputStream().use { + runAndWrite("adb shell am get-standby-bucket", it, true) + it.toString().trim().split("\n").forEachIndexed { i, line -> + log.info("#$i: $line") + if (line.split(":").size == 2) { + val b = line.split(":").get(0).trim() + val a = line.split(":").get(1).trim() + log.info("[$a]-[$b]") + buckets[StandbyBucket.fromValue(a.toInt())]!!.add(b) + } + } + } + StandbyBucket.entries.forEach { + log.info(it.toString() + "(${it.id})") + buckets[it]!!.apply { sort() }.forEach { + log.info("\t$it") + } + } + return buckets + } + + fun getStandbyBucket2(): HashMap { + val ret: HashMap = HashMap() + ByteArrayOutputStream().use { + runAndWrite("adb shell am get-standby-bucket", it, true) + it.toString().trim().split("\n").forEachIndexed { i, line -> + log.info("#$i: $line") + if (line.split(":").size == 2) { + val b = line.split(":").get(0).trim() + val a = line.split(":").get(1).trim() + log.info("[$a]-[$b]") + ret.put(b, StandbyBucket.fromValue(a.toInt())!!) + } + } + } + return ret + } + + fun getOom() { + val text = ByteArrayOutputStream().use { + runAndWrite("adb shell dumpsys activity oom", it, true) + it.toString() + } + log.info(text) + val lines = text.trim().split("\n") // Split lines + val regex = Regex("""^ +Proc #\d+: (.*?) +oom: max=\d+ curRaw=(-?\d+) setRaw=(-?\d+) cur=(-?\d+) set=(-?\d+)""") // Match relevant parts + val results = lines.mapNotNull { line -> + val matchResult = regex.matchEntire(line) ?: return@mapNotNull null + val groups = matchResult.groups + // Extract data from groups + val packageName = groups[1]?.value ?: "" + val oomCurValue = groups[2]?.value?.toIntOrNull() ?: 0 + val status = groups[3]?.value ?: "" + + // Create the Any array + arrayOf(packageName, oomCurValue, status) + log.info("$packageName -> $oomCurValue -> $status") + } + } + + + fun computeRankAndBucket(rank: MutableList, bkt: HashMap) { + val sb = StringBuilder() + rank.forEach { + val bktEntry = bkt.get(it) + if (bktEntry != null) { + sb.append(String.format("%-40s %s\n", it, bktEntry)) + } else { + sb.append(String.format("%-40s -\n", it)) + } + } + log.info("Writing to rank_bucket.log ...\n$sb") + File("rank_bucket.log").writeText(sb.toString()) + } + + } } \ No newline at end of file diff --git a/lazybox/src/main/kotlin/cfig/lazybox/App.kt b/lazybox/src/main/kotlin/cfig/lazybox/App.kt index 6dbdd55..9c6423a 100644 --- a/lazybox/src/main/kotlin/cfig/lazybox/App.kt +++ b/lazybox/src/main/kotlin/cfig/lazybox/App.kt @@ -24,6 +24,8 @@ fun main(args: Array) { println("tracecmd : analyze trace-cmd report") println("cpuinfo : get cpu info from /sys/devices/system/cpu/") println("sysinfo : get overall system info from Android") + println("\nIncubating usage:") + println("apps : get apk file list from Android") exitProcess(0) } if (args[0] == "cpuinfo") { @@ -65,4 +67,10 @@ fun main(args: Array) { } CompileCommand().run(args[1], args[2]) } + if (args[0] == "apps") { + AppList.retrieveList() + } + if (args[0] == "x") { + AMS.computeRankAndBucket(AMS.getProcRank(), AMS.getStandbyBucket2()) + } } diff --git a/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt b/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt index 59c5518..4b85281 100644 --- a/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt +++ b/lazybox/src/main/kotlin/cfig/lazybox/sysinfo/SysInfo.kt @@ -17,21 +17,6 @@ import kotlin.io.path.writeText import java.io.ByteArrayOutputStream class SysInfo { - private fun runAndWrite(cmd: String, outStream: OutputStream, check: Boolean) { - Helper.powerRun2(cmd, null).let { - if (it[0] as Boolean) { - outStream.write(it[1] as ByteArray) - } else { - if (check) { - log.warn(String(it[1] as ByteArray)) - throw RuntimeException(String(it[2] as ByteArray)) - } else { - log.warn(String(it[1] as ByteArray)) - log.warn(String(it[2] as ByteArray)) - } - } - } - } fun makeTar(tarFile: String, srcDir: String, fmt: String) { val pyScript = @@ -153,5 +138,22 @@ makeTar("%s", "%s") companion object { private val log = LoggerFactory.getLogger(SysInfo::class.java) + + fun runAndWrite(cmd: String, outStream: OutputStream, check: Boolean) { + Helper.powerRun2(cmd, null).let { + if (it[0] as Boolean) { + outStream.write(it[1] as ByteArray) + } else { + if (check) { + log.warn(String(it[1] as ByteArray)) + log.warn(String(it[2] as ByteArray)) + throw RuntimeException(String(it[2] as ByteArray)) + } else { + outStream.write(it[1] as ByteArray) + outStream.write(it[2] as ByteArray) + } + } + } + } } } diff --git a/tools/factory_image_parser.py b/tools/factory_image_parser.py index db529d0..9e89200 100755 --- a/tools/factory_image_parser.py +++ b/tools/factory_image_parser.py @@ -38,7 +38,9 @@ known_list = [ "vendor_boot.img", "vendor_dlkm.img", "vendor.img", - "vendor_kernel_boot.img" + "vendor_kernel_boot.img", + "odm.img", + "recovery.img", ] unknown_list = [ @@ -67,6 +69,10 @@ unknown_list = [ "ldfw.img", #Pixel8a "pbl.img", #Pixel8a "tzsw.img", #Pixel8a + "logo.img", #ADT-3 + "super_empty_all.img", #ADT-3 + "bootloader.img", #many + "dt.img", ] tmp2 = "tmp2" diff --git a/tools/abe b/tools/local/bin/abe similarity index 65% rename from tools/abe rename to tools/local/bin/abe index b27c8d8..ed14df1 100755 --- a/tools/abe +++ b/tools/local/bin/abe @@ -11,17 +11,24 @@ if [[ $# -eq 0 ]]; then fi operation=$1 +echo arg num: $# +echo "args: $0 $1 $2" # Determine which operation to perform case $operation in "unpack") - if [[ $# -eq 3 ]]; then + if [[ $# -eq 2 ]]; then + file=`realpath $2` + dir=`realpath out` + cd ${baseDir}/../../../ + gradle unpack --args="unpackInternal $file $dir" + elif [[ $# -eq 3 ]]; then file=`realpath $2` dir=`realpath $3` - cd ${baseDir}/../ + cd ${baseDir}/../../../ gradle unpack --args="unpackInternal $file $dir" else - cd ${baseDir}/../ + cd ${baseDir}/../../../ gradle unpack fi ;; @@ -29,10 +36,10 @@ case $operation in if [[ $# -eq 3 ]]; then dir=`realpath $2` file=`realpath $3` - cd ${baseDir}/../ + cd ${baseDir}/../../../ gradle pack --args="packInternal $dir $file" else - cd ${baseDir}/../ + cd ${baseDir}/../../../ gradle pack fi ;;