David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | package android.util; |
| 17 | |
| 18 | import android.Manifest; |
| 19 | import android.annotation.RequiresPermission; |
| 20 | import android.annotation.SystemApi; |
| 21 | import android.os.IBinder; |
| 22 | import android.os.IStatsManager; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
| 25 | |
| 26 | /** |
| 27 | * API for StatsD clients to send configurations and retrieve data. |
| 28 | * |
| 29 | * @hide |
| 30 | */ |
| 31 | @SystemApi |
| 32 | public final class StatsManager { |
| 33 | IStatsManager mService; |
| 34 | private static final String TAG = "StatsManager"; |
| 35 | |
| 36 | /** |
| 37 | * Constructor for StatsManagerClient. |
| 38 | * |
| 39 | * @hide |
| 40 | */ |
| 41 | public StatsManager() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Clients can send a configuration and simultaneously registers the name of a broadcast |
| 46 | * receiver that listens for when it should request data. |
| 47 | * |
| 48 | * @param configKey An arbitrary string that allows clients to track the configuration. |
| 49 | * @param config Wire-encoded StatsDConfig proto that specifies metrics (and all |
| 50 | * dependencies eg, conditions and matchers). |
| 51 | * @param pkg The package name to receive the broadcast. |
| 52 | * @param cls The name of the class that receives the broadcast. |
| 53 | * @return true if successful |
| 54 | */ |
| 55 | @RequiresPermission(Manifest.permission.DUMP) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 56 | public boolean addConfiguration(long configKey, byte[] config, String pkg, String cls) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 57 | synchronized (this) { |
| 58 | try { |
| 59 | IStatsManager service = getIStatsManagerLocked(); |
| 60 | if (service == null) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 61 | Slog.d(TAG, "Failed to find statsd when adding configuration"); |
| 62 | return false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 63 | } |
| 64 | return service.addConfiguration(configKey, config, pkg, cls); |
| 65 | } catch (RemoteException e) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 66 | Slog.d(TAG, "Failed to connect to statsd when adding configuration"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Remove a configuration from logging. |
| 74 | * |
| 75 | * @param configKey Configuration key to remove. |
| 76 | * @return true if successful |
| 77 | */ |
| 78 | @RequiresPermission(Manifest.permission.DUMP) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 79 | public boolean removeConfiguration(long configKey) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 80 | synchronized (this) { |
| 81 | try { |
| 82 | IStatsManager service = getIStatsManagerLocked(); |
| 83 | if (service == null) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 84 | Slog.d(TAG, "Failed to find statsd when removing configuration"); |
| 85 | return false; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 86 | } |
| 87 | return service.removeConfiguration(configKey); |
| 88 | } catch (RemoteException e) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 89 | Slog.d(TAG, "Failed to connect to statsd when removing configuration"); |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 96 | * Clients can request data with a binder call. This getter is destructive and also clears |
| 97 | * the retrieved metrics from statsd memory. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 98 | * |
| 99 | * @param configKey Configuration key to retrieve data from. |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 100 | * @return Serialized ConfigMetricsReportList proto. Returns null on failure. |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 101 | */ |
| 102 | @RequiresPermission(Manifest.permission.DUMP) |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame^] | 103 | public byte[] getData(long configKey) { |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 104 | synchronized (this) { |
| 105 | try { |
| 106 | IStatsManager service = getIStatsManagerLocked(); |
| 107 | if (service == null) { |
David Chen | c562bfb | 2017-11-17 17:44:33 -0800 | [diff] [blame] | 108 | Slog.d(TAG, "Failed to find statsd when getting data"); |
| 109 | return null; |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 110 | } |
| 111 | return service.getData(configKey); |
| 112 | } catch (RemoteException e) { |
| 113 | Slog.d(TAG, "Failed to connecto statsd when getting data"); |
| 114 | return null; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
David Chen | 2e8f380 | 2017-11-22 10:56:48 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Clients can request metadata for statsd. Will contain stats across all configurations but not |
| 121 | * the actual metrics themselves (metrics must be collected via {@link #getData(String)}. |
| 122 | * This getter is not destructive and will not reset any metrics/counters. |
| 123 | * |
| 124 | * @return Serialized StatsdStatsReport proto. Returns null on failure. |
| 125 | */ |
| 126 | @RequiresPermission(Manifest.permission.DUMP) |
| 127 | public byte[] getMetadata() { |
| 128 | synchronized (this) { |
| 129 | try { |
| 130 | IStatsManager service = getIStatsManagerLocked(); |
| 131 | if (service == null) { |
| 132 | Slog.d(TAG, "Failed to find statsd when getting metadata"); |
| 133 | return null; |
| 134 | } |
| 135 | return service.getMetadata(); |
| 136 | } catch (RemoteException e) { |
| 137 | Slog.d(TAG, "Failed to connecto statsd when getting metadata"); |
| 138 | return null; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
David Chen | adaf8b3 | 2017-11-03 15:42:08 -0700 | [diff] [blame] | 143 | private class StatsdDeathRecipient implements IBinder.DeathRecipient { |
| 144 | @Override |
| 145 | public void binderDied() { |
| 146 | synchronized (this) { |
| 147 | mService = null; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private IStatsManager getIStatsManagerLocked() throws RemoteException { |
| 153 | if (mService != null) { |
| 154 | return mService; |
| 155 | } |
| 156 | mService = IStatsManager.Stub.asInterface(ServiceManager.getService("stats")); |
| 157 | if (mService != null) { |
| 158 | mService.asBinder().linkToDeath(new StatsdDeathRecipient(), 0); |
| 159 | } |
| 160 | return mService; |
| 161 | } |
| 162 | } |