delayvector.cpp 3.21 KB
Newer Older
Ralf Stemmer's avatar
Ralf Stemmer committed
1 2 3 4 5 6 7 8 9 10 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
#include <delayvector.hpp>
#include <cstdlib>
#include <iostream>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_statistics_double.h>



DelayVector::DelayVector(const char *path, DISTRIBUTION distribution)
    : delayindex(0)
    , rng(NULL)
    , distribution(distribution)
{
    this->ReadDelayVector(path);
    this->rng = gsl_rng_alloc(gsl_rng_default);
    // By default, the random number generator gets seeded with 0
    // This shall not be changed to get reproducible results
}

DelayVector::DelayVector(std::string path, DISTRIBUTION distribution)
    : delayindex(0)
    , rng(NULL)
    , distribution(distribution)
{
    this->ReadDelayVector(path.c_str());
    this->rng = gsl_rng_alloc(gsl_rng_default);
    // By default, the random number generator gets seeded with 0
    // This shall not be changed to get reproducible results
}



sc_core::sc_time DelayVector::GetDelay()
{
    double delay;

    switch(this->distribution)
    {
        case DISTRIBUTION::INJECTED:
            delay = this->GetInjectedDelay();
            break;

        case DISTRIBUTION::GAUSSIAN:
            delay = this->GetGaussianDelay();
            break;

        case DISTRIBUTION::UNIFORM:
            delay = this->GetUniformDelay();
            break;
50 51 52 53

        case DISTRIBUTION::WCET:
            delay = this->GetWCETDelay();
            break;
Ralf Stemmer's avatar
Ralf Stemmer committed
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
    }

    return sc_core::sc_time(delay, sc_core::SC_NS);
}



double DelayVector::GetInjectedDelay()
{
    double delay = this->delayvector[this->delayindex];
    this->delayindex = (this->delayindex + 1) % this->delayvector.size();

    return delay;
}



double DelayVector::GetGaussianDelay()
{
    auto delay = gsl_ran_gaussian(this->rng, this->sigma);
    delay += this->mu;

    return delay;
}



double DelayVector::GetUniformDelay()
{
    auto delay = gsl_ran_flat(this->rng, this->BCET, this->WCET);

    return delay;
}



90 91 92 93 94 95 96
double DelayVector::GetWCETDelay()
{
    return this->WCET;
}



Ralf Stemmer's avatar
Ralf Stemmer committed
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 140 141 142 143 144 145
void DelayVector::ReadDelayVector(const char *path)
{
    std::ifstream ifs;
    ifs.open(path);

    // Initialize min/max with opposite extremes
    // to approach to the actual values from the file
    this->WCET = 0.0;
    this->BCET = std::numeric_limits<decltype(this->BCET)>::max();

    for(std::string line; std::getline(ifs, line); )
    {
        double time;
        try
        {
            time = std::stod(line);
        }
        catch(std::exception &e)
        {
            std::cerr << "Parsing line \""
                      << line
                      << "\" failed."
                      << "This line will be ignored!\n";
        }

        this->delayvector.emplace_back(time);
        if(time > this->WCET)
            this->WCET = time;
        if(time < this->BCET)
            this->BCET = time;

    }

    // Shuffle delay vector to make sure it is not just a repetition of the measured data
    std::random_shuffle(this->delayvector.begin(), this->delayvector.end());

    // Get further properties from the measured data
    double *data = this->delayvector.data();
    size_t  n    = this->delayvector.size();
    this->sigma  = gsl_stats_sd(  data, 1, n);
    this->mu     = gsl_stats_mean(data, 1, n);

    ifs.close();
    return;
}


// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4