runtime.h revision e810452722ac83b294d1f7aa80bdd88e547d5af0
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 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 DCHECK(intern_table_ != NULL); 283 return intern_table_; 284 } 285 286 JavaVMExt* GetJavaVM() const { 287 return java_vm_; 288 } 289 290 MonitorList* GetMonitorList() const { 291 return monitor_list_; 292 } 293 294 mirror::Throwable* GetPreAllocatedOutOfMemoryError() const 295 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 296 297 const std::vector<std::string>& GetProperties() const { 298 return properties_; 299 } 300 301 ThreadList* GetThreadList() const { 302 return thread_list_; 303 } 304 305 const char* GetVersion() const { 306 return "2.0.0"; 307 } 308 309 void DisallowNewSystemWeaks() EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_); 310 void AllowNewSystemWeaks() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 311 312 // Visit all the roots. If only_dirty is true then non-dirty roots won't be visited. If 313 // clean_dirty is true then dirty roots will be marked as non-dirty after visiting. 314 void VisitRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty) 315 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 316 317 // Visit all of the roots we can do safely do concurrently. 318 void VisitConcurrentRoots(RootVisitor* visitor, void* arg, bool only_dirty, bool clean_dirty); 319 320 // Visit all of the non thread roots, we can do this with mutators unpaused. 321 void VisitNonThreadRoots(RootVisitor* visitor, void* arg); 322 323 // Visit all other roots which must be done with mutators suspended. 324 void VisitNonConcurrentRoots(RootVisitor* visitor, void* arg) 325 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 326 327 // Returns a special method that calls into a trampoline for runtime method resolution 328 mirror::ArtMethod* GetResolutionMethod() const { 329 CHECK(HasResolutionMethod()); 330 return resolution_method_; 331 } 332 333 bool HasResolutionMethod() const { 334 return resolution_method_ != NULL; 335 } 336 337 void SetResolutionMethod(mirror::ArtMethod* method) { 338 resolution_method_ = method; 339 } 340 341 mirror::ArtMethod* CreateResolutionMethod() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 342 343 // Returns a special method that describes all callee saves being spilled to the stack. 344 enum CalleeSaveType { 345 kSaveAll, 346 kRefsOnly, 347 kRefsAndArgs, 348 kLastCalleeSaveType // Value used for iteration 349 }; 350 351 bool HasCalleeSaveMethod(CalleeSaveType type) const { 352 return callee_save_methods_[type] != NULL; 353 } 354 355 mirror::ArtMethod* GetCalleeSaveMethod(CalleeSaveType type) const { 356 DCHECK(HasCalleeSaveMethod(type)); 357 return callee_save_methods_[type]; 358 } 359 360 void SetCalleeSaveMethod(mirror::ArtMethod* method, CalleeSaveType type); 361 362 mirror::ArtMethod* CreateCalleeSaveMethod(InstructionSet instruction_set, 363 CalleeSaveType type) 364 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 365 366 mirror::ArtMethod* CreateRefOnlyCalleeSaveMethod(InstructionSet instruction_set) 367 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 368 369 mirror::ArtMethod* CreateRefAndArgsCalleeSaveMethod(InstructionSet instruction_set) 370 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 371 372 int32_t GetStat(int kind); 373 374 RuntimeStats* GetStats() { 375 return &stats_; 376 } 377 378 bool HasStatsEnabled() const { 379 return stats_enabled_; 380 } 381 382 void ResetStats(int kinds); 383 384 void SetStatsEnabled(bool new_state); 385 386 bool PreZygoteFork(); 387 bool InitZygote(); 388 void DidForkFromZygote(); 389 390 instrumentation::Instrumentation* GetInstrumentation() { 391 return &instrumentation_; 392 } 393 394 bool UseCompileTimeClassPath() const { 395 return use_compile_time_class_path_; 396 } 397 398 const std::vector<const DexFile*>& GetCompileTimeClassPath(jobject class_loader); 399 void SetCompileTimeClassPath(jobject class_loader, std::vector<const DexFile*>& class_path); 400 401 private: 402 static void InitPlatformSignalHandlers(); 403 404 Runtime(); 405 406 void BlockSignals(); 407 408 bool Init(const Options& options, bool ignore_unrecognized) 409 SHARED_TRYLOCK_FUNCTION(true, Locks::mutator_lock_); 410 void InitNativeMethods() LOCKS_EXCLUDED(Locks::mutator_lock_); 411 void InitThreadGroups(Thread* self); 412 void RegisterRuntimeNativeMethods(JNIEnv* env); 413 414 void StartDaemonThreads(); 415 void StartSignalCatcher(); 416 417 // A pointer to the active runtime or NULL. 418 static Runtime* instance_; 419 420 bool is_compiler_; 421 bool is_zygote_; 422 bool is_concurrent_gc_enabled_; 423 bool is_explicit_gc_disabled_; 424 425 CompilerFilter compiler_filter_; 426 size_t huge_method_threshold_; 427 size_t large_method_threshold_; 428 size_t small_method_threshold_; 429 size_t tiny_method_threshold_; 430 size_t num_dex_methods_threshold_; 431 432 bool sea_ir_mode_; 433 434 // The host prefix is used during cross compilation. It is removed 435 // from the start of host paths such as: 436 // $ANDROID_PRODUCT_OUT/system/framework/boot.oat 437 // to produce target paths such as 438 // /system/framework/boot.oat 439 // Similarly it is prepended to target paths to arrive back at a 440 // host past. In both cases this is necessary because image and oat 441 // files embedded expect paths of dependent files (an image points 442 // to an oat file and an oat files to one or more dex files). These 443 // files contain the expected target path. 444 std::string host_prefix_; 445 446 std::string boot_class_path_string_; 447 std::string class_path_string_; 448 std::vector<std::string> properties_; 449 450 // The default stack size for managed threads created by the runtime. 451 size_t default_stack_size_; 452 453 gc::Heap* heap_; 454 455 MonitorList* monitor_list_; 456 457 ThreadList* thread_list_; 458 459 InternTable* intern_table_; 460 461 ClassLinker* class_linker_; 462 463 SignalCatcher* signal_catcher_; 464 std::string stack_trace_file_; 465 466 JavaVMExt* java_vm_; 467 468 mirror::Throwable* pre_allocated_OutOfMemoryError_; 469 470 mirror::ArtMethod* callee_save_methods_[kLastCalleeSaveType]; 471 472 mirror::ArtMethod* resolution_method_; 473 474 // A non-zero value indicates that a thread has been created but not yet initialized. Guarded by 475 // the shutdown lock so that threads aren't born while we're shutting down. 476 size_t threads_being_born_ GUARDED_BY(Locks::runtime_shutdown_lock_); 477 478 // Waited upon until no threads are being born. 479 UniquePtr<ConditionVariable> shutdown_cond_ GUARDED_BY(Locks::runtime_shutdown_lock_); 480 481 // Set when runtime shutdown is past the point that new threads may attach. 482 bool shutting_down_ GUARDED_BY(Locks::runtime_shutdown_lock_); 483 484 // The runtime is starting to shutdown but is blocked waiting on shutdown_cond_. 485 bool shutting_down_started_ GUARDED_BY(Locks::runtime_shutdown_lock_); 486 487 bool started_; 488 489 // New flag added which tells us if the runtime has finished starting. If 490 // this flag is set then the Daemon threads are created and the class loader 491 // is created. This flag is needed for knowing if its safe to request CMS. 492 bool finished_starting_; 493 494 // Hooks supported by JNI_CreateJavaVM 495 jint (*vfprintf_)(FILE* stream, const char* format, va_list ap); 496 void (*exit_)(jint status); 497 void (*abort_)(); 498 499 bool stats_enabled_; 500 RuntimeStats stats_; 501 502 bool method_trace_; 503 std::string method_trace_file_; 504 size_t method_trace_file_size_; 505 instrumentation::Instrumentation instrumentation_; 506 507 typedef SafeMap<jobject, std::vector<const DexFile*>, JobjectComparator> CompileTimeClassPaths; 508 CompileTimeClassPaths compile_time_class_paths_; 509 bool use_compile_time_class_path_; 510 511 jobject main_thread_group_; 512 jobject system_thread_group_; 513 514 // As returned by ClassLoader.getSystemClassLoader(). 515 jobject system_class_loader_; 516 517 DISALLOW_COPY_AND_ASSIGN(Runtime); 518}; 519 520} // namespace art 521 522#endif // ART_RUNTIME_RUNTIME_H_ 523