ipp.cpp 9.16 KB
Newer Older
Millian Poquet's avatar
Millian Poquet committed
1 2
/**
 * @file ipp.cpp
Millian Poquet's avatar
Millian Poquet committed
3
 * @brief Inter-Process Protocol (within Batsim, not with the Decision real process)
Millian Poquet's avatar
Millian Poquet committed
4 5
 */

6 7 8 9 10 11
#include "ipp.hpp"

#include <simgrid/msg.h>

using namespace std;

Millian Poquet's avatar
Millian Poquet committed
12
XBT_LOG_NEW_DEFAULT_CATEGORY(ipp, "ipp"); //!< Logging
13

Millian Poquet's avatar
Millian Poquet committed
14 15 16 17
void generic_send_message(const std::string & destination_mailbox,
                          IPMessageType type,
                          void * data,
                          bool detached)
18 19 20 21 22 23 24
{
    IPMessage * message = new IPMessage;
    message->type = type;
    message->data = data;

    msg_task_t task_to_send = MSG_task_create(NULL, 0, 1e-6, message);

25 26
    XBT_DEBUG("message from '%s' to '%s' of type '%s' with data %p",
              MSG_process_get_name(MSG_process_self()), destination_mailbox.c_str(),
Millian Poquet's avatar
Millian Poquet committed
27
              ip_message_type_to_string(type).c_str(), data);
28

29
    if (detached)
30
    {
31
        MSG_task_dsend(task_to_send, destination_mailbox.c_str(), NULL);
32
    }
33
    else
34 35 36 37 38 39 40
    {
        msg_error_t err = MSG_task_send(task_to_send, destination_mailbox.c_str());
        xbt_assert(err == MSG_OK,
                   "Sending message from '%s' to '%s' of type '%s' with data %p FAILED!",
                   MSG_process_get_name(MSG_process_self()), destination_mailbox.c_str(),
                   ip_message_type_to_string(type).c_str(), data);
    }
41

42 43
    XBT_DEBUG("message from '%s' to '%s' of type '%s' with data %p done",
              MSG_process_get_name(MSG_process_self()), destination_mailbox.c_str(),
Millian Poquet's avatar
Millian Poquet committed
44
              ip_message_type_to_string(type).c_str(), data);
45 46 47 48
}

void send_message(const std::string & destination_mailbox, IPMessageType type, void * data)
{
49
    generic_send_message(destination_mailbox, type, data, false);
50 51 52 53
}

void dsend_message(const std::string & destination_mailbox, IPMessageType type, void * data)
{
54
    generic_send_message(destination_mailbox, type, data, true);
55 56
}

