Subversion Repositories shark

Rev

Rev 1018 | Rev 1023 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
961 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2000,2002 Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 */
37
 
38
/* Interrupt Driver Module */
39
 
40
#include <intdrive/intdrive/intdrive.h>
1018 mauro 41
#include <intdrive/intdrive/inttask.h>
961 pj 42
#include <kernel/model.h>
43
#include <kernel/descr.h>
44
#include <kernel/var.h>
45
#include <kernel/func.h>
46
#include <tracer.h>
47
 
48
#include <ll/i386/64bit.h>
49
 
50
/*+ Status used in the level +*/
51
#define INTDRIVE_READY         MODULE_STATUS_BASE    /*+ - Ready status        +*/
52
#define INTDRIVE_WCET_VIOLATED MODULE_STATUS_BASE+2  /*+ when wcet is finished +*/
53
#define INTDRIVE_IDLE          MODULE_STATUS_BASE+3  /*+ to wait the replenish +*/
54
#define INTDRIVE_WAIT          MODULE_STATUS_BASE+4  /*+ to wait the activation */
55
 
56
//#define INTDRIVE_DEBUG
57
 
58
/*+ the level redefinition for the IntDrive +*/
59
typedef struct {
1021 mauro 60
        level_des l;     /*+ the standard level descriptor          +*/
961 pj 61
 
1021 mauro 62
        TIME replenish_period;
63
        TIME capacity;
64
        TIME q_theta;
961 pj 65
 
1021 mauro 66
        struct timespec act_time;
961 pj 67
 
1021 mauro 68
        int avail;
69
        int replenish_timer;
961 pj 70
 
1021 mauro 71
        //struct timespec replenish_expires;
72
        //int wcet_timer;
961 pj 73
 
1021 mauro 74
        int act_number;   /*+ the activation number                  +*/
961 pj 75
 
1021 mauro 76
        int flags;        /*+ the init flags...                      +*/
961 pj 77
 
1021 mauro 78
        bandwidth_t U;    /*+ the used bandwidth                     +*/
961 pj 79
 
80
} INTDRIVE_level_des;
81
 
82
PID INTDRIVE_task = NIL;
83
 
84
/* Replenish the capacity */
85
static void INTDRIVE_timer(void *arg)
86
{
1021 mauro 87
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(arg);
961 pj 88
 
1021 mauro 89
        lev->replenish_timer = NIL;
961 pj 90
 
1021 mauro 91
#ifdef INTDRIVE_DEBUG
92
        kern_printf("(INTD:TIMER)");
93
#endif
961 pj 94
 
1021 mauro 95
        if (INTDRIVE_task == NIL) return;
961 pj 96
 
1021 mauro 97
        lev->avail = lev->q_theta;
961 pj 98
 
1021 mauro 99
        TRACER_LOGEVENT(FTrace_EVT_user_event_0, 0, lev->avail + INT_MAX);
961 pj 100
 
1021 mauro 101
        switch (proc_table[INTDRIVE_task].status) {
102
                case INTDRIVE_IDLE:
103
                        if (lev->act_number) {
104
                                proc_table[INTDRIVE_task].status = INTDRIVE_READY;
105
                                event_need_reschedule();
106
                        } else {
107
                                proc_table[INTDRIVE_task].status = INTDRIVE_WAIT;
108
                        }
109
                        break;
110
        }
961 pj 111
}
112
 
113
/*static void INTDRIVE_wcet_timer(void *arg)
114
{
1021 mauro 115
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(arg);
961 pj 116
 
1021 mauro 117
        lev->wcet_timer = NIL;
961 pj 118
 
1021 mauro 119
        kern_raise(XWCET_VIOLATION,INTDRIVE_task);
961 pj 120
}*/
121
 
122
static PID INTDRIVE_public_scheduler(LEVEL l)
123
{
1021 mauro 124
        if (INTDRIVE_task == NIL) return NIL;
961 pj 125
 
1021 mauro 126
        if (proc_table[INTDRIVE_task].status == INTDRIVE_READY ||
127
            proc_table[INTDRIVE_task].status == EXE)
128
                return INTDRIVE_task;
129
        else
130
                return NIL;
961 pj 131
}
132
 
