-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer.cpp
81 lines (69 loc) · 1.91 KB
/
balancer.cpp
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
#include "balancer.h"
#include "waitqueue.h"
#include "console.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
BALANCER::BALANCER(int CPU_N, int WAITQUEUE_N, int tick){
int nqueue = 0;
int ncpu = 0;
waitqueue_n = WAITQUEUE_N;
time_delta = tick;
// Start Screen
console_init();
while(ncpu < CPU_N)
{
cpu.push_back(new CPU(this, time_delta));
cpu[ncpu]->number = ncpu;
ncpu++;
}
while(nqueue < WAITQUEUE_N)
{
wqs.push_back(new WAITQUEUE());
nqueue++;
}
pthread_t idle_thread;
pthread_create(&idle_thread, NULL, (&tick_idle), (void*) this);
pthread_detach(idle_thread);
}
void BALANCER::push_to_idle(TASK &task){
int nqueue = rand()%waitqueue_n;
wqs[nqueue]->push(task);
print_queues(nqueue, wqs[nqueue]->get_id(), wqs[nqueue]->size(), wqs[nqueue]->get_last());
}
void* BALANCER::tick_idle(void* arg)
{
BALANCER *balancer = (BALANCER*) arg;
while(true)
{
usleep(balancer->time_delta);
for(int i=0; i < balancer->waitqueue_n; i++)
{
if(balancer->wqs[i]->empty())
{ print_queues(i, balancer->wqs[i]->get_id(), balancer->wqs[i]->size(), balancer->wqs[i]->get_last());
continue;
}
int out_prob = rand()%100;
if(out_prob <= balancer->wqs[i]->idle_prob)
{
TASK tsk = balancer->wqs[i]->pop();
int mn_sz = balancer->cpu[0]->size(), mn_id=0;
for(int j=1; j<(int)balancer->cpu.size(); j++)
{
if(balancer->cpu[j]->size() < mn_sz)
{
mn_sz = balancer->cpu[j]->size();
mn_id = j;
}
}
if(mn_sz*10 <= balancer->cpu[tsk.last_cpu]->size())
{
tsk.last_cpu = mn_id;
}
balancer->cpu[tsk.last_cpu]->rbt_queue_push(tsk);
}
print_queues(i, balancer->wqs[i]->get_id(), balancer->wqs[i]->size(), balancer->wqs[i]->get_last());
}
}
return(NULL);
}