Skip to content

Commit

Permalink
Add sync reason to dumpsys
Browse files Browse the repository at this point in the history
Also:
 * add a second history section that logs
 * log mesg as text instead of number
 * dump Sync Status as a table

 Sample log:
Recent Sync History
  CyanogenDefy#1  : 2012-10-11 15:06:11     USER    0.4s            [email protected]/com.google u0  com.android.calendar                                  com.google.android.calendar
  CyanogenDefy#2  : 2012-10-11 15:06:11     USER    0.1s            [email protected]/com.google u0  subscribedfeeds                                       android.uid.system:1000
    mesg=parse-error
  zsol#3  : 2012-10-11 15:06:11     USER    0.0s            [email protected]/com.google u0  com.google.android.apps.uploader.PicasaUploadProvider android.uid.system:1000
  #4  : 2012-10-11 15:06:10     USER    0.1s            [email protected]/com.google u0  com.google.android.gms.plus.action                    android.uid.system:1000

Recent Sync History Extras
  CyanogenDefy#1  : 2012-10-11 15:06:11     USER   [email protected]/com.google u0  com.android.calendar                                  Bundle[{[email protected], force=true, ignore_settings=true, ignore_backoff=true}]
  CyanogenDefy#2  : 2012-10-11 15:06:11     USER   [email protected]/com.google u0  subscribedfeeds                                       Bundle[{ignore_backoff=true, force=true, ignore_settings=true}]
  zsol#3  : 2012-10-11 15:06:11     USER   [email protected]/com.google u0  com.google.android.apps.uploader.PicasaUploadProvider Bundle[{ignore_backoff=true, force=true, ignore_settings=true}]
  #4  : 2012-10-11 15:06:10     USER   [email protected]/com.google u0  com.google.android.gms.plus.action                    Bundle[{ignore_backoff=true, force=true, ignore_settings=true}]

Sync Status
Account [email protected] u0 com.google
=======================================================================
Authority                                           Syncable  Enabled  Delay  Loc  Poll  Per  Serv  User  Tot  Time  Last Sync            Periodic
-------------------------------------------------------------------------------------------------------------------------------------------------------------
com.google.android.apps.currents                    1         true            0    2     1    2     1     6    0:35  PERIODIC SUCCESS     86400
                                                                                                                     2012-10-12 14:59:40  2012-10-13 14:58:13
com.google.android.music.MusicContent               1         true            0    0     1    2     1     4    0:09  PERIODIC SUCCESS     86400
                                                                                                                     2012-10-12 14:59:18  2012-10-13 14:58:13
com.google.android.gms.plus.action                  1         true            0    0     1    1     1     3    0:00  PERIODIC SUCCESS     86400
                                                                                                                     2012-10-12 14:59:15  2012-10-13 14:58:13
com.google.android.apps.magazines                   1         true            0    1     1    2     1     5    0:14  PERIODIC SUCCESS     86400
                                                                                                                     2012-10-12 14:59:00  2012-10-13 14:58:13

Change-Id: Iffeb825e4b4f6217940a39b0dd71e06856f08f3f
  • Loading branch information
alonalbert committed Oct 19, 2012
1 parent 32d7f8c commit 57286f9
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 108 deletions.
19 changes: 19 additions & 0 deletions core/java/android/content/ContentResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ public abstract class ContentResolver {
/** @hide */
public static final int SYNC_ERROR_INTERNAL = 8;

private static final String[] SYNC_ERROR_NAMES = new String[] {
"already-in-progress",
"authentication-error",
"io-error",
"parse-error",
"conflict",
"too-many-deletions",
"too-many-retries",
"internal-error",
};

/** @hide */
static String syncErrorToString(int error) {
if (error < 1 || error > SYNC_ERROR_NAMES.length) {
return String.valueOf(error);
}
return SYNC_ERROR_NAMES[error - 1];
}

public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
Expand Down
6 changes: 4 additions & 2 deletions core/java/android/content/ContentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public void notifyChange(Uri uri, IContentObserver observer,
}
}

