runtime.h revision e0a53e99e2a01f8668d6616c3cec7e2f5a711286
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef ART_RUNTIME_RUNTIME_H_ 18#define ART_RUNTIME_RUNTIME_H_ 19 20#include <jni.h> 21#include <stdio.h> 22 23#include <iosfwd> 24#include <string> 25#include <utility> 26#include <vector> 27 28#include "base/macros.h" 29#include "base/stringpiece.h" 30#include "gc/heap.h" 31#include "globals.h" 32#include "instruction_set.h" 33#include "instrumentation.h" 34#include "jobject_comparator.h" 35#include "locks.h" 36#include "root_visitor.h" 37#include "runtime_stats.h" 38#include "safe_map.h" 39 40namespace art { 41 42namespace gc { 43 class Heap; 44} 45namespace mirror { 46 class AbstractMethod; 47 class ClassLoader; 48 template<class T> class PrimitiveArray; 49 typedef PrimitiveArray<int8_t> ByteArray; 50 class String; 51 class Throwable; 52} // namespace mirror 53class ClassLinker; 54class DexFile; 55class InternTable; 56struct JavaVMExt; 57class MonitorList; 58class SignalCatcher; 59class ThreadList; 60class Trace; 61 62class Runtime { 63 public: 64 typedef std::vector<std::pair<std::string, const void*> > Options; 65 66 // In small mode, apps with fewer than this number of methods will be compiled 67 // anyways. 68 // TODO: come up with a reasonable default. 69 static const size_t kDefaultSmallModeMethodThreshold = 0; 70 71 // In small mode, methods smaller than this dex op count limit will get compiled 72 // anyways. 73 // TODO: come up with a reasonable default. 74 static const size_t kDefaultSmallModeMethodDexSizeLimit = 300; 75 76 class ParsedOptions { 77 public: 78 // returns null if problem parsing and ignore_unrecognized is false 79 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized); 80 81 const std::vector<const DexFile*>* boot_class_path_; 82 std::string boot_class_path_string_; 83 std::string class_path_string_; 84 std::string host_prefix_; 85 std::string image_; 86 bool check_jni_; 87 std::string jni_trace_; 88 bool is_compiler_; 89 bool is_zygote_; 90 bool interpreter_only_; 91 bool is_concurrent_gc_enabled_; 92 size_t heap_initial_size_; 93 size_t heap_maximum_size_; 94 size_t heap_growth_limit_; 95 size_t heap_gc_threads_; 96 size_t heap_min_free_; 97 size_t heap_max_free_; 98 double heap_target_utilization_; 99 size_t stack_size_; 100 bool low_memory_mode_; 101 size_t lock_profiling_threshold_; 102 std::string stack_trace_file_; 103 bool method_trace_; 104 std::string method_trace_file_; 105 size_t method_trace_file_size_; 106 bool (*hook_is_sensitive_thread_)(); 107 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap); 108 void (*hook_exit_)(jint status); 109 void (*hook_abort_)(); 110 std::vector<std::string> properties_; 111 bool small_mode_; 112 113 size_t small_mode_method_threshold_; 114 size_t small_mode_method_dex_size_limit_; 115 116 bool sea_ir_mode_; 117 118 private: 119 ParsedOptions() {} 120 }; 121 122 // Creates and initializes a new runtime. 123 static bool Create(const Options& options, bool ignore_unrecognized) 124 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_); 125 126 bool IsCompiler() const { 127 return is_compiler_; 128 } 129 130 bool IsZygote() const { 131 return is_zygote_; 132 } 133 134 bool IsConcurrentGcEnabled() const { 135 return is_concurrent_gc_enabled_; 136 } 137 138#ifdef ART_SEA_IR_MODE 139 bool IsSeaIRMode() const { 140 return sea_ir_mode_; 141 } 142#endif 143 144 void SetSeaIRMode(bool sea_ir_mode) { 145 sea_ir_mode_ = sea_ir_mode; 146 } 147 148 bool IsSmallMode() const { 149 return small_mode_; 150 } 151 152 153 void SetSmallMode(bool small_mode) { 154 small_mode_ = small_mode; 155 } 156 157 size_t GetSmallModeMethodThreshold() const { 158 return small_mode_method_threshold_; 159 } 160 161 size_t GetSmallModeMethodDexSizeLimit() const { 162 return small_mode_method_dex_size_limit_; 163 } 164 165 const std::string& GetHostPrefix() const { 166 DCHECK(!IsStarted()); 167 return host_prefix_; 168 } 169 170 // Starts a runtime, which may cause threads to be started and code to run. 171 bool Start() UNLOCK_FUNCTION(Locks::mutator_lock_); 172 173 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 174 return shutting_down_; 175 } 176 177 size_t NumberOfThreadsBeingBorn() const EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 178 return threads_being_born_; 179 } 180 181 void StartThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 182 threads_being_born_++; 183 } 184 185 void EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_); 186 187 bool IsStarted() const { 188 return started_; 189 } 190 191 bool IsFinishedStarting() const { 192 return finished_starting_; 193 } 194 195 static Runtime* Current() { 196 return instance_; 197 } 198 199 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most 200 // callers should prefer. 201 // This isn't marked ((noreturn)) because then gcc will merge multiple calls 202 // in a single function together. This reduces code size slightly, but means 203 // that the native stack trace we get may point at the wrong call site. 204 static void Abort() LOCKS_EXCLUDED(Locks::abort_lock_); 205 206 // Returns the "main" ThreadGroup, used when attaching user threads. 207 jobject GetMainThreadGroup() const; 208 209 // Returns the "system" ThreadGroup, used when attaching our internal threads. 210 jobject GetSystemThreadGroup() const; 211 212 // Attaches the calling native thread to the runtime. 213 bool AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group, 214 bool create_peer); 215 216 void CallExitHook(jint status); 217 218 // Detaches the current native thread from the runtime. 219 void DetachCurrentThread() LOCKS_EXCLUDED(Locks::mutator_lock_); 220 221 void DumpForSigQuit(std::ostream& os) 222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 223 void DumpLockHolders(std::ostream& os); 224 225 ~Runtime(); 226 227 const std::string& GetBootClassPathString() const { 228 return boot_class_path_string_; 229 } 230 231 const std::string& GetClassPathString() const { 232 return class_path_string_; 233 } 234 235 ClassLinker* GetClassLinker() const { 236 return class_linker_; 237 } 238 239 size_t GetDefaultStackSize() const { 240 return default_stack_size_; 241 } 242 243 gc::Heap* GetHeap() const { 244 return heap_; 245 } 246 247 InternTable* GetInternTable() const { 248 return intern_table_; 249 } 250 251 JavaVMExt* GetJavaVM() const { 252 return java_vm_; 253 } 254 255 MonitorList* GetMonitorList() const { 256 return monitor_list_; 257 } 258 259 mirror::Throwable* GetPreAllocatedOutOfMemoryError() { 260 return pre_allocated_OutOfMemoryError_; 261 } 262 263 const std::vector<std::string>& GetProperties() const { 264 return properties_; 265 } 266 267 ThreadList* GetThreadList() const { 268 return thread_list_; 269 } 270 271 const char* GetVersion() const { 272 return "2.0.0"; 273 } 274 275 // Visit all the roots. If only_dirty is true then non-dirty roots won't be visited. If 276 // clean_dirty is true then dirty roots will be marked as non-dirty after visiting. 277 void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty) 278 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 279 280 // Visit all of the roots we can do safely do concurrently. 281 void VisitConcurrentRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty); 282 283 // Visit all of the non thread roots, we can do this with mutators unpaused. 284 void VisitNonThreadRoots(RootVisitor* visitor, void* arg); 285 286 // Visit all other roots which must be done with mutators suspended. 287 void VisitNonConcurrentRoots(RootVisitor* visitor, void* arg) 288 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 289 290 // Returns a special method that calls into a trampoline for runtime method resolution 291 mirror::AbstractMethod* GetResolutionMethod() const { 292 CHECK(HasResolutionMethod()); 293 return resolution_method_; 294 } 295 296 bool HasResolutionMethod() const { 297 return resolution_method_ != NULL; 298 } 299 300 void SetResolutionMethod(mirror::AbstractMethod* method) { 301 resolution_method_ = method; 302 } 303 304 mirror::AbstractMethod* CreateResolutionMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 305 306 // Returns a special method that describes all callee saves being spilled to the stack. 307 enum CalleeSaveType { 308 kSaveAll, 309 kRefsOnly, 310 kRefsAndArgs, 311 kLastCalleeSaveType // Value used for iteration 312 }; 313 314 bool HasCalleeSaveMethod(CalleeSaveType type) const { 315 return callee_save_methods_[type] != NULL; 316 } 317 318 mirror::AbstractMethod* GetCalleeSaveMethod(CalleeSaveType type) const { 319 DCHECK(HasCalleeSaveMethod(type)); 320 return callee_save_methods_[type]; 321 } 322 323 void SetCalleeSaveMethod(mirror::AbstractMethod* method, CalleeSaveType type); 324 325 mirror::AbstractMethod* CreateCalleeSaveMethod(InstructionSet instruction_set, 326 CalleeSaveType type) 327 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 328 329 mirror::AbstractMethod* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set) 330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 331 332 mirror::AbstractMethod* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set) 333 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 334 335 int32_t GetStat(int kind); 336 337 RuntimeStats* GetStats() { 338 return &stats_; 339 } 340 341 bool HasStatsEnabled() const { 342 return stats_enabled_; 343 } 344 345 void ResetStats(int kinds); 346 347 void SetStatsEnabled(bool new_state); 348 349 bool PreZygoteFork(); 350 bool InitZygote(); 351 void DidForkFromZygote(); 352 353 instrumentation::Instrumentation* GetInstrumentation() { 354 return &instrumentation_; 355 } 356 357 bool UseCompileTimeClassPath() const { 358 return use_compile_time_class_path_; 359 } 360 361 const std::vector<const DexFile*>& GetCompileTimeClassPath(jobject class_loader); 362 void SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path); 363 364 private: 365 static void InitPlatformSignalHandlers(); 366 367 Runtime(); 368 369 void BlockSignals(); 370 371 bool Init(const Options& options, bool ignore_unrecognized) 372 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_); 373 void InitNativeMethods() LOCKS_EXCLUDED(Locks::mutator_lock_); 374 void InitThreadGroups(Thread* self); 375 void RegisterRuntimeNativeMethods(JNIEnv* env); 376 377 void StartDaemonThreads(); 378 void StartSignalCatcher(); 379 380 // A pointer to the active runtime or NULL. 381 static Runtime* instance_; 382 383 bool is_compiler_; 384 bool is_zygote_; 385 bool is_concurrent_gc_enabled_; 386 387 bool small_mode_; 388 size_t small_mode_method_threshold_; 389 size_t small_mode_method_dex_size_limit_; 390 391 bool sea_ir_mode_; 392 393 // The host prefix is used during cross compilation. It is removed 394 // from the start of host paths such as: 395 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat 396 // to produce target paths such as 397 // /system/framework/boot.oat 398 // Similarly it is prepended to target paths to arrive back at a 399 // host past. In both cases this is necessary because image and oat 400 // files embedded expect paths of dependent files (an image points 401 // to an oat file and an oat files to one or more dex files). These 402 // files contain the expected target path. 403 std::string host_prefix_; 404 405 std::string boot_class_path_string_; 406 std::string class_path_string_; 407 std::vector<std::string> properties_; 408 409 // The default stack size for managed threads created by the runtime. 410 size_t default_stack_size_; 411 412 gc::Heap* heap_; 413 414 MonitorList* monitor_list_; 415 416 ThreadList* thread_list_; 417 418 InternTable* intern_table_; 419 420 ClassLinker* class_linker_; 421 422 SignalCatcher* signal_catcher_; 423 std::string stack_trace_file_; 424 425 JavaVMExt* java_vm_; 426 427 mirror::Throwable* pre_allocated_OutOfMemoryError_; 428 429 mirror::AbstractMethod* callee_save_methods_[kLastCalleeSaveType]; 430 431 mirror::AbstractMethod* resolution_method_; 432 433 // As returned by ClassLoader.getSystemClassLoader() 434 mirror::ClassLoader* system_class_loader_; 435 436 // A non-zero value indicates that a thread has been created but not yet initialized. Guarded by 437 // the shutdown lock so that threads aren't born while we're shutting down. 438 size_t threads_being_born_ GUARDED_BY(Locks::runtime_shutdown_lock_); 439 440 // Waited upon until no threads are being born. 441 UniquePtr<ConditionVariable> shutdown_cond_ GUARDED_BY(Locks::runtime_shutdown_lock_); 442 443 // Set when runtime shutdown is past the point that new threads may attach. 444 bool shutting_down_ GUARDED_BY(Locks::runtime_shutdown_lock_); 445 446 // The runtime is starting to shutdown but is blocked waiting on shutdown_cond_. 447 bool shutting_down_started_ GUARDED_BY(Locks::runtime_shutdown_lock_); 448 449 bool started_; 450 451 // New flag added which tells us if the runtime has finished starting. If 452 // this flag is set then the Daemon threads are created and the class loader 453 // is created. This flag is needed for knowing if its safe to request CMS. 454 bool finished_starting_; 455 456 // Hooks supported by JNI_CreateJavaVM 457 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); 458 void (*exit_)(jint status); 459 void (*abort_)(); 460 461 bool stats_enabled_; 462 RuntimeStats stats_; 463 464 bool method_trace_; 465 std::string method_trace_file_; 466 size_t method_trace_file_size_; 467 instrumentation::Instrumentation instrumentation_; 468 469 typedef SafeMap<jobject, std::vector<const DexFile*>, JobjectComparator> CompileTimeClassPaths; 470 CompileTimeClassPaths compile_time_class_paths_; 471 bool use_compile_time_class_path_; 472 473 jobject main_thread_group_; 474 jobject system_thread_group_; 475 476 DISALLOW_COPY_AND_ASSIGN(Runtime); 477}; 478 479} // namespace art 480 481#endif // ART_RUNTIME_RUNTIME_H_ 482