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

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

Merge "Add transaction info on ContextHubService dump"

parents b29338a9 400d0860
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.location;

import java.util.concurrent.ConcurrentLinkedDeque;

/**
 * Helper class to make a ConcurrentLinkedDeque fixed-size, evicting old entries when full.
 *
 * @param <E> The type of elements held in this queue.
 */
public class ConcurrentLinkedEvictingDeque<E> extends ConcurrentLinkedDeque<E> {
    private int mSize;

    ConcurrentLinkedEvictingDeque(int size) {
        mSize = size;
    }

    @Override
    public boolean add(E elem) {
        synchronized (this) {
            if (size() == mSize) {
                poll();
            }

            return super.add(elem);
        }
    }
}
+0 −23
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;

/**
@@ -103,28 +102,6 @@ import java.util.function.Consumer;
    public static final int ACTION_UNREGISTERED = 1;
    public static final int ACTION_CANCELLED = 2;

    /**
     * Helper class to make a ConcurrentLinkedDeque fixed-size, evicting old entries when full.
     */
    private class ConcurrentLinkedEvictingDeque<E> extends ConcurrentLinkedDeque<E> {
        private int mSize;

        ConcurrentLinkedEvictingDeque(int size) {
            mSize = size;
        }

        @Override
        public boolean add(E elem) {
            synchronized (this) {
                if (size() == mSize) {
                    poll();
                }

                return super.add(elem);
            }
        }
    }

    /**
     * A container class to store a record of ContextHubClient registration.
     */
+17 −8
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import android.hardware.location.NanoAppInstanceInfo;
import android.hardware.location.NanoAppMessage;
import android.hardware.location.NanoAppState;
import android.location.LocationManager;
import android.os.Binder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.UserHandle;
@@ -386,7 +387,7 @@ public class ContextHubService extends IContextHubService.Stub {
                createLoadTransactionCallback(contextHubHandle, nanoAppBinary);

        ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction(
                contextHubHandle, nanoAppBinary, onCompleteCallback);
                contextHubHandle, nanoAppBinary, onCompleteCallback, getCallingPackageName());

        mTransactionManager.addTransaction(transaction);
        return 0;
@@ -411,7 +412,7 @@ public class ContextHubService extends IContextHubService.Stub {
        IContextHubTransactionCallback onCompleteCallback =
                createUnloadTransactionCallback(contextHubId);
        ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction(
                contextHubId, nanoAppId, onCompleteCallback);
                contextHubId, nanoAppId, onCompleteCallback, getCallingPackageName());

        mTransactionManager.addTransaction(transaction);
        return 0;
@@ -464,7 +465,7 @@ public class ContextHubService extends IContextHubService.Stub {
        IContextHubTransactionCallback onCompleteCallback =
                createQueryTransactionCallback(contextHubId);
        ContextHubServiceTransaction transaction = mTransactionManager.createQueryTransaction(
                contextHubId, onCompleteCallback);
                contextHubId, onCompleteCallback, getCallingPackageName());

        mTransactionManager.addTransaction(transaction);
        return Result.OK;
@@ -696,7 +697,7 @@ public class ContextHubService extends IContextHubService.Stub {
        }

        ContextHubServiceTransaction transaction = mTransactionManager.createLoadTransaction(
                contextHubId, nanoAppBinary, transactionCallback);
                contextHubId, nanoAppBinary, transactionCallback, getCallingPackageName());
        mTransactionManager.addTransaction(transaction);
    }

@@ -720,7 +721,7 @@ public class ContextHubService extends IContextHubService.Stub {
        }

        ContextHubServiceTransaction transaction = mTransactionManager.createUnloadTransaction(
                contextHubId, nanoAppId, transactionCallback);
                contextHubId, nanoAppId, transactionCallback, getCallingPackageName());
        mTransactionManager.addTransaction(transaction);
    }

@@ -744,7 +745,7 @@ public class ContextHubService extends IContextHubService.Stub {
        }

        ContextHubServiceTransaction transaction = mTransactionManager.createEnableTransaction(
                contextHubId, nanoAppId, transactionCallback);
                contextHubId, nanoAppId, transactionCallback, getCallingPackageName());
        mTransactionManager.addTransaction(transaction);
    }

