Merge "Refactor ChangeReporter and rate limit stats logging."

This commit is contained in:
Treehugger Robot
2019-09-26 17:25:40 +00:00
committed by Gerrit Code Review
3 changed files with 81 additions and 41 deletions

View File

@@ -18,7 +18,6 @@ package android.app;
import android.compat.Compatibility;
import android.os.Process;
import android.util.Log;
import android.util.StatsLog;
import com.android.internal.compat.ChangeReporter;
@@ -31,8 +30,6 @@ import java.util.Arrays;
* @hide
*/
public final class AppCompatCallbacks extends Compatibility.Callbacks {
private static final String TAG = "Compatibility";
private final long[] mDisabledChanges;
private final ChangeReporter mChangeReporter;
@@ -48,7 +45,8 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks {
private AppCompatCallbacks(long[] disabledChanges) {
mDisabledChanges = Arrays.copyOf(disabledChanges, disabledChanges.length);
Arrays.sort(mDisabledChanges);
mChangeReporter = new ChangeReporter();
mChangeReporter = new ChangeReporter(
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS);
}
protected void reportChange(long changeId) {
@@ -67,10 +65,7 @@ public final class AppCompatCallbacks extends Compatibility.Callbacks {
private void reportChange(long changeId, int state) {
int uid = Process.myUid();
//TODO(b/138374585): Implement rate limiting for the logs.
Log.d(TAG, ChangeReporter.createLogString(uid, changeId, state));
mChangeReporter.reportChange(uid, changeId,
state, /* source */StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__APP_PROCESS);
mChangeReporter.reportChange(uid, changeId, state);
}
}

View File

@@ -16,14 +16,89 @@
package com.android.internal.compat;
import android.util.Log;
import android.util.Slog;
import android.util.StatsLog;
import com.android.internal.annotations.GuardedBy;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
/**
* A helper class to report changes to stats log.
*
* @hide
*/
public final class ChangeReporter {
private static final String TAG = "CompatibilityChangeReporter";
private int mSource;
private final class ChangeReport {
int mUid;
long mChangeId;
int mState;
ChangeReport(int uid, long changeId, int state) {
mUid = uid;
mChangeId = changeId;
mState = state;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ChangeReport that = (ChangeReport) o;
return mUid == that.mUid
&& mChangeId == that.mChangeId
&& mState == that.mState;
}
@Override
public int hashCode() {
return Objects.hash(mUid, mChangeId, mState);
}
}
@GuardedBy("mReportedChanges")
private Set<ChangeReport> mReportedChanges = new HashSet<>();
public ChangeReporter(int source) {
mSource = source;
}
/**
* Report the change to stats log.
*
* @param uid affected by the change
* @param changeId the reported change id
* @param state of the reported change - enabled/disabled/only logged
*/
public void reportChange(int uid, long changeId, int state) {
debugLog(uid, changeId, state);
ChangeReport report = new ChangeReport(uid, changeId, state);
synchronized (mReportedChanges) {
if (!mReportedChanges.contains(report)) {
StatsLog.write(StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, changeId,
state, mSource);
mReportedChanges.add(report);
}
}
}
private void debugLog(int uid, long changeId, int state) {
//TODO(b/138374585): Implement rate limiting for the logs.
String message = String.format("Compat change id reported: %d; UID %d; state: %s", changeId,
uid, stateToString(state));
if (mSource == StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER) {
Slog.d(TAG, message);
} else {
Log.d(TAG, message);
}
}
/**
* Transforms StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__STATE enum to a string.
@@ -43,31 +118,4 @@ public final class ChangeReporter {
return "UNKNOWN";
}
}
/**
* Constructs and returns a string to be logged to logcat when a change is reported.
*
* @param uid affected by the change
* @param changeId the reported change id
* @param state of the reported change - enabled/disabled/only logged
* @return string to log
*/
public static String createLogString(int uid, long changeId, int state) {
return String.format("Compat change id reported: %d; UID %d; state: %s", changeId, uid,
stateToString(state));
}
/**
* Report the change to stats log.
*
* @param uid affected by the change
* @param changeId the reported change id
* @param state of the reported change - enabled/disabled/only logged
* @param source of the logging - app process or system server
*/
public void reportChange(int uid, long changeId, int state, int source) {
//TODO(b/138374585): Implement rate limiting for stats log.
StatsLog.write(StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED, uid, changeId,
state, source);
}
}

View File

@@ -41,7 +41,8 @@ public class PlatformCompat extends IPlatformCompat.Stub {
public PlatformCompat(Context context) {
mContext = context;
mChangeReporter = new ChangeReporter();
mChangeReporter = new ChangeReporter(
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
}
@Override
@@ -96,10 +97,6 @@ public class PlatformCompat extends IPlatformCompat.Stub {
private void reportChange(long changeId, ApplicationInfo appInfo, int state) {
int uid = appInfo.uid;
//TODO(b/138374585): Implement rate limiting for the logs.
Slog.d(TAG, ChangeReporter.createLogString(uid, changeId, state));
mChangeReporter.reportChange(uid, changeId,
state, /* source */
StatsLog.APP_COMPATIBILITY_CHANGE_REPORTED__SOURCE__SYSTEM_SERVER);
mChangeReporter.reportChange(uid, changeId, state);
}
}