当前位置:   article > 正文

gem5学习(17):ARM功耗建模——ARM Power Modelling_gem5 功耗

gem5 功耗

目录

一、Dynamic Power States

二、Power Usage Types

三、MathExprPowerModels

四、Extending an existing simulation

五、Stat dump frequency

六、Common Problems


官网教程:gem5: ARM Power Modelling

通过使用gem5中已记录的各种统计数据,可以在gem5模拟中对能量和功率使用(energy and power usage)进行建模和监控。这是通过使用MathExprPowerModel实现的,它是一种通过数学方程来建模功率使用的方法。本教程详细介绍了功耗建模所需的各个组件,并解释了如何将它们添加到现有的ARM模拟中。

本章借鉴了位于configs/example/arm目录中的fs_power.py配置脚本,并提供了扩展此脚本或其他脚本的说明。

请注意,只有在使用更详细的“timing” CPU时才能应用功耗模型。

关于功耗建模如何集成到gem5中以及它们与模拟器的其他部分如何交互的概述,可以在Sascha Bischoff在2017年ARM Research Summit上的演示中找到。

完整的fs_power.py配置脚本:

  1. import argparse
  2. import os
  3. import m5
  4. from m5.objects import MathExprPowerModel, PowerModel
  5. import fs_bigLITTLE as bL
  6. class CpuPowerOn(MathExprPowerModel):
  7. def __init__(self, cpu_path, **kwargs):
  8. super(CpuPowerOn, self).__init__(**kwargs)
  9. # 2A per IPC, 3pA per cache miss
  10. # and then convert to Watt
  11. self.dyn = (
  12. "voltage * (2 * {}.ipc + 3 * 0.000000001 * "
  13. "{}.dcache.overallMisses / simSeconds)".format(cpu_path, cpu_path)
  14. )
  15. self.st = "4 * temp"
  16. class CpuPowerOff(MathExprPowerModel):
  17. dyn = "0"
  18. st = "0"
  19. class CpuPowerModel(PowerModel):
  20. def __init__(self, cpu_path, **kwargs):
  21. super(CpuPowerModel, self).__init__(**kwargs)
  22. self.pm = [
  23. CpuPowerOn(cpu_path), # ON
  24. CpuPowerOff(), # CLK_GATED
  25. CpuPowerOff(), # SRAM_RETENTION
  26. CpuPowerOff(), # OFF
  27. ]
  28. class L2PowerOn(MathExprPowerModel):
  29. def __init__(self, l2_path, **kwargs):
  30. super(L2PowerOn, self).__init__(**kwargs)
  31. # Example to report l2 Cache overallAccesses
  32. # The estimated power is converted to Watt and will vary based
  33. # on the size of the cache
  34. self.dyn = f"{l2_path}.overallAccesses * 0.000018000"
  35. self.st = "(voltage * 3)/10"
  36. class L2PowerOff(MathExprPowerModel):
  37. dyn = "0"
  38. st = "0"
  39. class L2PowerModel(PowerModel):
  40. def __init__(self, l2_path, **kwargs):
  41. super(L2PowerModel, self).__init__(**kwargs)
  42. # Choose a power model for every power state
  43. self.pm = [
  44. L2PowerOn(l2_path), # ON
  45. L2PowerOff(), # CLK_GATED
  46. L2PowerOff(), # SRAM_RETENTION
  47. L2PowerOff(), # OFF
  48. ]
  49. def main():
  50. parser = argparse.ArgumentParser(
  51. description="Generic ARM big.LITTLE configuration with "
  52. "example power models"
  53. )
  54. bL.addOptions(parser)
  55. options = parser.parse_args()
  56. if options.cpu_type != "timing":
  57. m5.fatal("The power example script requires 'timing' CPUs.")
  58. root = bL.build(options)
  59. # Wire up some example power models to the CPUs
  60. for cpu in root.system.descendants():
  61. if not isinstance(cpu, m5.objects.BaseCPU):
  62. continue
  63. cpu.power_state.default_state = "ON"
  64. cpu.power_model = CpuPowerModel(cpu.path())
  65. # Example power model for the L2 Cache of the bigCluster
  66. for l2 in root.system.bigCluster.l2.descendants():
  67. if not isinstance(l2, m5.objects.Cache):
  68. continue
  69. l2.power_state.default_state = "ON"
  70. l2.power_model = L2PowerModel(l2.path())
  71. bL.instantiate(options)
  72. print("*" * 70)
  73. print(
  74. "WARNING: The power numbers generated by this script are "
  75. "examples. They are not representative of any particular "
  76. "implementation or process."
  77. )
  78. print("*" * 70)
  79. # Dumping stats periodically
  80. m5.stats.periodicStatDump(m5.ticks.fromSeconds(0.1e-3))
  81. bL.run()
  82. if __name__ == "__m5_main__":
  83. main()
  • 实现了一个使用gem5模拟器的ARM big.LITTLE配置,并使用了示例的功耗模型。它定义了几个功耗模型类,包括CpuPowerOn、CpuPowerOff、CpuPowerModel、L2PowerOn、L2PowerOff和L2PowerModel。这些类通过继承MathExprPowerModel和PowerModel来定义不同的功耗模型。
  • 主函数main()中首先解析命令行参数,并构建gem5模拟器的系统。然后,将示例的功耗模型与CPU和L2 Cache相关联。对于每个CPU,设置默认的功耗状态为“ON”,并将CpuPowerModel功耗模型与其关联。对于bigCluster的L2 Cache,设置默认的功耗状态为“ON”,并将L2PowerModel功耗模型与其关联。

