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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc2/] [or1ksim/] [support/] [sched.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 807 markom
/* sched.c -- Abstract entities, handling job scheduling
2 1748 jeremybenn
 
3 807 markom
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
4 1748 jeremybenn
   Copyright (C) 2008 Embecosm Limited
5 807 markom
 
6 1748 jeremybenn
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
7 807 markom
 
8 1748 jeremybenn
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
9 807 markom
 
10 1748 jeremybenn
   This program is free software; you can redistribute it and/or modify it
11
   under the terms of the GNU General Public License as published by the Free
12
   Software Foundation; either version 3 of the License, or (at your option)
13
   any later version.
14 807 markom
 
15 1748 jeremybenn
   This program is distributed in the hope that it will be useful, but WITHOUT
16
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
18
   more details.
19 807 markom
 
20 1748 jeremybenn
   You should have received a copy of the GNU General Public License along
21
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
22 807 markom
 
23 1748 jeremybenn
/* This program is commented throughout in a fashion suitable for processing
24
   with Doxygen. */
25
 
26
 
27
/* Autoconf and/or portability configuration */
28
#include "config.h"
29
#include "port.h"
30
 
31
/* System includes */
32 807 markom
#include <stdlib.h>
33
#include <stdio.h>
34
#include <limits.h>
35
 
36 1748 jeremybenn
/* Package includes */
37 807 markom
#include "sched.h"
38 1688 nogj
#include "debug.h"
39 1748 jeremybenn
#include "sim-config.h"
40 807 markom
 
41 1748 jeremybenn
 
42 1688 nogj
DECLARE_DEBUG_CHANNEL(sched_jobs);
43
 
44
#define SCHED_HEAP_SIZE 128
45
#define SCHED_TIME_MAX  INT32_MAX
46
 
47 1557 nogj
/* FIXME: Scheduler should continue from previous cycles not current ones */
48
 
49 807 markom
struct scheduler_struct scheduler;
50
 
51
/* Dummy function, representing a guard, which protects heap from
52
   emptying */
53 1365 nogj
void sched_guard (void *dat)
54 807 markom
{
55 1390 nogj
  if(scheduler.job_queue)
56
    SCHED_ADD(sched_guard, dat, SCHED_TIME_MAX);
57
  else {
58
    scheduler.job_queue = scheduler.free_job_queue;
59
    scheduler.free_job_queue = scheduler.free_job_queue->next;
60
    scheduler.job_queue->next = NULL;
61
    scheduler.job_queue->func = sched_guard;
62
    scheduler.job_queue->time = SCHED_TIME_MAX;
63
  }
64 807 markom
}
65 1390 nogj
 
66
void sched_reset(void)
67
{
68
  struct sched_entry *cur, *next;
69
 
70
  for(cur = scheduler.job_queue; cur; cur = next) {
71
    next = cur->next;
72
    cur->next = scheduler.free_job_queue;
73
    scheduler.free_job_queue = cur;
74
  }
75
  scheduler.job_queue = NULL;
76
  sched_guard(NULL);
77
}
78
 
79
void sched_init(void)
80
{
81
  int i;
82
  struct sched_entry *new;
83
 
84
  scheduler.free_job_queue = NULL;
85
 
86
  for(i = 0; i < SCHED_HEAP_SIZE; i++) {
87
    if(!(new = malloc(sizeof(struct sched_entry)))) {
88
      fprintf(stderr, "Out-of-memory while allocateing scheduler queue\n");
89
      exit(1);
90
    }
91
    new->next = scheduler.free_job_queue;
92
    scheduler.free_job_queue = new;
93
  }
94
  scheduler.job_queue = NULL;
95
  sched_guard(NULL);
96
}
97 1545 nogj
 
98 1689 nogj
/* Executes jobs in time queue */
99
void do_scheduler(void)
100
{
101
  struct sched_entry *tmp;
102
 
103
  /* Execute all jobs till now */
104
  do {
105
    tmp = scheduler.job_queue;
106
    scheduler.job_queue = tmp->next;
107
    tmp->next = scheduler.free_job_queue;
108
    scheduler.free_job_queue = tmp;
109
 
110
    scheduler.job_queue->time += tmp->time;
111
 
112
    tmp->func (tmp->param);
113
  } while(scheduler.job_queue->time <= 0);
114
}
115
 
116 1688 nogj
/* Adds new job to the queue */
117
void sched_add(void (*job_func)(void *), void *job_param, int32_t job_time,
118
               const char *func)
119
{
120
  struct sched_entry *cur, *prev, *new_job;
121
  int32_t alltime;
122
 
123
  cur = scheduler.job_queue;
124
  prev = NULL;
125
  alltime = cur->time;
126
  while(cur && (alltime < job_time)) {
127
    prev = cur;
128
    cur = cur->next;
129
    if(cur)
130
      alltime += cur->time;
131
  }
132
 
133
  new_job = scheduler.free_job_queue;
134
  scheduler.free_job_queue = new_job->next;
135
  new_job->next = cur;
136
 
137
  new_job->func = job_func;
138
  new_job->param = job_param;
139
 
140
  if(prev) {
141
    new_job->time = job_time - (alltime - (cur ? cur->time : 0));
142
    prev->next = new_job;
143
  } else {
144
    scheduler.job_queue = new_job;
145
    new_job->time = job_time >= 0 ? job_time : cur->time;
146
  }
147
 
148
  if(cur)
149
    cur->time -= new_job->time;
150
 
151
}
152
 
153
/* Returns a job with specified function and param, NULL if not found */
154 1751 jeremybenn
void sched_find_remove(void (*job_func)(void *), void *dat)
155 1688 nogj
{
156
  struct sched_entry *cur;
157
  struct sched_entry *prev = NULL;
158
 
159
  for (cur = scheduler.job_queue; cur; prev = cur, cur = cur->next) {
160
    if ((cur->func == job_func) && (cur->param == dat)) {
161
      if(cur->next)
162
        cur->next->time += cur->time;
163
 
164
      if(prev)
165
        prev->next = cur->next;
166
      else
167
        scheduler.job_queue = cur->next;
168
      cur->next = scheduler.free_job_queue;
169
      scheduler.free_job_queue = cur;
170
      break;
171
    }
172
  }
173
}
174
 
175 1545 nogj
/* Schedules the next job so that it will run after the next instruction */
176
void sched_next_insn(void (*func)(void *), void *dat)
177
{
178
  int32_t cycles = 1;
179
  struct sched_entry *cur = scheduler.job_queue;
180
 
181
  /* The cycles count of the jobs may go into negatives.  If this happens, func
182
   * will get called before the next instruction has executed. */
183
  while(cur && (cur->time < 0)) {
184
    cycles -= cur->time;
185
    cur = cur->next;
186
  }
187
 
188
  SCHED_ADD(func, dat, cycles);
189
}
190
 
191
 

powered by: WebSVN 2.1.0

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