Android的dumpsys命令

轉自 https://www.jianshu.com/p/6bd4813b3c60

我們都知道Android是基於Linux的操作系統,它使用的是默認的Shell是Ash,之前的一篇文章有提到過《Shell腳本的簡單介紹》,但是這個東西很明顯是不能滿足Android需求的,所以Google開發了很多針對Android的命令行工具。

從git上把Android的源碼拉下,就會發現有一個cmds的文件夾,裏邊放着的全是Google爲Android開發的shell命令。其中dumpsys在*${path}/frameworks/native/cmds/dumpsys*目錄,如果感興趣可以自行去研究研究。

執行adb shell dumpsys,Android就會把系統裏邊的各種服務的狀態給你dump出來。這裏要提一下,不同的Android版本提供的服務會有所不同,不同的ROM廠商也會定製自己的服務跑在系統裏邊。

我的手機小米4C,MIUI6.3.31的系統(Android5.1.1),Dump出來的 結果大概是這樣子。

Currently running services:
  DisplayFeatureControl
  DockObserver
  MiuiBackup
  MiuiInit
  SurfaceFlinger
  accessibility
  account
  activity
  alarm
  android.security.keystore
  appops
  appwidget
  assetatlas
  audio
  backup
  battery
  batteryproperties
  batterystats
  bluetooth_manager
  clipboard
  cneservice
  com.qualcomm.qti.auth.fidocryptodaemon
  com.xiaomi.mtservice
  com.xiaomi.vsimservice
  commontime_management
  connectivity
  consumer_ir
  content
  country_detector
  cpuinfo
  dbinfo
  device_policy
  devicestoragemonitor
  diskstats
  display
  display.qservice
  dpmservice
  dreams
  drm.drmManager
  dropbox
  entropy
  ethernet
  fingerprint
  gfxinfo
  hardware
  imms
  ims
  input
  input_method
  iphonesubinfo
  isms
  isub
  jobscheduler
  launcherapps
  location
  locationpolicy
  lock_settings
  media.audio_flinger
  media.audio_policy
  media.camera
  media.player
  media.sound_trigger_hw
  media_projection
  media_router
  media_session
  meminfo
  miui.fdpp
  miui.security.keystore
  miui.shell
  miui.whetstone
  miui.whetstone.klo
  miui.whetstone.mcd
  miui.whetstone.net
  miui.whetstone.power
  mount
  netpolicy
  netstats
  network_management
  network_score
  notification
  package
  permission
  phone
  power
  print
  procstats
  qti.ims.connectionmanagerservice
  restrictions
  rttmanager
  samplingprofiler
  scheduling_policy
  search
  security
  sensorservice
  serial
  servicediscovery
  simphonebook
  sip
  statusbar
  telecom
  telephony.registry
  textservices
  trust
  uimode
  updatelock
  usagestats
  usb
  user
  vendor.qcom.PeripheralManager
  vibrator
  voiceinteraction
  wallpaper
  webviewupdate
  wfdservice
  whetstone.activity
  wifi
  wifip2p
  wifiscanner
  window

然後後面還有很長,緊接着會把每個服務的dump的詳細狀況列舉出來。這裏我們能看到有幾個Miui、com.xiaomi 開頭的,這就是小米公司提供的服務。其他大部分是Android的標準的服務。

爲了方便查看,我們可以傳一個參數,來過濾掉多餘的信息,比如dumpsys meminfo查看各個進程所佔內存的情況。

下面我們就來介紹幾個常用的services,默認是在Android的shell環境下。

  • account:賬號管理
    就是App向AccountManager註冊的賬號,執行dumpsys account,會列舉出綁定的賬號,和各個App註冊的ServiceInfo。
  • devicestoragemonitor存儲狀態
    執行dumpsys devicestoragemonitor,主要是會顯示磁盤的使用狀況
    結果:
Current DeviceStorageMonitor state:
  mFreeMem=8.55 GB mTotalMemory=11.31 GB
  mFreeMemAfterLastCacheClear=8.55 GB
  mLastReportedFreeMem=8.55 GB mLastReportedFreeMemTime=-21m31s397ms
  mLowMemFlag=false mMemFullFlag=false
 mIsBootImageOnDisk=true  mClearSucceeded=false mClearingCache=false
  mMemLowThreshold=500 MB mMemFullThreshold=1.00 MB
  mMemCacheStartTrimThreshold=375 MB mMemCacheTrimToThreshold=750 MB
  • activity:跟Activity相關的組件的狀態
    這個命令支持子命令,通過-h可以看到子參數的列表。
    執行dumpsys activity -h,查看到的參數列表
