-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
137 lines (115 loc) · 3.82 KB
/
Copy pathmain.c
File metadata and controls
137 lines (115 loc) · 3.82 KB
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
/*
Arjun Krishna (a68krish@uwaterloo.ca)
Andrei Ikic (a2ikic@uwaterloo.ca)
*/
#include <cmsis_os2.h> // include RTOS functions
#include <LPC17xx.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <random.h>
#define N 2
#define MONITOR_PERIOD 1
#define AVERAGE_ARRIVAL_RATE 9
#define AVERAGE_SERVICE_RATE 10
#define QUEUE_SIZE 10
// TODO make sure osDelay wait is correct
int workOrder = 7;
int balance[N];
// create a new message queue
osMessageQueueId_t messageQueues [N];
int messageIndices[N];
// arrays to hold statistics for each message queue
int messagesSent[N];
int messagesRecieved[N];
int messagesOverflow[N];
float serverRandomSleepTime[N];
__NO_RETURN void client(void *q_id_void)
{
int q_id_int = *(int *) q_id_void;
osMessageQueueId_t q_id = messageQueues[q_id_int];
printf("client %d\n", osMessageQueueGetCapacity(q_id));
while(1)
{
osDelay(((next_event() * osKernelGetTickFreq()) / AVERAGE_ARRIVAL_RATE) >> 16);
osStatus_t status = osMessageQueuePut(q_id, &workOrder, 0, 0);
if(status == osOK)
messagesSent[q_id_int] += 1;
else if(status == osErrorResource)
messagesOverflow[q_id_int] += 1;
osThreadYield();
}
}
__NO_RETURN void server(void *q_id_void)
{
int q_id_int = *(int *) q_id_void;
osMessageQueueId_t q_id = messageQueues[q_id_int];
printf("server %d\n", osMessageQueueGetCapacity(q_id));
while(1)
{
int randomSleepTime = ((next_event() * osKernelGetTickFreq()) / AVERAGE_SERVICE_RATE) >> 16;
osDelay(randomSleepTime);
serverRandomSleepTime[q_id_int] += ((float)randomSleepTime) / ((float)osKernelGetTickFreq());
int msg;
osStatus_t status = osMessageQueueGet(q_id, &msg, 0, osWaitForever);
if(status == osOK)
messagesRecieved[q_id_int] += 1;
osThreadYield();
}
}
__NO_RETURN void monitor(void *arg)
{
printf("monitor\n");
float loadFactor = ((float)AVERAGE_ARRIVAL_RATE) / ((float)AVERAGE_SERVICE_RATE);
float expectedBlockProbability = ((float)((pow(loadFactor, QUEUE_SIZE))*(1 - loadFactor)))/((float)(1 - pow(loadFactor, QUEUE_SIZE)));
//printf("expectedBlockProbability %8.5f", expectedBlockProbability);
while(1)
{
int elapsedTime = osKernelGetTickCount() / osKernelGetTickFreq();
if(!(elapsedTime % 20))
{
printf("Qid, Time, Sent, Recv, Over, Wait, P_blk,");
printf(" Arrv, Serv, Epblk, Earrv, Eserv\n");
}
for(int i = 0; i < N; i++)
{
printf(" Q%d,", i);
printf("%5d,", elapsedTime);
printf("%5d,", messagesSent[i]);
printf("%5d,", messagesRecieved[i]);
printf("%5d,", messagesOverflow[i]);
printf("%5d,", osMessageQueueGetCount(messageQueues[i]));
float blockProbability = ((float)messagesOverflow[i])/((float)messagesSent[i]);
printf("%8.4f,", blockProbability);
float arrivalRate = ((float)messagesSent[i]) / ((float)elapsedTime);
printf("%8.4f,", arrivalRate);
float serviceRate = ((float)messagesRecieved[i] )/ serverRandomSleepTime[i];
printf("%8.4f,", serviceRate);
printf("%8.4f,", ((float)(blockProbability - expectedBlockProbability) )/ ((float) expectedBlockProbability));
printf("%8.4f,", ((float)(arrivalRate - AVERAGE_ARRIVAL_RATE))/ ((float) AVERAGE_ARRIVAL_RATE));
printf("%8.4f", ((float)(serviceRate - AVERAGE_SERVICE_RATE)/ ((float) AVERAGE_SERVICE_RATE)));
printf("\n");
}
// wait for one second
osDelay(osKernelGetTickFreq()*(MONITOR_PERIOD));
}
}
int main (void)
{
SystemCoreClockUpdate();
osKernelInitialize();
// create message queues
for(int i = 0; i < N; i++)
messageQueues[i] = osMessageQueueNew(QUEUE_SIZE, sizeof(int), NULL);
// create client and serverthreads
for(int i = 0; i < N; i ++)
{
messageIndices[i] = i;
osThreadNew(client, (void*)&messageIndices[i], NULL);
osThreadNew(server, (void*)&messageIndices[i], NULL);
}
osThreadNew(monitor, NULL, NULL);
printf("starting kernels\n");
osKernelStart();
return 0;
}