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>
31 #include <sys/types.h>
33 #include <libxml/parser.h>
37 #define MAX_SIZE_FOR_HOSTNAME 256;
41 static LoadRateManagerFirst first;
42 static LoadRateManagerCycl cycl;
43 static LoadRateManagerAltCycl altcycl;
44 //=============================================================================
48 //=============================================================================
50 ResourcesManager_cpp::
51 ResourcesManager_cpp(const char *xmlFilePath) :
52 _path_resources(xmlFilePath)
54 #if defined(_DEBUG_) || defined(_DEBUG)
55 cerr << "ResourcesManager_cpp constructor" << endl;
57 _resourceManagerMap["first"]=&first;
58 _resourceManagerMap["cycl"]=&cycl;
59 _resourceManagerMap["altcycl"]=&altcycl;
60 _resourceManagerMap["best"]=&altcycl;
61 _resourceManagerMap[""]=&altcycl;
64 //=============================================================================
66 * Standard constructor, parse resource file.
67 * - if ${APPLI} exists in environment,
68 * look for ${HOME}/${APPLI}/CatalogResources.xml
69 * - else look for default:
70 * ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
71 * - parse XML resource file.
73 //=============================================================================
75 ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException)
77 #if defined(_DEBUG_) || defined(_DEBUG)
78 cerr << "ResourcesManager_cpp constructor" << endl;
80 _resourceManagerMap["first"]=&first;
81 _resourceManagerMap["cycl"]=&cycl;
82 _resourceManagerMap["altcycl"]=&altcycl;
83 _resourceManagerMap["best"]=&altcycl;
84 _resourceManagerMap[""]=&altcycl;
85 _isAppliSalomeDefined = (getenv("APPLI") != 0);
86 if(!getenv("KERNEL_ROOT_DIR"))
87 throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!!");
89 if (_isAppliSalomeDefined)
91 _path_resources = getenv("HOME");
92 _path_resources += "/";
93 _path_resources += getenv("APPLI");
94 _path_resources += "/CatalogResources.xml";
99 _path_resources = getenv("KERNEL_ROOT_DIR");
100 _path_resources += "/share/salome/resources/kernel/CatalogResources.xml";
104 #if defined(_DEBUG_) || defined(_DEBUG)
105 cerr << "ResourcesManager_cpp constructor end";
109 //=============================================================================
111 * Standard Destructor
113 //=============================================================================
115 ResourcesManager_cpp::~ResourcesManager_cpp()
117 #if defined(_DEBUG_) || defined(_DEBUG)
118 cerr << "ResourcesManager_cpp destructor" << endl;
122 //=============================================================================
124 * get the list of name of ressources fitting for the specified component.
125 * If hostname specified, check it is local or known in resources catalog.
128 * - select first machines with corresponding OS (all machines if
129 * parameter OS empty),
130 * - then select the sublist of machines on witch the component is known
131 * (if the result is empty, that probably means that the inventory of
132 * components is probably not done, so give complete list from previous step)
134 //=============================================================================
136 std::vector<std::string>
137 ResourcesManager_cpp::GetFittingResources(const machineParams& params,
138 const std::vector<std::string>& componentList) throw(ResourcesException)
140 vector <std::string> vec;
144 const char *hostname = params.hostname.c_str();
145 #if defined(_DEBUG_) || defined(_DEBUG)
146 cerr << "GetFittingResources " << hostname << " " << Kernel_Utils::GetHostname().c_str() << endl;
149 if (hostname[0] != '\0'){
151 if ( strcmp(hostname, "localhost") == 0 ||
152 strcmp(hostname, Kernel_Utils::GetHostname().c_str()) == 0 )
154 //#if defined(_DEBUG_) || defined(_DEBUG)
155 // cerr << "ResourcesManager_cpp::GetFittingResources : localhost" << endl;
157 vec.push_back(Kernel_Utils::GetHostname().c_str());
158 //#if defined(_DEBUG_) || defined(_DEBUG)
159 // cerr << "ResourcesManager_cpp::GetFittingResources : " << vec.size() << endl;
163 else if (_resourcesList.find(hostname) != _resourcesList.end())
165 // --- params.hostname is in the list of resources so return it.
166 vec.push_back(hostname);
169 else if (_resourcesBatchList.find(hostname) != _resourcesBatchList.end())
171 // --- params.hostname is in the list of resources so return it.
172 vec.push_back(hostname);
177 // Cas d'un cluster: nombre de noeuds > 1
179 for (map<string, ParserResourcesType>::const_iterator iter = _resourcesList.begin(); iter != _resourcesList.end(); iter++){
180 if( (*iter).second.DataForSort._nbOfNodes > 1 ){
181 if( strncmp(hostname,(*iter).first.c_str(),strlen(hostname)) == 0 ){
182 vec.push_back((*iter).first.c_str());
188 // --- user specified an unknown hostame so notify him.
189 #if defined(_DEBUG_) || defined(_DEBUG)
190 cerr << "ResourcesManager_cpp::GetFittingResources : SALOME_Exception" << endl;
192 throw ResourcesException("unknown host");
198 // --- Search for available resources sorted by priority
199 SelectOnlyResourcesWithOS(vec, params.OS.c_str());
201 KeepOnlyResourcesWithComponent(vec, componentList);
204 SelectOnlyResourcesWithOS(vec, params.OS.c_str());
206 // --- set wanted parameters
207 ResourceDataToSort::_nbOfNodesWanted = params.nb_node;
209 ResourceDataToSort::_nbOfProcPerNodeWanted = params.nb_proc_per_node;
211 ResourceDataToSort::_CPUFreqMHzWanted = params.cpu_clock;
213 ResourceDataToSort::_memInMBWanted = params.mem_mb;
217 list<ResourceDataToSort> li;
219 for (vector<string>::iterator iter = vec.begin();
222 li.push_back(_resourcesList[(*iter)].DataForSort);
228 for (list<ResourceDataToSort>::iterator iter2 = li.begin();
231 vec[i++] = (*iter2)._hostName;
238 //=============================================================================
240 * add an entry in the ressources catalog xml file.
241 * Return 0 if OK (KERNEL found in new resources components) else throw exception
243 //=============================================================================
246 ResourcesManager_cpp::
247 AddResourceInCatalog(const machineParams& paramsOfNewResources,
248 const vector<string>& componentsOnNewResources,
250 const char *userName,
252 AccessProtocolType prot)
253 throw(ResourcesException)
255 vector<string>::const_iterator iter = find(componentsOnNewResources.begin(),
256 componentsOnNewResources.end(),
259 if (iter != componentsOnNewResources.end())
261 ParserResourcesType newElt;
262 newElt.DataForSort._hostName = paramsOfNewResources.hostname;
263 newElt.Alias = alias;
264 newElt.Protocol = prot;
266 newElt.UserName = userName;
267 newElt.ComponentsList = componentsOnNewResources;
268 newElt.OS = paramsOfNewResources.OS;
269 newElt.DataForSort._memInMB = paramsOfNewResources.mem_mb;
270 newElt.DataForSort._CPUFreqMHz = paramsOfNewResources.cpu_clock;
271 newElt.DataForSort._nbOfNodes = paramsOfNewResources.nb_node;
272 newElt.DataForSort._nbOfProcPerNode =
273 paramsOfNewResources.nb_proc_per_node;
274 _resourcesList[newElt.DataForSort._hostName] = newElt;
279 throw ResourcesException("KERNEL is not present in this resource");
282 //=============================================================================
284 * Deletes a resource from the catalog
286 //=============================================================================
288 void ResourcesManager_cpp::DeleteResourceInCatalog(const char *hostname)
290 _resourcesList.erase(hostname);
293 //=============================================================================
295 * write the current data in memory in file.
297 //=============================================================================
299 void ResourcesManager_cpp::WriteInXmlFile()
301 const char* aFilePath = _path_resources.c_str();
303 FILE* aFile = fopen(aFilePath, "w");
307 #if defined(_DEBUG_) || defined(_DEBUG)
308 cerr << "Error opening file !" << endl;
313 xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0");
314 xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
316 SALOME_ResourcesCatalog_Handler* handler =
317 new SALOME_ResourcesCatalog_Handler(_resourcesList, _resourcesBatchList);
318 handler->PrepareDocToXmlFile(aDoc);
321 #if defined(_DEBUG_) || defined(_DEBUG)
322 int isOk = xmlSaveFile(aFilePath, aDoc);
323 if (!isOk) cerr << "Error while XML file saving." << endl;
325 xmlSaveFile(aFilePath, aDoc);
333 #if defined(_DEBUG_) || defined(_DEBUG)
334 cerr << "WRITING DONE!" << endl;
338 //=============================================================================
340 * parse the data type catalog
342 //=============================================================================
344 const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFile()
346 SALOME_ResourcesCatalog_Handler* handler =
347 new SALOME_ResourcesCatalog_Handler(_resourcesList, _resourcesBatchList);
349 const char* aFilePath = _path_resources.c_str();
350 FILE* aFile = fopen(aFilePath, "r");
354 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
357 handler->ProcessXmlDocument(aDoc);
358 #if defined(_DEBUG_) || defined(_DEBUG)
360 cerr << "ResourcesManager_cpp: could not parse file "<< aFilePath << endl;
368 #if defined(_DEBUG_) || defined(_DEBUG)
370 cerr << "ResourcesManager_cpp: file "<<aFilePath<<" is not readable." << endl;
375 return _resourcesList;
378 //=============================================================================
380 * consult the content of the list
382 //=============================================================================
384 const MapOfParserResourcesType& ResourcesManager_cpp::GetList() const
386 return _resourcesList;
389 string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfMachines)
391 if(_resourceManagerMap.count(policy)==0)
392 return _resourceManagerMap[""]->Find(listOfMachines,_resourcesList);
393 return _resourceManagerMap[policy]->Find(listOfMachines,_resourcesList);
396 //=============================================================================
398 * Gives a sublist of machines with matching OS.
399 * If parameter OS is empty, gives the complete list of machines
401 //=============================================================================
403 // Warning need an updated parsed list : _resourcesList
404 void ResourcesManager_cpp::SelectOnlyResourcesWithOS( vector<string>& hosts, const char *OS) const
405 throw(ResourcesException)
409 for (map<string, ParserResourcesType>::const_iterator iter =
410 _resourcesList.begin();
411 iter != _resourcesList.end();
414 if ( (*iter).second.OS == base || base.size() == 0)
415 hosts.push_back((*iter).first);
420 //=============================================================================
422 * Gives a sublist of machines on which the component is known.
424 //=============================================================================
426 //Warning need an updated parsed list : _resourcesList
427 void ResourcesManager_cpp::KeepOnlyResourcesWithComponent( vector<string>& hosts, const vector<string>& componentList) const
428 throw(ResourcesException)
430 for (vector<string>::iterator iter = hosts.begin(); iter != hosts.end();)
432 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
433 const vector<string>& mapOfComponentsOfCurrentHost = (((*it).second).ComponentsList);
435 bool erasedHost = false;
436 if( mapOfComponentsOfCurrentHost.size() > 0 ){
437 for(unsigned int i=0;i<componentList.size();i++){
438 const char* compoi = componentList[i].c_str();
439 vector<string>::const_iterator itt = find(mapOfComponentsOfCurrentHost.begin(),
440 mapOfComponentsOfCurrentHost.end(),
442 // componentList[i]);
443 if (itt == mapOfComponentsOfCurrentHost.end()){
457 ParserResourcesType ResourcesManager_cpp::GetResourcesList(const std::string& machine)
459 if (_resourcesList.find(machine) != _resourcesList.end())
460 return _resourcesList[machine];
462 return _resourcesBatchList[machine];