1 // Copyright (C) 2007-2008 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
22 #include "ResourcesManager.hxx"
23 #include <Basics_Utils.hxx>
30 #include <sys/types.h>
36 #include <libxml/parser.h>
40 #define MAX_SIZE_FOR_HOSTNAME 256;
44 static LoadRateManagerFirst first;
45 static LoadRateManagerCycl cycl;
46 static LoadRateManagerAltCycl altcycl;
47 //=============================================================================
51 //=============================================================================
53 ResourcesManager_cpp::
54 ResourcesManager_cpp(const char *xmlFilePath)
56 _path_resources.push_back(xmlFilePath);
57 #if defined(_DEBUG_) || defined(_DEBUG)
58 cerr << "ResourcesManager_cpp constructor" << endl;
60 _resourceManagerMap["first"]=&first;
61 _resourceManagerMap["cycl"]=&cycl;
62 _resourceManagerMap["altcycl"]=&altcycl;
63 _resourceManagerMap["best"]=&altcycl;
64 _resourceManagerMap[""]=&altcycl;
67 //=============================================================================
69 * Standard constructor, parse resource file.
70 * - if ${APPLI} exists in environment,
71 * look for ${HOME}/${APPLI}/CatalogResources.xml
72 * - else look for default:
73 * ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
74 * - parse XML resource file.
76 //=============================================================================
78 ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException)
80 RES_MESSAGE("ResourcesManager_cpp constructor");
82 _resourceManagerMap["first"]=&first;
83 _resourceManagerMap["cycl"]=&cycl;
84 _resourceManagerMap["altcycl"]=&altcycl;
85 _resourceManagerMap["best"]=&altcycl;
86 _resourceManagerMap[""]=&altcycl;
88 if (getenv("USER_CATALOG_RESOURCES_FILE") != 0)
90 std::string user_file("");
91 user_file = getenv("USER_CATALOG_RESOURCES_FILE");
92 _path_resources.push_back(user_file);
96 std::string default_file("");
97 if (getenv("APPLI") != 0)
99 default_file += getenv("HOME");
101 default_file += getenv("APPLI");
102 default_file += "/CatalogResources.xml";
103 _path_resources.push_back(default_file);
107 if(!getenv("KERNEL_ROOT_DIR"))
108 throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!! -> cannot load a CatalogResources.xml");
109 default_file = getenv("KERNEL_ROOT_DIR");
110 default_file += "/share/salome/resources/kernel/CatalogResources.xml";
111 _path_resources.push_back(default_file);
118 RES_MESSAGE("ResourcesManager_cpp constructor end");
121 //=============================================================================
123 * Standard Destructor
125 //=============================================================================
127 ResourcesManager_cpp::~ResourcesManager_cpp()
129 RES_MESSAGE("ResourcesManager_cpp destructor");
132 //=============================================================================
133 //! get the list of resource names fitting constraints given by params
136 * 1: Restrict list with resourceList if defined
137 * 2: If name is defined -> check resource list
138 * 3: If not 2:, if hostname is defined -> check resource list
139 * 4: If not 3:, sort resource with nb_proc, etc...
140 * 5: In all cases remove resource that does not correspond with OS
141 * 6: And remove resource with componentList - if list is empty ignored it...
143 //=============================================================================
145 std::vector<std::string>
146 ResourcesManager_cpp::GetFittingResources(const resourceParams& params) throw(ResourcesException)
148 RES_MESSAGE("[GetFittingResources] on computer " << Kernel_Utils::GetHostname().c_str());
149 RES_MESSAGE("[GetFittingResources] with resource name: " << params.name);
150 RES_MESSAGE("[GetFittingResources] with hostname: "<< params.hostname);
153 std::vector<std::string> vec;
155 // Parse Again CalatogResource File
159 // 1: Restrict list with resourceList if defined
160 // 2: If name is defined -> check resource list
161 // 3: If not 2:, if hostname is defined -> check resource list
162 // 4: If not 3:, sort resource with nb_proc, etc...
163 // 5: In all cases remove resource that does not correspond with OS
164 // 6: And remove resource with componentList - if list is empty ignored it...
167 MapOfParserResourcesType local_resourcesList = _resourcesList;
169 if (params.resourceList.size() > 0)
171 RES_MESSAGE("[GetFittingResources] Restricted resource list found !");
172 local_resourcesList.clear();
173 std::vector<std::string>::size_type sz = params.resourceList.size();
175 for (unsigned int i=0; i < sz; i++)
177 if (_resourcesList.find(params.resourceList[i]) != _resourcesList.end())
178 local_resourcesList[params.resourceList[i]] = _resourcesList[params.resourceList[i]];
183 if (params.name != "")
185 RES_MESSAGE("[GetFittingResources] name parameter found !");
186 if (_resourcesList.find(params.name) != _resourcesList.end())
188 vec.push_back(params.name);
191 RES_MESSAGE("[GetFittingResources] name was not found on resource list ! name was " << params.name);
195 else if (params.hostname != "")
197 RES_MESSAGE("[GetFittingResources] Entering in hostname case !");
199 std::string hostname = params.hostname;
200 if (hostname == "localhost")
201 hostname = Kernel_Utils::GetHostname().c_str();
203 std::map<std::string, ParserResourcesType>::const_iterator iter = _resourcesList.begin();
204 for (; iter != _resourcesList.end(); iter++)
206 if ((*iter).second.HostName == hostname)
207 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 (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 feets 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 _resourcesList.erase(name);
283 //=============================================================================
285 * write the current data in memory in file.
287 //=============================================================================
289 void ResourcesManager_cpp::WriteInXmlFile(std::string xml_file)
291 RES_MESSAGE("WriteInXmlFile : start");
295 _path_resources_it = _path_resources.begin();
296 xml_file = *_path_resources_it;
299 const char* aFilePath = xml_file.c_str();
300 FILE* aFile = fopen(aFilePath, "w");
304 std::cerr << "Error opening file in WriteInXmlFile : " << xml_file << std::endl;
308 xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0");
309 xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
311 SALOME_ResourcesCatalog_Handler* handler =
312 new SALOME_ResourcesCatalog_Handler(_resourcesList);
313 handler->PrepareDocToXmlFile(aDoc);
316 int isOk = xmlSaveFormatFile(aFilePath, aDoc, 1);
318 std::cerr << "Error while XML file saving : " << xml_file << std::endl;
323 RES_MESSAGE("WriteInXmlFile : WRITING DONE!");
326 //=============================================================================
328 * parse the data type catalog
330 //=============================================================================
332 const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFiles()
334 // Parse file only if its modification time is greater than lasttime (last registered modification time)
335 bool to_parse = false;
336 for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
338 struct stat statinfo;
339 int result = stat((*_path_resources_it).c_str(), &statinfo);
342 std::cerr << "Error in method stat for file : " << (*_path_resources_it).c_str() << " no new xml file is parsed" << std::endl;
343 return _resourcesList;
346 if(statinfo.st_mtime > _lasttime)
349 _lasttime = statinfo.st_mtime;
355 _resourcesList.clear();
356 // On parse tous les fichiers
357 for(_path_resources_it = _path_resources.begin(); _path_resources_it != _path_resources.end(); ++_path_resources_it)
359 MapOfParserResourcesType _resourcesList_tmp;
360 MapOfParserResourcesType _resourcesBatchList_tmp;
361 SALOME_ResourcesCatalog_Handler* handler =
362 new SALOME_ResourcesCatalog_Handler(_resourcesList_tmp);
363 const char* aFilePath = (*_path_resources_it).c_str();
364 FILE* aFile = fopen(aFilePath, "r");
368 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
371 handler->ProcessXmlDocument(aDoc);
373 // adding new resources to the file
374 for (MapOfParserResourcesType_it i = _resourcesList_tmp.begin(); i != _resourcesList_tmp.end(); ++i)
376 MapOfParserResourcesType_it j = _resourcesList.find(i->first);
377 if (j == _resourcesList.end())
379 _resourcesList[i->first] = i->second;
383 std::cerr << "ParseXmlFiles Warning, to resource with the same name was found, taking the first declaration : " << i->first << std::endl;
388 std::cerr << "ResourcesManager_cpp: could not parse file " << aFilePath << std::endl;
394 std::cerr << "ResourcesManager_cpp: file " << aFilePath << " is not readable." << std::endl;
399 return _resourcesList;
402 //=============================================================================
404 * consult the content of the list
406 //=============================================================================
408 const MapOfParserResourcesType& ResourcesManager_cpp::GetList() const
410 return _resourcesList;
413 string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfResources)
415 if(_resourceManagerMap.count(policy)==0)
416 return _resourceManagerMap[""]->Find(listOfResources, _resourcesList);
417 return _resourceManagerMap[policy]->Find(listOfResources, _resourcesList);
420 //=============================================================================
422 * Gives a sublist of resources with matching OS.
423 * If parameter OS is empty, gives the complete list of resources
425 //=============================================================================
427 ResourcesManager_cpp::SelectOnlyResourcesWithOS(std::vector<std::string>& resources, std::string OS)
431 // a computer list is given : take only resources with OS on those computers
432 std::vector<std::string> vec_tmp = resources;
434 vector<string>::iterator iter = vec_tmp.begin();
435 for (; iter != vec_tmp.end(); iter++)
437 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
438 if(it != _resourcesList.end())
439 if ( (*it).second.OS == OS)
440 resources.push_back(*iter);
446 //=============================================================================
448 * Gives a sublist of machines on which the component is known.
450 //=============================================================================
452 ResourcesManager_cpp::KeepOnlyResourcesWithComponent(std::vector<std::string>& resources,
453 const vector<string>& componentList)
455 std::vector<std::string>::iterator iter = resources.begin();
456 for (; iter != resources.end(); iter++)
458 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
459 const vector<string>& mapOfComponentsOfCurrentHost = (*it).second.ComponentsList;
461 bool erasedHost = false;
462 if( mapOfComponentsOfCurrentHost.size() > 0 )
464 for(unsigned int i=0; i<componentList.size(); i++)
466 const char* compoi = componentList[i].c_str();
467 vector<string>::const_iterator itt = find(mapOfComponentsOfCurrentHost.begin(),
468 mapOfComponentsOfCurrentHost.end(),
470 if (itt == mapOfComponentsOfCurrentHost.end())
478 resources.erase(iter);
484 ResourcesManager_cpp::GetResourcesDescr(const std::string & name)
486 if (_resourcesList.find(name) != _resourcesList.end())
487 return _resourcesList[name];
490 std::string error("[GetResourcesDescr] Resource does not exist: ");
492 throw ResourcesException(error);