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 |
|
|
|
28 |
|
|
/* Structure for holding one job entry */
|
29 |
|
|
struct sched_entry {
|
30 |
|
|
long time; /* Clock cycles before job starts */
|
31 |
|
|
int param; /* Parameter to pass to the function */
|
32 |
|
|
void (*func)(int); /* Function to call when time reaches 0 */
|
33 |
|
|
};
|
34 |
|
|
|
35 |
|
|
/* Heap of jobs */
|
36 |
|
|
struct scheduler_struct {
|
37 |
|
|
int size;
|
38 |
|
|
struct sched_entry heap[SCHED_HEAP_SIZE];
|
39 |
|
|
};
|
40 |
|
|
|
41 |
|
|
extern struct scheduler_struct scheduler;
|
42 |
|
|
|
43 |
|
|
/* Dummy function, representing a guard, which protects heap from
|
44 |
|
|
emptying */
|
45 |
|
|
extern void sched_guard (int i);
|
46 |
|
|
|
47 |
|
|
/* Init scheduler -- clear the heap */
|
48 |
|
|
#define SCHED_INIT() {\
|
49 |
|
|
scheduler.heap[1].func = sched_guard;\
|
50 |
|
|
scheduler.heap[1].time = INT_MAX;\
|
51 |
|
|
scheduler.size = 2;\
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
#if SCHED_DEBUG > 1
|
55 |
|
|
#define SCHED_PRINT_JOBS() {\
|
56 |
|
|
int i;\
|
57 |
|
|
for (i = 1; i < scheduler.size; i++) \
|
58 |
|
|
printf ("\t%i: %x $%i @ %i\n", i, scheduler.heap[i].func, scheduler.heap[i].param, scheduler.heap[i].time);\
|
59 |
|
|
}
|
60 |
|
|
#else
|
61 |
|
|
#define SCHED_PRINT_JOBS()
|
62 |
|
|
#endif
|
63 |
|
|
|
64 |
|
|
/* Adds new job to the queue */
|
65 |
|
|
#define SCHED_ADD(job_func, job_param, job_time) {\
|
66 |
821 |
markom |
int ___i;\
|
67 |
884 |
markom |
if (SCHED_DEBUG > 0) printf ("%s@%i:SCHED_ADD(func %x, param %i, time %i)\n", __FUNCTION__, runtime.sim.cycles, (job_func), (job_param), (job_time));\
|
68 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
69 |
|
|
if (SCHED_DEBUG > 1) printf ("--------\n");\
|
70 |
821 |
markom |
___i = scheduler.size++;\
|
71 |
|
|
while (___i > 1 && scheduler.heap[___i / 2].time > (job_time)) scheduler.heap[___i] = scheduler.heap[___i /= 2];\
|
72 |
|
|
scheduler.heap[___i].func = (job_func);\
|
73 |
|
|
scheduler.heap[___i].param = (job_param);\
|
74 |
|
|
scheduler.heap[___i].time = (job_time);\
|
75 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
/* Removes an item from the heap */
|
79 |
|
|
#define SCHED_REMOVE_ITEM(index) {\
|
80 |
|
|
struct sched_entry *tmp;\
|
81 |
821 |
markom |
int ___i = (index), ___j;\
|
82 |
884 |
markom |
if (SCHED_DEBUG > 0) printf ("%s@%i:SCHED_REMOVE%i(time %i)\n", __FUNCTION__, runtime.sim.cycles, (index), scheduler.heap[___i].time); \
|
83 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
84 |
|
|
if (SCHED_DEBUG > 1) printf ("--------\n");\
|
85 |
|
|
tmp = &scheduler.heap[--scheduler.size];\
|
86 |
821 |
markom |
while (___i <= scheduler.size / 2) {\
|
87 |
|
|
___j = 2 * ___i;\
|
88 |
|
|
if (___j < scheduler.size && scheduler.heap[___j].time > scheduler.heap[___j + 1].time) ___j++;\
|
89 |
|
|
if (scheduler.heap[___j].time >= tmp->time) break;\
|
90 |
|
|
scheduler.heap[___i] = scheduler.heap[___j];\
|
91 |
|
|
___i = ___j;\
|
92 |
807 |
markom |
}\
|
93 |
821 |
markom |
scheduler.heap[___i] = *tmp;\
|
94 |
807 |
markom |
SCHED_PRINT_JOBS();\
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/* Removes first item from the heap */
|
98 |
|
|
#define SCHED_REMOVE() SCHED_REMOVE_ITEM(1)
|
99 |
|
|
|
100 |
|
|
/* Returns item with lowest time in the heap */
|
101 |
|
|
#define SCHED_PEEK() scheduler.heap[1]
|
102 |
|
|
|
103 |
|
|
/* Returns a job with specified function and param, NULL if not found */
|
104 |
|
|
#define SCHED_FIND(f, p) ({\
|
105 |
|
|
int i;\
|
106 |
|
|
struct sched_entry *t = NULL;\
|
107 |
|
|
for (i = 1; i < scheduler.size; i++)\
|
108 |
|
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
109 |
|
|
t = &scheduler.heap[i]; break;\
|
110 |
|
|
}\
|
111 |
|
|
tmp;\
|
112 |
|
|
})
|
113 |
|
|
|
114 |
|
|
/* Returns a index of the job with specified function and param, 0 if not found */
|
115 |
|
|
#define SCHED_FIND_INDEX(f, p) ({\
|
116 |
|
|
int i, found = 0;\
|
117 |
|
|
for (i = 1; i < scheduler.size; i++)\
|
118 |
|
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
119 |
|
|
found = i; break;\
|
120 |
|
|
}\
|
121 |
|
|
found;\
|
122 |
|
|
})
|
123 |
|
|
|
124 |
|
|
/* Deletes job with specified function and param */
|
125 |
|
|
#define SCHED_FIND_REMOVE(f, p) {\
|
126 |
|
|
int index;\
|
127 |
|
|
index = SCHED_FIND_INDEX(f, p);\
|
128 |
|
|
if (index) SCHED_REMOVE_ITEM(index);\
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
#endif /* _SCHED_H_ */
|