@@ -768,7 +769,7 @@ public class ContextHubService extends IContextHubService.Stub {
        }

        ContextHubServiceTransaction transaction = mTransactionManager.createDisableTransaction(
                contextHubId, nanoAppId, transactionCallback);
                contextHubId, nanoAppId, transactionCallback, getCallingPackageName());
        mTransactionManager.addTransaction(transaction);
    }

@@ -790,7 +791,7 @@ public class ContextHubService extends IContextHubService.Stub {
        }

        ContextHubServiceTransaction transaction = mTransactionManager.createQueryTransaction(
                contextHubId, transactionCallback);
                contextHubId, transactionCallback, getCallingPackageName());
        mTransactionManager.addTransaction(transaction);
    }

@@ -822,6 +823,10 @@ public class ContextHubService extends IContextHubService.Stub {
        pw.println("=================== CLIENTS ====================");
        pw.println(mClientManager);

        pw.println("");
        pw.println("=================== TRANSACTIONS ====================");
        pw.println(mTransactionManager);

        // dump eventLog
    }

@@ -924,4 +929,8 @@ public class ContextHubService extends IContextHubService.Stub {
        mContextHubWrapper.onSettingChanged(Setting.LOCATION,
                enabled ? SettingValue.ENABLED : SettingValue.DISABLED);
    }

    private String getCallingPackageName() {
        return mContext.getPackageManager().getNameForUid(Binder.getCallingUid());
    }
}
+27 −3
Original line number Diff line number Diff line
@@ -32,14 +32,32 @@ import java.util.concurrent.TimeUnit;
    @ContextHubTransaction.Type
    private final int mTransactionType;

    /* The ID of the nanoapp this transaction is targeted for, null if not applicable. */
    private final Long mNanoAppId;

    /*
     * The host package associated with this transaction.
     */
    private final String mPackage;

    /*
     * true if the transaction has already completed, false otherwise
     */
    private boolean mIsComplete = false;

    /* package */ ContextHubServiceTransaction(int id, int type) {
    /* package */ ContextHubServiceTransaction(int id, int type, String packageName) {
        mTransactionId = id;
        mTransactionType = type;
        mNanoAppId = null;
        mPackage = packageName;
    }

    /* package */ ContextHubServiceTransaction(int id, int type, long nanoAppId,
            String packageName) {
        mTransactionId = id;
        mTransactionType = type;
        mNanoAppId = nanoAppId;
        mPackage = packageName;
    }

    /**
@@ -129,7 +147,13 @@ import java.util.concurrent.TimeUnit;

    @Override
    public String toString() {
        return ContextHubTransaction.typeToString(mTransactionType, true /* upperCase */)
                + " transaction (ID = " + mTransactionId + ")";
        String out = ContextHubTransaction.typeToString(mTransactionType, true /* upperCase */)
                + " (";
        if (mNanoAppId != null) {
            out += "appId = 0x" + Long.toHexString(mNanoAppId) + ", ";
        }
        out += "package = " + mPackage + ")";

        return out;
    }
}
+87 −22
Original line number Diff line number Diff line
@@ -26,11 +26,15 @@ import android.hardware.location.NanoAppState;
import android.os.RemoteException;
import android.util.Log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@@ -52,6 +56,11 @@ import java.util.concurrent.atomic.AtomicInteger;
     */
    private static final int MAX_PENDING_REQUESTS = 10000;

    /*
     * The DateFormat for printing TransactionRecord.
     */
    private static final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd HH:mm:ss.SSS");

    /*
     * The proxy to talk to the Context Hub
     */
