OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [support/] [sched.h] - Diff between revs 1308 and 1319

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1308 Rev 1319
/* 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
 
 
 
 
/* Structure for holding one job entry */
/* Structure for holding one job entry */
struct sched_entry {
struct sched_entry {
  long time;          /* Clock cycles before job starts */
  long long time;     /* Clock cycles before job starts */
  int  param;         /* Parameter to pass to the function */
  int  param;         /* Parameter to pass to the function */
  void (*func)(int);  /* Function to call when time reaches 0 */
  void (*func)(int);  /* 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 (int i);
 
 
/* 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 = INT_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) {\
#define SCHED_ADD(job_func, job_param, job_time) {\
        int ___i;\
        int ___i;\
        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));\
        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();\
        SCHED_PRINT_JOBS();\
        if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
        if (SCHED_DEBUG > 1) PRINTF ("--------\n");\
        ___i = scheduler.size++;\
        ___i = scheduler.size++;\
        while (___i > 1 && scheduler.heap[___i / 2].time > (job_time)) scheduler.heap[___i] = scheduler.heap[___i /= 2];\
        while (___i > 1 && scheduler.heap[___i / 2].time > (job_time)) scheduler.heap[___i] = scheduler.heap[___i /= 2];\
        scheduler.heap[___i].func = (job_func);\
        scheduler.heap[___i].func = (job_func);\
        scheduler.heap[___i].param = (job_param);\
        scheduler.heap[___i].param = (job_param);\
        scheduler.heap[___i].time = (job_time);\
        scheduler.heap[___i].time = (job_time);\
        SCHED_PRINT_JOBS();\
        SCHED_PRINT_JOBS();\
}
}
 
 
/* 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_ */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.