-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·319 lines (266 loc) · 14.1 KB
/
main.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
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
#include <iostream>
#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "DIM_core.h"
#include <pthread.h>
#define MAX_CORES 256
#define STATIC_SCHEDULER 0
void* static_scheduler_function(void * argv);
void generate_cache_statistics(uint32_t cores,uint32_t l2_number,uint32_t l3_number);
pthread_mutex_t mutex_sync;
uint32_t can_execute[MAX_CORES];
pthread_mutex_t mutex_can_execute[MAX_CORES];
pthread_mutex_t mutex_bus_l1_to_l2[MAX_CORES];
pthread_mutex_t mutex_bus_l2_to_l3[MAX_CORES];
cache* cache_l2_vector = NULL;
cache* cache_l3_vector = NULL;
pthread_mutex_t mutex_finish;
pthread_cond_t var_cond_threads[MAX_CORES];
pthread_cond_t var_cond_main;
pthread_barrier_t barrier;
uint32_t syncronized = 0;
uint32_t finish = 0;
uint32_t total_cores = 0;
gzFile_s *traces[MAX_CORES];
uint64_t *l1_instructions_accesses_counter = NULL;
uint64_t *l1_configurations_accesses_counter = NULL;
uint64_t *l1_instructions_hits_counter = NULL;
uint64_t *l1_configurations_hits_counter = NULL;
int main(int ac, char *av[])
{
DIM_core *cpus_dim[MAX_CORES] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
pthread_t threads[MAX_CORES];
FILE *args_file = NULL;
cache* temp_l2 = NULL;
cache* temp_l3 = NULL;
uint32_t ul2_unified_cache_size;
uint32_t ul2_unified_cache_associativity;
uint32_t ul2_unified_cache_block_size;
uint32_t ul2_unified_cache_replacement_method;
uint32_t ul2_unified_cores_per_cache;
uint32_t ul3_unified_cache_size;
uint32_t ul3_unified_cache_associativity;
uint32_t ul3_unified_cache_block_size;
uint32_t ul3_unified_cache_replacement_method;
uint32_t ul3_unified_cores_per_cache;
char line_buffer[1024];
uint32_t scheduler_id = 0;
args_file = fopen(av[1],"r");
if(!args_file)
{
printf("arquivo de configuracoes inexistente!!\n");
return 1;
}
/*Leitura da configuração das caches L2*/
fgets(line_buffer,sizeof(line_buffer),args_file);
sscanf(line_buffer, "-UL2 [%u,%u,%u,%u,%u]",&ul2_unified_cache_size,&ul2_unified_cache_associativity,&ul2_unified_cache_block_size,&ul2_unified_cache_replacement_method,&ul2_unified_cores_per_cache);
/*Leitura da configuração das caches L3*/
fgets(line_buffer,sizeof(line_buffer),args_file);
sscanf(line_buffer, "-UL3 [%u,%u,%u,%u,%u]",&ul3_unified_cache_size,&ul3_unified_cache_associativity,&ul3_unified_cache_block_size,&ul3_unified_cache_replacement_method,&ul3_unified_cores_per_cache);
/*leitura do numero de processadores*/
fgets(line_buffer,sizeof(line_buffer),args_file);
sscanf(line_buffer,"%u",&total_cores);
/*Criação dos vetores com as taxas de hit nas caches L1*/
l1_instructions_accesses_counter = new unsigned long int[total_cores];
l1_configurations_accesses_counter = new unsigned long int[total_cores];
l1_instructions_hits_counter = new unsigned long int[total_cores];
l1_configurations_hits_counter = new unsigned long int[total_cores];
cache_l2_vector = new cache[total_cores/ul2_unified_cores_per_cache];
/*Criação das UL2*/
for(uint32_t counter_l2 = 0;counter_l2 < (total_cores/ul2_unified_cores_per_cache); counter_l2 ++)
{
if(ul2_unified_cache_size)
{
temp_l2 = new cache(ul2_unified_cache_size,ul2_unified_cache_associativity,ul2_unified_cache_block_size,ul2_unified_cache_replacement_method,UNIFIED_CACHE);
temp_l2->cache_print_status();
}
else
{
temp_l2 = NULL;
}
cache_l2_vector[counter_l2] = *(temp_l2);
}
cache_l3_vector = new cache[total_cores/ul3_unified_cores_per_cache];
/*Criação das UL3*/
for(uint32_t counter_l3 = 0;counter_l3 < (total_cores/ul3_unified_cores_per_cache); counter_l3 ++)
{
if(ul3_unified_cache_size)
{
temp_l3 = new cache(ul3_unified_cache_size,ul3_unified_cache_associativity,ul3_unified_cache_block_size,ul3_unified_cache_replacement_method,UNIFIED_CACHE);
temp_l3->cache_print_status();
}
else
{
temp_l3 = NULL;
}
cache_l3_vector[counter_l3] = *(temp_l3);
}
/*leitura dos argumentos para os cores e criacao dos mesmos*/
for(uint32_t core = 0;core < total_cores; core++)
{
fgets(line_buffer,sizeof(line_buffer),args_file);
cpus_dim[core] = new DIM_core(line_buffer,&cache_l2_vector[core/(ul2_unified_cores_per_cache)],&mutex_bus_l1_to_l2[core/(ul2_unified_cores_per_cache)],&cache_l3_vector[core/(ul3_unified_cores_per_cache)],&mutex_bus_l2_to_l3[core/(ul3_unified_cores_per_cache)]);
}
/*leitura do identificador do escalonador*/
fgets(line_buffer,sizeof(line_buffer),args_file);
sscanf(line_buffer,"%u",&scheduler_id);
/*toma a acao de acordo com o escalonador utilizado*/
switch(scheduler_id)
{
/*escalonador estatico*/
case STATIC_SCHEDULER:
{
char trace_name[512];
/*le a localizacao dos traces compactados de cada core e cria
as threads que executarao cada core*/
for(uint32_t core = 0;core < total_cores;core++)
{
/*obtem a localizacao do arquivo de trace compactado*/
fgets(line_buffer,sizeof(line_buffer),args_file);
sscanf(line_buffer,"%s",trace_name);
cpus_dim[core]->DIM_core_set_trace_name(trace_name);
/*cria a thread*/
pthread_create(&threads[core], NULL,static_scheduler_function, (void *)cpus_dim[core]);
//static_scheduler_function((void *)cpus_dim[core]);
}
/*fecha o arquivo com os argumentos para o simulador*/
fclose(args_file);
/*espera ate que todas as threads tenham terminado*/
for(uint32_t core = 0;core < total_cores;core++)
{
pthread_join(threads[core],NULL);
}
break;
}
default:
{
printf("escalonador nao definido\n");
return 1;
}
}
generate_cache_statistics(total_cores,total_cores/ul2_unified_cores_per_cache,total_cores/ul3_unified_cores_per_cache);
delete[] l1_instructions_accesses_counter;
delete[] l1_configurations_accesses_counter;
delete[] l1_instructions_hits_counter;
delete[] l1_configurations_hits_counter;
pthread_exit(NULL);
return 0;
}
void* static_scheduler_function(void * argv)
{
DIM_core *core = (DIM_core*)argv;
char buffer[2048];
buffer[0] = 0;
if(!(traces[core->thread_number] = gzopen(core->trace_name,"r")))
{
printf("Nao foi possivel abrir o arquivo %s\n",core->trace_name);
/*deleta o core*/
delete core;
/*retorna*/
pthread_exit(NULL);
return NULL;
}
while(1)
{
/*le uma linha do arquivo da thread*/
gzgets(traces[core->thread_number],buffer,2047);
/*ainda não chegou ao fim do arquivo*/
if(buffer[0])
{
if(core->DIM_core_process_trace_line(buffer) == BARRIER_INSTRUCTION)
{
core->dim_results->results_gain_between_barriers(NORMAL_RESULTS);
core->dim_results->results_reset_counters();
}
}
/*fim do arquivo*/
else
{
break;
}
/*limpa o buffer*/
buffer[0] = 0;
}
/*fecha o arquivo do trace*/
gzclose(traces[core->thread_number]);
/*contabiliza as metricas de desempenho para as instrucoes executadas
* apos a ultima barreira de sincronizacao*/
core->dim_results->results_gain_between_barriers(END_RESULTS);
l1_instructions_accesses_counter[core->thread_number] = core->cache_uni_dim->instructions_accesses_counter;
l1_configurations_accesses_counter[core->thread_number] = core->cache_uni_dim->configurations_accesses_counter;
l1_instructions_hits_counter[core->thread_number] = core->cache_uni_dim->instructions_hits_counter;
l1_configurations_hits_counter[core->thread_number] = core->cache_uni_dim->configurations_hits_counter;
/*deleta o core*/
delete core;
/*retorna*/
pthread_exit(NULL);
return NULL;
}
void generate_cache_statistics(uint32_t cores,uint32_t l2_number,uint32_t l3_number)
{
FILE* results_cache = NULL;
uint64_t l1_max_instructions_accesses_counter = 0;
uint64_t l1_max_configurations_accesses_counter = 0;
uint64_t l1_max_instructions_hits_counter = 0;
uint64_t l1_max_configurations_hits_counter = 0;
uint64_t l2_max_instructions_accesses_counter = 0;
uint64_t l2_max_configurations_accesses_counter = 0;
uint64_t l2_max_instructions_hits_counter = 0;
uint64_t l2_max_configurations_hits_counter = 0;
uint64_t l3_max_instructions_accesses_counter = 0;
uint64_t l3_max_configurations_accesses_counter = 0;
uint64_t l3_max_instructions_hits_counter = 0;
uint64_t l3_max_configurations_hits_counter = 0;
for(uint32_t counter = 0;counter<cores;counter++)
{
if(l1_instructions_accesses_counter[counter] > l1_max_instructions_accesses_counter)
{
l1_max_instructions_accesses_counter = l1_instructions_accesses_counter[counter];
l1_max_instructions_hits_counter = l1_instructions_hits_counter[counter];
}
if(l1_configurations_accesses_counter[counter] > l1_max_configurations_accesses_counter)
{
l1_max_configurations_accesses_counter = l1_configurations_accesses_counter[counter];
l1_max_configurations_hits_counter = l1_configurations_hits_counter[counter];
}
}
for(uint32_t counter = 0;counter<l2_number;counter++)
{
if(cache_l2_vector[counter].instructions_accesses_counter > l2_max_instructions_accesses_counter )
{
l2_max_instructions_accesses_counter = cache_l2_vector[counter].instructions_accesses_counter;
l2_max_instructions_hits_counter = cache_l2_vector[counter].instructions_hits_counter;
}
if(cache_l2_vector[counter].configurations_accesses_counter > l2_max_configurations_accesses_counter)
{
l2_max_configurations_accesses_counter = cache_l2_vector[counter].configurations_accesses_counter;
l2_max_configurations_hits_counter = cache_l2_vector[counter].configurations_hits_counter;
}
}
for(uint32_t counter = 0;counter<l3_number;counter++)
{
if(cache_l3_vector[counter].instructions_accesses_counter > l3_max_instructions_accesses_counter)
{
l3_max_instructions_accesses_counter = cache_l3_vector[counter].instructions_accesses_counter;
l3_max_instructions_hits_counter = cache_l3_vector[counter].instructions_hits_counter;
}
if(cache_l3_vector[counter].configurations_accesses_counter > l3_max_configurations_accesses_counter)
{
l3_max_configurations_accesses_counter = cache_l3_vector[counter].configurations_accesses_counter;
l3_max_configurations_hits_counter = cache_l3_vector[counter].configurations_hits_counter;
}
}
results_cache = fopen("results_cache.txt","w");
fprintf(results_cache,"Sintaxe: INSTRUCTIONS ACCESSES,INSTRUCTIONS HITS,CONFIGURATIONS ACCESSES,CONFIGURATIONS HITS\n");
fprintf(results_cache,"%lu,%lu,%lu,%lu\n",l1_max_instructions_accesses_counter,l1_max_instructions_hits_counter,l1_max_configurations_accesses_counter,l1_max_configurations_hits_counter);
fprintf(results_cache,"%lu,%lu,%lu,%lu\n",l2_max_instructions_accesses_counter,l2_max_instructions_hits_counter,l2_max_configurations_accesses_counter,l2_max_configurations_hits_counter);
fprintf(results_cache,"%lu,%lu,%lu,%lu\n",l3_max_instructions_accesses_counter,l3_max_instructions_hits_counter,l3_max_configurations_accesses_counter,l3_max_configurations_hits_counter);
printf("Sintaxe: INSTRUCTIONS ACCESSES,INSTRUCTIONS HITS,CONFIGURATIONS ACCESSES,CONFIGURATIONS HITS\n");
printf("%lu,%lu,%lu,%lu\n",l1_max_instructions_accesses_counter,l1_max_instructions_hits_counter,l1_max_configurations_accesses_counter,l1_max_configurations_hits_counter);
printf("%lu,%lu,%lu,%lu\n",l2_max_instructions_accesses_counter,l2_max_instructions_hits_counter,l2_max_configurations_accesses_counter,l2_max_configurations_hits_counter);
printf("%lu,%lu,%lu,%lu\n",l3_max_instructions_accesses_counter,l3_max_instructions_hits_counter,l3_max_configurations_accesses_counter,l3_max_configurations_hits_counter);
fclose(results_cache);
}