Skip to content

Hardware Component

The HardwareComponent class is the base class for vCPU, vCPUCore and vNIC. It implements the mandatory abstract functions and property. You can create new physcial component based on this class.

Bases: Entity

Base class for all hardware components.

Source code in PyCloudSim\entity\v_hardware_component.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class vHardwareComponent(Entity):
    """Base class for all hardware components."""

    def __init__(
        self,
        label: str | None = None,
        create_at: int
        | float
        | Callable[..., int]
        | Callable[..., float]
        | None = None,
        terminate_at: int
        | float
        | Callable[..., int]
        | Callable[..., float]
        | None = None,
        precursor: Entity | List[Entity] | None = None,
    ) -> None:
        """Create a new hardware component.

        Args:
            label (str | None, optional): the short description of this new hardware component. Defaults to None.
            create_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when is this hardware component is created. Defaults to None.
            terminate_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when this hardware component will be terminated. Defaults to None.
            precursor (Entity | List[Entity] | None, optional): the other entity that this hardware component must not be created until their termination. Defaults to None.
        """
        super().__init__(label, create_at, terminate_at, precursor)

    def on_termination(self):
        """Power off upon termination."""
        self.power_off(simulation.now)

    def power_on(self, at: int | float) -> None:
        """Power on the hardware entity"""

        if self.powered_on:
            warnings.warn(f"{self.label} is already powered on.")
            return

        if self.failed:
            warnings.warn(f"{self.label} is failed.")
            return

        if self.terminated:
            warnings.warn(f"{self.label} is terminated.")
            return

        @self.instant_event(at, label=f"{self.label} Power On", priority=-1)
        def _power_on() -> None:
            if self.powered_on or self.terminated:
                return
            self.on_power_on()
            self.state.append(Constants.POWER_ON)
            if Constants.POWER_OFF in self.state:
                self.state.remove(Constants.POWER_OFF)
            if Constants.FAIL in self.state:
                self.state.remove(Constants.FAIL)

    def on_power_on(self) -> None:
        """Called when the hardware entity is powered on"""
        pass

    def power_off(self, at: int | float) -> None:
        """Power off the hardware entity"""

        if self.powered_off:
            warnings.warn(f"{self.label} is already powered off.")
            return
        if self.failed:
            warnings.warn(f"{self.label} is failed.")
            return

        if self.terminated:
            warnings.warn(f"{self.label} is terminated.")
            return

        @self.instant_event(at, label=f"{self.label} Power Off", priority=-1)
        def _power_off() -> None:
            if self.powered_off or self.terminated:
                return
            self.on_power_off()
            self.state.append(Constants.POWER_OFF)
            if Constants.POWER_ON in self.state:
                self.state.remove(Constants.POWER_ON)

    def on_power_off(self) -> None:
        """Called when the hardware entity is powered off"""
        pass

    def fail(self, at: int | float) -> None:
        """Fail the hardware component"""

        if self.failed:
            warnings.warn(f"{self.label} is already failed.")
            return

        if self.terminated:
            warnings.warn(f"{self.label} is terminated.")
            return

        @self.instant_event(at, label=f"{self.label} Fail")
        def _fail() -> None:
            if self.failed or self.terminated:
                return
            self.on_fail()
            self.power_off(simulation.now)
            self.state.append(Constants.FAIL)

    def on_fail(self) -> None:
        """Called when the hardware component fails"""
        pass

    def __str__(self) -> str:
        return f"{self.__class__.__name__}-{self.label}"

    @property
    def powered_on(self) -> bool:
        """Return True if the hardware component is powered on"""
        return Constants.POWER_ON in self.state

    @property
    def powered_off(self) -> bool:
        """Return True if the hardware component is powered off"""
        return Constants.POWER_OFF in self.state or Constants.POWER_ON not in self.state

    @property
    def failed(self) -> bool:
        """Return True if the hardware component fails"""
        return Constants.FAIL in self.state

failed: bool property

Return True if the hardware component fails

powered_off: bool property

Return True if the hardware component is powered off

powered_on: bool property

Return True if the hardware component is powered on

__init__(label=None, create_at=None, terminate_at=None, precursor=None)

Create a new hardware component.

Parameters:

Name Type Description Default
label str | None

the short description of this new hardware component. Defaults to None.

