runtime.h revision ad2541a59c00c2c69e8973088891a2b5257c9780
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 ArtMethod; 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 enum CompilerFilter { 67 kInterpretOnly, // Compile nothing. 68 kSpace, // Maximize space savings. 69 kBalanced, // Try to get the best performance return on compilation investment. 70 kSpeed, // Maximize runtime performance. 71 kEverything // Force compilation (Note: excludes compilaton of class initializers). 72 }; 73 74 // Guide heuristics to determine whether to compile method if profile data not available. 75#if ART_SMALL_MODE 76 static const CompilerFilter kDefaultCompilerFilter = kInterpretOnly; 77#else 78 static const CompilerFilter kDefaultCompilerFilter = kSpeed; 79#endif 80 static const size_t kDefaultHugeMethodThreshold = 10000; 81 static const size_t kDefaultLargeMethodThreshold = 600; 82 static const size_t kDefaultSmallMethodThreshold = 60; 83 static const size_t kDefaultTinyMethodThreshold = 20; 84 static const size_t kDefaultNumDexMethodsThreshold = 900; 85 86 class ParsedOptions { 87 public: 88 // returns null if problem parsing and ignore_unrecognized is false 89 static ParsedOptions* Create(const Options& options, bool ignore_unrecognized); 90 91 const std::vector<const DexFile*>* boot_class_path_; 92 std::string boot_class_path_string_; 93 std::string class_path_string_; 94 std::string host_prefix_; 95 std::string image_; 96 bool check_jni_; 97 std::string jni_trace_; 98 bool is_compiler_; 99 bool is_zygote_; 100 bool interpreter_only_; 101 bool is_concurrent_gc_enabled_; 102 bool is_explicit_gc_disabled_; 103 size_t long_pause_log_threshold_; 104 size_t long_gc_log_threshold_; 105 bool ignore_max_footprint_; 106 size_t heap_initial_size_; 107 size_t heap_maximum_size_; 108 size_t heap_growth_limit_; 109 size_t heap_min_free_; 110 size_t heap_max_free_; 111 double heap_target_utilization_; 112 size_t parallel_gc_threads_; 113 size_t conc_gc_threads_; 114 size_t stack_size_; 115 size_t max_spins_before_thin_lock_inflation_; 116 bool low_memory_mode_; 117 size_t lock_profiling_threshold_; 118 std::string stack_trace_file_; 119 bool method_trace_; 120 std::string method_trace_file_; 121 size_t method_trace_file_size_; 122 bool (*hook_is_sensitive_thread_)(); 123 jint (*hook_vfprintf_)(FILE* stream, const char* format, va_list ap); 124 void (*hook_exit_)(jint status); 125 void (*hook_abort_)(); 126 std::vector<std::string> properties_; 127 CompilerFilter compiler_filter_; 128 size_t huge_method_threshold_; 129 size_t large_method_threshold_; 130 size_t small_method_threshold_; 131 size_t tiny_method_threshold_; 132 size_t num_dex_methods_threshold_; 133 bool sea_ir_mode_; 134 135 private: 136 ParsedOptions() {} 137 }; 138 139 // Creates and initializes a new runtime. 140 static bool Create(const Options& options, bool ignore_unrecognized) 141 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_); 142 143 bool IsCompiler() const { 144 return is_compiler_; 145 } 146 147 bool IsZygote() const { 148 return is_zygote_; 149 } 150 151 bool IsConcurrentGcEnabled() const { 152 return is_concurrent_gc_enabled_; 153 } 154 155 bool IsExplicitGcDisabled() const { 156 return is_explicit_gc_disabled_; 157 } 158 159#ifdef ART_SEA_IR_MODE 160 bool IsSeaIRMode() const { 161 return sea_ir_mode_; 162 } 163#endif 164 165 void SetSeaIRMode(bool sea_ir_mode) { 166 sea_ir_mode_ = sea_ir_mode; 167 } 168 169 CompilerFilter GetCompilerFilter() const { 170 return compiler_filter_; 171 } 172 173 void SetCompilerFilter(CompilerFilter compiler_filter) { 174 compiler_filter_ = compiler_filter; 175 } 176 177 size_t GetHugeMethodThreshold() const { 178 return huge_method_threshold_; 179 } 180 181 size_t GetLargeMethodThreshold() const { 182 return large_method_threshold_; 183 } 184 185 size_t GetSmallMethodThreshold() const { 186 return small_method_threshold_; 187 } 188 189 size_t GetTinyMethodThreshold() const { 190 return tiny_method_threshold_; 191 } 192 193 size_t GetNumDexMethodsThreshold() const { 194 return num_dex_methods_threshold_; 195 } 196 197 const std::string& GetHostPrefix() const { 198 DCHECK(!IsStarted()); 199 return host_prefix_; 200 } 201 202 // Starts a runtime, which may cause threads to be started and code to run. 203 bool Start() UNLOCK_FUNCTION(Locks::mutator_lock_); 204 205 bool IsShuttingDown() const EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 206 return shutting_down_; 207 } 208 209 size_t NumberOfThreadsBeingBorn() const EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 210 return threads_being_born_; 211 } 212 213 void StartThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_) { 214 threads_being_born_++; 215 } 216 217 void EndThreadBirth() EXCLUSIVE_LOCKS_REQUIRED(Locks::runtime_shutdown_lock_); 218 219 bool IsStarted() const { 220 return started_; 221 } 222 223 bool IsFinishedStarting() const { 224 return finished_starting_; 225 } 226 227 static Runtime* Current() { 228 return instance_; 229 } 230 231 // Aborts semi-cleanly. Used in the implementation of LOG(FATAL), which most 232 // callers should prefer. 233 // This isn't marked ((noreturn)) because then gcc will merge multiple calls 234 // in a single function together. This reduces code size slightly, but means 235 // that the native stack trace we get may point at the wrong call site. 236 static void Abort() LOCKS_EXCLUDED(Locks::abort_lock_); 237 238 // Returns the "main" ThreadGroup, used when attaching user threads. 239 jobject GetMainThreadGroup() const; 240 241 // Returns the "system" ThreadGroup, used when attaching our internal threads. 242 jobject GetSystemThreadGroup() const; 243 244 // Returns the system ClassLoader which represents the CLASSPATH. 245 jobject GetSystemClassLoader() const; 246 247 // Attaches the calling native thread to the runtime. 248 bool AttachCurrentThread(const char* thread_name, bool as_daemon, jobject thread_group, 249 bool create_peer); 250 251 void CallExitHook(jint status); 252 253 // Detaches the current native thread from the runtime. 254 void DetachCurrentThread() LOCKS_EXCLUDED(Locks::mutator_lock_); 255 256 void DumpForSigQuit(std::ostream& os) 257 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 258 void DumpLockHolders(std::ostream& os); 259 260 ~Runtime(); 261 262 const std::string& GetBootClassPathString() const { 263 return boot_class_path_string_; 264 } 265 266 const std::string& GetClassPathString() const { 267 return class_path_string_; 268 } 269 270 ClassLinker* GetClassLinker() const { 271 return class_linker_; 272 } 273 274 size_t GetDefaultStackSize() const { 275 return default_stack_size_; 276 } 277 278 gc::Heap* GetHeap() const { 279 return heap_; 280 } 281 282 InternTable* GetInternTable() const { 283 DCHECK(intern_table_ != NULL); 284 return intern_table_; 285 } 286 287 JavaVMExt* GetJavaVM() const { 288 return java_vm_; 289 } 290 291 size_t GetMaxSpinsBeforeThinkLockInflation() const { 292 return max_spins_before_thin_lock_inflation_; 293 } 294 295 MonitorList* GetMonitorList() const { 296 return monitor_list_; 297 } 298 299 mirror::Throwable* GetPreAllocatedOutOfMemoryError() const 300 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 301 302 const std::vector<std::string>& GetProperties() const { 303 return properties_; 304 } 305 306 ThreadList* GetThreadList() const { 307 return thread_list_; 308 } 309 310 const char* GetVersion() const { 311 return "2.0.0"; 312 } 313 314 void DisallowNewSystemWeaks() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); 315 void AllowNewSystemWeaks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 316 317 // Visit all the roots. If only_dirty is true then non-dirty roots won't be visited. If 318 // clean_dirty is true then dirty roots will be marked as non-dirty after visiting. 319 void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty) 320 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 321 322 // Visit all of the roots we can do safely do concurrently. 323 void VisitConcurrentRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty); 324 325 // Visit all of the non thread roots, we can do this with mutators unpaused. 326 void VisitNonThreadRoots(RootVisitor* visitor, void* arg); 327 328 // Visit all other roots which must be done with mutators suspended. 329 void VisitNonConcurrentRoots(RootVisitor* visitor, void* arg) 330 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 331 332 // Sweep system weaks, the system weak is deleted if the visitor return nullptr. Otherwise, the 333 // system weak is updated to be the visitor's returned value. 334 void SweepSystemWeaks(RootVisitor* visitor, void* arg) 335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 336 337 // Returns a special method that calls into a trampoline for runtime method resolution 338 mirror::ArtMethod* GetResolutionMethod() const { 339 CHECK(HasResolutionMethod()); 340 return resolution_method_; 341 } 342 343 bool HasResolutionMethod() const { 344 return resolution_method_ != NULL; 345 } 346 347 void SetResolutionMethod(mirror::ArtMethod* method) { 348 resolution_method_ = method; 349 } 350 351 mirror::ArtMethod* CreateResolutionMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 352 353 // Returns a special method that describes all callee saves being spilled to the stack. 354 enum CalleeSaveType { 355 kSaveAll, 356 kRefsOnly, 357 kRefsAndArgs, 358 kLastCalleeSaveType // Value used for iteration 359 }; 360 361 bool HasCalleeSaveMethod(CalleeSaveType type) const { 362 return callee_save_methods_[type] != NULL; 363 } 364 365 mirror::ArtMethod* GetCalleeSaveMethod(CalleeSaveType type) const { 366 DCHECK(HasCalleeSaveMethod(type)); 367 return callee_save_methods_[type]; 368 } 369 370 void SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type); 371 372 mirror::ArtMethod* CreateCalleeSaveMethod(InstructionSet instruction_set, 373 CalleeSaveType type) 374 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 375 376 mirror::ArtMethod* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set) 377 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 378 379 mirror::ArtMethod* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set) 380 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 381 382 int32_t GetStat(int kind); 383 384 RuntimeStats* GetStats() { 385 return &stats_; 386 } 387 388 bool HasStatsEnabled() const { 389 return stats_enabled_; 390 } 391 392 void ResetStats(int kinds); 393 394 void SetStatsEnabled(bool new_state); 395 396 bool PreZygoteFork(); 397 bool InitZygote(); 398 void DidForkFromZygote(); 399 400 instrumentation::Instrumentation* GetInstrumentation() { 401 return &instrumentation_; 402 } 403 404 bool UseCompileTimeClassPath() const { 405 return use_compile_time_class_path_; 406 } 407 408 const std::vector<const DexFile*>& GetCompileTimeClassPath(jobject class_loader); 409 void SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path); 410 411 void InstrumentQuickAllocEntryPoints(); 412 void UninstrumentQuickAllocEntryPoints(); 413 414 private: 415 static void InitPlatformSignalHandlers(); 416 417 Runtime(); 418 419 void BlockSignals(); 420 421 bool Init(const Options& options, bool ignore_unrecognized) 422 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_); 423 void InitNativeMethods() LOCKS_EXCLUDED(Locks::mutator_lock_); 424 void InitThreadGroups(Thread* self); 425 void RegisterRuntimeNativeMethods(JNIEnv* env); 426 427 void StartDaemonThreads(); 428 void StartSignalCatcher(); 429 430 // A pointer to the active runtime or NULL. 431 static Runtime* instance_; 432 433 bool is_compiler_; 434 bool is_zygote_; 435 bool is_concurrent_gc_enabled_; 436 bool is_explicit_gc_disabled_; 437 438 CompilerFilter compiler_filter_; 439 size_t huge_method_threshold_; 440 size_t large_method_threshold_; 441 size_t small_method_threshold_; 442 size_t tiny_method_threshold_; 443 size_t num_dex_methods_threshold_; 444 445 bool sea_ir_mode_; 446 447 // The host prefix is used during cross compilation. It is removed 448 // from the start of host paths such as: 449 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat 450 // to produce target paths such as 451 // /system/framework/boot.oat 452 // Similarly it is prepended to target paths to arrive back at a 453 // host past. In both cases this is necessary because image and oat 454 // files embedded expect paths of dependent files (an image points 455 // to an oat file and an oat files to one or more dex files). These 456 // files contain the expected target path. 457 std::string host_prefix_; 458 459 std::string boot_class_path_string_; 460 std::string class_path_string_; 461 std::vector<std::string> properties_; 462 463 // The default stack size for managed threads created by the runtime. 464 size_t default_stack_size_; 465 466 gc::Heap* heap_; 467 468 // The number of spins that are done before thread suspension is used to forcibly inflate. 469 size_t max_spins_before_thin_lock_inflation_; 470 MonitorList* monitor_list_; 471 472 ThreadList* thread_list_; 473 474 InternTable* intern_table_; 475 476 ClassLinker* class_linker_; 477 478 SignalCatcher* signal_catcher_; 479 std::string stack_trace_file_; 480 481 JavaVMExt* java_vm_; 482 483 mirror::Throwable* pre_allocated_OutOfMemoryError_; 484 485 mirror::ArtMethod* callee_save_methods_[kLastCalleeSaveType]; 486 487 mirror::ArtMethod* resolution_method_; 488 489 // A non-zero value indicates that a thread has been created but not yet initialized. Guarded by 490 // the shutdown lock so that threads aren't born while we're shutting down. 491 size_t threads_being_born_ GUARDED_BY(Locks::runtime_shutdown_lock_); 492 493 // Waited upon until no threads are being born. 494 UniquePtr<ConditionVariable> shutdown_cond_ GUARDED_BY(Locks::runtime_shutdown_lock_); 495 496 // Set when runtime shutdown is past the point that new threads may attach. 497 bool shutting_down_ GUARDED_BY(Locks::runtime_shutdown_lock_); 498 499 // The runtime is starting to shutdown but is blocked waiting on shutdown_cond_. 500 bool shutting_down_started_ GUARDED_BY(Locks::runtime_shutdown_lock_); 501 502 bool started_; 503 504 // New flag added which tells us if the runtime has finished starting. If 505 // this flag is set then the Daemon threads are created and the class loader 506 // is created. This flag is needed for knowing if its safe to request CMS. 507 bool finished_starting_; 508 509 // Hooks supported by JNI_CreateJavaVM 510 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); 511 void (*exit_)(jint status); 512 void (*abort_)(); 513 514 bool stats_enabled_; 515 RuntimeStats stats_; 516 517 bool method_trace_; 518 std::string method_trace_file_; 519 size_t method_trace_file_size_; 520 instrumentation::Instrumentation instrumentation_; 521 522 typedef SafeMap<jobject, std::vector<const DexFile*>, JobjectComparator> CompileTimeClassPaths; 523 CompileTimeClassPaths compile_time_class_paths_; 524 bool use_compile_time_class_path_; 525 526 jobject main_thread_group_; 527 jobject system_thread_group_; 528 529 // As returned by ClassLoader.getSystemClassLoader(). 530 jobject system_class_loader_; 531 532 int quick_alloc_entry_points_instrumentation_counter_; 533 534 DISALLOW_COPY_AND_ASSIGN(Runtime); 535}; 536 537} // namespace art 538 539#endif // ART_RUNTIME_RUNTIME_H_ 540