-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.c
468 lines (454 loc) · 16.1 KB
/
common.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
/*
* common.c: Virtual Machine Placement Problem - Common Functions
* Date: 17-11-2014
* Author: Fabio Lopez Pires ([email protected])
* Corresponding Conference Paper: A Many-Objective Optimization Framework for Virtualized Datacenters
*/
/* include common header */
#include "common.h"
/* get_h_size: returns the number of physical machines
* parameter: path to the datacenter file
* returns: number of physical machines
*/
int get_h_size(char path_to_file[])
{
/* datacenter file to read from */
FILE *datacenter_file;
/* line readed from file */
char input_line[TAM_BUFFER];
/* number of physical machines */
int h_size = 0;
/* 1 if reading the physical machines block in the file */
int reading_physical = 0;
/* open the file for reading */
datacenter_file = fopen(path_to_file,"r");
/* if it is opened ok, we continue */
if (datacenter_file != NULL)
{
/* read until the end */
while(!feof(datacenter_file))
{
/* get line per line */
fgets(input_line, TAM_BUFFER, datacenter_file);
/* if the line is equal to H_HEADER, we begin the physical machines block in the file */
if (strstr(input_line,H_HEADER) != NULL)
{
reading_physical = 1;
}
/* if the line is equal to V_HEADER, we end the physical machines block in the file */
if (strstr(input_line,V_HEADER) != NULL)
{
reading_physical = 0;
break;
}
/* if it is the correct block in the file, it is not the header and it is not a blank line, we count */
if (reading_physical == 1 && strstr(input_line,H_HEADER) == NULL && strcmp(input_line, "\n") != 0)
{
h_size++;
}
}
}
/* close the file */
fclose(datacenter_file);
/* return the value */
return h_size;
}
/* get_v_size: returns the number of virtual machines
* parameter: path to the datacenter file
* returns: number of virtual machines
*/
int get_v_size(char path_to_file[])
{
/* datacenter file to read from */
FILE *datacenter_file;
/* line readed from file */
char input_line[TAM_BUFFER];
/* number of virtual machines */
int v_size = 0;
/* 1 if reading the virtual machines block in the file */
int reading_virtual = 0;
/* open the file for reading */
datacenter_file = fopen(path_to_file,"r");
/* if it is opened ok, we continue */
if (datacenter_file != NULL)
{
/* read until the end */
while(!feof(datacenter_file))
{
/* get line per line */
fgets(input_line, TAM_BUFFER, datacenter_file);
/* if the line is equal to V_HEADER, we begin the virtual machines block in the file */
if (strstr(input_line,V_HEADER) != NULL)
{
reading_virtual = 1;
}
/* if the line is equal to T_HEADER, we end the virtual machines block in the file */
if (strstr(input_line,T_HEADER) != NULL)
{
reading_virtual = 0;
break;
}
/* if we are in the correct block in the file, it is not the header and it is not a blank line, we count */
if (reading_virtual == 1 && strstr(input_line,V_HEADER) == NULL && strcmp(input_line, "\n") != 0)
{
v_size++;
}
}
}
/* close the file */
fclose(datacenter_file);
/* return the value */
return v_size;
}
/* print_int_matrix: prints on screen a integer matrix
* parameter: matrix to print
* parameter: number of individuals
* parameter: number of virtual machines
* returns: nothing, it's void
*/
void print_int_matrix(int **matrix, int rows, int columns)
{
/* iterators */
int iterator_row;
int iterator_column;
/* iterate on rows */
for (iterator_row=0; iterator_row < rows; iterator_row++)
{
printf("[DEBUG] ROW %d:\t\t",iterator_row);
/* iterate on columns */
for (iterator_column = 0; iterator_column < columns; iterator_column++)
{
printf("%d ",matrix[iterator_row][iterator_column]);
}
printf("\n");
}
}
/* print_float_matrix: prints on screen a float matrix
* parameter: matrix to print
* parameter: number of individuals
* parameter: number of virtual machines
* returns: nothing, it's void
*/
void print_float_matrix(float **matrix, int rows, int columns)
{
/* iterators */
int iterator_row;
int iterator_column;
/* iterate on rows */
for (iterator_row=0; iterator_row < rows; iterator_row++)
{
printf("[DEBUG] ROW %d:\t",iterator_row);
/* iterate on columns */
for (iterator_column = 0; iterator_column < columns; iterator_column++)
{
printf("%g\t",matrix[iterator_row][iterator_column]);
}
printf("\n");
}
}
/* print_int_array: prints on screen a int array
* parameter: array to print
* parameter: number of virtual machines
* returns: nothing, it's void
*/
void print_int_array(int *array, int columns)
{
/* iterators */
int iterator_column;
/* iterate on columns */
for (iterator_column = 0; iterator_column < columns; iterator_column++)
{
printf("[DEBUG] [%d]: %d\n",iterator_column,array[iterator_column]);
}
}
/* print_float_array: prints on screen a float array
* parameter: array to print
* parameter: number of columns
* returns: nothing, it's void
*/
void print_float_array(float *array, int columns)
{
/* iterators */
int iterator_column;
/* iterate on columns */
for (iterator_column = 0; iterator_column < columns; iterator_column++)
{
printf("[DEBUG] [%d]: %g\n",iterator_column,array[iterator_column]);
}
}
/* load_H: load the values of H
* parameter: number of physical machines
* parameter: path to the datacenter file
* returns: H matrix
*/
int** load_H(int h_size, char path_to_file[])
{
/* datacenter file to read from */
FILE *datacenter_file;
/* line readed from file */
char input_line[TAM_BUFFER];
/* iterator */
int iterator = 0;
/* 1 if is reading the physical machines block in the file */
int reading_physical = 0;
/* memory reserve for h_size physical machines */
int **H = (int **) malloc (h_size *sizeof (int *));
/* open the file for reading */
datacenter_file = fopen(path_to_file,"r");
/* if it is opened ok, we continue */
if (datacenter_file != NULL)
{
/* read until the end */
while(!feof(datacenter_file))
{
/* get line per line */
fgets(input_line, TAM_BUFFER, datacenter_file);
/* if the line is equal to H_HEADER, we begin the physical machines block in the file */
if (strstr(input_line,H_HEADER) != NULL)
{
reading_physical = 1;
}
/* if the line is equal to V_HEADER, we end the physical machines block in the file */
if (strstr(input_line,V_HEADER) != NULL)
{
reading_physical = 0;
break;
}
/* if it's the correct block in the file, it is not the header and it is not a blank line, we count */
if (reading_physical == 1 && strstr(input_line,H_HEADER) == NULL && strcmp(input_line, "\n") != 0)
{
/* reserve 4 columns for Processor, Memory, Storage and Power Consumption */
H[iterator] = (int *) malloc (4 *sizeof (int));
/* load on the matrix and increment iterator */
sscanf(input_line,"%d %d %d %d\n",&H[iterator][0],&H[iterator][1],&H[iterator][2],&H[iterator][3]);
iterator++;
}
}
}
fclose(datacenter_file);
return H;
}
/* load_V: load the values of V
* parameter: number of virtual machines
* parameter: path to the datacenter file
* returns: V matrix
*/
int** load_V(int v_size, char path_to_file[])
{
/* datacenter file to read from */
FILE *datacenter_file;
/* line readed from file */
char input_line[TAM_BUFFER];
/* iterator */
int iterator = 0;
/* 1 if is reading the virtual machines block in the file */
int reading_virtual = 0;
/* memory reserve for v_size virtual machines */
int **V = (int **) malloc (v_size *sizeof (int *));
/* open the file for reading */
datacenter_file = fopen(path_to_file,"r");
/* if it is opened ok, we continue */
if (datacenter_file != NULL)
{
/* read until the end */
while(!feof(datacenter_file))
{
/* get line per line */
fgets(input_line, TAM_BUFFER, datacenter_file);
/* if the line is equal to V_HEADER, we begin the virtual machines block in the file */
if (strstr(input_line,V_HEADER) != NULL)
{
reading_virtual = 1;
}
/* if the line is equal to T_HEADER, we end the virtual machines block in the file */
if (strstr(input_line,T_HEADER) != NULL)
{
reading_virtual = 0;
break;
}
/* if it's the correct block in the file, it is not the header and it is not a blank line, we count */
if (reading_virtual == 1 && strstr(input_line,V_HEADER) == NULL && strcmp(input_line, "\n") != 0)
{
/* reserve 5 columns for Processor, Memory, Storage and Economical Revenue */
V[iterator] = (int *) malloc (5 *sizeof (int));
sscanf(input_line,"%d %d %d %d %d\n",&V[iterator][0],&V[iterator][1],&V[iterator][2],&V[iterator][3],&V[iterator][4]);
iterator++;
}
}
}
fclose(datacenter_file);
return V;
}
/* load_utilization: loads the utilization of the physical machines of all the individuals
* parameter: population matrix
* parameter: physical machines matrix
* parameter: virtual machines matrix
* parameter: number of individuals
* parameter: number of physical machines
* parameter: number of virtual machines
* returns: utilization tridimentional matrix
*/
int*** load_utilization(int **population, int **H, int **V, int number_of_individuals, int h_size, int v_size)
{
/* iterators */
int iterator_individual;
int iterator_virtual;
int iterator_physical;
/* utilization holds the physical machines utilization of Processor, Memory and Storage of every individual */
int ***utilization = (int ***) malloc (number_of_individuals *sizeof (int **));
/* iterate on individuals */
for (iterator_individual=0; iterator_individual < number_of_individuals; iterator_individual++)
{
/* requirements matrix, holds the sum of virtual machines requirements for each physical machine */
int **requirements = (int **) malloc (h_size *sizeof (int *));
/* utilization holds the physical machines utilization of Processor, Memory and Storage of every individual */
utilization[iterator_individual] = (int **) malloc (h_size *sizeof (int *));
for (iterator_physical=0; iterator_physical < h_size; iterator_physical++)
{
/* virtual machine requirements in Processor, Memory and Storage. Initialized to 0 */
requirements[iterator_physical] = (int *) malloc (3 *sizeof (int));
requirements[iterator_physical][0] = requirements[iterator_physical][1] = requirements[iterator_physical][2] = 0;
/* physical machine utilization of Processor, Memory and Storage. Initialized to 0 */
utilization[iterator_individual][iterator_physical] = (int *) malloc (3 *sizeof (int));
utilization[iterator_individual][iterator_physical][0] = utilization[iterator_individual][iterator_physical][1] =
utilization[iterator_individual][iterator_physical][2] = 0;
}
/* iterate on positions of an individual */
for (iterator_virtual = 0; iterator_virtual < v_size; iterator_virtual++)
{
/* if the virtual machine has a placement assigned */
if (population[iterator_individual][iterator_virtual] != 0)
{
/* increment the requirements of the assigned physical machine with the virtual machine requirements
of Processor, Memory and Storage */
requirements[population[iterator_individual][iterator_virtual]-1][0] += V[iterator_virtual][0];
requirements[population[iterator_individual][iterator_virtual]-1][1] += V[iterator_virtual][1];
requirements[population[iterator_individual][iterator_virtual]-1][2] += V[iterator_virtual][2];
}
}
/* iterate on positions of an individual */
for (iterator_physical=0; iterator_physical < h_size; iterator_physical++)
{
/* virtual machine requirements in Processor, Memory and Storage. Initialized to 0 */
utilization[iterator_individual][iterator_physical][0] = requirements[iterator_physical][0];
utilization[iterator_individual][iterator_physical][1] = requirements[iterator_physical][1];
utilization[iterator_individual][iterator_physical][2] = requirements[iterator_physical][2];
}
}
return utilization;
}
/* load_objectives: calculate the cost of each objective of each solution
* parameter: population matrix
* parameter: physical machines matrix
* parameter: virtual machines matrix
* parameter: network traffic matrix
* parameter: number of individuals
* parameter: number of physical machines
* parameter: number of virtual machines
* returns: cost of each objetive matrix
*/
float** load_objectives(int **population, int ***utilization, int **H, int **V, int **T, int number_of_individuals, int h_size, int v_size, int *K, int **network_utilization, int l_size)
{
/* iterators */
int iterator_individual;
int iterator_virtual2;
int iterator_virtual;
int physical_position;
int iterator_physical;
int iterator_link;
float power_consumption;
float MLU = -1.0;
float n_u = 0.0;
float k = 0.0;
/* count the network traffic */
int network_traffic;
int count_traffic;
/* utility of a physical machine */
float utilidad;
/* value solution holds the cost of each solution */
float **value_solution = (float **) malloc (number_of_individuals *sizeof (float *));
for (iterator_individual = 0 ; iterator_individual < number_of_individuals; iterator_individual++)
value_solution[iterator_individual] = (float *) malloc (5 *sizeof (float));
/* iterate on individuals */
for (iterator_individual = 0; iterator_individual < number_of_individuals; iterator_individual++)
{
power_consumption = 0.0;
value_solution[iterator_individual][0] = value_solution[iterator_individual][1] = value_solution[iterator_individual][2] =
value_solution[iterator_individual][3] = value_solution[iterator_individual][4] = 0.0;
/* (OF1) calculate energy consumption of each solution*/
/* iterate on physical machines */
for (iterator_physical = 0 ; iterator_physical < h_size ; iterator_physical++)
{
if (utilization[iterator_individual][iterator_physical][0] > 0)
{
/* calculates utility of a physical machine */
utilidad = (float)utilization[iterator_individual][iterator_physical][0] / H[iterator_physical][0];
/* calculates energy consumption of a physical machine */
power_consumption += ((float)H[iterator_physical][3] - ((float)H[iterator_physical][3]*0.01)) * utilidad +
(float)H[iterator_physical][3]*0.6;
}
}
/* loads energy consumption of each solution */
value_solution[iterator_individual][0] = power_consumption;
/* (OF2) calculate network traffic between virtual machines */
network_traffic = 0;
/* iterate on virtual machines */
for (iterator_virtual = 0; iterator_virtual < v_size; iterator_virtual++)
{
physical_position = population[iterator_individual][iterator_virtual];
/* count the network traffic */
count_traffic = 0;
if (population[iterator_individual][iterator_virtual] != 0)
{
for (iterator_virtual2 = 0 ; iterator_virtual2 < v_size ; iterator_virtual2++)
{
if (physical_position == population[iterator_individual][iterator_virtual2] ||
population[iterator_individual][iterator_virtual2] == 0)
{
count_traffic = 0;
}
else
{
count_traffic = T[iterator_virtual][iterator_virtual2];
}
/* calculates network traffic between all virtual machines of each solution */
network_traffic = network_traffic + count_traffic;
}
}
}
/* loads the network traffic of each solution */
value_solution[iterator_individual][1] = (float)network_traffic;
/* (OF3) calculate revenue of each solution */
/* (OF4) calculate QoS of each solution */
/* iterate on virtual machines */
for (iterator_virtual = 0 ; iterator_virtual < v_size ; iterator_virtual++)
{
physical_position = population[iterator_individual][iterator_virtual];
if (physical_position > 0)
{
/* loads the revenue of each solution */
value_solution[iterator_individual][2] += (float) V[iterator_virtual][4];
/* loads the QoS of each solution */
value_solution[iterator_individual][3] += (float) pow (CONSTANT,V[iterator_virtual][3]) * V[iterator_virtual][3];
}
}
/* (OF5) calculate MLU of each solution */
/* iterate on network link */
for (iterator_link = 0 ; iterator_link < l_size ; iterator_link++)
{
n_u = network_utilization[iterator_individual][iterator_link] / 1.0f;
k = K[iterator_link] / 1.0f;
if ((float) n_u / k > MLU)
{
/* loads the MLU */
MLU = n_u / k;
}
}
/* loads the MLU of each solution */
value_solution[iterator_individual][4] = MLU;
MLU = -1.0;
n_u = 0.0;
k = 0.0;
}
return value_solution;
}