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