/* sched.h -- Abstract entities header file handling job scheduler
|
/* sched.h -- Abstract entities header file handling job scheduler
|
Copyright (C) 2001 Marko Mlinar, markom@opencores.org
|
Copyright (C) 2001 Marko Mlinar, markom@opencores.org
|
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 2 of the License, or
|
the Free Software Foundation; either version 2 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
|
|
#ifndef _SCHED_H_
|
#ifndef _SCHED_H_
|
#define _SCHED_H_
|
#define _SCHED_H_
|
|
|
/* Scheduler debug level */
|
/* Scheduler debug level */
|
#define SCHED_DEBUG 0
|
#define SCHED_DEBUG 0
|
|
|
#define SCHED_HEAP_SIZE 128
|
#define SCHED_HEAP_SIZE 128
|
#define SCHED_TIME_MAX 0x7fffffffffffffffLL
|
#define SCHED_TIME_MAX 0x7fffffffffffffffLL
|
|
|
|
|
/* Structure for holding one job entry */
|
/* Structure for holding one job entry */
|
struct sched_entry {
|
struct sched_entry {
|
long long time; /* Clock cycles before job starts */
|
long long time; /* Clock cycles before job starts */
|
int param; /* Parameter to pass to the function */
|
void *param; /* Parameter to pass to the function */
|
void (*func)(int); /* Function to call when time reaches 0 */
|
void (*func)(void *); /* Function to call when time reaches 0 */
|
};
|
};
|
|
|
/* Heap of jobs */
|
/* Heap of jobs */
|
struct scheduler_struct {
|
struct scheduler_struct {
|
int size;
|
int size;
|
struct sched_entry heap[SCHED_HEAP_SIZE];
|
struct sched_entry heap[SCHED_HEAP_SIZE];
|
};
|
};
|
|
|
extern struct scheduler_struct scheduler;
|
extern struct scheduler_struct scheduler;
|
|
|
/* Dummy function, representing a guard, which protects heap from
|
/* Dummy function, representing a guard, which protects heap from
|
emptying */
|
emptying */
|
extern void sched_guard (int i);
|
extern void sched_guard (void *);
|
|
|
/* Init scheduler -- clear the heap */
|
/* Init scheduler -- clear the heap */
|
#define SCHED_INIT() {\
|
#define SCHED_INIT() {\
|
scheduler.heap[1].func = sched_guard;\
|
scheduler.heap[1].func = sched_guard;\
|
scheduler.heap[1].time = SCHED_TIME_MAX;\
|
scheduler.heap[1].time = SCHED_TIME_MAX;\
|
scheduler.size = 2;\
|
scheduler.size = 2;\
|
}
|
}
|
|
|
#if SCHED_DEBUG > 1
|
#if SCHED_DEBUG > 1
|
#define SCHED_PRINT_JOBS() {\
|
#define SCHED_PRINT_JOBS() {\
|
int i;\
|
int i;\
|
for (i = 1; i < scheduler.size; i++) \
|
for (i = 1; i < scheduler.size; i++) \
|
PRINTF ("\t%i: %x $%i @ %i\n", i, scheduler.heap[i].func, scheduler.heap[i].param, scheduler.heap[i].time);\
|
PRINTF ("\t%i: %x $%i @ %i\n", i, scheduler.heap[i].func, scheduler.heap[i].param, scheduler.heap[i].time);\
|
}
|
}
|
#else
|
#else
|
#define SCHED_PRINT_JOBS()
|
#define SCHED_PRINT_JOBS()
|
#endif
|
#endif
|
|
|
/* Adds new job to the queue */
|
/* Adds new job to the queue */
|
#define SCHED_ADD(job_func, job_param, job_time) {\
|
static inline void sched_add(void (*job_func)(void *), void *job_param,
|
int ___i;\
|
long long job_time, const char *func)
|
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));\
|
{
|
SCHED_PRINT_JOBS();\
|
int i;
|
|
|
|
if (SCHED_DEBUG > 0)
|
|
PRINTF ("%s@%lli:SCHED_ADD(func %p, param %p, time %lli)\n", func,
|
|
runtime.sim.cycles, job_func, job_param, job_time);
|
|
SCHED_PRINT_JOBS();
|
|
|
if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
|
if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
|
___i = scheduler.size++;\
|
|
while (___i > 1 && scheduler.heap[___i / 2].time > (job_time)) scheduler.heap[___i] = scheduler.heap[___i /= 2];\
|
i = scheduler.size++;
|
scheduler.heap[___i].func = (job_func);\
|
while (i > 1 && scheduler.heap[i / 2].time > job_time) {
|
scheduler.heap[___i].param = (job_param);\
|
scheduler.heap[i] = scheduler.heap[i / 2];
|
scheduler.heap[___i].time = (job_time);\
|
i /= 2;
|
SCHED_PRINT_JOBS();\
|
|
}
|
}
|
|
|
|
scheduler.heap[i].func = job_func;
|
|
scheduler.heap[i].param = job_param;
|
|
scheduler.heap[i].time = job_time;
|
|
SCHED_PRINT_JOBS();
|
|
}
|
|
|
|
#define SCHED_ADD(job_func, job_param, job_time) sched_add(job_func, job_param, job_time, __FUNCTION__)
|
|
|
/* Removes an item from the heap */
|
/* Removes an item from the heap */
|
#define SCHED_REMOVE_ITEM(index) {\
|
#define SCHED_REMOVE_ITEM(index) {\
|
struct sched_entry *tmp;\
|
struct sched_entry *tmp;\
|
int ___i = (index), ___j;\
|
int ___i = (index), ___j;\
|
if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_REMOVE%i(time %li)\n", __FUNCTION__, runtime.sim.cycles, (index), scheduler.heap[___i].time); \
|
if (SCHED_DEBUG > 0) PRINTF ("%s@%i:SCHED_REMOVE%i(time %li)\n", __FUNCTION__, runtime.sim.cycles, (index), scheduler.heap[___i].time); \
|
SCHED_PRINT_JOBS();\
|
SCHED_PRINT_JOBS();\
|
if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
|
if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
|
tmp = &scheduler.heap[--scheduler.size];\
|
tmp = &scheduler.heap[--scheduler.size];\
|
while (___i <= scheduler.size / 2) {\
|
while (___i <= scheduler.size / 2) {\
|
___j = 2 * ___i;\
|
___j = 2 * ___i;\
|
if (___j < scheduler.size && scheduler.heap[___j].time > scheduler.heap[___j + 1].time) ___j++;\
|
if (___j < scheduler.size && scheduler.heap[___j].time > scheduler.heap[___j + 1].time) ___j++;\
|
if (scheduler.heap[___j].time >= tmp->time) break;\
|
if (scheduler.heap[___j].time >= tmp->time) break;\
|
scheduler.heap[___i] = scheduler.heap[___j];\
|
scheduler.heap[___i] = scheduler.heap[___j];\
|
___i = ___j;\
|
___i = ___j;\
|
}\
|
}\
|
scheduler.heap[___i] = *tmp;\
|
scheduler.heap[___i] = *tmp;\
|
SCHED_PRINT_JOBS();\
|
SCHED_PRINT_JOBS();\
|
}
|
}
|
|
|
/* Removes first item from the heap */
|
/* Removes first item from the heap */
|
#define SCHED_REMOVE() SCHED_REMOVE_ITEM(1)
|
#define SCHED_REMOVE() SCHED_REMOVE_ITEM(1)
|
|
|
/* Returns item with lowest time in the heap */
|
/* Returns item with lowest time in the heap */
|
#define SCHED_PEEK() scheduler.heap[1]
|
#define SCHED_PEEK() scheduler.heap[1]
|
|
|
/* Returns a job with specified function and param, NULL if not found */
|
/* Returns a job with specified function and param, NULL if not found */
|
#define SCHED_FIND(f, p) ({\
|
#define SCHED_FIND(f, p) ({\
|
int i;\
|
int i;\
|
struct sched_entry *t = NULL;\
|
struct sched_entry *t = NULL;\
|
for (i = 1; i < scheduler.size; i++)\
|
for (i = 1; i < scheduler.size; i++)\
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
t = &scheduler.heap[i]; break;\
|
t = &scheduler.heap[i]; break;\
|
}\
|
}\
|
tmp;\
|
tmp;\
|
})
|
})
|
|
|
/* Returns a index of the job with specified function and param, 0 if not found */
|
/* Returns a index of the job with specified function and param, 0 if not found */
|
#define SCHED_FIND_INDEX(f, p) ({\
|
#define SCHED_FIND_INDEX(f, p) ({\
|
int i, found = 0;\
|
int i, found = 0;\
|
for (i = 1; i < scheduler.size; i++)\
|
for (i = 1; i < scheduler.size; i++)\
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
if (scheduler.heap[i].func == (f) && scheduler.heap[i].param == (p)) {\
|
found = i; break;\
|
found = i; break;\
|
}\
|
}\
|
found;\
|
found;\
|
})
|
})
|
|
|
/* Deletes job with specified function and param */
|
/* Deletes job with specified function and param */
|
#define SCHED_FIND_REMOVE(f, p) {\
|
#define SCHED_FIND_REMOVE(f, p) {\
|
int index;\
|
int index;\
|
index = SCHED_FIND_INDEX(f, p);\
|
index = SCHED_FIND_INDEX(f, p);\
|
if (index) SCHED_REMOVE_ITEM(index);\
|
if (index) SCHED_REMOVE_ITEM(index);\
|
}
|
}
|
|
|
#endif /* _SCHED_H_ */
|
#endif /* _SCHED_H_ */
|
|
|