1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, 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.
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"
24 #include <Basics_Utils.hxx>
31 #include <sys/types.h>
37 #include <libxml/parser.h>
41 #define MAX_SIZE_FOR_HOSTNAME 256;
43 static LoadRateManagerFirst first;
44 static LoadRateManagerCycl cycl;
45 static LoadRateManagerAltCycl altcycl;
46 //=============================================================================
50 //=============================================================================
52 ResourcesManager_cpp::
53 ResourcesManager_cpp(const char *xmlFilePath)
55 _path_resources.push_back(xmlFilePath);
56 #if defined(_DEBUG_) || defined(_DEBUG)
57 std::cerr << "ResourcesManager_cpp constructor" << std::endl;
59 _resourceManagerMap["first"]=&first;
60 _resourceManagerMap["cycl"]=&cycl;
61 _resourceManagerMap["altcycl"]=&altcycl;
62 _resourceManagerMap["best"]=&altcycl;
63 _resourceManagerMap[""]=&altcycl;
66 //=============================================================================
68 * Standard constructor, parse resource file.
69 * - if ${APPLI} exists in environment,
70 * look for ${HOME}/${APPLI}/CatalogResources.xml
71 * - else look for default:
72 * ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
73 * - parse XML resource file.
75 //=============================================================================
77 ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException)
79 RES_MESSAGE("ResourcesManager_cpp constructor");
81 _resourceManagerMap["first"]=&first;
82 _resourceManagerMap["cycl"]=&cycl;
83 _resourceManagerMap["altcycl"]=&altcycl;
84 _resourceManagerMap["best"]=&altcycl;
85 _resourceManagerMap[""]=&altcycl;
87 if (getenv("USER_CATALOG_RESOURCES_FILE") != 0)
89 std::string user_file("");
90 user_file = getenv("USER_CATALOG_RESOURCES_FILE");
91 _path_resources.push_back(user_file);
95 std::string default_file("");
96 if (getenv("APPLI") != 0)
98 default_file += getenv("HOME");
100 default_file += getenv("APPLI");
101 default_file += "/CatalogResources.xml";
102 _path_resources.push_back(default_file);
106 if(!getenv("KERNEL_ROOT_DIR"))
107 throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!! -> cannot load a CatalogResources.xml");
108 default_file = getenv("KERNEL_ROOT_DIR");
109 default_file += "/share/salome/resources/kernel/CatalogResources.xml";
110 _path_resources.push_back(default_file);
117 RES_MESSAGE("ResourcesManager_cpp constructor end");
120 //=============================================================================
122 * Standard Destructor
124 //=============================================================================
126 ResourcesManager_cpp::~ResourcesManager_cpp()
128 RES_MESSAGE("ResourcesManager_cpp destructor");
131 //=============================================================================
132 //! get the list of resource names fitting constraints given by params
135 * 1: Restrict list with resourceList if defined
136 * 2: If name is defined -> check resource list
137 * 3: If not 2:, if hostname is defined -> check resource list
138 * 4: If not 3:, sort resource with nb_proc, etc...
139 * 5: In all cases remove resource that does not correspond with OS
140 * 6: And remove resource with componentList - if list is empty ignored it...
142 //=============================================================================
144 std::vector<std::string>
145 ResourcesManager_cpp::GetFittingResources(const resourceParams& params) throw(ResourcesException)
147 RES_MESSAGE("[GetFittingResources] on computer " << Kernel_Utils::GetHostname().c_str());
148 RES_MESSAGE("[GetFittingResources] with resource name: " << params.name);
149 RES_MESSAGE("[GetFittingResources] with hostname: "<< params.hostname);
152 std::vector<std::string> vec;
154 // Parse Again CalatogResource File
158 // 1: If name is defined -> check resource list
159 // 2: Restrict list with resourceList if defined
160 // 3: If not 2:, if hostname is defined -> check resource list
161 // 4: If not 3:, sort resource with nb_proc, etc...
162 // 5: In all cases remove resource that does not correspond with OS
163 // 6: And remove resource with componentList - if list is empty ignored it...
166 if (params.name != "")
168 RES_MESSAGE("[GetFittingResources] name parameter found !");
169 if (_resourcesList.find(params.name) != _resourcesList.end())
171 vec.push_back(params.name);
175 RES_MESSAGE("[GetFittingResources] resource name was not found on resource list ! name requested was " << params.name);
176 std::string error("[GetFittingResources] resource name was not found on resource list ! name requested was " + params.name);
177 throw ResourcesException(error);
180 MapOfParserResourcesType local_resourcesList = _resourcesList;
182 if (params.resourceList.size() > 0)
184 RES_MESSAGE("[GetFittingResources] Restricted resource list found !");
185 local_resourcesList.clear();
186 std::vector<std::string>::size_type sz = params.resourceList.size();
188 for (unsigned int i=0; i < sz; i++)
190 if (_resourcesList.find(params.resourceList[i]) != _resourcesList.end())
191 local_resourcesList[params.resourceList[i]] = _resourcesList[params.resourceList[i]];
196 if (params.hostname != "")
198 RES_MESSAGE("[GetFittingResources] Entering in hostname case !");
200 std::string hostname = params.hostname;
201 if (hostname == "localhost")
202 hostname = Kernel_Utils::GetHostname().c_str();
204 std::map<std::string, ParserResourcesType>::const_iterator iter = _resourcesList.begin();
205 for (; iter != _resourcesList.end(); iter++)
207 if ((*iter).second.HostName == hostname)
208 vec.push_back((*iter).first);
214 // --- Search for available resources sorted by priority
215 MapOfParserResourcesType_it i = local_resourcesList.begin();
216 for (; i != local_resourcesList.end(); ++i)
217 vec.push_back(i->first);
219 // --- set wanted parameters
220 ResourceDataToSort::_nbOfProcWanted = params.nb_proc;
221 ResourceDataToSort::_nbOfNodesWanted = params.nb_node;
222 ResourceDataToSort::_nbOfProcPerNodeWanted = params.nb_proc_per_node;
223 ResourceDataToSort::_CPUFreqMHzWanted = params.cpu_clock;
224 ResourceDataToSort::_memInMBWanted = params.mem_mb;
228 std::list<ResourceDataToSort> li;
229 std::vector<std::string>::iterator iter = vec.begin();
230 for (; iter != vec.end(); iter++)
231 li.push_back(local_resourcesList[(*iter)].DataForSort);
235 for (std::list<ResourceDataToSort>::iterator iter2 = li.begin(); iter2 != li.end(); iter2++)
236 vec.push_back((*iter2)._Name);
240 SelectOnlyResourcesWithOS(vec, params.OS.c_str());
243 std::vector<std::string> vec_save(vec);
244 KeepOnlyResourcesWithComponent(vec, params.componentList);
249 // Send an exception if return list is empty...
252 std::string error("[GetFittingResources] ResourcesManager doesn't find any resource that fits to your parameters");
253 throw ResourcesException(error);
259 //=============================================================================
261 * add an entry in the ressources catalog xml file.
263 //=============================================================================
266 ResourcesManager_cpp::AddResourceInCatalog(const ParserResourcesType & new_resource) throw(ResourcesException)
268 // TODO - Add minimal check
269 _resourcesList[new_resource.Name] = new_resource;
272 //=============================================================================
274 * Deletes a resource from the catalog
276 //=============================================================================
278 void ResourcesManager_cpp::DeleteResourceInCatalog(const char * name)
280 MapOfParserResourcesType_it it = _resourcesList.find(name);
281 if (it != _resourcesList.end())
282 _resourcesList.erase(name);
284 RES_INFOS("You try to delete a resource that does not exist... : " << name);
287 //=============================================================================
289 * write the current data in memory in file.
291 //=============================================================================
293 void ResourcesManager_cpp::WriteInXmlFile(std::string xml_file)
295 RES_MESSAGE("WriteInXmlFile : start");
299 _path_resources_it = _path_resources.begin();
300 xml_file = *_path_resources_it;
303 const char* aFilePath = xml_file.c_str();
304 FILE* aFile = fopen(aFilePath, "w");
308 std::cerr << "Error opening file in WriteInXmlFile : " << xml_file << std::endl;
312 xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0");
313 xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
315 SALOME_ResourcesCatalog_Handler* handler =
316 new SALOME_ResourcesCatalog_Handler(_resourcesList);
317 handler->PrepareDocToXmlFile(aDoc);
320 int isOk = xmlSaveFormatFile(aFilePath, aDoc, 1);
322 std::cerr << "Error while XML file saving : " << xml_file << std::endl;
327 RES_MESSAGE("WriteInXmlFile : WRITING DONE!");
330 //=============================================================================
332 * parse the data type catalog
334 //=============================================================================
336 const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
338 // Parse file only if its modification time is greater than lasttime (last registered modification time)
339 bool to_parse = false;
340 for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
342 struct stat statinfo;
343 int result = stat((*_path_resources_it).c_str(), &statinfo);
346 std::cerr << "Error in method stat for file : " << (*_path_resources_it).c_str() << " no new xml file is parsed" << std::endl;
347 return _resourcesList;
350 if(statinfo.st_mtime > _lasttime)
353 _lasttime = statinfo.st_mtime;
359 _resourcesList.clear();
360 // On parse tous les fichiers
361 for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
363 MapOfParserResourcesType _resourcesList_tmp;
364 MapOfParserResourcesType _resourcesBatchList_tmp;
365 SALOME_ResourcesCatalog_Handler* handler =
366 new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp);
367 const char* aFilePath = (*_path_resources_it).c_str();
368 FILE* aFile = fopen(aFilePath, "r");
372 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
375 handler->ProcessXmlDocument(aDoc);
377 // adding new resources to the file
378 for (MapOfParserResourcesType_it i = _resourcesList_tmp.begin(); i != _resourcesList_tmp.end(); ++i)
380 MapOfParserResourcesType_it j = _resourcesList.find(i->first);
381 if (j == _resourcesList.end())
383 _resourcesList[i->first] = i->second;
387 std::cerr << "ParseXmlFiles Warning, two resources with the same name were found, taking the first declaration : " << i->first << std::endl;
392 std::cerr << "ResourcesManager_cpp: could not parse file " << aFilePath << std::endl;
398 std::cerr << "ResourcesManager_cpp: file " << aFilePath << " is not readable." << std::endl;
403 return _resourcesList;
406 //=============================================================================
408 * consult the content of the list
410 //=============================================================================
412 const MapOfParserResourcesType& ResourcesManager_cpp::GetList() const
414 return _resourcesList;
417 std::string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfResources)
419 if(_resourceManagerMap.count(policy)==0)
420 return _resourceManagerMap[""]->Find(listOfResources, _resourcesList);
421 return _resourceManagerMap[policy]->Find(listOfResources, _resourcesList);
424 //=============================================================================
426 * Gives a sublist of resources with matching OS.
427 * If parameter OS is empty, gives the complete list of resources
429 //=============================================================================
431 ResourcesManager_cpp::SelectOnlyResourcesWithOS(std::vector<std::string>& resources, std::string OS)
435 // a computer list is given : take only resources with OS on those computers
436 std::vector<std::string> vec_tmp = resources;
438 std::vector<std::string>::iterator iter = vec_tmp.begin();
439 for (; iter != vec_tmp.end(); iter++)
441 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
442 if(it != _resourcesList.end())
443 if ( (*it).second.OS == OS)
444 resources.push_back(*iter);
450 //=============================================================================
452 * Gives a sublist of machines on which the component is known.
454 //=============================================================================
456 ResourcesManager_cpp::KeepOnlyResourcesWithComponent(std::vector<std::string>& resources,
457 const std::vector<std::string>& componentList)
459 std::vector<std::string> kept_resources;
461 std::vector<std::string>::iterator iter = resources.begin();
462 for (; iter != resources.end(); iter++)
464 const std::vector<std::string>& mapOfComponentsOfCurrentHost = _resourcesList[*iter].ComponentsList;
466 bool erasedHost = false;
467 if( mapOfComponentsOfCurrentHost.size() > 0 )
469 for(unsigned int i=0; i<componentList.size(); i++)
471 std::vector<std::string>::const_iterator itt = find(mapOfComponentsOfCurrentHost.begin(),
472 mapOfComponentsOfCurrentHost.end(),
474 if (itt == mapOfComponentsOfCurrentHost.end())
482 kept_resources.push_back(*iter);
484 resources=kept_resources;
489 ResourcesManager_cpp::GetResourcesDescr(const std::string & name)
491 if (_resourcesList.find(name) != _resourcesList.end())
492 return _resourcesList[name];
495 std::string error("[GetResourcesDescr] Resource does not exist: ");
497 throw ResourcesException(error);