Millian Poquet's avatar
Millian Poquet committed
57
std::string ip_message_type_to_string(IPMessageType type)
58
{
59 60
    string s;

61 62
    // Do not remove the switch. If one adds a new IPMessageType but forgets to handle it in the
    // switch, a compilation warning should help avoiding this bug.
63
    switch(type)
64
    {
65 66 67
        case IPMessageType::JOB_SUBMITTED:
            s = "JOB_SUBMITTED";
            break;
68 69 70
        case IPMessageType::JOB_SUBMITTED_BY_DP:
            s = "JOB_SUBMITTED_BY_DP";
            break;
71 72 73
        case IPMessageType::PROFILE_SUBMITTED_BY_DP:
            s = "PROFILE_SUBMITTED_BY_DP";
            break;
74 75 76 77 78 79
        case IPMessageType::JOB_COMPLETED:
            s = "JOB_COMPLETED";
            break;
        case IPMessageType::PSTATE_MODIFICATION:
            s = "PSTATE_MODIFICATION";
            break;
Millian Poquet's avatar
Millian Poquet committed
80 81
        case IPMessageType::SCHED_EXECUTE_JOB:
            s = "SCHED_EXECUTE_JOB";
82
            break;
83 84 85
        case IPMessageType::SCHED_CHANGE_JOB_STATE:
            s = "SCHED_CHANGE_JOB_STATE";
            break;
Millian Poquet's avatar
Millian Poquet committed
86 87
        case IPMessageType::SCHED_REJECT_JOB:
            s = "SCHED_REJECT_JOB";
88
            break;
89 90 91
        case IPMessageType::SCHED_KILL_JOB:
            s = "SCHED_KILL_JOB";
            break;
Millian Poquet's avatar
Millian Poquet committed
92 93
        case IPMessageType::SCHED_CALL_ME_LATER:
            s = "SCHED_CALL_ME_LATER";
94
            break;
95 96 97
        case IPMessageType::SCHED_TELL_ME_ENERGY:
            s = "SCHED_TELL_ME_ENERGY";
            break;
Pierre-François's avatar
Pierre-François committed
98 99 100 101 102 103
        case IPMessageType::SCHED_WAIT_ANSWER:
            s = "SCHED_WAIT_ANSWER";
            break;
        case IPMessageType::WAIT_QUERY:
            s = "WAIT_QUERY";
            break;
104 105 106 107 108 109 110 111 112
        case IPMessageType::SCHED_READY:
            s = "SCHED_READY";
            break;
        case IPMessageType::WAITING_DONE:
            s = "WAITING_DONE";
            break;
        case IPMessageType::SUBMITTER_HELLO:
            s = "SUBMITTER_HELLO";
            break;
113 114 115
        case IPMessageType::SUBMITTER_CALLBACK:
            s = "SUBMITTER_CALLBACK";
            break;
116 117 118 119 120 121 122 123 124
        case IPMessageType::SUBMITTER_BYE:
            s = "SUBMITTER_BYE";
            break;
        case IPMessageType::SWITCHED_ON:
            s = "SWITCHED_ON";
            break;
        case IPMessageType::SWITCHED_OFF:
            s = "SWITCHED_OFF";
            break;
125
        case IPMessageType::KILLING_DONE:
126
            s = "KILLING_DONE";
127
            break;
128 129
        case IPMessageType::END_DYNAMIC_SUBMIT:
            s = "END_DYNAMIC_SUBMIT";
130 131 132
            break;
        case IPMessageType::CONTINUE_DYNAMIC_SUBMIT:
            s = "CONTINUE_DYNAMIC_SUBMIT";
133 134 135
            break;
        case IPMessageType::TO_JOB_MSG:
            s = "TO_JOB_MSG";
136 137 138
            break;
        case IPMessageType::FROM_JOB_MSG:
            s = "FROM_JOB_MSG";
139
    }
140

141
    return s;
142
}
143 144 145 146 147 148

void send_message(const char *destination_mailbox, IPMessageType type, void *data)
{
    const string str = destination_mailbox;
    send_message(str, type, data);
}
149

150 151 152 153 154 155
void dsend_message(const char *destination_mailbox, IPMessageType type, void *data)
{
    const string str = destination_mailbox;
    dsend_message(str, type, data);
}