一、Dynamic Power States

功耗模型由两个函数组成,用于描述如何计算不同功耗状态下的功耗消耗。这些功耗状态包括以下几种(来自src/sim/PowerState.py):

  • UNDEFINED:无效状态,没有可用的功耗状态相关信息。这是默认状态。
  • ON:逻辑块正在主动运行,并根据所需的处理量消耗动态能量和漏电能量。
  • CLK_GATED:块内的时钟电路被关闭以节省动态能量,但块的电源仍然打开,并且块正在消耗漏电能量。
  • SRAM_RETENTION:逻辑块内的SRAM被拉入保持状态,进一步减少漏电能量。
  • OFF:逻辑块被断电,不消耗任何能量。

每个状态(除了UNDEFINED)都分配了一个功耗模型,使用PowerModel类的pm字段。它是一个包含4个功耗模型的列表,分别对应以下顺序的状态:

  • ON
  • CLK_GATED
  • SRAM_RETENTION
  • OFF

需要注意的是,虽然有4个不同的条目,但这些条目不一定是不同的功耗模型。提供的fs_power.py文件在ON状态使用一个功耗模型,然后在其余状态使用相同的功耗模型。

二、Power Usage Types

gem5模拟器对功耗使用模拟了两种类型:

  • 静态功耗(static):模拟系统在任何活动情况下使用的功耗。
  • 动态功耗(dynamic):由于各种活动而导致系统使用的功耗。

一个功耗模型必须包含用于模拟这两种功耗的方程(这个方程可以非常简单,比如,如果不需要或者与该功耗模型无关的静态功耗,可以将其设为st = "0")。

三、MathExprPowerModels

在fs_power.py中提供的功耗模型是继承自MathExprPowerModel类的。MathExprPowerModel是指定为包含用于计算系统功耗的数学表达式的字符串。它们通常包含了一些统计数据和自动变量,比如温度,例如:

  1. class CpuPowerOn(MathExprPowerModel):
  2. def __init__(self, cpu_path, **kwargs):
  3. super(CpuPowerOn, self).__init__(**kwargs)
  4. # 2A per IPC, 3pA per cache miss
  5. # and then convert to Watt
  6. self.dyn = "voltage * (2 * {}.ipc + 3 * 0.000000001 * " \
  7. "{}.dcache.overall_misses / sim_seconds)".format(cpu_path,
  8. cpu_path)
  9. self.st = "4 * temp"

上述的功耗模型来源于提供的fs_power.py文件。

可以看到自动变量(电压和温度【voltage and temp】)不需要路径,而组件特定的统计信息(CPU的每周期指令ipc)需要路径。在文件的主函数中,可以看到CPU对象有一个path()函数,返回组件在系统中的“路径”,例如system.bigCluster.cpus0。path函数由SimObject提供,因此可以被系统中任何扩展了SimObject的对象使用,例如稍后几行中的L2缓存对象也使用了它。

(注意将dcache.overall_misses除以sim_seconds以转换为瓦特。这是一个功耗模型,即能量随时间的变化。在使用这些术语时要小心,因为它们通常可以互换使用,但在功耗和能量模拟/建模方面,它们具有非常具体的含义。)

四、Extending an existing simulation

提供的fs_power.py脚本通过导入现有的fs_bigLITTLE.py脚本并修改其值来进行扩展。作为其中的一部分,使用了几个循环来迭代SimObjects的后代,以应用功耗模型。因此,为了扩展现有的仿真以支持功耗模型,可以定义一个辅助函数来帮助完成这个任务。

  1. def _apply_pm(simobj, power_model, so_class=None):
  2. for desc in simobj.descendants():
  3. if so_class is not None and not isinstance(desc, so_class):
  4. continue
  5. desc.power_state.default_state = "ON"
  6. desc.power_model = power_model(desc.path())

上述函数接受一个SimObject、一个Power Model和一个可选的类,SimObject的子孙必须实例化该类才能应用PM。如果没有指定类,则PM将应用于所有子孙。

