Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use clock_gettime() instead of gettimeofday() on FreeBSD. #843

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Makefile
Expand Up @@ -101,9 +101,9 @@ REDIS_SERVER_NAME= redis-server
REDIS_SENTINEL_NAME= redis-sentinel
REDIS_SERVER_OBJ= adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o
REDIS_CLI_NAME= redis-cli
REDIS_CLI_OBJ= anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o
REDIS_CLI_OBJ= anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o util.o
REDIS_BENCHMARK_NAME= redis-benchmark
REDIS_BENCHMARK_OBJ= ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o redis-benchmark.o
REDIS_BENCHMARK_OBJ= ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o redis-benchmark.o util.o
REDIS_CHECK_DUMP_NAME= redis-check-dump
REDIS_CHECK_DUMP_OBJ= redis-check-dump.o lzf_c.o lzf_d.o crc64.o
REDIS_CHECK_AOF_NAME= redis-check-aof
Expand Down
3 changes: 2 additions & 1 deletion src/ae.c
Expand Up @@ -42,6 +42,7 @@
#include "ae.h"
#include "zmalloc.h"
#include "config.h"
#include "util.h"

/* Include the best multiplexing layer supported by this system.
* The following should be ordered by performances, descending. */
Expand Down Expand Up @@ -147,7 +148,7 @@ static void aeGetTime(long *seconds, long *milliseconds)
{
struct timeval tv;

gettimeofday(&tv, NULL);
redis_gettimeofday(&tv, NULL);
*seconds = tv.tv_sec;
*milliseconds = tv.tv_usec/1000;
}
Expand Down
4 changes: 4 additions & 0 deletions src/config.h
Expand Up @@ -178,4 +178,8 @@
#endif
#endif

#if defined(__FreeBSD__)
#define USE_CLOCK_REALTIME_FAST
#endif

#endif
3 changes: 2 additions & 1 deletion src/dict.c
Expand Up @@ -45,6 +45,7 @@
#include <ctype.h>

#include "dict.h"
#include "util.h"
#include "zmalloc.h"