133
static int INTDRIVE_public_create(LEVEL l, PID p, TASK_MODEL *m)
134
{
1021 mauro 135
        HARD_TASK_MODEL *h;
961 pj 136
 
1021 mauro 137
        if (m->pclass != HARD_PCLASS) return -1;
138
        if (m->level != 0 && m->level != l) return -1;
139
        h = (HARD_TASK_MODEL *)m;
140
        if (!h->wcet && h->periodicity != INTDRIVE) return -1;
961 pj 141
 
1021 mauro 142
        if (INTDRIVE_task != NIL) return -1;
961 pj 143
 
1021 mauro 144
        INTDRIVE_task = p;
961 pj 145
 
1021 mauro 146
        proc_table[INTDRIVE_task].wcet = h->wcet;
147
        proc_table[INTDRIVE_task].avail_time = h->wcet;
148
        proc_table[INTDRIVE_task].status = INTDRIVE_WAIT;
149
        proc_table[INTDRIVE_task].control &= ~CONTROL_CAP;
961 pj 150
 
1021 mauro 151
        return 0;
961 pj 152
}
153
 
154
static void INTDRIVE_public_dispatch(LEVEL l, PID p, int nostop)
155
{
1021 mauro 156
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
157
        //struct timespec time;
961 pj 158
 
1021 mauro 159
        kern_gettime(&(lev->act_time));
961 pj 160
 
1021 mauro 161
        /*TIMESPEC_ASSIGN(&time,&(lev->act_time));
162
        ADDUSEC2TIMESPEC(proc_table[INTDRIVE_task].wcet,&time);
961 pj 163
 
1021 mauro 164
        if (lev->flags == INTDRIVE_CHECK_WCET)
165
                lev->wcet_timer = kern_event_post(&time,INTDRIVE_wcet_timer,(void *)lev);*/  
961 pj 166
}
167
 
168
static void INTDRIVE_public_epilogue(LEVEL l, PID p)
169
{
1021 mauro 170
        struct timespec time;
961 pj 171
 
1021 mauro 172
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
961 pj 173
 
1021 mauro 174
        /*if (lev->wcet_timer != NIL)
175
                kern_event_delete(lev->wcet_timer);*/
961 pj 176
 
1021 mauro 177
        SUBTIMESPEC(&schedule_time, &(lev->act_time), &time);
178
        lev->avail -= TIMESPEC2USEC(&time);
179
        TRACER_LOGEVENT(FTrace_EVT_user_event_0, 0, lev->avail + INT_MAX);
180
 
181
        if (proc_table[INTDRIVE_task].wcet < TIMESPEC2USEC(&time)) {
182
                kern_raise(XWCET_VIOLATION,INTDRIVE_task);    
183
        }
961 pj 184
}
185
 
186
static void INTDRIVE_public_activate(LEVEL l, PID p, struct timespec *t)
187
{
1021 mauro 188
        struct timespec acttime;
189
        TIME time, delta_capacity;
961 pj 190
 
1021 mauro 191
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
961 pj 192
 
1021 mauro 193
        if (proc_table[INTDRIVE_task].status == INTDRIVE_WAIT) {
194
                proc_table[INTDRIVE_task].status = INTDRIVE_READY;
195
                lev->act_number++;     
196
        } else {
197
                if (proc_table[INTDRIVE_task].status == INTDRIVE_IDLE ||
198
                    proc_table[INTDRIVE_task].status == INTDRIVE_READY ||
199
                    proc_table[INTDRIVE_task].status == EXE) {
200
#ifdef INTDRIVE_DEBUG
201
                        kern_printf("(INTD:WAIT_REC)");
202
#endif
203
                        lev->act_number++;
204
                }
205
        }
961 pj 206
 
1021 mauro 207
        if (lev->replenish_timer == NIL) {
208
                delta_capacity = lev->q_theta - lev->avail;
209
                mul32div32to32(delta_capacity, MAX_BANDWIDTH, lev->U, time);
210
                kern_gettime(&acttime);
211
                ADDUSEC2TIMESPEC(time,&acttime);
212
                lev->replenish_timer = kern_event_post(&acttime,INTDRIVE_timer,(void *)lev);
961 pj 213
 
1021 mauro 214
                /*kern_gettime(&(lev->replenish_expires));
215
                ADDUSEC2TIMESPEC(lev->replenish_period,&(lev->replenish_expires));
216
                lev->replenish_timer = kern_event_post(&(lev->replenish_expires),INTDRIVE_timer,(void *)lev);*/
217
        }
961 pj 218
}
219
 
