Skip to content
Snippets Groups Projects
OS.cpp 4.01 KiB
Newer Older
Christophe Geuzaine's avatar
Christophe Geuzaine committed
// Gmsh - Copyright (C) 1997-2009 C. Geuzaine, J.-F. Remacle
Christophe Geuzaine's avatar
Christophe Geuzaine committed
//
Christophe Geuzaine's avatar
Christophe Geuzaine committed
// See the LICENSE.txt file for license information. Please report all
// bugs and problems to <gmsh@geuz.org>.
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
// This file contains a bunch of functions that depend on OS-dependent
// features and/or system calls

// these are available on all OSes
#include <stdlib.h>
Christophe Geuzaine's avatar
Christophe Geuzaine committed
#include <stdio.h>
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#include <time.h>
Christophe Geuzaine's avatar
Christophe Geuzaine committed
#include <math.h>
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#if !defined(WIN32) || defined(__CYGWIN__)
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#endif

#if defined(WIN32)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#include <windows.h>
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#include <process.h>
Christophe Geuzaine's avatar
Christophe Geuzaine committed
#include <io.h>
#endif

#if defined(__APPLE__)
#define RUSAGE_SELF      0
#define RUSAGE_CHILDREN -1
#endif

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

double GetTimeInSeconds()
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
#if !defined(WIN32) || defined(__CYGWIN__)
  struct timeval tp;
  gettimeofday(&tp, (struct timezone *)0);
  double t = (double)tp.tv_sec + 1.e-6 * (double)tp.tv_usec;
  return t;
#else
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  FILETIME ft;
  GetSystemTimeAsFileTime(&ft);
  double t =  1.e-7 * 4294967296. * (double)ft.dwHighDateTime +
              1.e-7 * (double)ft.dwLowDateTime;
Christophe Geuzaine's avatar
Christophe Geuzaine committed
  return t;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

Christophe Geuzaine's avatar
Christophe Geuzaine committed
void SleepInSeconds(double s)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
#if !defined(WIN32) || defined(__CYGWIN__)
  usleep((long)(1.e6 * s));
#else
Christophe Geuzaine's avatar
Christophe Geuzaine committed
  Sleep((long)(1.e3 * s));
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
}

void GetResources(double *s, long *mem)
{
#if !defined(WIN32) || defined(__CYGWIN__)
  static struct rusage r;
  getrusage(RUSAGE_SELF, &r);
  *s = (double)r.ru_utime.tv_sec + 1.e-6 * (double)r.ru_utime.tv_usec;
  *mem = (long)r.ru_maxrss;
#else
  FILETIME creation, exit, kernel, user;
  if(GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user)){
    *s = 1.e-7 * 4294967296. * (double)user.dwHighDateTime +
         1.e-7 * (double)user.dwLowDateTime;
  }
  *mem = 0;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#endif
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
void CheckResources(){
#if !defined (WIN32) || defined(__CYGWIN__)
  static struct rlimit r;

  getrlimit(RLIMIT_STACK, &r);

  // Try to get at least 16 MB of stack. Running with too small a stack
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  // can cause crashes in the recursive calls (e.g. for tet
  // classification in 3D Delaunay)
  if(r.rlim_cur < 16 * 1024 * 1024){
    Msg::Info("Increasing process stack size (%d kB < 16 MB)", r.rlim_cur / 1024);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
    r.rlim_cur = r.rlim_max;
    setrlimit(RLIMIT_STACK, &r);
  }
#endif
}

double Cpu()
{
  long mem = 0;
  double s = 0.;
  GetResources(&s, &mem);
  return s;
}
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed

int GetProcessId()
{
#if !defined(WIN32) || defined(__CYGWIN__)
  return getpid();
#else
  return _getpid();
#endif
}

std::string GetHostName()
{
  char host[256];
  gethostname(host, sizeof(host));
  return std::string(host);
}

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
int UnlinkFile(const char *filename)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
#if !defined(WIN32) || defined(__CYGWIN__)
  return unlink(filename);
#else
  return _unlink(filename);
#endif
}

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
int StatFile(const char *filename)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
#if !defined(WIN32) || defined(__CYGWIN__)
  struct stat buf;
  return stat(filename, &buf);
#else
  struct _stat buf;
  return _stat(filename, &buf);
#endif
}

int KillProcess(int pid)
{
#if !defined(WIN32) || defined(__CYGWIN__)
  if(kill(pid, 9))
    return 0;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#else
  HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
  if(!TerminateProcess(hProc, 0)){
    CloseHandle(hProc);
    return 0;
  }
#endif
  return 1;
}

Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
int SystemCall(const char *command)
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
{
#if defined(WIN32)
  STARTUPINFO suInfo;
  PROCESS_INFORMATION prInfo;
  memset(&suInfo, 0, sizeof(suInfo));
  suInfo.cb = sizeof(suInfo);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  CreateProcess(NULL, (char*)command, NULL, NULL, FALSE,
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
                NORMAL_PRIORITY_CLASS, NULL, NULL, &suInfo, &prInfo);
  return 0;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#else
  if(!system(NULL)) {
    Msg::Error("Could not find /bin/sh: aborting system call");
    return 1;
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
  }
  return system(command);
Christophe Geuzaine's avatar
 
Christophe Geuzaine committed
#endif
}
Christophe Geuzaine's avatar
Christophe Geuzaine committed

//HACK: allow the use of mixed debug/non-debug code with MSVC
#if (defined (_MSC_VER) && defined (_DEBUG))
extern "C" {
   _CRTIMP void __cdecl _invalid_parameter_noinfo(void) {  }
}
#endif

//HACK: fix undefined hypot with MSVC
#if defined (_MSC_VER)
extern "C" {
  double hypot(double x, double y){ return _hypot(x, y); }
}
#endif