export.cpp 19.7 KB
Newer Older
1 2 3
#include "export.hpp"

#include <algorithm>
4 5
#include <fstream>

6 7 8 9 10
#include <stdlib.h>
#include <xbt.h>
#include <math.h>
#include <float.h>

11 12
#include "context.hpp"

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
using namespace std;

XBT_LOG_NEW_DEFAULT_CATEGORY(export, "export");

WriteBuffer::WriteBuffer(const std::string & filename, int bufferSize)
    : bufferSize(bufferSize)
{
    xbt_assert(bufferSize > 0, "Invalid buffer size (%d)", bufferSize);
    buffer = new char[bufferSize];

    f.open(filename, ios_base::trunc);
    xbt_assert(f.is_open(), "Cannot write file '%s'", filename.c_str());
}

WriteBuffer::~WriteBuffer()
{
    if (bufferPos > 0)
        flushBuffer();

    if (buffer != nullptr)
    {
        delete[] buffer;
        buffer = nullptr;
    }
}

void WriteBuffer::appendText(const char * text)
{
    const int textLength = strlen(text);

    // If the buffer is big enough
    if (bufferPos + textLength < bufferSize)
    {
        // Let's append the text to the buffer
        memcpy(buffer + bufferPos, text, textLength * sizeof(char));
        bufferPos += textLength;
    }
    else
    {
        // Let's write the current buffer content in the file
        flushBuffer();

        // Let's write the text in the buffer
        xbt_assert(textLength < bufferSize, "Text too large to fit in the buffer (%d required, only got %d)", textLength, bufferSize);
        memcpy(buffer, text, textLength * sizeof(char));
        bufferPos = textLength;
    }
}

void WriteBuffer::flushBuffer()
{
    f.write(buffer, bufferPos);
    bufferPos = 0;
}





72
PajeTracer::PajeTracer(bool logLaunchings) :
73
    _logLaunchings(logLaunchings)
74 75 76
{
    generateColors(64);
    shuffleColors();
77
}
78

79 80 81
void PajeTracer::setFilename(const string &filename)
{
    xbt_assert(_wbuf == nullptr);
82
    _wbuf = new WriteBuffer(filename);
83 84 85 86 87
}

PajeTracer::~PajeTracer()
{
    if (state != FINALIZED)
88
        fprintf(stderr, "Destruction of a PajeTracer object which has not been finalized. The corresponding trace file may be invalid.\n");
89

90
    if (_wbuf != nullptr)
91
    {
92 93
        delete _wbuf;
        _wbuf = nullptr;
94 95 96
    }
}

