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