stdlib,tests: Add Str-to-CPUTypes helper functions

The two functions are `get_cpu_types_str_set()` which returns a set of
valid CPUTypes as strings, and `get_cpu_type_from_str()` which will
return a CPUType enum given an input string.

The purpose of these functions is to aid and standardize user input
parameters or environment variables.

Test scripts are updated accordingly.

Change-Id: I7cb9263321fe36bc8a7530edfd0d8e8bbd329e0e
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/58491
Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu>
Maintainer: Bobby Bruce <bbruce@ucdavis.edu>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Bobby R. Bruce
2022-04-01 18:37:11 -07:00
committed by Bobby Bruce
parent 4f4c8b5eda
commit 8f629fa638
6 changed files with 75 additions and 102 deletions

View File

@@ -25,11 +25,43 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from enum import Enum
from typing import Set
import os
class CPUTypes(Enum):
ATOMIC = 1
KVM = 2
O3 = 3
TIMING = 4
MINOR = 5
ATOMIC = "atomic"
KVM = "kvm"
O3 = "o3"
TIMING = "timing"
MINOR = "minor"
def get_cpu_types_str_set() -> Set[CPUTypes]:
"""
Returns a set of all the CPU types as strings.
"""
return {cpu_type.value for cpu_type in CPUTypes}
def get_cpu_type_from_str(input: str) -> CPUTypes:
"""
Will return the correct enum given the input string. This is matched on
the enum's value. E.g., "kvm" will return ISA.KVM. Throws an exception if
the input string is invalid.
`get_cpu_types_str_set()` can be used to determine the valid strings.
This is for parsing text inputs that specify CPU Type targets.
:param input: The CPU Type to return, as a string. Case-insensitive.
"""
for cpu_type in CPUTypes:
if input.lower() == cpu_type.value:
return cpu_type
valid_cpu_types_list_str =str()
for cpu_type_str in get_cpu_types_str_set():
valid_cpu_types_list_str += f"{os.linesep}{cpu_type_str}"
raise Exception(
f"CPU type '{input}' does not correspond to a known CPU type. "
f"Known CPU Types:{valid_cpu_types_list_str}"
)