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

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

Add filtering and aggregate stats to log

Store and dump some aggregate stats in the location event log, and add
the ability to filter dumpsys output by provider.

Bug: 179416865
Bug: 174260507
Test: manual
Change-Id: I8ce8c2fb2526e9ef7cdd4b231db32124d62b8608
parent f6b292cc
Loading
Loading
Loading
Loading
+58 −26
Original line number Original line Diff line number Diff line
@@ -79,6 +79,7 @@ import android.os.UserHandle;
import android.os.WorkSource;
import android.os.WorkSource;
import android.os.WorkSource.WorkChain;
import android.os.WorkSource.WorkChain;
import android.stats.location.LocationStatsEnums;
import android.stats.location.LocationStatsEnums;
import android.util.ArrayMap;
import android.util.IndentingPrintWriter;
import android.util.IndentingPrintWriter;
import android.util.Log;
import android.util.Log;


@@ -87,6 +88,7 @@ import com.android.internal.util.DumpUtils;
import com.android.internal.util.Preconditions;
import com.android.internal.util.Preconditions;
import com.android.server.LocalServices;
import com.android.server.LocalServices;
import com.android.server.SystemService;
import com.android.server.SystemService;
import com.android.server.location.eventlog.LocationEventLog;
import com.android.server.location.geofence.GeofenceManager;
import com.android.server.location.geofence.GeofenceManager;
import com.android.server.location.geofence.GeofenceProxy;
import com.android.server.location.geofence.GeofenceProxy;
import com.android.server.location.gnss.GnssConfiguration;
import com.android.server.location.gnss.GnssConfiguration;
@@ -98,7 +100,6 @@ import com.android.server.location.injector.AppOpsHelper;
import com.android.server.location.injector.EmergencyHelper;
import com.android.server.location.injector.EmergencyHelper;
import com.android.server.location.injector.Injector;
import com.android.server.location.injector.Injector;
import com.android.server.location.injector.LocationAttributionHelper;
import com.android.server.location.injector.LocationAttributionHelper;
import com.android.server.location.injector.LocationEventLog;
import com.android.server.location.injector.LocationPermissionsHelper;
import com.android.server.location.injector.LocationPermissionsHelper;
import com.android.server.location.injector.LocationPowerSaveModeHelper;
import com.android.server.location.injector.LocationPowerSaveModeHelper;
import com.android.server.location.injector.LocationUsageLogger;
import com.android.server.location.injector.LocationUsageLogger;
@@ -147,9 +148,10 @@ public class LocationManagerService extends ILocationManager.Stub {


        public Lifecycle(Context context) {
        public Lifecycle(Context context) {
            super(context);
            super(context);
            LocationEventLog eventLog = new LocationEventLog();
            mUserInfoHelper = new LifecycleUserInfoHelper(context);
            mUserInfoHelper = new LifecycleUserInfoHelper(context);
            mSystemInjector = new SystemInjector(context, mUserInfoHelper);
            mSystemInjector = new SystemInjector(context, mUserInfoHelper, eventLog);
            mService = new LocationManagerService(context, mSystemInjector);
            mService = new LocationManagerService(context, mSystemInjector, eventLog);
        }
        }


        @Override
        @Override
@@ -159,7 +161,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            // client caching behavior is only enabled after seeing the first invalidate
            // client caching behavior is only enabled after seeing the first invalidate
            LocationManager.invalidateLocalLocationEnabledCaches();
            LocationManager.invalidateLocalLocationEnabledCaches();
            // disable caching for our own process
            // disable caching for our own process
            Objects.requireNonNull(mService.mContext.getSystemService(LocationManager.class))
            Objects.requireNonNull(getContext().getSystemService(LocationManager.class))
                    .disableLocalLocationEnabledCaches();
                    .disableLocalLocationEnabledCaches();
        }
        }


