1 /*------------------------------------------------------------------------
2 // Copyright (C) 1993,1994:
3 // J.C. Meza
4 // Sandia National Laboratories
5 // meza@california.sandia.gov
6 //----------------------------------------------------------------------*/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "OPT++_config.h"
10 #endif
11 
12 #include <sys/types.h>
13 
14 #ifdef HAVE_SYS_PARAM_H
15 #include <sys/param.h>
16 #endif
17 #ifdef HAVE_SYS_TIME_H
18 #include <sys/time.h>
19 #endif
20 #if defined(HAVE_SYS_TIMES_H) && !(defined(_MSC_VER) || defined(__MINGW32__))
21 #include <sys/times.h>
22 #endif
23 #if defined(HAVE_SYS_RESOURCE_H) && !(defined(_MSC_VER) || defined(__MINGW32__))
24 #include <sys/resource.h>
25 #endif
26 #include <stddef.h>
27 
28 #if !defined(HAVE_TIMES) && (defined(_MSC_VER) || defined(__MINGW32__))
29 #include <windows.h>
30 #include <time.h>
31 #endif
32 
33 #ifndef HZ
34 #define HZ 100
35 #endif
36 
get_cpu_time()37 double get_cpu_time()
38 {
39 /* ********************************************************************
40 **
41 **  Name: get_cpu_time
42 **
43 **  Purpose: general purpose CPU timing routine.
44 **
45 **  Arguments: none.
46 **
47 **  Return Value: user CPU time in (double) seconds.
48 **
49 **  Revision History:
50 **
51 **  10-May-94 -- initial development of get_cpu_time ().
52 **
53 ** *******************************************************************/
54 #if defined(HAVE_SYS_TIMES_H) && !(defined(_MSC_VER) || defined(__MINGW32__))
55     struct tms tms;
56 #endif
57     double time;
58 /*
59 **  Begin get_cpu_time.
60 */
61 #if defined(HAVE_TIMES) && !(defined(_MSC_VER) || defined(__MINGW32__))
62     times (&tms);
63     time = (double) tms.tms_utime / (double) HZ;
64 #else
65     FILETIME creationTime,kernTime,userTime,exitTime;
66     HANDLE process = GetCurrentProcess();
67     SYSTEMTIME sysTime;
68     GetProcessTimes(process,&creationTime,&exitTime,&kernTime,&userTime);
69     FileTimeToSystemTime(&userTime,&sysTime);
70     time = (double)sysTime.wSecond + (double)sysTime.wMilliseconds * 0.001;
71 #endif
72     return time;
73 /*
74 **  End get_cpu_time.
75 */
76 }
77 
get_wall_clock_time()78 double get_wall_clock_time()
79 {
80 /* ********************************************************************
81 **
82 **  Name: get_wall_clock_time
83 **
84 **  Purpose: general purpose wall-clock timing routine.
85 **
86 **  Arguments: none.
87 **
88 **  Return Value: time in (double) seconds since the Epoch.
89 **
90 **  Notes: The Paragon specific dclock() routine is used to avoid
91 **         unnecessary references from each node back to the boot
92 **         node as is required for system calls like gettimeofday(),
93 **         getrusage() or times().
94 **
95 **  Revision History:
96 **
97 **  10-May-94 -- TXF; initial development of get_wall_clock_time ().
98 **
99 ** *******************************************************************/
100 
101     double time;
102     struct timeval tp;
103 
104 
105 /*
106 **  Begin get_wall_clock_time.
107 */
108 
109     void* tzp = 0;
110     gettimeofday (&tp, tzp);
111     time = (double) tp.tv_sec + ((double) tp.tv_usec / (double) 1.0e06);
112 
113     return(time);
114 /*
115 **  End get_wall_clock_time.
116 */
117 }
118 
119 /* Modified from http://mywebpage.netscape.com/yongweiwu/timeval.h.txt */
120 #if !defined(HAVE_GETTIMEOFDAY) && (defined(_MSC_VER) || defined(__MINGW32__))
gettimeofday(struct timeval * tv,void * tz)121 int gettimeofday (struct timeval *tv, void* tz)
122 {
123   union {
124     __int64 ns100; /*time since 1 Jan 1601 in 100ns units */
125     FILETIME ft;
126   } now;
127 
128   GetSystemTimeAsFileTime (&now.ft);
129   tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
130   tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);
131   return (0);
132 }
133 #endif
134