1 /*
2  * Copyright (C) 2019 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 #include "palette/palette.h"
18 
19 #include <stdbool.h>
20 
21 #include <map>
22 #include <mutex>
23 
24 #include <android-base/logging.h>
25 #include <android-base/macros.h>  // For ATTRIBUTE_UNUSED
26 
27 #include "palette_system.h"
28 
29 // Cached thread priority for testing. No thread priorities are ever affected.
30 static std::mutex g_tid_priority_map_mutex;
31 static std::map<int32_t, int32_t> g_tid_priority_map;
32 
33 // Unless explicitly mentioned otherwise, the following methods have been
34 // introduced in version 1 API, corresponding to SDK level 31.
35 
PaletteSchedSetPriority(int32_t tid,int32_t priority)36 palette_status_t PaletteSchedSetPriority(int32_t tid, int32_t priority) {
37   if (priority < art::palette::kMinManagedThreadPriority ||
38       priority > art::palette::kMaxManagedThreadPriority) {
39     return PALETTE_STATUS_INVALID_ARGUMENT;
40   }
41   std::lock_guard guard(g_tid_priority_map_mutex);
42   g_tid_priority_map[tid] = priority;
43   return PALETTE_STATUS_OK;
44 }
45 
PaletteSchedGetPriority(int32_t tid,int32_t * priority)46 palette_status_t PaletteSchedGetPriority(int32_t tid,
47                                          /*out*/ int32_t* priority) {
48   std::lock_guard guard(g_tid_priority_map_mutex);
49   if (g_tid_priority_map.find(tid) == g_tid_priority_map.end()) {
50     g_tid_priority_map[tid] = art::palette::kNormalManagedThreadPriority;
51   }
52   *priority = g_tid_priority_map[tid];
53   return PALETTE_STATUS_OK;
54 }
55 
PaletteWriteCrashThreadStacks(const char * stacks,size_t stacks_len)56 palette_status_t PaletteWriteCrashThreadStacks(/*in*/ const char* stacks, size_t stacks_len) {
57   LOG(INFO) << std::string_view(stacks, stacks_len);
58   return PALETTE_STATUS_OK;
59 }
60 
PaletteTraceEnabled(bool * enabled)61 palette_status_t PaletteTraceEnabled(/*out*/ bool* enabled) {
62   *enabled = false;
63   return PALETTE_STATUS_OK;
64 }
65 
PaletteTraceBegin(const char * name ATTRIBUTE_UNUSED)66 palette_status_t PaletteTraceBegin(const char* name ATTRIBUTE_UNUSED) {
67   return PALETTE_STATUS_OK;
68 }
69 
PaletteTraceEnd()70 palette_status_t PaletteTraceEnd() {
71   return PALETTE_STATUS_OK;
72 }
73 
PaletteTraceIntegerValue(const char * name ATTRIBUTE_UNUSED,int32_t value ATTRIBUTE_UNUSED)74 palette_status_t PaletteTraceIntegerValue(const char* name ATTRIBUTE_UNUSED,
75                                           int32_t value ATTRIBUTE_UNUSED) {
76   return PALETTE_STATUS_OK;
77 }
78 
PaletteAshmemCreateRegion(const char * name ATTRIBUTE_UNUSED,size_t size ATTRIBUTE_UNUSED,int * fd)79 palette_status_t PaletteAshmemCreateRegion(const char* name ATTRIBUTE_UNUSED,
80                                            size_t size ATTRIBUTE_UNUSED, int* fd) {
81   *fd = -1;
82   return PALETTE_STATUS_NOT_SUPPORTED;
83 }
84 
PaletteAshmemSetProtRegion(int fd ATTRIBUTE_UNUSED,int prot ATTRIBUTE_UNUSED)85 palette_status_t PaletteAshmemSetProtRegion(int fd ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED) {
86   return PALETTE_STATUS_NOT_SUPPORTED;
87 }
88 
PaletteCreateOdrefreshStagingDirectory(const char ** staging_dir)89 palette_status_t PaletteCreateOdrefreshStagingDirectory(const char** staging_dir) {
90   *staging_dir = nullptr;
91   return PALETTE_STATUS_NOT_SUPPORTED;
92 }
93 
PaletteShouldReportDex2oatCompilation(bool * value)94 palette_status_t PaletteShouldReportDex2oatCompilation(bool* value) {
95   *value = false;
96   return PALETTE_STATUS_OK;
97 }
98 
PaletteNotifyStartDex2oatCompilation(int source_fd ATTRIBUTE_UNUSED,int art_fd ATTRIBUTE_UNUSED,int oat_fd ATTRIBUTE_UNUSED,int vdex_fd ATTRIBUTE_UNUSED)99 palette_status_t PaletteNotifyStartDex2oatCompilation(int source_fd ATTRIBUTE_UNUSED,
100                                                       int art_fd ATTRIBUTE_UNUSED,
101                                                       int oat_fd ATTRIBUTE_UNUSED,
102                                                       int vdex_fd ATTRIBUTE_UNUSED) {
103   return PALETTE_STATUS_OK;
104 }
105 
PaletteNotifyEndDex2oatCompilation(int source_fd ATTRIBUTE_UNUSED,int art_fd ATTRIBUTE_UNUSED,int oat_fd ATTRIBUTE_UNUSED,int vdex_fd ATTRIBUTE_UNUSED)106 palette_status_t PaletteNotifyEndDex2oatCompilation(int source_fd ATTRIBUTE_UNUSED,
107                                                     int art_fd ATTRIBUTE_UNUSED,
108                                                     int oat_fd ATTRIBUTE_UNUSED,
109                                                     int vdex_fd ATTRIBUTE_UNUSED) {
110   return PALETTE_STATUS_OK;
111 }
112 
PaletteNotifyDexFileLoaded(const char * path ATTRIBUTE_UNUSED)113 palette_status_t PaletteNotifyDexFileLoaded(const char* path ATTRIBUTE_UNUSED) {
114   return PALETTE_STATUS_OK;
115 }
116 
PaletteNotifyOatFileLoaded(const char * path ATTRIBUTE_UNUSED)117 palette_status_t PaletteNotifyOatFileLoaded(const char* path ATTRIBUTE_UNUSED) {
118   return PALETTE_STATUS_OK;
119 }
120 
PaletteShouldReportJniInvocations(bool * value)121 palette_status_t PaletteShouldReportJniInvocations(bool* value) {
122   *value = false;
123   return PALETTE_STATUS_OK;
124 }
125 
PaletteNotifyBeginJniInvocation(JNIEnv * env ATTRIBUTE_UNUSED)126 palette_status_t PaletteNotifyBeginJniInvocation(JNIEnv* env ATTRIBUTE_UNUSED) {
127   return PALETTE_STATUS_OK;
128 }
129 
PaletteNotifyEndJniInvocation(JNIEnv * env ATTRIBUTE_UNUSED)130 palette_status_t PaletteNotifyEndJniInvocation(JNIEnv* env ATTRIBUTE_UNUSED) {
131   return PALETTE_STATUS_OK;
132 }
133 
134 // Introduced in version 2 API, corresponding to SDK level 33.
PaletteReportLockContention(JNIEnv * env ATTRIBUTE_UNUSED,int32_t wait_ms ATTRIBUTE_UNUSED,const char * filename ATTRIBUTE_UNUSED,int32_t line_number ATTRIBUTE_UNUSED,const char * method_name ATTRIBUTE_UNUSED,const char * owner_filename ATTRIBUTE_UNUSED,int32_t owner_line_number ATTRIBUTE_UNUSED,const char * owner_method_name ATTRIBUTE_UNUSED,const char * proc_name ATTRIBUTE_UNUSED,const char * thread_name ATTRIBUTE_UNUSED)135 palette_status_t PaletteReportLockContention(
136     JNIEnv* env ATTRIBUTE_UNUSED, int32_t wait_ms ATTRIBUTE_UNUSED,
137     const char* filename ATTRIBUTE_UNUSED, int32_t line_number ATTRIBUTE_UNUSED,
138     const char* method_name ATTRIBUTE_UNUSED, const char* owner_filename ATTRIBUTE_UNUSED,
139     int32_t owner_line_number ATTRIBUTE_UNUSED, const char* owner_method_name ATTRIBUTE_UNUSED,
140     const char* proc_name ATTRIBUTE_UNUSED, const char* thread_name ATTRIBUTE_UNUSED) {
141   return PALETTE_STATUS_OK;
142 }
143 
144 // Introduced in version 3 API, corresponding to SDK level 34.
PaletteSetTaskProfiles(int32_t tid ATTRIBUTE_UNUSED,const char * const profiles[]ATTRIBUTE_UNUSED,size_t profiles_len ATTRIBUTE_UNUSED)145 palette_status_t PaletteSetTaskProfiles(int32_t tid ATTRIBUTE_UNUSED,
146                                         const char* const profiles[] ATTRIBUTE_UNUSED,
147                                         size_t profiles_len ATTRIBUTE_UNUSED) {
148   return PALETTE_STATUS_OK;
149 }
150 
151 // Introduced in version 4 API, corresponding to SDK level 36.
PaletteDebugStoreGetString(char * result ATTRIBUTE_UNUSED,size_t max_size ATTRIBUTE_UNUSED)152 palette_status_t PaletteDebugStoreGetString(char* result ATTRIBUTE_UNUSED,
153                                             size_t max_size ATTRIBUTE_UNUSED) {
154   result[0] = '\0';
155   return PALETTE_STATUS_OK;
156 }