summaryrefslogtreecommitdiff
path: root/com/android/server/wifi/WifiWakeMetrics.java
blob: fba72369f1327b02efab22e5c9354a583fbda37c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * 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.wifi;

import android.annotation.Nullable;
import android.os.SystemClock;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.wifi.nano.WifiMetricsProto.WifiWakeStats;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Holds WifiWake metrics and converts them to a protobuf included in WifiLog.
 */
public class WifiWakeMetrics {

    /** Maximum number of sessions to store in WifiWakeStats proto. */
    @VisibleForTesting
    static final int MAX_RECORDED_SESSIONS = 10;

    @GuardedBy("mLock")
    private final List<Session> mSessions = new ArrayList<>();
    @GuardedBy("mLock")
    private Session mCurrentSession;

    private boolean mIsInSession = false;
    private int mTotalSessions = 0;

    private final Object mLock = new Object();

    /**
     * Records the beginning of a Wifi Wake session.
     *
     * <p>Starts the session.
     *
     * @param numNetworks The total number of networks stored in the WakeupLock at initialization.
     */
    public void recordStartEvent(int numNetworks) {
        synchronized (mLock) {
            mCurrentSession = new Session(numNetworks, SystemClock.elapsedRealtime());
            mIsInSession = true;
        }
    }

    /**
     * Records the unlock event of the current Wifi Wake session.
     *
     * <p>The unlock event occurs when the WakeupLock has all of its networks removed. This event
     * will not be recorded if the start event recorded 0 locked networks.
     *
     * <p>Note: The start event must be recorded before this event, otherwise this call will be
     * ignored.
     *
     * @param numScans The total number of elapsed scans since start.
     */
    public void recordUnlockEvent(int numScans) {
        synchronized (mLock) {
            if (!mIsInSession) {
                return;
            }
            mCurrentSession.recordUnlockEvent(numScans, SystemClock.elapsedRealtime());
        }
    }

    /**
     * Records the wakeup event of the current Wifi Wake session.
     *
     * <p>The wakeup event occurs when Wifi is re-enabled by the WakeupController.
     *
     * <p>Note: The start event must be recorded before this event, otherwise this call will be
     * ignored.
     *
     * @param numScans The total number of elapsed scans since start.
     */
    public void recordWakeupEvent(int numScans) {
        synchronized (mLock) {
            if (!mIsInSession) {
                return;
            }
            mCurrentSession.recordWakeupEvent(numScans, SystemClock.elapsedRealtime());
        }
    }

    /**
     * Records the reset event of the current Wifi Wake session.
     *
     * <p>The reset event occurs when Wifi enters client mode. Stores the first
     * {@link #MAX_RECORDED_SESSIONS} in the session list.
     *
     * <p>Note: The start event must be recorded before this event, otherwise this call will be
     * ignored. This event ends the current session.
     *
     * @param numScans The total number of elapsed scans since start.
     */
    public void recordResetEvent(int numScans) {
        synchronized (mLock) {
            if (!mIsInSession) {
                return;
            }
            mCurrentSession.recordResetEvent(numScans, SystemClock.elapsedRealtime());

            mTotalSessions++;
            if (mSessions.size() < MAX_RECORDED_SESSIONS) {
                mSessions.add(mCurrentSession);
            }
            mIsInSession = false;
        }
    }

    /**
     * Returns the consolidated WifiWakeStats proto for WifiMetrics.
     */
    public WifiWakeStats buildProto() {
        WifiWakeStats proto = new WifiWakeStats();

        proto.numSessions = mTotalSessions;
        proto.sessions = new WifiWakeStats.Session[mSessions.size()];

        for (int i = 0; i < mSessions.size(); i++) {
            proto.sessions[i] = mSessions.get(i).buildProto();
        }

        return proto;
    }

    /**
     * Dump all WifiWake stats to console (pw)
     * @param pw
     */
    public void dump(PrintWriter pw) {
        synchronized (mLock) {
            pw.println("-------WifiWake metrics-------");
            pw.println("mTotalSessions: " + mTotalSessions);
            pw.println("mIsInSession: " + mIsInSession);
            pw.println("Stored Sessions: " + mSessions.size());
            for (Session session : mSessions) {
                session.dump(pw);
            }
            if (mCurrentSession != null) {
                pw.println("Current Session: ");
                mCurrentSession.dump(pw);
            }
            pw.println("----end of WifiWake metrics----");
        }
    }

