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

Commit 1aaf0110 authored by Kean Mariotti's avatar Kean Mariotti
Browse files

Extend DataSourceParams

- Add parameter willNotifyOnStop
- Add parameter noFlush
- Add DataSourceParams.Builder
- Set willNotifyOnStop=false and noFlush=true for
  IME tracing to avoid long timeouts when a tracing
  session ends and a producer is frozen (background app).

Bug: 276433199
Test: atest FrameworksCoreTests:android.tracing.perfetto.DataSourceTest
Change-Id: I8fc0de001956955431a5c891c7e5e4fd8b5291dc
parent 9d6d954f
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -130,7 +130,8 @@ public abstract class DataSource<DataSourceInstanceType extends DataSourceInstan
     * @param params Params to initialize the datasource with.
     */
    public void register(DataSourceParams params) {
        nativeRegisterDataSource(this.mNativeObj, params.bufferExhaustedPolicy);
        nativeRegisterDataSource(this.mNativeObj, params.bufferExhaustedPolicy,
                params.willNotifyOnStop, params.noFlush);
    }

    /**
@@ -163,8 +164,8 @@ public abstract class DataSource<DataSourceInstanceType extends DataSourceInstan
        return this.createInstance(inputStream, instanceIndex);
    }

    private static native void nativeRegisterDataSource(
            long dataSourcePtr, int bufferExhaustedPolicy);
    private static native void nativeRegisterDataSource(long dataSourcePtr,
            int bufferExhaustedPolicy, boolean willNotifyOnStop, boolean noFlush);

    private static native long nativeCreate(DataSource thiz, String name);
    private static native void nativeFlushAll(long nativeDataSourcePointer);
+58 −3
Original line number Diff line number Diff line
@@ -46,12 +46,67 @@ public class DataSourceParams {
    // after a while.
    public static final int PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_STALL_AND_ABORT = 1;

    public static DataSourceParams DEFAULTS =
            new DataSourceParams(PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_DROP);
    public static DataSourceParams DEFAULTS = new DataSourceParams.Builder().build();

    public DataSourceParams(@PerfettoDsBufferExhausted int bufferExhaustedPolicy) {
    private DataSourceParams(@PerfettoDsBufferExhausted int bufferExhaustedPolicy,
            boolean willNotifyOnStop, boolean noFlush) {
        this.bufferExhaustedPolicy = bufferExhaustedPolicy;
        this.willNotifyOnStop = willNotifyOnStop;
        this.noFlush = noFlush;
    }

    public final @PerfettoDsBufferExhausted int bufferExhaustedPolicy;
    public final boolean willNotifyOnStop;
    public final boolean noFlush;

    /**
     * DataSource Parameters builder
     *
     * @hide
     */
    public static final class Builder {
        /**
         * Specify behavior when running out of shared memory buffer space.
         */
        public Builder setBufferExhaustedPolicy(@PerfettoDsBufferExhausted int value) {
            this.mBufferExhaustedPolicy = value;
            return this;
        }

        /**
         * If true, the data source is expected to ack the stop request through the
         * NotifyDataSourceStopped() IPC. If false, the service won't wait for an ack.
         * Set this parameter to false when dealing with potentially frozen producers
         * that wouldn't be able to quickly ack the stop request.
         *
         * Default value: true
         */
        public Builder setWillNotifyOnStop(boolean value) {
            this.mWillNotifyOnStop = value;
            return this;
        }

        /**
         * If true, the service won't emit flush requests for this data source. This
         * allows the service to reduce the flush-related IPC traffic and better deal
         * with frozen producers (see go/perfetto-frozen).
         */
        public Builder setNoFlush(boolean value) {
            this.mNoFlush = value;
            return this;
        }

        /**
         * Build the DataSource parameters.
         */
        public DataSourceParams build() {
            return new DataSourceParams(
                    this.mBufferExhaustedPolicy, this.mWillNotifyOnStop, this.mNoFlush);
        }

        private @PerfettoDsBufferExhausted int mBufferExhaustedPolicy =
                PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_DROP;
        private boolean mWillNotifyOnStop = true;
        private boolean mNoFlush = false;
    }
}
+8 −2
Original line number Diff line number Diff line
@@ -52,8 +52,14 @@ final class ImeTracingPerfettoImpl extends ImeTracing {

    ImeTracingPerfettoImpl() {
        Producer.init(InitArguments.DEFAULTS);
        mDataSource.register(
                new DataSourceParams(PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_STALL_AND_ABORT));
        DataSourceParams params =
                new DataSourceParams.Builder()
                        .setBufferExhaustedPolicy(
                                PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_STALL_AND_ABORT)
                        .setNoFlush(true)
                        .setWillNotifyOnStop(false)
                        .build();
        mDataSource.register(params);
    }


+7 −2
Original line number Diff line number Diff line
@@ -131,8 +131,13 @@ public class PerfettoProtoLogImpl implements IProtoLog {
            Runnable cacheUpdater
    ) {
        Producer.init(InitArguments.DEFAULTS);
        mDataSource.register(new DataSourceParams(
                DataSourceParams.PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_STALL_AND_ABORT));
        DataSourceParams params =
                new DataSourceParams.Builder()
                        .setBufferExhaustedPolicy(
                                DataSourceParams
                                        .PERFETTO_DS_BUFFER_EXHAUSTED_POLICY_STALL_AND_ABORT)
                        .build();
        mDataSource.register(params);
        this.mViewerConfigInputStreamProvider = viewerConfigInputStreamProvider;
        this.mViewerConfigReader = viewerConfigReader;
        this.mLogGroups = logGroups;
+12 −8
Original line number Diff line number Diff line
@@ -256,10 +256,12 @@ void nativeFlushAll(JNIEnv* env, jclass clazz, jlong ptr) {
}

void nativeRegisterDataSource(JNIEnv* env, jclass clazz, jlong datasource_ptr,
                              jint buffer_exhausted_policy) {
                              jint buffer_exhausted_policy, jboolean will_notify_on_stop,
                              jboolean no_flush) {
    sp<PerfettoDataSource> datasource = reinterpret_cast<PerfettoDataSource*>(datasource_ptr);

    struct PerfettoDsParams params = PerfettoDsParamsDefault();
    params.will_notify_on_stop = will_notify_on_stop;
    params.buffer_exhausted_policy = (PerfettoDsBufferExhaustedPolicy)buffer_exhausted_policy;

    params.user_arg = reinterpret_cast<void*>(datasource.get());
@@ -325,13 +327,15 @@ void nativeRegisterDataSource(JNIEnv* env, jclass clazz, jlong datasource_ptr,
        datasource_instance->onStart(env);
    };

    params.on_flush_cb = [](struct PerfettoDsImpl*, PerfettoDsInstanceIndex, void*, void* inst_ctx,
                            struct PerfettoDsOnFlushArgs*) {
    if (!no_flush) {
        params.on_flush_cb = [](struct PerfettoDsImpl*, PerfettoDsInstanceIndex, void*,
                                void* inst_ctx, struct PerfettoDsOnFlushArgs*) {
            JNIEnv* env = GetOrAttachJNIEnvironment(gVm, JNI_VERSION_1_6);

            auto* datasource_instance = static_cast<PerfettoDataSourceInstance*>(inst_ctx);
            datasource_instance->onFlush(env);
        };
    }

    params.on_stop_cb = [](struct PerfettoDsImpl*, PerfettoDsInstanceIndex inst_id, void* user_arg,
                           void* inst_ctx, struct PerfettoDsOnStopArgs*) {
@@ -422,7 +426,7 @@ const JNINativeMethod gMethods[] = {
         (void*)nativeCreate},
        {"nativeFlushAll", "(J)V", (void*)nativeFlushAll},
        {"nativeGetFinalizer", "()J", (void*)nativeGetFinalizer},
        {"nativeRegisterDataSource", "(JI)V", (void*)nativeRegisterDataSource},
        {"nativeRegisterDataSource", "(JIZZ)V", (void*)nativeRegisterDataSource},
        {"nativeGetPerfettoInstanceLocked", "(JI)Landroid/tracing/perfetto/DataSourceInstance;",
         (void*)nativeGetPerfettoInstanceLocked},
        {"nativeReleasePerfettoInstanceLocked", "(JI)V",
Loading