forked from moyu6027/Android-monitor-shell-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.sh
576 lines (554 loc) · 22.3 KB
/
monitor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#!/system/bin/sh
if [ -f /data/local/tmp/busybox ];then
export bb="/data/local/tmp/busybox"
else
echo "No /data/local/tmp/busybox"
exit
fi
#root
root=`id|$bb grep -c root`
get_package(){
if [ ! -z "$packages" ];then
local grep_str=`echo $packages|$bb sed 's/\./\\\\./g;s/\[/\\\[/g;s/\]/\\\]/g'`
local tmp=`$bb ps -o pid,args`
local check=`echo "$tmp"|$bb sed 's/{.*.} //g'|$bb grep -E "$grep_str"`
if [ ! -z "$check" ];then
local tmp=`$bb ps -T`
local check1=`echo "$tmp"|$bb grep -E -c "$grep_str"`
if [ $threads -lt $check1 ];then
threads=$check1
echo "$tmp"|$bb grep -E "$grep_str" >$monitor/threads/$loop"_"$check1.log
fi
local i=0
echo "$packages"|$bb sed 's/|/\n/g'|while read p;do
local Ts=`$bb ps -T|$bb awk -v package="$p" '$NF==package{r+=1}END{print r}'`
local FD=-1
echo "$check"|$bb awk -v package="$p" '{if($2==package){t+=1;D[$1" "$2]=t}}END{for(i in D)print i" "D[i]}'|while read l;do
local pid=`echo $l|$bb awk '{print $1}'`
if [ -f /proc/$pid/cmdline ];then
if [ $root -eq 1 -a ! -z "$pid" ];then
local FD=`ls -l /proc/$pid/exe /proc/$pid/root /proc/$pid/fd/ 2>/dev/null|$bb awk 'END{print NR}'`
if [ $i -eq 0 ];then
local i=1
if [ -f $monitor/FDs/maxFDS.log ];then
local FDs=`$bb awk -F, -v p="$p" '$1==p{r=$2}END{printf r+0}' $monitor/FDs/maxFDS.log`
else
local FDs=0
fi
if [ $FDs -lt $FD ];then
if [ $FDs -eq 0 ];then
echo "$p,$FD,$pid" >>$monitor/FDs/maxFDS.log
else
local check=`cat $monitor/FDs/maxFDS.log`
echo "$check"|$bb awk -F, -v p="$p" -v r="$p,$FD,$pid" '{if($1==p)print r;else print $0}' >$monitor/FDs/maxFDS.log
fi
ls -l /proc/$pid/fd/ 1>$monitor/FDs/$pid"_"$loop"_"$FD.log 2>/dev/null
fi
fi
fi
local arg=`$bb awk 'BEGIN{r="null"}{if(NR>1){if(NR==2)r=$0;else r=r" "$0}}END{print r}' /proc/$pid/cmdline`
if [ -z "`echo $arg|$bb tr -d " "`" ];then
local arg="null"
fi
command=`echo $l|$bb awk '{print $2}'`
if [ -d /proc/$pid ];then
local pss=`dumpsys meminfo $pid|$bb awk -v pid=$pid -v comm=$command -v time=$uptime '{if($1=="**")comm=substr($6,2,length($6)-2);if($1=="Native"){n=n+$(NF-2);o=o+$(NF-1);p=p+$NF}else{if($1=="Dalvik"&&NF>6){d=d+$(NF-2);e=e+$(NF-1);f=f+$NF;if($2=="Heap"){g=g+$(NF-6)}else{g=g+$2}}else{if($1=="TOTAL"){s=$2+0}else{if($1=="Views:"){print time","comm","s","pid","n+0","o+0","p+0","d+0","e+0","f+0","g+0","$2+0}else{if($3=="Views:"){print time","comm","s","pid","n+0","o+0","p+0","d+0","e+0","f+0","g+0","$4+0}}}}}}'`
if [ ! -z "$pss" -a ! -z $Ts ];then
if [ $root -eq 1 ];then
echo "$pss,$Ts,$FD,\"$arg\""
else
echo "$pss,$Ts,\"$arg\""
fi
fi
fi
fi
done
done
fi
fi
}
get_meminfo(){
if [ ! -f $monitor/mem2.csv ];then
echo uptime,$mem2 >$monitor/mem2.csv
fi
dumpsys meminfo |$bb awk -v type=$awk -v time=$uptime -v packages="busybox|$packages" -v mem="$mem2" -v OFS=, -v csv=$monitor/mem2.csv -v csv1=$monitor/meminfo.csv 'BEGIN{ \
l=split(mem,O,","); \
mem="" \
} \
{ \
if($0=="")state=0; \
gsub(/\(|\)|Total PSS by |,/,"",$0); \
gsub(/ Services/,"_Services",$0); \
gsub(/K: /," kB: ",$0); \
if(state==1){ \
if($3"|"!~packages){ \
R=time","$3","$1","$5; \
if(type==1){ \
l=split($3,Check,"."); \
if(l==1){ \
cmd="cat /proc/"$5"/cmdline";Args=""; \
while(cmd|getline){if($0!=""){if(Args=="")Args=$0; else Args=Args" "$0}} \
}
}else{ \
Args="null" \
}; \
if(Args=="")Args="null"; \
print R",,,,,,,,,,\""Args"\"" >>csv1 \
} \
}else{ \
if(state==2){ \
C=$3; \
if(NF>3){ \
for(i=4;i<=NF;i++)C=C"_"$i \
}; \
D[C]=$1 \
} \
}; \
if($2=="RAM:"){ \
if($1=="Total"){ \
D["Total_RAM"]=substr($3,1,length($3)-1) \
}else{ \
if($1=="Free"){ \
D["Free_RAM"]=substr($3,1,length($3)-1); \
D["Free_cached_pss"]=substr($4,1,length($4)-1); \
D["Free_cached_kernel"]=substr($8,1,length($8)-1); \
D["free"]=substr($12,1,length($12)-1) \
}else{ \
if($1=="Used"){ \
D["Used_RAM"]=substr($3,1,length($3)-1); \
D["used_pss"]=substr($4,1,length($4)-1); \
D["used_kernel"]=substr($8,1,length($8)-1) \
}else{ \
if($1=="Lost")D["Lost_RAM"]=substr($3,1,length($3)-1) \
} \
} \
} \
}; \
if($1=="ZRAM:"){ \
D["swap_physical_used"]=substr($2,1,length($2)-1); \
D["swap_for"]=substr($6,1,length($6)-1); \
D["swap_total"]=substr($9,1,length($9)-1) \
}; \
if($1=="process:"){state=1}else{ \
if($1=="category:")state=2 \
} \
}END{ \
R=D[O[1]]; \
for(i=2;i<=l;i++)R=R","D[O[i]]; \
print time,R >>csv \
}'
}
getmem(){
local info=`$bb awk 'BEGIN{r=0}{if($1=="MemFree:"){a=$2};if($1=="Buffers:"){b=$2};if($1=="Cached:"){c=$2};if($1=="Active:"){e=$2};if($1=="Inactive:"){f=$2};if($1=="Active(anon):"){g=$2};if($1=="Inactive(anon):"){h=$2};if($1=="Active(file):"){i=$2};if($1=="Inactive(file):"){j=$2};if($1=="Dirty:"){k=$2};if($1=="Writeback:"){l=$2};if($1=="Mapped:"){m=$2};if($1=="Slab:"){n=$2};if($1=="CMA"&&$2=="Free:"){r=1;a=a-$3;o=$3}}END{if(r==0) print a","b","c","e","f","g","h","i","j","k","l","m","n;else print a","b","c","e","f","g","h","i","j","k","l","m","n","o}' /proc/meminfo`
if [ ! -z "$info" ];then
echo "$uptime,$info" >>$monitor/mem.csv
fi
}
getvss(){
$bb ps -o pid,ppid,vsz,rss,args|$bb sed 's/{.*.} //g'|$bb awk -v OFS="," -v time=$uptime -v csv="$monitor/meminfo2.csv" 'NR>1&&$5!="/data/local/tmp/busybox"{if(substr($3,length($3),1)=="m"){V=substr($3,1,length($3)-1)}else{if(substr($3,length($3),1)=="g")V=substr($3,1,length($3)-1)*1024;else V=sprintf("%.3f",$3/1024)+0};if(substr($4,length($4),1)=="m"){R=substr($4,1,length($4)-1)}else{if(substr($4,length($4),1)=="g")R=substr($4,1,length($4)-1)*1024;else R=sprintf("%.3f",$4/1024)+0};C=$5;A="";if(NF>=6)A=$6;if(NF>6){for(i=7;i<=NF;i++)A=A" "$i};if(A=="")A="null";if(V>0)print time,$1,$2,V,R,C,"\""A"\"">>csv}'
}
getcpu(){
local tmp=`$bb top -b -n 1|$bb grep -E -v "busybox|Shutdown thread"|$bb sed '2s/%//g;s/\\.0 / /g;s/ S N / SN /g;s/ R N / RN /g;s/ D N / DN /g;s/ Z N / ZN /g;s/ T N / TN /g;s/S </S</g;s/D </D</g;s/R </R</g;s/Z </Z</g;s/T </T</g;s/t </t</g;s/[0-9]m[0-9]/m /g;s/}/{/g;s/ th\\]/_th\\]/g;s/\\[mtk /\\[mtk_/g;s/Net Work Manage/Net-Work-Manage/g' 2>/dev/null|$bb awk -v time="$uptime" -v csv="$monitor/cpu.csv" -v csv2="$monitor/check.log" -v cpu=$1 -v T="{" -v T2="\"" '{if(NR==2) print time","$2","$4","$6","$8","$10","$12","$14 >>csv;if(NR>4){r="";if($cpu=="0") exit;if($cpu+0==0){if($(cpu-1)/1000>0){a=sprintf("%.0f",$(cpu-1)/1000)}else{a=sprintf("%.1f",$(cpu-1))}c=substr($(cpu-1),length(a)+1);r=$cpu;if(NF>cpu);r=r T$(cpu+1)}else{if($(cpu+1)+0!=0){print $0 >>csv2}else{c=$cpu;r=$(cpu+1)}};if($(cpu+1)+0==0){if(NF>cpu+1){r=r T$(cpu+2);if(NF>cpu+2)r=r T$(cpu+3);if(NF>cpu+3)for(i=(cpu+4);i<=NF;i++)r=r" "$i};print time","$1","c","r}}}'`
echo "$tmp"|$bb awk -F"{" -v S="}" -v T="\"" '{if(NF==1)print $0",,";if(NF==2)print $1","T$2T",";if(NF==3)print $1","T$2" "$3T",";if(NF==4&&$3=="")print $1$4",,"T$2T;if(NF==4&&$3!="")print $1","T$2" "$3S$4T",";if(NF>4&&$3==""){a=$5;if(NF>5)for(i=6;i<=NF;i++)a=a S $i;print $1$4","T a T","T$2T};if(NF>4&&$3!=""){a=$4;if(NF>4)for(i=5;i<=NF;i++)a=a S $i;print $1","T$2" "$3" "a T","}}' >>$monitor/cpuinfo.csv
}
getFPS(){
# shellcheck disable=SC2039
local KPI=100
echo "FU(s),LU(s),Date:$1,FPS:$2,Frames,jank,jank2,MFS(ms),OKT:$KPI,SS(%),WN" >$monitor/fps_window.csv
echo "FU(s),LU(s),Date:$1,FPS:$2,Frames,jank,jank2,MFS(ms),OKT:$KPI,SS(%),WN" >$monitor/fps_system.csv
dumpsys SurfaceFlinger --latency-clear
while true;do
if [ -f /data/local/tmp/stop ];then
break
fi
$bb usleep 1600000
# shellcheck disable=SC2039
# shellcheck disable=SC2155
local uptime=`echo $EPOCHREALTIME|$bb awk -F. '{print strftime("%F %T",$1+8*3600)"."substr($2,1,3)}'`
# shellcheck disable=SC2155
# shellcheck disable=SC2039
# shellcheck disable=SC2034
local app_pid=`ps -ef | grep "$3"$ | $bb awk '{print $2}'`
local window=`echo "$1"|sed "s#\[.*\]#[${app_pid}]#g"`
# echo $window
dumpsys SurfaceFlinger --latency "$window"|$bb awk -v T="$uptime" -v target=$2 -v kpi=$KPI '{if(NR==1){r=$1/1000000;if(r<0)r=$1/1000;b=0;n=0;w=1}else{if(n>0&&$0=="")O=1;if(NF==3&&$2!=0&&$2!=9223372036854775807){x=($3-$1)/1000000/r;if(b==0){b=$2;n=1;d=0;D=0;if(x<=1)C=r;if(x>1){d+=1;C=int(x)*r;if(x%1>0)C+=r};if(x>2)D+=1;m=r;o=0}else{c=($2-b)/1000000;if(c>500){O=1}else{n+=1;if(c>=r){C+=c;if(c>kpi)o+=1;if(c>=m)m=c;if(x>1)d+=1;if(x>2)D+=1;b=$2}else{C+=r;b=sprintf("%.0f",b+r*1000000)}}};if(n==1)s=sprintf("%.3f",$2/1000000000)};if(n>0&&O==1){O=0;if(n==1)t=sprintf("%.3f",s+C/1000);else t=sprintf("%.3f",b/1000000000);f=sprintf("%.2f",n*1000/C);m=sprintf("%.0f",m);g=f/target;if(g>1)g=1;h=kpi/m;if(h>1)h=1;e=sprintf("%.2f",g*60+h*20+(1-o/n)*20);print s","t","T","f+0","n","d","D","m","o","e","w;n=0;if($0==""){b=0;w+=1}else{b=$2;n=1;d=0;D=0;if(x<=1)C=r;if(x>1){d+=1;C=int(x)*r;if(x%1>0)C+=r};if(x>2)D+=1;m=r;o=0}}}}' >>$monitor/fps_window.csv
dumpsys SurfaceFlinger --latency |$bb awk -v T="$uptime" -v target=$2 -v kpi=$KPI '{if(NR==1){r=$1/1000000;if(r<0)r=$1/1000;b=0;n=0;w=1}else{if(n>0&&$0=="")O=1;if(NF==3&&$2!=0&&$2!=9223372036854775807){x=($3-$1)/1000000/r;if(b==0){b=$2;n=1;d=0;D=0;if(x<=1)C=r;if(x>1){d+=1;C=int(x)*r;if(x%1>0)C+=r};if(x>2)D+=1;m=r;o=0}else{c=($2-b)/1000000;if(c>500){O=1}else{n+=1;if(c>=r){C+=c;if(c>kpi)o+=1;if(c>=m)m=c;if(x>1)d+=1;if(x>2)D+=1;b=$2}else{C+=r;b=sprintf("%.0f",b+r*1000000)}}};if(n==1)s=sprintf("%.3f",$2/1000000000)};if(n>0&&O==1){O=0;if(n==1)t=sprintf("%.3f",s+C/1000);else t=sprintf("%.3f",b/1000000000);f=sprintf("%.2f",n*1000/C);m=sprintf("%.0f",m);g=f/target;if(g>1)g=1;h=kpi/m;if(h>1)h=1;e=sprintf("%.2f",g*60+h*20+(1-o/n)*20);print s","t","T","f+0","n","d","D","m","o","e","w;n=0;if($0==""){b=0;w+=1}else{b=$2;n=1;d=0;D=0;if(x<=1)C=r;if(x>1){d+=1;C=int(x)*r;if(x%1>0)C+=r};if(x>2)D+=1;m=r;o=0}}}}' >>$monitor/fps_system.csv
done
}
getGfxInfo(){
dumpsys gfxinfo "$1" reset &
while true;do
if [ -f /data/local/tmp/stop ];then
dumpsys gfxinfo "$1" | sed -n '6,19'p >>$monitor/fps_gfxinfo.txt
# dumpsys gfxinfo "$1" framestats|$bb awk '/^Flags/,/---PROFILEDATA---$/{print $0}' >>$monitor/fps_gfxinfo.csv
wait
break
fi
done
}
GFX(){
#如果systrace打开则关闭
# shellcheck disable=SC2046
if [ `cat /sys/kernel/debug/tracing/tracing_on` -eq 1 ];then
echo "atrace gfx stop"
atrace gfx --async_stop 1>/dev/null &
$bb sleep 3
kill $!
fi
#vsync 间隔获取
# shellcheck disable=SC2155
# shellcheck disable=SC2039
local sync=`dumpsys SurfaceFlinger --latency|$bb awk 'NR==1{r=$1/1000000;if(r<0)r=$1/1000;print r}'`
#当前选中的activity
# shellcheck disable=SC2155
# shellcheck disable=SC2039
local hasFocus=`dumpsys input|grep "hasFocus=true"|$bb awk '{print substr($4,1,length($4)-3)}'`
#提取当前显示activity的app
# shellcheck disable=SC2039
# shellcheck disable=SC2155
local app=`echo $hasFocus|$bb awk -F "/" '{print $1}'`
#开始输出systrace log: 1M 循环buffer 的gfx信息
echo "atrace gfx -b 1024 -c --async_start"
atrace gfx -b 1024 -c --async_start &
#awk解析
$bb awk -F "|" \
-v sync="$sync" \
-v OFS=, \
-v app="$app" \
-v activity="$hasFocus" \
-v target="$1" \
-v kpi="$2" \
-v type="$3" \
-v delay="$4" \
-v frames="$5" \
-v csv=$monitor/fps.csv \
-v csv1=$monitor/dropFrames.csv \
'BEGIN{ \
while("cat /sys/kernel/debug/tracing/trace_pipe"|getline){ \
if(NF==4||$3=="postFramebuffer"){ \
if($3~/VSYNC-sf|VSYNC-app|VSYNC-sf-app/){ \
gsub(/.*.) |: tracing_mark_write: C/,"",$1); \
split($1,T," "); \
if(f==""){ \
f=T[3]; \
logF=1; \
logD=0; \
if(type==1){ \
logD=1 \
}else{ \
if(type==2){ \
logD=2; \
}else{ \
if(type==3){ \
logF=0; \
logD=1 \
}else{ \
if(type==4){ \
logF=0; \
logD=2 \
} \
} \
} \
} \
}else{ \
if(post==1){ \
if(sv!=""){ \
SV=sv \
}else{ \
SV=activity \
}; \
p=TX","app",\""SV"\""; \
t=(T[3]-VSYNC)*1000; \
if(t>sync){ \
d=1 \
}else{ \
d=0 \
}; \
wt=(T[3]-V[p])*1000; \
if(wt<500){ \
if(d==1){ \
if(logD==2){ \
print VSYNC,T[3],t,$3,p,"\""info"\"">csv1 \
}else{ \
if(logD==1){ \
if(wt>=42)print V[p],T[3],wt,$3,p,"\""info"\"">csv1 \
} \
} \
}; \
if(logF==1){ \
N[p]=N[p]+1; \
Time[p]=Time[p]+wt; \
if(M[p]==""){ \
M[p]=wt \
}else{ \
if(M[p]<wt)M[p]=wt \
}; \
if(wt>=100){ \
A[p]=A[p]+1 \
}else{ \
if(wt>=50){ \
B[p]=B[p]+1 \
}else{ \
if(wt>=42)C[p]=C[p]+1 \
} \
} \
} \
}else{ \
if(logF==1&&b[p]!=""){ \
Stop[p]=Stop[p]+1; \
StopT[p]=StopT[p]+wt \
} \
}; \
if(logF==1){ \
if(d==1)D[p]=D[p]+1; \
if(b[p]=="")b[p]=T[3]; \
V[p]=T[3]; \
if(T[3]-f>=delay){ \
if(length(N)>0){ \
for(i in N){ \
if(N[i]>frames){ \
fps=sprintf("%.1f",N[i]*1000/Time[i]); \
if(fps>=target){ \
fps=int(fps); \
g=1 \
}else{ \
g=fps/target \
}; \
if(kpi<M[i])h=kpi/M[i];else h=1; \
ss=sprintf("%.2f",g*60+h*20+(1-A[i]/N[i])*10+(1-B[i]/N[i])*7+(1-C[i]/N[i])*3); \
print b[i],V[i],fps+0,N[i],sprintf("%.3f",Time[i]/1000)+0,M[i],sprintf("%.3f",StopT[i]/1000)+0,Stop[i]+0,A[i]+0,B[i]+0,C[i]+0,D[i]+0,ss+0,i>>csv; \
b[i]=V[i]; \
N[i]=""; \
Time[i]=""; \
M[i]=""; \
StopT[i]=""; \
Stop[i]=""; \
A[i]=""; \
B[i]=""; \
C[i]=""; \
D[i]="" \
} \
} \
}; \
f=T[3]; \
} \
} \
};
}; \
VSYNC=T[3];
state=1; \
TX=0; \
post=0; \
sv=""; \
info="" \
}else{ \
if(NF==3){ \
state=0; \
post=1 \
}else{ \
if(state==1&&$3!~/HW_VSYNC_ON_0|HW_VSYNC_0|hasClientComposition|FrameMissed|FramebufferSurface/){ \
if(logD>0){ \
if(info="")info=$3;else info=info"\n"$3 \
}; \
if($3~/TX - /){ \
TX=1 \
}else{ \
l=split($3,Check,"."); \
if(l>2){ \
l=split($3,tmp,"/"); \
if(l>1){ \
sv=$3; \
l=split($3,Check," "); \
if(l==1){ \
app=tmp[1]; \
activity=$3 \
} \
}else{ \
sv=$3 \
} \
}else{ \
sv=$3 \
} \
} \
} \
} \
} \
} \
}; \
if(length(N)>0){ \
for(i in N){ \
if(N[i]>0){ \
fps=sprintf("%.1f",N[i]*1000/Time[i]); \
if(fps>=target){ \
fps=int(fps); \
g=1 \
}else{ \
g=fps/target \
}; \
if(kpi<M[i])h=kpi/M[i];else h=1; \
ss=sprintf("%.2f",g*60+h*20+(1-A[i]/N[i])*10+(1-B[i]/N[i])*7+(1-C[i]/N[i])*3); \
print b[i],V[i],fps+0,N[i],sprintf("%.3f",Time[i]/1000)+0,M[i],sprintf("%.3f",StopT[i]/1000)+0,Stop[i]+0,A[i]+0,B[i]+0,C[i]+0,D[i]+0,ss+0,i>>csv \
} \
} \
} \
}' &
#获取cat进程pid
#local catPid=`$bb ps|$bb awk '$4=="cat"&&$5=="/sys/kernel/debug/tracing/trace_pipe"{print $1}'`
#echo "cat Pid="$catPid
#等待 cat /sys/kernel/debug/tracing/trace_pipe进程退出,后子进程退出
#wait
}
#main
${testresult="/data/local/tmp/"} 2>/dev/null
monitor="$testresult/$1"
if [ -d $monitor ];then
$bb rm -r $monitor
fi
mkdir -p $monitor/threads
threads=0
if [ -f /data/local/tmp/stop ];then
$bb rm /data/local/tmp/stop
fi
packages=$3
cpu_p=`$bb top -b -n 1|$bb awk 'NR==4{print NF-1}'`
loop=0
if [ -f /sys/class/kgsl/kgsl-3d0/devfreq/cur_freq -a -f /sys/class/power_supply/battery/current_now ];then
cpu_type=0
echo "uptime,gpu_freq,charge_current" >$monitor/others.csv
elif [ -f /sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu/load ];then
cpu_type=2
echo "uptime,gpu_freq" >$monitor/others.csv
else
cpu_type=1
fi
#info
$bb cat /proc/cpuinfo >$monitor/cpuinfo.txt
HW=`$bb awk -F":" 'substr($1,1,8)=="Hardware"{print substr($2,2)}' /proc/cpuinfo|$bb tr -d ,`
echo PID=$$ >$monitor/info.log
echo cpu_type=$cpu_type >>$monitor/info.log
echo sleep=$4 >>$monitor/info.log
echo Hardware=$HW >>$monitor/info.log
echo build=`getprop ro.build.fingerprint` >>$monitor/info.log
echo time=`echo $EPOCHREALTIME|$bb awk -F. '{print strftime("%F %T",$1+8*3600)"."$2}'` >>$monitor/info.log
echo uptime=`$bb awk -F. '{print $1}' /proc/uptime` >>$monitor/info.log
if [ $cpu_type -eq 0 ];then
echo charge_full=`$bb awk '{print $0/1000}' /sys/class/power_supply/bms/charge_full` >>$monitor/info.log
fi
#csv
echo "uptime,usr,sys,nic,idle,io,irq,sirq" >$monitor/cpu.csv
echo "uptime,PID,%CPU,Command,args,Thread" >$monitor/cpuinfo.csv
if [ `$bb grep -c "CMA Free" /proc/meminfo` -eq 0 ];then
echo "uptime:$4,MemFree,Buffers,Cached,Active,Inactive,Active(anon),Inactive(anon),Active(file),Inactive(file),Dirty,Writeback,Mapped,Slab" >$monitor/mem.csv
else
echo "uptime:$4,MemFree,Buffers,Cached,Active,Inactive,Active(anon),Inactive(anon),Active(file),Inactive(file),Dirty,Writeback,Mapped,Slab,CMA Free" >$monitor/mem.csv
fi
echo "uptime,BatteryLevel,PlugType" >$monitor/btm.csv
if [ $root -eq 1 ];then
mkdir -p $monitor/FDs
echo "uptime,Process_Name,Pss,PID,Native_Heap(Size),Native_Heap(Alloc),Native_Heap(Free),Dalvik_Heap(Size),Dalvik_Heap(Alloc),Dalvik_Heap(Free),Dalvik_Pss,Views,Threads,FD,Args" >$monitor/meminfo.csv
else
echo "uptime,Process_Name,Pss,PID,Native_Heap(Size),Native_Heap(Alloc),Native_Heap(Free),Dalvik_Heap(Size),Dalvik_Heap(Alloc),Dalvik_Heap(Free),Dalvik_Pss,Views,Threads,Args" >$monitor/meminfo.csv
fi
echo "uptime,PID,PPID,VSZ,RSS,COMMAND,Args" >$monitor/meminfo2.csv
echo "Loop:$4,uptime,Date_Time,DisplayID,FocusedWindow,FocusedApplication,Flags,Type,Pid,uid,frame" >$monitor/windows.csv
CPUS=`$bb awk -v csv="$monitor/cur_freq.csv" '{if(FNR==1)cpu+=1}END{R="uptime,0:"cpu;for(i=1;i<cpu;i++)R=R","i;print R>csv;print cpu}' /sys/devices/system/cpu/cpu*/uevent`
mem2=`dumpsys meminfo |$bb awk '{if($0=="")state=0;gsub(/K: /," kB: ",$0);if(state==2){C=$3;if(NF>3){for(i=4;i<=NF;i++)C=C"_"$i;if(R=="")R=C;else R=R","C}};if($2=="RAM:"){if($1=="Total")R=R",Total_RAM";else {if($1=="Free")R=R",Free_RAM,Free_cached_pss,Free_cached_kernel,free";else {if($1=="Used")R=R",Used_RAM,used_pss,used_kernel";else {if($1=="Lost")R=R",Lost_RAM"}}}}if($1=="ZRAM:"){R=R",swap_physical_used,swap_for,swap_total"};if($NF=="category:")state=2}END{print R}'`
#检查awk外部调用命令是否正常
if [ -z "`/data/local/tmp/busybox awk 'BEGIN{system("ps")}'`" ];then
awk=0
else
awk=1
fi
thermal_path=""
# shellcheck disable=SC2045
for i in `ls /sys/devices/virtual/thermal/thermal_zone*/type`;do
zone=`echo $i|$bb awk '{split($0,N,"/");print N[6]}'`
check=`cat $i 2>/dev/null`
if [ ! -z "$check" ];then
title=$zone"("$check")"
else
title=$zone"(unknown)"
fi
check=`cat /sys/devices/virtual/thermal/$zone/temp 2>/dev/null`
if [ ! -z "$check" ];then
if [ -z "$thermal_path" ];then
thermal_path=/sys/devices/virtual/thermal/$zone/temp
thermal_type="uptime,"$title
thermal_zones=$zone
else
thermal_path=$thermal_path" "/sys/devices/virtual/thermal/$zone/temp
thermal_type=$thermal_type","$title
thermal_zones=$thermal_zones" "$zone
fi
fi
done
thermal_type=`echo $thermal_type|$bb sed 's/thermal_zone//g'`
echo $thermal_type >$monitor/thermal.csv
unset zone
unset check
unset title
unset thermal_type
echo "start time,end time,FPS,frames,Time(S),max time(ms),waiting time(S),wait times,A,B,C,D,score,TX,app,Surface" >$monitor/fps.csv
#FPS
if [ ! -z "$2" ];then
getFPS "$2" 60 "$3" &
fi
#FPS2-gfxinfo
if [ ! -z "$2" ];then
getGfxInfo "$3" &
fi
target=60
KPI=100
type=0
delay=1
frames=20
GFX $target $KPI $type $delay $frames
while true;do
date_time="`echo $EPOCHREALTIME|$bb awk -F. '{print strftime("%F %T",$1+8*3600)"."substr($2,1,3)}'`"
uptime=`$bb awk '{print $1}' /proc/uptime`
#cpu freq
$bb awk -v cpus=$CPUS -v time=$uptime -v csv="$monitor/cur_freq.csv" '{split(FILENAME,N,"/");r[substr(N[6],4)]=$0/1000}END{R=r[0];for(i=1;i<cpus;i++)R=R","r[i];print time","R >>csv}' /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq
#thermal_zone
$bb awk -v time=$uptime -v TZs="$thermal_zones" -v csv="$monitor/thermal.csv" 'BEGIN{split(TZs,tmp," ")}{split(FILENAME,N,"/");r[N[6]]=$0}END{for(i in tmp){if(R=="")R=r[tmp[i]]+0;else R=R","r[tmp[i]]+0};print time","R >>csv}' $thermal_path
#windows.csv
dumpsys input|$bb grep " name="|$bb awk -v OFS="," -v loop=$loop -v time=$uptime -v date="$date_time" -v csv="$monitor/windows.csv" '{if($1=="FocusedApplication:"){A=$6}else{W=substr($4,1,length($4)-3);if($9=="visible=true,"){D=substr($5,11,length($5)-11);if(D>0)D=1;for(i=11;i<=NF;i++){if(substr($i,1,5)=="flags")F=substr($i,7,length($i)-7);if(substr($i,1,4)=="type")T=substr($i,6,length($12)-6);if(substr($i,1,5)=="frame"){S=substr($i,7,length($i)-7);gsub("\]","",S);gsub("\\[",",",S)};if(substr($i,1,8)=="ownerPid")P=substr($i,10,length($i)-10);if(substr($i,1,8)=="ownerUid")U=substr($i,10,length($i)-10)};if($7=="hasFocus=true,"){if(match(W,"/")){split(W,A,"/");gsub(A[1],"",A[2]);A=A[1]"/"A[2]};print loop,time,date,D,W,A,F,T,P,U,"\""S"\"" >>csv}}}}'
#btm.csv
dumpsys power|$bb grep -E "mBatteryLevel=|mPlugType"|$bb awk -F "=" -v OFS="," -v time=$uptime -v csv="$monitor/btm.csv" '{if(NR==1)a=$2;else{print time,$2,a >>csv;exit}}'
#others
if [ $cpu_type -eq 0 ];then
$bb awk -v time=$uptime -v csv="$monitor/others.csv" '{if(R=="")R=$0/1000000;else R=R","$0}END{print time","R >>csv}' /sys/class/kgsl/kgsl-3d0/devfreq/cur_freq /sys/class/power_supply/battery/current_now
elif [ $cpu_type -eq 2 ];then
$bb awk -v time=$uptime -v csv="$monitor/others.csv" '{print time","substr($1,3,length($1)-4)/1000000 >>csv}' /sys/devices/platform/ff9a0000.gpu/devfreq/ff9a0000.gpu/load
fi
#cpu
getcpu $cpu_p
getmem
getvss
if [ $5 -ge 1 ];then
get_meminfo
fi
get_package $5 >>$monitor/meminfo.csv
#stop
loop=$((loop+1))
if [ -f /data/local/tmp/stop ];then
echo "Found stop file!!!"
wait
break
elif [ `$bb df /data|$bb awk '{r=substr($(NF-1),1,length($(NF-1))-1)}END{print r+0}'` -ge 90 ];then
echo "The free space of data less 10%,stop!!!"
wait
break
fi
if [ $4 -gt 0 ];then
$bb sleep $4
fi
done
#停止systrace
if [ `cat /sys/kernel/debug/tracing/tracing_on` -eq 1 ];then
echo "finish: atrace gfx stop"
atrace gfx --async_stop 1>/dev/null &
$bb sleep 3
kill $!
# shellcheck disable=SC2105
break
fi