@@ -221,6 +223,7 @@ public class LocationManagerService extends ILocationManager.Stub {


    private final Context mContext;
    private final Context mContext;
    private final Injector mInjector;
    private final Injector mInjector;
    private final LocationEventLog mEventLog;
    private final LocalService mLocalService;
    private final LocalService mLocalService;


    private final GeofenceManager mGeofenceManager;
    private final GeofenceManager mGeofenceManager;
@@ -245,10 +248,10 @@ public class LocationManagerService extends ILocationManager.Stub {
    private final CopyOnWriteArrayList<LocationProviderManager> mProviderManagers =
    private final CopyOnWriteArrayList<LocationProviderManager> mProviderManagers =
            new CopyOnWriteArrayList<>();
            new CopyOnWriteArrayList<>();


    LocationManagerService(Context context, Injector injector) {
    LocationManagerService(Context context, Injector injector, LocationEventLog eventLog) {
        mContext = context.createAttributionContext(ATTRIBUTION_TAG);
        mContext = context.createAttributionContext(ATTRIBUTION_TAG);
        mInjector = injector;
        mInjector = injector;

        mEventLog = eventLog;
        mLocalService = new LocalService();
        mLocalService = new LocalService();
        LocalServices.addService(LocationManagerInternal.class, mLocalService);
        LocalServices.addService(LocationManagerInternal.class, mLocalService);


@@ -256,7 +259,7 @@ public class LocationManagerService extends ILocationManager.Stub {


        // set up passive provider first since it will be required for all other location providers,
        // set up passive provider first since it will be required for all other location providers,
        // which are loaded later once the system is ready.
        // which are loaded later once the system is ready.
        mPassiveManager = new PassiveLocationProviderManager(mContext, injector);
        mPassiveManager = new PassiveLocationProviderManager(mContext, injector, mEventLog);
        addLocationProviderManager(mPassiveManager, new PassiveLocationProvider(mContext));
        addLocationProviderManager(mPassiveManager, new PassiveLocationProvider(mContext));


        // TODO: load the gps provider here as well, which will require refactoring
        // TODO: load the gps provider here as well, which will require refactoring
@@ -297,7 +300,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            }
            }


            LocationProviderManager manager = new LocationProviderManager(mContext, mInjector,
            LocationProviderManager manager = new LocationProviderManager(mContext, mInjector,
                    providerName, mPassiveManager);
                    mEventLog, providerName, mPassiveManager);
            addLocationProviderManager(manager, null);
            addLocationProviderManager(manager, null);
            return manager;
            return manager;
        }
        }
@@ -341,7 +344,7 @@ public class LocationManagerService extends ILocationManager.Stub {
                com.android.internal.R.string.config_networkLocationProviderPackageName);
                com.android.internal.R.string.config_networkLocationProviderPackageName);
        if (networkProvider != null) {
        if (networkProvider != null) {
            LocationProviderManager networkManager = new LocationProviderManager(mContext,
            LocationProviderManager networkManager = new LocationProviderManager(mContext,
                    mInjector, NETWORK_PROVIDER, mPassiveManager);
                    mInjector, mEventLog, NETWORK_PROVIDER, mPassiveManager);
            addLocationProviderManager(networkManager, networkProvider);
            addLocationProviderManager(networkManager, networkProvider);
        } else {
        } else {
            Log.w(TAG, "no network location provider found");
            Log.w(TAG, "no network location provider found");
@@ -360,7 +363,7 @@ public class LocationManagerService extends ILocationManager.Stub {
                com.android.internal.R.string.config_fusedLocationProviderPackageName);
                com.android.internal.R.string.config_fusedLocationProviderPackageName);
        if (fusedProvider != null) {
        if (fusedProvider != null) {
            LocationProviderManager fusedManager = new LocationProviderManager(mContext, mInjector,
            LocationProviderManager fusedManager = new LocationProviderManager(mContext, mInjector,
                    FUSED_PROVIDER, mPassiveManager);
                    mEventLog, FUSED_PROVIDER, mPassiveManager);
            addLocationProviderManager(fusedManager, fusedProvider);
            addLocationProviderManager(fusedManager, fusedProvider);
        } else {
        } else {
            Log.wtf(TAG, "no fused location provider found");
            Log.wtf(TAG, "no fused location provider found");
@@ -375,7 +378,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            mGnssManagerService.onSystemReady();
            mGnssManagerService.onSystemReady();


            LocationProviderManager gnssManager = new LocationProviderManager(mContext, mInjector,
            LocationProviderManager gnssManager = new LocationProviderManager(mContext, mInjector,
                    GPS_PROVIDER, mPassiveManager);
                    mEventLog, GPS_PROVIDER, mPassiveManager);
            addLocationProviderManager(gnssManager, mGnssManagerService.getGnssLocationProvider());
            addLocationProviderManager(gnssManager, mGnssManagerService.getGnssLocationProvider());
        }
        }


@@ -431,7 +434,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            Log.d(TAG, "[u" + userId + "] location enabled = " + enabled);
            Log.d(TAG, "[u" + userId + "] location enabled = " + enabled);
        }
        }


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


        Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION)
        Intent intent = new Intent(LocationManager.MODE_CHANGED_ACTION)
                .putExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled)
                .putExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled)
