ART深入浅出2 -- 认识和了解Runtime Options

本文详细介绍了Android 7.1中RuntimeOptions解析为RuntimeArgumentMap的过程,涉及CmdlineParser、ArgumentBuilder及其相关类的功能和用法。内容包括MakeParse函数、CmdlineParser的工作原理、不同类型参数的解析,以及RuntimeArgumentMap的构建。通过分析源码,揭示了参数匹配、值提取和转换的具体步骤,帮助理解Android系统中命令行选项的处理机制。
摘要由CSDN通过智能技术生成

本文基于Android 7.1,不过因为从BSP拿到的版本略有区别,所以本文提到的源码未必与读者找到的源码完全一致。本文在提供源码片断时,将按照 <源码相对android工程的路径>:<行号> <类名> <函数名> 的方式,如果行号对不上,请参考类名和函数名来找到对应的源码。


RuntimeOptions到RuntimeArgumentMap

在JNI_CreateJavaVM函数中,JavaVMOption对象被转换为RuntimeOptions对象。
RuntimeOptions实际上只是一个vector定义:
art/runtime/parsed_options.h:38
typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
内部的parie对象,第一个参数保存的是放进去的字符串数据,第二个是附加参数。一般附加参数不用。

art/runtime/java_vm_ext.cc:947 JNI_CreateJavaVM
  for (int i = 0; i < args->nOptions; ++i) {
    JavaVMOption* option = &args->options[i];
    options.push_back(std::make_pair(std::string(option->optionString), option->extraInfo));
  }
当数据填充完成后,调用Runtime::Create方法,传递给Runtime,用于创建Runtime。
art/runtime/runtime.cc:486 Runtime::Create
bool Runtime::Create(const RuntimeOptions& raw_options, bool ignore_unrecognized) {
  RuntimeArgumentMap runtime_options;
  return ParseOptions(raw_options, ignore_unrecognized, &runtime_options) &&
      Create(std::move(runtime_options));
}
注意到return语句,先调用ParseOptions, 将RuntimeOptions转换为RuntimeArugmentMap。
本章要重点分析的就是这个ParseOptions函数。
首先看看RuntimeArugmentMap是什么结构。

Runtime::ParseOptions

该函数解析参数的入口。Runtime::ParseOptions调用 ParsedOptions:Parse函数,再调用 ParsedOptions::DoParse函数。
DoParse函数,主体有两个函数:MakeParse和RuntimeParser::Parse函数。

首先,看看MakeParse函数。

MakeParse函数

这个函数用了非常复杂的C++模板,生成一个解析数据结构。因为内容太多,我摘录了关键部分,进行解说。
请先看下面的代码
runtime/parsed_options.cc:61 ParsedOptions::MakeParser
std::unique_ptr<RuntimeParser> ParsedOptions::MakeParser(bool ignore_unrecognized) {
  using M = RuntimeArgumentMap;

  std::unique_ptr<RuntimeParser::Builder> parser_builder =
      std::unique_ptr<RuntimeParser::Builder>(new RuntimeParser::Builder());

  parser_builder->
       Define("-Xzygote")
          .IntoKey(M::Zygote)
      .Define("-help")
          .IntoKey(M::Help)
      .....
          .WithType<ParseStringList<':'>>()  // std::vector<std::string>, split by :
          .IntoKey(M::BootClassPathLocations)
      .Define({"-classpath _", "-cp _"})
          .WithType<std::string>()
          .IntoKey(M::ClassPath)
      .Define("-Ximage:_")
          .WithType<std::string>()
          .IntoKey(M::Image)
      ....
      .Define("-D_")
          .WithType<std::vector<std::string>>().AppendValues()
          .IntoKey(M::PropertiesList)
      ....
      .Define({"-XX:EnableHSpaceCompactForOOM", "-XX:DisableHSpaceCompactForOOM"})
          .WithValues({true, false})
          .IntoKey(M::EnableHSpaceCompactForOOM)
      ....
      .Define("-XX:HeapTargetUtilization=_")
          .WithType<double>().WithRange(0.1, 0.9)
      ....
      .Define("-Xint")
          .WithValue(true)
          .IntoKey(M::Interpret)
      ....
      .Define("-Xmx_")
          .WithType<MemoryKiB>()
          .IntoKey(M::MemoryMaximumSize)
      .....
      .Define("-XX:DumpNativeStackOnSigQuit:_")
          .WithType<bool>()
          .WithValueMap({
   {"false", false}, {"true", true}})
          .IntoKey(M::DumpNativeStackOnSigQuit)
      .....
      .Define("-XX:LargeObjectSpace=_")
          .WithType<gc::space::LargeObjectSpaceType>()
          .WithValueMap({
   {"disabled", gc::space::LargeObjectSpaceType::kDisabled},
                         {"freelist", gc::space::LargeObjectSpaceType::kFreeList},
                         {"map",      gc::space::LargeObjectSpaceType::kMap}})
          .IntoKey(M::La
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值