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

Commit 4bc790d2 authored by Felipe Leme's avatar Felipe Leme
Browse files

Integrate pooled lambda with systrace.

Looper traces handler calls by calling Handler.getTraceName(), which when used with PooledLambda
ends up showing just:

"android.os.Handler: com.android.internal.util.function.pooled.PooledLambdaImpl"

That name doesn't help understand what's the real bottleneck, and even worse, could let people
think it's the PooledLambda who's causing it.

Which this change, it will display names like:

com.android.server.appop.AppOpsService$Lambda
com.android.server.wm$Lambda

Fixes: 141172840
Test: manual verification (neither PoolLambda nor Message have unit tests)

Change-Id: I80d2158a50a644b10f3724fb42a4a9c2aee63ae9
parent a5678698
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -295,6 +295,10 @@ public class Handler {
    /** {@hide} */
    @NonNull
    public String getTraceName(@NonNull Message message) {
        if (message.callback instanceof TraceNameSupplier) {
            return ((TraceNameSupplier) message.callback).getTraceName();
        }

        final StringBuilder sb = new StringBuilder();
        sb.append(getClass().getName()).append(": ");
        if (message.callback != null) {
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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 android.os;

import android.annotation.NonNull;

/**
 * Supplier for custom trace messages.
 *
 * @hide
 */
public interface TraceNameSupplier {

    /**
     * Gets the name used for trace messages.
     */
    @NonNull String getTraceName();
}
+36 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.util.function.pooled;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.Message;
import android.text.TextUtils;
@@ -527,6 +528,36 @@ final class PooledLambdaImpl<R> extends OmniFunction<Object,
        return r;
    }

    // TODO: add unit test
    @NonNull
    private static String getFriendlyName(@NonNull Object function) {
        // Full function has one of the following formats:
        //   package-$$Lambda$class$randomId
        //   package-$$Lambda$randomId
        //
        // We just want just package.class$Lambda (or package$Lambda) respectively

        final String fullFunction = function.toString();

        final int endPkgIdx = fullFunction.indexOf("-$$");
        if (endPkgIdx == -1) return fullFunction;

        // firstDollarIdx could be either beginning of class or beginning of the random id
        final int firstDollarIdx = fullFunction.indexOf('$', endPkgIdx + 3);
        if (firstDollarIdx == -1) return fullFunction;

        final int endClassIdx = fullFunction.indexOf('$', firstDollarIdx + 1);
        if (endClassIdx == -1) {
            // Just package
            return fullFunction.substring(0, endPkgIdx - 1) + "$Lambda";
        }

        // Package + class
        return fullFunction.substring(0, endPkgIdx)
                + fullFunction.substring(firstDollarIdx + 1, endClassIdx)
                + "$Lambda";
    }

    private static void setIfInBounds(Object[] array, int i, Object a) {
        if (i < ArrayUtils.size(array)) array[i] = a;
    }
@@ -566,6 +597,11 @@ final class PooledLambdaImpl<R> extends OmniFunction<Object,
        return this;
    }

    @Override
    public String getTraceName() {
        return getFriendlyName(mFunc);
    }

    private boolean isRecycled() {
        return (mFlags & FLAG_RECYCLED) != 0;
    }
+4 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.internal.util.function.pooled;

import android.os.TraceNameSupplier;

import com.android.internal.util.FunctionalUtils.ThrowingRunnable;

/**
@@ -24,7 +26,8 @@ import com.android.internal.util.FunctionalUtils.ThrowingRunnable;
 * @see PooledLambda
 * @hide
 */
public interface PooledRunnable extends PooledLambda, Runnable, ThrowingRunnable {
public interface PooledRunnable
        extends PooledLambda, Runnable, ThrowingRunnable, TraceNameSupplier {
    /** @inheritDoc */
    PooledRunnable recycleOnUse();
}