forked from edf-hpc/verrou
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvr_clreq.c
More file actions
410 lines (363 loc) · 12.3 KB
/
vr_clreq.c
File metadata and controls
410 lines (363 loc) · 12.3 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
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
/*--------------------------------------------------------------------*/
/*--- Verrou: a FPU instrumentation tool. ---*/
/*--- This file contains code related to client requests handling. ---*/
/*--- vr_clreq.c ---*/
/*--------------------------------------------------------------------*/
/*
This file is part of Verrou, a FPU instrumentation tool.
Copyright (C) 2014-2021 EDF
F. Févotte <francois.fevotte@edf.fr>
B. Lathuilière <bruno.lathuiliere@edf.fr>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307, USA.
The GNU General Public License is contained in the file COPYING.
*/
#include "vr_main.h"
#include "vr_clreq.h"
#include "pub_tool_transtab.h" // VG_(discard_translations_safely)
// * Start-stop instrumentation
#ifdef PROFILING_EXACT
#include "interflop_backends/interflop_verrou/interflop_verrou.h"
#endif
#include "interflop_backends/backend_checkdenorm/interflop_checkdenorm.h"
void vr_set_instrument_state (const HChar* reason, Vr_Instr state, Bool isHard) {
if(isHard){
if (vr.instrument_hard == state) {
if(vr.verbose){
VG_(message)(Vg_DebugMsg,"%s: instrumentation already %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
return;
}
vr.instrument_hard = state;
VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "verrou");
vr_clean_cache();
if(vr.instrument_soft_used){
if(vr.instrument_hard && vr.instrument_soft){
vr.instrument_soft_used=False;
}
}
if(vr.verbose){
VG_(message)(Vg_DebugMsg, "%s: instrumentation switched %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
}else{//soft
if(vr.instrument_soft_used==False && vr.instrument_hard==VR_INSTR_ON ){
VG_(discard_translations_safely)( (Addr)0x1000, ~(SizeT)0xfff, "verrou");
}
vr.instrument_soft_used= True;
if (vr.instrument_soft == state) {
if(vr.verbose){
VG_(message)(Vg_DebugMsg,"%s: instrumentation (soft) already %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
return;
}
vr.instrument_soft = state;
vr_clean_cache();
if(vr.verbose){
VG_(message)(Vg_DebugMsg, "%s: instrumentation (soft) switched %s\n",
reason, (state==VR_INSTR_ON) ? "ON" : "OFF");
}
}
}
// * Enter/leave deterministic section
static void vr_deterministic_section_name (unsigned int level,
HChar * name,
unsigned int len)
{
Addr ips[8];
const HChar* fnname;
const HChar* filename;
UInt linenum;
Addr addr;
DiEpoch de = VG_(current_DiEpoch)();
VG_(get_StackTrace)(VG_(get_running_tid)(),
ips, 8,
NULL, NULL,
0);
addr = ips[level];
//fnname[0] = 0;
VG_(get_fnname)(de, addr, &fnname);
// filename[0] = 0;
VG_(get_filename_linenum)(de,
addr,
&filename,
NULL,
&linenum);
VG_(snprintf)(name, len,
"%s (%s:%u)", fnname, filename, linenum);
}
static ULong vr_deterministic_section_hash (HChar const*const name)
{
ULong hash = VG_(getpid)();
int i = 0;
while (name[i] != 0) {
hash += i * name[i];
++i;
}
return hash;
}
static void vr_start_deterministic_section (unsigned int level) {
HChar name[256];
ULong hash;
vr_deterministic_section_name (level, name, 256);
hash = vr_deterministic_section_hash (name);
verrou_seed_save_state ();
verrou_set_seed (hash);
#ifdef USE_VERROU_QUADMATH
mcaquad_set_seed (hash);
#endif
VG_(message)(Vg_DebugMsg, "Entering deterministic section %llu: %s\n",
hash, name);
}
static void vr_stop_deterministic_section (unsigned int level) {
HChar name[256];
vr_deterministic_section_name (level, name, 256);
VG_(message)(Vg_DebugMsg, "Leaving deterministic section: %s\n",
name);
verrou_seed_restore_state ();
#ifdef USE_VERROU_QUADMATH
mcaquad_set_random_seed ();
#endif
}
#ifdef PROFILING_EXACT
static void vr_print_profiling_exact(void){
unsigned int num;
unsigned int numExact;
verrou_get_profiling_exact(&num,&numExact);
VG_(umsg)("%s #total: %u #exact: %u\n","Profiling exact", num, numExact);
}
#endif
void vr_print_denorm_counter(void){
if(vr.backend==vr_checkdenorm){
checkdenorm_print_counter(&VG_(umsg));
}else{
if(vr.verbose){
VG_(umsg)("PRINT_COUNT_DENORM client request needs --count-denorm=yes\n");
}
}
}
void vr_reset_denorm_counter(void){
if(vr.backend==vr_checkdenorm){
checkdenorm_reset_counter();
}else{
if(vr.verbose){
VG_(umsg)("RESET_COUNT_DENORM client request needs --count-denorm=yes\n");
}
}
}
// * Handle client requests
// ** GDB monitor commands
static void vr_handle_monitor_instrumentation_print (void) {
VG_(gdb_printf) ("instrumentation: %s\n",
(vr.instrument_hard && vr.instrument_soft) ==VR_INSTR_ON ? "on" : "off");
}
static Bool vr_handle_monitor_instrumentation (HChar ** ssaveptr) {
HChar * arg = VG_(strtok_r)(0, " ", ssaveptr);
if (!arg) { /* no argument */
vr_handle_monitor_instrumentation_print();
return True;
}
switch (VG_(keyword_id) ("on off", arg, kwd_report_duplicated_matches)) {
case -2: /* multiple matches */
return True;
case -1: /* not found */
return False;
case 0: /* on */
vr_set_instrument_state("Monitor", VR_INSTR_ON, False);
vr_handle_monitor_instrumentation_print();
return True;
case 1: /* off */
vr_set_instrument_state("Monitor", VR_INSTR_OFF, False);
vr_handle_monitor_instrumentation_print();
return True;
}
return False;
}
static Bool vr_handle_monitor_help (void) {
VG_(gdb_printf)("\n");
VG_(gdb_printf)("verrou monitor commands:\n");
VG_(gdb_printf)(" help : print this help\n");
VG_(gdb_printf)(" count : print instruction counters\n");
VG_(gdb_printf)(" instrumentation : get instrumentation state\n");
VG_(gdb_printf)(" instrumentation [on|off] : set instrumentation state\n");
VG_(gdb_printf)("\n");
return True;
}
static Bool vr_handle_monitor_command (HChar * req) {
HChar * wcmd;
HChar s[VG_(strlen(req)) + 1];
HChar *ssaveptr;
VG_(strcpy)(s, req);
wcmd = VG_(strtok_r)(s, " ", &ssaveptr);
switch (VG_(keyword_id) ("help instrumentation count",
wcmd, kwd_report_duplicated_matches)) {
case -2: /* multiple matches */
return True;
case -1: /* not found */
return False;
case 0: /* help */
return vr_handle_monitor_help ();
case 1: /* instrumentation */
return vr_handle_monitor_instrumentation (&ssaveptr);
case 2: /* count */
vr_ppOpCount();
return True;
}
return False;
}
// ** Client requests entry point
Bool vr_handle_client_request (ThreadId tid, UWord *args, UWord *ret) {
if (!VG_IS_TOOL_USERREQ('V','R', args[0])
&& VG_USERREQ__GDB_MONITOR_COMMAND != args[0])
return False;
switch (args[0]) {
case VR_USERREQ__START_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", True, True);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__STOP_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", False, True);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__START_SOFT_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", True, False);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__STOP_SOFT_INSTRUMENTATION:
vr_set_instrument_state ("Client Request", False, False);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__START_DETERMINISTIC:
vr_start_deterministic_section (args[1]);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__STOP_DETERMINISTIC:
vr_stop_deterministic_section (args[1]);
*ret = 0; /* meaningless */
break;
case VR_USERREQ__DISPLAY_COUNTERS:
vr_ppOpCount();
*ret = 0; /* meaningless */
break;
case VR_USERREQ__DUMP_COVER:
*ret=vr_traceBB_dumpCov();
break;
case VR_USERREQ__PRINT_PROFILING_EXACT:
#ifdef PROFILING_EXACT
vr_print_profiling_exact();
verrou_init_profiling_exact();
#else
VG_(umsg)("Warning : verrou need to be compiled with --enable-verrou-profiling-exact to use the client request VERROU_PRINT_PROFILING_EXACT;\n");
#endif
*ret=0;
break;
case VR_USERREQ__COUNT_FP_INSTRUMENTED:
*ret=(UWord)vr_count_fp_instrumented();
break;
case VR_USERREQ__COUNT_FP_NOT_INSTRUMENTED:
*ret=(UWord)vr_count_fp_not_instrumented();
break;
case VG_USERREQ__GDB_MONITOR_COMMAND:
if (vr_handle_monitor_command((HChar*)args[1])) {
*ret = 1;
return True;
} else {
*ret = 0;
return False;
}
break;
case VR_USERREQ__SET_SEED:
vr.firstSeed=args[1];
VG_(umsg)("New seed : %llu\n", vr.firstSeed);
verrou_set_seed (vr.firstSeed);
vr_clean_cache_seed();
#ifdef USE_VERROU_QUADMATH
mcaquad_set_seed(vr.firstSeed);
#endif
*ret = 0; /* meaningless */
break;
case VR_USERREQ__PRANDOM_UPDATE:
verrou_updatep_prandom();
if(vr.verbose){
if( (vr.roundingMode==VR_PRANDOM)||(vr.roundingMode==VR_PRANDOM_DET) ||(vr.roundingMode==VR_PRANDOM_COMDET)){
VG_(umsg)("\tPRANDOM_UPDATE: pvalue=%f\n", verrou_prandom_pvalue());
}
}
break;
case VR_USERREQ__PRANDOM_UPDATE_VALUE:
{
double value=VG_(strtod)((char*)args[1],NULL);
verrou_updatep_prandom_double(value);
}
if(vr.verbose){
if( (vr.roundingMode==VR_PRANDOM)||(vr.roundingMode==VR_PRANDOM_DET) ||(vr.roundingMode==VR_PRANDOM_COMDET)){
VG_(umsg)("\tPRANDOM_UPDATE_VALUE: pvalue=%f\n", verrou_prandom_pvalue());
}
}
break;
case VR_USERREQ__GET_ROUNDING:
*ret=(UWord)vr.roundingMode;
break;
case VR_USERREQ__GET_LIBM_ROUNDING:
*ret=(UWord)vr.roundingMode;
break;
case VR_USERREQ__GET_LIBM_ROUNDING_NO_INST:
*ret=(UWord)vr.roundingModeNoInst;
break;
case VR_USERREQ__NAN_DETECTED:
vr_handle_NaN();
break;
case VR_USERREQ__INF_DETECTED:
vr_handle_Inf();
break;
case VR_USERREQ__IS_INSTRUMENTED_FLOAT:
*ret=(UWord)( (vr.instrument_hard && vr.instrument_soft) && vr.instr_prec[VR_PREC_FLT] );
break;
case VR_USERREQ__IS_INSTRUMENTED_DOUBLE:
*ret=(UWord)( (vr.instrument_hard && vr.instrument_soft) && vr.instr_prec[VR_PREC_DBL] );
break;
case VR_USERREQ__IS_INSTRUMENTED_LDOUBLE:
*ret=(UWord)( (vr.instrument_hard && vr.instrument_soft) && vr.instr_prec[VR_PREC_LDBL] );
break;
case VR_USERREQ__COUNT_OP:
*ret=(UWord)( (Bool)(vr.count) );
break;
case VR_USERREQ__GENERATE_EXCLUDE_SOURCE:
vr_generate_exclude_source((const char*)args[1], *(int*)args[2], (const char*)args[3], (const char*)args[4] );
break;
case VR_USERREQ__IS_INSTRUMENTED_EXCLUDE_SOURCE:
*ret=(UWord)( (Bool)(vr_clrIsInstrumented((const char*)args[1], *(int*)args[2], (const char*)args[3],(const char*)args[4] )));
break;
case VR_USERREQ__REGISTER_CACHE:
vr_register_cache((unsigned int*) args[1], (unsigned int)args[2]);
break;
case VR_USERREQ__GET_SEED:
*ret = verrou_get_seed();
break;
case VR_USERREQ__REGISTER_CACHE_SEED:
vr_register_cache_seed((unsigned int*) args[1]);
break;
case VR_USERREQ__FLOAT_CONV:
*ret=(UWord)( vr.float_conv );
break;
case VR_USERREQ__PRINT_COUNT_DENORM:
vr_print_denorm_counter();
break;
case VR_USERREQ__RESET_COUNT_DENORM:
vr_reset_denorm_counter();
break;
}
return True;
}