MetricsReader.java revision b62371434c9b63560c78a85123fe9386edac1205
126ca65d42523fca95081d21589f46708987d647cChris Wren/*
226ca65d42523fca95081d21589f46708987d647cChris Wren * Copyright (C) 2017 The Android Open Source Project
326ca65d42523fca95081d21589f46708987d647cChris Wren *
426ca65d42523fca95081d21589f46708987d647cChris Wren * Licensed under the Apache License, Version 2.0 (the "License");
526ca65d42523fca95081d21589f46708987d647cChris Wren * you may not use this file except in compliance with the License.
626ca65d42523fca95081d21589f46708987d647cChris Wren * You may obtain a copy of the License at
726ca65d42523fca95081d21589f46708987d647cChris Wren *
826ca65d42523fca95081d21589f46708987d647cChris Wren *      http://www.apache.org/licenses/LICENSE-2.0
926ca65d42523fca95081d21589f46708987d647cChris Wren *
1026ca65d42523fca95081d21589f46708987d647cChris Wren * Unless required by applicable law or agreed to in writing, software
1126ca65d42523fca95081d21589f46708987d647cChris Wren * distributed under the License is distributed on an "AS IS" BASIS,
1226ca65d42523fca95081d21589f46708987d647cChris Wren * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1326ca65d42523fca95081d21589f46708987d647cChris Wren * See the License for the specific language governing permissions and
1426ca65d42523fca95081d21589f46708987d647cChris Wren * limitations under the License.
1526ca65d42523fca95081d21589f46708987d647cChris Wren */
16b62371434c9b63560c78a85123fe9386edac1205Chris Wrenpackage android.metrics;
17b62371434c9b63560c78a85123fe9386edac1205Chris Wren
18b62371434c9b63560c78a85123fe9386edac1205Chris Wrenimport android.annotation.SystemApi;
1926ca65d42523fca95081d21589f46708987d647cChris Wren
2026ca65d42523fca95081d21589f46708987d647cChris Wrenimport com.android.internal.logging.legacy.LegacyConversionLogger;
2126ca65d42523fca95081d21589f46708987d647cChris Wrenimport com.android.internal.logging.legacy.EventLogCollector;
2226ca65d42523fca95081d21589f46708987d647cChris Wren
2326ca65d42523fca95081d21589f46708987d647cChris Wrenimport java.util.Queue;
2426ca65d42523fca95081d21589f46708987d647cChris Wren
2526ca65d42523fca95081d21589f46708987d647cChris Wren/**
2626ca65d42523fca95081d21589f46708987d647cChris Wren * Read platform logs.
27b62371434c9b63560c78a85123fe9386edac1205Chris Wren * @hide
2826ca65d42523fca95081d21589f46708987d647cChris Wren */
29b62371434c9b63560c78a85123fe9386edac1205Chris Wren@SystemApi
3026ca65d42523fca95081d21589f46708987d647cChris Wrenpublic class MetricsReader {
3126ca65d42523fca95081d21589f46708987d647cChris Wren    private EventLogCollector mReader;
32b62371434c9b63560c78a85123fe9386edac1205Chris Wren    private Queue<LogMaker> mEventQueue;
3326ca65d42523fca95081d21589f46708987d647cChris Wren    private long mLastEventMs;
3426ca65d42523fca95081d21589f46708987d647cChris Wren    private long mCheckpointMs;
3526ca65d42523fca95081d21589f46708987d647cChris Wren
3626ca65d42523fca95081d21589f46708987d647cChris Wren    /** Open a new session and start reading logs.
3726ca65d42523fca95081d21589f46708987d647cChris Wren     *
3826ca65d42523fca95081d21589f46708987d647cChris Wren     * Starts reading from the oldest log not already read by this reader object.
3926ca65d42523fca95081d21589f46708987d647cChris Wren     * On first invocation starts from the oldest available log ion the system.
4026ca65d42523fca95081d21589f46708987d647cChris Wren     */
4126ca65d42523fca95081d21589f46708987d647cChris Wren    public void read(long startMs) {
4226ca65d42523fca95081d21589f46708987d647cChris Wren        EventLogCollector reader = EventLogCollector.getInstance();
4326ca65d42523fca95081d21589f46708987d647cChris Wren        LegacyConversionLogger logger = new LegacyConversionLogger();
4426ca65d42523fca95081d21589f46708987d647cChris Wren        mLastEventMs = reader.collect(logger, startMs);
4526ca65d42523fca95081d21589f46708987d647cChris Wren        mEventQueue = logger.getEvents();
4626ca65d42523fca95081d21589f46708987d647cChris Wren    }
4726ca65d42523fca95081d21589f46708987d647cChris Wren
4826ca65d42523fca95081d21589f46708987d647cChris Wren    public void checkpoint() {
4926ca65d42523fca95081d21589f46708987d647cChris Wren        read(0L);
5026ca65d42523fca95081d21589f46708987d647cChris Wren        mCheckpointMs = mLastEventMs;
5126ca65d42523fca95081d21589f46708987d647cChris Wren        mEventQueue = null;
5226ca65d42523fca95081d21589f46708987d647cChris Wren    }
5326ca65d42523fca95081d21589f46708987d647cChris Wren
5426ca65d42523fca95081d21589f46708987d647cChris Wren    public void reset() {
5526ca65d42523fca95081d21589f46708987d647cChris Wren        read(mCheckpointMs);
5626ca65d42523fca95081d21589f46708987d647cChris Wren    }
5726ca65d42523fca95081d21589f46708987d647cChris Wren
5826ca65d42523fca95081d21589f46708987d647cChris Wren    /* Does the current log session have another entry? */
5926ca65d42523fca95081d21589f46708987d647cChris Wren    public boolean hasNext() {
6026ca65d42523fca95081d21589f46708987d647cChris Wren        return mEventQueue == null ? false : !mEventQueue.isEmpty();
6126ca65d42523fca95081d21589f46708987d647cChris Wren    }
6226ca65d42523fca95081d21589f46708987d647cChris Wren
6326ca65d42523fca95081d21589f46708987d647cChris Wren    /* Next entry in the current log session. */
64b62371434c9b63560c78a85123fe9386edac1205Chris Wren    public LogMaker next() {
6526ca65d42523fca95081d21589f46708987d647cChris Wren        return mEventQueue == null ? null : mEventQueue.remove();
6626ca65d42523fca95081d21589f46708987d647cChris Wren    }
6726ca65d42523fca95081d21589f46708987d647cChris Wren
6826ca65d42523fca95081d21589f46708987d647cChris Wren}
69