Activity manager dump options:
  [-a] [-c] [-p package] [-h] [cmd] ...
  cmd may be one of:
    a[ctivities]: activity stack state
    r[recents]: recent activities state
    b[roadcasts] [PACKAGE_NAME] [history [-s]]: broadcast state
    i[ntents] [PACKAGE_NAME]: pending intent state
    p[rocesses] [PACKAGE_NAME]: process state
    o[om]: out of memory management
    prov[iders] [COMP_SPEC ...]: content provider state
    provider [COMP_SPEC]: provider client-side state
    s[ervices] [COMP_SPEC ...]: service state
    as[sociations]: tracked app associations
    service [COMP_SPEC]: service client-side state
    package [PACKAGE_NAME]: all state related to given package
    all: dump all activities
    top: dump the top activity
    write: write all pending state to storage
    track-associations: enable association tracking
    untrack-associations: disable and clear association tracking
  cmd may also be a COMP_SPEC to dump activities.
  COMP_SPEC may be a component name (com.foo/.myApp),
    a partial substring in a component name, a
    hex object identifier.
  -a: include all available server state.
  -c: include client state.
  -p: limit output to given package.

這裏非常值得一提的就是 OOM參數,這裏是指被踢出內存的優先級,也就是我們常說的進程優先級,值越小,就優先級越高,越不容易被殺,可以出現負數,系統App一般纔會爲負數,一般App最高爲0。
當然你還可以直接查看進程的優先級oom_adj的值
//查看進程的優先級

cat /proc/${pid}/oom_adj
  • battery:電池狀態
    執行dumpsys battery
    結果:
Current Battery Service state:
  AC powered: false
  USB powered: true
  Wireless powered: false
  status: 5
  health: 2
  present: true
  level: 100
  scale: 100
  voltage: 4376
  temperature: 272
  technology: Li-poly
  • batterystats:各應用耗電量信息
    支持子命令的,執行dumpsys batterystats -h查看參數列表
Battery stats (batterystats) dump options:
  [--checkin] [--history] [--history-start] [--unplugged] [--charged] [-c]
  [--reset] [--write] [-h] [<package.name>]
  --checkin: format output for a checkin report.
  --history: show only history data.
  --history-start <num>: show only history data starting at given time offset.
  --unplugged: only output data since last unplugged.
  --charged: only output data since last charged.
  --reset: reset the stats, clearing all current data.
  --write: force write current collected stats to disk.
  <package.name>: optional name of package to filter output by.
  -h: print this help text.
Battery stats (batterystats) commands:
  enable|disable <option>
    Enable or disable a running option.  Option state is not saved across boots.
   
   - Options are:
      full-history: include additional detailed events in battery history:
          wake_lock_in and proc events
      no-auto-reset: don't automatically reset stats when unplugged
  • alarm:Alarm的喚醒隊列信息
    查看放到定時隊列中的事件
    有些時候adb dumpsys alarm命令輸出再cmd窗口顯示不全,可以通過 以下命令輸出到電腦對應文件中查看
 adb shell dumpsys alarm > f:/alarm.txt
  • cpuinfo:各進程使用CPU信息
    查看一段時間段內各個進程使用CPU的信息,包括user、kernel區分
  • dbinfo:各進程使用SQLite數據的信息
    查看各個進程使用數據庫的狀況,sql的執行時間等
  • diskstats:磁盤相關信息
    執行dumpsys diskstats
    結果:
Latency: 1ms [512B Data Write]
Data-Free: 8963580K / 11855028K total = 75% free
Cache-Free: 371304K / 380888K total = 97% free
System-Free: 299116K / 1889440K total = 15% free
  • dropbox:各進程的dropbox的日誌信息

  • statusbar:狀態欄相關的信息

  • usagestats:每個界面啓動的時間

  • meminfo:各進程的內存使用狀況
    執行dumpsys meminfo查看的每個進程佔用PSS的列表
    如果後面接pid或者包名就可以查看具體某個進程的內存詳細狀況
    執行dumpsys meminfo com.eg.android.AlipayGphone
    結果:
    支付寶的內存詳情

  • package : 查看安裝的信息
    主要就是獲取AndroidManifest.xml裏邊的信息

  • window:鍵盤,窗口和它們的關係

  • wifi:WIFI的信息
    但是這個log太多了,看不明白

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章