base, python: Add a Temperature type and associated param

Add a class to represent a temperature. The class stores temperatures
in Kelvin and provides helper methods to convert to/from Celsius. The
corresponding param type automatically converts from Kelvin, Celsius,
and Fahrenheit to the underlying C++ type.

Change-Id: I5783cc4f4fecbea5aba9821dfc71bfa77c3f75a9
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/39218
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Andreas Sandberg
2021-01-19 10:09:56 +00:00
parent e0441fa7a7
commit 69f4aee33c
8 changed files with 478 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2012-2014, 2017-2019 ARM Limited
# Copyright (c) 2012-2014, 2017-2019, 2021 Arm Limited
# All rights reserved.
#
# The license below extends only to copyright in the software and shall
@@ -1693,6 +1693,43 @@ class Energy(Float):
value = convert.toEnergy(value)
super(Energy, self).__init__(value)
class Temperature(ParamValue):
cxx_type = 'Temperature'
cmd_line_settable = True
ex_str = "1C"
def __init__(self, value):
self.value = convert.toTemperature(value)
def __call__(self, value):
self.__init__(value)
return value
def getValue(self):
from _m5.core import Temperature
return Temperature.fromKelvin(self.value)
def config_value(self):
return self
@classmethod
def cxx_predecls(cls, code):
code('#include "base/temperature.hh"')
@classmethod
def cxx_ini_predecls(cls, code):
# Assume that base/str.hh will be included anyway
# code('#include "base/str.hh"')
pass
@classmethod
def cxx_ini_parse(self, code, src, dest, ret):
code('double _temp;')
code('bool _ret = to_number(%s, _temp);' % src)
code('if (_ret)')
code(' %s = Temperature(_temp);' % dest)
code('%s _ret;' % ret)
class NetworkBandwidth(float,ParamValue):
cxx_type = 'float'
ex_str = "1Gbps"
@@ -2231,6 +2268,7 @@ __all__ = ['Param', 'VectorParam',
'IpAddress', 'IpNetmask', 'IpWithPort',
'MemorySize', 'MemorySize32',
'Latency', 'Frequency', 'Clock', 'Voltage', 'Current', 'Energy',
'Temperature',
'NetworkBandwidth', 'MemoryBandwidth',
'AddrRange',
'MaxAddr', 'MaxTick', 'AllMemory',

View File

@@ -292,3 +292,25 @@ def toCurrent(value):
def toEnergy(value):
return toMetricFloat(value, 'energy', 'J')
def toTemperature(value):
"""Convert a string value specified to a temperature in Kelvin"""
magnitude, unit = toNum(value,
target_type='temperature',
units=('K', 'C', 'F'),
prefixes=metric_prefixes,
converter=float)
if unit == 'K':
kelvin = magnitude
elif unit == 'C':
kelvin = magnitude + 273.15
elif unit == 'F':
kelvin = (magnitude + 459.67) / 1.8
else:
raise ValueError(f"'{value}' needs a valid temperature unit.")
if kelvin < 0:
raise ValueError(f"{value} is an invalid temperature")
return kelvin