Skip to content

Commit 423500d

Browse files
authored
Merge pull request #3413 from facebook/timefn
minor refactoring for timefn
2 parents fd2eb8a + 8b13000 commit 423500d

File tree

5 files changed

+25
-53
lines changed

5 files changed

+25
-53
lines changed

programs/fileio.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <stdio.h> /* fprintf, open, fdopen, fread, _fileno, stdin, stdout */
2929
#include <stdlib.h> /* malloc, free */
3030
#include <string.h> /* strcmp, strlen */
31+
#include <time.h> /* clock_t, to measure process time */
3132
#include <fcntl.h> /* O_WRONLY */
3233
#include <assert.h>
3334
#include <errno.h> /* errno */

programs/timefn.c

+6-39
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@
2525

2626
UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
2727

28-
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
29-
{
30-
static LARGE_INTEGER ticksPerSecond;
31-
static int init = 0;
32-
if (!init) {
33-
if (!QueryPerformanceFrequency(&ticksPerSecond)) {
34-
perror("timefn::QueryPerformanceFrequency");
35-
abort();
36-
}
37-
init = 1;
38-
}
39-
return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
40-
}
41-
4228
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
4329
{
4430
static LARGE_INTEGER ticksPerSecond;
@@ -59,17 +45,6 @@ PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
5945

6046
UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
6147

62-
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
63-
{
64-
static mach_timebase_info_data_t rate;
65-
static int init = 0;
66-
if (!init) {
67-
mach_timebase_info(&rate);
68-
init = 1;
69-
}
70-
return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;
71-
}
72-
7348
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
7449
{
7550
static mach_timebase_info_data_t rate;
@@ -115,15 +90,6 @@ static UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
11590
return diff;
11691
}
11792

118-
PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
119-
{
120-
UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
121-
PTime micro = 0;
122-
micro += 1000000ULL * diff.tv_sec;
123-
micro += diff.tv_nsec / 1000ULL;
124-
return micro;
125-
}
126-
12793
PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
12894
{
12995
UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
@@ -134,25 +100,26 @@ PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
134100
}
135101

136102

137-
138-
#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
103+
#else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
139104

140105
UTIL_time_t UTIL_getTime(void) { return clock(); }
141-
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
142106
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
143107

144108
#endif
145109

110+
/* ==== Common functions, valid for all time API ==== */
146111

112+
PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
113+
{
114+
return UTIL_getSpanTimeNano(begin, end) / 1000ULL;
115+
}
147116

148-
/* returns time span in microseconds */
149117
PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
150118
{
151119
UTIL_time_t const clockEnd = UTIL_getTime();
152120
return UTIL_getSpanTimeMicro(clockStart, clockEnd);
153121
}
154122

155-
/* returns time span in microseconds */
156123
PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
157124
{
158125
UTIL_time_t const clockEnd = UTIL_getTime();

programs/timefn.h

+14-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ extern "C" {
1616
#endif
1717

1818

19-
/*-****************************************
20-
* Dependencies
21-
******************************************/
22-
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
23-
24-
2519

2620
/*-****************************************
2721
* Local Types
@@ -31,7 +25,7 @@ extern "C" {
3125
# if defined(_AIX)
3226
# include <inttypes.h>
3327
# else
34-
# include <stdint.h> /* intptr_t */
28+
# include <stdint.h> /* uint64_t */
3529
# endif
3630
typedef uint64_t PTime; /* Precise Time */
3731
#else
@@ -41,8 +35,10 @@ extern "C" {
4135

4236

4337
/*-****************************************
44-
* Time functions
38+
* Time types (note: OS dependent)
4539
******************************************/
40+
#include <time.h> /* TIME_UTC, then struct timespec and clock_t */
41+
4642
#if defined(_WIN32) /* Windows */
4743

4844
#include <windows.h> /* LARGE_INTEGER */
@@ -63,7 +59,7 @@ extern "C" {
6359
typedef struct timespec UTIL_time_t;
6460
#define UTIL_TIME_INITIALIZER { 0, 0 }
6561

66-
#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
62+
#else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
6763

6864
#define UTIL_TIME_USES_C90_CLOCK
6965
typedef clock_t UTIL_time_t;
@@ -72,15 +68,21 @@ extern "C" {
7268
#endif
7369

7470

71+
/*-****************************************
72+
* Time functions
73+
******************************************/
74+
7575
UTIL_time_t UTIL_getTime(void);
76-
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
76+
void UTIL_waitForNextTick(void);
77+
7778
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
79+
PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
7880

7981
#define SEC_TO_MICRO ((PTime)1000000)
82+
PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
8083
PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
81-
PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
8284

83-
void UTIL_waitForNextTick(void);
85+
8486

8587

8688
#if defined (__cplusplus)

tests/decodecorpus.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <stdio.h>
1515
#include <stdlib.h>
1616
#include <string.h>
17+
#include <time.h> /* time(), for seed random initialization */
1718

1819
#include "util.h"
1920
#include "timefn.h" /* UTIL_clockSpanMicro, SEC_TO_MICRO, UTIL_TIME_INITIALIZER */
@@ -24,7 +25,7 @@
2425
#include "zdict.h"
2526

2627
/* Direct access to internal compression functions is required */
27-
#include "zstd_compress.c"
28+
#include "zstd_compress.c" /* ZSTD_resetSeqStore, ZSTD_storeSeq, *_TO_OFFBASE, HIST_countFast_wksp, HIST_isError */
2829

2930
#define XXH_STATIC_LINKING_ONLY
3031
#include "xxhash.h" /* XXH64 */
@@ -165,7 +166,7 @@ static double RAND_exp(U32* seed, double mean)
165166
/*-*******************************************************
166167
* Constants and Structs
167168
*********************************************************/
168-
const char *BLOCK_TYPES[] = {"raw", "rle", "compressed"};
169+
const char* BLOCK_TYPES[] = {"raw", "rle", "compressed"};
169170

170171
#define MAX_DECOMPRESSED_SIZE_LOG 20
171172
#define MAX_DECOMPRESSED_SIZE (1ULL << MAX_DECOMPRESSED_SIZE_LOG)

tests/zstreamtest.c

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <stdlib.h> /* free */
2626
#include <stdio.h> /* fgets, sscanf */
2727
#include <string.h> /* strcmp */
28+
#include <time.h> /* time_t, time(), to randomize seed */
2829
#include <assert.h> /* assert */
2930
#include "timefn.h" /* UTIL_time_t, UTIL_getTime */
3031
#include "mem.h"

0 commit comments

Comments
 (0)