1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.os;
18 
19 /**
20  * Tracks who triggered the work currently executed on this thread.
21  *
22  * <p>ThreadLocalWorkSource is automatically updated inside system server for incoming/outgoing
23  * binder calls and messages posted to handler threads.
24  *
25  * <p>ThreadLocalWorkSource can also be set manually if needed to refine the WorkSource.
26  *
27  * <p>Example:
28  * <ul>
29  * <li>Bluetooth process calls {@link PowerManager#isInteractive()} API on behalf of app foo.
30  * <li>ThreadLocalWorkSource will be automatically set to the UID of foo.
31  * <li>Any code on the thread handling {@link PowerManagerService#isInteractive()} can call
32  * {@link ThreadLocalWorkSource#getUid()} to blame any resource used to handle this call.
33  * <li>If a message is posted from the binder thread, the code handling the message can also call
34  * {@link ThreadLocalWorkSource#getUid()} and it will return the UID of foo since the work source is
35  * automatically propagated.
36  * </ul>
37  *
38  * @hide Only for use within system server.
39  */
40 @android.ravenwood.annotation.RavenwoodKeepWholeClass
41 public final class ThreadLocalWorkSource {
42     public static final int UID_NONE = Message.UID_NONE;
43     private static final ThreadLocal<int []> sWorkSourceUid =
44             ThreadLocal.withInitial(() -> new int[] {UID_NONE});
45 
46     /**
47      * Returns the UID to blame for the code currently executed on this thread.
48      *
49      * <p>This UID is set automatically by common frameworks (e.g. Binder and Handler frameworks)
50      * and automatically propagated inside system server.
51      * <p>It can also be set manually using {@link #setUid(int)}.
52      */
getUid()53     public static int getUid() {
54         return sWorkSourceUid.get()[0];
55     }
56 
57     /**
58      * Sets the UID to blame for the code currently executed on this thread.
59      *
60      * <p>Inside system server, this UID will be automatically propagated.
61      * <p>It will be used to attribute future resources used on this thread (e.g. binder
62      * transactions or processing handler messages) and on any other threads the UID is propagated
63      * to.
64      *
65      * @return a token that can be used to restore the state.
66      */
setUid(int uid)67     public static long setUid(int uid) {
68         final long token = getToken();
69         sWorkSourceUid.get()[0] = uid;
70         return token;
71     }
72 
73     /**
74      * Restores the state using the provided token.
75      */
restore(long token)76     public static void restore(long token) {
77         sWorkSourceUid.get()[0] = parseUidFromToken(token);
78     }
79 
80     /**
81      * Clears the stored work source uid.
82      *
83      * <p>This method should be used when we do not know who to blame. If the UID to blame is the
84      * UID of the current process, it is better to attribute the work to the current process
85      * explicitly instead of clearing the work source:
86      *
87      * <pre>
88      * ThreadLocalWorkSource.setUid(Process.myUid());
89      * </pre>
90      *
91      * @return a token that can be used to restore the state.
92      */
clear()93     public static long clear() {
94         return setUid(UID_NONE);
95     }
96 
parseUidFromToken(long token)97     private static int parseUidFromToken(long token) {
98         return (int) token;
99     }
100 
getToken()101     private static long getToken() {
102         return sWorkSourceUid.get()[0];
103     }
104 
ThreadLocalWorkSource()105     private ThreadLocalWorkSource() {
106     }
107 }
108