无论是否决定使用辅助函数,现在需要定义一些Power Models。可以按照fs_power.py文件中的模式进行操作:

  1. 为感兴趣的每个功耗状态定义一个类。这些类应该扩展MathExprPowerModel,并包含dynst字段(分别表示动态和静态功耗)。每个字段应包含一个字符串,描述在该状态下如何计算各自类型的功耗。它们的构造函数应接受一个路径,用于通过格式在描述功耗计算方程的字符串中使用,并接受一些kwargs参数,以传递给超级构造函数。
  2. 定义一个类来保存在上一步中定义的所有Power Models。这个类应该扩展PowerModel,并包含一个名为pm的单一字段,其中包含一个包含4个元素的列表:pm[0]应该是“ON”功耗状态的Power Model的实例;pm[1]应该是“CLK_GATED”功耗状态的Power Model的实例;等等。这个类的构造函数应接受要传递给各个Power Models的路径,以及一些kwargs参数,这些参数将传递给超级构造函数。
  3. 有了辅助函数和上述类的定义,您可以扩展build函数,并在addOptions函数中添加一个命令行标志(如果希望能够切换使用这些模型)。

示例实现:

  1. class CpuPowerOn(MathExprPowerModel):
  2. def __init__(self, cpu_path, **kwargs):
  3. super(CpuPowerOn, self).__init__(**kwargs)
  4. self.dyn = "voltage * 2 * {}.ipc".format(cpu_path)
  5. self.st = "4 * temp"
  6. class CpuPowerClkGated(MathExprPowerModel):
  7. def __init__(self, cpu_path, **kwargs):
  8. super(CpuPowerOn, self).__init__(**kwargs)
  9. self.dyn = "voltage / sim_seconds"
  10. self.st = "4 * temp"
  11. class CpuPowerOff(MathExprPowerModel):
  12. dyn = "0"
  13. st = "0"
  14. class CpuPowerModel(PowerModel):
  15. def __init__(self, cpu_path, **kwargs):
  16. super(CpuPowerModel, self).__init__(**kwargs)
  17. self.pm = [
  18. CpuPowerOn(cpu_path), # ON
  19. CpuPowerClkGated(cpu_path), # CLK_GATED
  20. CpuPowerOff(), # SRAM_RETENTION
  21. CpuPowerOff(), # OFF
  22. ]
  23. [...]
  24. def addOptions(parser):
  25. [...]
  26. parser.add_argument("--power-models", action="store_true",
  27. help="Add power models to the simulated system. "
  28. "Requires using the 'timing' CPU."
  29. return parser
  30. def build(options):
  31. root = Root(full_system=True)
  32. [...]
  33. if options.power_models:
  34. if options.cpu_type != "timing":
  35. m5.fatal("The power models require the 'timing' CPUs.")
  36. _apply_pm(root.system.bigCluster.cpus, CpuPowerModel
  37. so_class=m5.objects.BaseCpu)
  38. _apply_pm(root.system.littleCluster.cpus, CpuPowerModel)
  39. return root
  40. [...]

五、Stat dump frequency

默认情况下,gem5每模拟一秒钟就将模拟统计信息转储到stats.txt文件中。可以通过m5.stats.periodicStatDump函数进行控制,该函数接受以模拟时钟周期为单位的统计信息转储频率,而不是以秒为单位。同时,m5.ticks提供了一个fromSeconds函数,以便于使用。

下面是一个示例,展示了统计信息转储频率如何影响结果的分辨率,取自Sascha Bischoff的演示幻灯片第16页:

统计信息转储的频率直接影响基于stats.txt文件生成的图表的分辨率。然而,它也会影响输出文件的大小。每模拟一秒转储统计信息与每模拟一毫秒转储统计信息相比,会增加文件大小几百倍。因此,有意控制统计信息转储频率是合理的。

使用提供的fs_power.py脚本,可以按以下方式进行设置:

  1. [...]
  2. def addOptions(parser):
  3. [...]
  4. parser.add_argument("--stat-freq", type=float, default=1.0,
  5. help="Frequency (in seconds) to dump stats to the "
  6. "'stats.txt' file. Supports scientific notation, "
  7. "e.g. '1.0E-3' for milliseconds.")
  8. return parser
  9. [...]
  10. def main():
  11. [...]
  12. m5.stats.periodicStatDump(m5.ticks.fromSeconds(options.stat_freq))
  13. bL.run()
  14. [...]

可以使用以下方式指定统计信息转储频率:

--stat-freq <val>

在调用模拟时进行设置。

六、Common Problems

使用提供的fs_power.py时,gem5崩溃,并显示以下错误消息:致命错误:统计信息''(160)未通过regStats()函数正确初始化
使用提供的fs_power.py时,gem5崩溃,并显示以下错误消息:致命错误:无法评估功耗表达式:[...]
这是因为gem5的统计框架最近进行了重构。获取最新版本的gem5源代码并重新构建应该可以解决这个问题。如果不希望这样做,可以使用以下两组补丁:

​​​​​​

  1. https://gem5-review.googlesource.com/c/public/gem5/+/26643
  1. https://gem5-review.googlesource.com/c/public/gem5/+/26785

可以通过按照各自链接中的下载说明来检出和应用这些补丁。

前阵子放假断更了一段时间(在家除了学习都是有意思的事情),现在逐步开始捡起学习状态。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/302338?site
推荐阅读
相关标签
  

闽ICP备14008679号