| /* |
| * Copyright (C) 2018 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; |
| |
| import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_AWARE; |
| import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
| |
| import android.app.AppGlobals; |
| import android.content.Context; |
| import android.content.pm.PackageInfo; |
| import android.content.pm.PackageManager; |
| import android.content.pm.PackageManager.NameNotFoundException; |
| import android.database.ContentObserver; |
| import android.net.Uri; |
| import android.os.Binder; |
| import android.os.Process; |
| import android.os.RemoteException; |
| import android.os.SystemProperties; |
| import android.os.ThreadLocalWorkSource; |
| import android.os.UserHandle; |
| import android.provider.Settings; |
| import android.util.ArrayMap; |
| import android.util.ArraySet; |
| import android.util.KeyValueListParser; |
| import android.util.Slog; |
| |
| import com.android.internal.annotations.VisibleForTesting; |
| import com.android.internal.os.BackgroundThread; |
| import com.android.internal.os.BinderCallsStats; |
| import com.android.internal.os.BinderInternal; |
| import com.android.internal.os.BinderInternal.CallSession; |
| import com.android.internal.os.CachedDeviceState; |
| |
| import java.io.FileDescriptor; |
| import java.io.PrintWriter; |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| public class BinderCallsStatsService extends Binder { |
| |
| private static final String TAG = "BinderCallsStatsService"; |
| |
| private static final String PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING |
| = "persist.sys.binder_calls_detailed_tracking"; |
| |
| /** Resolves the work source of an incoming binder transaction. */ |
| static class WorkSourceProvider { |
| private ArraySet<Integer> mAppIdWhitelist; |
| |
| WorkSourceProvider() { |
| mAppIdWhitelist = new ArraySet<>(); |
| } |
| |
| public int resolveWorkSourceUid() { |
| final int callingUid = getCallingUid(); |
| final int appId = UserHandle.getAppId(callingUid); |
| if (mAppIdWhitelist.contains(appId)) { |
| final int workSource = getCallingWorkSourceUid(); |
| final boolean isWorkSourceSet = workSource != Binder.UNSET_WORKSOURCE; |
| return isWorkSourceSet ? workSource : callingUid; |
| } |
| return callingUid; |
| } |
| |
| public void systemReady(Context context) { |
| mAppIdWhitelist = createAppidWhitelist(context); |
| } |
| |
| public void dump(PrintWriter pw, Map<Integer, String> appIdToPackageName) { |
| pw.println("AppIds of apps that can set the work source:"); |
| final ArraySet<Integer> whitelist = mAppIdWhitelist; |
| for (Integer appId : whitelist) { |
| pw.println("\t- " + appIdToPackageName.getOrDefault(appId, String.valueOf(appId))); |
| } |
| } |
| |
| protected int getCallingUid() { |
| return Binder.getCallingUid(); |
| } |
| |
| protected int getCallingWorkSourceUid() { |
| return Binder.getCallingWorkSourceUid(); |
| } |
| |
| private ArraySet<Integer> createAppidWhitelist(Context context) { |
| // Use a local copy instead of mAppIdWhitelist to prevent concurrent read access. |
| final ArraySet<Integer> whitelist = new ArraySet<>(); |
| |
| // We trust our own process. |
| whitelist.add(Process.myUid()); |
| // We only need to initialize it once. UPDATE_DEVICE_STATS is a system permission. |
| final PackageManager pm = context.getPackageManager(); |
| final String[] permissions = { android.Manifest.permission.UPDATE_DEVICE_STATS }; |
| final int queryFlags = MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE; |
| final List<PackageInfo> packages = |
| pm.getPackagesHoldingPermissions(permissions, queryFlags); |
| final int packagesSize = packages.size(); |
| for (int i = 0; i < packagesSize; i++) { |
| final PackageInfo pkgInfo = packages.get(i); |
| try { |
| final int uid = pm.getPackageUid(pkgInfo.packageName, queryFlags); |
| final int appId = UserHandle.getAppId(uid); |
| whitelist.add(appId); |
| } catch (NameNotFoundException e) { |
| Slog.e(TAG, "Cannot find uid for package name " + pkgInfo.packageName, e); |
| } |
| } |
| return whitelist; |
| } |
| } |
| |
| /** Observer for all system server incoming binder transactions. */ |
| @VisibleForTesting |
| static class BinderCallsObserver implements BinderInternal.Observer { |
| private final BinderInternal.Observer mBinderCallsStats; |
| private final WorkSourceProvider mWorkSourceProvider; |
| |
| BinderCallsObserver(BinderInternal.Observer callsStats, |
| WorkSourceProvider workSourceProvider) { |
| mBinderCallsStats = callsStats; |
| mWorkSourceProvider = workSourceProvider; |
| } |
| |
| @Override |
| public CallSession callStarted(Binder binder, int code) { |
| // We depend on the code in Binder#execTransact to reset the state of |
| // ThreadLocalWorkSource |
| setThreadLocalWorkSourceUid(mWorkSourceProvider.resolveWorkSourceUid()); |
| return mBinderCallsStats.callStarted(binder, code); |
| } |
| |
| @Override |
| public void callEnded(CallSession s, int parcelRequestSize, int parcelReplySize) { |
| mBinderCallsStats.callEnded(s, parcelRequestSize, parcelReplySize); |
| } |
| |
| @Override |
| public void callThrewException(CallSession s, Exception exception) { |
| mBinderCallsStats.callThrewException(s, exception); |
| } |
| |
| protected void setThreadLocalWorkSourceUid(int uid) { |
| ThreadLocalWorkSource.setUid(uid); |
| } |
| } |
| |
| /** Listens for flag changes. */ |
| private static class SettingsObserver extends ContentObserver { |
| private static final String SETTINGS_ENABLED_KEY = "enabled"; |
| private static final String SETTINGS_DETAILED_TRACKING_KEY = "detailed_tracking"; |
| private static final String SETTINGS_UPLOAD_DATA_KEY = "upload_data"; |
| private static final String SETTINGS_SAMPLING_INTERVAL_KEY = "sampling_interval"; |
| private static final String SETTINGS_MAX_CALL_STATS_KEY = "max_call_stats_count"; |
| |
| private boolean mEnabled; |
| private final Uri mUri = Settings.Global.getUriFor(Settings.Global.BINDER_CALLS_STATS); |
| private final Context mContext; |
| private final KeyValueListParser mParser = new KeyValueListParser(','); |
| private final BinderCallsStats mBinderCallsStats; |
| private final BinderCallsObserver mBinderCallsObserver; |
| |
| SettingsObserver(Context context, BinderCallsStats binderCallsStats, |
| BinderCallsObserver observer) { |
| super(BackgroundThread.getHandler()); |
| mContext = context; |
| context.getContentResolver().registerContentObserver(mUri, false, this, |
| UserHandle.USER_SYSTEM); |
| mBinderCallsStats = binderCallsStats; |
| mBinderCallsObserver = observer; |
| // Always kick once to ensure that we match current state |
| onChange(); |
| } |
| |
| @Override |
| public void onChange(boolean selfChange, Uri uri, int userId) { |
| if (mUri.equals(uri)) { |
| onChange(); |
| } |
| } |
| |
| public void onChange() { |
| // Do not overwrite the default set manually. |
| if (!SystemProperties.get(PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING).isEmpty()) { |
| return; |
| } |
| |
| try { |
| mParser.setString(Settings.Global.getString(mContext.getContentResolver(), |
| Settings.Global.BINDER_CALLS_STATS)); |
| } catch (IllegalArgumentException e) { |
| Slog.e(TAG, "Bad binder call stats settings", e); |
| } |
| mBinderCallsStats.setDetailedTracking(mParser.getBoolean( |
| SETTINGS_DETAILED_TRACKING_KEY, BinderCallsStats.DETAILED_TRACKING_DEFAULT)); |
| mBinderCallsStats.setSamplingInterval(mParser.getInt( |
| SETTINGS_SAMPLING_INTERVAL_KEY, |
| BinderCallsStats.PERIODIC_SAMPLING_INTERVAL_DEFAULT)); |
| mBinderCallsStats.setMaxBinderCallStats(mParser.getInt( |
| SETTINGS_MAX_CALL_STATS_KEY, |
| BinderCallsStats.MAX_BINDER_CALL_STATS_COUNT_DEFAULT)); |
| |
| |
| final boolean enabled = |
| mParser.getBoolean(SETTINGS_ENABLED_KEY, BinderCallsStats.ENABLED_DEFAULT); |
| if (mEnabled != enabled) { |
| if (enabled) { |
| Binder.setObserver(mBinderCallsObserver); |
| Binder.setProxyTransactListener( |
| new Binder.PropagateWorkSourceTransactListener()); |
| } else { |
| Binder.setObserver(null); |
| Binder.setProxyTransactListener(null); |
| } |
| mEnabled = enabled; |
| mBinderCallsStats.reset(); |
| mBinderCallsStats.setAddDebugEntries(enabled); |
| } |
| } |
| } |
| |
| /** |
| * @hide Only for use within the system server. |
| */ |
| public static class Internal { |
| private final BinderCallsStats mBinderCallsStats; |
| |
| Internal(BinderCallsStats binderCallsStats) { |
| this.mBinderCallsStats = binderCallsStats; |
| } |
| |
| /** @see BinderCallsStats#reset */ |
| public void reset() { |
| mBinderCallsStats.reset(); |
| } |
| |
| /** |
| * @see BinderCallsStats#getExportedCallStats. |
| * |
| * Note that binder calls stats will be reset by statsd every time |
| * the data is exported. |
| */ |
| public ArrayList<BinderCallsStats.ExportedCallStat> getExportedCallStats() { |
| return mBinderCallsStats.getExportedCallStats(); |
| } |
| |
| /** @see BinderCallsStats#getExportedExceptionStats */ |
| public ArrayMap<String, Integer> getExportedExceptionStats() { |
| return mBinderCallsStats.getExportedExceptionStats(); |
| } |
| } |
| |
| public static class LifeCycle extends SystemService { |
| private BinderCallsStatsService mService; |
| private BinderCallsStats mBinderCallsStats; |
| private WorkSourceProvider mWorkSourceProvider; |
| |
| public LifeCycle(Context context) { |
| super(context); |
| } |
| |
| @Override |
| public void onStart() { |
| mBinderCallsStats = new BinderCallsStats(new BinderCallsStats.Injector()); |
| mWorkSourceProvider = new WorkSourceProvider(); |
| BinderCallsObserver binderCallsObserver = |
| new BinderCallsObserver(mBinderCallsStats, mWorkSourceProvider); |
| mService = new BinderCallsStatsService( |
| mBinderCallsStats, binderCallsObserver, mWorkSourceProvider); |
| publishLocalService(Internal.class, new Internal(mBinderCallsStats)); |
| publishBinderService("binder_calls_stats", mService); |
| boolean detailedTrackingEnabled = SystemProperties.getBoolean( |
| PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING, false); |
| |
| if (detailedTrackingEnabled) { |
| Slog.i(TAG, "Enabled CPU usage tracking for binder calls. Controlled by " |
| + PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING |
| + " or via dumpsys binder_calls_stats --enable-detailed-tracking"); |
| mBinderCallsStats.setDetailedTracking(true); |
| } |
| } |
| |
| @Override |
| public void onBootPhase(int phase) { |
| if (SystemService.PHASE_SYSTEM_SERVICES_READY == phase) { |
| CachedDeviceState.Readonly deviceState = getLocalService( |
| CachedDeviceState.Readonly.class); |
| mBinderCallsStats.setDeviceState(deviceState); |
| // It needs to be called before mService.systemReady to make sure the observer is |
| // initialized before installing it. |
| mWorkSourceProvider.systemReady(getContext()); |
| mService.systemReady(getContext()); |
| } |
| } |
| } |
| |
| private SettingsObserver mSettingsObserver; |
| private final BinderCallsStats mBinderCallsStats; |
| private final BinderCallsObserver mBinderCallsObserver; |
| private final WorkSourceProvider mWorkSourceProvider; |
| |
| BinderCallsStatsService(BinderCallsStats binderCallsStats, BinderCallsObserver observer, |
| WorkSourceProvider workSourceProvider) { |
| mBinderCallsStats = binderCallsStats; |
| mBinderCallsObserver = observer; |
| mWorkSourceProvider = workSourceProvider; |
| } |
| |
| public void systemReady(Context context) { |
| mSettingsObserver = new SettingsObserver(context, mBinderCallsStats, mBinderCallsObserver); |
| } |
| |
| public void reset() { |
| Slog.i(TAG, "Resetting stats"); |
| mBinderCallsStats.reset(); |
| } |
| |
| @Override |
| protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| boolean verbose = false; |
| if (args != null) { |
| for (final String arg : args) { |
| if ("-a".equals(arg)) { |
| verbose = true; |
| } else if ("--reset".equals(arg)) { |
| reset(); |
| pw.println("binder_calls_stats reset."); |
| return; |
| } else if ("--enable".equals(arg)) { |
| Binder.setObserver(mBinderCallsObserver); |
| return; |
| } else if ("--disable".equals(arg)) { |
| Binder.setObserver(null); |
| return; |
| } else if ("--no-sampling".equals(arg)) { |
| mBinderCallsStats.setSamplingInterval(1); |
| return; |
| } else if ("--enable-detailed-tracking".equals(arg)) { |
| SystemProperties.set(PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING, "1"); |
| mBinderCallsStats.setDetailedTracking(true); |
| pw.println("Detailed tracking enabled"); |
| return; |
| } else if ("--disable-detailed-tracking".equals(arg)) { |
| SystemProperties.set(PERSIST_SYS_BINDER_CALLS_DETAILED_TRACKING, ""); |
| mBinderCallsStats.setDetailedTracking(false); |
| pw.println("Detailed tracking disabled"); |
| return; |
| } else if ("--dump-worksource-provider".equals(arg)) { |
| mWorkSourceProvider.dump(pw, getAppIdToPackagesMap()); |
| return; |
| } else if ("-h".equals(arg)) { |
| pw.println("binder_calls_stats commands:"); |
| pw.println(" --reset: Reset stats"); |
| pw.println(" --enable: Enable tracking binder calls"); |
| pw.println(" --disable: Disables tracking binder calls"); |
| pw.println(" --no-sampling: Tracks all calls"); |
| pw.println(" --enable-detailed-tracking: Enables detailed tracking"); |
| pw.println(" --disable-detailed-tracking: Disables detailed tracking"); |
| return; |
| } else { |
| pw.println("Unknown option: " + arg); |
| } |
| } |
| } |
| mBinderCallsStats.dump(pw, getAppIdToPackagesMap(), verbose); |
| } |
| |
| private Map<Integer, String> getAppIdToPackagesMap() { |
| List<PackageInfo> packages; |
| try { |
| packages = AppGlobals.getPackageManager() |
| .getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES, |
| UserHandle.USER_SYSTEM).getList(); |
| } catch (RemoteException e) { |
| throw e.rethrowFromSystemServer(); |
| } |
| Map<Integer,String> map = new HashMap<>(); |
| for (PackageInfo pkg : packages) { |
| String name = pkg.packageName; |
| int uid = pkg.applicationInfo.uid; |
| // Use sharedUserId string as package name if there are collisions |
| if (pkg.sharedUserId != null && map.containsKey(uid)) { |
| name = "shared:" + pkg.sharedUserId; |
| } |
| map.put(uid, name); |
| } |
| return map; |
| } |
| } |