Art系列<一>

Art虚拟机是android 性能加速的保证。有必要对其源码进行深入探讨。废话不多说,先上代码。
代码位置:/art/runtime/runtime.cc。还是从运行时构造方法开始说起。

Runtime::Runtime()
    : resolution_method_(nullptr),
      imt_conflict_method_(nullptr),
      imt_unimplemented_method_(nullptr),
      instruction_set_(kNone),
      compiler_callbacks_(nullptr),
      is_zygote_(false),
      must_relocate_(false),
      is_concurrent_gc_enabled_(true),
      is_explicit_gc_disabled_(false),
      dex2oat_enabled_(true),
      image_dex2oat_enabled_(true),
      default_stack_size_(0),
      heap_(nullptr),
      max_spins_before_thin_lock_inflation_(Monitor::kDefaultMaxSpinsBeforeThinLockInflation),
      monitor_list_(nullptr),
      monitor_pool_(nullptr),
      thread_list_(nullptr),
      intern_table_(nullptr),
      class_linker_(nullptr),
      signal_catcher_(nullptr),
      use_tombstoned_traces_(false),
      java_vm_(nullptr),
      fault_message_lock_("Fault message lock"),
      fault_message_(""),
      threads_being_born_(0),
      shutdown_cond_(new ConditionVariable("Runtime shutdown", *Locks::runtime_shutdown_lock_)),
      shutting_down_(false),
      shutting_down_started_(false),
      started_(false),
      finished_starting_(false),
      vfprintf_(nullptr),
      exit_(nullptr),
      abort_(nullptr),
      stats_enabled_(false),
      is_running_on_memory_tool_(RUNNING_ON_MEMORY_TOOL),
      instrumentation_(),
      main_thread_group_(nullptr),
      system_thread_group_(nullptr),
      system_class_loader_(nullptr),
      dump_gc_performance_on_shutdown_(false),
      preinitialization_transaction_(nullptr),
      verify_(verifier::VerifyMode::kNone),
      allow_dex_file_fallback_(true),
      target_sdk_version_(0),
      implicit_null_checks_(false),
      implicit_so_checks_(false),
      implicit_suspend_checks_(false),
      no_sig_chain_(false),
      force_native_bridge_(false),
      is_native_bridge_loaded_(false),
      is_native_debuggable_(false),
      is_java_debuggable_(false),
      zygote_max_failed_boots_(0),
      experimental_flags_(ExperimentalFlags::kNone),
      oat_file_manager_(nullptr),
      is_low_memory_mode_(false),
      safe_mode_(false),
      dump_native_stack_on_sig_quit_(true),
      pruned_dalvik_cache_(false),
      // Initially assume we perceive jank in case the process state is never updated.
      process_state_(kProcessStateJankPerceptible),
      zygote_no_threads_(false) {
  static_assert(Runtime::kCalleeSaveSize ==
                    static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType), "Unexpected size");

  CheckAsmSupportOffsetsAndSizes();
  std::fill(callee_save_methods_, callee_save_methods_ + arraysize(callee_save_methods_), 0u);
  interpreter::CheckInterpreterAsmConstants();
  callbacks_.reset(new RuntimeCallbacks());
  for (size_t i = 0; i <= static_cast<size_t>(DeoptimizationKind::kLast); ++i) {
    deoptimization_counts_[i] = 0u;
  }
}

先看下初始化了哪些变量。
resolution_method_:ArtMethod类型指针。代表了一个Art虚拟机方法。
imt_conflict_method_:ArtMethod类型指针。代表了一个Art虚拟机方法。
imt_unimplemented_method_:ArtMethod类型指针。代表了一个Art虚拟机方法。
instruction_set_:代表一个枚举类型,就是代表具体哪个架构。如下所述。包含None内有8种架构。

enum InstructionSet {
  kNone,
  kArm,
  kArm64,
  kThumb2,
  kX86,
  kX86_64,
  kMips,
  kMips64
};

compiler_callbacks_:方法指针。先简单看CompilerCallbacks的方法定义。

class CompilerCallbacks {
 public:
  enum class CallbackMode {  // private
  //编译返回模式。是BootImage还是AppImage。
    kCompileBootImage,
    kCompileApp
  };

  virtual ~CompilerCallbacks() { }

  virtual void MethodVerified(verifier::MethodVerifier* verifier)
      REQUIRES_SHARED(Locks::mutator_lock_) = 0;
  virtual void ClassRejected(ClassReference ref) = 0;

  // Return true if we should attempt to relocate to a random base address if we have not already
  // done so. Return false if relocating in this way would be problematic.
  virtual bool IsRelocationPossible() = 0;

  virtual verifier::VerifierDeps* GetVerifierDeps() const = 0;
  virtual void SetVerifierDeps(verifier::VerifierDeps* deps ATTRIBUTE_UNUSED) {}

  virtual bool CanAssumeVerified(ClassReference ref ATTRIBUTE_UNUSED) {
    return false;
  }

