machines.hpp 5.95 KB
Newer Older
Millian Poquet's avatar
Millian Poquet committed
1 2 3 4 5
/**
 * @file machines.hpp
 * @brief Contains machine-related classes
 */

Millian Poquet's avatar
Millian Poquet committed
6 7 8 9
#pragma once

#include <vector>
#include <set>
10
#include <map>
11
#include <string>
Millian Poquet's avatar
Millian Poquet committed
12 13 14

#include <simgrid/msg.h>

15
#include "pstate.hpp"
16
#include "machine_range.hpp"
17

18
class PajeTracer;
19
struct BatsimContext;
20

Millian Poquet's avatar
Millian Poquet committed
21 22 23
/**
 * @brief Enumerates the different states of a Machine
 */
24 25
enum class MachineState
{
Millian Poquet's avatar
Millian Poquet committed
26 27 28 29 30
    SLEEPING                                //!< The machine is currently sleeping
    ,IDLE                                   //!< The machine is currently idle
    ,COMPUTING                              //!< The machine is currently computing a job
    ,TRANSITING_FROM_SLEEPING_TO_COMPUTING  //!< The machine is in transition from a sleeping state to a computing state
    ,TRANSITING_FROM_COMPUTING_TO_SLEEPING  //!< The machine is in transition from a computing state to a sleeping state
31 32
};

Millian Poquet's avatar
Millian Poquet committed
33 34 35
/**
 * @brief Represents a machine
 */
36
struct Machine
Millian Poquet's avatar
Millian Poquet committed
37
{
Millian Poquet's avatar
Millian Poquet committed
38 39 40
    /**
     * @brief Destroys a Machine
     */
41 42
    ~Machine();

Millian Poquet's avatar
Millian Poquet committed
43 44 45 46 47
    int id; //!< The machine unique number
    std::string name; //!< The machine name
    msg_host_t host; //!< The SimGrid host corresponding to the machine
    MachineState state; //!< The current state of the Machine
    std::set<int> jobs_being_computed; //!< The set of jobs being computed on the Machine
48

Millian Poquet's avatar
Millian Poquet committed
49 50
    std::map<int, PStateType> pstates; //!< Maps power state number to their power state type
    std::map<int, SleepPState *> sleep_pstates; //!< Maps sleep power state numbers to their SleepPState
51

Millian Poquet's avatar
Millian Poquet committed
52 53 54 55 56
    /**
     * @brief Returns whether the Machine has the given power state
     * @param[in] pstate The power state
     * @return True if and only if the Machine has the given power state
     */
57
    bool has_pstate(int pstate) const;
Millian Poquet's avatar
Millian Poquet committed
58 59 60 61 62

    /**
     * @brief Displays the Machine (debug purpose)
     * @param[in] is_energy_used Must be set to true if energy information should be displayed
     */
63
    void display_machine(bool is_energy_used) const;
Millian Poquet's avatar
Millian Poquet committed
64 65 66 67 68

    /**
     * @brief Returns a std::string corresponding to the jobs being computed of the Machine
     * @return A std::string corresponding to the jobs being computed of the Machine
     */
69
    std::string jobs_being_computed_as_string() const;
Millian Poquet's avatar
Millian Poquet committed
70 71
};

Millian Poquet's avatar
Millian Poquet committed
72 73 74 75 76 77
/**
 * @brief Compares two machines according to the lexicographical order of their names
 * @param[in] m1 The first machine
 * @param[in] m2 The second machine
 * @return True if and only if the machine name of the first machine if before the machine name of the second machine, lexicographically speaking
 */
78 79
bool machine_comparator_name(const Machine * m1, const Machine * m2);

Millian Poquet's avatar
Millian Poquet committed
80 81 82
/**
 * @brief Handles all the machines used in the simulation
 */
Millian Poquet's avatar
Millian Poquet committed
83 84 85
class Machines
{
public:
Millian Poquet's avatar
Millian Poquet committed
86 87 88
    /**
     * @brief Constructs an empty Machines
     */
89
    Machines();
Millian Poquet's avatar
Millian Poquet committed
90 91 92 93

