1 /*
2  * Copyright (C) 2018 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 com.android.powermodel.component;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 import com.android.powermodel.AttributionKey;
22 import com.android.powermodel.ComponentActivity;
23 import com.android.powermodel.RawBatteryStats;
24 import com.android.powermodel.SpecialApp;
25 
26 public class ModemBatteryStatsReader {
ModemBatteryStatsReader()27     private ModemBatteryStatsReader() {
28     }
29 
createActivities(RawBatteryStats bs)30     public static List<ComponentActivity> createActivities(RawBatteryStats bs) {
31         final List<ComponentActivity> result = new ArrayList<ComponentActivity>();
32 
33         // The whole system
34         createGlobal(result, bs);
35 
36         // The apps
37         createApps(result, bs);
38 
39         // The synthetic "cell" app.
40         createRemainder(result, bs);
41 
42         return result;
43     }
44 
createGlobal(List<ComponentActivity> result, RawBatteryStats bs)45     private static void createGlobal(List<ComponentActivity> result, RawBatteryStats bs) {
46         final ModemGlobalActivity global
47                 = new ModemGlobalActivity(new AttributionKey(SpecialApp.GLOBAL));
48 
49         final RawBatteryStats.GlobalNetwork gn = bs.getSingle(RawBatteryStats.GlobalNetwork.class);
50         final RawBatteryStats.Misc misc = bs.getSingle(RawBatteryStats.Misc.class);
51 
52         // Null here just means no network activity.
53         if (gn != null && misc != null) {
54             global.rxPacketCount = gn.mobileRxTotalPackets;
55             global.txPacketCount = gn.mobileTxTotalPackets;
56 
57             global.totalActiveTimeMs = misc.mobileRadioActiveTimeMs;
58         }
59 
60         result.add(global);
61     }
62 
createApps(List<ComponentActivity> result, RawBatteryStats bs)63     private static void createApps(List<ComponentActivity> result, RawBatteryStats bs) {
64         for (AttributionKey key: bs.getApps()) {
65             final int uid = key.getUid();
66             final RawBatteryStats.Network network
67                     = bs.getSingle(RawBatteryStats.Network.class, uid);
68 
69             // Null here just means no network activity.
70             if (network != null) {
71                 final ModemAppActivity app = new ModemAppActivity(key);
72 
73                 app.rxPacketCount = network.mobileRxPackets;
74                 app.txPacketCount = network.mobileTxPackets;
75 
76                 result.add(app);
77             }
78         }
79     }
80 
createRemainder(List<ComponentActivity> result, RawBatteryStats bs)81     private static void createRemainder(List<ComponentActivity> result, RawBatteryStats bs) {
82         final RawBatteryStats.SignalStrengthTime strength
83                 = bs.getSingle(RawBatteryStats.SignalStrengthTime.class);
84         final RawBatteryStats.SignalScanningTime scanning
85                 = bs.getSingle(RawBatteryStats.SignalScanningTime.class);
86         final RawBatteryStats.Misc misc = bs.getSingle(RawBatteryStats.Misc.class);
87 
88         if (strength != null && scanning != null && misc != null) {
89             final ModemRemainderActivity remainder
90                     = new ModemRemainderActivity(new AttributionKey(SpecialApp.REMAINDER));
91 
92             // Signal strength buckets
93             remainder.strengthTimeMs = strength.phoneSignalStrengthTimeMs;
94 
95             // Time spent scanning
96             remainder.scanningTimeMs = scanning.phoneSignalScanningTimeMs;
97 
98             // Unaccounted for active time
99             final long totalActiveTimeMs = misc.mobileRadioActiveTimeMs;
100             long appActiveTimeMs = 0;
101             for (RawBatteryStats.Network nw: bs.getMultiple(RawBatteryStats.Network.class)) {
102                 appActiveTimeMs += nw.mobileRadioActiveTimeUs / 1000;
103             }
104             remainder.activeTimeMs = totalActiveTimeMs - appActiveTimeMs;
105 
106             result.add(remainder);
107         }
108     }
109 }
110 
111