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