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

Commit bcca6f83 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add filtering and aggregate stats to log" into sc-dev

parents 9c7d3ad3 ce0260e2
Loading
Loading
Loading
Loading
+58 −26
Original line number Diff line number Diff line
@@ -79,6 +79,7 @@ import android.os.UserHandle;
import android.os.WorkSource;
import android.os.WorkSource.WorkChain;
import android.stats.location.LocationStatsEnums;
import android.util.ArrayMap;
import android.util.IndentingPrintWriter;
import android.util.Log;

@@ -87,6 +88,7 @@ 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.eventlog.LocationEventLog;
import com.android.server.location.geofence.GeofenceManager;
import com.android.server.location.geofence.GeofenceProxy;
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.Injector;
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.LocationPowerSaveModeHelper;
import com.android.server.location.injector.LocationUsageLogger;
@@ -147,9 +148,10 @@ public class LocationManagerService extends ILocationManager.Stub {

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

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

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

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

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

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

        mEventLog = eventLog;
        mLocalService = new LocalService();
        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,
        // 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));

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

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

@@ -431,7 +434,7 @@ public class LocationManagerService extends ILocationManager.Stub {
            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)
                .putExtra(LocationManager.EXTRA_LOCATION_ENABLED, enabled)
@@ -1193,10 +1196,28 @@ public class LocationManagerService extends ILocationManager.Stub {

        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);
                }
                return;
            }
        }

        ipw.println("Location Manager State:");
        ipw.increaseIndent();
@@ -1227,6 +1248,25 @@ public class LocationManagerService extends ILocationManager.Stub {
        }
        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) {
            ipw.println("GNSS Manager:");
            ipw.increaseIndent();
@@ -1241,7 +1281,7 @@ public class LocationManagerService extends ILocationManager.Stub {

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

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

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

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

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

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

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

package com.android.server.location.eventlog;

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

import com.android.internal.util.Preconditions;

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

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

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

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

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

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

    // 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
    private int startIndex() {
        return wrapIndex(mLogEndIndex - mLogSize);
@@ -205,12 +228,13 @@ public abstract class LocalEventLog {

    // returns the index after this one
    private int incrementIndex(int index) {
        if (index == -1) {
            return startIndex();
        } else if (index >= 0) {
            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
@@ -219,7 +243,9 @@ public abstract class LocalEventLog {
        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;

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

        LogIterator() {
            this(null);
        }

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

            increment();
        }

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

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

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

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

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

            return getTimePrefix(realtimeMs + mSystemTimeDeltaMs) + 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();
            return getTimePrefix(timeMs) + log.getLogString();
        }

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

        @Override
        public void set(String s) {
            throw new UnsupportedOperationException();
        private void increment() {
            long nextDeltaMs = mIndex == -1 ? 0 : mLog[mIndex].getTimeDeltaMs();
            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 Diff line number Diff line
@@ -56,7 +56,4 @@ public interface Injector {

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

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

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

import java.util.concurrent.CopyOnWriteArrayList;

/**
Loading