/* Using dictEnableResize() / dictDisableResize() we make possible to
Expand Down Expand Up @@ -282,7 +283,7 @@ int dictRehash(dict *d, int n) {
long long timeInMilliseconds(void) {
struct timeval tv;

gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
}

Expand Down
2 changes: 1 addition & 1 deletion src/intset.c
Expand Up @@ -303,7 +303,7 @@ void ok(void) {

long long usec(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}

Expand Down
4 changes: 2 additions & 2 deletions src/redis-benchmark.c
Expand Up @@ -96,7 +96,7 @@ static long long ustime(void) {
struct timeval tv;
long long ust;

gettimeofday(&tv, NULL);
redis_gettimeofday(&tv, NULL);
ust = ((long)tv.tv_sec)*1000000;
ust += tv.tv_usec;
return ust;
Expand All @@ -106,7 +106,7 @@ static long long mstime(void) {
struct timeval tv;
long long mst;

gettimeofday(&tv, NULL);
redis_gettimeofday(&tv, NULL);
mst = ((long)tv.tv_sec)*1000;
mst += tv.tv_usec/1000;
return mst;
Expand Down
3 changes: 2 additions & 1 deletion src/redis-cli.c
Expand Up @@ -49,6 +49,7 @@
#include "help.h"
#include "anet.h"
#include "ae.h"
#include "util.h"

#define REDIS_NOTUSED(V) ((void) V)

Expand Down Expand Up @@ -94,7 +95,7 @@ static long long mstime(void) {
struct timeval tv;
long long mst;

gettimeofday(&tv, NULL);
redis_gettimeofday(&tv, NULL);
mst = ((long)tv.tv_sec)*1000;
mst += tv.tv_usec/1000;
return mst;
Expand Down
8 changes: 4 additions & 4 deletions src/redis.c
Expand Up @@ -278,7 +278,7 @@ void redisLogRaw(int level, const char *msg) {
int off;
struct timeval tv;

gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
off = strftime(buf,sizeof(buf),"%d %b %H:%M:%S.",localtime(&tv.tv_sec));
snprintf(buf+off,sizeof(buf)-off,"%03d",(int)tv.tv_usec/1000);
fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
Expand Down Expand Up @@ -340,7 +340,7 @@ long long ustime(void) {
struct timeval tv;
long long ust;

gettimeofday(&tv, NULL);
redis_gettimeofday(&tv, NULL);
ust = ((long long)tv.tv_sec)*1000000;
ust += tv.tv_usec;
return ust;
Expand Down Expand Up @@ -1866,7 +1866,7 @@ void timeCommand(redisClient *c) {

/* gettimeofday() can only fail if &tv is a bad addresss so we
* don't check for errors. */
gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
addReplyMultiBulkLen(c,2);
addReplyBulkLongLong(c,tv.tv_sec);
addReplyBulkLongLong(c,tv.tv_usec);
Expand Down Expand Up @@ -2613,7 +2613,7 @@ int main(int argc, char **argv) {
zmalloc_enable_thread_safeness();
zmalloc_set_oom_handler(redisOutOfMemoryHandler);
srand(time(NULL)^getpid());
gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
dictSetHashFunctionSeed(tv.tv_sec^tv.tv_usec^getpid());
server.sentinel_mode = checkForSentinelMode(argc,argv);
initServerConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/replication.c
Expand Up @@ -82,7 +82,7 @@ void replicationFeedMonitors(redisClient *c, list *monitors, int dictid, robj **
char ip[32];
struct timeval tv;

gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
cmdrepr = sdscatprintf(cmdrepr,"%ld.%06ld ",(long)tv.tv_sec,(long)tv.tv_usec);
if (c->flags & REDIS_LUA_CLIENT) {
cmdrepr = sdscatprintf(cmdrepr,"[%d lua] ",dictid);
Expand Down
20 changes: 18 additions & 2 deletions src/util.c
Expand Up @@ -35,9 +35,9 @@
#include <limits.h>
#include <math.h>
#include <unistd.h>
#include <sys/time.h>
#include <float.h>

#include "config.h"
#include "util.h"

/* Glob-style pattern matching. */
Expand Down Expand Up @@ -378,7 +378,7 @@ void getRandomHexChars(char *p, unsigned int len) {
pid_t pid = getpid();

/* Use time and PID to fill the initial array. */
gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
if (l >= sizeof(tv.tv_usec)) {
memcpy(x,&tv.tv_usec,sizeof(tv.tv_usec));
l -= sizeof(tv.tv_usec);
Expand All @@ -405,6 +405,22 @@ void getRandomHexChars(char *p, unsigned int len) {
fclose(fp);
}

int redis_gettimeofday(struct timeval *tp, struct timezone *tzp) {
#ifndef USE_CLOCK_REALTIME_FAST
return gettimeofday(tp, tzp);
#else
struct timespec ts;
int r = clock_gettime(CLOCK_REALTIME_FAST, &ts);
tp->tv_sec = ts.tv_sec;
tp->tv_usec = ts.tv_nsec/1000;
if (NULL != tzp) {
tzp->tz_minuteswest = 0;
tzp->tz_dsttime = 0;
}
return r;
#endif /* USE_CLOCK_REALTIME_FAST */
} /* redis_gettimeofday() */

#ifdef UTIL_TEST_MAIN
#include <assert.h>

Expand Down
3 changes: 3 additions & 0 deletions src/util.h
Expand Up @@ -30,12 +30,15 @@
#ifndef __REDIS_UTIL_H
#define __REDIS_UTIL_H

#include <sys/time.h>

int stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase);
int stringmatch(const char *p, const char *s, int nocase);
long long memtoll(const char *p, int *err);
int ll2string(char *s, size_t len, long long value);
int string2ll(const char *s, size_t slen, long long *value);
int string2l(const char *s, size_t slen, long *value);
int d2string(char *buf, size_t len, double value);
int redis_gettimeofday(struct timeval *tp, struct timezone *tzp);

#endif
2 changes: 1 addition & 1 deletion src/ziplist.c
Expand Up @@ -988,7 +988,7 @@ unsigned char *createIntList() {

long long usec(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
redis_gettimeofday(&tv,NULL);
return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
}

Expand Down