批处理实现:自动抓取perfetto日志 自动导出到当前文件夹 自动打开分析页面

序言

最近在研究性能问题需要抓取trace文件。于是写了个脚本

使用

双击运行批处理文件,可以开始记录trace。而且以当前文件夹下面的。config.pbtx 作为配置文件。 (pbtx就是一个json文件。配置了需要抓取那些参数,可以通过ui.perfetto.dev/中的Record new trace (左侧)的菜单,在右侧设置最近要记录什么内容。来生成配置文件。可以参考Android Perfetto 系列 2:Perfetto Trace 抓取 最后的文件在Cmdline instructions菜单中显示。 右边下方的内容拷贝到。当前目录,并命名为config.pbtx 记得把配置中的时间去除,这样可以一直记录。 在当前文件夹下新建批处理

bat 复制代码
@echo off
chcp 65001 >nul

@echo off
setlocal enabledelayedexpansion

:: 获取当前目录
echo 当前目录: %cd%

:: 使用 PowerShell 获取时间戳
for /f "delims=" %%a in ('powershell.exe -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set timestamp=%%a
echo 时间戳: %timestamp%

:: 删除旧 trace 文件
adb shell rm -rf /data/misc/perfetto-traces/trace.perfetto
timeout /t 1 >nul

:: 推送配置文件
adb push config.pbtx /data/misc/perfetto-configs/config.pbtx
timeout /t 1 >nul

:: 开始录制
echo 开始记录 trace,请按 Ctrl+C 结束录制...
adb shell -tt perfetto --txt -c /data/misc/perfetto-configs/config.pbtx -o /data/misc/perfetto-traces/trace.perfetto

:: 导出 trace 到当前目录
adb pull /data/misc/perfetto-traces/trace.perfetto .\trace_%timestamp%.perfetto

if exist trace_%timestamp%.perfetto (
    echo ✅ 文件已成功导出到当前目录:
    echo     trace_%timestamp%.perfetto

    echo.
    echo 🔗 正在打开 Perfetto 官方网站,请稍后手动上传 trace 文件。
    echo     1. 浏览器将自动打开 Perfetto UI。
    echo     2. 点击左上角 ^> Open trace file^> 按钮。
    echo     3. 选择刚刚保存的 trace_%timestamp%.perfetto 文件。
    echo.

    :: 打开默认浏览器访问 Perfetto UI
    start "" https://ui.perfetto.dev/

) else (
    echo ❌ 文件未找到,请检查设备路径或配置。
)

pause

双击bat运行,要结束录制的话,只需要按下 Ctrl+C 就可以了。 最后文件会自动保存到当前文件夹下,以时间命名。 最后还会自动打开分析网站。只需要把日志文件拖入浏览器就可以打开。

附带配置

这是我的配置文件,可以记录logcat

js 复制代码
buffers {
  size_kb: 65536
  fill_policy: DISCARD
}
buffers {
  size_kb: 4096
  fill_policy: DISCARD
}
data_sources {
  config {
    name: "linux.ftrace"
    ftrace_config {
      ftrace_events: "sched/sched_process_exit"
      ftrace_events: "sched/sched_process_free"
      ftrace_events: "task/task_newtask"
      ftrace_events: "task/task_rename"
      ftrace_events: "binder/*"
      ftrace_events: "block/*"
      ftrace_events: "clk/*"
      ftrace_events: "devfreq/*"
      ftrace_events: "ext4/*"
      ftrace_events: "f2fs/*"
      ftrace_events: "i2c/*"
      ftrace_events: "irq/*"
      ftrace_events: "kmem/*"
      ftrace_events: "memory_bus/*"
      ftrace_events: "mmc/*"
      ftrace_events: "oom/*"
      ftrace_events: "power/*"
      ftrace_events: "regulator/*"
      ftrace_events: "sched/*"
      ftrace_events: "sync/*"
      ftrace_events: "task/*"
      ftrace_events: "vmscan/*"
      ftrace_events: "fastrpc/*"
      ftrace_events: "sched/sched_switch"
      ftrace_events: "power/suspend_resume"
      ftrace_events: "sched/sched_blocked_reason"
      ftrace_events: "sched/sched_wakeup"
      ftrace_events: "sched/sched_wakeup_new"
      ftrace_events: "sched/sched_waking"
      ftrace_events: "sched/sched_process_exit"
      ftrace_events: "sched/sched_process_free"
      ftrace_events: "task/task_newtask"
      ftrace_events: "task/task_rename"
      ftrace_events: "power/cpu_frequency"
      ftrace_events: "power/cpu_idle"
      ftrace_events: "power/suspend_resume"
      ftrace_events: "raw_syscalls/sys_enter"
      ftrace_events: "raw_syscalls/sys_exit"
      ftrace_events: "power/gpu_frequency"
      ftrace_events: "gpu_mem/gpu_mem_total"
      ftrace_events: "power/gpu_work_period"
      ftrace_events: "ftrace/print"
      atrace_categories: "adb"
      atrace_categories: "aidl"
      atrace_categories: "am"
      atrace_categories: "audio"
      atrace_categories: "binder_driver"
      atrace_categories: "binder_lock"
      atrace_categories: "bionic"
      atrace_categories: "camera"
      atrace_categories: "dalvik"
      atrace_categories: "database"
      atrace_categories: "gfx"
      atrace_categories: "hal"
      atrace_categories: "input"
      atrace_categories: "network"
      atrace_categories: "nnapi"
      atrace_categories: "pm"
      atrace_categories: "power"
      atrace_categories: "res"
      atrace_categories: "rro"
      atrace_categories: "rs"
      atrace_categories: "sm"
      atrace_categories: "ss"
      atrace_categories: "vibrator"
      atrace_categories: "video"
      atrace_categories: "view"
      atrace_categories: "webview"
      atrace_categories: "wm"
      symbolize_ksyms: true
      disable_generic_events: true
    }
  }
}
data_sources {
  config {
    name: "linux.process_stats"
    process_stats_config {
      scan_all_processes_on_start: true
    }
  }
}
data_sources {
  config {
    name: "linux.sys_stats"
    sys_stats_config {
      meminfo_period_ms: 5000
      stat_period_ms: 250
      stat_counters: STAT_CPU_TIMES
      stat_counters: STAT_FORK_COUNT
      cpufreq_period_ms: 250
    }
  }
}
data_sources {
  config {
    name: "android.gpu.memory"
  }
}
data_sources {
  config {
    name: "android.java_hprof"
    java_hprof_config {
    }
  }
}
data_sources {
  config {
    name: "android.log"
    android_log_config {
      log_ids: LID_CRASH
      log_ids: LID_DEFAULT
      log_ids: LID_EVENTS
      log_ids: LID_KERNEL
      log_ids: LID_RADIO
      log_ids: LID_SECURITY
      log_ids: LID_STATS
      log_ids: LID_SYSTEM
    }
  }
}
data_sources {
  config {
    name: "android.surfaceflinger.frametimeline"
  }
}
data_sources {
  config {
    name: "android.game_interventions"
  }
}
data_sources {
  config {
    name: "android.network_packets"
    network_packet_trace_config {
      poll_ms: 1000
    }
  }
}
data_sources {
  config {
    name: "android.packages_list"
  }
}
data_sources {
  config {
    name: "android.statsd"
    statsd_tracing_config {
      push_atom_id: ATOM_PROCESS_STATE_CHANGED
      push_atom_id: ATOM_BLE_SCAN_RESULT_RECEIVED
      push_atom_id: ATOM_SENSOR_STATE_CHANGED
      push_atom_id: ATOM_GPS_SCAN_STATE_CHANGED
    }
  }
}
data_sources {
  config {
    name: "linux.perf"
    perf_event_config {
      timebase {
        frequency: 100
        timestamp_clock: PERF_CLOCK_MONOTONIC
      }
      callstack_sampling {
      }
    }
  }
}
相关推荐
零叹2 小时前
篇章五 系统性能优化——资源优化——CPU优化(2)
性能优化·对象池·锁优化·java高并发编程·并发数据结构·缓存更新
异常君12 小时前
Redis String 类型的底层实现与性能优化
java·redis·性能优化
异常君12 小时前
用户态与内核态:Java 程序员必懂的两种执行状态
性能优化·操作系统·cpu
徐子竣1 天前
Unity性能优化-C#编码模块
unity·性能优化·游戏引擎
一只叫煤球的猫1 天前
1200行代码的前端组件?这套拆分套路让代码从此优雅
前端·vue.js·性能优化
异常君1 天前
Dubbo 高可用性核心机制详解与实战(下)
性能优化·dubbo·设计
快乐肚皮1 天前
快速排序优化技巧详解:提升性能的关键策略
java·算法·性能优化·排序算法
DemonAvenger2 天前
Go 内存分析工具链:从开发到生产环境
性能优化·架构·go
意疏2 天前
深入解析MySQL Join算法原理与性能优化实战指南
mysql·算法·性能优化