Merge commit '2f8c9e6825e31614f26f8d9f3c1de7be2f3e9f9e' into kraken * commit '2f8c9e6825e31614f26f8d9f3c1de7be2f3e9f9e': Misc fixes for throttling.
This commit is contained in:
@@ -20,7 +20,6 @@ import android.app.AlarmManager;
|
|||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -79,8 +78,6 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
private static final int TESTING_RESET_PERIOD_SEC = 60 * 10;
|
private static final int TESTING_RESET_PERIOD_SEC = 60 * 10;
|
||||||
private static final long TESTING_THRESHOLD = 1 * 1024 * 1024;
|
private static final long TESTING_THRESHOLD = 1 * 1024 * 1024;
|
||||||
|
|
||||||
private static final int PERIOD_COUNT = 6;
|
|
||||||
|
|
||||||
private int mPolicyPollPeriodSec;
|
private int mPolicyPollPeriodSec;
|
||||||
private long mPolicyThreshold;
|
private long mPolicyThreshold;
|
||||||
private int mPolicyThrottleValue;
|
private int mPolicyThrottleValue;
|
||||||
@@ -106,7 +103,6 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
private String mIface;
|
private String mIface;
|
||||||
|
|
||||||
private static final int NOTIFICATION_WARNING = 2;
|
private static final int NOTIFICATION_WARNING = 2;
|
||||||
private static final int NOTIFICATION_ALL = 0xFFFFFFFF;
|
|
||||||
|
|
||||||
private Notification mThrottlingNotification;
|
private Notification mThrottlingNotification;
|
||||||
private boolean mWarningNotificationSent = false;
|
private boolean mWarningNotificationSent = false;
|
||||||
@@ -177,32 +173,31 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
"ThrottleService");
|
"ThrottleService");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long ntpToWallTime(long ntpTime) {
|
||||||
|
long bestNow = getBestTime();
|
||||||
|
long localNow = System.currentTimeMillis();
|
||||||
|
return localNow + (ntpTime - bestNow);
|
||||||
|
}
|
||||||
|
|
||||||
// TODO - fetch for the iface
|
// TODO - fetch for the iface
|
||||||
// return time in the local, system wall time, correcting for the use of ntp
|
// return time in the local, system wall time, correcting for the use of ntp
|
||||||
|
|
||||||
public synchronized long getResetTime(String iface) {
|
public synchronized long getResetTime(String iface) {
|
||||||
enforceAccessPermission();
|
enforceAccessPermission();
|
||||||
long resetTime = 0;
|
long resetTime = 0;
|
||||||
if (mRecorder != null) {
|
if (mRecorder != null) {
|
||||||
long bestEnd = mRecorder.getPeriodEnd();
|
resetTime = ntpToWallTime(mRecorder.getPeriodEnd());
|
||||||
long bestNow = getBestTime();
|
|
||||||
long localNow = System.currentTimeMillis();
|
|
||||||
|
|
||||||
resetTime = localNow + (bestEnd - bestNow);
|
|
||||||
}
|
}
|
||||||
return resetTime;
|
return resetTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO - fetch for the iface
|
// TODO - fetch for the iface
|
||||||
// return time in the loca, system wall tiem, correcting for the use of ntp
|
// return time in the local, system wall time, correcting for the use of ntp
|
||||||
public synchronized long getPeriodStartTime(String iface) {
|
public synchronized long getPeriodStartTime(String iface) {
|
||||||
enforceAccessPermission();
|
enforceAccessPermission();
|
||||||
long startTime = 0;
|
long startTime = 0;
|
||||||
if (mRecorder != null) {
|
if (mRecorder != null) {
|
||||||
long bestStart = mRecorder.getPeriodStart();
|
startTime = ntpToWallTime(mRecorder.getPeriodStart());
|
||||||
long bestNow = getBestTime();
|
|
||||||
long localNow = System.currentTimeMillis();
|
|
||||||
|
|
||||||
startTime = localNow + (bestStart - bestNow);
|
|
||||||
}
|
}
|
||||||
return startTime;
|
return startTime;
|
||||||
}
|
}
|
||||||
@@ -440,8 +435,8 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
Intent broadcast = new Intent(ThrottleManager.THROTTLE_POLL_ACTION);
|
Intent broadcast = new Intent(ThrottleManager.THROTTLE_POLL_ACTION);
|
||||||
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_READ, periodRx);
|
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_READ, periodRx);
|
||||||
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_WRITE, periodTx);
|
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_WRITE, periodTx);
|
||||||
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_START, ThrottleService.this.getPeriodStartTime(mIface));
|
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_START, getPeriodStartTime(mIface));
|
||||||
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_END, ThrottleService.this.getResetTime(mIface));
|
broadcast.putExtra(ThrottleManager.EXTRA_CYCLE_END, getResetTime(mIface));
|
||||||
mContext.sendStickyBroadcast(broadcast);
|
mContext.sendStickyBroadcast(broadcast);
|
||||||
|
|
||||||
mAlarmManager.cancel(mPendingPollIntent);
|
mAlarmManager.cancel(mPendingPollIntent);
|
||||||
@@ -625,6 +620,7 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
|
|
||||||
if (mRecorder.setNextPeriod(start, end)) {
|
if (mRecorder.setNextPeriod(start, end)) {
|
||||||
clearThrottleAndNotification();
|
clearThrottleAndNotification();
|
||||||
|
onPollAlarm();
|
||||||
}
|
}
|
||||||
|
|
||||||
mAlarmManager.cancel(mPendingResetIntent);
|
mAlarmManager.cancel(mPendingResetIntent);
|
||||||
@@ -642,31 +638,40 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
private void checkForAuthoritativeTime() {
|
private void checkForAuthoritativeTime() {
|
||||||
if (mNtpActive || (mNtpServer == null)) return;
|
if (mNtpActive || (mNtpServer == null)) return;
|
||||||
|
|
||||||
SntpClient client = new SntpClient();
|
// will try to get the ntp time and switch to it if found.
|
||||||
if (client.requestTime(mNtpServer, 10000)) {
|
// will also cache the time so we don't fetch it repeatedly.
|
||||||
mNtpActive = true;
|
getBestTime();
|
||||||
if (DBG) Slog.d(TAG, "found Authoritative time - reseting alarm");
|
|
||||||
mHandler.obtainMessage(EVENT_RESET_ALARM).sendToTarget();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final int MAX_NTP_CACHE_AGE = 30 * 1000;
|
||||||
|
private static final int MAX_NTP_FETCH_WAIT = 10 * 1000;
|
||||||
|
private long cachedNtp;
|
||||||
|
private long cachedNtpTimestamp;
|
||||||
|
|
||||||
private long getBestTime() {
|
private long getBestTime() {
|
||||||
|
if (mNtpServer != null) {
|
||||||
|
if (mNtpActive) {
|
||||||
|
long ntpAge = SystemClock.elapsedRealtime() - cachedNtpTimestamp;
|
||||||
|
if (ntpAge < MAX_NTP_CACHE_AGE) {
|
||||||
|
return cachedNtp + ntpAge;
|
||||||
|
}
|
||||||
|
}
|
||||||
SntpClient client = new SntpClient();
|
SntpClient client = new SntpClient();
|
||||||
|
if (client.requestTime(mNtpServer, MAX_NTP_FETCH_WAIT)) {
|
||||||
long time;
|
cachedNtp = client.getNtpTime();
|
||||||
if ((mNtpServer != null) && client.requestTime(mNtpServer, 10000)) {
|
cachedNtpTimestamp = SystemClock.elapsedRealtime();
|
||||||
time = client.getNtpTime() ;
|
|
||||||
if (!mNtpActive) {
|
if (!mNtpActive) {
|
||||||
mNtpActive = true;
|
mNtpActive = true;
|
||||||
if (DBG) Slog.d(TAG, "found Authoritative time - reseting alarm");
|
if (DBG) Slog.d(TAG, "found Authoritative time - reseting alarm");
|
||||||
mHandler.obtainMessage(EVENT_RESET_ALARM).sendToTarget();
|
mHandler.obtainMessage(EVENT_RESET_ALARM).sendToTarget();
|
||||||
}
|
}
|
||||||
if (DBG) Slog.d(TAG, "using Authoritative time: " + time);
|
if (DBG) Slog.d(TAG, "using Authoritative time: " + cachedNtp);
|
||||||
} else {
|
return cachedNtp;
|
||||||
time = System.currentTimeMillis();
|
}
|
||||||
|
}
|
||||||
|
long time = System.currentTimeMillis();
|
||||||
if (DBG) Slog.d(TAG, "using User time: " + time);
|
if (DBG) Slog.d(TAG, "using User time: " + time);
|
||||||
mNtpActive = false;
|
mNtpActive = false;
|
||||||
}
|
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -705,7 +710,6 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
mPeriodStart = Calendar.getInstance();
|
mPeriodStart = Calendar.getInstance();
|
||||||
mPeriodEnd = Calendar.getInstance();
|
mPeriodEnd = Calendar.getInstance();
|
||||||
|
|
||||||
zeroData(0);
|
|
||||||
retrieve();
|
retrieve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -915,6 +919,9 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void retrieve() {
|
private void retrieve() {
|
||||||
|
// clean out any old data first. If we fail to read we don't want old stuff
|
||||||
|
zeroData(0);
|
||||||
|
|
||||||
File f = getDataFile();
|
File f = getDataFile();
|
||||||
byte[] buffer;
|
byte[] buffer;
|
||||||
FileInputStream s = null;
|
FileInputStream s = null;
|
||||||
@@ -952,7 +959,7 @@ public class ThrottleService extends IThrottleManager.Stub {
|
|||||||
|
|
||||||
mPeriodCount = Integer.parseInt(parsed[parsedUsed++]);
|
mPeriodCount = Integer.parseInt(parsed[parsedUsed++]);
|
||||||
if (parsed.length != 5 + (2 * mPeriodCount)) {
|
if (parsed.length != 5 + (2 * mPeriodCount)) {
|
||||||
Slog.e(TAG, "reading data file with bad length ("+parsed.length+" != "+(4 + (2*mPeriodCount))+") - ignoring");
|
Slog.e(TAG, "reading data file with bad length ("+parsed.length+" != "+(5 + (2*mPeriodCount))+") - ignoring");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user