@@ -83,6 +92,33 @@ import java.util.concurrent.atomic.AtomicInteger;
    private final ScheduledThreadPoolExecutor mTimeoutExecutor = new ScheduledThreadPoolExecutor(1);
    private ScheduledFuture<?> mTimeoutFuture = null;

    /*
     * The list of previous transaction records.
     */
    private static final int NUM_TRANSACTION_RECORDS = 20;
    private final ConcurrentLinkedEvictingDeque<TransactionRecord> mTransactionRecordDeque =
            new ConcurrentLinkedEvictingDeque<>(NUM_TRANSACTION_RECORDS);

    /**
     * A container class to store a record of transactions.
     */
    private class TransactionRecord {
        private final String mTransaction;
        private final long mTimestamp;

        TransactionRecord(String transaction) {
            mTransaction = transaction;
            mTimestamp = System.currentTimeMillis();
        }

        // TODO: Add dump to proto here

        @Override
        public String toString() {
            return DATE_FORMAT.format(new Date(mTimestamp)) + " " + mTransaction;
        }
    }

    /* package */ ContextHubTransactionManager(
            IContexthub contextHubProxy, ContextHubClientManager clientManager,
            NanoAppStateManager nanoAppStateManager) {
@@ -101,9 +137,10 @@ import java.util.concurrent.atomic.AtomicInteger;
     */
    /* package */ ContextHubServiceTransaction createLoadTransaction(
            int contextHubId, NanoAppBinary nanoAppBinary,
            IContextHubTransactionCallback onCompleteCallback) {
            IContextHubTransactionCallback onCompleteCallback, String packageName) {
        return new ContextHubServiceTransaction(
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_LOAD_NANOAPP) {
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_LOAD_NANOAPP,
                nanoAppBinary.getNanoAppId(), packageName) {
            @Override
                /* package */ int onTransact() {
                android.hardware.contexthub.V1_0.NanoAppBinary hidlNanoAppBinary =
@@ -149,9 +186,11 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @return the generated transaction
     */
    /* package */ ContextHubServiceTransaction createUnloadTransaction(
            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback) {
            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback,
            String packageName) {
        return new ContextHubServiceTransaction(
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_UNLOAD_NANOAPP) {
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_UNLOAD_NANOAPP,
                nanoAppId, packageName) {
            @Override
                /* package */ int onTransact() {
                try {
@@ -190,9 +229,11 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @return the generated transaction
     */
    /* package */ ContextHubServiceTransaction createEnableTransaction(
            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback) {
            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback,
            String packageName) {
        return new ContextHubServiceTransaction(
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_ENABLE_NANOAPP) {
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_ENABLE_NANOAPP,
                packageName) {
            @Override
                /* package */ int onTransact() {
                try {
@@ -225,9 +266,11 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @return the generated transaction
     */
    /* package */ ContextHubServiceTransaction createDisableTransaction(
            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback) {
            int contextHubId, long nanoAppId, IContextHubTransactionCallback onCompleteCallback,
            String packageName) {
        return new ContextHubServiceTransaction(
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_DISABLE_NANOAPP) {
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_DISABLE_NANOAPP,
                packageName) {
            @Override
                /* package */ int onTransact() {
                try {
@@ -259,9 +302,11 @@ import java.util.concurrent.atomic.AtomicInteger;
     * @return the generated transaction
     */
    /* package */ ContextHubServiceTransaction createQueryTransaction(
            int contextHubId, IContextHubTransactionCallback onCompleteCallback) {
            int contextHubId, IContextHubTransactionCallback onCompleteCallback,
            String packageName) {
        return new ContextHubServiceTransaction(
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_QUERY_NANOAPPS) {
                mNextAvailableId.getAndIncrement(), ContextHubTransaction.TYPE_QUERY_NANOAPPS,
                packageName) {
            @Override
                /* package */ int onTransact() {
                try {
@@ -307,6 +352,7 @@ import java.util.concurrent.atomic.AtomicInteger;
                    + MAX_PENDING_REQUESTS + ")");
        }
        mTransactionQueue.add(transaction);
        mTransactionRecordDeque.add(new TransactionRecord(transaction.toString()));

        if (mTransactionQueue.size() == 1) {
            startNextTransaction();
@@ -433,4 +479,23 @@ import java.util.concurrent.atomic.AtomicInteger;
            }
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(100);
        TransactionRecord[] arr;
        synchronized (this) {
            arr = mTransactionQueue.toArray(new TransactionRecord[0]);
        }
        for (int i = 0; i < arr.length; i++) {
            sb.append(i + ": " + arr[i] + "\n");
        }

        sb.append("Transaction History:\n");
        Iterator<TransactionRecord> iterator = mTransactionRecordDeque.descendingIterator();
        while (iterator.hasNext()) {
            sb.append(iterator.next() + "\n");
        }
        return sb.toString();
    }
}