-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrejit_handler.cpp
556 lines (469 loc) · 16.2 KB
/
rejit_handler.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include "rejit_handler.h"
#include "logger.h"
#include "stats.h"
namespace trace
{
//
// RejitHandlerModuleMethod
//
RejitHandlerModuleMethod::RejitHandlerModuleMethod(mdMethodDef methodDef,
RejitHandlerModule* module,
const FunctionInfo& functionInfo)
{
m_methodDef = methodDef;
SetFunctionInfo(functionInfo);
m_pFunctionControl = nullptr;
m_module = module;
}
mdMethodDef RejitHandlerModuleMethod::GetMethodDef()
{
return m_methodDef;
}
RejitHandlerModule* RejitHandlerModuleMethod::GetModule()
{
return m_module;
}
ICorProfilerFunctionControl* RejitHandlerModuleMethod::GetFunctionControl()
{
return m_pFunctionControl;
}
void RejitHandlerModuleMethod::SetFunctionControl(ICorProfilerFunctionControl* pFunctionControl)
{
m_pFunctionControl = pFunctionControl;
}
FunctionInfo* RejitHandlerModuleMethod::GetFunctionInfo()
{
return m_functionInfo.get();
}
void RejitHandlerModuleMethod::SetFunctionInfo(const FunctionInfo& functionInfo)
{
m_functionInfo = std::make_unique<FunctionInfo>(functionInfo);
}
bool RejitHandlerModuleMethod::RequestRejitForInlinersInModule(ModuleID moduleId)
{
// Enumerate all inliners and request rejit
ModuleID currentModuleId = m_module->GetModuleId();
mdMethodDef currentMethodDef = m_methodDef;
Logger::Debug("RejitHandlerModuleMethod::RequestRejitForInlinersInModule for ", "[ModuleInliner=", moduleId,
", ModuleId=", currentModuleId, ", MethodDef=", currentMethodDef, "]");
RejitHandler* handler = m_module->GetHandler();
ICorProfilerInfo7* pInfo = handler->GetCorProfilerInfo();
if (pInfo != nullptr)
{
// Now we enumerate all methods that inline the current methodDef
BOOL incompleteData = false;
ICorProfilerMethodEnum* methodEnum;
HRESULT hr = pInfo->EnumNgenModuleMethodsInliningThisMethod(moduleId, currentModuleId, currentMethodDef,
&incompleteData, &methodEnum);
std::ostringstream hexValue;
hexValue << std::hex << hr;
if (SUCCEEDED(hr))
{
COR_PRF_METHOD method;
unsigned int total = 0;
std::vector<ModuleID> modules;
std::vector<mdMethodDef> methods;
while (methodEnum->Next(1, &method, NULL) == S_OK)
{
Logger::Debug("NGEN:: Asking rewrite for inliner [ModuleId=", method.moduleId, ",MethodDef=",
method.methodId, "]");
modules.push_back(method.moduleId);
methods.push_back(method.methodId);
total++;
}
methodEnum->Release();
methodEnum = nullptr;
if (total > 0)
{
handler->EnqueueForRejit(modules, methods);
Logger::Info("NGEN:: Processed with ", total, " inliners [ModuleId=", currentModuleId, ",MethodDef=",
currentMethodDef, "]");
}
if (incompleteData)
{
Logger::Warn("NGen inliner data for module '", moduleId, "' is incomplete.");
return false;
}
}
else if (hr == E_INVALIDARG)
{
Logger::Info("NGEN:: Error Invalid arguments in [ModuleId=", currentModuleId, ",MethodDef=",
currentMethodDef, ", HR=", hexValue.str(), "]");
}
else if (hr == CORPROF_E_DATAINCOMPLETE)
{
Logger::Info("NGEN:: Error Incomplete data in [ModuleId=", currentModuleId, ",MethodDef=", currentMethodDef,
", HR=", hexValue.str(), "]");
return false;
}
else if (hr == CORPROF_E_UNSUPPORTED_CALL_SEQUENCE)
{
Logger::Info("NGEN:: Unsupported call sequence error in [ModuleId=", currentModuleId, ",MethodDef=",
currentMethodDef, ", HR=", hexValue.str(), "]");
}
else
{
Logger::Info("NGEN:: Error in [ModuleId=", currentModuleId, ",MethodDef=", currentMethodDef, ", HR=",
hexValue.str(), "]");
}
return true;
}
return false;
}
//
// TracerRejitHandlerModuleMethod
//
TracerRejitHandlerModuleMethod::TracerRejitHandlerModuleMethod(mdMethodDef methodDef,
RejitHandlerModule* module,
const FunctionInfo& functionInfo,
const IntegrationDefinition& integrationDefinition)
: RejitHandlerModuleMethod(methodDef, module, functionInfo)
, m_integrationDefinition(std::make_unique<IntegrationDefinition>(integrationDefinition))
{
}
IntegrationDefinition* TracerRejitHandlerModuleMethod::GetIntegrationDefinition()
{
return m_integrationDefinition.get();
}
MethodRewriter* TracerRejitHandlerModuleMethod::GetMethodRewriter()
{
return TracerMethodRewriter::Instance();
}
//
// RejitHandlerModule
//
RejitHandlerModule::RejitHandlerModule(ModuleID moduleId, RejitHandler* handler)
{
m_moduleId = moduleId;
m_metadata = nullptr;
m_handler = handler;
}
ModuleID RejitHandlerModule::GetModuleId()
{
return m_moduleId;
}
RejitHandler* RejitHandlerModule::GetHandler()
{
return m_handler;
}
ModuleMetadata* RejitHandlerModule::GetModuleMetadata()
{
return m_metadata.get();
}
void RejitHandlerModule::SetModuleMetadata(ModuleMetadata* metadata)
{
m_metadata = std::unique_ptr<ModuleMetadata>(metadata);
}
bool RejitHandlerModule::CreateMethodIfNotExists(const mdMethodDef methodDef,
RejitHandlerModuleMethodCreatorFunc creator)
{
std::lock_guard<std::mutex> guard(m_methods_lock);
auto find_res = m_methods.find(methodDef);
if (find_res != m_methods.end())
{
return false; // already exist and was not created
}
m_methods[methodDef] = creator(methodDef, this);
return true;
}
bool RejitHandlerModule::TryGetMethod(mdMethodDef methodDef, RejitHandlerModuleMethod** methodHandler)
{
std::lock_guard<std::mutex> guard(m_methods_lock);
auto find_res = m_methods.find(methodDef);
if (find_res != m_methods.end())
{
*methodHandler = find_res->second.get();
return true;
}
return false;
}
bool RejitHandlerModule::ContainsMethod(mdMethodDef methodDef)
{
std::lock_guard<std::mutex> guard(m_methods_lock);
return m_methods.find(methodDef) != m_methods.end();
}
void RejitHandlerModule::RequestRejitForInlinersInModule(ModuleID moduleId)
{
std::lock_guard<std::mutex> moduleGuard(m_ngenProcessedInlinerModulesLock);
// We check first if we already processed this module to skip it.
auto find_res = m_ngenProcessedInlinerModules.find(moduleId);
if (find_res != m_ngenProcessedInlinerModules.end())
{
return;
}
std::lock_guard<std::mutex> methodsGuard(m_methods_lock);
bool success = true;
for (const auto& method : m_methods)
{
success = success && method.second.get()->RequestRejitForInlinersInModule(moduleId);
// If we fail to process a method, we stop the processing and try again in another call.
if (!success)
{
break;
}
}
if (success)
{
// We mark module as processed.
m_ngenProcessedInlinerModules[moduleId] = true;
}
}
//
// RejitHandler
//
void RejitHandler::RequestRejit(std::vector<ModuleID>& modulesVector, std::vector<mdMethodDef>& modulesMethodDef)
{
if (IsShutdownRequested())
{
return;
}
// Request the ReJIT for all integrations found in the module.
HRESULT hr;
if (!modulesVector.empty())
{
// *************************************
// Request ReJIT
// *************************************
if (m_profilerInfo12 != nullptr)
{
// RequestReJITWithInliners is currently always failing with `Fatal error. Internal CLR error.
// (0x80131506)` more research is required, meanwhile we fallback to the normal RequestReJIT and
// manual track of inliners.
/*hr = m_profilerInfo12->RequestReJITWithInliners(COR_PRF_REJIT_BLOCK_INLINING, (ULONG)
modulesVector.size(),
&modulesVector[0], &modulesMethodDef[0]); if (FAILED(hr))
{
Warn("Error requesting ReJITWithInliners for ", vtModules.size(),
" methods, falling back to a normal RequestReJIT");
hr = m_profilerInfo12->RequestReJIT((ULONG) modulesVector.size(), &modulesVector[0],
&modulesMethodDef[0]);
}*/
hr = m_profilerInfo12->RequestReJIT((ULONG)modulesVector.size(), &modulesVector[0], &modulesMethodDef[0]);
}
else
{
hr = m_profilerInfo->RequestReJIT((ULONG)modulesVector.size(), &modulesVector[0], &modulesMethodDef[0]);
}
if (SUCCEEDED(hr))
{
Logger::Info("Request ReJIT done for ", modulesVector.size(), " methods");
}
else
{
Logger::Warn("Error requesting ReJIT for ", modulesVector.size(), " methods");
}
}
}
RejitHandler::RejitHandler(ICorProfilerInfo7* pInfo, std::shared_ptr<RejitWorkOffloader> work_offloader)
{
m_profilerInfo = pInfo;
m_profilerInfo12 = nullptr;
m_work_offloader = work_offloader;
}
RejitHandler::RejitHandler(ICorProfilerInfo12* pInfo, std::shared_ptr<RejitWorkOffloader> work_offloader)
{
m_profilerInfo = pInfo;
m_profilerInfo12 = pInfo;
m_work_offloader = work_offloader;
}
RejitHandlerModule* RejitHandler::GetOrAddModule(ModuleID moduleId)
{
if (IsShutdownRequested())
{
return nullptr;
}
std::lock_guard<std::mutex> guard(m_modules_lock);
auto find_res = m_modules.find(moduleId);
if (find_res != m_modules.end())
{
return find_res->second.get();
}
RejitHandlerModule* moduleHandler = new RejitHandlerModule(moduleId, this);
m_modules[moduleId] = std::unique_ptr<RejitHandlerModule>(moduleHandler);
return moduleHandler;
}
bool RejitHandler::HasModuleAndMethod(ModuleID moduleId, mdMethodDef methodDef)
{
if (IsShutdownRequested())
{
return false;
}
std::lock_guard<std::mutex> guard(m_modules_lock);
auto find_res = m_modules.find(moduleId);
if (find_res != m_modules.end())
{
auto moduleHandler = find_res->second.get();
return moduleHandler->ContainsMethod(methodDef);
}
return false;
}
void RejitHandler::RemoveModule(ModuleID moduleId)
{
if (IsShutdownRequested())
{
return;
}
std::lock_guard<std::mutex> guard(m_modules_lock);
m_modules.erase(moduleId);
}
void RejitHandler::AddNGenInlinerModule(ModuleID moduleId)
{
if (IsShutdownRequested())
{
return;
}
// Process the inliner module list ( to catch any incomplete data module )
// and also check if the module is already in the inliners list
std::lock_guard<std::mutex> guard(m_ngenInlinersModules_lock);
bool alreadyAdded = false;
if (m_profilerInfo != nullptr)
{
for (const auto& moduleInliner : m_ngenInlinersModules)
{
if (moduleInliner == moduleId)
{
alreadyAdded = true;
}
else
{
std::lock_guard<std::mutex> guard(m_modules_lock);
for (const auto& mod : m_modules)
{
mod.second->RequestRejitForInlinersInModule(moduleInliner);
}
}
}
}
if (!alreadyAdded)
{
// Add the new module inliner
m_ngenInlinersModules.push_back(moduleId);
if (m_profilerInfo != nullptr)
{
std::lock_guard<std::mutex> guard(m_modules_lock);
for (const auto& mod : m_modules)
{
mod.second->RequestRejitForInlinersInModule(moduleId);
}
}
}
}
void RejitHandler::EnqueueForRejit(std::vector<ModuleID>& modulesVector, std::vector<mdMethodDef>& modulesMethodDef)
{
if (IsShutdownRequested() || modulesVector.size() == 0 || modulesMethodDef.size() == 0)
{
return;
}
Logger::Debug("RejitHandler::EnqueueForRejit");
std::function<void()> action =
[ =, modules = std::move(modulesVector), methods = std::move(modulesMethodDef) ]() mutable
{
// Request ReJIT
RequestRejit(modules, methods);
};
// Enqueue
m_work_offloader->Enqueue(std::make_unique<RejitWorkItem>(std::move(action)));
}
void RejitHandler::Shutdown()
{
Logger::Debug("RejitHandler::Shutdown");
// Wait for exiting the thread
m_work_offloader->Enqueue(RejitWorkItem::CreateTerminatingWorkItem());
m_work_offloader->WaitForTermination();
std::lock_guard<std::mutex> moduleGuard(m_modules_lock);
std::lock_guard<std::mutex> ngenModuleGuard(m_ngenInlinersModules_lock);
WriteLock w_lock(m_shutdown_lock);
m_shutdown.store(true);
m_modules.clear();
m_profilerInfo = nullptr;
m_profilerInfo12 = nullptr;
}
bool RejitHandler::IsShutdownRequested()
{
ReadLock r_lock(m_shutdown_lock);
return m_shutdown;
}
HRESULT RejitHandler::NotifyReJITParameters(ModuleID moduleId,
mdMethodDef methodId,
ICorProfilerFunctionControl* pFunctionControl)
{
if (IsShutdownRequested())
{
return S_FALSE;
}
auto moduleHandler = GetOrAddModule(moduleId);
if (moduleHandler == nullptr)
{
return S_FALSE;
}
RejitHandlerModuleMethod* methodHandler = nullptr;
if (!moduleHandler->TryGetMethod(methodId, &methodHandler))
{
return S_FALSE;
}
methodHandler->SetFunctionControl(pFunctionControl);
if (methodHandler->GetMethodDef() == mdMethodDefNil)
{
Logger::Warn("NotifyReJITCompilationStarted: mdMethodDef is missing for "
"MethodDef: ",
methodId);
return S_FALSE;
}
if (methodHandler->GetFunctionControl() == nullptr)
{
Logger::Warn("NotifyReJITCompilationStarted: ICorProfilerFunctionControl is missing "
"for "
"MethodDef: ",
methodId);
return S_FALSE;
}
if (methodHandler->GetFunctionInfo() == nullptr)
{
Logger::Warn("NotifyReJITCompilationStarted: FunctionInfo is missing for "
"MethodDef: ",
methodId);
return S_FALSE;
}
if (moduleHandler->GetModuleId() == 0)
{
Logger::Warn("NotifyReJITCompilationStarted: ModuleID is missing for "
"MethodDef: ",
methodId);
return S_FALSE;
}
if (moduleHandler->GetModuleMetadata() == nullptr)
{
Logger::Warn("NotifyReJITCompilationStarted: ModuleMetadata is missing for "
"MethodDef: ",
methodId);
return S_FALSE;
}
auto rewriter = methodHandler->GetMethodRewriter();
if (rewriter == nullptr)
{
Logger::Error("NotifyReJITCompilationStarted: The rewriter is missing for "
"MethodDef: ",
methodId, ", methodHandler type name = ", typeid(methodHandler).name());
return S_FALSE;
}
return rewriter->Rewrite(moduleHandler, methodHandler);
}
HRESULT RejitHandler::NotifyReJITCompilationStarted(FunctionID functionId, ReJITID rejitId)
{
return S_OK;
}
ICorProfilerInfo7* RejitHandler::GetCorProfilerInfo()
{
return m_profilerInfo;
}
void RejitHandler::SetCorAssemblyProfiler(AssemblyProperty* pCorAssemblyProfiler)
{
m_pCorAssemblyProperty = pCorAssemblyProfiler;
}
AssemblyProperty* RejitHandler::GetCorAssemblyProperty()
{
return m_pCorAssemblyProperty;
}
} // namespace trace