Ejemplo n.º 1
0
StackTrace::StackTrace(const char *szFile, const char *szFunction, int iLine, bool bLogging)
{
    if(m_iStackNum >= MAX_STACK)
    {
        printf("StackTrace::StackTrace (%s %s %d) cannot increase stack from %d\n", szFile, szFunction, iLine, m_iStackNum);
    }
    else
    {
        // printf("StackTrace::StackTrace entry %s %s %d\n", szFile, szFunction, iLine);
        if(bLogging == true)
        {
            debug("%-*s%s entry (mem: %ld bytes)\n", m_iStackNum, "", szFunction, memusage());
        }

        strcpy(m_pNodes[m_iStackNum].m_szFile, szFile);
        if(szFunction != NULL)
        {
            strcpy(m_pNodes[m_iStackNum].m_szFunction, szFunction);
            strcpy(m_szCurrent, szFunction);
        }
        else
        {
            strcpy(m_pNodes[m_iStackNum].m_szFunction, "");
            strcpy(m_szCurrent, "");
        }
        m_pNodes[m_iStackNum].m_iLine = iLine;
        m_pNodes[m_iStackNum].m_bLogging = bLogging;
        m_pNodes[m_iStackNum].m_dTick = gettick();
    }

    m_iStackNum++;
}
Ejemplo n.º 2
0
int main (int argc, char **argv, char **envp)
{
  struct rusage usage;
  char **p;
  int status, mem;
  int tsource, ttarget;
  int v;

  redirect = stderr;
  safe_signal (SIGPIPE, SIG_DFL);

  tsource = time (NULL);
  p = parse (argv);
  if (p == NULL) {
    printusage (argv);
    return (EXIT_FAILURE);
  }

  if (profile.env_vars != NULL) {
    if (strcmp(profile.env_vars, "PY") == 0) { 
      /* shorthand for needed Python environment variables, assuming that
	 the necessary executables, libraries, etc. are in the root of jail */
      char* xnewenvp[] = { "PYTHONIOENCODING=utf_8", "PYTHONHOME=/", "PYTHONPATH=/static", NULL };
      envp = xnewenvp;
    }
    else {
      char * newenvp[100];
      char ** iter = newenvp;
      *iter = strtok (profile.env_vars,"\n");
      while (*iter != NULL) { 
	iter++;
	*iter = strtok (NULL, "\n");
      } 
      envp = newenvp;
    }
  }

  if (profile.report_file != NULL) {
    redirect = fopen (profile.report_file, "w");
    if (redirect == NULL)
      error ("Couldn't open redirection file\n");
  }

  if (profile.customuid) {
    profile.theuid = profile.customuid;
  }
  else {
    profile.theuid = profile.uidplus + getpid();
  }

  if (profile.exec_dir == NULL && profile.chroot_dir == NULL) {
    system("chmod -R 777 ./");
  }
  else if (profile.exec_dir != NULL) {
    chdir(profile.exec_dir);
    system("chmod -R 777 ./");
  }
  else {
    chdir(profile.chroot_dir);
    system("chmod -R 777 ./");
  }

  pid = fork ();
  if (pid < 0)
    error (NULL);

  if (pid == 0) { /* FORK: slave process that will run the submitted code */

    setlimit (RLIMIT_AS, profile.memory * 1024);   /* ENOMEM error, SIGSEGV */
    setlimit (RLIMIT_DATA, profile.memory * 1024); /* ENOMEM error */
    setlimit (RLIMIT_CORE, profile.core * 1024);   /*  */
    setlimit (RLIMIT_STACK, profile.stack * 1024); /* SIGSEGV */
    setlimit (RLIMIT_FSIZE, profile.fsize * 1024); /* SIGXFSZ */
    setlimit (RLIMIT_CPU, profile.cpu);            /* SIGKILL */
    setlimit (RLIMIT_NOFILE, profile.nfile);       /* EMFILE error */
    setlimit (RLIMIT_NPROC, profile.nproc + 1);    /* 1 required by setuid */
    
    if (profile.chroot_dir != NULL && 0 != chdir(profile.chroot_dir)) {
      error ("Could not chdir to chroot dir [%s] while in [%s]\n", profile.chroot_dir, getcwd(NULL, 0));
      kill (getpid (), SIGPIPE);
    }
    
    if (profile.chroot_dir != NULL && chroot(".") != 0) {
      error ("Cannot chroot while in [%s]\n", getcwd(NULL, 0));
      kill (getpid (), SIGPIPE);
    }
    
    if (profile.exec_dir != NULL && 0 != chdir(profile.exec_dir)) {
      error ("Cannot change to rundir");
      kill (getpid (), SIGPIPE);
    }
    
    if (0 != setpriority(PRIO_PROCESS, getpid(), profile.niceness)) {
      error ("Could not set priority");
      kill (getpid (), SIGPIPE);
    }
    
    if (setgid (profile.gid) < 0 || getgid () != profile.gid || profile.gid == 0)
      error ("setgid failed\n");
    
    if (setgroups (0, NULL) < 0 || getgroups (0, NULL) != 0)
      error ("setgroups failed\n");
    
    if (setuid (profile.theuid) < 0 || getuid() != profile.theuid || profile.theuid == 0)
      error ("setuid failed\n");

    if (execve (*p, p, envp) < 0) {
      error ("execve error\n");
      kill (getpid (), SIGPIPE);
    }
    /* goodbye process! execve never returns. */
  }
  
  /* FORK: supervisor process */
    
  if (signal (SIGALRM, wallclock) == SIG_ERR)
    printstats ("Couldn't install signal handler\n");
  
  else if (alarm (profile.clock) != 0)
    printstats ("Couldn't set alarm\n");
  
  else {
    mark = OK;
    
    /* Poll at INTERVAL ms and determine the maximum *
     * memory usage,  exit when the child terminates */
    
    mem = -1;
    do {
      msleep (INTERVAL);
      mem = max (mem, memusage (pid));
      if (mem > profile.memory) {
        terminate (pid);
        mark = MLE;
        exit_code = mle_code;
      }
      do
        v = wait4 (pid, &status, WNOHANG | WUNTRACED, &usage);
      while ((v < 0) && (errno != EINTR));
      if (v < 0)
        error (NULL);
    } while (v == 0);
    
    ttarget = time (NULL);
    
    if (mark == MLE)
      printstats ("Memory Limit Exceeded\n");
    else if (mark == RTLE)
      printstats ("Time Limit Exceeded\n");
    else {
      if (WIFEXITED (status) != 0) {
        if (WEXITSTATUS (status) != 0) {
          if (mark == OK)
            mark = RETNZ;
          printstats ("Command exited with non-zero status (%d)\n",
                      WEXITSTATUS (status));
        }
        /*else
          printstats ("OK\n"); */
      }
      else {
        if (WIFSIGNALED (status) != 0) {
          /* Was killed for a TLE (or was it an OLE) */
          if (WTERMSIG (status) == SIGKILL) {
            mark = TLE;
            exit_code = tle_code;
          }
          else if (WTERMSIG (status) == SIGXFSZ) {
            mark = OLE;
            exit_code = ole_code;
          }
          else if (WTERMSIG (status) == SIGHUP) {
            mark = RF;
            exit_code = rf_code;
          }
          else if (WTERMSIG (status) == SIGPIPE) {
            mark = IE;
            exit_code = ie_code;
          }
          else {
            if (mark == OK) {
              mark = TERM;
              /*exit_code = term_code; */
              exit_code = WTERMSIG (status);
            }
            printstats ("Command terminated by signal (%d: %s)\n",
                        WTERMSIG (status),
                        name (WTERMSIG (status)));
          }
        }
        else if (WIFSTOPPED (status) != 0) {
          if (mark == OK) {
            mark = TERM;
            /* exit_code = term_code; */
            exit_code = WTERMSIG (status);
          }
          printstats ("Command terminated by signal (%d: %s)\n",
                      WSTOPSIG (status), name (WSTOPSIG (status)));
        }
        else
          printstats ("OK\n");
        
        if (mark == TLE) {
          /* Adjust the timings... although we know the child   *
           * was been killed just in the right time seeing 1.99 *
           * as TLE when the limit is 2 seconds is anoying      */
          usage.ru_utime.tv_sec = profile.cpu;
          usage.ru_utime.tv_usec = 0;
          printstats ("Time Limit Exceeded\n");
        }
        else if (mark == OLE)
          printstats ("Output Limit Exceeded\n");
        else if (mark == RTLE)
          printstats ("Time Limit Exceeded\n");
        else if (mark == RF)
          printstats ("Invalid Function\n");
        else if (mark == IE)
          printstats ("Internal Error\n");
      }
    }
    /* printstats ("elapsed time: %d seconds\n", ttarget - tsource);
    if (mem != -1) // -1: died too fast to measure
      printstats ("memory usage: %d kbytes\n", mem);
    printstats ("cpu usage: %0.3f seconds\n",
                (float) milliseconds (&usage.ru_utime) / 1000.0); */
  } 

  setuid(0);
  if (profile.exec_dir == NULL && profile.chroot_dir == NULL) {
    system("chown -R grader:grader ./");
  }
  else if (profile.exec_dir != NULL) {
    chdir(profile.exec_dir);
    system("chown -R grader:grader ./");
  }
  else {
    chdir(profile.chroot_dir);
    system("chown -R grader:grader ./");
  }
  /* Clean up any orphaned descendant processes. kill everything with the target user id.
     The only reliable way to do this seems to be kill(-1) from that user.
     We also have to use setgid to change the euid away from 0. */
  if (setgid (profile.gid) < 0 || getgid () != profile.gid || profile.gid == 0)
    printstats ("cleanup setgid failed\n");
  else if (setgroups (0, NULL) < 0 || getgroups (0, NULL) != 0)
    printstats ("cleanup setgroups failed\n");
  else if (setuid (profile.theuid) < 0 || getuid() != profile.theuid || profile.theuid == 0)
    printstats ("cleanup setuid failed\n");

  if (getuid() != 0 && getuid() == profile.theuid 
      && geteuid() != 0 && geteuid() == profile.theuid
      && getgid() != 0 && getgid() == profile.gid) {
    kill(-1, SIGKILL);
  }
  else {
    printstats ("not safe to kill: uid %d, euid %d, gid %d, target uid %d, target gid %d\n", 
                getuid(), geteuid(), getgid(), profile.theuid, profile.gid);
  }

  fclose (redirect);

  if (mark == OK)
    return (EXIT_SUCCESS);
  else
    return (exit_code);
}
Ejemplo n.º 3
0
int main (int argc, char **argv, char **envp)
{

  struct rusage usage;

  char **p;
  int status, mem, skipped, memused;
  int tsource, ttarget;
  int v;

  redirect = stderr;
  safe_signal (SIGPIPE, SIG_DFL);

  tsource = time (NULL);
  p = parse (argv);
  if (p == NULL)
    {
      printusage (argv);
      return (EXIT_FAILURE);
    }
  else
    {
      /* Get an unused uid */
      if (profile.minuid != profile.maxuid)
        {
          srand (time (NULL) ^ getpid ());
          profile.minuid += rand () % (profile.maxuid - profile.minuid);
        }

      if (strcmp (usage_file, "/dev/null") != 0)
        {
          redirect = fopen (usage_file, "w");
          chown (usage_file, profile.minuid, getgid());
          chmod (usage_file, 0640);
          if (redirect == NULL)
            error ("Couldn't open usage file\n");
        }

      /* stderr from user program is junk */
      junk = fopen (error_file, "w");
      if (junk == NULL)
        error ("Couldn't open junk file %s\n", error_file);
      if (strcmp (error_file, "/dev/null") != 0)
        {
          chown (error_file, profile.minuid, getgid());
          chmod (error_file, 0640);
        }

      if (setgid (profile.minuid) < 0)
        {
          if (errno == EPERM)
            {
              error ("Couldn't setgid due to permission");
            }
          else
            {
              error (NULL);
            }
        }
      if (setuid (profile.minuid) < 0)
        {
          if (errno == EPERM)
            {
              error ("Couldn't setuid due to permission");
            }
          else
            {
              error (NULL);
            }
        }


      if (getuid () == 0)
        error ("Not changing the uid to an unpriviledged one is a BAD ideia");

      if (signal (SIGALRM, wallclock) == SIG_ERR)
        error ("Couldn't install signal handler");

      if (alarm (profile.clock) != 0)
        error ("Couldn't set alarm");

      /* Fork new process */
      pid = fork ();
      if (pid < 0)
        error (NULL);

      if (pid == 0)
        /* Forked/child process */
        {
          /* Chrooting */
          if (chroot_dir != NULL)
            {
              if (0 != chdir(chroot_dir))
                {
                  kill (getpid (), SIGPIPE);
                  error ("Can not change to chroot dir");
                }
              if (0 != chroot(chroot_dir))
                {
                  kill (getpid (), SIGPIPE);
                  error ("Can not chroot");
                }
            }
          /* Change dir */
          if (run_dir != NULL)
            {
              if (0 != chdir(run_dir))
                {
                  kill (getpid (), SIGPIPE);
                  error ("Cannot change to rundir");
                }
            }

          dup2(fileno(junk), fileno(stderr));

          if (setuid (profile.minuid) < 0)
            error (NULL);

          /* Don't run as root */
          if (getuid () == 0)
            error ("Running as a root is not secure!");

          /* Set priority */
          if (0 != setpriority(PRIO_USER,profile.minuid,NICE_LEVEL))
            {
              kill (getpid (), SIGPIPE);
              error (NULL);
            }

          /* Set resource limits - memory, time etc */
          setlimits(profile);

          /* Execute the program */
          if (execve (*p, p, envp) < 0)
            {
              kill (getpid (), SIGPIPE);
              error (NULL);
            }
        }
      else
        /* Parent process */
        {
          if (setuid (profile.minuid) < 0)
            error (NULL);

          if (getuid () == 0)
            error ("Running as a root is not secure!");

          memusage_init ();

          mark = OK;

          /* Poll every INTERVAL ms and get the maximum   *
           * memory usage, exit when the child terminates */
          mem = 64;
          skipped = 0;
          memused = 0;
          do
            {
              msleep (INTERVAL);
              memused = memusage (pid);
              if (memused > -1)
                {
                  mem = max (mem, memused);
                }
              else
                { /* Can not read memory usage! */
                  skipped++;
                }

              if (skipped > 10)
                { /* process is already finished or something wrong happened */
                  terminate (pid);
                  mark = MLE;
                }

              if (mem > profile.memory)
                {
                  terminate (pid);
                  mark = MLE;
                }

              do
                v = wait4 (pid, &status, WNOHANG | WUNTRACED, &usage);
              //v = wait4 (pid, &status, WNOHANG | WUNTRACED | WCONTINUED | WEXITED, &usage);
              while ((v < 0) && (errno != EINTR));
              if (v < 0)
                error (NULL);
            }
          while (v == 0);

          memusage_close ();

          ttarget = time (NULL);

          if (mark == MLE)
            printstats ("Memory Limit Exceeded\n");
          else if (mark == RTLE)
            printstats ("Time Limit Exceeded\n");
          else
            {
              if (WIFEXITED (status) != 0)
                {
                  if (WEXITSTATUS (status) != 0)
                    printstats ("Command exited with non-zero status (%d)\n",
                                WEXITSTATUS (status));
                  else
                    printstats ("OK\n");
                }
              else
                {
                  if (WIFSIGNALED (status) != 0)
                    {
                      /* Was killed for a TLE (or was it an OLE) */
                      if (WTERMSIG (status) == SIGKILL)
                        mark = TLE;
                      else if (WTERMSIG (status) == SIGXFSZ)
                        mark = OLE;
                      else if (WTERMSIG (status) == SIGHUP)
                        mark = RF;
                      else if (WTERMSIG (status) == SIGPIPE)
                        mark = IE;
                      else
                        printstats ("Command terminated by signal (%d: %s)\n",
                                    WTERMSIG (status),
                                    name (WTERMSIG (status)));
                    }
                  else if (WIFSTOPPED (status) != 0)
                    printstats ("Command terminated by signal (%d: %s)\n",
                                WSTOPSIG (status), name (WSTOPSIG (status)));
                  else
                    printstats ("OK\n");

                  if (mark == TLE)
                    {
                      /* We know the child has terminated at right time(OS did). *
                       * But seing 1.990 as TLE while limit 2.0 is confusing.    *
                       * So here is small adjustment for presentation.           */
                      usage.ru_utime.tv_sec = profile.cpu;
                      usage.ru_utime.tv_usec = 0;
                      printstats ("Time Limit Exceeded\n");
                    }
                  else if (mark == OLE)
                    printstats ("Output Limit Exceeded\n");
                  else if (mark == RTLE)
                    printstats ("Time Limit Exceeded\n");
                  else if (mark == RF)
                    printstats ("Invalid Function\n");
                  else if (mark == IE)
                    printstats ("Internal Error\n");
                }
            }
          printstats ("elapsed time: %d seconds\n", ttarget - tsource);
          printstats ("memory usage: %d kbytes\n", mem);
          printstats ("cpu usage: %0.3f seconds\n",
                      (float) miliseconds (&usage.ru_utime) / 1000.0);
        }
    }
  fclose (redirect);

  return (EXIT_SUCCESS);
}
Ejemplo n.º 4
0
int main (int argc, char **argv, char **envp)
{
  struct rusage usage;
  char **p;
  int status, mem;
  int tsource, ttarget;
  int v;

  redirect = stderr;
  safe_signal (SIGPIPE, SIG_DFL);

  tsource = time (NULL);
  p = parse (argv);
  if (p == NULL)
    {
      printusage (argv);
      return (EXIT_FAILURE);
    }
  else
    {
      /*
         fprintf (stderr, "profile: \"%s\"\n", limit->name);
         fprintf (stderr, "  cpu=%u\n  mem=%u\n", (unsigned int) limit->cpu,
         (unsigned int) limit->memory);
         fprintf (stderr, "  core=%u\n  stack=%u\n", (unsigned int) limit->core,
         (unsigned int) limit->stack);
         fprintf (stderr, "  fsize=%u\n  nproc=%u\n", (unsigned int) limit->fsize,
         (unsigned int) limit->nproc);
         fprintf (stderr, "  minuid=%u\n  maxuid=%u\n", (unsigned int) limit->minuid,
         (unsigned int) limit->maxuid);
         fprintf (stderr, "  clock=%u\n",
         (unsigned int) limit->clock);
       */

      /* Still missing: get an unused uid from interval */
      if (profile.minuid != profile.maxuid)
        {
          srand (time (NULL) ^ getpid ());
          profile.minuid += rand () % (profile.maxuid - profile.minuid);
        }

      if (setuid (profile.minuid) < 0)
        error (NULL);

      if (strcmp (usage_file, "/dev/null") != 0)
        {
          redirect = fopen (usage_file, "w");
          chmod (usage_file, 0644);
          if (redirect == NULL)
            error ("Couldn't open redirection file\n");
        }

      if (getuid () == 0)
        error ("Not changing the uid to an unpriviledged one is a BAD ideia");

      if (signal (SIGALRM, wallclock) == SIG_ERR)
        error ("Couldn't install signal handler");

      if (alarm (profile.clock) != 0)
        error ("Couldn't set alarm");

      pid = fork ();
      if (pid < 0)
        error (NULL);
      if (pid == 0)
        {
          /* change to chroot dir */
          if (0 != chdir(chroot_dir))
            {
              kill (getpid (), SIGPIPE);
              error ("Cannot change to chroot dir");
            }

          /* chroot to judge dir  */
          if (0 != chroot(chroot_dir))
            {
              kill (getpid (), SIGPIPE);
              error ("Cannot chroot");
            }
          /* change to run dir */
          if (0 != chdir(run_dir))
            {
              kill (getpid (), SIGPIPE);
              error ("Cannot change to rundir");
            }

          if (setuid (profile.minuid) < 0)
            error (NULL);

          if (getuid () == 0)
            error ("Not changing the uid to an unpriviledged one is a BAD ideia");

          /* set priority */
          if (0 != setpriority(PRIO_USER,profile.minuid,NICE_LEVEL))
            {
              kill (getpid (), SIGPIPE);
              error (NULL);
            }

          /* Set Address space limit, 1 mbyte tolerancy (librarys also count!) */
          /*setlimit (RLIMIT_AS, (1024 + limit->memory) * 1024); */
          setlimit (RLIMIT_CORE, profile.core * 1024);
          setlimit (RLIMIT_STACK, profile.stack * 1024);
          setlimit (RLIMIT_FSIZE, profile.fsize * 1024);
          setlimit (RLIMIT_NPROC, profile.nproc);
          setlimit (RLIMIT_CPU, profile.cpu);

          /* Execute the program */
          if (execve (*p, p, envp) < 0)
            {
              kill (getpid (), SIGPIPE);
              error (NULL);
            }
        }
      else
        {
          if (setuid (profile.minuid) < 0)
            error (NULL);

          if (getuid () == 0)
            error ("Not changing the uid to an unpriviledged one is a BAD ideia");

          mark = OK;

          /* Poll at INTERVAL ms and determine the maximum *
           * memory usage,  exit when the child terminates */

          mem = 64;
          do
            {
              msleep (INTERVAL);
              mem = max (mem, memusage (pid));
              if (mem > profile.memory)
                {
                  terminate (pid);
                  mark = MLE;
                }
              do
                v = wait4 (pid, &status, WNOHANG | WUNTRACED, &usage);
              while ((v < 0) && (errno != EINTR));
              if (v < 0)
                error (NULL);
            }
          while (v == 0);

          ttarget = time (NULL);

          if (mark == MLE)
            printstats ("Memory Limit Exceeded\n");
          else if (mark == RTLE)
            printstats ("Time Limit Exceeded\n");
          else
            {
              if (WIFEXITED (status) != 0)
                {
                  if (WEXITSTATUS (status) != 0)
                    printstats ("Command exited with non-zero status (%d)\n",
                                WEXITSTATUS (status));
                  else
                    printstats ("OK\n");
                }
              else
                {
                  if (WIFSIGNALED (status) != 0)
                    {
                      /* Was killed for a TLE (or was it an OLE) */
                      if (WTERMSIG (status) == SIGKILL)
                        mark = TLE;
                      else if (WTERMSIG (status) == SIGXFSZ)
                        mark = OLE;
                      else if (WTERMSIG (status) == SIGHUP)
                        mark = RF;
                      else if (WTERMSIG (status) == SIGPIPE)
                        mark = IE;
                      else
                        printstats ("Command terminated by signal (%d: %s)\n",
                                    WTERMSIG (status),
                                    name (WTERMSIG (status)));
                    }
                  else if (WIFSTOPPED (status) != 0)
                    printstats ("Command terminated by signal (%d: %s)\n",
                                WSTOPSIG (status), name (WSTOPSIG (status)));
                  else
                    printstats ("OK\n");

                  if (mark == TLE)
                    {
                      /* Adjust the timings... although we know the child   *
                       * was been killed just in the right time seing 1.990 *
                       * as TLE when the limit is 2 seconds is anoying      */
                      usage.ru_utime.tv_sec = profile.cpu;
                      usage.ru_utime.tv_usec = 0;
                      printstats ("Time Limit Exceeded\n");
                    }
                  else if (mark == OLE)
                    printstats ("Output Limit Exceeded\n");
                  else if (mark == RTLE)
                    printstats ("Time Limit Exceeded\n");
                  else if (mark == RF)
                    printstats ("Invalid Function\n");
                  else if (mark == IE)
                    printstats ("Internal Error\n");
                }
            }
          printstats ("elapsed time: %d seconds\n", ttarget - tsource);
          printstats ("memory usage: %d kbytes\n", mem);
          printstats ("cpu usage: %0.3f seconds\n",
                      (float) miliseconds (&usage.ru_utime) / 1000.0);
        }
    }
  fclose (redirect);

  return (EXIT_SUCCESS);
}
Ejemplo n.º 5
0
  main(CkArgMsg *)
  {

    // print banner
    iout << iINFO << "NAMD " << NAMD_VERSION << " for " << NAMD_PLATFORM
         << "\n"
#ifdef MEM_OPT_VERSION
         << iWARN << "\n"
         << iWARN << "       ***  EXPERIMENTAL MEMORY OPTIMIZED VERSION  ***\n"
         << iWARN << "\n"
#endif
#if 0
         << iWARN << "\n"
         << iWARN << "          ***  UNRELEASED EXPERIMENTAL VERSION  ***\n"
         << iWARN << "\n"
#endif
#ifdef SPEC_DISABLED_VERSION

         << iINFO << "\n"
         << iINFO << "NAMD is a parallel, object-oriented molecular dynamics\n"
         << iINFO << "code designed for high-performance simulation of large\n"
         << iINFO << "biomolecular systems.  NAMD is distributed free of\n"
         << iINFO << "charge and includes source code.  For more information\n" 
         << iINFO << "please visit http://www.ks.uiuc.edu/Research/namd/\n"
         << iINFO << "\n"
         << iINFO << "*********************************************************\n"
         << iINFO << "This version of NAMD may be distributed only as a part of\n"
         << iINFO << "the SPEC Workstation Benchmark and all other distribution\n"
         << iINFO << "is prohibited.  Any use of this software is bound by\n"
         << iINFO << "the terms of the NAMD License, which is available at\n"
         << iINFO << "http://www.ks.uiuc.edu/Research/namd/license.html\n"
         << iINFO << "The NAMD development team will not provide support for\n"
         << iINFO << "any version of NAMD unless you have first registered\n"
         << iINFO << "and downloaded the latest version of NAMD available at\n"
         << iINFO << "http://www.ks.uiuc.edu/Research/namd/\n"
         << iINFO << "*********************************************************\n"
#else
         << iINFO << "\n"
         << iINFO << "Please visit http://www.ks.uiuc.edu/Research/namd/\n"
         << iINFO << "for updates, documentation, and support information.\n"
#endif
<< iINFO << "\n"
<< iINFO << "Please cite Phillips et al., J. Comp. Chem. 26:1781-1802 (2005)\n"
<< iINFO << "in all publications reporting results obtained with NAMD.\n"
<< iINFO << "\n"
         << endi;

    char charm_version[64];
    sprintf(charm_version,"%d",CHARM_VERSION);

#if CHARM_VERSION < 60500
#error "Charm++ 6.5.1 or later is required to build NAMD"
#endif

    iout << iINFO << "Based on Charm++/Converse " << charm_version
         << " for " << CMK_MACHINE_NAME << "\n" << endi;

    iout << iINFO << "Built " << namd_build_date << " by "
         << namd_build_user << " on " << namd_build_machine << "\n"
         << endi;
#ifndef NO_SOCKET
    char numcpus[512];
    sprintf(numcpus,"%d",CkNumPes());
    tbsoft_sendusage("NAMD",NAMD_VERSION,NAMD_PLATFORM,numcpus,"");
#endif

#if CMK_BLUEGENE_CHARM
    iout << iINFO << "Running on BigSim using " << CmiNumPes() << " real processors.\n" << endi;
#endif
    iout << iINFO << "Running on " << CkNumPes() << " processors, "
         << CmiNumNodes() << " nodes, "
         << CmiNumPhysicalNodes() << " physical nodes.\n" << endi;
    iout << iINFO << "CPU topology information " << (CmiCpuTopologyEnabled()?"available":"unavailable") << ".\n" << endi;
    iout << iINFO << "Charm++/Converse parallel runtime startup completed at "
	 << CmiWallTimer() << " s\n"<< endi;
    const char* memsource;
    memusage(&memsource);
    iout << iINFO << memusage_MB() << " MB of memory in use"
	 << " based on " << memsource << "\n";
  }
