Skip to content

Commit

Permalink
Use clock_gettime and getrusage to compute real and user time.
Browse files Browse the repository at this point in the history
Better handling of clock jumps, from Scott Cheloa.
  • Loading branch information
jca committed Nov 24, 2017
1 parent ef99572 commit f4017f4
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions src/usr.bin/openssl/apps_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,48 @@
* Functions that need to be overridden by non-POSIX operating systems.
*/

#include <sys/times.h>
#include <sys/resource.h>
#include <sys/time.h>

#include <unistd.h>
#include <time.h>

#include "apps.h"

double
app_tminterval(int stop, int usertime)
static double
real_interval(int stop)
{
double ret = 0;
struct tms rus;
clock_t now = times(&rus);
static clock_t tmstart;
static struct timespec start;
struct timespec elapsed, now;

clock_gettime(CLOCK_MONOTONIC, &now);
if (stop) {
timespecsub(&now, &start, &elapsed);
return elapsed.tv_sec + elapsed.tv_nsec / 1000000000.0;
}
start = now;
return 0.0;
}

if (usertime)
now = rus.tms_utime;
static double
user_interval(int stop)
{
static struct timeval start;
struct timeval elapsed;
struct rusage now;

if (stop == TM_START)
tmstart = now;
else {
long int tck = sysconf(_SC_CLK_TCK);
ret = (now - tmstart) / (double) tck;
getrusage(RUSAGE_SELF, &now);
if (stop) {
timersub(&now.ru_utime, &start, &elapsed);
return elapsed.tv_sec + elapsed.tv_usec / 1000000.0;
}
start = now.ru_utime;
return 0.0;
}

return (ret);
double
app_tminterval(int stop, int usertime)
{
return (usertime) ? user_interval(stop) : real_interval(stop);
}

int
Expand Down

0 comments on commit f4017f4

Please sign in to comment.