97
void PajeTracer::initialize(BatsimContext *context, double time)
98 99 100 101 102 103 104 105 106 107
{
    xbt_assert(state == UNINITIALIZED, "Bad PajeTracer::initialize call: the object is not UNINITIALIZED");

    // The buffer used to write lines in the file
    const int bufSize = 1024;
    char buf[bufSize];

    // Let's write the Pajé schedule header
    // Let's write the Pajé schedule header
    snprintf(buf, bufSize,
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
             "%%EventDef PajeDefineContainerType %d\n"
             "%% Type string\n"
             "%% Alias string\n"
             "%% Name string\n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeCreateContainer %d\n"
             "%% Time date \n"
             "%% Type string  \n"
             "%% Alias string \n"
             "%% Name string   \n"
             "%% Container string  \n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeDestroyContainer %d\n"
             "%% Time date\n"
             "%% Name string \n"
             "%% Type string\n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeDefineStateType %d\n"
             "%% Alias string \n"
             "%% Type string \n"
             "%% Name string \n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeDefineEntityValue %d\n"
             "%% Alias string  \n"
             "%% Type string  \n"
             "%% Name string  \n"
             "%% Color color \n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeSetState %d\n"
             "%% Time date  \n"
             "%% Type string  \n"
             "%% Container string  \n"
             "%% Value string  \n"
             "%%EndEventDef \n"
             "\n"
             "%%EventDef PajeDefineEventType %d\n"
             "%% Type string\n"
             "%% Alias string\n"
             "%% Name string\n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeNewEvent %d\n"
             "%% Time date\n"
             "%% Type string\n"
             "%% Container string\n"
             "%% Value string\n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeDefineVariableType %d\n"
             "%% Type string\n"
             "%% Alias string\n"
             "%% Name string\n"
             "%% Color string\n"
             "%%EndEventDef\n"
             "\n"
             "%%EventDef PajeSetVariable %d\n"
             "%% Time date\n"
             "%% Type string\n"
             "%% Container string\n"
             "%% Value double\n"
             "%%EndEventDef\n"
             "\n",
             DEFINE_CONTAINER_TYPE, CREATE_CONTAINER, DESTROY_CONTAINER,
             DEFINE_STATE_TYPE, DEFINE_ENTITY_VALUE, SET_STATE,
             DEFINE_EVENT_TYPE, NEW_EVENT, DEFINE_VARIABLE_TYPE,
             SET_VARIABLE);
179
    _wbuf->appendText(buf);
180 181 182

    // Let's create our container types
    snprintf(buf, bufSize,
183 184 185 186 187 188 189 190 191 192
             "# Container types creation\n"
             "%d 0 %s \"Machines\"\n"
             "%d %s %s \"Machine\"\n"
             "%d 0 %s \"Scheduler\"\n"
             "%d %s %s \"Killer\"\n"
             "\n",
             DEFINE_CONTAINER_TYPE,                rootType,
             DEFINE_CONTAINER_TYPE, rootType,      machineType,
             DEFINE_CONTAINER_TYPE,                schedulerType,
             DEFINE_CONTAINER_TYPE, schedulerType, killerType);
193
    _wbuf->appendText(buf);
194 195 196

    // Let's create our event types
    snprintf(buf, bufSize,
197 198 199 200 201 202
             "# Event types creation\n"
             "%d %s %s \"Job kill\"\n"
             "%d %s %s \"Job kill\"\n"
             "\n",
             DEFINE_EVENT_TYPE, killerType, killEventKiller,
             DEFINE_EVENT_TYPE, machineType, killEventMachine);
203
    _wbuf->appendText(buf);
204 205 206

    // Let's create our variable types
    snprintf(buf, bufSize,
207 208 209 210
             "# Variable types creation\n"
             "%d %s %s \"Utilization\" %s\n"
             "\n",
             DEFINE_VARIABLE_TYPE, schedulerType, utilizationVarType, utilizationColor);
211
    _wbuf->appendText(buf);
212 213

    snprintf(buf, bufSize,
214 215 216
             "# Containers creation\n"
             "%d %lf %s %s \"Machines\" 0\n",
             CREATE_CONTAINER, time, rootType, root);
217
    _wbuf->appendText(buf);
218

219
    for (const Machine * m : context->machines.machines())
220 221 222
    {
        // todo : clean machine name
        snprintf(buf, bufSize,
223 224
                 "%d %lf %s %s%d \"%s\" %s\n",
                 CREATE_CONTAINER, time, machineType,
225 226
                 machinePrefix, m->id,
                 m->name.c_str(), root);
227
        _wbuf->appendText(buf);
228 229 230
    }

    snprintf(buf, bufSize,
231 232 233 234 235
             "%d %lf %s %s \"Scheduler\" 0\n"
             "%d %lf %s %s \"Killer\" %s\n"
             "\n",
             CREATE_CONTAINER, time, schedulerType, scheduler,
             CREATE_CONTAINER, time, killerType, killer, scheduler);
236
    _wbuf->appendText(buf);
237 238 239

    // Let's declare that machines have a state
    snprintf(buf, bufSize,
240 241 242 243
             "# States creation\n"
             "%d %s %s \"Machine state\"\n"
             "\n",
             DEFINE_STATE_TYPE, machineState, machineType);
244
    _wbuf->appendText(buf);
245 246 247

    // Let's declare some machine states
    snprintf(buf, bufSize,
248 249 250 251 252 253 254
             "# Creation of the different values the machine state can be\n"
             "%d %s %s \"Waiting\" %s\n"
             "%d %s %s \"Launching\" %s\n"
             "\n"
             "# Begin of events\n",
             DEFINE_ENTITY_VALUE, mstateWaiting, machineState, waitingColor,
             DEFINE_ENTITY_VALUE, mstateLaunching, machineState, launchingColor);
255
    _wbuf->appendText(buf);
256 257

    // Let's set all the machines in waiting state
258
    for (const Machine * m : context->machines.machines())
259 260
    {
        snprintf(buf, bufSize,
261
                 "%d %lf %s %s%d %s\n",
262
                 SET_STATE, time, machineState, machinePrefix, m->id, mstateWaiting);
263
        _wbuf->appendText(buf);
264 265 266 267 268
    }

    state = INITIALIZED;
}