156 157
IPMessage::~IPMessage()
{
158 159
    // Do not remove the switch. If one adds a new IPMessageType but forgets to handle it in the
    // switch, a compilation warning should help avoiding this bug.
160 161 162 163 164 165 166
    switch (type)
    {
        case IPMessageType::JOB_SUBMITTED:
        {
            JobSubmittedMessage * msg = (JobSubmittedMessage *) data;
            delete msg;
        } break;
167 168 169 170 171
        case IPMessageType::JOB_SUBMITTED_BY_DP:
        {
            JobSubmittedByDPMessage * msg = (JobSubmittedByDPMessage *) data;
            delete msg;
        } break;
172 173 174 175 176
        case IPMessageType::PROFILE_SUBMITTED_BY_DP:
        {
            ProfileSubmittedByDPMessage * msg = (ProfileSubmittedByDPMessage *) data;
            delete msg;
        } break;
177 178 179 180 181 182 183 184 185 186
        case IPMessageType::JOB_COMPLETED:
        {
            JobCompletedMessage * msg = (JobCompletedMessage *) data;
            delete msg;
        } break;
        case IPMessageType::PSTATE_MODIFICATION:
        {
            PStateModificationMessage * msg = (PStateModificationMessage *) data;
            delete msg;
        } break;
Millian Poquet's avatar
Millian Poquet committed
187
        case IPMessageType::SCHED_EXECUTE_JOB:
188
        {
189
            ExecuteJobMessage * msg = (ExecuteJobMessage *) data;
190
            // The Allocations themselves are not memory-deallocated there but at the end of the job execution.
191 192
            delete msg;
        } break;
193 194 195 196 197
        case IPMessageType::SCHED_CHANGE_JOB_STATE:
        {
            ChangeJobStateMessage * msg = (ChangeJobStateMessage *) data;
            delete msg;
        } break;
Millian Poquet's avatar
Millian Poquet committed
198
        case IPMessageType::SCHED_REJECT_JOB:
199 200 201 202
        {
            JobRejectedMessage * msg = (JobRejectedMessage *) data;
            delete msg;
        } break;
203 204 205 206 207
        case IPMessageType::SCHED_KILL_JOB:
        {
            KillJobMessage * msg = (KillJobMessage *) data;
            delete msg;
        } break;
Millian Poquet's avatar
Millian Poquet committed
208
        case IPMessageType::SCHED_CALL_ME_LATER:
209
        {
Millian Poquet's avatar
Millian Poquet committed
210
            CallMeLaterMessage * msg = (CallMeLaterMessage*) data;
211 212
            delete msg;
        } break;
213 214 215
        case IPMessageType::SCHED_TELL_ME_ENERGY:
        {
        } break;
Pierre-François's avatar
Pierre-François committed
216 217 218 219 220 221 222 223
        case IPMessageType::WAIT_QUERY:
        {
            WaitQueryMessage * msg = (WaitQueryMessage *) data;
            delete msg;
        } break;
        case IPMessageType::SCHED_WAIT_ANSWER:
        {
            SchedWaitAnswerMessage * msg = (SchedWaitAnswerMessage *) data;
224
            delete msg;
Pierre-François's avatar
Pierre-François committed
225
        } break;
226 227 228 229 230
        case IPMessageType::SCHED_READY:
        {
        } break;
        case IPMessageType::SUBMITTER_HELLO:
        {
231 232 233 234 235 236 237
            SubmitterHelloMessage * msg = (SubmitterHelloMessage *) data;
            delete msg;
        } break;
        case IPMessageType::SUBMITTER_CALLBACK:
        {
            SubmitterJobCompletionCallbackMessage * msg = (SubmitterJobCompletionCallbackMessage *) data;
            delete msg;
238 239 240
        } break;
        case IPMessageType::SUBMITTER_BYE:
        {
241 242
            SubmitterByeMessage * msg = (SubmitterByeMessage *) data;
            delete msg;
243 244 245
        } break;
        case IPMessageType::SWITCHED_ON:
        {
246
            SwitchMessage * msg = (SwitchMessage *) data;
247 248 249 250
            delete msg;
        } break;
        case IPMessageType::SWITCHED_OFF:
        {
251
            SwitchMessage * msg = (SwitchMessage *) data;
252 253
            delete msg;
        } break;
254 255 256
        case IPMessageType::WAITING_DONE:
        {
        } break;
257 258 259 260 261
        case IPMessageType::KILLING_DONE:
        {
            KillingDoneMessage * msg = (KillingDoneMessage *) data;
            delete msg;
        } break;
262 263 264
        case IPMessageType::END_DYNAMIC_SUBMIT:
        {
        } break;
265 266 267
        case IPMessageType::CONTINUE_DYNAMIC_SUBMIT:
        {
        } break;
268 269 270 271 272
        case IPMessageType::TO_JOB_MSG:
        {
            ToJobMessage * msg = (ToJobMessage *) data;
            delete msg;
        } break;
273 274 275 276 277
        case IPMessageType::FROM_JOB_MSG:
        {
            FromJobMessage * msg = (FromJobMessage *) data;
            delete msg;
        } break;
278 279 280 281
    }

    data = nullptr;
}
282

283
JobIdentifier::JobIdentifier(const string &workload_name, const string &job_number) :
284
    workload_name(workload_name),
285
    job_name(job_number)
286 287 288 289
{

}

290 291
string JobIdentifier::to_string() const
{
292
    return workload_name + '!' + job_name;
293
}
294 295 296 297 298

bool operator<(const JobIdentifier &ji1, const JobIdentifier &ji2)
{
    return ji1.to_string() < ji2.to_string();
}