220
static void INTDRIVE_public_unblock(LEVEL l, PID p)
221
{
1021 mauro 222
        /* Insert task in the correct position */
223
        proc_table[INTDRIVE_task].status = INTDRIVE_READY;
961 pj 224
}
225
 
226
static void INTDRIVE_public_block(LEVEL l, PID p)
227
{
228
 
229
}
230
 
231
static int INTDRIVE_public_message(LEVEL l, PID p, void *m)
232
{
1021 mauro 233
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
234
        struct timespec time, acttime;
235
        //int delta_time;
236
        TIME delta_capacity, delta_time;
961 pj 237
 
1021 mauro 238
        lev->act_number--;
961 pj 239
 
1021 mauro 240
        /*if (lev->wcet_timer != NIL)
241
        kern_event_delete(lev->wcet_timer);*/
961 pj 242
 
1021 mauro 243
        kern_gettime(&acttime);
244
        SUBTIMESPEC(&acttime, &(lev->act_time), &time);
245
        delta_time = TIMESPEC2USEC(&time);
246
        mul32div32to32(delta_time, (MAX_BANDWIDTH-lev->U), MAX_BANDWIDTH, delta_capacity);
247
        lev->avail -= delta_capacity;
1018 mauro 248
 
1021 mauro 249
        //lev->avail -= TIMESPEC2USEC(&time);
961 pj 250
 
1021 mauro 251
        TRACER_LOGEVENT(FTrace_EVT_user_event_0, 0, lev->avail + INT_MAX);
961 pj 252
 
1021 mauro 253
#ifdef INTDRIVE_DEBUG
254
        kern_printf("(INTD:AV:%d)",(int)(lev->avail));
255
#endif
961 pj 256
 
1021 mauro 257
        if (lev->avail < 0) {
258
                proc_table[INTDRIVE_task].status = INTDRIVE_IDLE;
961 pj 259
 
1021 mauro 260
                if (lev->replenish_timer != NIL)
261
                        kern_event_delete(lev->replenish_timer);
961 pj 262
 
1021 mauro 263
                delta_capacity = lev->q_theta - lev->avail;
264
                mul32div32to32(delta_capacity, MAX_BANDWIDTH, lev->U, delta_time);
265
                kern_gettime(&acttime);
266
                ADDUSEC2TIMESPEC(delta_time,&acttime);
267
                lev->replenish_timer = kern_event_post(&acttime,INTDRIVE_timer,(void *)lev);
961 pj 268
 
1021 mauro 269
                /*temp = -lev->avail;
270
                mul32div32to32(temp,lev->replenish_period,lev->capacity,delta_time)
271
                ADDUSEC2TIMESPEC(delta_time,&(lev->replenish_expires));
272
                lev->replenish_timer = kern_event_post(&(lev->replenish_expires),INTDRIVE_timer,(void *)lev);*/
961 pj 273
 
1021 mauro 274
#ifdef INTDRIVE_DEBUG
275
                kern_printf("(INTD:IDLE:%d)",delta_time);
276
#endif
277
        } else {
278
                if (lev->act_number) {
279
                        proc_table[INTDRIVE_task].status = INTDRIVE_READY;
280
#ifdef INTDRIVE_DEBUG
281
                        kern_printf("(INTD:NEXT_ACT)");
282
#endif
283
                } else {
284
#ifdef INTDRIVE_DEBUG
285
                        kern_printf("(INTD:WAIT_ACT)");
286
#endif
287
                        proc_table[INTDRIVE_task].status = INTDRIVE_WAIT;
288
                }
289
        }
961 pj 290
 
1021 mauro 291
        TRACER_LOGEVENT(FTrace_EVT_task_end_cycle, (unsigned short int)proc_table[INTDRIVE_task].context,(unsigned int)l);
961 pj 292
 
1021 mauro 293
        return 0;
961 pj 294
}
295
 
