This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.c
222 lines (183 loc) · 4.99 KB
/
model.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include "gsl/gsl.h"
#include <getopt.h>
#include "vecs.h"
#define MAX_OUTFILE 1024
size_t size = 10000;
timings_t av = 1;
double util = 0.5;
size_t colns=128;
int outfile = 0;
void print_usage(const char *filename){
printf("%s [-f] -a <av> -u <util> -s <size>\n", filename);
}
size_t parse_model_param(const char *param_string, struct distr_param_s* params, size_t size) {
const char *delim = "/";
char *str;
char **curs = &str;
char *m;
size_t i;
str = (char*) calloc(1024,sizeof(char));
strncpy(str, param_string, 1024-1);
i=0;
while((m = strsep(curs, delim)) && (i < size)) {
struct distr_param_s *p = ¶ms[i];
switch(m[0]){
case 'M':
p->type = exponential;
break;
case 'E':
p->type = erlang;
p->g_form = 0;
p->g_form = strtod(&m[1], 0);
if (p->g_form <= 0 || ERANGE == errno) {
p->g_form = 1;
printf("Bad form for E provided, set to %f\n", p->g_form);
}
break;
default:
continue;
}
i++;
}
return i;
}
void out_vec(const char *file, const double *print_vec, size_t size){
FILE *fo;
size_t i;
if(NULL == (fo = fopen(file, "w+"))){
perror("Output error");
return;
}
for(i=0; i < size; ++i) {
fprintf(fo, "%zu\t\%f\n", i, print_vec[i]);
}
fclose(fo);
}
void out_vec_bars(const char *file, const double *print_vec, size_t size,size_t cl){
size_t *bars = (size_t*) calloc(sizeof(size_t),colns);
double max = 0, min = 0;
double delta = 0;
FILE *fo;
if(NULL == (fo = fopen(file, "w+"))){
perror("Output error");
return;
}
size_t i,j;
for(i = 0; i < size; ++i) {
min=fmin(min, print_vec[i]);
max=fmax(max, print_vec[i]);
}
delta = (max-min)/colns;
if(0 == delta) {
delta++;
}
for(i = 0; i < size; ++i) {
j = (print_vec[i]-min)/delta;
bars[j]++;
}
for(i = 0; i < colns; ++i) {
double norm = bars[i];
norm /= size;
fprintf(fo, "%f\t%f\n", (delta*(i+1))+min, norm);
}
fclose(fo);
free(bars);
}
int main(int argc, char* argv[]){
int rc=0;
char *q_file = 0;
char *s_file = 0;
struct distr_param_s dp_v[2];
char opt;
while((opt=getopt(argc, argv, "u:a:s:fc:m:")) != -1){
switch(opt){
case('f'):
outfile = 1;
break;
case('c'):
colns=strtoul(optarg, 0, 10);
break;
case('u'):
util = (timings_t) strtod(optarg, 0);
break;
case('a'):
av = (timings_t) strtod(optarg, 0);
break;
case('s'):
size = (size_t) strtoul(optarg, 0, 10);
break;
case('m'):
if( 2 != parse_model_param(optarg, dp_v, 2)) {
printf("Model : parse fail\n");
break;
}
int z;
for(z = 0; z < 2; z++){
if (dp_v[z].type == exponential) {
printf("Model : exp\n");
} else {
printf("Model : erlang\n");
}
}
break;
default:
print_usage(argv[0]);
rc=1;
goto end;
}
}
timev_t q_vec = malloc(size * sizeof(timings_t));
timev_t s_vec = malloc(size * sizeof(timings_t));
timev_t w_vec = malloc(size * sizeof(timings_t));
dp_v[0].mean = av;
dp_v[1].mean = dp_v[0].mean * util;
dp_v[0].g_mult = dp_v[0].mean / dp_v[0].g_form;
dp_v[1].g_mult = dp_v[1].mean / dp_v[1].g_form;
rand_v((double*) q_vec, size, dp_v[0]); /*generating query intervals*/
rand_v((double*) s_vec, size, dp_v[1]); /*generatig serving intervals*/
calculate_wait_vec(q_vec, s_vec, w_vec, size);
printf("Среднее время интервалов запросов:\t%f\n", ave_vec(q_vec, size));
printf("Cреднее время обслуживания запросов:\t%f\n", ave_vec(s_vec, size));
printf("Среднее время ожидания запроса в очереди:\t:%f\n", ave_vec(w_vec, size));
if(outfile) {
out_vec_bars("./latex/data.serv", s_vec, size, colns);
out_vec_bars("./latex/data.gen", q_vec, size, colns);
out_vec("./latex/data.wait", w_vec + 1000, 25); // +1000 to let quque stabilize
size_t i,j;
double *vec = w_vec;
double *filtered_vec = malloc(size*sizeof(double));
size_t filtered_size;
for(i=0, j=0; i < size; i++){
if(vec[i] > 0) { filtered_vec[j++] = vec[i]; }
}
filtered_size = j+1;
out_vec_bars("./latex/data.wait.bars", filtered_vec, filtered_size, colns);
free(filtered_vec);
size_t s = size;
double *output_vec = (double*) malloc(s * sizeof(double));
double *output_vec2 = (double*) malloc(s * sizeof(double));
output_vec[0] = q_vec[0];
double aq1=0, aq2 = 0;
for(i=1; i < s; ++i) {
aq1+=q_vec[i];
if(aq1<aq2) { printf("Overflow of out\n"); rc=3; goto end; }
aq2=aq1;
output_vec[i] = fmax(aq2, output_vec[i-1]) + s_vec[i]; /* if i-1 query left before i arrived take aq (absolute clock
else take i-1 departue time + i serve time */
}
for(i=0; i < s-1; ++i) {
output_vec2[i] = output_vec[i+1] - output_vec[i];
}
out_vec_bars("./latex/data.depart", output_vec2, s-1, colns);
free(output_vec);
}
end:
return rc;
}