99#include " TargetRegistry.h"
1010#include " ResourceRegistry.h"
1111
12- static std::string getClusterTypeResourceNodePath (Resource* resource, int32_t clusterID) {
13- ResConfInfo* resourceConfig =
14- ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
15-
16- if (resourceConfig == nullptr ) return " " ;
17- std::string filePath = resourceConfig->mResourcePath ;
12+ static std::string getFullResourceNodePath (ResConfInfo* rConf, int32_t id) {
13+ if (rConf == nullptr ) return " " ;
14+ std::string filePath = rConf->mResourcePath ;
1815
1916 // Replace %d in above file path with the actual cluster id
2017 char pathBuffer[128 ];
21- std::snprintf (pathBuffer, sizeof (pathBuffer), filePath.c_str (), clusterID);
22- filePath = std::string (pathBuffer);
23-
24- return filePath;
25- }
26-
27- static std::string getCoreTypeResourceNodePath (Resource* resource, int32_t coreID) {
28- ResConfInfo* resourceConfig =
29- ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
30-
31- if (resourceConfig == nullptr ) return " " ;
32- std::string filePath = resourceConfig->mResourcePath ;
33-
34- // Replace %d in above file path with the actual core id
35- char pathBuffer[128 ];
36- std::snprintf (pathBuffer, sizeof (pathBuffer), filePath.c_str (), coreID);
18+ std::snprintf (pathBuffer, sizeof (pathBuffer), filePath.c_str (), id);
3719 filePath = std::string (pathBuffer);
3820
3921 return filePath;
4022}
4123
4224static std::string getCGroupTypeResourceNodePath (Resource* resource, const std::string& cGroupName) {
43- ResConfInfo* resourceConfig =
44- ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
25+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
4526
46- if (resourceConfig == nullptr ) return " " ;
47- std::string filePath = resourceConfig ->mResourcePath ;
27+ if (rConf == nullptr ) return " " ;
28+ std::string filePath = rConf ->mResourcePath ;
4829
4930 // Replace %s in above file path with the actual cgroup name
5031 char pathBuffer[128 ];
@@ -58,11 +39,13 @@ static std::string getCGroupTypeResourceNodePath(Resource* resource, const std::
5839void defaultClusterLevelApplierCb (void * context) {
5940 if (context == nullptr ) return ;
6041 Resource* resource = static_cast <Resource*>(context);
42+
6143 ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
44+ if (rConf == nullptr ) return ;
6245
6346 // Get the Cluster ID
6447 int32_t clusterID = resource->getClusterValue ();
65- std::string resourceNodePath = getClusterTypeResourceNodePath (resource , clusterID);
48+ std::string resourceNodePath = getFullResourceNodePath (rConf , clusterID);
6649
6750 // 32-bit, unit-dependent value to be written
6851 int32_t valueToBeWritten = resource->getValueAt (0 );
@@ -97,20 +80,22 @@ void defaultClusterLevelTearCb(void* context) {
9780 if (context == nullptr ) return ;
9881 Resource* resource = static_cast <Resource*>(context);
9982
83+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
84+ if (rConf == nullptr ) return ;
85+
10086 // Get the Cluster ID
10187 int32_t clusterID = resource->getClusterValue ();
102- std::string resourceNodePath = getClusterTypeResourceNodePath (resource, clusterID);
103- std::string defaultValue =
104- ResourceRegistry::getInstance ()->getDefaultValue (resourceNodePath);
88+ std::string resourceNodePath = getFullResourceNodePath (rConf, clusterID);
89+ std::string defVal = ResourceRegistry::getInstance ()->getDefaultValue (resourceNodePath);
10590
106- TYPELOGV (NOTIFY_NODE_RESET, resourceNodePath.c_str (), defaultValue .c_str ());
91+ TYPELOGV (NOTIFY_NODE_RESET, resourceNodePath.c_str (), defVal .c_str ());
10792 std::ofstream resourceFileStream (resourceNodePath);
10893 if (!resourceFileStream.is_open ()) {
10994 TYPELOGV (ERRNO_LOG, " open" , strerror (errno));
11095 return ;
11196 }
11297
113- resourceFileStream<<defaultValue <<std::endl;
98+ resourceFileStream<<defVal <<std::endl;
11499
115100 if (resourceFileStream.fail ()) {
116101 TYPELOGV (ERRNO_LOG, " write" , strerror (errno));
@@ -119,8 +104,11 @@ void defaultClusterLevelTearCb(void* context) {
119104}
120105
121106static void defaultCoreLevelApplierHelper (Resource* resource, int32_t coreID) {
122- std::string resourceNodePath = getCoreTypeResourceNodePath (resource, coreID) ;
107+ if (resource == nullptr ) return ;
123108 ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
109+ if (rConf == nullptr ) return ;
110+
111+ std::string resourceNodePath = getFullResourceNodePath (rConf, coreID);
124112
125113 // 32-bit, unit-dependent value to be written
126114 int32_t valueToBeWritten = resource->getValueAt (0 );
@@ -155,6 +143,9 @@ void defaultCoreLevelApplierCb(void* context) {
155143 if (context == nullptr ) return ;
156144 Resource* resource = static_cast <Resource*>(context);
157145
146+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
147+ if (rConf == nullptr ) return ;
148+
158149 // Get the Core ID
159150 int32_t coreID = resource->getCoreValue ();
160151 if (coreID == 0 ) {
@@ -174,18 +165,20 @@ void defaultCoreLevelApplierCb(void* context) {
174165}
175166
176167static void defaultCoreLevelTearHelper (Resource* resource, int32_t coreID) {
177- std::string resourceNodePath = getClusterTypeResourceNodePath (resource, coreID);
178- std::string defaultValue =
179- ResourceRegistry::getInstance ()->getDefaultValue (resourceNodePath);
168+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
169+ if (rConf == nullptr ) return ;
180170
181- TYPELOGV (NOTIFY_NODE_RESET, resourceNodePath.c_str (), defaultValue.c_str ());
171+ std::string resourceNodePath = getFullResourceNodePath (rConf, coreID);
172+ std::string defVal = ResourceRegistry::getInstance ()->getDefaultValue (resourceNodePath);
173+
174+ TYPELOGV (NOTIFY_NODE_RESET, resourceNodePath.c_str (), defVal.c_str ());
182175 std::ofstream controllerFile (resourceNodePath);
183176 if (!controllerFile.is_open ()) {
184177 TYPELOGV (ERRNO_LOG, " open" , strerror (errno));
185178 return ;
186179 }
187180
188- controllerFile<<defaultValue <<std::endl;
181+ controllerFile<<defVal <<std::endl;
189182
190183 if (controllerFile.fail ()) {
191184 TYPELOGV (ERRNO_LOG, " write" , strerror (errno));
@@ -272,9 +265,9 @@ void defaultCGroupLevelApplierCb(void* context) {
272265void defaultCGroupLevelTearCb (void * context) {
273266 if (context == nullptr ) return ;
274267 Resource* resource = static_cast <Resource*>(context);
275- ResConfInfo* resourceConfigInfo =
276- ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
277- if (resourceConfigInfo == nullptr ) return ;
268+
269+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
270+ if (rConf == nullptr ) return ;
278271
279272 int32_t cGroupIdentifier = resource->getValueAt (0 );
280273 CGroupConfigInfo* cGroupConfig =
@@ -289,17 +282,16 @@ void defaultCGroupLevelTearCb(void* context) {
289282
290283 if (cGroupName.length () > 0 ) {
291284 std::string controllerFilePath = getCGroupTypeResourceNodePath (resource, cGroupName);
292- std::string defaultValue =
293- ResourceRegistry::getInstance ()->getDefaultValue (controllerFilePath);
285+ std::string defVal = ResourceRegistry::getInstance ()->getDefaultValue (controllerFilePath);
294286
295- TYPELOGV (NOTIFY_NODE_RESET, controllerFilePath.c_str (), defaultValue .c_str ());
287+ TYPELOGV (NOTIFY_NODE_RESET, controllerFilePath.c_str (), defVal .c_str ());
296288 std::ofstream controllerFile (controllerFilePath);
297289 if (!controllerFile.is_open ()) {
298290 TYPELOGV (ERRNO_LOG, " open" , strerror (errno));
299291 return ;
300292 }
301293
302- controllerFile<<defaultValue <<std::endl;
294+ controllerFile<<defVal <<std::endl;
303295
304296 if (controllerFile.fail ()) {
305297 TYPELOGV (ERRNO_LOG, " write" , strerror (errno));
@@ -313,30 +305,40 @@ void defaultGlobalLevelApplierCb(void* context) {
313305 if (context == nullptr ) return ;
314306 Resource* resource = static_cast <Resource*>(context);
315307
316- ResConfInfo* resourceConfig =
317- ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
308+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
309+ if (rConf == nullptr ) return ;
310+
311+ int32_t valueToWrite = resource->getValueAt (0 );
312+ std::string resourceNodePath = rConf->mResourcePath ;
318313
319- if (resourceConfig != nullptr ) {
320- TYPELOGV (NOTIFY_NODE_WRITE, resourceConfig->mResourcePath .c_str (), resource->getValueAt (0 ));
321- AuxRoutines::writeToFile (resourceConfig->mResourcePath , std::to_string (resource->getValueAt (0 )));
314+ if (resource->getValuesCount () == 2 ) {
315+ int32_t id = resource->getValueAt (0 );
316+ valueToWrite = resource->getValueAt (1 );
317+ resourceNodePath = getFullResourceNodePath (rConf, id);
322318 }
319+
320+ TYPELOGV (NOTIFY_NODE_WRITE, resourceNodePath.c_str (), valueToWrite);
321+ AuxRoutines::writeToFile (resourceNodePath, std::to_string (valueToWrite));
323322}
324323
325324// Default Tear Callback for Resources with ApplyType = "global"
326325void defaultGlobalLevelTearCb (void * context) {
327326 if (context == nullptr ) return ;
328327 Resource* resource = static_cast <Resource*>(context);
329328
330- ResConfInfo* resourceConfig =
331- ResourceRegistry::getInstance ()-> getResConf (resource-> getResCode ()) ;
329+ ResConfInfo* rConf = ResourceRegistry::getInstance ()-> getResConf (resource-> getResCode ());
330+ if (rConf == nullptr ) return ;
332331
333- if (resourceConfig != nullptr ) {
334- std::string defaultValue =
335- ResourceRegistry::getInstance ()->getDefaultValue (resourceConfig->mResourcePath );
332+ std::string resourceNodePath = rConf->mResourcePath ;
336333
337- TYPELOGV (NOTIFY_NODE_RESET, resourceConfig->mResourcePath .c_str (), defaultValue.c_str ());
338- AuxRoutines::writeToFile (resourceConfig->mResourcePath , defaultValue);
334+ if (resource->getValuesCount () == 2 ) {
335+ int32_t id = resource->getValueAt (0 );
336+ resourceNodePath = getFullResourceNodePath (rConf, id);
339337 }
338+
339+ std::string defVal = ResourceRegistry::getInstance ()->getDefaultValue (resourceNodePath);
340+ TYPELOGV (NOTIFY_NODE_RESET, resourceNodePath.c_str (), defVal.c_str ());
341+ AuxRoutines::writeToFile (resourceNodePath, defVal);
340342}
341343
342344// Specific callbacks for certain special Resources (which cannot be handled via the default versions)
@@ -352,8 +354,7 @@ static void moveProcessToCGroup(void* context) {
352354 CGroupConfigInfo* cGroupConfig =
353355 TargetRegistry::getInstance ()->getCGroupConfig (cGroupIdentifier);
354356
355- ResConfInfo* resourceConfig =
356- ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
357+ ResConfInfo* rConf = ResourceRegistry::getInstance ()->getResConf (resource->getResCode ());
357358
358359 if (cGroupConfig == nullptr ) {
359360 TYPELOGV (VERIFIER_CGROUP_NOT_FOUND, cGroupIdentifier);
@@ -366,7 +367,7 @@ static void moveProcessToCGroup(void* context) {
366367 return ;
367368 }
368369
369- std::string filePath = resourceConfig ->mResourcePath ;
370+ std::string filePath = rConf ->mResourcePath ;
370371
371372 // Replace %s in above file path with the actual cgroup name
372373 char pathBuffer[128 ] = {0 };
@@ -642,17 +643,16 @@ static void resetRunOnCoresExclusively(void* context) {
642643 const std::string cGroupCpuSetFilePath =
643644 UrmSettings::mBaseCGroupPath + cGroupName + " /cpuset.cpus" ;
644645
645- std::string defaultValue =
646- ResourceRegistry::getInstance ()->getDefaultValue (cGroupCpuSetFilePath);
646+ std::string defVal = ResourceRegistry::getInstance ()->getDefaultValue (cGroupCpuSetFilePath);
647647
648- TYPELOGV (NOTIFY_NODE_RESET, cGroupCpuSetFilePath.c_str (), defaultValue .c_str ());
648+ TYPELOGV (NOTIFY_NODE_RESET, cGroupCpuSetFilePath.c_str (), defVal .c_str ());
649649 std::ofstream controllerFile (cGroupCpuSetFilePath);
650650 if (!controllerFile.is_open ()) {
651651 TYPELOGV (ERRNO_LOG, " open" , strerror (errno));
652652 return ;
653653 }
654654
655- controllerFile<<defaultValue <<std::endl;
655+ controllerFile<<defVal <<std::endl;
656656
657657 if (controllerFile.fail ()) {
658658 TYPELOGV (ERRNO_LOG, " write" , strerror (errno));
@@ -668,9 +668,9 @@ static void resetRunOnCoresExclusively(void* context) {
668668 return ;
669669 }
670670
671- defaultValue = ResourceRegistry::getInstance ()->getDefaultValue (cGroupCpusetPartitionFilePath);
671+ defVal = ResourceRegistry::getInstance ()->getDefaultValue (cGroupCpusetPartitionFilePath);
672672
673- partitionFile<<defaultValue <<std::endl;
673+ partitionFile<<defVal <<std::endl;
674674
675675 if (partitionFile.fail ()) {
676676 TYPELOGV (ERRNO_LOG, " write" , strerror (errno));
0 commit comments