-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu.c
613 lines (522 loc) · 13.7 KB
/
cpu.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* cpu.c
* Main source for linux scheduler virtual machine.
* Emulates a simple CPU running processes with a generic interrupt
* for "IO."
*
* Requires a properly written schedule.c and schedule.h files
* with stubs filled out.
*/
#include "privatestructs.h"
#include "schedule.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "list.h"
#include "macros.h"
/* Static methods */
static void __init_sched();
static int taskEnd();
static void spawnChildren();
static int cycle();
static int interrupt();
static void runcpu();
static void killtask(struct task_struct **p);
static void shutdowncpu();
static void badshutdowncpu();
static void cleanuptask(struct thread_info *p);
static void forktask(struct thread_info *thread, struct task_struct *parent);
/* External Prototypes */
struct task_struct *createTask();
struct thread_info *createInfo(const char *name);
int readProfile(char *filename);
/* Macros
* CLOCK_HZ - Sets the clock speed for the VM
* The other macros are bookkeeping
* macros for debugging output, and should
* be self-exlainitory.
*/
#define CLOCK_HZ 500000
#define HZ_TO_MS (1000 / HZ)
#define MS_TO_TICKS(ms) (CLOCK_HZ / 1000 * (ms))
#define TICKS_TO_MS(tick) ((long long)((tick) / (long double)CLOCK_HZ * (1000)))
#define LEVEL1(p, q) OUTPUT("", p, q)
#define LEVEL2(p, q) OUTPUT("\t", p, q)
#define LEVEL3(p, q) OUTPUT("\t\t", p, q)
#define OUTPUT(o, p, q) (printf("%s%s/%d/%lldms - %s\n", (o), (p)->thread_info->processName, (p)->time_slice, TICKS_TO_MS(clocktick), (q)))
#define ALERT(p) (printf("###-%s-###\n", (p)))
/* Globals
* jiffies - A jiffy represents the smallest unit of time that can occur
* between schedule ticks
* clocktick - The number of cycles the clock has run
* timer - A "hardware" timer that fires for schedule ticks
* processID - Next Process ID value
* rq - A poiner to the runqueue
* idle - Pointer to the idle task
* init - Pointer to the init task
* current - A pointer to the current process in the CPU
*/
long long jiffies = 0;
long long clocktick = 0;
long long timer = 0;
unsigned int processID = 0;
extern struct runqueue *rq;
struct task_struct *idle = NULL;
struct task_struct *init = NULL;
extern struct task_struct *current;
/* Control Data
* cycletime - The delay between cycles in the VM
* ranSeed - A random seed for the VM
* intTimer - The general "IO" interrupt, runs from a timer that is
* is randomly set when a process goes to sleep
* intWaitTimer - A process timer to dictate when an interactive process
* will go and wait on IO
* endtime - The time in ms when the VM will shutdown (approximately)
*/
long cycletime = 10;
int ranSeed = 42;
long long intTimer = -1;
long long intWaitTimer = -1;
long endtime = 1;
/*-------------- INTERRUPT DATA ---------------*/
/* This section has data specific to our
* machines "interrupts."
*/
/* Return Values */
#define RESCHEDULE 1
/* This is an IO wait queue */
struct waitlist
{
struct task_struct *task;
struct list_head list;
};
/* Wait Queues
* This is the wait queue for our "IO"
*/
struct list_head intwaitlist;
/*---------------APPLICATION LOGIC------------------*/
/* main
* Takes a profile to load and run
*/
int main(int argc, char *argv[])
{
if(argc < 2)
{
printf("Virtual Scheduler\nUsage: vsch [filename]\n");
return(1);
}
/* Initialize CPU and scheduler */
__init_sched();
/* Read in the profile */
if(!readProfile(argv[1]))
goto ERROR;
/* Set the random seed we read in */
srand(ranSeed);
/* Schedule the idle task to "prep" the scheduler */
ALERT("Starting CPU");
schedule();
/* Set first schedule tick timer */
timer = MS_TO_TICKS(HZ_TO_MS);
/* Start the CPU */
runcpu();
/* Clean up from the CPU */
ALERT("Shutting Down CPU");
shutdowncpu();
return(0);
ERROR:
/* Cleanup from an error */
badshutdowncpu();
shutdowncpu();
return(1);
}
/* ---------------------- VM FUNCTIONS -------------------*/
/* runcpu
* This is the primary application loop that simulates
* our "CPU". This code also controls the checking
* of interrupts.
*/
static void runcpu()
{
long long lastMS = 0;
do
{
/* Run a single cycle of our application */
if(cycle())
goto END_CYCLE;
INTERRUPT:
/* Check for interrupts
* This routine checks for fired
* IO routines or timer ticks
*/
switch(interrupt())
{
/* Current task signaled for
* reschedule
*/
case RESCHEDULE:
clocktick++;
schedule();
goto END_CYCLE;
break;
}
/* We've completed a clock cycle,
* add a tick.
*/
clocktick++;
END_CYCLE:
/* Check for ending conditions for our simulation */
if(!init->thread_info->kill && TICKS_TO_MS(clocktick) >= endtime)
{
ALERT("Sending Kill Message");
//isEnding = 1;
init->thread_info->kill = 1;
}
/* Flush output and sleep */
fflush(stdout);
if(TICKS_TO_MS(clocktick) > lastMS)
{
lastMS = TICKS_TO_MS(clocktick);
if(cycletime > 0)
usleep(cycletime);
}
}while(rq->nr_running);
}
/* cycle
* Controls process logic, spawning and sleeping,
* as well as new process creation and process
* death.
*/
static int cycle()
{
struct waitlist *tempwaitlist;
/* Check to see if the task is ending */
if(taskEnd())
return(0);
/* Run logic based on task type */
switch(current->thread_info->thread_type)
{
case INIT:
break;
/* Interactive tasks need to check to see
* if they are going to wait on IO
*/
case INTERACTIVE:
/* Tick the timer for sleeping */
if(intWaitTimer > 0)
intWaitTimer--;
/* When timer expires, sleep! */
if(intWaitTimer == 0)
{
intWaitTimer--;
LEVEL2(current, "Going to Sleep");
/* Add task to wait queue */
tempwaitlist = (struct waitlist*)malloc(sizeof(struct waitlist));
INIT_LIST_HEAD(&tempwaitlist->list);
tempwaitlist->task = current;
list_add_tail(&tempwaitlist->list, &intwaitlist);
/* Deactivate the task and remove it from the
* scheduler.
*/
deactivate_task(current);
/* We need to be rescheduled! */
current->need_reschedule = 1;
}
break;
case NONINTERACTIVE:
break;
}
/* If no other task has slept on IO,
* set a random time for the next "IO"
* event.
*/
if(intTimer < 0)
intTimer = MS_TO_TICKS(rand() % 1000 + 50);
/* Create any children */
spawnChildren();
return(0);
}
/* interrupt
* Checks our computers interrupts
*/
static int interrupt()
{
struct list_head *listcur, *listnext;
struct waitlist *tempwaitlist;
/*----------SCHEDULE TICK TIMER-------------*/
/* Decrement the timer */
if(timer > 0)
timer--;
/* Timer Tick! Run the scheduler */
if(timer <= 0)
{
jiffies++;
timer = MS_TO_TICKS(HZ_TO_MS);
if(current->array != NULL)
scheduler_tick(current);
}
/*-------IO EVENT TIMER----------*/
/* Decrement the timer */
if(intTimer > 0)
intTimer--;
/* Timer tick! */
if(intTimer == 0)
{
intTimer--;
listcur = intwaitlist.next;
ALERT("An Interrupt has fired!");
/* Check the IO waitlist to see if
* there are processes sleeping.
*/
while(listcur != &intwaitlist)
{
tempwaitlist = list_entry(listcur, struct waitlist, list);
LEVEL2(tempwaitlist->task, "Waking Up from Sleep");
activate_task(tempwaitlist->task);
listnext = listcur->next;
list_del(listcur);
free(tempwaitlist);
listcur = listnext;
}
/* Notify that we need to reschedule! */
current->need_reschedule = 1;
}
/* If a task needs rescheduling, alert! */
if(current->need_reschedule)
return(RESCHEDULE);
return(0);
}
/*------------------ SYSTEM CALLS --------------------*/
/* context_switch
* This performs a "context switch" for the
* scheduler.
*/
void context_switch(struct task_struct *next)
{
LEVEL1(next, "Switching Process In");
if(TICKS_TO_MS(clocktick) == 8790)
printf("BREAK\n");
/* If this is an interactive task,
* set random chance for sleep.
*/
if(next->thread_info->thread_type == INTERACTIVE)
intWaitTimer = MS_TO_TICKS(rand() % (next->time_slice * HZ / 1000 + 100) + 5);
/* Set new task as current */
current = next;
}
/* sched_clock
* Returns the current time (approx)
* to the scheduler based on
* jiffies.
*/
unsigned long long sched_clock()
{
return(JIFFIES_TO_NS(jiffies));
}
/*-------------------Local Methods-------------------*/
/* __init_sched
* A pre-initialization function. Sets up
* Initial tasks and runqueue for scheduler
* before calling user's function to
* setup custom queues.
*/
static void __init_sched()
{
struct task_struct *task;
/* Create Init Task */
task = createTask();
task->thread_info = createInfo("Init");
task->thread_info->thread_type = INIT;
task->thread_info->kill_time = -1;
INIT_LIST_HEAD(&task->run_list);
INIT_LIST_HEAD(&task->thread_info->list);
//task->time_slice = task_timeslice(task);
/* Assign to global pointer for Config */
init = task;
/* Initialize Runqueue */
rq = (struct runqueue*)malloc(sizeof(struct runqueue));
rq->curr = NULL;
rq->nr_running = 0;
rq->nr_switches = 0;
rq->best_expired_prio = MAX_PRIO;
rq->expired_timestamp = 0;
/* Initialize Scheduler */
initschedule(rq, init);
/* Create Idle Task */
idle = createTask();
idle->thread_info = createInfo("IDLE");
processID--;
current = idle;
/* Prepare List heads */
INIT_LIST_HEAD(&intwaitlist);
}
/* forktask
* Creates data structures for a new process being spawned
* from a parent. Finally, it submits the task to the
* scheduler.
*/
static void forktask(struct thread_info *thread, struct task_struct *parent)
{
struct task_struct *task;
char str[1024];
task = createTask();
task->thread_info = thread;
task->thread_info->id = processID++;
task->thread_info->children = 0;
task->thread_info->kill = 0;
/* Assigne Thread Name */
if(parent->thread_info->parent != NULL)
sprintf(str, "%s:(%s:%d)", parent->thread_info->processName, task->thread_info->processName, task->thread_info->id);
else
sprintf(str, "(%s:%d)", task->thread_info->processName, task->thread_info->id);
free(task->thread_info->processName);
task->thread_info->processName = (char*)malloc(strlen(str) + 1);
memcpy(task->thread_info->processName, str, strlen(str) + 1);
/* If the parent is not INIT, display parent info */
if(thread->parent != NULL)
thread->parent->children++;
/* Set NICE value */
task->static_prio = NICE_TO_PRIO(task->thread_info->niceValue);
/* Alert Creation */
printf("###-Process: %s has been created-###\n", thread->processName);
/* Fork process in Scheduler */
sched_fork(task);
/* Wake up the task */
wake_up_new_task(task);
/* Signal need for schedule call */
current->need_reschedule = 1;
}
/* taskEnd
* Checks for an exit signal for the current
* running task.
*/
static int taskEnd()
{
/* Check to see if the time for this process to end
* has passed.
*/
if(current->thread_info->kill ||
(current->thread_info->parent != NULL && current->thread_info->parent->kill) ||
(current->thread_info->kill_time >= 0 && TICKS_TO_MS(clocktick) >= current->thread_info->kill_time)
)
{
if(!current->thread_info->kill)
current->thread_info->kill = 1;
if(current->thread_info->children == 0)
{
deactivate_task(current);
killtask(¤t);
return(1);
}
}
return(0);
}
/* spawnChildren
* Spanws children for processes
*/
static void spawnChildren()
{
struct list_head *child, *next;;
struct thread_info *temp;
/* Make sure the current process can spawn */
if(current->thread_info->spawns)
{
/* Run through the list of children to be spawned */
child = ¤t->thread_info->list;
child = child->next;
while(child != ¤t->thread_info->list)
{
/* If it is time to spawn a child,
* spawn that child.
*/
temp = list_entry(child, struct thread_info, clist);
if(MS_TO_TICKS(temp->spawn_time) <= clocktick)
{
forktask(temp, current);
next = child;
child = child->next;
list_del(next);
}
else
child = child->next;
}
}
}
/* killtask
* Kills the current running task and
* removes it from the scheduler.
*/
static void killtask(struct task_struct **p)
{
struct task_struct *j = *p;
printf("###-Process: %s is going down-###\n", j->thread_info->processName);
/* If task has a parent, decrement the parent's
* count of running children.
*/
if(j->thread_info->parent != NULL)
j->thread_info->parent->children--;
/* Free data structures */
free(j->thread_info->processName);
free(j->thread_info);
free(j);
/* Set the idle task in place
* of the current one so the
* scheduler works correctly.
*/
*p = idle;
j = *p;
/* If there are still tasks to be run, run them. */
if(rq->nr_running != 0)
j->need_reschedule = 1;
}
/* shutdowncpu
* Releases data structures during a normal
* shutdown.
*/
static void shutdowncpu()
{
free(idle->thread_info->processName);
free(idle->thread_info);
free(idle);
/* Shuts down the scheduler */
killschedule();
/* Free the runqueue */
free(rq);
}
/* badshutdowncpu
* Frees data structures left in memory when
* an error occurs.
*/
static void badshutdowncpu()
{
cleanuptask(init->thread_info);
if(init->thread_info->processName != NULL)
free(init->thread_info->processName);
free(init->thread_info);
free(init);
}
/* cleanuptask
* A recursive helper function
* for badshutdowncpu which
* recursively frees child tasks
*/
static void cleanuptask(struct thread_info *p)
{
struct list_head *child, *next;
struct thread_info *temp;
child = &p->list;
child = child->next;
while(child != NULL && child != &p->list)
{
temp = list_entry(child, struct thread_info, clist);
next = child;
child = child->next;
list_del(next);
/* Clean up task children */
cleanuptask(temp);
/* Clean up this task */
if(temp->processName != NULL)
free(temp->processName);
free(temp);
}
}