blob: 894b1cc475b0e6e6463a89bafe7b81208892c81d [file] [log] [blame]
Marcin Oczeretkoec758722018-09-12 12:53:47 +01001/*
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
17package android.os;
18
19/**
Olivier Gaillarda50fdae2018-11-14 17:15:02 +000020 * 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 *
Marcin Oczeretkoec758722018-09-12 12:53:47 +010038 * @hide Only for use within system server.
39 */
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000040public final class ThreadLocalWorkSource {
Marcin Oczeretkoec758722018-09-12 12:53:47 +010041 public static final int UID_NONE = Message.UID_NONE;
42 private static final ThreadLocal<Integer> sWorkSourceUid =
43 ThreadLocal.withInitial(() -> UID_NONE);
44
Olivier Gaillarda50fdae2018-11-14 17:15:02 +000045 /**
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 */
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000052 public static int getUid() {
Marcin Oczeretkoec758722018-09-12 12:53:47 +010053 return sWorkSourceUid.get();
54 }
55
Olivier Gaillarda50fdae2018-11-14 17:15:02 +000056 /**
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 */
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000066 public static long setUid(int uid) {
67 final long token = getToken();
Marcin Oczeretkoec758722018-09-12 12:53:47 +010068 sWorkSourceUid.set(uid);
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000069 return token;
70 }
71
Olivier Gaillarda50fdae2018-11-14 17:15:02 +000072 /**
73 * Restores the state using the provided token.
74 */
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000075 public static void restore(long token) {
76 sWorkSourceUid.set(parseUidFromToken(token));
Marcin Oczeretkoec758722018-09-12 12:53:47 +010077 }
78
Olivier Gaillarda50fdae2018-11-14 17:15:02 +000079 /**
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 **/
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000092 public static long clear() {
93 return setUid(UID_NONE);
Marcin Oczeretkoec758722018-09-12 12:53:47 +010094 }
95
Olivier Gaillardd542b1c2018-11-14 15:24:35 +000096 private static int parseUidFromToken(long token) {
97 return (int) token;
98 }
99
100 private static long getToken() {
101 return sWorkSourceUid.get();
102 }
103
104 private ThreadLocalWorkSource() {
Marcin Oczeretkoec758722018-09-12 12:53:47 +0100105 }
106}