PlasCom2  1.0
XPACC Multi-physics simluation application
UnixUtils.C
Go to the documentation of this file.
1 #include <sstream>
7 #include <cstdlib>
8 
9 #include "UnixUtils.H"
10 #include "FDUtils.H"
11 
12 extern char **environ;
13 
14 namespace ix {
15  namespace sys {
16 
17  const std::string Hostname(bool longname)
18  {
19  char buf[64];
20  gethostname(buf,64);
21  std::string hostName(buf);
22  if(!longname){
23  hostName = hostName.substr(0,hostName.find("."));
24  }
25  return(hostName);
26  }
27 
28  std::ostream &operator<<(std::ostream &outStream,const struct platform_info &platformInfo)
29  {
30  if(!platformInfo.hostName.empty())
31  outStream << "Hostname: " << platformInfo.hostName << std::endl;
32  if(!platformInfo.operatingSystem.empty())
33  outStream << "OS: " << platformInfo.operatingSystem << std::endl;
34  if(!platformInfo.systemArchitecture.empty())
35  outStream << "Architecture: " << platformInfo.systemArchitecture << std::endl;
36  if(platformInfo.numProcessors > 0)
37  outStream << "Processors: " << platformInfo.numProcessors << std::endl;
38  if(platformInfo.numCores > 0)
39  outStream << "Cores/Proc: " << platformInfo.numCores << std::endl;
40  if(platformInfo.numThreads > 0)
41  outStream << "Threads/Core: " << platformInfo.numThreads << std::endl;
42  return(outStream);
43  };
44 
45  struct platform_info PlatformInfo(const std::string &systemInfo)
46  {
47  struct platform_info platformInfo;
48  platformInfo.numProcessors = 0;
49  platformInfo.numCores = 0;
50  platformInfo.numThreads = 0;
51  std::istringstream Istr(systemInfo);
52  std::string line;
53  platformInfo.hostName = Hostname();
54  while(std::getline(Istr,line)){
55  std::istringstream lineStream(line);
56  std::ostringstream keyWordStream;
57  std::string keyWord;
58  std::string token;
59  while(lineStream && keyWord.empty()){
60  lineStream >> token;
61  if(token.find(":") != std::string::npos) {
62  keyWord = keyWordStream.str()+token;
63  } else {
64  keyWordStream << token << " ";
65  }
66  }
67  if(!keyWord.empty()){
68  std::string valueWord;
69  std::getline(lineStream,valueWord);
70  std::istringstream wordStream(valueWord);
71  if(keyWord == "OS:"){
72  wordStream >> platformInfo.operatingSystem;
73  } else if (keyWord == "Hostname:"){
74  wordStream >> platformInfo.hostName;
75  } else if (keyWord == "Machine:") {
76  wordStream >> platformInfo.systemArchitecture;
77  } else if (keyWord == "Core(s) per socket:") {
78  wordStream >> platformInfo.numCores;
79  } else if (keyWord == "Thread(s) per core:") {
80  wordStream >> platformInfo.numThreads;
81  } else if (keyWord == "Socket(s):") {
82  wordStream >> platformInfo.numProcessors;
83  } else if (keyWord == "machdep.cpu.core_count:") {
84  wordStream >> platformInfo.numCores;
85  } else if (keyWord == "machdep.cpu.thread_count:") {
86  wordStream >> platformInfo.numThreads;
87  } else if (keyWord == "hw.physicalcpu:"){
88  wordStream >> platformInfo.numProcessors;
89  }
90  }
91  }
92 #ifdef DARWIN
93  if(platformInfo.numCores > 0)
94  platformInfo.numProcessors /= platformInfo.numCores;
95 #endif
96  return(platformInfo);
97  };
98 
99  std::string SystemInfo()
100  {
101  struct utsname sysInfo;
102  uname(&sysInfo);
103  std::string hostName(Hostname());
104  std::ostringstream outStream;
105  outStream << "Hostname: " << hostName << std::endl
106  << "OS: " << sysInfo.sysname << std::endl
107  << "Node: " << sysInfo.nodename << std::endl
108  << "Release: " << sysInfo.release << std::endl
109  << "Version: " << sysInfo.version << std::endl
110  << "Machine: " << sysInfo.machine << std::endl;
111  InProcess sysInfoProcess;
112 #ifdef DARWIN
113  std::string commandString("sysctl -a hw | grep -v optional");
114 #else
115  std::string commandString("lscpu");
116 #endif
117  sysInfoProcess.Execute(commandString);
118  std::string outLine;
119  while(std::getline(sysInfoProcess,outLine))
120  outStream << outLine << std::endl;
121 #ifdef DARWIN
122  std::ostringstream commandStream;
123  commandStream << "sysctl -a machdep.cpu | grep -v thermal | grep -v feature"
124  << " | grep -v mwait | grep -v arch_perf";
125  sysInfoProcess.Execute(commandStream.str());
126  while(std::getline(sysInfoProcess,outLine))
127  outStream << outLine << std::endl;
128 #endif
129  return(outStream.str());
130  };
131 
132  int Rename(const std::string &source_file,const std::string &target_file)
133  {
134  return(rename(source_file.c_str(),target_file.c_str()));
135  }
136  int SymLink(const std::string &source,const std::string &target)
137  {
138  return(symlink(source.c_str(),target.c_str()));
139  }
140 
141  int Remove(const std::string &fname)
142  {
143  // std::cout << fname << std::endl;
144  if(ISDIR(fname)){
145  Directory RemoveDir(fname);
146  if(RemoveDir.size() != 0){
147  for(std::vector<std::string>::iterator it = RemoveDir.begin() ; it != RemoveDir.end(); ++it){
148  if(Remove(fname + "/" + *it) < 0)
149  return -1;
150  }
151  }
152  // std::cout << "removing directory" << std::endl;
153  return(rmdir(fname.c_str()));
154  }
155  else{
156  // std::cout << "removing file" << std::endl;
157  return(unlink(fname.c_str()));
158  }
159  }
160  std::string LogTime()
161  {
162  time_t t_ptr;
163  time(&t_ptr);
164  char timesbuf[64];
165  std::string format("[%m/%d/%y%%%X]: ");
166  strftime(timesbuf,64,format.c_str(),gmtime(&t_ptr));
167  std::string retval(timesbuf);
168  return(retval);
169  }
170  void SafeRemove(const std::string &fname,const std::string &ext)
171  {
172  if(!FILEEXISTS(fname))
173  return;
174  if(ISLINK(fname)){
175  unlink(fname.c_str());
176  return;
177  }
178  std::string savename_base(fname+"."+ext);
179  std::string savename(savename_base);
180  unsigned int n = 1;
181  while(FILEEXISTS(savename)){
182  std::ostringstream Ostr;
183  Ostr << savename_base << n++;
184  savename.assign(Ostr.str());
185  }
186  rename(fname.c_str(),savename.c_str());
187  }
188 
189  bool FILEEXISTS(const std::string &fname)
190  {
191  struct stat fstat;
192  if(lstat(fname.c_str(),&fstat))
193  return false;
194  return true;
195  }
196 
197  bool ISDIR(const std::string &fname)
198  {
199  struct stat fstat;
200  if(stat(fname.c_str(),&fstat))
201  return false;
202  if(S_ISDIR(fstat.st_mode))
203  return true;
204  return false;
205  }
206 
207  bool ISLINK(const std::string &fname)
208  {
209  struct stat fstat;
210  if(lstat(fname.c_str(),&fstat))
211  return false;
212  if(S_ISLNK(fstat.st_mode))
213  return true;
214  return(false);
215  }
216 
217  int CreateDirectory(const std::string &fname)
218  {
219  return(mkdir(fname.c_str(),S_IRGRP | S_IXGRP | S_IRWXU));
220  }
221 
222  const std::string ResolveLink(const std::string &path)
223  {
224  std::string retVal;
225  char buf[1024];
226  size_t s = readlink(path.c_str(),buf,1024);
227  if(!(s <= 0)){
228  buf[s] = '\0';
229  retVal.assign(buf);
230  }
231  std::string::size_type x = retVal.find_last_of("/");
232  if(x != std::string::npos)
233  retVal.erase(x);
234  return (retVal);
235  }
236 
237 
238  Directory::Directory(const std::string &path)
239  {
240  _good = false;
241  _dir = NULL;
242  _path.assign(path);
243  if(open(path))
244  std::cerr << "Directory::Error: Could not open " << path
245  << " as a directory." << std::endl;
246  }
247 
249  {
250  if (_good)
251  closedir(_dir);
252  }
253 
254  Directory::operator void* ()
255  {
256  return(_good ? this : NULL);
257  }
258 
260  {
261  return(!_good);
262  }
263 
265  {
266  if(_good)
267  closedir(_dir);
268  }
269 
270  int Directory::open(const std::string &path)
271  {
272  if(_good){
273  this->close();
274  _path = path;
275  }
276  if(path.empty())
277  return(1);
278  if(!(_dir = opendir(path.c_str())))
279  return(1);
280  _path = path;
281  _good = true;
282  struct dirent *entry;
283  // Skip . and ..
284  while((entry = readdir(_dir)) != NULL){
285  if(std::string(entry->d_name) != "." && std::string(entry->d_name) != "..")
286  this->push_back(entry->d_name);
287  }
288  return(0);
289  }
290 
291  const std::string CWD()
292  {
293  char buf[1024];
294  return(std::string(getcwd(buf,1024)));
295  }
296 
297  int ChDir(const std::string &path)
298  {
299  return(chdir(path.c_str()));
300  }
301 
302  const std::string
303  StripDirs(const std::string &pname)
304  {
305  std::string retval;
306  std::string::size_type x = pname.find("/");
307  if(x == std::string::npos)
308  return(pname);
309  return(pname.substr(pname.find_last_of("/")+1));
310  }
311 
312  std::string
313  TempFileName(const std::string &stub)
314  {
315  int fd;
316  std::string tstub(stub);
317  tstub += "XXXXXX";
318  char *name = new char [tstub.length() + 1];
319  std::strcpy(name,tstub.c_str());
320  name[tstub.length()] = '\0';
321  fd = mkstemp(name);
322  // if(!fd)
323  // stub = name;
324  tstub.assign(name);
325  delete [] name;
326  close(fd);
327  // Get rid of the file - we just
328  // want the name at this point.
329  ix::sys::Remove(tstub);
330  return (tstub);
331  }
332 
333  int OpenTemp(std::string &stub)
334  {
335  int fd;
336  std::string tstub(stub);
337  tstub += "XXXXXX";
338  char *name = new char [tstub.length() + 1];
339  std::strcpy(name,tstub.c_str());
340  name[tstub.length()] = '\0';
341  fd = mkstemp(name);
342  stub = name;
343  delete [] name;
344  return(fd);
345  }
346  // Tokenize Path
347  // Takes an input path and makes string tokens of each
348  // directory and the final argument
349  void
350  TokenizePath(std::vector<std::string> rv,const std::string &path)
351  {
352  rv.resize(0);
353  std::istringstream Istr(path);
354  std::string tok;
355  while(std::getline(Istr,tok,'/'))
356  if(!tok.empty())
357  rv.push_back(tok);
358  }
359 
361  {
362  this->init();
363  }
364 
365  void
367  {
368  this->clear();
369  std::vector<std::string> raw_env;
370  unsigned int i = 0;
371  while(environ[i])
372  raw_env.push_back(environ[i++]);
373  // util::Vectorize(raw_env,(const char **)environ);
374  std::vector<std::string>::iterator rei = raw_env.begin();
375  while(rei != raw_env.end()){
376  unsigned int x = (*rei).find("=");
377  std::string var((*rei).substr(0,x));
378  std::string val((*rei).substr(x+1));
379  this->push_back(make_pair(var,val));
380  rei++;
381  }
382  }
383 
384  int
385  Environment::SetEnv(const std::string &var,const std::string &val,bool ow)
386  {
387  int retVal = setenv(var.c_str(),val.c_str(),(int)ow);
388  this->init();
389  return(retVal);
390  }
391 
392  void
393  Environment::UnSetEnv(const std::string &var)
394  {
395  unsetenv(var.c_str());
396  this->init();
397  }
398 
399 #ifndef DARWIN
400  int
402  {
403  clearenv();
404  this->init();
405  return(1);
406  }
407 #endif
408 
409  const std::string
410  Environment::GetEnv(const std::string &var) const
411  {
412  Environment::const_iterator ei = this->begin();
413  while(ei != this->end()){
414  if((*ei).first == var)
415  return((*ei).second);
416  ei++;
417  }
418  return(std::string(""));
419  }
420 
421  std::string &
422  Environment::GetEnv(const std::string &var)
423  {
424  Environment::iterator ei = this->begin();
425  while(ei != this->end()){
426  if((*ei).first == var)
427  return((*ei).second);
428  ei++;
429  }
430  empty_string.clear();
431  return(empty_string);
432  }
433 
434  int
436  {
437  int retVal = putenv(envs);
438  this->init();
439  return(retVal);
440  }
441 
442  void
444  {
445  this->init();
446  }
447 
448  char **
450  {
451  return(environ);
452  }
453 
454  std::ostream& operator<<(std::ostream &output,const Environment &env)
455  {
456  std::vector< std::pair<std::string,std::string> >::const_iterator ei = env.begin();
457  while(ei != env.end()){
458  output << (*ei).first << " = " << (*ei).second << std::endl;
459  ei++;
460  }
461  return(output);
462  }
463  };
464 };
465 
Directory(const std::string &s="")
Definition: UnixUtils.C:238
bool ISDIR(const std::string &fname)
Definition: UnixUtils.C:197
int Remove(const std::string &fname)
Definition: UnixUtils.C:141
char ** GetRawEnv()
Definition: UnixUtils.C:449
char ** environ
STL namespace.
std::ostream & operator<<(std::ostream &outStream, const platform_info &platformInfo)
const std::string GetEnv(const std::string &) const
Definition: UnixUtils.C:410
int ChDir(const std::string &path)
Definition: UnixUtils.C:297
Defines MPI-specific parallel global and program classes.
const std::string StripDirs(const std::string &)
Definition: UnixUtils.C:303
const std::string ResolveLink(const std::string &path)
Definition: UnixUtils.C:222
std::string hostName
Definition: UnixUtils.H:32
int OpenTemp(std::string &stub)
Definition: UnixUtils.C:333
void const size_t const size_t const size_t const double const double * x
std::string TempFileName(const std::string &stub)
Definition: UnixUtils.C:313
bool ISLINK(const std::string &fname)
Definition: UnixUtils.C:207
void SafeRemove(const std::string &fname, const std::string &ext)
Definition: UnixUtils.C:170
int CreateDirectory(const std::string &fname)
Definition: UnixUtils.C:217
std::string LogTime()
Definition: UnixUtils.C:160
int SymLink(const std::string &source, const std::string &target)
Definition: UnixUtils.C:136
int SetEnv(const std::string &, const std::string &, bool)
Definition: UnixUtils.C:385
bool FILEEXISTS(const std::string &fname)
Definition: UnixUtils.C:189
std::string operatingSystem
Definition: UnixUtils.H:34
int Rename(const std::string &source_file, const std::string &target_file)
Definition: UnixUtils.C:132
const std::string Hostname(bool longname=false)
Definition: UnixUtils.C:17
Unix System Tools interface.
std::string SystemInfo()
Definition: UnixUtils.C:99
const std::string CWD()
Definition: UnixUtils.C:291
std::string systemArchitecture
Definition: UnixUtils.H:35
virtual int Execute(const std::string &command)
Definition: FDUtils.H:179
Implementation of stream object for Unix file descriptors.
int PutEnv(char *)
Definition: UnixUtils.C:435
int open(const std::string &s="")
Definition: UnixUtils.C:270
void UnSetEnv(const std::string &)
Definition: UnixUtils.C:393
platform_info PlatformInfo(const std::string &systemInfo)
Definition: UnixUtils.C:45
void TokenizePath(std::vector< std::string > rv, const std::string &path)
Definition: UnixUtils.C:350