1 /*
<lambda>null2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package bench.flame.diff
17 
18 import bench.flame.diff.command.Diff
19 import bench.flame.diff.command.Init
20 import bench.flame.diff.command.List
21 import bench.flame.diff.command.Open
22 import bench.flame.diff.command.Save
23 import com.github.ajalt.clikt.core.CliktCommand
24 import com.github.ajalt.clikt.core.context
25 import com.github.ajalt.clikt.core.subcommands
26 import com.github.ajalt.clikt.output.MordantHelpFormatter
27 import com.github.ajalt.mordant.rendering.Widget
28 import com.github.ajalt.mordant.table.horizontalLayout
29 import com.github.ajalt.mordant.table.verticalLayout
30 import com.github.ajalt.mordant.widgets.Text
31 import com.github.ajalt.mordant.widgets.withPadding
32 
33 fun main(args: Array<out String>) {
34     BenchFlameDiff().subcommands(Init(), Save(), List(), Diff(), Open()).main(args)
35 }
36 
37 class BenchFlameDiff : CliktCommand(
38     help = "Generate differential flame graphs from microbenchmark CPU traces.",
39     epilog = """
40         Dependencies
41         FlameGraph - https://github.com/brendangregg/FlameGraph for generating graphs.
42         Simpleperf - Android NDK's Simpleperf for converting traces into FlameGraph input format.
43         Clikt      - com.github.ajalt.clikt:clikt for the CLI interface.
44         NuProcess  - com.zaxxer:nuprocess for running external processes.
45     """.trimIndent()
46 ) {
47     init {
<lambda>null48         context {
49             helpFormatter = {
50                 object : MordantHelpFormatter(it, showRequiredTag = true) {
51                     override fun renderEpilog(epilog: String): Widget = verticalLayout {
52                         val lines = epilog.lines()
53                         val header = lines.first() + ":"
54                         val dependencies = lines.drop(1).map {
55                             it.split(" - ").also { check(it.size == 2) }
56                                 .let { (name, desc) -> name to desc }
57                         }
58 
59                         cell(Text(theme.warning(header)))
60                         for ((name, desc) in dependencies) cell(
61                             horizontalLayout {
62                                 cell(Text(theme.info(name)).withPadding { left = 2; right = 1 })
63                                 cell(desc)
64                             }
65                         )
66                     }
67                 }
68             }
69         }
70     }
71 
runnull72     override fun run() = Unit
73 }
74