296
static void INTDRIVE_public_end(LEVEL l, PID p)
297
{
1021 mauro 298
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
961 pj 299
 
1021 mauro 300
        if (lev->replenish_timer != NIL)
301
                kern_event_delete(lev->replenish_timer);
961 pj 302
 
1021 mauro 303
        /*if (lev->wcet_timer != NIL)
304
                kern_event_delete(lev->wcet_timer);*/
961 pj 305
 
1021 mauro 306
        proc_table[INTDRIVE_task].status = INTDRIVE_IDLE;
961 pj 307
}
308
 
309
/* Registration functions */
310
 
311
/*+ Registration function: +*/
1004 mauro 312
LEVEL INTDRIVE_register_level(TIME capacity, TIME q_theta, int U, int flags)
961 pj 313
{
1021 mauro 314
        LEVEL l;            /* the level that we register */
315
        INTDRIVE_level_des *lev;
961 pj 316
 
1021 mauro 317
        printk("INTDRIVE_register_level\n");
961 pj 318
 
1021 mauro 319
        /* request an entry in the level_table */
320
        l = level_alloc_descriptor(sizeof(INTDRIVE_level_des));
961 pj 321
 
1021 mauro 322
        lev = (INTDRIVE_level_des *)level_table[l];
961 pj 323
 
1021 mauro 324
        lev->l.public_scheduler = INTDRIVE_public_scheduler;
325
        lev->l.public_guarantee = NULL;
326
        lev->l.public_create    = INTDRIVE_public_create;
327
        lev->l.public_end       = INTDRIVE_public_end;
328
        lev->l.public_dispatch  = INTDRIVE_public_dispatch;
329
        lev->l.public_epilogue  = INTDRIVE_public_epilogue;
330
        lev->l.public_activate  = INTDRIVE_public_activate;
331
        lev->l.public_unblock   = INTDRIVE_public_unblock;
332
        lev->l.public_block     = INTDRIVE_public_block;
333
        lev->l.public_message   = INTDRIVE_public_message;
961 pj 334
 
1021 mauro 335
        NULL_TIMESPEC(&(lev->act_time));
961 pj 336
 
1021 mauro 337
        lev->capacity = capacity;
338
        lev->replenish_timer = NIL;
339
        lev->flags = flags;
340
        lev->act_number = 0;
341
        lev->avail = 0;
342
        lev->q_theta = q_theta;
343
        lev->U = U;
344
        //mul32div32to32(MAX_BANDWIDTH,U,10000,lev->U);
345
#ifdef INTDRIVE_DEBUG
346
        printk("INTDRIVE Init: %d %d %d\n", lev->capacity, lev->q_theta, lev->U);
347
#endif
1018 mauro 348
 
1021 mauro 349
        //TODO !!! Fixing 10000 is not a clean way to work !!!
350
        if (intdrive_taskinit(l, 10000) == -1)
351
                printk("Error: INTDRIVE task not activated!\n");
961 pj 352
 
1021 mauro 353
        return l;
961 pj 354
}
355
 
356
bandwidth_t INTDRIVE_usedbandwidth(LEVEL l)
357
{
1021 mauro 358
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
961 pj 359
 
1021 mauro 360
        return lev->U;
961 pj 361
}
362
 
363
TIME INTDRIVE_set_q_theta(LEVEL l, TIME new_q_theta)
364
{
1021 mauro 365
        INTDRIVE_level_des *lev = (INTDRIVE_level_des *)(level_table[l]);
961 pj 366
 
1021 mauro 367
        lev->q_theta = new_q_theta;
368
        if (lev->q_theta < 0) lev->q_theta = 0;
369
        if (lev->q_theta > lev->capacity) lev->q_theta = lev->capacity;
370
 
371
        return lev->q_theta;
961 pj 372
}