    /**
     * Clears WifiWakeMetrics.
     *
     * <p>Keeps the current WifiWake session.
     */
    public void clear() {
        mSessions.clear();
        mTotalSessions = 0;
    }

    /** A single WifiWake session. */
    public static class Session {

        private final long mStartTimestamp;
        private final int mNumNetworks;

        @VisibleForTesting
        @Nullable
        Event mUnlockEvent;
        @VisibleForTesting
        @Nullable
        Event mWakeupEvent;
        @VisibleForTesting
        @Nullable
        Event mResetEvent;

        /** Creates a new WifiWake session. */
        public Session(int numNetworks, long timestamp) {
            mNumNetworks = numNetworks;
            mStartTimestamp = timestamp;
        }

        /**
         * Records an unlock event.
         *
         * <p>Ignores subsequent calls.
         *
         * @param numScans Total number of scans at the time of this event.
         * @param timestamp The timestamp of the event.
         */
        public void recordUnlockEvent(int numScans, long timestamp) {
            if (mUnlockEvent == null) {
                mUnlockEvent = new Event(numScans, timestamp - mStartTimestamp);
            }
        }

        /**
         * Records a wakeup event.
         *
         * <p>Ignores subsequent calls.
         *
         * @param numScans Total number of scans at the time of this event.
         * @param timestamp The timestamp of the event.
         */
        public void recordWakeupEvent(int numScans, long timestamp) {
            if (mWakeupEvent == null) {
                mWakeupEvent = new Event(numScans, timestamp - mStartTimestamp);
            }
        }

        /**
         * Records a reset event.
         *
         * <p>Ignores subsequent calls.
         *
         * @param numScans Total number of scans at the time of this event.
         * @param timestamp The timestamp of the event.
         */
        public void recordResetEvent(int numScans, long timestamp) {
            if (mResetEvent == null) {
                mResetEvent = new Event(numScans, timestamp - mStartTimestamp);
            }
        }

        /** Returns the proto representation of this session. */
        public WifiWakeStats.Session buildProto() {
            WifiWakeStats.Session sessionProto = new WifiWakeStats.Session();
            sessionProto.startTimeMillis = mStartTimestamp;
            sessionProto.lockedNetworksAtStart = mNumNetworks;

            if (mUnlockEvent != null) {
                sessionProto.unlockEvent = mUnlockEvent.buildProto();
            }
            if (mWakeupEvent != null) {
                sessionProto.wakeupEvent = mWakeupEvent.buildProto();
            }
            if (mResetEvent != null) {
                sessionProto.resetEvent = mResetEvent.buildProto();
            }

            return sessionProto;
        }

        /** Dumps the current state of the session. */
        public void dump(PrintWriter pw) {
            pw.println("WifiWakeMetrics.Session:");
            pw.println("mStartTimestamp: " + mStartTimestamp);
            pw.println("mNumNetworks: " + mNumNetworks);
            pw.println("mUnlockEvent: " + (mUnlockEvent == null ? "{}" : mUnlockEvent.toString()));
            pw.println("mWakeupEvent: " + (mWakeupEvent == null ? "{}" : mWakeupEvent.toString()));
            pw.println("mResetEvent: " + (mResetEvent == null ? "{}" : mResetEvent.toString()));
        }
    }

    /** An event in a WifiWake session. */
    public static class Event {

        /** Total number of scans that have elapsed prior to this event. */
        public final int mNumScans;
        /** Total elapsed time in milliseconds at the instant of this event. */
        public final long mElapsedTime;

        public Event(int numScans, long elapsedTime) {
            mNumScans = numScans;
            mElapsedTime = elapsedTime;
        }

        /** Returns the proto representation of this event. */
        public WifiWakeStats.Session.Event buildProto() {
            WifiWakeStats.Session.Event eventProto = new WifiWakeStats.Session.Event();
            eventProto.elapsedScans = mNumScans;
            eventProto.elapsedTimeMillis = mElapsedTime;
            return eventProto;
        }

        @Override
        public String toString() {
            return "{ mNumScans: " + mNumScans + ", elapsedTime: " + mElapsedTime + " }";
        }
    }
}