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 public final class ThreadLocalWorkSource {
41     public static final int UID_NONE = Message.UID_NONE;
42     private static final ThreadLocal<Integer> sWorkSourceUid =
43             ThreadLocal.withInitial(() -> UID_NONE);
44 
45     /**
46      * Returns the UID to blame for the code currently executed on this thread.
47      *
48      * <p>This UID is set automatically by common frameworks (e.g. Binder and Handler frameworks)
49      * and automatically propagated inside system server.
50      * <p>It can also be set manually using {@link #setUid(int)}.
51      */
getUid()52     public static int getUid() {
53         return sWorkSourceUid.get();
54     }
55 
56     /**
57      * Sets the UID to blame for the code currently executed on this thread.
58      *
59      * <p>Inside system server, this UID will be automatically propagated.
60      * <p>It will be used to attribute future resources used on this thread (e.g. binder
61      * transactions or processing handler messages) and on any other threads the UID is propagated
62      * to.
63      *
64      * @return a token that can be used to restore the state.
65      */
setUid(int uid)66     public static long setUid(int uid) {
67         final long token = getToken();
68         sWorkSourceUid.set(uid);
69         return token;
70     }
71 
72     /**
73      * Restores the state using the provided token.
74      */
restore(long token)75     public static void restore(long token) {
76         sWorkSourceUid.set(parseUidFromToken(token));
77     }
78 
79     /**
80      * Clears the stored work source uid.
81      *
82      * <p>This method should be used when we do not know who to blame. If the UID to blame is the
83      * UID of the current process, it is better to attribute the work to the current process
84      * explicitly instead of clearing the work source:
85      *
86      * <pre>
87      * ThreadLocalWorkSource.setUid(Process.myUid());
88      * </pre>
89      *
90      * @return a token that can be used to restore the state.
91      **/
clear()92     public static long clear() {
93         return setUid(UID_NONE);
94     }
95 
parseUidFromToken(long token)96     private static int parseUidFromToken(long token) {
97         return (int) token;
98     }
99 
getToken()100     private static long getToken() {
101         return sWorkSourceUid.get();
102     }
103 
ThreadLocalWorkSource()104     private ThreadLocalWorkSource() {
105     }
106 }
107