1 /* 2 * Copyright (C) 2024 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.os; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SystemApi; 23 import android.annotation.SystemApi.Client; 24 import android.os.profiling.Flags; 25 import android.util.Log; 26 27 import com.android.internal.annotations.GuardedBy; 28 29 /** 30 * Class for system to interact with {@link ProfilingService} to notify of trigger occurrences. 31 * 32 * @hide 33 */ 34 @FlaggedApi(Flags.FLAG_SYSTEM_TRIGGERED_PROFILING_NEW) 35 @SystemApi(client = Client.MODULE_LIBRARIES) 36 public class ProfilingServiceHelper { 37 private static final String TAG = ProfilingServiceHelper.class.getSimpleName(); 38 private static final boolean DEBUG = false; 39 40 private static final Object sLock = new Object(); 41 42 @Nullable 43 @GuardedBy("sLock") 44 private static ProfilingServiceHelper sInstance; 45 46 private final Object mLock = new Object(); 47 48 @NonNull 49 @GuardedBy("mLock") 50 private final IProfilingService mProfilingService; 51 ProfilingServiceHelper(@onNull IProfilingService service)52 private ProfilingServiceHelper(@NonNull IProfilingService service) { 53 mProfilingService = service; 54 } 55 56 /** 57 * Returns an instance of {@link ProfilingServiceHelper}. 58 * 59 * @throws IllegalStateException if called before ProfilingService is set up. 60 */ 61 @NonNull getInstance()62 public static ProfilingServiceHelper getInstance() { 63 synchronized (sLock) { 64 if (sInstance != null) { 65 return sInstance; 66 } 67 68 IProfilingService service = Flags.telemetryApis() ? IProfilingService.Stub.asInterface( 69 ProfilingFrameworkInitializer.getProfilingServiceManager() 70 .getProfilingServiceRegisterer().get()) : null; 71 72 if (service == null) { 73 throw new IllegalStateException("ProfilingService not yet set up."); 74 } 75 76 sInstance = new ProfilingServiceHelper(service); 77 78 return sInstance; 79 } 80 } 81 82 /** Send a trigger to {@link ProfilingService}. */ onProfilingTriggerOccurred(int uid, @NonNull String packageName, int triggerType)83 public void onProfilingTriggerOccurred(int uid, @NonNull String packageName, int triggerType) { 84 synchronized (mLock) { 85 try { 86 mProfilingService.processTrigger(uid, packageName, triggerType); 87 } catch (RemoteException e) { 88 // Exception sending trigger to service. Nothing to do here, trigger will be lost. 89 if (DEBUG) Log.e(TAG, "Exception sending trigger", e); 90 } 91 } 92 } 93 } 94