StatsCompanionService.java revision 330af58f2b8582b855085655fae553cdfaf44e6c
1/*
2 * Copyright (C) 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 */
16package com.android.server.stats;
17
18import android.annotation.Nullable;
19import android.app.AlarmManager;
20import android.app.PendingIntent;
21import android.app.StatsManager;
22import android.bluetooth.BluetoothActivityEnergyInfo;
23import android.bluetooth.BluetoothAdapter;
24import android.bluetooth.UidTraffic;
25import android.content.BroadcastReceiver;
26import android.content.Context;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.IntentSender;
30import android.content.pm.PackageInfo;
31import android.content.pm.PackageManager;
32import android.content.pm.UserInfo;
33import android.net.NetworkStats;
34import android.net.wifi.IWifiManager;
35import android.net.wifi.WifiActivityEnergyInfo;
36import android.os.BatteryStatsInternal;
37import android.os.Binder;
38import android.os.Bundle;
39import android.os.Environment;
40import android.os.IBinder;
41import android.os.IStatsCompanionService;
42import android.os.IStatsManager;
43import android.os.Parcelable;
44import android.os.Process;
45import android.os.RemoteException;
46import android.os.ServiceManager;
47import android.os.StatFs;
48import android.os.StatsDimensionsValue;
49import android.os.StatsLogEventWrapper;
50import android.os.SynchronousResultReceiver;
51import android.os.SystemClock;
52import android.os.UserHandle;
53import android.os.UserManager;
54import android.telephony.ModemActivityInfo;
55import android.telephony.TelephonyManager;
56import android.util.Slog;
57import android.util.StatsLog;
58
59import com.android.internal.annotations.GuardedBy;
60import com.android.internal.net.NetworkStatsFactory;
61import com.android.internal.os.KernelCpuSpeedReader;
62import com.android.internal.os.KernelWakelockReader;
63import com.android.internal.os.KernelWakelockStats;
64import com.android.internal.os.PowerProfile;
65import com.android.server.LocalServices;
66import com.android.server.SystemService;
67
68import java.util.ArrayList;
69import java.util.List;
70import java.util.Map;
71import java.util.concurrent.TimeoutException;
72
73/**
74 * Helper service for statsd (the native stats management service in cmds/statsd/).
75 * Used for registering and receiving alarms on behalf of statsd.
76 *
77 * @hide
78 */
79public class StatsCompanionService extends IStatsCompanionService.Stub {
80    /**
81     * How long to wait on an individual subsystem to return its stats.
82     */
83    private static final long EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS = 2000;
84
85    public static final String RESULT_RECEIVER_CONTROLLER_KEY = "controller_activity";
86
87    static final String TAG = "StatsCompanionService";
88    static final boolean DEBUG = false;
89
90    public static final int CODE_DATA_BROADCAST = 1;
91    public static final int CODE_SUBSCRIBER_BROADCAST = 1;
92
93    private final Context mContext;
94    private final AlarmManager mAlarmManager;
95    @GuardedBy("sStatsdLock")
96    private static IStatsManager sStatsd;
97    private static final Object sStatsdLock = new Object();
98
99    private final PendingIntent mAnomalyAlarmIntent;
100    private final PendingIntent mPullingAlarmIntent;
101    private final BroadcastReceiver mAppUpdateReceiver;
102    private final BroadcastReceiver mUserUpdateReceiver;
103    private final ShutdownEventReceiver mShutdownEventReceiver;
104    private final KernelWakelockReader mKernelWakelockReader = new KernelWakelockReader();
105    private final KernelWakelockStats mTmpWakelockStats = new KernelWakelockStats();
106    private final KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
107    private IWifiManager mWifiManager = null;
108    private TelephonyManager mTelephony = null;
109    private final StatFs mStatFsData = new StatFs(Environment.getDataDirectory().getAbsolutePath());
110    private final StatFs mStatFsSystem =
111            new StatFs(Environment.getRootDirectory().getAbsolutePath());
112    private final StatFs mStatFsTemp =
113            new StatFs(Environment.getDownloadCacheDirectory().getAbsolutePath());
114
115    public StatsCompanionService(Context context) {
116        super();
117        mContext = context;
118        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
119
120        mAnomalyAlarmIntent = PendingIntent.getBroadcast(mContext, 0,
121                new Intent(mContext, AnomalyAlarmReceiver.class), 0);
122        mPullingAlarmIntent = PendingIntent.getBroadcast(
123                mContext, 0, new Intent(mContext, PullingAlarmReceiver.class), 0);
124        mAppUpdateReceiver = new AppUpdateReceiver();
125        mUserUpdateReceiver = new BroadcastReceiver() {
126            @Override
127            public void onReceive(Context context, Intent intent) {
128                synchronized (sStatsdLock) {
129                    sStatsd = fetchStatsdService();
130                    if (sStatsd == null) {
131                        Slog.w(TAG, "Could not access statsd");
132                        return;
133                    }
134                    try {
135                        // Pull the latest state of UID->app name, version mapping.
136                        // Needed since the new user basically has a version of every app.
137                        informAllUidsLocked(context);
138                    } catch (RemoteException e) {
139                        Slog.e(TAG, "Failed to inform statsd latest update of all apps", e);
140                        forgetEverything();
141                    }
142                }
143            }
144        };
145        mShutdownEventReceiver = new ShutdownEventReceiver();
146        Slog.w(TAG, "Registered receiver for ACTION_PACKAGE_REPLACE AND ADDED.");
147        PowerProfile powerProfile = new PowerProfile(context);
148        final int numClusters = powerProfile.getNumCpuClusters();
149        mKernelCpuSpeedReaders = new KernelCpuSpeedReader[numClusters];
150        int firstCpuOfCluster = 0;
151        for (int i = 0; i < numClusters; i++) {
152            final int numSpeedSteps = powerProfile.getNumSpeedStepsInCpuCluster(i);
153            mKernelCpuSpeedReaders[i] = new KernelCpuSpeedReader(firstCpuOfCluster,
154                    numSpeedSteps);
155            firstCpuOfCluster += powerProfile.getNumCoresInCpuCluster(i);
156        }
157    }
158
159    @Override
160    public void sendDataBroadcast(IBinder intentSenderBinder) {
161        enforceCallingPermission();
162        IntentSender intentSender = new IntentSender(intentSenderBinder);
163        Intent intent = new Intent();
164        try {
165            intentSender.sendIntent(mContext, CODE_DATA_BROADCAST, intent, null, null);
166        } catch (IntentSender.SendIntentException e) {
167            Slog.w(TAG, "Unable to send using IntentSender");
168        }
169    }
170
171    @Override
172    public void sendSubscriberBroadcast(IBinder intentSenderBinder, long configUid, long configKey,
173                                        long subscriptionId, long subscriptionRuleId,
174                                        StatsDimensionsValue dimensionsValue) {
175        if (DEBUG) Slog.d(TAG, "Statsd requested to sendSubscriberBroadcast.");
176        enforceCallingPermission();
177        IntentSender intentSender = new IntentSender(intentSenderBinder);
178        Intent intent = new Intent()
179                .putExtra(StatsManager.EXTRA_STATS_CONFIG_UID, configUid)
180                .putExtra(StatsManager.EXTRA_STATS_CONFIG_KEY, configKey)
181                .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_ID, subscriptionId)
182                .putExtra(StatsManager.EXTRA_STATS_SUBSCRIPTION_RULE_ID, subscriptionRuleId)
183                .putExtra(StatsManager.EXTRA_STATS_DIMENSIONS_VALUE, dimensionsValue);
184        try {
185            intentSender.sendIntent(mContext, CODE_SUBSCRIBER_BROADCAST, intent, null, null);
186        } catch (IntentSender.SendIntentException e) {
187            Slog.w(TAG, "Unable to send using IntentSender from uid " + configUid
188                    + "; presumably it had been cancelled.");
189            if (DEBUG) {
190                Slog.d(TAG, String.format("SubscriberBroadcast params {%d %d %d %d %s}",
191                        configUid, configKey, subscriptionId,
192                        subscriptionRuleId, dimensionsValue));
193            }
194        }
195    }
196
197    private final static int[] toIntArray(List<Integer> list) {
198        int[] ret = new int[list.size()];
199        for (int i = 0; i < ret.length; i++) {
200            ret[i] = list.get(i);
201        }
202        return ret;
203    }
204
205    private final static long[] toLongArray(List<Long> list) {
206        long[] ret = new long[list.size()];
207        for (int i = 0; i < ret.length; i++) {
208            ret[i] = list.get(i);
209        }
210        return ret;
211    }
212
213    // Assumes that sStatsdLock is held.
214    @GuardedBy("sStatsdLock")
215    private final void informAllUidsLocked(Context context) throws RemoteException {
216        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
217        PackageManager pm = context.getPackageManager();
218        final List<UserInfo> users = um.getUsers(true);
219        if (DEBUG) {
220            Slog.w(TAG, "Iterating over " + users.size() + " profiles.");
221        }
222
223        List<Integer> uids = new ArrayList();
224        List<Long> versions = new ArrayList();
225        List<String> apps = new ArrayList();
226
227        // Add in all the apps for every user/profile.
228        for (UserInfo profile : users) {
229            List<PackageInfo> pi = pm.getInstalledPackagesAsUser(0, profile.id);
230            for (int j = 0; j < pi.size(); j++) {
231                if (pi.get(j).applicationInfo != null) {
232                    uids.add(pi.get(j).applicationInfo.uid);
233                    versions.add(pi.get(j).getLongVersionCode());
234                    apps.add(pi.get(j).packageName);
235                }
236            }
237        }
238        sStatsd.informAllUidData(toIntArray(uids), toLongArray(versions), apps.toArray(new
239                String[apps.size()]));
240        if (DEBUG) {
241            Slog.w(TAG, "Sent data for " + uids.size() + " apps");
242        }
243    }
244
245    private final static class AppUpdateReceiver extends BroadcastReceiver {
246        @Override
247        public void onReceive(Context context, Intent intent) {
248            /**
249             * App updates actually consist of REMOVE, ADD, and then REPLACE broadcasts. To avoid
250             * waste, we ignore the REMOVE and ADD broadcasts that contain the replacing flag.
251             * If we can't find the value for EXTRA_REPLACING, we default to false.
252             */
253            if (!intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)
254                    && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
255                return; // Keep only replacing or normal add and remove.
256            }
257            Slog.i(TAG, "StatsCompanionService noticed an app was updated.");
258            synchronized (sStatsdLock) {
259                if (sStatsd == null) {
260                    Slog.w(TAG, "Could not access statsd to inform it of an app update");
261                    return;
262                }
263                try {
264                    if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
265                        Bundle b = intent.getExtras();
266                        int uid = b.getInt(Intent.EXTRA_UID);
267                        boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
268                        if (!replacing) {
269                            // Don't bother sending an update if we're right about to get another
270                            // intent for the new version that's added.
271                            PackageManager pm = context.getPackageManager();
272                            String app = intent.getData().getSchemeSpecificPart();
273                            sStatsd.informOnePackageRemoved(app, uid);
274                        }
275                    } else {
276                        PackageManager pm = context.getPackageManager();
277                        Bundle b = intent.getExtras();
278                        int uid = b.getInt(Intent.EXTRA_UID);
279                        String app = intent.getData().getSchemeSpecificPart();
280                        PackageInfo pi = pm.getPackageInfo(app, PackageManager.MATCH_ANY_USER);
281                        sStatsd.informOnePackage(app, uid, pi.getLongVersionCode());
282                    }
283                } catch (Exception e) {
284                    Slog.w(TAG, "Failed to inform statsd of an app update", e);
285                }
286            }
287        }
288    }
289
290    public final static class AnomalyAlarmReceiver extends BroadcastReceiver {
291        @Override
292        public void onReceive(Context context, Intent intent) {
293            Slog.i(TAG, "StatsCompanionService believes an anomaly has occurred at time "
294                    + System.currentTimeMillis() + "ms.");
295            synchronized (sStatsdLock) {
296                if (sStatsd == null) {
297                    Slog.w(TAG, "Could not access statsd to inform it of anomaly alarm firing");
298                    return;
299                }
300                try {
301                    // Two-way call to statsd to retain AlarmManager wakelock
302                    sStatsd.informAnomalyAlarmFired();
303                } catch (RemoteException e) {
304                    Slog.w(TAG, "Failed to inform statsd of anomaly alarm firing", e);
305                }
306            }
307            // AlarmManager releases its own wakelock here.
308        }
309    }
310
311    public final static class PullingAlarmReceiver extends BroadcastReceiver {
312        @Override
313        public void onReceive(Context context, Intent intent) {
314            if (DEBUG)
315                Slog.d(TAG, "Time to poll something.");
316            synchronized (sStatsdLock) {
317                if (sStatsd == null) {
318                    Slog.w(TAG, "Could not access statsd to inform it of pulling alarm firing.");
319                    return;
320                }
321                try {
322                    // Two-way call to statsd to retain AlarmManager wakelock
323                    sStatsd.informPollAlarmFired();
324                } catch (RemoteException e) {
325                    Slog.w(TAG, "Failed to inform statsd of pulling alarm firing.", e);
326                }
327            }
328        }
329    }
330
331    private final static class ShutdownEventReceiver extends BroadcastReceiver {
332        @Override
333        public void onReceive(Context context, Intent intent) {
334            /**
335             * Skip immediately if intent is not relevant to device shutdown.
336             */
337            if (!intent.getAction().equals(Intent.ACTION_REBOOT)
338                    && !(intent.getAction().equals(Intent.ACTION_SHUTDOWN)
339                    && (intent.getFlags() & Intent.FLAG_RECEIVER_FOREGROUND) != 0)) {
340                return;
341            }
342
343            Slog.i(TAG, "StatsCompanionService noticed a shutdown.");
344            synchronized (sStatsdLock) {
345                if (sStatsd == null) {
346                    Slog.w(TAG, "Could not access statsd to inform it of a shutdown event.");
347                    return;
348                }
349                try {
350                    sStatsd.writeDataToDisk();
351                } catch (Exception e) {
352                    Slog.w(TAG, "Failed to inform statsd of a shutdown event.", e);
353                }
354            }
355        }
356    }
357
358    @Override // Binder call
359    public void setAnomalyAlarm(long timestampMs) {
360        enforceCallingPermission();
361        if (DEBUG) Slog.d(TAG, "Setting anomaly alarm for " + timestampMs);
362        final long callingToken = Binder.clearCallingIdentity();
363        try {
364            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
365            // only fire when it awakens.
366            // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
367            // AlarmManager will automatically cancel any previous mAnomalyAlarmIntent alarm.
368            mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME, timestampMs, mAnomalyAlarmIntent);
369        } finally {
370            Binder.restoreCallingIdentity(callingToken);
371        }
372    }
373
374    @Override // Binder call
375    public void cancelAnomalyAlarm() {
376        enforceCallingPermission();
377        if (DEBUG) Slog.d(TAG, "Cancelling anomaly alarm");
378        final long callingToken = Binder.clearCallingIdentity();
379        try {
380            mAlarmManager.cancel(mAnomalyAlarmIntent);
381        } finally {
382            Binder.restoreCallingIdentity(callingToken);
383        }
384    }
385
386    @Override // Binder call
387    public void setPullingAlarms(long timestampMs, long intervalMs) {
388        enforceCallingPermission();
389        if (DEBUG)
390            Slog.d(TAG, "Setting pulling alarm for " + timestampMs + " every " + intervalMs + "ms");
391        final long callingToken = Binder.clearCallingIdentity();
392        try {
393            // using ELAPSED_REALTIME, not ELAPSED_REALTIME_WAKEUP, so if device is asleep, will
394            // only fire when it awakens.
395            // This alarm is inexact, leaving its exactness completely up to the OS optimizations.
396            // TODO: totally inexact means that stats per bucket could be quite off. Is this okay?
397            mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, timestampMs, intervalMs,
398                    mPullingAlarmIntent);
399        } finally {
400            Binder.restoreCallingIdentity(callingToken);
401        }
402    }
403
404    @Override // Binder call
405    public void cancelPullingAlarms() {
406        enforceCallingPermission();
407        if (DEBUG)
408            Slog.d(TAG, "Cancelling pulling alarm");
409        final long callingToken = Binder.clearCallingIdentity();
410        try {
411            mAlarmManager.cancel(mPullingAlarmIntent);
412        } finally {
413            Binder.restoreCallingIdentity(callingToken);
414        }
415    }
416
417    private void addNetworkStats(
418            int tag, List<StatsLogEventWrapper> ret, NetworkStats stats, boolean withFGBG) {
419        int size = stats.size();
420        NetworkStats.Entry entry = new NetworkStats.Entry(); // For recycling
421        for (int j = 0; j < size; j++) {
422            stats.getValues(j, entry);
423            StatsLogEventWrapper e = new StatsLogEventWrapper(tag, withFGBG ? 6 : 5);
424            e.writeInt(entry.uid);
425            if (withFGBG) {
426                e.writeInt(entry.set);
427            }
428            e.writeLong(entry.rxBytes);
429            e.writeLong(entry.rxPackets);
430            e.writeLong(entry.txBytes);
431            e.writeLong(entry.txPackets);
432            ret.add(e);
433        }
434    }
435
436    /**
437     * Allows rollups per UID but keeping the set (foreground/background) slicing.
438     * Adapted from groupedByUid in frameworks/base/core/java/android/net/NetworkStats.java
439     */
440    private NetworkStats rollupNetworkStatsByFGBG(NetworkStats stats) {
441        final NetworkStats ret = new NetworkStats(stats.getElapsedRealtime(), 1);
442
443        final NetworkStats.Entry entry = new NetworkStats.Entry();
444        entry.iface = NetworkStats.IFACE_ALL;
445        entry.tag = NetworkStats.TAG_NONE;
446        entry.metered = NetworkStats.METERED_ALL;
447        entry.roaming = NetworkStats.ROAMING_ALL;
448
449        int size = stats.size();
450        NetworkStats.Entry recycle = new NetworkStats.Entry(); // Used for retrieving values
451        for (int i = 0; i < size; i++) {
452            stats.getValues(i, recycle);
453
454            // Skip specific tags, since already counted in TAG_NONE
455            if (recycle.tag != NetworkStats.TAG_NONE) continue;
456
457            entry.set = recycle.set; // Allows slicing by background/foreground
458            entry.uid = recycle.uid;
459            entry.rxBytes = recycle.rxBytes;
460            entry.rxPackets = recycle.rxPackets;
461            entry.txBytes = recycle.txBytes;
462            entry.txPackets = recycle.txPackets;
463            // Operations purposefully omitted since we don't use them for statsd.
464            ret.combineValues(entry);
465        }
466        return ret;
467    }
468
469    /**
470     * Helper method to extract the Parcelable controller info from a
471     * SynchronousResultReceiver.
472     */
473    private static <T extends Parcelable> T awaitControllerInfo(
474            @Nullable SynchronousResultReceiver receiver) {
475        if (receiver == null) {
476            return null;
477        }
478
479        try {
480            final SynchronousResultReceiver.Result result =
481                    receiver.awaitResult(EXTERNAL_STATS_SYNC_TIMEOUT_MILLIS);
482            if (result.bundle != null) {
483                // This is the final destination for the Bundle.
484                result.bundle.setDefusable(true);
485
486                final T data = result.bundle.getParcelable(
487                        RESULT_RECEIVER_CONTROLLER_KEY);
488                if (data != null) {
489                    return data;
490                }
491            }
492            Slog.e(TAG, "no controller energy info supplied for " + receiver.getName());
493        } catch (TimeoutException e) {
494            Slog.w(TAG, "timeout reading " + receiver.getName() + " stats");
495        }
496        return null;
497    }
498
499    private void pullKernelWakelock(int tagId, List<StatsLogEventWrapper> pulledData) {
500        final KernelWakelockStats wakelockStats =
501                mKernelWakelockReader.readKernelWakelockStats(mTmpWakelockStats);
502        for (Map.Entry<String, KernelWakelockStats.Entry> ent : wakelockStats.entrySet()) {
503            String name = ent.getKey();
504            KernelWakelockStats.Entry kws = ent.getValue();
505            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 4);
506            e.writeString(name);
507            e.writeInt(kws.mCount);
508            e.writeInt(kws.mVersion);
509            e.writeLong(kws.mTotalTime);
510            pulledData.add(e);
511        }
512    }
513
514    private void pullWifiBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
515        long token = Binder.clearCallingIdentity();
516        try {
517            // TODO: Consider caching the following call to get BatteryStatsInternal.
518            BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
519            String[] ifaces = bs.getWifiIfaces();
520            if (ifaces.length == 0) {
521                return;
522            }
523            NetworkStatsFactory nsf = new NetworkStatsFactory();
524            // Combine all the metrics per Uid into one record.
525            NetworkStats stats =
526                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)
527                            .groupedByUid();
528            addNetworkStats(tagId, pulledData, stats, false);
529        } catch (java.io.IOException e) {
530            Slog.e(TAG, "Pulling netstats for wifi bytes has error", e);
531        } finally {
532            Binder.restoreCallingIdentity(token);
533        }
534    }
535
536    private void pullWifiBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
537        long token = Binder.clearCallingIdentity();
538        try {
539            BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
540            String[] ifaces = bs.getWifiIfaces();
541            if (ifaces.length == 0) {
542                return;
543            }
544            NetworkStatsFactory nsf = new NetworkStatsFactory();
545            NetworkStats stats = rollupNetworkStatsByFGBG(
546                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null));
547            addNetworkStats(tagId, pulledData, stats, true);
548        } catch (java.io.IOException e) {
549            Slog.e(TAG, "Pulling netstats for wifi bytes w/ fg/bg has error", e);
550        } finally {
551            Binder.restoreCallingIdentity(token);
552        }
553    }
554
555    private void pullMobileBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
556        long token = Binder.clearCallingIdentity();
557        try {
558            BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
559            String[] ifaces = bs.getMobileIfaces();
560            if (ifaces.length == 0) {
561                return;
562            }
563            NetworkStatsFactory nsf = new NetworkStatsFactory();
564            // Combine all the metrics per Uid into one record.
565            NetworkStats stats =
566                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null)
567                            .groupedByUid();
568            addNetworkStats(tagId, pulledData, stats, false);
569        } catch (java.io.IOException e) {
570            Slog.e(TAG, "Pulling netstats for mobile bytes has error", e);
571        } finally {
572            Binder.restoreCallingIdentity(token);
573        }
574    }
575
576    private void pullBluetoothBytesTransfer(int tagId, List<StatsLogEventWrapper> pulledData) {
577        BluetoothActivityEnergyInfo info = pullBluetoothData();
578        for (UidTraffic traffic : info.getUidTraffic()) {
579            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
580            e.writeInt(traffic.getUid());
581            e.writeLong(traffic.getRxBytes());
582            e.writeLong(traffic.getTxBytes());
583            pulledData.add(e);
584        }
585    }
586
587    private void pullMobileBytesTransferByFgBg(int tagId, List<StatsLogEventWrapper> pulledData) {
588        long token = Binder.clearCallingIdentity();
589        try {
590            BatteryStatsInternal bs = LocalServices.getService(BatteryStatsInternal.class);
591            String[] ifaces = bs.getMobileIfaces();
592            if (ifaces.length == 0) {
593                return;
594            }
595            NetworkStatsFactory nsf = new NetworkStatsFactory();
596            NetworkStats stats = rollupNetworkStatsByFGBG(
597                    nsf.readNetworkStatsDetail(NetworkStats.UID_ALL, ifaces, NetworkStats.TAG_NONE, null));
598            addNetworkStats(tagId, pulledData, stats, true);
599        } catch (java.io.IOException e) {
600            Slog.e(TAG, "Pulling netstats for mobile bytes w/ fg/bg has error", e);
601        } finally {
602            Binder.restoreCallingIdentity(token);
603        }
604    }
605
606    private void pullCpuTimePerFreq(int tagId, List<StatsLogEventWrapper> pulledData) {
607        for (int cluster = 0; cluster < mKernelCpuSpeedReaders.length; cluster++) {
608            long[] clusterTimeMs = mKernelCpuSpeedReaders[cluster].readAbsolute();
609            if (clusterTimeMs != null) {
610                for (int speed = clusterTimeMs.length - 1; speed >= 0; --speed) {
611                    StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
612                    e.writeInt(cluster);
613                    e.writeInt(speed);
614                    e.writeLong(clusterTimeMs[speed]);
615                    pulledData.add(e);
616                }
617            }
618        }
619    }
620
621    private void pullWifiActivityEnergyInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
622        long token = Binder.clearCallingIdentity();
623        if (mWifiManager == null) {
624            mWifiManager =
625                    IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
626        }
627        if (mWifiManager != null) {
628            try {
629                SynchronousResultReceiver wifiReceiver = new SynchronousResultReceiver("wifi");
630                mWifiManager.requestActivityInfo(wifiReceiver);
631                final WifiActivityEnergyInfo wifiInfo = awaitControllerInfo(wifiReceiver);
632                StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
633                e.writeLong(wifiInfo.getTimeStamp());
634                e.writeInt(wifiInfo.getStackState());
635                e.writeLong(wifiInfo.getControllerTxTimeMillis());
636                e.writeLong(wifiInfo.getControllerRxTimeMillis());
637                e.writeLong(wifiInfo.getControllerIdleTimeMillis());
638                e.writeLong(wifiInfo.getControllerEnergyUsed());
639                pulledData.add(e);
640            } catch (RemoteException e) {
641                Slog.e(TAG, "Pulling wifiManager for wifi controller activity energy info has error", e);
642            } finally {
643                Binder.restoreCallingIdentity(token);
644            }
645        }
646    }
647
648    private void pullModemActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
649        long token = Binder.clearCallingIdentity();
650        if (mTelephony == null) {
651            mTelephony = TelephonyManager.from(mContext);
652        }
653        if (mTelephony != null) {
654            SynchronousResultReceiver modemReceiver = new SynchronousResultReceiver("telephony");
655            mTelephony.requestModemActivityInfo(modemReceiver);
656            final ModemActivityInfo modemInfo = awaitControllerInfo(modemReceiver);
657            StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
658            e.writeLong(modemInfo.getTimestamp());
659            e.writeLong(modemInfo.getSleepTimeMillis());
660            e.writeLong(modemInfo.getIdleTimeMillis());
661            e.writeLong(modemInfo.getTxTimeMillis()[0]);
662            e.writeLong(modemInfo.getTxTimeMillis()[1]);
663            e.writeLong(modemInfo.getTxTimeMillis()[2]);
664            e.writeLong(modemInfo.getTxTimeMillis()[3]);
665            e.writeLong(modemInfo.getTxTimeMillis()[4]);
666            e.writeLong(modemInfo.getRxTimeMillis());
667            e.writeLong(modemInfo.getEnergyUsed());
668            pulledData.add(e);
669        }
670    }
671
672    private void pullBluetoothActivityInfo(int tagId, List<StatsLogEventWrapper> pulledData) {
673        BluetoothActivityEnergyInfo info = pullBluetoothData();
674        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 6);
675        e.writeLong(info.getTimeStamp());
676        e.writeInt(info.getBluetoothStackState());
677        e.writeLong(info.getControllerTxTimeMillis());
678        e.writeLong(info.getControllerRxTimeMillis());
679        e.writeLong(info.getControllerIdleTimeMillis());
680        e.writeLong(info.getControllerEnergyUsed());
681        pulledData.add(e);
682    }
683
684    private synchronized BluetoothActivityEnergyInfo pullBluetoothData() {
685        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
686        if (adapter != null) {
687            SynchronousResultReceiver bluetoothReceiver = new SynchronousResultReceiver("bluetooth");
688            adapter.requestControllerActivityEnergyInfo(bluetoothReceiver);
689            return awaitControllerInfo(bluetoothReceiver);
690        } else {
691            Slog.e(TAG, "Failed to get bluetooth adapter!");
692            return null;
693        }
694    }
695
696    private void pullSystemElapsedRealtime(int tagId, List<StatsLogEventWrapper> pulledData) {
697        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
698        e.writeLong(SystemClock.elapsedRealtime());
699        pulledData.add(e);
700    }
701
702    private void pullDiskSpace(int tagId, List<StatsLogEventWrapper> pulledData) {
703        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 3);
704        e.writeLong(mStatFsData.getAvailableBytes());
705        e.writeLong(mStatFsSystem.getAvailableBytes());
706        e.writeLong(mStatFsTemp.getAvailableBytes());
707        pulledData.add(e);
708    }
709
710    private void pullSystemUpTime(int tagId, List<StatsLogEventWrapper> pulledData) {
711        StatsLogEventWrapper e = new StatsLogEventWrapper(tagId, 1);
712        e.writeLong(SystemClock.uptimeMillis());
713        pulledData.add(e);
714    }
715
716    /**
717     * Pulls various data.
718     */
719    @Override // Binder call
720    public StatsLogEventWrapper[] pullData(int tagId) {
721        enforceCallingPermission();
722        if (DEBUG)
723            Slog.d(TAG, "Pulling " + tagId);
724        List<StatsLogEventWrapper> ret = new ArrayList();
725        switch (tagId) {
726            case StatsLog.WIFI_BYTES_TRANSFER: {
727                pullWifiBytesTransfer(tagId, ret);
728                break;
729            }
730            case StatsLog.MOBILE_BYTES_TRANSFER: {
731                pullMobileBytesTransfer(tagId, ret);
732                break;
733            }
734            case StatsLog.WIFI_BYTES_TRANSFER_BY_FG_BG: {
735                pullWifiBytesTransferByFgBg(tagId, ret);
736                break;
737            }
738            case StatsLog.MOBILE_BYTES_TRANSFER_BY_FG_BG: {
739                pullMobileBytesTransferByFgBg(tagId, ret);
740                break;
741            }
742            case StatsLog.BLUETOOTH_BYTES_TRANSFER: {
743                pullBluetoothBytesTransfer(tagId, ret);
744                break;
745            }
746            case StatsLog.KERNEL_WAKELOCK: {
747                pullKernelWakelock(tagId, ret);
748                break;
749            }
750            case StatsLog.CPU_TIME_PER_FREQ: {
751                pullCpuTimePerFreq(tagId, ret);
752                break;
753            }
754            case StatsLog.WIFI_ACTIVITY_ENERGY_INFO: {
755                pullWifiActivityEnergyInfo(tagId, ret);
756                break;
757            }
758            case StatsLog.MODEM_ACTIVITY_INFO: {
759                pullModemActivityInfo(tagId, ret);
760                break;
761            }
762            case StatsLog.BLUETOOTH_ACTIVITY_INFO: {
763                pullBluetoothActivityInfo(tagId, ret);
764                break;
765            }
766            case StatsLog.SYSTEM_UPTIME: {
767                pullSystemUpTime(tagId, ret);
768                break;
769            }
770            case StatsLog.SYSTEM_ELAPSED_REALTIME: {
771                pullSystemElapsedRealtime(tagId, ret);
772                break;
773            }
774            case StatsLog.DISK_SPACE: {
775                pullDiskSpace(tagId, ret);
776                break;
777            }
778            default:
779                Slog.w(TAG, "No such tagId data as " + tagId);
780                return null;
781        }
782        return ret.toArray(new StatsLogEventWrapper[ret.size()]);
783    }
784
785    @Override // Binder call
786    public void statsdReady() {
787        enforceCallingPermission();
788        if (DEBUG) Slog.d(TAG, "learned that statsdReady");
789        sayHiToStatsd(); // tell statsd that we're ready too and link to it
790        mContext.sendBroadcastAsUser(
791                new Intent(StatsManager.ACTION_STATSD_STARTED)
792                        .addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND),
793                UserHandle.SYSTEM,
794                android.Manifest.permission.DUMP);
795    }
796
797    @Override
798    public void triggerUidSnapshot() {
799        enforceCallingPermission();
800        synchronized (sStatsdLock) {
801            try {
802                informAllUidsLocked(mContext);
803            } catch (RemoteException e) {
804                Slog.e(TAG, "Failed to trigger uid snapshot.", e);
805            }
806        }
807    }
808
809    private void enforceCallingPermission() {
810        if (Binder.getCallingPid() == Process.myPid()) {
811            return;
812        }
813        mContext.enforceCallingPermission(android.Manifest.permission.STATSCOMPANION, null);
814    }
815
816    // Lifecycle and related code
817
818    /**
819     * Fetches the statsd IBinder service
820     */
821    private static IStatsManager fetchStatsdService() {
822        return IStatsManager.Stub.asInterface(ServiceManager.getService("stats"));
823    }
824
825    public static final class Lifecycle extends SystemService {
826        private StatsCompanionService mStatsCompanionService;
827
828        public Lifecycle(Context context) {
829            super(context);
830        }
831
832        @Override
833        public void onStart() {
834            mStatsCompanionService = new StatsCompanionService(getContext());
835            try {
836                publishBinderService(Context.STATS_COMPANION_SERVICE, mStatsCompanionService);
837                if (DEBUG) Slog.d(TAG, "Published " + Context.STATS_COMPANION_SERVICE);
838            } catch (Exception e) {
839                Slog.e(TAG, "Failed to publishBinderService", e);
840            }
841        }
842
843        @Override
844        public void onBootPhase(int phase) {
845            super.onBootPhase(phase);
846            if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
847                mStatsCompanionService.systemReady();
848            }
849        }
850    }
851
852    /**
853     * Now that the android system is ready, StatsCompanion is ready too, so inform statsd.
854     */
855    private void systemReady() {
856        if (DEBUG) Slog.d(TAG, "Learned that systemReady");
857        sayHiToStatsd();
858    }
859
860    /**
861     * Tells statsd that statscompanion is ready. If the binder call returns, link to statsd.
862     */
863    private void sayHiToStatsd() {
864        synchronized (sStatsdLock) {
865            if (sStatsd != null) {
866                Slog.e(TAG, "Trying to fetch statsd, but it was already fetched",
867                        new IllegalStateException("sStatsd is not null when being fetched"));
868                return;
869            }
870            sStatsd = fetchStatsdService();
871            if (sStatsd == null) {
872                Slog.w(TAG, "Could not access statsd");
873                return;
874            }
875            if (DEBUG) Slog.d(TAG, "Saying hi to statsd");
876            try {
877                sStatsd.statsCompanionReady();
878                // If the statsCompanionReady two-way binder call returns, link to statsd.
879                try {
880                    sStatsd.asBinder().linkToDeath(new StatsdDeathRecipient(), 0);
881                } catch (RemoteException e) {
882                    Slog.e(TAG, "linkToDeath(StatsdDeathRecipient) failed", e);
883                    forgetEverything();
884                }
885                // Setup broadcast receiver for updates.
886                IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REPLACED);
887                filter.addAction(Intent.ACTION_PACKAGE_ADDED);
888                filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
889                filter.addDataScheme("package");
890                mContext.registerReceiverAsUser(mAppUpdateReceiver, UserHandle.ALL, filter, null,
891                        null);
892
893                // Setup receiver for user initialize (which happens once for a new user) and
894                // if a user is removed.
895                filter = new IntentFilter(Intent.ACTION_USER_INITIALIZE);
896                filter.addAction(Intent.ACTION_USER_REMOVED);
897                mContext.registerReceiverAsUser(mUserUpdateReceiver, UserHandle.ALL,
898                        filter, null, null);
899
900                // Setup receiver for device reboots or shutdowns.
901                filter = new IntentFilter(Intent.ACTION_REBOOT);
902                filter.addAction(Intent.ACTION_SHUTDOWN);
903                mContext.registerReceiverAsUser(
904                        mShutdownEventReceiver, UserHandle.ALL, filter, null, null);
905                final long token = Binder.clearCallingIdentity();
906                try {
907                    // Pull the latest state of UID->app name, version mapping when statsd starts.
908                    informAllUidsLocked(mContext);
909                } finally {
910                    restoreCallingIdentity(token);
911                }
912            } catch (RemoteException e) {
913                Slog.e(TAG, "Failed to inform statsd that statscompanion is ready", e);
914                forgetEverything();
915            }
916        }
917    }
918
919    private class StatsdDeathRecipient implements IBinder.DeathRecipient {
920        @Override
921        public void binderDied() {
922            Slog.i(TAG, "Statsd is dead - erase all my knowledge.");
923            forgetEverything();
924        }
925    }
926
927    private void forgetEverything() {
928        synchronized (sStatsdLock) {
929            sStatsd = null;
930            mContext.unregisterReceiver(mAppUpdateReceiver);
931            mContext.unregisterReceiver(mUserUpdateReceiver);
932            mContext.unregisterReceiver(mShutdownEventReceiver);
933            cancelAnomalyAlarm();
934            cancelPullingAlarms();
935        }
936    }
937
938}
939