@@ -1193,10 +1196,28 @@ public class LocationManagerService extends ILocationManager.Stub {


        IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "  ");
        IndentingPrintWriter ipw = new IndentingPrintWriter(pw, "  ");


        if (mGnssManagerService != null && args.length > 0 && args[0].equals("--gnssmetrics")) {
        if (args.length > 0) {
            LocationProviderManager manager = getLocationProviderManager(args[0]);
            if (manager != null) {
                ipw.println("Provider:");
                ipw.increaseIndent();
                manager.dump(fd, ipw, args);
                ipw.decreaseIndent();

                ipw.println("Event Log:");
                ipw.increaseIndent();
                mEventLog.iterate(manager.getName(), ipw::println);
                ipw.decreaseIndent();
                return;
            }

            if ("--gnssmetrics".equals(args[0])) {
                if (mGnssManagerService != null) {
                    mGnssManagerService.dump(fd, ipw, args);
                    mGnssManagerService.dump(fd, ipw, args);
                }
                return;
                return;
            }
            }
        }


        ipw.println("Location Manager State:");
        ipw.println("Location Manager State:");
        ipw.increaseIndent();
        ipw.increaseIndent();
@@ -1227,6 +1248,25 @@ public class LocationManagerService extends ILocationManager.Stub {
        }
        }
        ipw.decreaseIndent();
        ipw.decreaseIndent();


        ipw.println("Historical Aggregate Location Provider Data:");
        ipw.increaseIndent();
        ArrayMap<String, ArrayMap<String, LocationEventLog.AggregateStats>> aggregateStats =
                mEventLog.copyAggregateStats();
        for (int i = 0; i < aggregateStats.size(); i++) {
            ipw.println(aggregateStats.keyAt(i));
            ipw.increaseIndent();
            ArrayMap<String, LocationEventLog.AggregateStats> providerStats =
                    aggregateStats.valueAt(i);
            for (int j = 0; j < providerStats.size(); j++) {
                ipw.print(providerStats.keyAt(j));
                ipw.print(": ");
                providerStats.valueAt(j).updateTotals();
                ipw.println(providerStats.valueAt(j));
            }
            ipw.decreaseIndent();
        }
        ipw.decreaseIndent();

        if (mGnssManagerService != null) {
        if (mGnssManagerService != null) {
            ipw.println("GNSS Manager:");
            ipw.println("GNSS Manager:");
            ipw.increaseIndent();
            ipw.increaseIndent();
@@ -1241,7 +1281,7 @@ public class LocationManagerService extends ILocationManager.Stub {


        ipw.println("Event Log:");
        ipw.println("Event Log:");
        ipw.increaseIndent();
        ipw.increaseIndent();
        mInjector.getLocationEventLog().iterate(ipw::println);
        mEventLog.iterate(ipw::println);
        ipw.decreaseIndent();
        ipw.decreaseIndent();
    }
    }


@@ -1320,7 +1360,6 @@ public class LocationManagerService extends ILocationManager.Stub {
        private final Context mContext;
        private final Context mContext;


        private final UserInfoHelper mUserInfoHelper;
        private final UserInfoHelper mUserInfoHelper;
        private final LocationEventLog mLocationEventLog;
        private final AlarmHelper mAlarmHelper;
        private final AlarmHelper mAlarmHelper;
        private final SystemAppOpsHelper mAppOpsHelper;
        private final SystemAppOpsHelper mAppOpsHelper;
        private final SystemLocationPermissionsHelper mLocationPermissionsHelper;
        private final SystemLocationPermissionsHelper mLocationPermissionsHelper;
@@ -1339,19 +1378,17 @@ public class LocationManagerService extends ILocationManager.Stub {
        @GuardedBy("this")
        @GuardedBy("this")
        private boolean mSystemReady;
        private boolean mSystemReady;


        SystemInjector(Context context, UserInfoHelper userInfoHelper) {
        SystemInjector(Context context, UserInfoHelper userInfoHelper, LocationEventLog eventLog) {
            mContext = context;
            mContext = context;


            mUserInfoHelper = userInfoHelper;
            mUserInfoHelper = userInfoHelper;
            mLocationEventLog = new LocationEventLog();
            mAlarmHelper = new SystemAlarmHelper(context);
            mAlarmHelper = new SystemAlarmHelper(context);
            mAppOpsHelper = new SystemAppOpsHelper(context);
            mAppOpsHelper = new SystemAppOpsHelper(context);
            mLocationPermissionsHelper = new SystemLocationPermissionsHelper(context,
            mLocationPermissionsHelper = new SystemLocationPermissionsHelper(context,
                    mAppOpsHelper);
                    mAppOpsHelper);
            mSettingsHelper = new SystemSettingsHelper(context);
            mSettingsHelper = new SystemSettingsHelper(context);
            mAppForegroundHelper = new SystemAppForegroundHelper(context);
            mAppForegroundHelper = new SystemAppForegroundHelper(context);
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context,
            mLocationPowerSaveModeHelper = new SystemLocationPowerSaveModeHelper(context, eventLog);
                    mLocationEventLog);
            mScreenInteractiveHelper = new SystemScreenInteractiveHelper(context);
            mScreenInteractiveHelper = new SystemScreenInteractiveHelper(context);
            mLocationAttributionHelper = new LocationAttributionHelper(mAppOpsHelper);
            mLocationAttributionHelper = new LocationAttributionHelper(mAppOpsHelper);
            mLocationUsageLogger = new LocationUsageLogger();
            mLocationUsageLogger = new LocationUsageLogger();
@@ -1429,11 +1466,6 @@ public class LocationManagerService extends ILocationManager.Stub {
            return mEmergencyCallHelper;
            return mEmergencyCallHelper;
        }
        }


        @Override
        public LocationEventLog getLocationEventLog() {
            return mLocationEventLog;
        }

        @Override
        @Override
        public LocationUsageLogger getLocationUsageLogger() {
        public LocationUsageLogger getLocationUsageLogger() {
            return mLocationUsageLogger;
            return mLocationUsageLogger;
+56 −74
Original line number Original line Diff line number Diff line
@@ -16,12 +16,13 @@


package com.android.server.location.eventlog;
package com.android.server.location.eventlog;


import android.annotation.Nullable;
import android.os.SystemClock;
import android.os.SystemClock;
import android.util.TimeUtils;
import android.util.TimeUtils;


import com.android.internal.util.Preconditions;
import com.android.internal.util.Preconditions;


import java.util.ListIterator;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import java.util.function.Consumer;


@@ -35,6 +36,7 @@ public abstract class LocalEventLog {
        boolean isFiller();
        boolean isFiller();
        long getTimeDeltaMs();
        long getTimeDeltaMs();
        String getLogString();
        String getLogString();
        boolean filter(@Nullable String filter);
    }
    }


    private static final class FillerEvent implements Log {
    private static final class FillerEvent implements Log {
@@ -62,6 +64,11 @@ public abstract class LocalEventLog {
        public String getLogString() {
        public String getLogString() {
            throw new AssertionError();
            throw new AssertionError();
        }
        }

        @Override
        public boolean filter(String filter) {
            return false;
        }
    }
    }


    /**
    /**
@@ -87,6 +94,11 @@ public abstract class LocalEventLog {
        public final long getTimeDeltaMs() {
        public final long getTimeDeltaMs() {
            return Integer.toUnsignedLong(mTimeDelta);
            return Integer.toUnsignedLong(mTimeDelta);
        }
        }

        @Override
        public boolean filter(String filter) {
            return false;
        }
    }
    }


    // circular buffer of log entries
    // circular buffer of log entries
@@ -198,6 +210,17 @@ public abstract class LocalEventLog {
        }
        }
    }
    }


    /**
     * Iterates over the event log, passing each filter-matching log string to the given
     * consumer.
     */
    public synchronized void iterate(String filter, Consumer<String> consumer) {
        LogIterator it = new LogIterator(filter);
        while (it.hasNext()) {
            consumer.accept(it.next());
        }
    }

    // returns the index of the first element
    // returns the index of the first element
    private int startIndex() {
    private int startIndex() {
        return wrapIndex(mLogEndIndex - mLogSize);
        return wrapIndex(mLogEndIndex - mLogSize);
@@ -205,12 +228,13 @@ public abstract class LocalEventLog {


    // returns the index after this one
    // returns the index after this one
    private int incrementIndex(int index) {
    private int incrementIndex(int index) {
        if (index == -1) {
            return startIndex();
        } else if (index >= 0) {
            return wrapIndex(index + 1);
            return wrapIndex(index + 1);
        } else {
            throw new IllegalArgumentException();
        }
        }

    // returns the index before this one
    private int decrementIndex(int index) {
        return wrapIndex(index - 1);
    }
    }


    // rolls over the given index if necessary
    // rolls over the given index if necessary
@@ -219,7 +243,9 @@ public abstract class LocalEventLog {
        return (index % mLog.length + mLog.length) % mLog.length;
        return (index % mLog.length + mLog.length) % mLog.length;
    }
    }


    private class LogIterator implements ListIterator<String> {
    private class LogIterator implements Iterator<String> {

        private final @Nullable String mFilter;


        private final long mSystemTimeDeltaMs;
        private final long mSystemTimeDeltaMs;


@@ -228,10 +254,17 @@ public abstract class LocalEventLog {
        private int mCount;
        private int mCount;


        LogIterator() {
        LogIterator() {
            this(null);
        }

        LogIterator(@Nullable String filter) {
            mFilter = filter;
            mSystemTimeDeltaMs = System.currentTimeMillis() - SystemClock.elapsedRealtime();
            mSystemTimeDeltaMs = System.currentTimeMillis() - SystemClock.elapsedRealtime();
            mCurrentRealtimeMs = mStartRealtimeMs;
            mCurrentRealtimeMs = mStartRealtimeMs;
            mIndex = startIndex();
            mIndex = -1;
            mCount = 0;
            mCount = -1;

            increment();
        }
        }


        @Override
        @Override
@@ -239,75 +272,17 @@ public abstract class LocalEventLog {
            return mCount < mLogSize;
            return mCount < mLogSize;
        }
        }


        @Override
        public boolean hasPrevious() {
            return mCount > 0;
        }

        @Override
        // return then increment
        public String next() {
        public String next() {
            if (!hasNext()) {
            if (!hasNext()) {
                throw new NoSuchElementException();
                throw new NoSuchElementException();
            }
            }


            Log log = mLog[mIndex];
            Log log = mLog[mIndex];
            long nextDeltaMs = log.getTimeDeltaMs();
            long timeMs = mCurrentRealtimeMs + log.getTimeDeltaMs() + mSystemTimeDeltaMs;
            long realtimeMs = mCurrentRealtimeMs + nextDeltaMs;


            // calculate next index, skipping filler events
            increment();
            do {
                mCurrentRealtimeMs += nextDeltaMs;
                mIndex = incrementIndex(mIndex);
                if (++mCount < mLogSize) {
                    nextDeltaMs = mLog[mIndex].getTimeDeltaMs();
                }
            } while (mCount < mLogSize && mLog[mIndex].isFiller());


            return getTimePrefix(realtimeMs + mSystemTimeDeltaMs) + log.getLogString();
            return getTimePrefix(timeMs) + log.getLogString();
        }

        @Override
        // decrement then return
        public String previous() {
            Log log;
            long currentDeltaMs;
            long realtimeMs;

            // calculate previous index, skipping filler events with MAX_TIME_DELTA
            do {
                if (!hasPrevious()) {
                    throw new NoSuchElementException();
                }

                mIndex = decrementIndex(mIndex);
                mCount--;

                log = mLog[mIndex];
                realtimeMs = mCurrentRealtimeMs;

                if (mCount > 0) {
                    currentDeltaMs = log.getTimeDeltaMs();
                    mCurrentRealtimeMs -= currentDeltaMs;
                }
            } while (mCount >= 0 && log.isFiller());

            return getTimePrefix(realtimeMs + mSystemTimeDeltaMs) + log.getLogString();
        }

        @Override
        public int nextIndex() {
            throw new UnsupportedOperationException();
        }

        @Override
        public int previousIndex() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void add(String s) {
            throw new UnsupportedOperationException();
        }
        }


        @Override
        @Override
@@ -315,9 +290,16 @@ public abstract class LocalEventLog {
            throw new UnsupportedOperationException();
            throw new UnsupportedOperationException();
        }
        }


        @Override
        private void increment() {
        public void set(String s) {
            long nextDeltaMs = mIndex == -1 ? 0 : mLog[mIndex].getTimeDeltaMs();
            throw new UnsupportedOperationException();
            do {
                mCurrentRealtimeMs += nextDeltaMs;
                mIndex = incrementIndex(mIndex);
                if (++mCount < mLogSize) {
                    nextDeltaMs = mLog[mIndex].getTimeDeltaMs();
                }
            } while (mCount < mLogSize && (mLog[mIndex].isFiller() || (mFilter != null
                    && !mLog[mIndex].filter(mFilter))));
        }
        }
    }
    }
}
}
+209 −31

File changed and moved.

Preview size limit exceeded, changes collapsed.

+0 −3
Original line number Original line Diff line number Diff line
@@ -56,7 +56,4 @@ public interface Injector {


    /** Returns a LocationUsageLogger. */
    /** Returns a LocationUsageLogger. */
    LocationUsageLogger getLocationUsageLogger();
    LocationUsageLogger getLocationUsageLogger();

    /** Returns a LocationEventLog. */
    LocationEventLog getLocationEventLog();
}
}
+2 −0
Original line number Original line Diff line number Diff line
@@ -24,6 +24,8 @@ import static com.android.server.location.LocationManagerService.TAG;
import android.os.PowerManager.LocationPowerSaveMode;
import android.os.PowerManager.LocationPowerSaveMode;
import android.util.Log;
import android.util.Log;


import com.android.server.location.eventlog.LocationEventLog;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArrayList;


/**
/**
Loading