1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.tracing.perfetto; 18 19 import com.android.internal.annotations.VisibleForTesting; 20 21 /** 22 * @hide 23 */ 24 public abstract class DataSourceInstance implements AutoCloseable { 25 private final DataSource mDataSource; 26 private final int mInstanceIndex; 27 DataSourceInstance(DataSource dataSource, int instanceIndex)28 public DataSourceInstance(DataSource dataSource, int instanceIndex) { 29 this.mDataSource = dataSource; 30 this.mInstanceIndex = instanceIndex; 31 } 32 33 /** 34 * Executed when the tracing instance starts running. 35 * <p> 36 * NOTE: This callback executes on the Perfetto internal thread and is blocking. 37 * Anything that is run in this callback should execute quickly. 38 * 39 * @param args Start arguments. 40 */ onStart(StartCallbackArguments args)41 protected void onStart(StartCallbackArguments args) {} 42 43 /** 44 * Executed when a flush is triggered. 45 * <p> 46 * NOTE: This callback executes on the Perfetto internal thread and is blocking. 47 * Anything that is run in this callback should execute quickly. 48 * @param args Flush arguments. 49 */ onFlush(FlushCallbackArguments args)50 protected void onFlush(FlushCallbackArguments args) {} 51 52 /** 53 * Executed when the tracing instance is stopped. 54 * <p> 55 * NOTE: This callback executes on the Perfetto internal thread and is blocking. 56 * Anything that is run in this callback should execute quickly. 57 * @param args Stop arguments. 58 */ onStop(StopCallbackArguments args)59 protected void onStop(StopCallbackArguments args) {} 60 61 @Override close()62 public final void close() { 63 this.release(); 64 } 65 66 /** 67 * Release the lock on the datasource once you are finished using it. 68 * Only required to be called when instance was retrieved with 69 * `DataSource#getDataSourceInstanceLocked`. 70 */ 71 @VisibleForTesting release()72 public void release() { 73 mDataSource.releaseDataSourceInstance(mInstanceIndex); 74 } 75 getInstanceIndex()76 public final int getInstanceIndex() { 77 return mInstanceIndex; 78 } 79 } 80