@@ -36,9 +36,10 @@ namespace KINETO_NAMESPACE {
36
36
#if __linux__ || defined(HAS_CUPTI)
37
37
static bool initialized = false ;
38
38
39
- static void initProfilersCPU () {
39
+ static void initProfilers () {
40
40
if (!initialized) {
41
41
libkineto::api ().initProfilerIfRegistered ();
42
+ libkineto::api ().configLoader ().initBaseConfig ();
42
43
initialized = true ;
43
44
VLOG (0 ) << " libkineto profilers activated" ;
44
45
}
@@ -47,11 +48,6 @@ static void initProfilersCPU() {
47
48
#endif // __linux__ || defined(HAS_CUPTI)
48
49
49
50
#ifdef HAS_CUPTI
50
- static std::mutex& initEventMutex () {
51
- static std::mutex initMutex_;
52
- return initMutex_;
53
- }
54
-
55
51
bool enableEventProfiler () {
56
52
if (getenv (" KINETO_ENABLE_EVENT_PROFILER" ) != nullptr ) {
57
53
return true ;
@@ -60,28 +56,15 @@ bool enableEventProfiler() {
60
56
}
61
57
}
62
58
63
- static void initProfilers (
59
+ static void initProfilersCallback (
64
60
CUpti_CallbackDomain /* domain*/ ,
65
61
CUpti_CallbackId /* cbid*/ ,
66
- const CUpti_CallbackData* cbInfo) {
62
+ const CUpti_CallbackData* /* cbInfo*/ ) {
67
63
VLOG (0 ) << " CUDA Context created" ;
68
- initProfilersCPU ();
64
+ initProfilers ();
69
65
70
- if (!enableEventProfiler ()) {
71
- VLOG (0 ) << " Kineto EventProfiler disabled, skipping start" ;
72
- return ;
73
- } else {
74
- std::lock_guard<std::mutex> lock (initEventMutex ());
75
- CUpti_ResourceData* d = (CUpti_ResourceData*)cbInfo;
76
- CUcontext ctx = d->context ;
77
- ConfigLoader& config_loader = libkineto::api ().configLoader ();
78
- config_loader.initBaseConfig ();
79
- auto config = config_loader.getConfigCopy ();
80
- if (config->eventProfilerEnabled ()) {
81
- // This function needs to be called under lock.
82
- EventProfilerController::start (ctx, config_loader);
83
- LOG (INFO) << " Kineto EventProfiler started" ;
84
- }
66
+ if (enableEventProfiler ()) {
67
+ LOG (WARNING) << " Event Profiler is no longer supported in kineto" ;
85
68
}
86
69
}
87
70
@@ -98,17 +81,39 @@ static bool shouldPreloadCuptiInstrumentation() {
98
81
#endif
99
82
}
100
83
101
- static void stopProfiler (
102
- CUpti_CallbackDomain /* domain*/ ,
103
- CUpti_CallbackId /* cbid*/ ,
104
- const CUpti_CallbackData* cbInfo) {
105
- VLOG (0 ) << " CUDA Context destroyed" ;
106
- std::lock_guard<std::mutex> lock (initEventMutex ());
107
- CUpti_ResourceData* d = (CUpti_ResourceData*)cbInfo;
108
- CUcontext ctx = d->context ;
109
- // This function needs to be called under lock.
110
- EventProfilerController::stopIfEnabled (ctx);
111
- LOG (INFO) << " Kineto EventProfiler stopped" ;
84
+ bool setupCuptiInitCallback (bool logOnError) {
85
+ // libcupti will be lazily loaded on this call.
86
+ // If it is not available (e.g. CUDA is not installed),
87
+ // then this call will return an error and we just abort init.
88
+ auto cbapi = CuptiCallbackApi::singleton ();
89
+ cbapi->initCallbackApi ();
90
+
91
+ bool status = false ;
92
+
93
+ if (cbapi->initSuccess ()) {
94
+ const CUpti_CallbackDomain domain = CUPTI_CB_DOMAIN_RESOURCE;
95
+ status = cbapi->registerCallback (
96
+ domain,
97
+ CuptiCallbackApi::RESOURCE_CONTEXT_CREATED,
98
+ initProfilersCallback);
99
+ if (status) {
100
+ status = cbapi->enableCallback (
101
+ domain, CuptiCallbackApi::RESOURCE_CONTEXT_CREATED);
102
+ }
103
+ }
104
+
105
+ if (!cbapi->initSuccess () || !status) {
106
+ if (logOnError) {
107
+ CUPTI_CALL (cbapi->getCuptiStatus ());
108
+ LOG (WARNING) << " CUPTI initialization failed - "
109
+ << " CUDA profiler activities will be missing" ;
110
+ LOG (INFO)
111
+ << " If you see CUPTI_ERROR_INSUFFICIENT_PRIVILEGES, refer to "
112
+ << " https://developer.nvidia.com/nvidia-development-tools-solutions-err-nvgpuctrperm-cupti" ;
113
+ }
114
+ }
115
+
116
+ return status;
112
117
}
113
118
114
119
static std::unique_ptr<CuptiRangeProfilerInit> rangeProfilerInit;
@@ -120,7 +125,6 @@ static std::unique_ptr<CuptiRangeProfilerInit> rangeProfilerInit;
120
125
using namespace KINETO_NAMESPACE ;
121
126
extern " C" {
122
127
123
- // Return true if no CUPTI errors occurred during init
124
128
void libkineto_init (bool cpuOnly, bool logOnError) {
125
129
// Start with initializing the log level
126
130
const char * logLevelEnv = getenv (" KINETO_LOG_LEVEL" );
@@ -139,60 +143,22 @@ void libkineto_init(bool cpuOnly, bool logOnError) {
139
143
#endif
140
144
141
145
#ifdef HAS_CUPTI
142
- if (!cpuOnly) {
143
- // libcupti will be lazily loaded on this call.
144
- // If it is not available (e.g. CUDA is not installed),
145
- // then this call will return an error and we just abort init.
146
- auto cbapi = CuptiCallbackApi::singleton ();
147
- cbapi->initCallbackApi ();
148
- bool status = false ;
149
- bool initRangeProfiler = true ;
150
-
151
- if (cbapi->initSuccess ()) {
152
- const CUpti_CallbackDomain domain = CUPTI_CB_DOMAIN_RESOURCE;
153
- status = cbapi->registerCallback (
154
- domain, CuptiCallbackApi::RESOURCE_CONTEXT_CREATED, initProfilers);
155
- if (status) {
156
- status = cbapi->enableCallback (
157
- domain, CuptiCallbackApi::RESOURCE_CONTEXT_CREATED);
158
- }
159
-
160
- // Register stopProfiler callback only for event profiler.
161
- // This callback is not required for activities tracing.
162
- if (enableEventProfiler ()) {
163
- if (status) {
164
- status = cbapi->registerCallback (
165
- domain,
166
- CuptiCallbackApi::RESOURCE_CONTEXT_DESTROYED,
167
- stopProfiler);
168
- }
169
- if (status) {
170
- status = cbapi->enableCallback (
171
- domain, CuptiCallbackApi::RESOURCE_CONTEXT_DESTROYED);
172
- }
173
- }
174
- }
146
+ bool initRangeProfiler = true ;
175
147
176
- if (!cbapi->initSuccess () || !status) {
177
- initRangeProfiler = false ;
178
- cpuOnly = true ;
179
- if (logOnError) {
180
- CUPTI_CALL (cbapi->getCuptiStatus ());
181
- LOG (WARNING) << " CUPTI initialization failed - "
182
- << " CUDA profiler activities will be missing" ;
183
- LOG (INFO)
184
- << " If you see CUPTI_ERROR_INSUFFICIENT_PRIVILEGES, refer to "
185
- << " https://developer.nvidia.com/nvidia-development-tools-solutions-err-nvgpuctrperm-cupti" ;
186
- }
187
- }
148
+ if (!cpuOnly && !libkineto::isDaemonEnvVarSet ()) {
149
+ bool success = setupCuptiInitCallback (logOnError);
150
+ cpuOnly = !success;
151
+ initRangeProfiler = success;
152
+ }
188
153
189
- // initialize CUPTI Range Profiler API
190
- if (initRangeProfiler) {
191
- rangeProfilerInit = std::make_unique<CuptiRangeProfilerInit>();
192
- }
154
+ // Initialize CUPTI Range Profiler API
155
+ // Note: the following is a no-op if Range Profiler is not supported
156
+ // currently it is only enabled in fbcode.
157
+ if (!cpuOnly && initRangeProfiler) {
158
+ rangeProfilerInit = std::make_unique<CuptiRangeProfilerInit>();
193
159
}
194
160
195
- if (shouldPreloadCuptiInstrumentation ()) {
161
+ if (!cpuOnly && shouldPreloadCuptiInstrumentation ()) {
196
162
CuptiActivityApi::forceLoadCupti ();
197
163
}
198
164
#endif // HAS_CUPTI
@@ -224,13 +190,10 @@ void libkineto_init(bool cpuOnly, bool logOnError) {
224
190
#endif // HAS_XPUPTI
225
191
226
192
#if __linux__
227
- // When CUDA/GPU is used the profiler initialization happens on the
228
- // creation of the first CUDA stream (see initProfilers()).
229
- // This section bootstraps the profiler and its connection to a profiling
230
- // daemon in the CPU only case.
231
- if (cpuOnly && getenv (kUseDaemonEnvVar ) != nullptr ) {
232
- initProfilersCPU ();
233
- libkineto::api ().configLoader ().initBaseConfig ();
193
+ // For open source users that would like to connect to a profiling daemon
194
+ // we should always initialize the profiler at this point.
195
+ if (libkineto::isDaemonEnvVarSet ()) {
196
+ initProfilers ();
234
197
}
235
198
#endif
236
199
}
0 commit comments