269
void PajeTracer::finalize(BatsimContext * context, double time)
270 271 272 273 274 275 276
{
    xbt_assert(state == INITIALIZED, "Bad PajeTracer::finalize call: the object has not been initialized yet");

    const int bufSize = 64;
    char buf[bufSize];

    snprintf(buf, bufSize,
277 278
             "\n"
             "# End of events, containers destruction\n");
279
    _wbuf->appendText(buf);
280

281
    for (const Machine * m : context->machines.machines())
282 283
    {
        snprintf(buf, bufSize,
284
                 "%d %lf %s%d %s\n",
285
                 DESTROY_CONTAINER, time, machinePrefix, m->id, machineType);
286
        _wbuf->appendText(buf);
287 288 289
    }

    snprintf(buf, bufSize,
290 291
             "%d %lf %s %s\n",
             DESTROY_CONTAINER, time, root, rootType);
292
    _wbuf->appendText(buf);
293 294 295 296 297 298

    state = FINALIZED;
}

void PajeTracer::addJobLaunching(int jobID, const std::vector<int> & usedMachineIDs, double time)
{
299
    (void) jobID;
300 301 302 303 304
    xbt_assert(state == INITIALIZED, "Bad addJobLaunching call: the PajeTracer object is not initialized or had been finalized");

    const int bufSize = 64;
    char buf[bufSize];

305
    if (_logLaunchings)
306 307 308 309 310
    {
        // Let's change the state of all the machines which launch the job
        for (const int & machineID : usedMachineIDs)
        {
            snprintf(buf, bufSize,
311 312
                     "%d %lf %s %s%d %s\n",
                     SET_STATE, time, machineState, machinePrefix, machineID, mstateLaunching);
313
            _wbuf->appendText(buf);
314 315
        }
    }
316 317 318 319 320 321 322 323 324 325
}

void PajeTracer::register_new_job(int jobID)
{
    xbt_assert(_jobs.find(jobID) == _jobs.end(), "Cannot register new job %d: it already exists", jobID);
    const int bufSize = 64;
    char buf[bufSize];

    // Let's create a state value corresponding to this job
    snprintf(buf, bufSize,
326 327
             "%d %s%d %s \"%d\" %s\n",
             DEFINE_ENTITY_VALUE, jobPrefix, jobID, machineState, jobID, _colors[jobID % (int)_colors.size()].c_str());
328 329 330 331 332 333 334 335 336 337
    _wbuf->appendText(buf);

    _jobs[jobID] = jobPrefix + to_string(jobID);
}

void PajeTracer::set_machine_idle(int machineID, double time)
{
    const int bufSize = 64;
    char buf[bufSize];
    snprintf(buf, bufSize,
338 339
             "%d %lf %s %s%d %s\n",
             SET_STATE, time, machineState, machinePrefix, machineID, mstateWaiting);
340 341 342 343 344 345 346 347 348 349 350
    _wbuf->appendText(buf);
}

void PajeTracer::set_machine_as_computing_job(int machineID, int jobID, double time)
{
    auto mit = _jobs.find(jobID);
    if (mit == _jobs.end())
    {
        register_new_job(jobID);
        mit = _jobs.find(jobID);
    }
351

352 353 354
    const int bufSize = 64;
    char buf[bufSize];
    snprintf(buf, bufSize,
355 356
             "%d %lf %s %s%d %s\n",
             SET_STATE, time, machineState, machinePrefix, machineID, mit->second.c_str());
357
    _wbuf->appendText(buf);
358 359 360 361 362 363 364 365 366 367 368 369 370
}

void PajeTracer::addJobRunning(int jobID, const vector<int> & usedMachineIDs, double time)
{
    xbt_assert(state == INITIALIZED, "Bad addJobLaunching call: the PajeTracer object is not initialized or had been finalized");

    const int bufSize = 64;
    char buf[bufSize];

    // Let's change the state of all the machines which run the job
    for (const int & machineID : usedMachineIDs)
    {
        snprintf(buf, bufSize,
371 372
                 "%d %lf %s %s%d %s%d\n",
                 SET_STATE, time, machineState, machinePrefix, machineID, jobPrefix, jobID);
373
        _wbuf->appendText(buf);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    }
}

void PajeTracer::addJobEnding(int jobID, const vector<int> & usedMachineIDs, double time)
{
    xbt_assert(state == INITIALIZED, "Bad addJobLaunching call: the PajeTracer object is not initialized or had been finalized");
    (void) jobID;

    const int bufSize = 64;
    char buf[bufSize];

    // Let's change the state of all the machines which run the job
    for (const int & machineID : usedMachineIDs)
    {
        snprintf(buf, bufSize,
389 390
                 "%d %lf %s %s%d %s\n",
                 SET_STATE, time, machineState, machinePrefix, machineID, mstateWaiting);
391
        _wbuf->appendText(buf);
392 393 394
    }
}

