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