final int uid = Binder.getCallingUid();
// This makes it so that future permission checks will be in the context of this
// process rather than the caller's process. We will restore this before returning.
long identityToken = clearCallingIdentity();
Expand Down Expand Up @@ -264,7 +265,7 @@ public void notifyChange(Uri uri, IContentObserver observer,
if (syncToNetwork) {
SyncManager syncManager = getSyncManager();
if (syncManager != null) {
syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle,
syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle, uid,
uri.getAuthority());
}
}
Expand Down Expand Up @@ -300,14 +301,15 @@ public static final class ObserverCall {
public void requestSync(Account account, String authority, Bundle extras) {
ContentResolver.validateSyncExtrasBundle(extras);
int userId = UserHandle.getCallingUserId();
int uId = Binder.getCallingUid();

// This makes it so that future permission checks will be in the context of this
// process rather than the caller's process. We will restore this before returning.
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager != null) {
syncManager.scheduleSync(account, userId, authority, extras, 0 /* no delay */,
syncManager.scheduleSync(account, userId, uId, authority, extras, 0 /* no delay */,
false /* onlyThoseWithUnkownSyncableState */);
}
} finally {
Expand Down
299 changes: 219 additions & 80 deletions core/java/android/content/SyncManager.java

Large diffs are not rendered by default.

60 changes: 56 additions & 4 deletions core/java/android/content/SyncOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package android.content;

import android.accounts.Account;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.SystemClock;

Expand All @@ -25,8 +26,29 @@
* @hide
*/
public class SyncOperation implements Comparable {
public static final int REASON_BACKGROUND_DATA_SETTINGS_CHANGED = -1;
public static final int REASON_ACCOUNTS_UPDATED = -2;
public static final int REASON_SERVICE_CHANGED = -3;
public static final int REASON_PERIODIC = -4;
public static final int REASON_IS_SYNCABLE = -5;
public static final int REASON_SYNC_AUTO = -6;
public static final int REASON_MASTER_SYNC_AUTO = -7;
public static final int REASON_USER_START = -8;

private static String[] REASON_NAMES = new String[] {
"DataSettingsChanged",
"AccountsUpdated",
"ServiceChanged",
"Periodic",
"IsSyncable",
"AutoSync",
"MasterSyncAuto",
"UserStart",
};

public final Account account;
public final int userId;
public final int reason;
public int syncSource;
public String authority;
public final boolean allowParallelSyncs;
Expand All @@ -39,10 +61,12 @@ public class SyncOperation implements Comparable {
public long delayUntil;
public long effectiveRunTime;

public SyncOperation(Account account, int userId, int source, String authority, Bundle extras,
long delayInMs, long backoff, long delayUntil, boolean allowParallelSyncs) {
public SyncOperation(Account account, int userId, int reason, int source, String authority,
Bundle extras, long delayInMs, long backoff, long delayUntil,
boolean allowParallelSyncs) {
this.account = account;
this.userId = userId;
this.reason = reason;
this.syncSource = source;
this.authority = authority;
this.allowParallelSyncs = allowParallelSyncs;
Expand Down Expand Up @@ -78,6 +102,7 @@ private void removeFalseExtra(String extraName) {
SyncOperation(SyncOperation other) {
this.account = other.account;
this.userId = other.userId;
this.reason = other.reason;
this.syncSource = other.syncSource;
this.authority = other.authority;
this.extras = new Bundle(other.extras);
Expand All @@ -91,10 +116,10 @@ private void removeFalseExtra(String extraName) {
}

public String toString() {
return dump(true);
return dump(null, true);
}

public String dump(boolean useOneLine) {
public String dump(PackageManager pm, boolean useOneLine) {
StringBuilder sb = new StringBuilder()
.append(account.name)
.append(" u")
Expand All @@ -110,13 +135,40 @@ public String dump(boolean useOneLine) {
if (expedited) {
sb.append(", EXPEDITED");
}
sb.append(", reason: ");
sb.append(reasonToString(pm, reason));
if (!useOneLine && !extras.keySet().isEmpty()) {
sb.append("\n ");
extrasToStringBuilder(extras, sb);
}
return sb.toString();
}

public static String reasonToString(PackageManager pm, int reason) {
if (reason >= 0) {
if (pm != null) {
final String[] packages = pm.getPackagesForUid(reason);
if (packages != null && packages.length == 1) {
return packages[0];
}
final String name = pm.getNameForUid(reason);
if (name != null) {
return name;
}
return String.valueOf(reason);
} else {
return String.valueOf(reason);
}
} else {
final int index = -reason - 1;
if (index >= REASON_NAMES.length) {
return String.valueOf(reason);
} else {
return REASON_NAMES[index];
}
}
}

public boolean isInitialization() {
return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false);
}
Expand Down
16 changes: 10 additions & 6 deletions core/java/android/content/SyncQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package android.content;

import android.accounts.Account;
import android.content.pm.PackageManager;
import android.content.pm.RegisteredServicesCache;
import android.content.pm.RegisteredServicesCache.ServiceInfo;
import android.os.SystemClock;
import android.os.UserHandle;
Expand All @@ -40,15 +42,17 @@
*/
public class SyncQueue {
private static final String TAG = "SyncManager";

private final SyncStorageEngine mSyncStorageEngine;
private final SyncAdaptersCache mSyncAdapters;
private final PackageManager mPackageManager;

// A Map of SyncOperations operationKey -> SyncOperation that is designed for
// quick lookup of an enqueued SyncOperation.
private final HashMap<String, SyncOperation> mOperationsMap = Maps.newHashMap();

public SyncQueue(SyncStorageEngine syncStorageEngine, final SyncAdaptersCache syncAdapters) {
public SyncQueue(PackageManager packageManager, SyncStorageEngine syncStorageEngine,
final SyncAdaptersCache syncAdapters) {
mPackageManager = packageManager;
mSyncStorageEngine = syncStorageEngine;
mSyncAdapters = syncAdapters;
}
Expand All @@ -67,8 +71,8 @@ public void addPendingOperations(int userId) {
continue;
}
SyncOperation syncOperation = new SyncOperation(
op.account, op.userId, op.syncSource, op.authority, op.extras, 0 /* delay */,
backoff != null ? backoff.first : 0,
op.account, op.userId, op.reason, op.syncSource, op.authority, op.extras,
0 /* delay */, backoff != null ? backoff.first : 0,
mSyncStorageEngine.getDelayUntilTime(op.account, op.userId, op.authority),
syncAdapterInfo.type.allowParallelSyncs());
syncOperation.expedited = op.expedited;
Expand Down Expand Up @@ -112,7 +116,7 @@ private boolean add(SyncOperation operation,
operation.pendingOperation = pop;
if (operation.pendingOperation == null) {
pop = new SyncStorageEngine.PendingOperation(
operation.account, operation.userId, operation.syncSource,
operation.account, operation.userId, operation.reason, operation.syncSource,
operation.authority, operation.extras, operation.expedited);
pop = mSyncStorageEngine.insertIntoPending(pop);
if (pop == null) {
Expand Down Expand Up @@ -214,7 +218,7 @@ public void dump(StringBuilder sb) {
sb.append(DateUtils.formatElapsedTime((operation.effectiveRunTime - now) / 1000));
}
sb.append(" - ");
sb.append(operation.dump(false)).append("\n");
sb.append(operation.dump(mPackageManager, false)).append("\n");
}
}
}
Loading

0 comments on commit 57286f9

Please sign in to comment.