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;
63 //=============================================================================
65 * Standard constructor, parse resource file.
66 * - if ${APPLI} exists in environment,
67 * look for ${HOME}/${APPLI}/CatalogResources.xml
68 * - else look for default:
69 * ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml
70 * - parse XML resource file.
72 //=============================================================================
74 ResourcesManager_cpp::ResourcesManager_cpp() throw(ResourcesException)
76 #if defined(_DEBUG_) || defined(_DEBUG)
77 cerr << "ResourcesManager_cpp constructor" << endl;
79 _resourceManagerMap["first"]=&first;
80 _resourceManagerMap["cycl"]=&cycl;
81 _resourceManagerMap["altcycl"]=&altcycl;
82 _resourceManagerMap["best"]=&altcycl;
83 _isAppliSalomeDefined = (getenv("APPLI") != 0);
84 if(!getenv("KERNEL_ROOT_DIR"))
85 throw ResourcesException("you must define KERNEL_ROOT_DIR environment variable!!");
87 if (_isAppliSalomeDefined)
89 _path_resources = getenv("HOME");
90 _path_resources += "/";
91 _path_resources += getenv("APPLI");
92 _path_resources += "/CatalogResources.xml";
97 _path_resources = getenv("KERNEL_ROOT_DIR");
98 _path_resources += "/share/salome/resources/kernel/CatalogResources.xml";
102 #if defined(_DEBUG_) || defined(_DEBUG)
103 cerr << "ResourcesManager_cpp constructor end";
107 //=============================================================================
109 * Standard Destructor
111 //=============================================================================
113 ResourcesManager_cpp::~ResourcesManager_cpp()
115 #if defined(_DEBUG_) || defined(_DEBUG)
116 cerr << "ResourcesManager_cpp destructor" << endl;
120 //=============================================================================
122 * get the list of name of ressources fitting for the specified component.
123 * If hostname specified, check it is local or known in resources catalog.
126 * - select first machines with corresponding OS (all machines if
127 * parameter OS empty),
128 * - then select the sublist of machines on witch the component is known
129 * (if the result is empty, that probably means that the inventory of
130 * components is probably not done, so give complete list from previous step)
132 //=============================================================================
134 std::vector<std::string>
135 ResourcesManager_cpp::GetFittingResources(const machineParams& params,
136 const std::vector<std::string>& componentList) throw(ResourcesException)
138 vector <std::string> vec;
142 const char *hostname = params.hostname.c_str();
143 #if defined(_DEBUG_) || defined(_DEBUG)
144 cerr << "GetFittingResources " << hostname << " " << Kernel_Utils::GetHostname().c_str() << endl;
147 if (hostname[0] != '\0'){
149 if ( strcmp(hostname, "localhost") == 0 ||
150 strcmp(hostname, Kernel_Utils::GetHostname().c_str()) == 0 )
152 //#if defined(_DEBUG_) || defined(_DEBUG)
153 // cerr << "ResourcesManager_cpp::GetFittingResources : localhost" << endl;
155 vec.push_back(Kernel_Utils::GetHostname().c_str());
156 //#if defined(_DEBUG_) || defined(_DEBUG)
157 // cerr << "ResourcesManager_cpp::GetFittingResources : " << vec.size() << endl;
161 else if (_resourcesList.find(hostname) != _resourcesList.end())
163 // --- params.hostname is in the list of resources so return it.
164 vec.push_back(hostname);
167 else if (_resourcesBatchList.find(hostname) != _resourcesBatchList.end())
169 // --- params.hostname is in the list of resources so return it.
170 vec.push_back(hostname);
175 // Cas d'un cluster: nombre de noeuds > 1
177 for (map<string, ParserResourcesType>::const_iterator iter = _resourcesList.begin(); iter != _resourcesList.end(); iter++){
178 if( (*iter).second.DataForSort._nbOfNodes > 1 ){
179 if( strncmp(hostname,(*iter).first.c_str(),strlen(hostname)) == 0 ){
180 vec.push_back((*iter).first.c_str());
186 // --- user specified an unknown hostame so notify him.
187 #if defined(_DEBUG_) || defined(_DEBUG)
188 cerr << "ResourcesManager_cpp::GetFittingResources : SALOME_Exception" << endl;
190 throw ResourcesException("unknown host");
196 // --- Search for available resources sorted by priority
197 SelectOnlyResourcesWithOS(vec, params.OS.c_str());
199 KeepOnlyResourcesWithComponent(vec, componentList);
202 SelectOnlyResourcesWithOS(vec, params.OS.c_str());
204 // --- set wanted parameters
205 ResourceDataToSort::_nbOfNodesWanted = params.nb_node;
207 ResourceDataToSort::_nbOfProcPerNodeWanted = params.nb_proc_per_node;
209 ResourceDataToSort::_CPUFreqMHzWanted = params.cpu_clock;
211 ResourceDataToSort::_memInMBWanted = params.mem_mb;
215 list<ResourceDataToSort> li;
217 for (vector<string>::iterator iter = vec.begin();
220 li.push_back(_resourcesList[(*iter)].DataForSort);
226 for (list<ResourceDataToSort>::iterator iter2 = li.begin();
229 vec[i++] = (*iter2)._hostName;
236 //=============================================================================
238 * add an entry in the ressources catalog xml file.
239 * Return 0 if OK (KERNEL found in new resources components) else throw exception
241 //=============================================================================
244 ResourcesManager_cpp::
245 AddResourceInCatalog(const machineParams& paramsOfNewResources,
246 const vector<string>& componentsOnNewResources,
248 const char *userName,
250 AccessProtocolType prot)
251 throw(ResourcesException)
253 vector<string>::const_iterator iter = find(componentsOnNewResources.begin(),
254 componentsOnNewResources.end(),
257 if (iter != componentsOnNewResources.end())
259 ParserResourcesType newElt;
260 newElt.DataForSort._hostName = paramsOfNewResources.hostname;
261 newElt.Alias = alias;
262 newElt.Protocol = prot;
264 newElt.UserName = userName;
265 newElt.ComponentsList = componentsOnNewResources;
266 newElt.OS = paramsOfNewResources.OS;
267 newElt.DataForSort._memInMB = paramsOfNewResources.mem_mb;
268 newElt.DataForSort._CPUFreqMHz = paramsOfNewResources.cpu_clock;
269 newElt.DataForSort._nbOfNodes = paramsOfNewResources.nb_node;
270 newElt.DataForSort._nbOfProcPerNode =
271 paramsOfNewResources.nb_proc_per_node;
272 _resourcesList[newElt.DataForSort._hostName] = newElt;
277 throw ResourcesException("KERNEL is not present in this resource");
280 //=============================================================================
282 * Deletes a resource from the catalog
284 //=============================================================================
286 void ResourcesManager_cpp::DeleteResourceInCatalog(const char *hostname)
288 _resourcesList.erase(hostname);
291 //=============================================================================
293 * write the current data in memory in file.
295 //=============================================================================
297 void ResourcesManager_cpp::WriteInXmlFile()
299 const char* aFilePath = _path_resources.c_str();
301 FILE* aFile = fopen(aFilePath, "w");
305 #if defined(_DEBUG_) || defined(_DEBUG)
306 cerr << "Error opening file !" << endl;
311 xmlDocPtr aDoc = xmlNewDoc(BAD_CAST "1.0");
312 xmlNewDocComment(aDoc, BAD_CAST "ResourcesCatalog");
314 SALOME_ResourcesCatalog_Handler* handler =
315 new SALOME_ResourcesCatalog_Handler(_resourcesList, _resourcesBatchList);
316 handler->PrepareDocToXmlFile(aDoc);
319 #if defined(_DEBUG_) || defined(_DEBUG)
320 int isOk = xmlSaveFile(aFilePath, aDoc);
321 if (!isOk) cerr << "Error while XML file saving." << endl;
323 xmlSaveFile(aFilePath, aDoc);
331 #if defined(_DEBUG_) || defined(_DEBUG)
332 cerr << "WRITING DONE!" << endl;
336 //=============================================================================
338 * parse the data type catalog
340 //=============================================================================
342 const MapOfParserResourcesType& ResourcesManager_cpp::ParseXmlFile()
344 SALOME_ResourcesCatalog_Handler* handler =
345 new SALOME_ResourcesCatalog_Handler(_resourcesList, _resourcesBatchList);
347 const char* aFilePath = _path_resources.c_str();
348 FILE* aFile = fopen(aFilePath, "r");
352 xmlDocPtr aDoc = xmlReadFile(aFilePath, NULL, 0);
355 handler->ProcessXmlDocument(aDoc);
356 #if defined(_DEBUG_) || defined(_DEBUG)
358 cerr << "ResourcesManager_cpp: could not parse file "<< aFilePath << endl;
366 #if defined(_DEBUG_) || defined(_DEBUG)
368 cerr << "ResourcesManager_cpp: file "<<aFilePath<<" is not readable." << endl;
373 return _resourcesList;
376 //=============================================================================
378 * consult the content of the list
380 //=============================================================================
382 const MapOfParserResourcesType& ResourcesManager_cpp::GetList() const
384 return _resourcesList;
387 string ResourcesManager_cpp::Find(const std::string& policy, const std::vector<std::string>& listOfMachines)
389 if(_resourceManagerMap.count(policy)==0)return "";
390 return _resourceManagerMap[policy]->Find(listOfMachines,_resourcesList);
393 //=============================================================================
395 * Gives a sublist of machines with matching OS.
396 * If parameter OS is empty, gives the complete list of machines
398 //=============================================================================
400 // Warning need an updated parsed list : _resourcesList
401 void ResourcesManager_cpp::SelectOnlyResourcesWithOS( vector<string>& hosts, const char *OS) const
402 throw(ResourcesException)
406 for (map<string, ParserResourcesType>::const_iterator iter =
407 _resourcesList.begin();
408 iter != _resourcesList.end();
411 if ( (*iter).second.OS == base || base.size() == 0)
412 hosts.push_back((*iter).first);
417 //=============================================================================
419 * Gives a sublist of machines on which the component is known.
421 //=============================================================================
423 //Warning need an updated parsed list : _resourcesList
424 void ResourcesManager_cpp::KeepOnlyResourcesWithComponent( vector<string>& hosts, const vector<string>& componentList) const
425 throw(ResourcesException)
427 for (vector<string>::iterator iter = hosts.begin(); iter != hosts.end();)
429 MapOfParserResourcesType::const_iterator it = _resourcesList.find(*iter);
430 const vector<string>& mapOfComponentsOfCurrentHost = (((*it).second).ComponentsList);
432 bool erasedHost = false;
433 if( mapOfComponentsOfCurrentHost.size() > 0 ){
434 for(unsigned int i=0;i<componentList.size();i++){
435 const char* compoi = componentList[i].c_str();
436 vector<string>::const_iterator itt = find(mapOfComponentsOfCurrentHost.begin(),
437 mapOfComponentsOfCurrentHost.end(),
439 // componentList[i]);
440 if (itt == mapOfComponentsOfCurrentHost.end()){
454 ParserResourcesType ResourcesManager_cpp::GetResourcesList(const std::string& machine)
456 if (_resourcesList.find(machine) != _resourcesList.end())
457 return _resourcesList[machine];
459 return _resourcesBatchList[machine];