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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.internal.os;
import static com.android.internal.os.BinderLatencyProto.Dims.SYSTEM_SERVER;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.IntArray;
import android.util.KeyValueListParser;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.BinderInternal.CallSession;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.ToDoubleFunction;
/**
* Collects statistics about CPU time spent per binder call across multiple dimensions, e.g.
* per thread, uid or call description.
*/
public class BinderCallsStats implements BinderInternal.Observer {
public static final boolean ENABLED_DEFAULT = true;
public static final boolean DETAILED_TRACKING_DEFAULT = true;
public static final int PERIODIC_SAMPLING_INTERVAL_DEFAULT = 1000;
public static final boolean DEFAULT_TRACK_SCREEN_INTERACTIVE = false;
public static final boolean DEFAULT_TRACK_DIRECT_CALLING_UID = true;
public static final boolean DEFAULT_IGNORE_BATTERY_STATUS = false;
public static final boolean DEFAULT_COLLECT_LATENCY_DATA = true;
public static final int MAX_BINDER_CALL_STATS_COUNT_DEFAULT = 1500;
public static final int SHARDING_MODULO_DEFAULT = 1;
private static final String DEBUG_ENTRY_PREFIX = "__DEBUG_";
private static class OverflowBinder extends Binder {}
private static final String TAG = "BinderCallsStats";
private static final int CALL_SESSIONS_POOL_SIZE = 100;
private static final int MAX_EXCEPTION_COUNT_SIZE = 50;
private static final String EXCEPTION_COUNT_OVERFLOW_NAME = "overflow";
// Default values for overflow entry. The work source uid does not use a default value in order
// to have on overflow entry per work source uid.
private static final Class<? extends Binder> OVERFLOW_BINDER = OverflowBinder.class;
private static final boolean OVERFLOW_SCREEN_INTERACTIVE = false;
private static final int OVERFLOW_DIRECT_CALLING_UID = -1;
private static final int OVERFLOW_TRANSACTION_CODE = -1;
// Whether to collect all the data: cpu + exceptions + reply/request sizes.
private boolean mDetailedTracking = DETAILED_TRACKING_DEFAULT;
// If set to true, indicates that all transactions for specific UIDs are being
// recorded, ignoring sampling. The UidEntry.recordAllTransactions flag is also set
// for the UIDs being tracked.
private boolean mRecordingAllTransactionsForUid;
// Sampling period to control how often to track CPU usage. 1 means all calls, 100 means ~1 out
// of 100 requests.
private int mPeriodicSamplingInterval = PERIODIC_SAMPLING_INTERVAL_DEFAULT;
private int mMaxBinderCallStatsCount = MAX_BINDER_CALL_STATS_COUNT_DEFAULT;
@GuardedBy("mLock")
private final SparseArray<UidEntry> mUidEntries = new SparseArray<>();
@GuardedBy("mLock")
private final ArrayMap<String, Integer> mExceptionCounts = new ArrayMap<>();
private final Queue<CallSession> mCallSessionsPool = new ConcurrentLinkedQueue<>();
private final Object mLock = new Object();
private final Random mRandom;
private long mStartCurrentTime = System.currentTimeMillis();
private long mStartElapsedTime = SystemClock.elapsedRealtime();
private long mCallStatsCount = 0;
private boolean mAddDebugEntries = false;
private boolean mTrackDirectCallingUid = DEFAULT_TRACK_DIRECT_CALLING_UID;
private boolean mTrackScreenInteractive = DEFAULT_TRACK_SCREEN_INTERACTIVE;
private boolean mIgnoreBatteryStatus = DEFAULT_IGNORE_BATTERY_STATUS;
private boolean mCollectLatencyData = DEFAULT_COLLECT_LATENCY_DATA;
// Controls how many APIs will be collected per device. 1 means all APIs, 10 means every 10th
// API will be collected.
private int mShardingModulo = SHARDING_MODULO_DEFAULT;
// Controls which shards will be collected on this device.
private int mShardingOffset;
private CachedDeviceState.Readonly mDeviceState;
private CachedDeviceState.TimeInStateStopwatch mBatteryStopwatch;
private static final int CALL_STATS_OBSERVER_DEBOUNCE_MILLIS = 5000;
private BinderLatencyObserver mLatencyObserver;
private BinderInternal.CallStatsObserver mCallStatsObserver;
private ArraySet<Integer> mSendUidsToObserver = new ArraySet<>(32);
private final Handler mCallStatsObserverHandler;
private Runnable mCallStatsObserverRunnable = new Runnable() {
@Override
public void run() {
if (mCallStatsObserver == null) {
return;
}
noteCallsStatsDelayed();
synchronized (mLock) {
int size = mSendUidsToObserver.size();
for (int i = 0; i < size; i++) {
UidEntry uidEntry = mUidEntries.get(mSendUidsToObserver.valueAt(i));
if (uidEntry != null) {
ArrayMap<CallStatKey, CallStat> callStats = uidEntry.mCallStats;
final int csize = callStats.size();
final ArrayList<CallStat> tmpCallStats = new ArrayList<>(csize);
for (int j = 0; j < csize; j++) {
tmpCallStats.add(callStats.valueAt(j).clone());
}
mCallStatsObserver.noteCallStats(uidEntry.workSourceUid,
uidEntry.incrementalCallCount, tmpCallStats
);
uidEntry.incrementalCallCount = 0;
for (int j = callStats.size() - 1; j >= 0; j--) {
callStats.valueAt(j).incrementalCallCount = 0;
}
}
}
mSendUidsToObserver.clear();
}
}
};
private final Object mNativeTidsLock = new Object();
// @GuardedBy("mNativeTidsLock") // Cannot mark it as "GuardedBy" because it's read
// directly, as a volatile field.
private volatile IntArray mNativeTids = new IntArray(0);
/** Injector for {@link BinderCallsStats}. */
public static class Injector {
public Random getRandomGenerator() {
return new Random();
}
public Handler getHandler() {
return new Handler(Looper.getMainLooper());
}
/** Create a latency observer for the specified process. */
public BinderLatencyObserver getLatencyObserver(int processSource) {
return new BinderLatencyObserver(new BinderLatencyObserver.Injector(), processSource);
}
}
public BinderCallsStats(Injector injector) {
this(injector, SYSTEM_SERVER);
}
public BinderCallsStats(Injector injector, int processSource) {
this.mRandom = injector.getRandomGenerator();
this.mCallStatsObserverHandler = injector.getHandler();
this.mLatencyObserver = injector.getLatencyObserver(processSource);
this.mShardingOffset = mRandom.nextInt(mShardingModulo);
}
public void setDeviceState(@NonNull CachedDeviceState.Readonly deviceState) {
if (mBatteryStopwatch != null) {
mBatteryStopwatch.close();
}
mDeviceState = deviceState;
mBatteryStopwatch = deviceState.createTimeOnBatteryStopwatch();
}
/**
* Registers an observer for call stats, which is invoked periodically with accumulated
* binder call stats.
*/
public void setCallStatsObserver(
BinderInternal.CallStatsObserver callStatsObserver) {
mCallStatsObserver = callStatsObserver;
noteBinderThreadNativeIds();
noteCallsStatsDelayed();
}
private void noteCallsStatsDelayed() {
mCallStatsObserverHandler.removeCallbacks(mCallStatsObserverRunnable);
if (mCallStatsObserver != null) {
mCallStatsObserverHandler.postDelayed(mCallStatsObserverRunnable,
CALL_STATS_OBSERVER_DEBOUNCE_MILLIS);
}
}
@Override
@Nullable
public CallSession callStarted(Binder binder, int code, int workSourceUid) {
noteNativeThreadId();
boolean collectCpu = canCollect();
// We always want to collect data for latency if it's enabled, regardless of device state.
if (!mCollectLatencyData && !collectCpu) {
return null;
}
final CallSession s = obtainCallSession();
s.binderClass = binder.getClass();
s.transactionCode = code;
s.exceptionThrown = false;
s.cpuTimeStarted = -1;
s.timeStarted = -1;
s.recordedCall = shouldRecordDetailedData();
if (collectCpu && (mRecordingAllTransactionsForUid || s.recordedCall)) {
s.cpuTimeStarted = getThreadTimeMicro();
s.timeStarted = getElapsedRealtimeMicro();
} else if (mCollectLatencyData) {
s.timeStarted = getElapsedRealtimeMicro();
}
return s;
}
private CallSession obtainCallSession() {
CallSession s = mCallSessionsPool.poll();
return s == null ? new CallSession() : s;
}
@Override
public void callEnded(@Nullable CallSession s, int parcelRequestSize,
int parcelReplySize, int workSourceUid) {
if (s == null) {
return;
}
processCallEnded(s, parcelRequestSize, parcelReplySize, workSourceUid);
if (mCallSessionsPool.size() < CALL_SESSIONS_POOL_SIZE) {
mCallSessionsPool.add(s);
}
}
private void processCallEnded(CallSession s,
int parcelRequestSize, int parcelReplySize, int workSourceUid) {
if (mCollectLatencyData) {
mLatencyObserver.callEnded(s);
}
// Latency collection has already been processed so check if the rest should be processed.
if (!canCollect()) {
return;
}
UidEntry uidEntry = null;
final boolean recordCall;
if (s.recordedCall) {
recordCall = true;
} else if (mRecordingAllTransactionsForUid) {
uidEntry = getUidEntry(workSourceUid);
recordCall = uidEntry.recordAllTransactions;
} else {
recordCall = false;
}
final long duration;
final long latencyDuration;
if (recordCall) {
duration = getThreadTimeMicro() - s.cpuTimeStarted;
latencyDuration = getElapsedRealtimeMicro() - s.timeStarted;
} else {
duration = 0;
latencyDuration = 0;
}
final boolean screenInteractive = mTrackScreenInteractive
? mDeviceState.isScreenInteractive()
: OVERFLOW_SCREEN_INTERACTIVE;
final int callingUid = mTrackDirectCallingUid
? getCallingUid()
: OVERFLOW_DIRECT_CALLING_UID;
synchronized (mLock) {
// This was already checked in #callStart but check again while synchronized.
if (!canCollect()) {
return;
}
if (uidEntry == null) {
uidEntry = getUidEntry(workSourceUid);
}
uidEntry.callCount++;
uidEntry.incrementalCallCount++;
if (recordCall) {
uidEntry.cpuTimeMicros += duration;
uidEntry.recordedCallCount++;
final CallStat callStat = uidEntry.getOrCreate(
callingUid, s.binderClass, s.transactionCode,
screenInteractive,
mCallStatsCount >= mMaxBinderCallStatsCount);
final boolean isNewCallStat = callStat.callCount == 0;
if (isNewCallStat) {
mCallStatsCount++;
}
callStat.callCount++;
callStat.incrementalCallCount++;
callStat.recordedCallCount++;
callStat.cpuTimeMicros += duration;
callStat.maxCpuTimeMicros = Math.max(callStat.maxCpuTimeMicros, duration);
callStat.latencyMicros += latencyDuration;
callStat.maxLatencyMicros =
Math.max(callStat.maxLatencyMicros, latencyDuration);
if (mDetailedTracking) {
callStat.exceptionCount += s.exceptionThrown ? 1 : 0;
callStat.maxRequestSizeBytes =
Math.max(callStat.maxRequestSizeBytes, parcelRequestSize);
callStat.maxReplySizeBytes =
Math.max(callStat.maxReplySizeBytes, parcelReplySize);
}
} else {
// Only record the total call count if we already track data for this key.
// It helps to keep the memory usage down when sampling is enabled.
final CallStat callStat = uidEntry.get(
callingUid, s.binderClass, s.transactionCode,
screenInteractive);
if (callStat != null) {
callStat.callCount++;
callStat.incrementalCallCount++;
}
}
if (mCallStatsObserver != null && !UserHandle.isCore(workSourceUid)) {
mSendUidsToObserver.add(workSourceUid);
}
}
}
private boolean shouldExport(ExportedCallStat e, boolean applySharding) {
if (!applySharding) {
return true;
}
int hash = e.binderClass.hashCode();
hash = 31 * hash + e.transactionCode;
hash = 31 * hash + e.callingUid;
hash = 31 * hash + (e.screenInteractive ? 1231 : 1237);
return (hash + mShardingOffset) % mShardingModulo == 0;
}
private UidEntry getUidEntry(int uid) {
UidEntry uidEntry = mUidEntries.get(uid);
if (uidEntry == null) {
uidEntry = new UidEntry(uid);
mUidEntries.put(uid, uidEntry);
}
return uidEntry;
}
@Override
public void callThrewException(@Nullable CallSession s, Exception exception) {
if (s == null) {
return;
}
s.exceptionThrown = true;
try {
String className = exception.getClass().getName();
synchronized (mLock) {
if (mExceptionCounts.size() >= MAX_EXCEPTION_COUNT_SIZE) {
className = EXCEPTION_COUNT_OVERFLOW_NAME;
}
final Integer count = mExceptionCounts.get(className);
mExceptionCounts.put(className, count == null ? 1 : count + 1);
}
} catch (RuntimeException e) {
// Do not propagate the exception. We do not want to swallow original exception.
Slog.wtf(TAG, "Unexpected exception while updating mExceptionCounts");
}
}
private void noteNativeThreadId() {
final int tid = getNativeTid();
int index = mNativeTids.binarySearch(tid);
if (index >= 0) {
return;
}
// Use the copy-on-write approach. The changes occur exceedingly infrequently, so
// this code path is exercised just a few times per boot
synchronized (mNativeTidsLock) {
IntArray nativeTids = mNativeTids;
index = nativeTids.binarySearch(tid);
if (index < 0) {
IntArray copyOnWriteArray = new IntArray(nativeTids.size() + 1);
copyOnWriteArray.addAll(nativeTids);
copyOnWriteArray.add(-index - 1, tid);
mNativeTids = copyOnWriteArray;
}
}
noteBinderThreadNativeIds();
}
private void noteBinderThreadNativeIds() {
if (mCallStatsObserver == null) {
return;
}
mCallStatsObserver.noteBinderThreadNativeIds(getNativeTids());
}
private boolean canCollect() {
if (mRecordingAllTransactionsForUid) {
return true;
}
if (mIgnoreBatteryStatus) {
return true;
}
if (mDeviceState == null) {
return false;
}
if (mDeviceState.isCharging()) {
return false;
}
return true;
}
/**
* This method is expensive to call.
*/
public ArrayList<ExportedCallStat> getExportedCallStats() {
return getExportedCallStats(false);
}
/**
* This method is expensive to call.
* Exports call stats and applies sharding if requested.
*/
@VisibleForTesting
public ArrayList<ExportedCallStat> getExportedCallStats(boolean applySharding) {
// We do not collect all the data if detailed tracking is off.
if (!mDetailedTracking) {
return new ArrayList<>();
}
ArrayList<ExportedCallStat> resultCallStats = new ArrayList<>();
synchronized (mLock) {
final int uidEntriesSize = mUidEntries.size();
for (int entryIdx = 0; entryIdx < uidEntriesSize; entryIdx++) {
final UidEntry entry = mUidEntries.valueAt(entryIdx);
for (CallStat stat : entry.getCallStatsList()) {
ExportedCallStat e = getExportedCallStat(entry.workSourceUid, stat);
if (shouldExport(e, applySharding)) {
resultCallStats.add(e);
}
}
}
}
// Resolve codes outside of the lock since it can be slow.
resolveBinderMethodNames(resultCallStats);
// Debug entries added to help validate the data.
if (mAddDebugEntries && mBatteryStopwatch != null) {
resultCallStats.add(createDebugEntry("start_time_millis", mStartElapsedTime));
resultCallStats.add(createDebugEntry("end_time_millis", SystemClock.elapsedRealtime()));
resultCallStats.add(
createDebugEntry("battery_time_millis", mBatteryStopwatch.getMillis()));
resultCallStats.add(createDebugEntry("sampling_interval", mPeriodicSamplingInterval));
resultCallStats.add(createDebugEntry("sharding_modulo", mShardingModulo));
}
return resultCallStats;
}
/**
* This method is expensive to call.
*/
public ArrayList<ExportedCallStat> getExportedCallStats(int workSourceUid) {
return getExportedCallStats(workSourceUid, false);
}
/**
* This method is expensive to call.
* Exports call stats and applies sharding if requested.
*/
@VisibleForTesting
public ArrayList<ExportedCallStat> getExportedCallStats(
int workSourceUid, boolean applySharding) {
ArrayList<ExportedCallStat> resultCallStats = new ArrayList<>();
synchronized (mLock) {
final UidEntry entry = getUidEntry(workSourceUid);
for (CallStat stat : entry.getCallStatsList()) {
ExportedCallStat e = getExportedCallStat(workSourceUid, stat);
if (shouldExport(e, applySharding)) {
resultCallStats.add(e);
}
}
}
// Resolve codes outside of the lock since it can be slow.
resolveBinderMethodNames(resultCallStats);
return resultCallStats;
}
private ExportedCallStat getExportedCallStat(int workSourceUid, CallStat stat) {
ExportedCallStat exported = new ExportedCallStat();
exported.workSourceUid = workSourceUid;
exported.callingUid = stat.callingUid;
exported.className = stat.binderClass.getName();
exported.binderClass = stat.binderClass;
exported.transactionCode = stat.transactionCode;
exported.screenInteractive = stat.screenInteractive;
exported.cpuTimeMicros = stat.cpuTimeMicros;
exported.maxCpuTimeMicros = stat.maxCpuTimeMicros;
exported.latencyMicros = stat.latencyMicros;
exported.maxLatencyMicros = stat.maxLatencyMicros;
exported.recordedCallCount = stat.recordedCallCount;
exported.callCount = stat.callCount;
exported.maxRequestSizeBytes = stat.maxRequestSizeBytes;
exported.maxReplySizeBytes = stat.maxReplySizeBytes;
exported.exceptionCount = stat.exceptionCount;
return exported;
}
private void resolveBinderMethodNames(
ArrayList<ExportedCallStat> resultCallStats) {
// Resolve codes outside of the lock since it can be slow.
ExportedCallStat previous = null;
String previousMethodName = null;
resultCallStats.sort(BinderCallsStats::compareByBinderClassAndCode);
BinderTransactionNameResolver resolver = new BinderTransactionNameResolver();
for (ExportedCallStat exported : resultCallStats) {
final boolean isClassDifferent = previous == null
|| !previous.className.equals(exported.className);
final boolean isCodeDifferent = previous == null
|| previous.transactionCode != exported.transactionCode;
final String methodName;
if (isClassDifferent || isCodeDifferent) {
methodName = resolver.getMethodName(exported.binderClass, exported.transactionCode);
} else {
methodName = previousMethodName;
}
previousMethodName = methodName;
exported.methodName = methodName;
previous = exported;
}
}
private ExportedCallStat createDebugEntry(String variableName, long value) {
final int uid = Process.myUid();
final ExportedCallStat callStat = new ExportedCallStat();
callStat.className = "";
callStat.workSourceUid = uid;
callStat.callingUid = uid;
callStat.recordedCallCount = 1;
callStat.callCount = 1;
callStat.methodName = DEBUG_ENTRY_PREFIX + variableName;
callStat.latencyMicros = value;
return callStat;
}
/** @hide */
public ArrayMap<String, Integer> getExportedExceptionStats() {
synchronized (mLock) {
return new ArrayMap(mExceptionCounts);
}
}
/** Writes the collected statistics to the supplied {@link PrintWriter}.*/
public void dump(PrintWriter pw, AppIdToPackageMap packageMap, int workSourceUid,
boolean verbose) {
synchronized (mLock) {
dumpLocked(pw, packageMap, workSourceUid, verbose);
}
}
private void dumpLocked(PrintWriter pw, AppIdToPackageMap packageMap, int workSourceUid,
boolean verbose) {
if (workSourceUid != Process.INVALID_UID) {
verbose = true;
}
pw.print("Start time: ");
pw.println(DateFormat.format("yyyy-MM-dd HH:mm:ss", mStartCurrentTime));
pw.print("On battery time (ms): ");
pw.println(mBatteryStopwatch != null ? mBatteryStopwatch.getMillis() : 0);
pw.println("Sampling interval period: " + mPeriodicSamplingInterval);
pw.println("Sharding modulo: " + mShardingModulo);
final String datasetSizeDesc = verbose ? "" : "(top 90% by cpu time) ";
final StringBuilder sb = new StringBuilder();
pw.println("Per-UID raw data " + datasetSizeDesc
+ "(package/uid, worksource, call_desc, screen_interactive, "
+ "cpu_time_micros, max_cpu_time_micros, "
+ "latency_time_micros, max_latency_time_micros, exception_count, "
+ "max_request_size_bytes, max_reply_size_bytes, recorded_call_count, "
+ "call_count):");
final List<ExportedCallStat> exportedCallStats;
if (workSourceUid != Process.INVALID_UID) {
exportedCallStats = getExportedCallStats(workSourceUid, true);
} else {
exportedCallStats = getExportedCallStats(true);
}
exportedCallStats.sort(BinderCallsStats::compareByCpuDesc);
for (ExportedCallStat e : exportedCallStats) {
if (e.methodName != null && e.methodName.startsWith(DEBUG_ENTRY_PREFIX)) {
// Do not dump debug entries.
continue;
}
sb.setLength(0);
sb.append(" ")
.append(packageMap.mapUid(e.callingUid))
.append(',')
.append(packageMap.mapUid(e.workSourceUid))
.append(',').append(e.className)
.append('#').append(e.methodName)
.append(',').append(e.screenInteractive)
.append(',').append(e.cpuTimeMicros)
.append(',').append(e.maxCpuTimeMicros)
.append(',').append(e.latencyMicros)
.append(',').append(e.maxLatencyMicros)
.append(',').append(mDetailedTracking ? e.exceptionCount : '_')
.append(',').append(mDetailedTracking ? e.maxRequestSizeBytes : '_')
.append(',').append(mDetailedTracking ? e.maxReplySizeBytes : '_')
.append(',').append(e.recordedCallCount)
.append(',').append(e.callCount);
pw.println(sb);
}
pw.println();
final List<UidEntry> entries = new ArrayList<>();
long totalCallsCount = 0;
long totalRecordedCallsCount = 0;
long totalCpuTime = 0;
if (workSourceUid != Process.INVALID_UID) {
UidEntry e = getUidEntry(workSourceUid);
entries.add(e);
totalCpuTime += e.cpuTimeMicros;
totalRecordedCallsCount += e.recordedCallCount;
totalCallsCount += e.callCount;
} else {
final int uidEntriesSize = mUidEntries.size();
for (int i = 0; i < uidEntriesSize; i++) {
UidEntry e = mUidEntries.valueAt(i);
entries.add(e);
totalCpuTime += e.cpuTimeMicros;
totalRecordedCallsCount += e.recordedCallCount;
totalCallsCount += e.callCount;
}
entries.sort(
Comparator.<UidEntry>comparingDouble(value -> value.cpuTimeMicros).reversed());
}
pw.println("Per-UID Summary " + datasetSizeDesc
+ "(cpu_time, % of total cpu_time, recorded_call_count, call_count, package/uid):");
final List<UidEntry> summaryEntries = verbose ? entries
: getHighestValues(entries, value -> value.cpuTimeMicros, 0.9);
for (UidEntry entry : summaryEntries) {
String uidStr = packageMap.mapUid(entry.workSourceUid);
pw.println(String.format(" %10d %3.0f%% %8d %8d %s",
entry.cpuTimeMicros, 100d * entry.cpuTimeMicros / totalCpuTime,
entry.recordedCallCount, entry.callCount, uidStr));
}
pw.println();
if (workSourceUid == Process.INVALID_UID) {
pw.println(String.format(" Summary: total_cpu_time=%d, "
+ "calls_count=%d, avg_call_cpu_time=%.0f",
totalCpuTime, totalCallsCount,
(double) totalCpuTime / totalRecordedCallsCount));
pw.println();
}
pw.println("Exceptions thrown (exception_count, class_name):");
final List<Pair<String, Integer>> exceptionEntries = new ArrayList<>();
// We cannot use new ArrayList(Collection) constructor because MapCollections does not
// implement toArray method.
mExceptionCounts.entrySet().iterator().forEachRemaining(
(e) -> exceptionEntries.add(Pair.create(e.getKey(), e.getValue())));
exceptionEntries.sort((e1, e2) -> Integer.compare(e2.second, e1.second));
for (Pair<String, Integer> entry : exceptionEntries) {
pw.println(String.format(" %6d %s", entry.second, entry.first));
}
if (mPeriodicSamplingInterval != 1) {
pw.println("");
pw.println("/!\\ Displayed data is sampled. See sampling interval at the top.");
}
}
protected long getThreadTimeMicro() {
return SystemClock.currentThreadTimeMicro();
}
protected int getCallingUid() {
return Binder.getCallingUid();
}
protected int getNativeTid() {
return Process.myTid();
}
/**
* Returns known Linux TIDs for threads taking incoming binder calls.
*/
public int[] getNativeTids() {
return mNativeTids.toArray();
}
protected long getElapsedRealtimeMicro() {
return SystemClock.elapsedRealtimeNanos() / 1000;
}
protected boolean shouldRecordDetailedData() {
return mRandom.nextInt() % mPeriodicSamplingInterval == 0;
}
/**
* Sets to true to collect all the data.
*/
public void setDetailedTracking(boolean enabled) {
synchronized (mLock) {
if (enabled != mDetailedTracking) {
mDetailedTracking = enabled;
reset();
}
}
}
/**
* Whether to track the screen state.
*/
public void setTrackScreenInteractive(boolean enabled) {
synchronized (mLock) {
if (enabled != mTrackScreenInteractive) {
mTrackScreenInteractive = enabled;
reset();
}
}
}
/**
* Whether to track direct caller uid.
*/
public void setTrackDirectCallerUid(boolean enabled) {
synchronized (mLock) {
if (enabled != mTrackDirectCallingUid) {
mTrackDirectCallingUid = enabled;
reset();
}
}
}
/**
* Whether to ignore battery status when collecting stats
*/
public void setIgnoreBatteryStatus(boolean ignored) {
synchronized (mLock) {
if (ignored != mIgnoreBatteryStatus) {
mIgnoreBatteryStatus = ignored;
reset();
}
}
}
/**
* Marks the specified work source UID for total binder call tracking: detailed information
* will be recorded for all calls from this source ID.
*
* This is expensive and can cause memory pressure, therefore this mode should only be used
* for debugging.
*/
public void recordAllCallsForWorkSourceUid(int workSourceUid) {
setDetailedTracking(true);
Slog.i(TAG, "Recording all Binder calls for UID: " + workSourceUid);
UidEntry uidEntry = getUidEntry(workSourceUid);
uidEntry.recordAllTransactions = true;
mRecordingAllTransactionsForUid = true;
}
public void setAddDebugEntries(boolean addDebugEntries) {
mAddDebugEntries = addDebugEntries;
}
/**
* Sets the maximum number of items to track.
*/
public void setMaxBinderCallStats(int maxKeys) {
if (maxKeys <= 0) {
Slog.w(TAG, "Ignored invalid max value (value must be positive): "
+ maxKeys);
return;
}
synchronized (mLock) {
if (maxKeys != mMaxBinderCallStatsCount) {
mMaxBinderCallStatsCount = maxKeys;
reset();
}
}
}
public void setSamplingInterval(int samplingInterval) {
if (samplingInterval <= 0) {
Slog.w(TAG, "Ignored invalid sampling interval (value must be positive): "
+ samplingInterval);
return;
}
synchronized (mLock) {
if (samplingInterval != mPeriodicSamplingInterval) {
mPeriodicSamplingInterval = samplingInterval;
reset();
}
}
}
/** Updates the sharding modulo. */
public void setShardingModulo(int shardingModulo) {
if (shardingModulo <= 0) {
Slog.w(TAG, "Ignored invalid sharding modulo (value must be positive): "
+ shardingModulo);
return;
}
synchronized (mLock) {
if (shardingModulo != mShardingModulo) {
mShardingModulo = shardingModulo;
mShardingOffset = mRandom.nextInt(shardingModulo);
reset();
}
}
}
/** Whether to collect latency histograms. */
public void setCollectLatencyData(boolean collectLatencyData) {
mCollectLatencyData = collectLatencyData;
}
/** Whether to collect latency histograms. */
@VisibleForTesting
public boolean getCollectLatencyData() {
return mCollectLatencyData;
}
public void reset() {
synchronized (mLock) {
mCallStatsCount = 0;
mUidEntries.clear();
mExceptionCounts.clear();
mStartCurrentTime = System.currentTimeMillis();
mStartElapsedTime = SystemClock.elapsedRealtime();
if (mBatteryStopwatch != null) {
mBatteryStopwatch.reset();
}
mRecordingAllTransactionsForUid = false;
// Do not reset the latency observer as binder stats and latency will be pushed to WW
// at different intervals so the resets should not be coupled.
}
}
/**
* Aggregated data by uid/class/method to be sent through statsd.
*/
public static class ExportedCallStat {
public int callingUid;
public int workSourceUid;
public String className;
public String methodName;
public boolean screenInteractive;
public long cpuTimeMicros;
public long maxCpuTimeMicros;
public long latencyMicros;
public long maxLatencyMicros;
public long callCount;
public long recordedCallCount;
public long maxRequestSizeBytes;
public long maxReplySizeBytes;
public long exceptionCount;
// Used internally.
Class<? extends Binder> binderClass;
int transactionCode;
}
@VisibleForTesting
public static class CallStat {
// The UID who executed the transaction (i.e. Binder#getCallingUid).
public final int callingUid;
public final Class<? extends Binder> binderClass;
public final int transactionCode;
// True if the screen was interactive when the call ended.
public final boolean screenInteractive;
// Number of calls for which we collected data for. We do not record data for all the calls
// when sampling is on.
public long recordedCallCount;
// Roughly the real number of total calls. We only track only track the API call count once
// at least one non-sampled count happened.
public long callCount;
// Total CPU of all for all the recorded calls.
// Approximate total CPU usage can be computed by
// cpuTimeMicros * callCount / recordedCallCount
public long cpuTimeMicros;
public long maxCpuTimeMicros;
// Total latency of all for all the recorded calls.
// Approximate average latency can be computed by
// latencyMicros * callCount / recordedCallCount
public long latencyMicros;
public long maxLatencyMicros;
// The following fields are only computed if mDetailedTracking is set.
public long maxRequestSizeBytes;
public long maxReplySizeBytes;
public long exceptionCount;
// Call count since reset
public long incrementalCallCount;
public CallStat(int callingUid, Class<? extends Binder> binderClass, int transactionCode,
boolean screenInteractive) {
this.callingUid = callingUid;
this.binderClass = binderClass;
this.transactionCode = transactionCode;
this.screenInteractive = screenInteractive;
}
@Override
public CallStat clone() {
CallStat clone = new CallStat(callingUid, binderClass, transactionCode,
screenInteractive);
clone.recordedCallCount = recordedCallCount;
clone.callCount = callCount;
clone.cpuTimeMicros = cpuTimeMicros;
clone.maxCpuTimeMicros = maxCpuTimeMicros;
clone.latencyMicros = latencyMicros;
clone.maxLatencyMicros = maxLatencyMicros;
clone.maxRequestSizeBytes = maxRequestSizeBytes;
clone.maxReplySizeBytes = maxReplySizeBytes;
clone.exceptionCount = exceptionCount;
clone.incrementalCallCount = incrementalCallCount;
return clone;
}
@Override
public String toString() {
// This is expensive, but CallStat.toString() is only used for debugging.
String methodName = new BinderTransactionNameResolver().getMethodName(binderClass,
transactionCode);
return "CallStat{"
+ "callingUid=" + callingUid
+ ", transaction=" + binderClass.getSimpleName() + '.' + methodName
+ ", callCount=" + callCount
+ ", incrementalCallCount=" + incrementalCallCount
+ ", recordedCallCount=" + recordedCallCount
+ ", cpuTimeMicros=" + cpuTimeMicros
+ ", latencyMicros=" + latencyMicros
+ '}';
}
}
/** Key used to store CallStat object in a Map. */
public static class CallStatKey {
public int callingUid;
public Class<? extends Binder> binderClass;
public int transactionCode;
private boolean screenInteractive;
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
final CallStatKey key = (CallStatKey) o;
return callingUid == key.callingUid
&& transactionCode == key.transactionCode
&& screenInteractive == key.screenInteractive
&& (binderClass.equals(key.binderClass));
}
@Override
public int hashCode() {
int result = binderClass.hashCode();
result = 31 * result + transactionCode;
result = 31 * result + callingUid;
result = 31 * result + (screenInteractive ? 1231 : 1237);
return result;
}
}
@VisibleForTesting
public static class UidEntry {
// The UID who is responsible for the binder transaction. If the bluetooth process execute a
// transaction on behalf of app foo, the workSourceUid will be the uid of app foo.
public int workSourceUid;
// Number of calls for which we collected data for. We do not record data for all the calls
// when sampling is on.
public long recordedCallCount;
// Real number of total calls.
public long callCount;
// Total CPU of all for all the recorded calls.
// Approximate total CPU usage can be computed by
// cpuTimeMicros * callCount / recordedCallCount
public long cpuTimeMicros;
// Call count that gets reset after delivery to BatteryStats
public long incrementalCallCount;
// Indicates that all transactions for the UID must be tracked
public boolean recordAllTransactions;
UidEntry(int uid) {
this.workSourceUid = uid;
}
// Aggregate time spent per each call name: call_desc -> cpu_time_micros
private ArrayMap<CallStatKey, CallStat> mCallStats = new ArrayMap<>();
private CallStatKey mTempKey = new CallStatKey();
@Nullable
CallStat get(int callingUid, Class<? extends Binder> binderClass, int transactionCode,
boolean screenInteractive) {
// Use a global temporary key to avoid creating new objects for every lookup.
mTempKey.callingUid = callingUid;
mTempKey.binderClass = binderClass;
mTempKey.transactionCode = transactionCode;
mTempKey.screenInteractive = screenInteractive;
return mCallStats.get(mTempKey);
}
CallStat getOrCreate(int callingUid, Class<? extends Binder> binderClass,
int transactionCode, boolean screenInteractive, boolean maxCallStatsReached) {
CallStat mapCallStat = get(callingUid, binderClass, transactionCode, screenInteractive);
// Only create CallStat if it's a new entry, otherwise update existing instance.
if (mapCallStat == null) {
if (maxCallStatsReached) {
mapCallStat = get(OVERFLOW_DIRECT_CALLING_UID, OVERFLOW_BINDER,
OVERFLOW_TRANSACTION_CODE, OVERFLOW_SCREEN_INTERACTIVE);
if (mapCallStat != null) {
return mapCallStat;
}
callingUid = OVERFLOW_DIRECT_CALLING_UID;
binderClass = OVERFLOW_BINDER;
transactionCode = OVERFLOW_TRANSACTION_CODE;
screenInteractive = OVERFLOW_SCREEN_INTERACTIVE;
}
mapCallStat = new CallStat(callingUid, binderClass, transactionCode,
screenInteractive);
CallStatKey key = new CallStatKey();
key.callingUid = callingUid;
key.binderClass = binderClass;
key.transactionCode = transactionCode;
key.screenInteractive = screenInteractive;
mCallStats.put(key, mapCallStat);
}
return mapCallStat;
}
/**
* Returns list of calls sorted by CPU time
*/
public Collection<CallStat> getCallStatsList() {
return mCallStats.values();
}
@Override
public String toString() {
return "UidEntry{" +
"cpuTimeMicros=" + cpuTimeMicros +
", callCount=" + callCount +
", mCallStats=" + mCallStats +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
UidEntry uidEntry = (UidEntry) o;
return workSourceUid == uidEntry.workSourceUid;
}
@Override
public int hashCode() {
return workSourceUid;
}
}
@VisibleForTesting
public SparseArray<UidEntry> getUidEntries() {
return mUidEntries;
}
@VisibleForTesting
public ArrayMap<String, Integer> getExceptionCounts() {
return mExceptionCounts;
}
public BinderLatencyObserver getLatencyObserver() {
return mLatencyObserver;
}
@VisibleForTesting
public static <T> List<T> getHighestValues(List<T> list, ToDoubleFunction<T> toDouble,
double percentile) {
List<T> sortedList = new ArrayList<>(list);
sortedList.sort(Comparator.comparingDouble(toDouble).reversed());
double total = 0;
for (T item : list) {
total += toDouble.applyAsDouble(item);
}
List<T> result = new ArrayList<>();
double runningSum = 0;
for (T item : sortedList) {
if (runningSum > percentile * total) {
break;
}
result.add(item);
runningSum += toDouble.applyAsDouble(item);
}
return result;
}
private static int compareByCpuDesc(
ExportedCallStat a, ExportedCallStat b) {
return Long.compare(b.cpuTimeMicros, a.cpuTimeMicros);
}
private static int compareByBinderClassAndCode(
ExportedCallStat a, ExportedCallStat b) {
int result = a.className.compareTo(b.className);
return result != 0
? result
: Integer.compare(a.transactionCode, b.transactionCode);
}
/** @hide */
public static void startForBluetooth(Context context) {
new BinderCallsStats.SettingsObserver(
context,
new BinderCallsStats(
new BinderCallsStats.Injector(),
com.android.internal.os.BinderLatencyProto.Dims.BLUETOOTH));
}
/**
* Settings observer for other processes (not system_server).
*
* We do not want to collect cpu data from other processes so only latency collection should be
* possible to enable.
*/
public static class SettingsObserver extends ContentObserver {
// Settings for BinderCallsStats.
public static final String SETTINGS_ENABLED_KEY = "enabled";
public static final String SETTINGS_DETAILED_TRACKING_KEY = "detailed_tracking";
public static final String SETTINGS_UPLOAD_DATA_KEY = "upload_data";
public static final String SETTINGS_SAMPLING_INTERVAL_KEY = "sampling_interval";
public static final String SETTINGS_TRACK_SCREEN_INTERACTIVE_KEY = "track_screen_state";
public static final String SETTINGS_TRACK_DIRECT_CALLING_UID_KEY = "track_calling_uid";
public static final String SETTINGS_MAX_CALL_STATS_KEY = "max_call_stats_count";
public static final String SETTINGS_IGNORE_BATTERY_STATUS_KEY = "ignore_battery_status";
public static final String SETTINGS_SHARDING_MODULO_KEY = "sharding_modulo";
// Settings for BinderLatencyObserver.
public static final String SETTINGS_COLLECT_LATENCY_DATA_KEY = "collect_latency_data";
public static final String SETTINGS_LATENCY_OBSERVER_SAMPLING_INTERVAL_KEY =
"latency_observer_sampling_interval";
public static final String SETTINGS_LATENCY_OBSERVER_SHARDING_MODULO_KEY =
"latency_observer_sharding_modulo";
public static final String SETTINGS_LATENCY_OBSERVER_PUSH_INTERVAL_MINUTES_KEY =
"latency_observer_push_interval_minutes";
public static final String SETTINGS_LATENCY_HISTOGRAM_BUCKET_COUNT_KEY =
"latency_histogram_bucket_count";
public static final String SETTINGS_LATENCY_HISTOGRAM_FIRST_BUCKET_SIZE_KEY =
"latency_histogram_first_bucket_size";
public static final String SETTINGS_LATENCY_HISTOGRAM_BUCKET_SCALE_FACTOR_KEY =
"latency_histogram_bucket_scale_factor";
private boolean mEnabled;
private final Uri mUri = Settings.Global.getUriFor(Settings.Global.BINDER_CALLS_STATS);
private final Context mContext;
private final KeyValueListParser mParser = new KeyValueListParser(',');
private final BinderCallsStats mBinderCallsStats;
public SettingsObserver(Context context, BinderCallsStats binderCallsStats) {
super(BackgroundThread.getHandler());
mContext = context;
context.getContentResolver().registerContentObserver(mUri, false, this);
mBinderCallsStats = binderCallsStats;
// Always kick once to ensure that we match current state
onChange();
}
@Override
public void onChange(boolean selfChange, Uri uri, int userId) {
if (mUri.equals(uri)) {
onChange();
}
}
void onChange() {
try {
mParser.setString(Settings.Global.getString(mContext.getContentResolver(),
Settings.Global.BINDER_CALLS_STATS));
} catch (IllegalArgumentException e) {
Slog.e(TAG, "Bad binder call stats settings", e);
}
// Cpu data collection should always be disabled for other processes.
mBinderCallsStats.setDetailedTracking(false);
mBinderCallsStats.setTrackScreenInteractive(false);
mBinderCallsStats.setTrackDirectCallerUid(false);
mBinderCallsStats.setIgnoreBatteryStatus(
mParser.getBoolean(SETTINGS_IGNORE_BATTERY_STATUS_KEY,
BinderCallsStats.DEFAULT_IGNORE_BATTERY_STATUS));
mBinderCallsStats.setCollectLatencyData(
mParser.getBoolean(SETTINGS_COLLECT_LATENCY_DATA_KEY,
BinderCallsStats.DEFAULT_COLLECT_LATENCY_DATA));
// Binder latency observer settings.
configureLatencyObserver(mParser, mBinderCallsStats.getLatencyObserver());
final boolean enabled =
mParser.getBoolean(SETTINGS_ENABLED_KEY, BinderCallsStats.ENABLED_DEFAULT);
if (mEnabled != enabled) {
if (enabled) {
Binder.setObserver(mBinderCallsStats);
} else {
Binder.setObserver(null);
}
mEnabled = enabled;
mBinderCallsStats.reset();
mBinderCallsStats.setAddDebugEntries(enabled);
mBinderCallsStats.getLatencyObserver().reset();
}
}
/** Configures the binder latency observer related settings. */
public static void configureLatencyObserver(
KeyValueListParser mParser, BinderLatencyObserver binderLatencyObserver) {
binderLatencyObserver.setSamplingInterval(mParser.getInt(
SETTINGS_LATENCY_OBSERVER_SAMPLING_INTERVAL_KEY,
BinderLatencyObserver.PERIODIC_SAMPLING_INTERVAL_DEFAULT));
binderLatencyObserver.setShardingModulo(mParser.getInt(
SETTINGS_LATENCY_OBSERVER_SHARDING_MODULO_KEY,
BinderLatencyObserver.SHARDING_MODULO_DEFAULT));
binderLatencyObserver.setHistogramBucketsParams(
mParser.getInt(
SETTINGS_LATENCY_HISTOGRAM_BUCKET_COUNT_KEY,
BinderLatencyObserver.BUCKET_COUNT_DEFAULT),
mParser.getInt(
SETTINGS_LATENCY_HISTOGRAM_FIRST_BUCKET_SIZE_KEY,
BinderLatencyObserver.FIRST_BUCKET_SIZE_DEFAULT),
mParser.getFloat(
SETTINGS_LATENCY_HISTOGRAM_BUCKET_SCALE_FACTOR_KEY,
BinderLatencyObserver.BUCKET_SCALE_FACTOR_DEFAULT));
binderLatencyObserver.setPushInterval(mParser.getInt(
SETTINGS_LATENCY_OBSERVER_PUSH_INTERVAL_MINUTES_KEY,
BinderLatencyObserver.STATSD_PUSH_INTERVAL_MINUTES_DEFAULT));
}
}
}
|