1 // Copyright (C) 2007-2023 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "ResourcesManager.hxx"
25 #include "SALOME_ResourcesCatalog_Handler.hxx"
26 #include <Basics_Utils.hxx>
27 #include <Basics_DirUtils.hxx>
28 #include "utilities.h"
36 #include <sys/types.h>
42 #include <libxml/parser.h>
46 #define MAX_SIZE_FOR_HOSTNAME 256;
50 const string ResourcesManager_cpp::DEFAULT_RESOURCE_NAME = "localhost";
52 static LoadRateManagerFirst first;
53 static LoadRateManagerCycl cycl;
54 static LoadRateManagerAltCycl altcycl;
56 resourceParams::resourceParams()
57 : can_launch_batch_jobs(false),
58 can_run_containers(false),
67 //=============================================================================
71 //=============================================================================
73 ResourcesManager_cpp::
74 ResourcesManager_cpp(const char *xmlFilePath)
76 _path_resources.push_back(xmlFilePath);
77 #if defined(_DEBUG_) || defined(_DEBUG)
78 std::cerr << "ResourcesManager_cpp constructor" << std::endl;
80 _resourceManagerMap["first"]=&first;
81 _resourceManagerMap["cycl"]=&cycl;
82 _resourceManagerMap["altcycl"]=&altcycl;
83 _resourceManagerMap["best"]=&altcycl;
84 _resourceManagerMap[""]=&altcycl;
86 AddDefaultResourceInCatalog();
90 //=============================================================================
92 * Standard constructor, parse resource file.
93 * - if ${APPLI} exists in environment,
94 * look for ${HOME}/${APPLI}/CatalogResources.xml
95 * - else look for default:
96 * ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
97 * - parse XML resource file.
99 //=============================================================================
101 ResourcesManager_cpp::ResourcesManager_cpp()
103 RES_MESSAGE("ResourcesManager_cpp constructor");
105 _resourceManagerMap["first"]=&first;
106 _resourceManagerMap["cycl"]=&cycl;
107 _resourceManagerMap["altcycl"]=&altcycl;
108 _resourceManagerMap["best"]=&altcycl;
109 _resourceManagerMap[""]=&altcycl;
111 AddDefaultResourceInCatalog();
113 bool default_catalog_resource = true;
114 if (getenv("USER_CATALOG_RESOURCES_FILE") != 0)
116 default_catalog_resource = false;
117 std::string user_file("");
118 user_file = getenv("USER_CATALOG_RESOURCES_FILE");
119 std::ifstream ifile(user_file.c_str(), std::ifstream::in );
121 // The file exists, and is open for input
122 DEBUG_MESSAGE("USER_CATALOG_RESOURCES_FILE positioned -> add it into resourcefiles list");
123 _path_resources.push_back(user_file);
126 default_catalog_resource = false;
127 WARNING_MESSAGE("Warning: USER_CATALOG_RESOURCES_FILE is set and file cannot be found.")
128 WARNING_MESSAGE("Warning: That's why we try to create a new one.")
129 std::ofstream user_catalog_file;
130 user_catalog_file.open(user_file.c_str());
131 if (user_catalog_file.fail())
133 WARNING_MESSAGE("Error: cannot write in the user catalog resources files");
134 WARNING_MESSAGE("Error: using default CatalogResources.xml file");
135 default_catalog_resource = true;
139 user_catalog_file << "<!-- File created by SALOME -->" << std::endl;
140 user_catalog_file << "<!DOCTYPE ResourcesCatalog>" << std::endl;
141 user_catalog_file << "<resources>" << std::endl;
142 user_catalog_file << " <machine name=\"localhost\" hostname=\"localhost\" />" << std::endl;
143 user_catalog_file << "</resources>" << std::endl;
144 user_catalog_file.close();
148 if (default_catalog_resource)
150 std::string default_file("");
151 if (getenv("APPLI") != 0)
153 default_file += Kernel_Utils::HomePath();
155 default_file += getenv("APPLI");
156 default_file += "/CatalogResources.xml";
157 std::ifstream ifile(default_file.c_str(), std::ifstream::in );
159 // The file exists, and is open for input
160 DEBUG_MESSAGE("${APPLI}/CatalogResources.xml exists -> add it into resourcefiles list");
161 _path_resources.push_back(default_file);
162 default_catalog_resource=false;
166 if (default_catalog_resource)
168 std::string default_file("");
169 if(!getenv("KERNEL_ROOT_DIR"))
170 throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!! -> cannot load a CatalogResources.xml");
171 default_file = getenv("KERNEL_ROOT_DIR");
172 default_file += "/share/salome/resources/kernel/CatalogResources.xml";
173 DEBUG_MESSAGE("${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml -> add it into resourcefiles list");
174 _path_resources.push_back(default_file);
178 RES_MESSAGE("ResourcesManager_cpp constructor end");
181 //=============================================================================
183 * Standard Destructor
185 //=============================================================================
187 ResourcesManager_cpp::~ResourcesManager_cpp()
189 RES_MESSAGE("ResourcesManager_cpp destructor");
192 //=============================================================================
193 //! get the list of resource names fitting constraints given by params
196 * 1: Restrict list with resourceList if defined
197 * 2: If name is defined -> check resource list
198 * 3: If not 2:, if hostname is defined -> check resource list
199 * 4: If not 3:, sort resource with nb_proc, etc...
200 * 5: In all cases remove resource that does not correspond with OS
201 * 6: And remove resource with componentList - if list is empty ignored it...
203 //=============================================================================
205 std::vector<std::string>
206 ResourcesManager_cpp::GetFittingResources(const resourceParams& params)
208 RES_MESSAGE("[GetFittingResources] on computer " << Kernel_Utils::GetHostname().c_str());
209 RES_MESSAGE("[GetFittingResources] with resource name: " << params.name);
210 RES_MESSAGE("[GetFittingResources] with hostname: "<< params.hostname);
213 std::vector<std::string> vec;
215 // Parse Again CalatogResource File
219 // 1: If name is defined -> check resource list
220 // 2: Restrict list with resourceList if defined
221 // 3: If not 2:, if hostname is defined -> check resource list
222 // 4: If not 3:, sort resource with nb_proc, etc...
223 // 5: In all cases remove resource that does not correspond with OS
224 // 6: And remove resource with componentList - if list is empty ignored it...
227 if (params.name != "")
229 RES_MESSAGE("[GetFittingResources] name parameter found !");
230 if (_resourcesList.find(params.name) != _resourcesList.end())
232 vec.push_back(params.name);
236 RES_MESSAGE("[GetFittingResources] resource name was not found on resource list ! name requested was " << params.name);
237 std::string error("[GetFittingResources] resource name was not found on resource list ! name requested was " + params.name);
238 throw ResourcesException(error);
241 MapOfParserResourcesType local_resourcesList = _resourcesList;
243 if (params.resourceList.size() > 0)
245 RES_MESSAGE("[GetFittingResources] Restricted resource list found !");
246 local_resourcesList.clear();
247 std::vector<std::string>::size_type sz = params.resourceList.size();
249 for (unsigned int i=0; i < sz; i++)
251 if (_resourcesList.find(params.resourceList[i]) != _resourcesList.end())
252 local_resourcesList[params.resourceList[i]] = _resourcesList[params.resourceList[i]];
257 if (params.hostname != "")
259 RES_MESSAGE("[GetFittingResources] Entering in hostname case !");
261 std::string hostname = params.hostname;
262 if (hostname == "localhost")
263 hostname = Kernel_Utils::GetHostname().c_str();
265 std::map<std::string, ParserResourcesType>::const_iterator iter = _resourcesList.begin();
266 for (; iter != _resourcesList.end(); iter++)
268 if ((*iter).second.HostName == hostname)
269 vec.push_back((*iter).first);
275 // --- Search for available resources sorted by priority
276 MapOfParserResourcesType_it i = local_resourcesList.begin();
277 for (; i != local_resourcesList.end(); ++i)
278 vec.push_back(i->first);
280 // --- set wanted parameters
281 ResourceDataToSort::_nbOfProcWanted = params.nb_proc;
282 ResourceDataToSort::_nbOfNodesWanted = params.nb_node;
283 ResourceDataToSort::_nbOfProcPerNodeWanted = params.nb_proc_per_node;
284 ResourceDataToSort::_CPUFreqMHzWanted = params.cpu_clock;
285 ResourceDataToSort::_memInMBWanted = params.mem_mb;
289 std::list<ResourceDataToSort> li;
290 std::vector<std::string>::iterator iter = vec.begin();
291 for (; iter != vec.end(); iter++)
292 li.push_back(local_resourcesList[(*iter)].DataForSort);
296 for (std::list<ResourceDataToSort>::iterator iter2 = li.begin(); iter2 != li.end(); iter2++)
297 vec.push_back((*iter2)._Name);
301 SelectOnlyResourcesWithOS(vec, params.OS.c_str());
304 std::vector<std::string> vec_save(vec);
305 KeepOnlyResourcesWithComponent(vec, params.componentList);
309 // Step 7 : Filter on possible usage
310 vector<string> prev_list(vec);
312 for (vector<string>::iterator iter = prev_list.begin() ; iter != prev_list.end() ; iter++)
314 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
315 if (it != _resourcesList.end() &&
316 (!params.can_launch_batch_jobs || it->second.can_launch_batch_jobs) &&
317 (!params.can_run_containers || it->second.can_run_containers))
318 vec.push_back(*iter);
322 // Send an exception if return list is empty...
325 std::string error("[GetFittingResources] ResourcesManager doesn't find any resource that fits to your parameters");
326 throw ResourcesException(error);
332 //=============================================================================
334 * add an entry in the resources catalog xml file.
336 //=============================================================================
339 ResourcesManager_cpp::AddResourceInCatalog(const ParserResourcesType & new_resource)
341 if (new_resource.Name == DEFAULT_RESOURCE_NAME){
342 ParserResourcesType default_resource = _resourcesList[DEFAULT_RESOURCE_NAME];
343 // some of the properties of the default resource shouldn't be modified
345 if( default_resource.HostName != new_resource.HostName)
346 check += "The Hostname property of the default resource can not be modified.\n";
347 if( default_resource.AppliPath != new_resource.AppliPath)
348 check += "The Applipath property of the default resource can not be modified.\n";
349 if( !new_resource.can_run_containers)
350 check += "The default resource should be able to run containers.\n";
351 if( !new_resource.can_launch_batch_jobs)
352 check += "The default resource should be able to launch batch jobs.\n";
353 if( default_resource.Protocol != new_resource.Protocol)
354 check += "The Protocol property of the default resource can not be modified.\n";
356 throw ResourcesException(check);
358 // TODO - Add minimal check
359 _resourcesList[new_resource.Name] = new_resource;
362 void ResourcesManager_cpp::DeleteAllResourcesInCatalog()
364 _resourcesList.clear();
367 //=============================================================================
369 * Deletes a resource from the catalog
371 //=============================================================================
373 void ResourcesManager_cpp::DeleteResourceInCatalog(const char * name)
375 if (DEFAULT_RESOURCE_NAME == name){
376 std::string error("Cannot delete default local resource \"" + DEFAULT_RESOURCE_NAME + "\"");
377 throw ResourcesException(error);
379 MapOfParserResourcesType_it it = _resourcesList.find(name);
380 if (it != _resourcesList.end())
381 _resourcesList.erase(name);
383 RES_INFOS("You try to delete a resource that does not exist... : " << name);
386 //=============================================================================
388 * write the current data in memory in file.
390 //=============================================================================
392 void ResourcesManager_cpp::WriteInXmlFile(std::string xml_file)
394 RES_MESSAGE("WriteInXmlFile : start");
396 MapOfParserResourcesType resourceListToSave(_resourcesList);
397 if (resourceListToSave.empty())
399 RES_MESSAGE("WriteInXmlFile: nothing to do, no resource to save!");
405 _path_resources_it = _path_resources.begin();
406 xml_file = *_path_resources_it;
409 const char* aFilePath = xml_file.c_str();
410 FILE* aFile = fopen(aFilePath, "w");
414 std::cerr << "Error opening file in WriteInXmlFile : " << xml_file << std::endl;
418 xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0");
419 xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
421 SALOME_ResourcesCatalog_Handler* handler =
422 new SALOME_ResourcesCatalog_Handler(resourceListToSave);
423 handler->PrepareDocToXmlFile(aDoc);
426 int isOk = xmlSaveFormatFile(aFilePath, aDoc, 1);
428 std::cerr << "Error while XML file saving : " << xml_file << std::endl;
433 RES_MESSAGE("WriteInXmlFile : WRITING DONE!");
436 //=============================================================================
438 * parse the data type catalog
440 //=============================================================================
442 const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
444 // Parse file only if its modification time is greater than lasttime (last registered modification time)
445 bool to_parse = false;
446 for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
448 struct stat statinfo;
449 int result = stat((*_path_resources_it).c_str(), &statinfo);
452 WARNING_MESSAGE("Resource file " << *_path_resources_it << " does not exist -> no parsing");
453 return _resourcesList;
456 if(_lasttime == 0 || statinfo.st_mtime > _lasttime)
458 DEBUG_MESSAGE("Resource file " << *_path_resources_it << " has been detected to be present and newer than Resources in memory");
460 _lasttime = statinfo.st_mtime;
466 DEBUG_MESSAGE("After analyze of resoure files time meta data, a load of resources from scratch from files is necessary.");
467 _resourcesList.clear();
468 AddDefaultResourceInCatalog();
469 // On parse tous les fichiers
470 for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
472 MapOfParserResourcesType _resourcesList_tmp;
473 MapOfParserResourcesType _resourcesBatchList_tmp;
474 std::unique_ptr<SALOME_ResourcesCatalog_Handler> handler( new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp) );
475 const char *aFilePath( (*_path_resources_it).c_str() );
476 FILE* aFile = fopen(aFilePath, "r");
479 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
482 DEBUG_MESSAGE("XML parsing of Resource file \"" << aFilePath << "\"");
483 handler->ProcessXmlDocument(aDoc);
485 // adding new resources to the file
486 for (MapOfParserResourcesType_it i = _resourcesList_tmp.begin(); i != _resourcesList_tmp.end(); ++i)
488 MapOfParserResourcesType_it j = _resourcesList.find(i->first);
489 if (i->second.HostName == DEFAULT_RESOURCE_NAME || i->second.HostName == Kernel_Utils::GetHostname())
491 DEBUG_MESSAGE("Resource \"" << i->first << "\" in file \"" << aFilePath << "\" is detected as localhost");
492 MapOfParserResourcesType_it it0(_resourcesList.find(DEFAULT_RESOURCE_NAME));
493 if(it0!=_resourcesList.end())
495 DEBUG_MESSAGE("Resource \"" << i->first << "\" in file \"" << aFilePath << "\" detected as localhost is already in memory -> update resource with content in file ( for attributes : nbOfNodes, nbOfProcPerNode, CPUFreqMHz and memInMB)");
496 ParserResourcesType& localhostElt((*it0).second);
497 localhostElt.DataForSort._nbOfNodes=(*i).second.DataForSort._nbOfNodes;
498 localhostElt.DataForSort._nbOfProcPerNode=(*i).second.DataForSort._nbOfProcPerNode;
499 localhostElt.DataForSort._CPUFreqMHz=(*i).second.DataForSort._CPUFreqMHz;
500 localhostElt.DataForSort._memInMB=(*i).second.DataForSort._memInMB;
502 DEBUG_MESSAGE("Resource \"" << i->first << "\" is not added because it is the same machine as default local resource \"" << DEFAULT_RESOURCE_NAME << "\"");
504 else if (j != _resourcesList.end())
506 WARNING_MESSAGE("ParseXmlFiles Warning, two resources with the same name were found, taking the first declaration : " << i->first );
510 DEBUG_MESSAGE("Resource \"" << i->first << "\" added");
511 _resourcesList[i->first] = i->second;
516 ERROR_MESSAGE( "ResourcesManager_cpp: could not parse file " << aFilePath );
522 ERROR_MESSAGE( "ResourcesManager_cpp: file " << aFilePath << " is not readable." );
525 return _resourcesList;
528 //=============================================================================
530 * consult the content of the list
532 //=============================================================================
534 const MapOfParserResourcesType& ResourcesManager_cpp::GetList() const
536 return _resourcesList;
540 std::string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfResources) const
542 std::map<std::string , LoadRateManager*>::const_iterator it(_resourceManagerMap.find(policy));
543 if(it==_resourceManagerMap.end())
545 it=_resourceManagerMap.find("");
546 return ((*it).second)->Find(listOfResources, _resourcesList);
548 return ((*it).second)->Find(listOfResources, _resourcesList);
551 //=============================================================================
553 * Gives a sublist of resources with matching OS.
554 * If parameter OS is empty, gives the complete list of resources
556 //=============================================================================
558 ResourcesManager_cpp::SelectOnlyResourcesWithOS(std::vector<std::string>& resources, std::string OS)
562 // a computer list is given : take only resources with OS on those computers
563 std::vector<std::string> vec_tmp = resources;
565 std::vector<std::string>::iterator iter = vec_tmp.begin();
566 for (; iter != vec_tmp.end(); iter++)
568 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
569 if(it != _resourcesList.end())
570 if ( (*it).second.OS == OS)
571 resources.push_back(*iter);
577 //=============================================================================
579 * Gives a sublist of machines on which the component is known.
581 //=============================================================================
583 ResourcesManager_cpp::KeepOnlyResourcesWithComponent(std::vector<std::string>& resources,
584 const std::vector<std::string>& componentList)
586 std::vector<std::string> kept_resources;
588 std::vector<std::string>::iterator iter = resources.begin();
589 for (; iter != resources.end(); iter++)
591 const std::vector<std::string>& mapOfComponentsOfCurrentHost = _resourcesList[*iter].ComponentsList;
593 bool erasedHost = false;
594 if( mapOfComponentsOfCurrentHost.size() > 0 )
596 for(unsigned int i=0; i<componentList.size(); i++)
598 std::vector<std::string>::const_iterator itt = find(mapOfComponentsOfCurrentHost.begin(),
599 mapOfComponentsOfCurrentHost.end(),
601 if (itt == mapOfComponentsOfCurrentHost.end())
609 kept_resources.push_back(*iter);
611 resources=kept_resources;
615 ParserResourcesType ResourcesManager_cpp::GetResourcesDescr(const std::string & name) const
617 MapOfParserResourcesType::const_iterator it(_resourcesList.find(name));
618 if (it != _resourcesList.end())
622 std::string error("[GetResourcesDescr] Resource does not exist: ");
624 throw ResourcesException(error);
628 void ResourcesManager_cpp::AddDefaultResourceInCatalog()
630 ParserResourcesType resource;
631 resource.Name = DEFAULT_RESOURCE_NAME;
632 // We can't use "localhost" for parameter hostname because the containers are registered in the
633 // naming service with the real hostname, not "localhost"
634 resource.HostName = Kernel_Utils::GetHostname();
635 resource.DataForSort._Name = DEFAULT_RESOURCE_NAME;
636 resource.Protocol = sh;
637 resource.Batch = none;
640 std::string aHomePath = Kernel_Utils::HomePath();
641 if (aHomePath != "" && getenv("APPLI") != NULL)
643 if (stat(getenv("APPLI"), &statbuf) ==0 && S_ISREG(statbuf.st_mode))
645 // if $APPLI is a regular file, we asume it's a salome Launcher file
646 resource.AppliPath = string(getenv("APPLI"));
650 resource.AppliPath = aHomePath + "/" + getenv("APPLI");
653 string tmpdir = "/tmp";
654 if (getenv("TMPDIR") != NULL)
655 tmpdir = getenv("TMPDIR");
656 resource.working_directory = tmpdir + "/salome_localres_workdir";
657 if (getenv("USER") != NULL)
658 resource.working_directory += string("_") + getenv("USER");
660 if (getenv("USERPROFILE") != NULL && getenv("APPLI") != NULL)
662 resource.AppliPath = string(getenv("USERPROFILE")) + "\\" + getenv("APPLI");
664 string tmpdir = "C:\\tmp";
665 if (getenv("TEMP") != NULL)
666 tmpdir = getenv("TEMP");
667 resource.working_directory = tmpdir + "\\salome_localres_workdir";
668 if (getenv("USERNAME") != NULL)
669 resource.working_directory += string("_") + getenv("USERNAME");
671 resource.can_launch_batch_jobs = true;
672 resource.can_run_containers = true;
673 _resourcesList[resource.Name] = resource;
674 DEBUG_MESSAGE("Put Ressource \"" << resource.Name << "\" in dictionary of resources. This resource will be present even if resource files define it later.");