| 1 |
807 |
markom |
/* sched.h -- Abstract entities header file handling job scheduler
|
| 2 |
|
|
Copyright (C) 2001 Marko Mlinar, markom@opencores.org
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
| 5 |
|
|
|
| 6 |
|
|
This program is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
| 9 |
|
|
(at your option) any later version.
|
| 10 |
|
|
|
| 11 |
|
|
This program is distributed in the hope that it will be useful,
|
| 12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 |
|
|
GNU General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with this program; if not, write to the Free Software
|
| 18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
| 19 |
|
|
|
| 20 |
|
|
#ifndef _SCHED_H_
|
| 21 |
|
|
#define _SCHED_H_
|
| 22 |
|
|
|
| 23 |
|
|
/* Scheduler debug level */
|
| 24 |
|
|
#define SCHED_DEBUG 0
|
| 25 |
|
|
|
| 26 |
|
|
#define SCHED_HEAP_SIZE 128
|
| 27 |
1319 |
phoenix |
#define SCHED_TIME_MAX 0x7fffffffffffffffLL
|
| 28 |
807 |
markom |
|
| 29 |
1319 |
phoenix |
|
| 30 |
807 |
markom |
/* Structure for holding one job entry */
|
| 31 |
|
|
struct sched_entry {
|
| 32 |
1319 |
phoenix |
long long time; /* Clock cycles before job starts */
|
| 33 |
807 |
markom |
int param; /* Parameter to pass to the function */
|
| 34 |
|
|
void (*func)(int); /* Function to call when time reaches 0 */
|
| 35 |
|
|
};
|
| 36 |
|
|
|
| 37 |
|
|
/* Heap of jobs */
|
| 38 |
|
|
struct scheduler_struct {
|
| 39 |
|
|
int size;
|
| 40 |
|
|
struct sched_entry heap[SCHED_HEAP_SIZE];
|
| 41 |
|
|
};
|
| 42 |
|
|
|
| 43 |
|
|
extern struct scheduler_struct scheduler;
|
| 44 |
|
|
|
| 45 |
|
|
/* Dummy function, representing a guard, which protects heap from
|
| 46 |
|
|
emptying */
|
| 47 |
|
|
extern void sched_guard (int i);
|
| 48 |
|
|
|
| 49 |
|
|
/* Init scheduler -- clear the heap */
|
| 50 |
|
|
#define SCHED_INIT() {\
|
| 51 |
|
|
scheduler.heap[1].func = sched_guard;\
|
| 52 |
1319 |
phoenix |
scheduler.heap[1].time = SCHED_TIME_MAX;\
|
| 53 |
807 |
markom |
scheduler.size = 2;\
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
#if SCHED_DEBUG > 1
|
| 57 |
|
|
#define SCHED_PRINT_JOBS() {\
|
| 58 |
|
|
int i;\
|
| 59 |
|
|
for (i = 1; i < scheduler.size; i++) \
|
| 60 |
997 |
markom |
PRINTF ("\t%i: %x $%i @ %i\n", i, scheduler.heap[i].func, scheduler.heap[i].param, scheduler.heap[i].time);\
|
| 61 |
807 |
markom |
}
|
| 62 |
|
|
#else
|
| 63 |
|
|
#define SCHED_PRINT_JOBS()
|
| 64 |
|
|
#endif
|
| 65 |
|
|
|
| 66 |
|
|
/* Adds new job to the queue */
|
| 67 |
|
|
#define SCHED_ADD(job_func, job_param, job_time) {\
|
| 68 |
821 |
markom |
int ___i;\
|
| 69 |
1308 |
phoenix |
if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_ADD(func %p, param %i, time %i)\n", __FUNCTION__, runtime.sim.cycles, (job_func), (job_param), (job_time));\
|
| 70 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
| 71 |
997 |
markom |
if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
|
| 72 |
821 |
markom |
___i = scheduler.size++;\
|
| 73 |
|
|
while (___i > 1 && scheduler.heap[___i / 2].time > (job_time)) scheduler.heap[___i] = scheduler.heap[___i /= 2];\
|
| 74 |
|
|
scheduler.heap[___i].func = (job_func);\
|
| 75 |
|
|
scheduler.heap[___i].param = (job_param);\
|
| 76 |
|
|
scheduler.heap[___i].time = (job_time);\
|
| 77 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
| 78 |
|
|
}
|
| 79 |
|
|
|
| 80 |
|
|
/* Removes an item from the heap */
|
| 81 |
|
|
#define SCHED_REMOVE_ITEM(index) {\
|
| 82 |
|
|
struct sched_entry *tmp;\
|
| 83 |
821 |
markom |
int ___i = (index), ___j;\
|
| 84 |
1308 |
phoenix |
if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_REMOVE%i(time %li)\n", __FUNCTION__, runtime.sim.cycles, (index), scheduler.heap[___i].time); \
|
| 85 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
| 86 |
997 |
markom |
if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
|
| 87 |
807 |
markom |
tmp = &scheduler.heap[--scheduler.size];\
|
| 88 |
821 |
markom |
while (___i <= scheduler.size / 2) {\
|
| 89 |
|
|
___j = 2 * ___i;\
|
| 90 |
|
|
if (___j < scheduler.size && scheduler.heap[___j].time > scheduler.heap[___j + 1].time) ___j++;\
|
| 91 |
|
|
if (scheduler.heap[___j].time >= tmp->time) break;\
|
| 92 |
|
|
scheduler.heap[___i] = scheduler.heap[___j];\
|
| 93 |
|
|
___i = ___j;\
|
| 94 |
807 |
markom |
}\
|
| 95 |
821 |
markom |
scheduler.heap[___i] = *tmp;\
|
| 96 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
/* Removes first item from the heap */
|
| 100 |
|
|
#define SCHED_REMOVE() SCHED_REMOVE_ITEM(1)
|
| 101 |
|
|
|
| 102 |
|
|
/* Returns item with lowest time in the heap */
|
| 103 |
|
|
#define SCHED_PEEK() scheduler.heap[1]
|
| 104 |
|
|
|
| 105 |
|
|
/* Returns a job with specified function and param, NULL if not found */
|
| 106 |
|
|
#define SCHED_FIND(f, p) ({\
|
| 107 |
|
|
int i;\
|
| 108 |
|
|
struct sched_entry *t = NULL;\
|
| 109 |
|
|
for (i = 1; i < scheduler.size; i++)\
|
| 110 |
|
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
| 111 |
|
|
t = &scheduler.heap[i]; break;\
|
| 112 |
|
|
}\
|
| 113 |
|
|
tmp;\
|
| 114 |
|
|
})
|
| 115 |
|
|
|
| 116 |
|
|
/* Returns a index of the job with specified function and param, 0 if not found */
|
| 117 |
|
|
#define SCHED_FIND_INDEX(f, p) ({\
|
| 118 |
|
|
int i, found = 0;\
|
| 119 |
|
|
for (i = 1; i < scheduler.size; i++)\
|
| 120 |
|
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
| 121 |
|
|
found = i; break;\
|
| 122 |
|
|
}\
|
| 123 |
|
|
found;\
|
| 124 |
|
|
})
|
| 125 |
|
|
|
| 126 |
|
|
/* Deletes job with specified function and param */
|
| 127 |
|
|
#define SCHED_FIND_REMOVE(f, p) {\
|
| 128 |
|
|
int index;\
|
| 129 |
|
|
index = SCHED_FIND_INDEX(f, p);\
|
| 130 |
|
|
if (index) SCHED_REMOVE_ITEM(index);\
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
#endif /* _SCHED_H_ */
|