Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 83259675 authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Refactor location event log

Log a much wider variety of important events in memory for bugreports
and dumpsys. This will make debugging much easier, hopefully at a
minimal memory cost. Memory cost could be improved in the future as
well.

Test: manual + presubmits
Change-Id: Iab867575f783f1c5f41a405da66f72d5f52691bf
parent f20b5f03
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ import com.android.internal.location.ProviderProperties;
 */
interface ILocationManager
{
    Location getLastLocation(String provider, String packageName, String attributionTag);
    @nullable Location getLastLocation(String provider, String packageName, String attributionTag);
    @nullable ICancellationSignal getCurrentLocation(String provider, in LocationRequest request, in ILocationCallback callback, String packageName, String attributionTag, String listenerId);

    void registerLocationListener(String provider, in LocationRequest request, in ILocationListener listener, String packageName, String attributionTag, String listenerId);
+3 −2
Original line number Diff line number Diff line
@@ -750,7 +750,7 @@ public final class LocationRequest implements Parcelable {
            s.append(qualityToString(mQuality)).append(" ");
        }
        if (mInterval != PASSIVE_INTERVAL) {
            s.append("interval=");
            s.append("@");
            TimeUtils.formatDuration(mInterval, s);
        } else {
            s.append("PASSIVE");
@@ -765,7 +765,8 @@ public final class LocationRequest implements Parcelable {
        if (mMaxUpdates != Integer.MAX_VALUE) {
            s.append(" maxUpdates=").append(mMaxUpdates);
        }
        if (mMinUpdateIntervalMillis < mInterval) {
        if (mMinUpdateIntervalMillis != IMPLICIT_MIN_UPDATE_INTERVAL
                && mMinUpdateIntervalMillis < mInterval) {
            s.append(" minUpdateInterval=");
            TimeUtils.formatDuration(mMinUpdateIntervalMillis, s);
        }
+16 −21
Original line number Diff line number Diff line
@@ -83,8 +83,6 @@ import com.android.internal.util.DumpUtils;
import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.location.LocationRequestStatistics.PackageProviderKey;
import com.android.server.location.LocationRequestStatistics.PackageStatistics;
import com.android.server.location.geofence.GeofenceManager;
import com.android.server.location.geofence.GeofenceProxy;
import com.android.server.location.gnss.GnssManagerService;
@@ -93,6 +91,7 @@ import com.android.server.location.util.AppForegroundHelper;
import com.android.server.location.util.AppOpsHelper;
import com.android.server.location.util.Injector;
import com.android.server.location.util.LocationAttributionHelper;
import com.android.server.location.util.LocationEventLog;
import com.android.server.location.util.LocationPermissionsHelper;
import com.android.server.location.util.LocationPowerSaveModeHelper;
import com.android.server.location.util.LocationUsageLogger;
@@ -114,9 +113,7 @@ import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
@@ -419,6 +416,8 @@ public class LocationManagerService extends ILocationManager.Stub {
            Log.d(TAG, "[u" + userId + "] location enabled = " + enabled);
        }

        mInjector.getLocationEventLog().logLocationEnabled(userId, enabled);

        Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION)
                .putExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled)
                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY)
@@ -1131,18 +1130,6 @@ public class LocationManagerService extends ILocationManager.Stub {
        mInjector.getSettingsHelper().dump(fd, ipw, args);
        ipw.decreaseIndent();

        ipw.println("Historical Records by Provider:");
        ipw.increaseIndent();
        TreeMap<PackageProviderKey, PackageStatistics> sorted = new TreeMap<>(
                mInjector.getLocationRequestStatistics().statistics);
        for (Map.Entry<PackageProviderKey, PackageStatistics> entry
                : sorted.entrySet()) {
            ipw.println(entry.getKey() + ": " + entry.getValue());
        }
        ipw.decreaseIndent();

        mInjector.getLocationRequestStatistics().history.dump(ipw);

        synchronized (mLock) {
            if (mExtraLocationControllerPackage != null) {
                ipw.println(
@@ -1170,6 +1157,13 @@ public class LocationManagerService extends ILocationManager.Stub {
        ipw.increaseIndent();
        mGeofenceManager.dump(fd, ipw, args);
        ipw.decreaseIndent();

        ipw.println("Event Log:");
        ipw.increaseIndent();
        for (String log : mInjector.getLocationEventLog()) {
            ipw.println(log);
        }
        ipw.decreaseIndent();
    }

    private class LocalService extends LocationManagerInternal {
@@ -1231,6 +1225,7 @@ public class LocationManagerService extends ILocationManager.Stub {

    private static class SystemInjector implements Injector {

        private final LocationEventLog mLocationEventLog;
        private final UserInfoHelper mUserInfoHelper;
        private final AlarmHelper mAlarmHelper;
        private final SystemAppOpsHelper mAppOpsHelper;
@@ -1241,9 +1236,9 @@ public class LocationManagerService extends ILocationManager.Stub {
        private final SystemScreenInteractiveHelper mScreenInteractiveHelper;
        private final LocationAttributionHelper mLocationAttributionHelper;
        private final LocationUsageLogger mLocationUsageLogger;
        private final LocationRequestStatistics mLocationRequestStatistics;

        SystemInjector(Context context, UserInfoHelper userInfoHelper) {
            mLocationEventLog = new LocationEventLog();
            mUserInfoHelper = userInfoHelper;
            mAlarmHelper = new SystemAlarmHelper(context);
            mAppOpsHelper = new SystemAppOpsHelper(context);
@@ -1251,11 +1246,11 @@ public class LocationManagerService extends ILocationManager.Stub {
                    mAppOpsHelper);
            mSettingsHelper = new SystemSettingsHelper(context);
            mAppForegroundHelper = new SystemAppForegroundHelper(context);
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context);
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context,
                    mLocationEventLog);
            mScreenInteractiveHelper = new SystemScreenInteractiveHelper(context);
            mLocationAttributionHelper = new LocationAttributionHelper(mAppOpsHelper);
            mLocationUsageLogger = new LocationUsageLogger();
            mLocationRequestStatistics = new LocationRequestStatistics();
        }

        void onSystemReady() {
@@ -1318,8 +1313,8 @@ public class LocationManagerService extends ILocationManager.Stub {
        }

        @Override
        public LocationRequestStatistics getLocationRequestStatistics() {
            return mLocationRequestStatistics;
        public LocationEventLog getLocationEventLog() {
            return mLocationEventLog;
        }
    }
}
+38 −19
Original line number Diff line number Diff line
@@ -95,6 +95,7 @@ import com.android.server.location.util.AppForegroundHelper.AppForegroundListene
import com.android.server.location.util.AppOpsHelper;
import com.android.server.location.util.Injector;
import com.android.server.location.util.LocationAttributionHelper;
import com.android.server.location.util.LocationEventLog;
import com.android.server.location.util.LocationPermissionsHelper;
import com.android.server.location.util.LocationPermissionsHelper.LocationPermissionsListener;
import com.android.server.location.util.LocationPowerSaveModeHelper;
@@ -276,6 +277,8 @@ class LocationProviderManager extends
                        + getRequest());
            }

            mLocationEventLog.logProviderClientRegistered(mName, getIdentity(), super.getRequest());

            // initialization order is important as there are ordering dependencies
            mPermitted = mLocationPermissionsHelper.hasLocationPermissions(mPermissionLevel,
                    getIdentity());
@@ -295,6 +298,8 @@ class LocationProviderManager extends

            onProviderListenerUnregister();

            mLocationEventLog.logProviderClientUnregistered(mName, getIdentity());

            if (D) {
                Log.d(TAG, mName + " provider removed registration from " + getIdentity());
            }
@@ -793,6 +798,7 @@ class LocationProviderManager extends

                    listener.deliverOnLocationChanged(deliveryLocation,
                            location.isFromMockProvider() ? null : mWakeLock::release);
                    mLocationEventLog.logProviderDeliveredLocation(mName, getIdentity());
                }

                @Override
@@ -1095,6 +1101,7 @@ class LocationProviderManager extends
                    // we currently don't hold a wakelock for getCurrentLocation deliveries
                    try {
                        listener.deliverOnLocationChanged(deliveryLocation, null);
                        mLocationEventLog.logProviderDeliveredLocation(mName, getIdentity());
                    } catch (Exception exception) {
                        if (exception instanceof RemoteException) {
                            Log.w(TAG, "registration " + this + " failed", exception);
@@ -1175,7 +1182,7 @@ class LocationProviderManager extends
    protected final LocationAttributionHelper mLocationAttributionHelper;
    protected final LocationUsageLogger mLocationUsageLogger;
    protected final LocationFudger mLocationFudger;
    protected final LocationRequestStatistics mLocationRequestStatistics;
    protected final LocationEventLog mLocationEventLog;

    private final UserListener mUserChangedListener = this::onUserChanged;
    private final UserSettingChangedListener mLocationEnabledChangedListener =
@@ -1236,7 +1243,7 @@ class LocationProviderManager extends
        mScreenInteractiveHelper = injector.getScreenInteractiveHelper();
        mLocationAttributionHelper = injector.getLocationAttributionHelper();
        mLocationUsageLogger = injector.getLocationUsageLogger();
        mLocationRequestStatistics = injector.getLocationRequestStatistics();
        mLocationEventLog = injector.getLocationEventLog();
        mLocationFudger = new LocationFudger(mSettingsHelper.getCoarseLocationAccuracyM());

        // initialize last since this lets our reference escape
@@ -1354,6 +1361,8 @@ class LocationProviderManager extends
        synchronized (mLock) {
            Preconditions.checkState(mStarted);

            mLocationEventLog.logProviderMocked(mName, provider != null);

            long identity = Binder.clearCallingIdentity();
            try {
                mProvider.setMockProvider(provider);
@@ -1717,13 +1726,6 @@ class LocationProviderManager extends
                key instanceof PendingIntent,
                /* geofence= */ key instanceof IBinder,
                null, registration.isForeground());

        mLocationRequestStatistics.startRequesting(
                registration.getIdentity().getPackageName(),
                registration.getIdentity().getAttributionTag(),
                mName,
                registration.getRequest().getIntervalMillis(),
                registration.isForeground());
    }

    @GuardedBy("mLock")
@@ -1743,11 +1745,6 @@ class LocationProviderManager extends
            Preconditions.checkState(Thread.holdsLock(mLock));
        }

        mLocationRequestStatistics.stopRequesting(
                registration.getIdentity().getPackageName(),
                registration.getIdentity().getAttributionTag(),
                mName);

        mLocationUsageLogger.logLocationApiUsage(
                LocationStatsEnums.USAGE_ENDED,
                LocationStatsEnums.API_REQUEST_LOCATION_UPDATES,
@@ -1799,13 +1796,20 @@ class LocationProviderManager extends
        Preconditions.checkState(delayMs >= 0 && delayMs <= newRequest.getIntervalMillis());

        if (delayMs < MIN_REQUEST_DELAY_MS) {
            mLocationEventLog.logProviderUpdateRequest(mName, newRequest);
            mProvider.setRequest(newRequest);
        } else {
            if (D) {
                Log.d(TAG, mName + " provider delaying request update " + newRequest + " by "
                        + TimeUtils.formatDuration(delayMs));
            }

            mDelayedRegister = new OnAlarmListener() {
                @Override
                public void onAlarm() {
                    synchronized (mLock) {
                        if (mDelayedRegister == this) {
                            mLocationEventLog.logProviderUpdateRequest(mName, newRequest);
                            mProvider.setRequest(newRequest);
                            mDelayedRegister = null;
                        }
@@ -1825,6 +1829,7 @@ class LocationProviderManager extends
            Preconditions.checkState(Thread.holdsLock(mLock));
        }

        mLocationEventLog.logProviderUpdateRequest(mName, EMPTY_REQUEST);
        mProvider.setRequest(EMPTY_REQUEST);
    }

@@ -2093,16 +2098,21 @@ class LocationProviderManager extends
            return;
        }

        if (mPassiveManager != null) {
            // don't log location received for passive provider because it's spammy
            mLocationEventLog.logProviderReceivedLocation(mName);
        }

        // update last location
        setLastLocation(location, UserHandle.USER_ALL);

        // attempt listener delivery
        deliverToListeners(registration -> registration.acceptLocationChange(location));

        // notify passive provider
        if (mPassiveManager != null) {
            mPassiveManager.updateLocation(location);
        }

        // attempt listener delivery
        deliverToListeners(registration -> registration.acceptLocationChange(location));
    }

    @GuardedBy("mLock")
@@ -2194,9 +2204,13 @@ class LocationProviderManager extends

        mEnabled.put(userId, enabled);

        // don't log unknown -> false transitions for brevity
        if (wasEnabled != null || enabled) {
            if (D) {
                Log.d(TAG, "[u" + userId + "] " + mName + " provider enabled = " + enabled);
            }
            mLocationEventLog.logProviderEnabled(mName, userId, enabled);
        }

        // clear last locations if we become disabled
        if (!enabled) {
@@ -2270,6 +2284,11 @@ class LocationProviderManager extends
        ipw.decreaseIndent();
    }

    @Override
    protected String getServiceState() {
        return mProvider.getCurrentRequest().toString();
    }

    private static class LastLocation {

        @Nullable private Location mFineLocation;
+0 −449

File deleted.

Preview size limit exceeded, changes collapsed.

Loading