None
create_at int | float | Callable[..., int] | Callable[..., float] | None

when is this hardware component is created. Defaults to None.

None
terminate_at int | float | Callable[..., int] | Callable[..., float] | None

when this hardware component will be terminated. Defaults to None.

None
precursor Entity | List[Entity] | None

the other entity that this hardware component must not be created until their termination. Defaults to None.

None
Source code in PyCloudSim\entity\v_hardware_component.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    label: str | None = None,
    create_at: int
    | float
    | Callable[..., int]
    | Callable[..., float]
    | None = None,
    terminate_at: int
    | float
    | Callable[..., int]
    | Callable[..., float]
    | None = None,
    precursor: Entity | List[Entity] | None = None,
) -> None:
    """Create a new hardware component.

    Args:
        label (str | None, optional): the short description of this new hardware component. Defaults to None.
        create_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when is this hardware component is created. Defaults to None.
        terminate_at (int | float | Callable[..., int] | Callable[..., float] | None, optional): when this hardware component will be terminated. Defaults to None.
        precursor (Entity | List[Entity] | None, optional): the other entity that this hardware component must not be created until their termination. Defaults to None.
    """
    super().__init__(label, create_at, terminate_at, precursor)

fail(at)

Fail the hardware component

Source code in PyCloudSim\entity\v_hardware_component.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def fail(self, at: int | float) -> None:
    """Fail the hardware component"""

    if self.failed:
        warnings.warn(f"{self.label} is already failed.")
        return

    if self.terminated:
        warnings.warn(f"{self.label} is terminated.")
        return

    @self.instant_event(at, label=f"{self.label} Fail")
    def _fail() -> None:
        if self.failed or self.terminated:
            return
        self.on_fail()
        self.power_off(simulation.now)
        self.state.append(Constants.FAIL)

on_fail()

Called when the hardware component fails

Source code in PyCloudSim\entity\v_hardware_component.py
119
120
121
def on_fail(self) -> None:
    """Called when the hardware component fails"""
    pass

on_power_off()

Called when the hardware entity is powered off

Source code in PyCloudSim\entity\v_hardware_component.py
96
97
98
def on_power_off(self) -> None:
    """Called when the hardware entity is powered off"""
    pass

on_power_on()

Called when the hardware entity is powered on

Source code in PyCloudSim\entity\v_hardware_component.py
69
70
71
def on_power_on(self) -> None:
    """Called when the hardware entity is powered on"""
    pass

on_termination()

Power off upon termination.

Source code in PyCloudSim\entity\v_hardware_component.py
39
40
41
def on_termination(self):
    """Power off upon termination."""
    self.power_off(simulation.now)

power_off(at)

Power off the hardware entity

Source code in PyCloudSim\entity\v_hardware_component.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def power_off(self, at: int | float) -> None:
    """Power off the hardware entity"""

    if self.powered_off:
        warnings.warn(f"{self.label} is already powered off.")
        return
    if self.failed:
        warnings.warn(f"{self.label} is failed.")
        return

    if self.terminated:
        warnings.warn(f"{self.label} is terminated.")
        return

    @self.instant_event(at, label=f"{self.label} Power Off", priority=-1)
    def _power_off() -> None:
        if self.powered_off or self.terminated:
            return
        self.on_power_off()
        self.state.append(Constants.POWER_OFF)
        if Constants.POWER_ON in self.state:
            self.state.remove(Constants.POWER_ON)

power_on(at)

Power on the hardware entity

Source code in PyCloudSim\entity\v_hardware_component.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def power_on(self, at: int | float) -> None:
    """Power on the hardware entity"""

    if self.powered_on:
        warnings.warn(f"{self.label} is already powered on.")
        return

    if self.failed:
        warnings.warn(f"{self.label} is failed.")
        return

    if self.terminated:
        warnings.warn(f"{self.label} is terminated.")
        return

    @self.instant_event(at, label=f"{self.label} Power On", priority=-1)
    def _power_on() -> None:
        if self.powered_on or self.terminated:
            return
        self.on_power_on()
        self.state.append(Constants.POWER_ON)
        if Constants.POWER_OFF in self.state:
            self.state.remove(Constants.POWER_OFF)
        if Constants.FAIL in self.state:
            self.state.remove(Constants.FAIL)