summaryrefslogtreecommitdiff
path: root/viengoos/scheduler.c
blob: bdf735f18d05efbfe93916fc2ada0ebaeb845976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* scheduler.c - Scheduler implementation.
   Copyright (C) 2009 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.  */

#include "scheduler.h"
#include "thread.h"
#include "vm.h"
#include "debug.h"
#include "timer.h"

/* Set to 0 to show the thread state transitions.  */
#define SHOW_THREAD_STATE_TRANSITIONS 5

/* 100 milliseconds.  */
#define TS (100 * 1000)

struct thread *current_thread;
/* When CURRENT_THREAD was scheduled.  */
static uint64_t quantum_start;

static struct thread_queue_list run_queue;
static struct thread_queue_list no_time_queue;

static void
thread_remove_from_run_queue (struct thread *thread)
{
  assertx (thread->state == THREAD_RUNNABLE,
	   "%s", thread_state_string (thread->state));

  if (thread->time > 0)
    thread_queue_list_unlink (&run_queue, thread);
  else
    thread_queue_list_unlink (&no_time_queue, thread);
}

static void
thread_add_to_run_queue (struct thread *thread)
{
  assert (thread->state != THREAD_RUNNABLE);

  if (thread->time > 0)
    thread_queue_list_enqueue (&run_queue, thread);
  else
    thread_queue_list_enqueue (&no_time_queue, thread);

  thread->state = THREAD_RUNNABLE;
}

/* The tick the last time allowances were distributed.  */
static uint64_t last_payout;

static void
deschedule_current_thread (bool suspend)
{
  assert (current_thread);
  assert (current_thread->state == THREAD_RUNNING
	  || current_thread->state == THREAD_SUSPENDED);

  int delta = (time_data.ns_since_boot - quantum_start) / 1000;
  current_thread->time -= delta;
  current_thread->total_time += delta;
  if (suspend || current_thread->state == THREAD_SUSPENDED)
    {
      debug (SHOW_THREAD_STATE_TRANSITIONS, OBJECT_NAME_FMT ": %s -> %s",
	     OBJECT_NAME_PRINTF ((struct vg_object *) current_thread),
	     thread_state_string (current_thread->state),
	     thread_state_string (THREAD_SUSPENDED));

      current_thread->state = THREAD_SUSPENDED;
    }
  else if (current_thread->state == THREAD_RUNNING)
    {
      debug (SHOW_THREAD_STATE_TRANSITIONS, OBJECT_NAME_FMT ": %s -> %s",
	     OBJECT_NAME_PRINTF ((struct vg_object *) current_thread),
	     thread_state_string (current_thread->state),
	     thread_state_string (THREAD_RUNNABLE));

      thread_add_to_run_queue (current_thread);
    }

  current_thread = NULL;
}

void
schedule (struct thread *thread)
{
  if (likely (current_thread))
    /* See if the current thread has exceeded its allowance.  */
    {
      if (current_thread->state == THREAD_SUSPENDED)
	deschedule_current_thread (true);
      else if (unlikely ((int64_t) time_data.ns_since_boot - quantum_start
			 >= (int64_t) current_thread->time))
	{
	  assert (current_thread->state == THREAD_RUNNING);

	  debug (5, OBJECT_NAME_FMT ": Quantum up.",
		 OBJECT_NAME_PRINTF ((struct vg_object *) current_thread));
	  deschedule_current_thread (false);
	}
    }

  if (likely (current_thread)
      && (likely (! thread) || thread == current_thread))
    /* We don't have to switch and the current thread hasn't exceed
       its allowance.  Keep it on the CPU.  */
    return;

  if (thread)
    /* The caller specified a different thread to run.  Use it.  */
    {
      assert (thread->state == THREAD_RUNNABLE
	      || thread->state == THREAD_SUSPENDED);

      if (thread->state == THREAD_RUNNABLE)
	thread_remove_from_run_queue (thread);
      else
	/* The specified thread is not runnable.  */
	thread = NULL;
    }

  if (! thread)
    /* The caller didn't specify a thread to run.  Dequeue the next
       thread thread from the run queue.  */
    {
      thread = thread_queue_list_dequeue (&run_queue);
      if (thread)
	assert (thread->state == THREAD_RUNNABLE);
    }

  if (! thread)
    /* It seems that there are no threads on the run queue.  Refill
       all ready threads' allowances and move them to the run
       queue.  */
    {
      debug (5, "Allocating time at %ld.", time_data.ns_since_boot);

      assert (! current_thread);
      assert (! thread_queue_list_head (&run_queue));

      last_payout = time_data.ns_since_boot;

      struct thread *t;
      for (t = thread_queue_list_head (&no_time_queue);
	   t;
	   t = thread_queue_list_next (t))
	{
	  assert (t->state == THREAD_RUNNABLE);

	  assert (t->time <= 0);

	  t->time += TS;
	  if (t->time > TS + TS / 2)
	    /* Can hold over at most half a time slice.  */
	    t->time = TS + TS / 2;
	  if (t->time < TS / 10)
	    /* The minimum is a tenth of a slice. */
	    t->time = TS / 10;

	  t->last_payout = last_payout;
	}

      thread_queue_list_move (&run_queue, &no_time_queue);

      thread = thread_queue_list_dequeue (&run_queue);
    }

  /* Remove the current thread from the CPU.  */
  if (current_thread)
    {
      assert (thread);

      deschedule_current_thread (false);
    }

  if (! thread)
    return;

  if (thread->state != THREAD_RUNNING)
    {
      debug (SHOW_THREAD_STATE_TRANSITIONS, OBJECT_NAME_FMT ": %s -> %s",
	     OBJECT_NAME_PRINTF ((struct vg_object *) thread),
	     thread_state_string (thread->state),
	     thread_state_string (THREAD_RUNNING));

      debug (5, OBJECT_NAME_FMT ": Scheduling (ip: %p) (%d ready).",
	     OBJECT_NAME_PRINTF ((struct vg_object *) thread),
	     thread_ip (thread),
	     thread_queue_list_count (&run_queue)
	     + thread_queue_list_count (&no_time_queue));
    }

  quantum_start = time_data.ns_since_boot;

  /* Mark THREAD as running.  */
  thread->state = THREAD_RUNNING;

  current_thread = thread;
}

