我们都知道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的唤醒队列信息
查看放到定时队列中的事件 - 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太多了,看不明白