Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "runtime.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 4 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 5 | #include <cstdio> |
| 6 | #include <cstdlib> |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 7 | #include <limits> |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 8 | #include <vector> |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 9 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 10 | #include "ScopedLocalRef.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 11 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | #include "class_linker.h" |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 13 | #include "class_loader.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 14 | #include "heap.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 15 | #include "image.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 16 | #include "intern_table.h" |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 17 | #include "jni_internal.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 18 | #include "oat_file.h" |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 19 | #include "ScopedLocalRef.h" |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 20 | #include "signal_catcher.h" |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 21 | #include "space.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 22 | #include "thread.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 23 | #include "thread_list.h" |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 25 | // TODO: this drags in cutil/log.h, which conflicts with our logging.h. |
| 26 | #include "JniConstants.h" |
| 27 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 30 | Runtime* Runtime::instance_ = NULL; |
| 31 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 32 | Runtime::Runtime() |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 33 | : verbose_startup_(false), |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 34 | is_zygote_(false), |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 35 | default_stack_size_(Thread::kDefaultStackSize), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 36 | thread_list_(NULL), |
| 37 | intern_table_(NULL), |
| 38 | class_linker_(NULL), |
| 39 | signal_catcher_(NULL), |
| 40 | java_vm_(NULL), |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 41 | jni_stub_array_(NULL), |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 42 | abstract_method_error_stub_array_(NULL), |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 43 | started_(false), |
| 44 | vfprintf_(NULL), |
| 45 | exit_(NULL), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 46 | abort_(NULL), |
| 47 | stats_enabled_(false) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 48 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 49 | resolution_stub_array_[i] = NULL; |
| 50 | } |
| 51 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 52 | callee_save_method_[i] = NULL; |
| 53 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 56 | Runtime::~Runtime() { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 57 | // Make sure our internal threads are dead before we start tearing down things they're using. |
| 58 | delete signal_catcher_; |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 59 | // TODO: GC thread. |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 61 | // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended. |
Elliott Hughes | 93e74e8 | 2011-09-13 11:07:03 -0700 | [diff] [blame] | 62 | delete thread_list_; |
| 63 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 64 | delete class_linker_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 65 | Heap::Destroy(); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 66 | delete intern_table_; |
Elliott Hughes | c5f7c91 | 2011-08-18 14:00:42 -0700 | [diff] [blame] | 67 | delete java_vm_; |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 68 | Thread::Shutdown(); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 69 | // TODO: acquire a static mutex on Runtime to avoid racing. |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 70 | CHECK(instance_ == NULL || instance_ == this); |
Carl Shapiro | 4acf464 | 2011-07-26 18:54:13 -0700 | [diff] [blame] | 71 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 74 | void Runtime::Abort(const char* file, int line) { |
| 75 | // Get any pending output out of the way. |
| 76 | fflush(NULL); |
| 77 | |
| 78 | // Many people have difficulty distinguish aborts from crashes, |
| 79 | // so be explicit. |
| 80 | LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting..."; |
| 81 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 82 | // Perform any platform-specific pre-abort actions. |
| 83 | PlatformAbort(file, line); |
| 84 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 85 | // use abort hook if we have one |
| 86 | if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) { |
| 87 | Runtime::Current()->abort_(); |
| 88 | // notreached |
| 89 | } |
| 90 | |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 91 | // If we call abort(3) on a device, all threads in the process |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 92 | // receive SIGABRT. debuggerd dumps the stack trace of the main |
| 93 | // thread, whether or not that was the thread that failed. By |
| 94 | // stuffing a value into a bogus address, we cause a segmentation |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 95 | // fault in the current thread, and get a useful log from debuggerd. |
| 96 | // We can also trivially tell the difference between a VM crash and |
| 97 | // a deliberate abort by looking at the fault address. |
| 98 | *reinterpret_cast<char*>(0xdeadd00d) = 38; |
| 99 | abort(); |
Elliott Hughes | ffe6736 | 2011-07-17 12:09:27 -0700 | [diff] [blame] | 100 | // notreached |
| 101 | } |
| 102 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 103 | void Runtime::CallExitHook(jint status) { |
| 104 | if (exit_ != NULL) { |
| 105 | ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative); |
| 106 | exit_(status); |
| 107 | LOG(WARNING) << "Exit hook returned instead of exiting!"; |
| 108 | } |
| 109 | } |
| 110 | |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 111 | // Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify |
| 112 | // memory sizes. [kK] indicates kilobytes, [mM] megabytes, and |
| 113 | // [gG] gigabytes. |
| 114 | // |
| 115 | // "s" should point just past the "-Xm?" part of the string. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 116 | // "div" specifies a divisor, e.g. 1024 if the value must be a multiple |
| 117 | // of 1024. |
| 118 | // |
| 119 | // The spec says the -Xmx and -Xms options must be multiples of 1024. It |
| 120 | // doesn't say anything about -Xss. |
| 121 | // |
| 122 | // Returns 0 (a useless size) if "s" is malformed or specifies a low or |
| 123 | // non-evenly-divisible value. |
| 124 | // |
| 125 | size_t ParseMemoryOption(const char *s, size_t div) { |
| 126 | // strtoul accepts a leading [+-], which we don't want, |
| 127 | // so make sure our string starts with a decimal digit. |
| 128 | if (isdigit(*s)) { |
| 129 | const char *s2; |
| 130 | size_t val = strtoul(s, (char **)&s2, 10); |
| 131 | if (s2 != s) { |
| 132 | // s2 should be pointing just after the number. |
| 133 | // If this is the end of the string, the user |
| 134 | // has specified a number of bytes. Otherwise, |
| 135 | // there should be exactly one more character |
| 136 | // that specifies a multiplier. |
| 137 | if (*s2 != '\0') { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 138 | // The remainder of the string is either a single multiplier |
| 139 | // character, or nothing to indicate that the value is in |
| 140 | // bytes. |
| 141 | char c = *s2++; |
| 142 | if (*s2 == '\0') { |
| 143 | size_t mul; |
| 144 | if (c == '\0') { |
| 145 | mul = 1; |
| 146 | } else if (c == 'k' || c == 'K') { |
| 147 | mul = KB; |
| 148 | } else if (c == 'm' || c == 'M') { |
| 149 | mul = MB; |
| 150 | } else if (c == 'g' || c == 'G') { |
| 151 | mul = GB; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 152 | } else { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 153 | // Unknown multiplier character. |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 154 | return 0; |
| 155 | } |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 156 | |
| 157 | if (val <= std::numeric_limits<size_t>::max() / mul) { |
| 158 | val *= mul; |
| 159 | } else { |
| 160 | // Clamp to a multiple of 1024. |
| 161 | val = std::numeric_limits<size_t>::max() & ~(1024-1); |
| 162 | } |
| 163 | } else { |
| 164 | // There's more than one character after the numeric part. |
| 165 | return 0; |
| 166 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 167 | } |
| 168 | // The man page says that a -Xm value must be a multiple of 1024. |
| 169 | if (val % div == 0) { |
| 170 | return val; |
| 171 | } |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 174 | return 0; |
Carl Shapiro | fc322c7 | 2011-07-27 00:20:01 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 177 | void LoadJniLibrary(JavaVMExt* vm, const char* name) { |
| 178 | // TODO: OS_SHARED_LIB_FORMAT_STR |
| 179 | std::string mapped_name(StringPrintf("lib%s.so", name)); |
Elliott Hughes | 7577075 | 2011-08-24 17:52:38 -0700 | [diff] [blame] | 180 | std::string reason; |
| 181 | if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 182 | LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": " |
| 183 | << reason; |
| 184 | } |
| 185 | } |
| 186 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 187 | Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) { |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 188 | UniquePtr<ParsedOptions> parsed(new ParsedOptions()); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 189 | const char* boot_class_path = getenv("BOOTCLASSPATH"); |
| 190 | if (boot_class_path != NULL) { |
| 191 | parsed->boot_class_path_ = getenv("BOOTCLASSPATH"); |
| 192 | } |
| 193 | const char* class_path = getenv("CLASSPATH"); |
| 194 | if (class_path != NULL) { |
| 195 | parsed->class_path_ = getenv("CLASSPATH"); |
| 196 | } |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 197 | #ifdef NDEBUG |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 198 | // -Xcheck:jni is off by default for regular builds... |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 199 | parsed->check_jni_ = false; |
| 200 | #else |
| 201 | // ...but on by default in debug builds. |
| 202 | parsed->check_jni_ = true; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 203 | #endif |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 204 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 205 | parsed->heap_initial_size_ = Heap::kInitialSize; |
| 206 | parsed->heap_maximum_size_ = Heap::kMaximumSize; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 207 | parsed->stack_size_ = Thread::kDefaultStackSize; |
| 208 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 209 | parsed->is_zygote_ = false; |
| 210 | |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 211 | parsed->hook_vfprintf_ = vfprintf; |
| 212 | parsed->hook_exit_ = exit; |
| 213 | parsed->hook_abort_ = abort; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 214 | |
| 215 | for (size_t i = 0; i < options.size(); ++i) { |
| 216 | const StringPiece& option = options[i].first; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 217 | if (false) { |
| 218 | LOG(INFO) << "option[" << i << "]=" << option; |
| 219 | } |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 220 | if (option.starts_with("-Xbootclasspath:")) { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 221 | parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 222 | } else if (option == "-classpath" || option == "-cp") { |
| 223 | // TODO: support -Djava.class.path |
| 224 | i++; |
| 225 | if (i == options.size()) { |
| 226 | // TODO: usage |
| 227 | LOG(FATAL) << "Missing required class path value for " << option; |
| 228 | return NULL; |
| 229 | } |
| 230 | const StringPiece& value = options[i].first; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 231 | parsed->class_path_ = value.data(); |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 232 | } else if (option.starts_with("-Ximage:")) { |
| 233 | parsed->images_.push_back(option.substr(strlen("-Ximage:")).data()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 234 | } else if (option.starts_with("-Xcheck:jni")) { |
| 235 | parsed->check_jni_ = true; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 236 | } else if (option.starts_with("-Xms")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 237 | size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024); |
| 238 | if (size == 0) { |
| 239 | if (ignore_unrecognized) { |
| 240 | continue; |
| 241 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 242 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 243 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 244 | return NULL; |
| 245 | } |
| 246 | parsed->heap_initial_size_ = size; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 247 | } else if (option.starts_with("-Xmx")) { |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 248 | size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024); |
| 249 | if (size == 0) { |
| 250 | if (ignore_unrecognized) { |
| 251 | continue; |
| 252 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 253 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 254 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | parsed->heap_maximum_size_ = size; |
| 258 | } else if (option.starts_with("-Xss")) { |
| 259 | size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1); |
| 260 | if (size == 0) { |
| 261 | if (ignore_unrecognized) { |
| 262 | continue; |
| 263 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 264 | // TODO: usage |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 265 | LOG(FATAL) << "Failed to parse " << option; |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 266 | return NULL; |
| 267 | } |
| 268 | parsed->stack_size_ = size; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 269 | } else if (option.starts_with("-D")) { |
| 270 | parsed->properties_.push_back(option.substr(strlen("-D")).data()); |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 271 | } else if (option.starts_with("-Xjnitrace:")) { |
| 272 | parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data(); |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 273 | } else if (option == "-Xzygote") { |
| 274 | parsed->is_zygote_ = true; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 275 | } else if (option.starts_with("-verbose:")) { |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 276 | std::vector<std::string> verbose_options; |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 277 | Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options); |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 278 | for (size_t i = 0; i < verbose_options.size(); ++i) { |
| 279 | parsed->verbose_.insert(verbose_options[i]); |
| 280 | } |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 281 | } else if (option == "vfprintf") { |
| 282 | parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second); |
| 283 | } else if (option == "exit") { |
| 284 | parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second); |
| 285 | } else if (option == "abort") { |
| 286 | parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 287 | } else if (option == "host-prefix") { |
| 288 | parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 289 | } else { |
| 290 | if (!ignore_unrecognized) { |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 291 | // TODO: print usage via vfprintf |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 292 | LOG(ERROR) << "Unrecognized option " << option; |
| 293 | // TODO: this should exit, but for now tolerate unknown options |
| 294 | //return NULL; |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 299 | LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off"); |
| 300 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 301 | return parsed.release(); |
Brian Carlstrom | 8a43659 | 2011-08-15 21:27:23 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) { |
Carl Shapiro | 2ed144c | 2011-07-26 16:52:08 -0700 | [diff] [blame] | 305 | // TODO: acquire a static mutex on Runtime to avoid racing. |
| 306 | if (Runtime::instance_ != NULL) { |
| 307 | return NULL; |
| 308 | } |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 309 | instance_ = new Runtime; |
| 310 | if (!instance_->Init(options, ignore_unrecognized)) { |
| 311 | delete instance_; |
| 312 | instance_ = NULL; |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 313 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 314 | return instance_; |
| 315 | } |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 316 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 317 | void CreateSystemClassLoader() { |
| 318 | if (ClassLoader::UseCompileTimeClassPath()) { |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | Thread* self = Thread::Current(); |
| 323 | |
| 324 | // Must be in the kNative state for calling native methods. |
| 325 | CHECK_EQ(self->GetState(), Thread::kNative); |
| 326 | |
| 327 | JNIEnv* env = self->GetJniEnv(); |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 328 | ScopedLocalRef<jclass> ClassLoader_class(env, env->FindClass("java/lang/ClassLoader")); |
| 329 | CHECK(ClassLoader_class.get() != NULL); |
| 330 | jmethodID getSystemClassLoader = env->GetStaticMethodID(ClassLoader_class.get(), |
| 331 | "getSystemClassLoader", |
| 332 | "()Ljava/lang/ClassLoader;"); |
| 333 | CHECK(getSystemClassLoader != NULL); |
| 334 | ScopedLocalRef<jobject> class_loader(env, env->CallStaticObjectMethod(ClassLoader_class.get(), |
| 335 | getSystemClassLoader)); |
| 336 | CHECK(class_loader.get() != NULL); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 337 | |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 338 | Thread::Current()->SetClassLoaderOverride(Decode<ClassLoader*>(env, class_loader.get())); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 341 | void Runtime::Start() { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 342 | if (IsVerboseStartup()) { |
| 343 | LOG(INFO) << "Runtime::Start entering"; |
| 344 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 345 | |
| 346 | CHECK(host_prefix_.empty()) << host_prefix_; |
| 347 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 348 | InitNativeMethods(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 349 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 350 | Thread::FinishStartup(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 351 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 352 | class_linker_->RunRootClinits(); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 353 | |
Brian Carlstrom | 5d40f18 | 2011-09-26 22:29:18 -0700 | [diff] [blame] | 354 | // Class::AllocObject asserts that all objects allocated better be |
| 355 | // initialized after Runtime::IsStarted is true, so this needs to |
| 356 | // come after ClassLinker::RunRootClinits. |
| 357 | started_ = true; |
| 358 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 359 | if (!is_zygote_) { |
| 360 | signal_catcher_ = new SignalCatcher; |
| 361 | } |
| 362 | |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 363 | StartDaemonThreads(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 364 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 365 | CreateSystemClassLoader(); |
| 366 | |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 367 | Thread::Current()->GetJniEnv()->locals.AssertEmpty(); |
| 368 | |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 369 | if (IsVerboseStartup()) { |
| 370 | LOG(INFO) << "Runtime::Start exiting"; |
| 371 | } |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void Runtime::StartDaemonThreads() { |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 375 | if (IsVerboseStartup()) { |
| 376 | LOG(INFO) << "Runtime::StartDaemonThreads entering"; |
| 377 | } |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 378 | |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 379 | Thread* self = Thread::Current(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 380 | |
| 381 | // Must be in the kNative state for calling native methods. |
| 382 | CHECK_EQ(self->GetState(), Thread::kNative); |
| 383 | |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 384 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 385 | ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons")); |
| 386 | CHECK(c.get() != NULL); |
| 387 | jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V"); |
Elliott Hughes | 719b323 | 2011-09-25 17:42:19 -0700 | [diff] [blame] | 388 | CHECK(mid != NULL); |
Elliott Hughes | 726079d | 2011-10-07 18:43:44 -0700 | [diff] [blame] | 389 | env->CallStaticVoidMethod(c.get(), mid); |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 390 | |
| 391 | if (IsVerboseStartup()) { |
| 392 | LOG(INFO) << "Runtime::StartDaemonThreads exiting"; |
| 393 | } |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 396 | bool Runtime::IsStarted() const { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 397 | return started_; |
| 398 | } |
| 399 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 400 | bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) { |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 401 | CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 402 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 403 | UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized)); |
| 404 | if (options.get() == NULL) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 405 | LOG(ERROR) << "Failed to parse options"; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 406 | return false; |
| 407 | } |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 408 | verbose_startup_ = options->IsVerbose("startup"); |
| 409 | if (IsVerboseStartup()) { |
| 410 | LOG(INFO) << "Runtime::Init -verbose:startup enabled"; |
| 411 | } |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 412 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 413 | host_prefix_ = options->host_prefix_; |
| 414 | boot_class_path_ = options->boot_class_path_; |
| 415 | class_path_ = options->class_path_; |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 416 | properties_ = options->properties_; |
| 417 | |
Elliott Hughes | 9ca7a1f | 2011-10-11 14:29:52 -0700 | [diff] [blame^] | 418 | is_zygote_ = options->is_zygote_; |
| 419 | |
Elliott Hughes | 0af5543 | 2011-08-17 18:37:28 -0700 | [diff] [blame] | 420 | vfprintf_ = options->hook_vfprintf_; |
| 421 | exit_ = options->hook_exit_; |
| 422 | abort_ = options->hook_abort_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 423 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 424 | default_stack_size_ = options->stack_size_; |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 425 | |
Elliott Hughes | 14357e8 | 2011-09-26 10:42:15 -0700 | [diff] [blame] | 426 | thread_list_ = new ThreadList(options->IsVerbose("thread")); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 427 | intern_table_ = new InternTable; |
| 428 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 429 | Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 430 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 431 | BlockSignals(); |
| 432 | |
Elliott Hughes | a095764 | 2011-09-02 14:27:33 -0700 | [diff] [blame] | 433 | java_vm_ = new JavaVMExt(this, options.get()); |
Elliott Hughes | 515a5bc | 2011-08-17 11:08:34 -0700 | [diff] [blame] | 434 | |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 435 | Thread::Startup(); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 436 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 437 | // ClassLinker needs an attached thread, but we can't fully attach a thread |
| 438 | // without creating objects. |
| 439 | Thread::Attach(this, "main", false); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 440 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 441 | CHECK_GE(Heap::GetSpaces().size(), 1U); |
| 442 | class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace()) |
| 443 | ? ClassLinker::Create(intern_table_) |
| 444 | : ClassLinker::Create(options->boot_class_path_, intern_table_)); |
Brian Carlstrom | 6ea095a | 2011-08-16 15:26:54 -0700 | [diff] [blame] | 445 | |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 446 | if (IsVerboseStartup()) { |
| 447 | LOG(INFO) << "Runtime::Init exiting"; |
| 448 | } |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 449 | return true; |
| 450 | } |
| 451 | |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 452 | void Runtime::InitNativeMethods() { |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 453 | if (IsVerboseStartup()) { |
| 454 | LOG(INFO) << "Runtime::InitNativeMethods entering"; |
| 455 | } |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 456 | Thread* self = Thread::Current(); |
| 457 | JNIEnv* env = self->GetJniEnv(); |
| 458 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 459 | // Must be in the kNative state for calling native methods (JNI_OnLoad code). |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 460 | CHECK_EQ(self->GetState(), Thread::kNative); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 461 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 462 | // First set up JniConstants, which is used by both the runtime's built-in native |
| 463 | // methods and libcore. |
Elliott Hughes | fea966e | 2011-09-22 10:26:20 -0700 | [diff] [blame] | 464 | JniConstants::init(env); |
| 465 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 466 | // Then set up the native methods provided by the runtime itself. |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 467 | RegisterRuntimeNativeMethods(env); |
| 468 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 469 | // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad. |
| 470 | // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's |
| 471 | // the library that implements System.loadLibrary! |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 472 | LoadJniLibrary(instance_->GetJavaVM(), "javacore"); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 473 | if (IsVerboseStartup()) { |
| 474 | LOG(INFO) << "Runtime::InitNativeMethods exiting"; |
| 475 | } |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) { |
| 479 | #define REGISTER(FN) extern void FN(JNIEnv*); FN(env) |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 480 | REGISTER(register_dalvik_system_DexFile); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 481 | REGISTER(register_dalvik_system_VMDebug); |
Elliott Hughes | 7ede61e | 2011-09-14 18:18:06 -0700 | [diff] [blame] | 482 | REGISTER(register_dalvik_system_VMRuntime); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 483 | REGISTER(register_dalvik_system_VMStack); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 484 | REGISTER(register_dalvik_system_Zygote); |
Elliott Hughes | d369bb7 | 2011-09-12 14:41:14 -0700 | [diff] [blame] | 485 | REGISTER(register_java_lang_Class); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 486 | REGISTER(register_java_lang_Object); |
| 487 | REGISTER(register_java_lang_Runtime); |
| 488 | REGISTER(register_java_lang_String); |
| 489 | REGISTER(register_java_lang_System); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 490 | REGISTER(register_java_lang_Thread); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 491 | REGISTER(register_java_lang_Throwable); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 492 | REGISTER(register_java_lang_VMClassLoader); |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 493 | REGISTER(register_java_lang_reflect_Array); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 494 | REGISTER(register_java_lang_reflect_Constructor); |
Brian Carlstrom | f867b6f | 2011-09-16 12:17:25 -0700 | [diff] [blame] | 495 | REGISTER(register_java_lang_reflect_Field); |
| 496 | REGISTER(register_java_lang_reflect_Method); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 497 | //REGISTER(register_java_lang_reflect_Proxy); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 498 | REGISTER(register_java_util_concurrent_atomic_AtomicLong); |
Brian Carlstrom | 395520e | 2011-09-25 19:35:00 -0700 | [diff] [blame] | 499 | REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 500 | //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal); |
Elliott Hughes | 5ee7a8b | 2011-09-13 16:40:07 -0700 | [diff] [blame] | 501 | REGISTER(register_sun_misc_Unsafe); |
Elliott Hughes | ad7c2a3 | 2011-08-31 11:58:10 -0700 | [diff] [blame] | 502 | #undef REGISTER |
| 503 | } |
| 504 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 505 | void Runtime::Dump(std::ostream& os) { |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 506 | // TODO: dump other runtime statistics? |
| 507 | os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n"; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 508 | os << "Intern table size: " << GetInternTable()->Size() << "\n"; |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 509 | // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d", |
| 510 | // gDvm.numDeclaredMethods, |
| 511 | // gDvm.numDeclaredInstFields, |
| 512 | // gDvm.numDeclaredStaticFields, |
| 513 | // gDvm.pBootLoaderAlloc->curOffset); |
| 514 | // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods)); |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 515 | os << "\n"; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 516 | |
| 517 | thread_list_->Dump(os); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 520 | void Runtime::SetStatsEnabled(bool new_state) { |
| 521 | if (new_state == true) { |
| 522 | GetStats()->Clear(~0); |
| 523 | // TODO: wouldn't it make more sense to clear _all_ threads' stats? |
| 524 | Thread::Current()->GetStats()->Clear(~0); |
| 525 | } |
| 526 | stats_enabled_ = new_state; |
| 527 | } |
| 528 | |
| 529 | void Runtime::ResetStats(int kinds) { |
| 530 | GetStats()->Clear(kinds & 0xffff); |
| 531 | // TODO: wouldn't it make more sense to clear _all_ threads' stats? |
| 532 | Thread::Current()->GetStats()->Clear(kinds >> 16); |
| 533 | } |
| 534 | |
| 535 | RuntimeStats* Runtime::GetStats() { |
| 536 | return &stats_; |
| 537 | } |
| 538 | |
| 539 | int32_t Runtime::GetStat(int kind) { |
| 540 | RuntimeStats* stats; |
| 541 | if (kind < (1<<16)) { |
| 542 | stats = GetStats(); |
| 543 | } else { |
| 544 | stats = Thread::Current()->GetStats(); |
| 545 | kind >>= 16; |
| 546 | } |
| 547 | switch (kind) { |
| 548 | case KIND_ALLOCATED_OBJECTS: |
| 549 | return stats->allocated_objects; |
| 550 | case KIND_ALLOCATED_BYTES: |
| 551 | return stats->allocated_bytes; |
| 552 | case KIND_FREED_OBJECTS: |
| 553 | return stats->freed_objects; |
| 554 | case KIND_FREED_BYTES: |
| 555 | return stats->freed_bytes; |
| 556 | case KIND_GC_INVOCATIONS: |
| 557 | return stats->gc_for_alloc_count; |
| 558 | case KIND_CLASS_INIT_COUNT: |
| 559 | return stats->class_init_count; |
| 560 | case KIND_CLASS_INIT_TIME: |
| 561 | // Convert ns to us, reduce to 32 bits. |
| 562 | return (int) (stats->class_init_time_ns / 1000); |
| 563 | case KIND_EXT_ALLOCATED_OBJECTS: |
| 564 | case KIND_EXT_ALLOCATED_BYTES: |
| 565 | case KIND_EXT_FREED_OBJECTS: |
| 566 | case KIND_EXT_FREED_BYTES: |
| 567 | return 0; // backward compatibility |
| 568 | default: |
| 569 | CHECK(false); |
| 570 | return -1; // unreachable |
| 571 | } |
| 572 | } |
| 573 | |
Elliott Hughes | c1674ed | 2011-08-25 18:09:09 -0700 | [diff] [blame] | 574 | void Runtime::BlockSignals() { |
| 575 | sigset_t sigset; |
| 576 | if (sigemptyset(&sigset) == -1) { |
| 577 | PLOG(FATAL) << "sigemptyset failed"; |
| 578 | } |
| 579 | if (sigaddset(&sigset, SIGPIPE) == -1) { |
| 580 | PLOG(ERROR) << "sigaddset SIGPIPE failed"; |
| 581 | } |
| 582 | // SIGQUIT is used to dump the runtime's state (including stack traces). |
| 583 | if (sigaddset(&sigset, SIGQUIT) == -1) { |
| 584 | PLOG(ERROR) << "sigaddset SIGQUIT failed"; |
| 585 | } |
| 586 | // SIGUSR1 is used to initiate a heap dump. |
| 587 | if (sigaddset(&sigset, SIGUSR1) == -1) { |
| 588 | PLOG(ERROR) << "sigaddset SIGUSR1 failed"; |
| 589 | } |
| 590 | CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0); |
| 591 | } |
| 592 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 593 | void Runtime::AttachCurrentThread(const char* name, bool as_daemon) { |
| 594 | Thread::Attach(instance_, name, as_daemon); |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 597 | void Runtime::DetachCurrentThread() { |
Elliott Hughes | 038a806 | 2011-09-18 14:12:41 -0700 | [diff] [blame] | 598 | // TODO: check we're not calling DetachCurrentThread from a call stack that |
| 599 | // includes managed frames. (It's only valid if the stack is all-native.) |
Elliott Hughes | 02b48d1 | 2011-09-07 17:15:51 -0700 | [diff] [blame] | 600 | thread_list_->Unregister(); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 603 | void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const { |
| 604 | class_linker_->VisitRoots(visitor, arg); |
| 605 | intern_table_->VisitRoots(visitor, arg); |
| 606 | java_vm_->VisitRoots(visitor, arg); |
| 607 | thread_list_->VisitRoots(visitor, arg); |
| 608 | visitor(jni_stub_array_, arg); |
| 609 | visitor(abstract_method_error_stub_array_, arg); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 610 | for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) { |
| 611 | visitor(resolution_stub_array_[i], arg); |
| 612 | } |
| 613 | for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) { |
| 614 | visitor(callee_save_method_[i], arg); |
| 615 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 616 | |
| 617 | //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg); |
| 618 | //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 619 | //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg); |
| 620 | UNIMPLEMENTED(WARNING) << "some roots not marked"; |
| 621 | } |
| 622 | |
| 623 | bool Runtime::HasJniStubArray() const { |
| 624 | return jni_stub_array_ != NULL; |
| 625 | } |
| 626 | |
| 627 | ByteArray* Runtime::GetJniStubArray() const { |
| 628 | CHECK(jni_stub_array_ != NULL); |
| 629 | return jni_stub_array_; |
| 630 | } |
| 631 | |
| 632 | void Runtime::SetJniStubArray(ByteArray* jni_stub_array) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 633 | CHECK(jni_stub_array != NULL) << " jni_stub_array=" << jni_stub_array; |
| 634 | CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array) |
| 635 | << "jni_stub_array_=" << jni_stub_array_ << " jni_stub_array=" << jni_stub_array; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 636 | jni_stub_array_ = jni_stub_array; |
| 637 | } |
| 638 | |
| 639 | bool Runtime::HasAbstractMethodErrorStubArray() const { |
| 640 | return abstract_method_error_stub_array_ != NULL; |
| 641 | } |
| 642 | |
| 643 | ByteArray* Runtime::GetAbstractMethodErrorStubArray() const { |
| 644 | CHECK(abstract_method_error_stub_array_ != NULL); |
| 645 | return abstract_method_error_stub_array_; |
| 646 | } |
| 647 | |
| 648 | void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) { |
| 649 | CHECK(abstract_method_error_stub_array != NULL); |
| 650 | CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array); |
| 651 | abstract_method_error_stub_array_ = abstract_method_error_stub_array; |
| 652 | } |
| 653 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 654 | |
| 655 | Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) { |
| 656 | if (method == NULL) { |
| 657 | return Runtime::kUnknownMethod; |
| 658 | } else if (method->IsStatic()) { |
| 659 | return Runtime::kStaticMethod; |
| 660 | } else { |
| 661 | return Runtime::kInstanceMethod; |
| 662 | } |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 665 | bool Runtime::HasResolutionStubArray(TrampolineType type) const { |
| 666 | return resolution_stub_array_[type] != NULL; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 669 | ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const { |
| 670 | CHECK(HasResolutionStubArray(type)); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 671 | DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType)); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 672 | return resolution_stub_array_[type]; |
| 673 | } |
| 674 | |
| 675 | void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) { |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 676 | CHECK(resolution_stub_array != NULL); |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame] | 677 | CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array); |
| 678 | resolution_stub_array_[type] = resolution_stub_array; |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 681 | Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) { |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 682 | Class* method_class = Method::GetMethodClass(); |
| 683 | Method* method = down_cast<Method*>(method_class->AllocObject()); |
| 684 | method->SetDeclaringClass(method_class); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 685 | const char* name; |
| 686 | if (type == kSaveAll) { |
| 687 | name = "$$$callee_save_method$$$"; |
| 688 | } else if (type == kRefsOnly) { |
| 689 | name = "$$$refs_only_callee_save_method$$$"; |
| 690 | } else { |
| 691 | DCHECK(type == kRefsAndArgs); |
| 692 | name = "$$$refs_and_args_callee_save_method$$$"; |
| 693 | } |
| 694 | method->SetName(intern_table_->InternStrong(name)); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 695 | method->SetSignature(intern_table_->InternStrong("()V")); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 696 | method->SetCode(NULL); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 697 | if ((insns == kThumb2) || (insns == kArm)) { |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 698 | uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) | |
| 699 | (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11); |
| 700 | uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3); |
| 701 | uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9); |
| 702 | uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) | |
| 703 | (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR); |
| 704 | uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) | |
| 705 | (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) | |
| 706 | (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) | |
| 707 | (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) | |
| 708 | (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) | |
| 709 | (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) | |
| 710 | (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) | |
| 711 | (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) | |
| 712 | (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) | |
| 713 | (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) | |
| 714 | (1 << art::arm::S30) | (1 << art::arm::S31); |
| 715 | uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0; |
| 716 | size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ + |
| 717 | __builtin_popcount(fp_spills) /* fprs */ + |
| 718 | 1 /* Method* */) * kPointerSize, kStackAlignment); |
Ian Rogers | 15fdb8c | 2011-09-25 15:45:07 -0700 | [diff] [blame] | 719 | method->SetFrameSizeInBytes(frame_size); |
| 720 | method->SetReturnPcOffsetInBytes(frame_size - kPointerSize); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 721 | method->SetCoreSpillMask(core_spills); |
| 722 | method->SetFpSpillMask(fp_spills); |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 723 | } else if (insns == kX86) { |
| 724 | method->SetFrameSizeInBytes(32); |
| 725 | method->SetReturnPcOffsetInBytes(28); |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 726 | method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 727 | (1 << art::x86::EDI)); |
| 728 | method->SetFpSpillMask(0); |
| 729 | } else { |
| 730 | UNIMPLEMENTED(FATAL); |
| 731 | } |
| 732 | return method; |
| 733 | } |
| 734 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 735 | bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const { |
| 736 | return callee_save_method_[type] != NULL; |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 739 | // Returns a special method that describes all callee saves being spilled to the stack. |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 740 | Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const { |
| 741 | CHECK(HasCalleeSaveMethod(type)); |
| 742 | return callee_save_method_[type]; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 745 | void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) { |
| 746 | DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType)); |
| 747 | callee_save_method_[type] = method; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 750 | } // namespace art |