395
void PajeTracer::addJobKill(int jobID, const vector<int> & usedMachineIDs, double time, bool associateKillToMachines)
396 397 398 399 400 401 402 403
{
    xbt_assert(state == INITIALIZED, "Bad addJobKill call: the PajeTracer object is not initialized or had been finalized");

    const int bufSize = 64;
    char buf[bufSize];

    // Let's add a kill event associated with the scheduler
    snprintf(buf, bufSize,
404 405
             "%d %lf %s %s \"%d\"\n",
             NEW_EVENT, time, killEventKiller, killer, jobID);
406
    _wbuf->appendText(buf);
407

408
    if (associateKillToMachines)
409
    {
410 411 412 413
        // Let's add a kill event associated with each machine
        for (const int & machineID : usedMachineIDs)
        {
            snprintf(buf, bufSize,
414 415
                     "%d %lf %s %s%d \"%d\"\n",
                     NEW_EVENT, time, killEventMachine, machinePrefix, machineID, jobID);
416 417
            _wbuf->appendText(buf);
        }
418 419 420 421 422 423 424 425 426 427 428 429
    }
}

void PajeTracer::addGlobalUtilization(double utilization, double time)
{
    xbt_assert(state == INITIALIZED, "Bad addJobKill call: the PajeTracer object is not initialized or had been finalized");

    const int bufSize = 64;
    char buf[bufSize];

    // Let's set the variable state correctly
    snprintf(buf, bufSize,
430 431
             "%d %lf %s %s %lf\n",
             SET_VARIABLE, time, utilizationVarType, scheduler, utilization);
432
    _wbuf->appendText(buf);
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
}

void PajeTracer::generateColors(int colorCount)
{
    xbt_assert(colorCount > 0);

    double h, s=1, v=1, r, g, b;
    const int bufSize = 32;
    char buf[bufSize];

    double hueFraction = 360.0 / colorCount;
    for (int i = 0; i < colorCount; ++i)
    {
        h = i * hueFraction;
        hsvToRgb(h,s,v, r,g,b);

        snprintf(buf, bufSize, "\"%lf %lf %lf\"", r, g, b);
450
        _colors.push_back(buf);
451 452 453 454 455
    }
}

void PajeTracer::shuffleColors()
{
456
    random_shuffle(_colors.begin(), _colors.end());
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
}

void PajeTracer::hsvToRgb(double h, double s, double v, double & r, double & g, double & b)
{
    if (s == 0) // Achromatic (grey)
    {
        r = g = b = v;
        return;
    }

    h /= 60;            // sector 0 to 5
    int i = floor(h);
    float f = h-i;      // factorial part of h
    float p = v*(1-s);
    float q = v*(1-s*f);
    float t = v*(1-s*(1-f));

    switch(i)
    {
    case 0:
        r = v;
        g = t;
        b = p;
        break;
    case 1:
        r = q;
        g = v;
        b = p;
        break;
    case 2:
        r = p;
        g = v;
        b = t;
        break;
    case 3:
        r = p;
        g = q;
        b = v;
        break;
    case 4:
        r = t;
        g = p;
        b = v;
        break;
    default:    // case 5:
        r = v;
        g = p;
        b = q;
        break;
    }
}

509
void exportJobsToCSV(const string &filename, BatsimContext *context)
510
{
511 512 513 514
    ofstream f(filename, ios_base::trunc);
    xbt_assert(f.is_open(), "Cannot write file '%s'", filename.c_str());

    // write headers
515
    f << "jobID,submission_time,requested_number_of_processors,requested_time,success,starting_time,execution_time,finish_time,waiting_time,turnaround_time,stretch,consumed_energy,allocated_processors\n";
516 517 518

    const auto & jobs = context->jobs.jobs();
    for (const auto & mit : jobs)
519
    {
520
        Job * job = mit.second;
521

522
        if (job->state == JobState::JOB_STATE_COMPLETED_SUCCESSFULLY || job->state == JobState::JOB_STATE_COMPLETED_KILLED)
523 524
        {
            char * buf;
525
            int success = job->state == JobState::JOB_STATE_COMPLETED_SUCCESSFULLY;
526 527
            int ret = asprintf(&buf, "%d,%lf,%d,%lf,%d,%lf,%lf,%lf,%lf,%lf,%lf,%Lf,", // finished by a ',' because the next part is written after asprintf
                     job->id,
528 529 530 531 532 533 534 535 536
                     job->submission_time,
                     job->required_nb_res,
                     job->walltime,
                     success,
                     job->starting_time,
                     job->runtime,
                     job->starting_time + job->runtime, // finish_time
                     job->starting_time - job->submission_time, // waiting_time
                     job->starting_time + job->runtime - job->submission_time, // turnaround_time
537 538
                     (job->starting_time + job->runtime - job->submission_time) / job->runtime, // stretch
                     job->consumed_energy
539 540 541 542 543
                     );
            xbt_assert(ret != -1, "asprintf failed (not enough memory?)");
            f << buf;
            free(buf);

544 545
            // TODO: use boost::join for the following if

546
            if (job->required_nb_res > 0)
547
            {
548 549 550 551 552 553
                int ret = asprintf(&buf, "%d", job->allocation[0]);
                xbt_assert(ret != -1, "asprintf failed (not enough memory?)");
                f << buf;
                free(buf);

                for (int i = 1; i < job->required_nb_res; ++i)
554
                {
555
                    ret = asprintf(&buf," %d", job->allocation[i]);
556
                    xbt_assert(ret != -1, "asprintf failed (not enough memory?)");
557
                    f << buf;
558 559 560 561
                    free(buf);
                }
            }

562 563
            f << "\n";
        }
564
    }
565 566
}

