Simulator

class batsim_py.simulator.Reservation(host, release_time)[source]

Bases: object

Describes the reservation of a host.

Parameters
  • host (Host) – The host.

  • release_time (float) – The remaining time to the host be released.

host

The host.

release_time

The remaining time to the host be released.

class batsim_py.simulator.SimulatorHandler(tcp_address=None)[source]

Bases: object

Simulator handler class.

This class will handle the Batsim simulation process.

Parameters

tcp_address (Optional[str]) – An address string consisting of three parts as follows: protocol://interface:port. Defaults to None. If no address is provided, a random one will be used.

Raises

ImportError – In case of Batsim cannot be found.

Examples

>>> handler = SimulatorHandler("tcp://localhost:28000")
property address

The address string.

Return type

str

property agenda

Host reservations.

Return type

Iterator[Reservation]

allocate(job_id, hosts_id, storage_mapping=None)[source]

Allocate resources for a job.

To start computing, a job must allocate some resources first. When these resources are ready, the simulator will automatically start the job. If for some reason any allocated resource is not ready (because it’s off or switching On/Off), the simulator will try to initialize the resource and will wait until it’s ready to start the job.

Parameters
  • job_id (str) – The job id.

  • hosts_id (Sequence[int]) – A sequence of host ids to be allocated for the job.

  • storage_mapping (Optional[Dict[str, int]]) – A mapping of storage names to resource ids. If the job needs a storage resource, this argument is required. Otherwise, it can just be ignored.

Raises
  • RuntimeError – In case of simulation is not running.

  • LookupError – In case of job/resource not found or resource type is invalid.

Return type

None

close()[source]

Close the simulation process.

Return type

None

property current_time

The current simulation time (seconds).

Return type

int

property is_running

Whether the simulation is running or not.

Return type

bool

property is_submitter_finished

Whether there are still some jobs to be submitted or not.

Return type

bool

property jobs

A sequence with all the jobs in the system.

This does not include jobs that already finished.

Return type

Sequence[Job]

kill_job(job_id)[source]

Kill a job that is running.

Parameters

job_id (str) – The id of the job to kill.

Raises
  • RuntimeError – In case of simulation is not running or the job cannot be killed.

  • LookupError – In case of job not found.

Return type

None

property platform

The simulation platform.

Return type

Platform

proceed_time(time=0)[source]

Proceed the simulation process to the next event or time.

Parameters

time (int) – The time to proceed (min is 1 sec). Defaults to 0. It’s possible to proceed directly to the next event or to a specific time. If the time is unset (equals 0), the simulation will proceed to the next event. Otherwise, if a time ‘t’ is provided, the simulation will proceed directly to it and only events that occur before ‘t’ will be dispatched.

Raises
  • ValueError – In case of invalid arguments value.

  • RuntimeError – In case of the simulation is not running or a deadlock happened. The latter case occurs only when there are no more events to happen. Consequently, the simulation does not know what to do and a deadlock error is raised by Batsim.

Return type

None

property queue

A sequence of all jobs in the queue.

Return type

Sequence[Job]

reject_job(job_id)[source]

Rejects a job.

Only jobs in the queue can be rejected. Once a job is rejected, it cannot be scheduled anymore.

Parameters

job_id (str) – The id of the job to reject.

Raises
  • RuntimeError – In case of simulation is not running or the job cannot be rejected.

  • LookupError – In case of job not found.

Return type

None

set_callback(at, call)[source]

Setup a callback.

The simulation will call the function at the defined time.

Parameters
  • at (int) – The time which the function must be called (min is 1 sec).

  • call (Callable[[float], None]) – A function that receives the current simulation time as an argument.

Raises
  • ValueError – In case of invalid arguments value.

  • RuntimeError – In case of simulation is not running.

Return type

None

start(platform, workload, verbosity='quiet', simulation_time=None, allow_compute_sharing=False, allow_storage_sharing=True, external_events=None)[source]

Start the simulation process.

Parameters
  • platform (str) – The XML file defining the platform. It must follow the format expected by Batsim and SimGrid. Check their documentation to know how to define a platform

  • workload (str) – A JSON file defining the jobs and their profiles. The simulation process will only submit the jobs that are defined in this JSON. Moreover, Batsim is responsible for the submission process.

  • verbosity (Literal[‘quiet’, ‘network-only’, ‘information’, ‘debug’]) – The Batsim verbosity level. Defaults to “quiet”.

  • simulation_time (Optional[float]) – The maximum simulation time. Defaults to None. If this argument is set, the simulation will stop only when this time is reached, no matter if there are jobs to be submitted or running. Otherwise, the simulation will only stop when all jobs in the workload were completed or rejected.

  • allow_compute_sharing – Whether a host can be used by multiple jobs or not. Defaults to False.

  • allow_storage_sharing – Whether a storage can be used by multiple jobs or not. Defaults to True.

  • external_events (Optional[str]) – The file containing external events to simulate. Check the Batsim documentation to know what kind of external events are supported.

Raises
  • RuntimeError – In case of simulation already running.

  • ValueError – In case of simulation_time is less than or equals to zero or the verbosity is invalid.

Examples

>>> handler = SimulatorHandler("tcp://localhost:28000")
>>> handler.start("platform.xml", "workload.json", "information", 1440)
Return type

None

subscribe(event: batsim_py.events.JobEvent, call: Callable[[batsim_py.jobs.Job], None]) → None[source]
subscribe(event: batsim_py.events.HostEvent, call: Callable[[batsim_py.resources.Host], None]) → None
subscribe(event: batsim_py.events.SimulatorEvent, call: Callable[[SimulatorHandler], None]) → None

Subscribe to an event.

Parameters
  • event (Union[JobEvent, HostEvent, SimulatorEvent]) – The event to subscribe.

  • call (Union[Callable[[Job], None], Callable[[Host], None], Callable[[SimulatorHandler], None]]) – The function to be called when the event is dispatched. It must accept the event sender as an argument.

Raises

RuntimeError – In case of simulation is not running.

Return type

None

switch_off(hosts_id)[source]

Switch off hosts.

Parameters

hosts_id (Sequence[int]) – A sequence of host ids to be switched off.

Raises
  • RuntimeError – In case of simulation is not running or a host cannot switch off because it’s in an invalid state or the power state is not defined.

  • LookupError – In case of host not found or the ‘off’ power state could not be found.

Return type

None

switch_on(hosts_id)[source]

Switch on hosts.

Parameters

hosts_id (Sequence[int]) – A sequence of host ids to be switched on.

Raises
  • RuntimeError – In case of the simulation is not running or the host cannot switch on because it’s in an invalid state or the power state is not defined.

  • LookupError – In case of host not found.

Return type

None

switch_power_state(host_id, pstate_id)[source]

Switch the host computation power state.

This is useful if you want to implement a DVFS policy.

Parameters
  • host_id (int) – The host id.

  • pstate_id (int) – The computation power state id.

Raises
  • RuntimeError – In case of the simulation is not running or the power state were is defined or the host cannot switch.

  • LookupError – In case of host or power state could not be found.

Return type

None