void
thread_make_runnable (struct thread *thread)
{
  if (thread->state == THREAD_RUNNABLE
      || thread->state == THREAD_RUNNING)
    return;
  assert (thread->state == THREAD_SUSPENDED);

  enum thread_state new
    = thread == current_thread ? THREAD_RUNNING : THREAD_RUNNABLE;
  debug (SHOW_THREAD_STATE_TRANSITIONS, OBJECT_NAME_FMT ": %s -> %s",
	 OBJECT_NAME_PRINTF ((struct vg_object *) thread),
	 thread_state_string (thread->state),
	 thread_state_string (new));

  if (thread == current_thread)
    thread->state = THREAD_RUNNING;
  else
    {
      if (thread->last_payout != last_payout)
	{
	  thread->time = TS;
	  thread->last_payout = last_payout;
	}

      thread_add_to_run_queue (thread);
    }
}

void
thread_suspend (struct thread *thread)
{
  if (thread->state == THREAD_SUSPENDED)
    return;
  assert (thread->state == THREAD_RUNNABLE
	  || thread->state == THREAD_RUNNING);

  debug (SHOW_THREAD_STATE_TRANSITIONS, OBJECT_NAME_FMT ": %s -> %s",
	 OBJECT_NAME_PRINTF ((struct vg_object *) thread),
	 thread_state_string (thread->state),
	 thread_state_string (THREAD_SUSPENDED));

  switch (thread->state)
    {
    case THREAD_RUNNING:
      assert (current_thread == thread);
      /* Mark it as suspended.  At the next reschedule, it will be
	 really removed from the CPU (unless it is not marked as
	 runnable in the mean time!).  */
      break;

    case THREAD_RUNNABLE:
      assert (current_thread != thread);
      thread_remove_from_run_queue (thread);
      break;
    }

  thread->state = THREAD_SUSPENDED;
}

static void
run_queue_dump (int argc, char *argv[])
{
  printf ("Current thread: ");
  if (current_thread)
    printf (OBJECT_NAME_FMT,
	    OBJECT_NAME_PRINTF ((struct vg_object *) current_thread));
  else
    printf ("NULL");
  putchar ('\n');

  struct thread *thread;
  for (thread = thread_queue_list_head (&run_queue);
       thread;
       thread = thread_queue_list_next (thread))
    printf (OBJECT_NAME_FMT"%s",
	    OBJECT_NAME_PRINTF ((struct vg_object *) thread),
	    thread_queue_list_next (thread) ? ", " : "\n");

  for (thread = thread_queue_list_head (&no_time_queue);
       thread;
       thread = thread_queue_list_next (thread))
    printf (OBJECT_NAME_FMT"%s",
	    OBJECT_NAME_PRINTF ((struct vg_object *) thread),
	    thread_queue_list_next (thread) ? ", " : "\n");
}

static struct debug_command debug_command =
  {
    "run-queue",
    'q',
    "Print the threads on the run queue.",
    run_queue_dump,
  };

void
scheduler_bootstrap (void)
{
  debug_register (&debug_command);
}