1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define DEBUG false
18 #include "Log.h"
19 
20 #include <android/os/IStatsCompanionService.h>
21 #include <android/os/IStatsPullerCallback.h>
22 #include <cutils/log.h>
23 #include <math.h>
24 #include <stdint.h>
25 #include <algorithm>
26 #include "../StatsService.h"
27 #include "../logd/LogEvent.h"
28 #include "../stats_log_util.h"
29 #include "../statscompanion_util.h"
30 #include "CarStatsPuller.h"
31 #include "GpuStatsPuller.h"
32 #include "PowerStatsPuller.h"
33 #include "ResourceHealthManagerPuller.h"
34 #include "StatsCallbackPuller.h"
35 #include "StatsCompanionServicePuller.h"
36 #include "StatsPullerManager.h"
37 #include "SubsystemSleepStatePuller.h"
38 #include "TrainInfoPuller.h"
39 #include "statslog.h"
40 
41 using std::shared_ptr;
42 using std::vector;
43 
44 namespace android {
45 namespace os {
46 namespace statsd {
47 
48 // Values smaller than this may require to update the alarm.
49 const int64_t NO_ALARM_UPDATE = INT64_MAX;
50 
51 std::map<int, PullAtomInfo> StatsPullerManager::kAllPullAtomInfo = {
52         // wifi_bytes_transfer
53         {android::util::WIFI_BYTES_TRANSFER,
54          {.additiveFields = {2, 3, 4, 5},
55           .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER)}},
56         // wifi_bytes_transfer_by_fg_bg
57         {android::util::WIFI_BYTES_TRANSFER_BY_FG_BG,
58          {.additiveFields = {3, 4, 5, 6},
59           .puller = new StatsCompanionServicePuller(android::util::WIFI_BYTES_TRANSFER_BY_FG_BG)}},
60         // mobile_bytes_transfer
61         {android::util::MOBILE_BYTES_TRANSFER,
62          {.additiveFields = {2, 3, 4, 5},
63           .puller = new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER)}},
64         // mobile_bytes_transfer_by_fg_bg
65         {android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG,
66          {.additiveFields = {3, 4, 5, 6},
67           .puller =
68                   new StatsCompanionServicePuller(android::util::MOBILE_BYTES_TRANSFER_BY_FG_BG)}},
69         // bluetooth_bytes_transfer
70         {android::util::BLUETOOTH_BYTES_TRANSFER,
71          {.additiveFields = {2, 3},
72           .puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_BYTES_TRANSFER)}},
73         // kernel_wakelock
74         {android::util::KERNEL_WAKELOCK,
75          {.puller = new StatsCompanionServicePuller(android::util::KERNEL_WAKELOCK)}},
76         // subsystem_sleep_state
77         {android::util::SUBSYSTEM_SLEEP_STATE, {.puller = new SubsystemSleepStatePuller()}},
78         // on_device_power_measurement
79         {android::util::ON_DEVICE_POWER_MEASUREMENT, {.puller = new PowerStatsPuller()}},
80         // cpu_time_per_freq
81         {android::util::CPU_TIME_PER_FREQ,
82          {.additiveFields = {3},
83           .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_FREQ)}},
84         // cpu_time_per_uid
85         {android::util::CPU_TIME_PER_UID,
86          {.additiveFields = {2, 3},
87           .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID)}},
88         // cpu_time_per_uid_freq
89         // the throttling is 3sec, handled in
90         // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
91         {android::util::CPU_TIME_PER_UID_FREQ,
92          {.additiveFields = {4},
93           .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_UID_FREQ)}},
94         // cpu_active_time
95         // the throttling is 3sec, handled in
96         // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
97         {android::util::CPU_ACTIVE_TIME,
98          {.additiveFields = {2},
99           .puller = new StatsCompanionServicePuller(android::util::CPU_ACTIVE_TIME)}},
100         // cpu_cluster_time
101         // the throttling is 3sec, handled in
102         // frameworks/base/core/java/com/android/internal/os/KernelCpuProcReader
103         {android::util::CPU_CLUSTER_TIME,
104          {.additiveFields = {3},
105           .puller = new StatsCompanionServicePuller(android::util::CPU_CLUSTER_TIME)}},
106         // wifi_activity_energy_info
107         {android::util::WIFI_ACTIVITY_INFO,
108          {.puller = new StatsCompanionServicePuller(android::util::WIFI_ACTIVITY_INFO)}},
109         // modem_activity_info
110         {android::util::MODEM_ACTIVITY_INFO,
111          {.puller = new StatsCompanionServicePuller(android::util::MODEM_ACTIVITY_INFO)}},
112         // bluetooth_activity_info
113         {android::util::BLUETOOTH_ACTIVITY_INFO,
114          {.puller = new StatsCompanionServicePuller(android::util::BLUETOOTH_ACTIVITY_INFO)}},
115         // system_elapsed_realtime
116         {android::util::SYSTEM_ELAPSED_REALTIME,
117          {.coolDownNs = NS_PER_SEC,
118           .puller = new StatsCompanionServicePuller(android::util::SYSTEM_ELAPSED_REALTIME),
119           .pullTimeoutNs = NS_PER_SEC / 2,
120          }},
121         // system_uptime
122         {android::util::SYSTEM_UPTIME,
123          {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_UPTIME)}},
124         // remaining_battery_capacity
125         {android::util::REMAINING_BATTERY_CAPACITY,
126          {.puller = new ResourceHealthManagerPuller(android::util::REMAINING_BATTERY_CAPACITY)}},
127         // full_battery_capacity
128         {android::util::FULL_BATTERY_CAPACITY,
129          {.puller = new ResourceHealthManagerPuller(android::util::FULL_BATTERY_CAPACITY)}},
130         // battery_voltage
131         {android::util::BATTERY_VOLTAGE,
132          {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_VOLTAGE)}},
133         // battery_level
134         {android::util::BATTERY_LEVEL,
135          {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_LEVEL)}},
136         // battery_cycle_count
137         {android::util::BATTERY_CYCLE_COUNT,
138          {.puller = new ResourceHealthManagerPuller(android::util::BATTERY_CYCLE_COUNT)}},
139         // process_memory_state
140         {android::util::PROCESS_MEMORY_STATE,
141          {.additiveFields = {4, 5, 6, 7, 8, 9},
142           .puller = new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_STATE)}},
143         // native_process_memory_state
144         {android::util::NATIVE_PROCESS_MEMORY_STATE,
145          {.additiveFields = {3, 4, 5, 6, 8},
146           .puller = new StatsCompanionServicePuller(android::util::NATIVE_PROCESS_MEMORY_STATE)}},
147         // process_memory_high_water_mark
148         {android::util::PROCESS_MEMORY_HIGH_WATER_MARK,
149          {.additiveFields = {3},
150           .puller =
151                   new StatsCompanionServicePuller(android::util::PROCESS_MEMORY_HIGH_WATER_MARK)}},
152         // system_ion_heap_size
153         {android::util::SYSTEM_ION_HEAP_SIZE,
154          {.puller = new StatsCompanionServicePuller(android::util::SYSTEM_ION_HEAP_SIZE)}},
155         // process_system_ion_heap_size
156         {android::util::PROCESS_SYSTEM_ION_HEAP_SIZE,
157          {.puller = new StatsCompanionServicePuller(android::util::PROCESS_SYSTEM_ION_HEAP_SIZE)}},
158         // temperature
159         {android::util::TEMPERATURE,
160          {.puller = new StatsCompanionServicePuller(android::util::TEMPERATURE)}},
161         // cooling_device
162         {android::util::COOLING_DEVICE,
163          {.puller = new StatsCompanionServicePuller(android::util::COOLING_DEVICE)}},
164         // binder_calls
165         {android::util::BINDER_CALLS,
166          {.additiveFields = {4, 5, 6, 8, 12},
167           .puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS)}},
168         // binder_calls_exceptions
169         {android::util::BINDER_CALLS_EXCEPTIONS,
170          {.puller = new StatsCompanionServicePuller(android::util::BINDER_CALLS_EXCEPTIONS)}},
171         // looper_stats
172         {android::util::LOOPER_STATS,
173          {.additiveFields = {5, 6, 7, 8, 9},
174           .puller = new StatsCompanionServicePuller(android::util::LOOPER_STATS)}},
175         // Disk Stats
176         {android::util::DISK_STATS,
177          {.puller = new StatsCompanionServicePuller(android::util::DISK_STATS)}},
178         // Directory usage
179         {android::util::DIRECTORY_USAGE,
180          {.puller = new StatsCompanionServicePuller(android::util::DIRECTORY_USAGE)}},
181         // Size of app's code, data, and cache
182         {android::util::APP_SIZE,
183          {.puller = new StatsCompanionServicePuller(android::util::APP_SIZE)}},
184         // Size of specific categories of files. Eg. Music.
185         {android::util::CATEGORY_SIZE,
186          {.puller = new StatsCompanionServicePuller(android::util::CATEGORY_SIZE)}},
187         // Number of fingerprints enrolled for each user.
188         {android::util::NUM_FINGERPRINTS_ENROLLED,
189          {.puller = new StatsCompanionServicePuller(android::util::NUM_FINGERPRINTS_ENROLLED)}},
190         // Number of faces enrolled for each user.
191         {android::util::NUM_FACES_ENROLLED,
192          {.puller = new StatsCompanionServicePuller(android::util::NUM_FACES_ENROLLED)}},
193         // ProcStats.
194         {android::util::PROC_STATS,
195          {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS)}},
196         // ProcStatsPkgProc.
197         {android::util::PROC_STATS_PKG_PROC,
198          {.puller = new StatsCompanionServicePuller(android::util::PROC_STATS_PKG_PROC)}},
199         // Disk I/O stats per uid.
200         {android::util::DISK_IO,
201          {.additiveFields = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
202           .coolDownNs = 3 * NS_PER_SEC,
203           .puller = new StatsCompanionServicePuller(android::util::DISK_IO)}},
204         // PowerProfile constants for power model calculations.
205         {android::util::POWER_PROFILE,
206          {.puller = new StatsCompanionServicePuller(android::util::POWER_PROFILE)}},
207         // Process cpu stats. Min cool-down is 5 sec, inline with what AcitivityManagerService uses.
208         {android::util::PROCESS_CPU_TIME,
209          {.coolDownNs = 5 * NS_PER_SEC /* min cool-down in seconds*/,
210           .puller = new StatsCompanionServicePuller(android::util::PROCESS_CPU_TIME)}},
211         {android::util::CPU_TIME_PER_THREAD_FREQ,
212          {.additiveFields = {7, 9, 11, 13, 15, 17, 19, 21},
213           .puller = new StatsCompanionServicePuller(android::util::CPU_TIME_PER_THREAD_FREQ)}},
214         // DeviceCalculatedPowerUse.
215         {android::util::DEVICE_CALCULATED_POWER_USE,
216          {.puller = new StatsCompanionServicePuller(android::util::DEVICE_CALCULATED_POWER_USE)}},
217         // DeviceCalculatedPowerBlameUid.
218         {android::util::DEVICE_CALCULATED_POWER_BLAME_UID,
219          {.puller = new StatsCompanionServicePuller(
220                   android::util::DEVICE_CALCULATED_POWER_BLAME_UID)}},
221         // DeviceCalculatedPowerBlameOther.
222         {android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER,
223          {.puller = new StatsCompanionServicePuller(
224                   android::util::DEVICE_CALCULATED_POWER_BLAME_OTHER)}},
225         // DebugElapsedClock.
226         {android::util::DEBUG_ELAPSED_CLOCK,
227          {.additiveFields = {1, 2, 3, 4},
228           .puller = new StatsCompanionServicePuller(android::util::DEBUG_ELAPSED_CLOCK)}},
229         // DebugFailingElapsedClock.
230         {android::util::DEBUG_FAILING_ELAPSED_CLOCK,
231          {.additiveFields = {1, 2, 3, 4},
232           .puller = new StatsCompanionServicePuller(android::util::DEBUG_FAILING_ELAPSED_CLOCK)}},
233         // BuildInformation.
234         {android::util::BUILD_INFORMATION,
235          {.puller = new StatsCompanionServicePuller(android::util::BUILD_INFORMATION)}},
236         // RoleHolder.
237         {android::util::ROLE_HOLDER,
238          {.puller = new StatsCompanionServicePuller(android::util::ROLE_HOLDER)}},
239         // PermissionState.
240         {android::util::DANGEROUS_PERMISSION_STATE,
241          {.puller = new StatsCompanionServicePuller(android::util::DANGEROUS_PERMISSION_STATE)}},
242         // TrainInfo.
243         {android::util::TRAIN_INFO, {.puller = new TrainInfoPuller()}},
244         // TimeZoneDataInfo.
245         {android::util::TIME_ZONE_DATA_INFO,
246          {.puller = new StatsCompanionServicePuller(android::util::TIME_ZONE_DATA_INFO)}},
247         // ExternalStorageInfo
248         {android::util::EXTERNAL_STORAGE_INFO,
249          {.puller = new StatsCompanionServicePuller(android::util::EXTERNAL_STORAGE_INFO)}},
250         // GpuStatsGlobalInfo
251         {android::util::GPU_STATS_GLOBAL_INFO,
252          {.puller = new GpuStatsPuller(android::util::GPU_STATS_GLOBAL_INFO)}},
253         // GpuStatsAppInfo
254         {android::util::GPU_STATS_APP_INFO,
255          {.puller = new GpuStatsPuller(android::util::GPU_STATS_APP_INFO)}},
256         // AppsOnExternalStorageInfo
257         {android::util::APPS_ON_EXTERNAL_STORAGE_INFO,
258          {.puller = new StatsCompanionServicePuller(android::util::APPS_ON_EXTERNAL_STORAGE_INFO)}},
259         // Face Settings
260         {android::util::FACE_SETTINGS,
261          {.puller = new StatsCompanionServicePuller(android::util::FACE_SETTINGS)}},
262         // App ops
263         {android::util::APP_OPS,
264          {.puller = new StatsCompanionServicePuller(android::util::APP_OPS)}},
265         // VmsClientStats
266         {android::util::VMS_CLIENT_STATS,
267          {.additiveFields = {5, 6, 7, 8, 9, 10},
268           .puller = new CarStatsPuller(android::util::VMS_CLIENT_STATS)}},
269         // NotiifcationRemoteViews.
270         {android::util::NOTIFICATION_REMOTE_VIEWS,
271          {.puller = new StatsCompanionServicePuller(android::util::NOTIFICATION_REMOTE_VIEWS)}},
272 };
273 
StatsPullerManager()274 StatsPullerManager::StatsPullerManager() : mNextPullTimeNs(NO_ALARM_UPDATE) {
275 }
276 
Pull(int tagId,vector<shared_ptr<LogEvent>> * data)277 bool StatsPullerManager::Pull(int tagId, vector<shared_ptr<LogEvent>>* data) {
278     VLOG("Initiating pulling %d", tagId);
279 
280     if (kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end()) {
281         bool ret = kAllPullAtomInfo.find(tagId)->second.puller->Pull(data);
282         VLOG("pulled %d items", (int)data->size());
283         if (!ret) {
284             StatsdStats::getInstance().notePullFailed(tagId);
285         }
286         return ret;
287     } else {
288         VLOG("Unknown tagId %d", tagId);
289         return false;  // Return early since we don't know what to pull.
290     }
291 }
292 
PullerForMatcherExists(int tagId) const293 bool StatsPullerManager::PullerForMatcherExists(int tagId) const {
294     // Vendor pulled atoms might be registered after we parse the config.
295     return isVendorPulledAtom(tagId) || kAllPullAtomInfo.find(tagId) != kAllPullAtomInfo.end();
296 }
297 
updateAlarmLocked()298 void StatsPullerManager::updateAlarmLocked() {
299     if (mNextPullTimeNs == NO_ALARM_UPDATE) {
300         VLOG("No need to set alarms. Skipping");
301         return;
302     }
303 
304     sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
305     if (statsCompanionServiceCopy != nullptr) {
306         statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
307     } else {
308         VLOG("StatsCompanionService not available. Alarm not set.");
309     }
310     return;
311 }
312 
SetStatsCompanionService(sp<IStatsCompanionService> statsCompanionService)313 void StatsPullerManager::SetStatsCompanionService(
314         sp<IStatsCompanionService> statsCompanionService) {
315     AutoMutex _l(mLock);
316     sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
317     mStatsCompanionService = statsCompanionService;
318     for (const auto& pulledAtom : kAllPullAtomInfo) {
319         pulledAtom.second.puller->SetStatsCompanionService(statsCompanionService);
320     }
321     if (mStatsCompanionService != nullptr) {
322         updateAlarmLocked();
323     }
324 }
325 
RegisterReceiver(int tagId,wp<PullDataReceiver> receiver,int64_t nextPullTimeNs,int64_t intervalNs)326 void StatsPullerManager::RegisterReceiver(int tagId, wp<PullDataReceiver> receiver,
327                                               int64_t nextPullTimeNs, int64_t intervalNs) {
328     AutoMutex _l(mLock);
329     auto& receivers = mReceivers[tagId];
330     for (auto it = receivers.begin(); it != receivers.end(); it++) {
331         if (it->receiver == receiver) {
332             VLOG("Receiver already registered of %d", (int)receivers.size());
333             return;
334         }
335     }
336     ReceiverInfo receiverInfo;
337     receiverInfo.receiver = receiver;
338 
339     // Round it to the nearest minutes. This is the limit of alarm manager.
340     // In practice, we should always have larger buckets.
341     int64_t roundedIntervalNs = intervalNs / NS_PER_SEC / 60 * NS_PER_SEC * 60;
342     // Scheduled pulling should be at least 1 min apart.
343     // This can be lower in cts tests, in which case we round it to 1 min.
344     if (roundedIntervalNs < 60 * (int64_t)NS_PER_SEC) {
345         roundedIntervalNs = 60 * (int64_t)NS_PER_SEC;
346     }
347 
348     receiverInfo.intervalNs = roundedIntervalNs;
349     receiverInfo.nextPullTimeNs = nextPullTimeNs;
350     receivers.push_back(receiverInfo);
351 
352     // There is only one alarm for all pulled events. So only set it to the smallest denom.
353     if (nextPullTimeNs < mNextPullTimeNs) {
354         VLOG("Updating next pull time %lld", (long long)mNextPullTimeNs);
355         mNextPullTimeNs = nextPullTimeNs;
356         updateAlarmLocked();
357     }
358     VLOG("Puller for tagId %d registered of %d", tagId, (int)receivers.size());
359 }
360 
UnRegisterReceiver(int tagId,wp<PullDataReceiver> receiver)361 void StatsPullerManager::UnRegisterReceiver(int tagId, wp<PullDataReceiver> receiver) {
362     AutoMutex _l(mLock);
363     if (mReceivers.find(tagId) == mReceivers.end()) {
364         VLOG("Unknown pull code or no receivers: %d", tagId);
365         return;
366     }
367     auto& receivers = mReceivers.find(tagId)->second;
368     for (auto it = receivers.begin(); it != receivers.end(); it++) {
369         if (receiver == it->receiver) {
370             receivers.erase(it);
371             VLOG("Puller for tagId %d unregistered of %d", tagId, (int)receivers.size());
372             return;
373         }
374     }
375 }
376 
OnAlarmFired(int64_t elapsedTimeNs)377 void StatsPullerManager::OnAlarmFired(int64_t elapsedTimeNs) {
378     AutoMutex _l(mLock);
379     int64_t wallClockNs = getWallClockNs();
380 
381     int64_t minNextPullTimeNs = NO_ALARM_UPDATE;
382 
383     vector<pair<int, vector<ReceiverInfo*>>> needToPull =
384             vector<pair<int, vector<ReceiverInfo*>>>();
385     for (auto& pair : mReceivers) {
386         vector<ReceiverInfo*> receivers = vector<ReceiverInfo*>();
387         if (pair.second.size() != 0) {
388             for (ReceiverInfo& receiverInfo : pair.second) {
389                 if (receiverInfo.nextPullTimeNs <= elapsedTimeNs) {
390                     receivers.push_back(&receiverInfo);
391                 } else {
392                     if (receiverInfo.nextPullTimeNs < minNextPullTimeNs) {
393                         minNextPullTimeNs = receiverInfo.nextPullTimeNs;
394                     }
395                 }
396             }
397             if (receivers.size() > 0) {
398                 needToPull.push_back(make_pair(pair.first, receivers));
399             }
400         }
401     }
402 
403     for (const auto& pullInfo : needToPull) {
404         vector<shared_ptr<LogEvent>> data;
405         bool pullSuccess = Pull(pullInfo.first, &data);
406         if (pullSuccess) {
407             StatsdStats::getInstance().notePullDelay(
408                     pullInfo.first, getElapsedRealtimeNs() - elapsedTimeNs);
409         } else {
410             VLOG("pull failed at %lld, will try again later", (long long)elapsedTimeNs);
411         }
412 
413         // Convention is to mark pull atom timestamp at request time.
414         // If we pull at t0, puller starts at t1, finishes at t2, and send back
415         // at t3, we mark t0 as its timestamp, which should correspond to its
416         // triggering event, such as condition change at t0.
417         // Here the triggering event is alarm fired from AlarmManager.
418         // In ValueMetricProducer and GaugeMetricProducer we do same thing
419         // when pull on condition change, etc.
420         for (auto& event : data) {
421             event->setElapsedTimestampNs(elapsedTimeNs);
422             event->setLogdWallClockTimestampNs(wallClockNs);
423         }
424 
425         for (const auto& receiverInfo : pullInfo.second) {
426             sp<PullDataReceiver> receiverPtr = receiverInfo->receiver.promote();
427             if (receiverPtr != nullptr) {
428                 receiverPtr->onDataPulled(data, pullSuccess, elapsedTimeNs);
429                 // We may have just come out of a coma, compute next pull time.
430                 int numBucketsAhead =
431                         (elapsedTimeNs - receiverInfo->nextPullTimeNs) / receiverInfo->intervalNs;
432                 receiverInfo->nextPullTimeNs += (numBucketsAhead + 1) * receiverInfo->intervalNs;
433                 if (receiverInfo->nextPullTimeNs < minNextPullTimeNs) {
434                     minNextPullTimeNs = receiverInfo->nextPullTimeNs;
435                 }
436             } else {
437                 VLOG("receiver already gone.");
438             }
439         }
440     }
441 
442     VLOG("mNextPullTimeNs: %lld updated to %lld", (long long)mNextPullTimeNs,
443          (long long)minNextPullTimeNs);
444     mNextPullTimeNs = minNextPullTimeNs;
445     updateAlarmLocked();
446 }
447 
ForceClearPullerCache()448 int StatsPullerManager::ForceClearPullerCache() {
449     int totalCleared = 0;
450     for (const auto& pulledAtom : kAllPullAtomInfo) {
451         totalCleared += pulledAtom.second.puller->ForceClearCache();
452     }
453     return totalCleared;
454 }
455 
ClearPullerCacheIfNecessary(int64_t timestampNs)456 int StatsPullerManager::ClearPullerCacheIfNecessary(int64_t timestampNs) {
457     int totalCleared = 0;
458     for (const auto& pulledAtom : kAllPullAtomInfo) {
459         totalCleared += pulledAtom.second.puller->ClearCacheIfNecessary(timestampNs);
460     }
461     return totalCleared;
462 }
463 
RegisterPullerCallback(int32_t atomTag,const sp<IStatsPullerCallback> & callback)464 void StatsPullerManager::RegisterPullerCallback(int32_t atomTag,
465         const sp<IStatsPullerCallback>& callback) {
466     AutoMutex _l(mLock);
467     // Platform pullers cannot be changed.
468     if (!isVendorPulledAtom(atomTag)) {
469         VLOG("RegisterPullerCallback: atom tag %d is not vendor pulled", atomTag);
470         return;
471     }
472     VLOG("RegisterPullerCallback: adding puller for tag %d", atomTag);
473     StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/true);
474     kAllPullAtomInfo[atomTag] = {.puller = new StatsCallbackPuller(atomTag, callback)};
475 }
476 
UnregisterPullerCallback(int32_t atomTag)477 void StatsPullerManager::UnregisterPullerCallback(int32_t atomTag) {
478     AutoMutex _l(mLock);
479     // Platform pullers cannot be changed.
480     if (!isVendorPulledAtom(atomTag)) {
481         return;
482     }
483     StatsdStats::getInstance().notePullerCallbackRegistrationChanged(atomTag, /*registered=*/false);
484     kAllPullAtomInfo.erase(atomTag);
485 }
486 
487 }  // namespace statsd
488 }  // namespace os
489 }  // namespace android
490