• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.server.location.gnss;
18 
19 import static android.hardware.gnss.ElapsedRealtime.HAS_TIMESTAMP_NS;
20 import static android.hardware.gnss.ElapsedRealtime.HAS_TIME_UNCERTAINTY_NS;
21 
22 import static java.util.concurrent.TimeUnit.NANOSECONDS;
23 
24 import android.location.GnssCapabilities;
25 import android.util.IndentingPrintWriter;
26 import android.util.TimeUtils;
27 
28 import com.android.internal.util.Preconditions;
29 import com.android.server.location.gnss.hal.GnssNative.GnssRealtimeFlags;
30 
31 import java.io.FileDescriptor;
32 
33 /**
34  * Represents Cumulative GNSS power statistics since boot.
35  */
36 public class GnssPowerStats {
37 
38     private final @GnssRealtimeFlags int mElapsedRealtimeFlags;
39     private final long mElapsedRealtimeNanos;
40     private final double mElapsedRealtimeUncertaintyNanos;
41     private final double mTotalEnergyMilliJoule;
42     private final double mSinglebandTrackingModeEnergyMilliJoule;
43     private final double mMultibandTrackingModeEnergyMilliJoule;
44     private final double mSinglebandAcquisitionModeEnergyMilliJoule;
45     private final double mMultibandAcquisitionModeEnergyMilliJoule;
46     private final double[] mOtherModesEnergyMilliJoule;
47 
GnssPowerStats(@nssRealtimeFlags int elapsedRealtimeFlags, long elapsedRealtimeNanos, double elapsedRealtimeUncertaintyNanos, double totalEnergyMilliJoule, double singlebandTrackingModeEnergyMilliJoule, double multibandTrackingModeEnergyMilliJoule, double singlebandAcquisitionModeEnergyMilliJoule, double multibandAcquisitionModeEnergyMilliJoule, double[] otherModesEnergyMilliJoule)48     public GnssPowerStats(@GnssRealtimeFlags int elapsedRealtimeFlags,
49             long elapsedRealtimeNanos,
50             double elapsedRealtimeUncertaintyNanos,
51             double totalEnergyMilliJoule,
52             double singlebandTrackingModeEnergyMilliJoule,
53             double multibandTrackingModeEnergyMilliJoule,
54             double singlebandAcquisitionModeEnergyMilliJoule,
55             double multibandAcquisitionModeEnergyMilliJoule,
56             double[] otherModesEnergyMilliJoule) {
57         mElapsedRealtimeFlags = elapsedRealtimeFlags;
58         mElapsedRealtimeNanos = elapsedRealtimeNanos;
59         mElapsedRealtimeUncertaintyNanos = elapsedRealtimeUncertaintyNanos;
60         mTotalEnergyMilliJoule = totalEnergyMilliJoule;
61         mSinglebandTrackingModeEnergyMilliJoule = singlebandTrackingModeEnergyMilliJoule;
62         mMultibandTrackingModeEnergyMilliJoule = multibandTrackingModeEnergyMilliJoule;
63         mSinglebandAcquisitionModeEnergyMilliJoule = singlebandAcquisitionModeEnergyMilliJoule;
64         mMultibandAcquisitionModeEnergyMilliJoule = multibandAcquisitionModeEnergyMilliJoule;
65         mOtherModesEnergyMilliJoule = otherModesEnergyMilliJoule;
66     }
67 
68     /** Returns true if {@link #getElapsedRealtimeNanos()} is available. */
hasElapsedRealtimeNanos()69     public boolean hasElapsedRealtimeNanos() {
70         return (mElapsedRealtimeFlags & HAS_TIMESTAMP_NS) != 0;
71     }
72 
73     /** Returns true if {@link #getElapsedRealtimeUncertaintyNanos()} is available. */
hasElapsedRealtimeUncertaintyNanos()74     public boolean hasElapsedRealtimeUncertaintyNanos() {
75         return (mElapsedRealtimeFlags & HAS_TIME_UNCERTAINTY_NS) != 0;
76     }
77 
78     /**
79      * Gets the elapsed realtime of the GnssPowerStats since boot in nanoseconds.
80      */
getElapsedRealtimeNanos()81     public long getElapsedRealtimeNanos() {
82         return mElapsedRealtimeNanos;
83     }
84 
85     /**
86      * Gets the estimate of the relative precision of the alignment of the
87      * {@link #getElapsedRealtimeNanos()} timestamp, with the reported measurements in
88      * nanoseconds (68% confidence).
89      */
getElapsedRealtimeUncertaintyNanos()90     public double getElapsedRealtimeUncertaintyNanos() {
91         return mElapsedRealtimeUncertaintyNanos;
92     }
93 
94     /**
95      * Total GNSS energy consumption in milli-joules (or mWatt-seconds).
96      */
getTotalEnergyMilliJoule()97     public double getTotalEnergyMilliJoule() {
98         return mTotalEnergyMilliJoule;
99     }
100 
101     /**
102      * Total energy consumption in milli-joules (or mWatt-seconds) for which the GNSS engine is
103      * tracking signals of a single frequency band.
104      */
getSinglebandTrackingModeEnergyMilliJoule()105     public double getSinglebandTrackingModeEnergyMilliJoule() {
106         return mSinglebandTrackingModeEnergyMilliJoule;
107     }
108 
109     /**
110      * Total energy consumption in milli-joules (or mWatt-seconds) for which the GNSS engine is
111      * tracking signals of multiple frequency bands.
112      */
getMultibandTrackingModeEnergyMilliJoule()113     public double getMultibandTrackingModeEnergyMilliJoule() {
114         return mMultibandTrackingModeEnergyMilliJoule;
115     }
116 
117     /**
118      * Total energy consumption in milli-joules (or mWatt-seconds) for which the GNSS engine is
119      * acquiring signals of a single frequency band.
120      */
getSinglebandAcquisitionModeEnergyMilliJoule()121     public double getSinglebandAcquisitionModeEnergyMilliJoule() {
122         return mSinglebandAcquisitionModeEnergyMilliJoule;
123     }
124 
125     /**
126      * Total energy consumption in milli-joules (or mWatt-seconds) for which the GNSS engine is
127      * acquiring signals of multiple frequency bands.
128      */
getMultibandAcquisitionModeEnergyMilliJoule()129     public double getMultibandAcquisitionModeEnergyMilliJoule() {
130         return mMultibandAcquisitionModeEnergyMilliJoule;
131     }
132 
133     /**
134      * Total energy consumption in milli-joules (or mWatt-seconds) for which the GNSS engine is
135      * operating in each of the vendor-specific power modes, in addition to other generic modes.
136      */
getOtherModesEnergyMilliJoule()137     public double[] getOtherModesEnergyMilliJoule() {
138         return mOtherModesEnergyMilliJoule;
139     }
140 
validate()141     public void validate() {
142         Preconditions.checkArgument(hasElapsedRealtimeNanos());
143     }
144 
145     /**
146      * Dumps power stat information filtered by the given capabilities.
147      */
dump(FileDescriptor fd, IndentingPrintWriter ipw, String[] args, GnssCapabilities capabilities)148     public void dump(FileDescriptor fd, IndentingPrintWriter ipw, String[] args,
149             GnssCapabilities capabilities) {
150         if (hasElapsedRealtimeNanos()) {
151             ipw.print("time: ");
152             ipw.print(TimeUtils.formatRealtime(NANOSECONDS.toMillis(mElapsedRealtimeNanos)));
153             if (hasElapsedRealtimeUncertaintyNanos() && mElapsedRealtimeUncertaintyNanos != 0) {
154                 ipw.print(" +/- ");
155                 ipw.print(NANOSECONDS.toMillis((long) mElapsedRealtimeUncertaintyNanos));
156             }
157         }
158         if (capabilities.hasPowerTotal()) {
159             ipw.print("total power: ");
160             ipw.print(mTotalEnergyMilliJoule);
161             ipw.println("mJ");
162         }
163         if (capabilities.hasPowerSinglebandTracking()) {
164             ipw.print("single-band tracking power: ");
165             ipw.print(mSinglebandTrackingModeEnergyMilliJoule);
166             ipw.println("mJ");
167         }
168         if (capabilities.hasPowerMultibandTracking()) {
169             ipw.print("multi-band tracking power: ");
170             ipw.print(mMultibandTrackingModeEnergyMilliJoule);
171             ipw.println("mJ");
172         }
173         if (capabilities.hasPowerSinglebandAcquisition()) {
174             ipw.print("single-band acquisition power: ");
175             ipw.print(mSinglebandAcquisitionModeEnergyMilliJoule);
176             ipw.println("mJ");
177         }
178         if (capabilities.hasPowerMultibandAcquisition()) {
179             ipw.print("multi-band acquisition power: ");
180             ipw.print(mMultibandAcquisitionModeEnergyMilliJoule);
181             ipw.println("mJ");
182         }
183         if (capabilities.hasPowerOtherModes()) {
184             for (int i = 0; i < mOtherModesEnergyMilliJoule.length; i++) {
185                 ipw.print("other mode [" + i + "] power: ");
186                 ipw.print(mOtherModesEnergyMilliJoule[i]);
187                 ipw.println("mJ");
188             }
189         }
190     }
191 }
192