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 
22 #include <android-base/logging.h>
23 #include <android-base/macros.h>  // For ATTRIBUTE_UNUSED
24 
25 #include "palette_system.h"
26 
PaletteGetVersion(int32_t * version)27 enum PaletteStatus PaletteGetVersion(int32_t* version) {
28   *version = art::palette::kPaletteVersion;
29   return PaletteStatus::kOkay;
30 }
31 
32 // Cached thread priority for testing. No thread priorities are ever affected.
33 static std::mutex g_tid_priority_map_mutex;
34 static std::map<int32_t, int32_t> g_tid_priority_map;
35 
PaletteSchedSetPriority(int32_t tid,int32_t priority)36 enum PaletteStatus PaletteSchedSetPriority(int32_t tid, int32_t priority) {
37   if (priority < art::palette::kMinManagedThreadPriority ||
38       priority > art::palette::kMaxManagedThreadPriority) {
39     return PaletteStatus::kInvalidArgument;
40   }
41   std::lock_guard guard(g_tid_priority_map_mutex);
42   g_tid_priority_map[tid] = priority;
43   return PaletteStatus::kOkay;
44 }
45 
PaletteSchedGetPriority(int32_t tid,int32_t * priority)46 enum PaletteStatus 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 PaletteStatus::kOkay;
54 }
55 
PaletteWriteCrashThreadStacks(const char * stacks,size_t stacks_len)56 enum PaletteStatus PaletteWriteCrashThreadStacks(/*in*/ const char* stacks, size_t stacks_len) {
57   LOG(INFO) << std::string_view(stacks, stacks_len);
58   return PaletteStatus::kOkay;
59 }
60 
PaletteTraceEnabled(int32_t * enabled)61 enum PaletteStatus PaletteTraceEnabled(/*out*/int32_t* enabled) {
62   *enabled = 0;
63   return PaletteStatus::kOkay;
64 }
65 
PaletteTraceBegin(const char * name ATTRIBUTE_UNUSED)66 enum PaletteStatus PaletteTraceBegin(const char* name ATTRIBUTE_UNUSED) {
67   return PaletteStatus::kOkay;
68 }
69 
PaletteTraceEnd()70 enum PaletteStatus PaletteTraceEnd() {
71   return PaletteStatus::kOkay;
72 }
73 
PaletteTraceIntegerValue(const char * name ATTRIBUTE_UNUSED,int32_t value ATTRIBUTE_UNUSED)74 enum PaletteStatus PaletteTraceIntegerValue(const char* name ATTRIBUTE_UNUSED,
75                                             int32_t value ATTRIBUTE_UNUSED) {
76   return PaletteStatus::kOkay;
77 }
78 
PaletteAshmemCreateRegion(const char * name ATTRIBUTE_UNUSED,size_t size ATTRIBUTE_UNUSED,int * fd)79 enum PaletteStatus PaletteAshmemCreateRegion(const char* name ATTRIBUTE_UNUSED,
80                                              size_t size ATTRIBUTE_UNUSED,
81                                              int* fd) {
82   *fd = -1;
83   return PaletteStatus::kNotSupported;
84 }
85 
PaletteAshmemSetProtRegion(int fd ATTRIBUTE_UNUSED,int prot ATTRIBUTE_UNUSED)86 enum PaletteStatus PaletteAshmemSetProtRegion(int fd ATTRIBUTE_UNUSED,
87                                               int prot ATTRIBUTE_UNUSED) {
88   return PaletteStatus::kNotSupported;
89 }
90