Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3132b32 authored by Jorge Ruesga's avatar Jorge Ruesga
Browse files

base: fix battery stats

* Fix IBatteryStats.aidl posititions. BatteryService.cpp from hardware sensors uses a position token
from the binder. The dock battery patch broke this order, causing sensor stats to be send to
wakelock stats, causing a breakage in the partial wakelock counter.
* Create a separate dock stats implementation to avoid collision in the internal statics variables
of the normal stats implementation.

Change-Id: I22c92d5941804449668530715bf5381b00164593
JIRA: CYAN-3127
Issue: https://jira.cyanogenmod.org/browse/CYAN-3127


Signed-off-by: default avatarJorge Ruesga <jorge@ruesga.com>
parent 451fa53c
Loading
Loading
Loading
Loading
+8 −5
Original line number Diff line number Diff line
@@ -23,10 +23,6 @@ import android.telephony.SignalStrength;

interface IBatteryStats {
    byte[] getStatistics();
    /** @hide **/
    byte[] getDockStatistics();
    /** @hide **/
    void resetStatistics();
    void noteStartWakelock(int uid, int pid, String name, int type);
    void noteStopWakelock(int uid, int pid, String name, int type);
    
@@ -79,7 +75,14 @@ interface IBatteryStats {
    void noteNetworkInterfaceType(String iface, int type);
    void noteNetworkStatsEnabled();
    void setBatteryState(int status, int health, int plugType, int level, int temp, int volt);
    void setDockBatteryState(int status, int health, int plugType, int level, int temp, int volt);
    long getAwakeTimeBattery();
    long getAwakeTimePlugged();

    // CM Implementation
    /** @hide **/
    byte[] getDockStatistics();
    /** @hide **/
    void resetStatistics();
    /** @hide **/
    void setDockBatteryState(int status, int health, int plugType, int level, int temp, int volt);
}
+25 −17
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ import java.util.concurrent.locks.ReentrantLock;
 * battery life.  All times are represented in microseconds except where indicated
 * otherwise.
 */
public final class BatteryStatsImpl extends BatteryStats {
public class BatteryStatsImpl extends BatteryStats {
    private static final String TAG = "BatteryStatsImpl";
    private static final boolean DEBUG = false;
    private static final boolean DEBUG_HISTORY = false;
@@ -1309,7 +1309,7 @@ public final class BatteryStatsImpl extends BatteryStats {
        synchronized(this) {
            Map<String, KernelWakelockStats> m = mProcWakelockFileStats;

            sKernelWakelockUpdateVersion++;
            setKernelWakelockUpdateVersion(getKernelWakelockUpdateVersion() + 1);
            while (endIndex < len) {
                for (endIndex=startIndex;
                        endIndex < len && wlBuffer[endIndex] != '\n' && wlBuffer[endIndex] != '\0';
@@ -1348,17 +1348,17 @@ public final class BatteryStatsImpl extends BatteryStats {
                if (parsed && name.length() > 0) {
                    if (!m.containsKey(name)) {
                        m.put(name, new KernelWakelockStats(count, totalTime,
                                sKernelWakelockUpdateVersion));
                                getKernelWakelockUpdateVersion()));
                        numUpdatedWlNames++;
                    } else {
                        KernelWakelockStats kwlStats = m.get(name);
                        if (kwlStats.mVersion == sKernelWakelockUpdateVersion) {
                        if (kwlStats.mVersion == getKernelWakelockUpdateVersion()) {
                            kwlStats.mCount += count;
                            kwlStats.mTotalTime += totalTime;
                        } else {
                            kwlStats.mCount = count;
                            kwlStats.mTotalTime = totalTime;
                            kwlStats.mVersion = sKernelWakelockUpdateVersion;
                            kwlStats.mVersion = getKernelWakelockUpdateVersion();
                            numUpdatedWlNames++;
                        }
                    }
@@ -1370,7 +1370,7 @@ public final class BatteryStatsImpl extends BatteryStats {
                // Don't report old data.
                Iterator<KernelWakelockStats> itr = m.values().iterator();
                while (itr.hasNext()) {
                    if (itr.next().mVersion != sKernelWakelockUpdateVersion) {
                    if (itr.next().mVersion != getKernelWakelockUpdateVersion()) {
                        itr.remove();
                    }
                }
@@ -4354,10 +4354,6 @@ public final class BatteryStatsImpl extends BatteryStats {
                    wl = mWakelockStats.get(name);
                }
                if (wl == null) {
                    // protect from unnamed wakelocks
                    if (name == null) {
                        name = "undefined-wakelock-timer";
                    }
                    wl = new Wakelock();
                    mWakelockStats.put(name, wl);
                }
@@ -4543,7 +4539,7 @@ public final class BatteryStatsImpl extends BatteryStats {
    }

    public void setNumSpeedSteps(int steps) {
        if (sNumSpeedSteps == 0) sNumSpeedSteps = steps;
        if (getCpuSpeedSteps() == 0) setCpuSpeedSteps(steps);
    }

    public void setRadioScanningTimeout(long timeout) {
@@ -4912,14 +4908,14 @@ public final class BatteryStatsImpl extends BatteryStats {
            }
            kwlt.updateCurrentReportedCount(kws.mCount);
            kwlt.updateCurrentReportedTotalTime(kws.mTotalTime);
            kwlt.setUpdateVersion(sKernelWakelockUpdateVersion);
            kwlt.setUpdateVersion(getKernelWakelockUpdateVersion());
        }

        if (m.size() != mKernelWakelockStats.size()) {
            // Set timers to stale if they didn't appear in /proc/wakelocks this time.
            for (Map.Entry<String, SamplingTimer> ent : mKernelWakelockStats.entrySet()) {
                SamplingTimer st = ent.getValue();
                if (st.getUpdateVersion() != sKernelWakelockUpdateVersion) {
                if (st.getUpdateVersion() != getKernelWakelockUpdateVersion()) {
                    st.setStale();
                }
            }
@@ -5155,6 +5151,18 @@ public final class BatteryStatsImpl extends BatteryStats {
        return sNumSpeedSteps;
    }

    protected void setCpuSpeedSteps(int numSpeedSteps) {
        sNumSpeedSteps = numSpeedSteps;
    }

    protected int getKernelWakelockUpdateVersion() {
        return sKernelWakelockUpdateVersion;
    }

    protected void setKernelWakelockUpdateVersion(int kernelWakelockUpdateVersion) {
        sKernelWakelockUpdateVersion = kernelWakelockUpdateVersion;
    }

    /**
     * Retrieve the statistics object for a particular uid, creating if needed.
     */
@@ -5563,7 +5571,7 @@ public final class BatteryStatsImpl extends BatteryStats {
            }
        }

        sNumSpeedSteps = in.readInt();
        setCpuSpeedSteps(in.readInt());

        final int NU = in.readInt();
        if (NU > 10000) {
@@ -5780,7 +5788,7 @@ public final class BatteryStatsImpl extends BatteryStats {
            }
        }

        out.writeInt(sNumSpeedSteps);
        out.writeInt(getCpuSpeedSteps());
        final int NU = mUidStats.size();
        out.writeInt(NU);
        for (int iu = 0; iu < NU; iu++) {
@@ -6051,7 +6059,7 @@ public final class BatteryStatsImpl extends BatteryStats {
        mWifiBatchedScanTimers.clear();
        mWifiMulticastTimers.clear();

        sNumSpeedSteps = in.readInt();
        setCpuSpeedSteps(in.readInt());

        int numUids = in.readInt();
        mUidStats.clear();
@@ -6150,7 +6158,7 @@ public final class BatteryStatsImpl extends BatteryStats {
            out.writeInt(0);
        }

        out.writeInt(sNumSpeedSteps);
        out.writeInt(getCpuSpeedSteps());

        if (inclUids) {
            int size = mUidStats.size();
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2006-2007 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.internal.os;

import android.os.Parcel;
import android.os.Parcelable;

public final class DockBatteryStatsImpl extends BatteryStatsImpl {
    private static int sNumSpeedSteps;
    private static int sKernelWakelockUpdateVersion = 0;

    public DockBatteryStatsImpl() {
        super();
    }

    public DockBatteryStatsImpl(Parcel p) {
        super(p);
    }

    public DockBatteryStatsImpl(String filename) {
        super(filename);
    }

    @Override
    public int getCpuSpeedSteps() {
        return sNumSpeedSteps;
    }

    @Override
    protected void setCpuSpeedSteps(int numSpeedSteps) {
        sNumSpeedSteps = numSpeedSteps;
    }

    @Override
    protected int getKernelWakelockUpdateVersion() {
        return sKernelWakelockUpdateVersion;
    }

    @Override
    protected void setKernelWakelockUpdateVersion(int kernelWakelockUpdateVersion) {
        sKernelWakelockUpdateVersion = kernelWakelockUpdateVersion;
    }

    public static final Parcelable.Creator<DockBatteryStatsImpl> CREATOR =
        new Parcelable.Creator<DockBatteryStatsImpl>() {
        public DockBatteryStatsImpl createFromParcel(Parcel in) {
            return new DockBatteryStatsImpl(in);
        }

        public DockBatteryStatsImpl[] newArray(int size) {
            return new DockBatteryStatsImpl[size];
        }
    };
}
+2 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.util.Slog;

import com.android.internal.app.IBatteryStats;
import com.android.internal.os.BatteryStatsImpl;
import com.android.internal.os.DockBatteryStatsImpl;
import com.android.internal.os.PowerProfile;

import java.io.FileDescriptor;
@@ -59,7 +60,7 @@ public final class BatteryStatsService extends IBatteryStats.Stub {

    BatteryStatsService(String filename, String dockfilename) {
        mStats = new BatteryStatsImpl(filename);
        mDockStats = new BatteryStatsImpl(dockfilename);
        mDockStats = new DockBatteryStatsImpl(dockfilename);
    }
    
    public void publish(Context context) {