| 1 |
/* |
| 2 |
* Copyright(C) 1999-2022 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
/* |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* DESCRIPTION: |
| 13 |
* This routine returns an accumulated CPU time. The base time is |
| 14 |
* undefined; only relative times are valid. |
| 15 |
* |
| 16 |
* FORMAL PARAMETERS: |
| 17 |
* CPUSEC REAL CPU time |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "fortranc.h" |
| 22 |
|
| 23 |
#if defined(aix) || defined(__VACPP__) || defined(hpux) || defined(sgi) || defined(__osf__) || \ |
| 24 |
defined(__linux__) || defined(__APPLE__) || defined(__CYGWIN__) |
| 25 |
|
| 26 |
#if defined(__NO_CYGWIN_OPTION__) |
| 27 |
#define NOMINMAX |
| 28 |
#include <windows.h> |
| 29 |
#else |
| 30 |
#include <sys/resource.h> |
| 31 |
#include <sys/time.h> |
| 32 |
#endif |
| 33 |
#endif |
| 34 |
|
| 35 |
#if defined(interix) |
| 36 |
#include <time.h> |
| 37 |
void excpus_(FTNREAL *cpusec) |
| 38 |
#endif /* interix */ |
| 39 |
|
| 40 |
#if defined ADDC_ |
| 41 |
void excpus_(FTNREAL *cpusec) |
| 42 |
#else |
| 43 |
void excpus(FTNREAL *cpusec) |
| 44 |
#endif |
| 45 |
{ |
| 46 |
#if defined(interix) |
| 47 |
*cpusec = ((FTNREAL)clock()) / CLOCKS_PER_SEC; |
| 48 |
#endif /* interix */ |
| 49 |
|
| 50 |
#if defined(__NO_CYGWIN_OPTION__) |
| 51 |
FILETIME createTime; |
| 52 |
FILETIME exitTime; |
| 53 |
FILETIME kernelTimeProcess; |
| 54 |
FILETIME userTimeProcess; |
| 55 |
HANDLE hProcess = GetCurrentProcess(); |
| 56 |
LONGLONG tUser, tKernel; |
| 57 |
|
| 58 |
// Retrieve the file times for the process. |
| 59 |
if (hProcess) { |
| 60 |
// Get process times |
| 61 |
if (!GetProcessTimes(hProcess, &createTime, &exitTime, &kernelTimeProcess, &userTimeProcess)) |
| 62 |
*cpusec = 0.0; |
| 63 |
else { |
| 64 |
// Convert to 64-bit LONGLONG |
| 65 |
tUser = *(LONGLONG *)&userTimeProcess; |
| 66 |
tKernel = *(LONGLONG *)&kernelTimeProcess; |
| 67 |
// Time interval 100 nanoseconds, divide by 10,000,000 |
| 68 |
// to convert to seconds |
| 69 |
*cpusec = ((double)(tUser) + (double)(tKernel)) / 10000000.0; |
| 70 |
} |
| 71 |
} |
| 72 |
else { |
| 73 |
*cpusec = 0.0; |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
#if defined(sgi) || defined(__osf__) || defined(__linux__) || defined(aix) || \ |
| 78 |
defined(__VACPP__) || defined(paragon) || defined(hpux) || defined(__APPLE__) |
| 79 |
struct rusage rusage; |
| 80 |
getrusage(RUSAGE_SELF, &rusage); |
| 81 |
long secs = rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec; |
| 82 |
long mics = rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec; |
| 83 |
*cpusec = (FTNREAL)secs + ((FTNREAL)mics / 1.e6); |
| 84 |
#endif |
| 85 |
} |