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