tasksetgen.py 10.3 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3

"""
Generates real time tasksets according to the method described in 'A Comparison
of Global and Partitioned EDF Schedulability Tests for Multiprocessors'.
6
Therefore tasks with random periods within the range 1 to 100 are generated.
7 8 9 10 11 12 13 14 15 16
Depending on the mode, the utilization of these tasks is either determined by
* uniform distribution between 1/period and 1
* bimodal distribution heavy tasks with uniform distribution between 0.5 / 1 and
light tasks uniform between 0.1 and 0.5 where the probability of being heavy
is 1/3
* exponential distribution with mean 0.25
* exponential distribution with mean 0.50
"""

import datetime
17
import shutil
18 19 20 21 22
import sys
import os
import random
import argparse
import json
23
import math
24
import copy
25 26
from enum import Enum

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
def query_yes_no(question, default=None):
    """
    Queries the user for a decision.
    """

    if default is None:
        prompt = ' [y/n]'
    elif default.lower() == 'yes':
        prompt = ' [Y/n]'
    elif default.lower() == 'no':
        prompt = ' [y/N]'
    else:
        raise ValueError('Invalid default answer {}'.format(default))

    while True:
        print(question, prompt, end='')
        choice = input().lower()
        if 'yes'.find(choice) == 0:
            return True
        elif 'no'.find(choice) == 0:
            return False

49
def gcd(a, b):
Tobias Langer committed
50 51
    if b == 0:
        return a
52 53
    return gcd(b, a % b)

54
def lcm(a, b):
55
    return (a * b) / gcd(a, b)
56 57

def hyperperiod(tasks):
Tobias Langer committed
58 59 60 61 62 63 64 65
    try:
        periods = [task.period for task in tasks]
        hyperperiod = periods[0]
        for period in periods[1:]:
            hyperperiod = lcm(hyperperiod, period)
        return hyperperiod
    except IndexError:
        return 0
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
class Timebase(Enum):
    seconds = 1
    milliseconds = 2
    microseconds = 3
    nanoseconds = 4

class Distribution(Enum):
    uniform = 1
    bimodal = 2
    exp_quart = 3
    exp_half = 4

class Task:
    """
    Representation of a generated task.
    """
    def __init__(self, wcet, period, deadline):
        self.wcet = int(wcet)
        self.period = int(period)
        self.deadline = int(deadline)

88
def calc_utilization(tasklist):
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    sum = 0
    for task in tasklist:
        sum += task.wcet / task.period
    return sum

def get_timebase_string(base):
    if base is Timebase.seconds:
        return 'std::chrono::seconds', 'embb::base::DurationSeconds'
    elif base is Timebase.milliseconds:
        return 'std::chrono::milliseconds', 'embb::base::DurationMilliseconds'
    elif base is Timebase.microseconds:
        return 'std::chrono::microseconds', 'embb::base::DurationMicroseconds'
    elif base is Timebase.nanoseconds:
        return 'std::chrono::nanoseconds', 'embb::base::DurationNanoseconds'

def create_task(distribution):
Tobias Langer committed
105
    period = random.uniform(10, 100)
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
    if distribution is Distribution.uniform:
        util = random.uniform(1.0 / period, 1.0)
    elif distribution is Distribution.bimodal:
        check = random.uniform(0.0, 1.0)
        if check < 1 / 3:
            util = random.uniform(0.5, 1.0)
        else:
            util = random.uniform(1.0 / period, 1.0)
    elif distribution is Distribution.exp_quart:
        util = random.expovariant(1.0 / 0.25)
    elif distribution is Distribution.exp_half:
        util = random.expovariant(1.0 / 0.5)
    util = min(1.0, max(0.0, util))
    wcet = period * util

    deadline = random.uniform(wcet, period)

    return Task(wcet, period, deadline)

def main():
    parser = argparse.ArgumentParser(description='Generate tasksets.')
    parser.add_argument('cores', type=int, help='Number of cores the taskset'
                                                'should be generated for.')
    parser.add_argument('tasksetcount', type=int, help='Number of tasksets that'
                                                       'should be generated.')
    parser.add_argument('target', type=str, help='Output directory.', nargs='?',
                        default='.')
Tobias Langer committed
133 134
    parser.add_argument('experiment_target', type=str, help='Output directory for experiments.', nargs='?',
                        default='.')
135