Ejemplo n.º 6
0
int main(int argc, char **argv){
  char *prog;
  int i;
  int debug;
  int argstart=-1;
  float delay_time=0.0;
  int cpu_usage, cpu_usage_max=25;
  int mem_usage, mem_usage_max=75;
#ifdef pp_LINUX  
  char command_buffer[1024];
  char user_path[1024];
  FILE *stream=NULL;
#endif  
#ifdef pp_OSX
  char command_buffer[1024];
  char user_path[1024];
#endif

  int itime;
  char *arg;
  char *command;

#ifdef pp_LINUX  
  hostlistfile=NULL;
  host=NULL;
  strcpy(user_path,"");
  sprintf(pid,"%i",getpid());
#endif
#ifdef pp_OSX
  sprintf(pid,"%i",getpid());
#endif

  debug=0;
  prog=argv[0];

  if(argc==1){
    version();
    return 1;
  }

  for(i=1;i<argc;i++){
    int lenarg;

    arg=argv[i];
    lenarg=strlen(arg);
    if(arg[0]=='-'){
      if(lenarg>1){
        switch(arg[1]){
          case 'd':
            if(strlen(arg)<=2||strcmp(arg,"-debug")!=0){
              i++;
              if(i<argc){
                arg=argv[i];
                sscanf(arg,"%f",&delay_time);
                if(delay_time<0.0)delay_time=0.0;
              }
            }
            else{
		          debug=1;
		        }
            break;
          case 'h':
#ifdef pp_LINUX
            if(strlen(arg)<=2||strcmp(arg,"-hosts")!=0){
#endif
              usage(prog);
              return 1;
#ifdef pp_LINUX
            }
            else{
              i++;
              if(i<argc){
                arg=argv[i];
                hostlistfile=malloc(strlen(arg)+1);
                strcpy(hostlistfile,arg);
              }
            }
            break;
          case 'p':
            i++;
            if(i<argc){
              arg=argv[i];
              if(strlen(arg)>0){
                strcpy(user_path,arg);
              }
            }
            break;
#endif
          case 'm':
            i++;
            if(i<argc){
              arg=argv[i];
              sscanf(arg,"%i",&mem_usage_max);
              if(mem_usage_max<25)mem_usage_max=25;
              if(mem_usage_max>90)mem_usage_max=90;
            }
            break;
          case 'u':
            i++;
            if(i<argc){
              arg=argv[i];
              sscanf(arg,"%i",&cpu_usage_max);
              if(cpu_usage_max<25)cpu_usage_max=25;
              if(cpu_usage_max>100)cpu_usage_max=100;
            }
            break;
          case 'v':
            version();
            return 1;
          default:
            printf("Unknown option: %s\n",arg);
            usage(prog);
            return 1;
	      }
      }
    }
    else{
      argstart=i;
      break;

    }
  }
#ifdef pp_LINUX
  nhostinfo=0;
  if(hostlistfile!=NULL){
    stream=fopen(hostlistfile,"r");
  }
  if(hostlistfile!=NULL&&stream!=NULL){
    char buffer[255];

    while(!feof(stream)){
      if(fgets(buffer,255,stream)==NULL)break;
      nhostinfo++;
    }
    if(nhostinfo>0){
      hostdata *hd;

      hostinfo=malloc(nhostinfo*sizeof(hostdata));
      hd = hostinfo;
      rewind(stream);
      while(!feof(stream)){
        char *hostname;

        if(fgets(buffer,255,stream)==NULL)break;
        trim(buffer);
        hostname=malloc(strlen(buffer)+1);
        strcpy(hostname,buffer);
        hd->hostname=hostname;
        hd->ncores=get_host_ncores(hostname);
        printf("host: %s ncores=%i\n",hostname,hd->ncores);
        hd++;
      }

    }
  }
  if(stream!=NULL)fclose(stream);
#endif

  if(argstart<0)return 0;

  itime = delay_time*1000;
  if(itime>0){
    Sleep(itime);
  }

#ifdef WIN32
  GetSystemTimesAddress();
  cpu_usage=cpuusage();
  mem_usage=memusage();
  Sleep(200);
  cpu_usage=cpuusage();
  mem_usage=memusage();
  while(cpu_usage>cpu_usage_max||mem_usage>mem_usage_max){
    Sleep(1000);
    cpu_usage=cpuusage();
    mem_usage=memusage();
  }
  command=argv[argstart];
  _spawnvp(_P_NOWAIT,command, argv+argstart);
#endif
#ifdef pp_LINUXOSX
  strcpy(command_buffer,"");
  if(hostinfo==NULL){
    cpu_usage=cpuusage();
    mem_usage=memusage();
    Sleep(200);
    cpu_usage=cpuusage();
    mem_usage=memusage();
    host=NULL;
    while(cpu_usage>cpu_usage_max||mem_usage>mem_usage_max){
      Sleep(1000);
      cpu_usage=cpuusage();
      mem_usage=memusage();
    }
  }
  else{
    int doit=0;

    while(doit==0){
      for(i=0;i<nhostinfo;i++){
        hostdata *hd;
        float fusage;

        hd = hostinfo + i;
        host = hd->hostname;
        cpu_usage=cpuusage_host(host,hd->ncores);
        fusage=(float)cpu_usage/255.0;
        if(debug==1)printf("host: %s cpu_usage=%f\n",host,fusage);
        if(cpu_usage<cpu_usage_max){
          if(debug==1)printf(" host %s is now free\n",host);
          doit=1;
          if(strlen(user_path)==0){
            getcwd(user_path,1024);
          }
          strcat(command_buffer,"ssh ");
          strcat(command_buffer,host);
          strcat(command_buffer," \"( cd ");
          strcat(command_buffer,user_path);
          strcat(command_buffer,";");
          break;
        }
      }
      if(doit==0)Sleep(1000);
    }
  }
  for(i=argstart;i<argc;i++){
    arg=argv[i];
    strcat(command_buffer,arg);
    if(i<argc-1){
      strcat(command_buffer," ");    
    }
  }
  if(nhostinfo>0)strcat(command_buffer,")\"");
  strcat(command_buffer,"&");
  system(command_buffer);
#endif
  return 0;
}
Ejemplo n.º 7
0
StackTrace::~StackTrace()
{
    m_iStackNum--;
    strcpy(m_szCurrent, m_pNodes[m_iStackNum].m_szFunction);

    if(m_pNodes[m_iStackNum].m_bLogging == true)
    {
        debug("%-*s%s exit (mem: %ld bytes, %ld ms)\n", m_iStackNum, "", m_pNodes[m_iStackNum].m_szFunction, memusage(), tickdiff(m_pNodes[m_iStackNum].m_dTick));
    }
}