diff options
| author | 2021-12-15 06:10:10 +0000 | |
|---|---|---|
| committer | 2021-12-24 00:07:13 +0000 | |
| commit | 64d95faa18d0310cb00f97c4c10c27c21f231a81 (patch) | |
| tree | 76b9d723c38713baa7921748a6852c3b75b0817f | |
| parent | 6d2adf4215bd42386572dc1181d2570a488d9414 (diff) | |
Add daily metric snapshot logging framework
Bug: 209811134
Test: ran on device and checked on westworld
Change-Id: I4bb65de9b88d290a82972bebafe544fd978433c2
| -rw-r--r-- | services/core/java/com/android/server/location/contexthub/ContextHubService.java | 46 | ||||
| -rw-r--r-- | services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java | 6 |
2 files changed, 51 insertions, 1 deletions
diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubService.java b/services/core/java/com/android/server/location/contexthub/ContextHubService.java index fc9697debb1d..61e6d148c0fb 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubService.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubService.java @@ -72,6 +72,9 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -138,11 +141,23 @@ public class ContextHubService extends IContextHubService.Stub { // The manager for the internal nanoapp state cache private final NanoAppStateManager mNanoAppStateManager = new NanoAppStateManager(); + // An executor and the future object for scheduling timeout timers + private final ScheduledThreadPoolExecutor mDailyMetricTimer = + new ScheduledThreadPoolExecutor(1); + + + // The period of the recurring time + private static final int PERIOD_METRIC_QUERY_DAYS = 1; + // True if WiFi is available for the Context Hub private boolean mIsWifiAvailable = false; private boolean mIsWifiScanningEnabled = false; private boolean mIsWifiMainEnabled = false; + // A hashmap used to record if a contexthub is waiting for daily query + private Set<Integer> mMetricQueryPendingContextHubIds = + Collections.newSetFromMap(new ConcurrentHashMap<Integer, Boolean>()); + // Lock object for sendWifiSettingUpdate() private final Object mSendWifiSettingUpdateLock = new Object(); @@ -317,6 +332,8 @@ public class ContextHubService extends IContextHubService.Stub { }); } + + scheduleDailyMetricSnapshot(); } /** @@ -747,6 +764,18 @@ public class ContextHubService extends IContextHubService.Stub { * @param nanoappStateList the list of loaded nanoapps */ private void handleQueryAppsCallback(int contextHubId, List<NanoAppState> nanoappStateList) { + if (mMetricQueryPendingContextHubIds.contains(contextHubId)) { + for (NanoAppState nanoappState : nanoappStateList) { + ContextHubStatsLog.write( + ContextHubStatsLog.CONTEXT_HUB_LOADED_NANOAPP_SNAPSHOT_REPORTED, + contextHubId, nanoappState.getNanoAppId(), + (int) nanoappState.getNanoAppVersion()); + } + mMetricQueryPendingContextHubIds.remove(contextHubId); + if (mMetricQueryPendingContextHubIds.isEmpty()) { + scheduleDailyMetricSnapshot(); + } + } mNanoAppStateManager.updateCache(contextHubId, nanoappStateList); mTransactionManager.onQueryResponse(nanoappStateList); } @@ -1135,6 +1164,23 @@ public class ContextHubService extends IContextHubService.Stub { sendMicrophoneDisableSettingUpdate(isEnabled); } + /** + * Invokes a daily timer to query all context hubs + */ + private void scheduleDailyMetricSnapshot() { + Runnable queryAllContextHub = () -> { + for (int contextHubId : mContextHubIdToInfoMap.keySet()) { + mMetricQueryPendingContextHubIds.add(contextHubId); + queryNanoAppsInternal(contextHubId); + } + }; + try { + mDailyMetricTimer.schedule(queryAllContextHub, PERIOD_METRIC_QUERY_DAYS, + TimeUnit.DAYS); + } catch (Exception e) { + Log.e(TAG, "Error when schedule a timer", e); + } + } private String getCallingPackageName() { return mContext.getPackageManager().getNameForUid(Binder.getCallingUid()); diff --git a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java index abf5a2494224..19f7c4d8da37 100644 --- a/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java +++ b/services/core/java/com/android/server/location/contexthub/ContextHubTransactionManager.java @@ -463,8 +463,12 @@ import java.util.concurrent.atomic.AtomicInteger; }; long timeoutSeconds = transaction.getTimeout(TimeUnit.SECONDS); - mTimeoutFuture = mTimeoutExecutor.schedule(onTimeoutFunc, timeoutSeconds, + try { + mTimeoutFuture = mTimeoutExecutor.schedule(onTimeoutFunc, timeoutSeconds, TimeUnit.SECONDS); + } catch (Exception e) { + Log.e(TAG, "Error when schedule a timer", e); + } } else { transaction.onTransactionComplete( ContextHubServiceUtil.toTransactionResult(result)); |