1515#include < chrono>
1616#include < fstream>
1717
18- #include " libkineto.h"
19- #include " ActivityProfilerProxy.h"
2018#include " DaemonConfigLoader.h"
2119
2220#include " Logger.h"
@@ -120,7 +118,7 @@ void ConfigLoader::setDaemonConfigLoaderFactory(
120118}
121119
122120ConfigLoader& ConfigLoader::instance () {
123- static ConfigLoader config_loader ( libkineto::api ()) ;
121+ static ConfigLoader config_loader;
124122 return config_loader;
125123}
126124
@@ -130,9 +128,8 @@ std::string ConfigLoader::readOnDemandConfigFromDaemon(
130128 if (!daemonConfigLoader_) {
131129 return " " ;
132130 }
133- bool events =
134- now > onDemandEventProfilerConfig_->eventProfilerOnDemandEndTime ();
135- bool activities = !libkinetoApi_.activityProfiler ().isActive ();
131+ bool events = canHandlerAcceptConfig (ConfigKind::EventProfiler);
132+ bool activities = canHandlerAcceptConfig (ConfigKind::ActivityProfiler);
136133 return daemonConfigLoader_->readOnDemandConfig (events, activities);
137134}
138135
@@ -144,10 +141,8 @@ int ConfigLoader::contextCountForGpu(uint32_t device) {
144141 return daemonConfigLoader_->gpuContextCount (device);
145142}
146143
147- ConfigLoader::ConfigLoader (LibkinetoApi& api)
148- : libkinetoApi_(api),
149- onDemandEventProfilerConfig_ (new Config()),
150- configUpdateIntervalSecs_(kConfigUpdateIntervalSecs ),
144+ ConfigLoader::ConfigLoader ()
145+ : configUpdateIntervalSecs_(kConfigUpdateIntervalSecs ),
151146 onDemandConfigUpdateIntervalSecs_ (kOnDemandConfigUpdateIntervalSecs ),
152147 stopFlag_(false ),
153148 onDemandSignal_(false ) {
@@ -158,15 +153,21 @@ ConfigLoader::ConfigLoader(LibkinetoApi& api)
158153 config_.parse (readConfigFromConfigFile (configFileName_));
159154 SET_LOG_VERBOSITY_LEVEL (config_.verboseLogLevel (), config_.verboseLogModules ());
160155 setupSignalHandler (config_.sigUsr2Enabled ());
161- if (daemonConfigLoaderFactory ()) {
162- daemonConfigLoader_ = daemonConfigLoaderFactory ()();
163- daemonConfigLoader_->setCommunicationFabric (config_.ipcFabricEnabled ());
156+ }
157+
158+ void ConfigLoader::startThread () {
159+ if (!updateThread_) {
160+ if (daemonConfigLoaderFactory ()) {
161+ daemonConfigLoader_ = daemonConfigLoaderFactory ()();
162+ daemonConfigLoader_->setCommunicationFabric (config_.ipcFabricEnabled ());
163+ }
164+ updateThread_ =
165+ std::make_unique<std::thread>(&ConfigLoader::updateConfigThread, this );
164166 }
165- updateThread_ =
166- std::make_unique<std::thread>(&ConfigLoader::updateConfigThread, this );
167167}
168168
169169ConfigLoader::~ConfigLoader () {
170+ LOG (INFO) << " ConfigLoader::~ConfigLoader()" ;
170171 if (updateThread_) {
171172 stopFlag_ = true ;
172173 {
@@ -211,51 +212,23 @@ void ConfigLoader::configureFromSignal(
211212 if (daemonConfigLoader_) {
212213 daemonConfigLoader_->setCommunicationFabric (config_.ipcFabricEnabled ());
213214 }
214- if (eventProfilerRequest (config)) {
215- if (now > onDemandEventProfilerConfig_->eventProfilerOnDemandEndTime ()) {
216- LOG (INFO) << " Starting on-demand event profiling from signal" ;
217- std::lock_guard<std::mutex> lock (configLock_);
218- onDemandEventProfilerConfig_ = config.clone ();
219- } else {
220- LOG (ERROR) << " On-demand event profiler is busy" ;
221- }
222- }
223- // Initiate a trace by default, even when not specified in the config.
224- // Set trace duration and iterations to 0 to suppress.
225- config.updateActivityProfilerRequestReceivedTime ();
226- try {
227- auto & profiler = dynamic_cast <ActivityProfilerProxy&>(
228- libkinetoApi_.activityProfiler ());
229- profiler.scheduleTrace (config);
230- } catch (const std::exception& e) {
231- LOG (ERROR) << " Failed to schedule profiler request (busy?)" ;
232- }
215+ notifyHandlers (config);
233216}
234217
235218void ConfigLoader::configureFromDaemon (
236219 time_point<system_clock> now,
237220 Config& config) {
238221 const std::string config_str = readOnDemandConfigFromDaemon (now);
239- LOG_IF (INFO, !config_str.empty ()) << " Received config from dyno:\n "
240- << config_str;
222+ if (config_str.empty ()) {
223+ return ;
224+ }
225+
226+ LOG (INFO) << " Received config from dyno:\n " << config_str;
241227 config.parse (config_str);
242228 if (daemonConfigLoader_) {
243229 daemonConfigLoader_->setCommunicationFabric (config_.ipcFabricEnabled ());
244230 }
245- if (eventProfilerRequest (config)) {
246- std::lock_guard<std::mutex> lock (configLock_);
247- onDemandEventProfilerConfig_ = config.clone ();
248- }
249- if (config_.activityProfilerEnabled () &&
250- config.activityProfilerRequestReceivedTime () > now) {
251- try {
252- auto & profiler = dynamic_cast <ActivityProfilerProxy&>(
253- libkinetoApi_.activityProfiler ());
254- profiler.scheduleTrace (config);
255- } catch (const std::exception& e) {
256- LOG (ERROR) << " Failed to schedule profiler request (busy?)" ;
257- }
258- }
231+ notifyHandlers (config);
259232}
260233
261234void ConfigLoader::updateConfigThread () {
@@ -313,10 +286,4 @@ bool ConfigLoader::hasNewConfig(const Config& oldConfig) {
313286 return config_.timestamp () > oldConfig.timestamp ();
314287}
315288
316- bool ConfigLoader::hasNewEventProfilerOnDemandConfig (const Config& oldConfig) {
317- std::lock_guard<std::mutex> lock (configLock_);
318- return onDemandEventProfilerConfig_->eventProfilerOnDemandStartTime () >
319- oldConfig.eventProfilerOnDemandStartTime ();
320- }
321-
322289} // namespace KINETO_NAMESPACE
0 commit comments