  virtual void SetDoesClassUnloading(bool does_class_unloading ATTRIBUTE_UNUSED,
                                     CompilerDriver* compiler_driver ATTRIBUTE_UNUSED) {}

  bool IsBootImage() {
    return mode_ == CallbackMode::kCompileBootImage;
  }

 protected:
  explicit CompilerCallbacks(CallbackMode mode) : mode_(mode) { }

 private:
  // Whether the compiler is creating a boot image.
  const CallbackMode mode_;
};

} 

is_zygote_:是否初始化。
must_relocate_:bool 是否重新定位。
is_concurrent_gc_enabled_:是否允许运行时同时gc
is_explicit_gc_disabled_:是否禁止明确gc
dex2oat_enabled_:是否允许dex转oat。默认是开启
image_dex2oat_enabled_:镜像文件是否开启dex转oat。
default_stack_size_:默认栈大小为0
heap_:指向Heap堆的指针。默认为nullptr。Heap结构如下。
这个Heap非常重要,先看下他的一些变量信息。

class Heap {
 public:
  // If true, measure the total allocation time.
  //默认初始的大小
  static constexpr size_t kDefaultStartingSize = kPageSize;
  //堆初始化为2MB
  static constexpr size_t kDefaultInitialSize = 2 * MB;
  //堆最大为256MB
  static constexpr size_t kDefaultMaximumSize = 256 * MB;
  //堆中不能移动的区域大小为64MB
  static constexpr size_t kDefaultNonMovingSpaceCapacity = 64 * MB;
  //最大空闲区域大小
  static constexpr size_t kDefaultMaxFree = 2 * MB;
  //最小空闲区域
  static constexpr size_t kDefaultMinFree = kDefaultMaxFree / 4;
  //
  static constexpr size_t kDefaultLongPauseLogThreshold = MsToNs(5);
  //默认GC阈值
  static constexpr size_t kDefaultLongGCLogThreshold = MsToNs(100);
  //默认LAB的大小。
  static constexpr size_t kDefaultTLABSize = 32 * KB;
  static constexpr double kDefaultTargetUtilization = 0.5;
  //堆大小增长乘数
  static constexpr double kDefaultHeapGrowthMultiplier = 2.0;
  // Primitive arrays larger than this size are put in the large object space.
  static constexpr size_t kMinLargeObjectThreshold = 3 * kPageSize;
  static constexpr size_t kDefaultLargeObjectThreshold = kMinLargeObjectThreshold;
  // Whether or not parallel GC is enabled. If not, then we never create the thread pool.
  //默认是否能够同时进行GC
  static constexpr bool kDefaultEnableParallelGC = false;

  // Whether or not we use the free list large object space. Only use it if USE_ART_LOW_4G_ALLOCATOR
  // since this means that we have to use the slow msync loop in MemMap::MapAnonymous.
  static constexpr space::LargeObjectSpaceType kDefaultLargeObjectSpaceType =
      USE_ART_LOW_4G_ALLOCATOR ?
          space::LargeObjectSpaceType::kFreeList
        : space::LargeObjectSpaceType::kMap;

  // Used so that we don't overflow the allocation time atomic integer.
  static constexpr size_t kTimeAdjust = 1024;

  // How often we allow heap trimming to happen (nanoseconds).
  static constexpr uint64_t kHeapTrimWait = MsToNs(5000);
  // How long we wait after a transition request to perform a collector transition (nanoseconds).
  static constexpr uint64_t kCollectorTransitionWait = MsToNs(5000);

monitor_list_:MonitorList指针
monitor_pool_:MonitorPool指针
intern_table_:InternTable 内部存储着String的常量池,主要是为了节省内存。
class_linker_:ClassLinker
signal_catcher_:SignalCatcher 一个捕获信号量的线程。
use_tombstoned_traces_:是否跟踪记录进程信息
java_vm_:JavaVMExt 指针。JavaVM虚拟机环境指针。
threads_being_born_:非零值表示线程已创建但尚未初始化
instrumentation_:Instrumentation 检测类。如下可以看下它的一些结构。

class Instrumentation {
 public:
  enum InstrumentationEvent {
    kMethodEntered = 0x1,
    kMethodExited = 0x2,
    kMethodUnwind = 0x4,
    kDexPcMoved = 0x8,
    kFieldRead = 0x10,
    kFieldWritten = 0x20,
    kExceptionCaught = 0x40,
    kBranch = 0x80,
    kInvokeVirtualOrInterface = 0x100,
  };

  enum class InstrumentationLevel {
    kInstrumentNothing,                   // execute without instrumentation
    kInstrumentWithInstrumentationStubs,  // execute with instrumentation entry/exit stubs
    kInstrumentWithInterpreter            // execute with interpreter
  };

  Instrumentation();

system_class_loader_:jobect As returned by ClassLoader.getSystemClassLoader()
verify_:VerifyMode 检验模式
oat_file_manager_:OatFileManager Oat file manager, keeps track of what oat files are open
变量到这里就基本结束了,有好多变量暂时也不能去串起来,后续一文分析art的init方法。整个变量和方法的含义会进一步清晰。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值