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

Commit 9c03b188 authored by Xin Guan's avatar Xin Guan Committed by Android (Google) Code Review
Browse files

Merge "Avoid holding main lock for pending event processing" into main

parents 551eb75f 7712cba7
Loading
Loading
Loading
Loading
+21 −15
Original line number Diff line number Diff line
@@ -495,9 +495,12 @@ public class UsageStatsService extends SystemService implements
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "loadPendingEvents");
            loadPendingEventsLocked(userId, pendingEvents);
            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
            synchronized (mReportedEvents) {
                final LinkedList<Event> eventsInMem = mReportedEvents.get(userId);
                if (eventsInMem != null) {
                    pendingEvents.addAll(eventsInMem);
                    mReportedEvents.remove(userId);
                }
            }
            boolean needToFlush = !pendingEvents.isEmpty();

@@ -518,8 +521,7 @@ public class UsageStatsService extends SystemService implements
            mIoHandler.obtainMessage(MSG_HANDLE_LAUNCH_TIME_ON_USER_UNLOCK,
                    userId, 0).sendToTarget();

            // Remove all the stats stored in memory and in system DE.
            mReportedEvents.remove(userId);
            // Remove all the stats stored in system DE.
            deleteRecursively(new File(Environment.getDataSystemDeDirectory(userId), "usagestats"));

            // Force a flush to disk for the current user to ensure important events are persisted.
@@ -916,6 +918,7 @@ public class UsageStatsService extends SystemService implements
        }
    }

    @GuardedBy({"mLock", "mReportedEvents"})
    private void persistPendingEventsLocked(int userId) {
        final LinkedList<Event> pendingEvents = mReportedEvents.get(userId);
        if (pendingEvents == null || pendingEvents.isEmpty()) {
@@ -1019,7 +1022,7 @@ public class UsageStatsService extends SystemService implements
                    + UserUsageStatsService.eventToString(event.mEventType);
            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, traceTag);
        }
        synchronized (mLock) {
        synchronized (mReportedEvents) {
            LinkedList<Event> events = mReportedEvents.get(userId);
            if (events == null) {
                events = new LinkedList<>();
@@ -1943,18 +1946,21 @@ public class UsageStatsService extends SystemService implements
                        idpw.println();
                    }
                } else {
                    synchronized (mReportedEvents) {
                        final LinkedList<Event> pendingEvents = mReportedEvents.get(userId);
                        if (pendingEvents != null && !pendingEvents.isEmpty()) {
                            final int eventCount = pendingEvents.size();
                            idpw.println("Pending events: count=" + eventCount);
                            idpw.increaseIndent();
                            for (int idx = 0; idx < eventCount; idx++) {
                            UserUsageStatsService.printEvent(idpw, pendingEvents.get(idx), true);
                                UserUsageStatsService.printEvent(idpw, pendingEvents.get(idx),
                                        true);
                            }
                            idpw.decreaseIndent();
                            idpw.println();
                        }
                    }
                }
                idpw.decreaseIndent();
            }