1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <cstdio>
#include "CpuTimer.hpp"

#include "CubitMessage.hpp"

// Added by Cat for WIN32 port
#ifndef _WIN32
  #include <sys/param.h>
  #include <sys/times.h>
#endif


#include <time.h>
#ifdef SOLARIS
#include <unistd.h>
#endif

#ifndef _WIN32
#ifndef HZ
#ifdef CLK_TCK
#define HZ CLK_TCK
#else
#define HZ 60
#endif
#endif
#else
#define HZ CLOCKS_PER_SEC
#endif



// Added by Cat for WIN32 port
#ifdef _WIN32
struct tms {
	   clock_t tms_utime;		/* user time */
	   clock_t tms_stime;		/* system time */
	   clock_t tms_cutime;		/* user time, children */
	   clock_t tms_cstime;		/* system time, children */
      };

  #include <windows.h>

#endif


CpuTimer::CpuTimer()
{
  tms current;

  // Added by Cat for WIN32 port
#ifdef _WIN32
  nt_times(&current);
  wallTimeInitial = clock();
  wallTime = wallTimeInitial;
#else
  gettimeofday(&wallTimeInitial, NULL);
  wallTime = wallTimeInitial;
  times( &current );
#endif
  
  // Store current value of cpu time
  cpu = current.tms_utime +
        current.tms_stime +
        current.tms_cutime +
        current.tms_cstime;
  cpuInitial = cpu;
}

// Return time values

double
CpuTimer::cpu_secs()
{
  tms current;

  // Added by Cat for WIN32 port
#ifdef _WIN32
  nt_times(&current);    
#else
  times( &current );
#endif
  
  time_t cpu_now = current.tms_utime +
    current.tms_stime +
    current.tms_cutime +
    current.tms_cstime;
  time_t delta = cpu_now - cpu;
  cpu   = cpu_now;
#ifdef _WIN32
  if( delta == 0 )
     delta = 1;
#endif
  return (double) delta / HZ;<--- Skipping configuration 'CLK_TCK' since the value of 'CLK_TCK' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
}

double
CpuTimer::clock_secs()
{
  double elapsed_time;
#ifdef _WIN32
  clock_t current;
  current = clock();
  elapsed_time = double( current - wallTime );
  elapsed_time = elapsed_time / HZ;

  wallTime = current;
#else
  timeval current;
  gettimeofday( &current, NULL );
  double prev_time = wallTime.tv_sec;
  prev_time += double( wallTime.tv_usec ) / 1000000.0;
  
  elapsed_time = current.tv_sec;
  elapsed_time += double( current.tv_usec ) / 1000000.0;
  elapsed_time = elapsed_time - prev_time;
  
  wallTime = current;
#endif
  
  return elapsed_time;
}

double
CpuTimer::elapsed(bool wall_time)
{
  if( wall_time )
  {
    double elapsed;
#ifdef _WIN32
    clock_t current;
    current = clock();
    elapsed = double( current - wallTimeInitial);
    elapsed = elapsed / HZ;
#else    
    double current_time;
    timeval current;
    gettimeofday( &current, NULL );
    double initial_time;
    initial_time = wallTimeInitial.tv_sec;
    initial_time += double(wallTimeInitial.tv_usec) / 1000000.0;
    current_time = current.tv_sec;
    current_time += double( current.tv_usec ) / 1000000.0;
    
    elapsed = current_time - initial_time;
#endif
    return elapsed;
  }
  else
  {
    tms current;

// Added by Cat for WIN32 port
#ifdef _WIN32
    nt_times(&current);    
#else
    times( &current );
#endif
// Store totals
  
    time_t cpu_now = current.tms_utime +
        current.tms_stime +
        current.tms_cutime +
        current.tms_cstime;
    time_t elapsed = cpu_now - cpuInitial;
    return (double) elapsed / HZ;<--- Skipping configuration 'CLK_TCK' since the value of 'CLK_TCK' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
  }
}


// Added by Cat for WIN32 port
#ifdef _WIN32

void CpuTimer::nt_times(tms *sys)
{  
  HANDLE current_process = GetCurrentProcess();

  FILETIME ftCreation, ftExit, ftKernel, ftUser;  
  GetProcessTimes(current_process, &ftCreation, &ftExit, &ftKernel, &ftUser);  

  //kernel and user times are amounts of time rather than an actual time period. 
  //The value in the FILETIME structure is expressed in 100-nanosecond units.
  //Divide by 10 million to convert to seconds.

  __int64 i64 = *((__int64 *) &ftKernel);  
  double dw_kernel = (i64 / 10000000.0);

  i64 = *((__int64 *) &ftUser);
  double dw_user = (i64 / 10000000.0);

	sys->tms_utime = dw_user*HZ;
	sys->tms_stime = dw_kernel*HZ;
	sys->tms_cutime = 0;		
	sys->tms_cstime = 0;
        //PRINT_DEBUG( CUBIT_DEBUG_3,"clock returned %d\n", sys->tms_stime );  
}
#endif