stdlib: Specialize the gem5-resources
This commit specializes the Resource class into specific sub-types. The `Resource`, `CustomResource` and `CustomDiskImageResource` classes have been deprecated in favor of the `AbstractResource` subclasses. Custom Resources can be created via the resource specialization constructor. Resources can be obtained via the gem5-resource infrastructure with the `obtain_resource` function. Fully implemented: - DiskImageResource - BinaryResource - KernelResource - BootloaderResource - FileResource - DirectoryResource Partially implemented: - SimpointResource - CheckpointResource While the schema of the resource.json file has changed, efforts have been made to ensure backwards compatibility is maintained during this transition. Tests are included in this commit to verify this feature works as expected. **Note:** The Simpoint tests are disabled in this commit, to be reenabled when Simpoint resource specialization is fully incorporated here: https://gem5-review.googlesource.com/c/public/gem5/+/67339 Change-Id: I77277ecaffc7abc86db08526aacc0b606ef04fe8 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/67175 Tested-by: kokoro <noreply+kokoro@google.com> Reviewed-by: Bobby Bruce <bbruce@ucdavis.edu> Maintainer: Bobby Bruce <bbruce@ucdavis.edu>
This commit is contained in:
committed by
Zhantong Qiu
parent
3892ee029a
commit
4ee724e054
196
tests/pyunit/stdlib/resources/pyunit_resource_specialization.py
Normal file
196
tests/pyunit/stdlib/resources/pyunit_resource_specialization.py
Normal file
@@ -0,0 +1,196 @@
|
||||
# Copyright (c) 2023 The Regents of the University of California
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met: redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer;
|
||||
# redistributions in binary form must reproduce the above copyright
|
||||
# notice, this list of conditions and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution;
|
||||
# neither the name of the copyright holders nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from gem5.resources.resource import *
|
||||
from gem5.isas import ISA
|
||||
|
||||
|
||||
class ResourceSpecializationSuite(unittest.TestCase):
|
||||
"""This suite tests that `gem5.resource.resource` casts to the correct
|
||||
`AbstractResource` specialization when using the `obtain_resource`
|
||||
function.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
"""Prior to running the suite we set the resource directory to
|
||||
"ref/resource-specialization.json"
|
||||
"""
|
||||
os.environ["GEM5_RESOURCE_JSON"] = os.path.join(
|
||||
os.path.realpath(os.path.dirname(__file__)),
|
||||
"refs",
|
||||
"resource-specialization.json",
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls) -> None:
|
||||
"""After running the suite we unset the gem5-resource JSON file, as to
|
||||
not interfere with others tests.
|
||||
"""
|
||||
del os.environ["GEM5_RESOURCE_JSON"]
|
||||
|
||||
def get_resource_dir(cls) -> str:
|
||||
"""To ensure the resources are cached to the same directory as all
|
||||
other tests, this function returns the location of the testing
|
||||
directories "resources" directory.
|
||||
"""
|
||||
return os.path.join(
|
||||
os.path.realpath(os.path.dirname(__file__)),
|
||||
os.pardir,
|
||||
os.pardir,
|
||||
os.pardir,
|
||||
"gem5",
|
||||
"resources",
|
||||
)
|
||||
|
||||
def test_binary_resource(self) -> None:
|
||||
"""Tests the loading of of a BinaryResource"""
|
||||
resource = obtain_resource(
|
||||
resource_name="binary-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, BinaryResource)
|
||||
|
||||
self.assertEquals(
|
||||
"binary-example documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertEquals("src/simple", resource.get_source())
|
||||
self.assertEquals(ISA.ARM, resource.get_architecture())
|
||||
|
||||
def test_kernel_resource(self) -> None:
|
||||
"""Tests the loading of a KernelResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="kernel-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, KernelResource)
|
||||
|
||||
self.assertEquals(
|
||||
"kernel-example documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertEquals("src/linux-kernel", resource.get_source())
|
||||
self.assertEquals(ISA.RISCV, resource.get_architecture())
|
||||
|
||||
def test_bootloader_resource(self) -> None:
|
||||
"""Tests the loading of a BootloaderResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="bootloader-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, BootloaderResource)
|
||||
|
||||
self.assertEquals(
|
||||
"bootloader documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertIsNone(resource.get_source())
|
||||
self.assertIsNone(resource.get_architecture())
|
||||
|
||||
def test_disk_image_resource(self) -> None:
|
||||
"""Tests the loading of a DiskImageResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="disk-image-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, DiskImageResource)
|
||||
|
||||
self.assertEquals(
|
||||
"disk-image documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertEquals("src/x86-ubuntu", resource.get_source())
|
||||
self.assertEquals("1", resource.get_root_partition())
|
||||
|
||||
def test_checkpoint_resource(self) -> None:
|
||||
"""Tests the loading of a CheckpointResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="checkpoint-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, CheckpointResource)
|
||||
|
||||
self.assertEquals(
|
||||
"checkpoint-example documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertIsNone(resource.get_source())
|
||||
|
||||
def test_git_resource(self) -> None:
|
||||
"""Tests the loading of a GitResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="git-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, GitResource)
|
||||
|
||||
self.assertIsNone(resource.get_documentation())
|
||||
self.assertIsNone(resource.get_source())
|
||||
|
||||
def test_simpoint_resource(self) -> None:
|
||||
"""Tests the loading of a Simpoint resource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="simpoint-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, SimpointResource)
|
||||
|
||||
self.assertEquals(
|
||||
"simpoint documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertIsNone(resource.get_source())
|
||||
|
||||
def test_file_resource(self) -> None:
|
||||
"""Tests the loading of a FileResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="file-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, FileResource)
|
||||
|
||||
self.assertIsNone(resource.get_documentation())
|
||||
self.assertIsNone(resource.get_source())
|
||||
|
||||
def test_directory_resource(self) -> None:
|
||||
"""Tests the loading of a DirectoryResource."""
|
||||
resource = obtain_resource(
|
||||
resource_name="directory-example",
|
||||
resource_directory=self.get_resource_dir(),
|
||||
)
|
||||
|
||||
self.assertIsInstance(resource, DirectoryResource)
|
||||
|
||||
self.assertEquals(
|
||||
"directory-example documentation.", resource.get_documentation()
|
||||
)
|
||||
self.assertIsNone(resource.get_source())
|
||||
@@ -29,7 +29,11 @@ import tempfile
|
||||
import os
|
||||
|
||||
from gem5.resources.workload import Workload, CustomWorkload
|
||||
from gem5.resources.resource import Resource
|
||||
from gem5.resources.resource import (
|
||||
BinaryResource,
|
||||
DiskImageResource,
|
||||
obtain_resource,
|
||||
)
|
||||
from gem5.resources.downloader import _resources_json_version_required
|
||||
|
||||
from typing import Dict
|
||||
@@ -50,7 +54,7 @@ class CustomWorkloadTestSuite(unittest.TestCase):
|
||||
"previous-versions" : {},
|
||||
"resources": [
|
||||
{
|
||||
"type" : "resource",
|
||||
"type" : "binary",
|
||||
"name" : "x86-hello64-static",
|
||||
"documentation" : "A 'Hello World!' binary.",
|
||||
"architecture" : "X86",
|
||||
@@ -73,7 +77,7 @@ class CustomWorkloadTestSuite(unittest.TestCase):
|
||||
cls.custom_workload = CustomWorkload(
|
||||
function="set_se_binary_workload",
|
||||
parameters={
|
||||
"binary": Resource("x86-hello64-static"),
|
||||
"binary": obtain_resource("x86-hello64-static"),
|
||||
"arguments": ["hello", 6],
|
||||
},
|
||||
)
|
||||
@@ -100,7 +104,7 @@ class CustomWorkloadTestSuite(unittest.TestCase):
|
||||
self.assertEquals(2, len(parameters))
|
||||
|
||||
self.assertTrue("binary" in parameters)
|
||||
self.assertTrue(isinstance(parameters["binary"], Resource))
|
||||
self.assertTrue(isinstance(parameters["binary"], BinaryResource))
|
||||
|
||||
self.assertTrue("arguments" in parameters)
|
||||
self.assertTrue(isinstance(parameters["arguments"], list))
|
||||
@@ -156,7 +160,7 @@ class WorkloadTestSuite(unittest.TestCase):
|
||||
"previous-versions" : {},
|
||||
"resources": [
|
||||
{
|
||||
"type" : "resource",
|
||||
"type" : "kernel",
|
||||
"name" : "x86-linux-kernel-5.2.3",
|
||||
"documentation" : "The linux kernel (v5.2.3), compiled to X86.",
|
||||
"architecture" : "X86",
|
||||
@@ -166,7 +170,7 @@ class WorkloadTestSuite(unittest.TestCase):
|
||||
"source" : "src/linux-kernel"
|
||||
},
|
||||
{
|
||||
"type" : "resource",
|
||||
"type" : "disk-image",
|
||||
"name" : "x86-ubuntu-18.04-img",
|
||||
"documentation" : "A disk image containing Ubuntu 18.04 for x86..",
|
||||
"architecture" : "X86",
|
||||
@@ -174,9 +178,7 @@ class WorkloadTestSuite(unittest.TestCase):
|
||||
"md5sum" : "90e363abf0ddf22eefa2c7c5c9391c49",
|
||||
"url" : "{url_base}/images/x86/ubuntu-18-04/x86-ubuntu.img.gz",
|
||||
"source" : "src/x86-ubuntu",
|
||||
"additional_metadata" : {
|
||||
"root_partition": "1"
|
||||
}
|
||||
"root_partition": "1"
|
||||
},
|
||||
{
|
||||
"type" : "workload",
|
||||
@@ -226,10 +228,12 @@ class WorkloadTestSuite(unittest.TestCase):
|
||||
self.assertEqual(3, len(parameters))
|
||||
|
||||
self.assertTrue("kernel" in parameters)
|
||||
self.assertTrue(isinstance(parameters["kernel"], Resource))
|
||||
self.assertTrue(isinstance(parameters["kernel"], BinaryResource))
|
||||
|
||||
self.assertTrue("disk_image" in parameters)
|
||||
self.assertTrue(isinstance(parameters["disk_image"], Resource))
|
||||
self.assertTrue(
|
||||
isinstance(parameters["disk_image"], DiskImageResource)
|
||||
)
|
||||
|
||||
self.assertTrue("readfile_contents" in parameters)
|
||||
self.assertTrue(
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
|
||||
{
|
||||
"version" : "develop",
|
||||
"url_base" : "http://dist.gem5.org/dist/v22-1",
|
||||
"previous-versions" : {
|
||||
"develop" : "https://gem5.googlesource.com/public/gem5-resources/+/refs/heads/develop/resources.json?format=TEXT",
|
||||
"21.2" : "http://resources.gem5.org/prev-resources-json/resources-21-2.json"
|
||||
},
|
||||
"resources": [
|
||||
{
|
||||
"type" : "kernel",
|
||||
"name" : "kernel-example",
|
||||
"documentation" : "kernel-example documentation.",
|
||||
"architecture" : "RISCV",
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "60a53c7d47d7057436bf4b9df707a841",
|
||||
"url" : "{url_base}/kernels/x86/static/vmlinux-5.4.49",
|
||||
"source" : "src/linux-kernel"
|
||||
},
|
||||
{
|
||||
"type" : "disk-image",
|
||||
"name" : "disk-image-example",
|
||||
"documentation" : "disk-image documentation.",
|
||||
"architecture" : "X86",
|
||||
"is_zipped" : true,
|
||||
"md5sum" : "90e363abf0ddf22eefa2c7c5c9391c49",
|
||||
"url" : "{url_base}/images/x86/ubuntu-18-04/x86-ubuntu.img.gz",
|
||||
"source" : "src/x86-ubuntu",
|
||||
"root_partition": "1"
|
||||
},
|
||||
{
|
||||
"type" : "binary",
|
||||
"name" : "binary-example",
|
||||
"documentation" : "binary-example documentation.",
|
||||
"architecture" : "ARM",
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "71b2cb004fe2cda4556f0b1a38638af6",
|
||||
"url" : "{url_base}/test-progs/hello/bin/arm/linux/hello64-static",
|
||||
"source" : "src/simple"
|
||||
},
|
||||
{
|
||||
"type" : "bootloader",
|
||||
"name" : "bootloader-example",
|
||||
"documentation" : "bootloader documentation.",
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "71b2cb004fe2cda4556f0b1a38638af6",
|
||||
"url" : "{url_base}/test-progs/hello/bin/arm/linux/hello64-static"
|
||||
},
|
||||
{
|
||||
"type" : "checkpoint",
|
||||
"name" : "checkpoint-example",
|
||||
"documentation" : "checkpoint-example documentation.",
|
||||
"architecture": "RISCV",
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "3a57c1bb1077176c4587b8a3bf4f8ace",
|
||||
"source" : null,
|
||||
"is_tar_archive" : true,
|
||||
"url": "{url_base}/checkpoints/riscv-hello-example-checkpoint.tar"
|
||||
},
|
||||
{
|
||||
"type" : "git",
|
||||
"name" : "git-example",
|
||||
"documentation" : null,
|
||||
"is_zipped" : false,
|
||||
"is_tar_archive" : true,
|
||||
"md5sum" : "71b2cb004fe2cda4556f0b1a38638af6",
|
||||
"url": "{url_base}/checkpoints/riscv-hello-example-checkpoint.tar"
|
||||
},
|
||||
{
|
||||
"type" : "file",
|
||||
"name" : "file-example",
|
||||
"documentation" : null,
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "71b2cb004fe2cda4556f0b1a38638af6",
|
||||
"url": "{url_base}/checkpoints/riscv-hello-example-checkpoint.tar",
|
||||
"source" : null
|
||||
},
|
||||
{
|
||||
"type" : "directory",
|
||||
"name" : "directory-example",
|
||||
"documentation" : "directory-example documentation.",
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "3a57c1bb1077176c4587b8a3bf4f8ace",
|
||||
"source" : null,
|
||||
"is_tar_archive" : true,
|
||||
"url": "{url_base}/checkpoints/riscv-hello-example-checkpoint.tar"
|
||||
},
|
||||
{
|
||||
"type" : "simpoint",
|
||||
"name" : "simpoint-example",
|
||||
"documentation" : "simpoint documentation.",
|
||||
"is_zipped" : false,
|
||||
"md5sum" : "3a57c1bb1077176c4587b8a3bf4f8ace",
|
||||
"source" : null,
|
||||
"is_tar_archive" : true,
|
||||
"url": "{url_base}/checkpoints/riscv-hello-example-checkpoint.tar"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user