From a50fdae6108bfca1573544e6edcc01d05a579636 Mon Sep 17 00:00:00 2001 From: Olivier Gaillard Date: Wed, 14 Nov 2018 17:15:02 +0000 Subject: [PATCH] Update documentation for ThreadLocalWorkSource. Test: unit test Change-Id: I446af267dafd069b1bf5f64a84347b69fc6fec60 --- .../android/os/ThreadLocalWorkSource.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/core/java/android/os/ThreadLocalWorkSource.java b/core/java/android/os/ThreadLocalWorkSource.java index 53dd460d62c4a..894b1cc475b0e 100644 --- a/core/java/android/os/ThreadLocalWorkSource.java +++ b/core/java/android/os/ThreadLocalWorkSource.java @@ -17,6 +17,24 @@ package android.os; /** + * Tracks who triggered the work currently executed on this thread. + * + *

ThreadLocalWorkSource is automatically updated inside system server for incoming/outgoing + * binder calls and messages posted to handler threads. + * + *

ThreadLocalWorkSource can also be set manually if needed to refine the WorkSource. + * + *

Example: + *

+ * * @hide Only for use within system server. */ public final class ThreadLocalWorkSource { @@ -24,24 +42,53 @@ public final class ThreadLocalWorkSource { private static final ThreadLocal sWorkSourceUid = ThreadLocal.withInitial(() -> UID_NONE); - /** Returns the original work source uid. */ + /** + * Returns the UID to blame for the code currently executed on this thread. + * + *

This UID is set automatically by common frameworks (e.g. Binder and Handler frameworks) + * and automatically propagated inside system server. + *

It can also be set manually using {@link #setUid(int)}. + */ public static int getUid() { return sWorkSourceUid.get(); } - /** Sets the original work source uid. */ + /** + * Sets the UID to blame for the code currently executed on this thread. + * + *

Inside system server, this UID will be automatically propagated. + *

It will be used to attribute future resources used on this thread (e.g. binder + * transactions or processing handler messages) and on any other threads the UID is propagated + * to. + * + * @return a token that can be used to restore the state. + */ public static long setUid(int uid) { final long token = getToken(); sWorkSourceUid.set(uid); return token; } - /** Restores the state using the provided token. */ + /** + * Restores the state using the provided token. + */ public static void restore(long token) { sWorkSourceUid.set(parseUidFromToken(token)); } - /** Clears the stored work source uid. */ + /** + * Clears the stored work source uid. + * + *

This method should be used when we do not know who to blame. If the UID to blame is the + * UID of the current process, it is better to attribute the work to the current process + * explicitly instead of clearing the work source: + * + *

+     * ThreadLocalWorkSource.setUid(Process.myUid());
+     * 
+ * + * @return a token that can be used to restore the state. + **/ public static long clear() { return setUid(UID_NONE); }