blob: 64df1d349e47b811aca74912d8f0c7deff6fc6ec [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "runtime.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07004
Elliott Hughesffe67362011-07-17 12:09:27 -07005#include <cstdio>
6#include <cstdlib>
Brian Carlstrom8a436592011-08-15 21:27:23 -07007#include <limits>
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include <vector>
Elliott Hughesffe67362011-07-17 12:09:27 -07009
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "class_linker.h"
12#include "heap.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070013#include "image.h"
Elliott Hughescf4c6c42011-09-01 15:16:42 -070014#include "intern_table.h"
Elliott Hughesc5f7c912011-08-18 14:00:42 -070015#include "jni_internal.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070016#include "oat_file.h"
Elliott Hughes726079d2011-10-07 18:43:44 -070017#include "ScopedLocalRef.h"
Elliott Hughese27955c2011-08-26 15:21:24 -070018#include "signal_catcher.h"
Brian Carlstrome24fa612011-09-29 00:53:55 -070019#include "space.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070020#include "thread.h"
Elliott Hughes8daa0922011-09-11 13:46:25 -070021#include "thread_list.h"
Carl Shapiro61e019d2011-07-14 16:53:09 -070022
Elliott Hughes90a33692011-08-30 13:27:07 -070023// TODO: this drags in cutil/log.h, which conflicts with our logging.h.
24#include "JniConstants.h"
25
Carl Shapiro1fb86202011-06-27 17:43:13 -070026namespace art {
27
Carl Shapiro2ed144c2011-07-26 16:52:08 -070028Runtime* Runtime::instance_ = NULL;
29
Elliott Hughesdcc24742011-09-07 14:02:44 -070030Runtime::Runtime()
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070031 : verbose_startup_(false),
32 default_stack_size_(Thread::kDefaultStackSize),
Elliott Hughesdcc24742011-09-07 14:02:44 -070033 thread_list_(NULL),
34 intern_table_(NULL),
35 class_linker_(NULL),
36 signal_catcher_(NULL),
37 java_vm_(NULL),
Brian Carlstrom16192862011-09-12 17:50:06 -070038 jni_stub_array_(NULL),
Brian Carlstrome24fa612011-09-29 00:53:55 -070039 abstract_method_error_stub_array_(NULL),
Elliott Hughesdcc24742011-09-07 14:02:44 -070040 started_(false),
41 vfprintf_(NULL),
42 exit_(NULL),
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070043 abort_(NULL),
44 stats_enabled_(false) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -070045 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
46 resolution_stub_array_[i] = NULL;
47 }
48 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
49 callee_save_method_[i] = NULL;
50 }
Elliott Hughesdcc24742011-09-07 14:02:44 -070051}
52
Carl Shapiro61e019d2011-07-14 16:53:09 -070053Runtime::~Runtime() {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070054 // Make sure our internal threads are dead before we start tearing down things they're using.
55 delete signal_catcher_;
Elliott Hughes038a8062011-09-18 14:12:41 -070056 // TODO: GC thread.
Elliott Hughes5fe594f2011-09-08 12:33:17 -070057
Elliott Hughes038a8062011-09-18 14:12:41 -070058 // Make sure all other non-daemon threads have terminated, and all daemon threads are suspended.
Elliott Hughes93e74e82011-09-13 11:07:03 -070059 delete thread_list_;
60
Carl Shapiro61e019d2011-07-14 16:53:09 -070061 delete class_linker_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070062 Heap::Destroy();
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063 delete intern_table_;
Elliott Hughesc5f7c912011-08-18 14:00:42 -070064 delete java_vm_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -070065 Thread::Shutdown();
Carl Shapiro4acf4642011-07-26 18:54:13 -070066 // TODO: acquire a static mutex on Runtime to avoid racing.
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070067 CHECK(instance_ == NULL || instance_ == this);
Carl Shapiro4acf4642011-07-26 18:54:13 -070068 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -070069}
70
Elliott Hughesffe67362011-07-17 12:09:27 -070071void Runtime::Abort(const char* file, int line) {
72 // Get any pending output out of the way.
73 fflush(NULL);
74
75 // Many people have difficulty distinguish aborts from crashes,
76 // so be explicit.
77 LogMessage(file, line, ERROR, -1).stream() << "Runtime aborting...";
78
Elliott Hughesffe67362011-07-17 12:09:27 -070079 // Perform any platform-specific pre-abort actions.
80 PlatformAbort(file, line);
81
Brian Carlstrom6ea095a2011-08-16 15:26:54 -070082 // use abort hook if we have one
83 if (Runtime::Current() != NULL && Runtime::Current()->abort_ != NULL) {
84 Runtime::Current()->abort_();
85 // notreached
86 }
87
Elliott Hughesffe67362011-07-17 12:09:27 -070088 // If we call abort(3) on a device, all threads in the process
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 // receive SIGABRT. debuggerd dumps the stack trace of the main
90 // thread, whether or not that was the thread that failed. By
91 // stuffing a value into a bogus address, we cause a segmentation
Elliott Hughesffe67362011-07-17 12:09:27 -070092 // fault in the current thread, and get a useful log from debuggerd.
93 // We can also trivially tell the difference between a VM crash and
94 // a deliberate abort by looking at the fault address.
95 *reinterpret_cast<char*>(0xdeadd00d) = 38;
96 abort();
Elliott Hughesffe67362011-07-17 12:09:27 -070097 // notreached
98}
99
Elliott Hughesbf86d042011-08-31 17:53:14 -0700100void Runtime::CallExitHook(jint status) {
101 if (exit_ != NULL) {
102 ScopedThreadStateChange tsc(Thread::Current(), Thread::kNative);
103 exit_(status);
104 LOG(WARNING) << "Exit hook returned instead of exiting!";
105 }
106}
107
Brian Carlstrom8a436592011-08-15 21:27:23 -0700108// Parse a string of the form /[0-9]+[kKmMgG]?/, which is used to specify
109// memory sizes. [kK] indicates kilobytes, [mM] megabytes, and
110// [gG] gigabytes.
111//
112// "s" should point just past the "-Xm?" part of the string.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700113// "div" specifies a divisor, e.g. 1024 if the value must be a multiple
114// of 1024.
115//
116// The spec says the -Xmx and -Xms options must be multiples of 1024. It
117// doesn't say anything about -Xss.
118//
119// Returns 0 (a useless size) if "s" is malformed or specifies a low or
120// non-evenly-divisible value.
121//
122size_t ParseMemoryOption(const char *s, size_t div) {
123 // strtoul accepts a leading [+-], which we don't want,
124 // so make sure our string starts with a decimal digit.
125 if (isdigit(*s)) {
126 const char *s2;
127 size_t val = strtoul(s, (char **)&s2, 10);
128 if (s2 != s) {
129 // s2 should be pointing just after the number.
130 // If this is the end of the string, the user
131 // has specified a number of bytes. Otherwise,
132 // there should be exactly one more character
133 // that specifies a multiplier.
134 if (*s2 != '\0') {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700135 // The remainder of the string is either a single multiplier
136 // character, or nothing to indicate that the value is in
137 // bytes.
138 char c = *s2++;
139 if (*s2 == '\0') {
140 size_t mul;
141 if (c == '\0') {
142 mul = 1;
143 } else if (c == 'k' || c == 'K') {
144 mul = KB;
145 } else if (c == 'm' || c == 'M') {
146 mul = MB;
147 } else if (c == 'g' || c == 'G') {
148 mul = GB;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700149 } else {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700150 // Unknown multiplier character.
Brian Carlstrom8a436592011-08-15 21:27:23 -0700151 return 0;
152 }
Brian Carlstromf734cf52011-08-17 16:28:14 -0700153
154 if (val <= std::numeric_limits<size_t>::max() / mul) {
155 val *= mul;
156 } else {
157 // Clamp to a multiple of 1024.
158 val = std::numeric_limits<size_t>::max() & ~(1024-1);
159 }
160 } else {
161 // There's more than one character after the numeric part.
162 return 0;
163 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700164 }
165 // The man page says that a -Xm value must be a multiple of 1024.
166 if (val % div == 0) {
167 return val;
168 }
Carl Shapirofc322c72011-07-27 00:20:01 -0700169 }
170 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700171 return 0;
Carl Shapirofc322c72011-07-27 00:20:01 -0700172}
173
Elliott Hughes0af55432011-08-17 18:37:28 -0700174void LoadJniLibrary(JavaVMExt* vm, const char* name) {
175 // TODO: OS_SHARED_LIB_FORMAT_STR
176 std::string mapped_name(StringPrintf("lib%s.so", name));
Elliott Hughes75770752011-08-24 17:52:38 -0700177 std::string reason;
178 if (!vm->LoadNativeLibrary(mapped_name, NULL, reason)) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700179 LOG(FATAL) << "LoadNativeLibrary failed for \"" << mapped_name << "\": "
180 << reason;
181 }
182}
183
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700184Runtime::ParsedOptions* Runtime::ParsedOptions::Create(const Options& options, bool ignore_unrecognized) {
Elliott Hughes90a33692011-08-30 13:27:07 -0700185 UniquePtr<ParsedOptions> parsed(new ParsedOptions());
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700186 const char* boot_class_path = getenv("BOOTCLASSPATH");
187 if (boot_class_path != NULL) {
188 parsed->boot_class_path_ = getenv("BOOTCLASSPATH");
189 }
190 const char* class_path = getenv("CLASSPATH");
191 if (class_path != NULL) {
192 parsed->class_path_ = getenv("CLASSPATH");
193 }
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700194#ifdef NDEBUG
Elliott Hughes5174fe62011-08-23 15:12:35 -0700195 // -Xcheck:jni is off by default for regular builds...
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700196 parsed->check_jni_ = false;
197#else
198 // ...but on by default in debug builds.
199 parsed->check_jni_ = true;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700200#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700201
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700202 parsed->heap_initial_size_ = Heap::kInitialSize;
203 parsed->heap_maximum_size_ = Heap::kMaximumSize;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700204 parsed->stack_size_ = Thread::kDefaultStackSize;
205
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700206 parsed->hook_vfprintf_ = vfprintf;
207 parsed->hook_exit_ = exit;
208 parsed->hook_abort_ = abort;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700209
210 for (size_t i = 0; i < options.size(); ++i) {
211 const StringPiece& option = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700212 if (false) {
213 LOG(INFO) << "option[" << i << "]=" << option;
214 }
Brian Carlstrom8a436592011-08-15 21:27:23 -0700215 if (option.starts_with("-Xbootclasspath:")) {
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700216 parsed->boot_class_path_ = option.substr(strlen("-Xbootclasspath:")).data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700217 } else if (option == "-classpath" || option == "-cp") {
218 // TODO: support -Djava.class.path
219 i++;
220 if (i == options.size()) {
221 // TODO: usage
222 LOG(FATAL) << "Missing required class path value for " << option;
223 return NULL;
224 }
225 const StringPiece& value = options[i].first;
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700226 parsed->class_path_ = value.data();
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700227 } else if (option.starts_with("-Ximage:")) {
228 parsed->images_.push_back(option.substr(strlen("-Ximage:")).data());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700229 } else if (option.starts_with("-Xcheck:jni")) {
230 parsed->check_jni_ = true;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700231 } else if (option.starts_with("-Xms")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700232 size_t size = ParseMemoryOption(option.substr(strlen("-Xms")).data(), 1024);
233 if (size == 0) {
234 if (ignore_unrecognized) {
235 continue;
236 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700237 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700238 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700239 return NULL;
240 }
241 parsed->heap_initial_size_ = size;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700242 } else if (option.starts_with("-Xmx")) {
Brian Carlstromf734cf52011-08-17 16:28:14 -0700243 size_t size = ParseMemoryOption(option.substr(strlen("-Xmx")).data(), 1024);
244 if (size == 0) {
245 if (ignore_unrecognized) {
246 continue;
247 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700248 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700249 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700250 return NULL;
251 }
252 parsed->heap_maximum_size_ = size;
253 } else if (option.starts_with("-Xss")) {
254 size_t size = ParseMemoryOption(option.substr(strlen("-Xss")).data(), 1);
255 if (size == 0) {
256 if (ignore_unrecognized) {
257 continue;
258 }
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700259 // TODO: usage
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700260 LOG(FATAL) << "Failed to parse " << option;
Brian Carlstromf734cf52011-08-17 16:28:14 -0700261 return NULL;
262 }
263 parsed->stack_size_ = size;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700264 } else if (option.starts_with("-D")) {
265 parsed->properties_.push_back(option.substr(strlen("-D")).data());
Elliott Hughesa0957642011-09-02 14:27:33 -0700266 } else if (option.starts_with("-Xjnitrace:")) {
267 parsed->jni_trace_ = option.substr(strlen("-Xjnitrace:")).data();
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700268 } else if (option.starts_with("-verbose:")) {
Elliott Hughes0af55432011-08-17 18:37:28 -0700269 std::vector<std::string> verbose_options;
Elliott Hughes34023802011-08-30 12:06:17 -0700270 Split(option.substr(strlen("-verbose:")).data(), ',', verbose_options);
Elliott Hughes0af55432011-08-17 18:37:28 -0700271 for (size_t i = 0; i < verbose_options.size(); ++i) {
272 parsed->verbose_.insert(verbose_options[i]);
273 }
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700274 } else if (option == "vfprintf") {
275 parsed->hook_vfprintf_ = reinterpret_cast<int (*)(FILE *, const char*, va_list)>(options[i].second);
276 } else if (option == "exit") {
277 parsed->hook_exit_ = reinterpret_cast<void(*)(jint)>(options[i].second);
278 } else if (option == "abort") {
279 parsed->hook_abort_ = reinterpret_cast<void(*)()>(options[i].second);
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700280 } else if (option == "host-prefix") {
281 parsed->host_prefix_ = reinterpret_cast<const char*>(options[i].second);
Brian Carlstrom8a436592011-08-15 21:27:23 -0700282 } else {
283 if (!ignore_unrecognized) {
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700284 // TODO: print usage via vfprintf
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700285 LOG(ERROR) << "Unrecognized option " << option;
286 // TODO: this should exit, but for now tolerate unknown options
287 //return NULL;
Brian Carlstrom8a436592011-08-15 21:27:23 -0700288 }
289 }
290 }
291
Elliott Hughes85d15452011-09-16 17:33:01 -0700292 LOG(INFO) << "CheckJNI is " << (parsed->check_jni_ ? "on" : "off");
293
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700294 return parsed.release();
Brian Carlstrom8a436592011-08-15 21:27:23 -0700295}
296
297Runtime* Runtime::Create(const Options& options, bool ignore_unrecognized) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700298 // TODO: acquire a static mutex on Runtime to avoid racing.
299 if (Runtime::instance_ != NULL) {
300 return NULL;
301 }
Elliott Hughesdcc24742011-09-07 14:02:44 -0700302 instance_ = new Runtime;
303 if (!instance_->Init(options, ignore_unrecognized)) {
304 delete instance_;
305 instance_ = NULL;
Carl Shapiro61e019d2011-07-14 16:53:09 -0700306 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700307 return instance_;
308}
Elliott Hughes0af55432011-08-17 18:37:28 -0700309
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700310void Runtime::Start() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700311 if (IsVerboseStartup()) {
312 LOG(INFO) << "Runtime::Start entering";
313 }
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700314
315 CHECK(host_prefix_.empty()) << host_prefix_;
316
Elliott Hughes038a8062011-09-18 14:12:41 -0700317 InitNativeMethods();
Elliott Hughes85d15452011-09-16 17:33:01 -0700318
Elliott Hughes038a8062011-09-18 14:12:41 -0700319 Thread::FinishStartup();
Elliott Hughes85d15452011-09-16 17:33:01 -0700320
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700321 class_linker_->RunRootClinits();
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700322
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700323 // Class::AllocObject asserts that all objects allocated better be
324 // initialized after Runtime::IsStarted is true, so this needs to
325 // come after ClassLinker::RunRootClinits.
326 started_ = true;
327
Elliott Hughes85d15452011-09-16 17:33:01 -0700328 StartDaemonThreads();
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700329
Elliott Hughes726079d2011-10-07 18:43:44 -0700330 Thread::Current()->GetJniEnv()->locals.AssertEmpty();
331
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700332 if (IsVerboseStartup()) {
333 LOG(INFO) << "Runtime::Start exiting";
334 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700335}
336
337void Runtime::StartDaemonThreads() {
338 signal_catcher_ = new SignalCatcher;
339
Elliott Hughes719b3232011-09-25 17:42:19 -0700340 Thread* self = Thread::Current();
341 JNIEnv* env = self->GetJniEnv();
Elliott Hughes726079d2011-10-07 18:43:44 -0700342 ScopedLocalRef<jclass> c(env, env->FindClass("java/lang/Daemons"));
343 CHECK(c.get() != NULL);
344 jmethodID mid = env->GetStaticMethodID(c.get(), "start", "()V");
Elliott Hughes719b3232011-09-25 17:42:19 -0700345 CHECK(mid != NULL);
Elliott Hughes726079d2011-10-07 18:43:44 -0700346 env->CallStaticVoidMethod(c.get(), mid);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700347}
348
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700349bool Runtime::IsStarted() const {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700350 return started_;
351}
352
Elliott Hughes0af55432011-08-17 18:37:28 -0700353bool Runtime::Init(const Options& raw_options, bool ignore_unrecognized) {
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700354 CHECK_EQ(sysconf(_SC_PAGE_SIZE), kPageSize);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700355
Elliott Hughes90a33692011-08-30 13:27:07 -0700356 UniquePtr<ParsedOptions> options(ParsedOptions::Create(raw_options, ignore_unrecognized));
357 if (options.get() == NULL) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700358 LOG(ERROR) << "Failed to parse options";
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700359 return false;
360 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700361 verbose_startup_ = options->IsVerbose("startup");
362 if (IsVerboseStartup()) {
363 LOG(INFO) << "Runtime::Init -verbose:startup enabled";
364 }
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700365
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700366 host_prefix_ = options->host_prefix_;
367 boot_class_path_ = options->boot_class_path_;
368 class_path_ = options->class_path_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700369 properties_ = options->properties_;
370
Elliott Hughes0af55432011-08-17 18:37:28 -0700371 vfprintf_ = options->hook_vfprintf_;
372 exit_ = options->hook_exit_;
373 abort_ = options->hook_abort_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700374
Elliott Hughesbe759c62011-09-08 19:38:21 -0700375 default_stack_size_ = options->stack_size_;
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700376
Elliott Hughes14357e82011-09-26 10:42:15 -0700377 thread_list_ = new ThreadList(options->IsVerbose("thread"));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700378 intern_table_ = new InternTable;
379
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700380 Heap::Init(options->heap_initial_size_, options->heap_maximum_size_, options->images_);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700381
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700382 BlockSignals();
383
Elliott Hughesa0957642011-09-02 14:27:33 -0700384 java_vm_ = new JavaVMExt(this, options.get());
Elliott Hughes515a5bc2011-08-17 11:08:34 -0700385
Elliott Hughesbe759c62011-09-08 19:38:21 -0700386 Thread::Startup();
Elliott Hughesd92bec42011-09-02 17:04:36 -0700387
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700388 // ClassLinker needs an attached thread, but we can't fully attach a thread
389 // without creating objects.
390 Thread::Attach(this, "main", false);
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700391
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700392 CHECK_GE(Heap::GetSpaces().size(), 1U);
393 class_linker_ = ((Heap::GetSpaces()[0]->IsImageSpace())
394 ? ClassLinker::Create(intern_table_)
395 : ClassLinker::Create(options->boot_class_path_, intern_table_));
Brian Carlstrom6ea095a2011-08-16 15:26:54 -0700396
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700397 if (IsVerboseStartup()) {
398 LOG(INFO) << "Runtime::Init exiting";
399 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700400 return true;
401}
402
Elliott Hughes038a8062011-09-18 14:12:41 -0700403void Runtime::InitNativeMethods() {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700404 if (IsVerboseStartup()) {
405 LOG(INFO) << "Runtime::InitNativeMethods entering";
406 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700407 Thread* self = Thread::Current();
408 JNIEnv* env = self->GetJniEnv();
409
Elliott Hughes418d20f2011-09-22 14:00:39 -0700410 // Must be in the kNative state for calling native methods (JNI_OnLoad code).
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700411 ScopedThreadStateChange tsc(self, Thread::kNative);
412
Elliott Hughes418d20f2011-09-22 14:00:39 -0700413 // First set up JniConstants, which is used by both the runtime's built-in native
414 // methods and libcore.
Elliott Hughesfea966e2011-09-22 10:26:20 -0700415 JniConstants::init(env);
416
Elliott Hughes418d20f2011-09-22 14:00:39 -0700417 // Then set up the native methods provided by the runtime itself.
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700418 RegisterRuntimeNativeMethods(env);
419
Elliott Hughes418d20f2011-09-22 14:00:39 -0700420 // Then set up libcore, which is just a regular JNI library with a regular JNI_OnLoad.
421 // Most JNI libraries can just use System.loadLibrary, but libcore can't because it's
422 // the library that implements System.loadLibrary!
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700423 LoadJniLibrary(instance_->GetJavaVM(), "javacore");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700424 if (IsVerboseStartup()) {
425 LOG(INFO) << "Runtime::InitNativeMethods exiting";
426 }
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700427}
428
429void Runtime::RegisterRuntimeNativeMethods(JNIEnv* env) {
430#define REGISTER(FN) extern void FN(JNIEnv*); FN(env)
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700431 REGISTER(register_dalvik_system_DexFile);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700432 REGISTER(register_dalvik_system_VMDebug);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700433 REGISTER(register_dalvik_system_VMRuntime);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700434 REGISTER(register_dalvik_system_VMStack);
Elliott Hughes01158d72011-09-19 19:47:10 -0700435 REGISTER(register_dalvik_system_Zygote);
Elliott Hughesd369bb72011-09-12 14:41:14 -0700436 REGISTER(register_java_lang_Class);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700437 REGISTER(register_java_lang_Object);
438 REGISTER(register_java_lang_Runtime);
439 REGISTER(register_java_lang_String);
440 REGISTER(register_java_lang_System);
Elliott Hughes8daa0922011-09-11 13:46:25 -0700441 REGISTER(register_java_lang_Thread);
Elliott Hughes1240dad2011-09-09 16:24:50 -0700442 REGISTER(register_java_lang_Throwable);
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700443 REGISTER(register_java_lang_VMClassLoader);
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700444 REGISTER(register_java_lang_reflect_Array);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700445 REGISTER(register_java_lang_reflect_Constructor);
Brian Carlstromf867b6f2011-09-16 12:17:25 -0700446 REGISTER(register_java_lang_reflect_Field);
447 REGISTER(register_java_lang_reflect_Method);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700448 //REGISTER(register_java_lang_reflect_Proxy);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700449 REGISTER(register_java_util_concurrent_atomic_AtomicLong);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700450 REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmServer);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700451 //REGISTER(register_org_apache_harmony_dalvik_ddmc_DdmVmInternal);
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700452 REGISTER(register_sun_misc_Unsafe);
Elliott Hughesad7c2a32011-08-31 11:58:10 -0700453#undef REGISTER
454}
455
Elliott Hughes8daa0922011-09-11 13:46:25 -0700456void Runtime::Dump(std::ostream& os) {
Elliott Hughese27955c2011-08-26 15:21:24 -0700457 // TODO: dump other runtime statistics?
458 os << "Loaded classes: " << class_linker_->NumLoadedClasses() << "\n";
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700459 os << "Intern table size: " << GetInternTable()->Size() << "\n";
Elliott Hughese27955c2011-08-26 15:21:24 -0700460 // LOGV("VM stats: meth=%d ifld=%d sfld=%d linear=%d",
461 // gDvm.numDeclaredMethods,
462 // gDvm.numDeclaredInstFields,
463 // gDvm.numDeclaredStaticFields,
464 // gDvm.pBootLoaderAlloc->curOffset);
465 // LOGI("GC precise methods: %d", dvmPointerSetGetCount(gDvm.preciseMethods));
Elliott Hughes42ee1422011-09-06 12:33:32 -0700466 os << "\n";
Elliott Hughes8daa0922011-09-11 13:46:25 -0700467
468 thread_list_->Dump(os);
Elliott Hughese27955c2011-08-26 15:21:24 -0700469}
470
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700471void Runtime::SetStatsEnabled(bool new_state) {
472 if (new_state == true) {
473 GetStats()->Clear(~0);
474 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
475 Thread::Current()->GetStats()->Clear(~0);
476 }
477 stats_enabled_ = new_state;
478}
479
480void Runtime::ResetStats(int kinds) {
481 GetStats()->Clear(kinds & 0xffff);
482 // TODO: wouldn't it make more sense to clear _all_ threads' stats?
483 Thread::Current()->GetStats()->Clear(kinds >> 16);
484}
485
486RuntimeStats* Runtime::GetStats() {
487 return &stats_;
488}
489
490int32_t Runtime::GetStat(int kind) {
491 RuntimeStats* stats;
492 if (kind < (1<<16)) {
493 stats = GetStats();
494 } else {
495 stats = Thread::Current()->GetStats();
496 kind >>= 16;
497 }
498 switch (kind) {
499 case KIND_ALLOCATED_OBJECTS:
500 return stats->allocated_objects;
501 case KIND_ALLOCATED_BYTES:
502 return stats->allocated_bytes;
503 case KIND_FREED_OBJECTS:
504 return stats->freed_objects;
505 case KIND_FREED_BYTES:
506 return stats->freed_bytes;
507 case KIND_GC_INVOCATIONS:
508 return stats->gc_for_alloc_count;
509 case KIND_CLASS_INIT_COUNT:
510 return stats->class_init_count;
511 case KIND_CLASS_INIT_TIME:
512 // Convert ns to us, reduce to 32 bits.
513 return (int) (stats->class_init_time_ns / 1000);
514 case KIND_EXT_ALLOCATED_OBJECTS:
515 case KIND_EXT_ALLOCATED_BYTES:
516 case KIND_EXT_FREED_OBJECTS:
517 case KIND_EXT_FREED_BYTES:
518 return 0; // backward compatibility
519 default:
520 CHECK(false);
521 return -1; // unreachable
522 }
523}
524
Elliott Hughesc1674ed2011-08-25 18:09:09 -0700525void Runtime::BlockSignals() {
526 sigset_t sigset;
527 if (sigemptyset(&sigset) == -1) {
528 PLOG(FATAL) << "sigemptyset failed";
529 }
530 if (sigaddset(&sigset, SIGPIPE) == -1) {
531 PLOG(ERROR) << "sigaddset SIGPIPE failed";
532 }
533 // SIGQUIT is used to dump the runtime's state (including stack traces).
534 if (sigaddset(&sigset, SIGQUIT) == -1) {
535 PLOG(ERROR) << "sigaddset SIGQUIT failed";
536 }
537 // SIGUSR1 is used to initiate a heap dump.
538 if (sigaddset(&sigset, SIGUSR1) == -1) {
539 PLOG(ERROR) << "sigaddset SIGUSR1 failed";
540 }
541 CHECK_EQ(sigprocmask(SIG_BLOCK, &sigset, NULL), 0);
542}
543
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700544void Runtime::AttachCurrentThread(const char* name, bool as_daemon) {
545 Thread::Attach(instance_, name, as_daemon);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700546}
547
Elliott Hughesd92bec42011-09-02 17:04:36 -0700548void Runtime::DetachCurrentThread() {
Elliott Hughes038a8062011-09-18 14:12:41 -0700549 // TODO: check we're not calling DetachCurrentThread from a call stack that
550 // includes managed frames. (It's only valid if the stack is all-native.)
Elliott Hughes02b48d12011-09-07 17:15:51 -0700551 thread_list_->Unregister();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700552}
553
Brian Carlstrome24fa612011-09-29 00:53:55 -0700554void Runtime::VisitRoots(Heap::RootVisitor* visitor, void* arg) const {
555 class_linker_->VisitRoots(visitor, arg);
556 intern_table_->VisitRoots(visitor, arg);
557 java_vm_->VisitRoots(visitor, arg);
558 thread_list_->VisitRoots(visitor, arg);
559 visitor(jni_stub_array_, arg);
560 visitor(abstract_method_error_stub_array_, arg);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700561 for (int i = 0; i < Runtime::kLastTrampolineMethodType; i++) {
562 visitor(resolution_stub_array_[i], arg);
563 }
564 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
565 visitor(callee_save_method_[i], arg);
566 }
Brian Carlstrome24fa612011-09-29 00:53:55 -0700567
568 //(*visitor)(&gDvm.outOfMemoryObj, 0, ROOT_VM_INTERNAL, arg);
569 //(*visitor)(&gDvm.internalErrorObj, 0, ROOT_VM_INTERNAL, arg);
570 //(*visitor)(&gDvm.noClassDefFoundErrorObj, 0, ROOT_VM_INTERNAL, arg);
571 UNIMPLEMENTED(WARNING) << "some roots not marked";
572}
573
574bool Runtime::HasJniStubArray() const {
575 return jni_stub_array_ != NULL;
576}
577
578ByteArray* Runtime::GetJniStubArray() const {
579 CHECK(jni_stub_array_ != NULL);
580 return jni_stub_array_;
581}
582
583void Runtime::SetJniStubArray(ByteArray* jni_stub_array) {
584 CHECK(jni_stub_array != NULL);
585 CHECK(jni_stub_array_ == NULL || jni_stub_array_ == jni_stub_array);
586 jni_stub_array_ = jni_stub_array;
587}
588
589bool Runtime::HasAbstractMethodErrorStubArray() const {
590 return abstract_method_error_stub_array_ != NULL;
591}
592
593ByteArray* Runtime::GetAbstractMethodErrorStubArray() const {
594 CHECK(abstract_method_error_stub_array_ != NULL);
595 return abstract_method_error_stub_array_;
596}
597
598void Runtime::SetAbstractMethodErrorStubArray(ByteArray* abstract_method_error_stub_array) {
599 CHECK(abstract_method_error_stub_array != NULL);
600 CHECK(abstract_method_error_stub_array_ == NULL || abstract_method_error_stub_array_ == abstract_method_error_stub_array);
601 abstract_method_error_stub_array_ = abstract_method_error_stub_array;
602}
603
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700604
605Runtime::TrampolineType Runtime::GetTrampolineType(Method* method) {
606 if (method == NULL) {
607 return Runtime::kUnknownMethod;
608 } else if (method->IsStatic()) {
609 return Runtime::kStaticMethod;
610 } else {
611 return Runtime::kInstanceMethod;
612 }
Ian Rogersad25ac52011-10-04 19:13:33 -0700613}
614
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700615bool Runtime::HasResolutionStubArray(TrampolineType type) const {
616 return resolution_stub_array_[type] != NULL;
Ian Rogersad25ac52011-10-04 19:13:33 -0700617}
618
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700619ByteArray* Runtime::GetResolutionStubArray(TrampolineType type) const {
620 CHECK(HasResolutionStubArray(type));
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700621 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastTrampolineMethodType));
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700622 return resolution_stub_array_[type];
623}
624
625void Runtime::SetResolutionStubArray(ByteArray* resolution_stub_array, TrampolineType type) {
Ian Rogersad25ac52011-10-04 19:13:33 -0700626 CHECK(resolution_stub_array != NULL);
Ian Rogers1cb0a1d2011-10-06 15:24:35 -0700627 CHECK(!HasResolutionStubArray(type) || resolution_stub_array_[type] == resolution_stub_array);
628 resolution_stub_array_[type] = resolution_stub_array;
Ian Rogersad25ac52011-10-04 19:13:33 -0700629}
630
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700631Method* Runtime::CreateCalleeSaveMethod(InstructionSet insns, CalleeSaveType type) {
Ian Rogersff1ed472011-09-20 13:46:24 -0700632 Class* method_class = Method::GetMethodClass();
633 Method* method = down_cast<Method*>(method_class->AllocObject());
634 method->SetDeclaringClass(method_class);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700635 const char* name;
636 if (type == kSaveAll) {
637 name = "$$$callee_save_method$$$";
638 } else if (type == kRefsOnly) {
639 name = "$$$refs_only_callee_save_method$$$";
640 } else {
641 DCHECK(type == kRefsAndArgs);
642 name = "$$$refs_and_args_callee_save_method$$$";
643 }
644 method->SetName(intern_table_->InternStrong(name));
Ian Rogersff1ed472011-09-20 13:46:24 -0700645 method->SetSignature(intern_table_->InternStrong("()V"));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700646 method->SetCode(NULL);
Ian Rogersff1ed472011-09-20 13:46:24 -0700647 if ((insns == kThumb2) || (insns == kArm)) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700648 uint32_t ref_spills = (1 << art::arm::R5) | (1 << art::arm::R6) | (1 << art::arm::R7) |
649 (1 << art::arm::R8) | (1 << art::arm::R10) | (1 << art::arm::R11);
650 uint32_t arg_spills = (1 << art::arm::R1) | (1 << art::arm::R2) | (1 << art::arm::R3);
651 uint32_t all_spills = (1 << art::arm::R4) | (1 << art::arm::R9);
652 uint32_t core_spills = ref_spills | (type == kRefsAndArgs ? arg_spills :0) |
653 (type == kSaveAll ? all_spills :0) | (1 << art::arm::LR);
654 uint32_t fp_all_spills = (1 << art::arm::S0) | (1 << art::arm::S1) | (1 << art::arm::S2) |
655 (1 << art::arm::S3) | (1 << art::arm::S4) | (1 << art::arm::S5) |
656 (1 << art::arm::S6) | (1 << art::arm::S7) | (1 << art::arm::S8) |
657 (1 << art::arm::S9) | (1 << art::arm::S10) | (1 << art::arm::S11) |
658 (1 << art::arm::S12) | (1 << art::arm::S13) | (1 << art::arm::S14) |
659 (1 << art::arm::S15) | (1 << art::arm::S16) | (1 << art::arm::S17) |
660 (1 << art::arm::S18) | (1 << art::arm::S19) | (1 << art::arm::S20) |
661 (1 << art::arm::S21) | (1 << art::arm::S22) | (1 << art::arm::S23) |
662 (1 << art::arm::S24) | (1 << art::arm::S25) | (1 << art::arm::S26) |
663 (1 << art::arm::S27) | (1 << art::arm::S28) | (1 << art::arm::S29) |
664 (1 << art::arm::S30) | (1 << art::arm::S31);
665 uint32_t fp_spills = type == kSaveAll ? fp_all_spills : 0;
666 size_t frame_size = RoundUp((__builtin_popcount(core_spills) /* gprs */ +
667 __builtin_popcount(fp_spills) /* fprs */ +
668 1 /* Method* */) * kPointerSize, kStackAlignment);
Ian Rogers15fdb8c2011-09-25 15:45:07 -0700669 method->SetFrameSizeInBytes(frame_size);
670 method->SetReturnPcOffsetInBytes(frame_size - kPointerSize);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700671 method->SetCoreSpillMask(core_spills);
672 method->SetFpSpillMask(fp_spills);
Ian Rogersff1ed472011-09-20 13:46:24 -0700673 } else if (insns == kX86) {
674 method->SetFrameSizeInBytes(32);
675 method->SetReturnPcOffsetInBytes(28);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700676 method->SetCoreSpillMask((1 << art::x86::EBX) | (1 << art::x86::EBP) | (1 << art::x86::ESI) |
Ian Rogersff1ed472011-09-20 13:46:24 -0700677 (1 << art::x86::EDI));
678 method->SetFpSpillMask(0);
679 } else {
680 UNIMPLEMENTED(FATAL);
681 }
682 return method;
683}
684
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700685bool Runtime::HasCalleeSaveMethod(CalleeSaveType type) const {
686 return callee_save_method_[type] != NULL;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700687}
688
Brian Carlstrome24fa612011-09-29 00:53:55 -0700689// Returns a special method that describes all callee saves being spilled to the stack.
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700690Method* Runtime::GetCalleeSaveMethod(CalleeSaveType type) const {
691 CHECK(HasCalleeSaveMethod(type));
692 return callee_save_method_[type];
Brian Carlstrome24fa612011-09-29 00:53:55 -0700693}
694
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700695void Runtime::SetCalleeSaveMethod(Method* method, CalleeSaveType type) {
696 DCHECK_LT(static_cast<int>(type), static_cast<int>(kLastCalleeSaveType));
697 callee_save_method_[type] = method;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700698}
699
Carl Shapiro1fb86202011-06-27 17:43:13 -0700700} // namespace art