136
    parser.add_argument('--utilization', type=float, help='The utility for which tasksets should be generated. Is a range such as 2.5 3.5', nargs=2)
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 179 180 181 182 183
    parser.add_argument('--baseclock', type=str, default='system_clock',
                        nargs='?',
                        help='The clock which is to be used for the execution.')

    timebase_grp = parser.add_mutually_exclusive_group(required=True)
    timebase_grp.add_argument('--seconds', action='store_const',
                              const=Timebase.seconds)
    timebase_grp.add_argument('--milliseconds', action='store_const',
                              const=Timebase.milliseconds)
    timebase_grp.add_argument('--microseconds', action='store_const',
                              const=Timebase.microseconds)
    timebase_grp.add_argument('--nanoseconds', action='store_const',
                              const=Timebase.nanoseconds)

    distr_grp = parser.add_mutually_exclusive_group()
    distr_grp.add_argument('--uniform', action='store_const',
                           const=Distribution.uniform)
    distr_grp.add_argument('--bimodal', action='store_const',
                           const=Distribution.bimodal)
    distr_grp.add_argument('--exp_quart', action='store_const',
                           const=Distribution.exp_quart)
    distr_grp.add_argument('--exp_half', action='store_const',
                           const=Distribution.exp_half)

    args = parser.parse_args()

    if args.seconds is not None:
        timebase = args.seconds
    elif args.milliseconds is not None:
        timebase = args.milliseconds
    elif args.microseconds is not None:
        timebase = args.microseconds
    elif args.nanoseconds is not None:
        timebase = args.nanoseconds

    if args.uniform is not None:
        distribution = args.uniform
    elif args.bimodal is not None:
        distribution = args.bimodal
    elif args.exp_quart is not None:
        distribution = args.exp_quart
    elif args.exp_half is not None:
        distribution = args.exp_half
    else:
        distribution = Distribution.uniform

184 185
    utility = None
    try:
186
        utility = args.utilization
187 188 189
    except AttributeError:
        pass

190 191 192 193
    print('Generating tasks…', file=sys.stderr)
    tasksets = []
    while len(tasksets) < args.tasksetcount:
        taskset = []
194 195 196 197 198 199 200 201
        if utility is None:
            while len(taskset) < args.cores + 1 or \
                  calc_utilization(taskset) <= args.cores and \
                  hyperperiod(taskset) < 300000: # Limit hyperperiod to 5 minutes
                taskset.append(create_task(distribution))
                if len(taskset) >= args.cores + 1:
                    tasksets.append(taskset)
        else:
202
            taskset_util = 0
203
            while taskset_util < utility[1] and hyperperiod(taskset) < 300000:
204
                taskset.append(create_task(distribution))
205
                taskset_util = calc_utilization(taskset)
206
                if utility[0] <= taskset_util <= utility[1]:
207 208
                    if len(taskset) >= args.cores + 1:
                        tasksets.append(taskset)
209
                        taskset = copy.deepcopy(taskset)
210 211 212 213 214

    now = datetime.datetime.now()

    cpp_base, embb_base = get_timebase_string(timebase)

215 216 217 218 219 220 221 222
    try:
        os.makedirs(args.target)
    except FileExistsError:
        if not query_yes_no('Folder exists, remove?', default='yes'):
            sys.exit(1)
    shutil.rmtree(args.target)
    os.makedirs(args.target)

223
    print('Writing tasks…', file=sys.stderr)
224
    taken_names={}
225 226 227 228 229 230 231 232 233
    for taskset in tasksets:
        task_out = []
        for task in taskset:
            out = {}
            out['wcet'] = task.wcet
            out['period'] = task.period
            out['deadline'] = task.deadline
            task_out.append(out)

234
        utilization = str(round(calc_utilization(taskset), 2))
235 236
        utilization.replace('.', '_')

237
        name = 'taskset_{}_{}_{}'.format(now.strftime('%Y_%m_%d_%H_%M_%S'),
238 239
                                                len(taskset),
                                                utilization)
240 241 242 243 244 245
        try:
            taken_names[name] += 1
        except KeyError:
            taken_names[name] = 1

        name += '_{}'.format(taken_names[name])
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

        out = {}
        out['name'] = name
        out['template'] = 'templates/normal/'
        out['includes'] = [ { 'name' : '<embb/base/duration.h>' } ]
        out['cpp_time_base'] = cpp_base
        out['embb_time_base'] = embb_base
        out['base_clock'] = 'std::chrono::system_clock'
        out['data_description'] = [
                                    { 'name' : 'task',
                                      'fields' : [
                                                    {
                                                        'type' : 'int',
                                                        'name' : 'wcet'
                                                    },
                                                    {
                                                        'type' : 'int',
                                                        'name' : 'period'
                                                    },
                                                    {
                                                        'type' : 'int',
                                                        'name' : 'deadline'
                                                    },
                                                    {
                                                        'type' : 'int',
                                                        'name' : 'count'
                                                    }
                                                 ]
                                    }
                                  ]
        out['data'] = [
                          {
                              'type' : 'task',
                              'name' : 'taskset',
                              'elem' : task_out
                          }
                      ]
Tobias Langer committed
283 284 285 286
        try:
            out['output'] = args.experiment_target
        except KeyError:
            pass
287 288 289 290 291 292 293

        outname = os.path.join(args.target, name + '.json')
        with open(outname, 'w') as outfile:
            outfile.write(json.dumps(out))

if __name__ == '__main__':
    main()