    /**
     * @brief Destroys a Machines
     */
94
    ~Machines();
Millian Poquet's avatar
Millian Poquet committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    /**
     * @brief Fill the Machines with SimGrid hosts
     * @param[in] hosts The SimGrid hosts
     * @param[in] context The Batsim Context
     * @param[in] masterHostName The name of the host which should be used as the Master host
     */
    void createMachines(xbt_dynar_t hosts, const BatsimContext * context, const std::string & masterHostName);

    /**
     * @brief Must be called when a job is executed on some machines
     * @details Puts the used machines in a computing state and traces the job beginning
     * @param[in] jobID The unique job number
     * @param[in] usedMachines The machines on which the job is executed
     */
110
    void updateMachinesOnJobRun(int jobID, const MachineRange & usedMachines);
Millian Poquet's avatar
Millian Poquet committed
111 112 113 114 115 116 117

    /**
     * @brief Must be called when a job finishes its execution on some machines
     * @details Puts the used machines in an idle state and traces the job ending
     * @param[in] jobID The unique job number
     * @param[in] usedMachines The machines on which the job is executed
     */
118
    void updateMachinesOnJobEnd(int jobID, const MachineRange & usedMachines);
Millian Poquet's avatar
Millian Poquet committed
119

Millian Poquet's avatar
Millian Poquet committed
120 121 122
    /**
     * @brief Sorts the machine by ascending name (lexicographically speaking)
     */
123 124
    void sortMachinesByAscendingName();

Millian Poquet's avatar
Millian Poquet committed
125 126 127 128
    /**
     * @brief Sets the PajeTracer
     * @param[in] tracer The PajeTracer
     */
129 130
    void setTracer(PajeTracer * tracer);

Millian Poquet's avatar
Millian Poquet committed
131 132 133 134 135
    /**
     * @brief Accesses a Machine thanks to its unique number
     * @param[in] machineID The unique machine number
     * @return The machine whose machine number is given
     */
136
    const Machine * operator[](int machineID) const;
Millian Poquet's avatar
Millian Poquet committed
137 138 139 140 141 142

    /**
     * @brief Accesses a Machine thanks to its unique number
     * @param[in] machineID The machine unique number
     * @return The machine whose machine number is given
     */
143
    Machine * operator[](int machineID);
144

Millian Poquet's avatar
Millian Poquet committed
145 146 147 148 149
    /**
     * @brief Checks whether a machine exists
     * @param[in] machineID The machine unique number
     * @return True if and only if the machine exists
     */
150
    bool exists(int machineID) const;
Millian Poquet's avatar
Millian Poquet committed
151 152 153 154

    /**
     * @brief Displays all machines (debug purpose)
     */
155
    void displayDebug() const;
Millian Poquet's avatar
Millian Poquet committed
156

Millian Poquet's avatar
Millian Poquet committed
157 158 159 160
    /**
     * @brief Returns a const reference to the vector of Machine
     * @return A const reference to the vector of Machine
     */
161
    const std::vector<Machine *> & machines() const;
Millian Poquet's avatar
Millian Poquet committed
162 163 164 165 166

    /**
     * @brief Returns a const pointer to the Master host machine
     * @return A const pointer to the Master host machine
     */
167
    const Machine * masterMachine() const;
Millian Poquet's avatar
Millian Poquet committed
168 169 170

    /**
     * @brief Computes and returns the total consumed energy of all the computing machines
171
     * @param[in] context The Batsim context
Millian Poquet's avatar
Millian Poquet committed
172 173 174
     * @return The total consumed energy of all the computing machines
     */
    long double total_consumed_energy(const BatsimContext * context) const;
175

Millian Poquet's avatar
Millian Poquet committed
176
private:
Millian Poquet's avatar
Millian Poquet committed
177 178 179
    std::vector<Machine *> _machines; //!< The vector of computing machines
    Machine * _masterMachine = nullptr; //!< The Master host
    PajeTracer * _tracer = nullptr; //!< The PajeTracer
Millian Poquet's avatar
Millian Poquet committed
180 181
};

Millian Poquet's avatar
Millian Poquet committed
182 183 184 185 186
/**
 * @brief Returns a std::string corresponding to a given MachineState
 * @param[in] state The MachineState
 * @return A std::string corresponding to a given MachineState
 */
187
std::string machineStateToString(MachineState state);