567

568
void exportScheduleToCSV(const string &filename, double scheduling_time, BatsimContext *context)
569
{
570 571
    ofstream f(filename, ios_base::trunc);
    xbt_assert(f.is_open(), "Cannot write file '%s'", filename.c_str());
572

573
    f << "nb_jobs,nb_jobs_finished,nb_jobs_success,nb_jobs_killed,success_rate,makespan,max_turnaround_time,scheduling_time,jobs_execution_time_boundary_ratio,consumed_joules\n";
574

575 576 577 578 579 580 581 582
    int nb_jobs = 0;
    int nb_jobs_finished = 0;
    int nb_jobs_success = 0;
    int nb_jobs_killed = 0;
    double makespan = 0;
    double max_turnaround_time = 0;
    double min_job_execution_time = DBL_MAX;
    double max_job_execution_time = DBL_MIN;
583

584 585
    const auto & jobs = context->jobs.jobs();
    for (const auto & mit : jobs)
586
    {
587
        Job * job = mit.second;
588

589
        if (job->state == JobState::JOB_STATE_COMPLETED_SUCCESSFULLY || job->state == JobState::JOB_STATE_COMPLETED_KILLED)
590
        {
591
            nb_jobs_finished++;
592

593 594 595 596
            if (job->runtime < min_job_execution_time)
                min_job_execution_time = job->runtime;
            if (job->runtime > max_job_execution_time)
                max_job_execution_time = job->runtime;
597

598 599 600 601
            if (job->state == JobState::JOB_STATE_COMPLETED_SUCCESSFULLY)
                nb_jobs_success++;
            else
                nb_jobs_killed++;
602

603 604
            double completion_time = job->starting_time + job->runtime;
            double turnaround_time = job->starting_time + job->runtime - job->submission_time;
605

606 607
            if (completion_time > makespan)
                makespan = completion_time;
608

609 610
            if (turnaround_time > max_turnaround_time)
                max_turnaround_time = turnaround_time;
611 612
        }
    }
613

614
    long double total_consumed_energy = context->machines.total_consumed_energy(context);
615

616
    char * buf;
617
    int ret = asprintf(&buf, "%d,%d,%d,%d,%lf,%lf,%lf,%lf,%lf,%Lg\n",
618 619
                       nb_jobs, nb_jobs_finished, nb_jobs_success, nb_jobs_killed,
                       (double)nb_jobs_success/nb_jobs, makespan, max_turnaround_time,
620 621
                       scheduling_time, max_job_execution_time / min_job_execution_time,
                       total_consumed_energy);
622 623 624 625
    xbt_assert(ret != -1, "asprintf failed (not enough memory?)");

    f << buf;
    free(buf);
626
}
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

PStateChangeTracer::PStateChangeTracer()
{

}

void PStateChangeTracer::setFilename(const string &filename)
{
    xbt_assert(_wbuf == nullptr, "Double call of PStateChangeTracer::setFilename");
    _wbuf = new WriteBuffer(filename);

    _wbuf->appendText("time,machine_id,new_pstate\n");
}

PStateChangeTracer::~PStateChangeTracer()
{
    if (_wbuf != nullptr)
    {
        delete _wbuf;
        _wbuf = nullptr;
    }
}

void PStateChangeTracer::add_pstate_change(double time, int machine_id, int pstate_after)
{
    const int bufSize = 64;
    char buf[bufSize];

    snprintf(buf, bufSize, "%lg,%d,%d\n", time, machine_id, pstate_after);
    _wbuf->appendText(buf);
}