Skip to content
This repository

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

Open
wants to merge 1 commit into from

1 participant

Lenny Maiorani
Lenny Maiorani

On FreeBSD calling gettimeofday() causes all the cores on a multicore
system to be synchronized. On a heavily loaded system with a
significantly CPU bound application this can cause a 40% overall
system degradation.

A better option is to use clock_gettime() and pass in the
CLOCK_REALTIME_FAST clock as the clock to use. This achieves the
same behavior as gettimeofday() on Linux.

Lenny Maiorani ldm5180 Use clock_gettime() instead of gettimeofday() on FreeBSD.
On FreeBSD calling gettimeofday() causes all the cores on a multicore
system to be synchronized. On a heavily loaded system with a
significantly CPU bound application this can cause a 40% overall
system degradation.

A better option is to use clock_gettime() and pass in the
CLOCK_REALTIME_FAST clock as the clock to use. This achieves the
same behavior as gettimeofday() on Linux.
a07ca70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 21, 2012
Lenny Maiorani ldm5180 Use clock_gettime() instead of gettimeofday() on FreeBSD.
On FreeBSD calling gettimeofday() causes all the cores on a multicore
system to be synchronized. On a heavily loaded system with a
significantly CPU bound application this can cause a 40% overall
system degradation.

A better option is to use clock_gettime() and pass in the
CLOCK_REALTIME_FAST clock as the clock to use. This achieves the
same behavior as gettimeofday() on Linux.
a07ca70
This page is out of date. Refresh to see the latest.
4 src/Makefile
@@ -101,9 +101,9 @@ REDIS_SERVER_NAME= redis-server
101 101 REDIS_SENTINEL_NAME= redis-sentinel
102 102 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
103 103 REDIS_CLI_NAME= redis-cli
104   -REDIS_CLI_OBJ= anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o
  104 +REDIS_CLI_OBJ= anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o util.o
105 105 REDIS_BENCHMARK_NAME= redis-benchmark
106   -REDIS_BENCHMARK_OBJ= ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o redis-benchmark.o
  106 +REDIS_BENCHMARK_OBJ= ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o redis-benchmark.o util.o
107 107 REDIS_CHECK_DUMP_NAME= redis-check-dump
108 108 REDIS_CHECK_DUMP_OBJ= redis-check-dump.o lzf_c.o lzf_d.o crc64.o
109 109 REDIS_CHECK_AOF_NAME= redis-check-aof
3  src/ae.c
@@ -42,6 +42,7 @@
42 42 #include "ae.h"
43 43 #include "zmalloc.h"
44 44 #include "config.h"
  45 +#include "util.h"
45 46
46 47 /* Include the best multiplexing layer supported by this system.
47 48 * The following should be ordered by performances, descending. */
@@ -147,7 +148,7 @@ static void aeGetTime(long *seconds, long *milliseconds)
147 148 {
148 149 struct timeval tv;
149 150
150   - gettimeofday(&tv, NULL);
  151 + redis_gettimeofday(&tv, NULL);
151 152 *seconds = tv.tv_sec;
152 153 *milliseconds = tv.tv_usec/1000;
153 154 }
4 src/config.h
@@ -178,4 +178,8 @@
178 178 #endif
179 179 #endif
180 180
  181 +#if defined(__FreeBSD__)
  182 +#define USE_CLOCK_REALTIME_FAST
  183 +#endif
  184 +
181 185 #endif
3  src/dict.c
@@ -45,6 +45,7 @@
45 45 #include <ctype.h>
46 46
47 47 #include "dict.h"
  48 +#include "util.h"
48 49 #include "zmalloc.h"
49 50
50 51 /* Using dictEnableResize() / dictDisableResize() we make possible to
@@ -282,7 +283,7 @@ int dictRehash(dict *d, int n) {
282 283 long long timeInMilliseconds(void) {
283 284 struct timeval tv;
284 285
285   - gettimeofday(&tv,NULL);
  286 + redis_gettimeofday(&tv,NULL);
286 287 return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
287 288 }
288 289
2  src/intset.c
@@ -303,7 +303,7 @@ void ok(void) {
303 303
304 304 long long usec(void) {
305 305 struct timeval tv;
306   - gettimeofday(&tv,NULL);
  306 + redis_gettimeofday(&tv,NULL);
307 307 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
308 308 }
309 309
4 src/redis-benchmark.c
@@ -96,7 +96,7 @@ static long long ustime(void) {
96 96 struct timeval tv;
97 97 long long ust;
98 98
99   - gettimeofday(&tv, NULL);
  99 + redis_gettimeofday(&tv, NULL);
100 100 ust = ((long)tv.tv_sec)*1000000;
101 101 ust += tv.tv_usec;
102 102 return ust;
@@ -106,7 +106,7 @@ static long long mstime(void) {
106 106 struct timeval tv;
107 107 long long mst;
108 108
109   - gettimeofday(&tv, NULL);
  109 + redis_gettimeofday(&tv, NULL);
110 110 mst = ((long)tv.tv_sec)*1000;
111 111 mst += tv.tv_usec/1000;
112 112 return mst;
3  src/redis-cli.c
@@ -49,6 +49,7 @@
49 49 #include "help.h"
50 50 #include "anet.h"
51 51 #include "ae.h"
  52 +#include "util.h"
52 53
53 54 #define REDIS_NOTUSED(V) ((void) V)
54 55
@@ -94,7 +95,7 @@ static long long mstime(void) {
94 95 struct timeval tv;
95 96 long long mst;
96 97
97   - gettimeofday(&tv, NULL);
  98 + redis_gettimeofday(&tv, NULL);
98 99 mst = ((long)tv.tv_sec)*1000;
99 100 mst += tv.tv_usec/1000;
100 101 return mst;
8 src/redis.c
@@ -278,7 +278,7 @@ void redisLogRaw(int level, const char *msg) {
278 278 int off;
279 279 struct timeval tv;
280 280
281   - gettimeofday(&tv,NULL);
  281 + redis_gettimeofday(&tv,NULL);
282 282 off = strftime(buf,sizeof(buf),"%d %b %H:%M:%S.",localtime(&tv.tv_sec));
283 283 snprintf(buf+off,sizeof(buf)-off,"%03d",(int)tv.tv_usec/1000);
284 284 fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
@@ -340,7 +340,7 @@ long long ustime(void) {
340 340 struct timeval tv;
341 341 long long ust;
342 342
343   - gettimeofday(&tv, NULL);
  343 + redis_gettimeofday(&tv, NULL);
344 344 ust = ((long long)tv.tv_sec)*1000000;
345 345 ust += tv.tv_usec;
346 346 return ust;
@@ -1866,7 +1866,7 @@ void timeCommand(redisClient *c) {
1866 1866
1867 1867 /* gettimeofday() can only fail if &tv is a bad addresss so we
1868 1868 * don't check for errors. */
1869   - gettimeofday(&tv,NULL);
  1869 + redis_gettimeofday(&tv,NULL);
1870 1870 addReplyMultiBulkLen(c,2);
1871 1871 addReplyBulkLongLong(c,tv.tv_sec);
1872 1872 addReplyBulkLongLong(c,tv.tv_usec);
@@ -2613,7 +2613,7 @@ int main(int argc, char **argv) {
2613 2613 zmalloc_enable_thread_safeness();
2614 2614 zmalloc_set_oom_handler(redisOutOfMemoryHandler);
2615 2615 srand(time(NULL)^getpid());
2616   - gettimeofday(&tv,NULL);
  2616 + redis_gettimeofday(&tv,NULL);
2617 2617 dictSetHashFunctionSeed(tv.tv_sec^tv.tv_usec^getpid());
2618 2618 server.sentinel_mode = checkForSentinelMode(argc,argv);
2619 2619 initServerConfig();
2  src/replication.c
@@ -82,7 +82,7 @@ void replicationFeedMonitors(redisClient *c, list *monitors, int dictid, robj **
82 82 char ip[32];
83 83 struct timeval tv;
84 84
85   - gettimeofday(&tv,NULL);
  85 + redis_gettimeofday(&tv,NULL);
86 86 cmdrepr = sdscatprintf(cmdrepr,"%ld.%06ld ",(long)tv.tv_sec,(long)tv.tv_usec);
87 87 if (c->flags & REDIS_LUA_CLIENT) {
88 88 cmdrepr = sdscatprintf(cmdrepr,"[%d lua] ",dictid);
20 src/util.c
@@ -35,9 +35,9 @@
35 35 #include <limits.h>
36 36 #include <math.h>
37 37 #include <unistd.h>
38   -#include <sys/time.h>
39 38 #include <float.h>
40 39
  40 +#include "config.h"
41 41 #include "util.h"
42 42
43 43 /* Glob-style pattern matching. */
@@ -378,7 +378,7 @@ void getRandomHexChars(char *p, unsigned int len) {
378 378 pid_t pid = getpid();
379 379
380 380 /* Use time and PID to fill the initial array. */
381   - gettimeofday(&tv,NULL);
  381 + redis_gettimeofday(&tv,NULL);
382 382 if (l >= sizeof(tv.tv_usec)) {
383 383 memcpy(x,&tv.tv_usec,sizeof(tv.tv_usec));
384 384 l -= sizeof(tv.tv_usec);
@@ -405,6 +405,22 @@ void getRandomHexChars(char *p, unsigned int len) {
405 405 fclose(fp);
406 406 }
407 407
  408 +int redis_gettimeofday(struct timeval *tp, struct timezone *tzp) {
  409 +#ifndef USE_CLOCK_REALTIME_FAST
  410 + return gettimeofday(tp, tzp);
  411 +#else
  412 + struct timespec ts;
  413 + int r = clock_gettime(CLOCK_REALTIME_FAST, &ts);
  414 + tp->tv_sec = ts.tv_sec;
  415 + tp->tv_usec = ts.tv_nsec/1000;
  416 + if (NULL != tzp) {
  417 + tzp->tz_minuteswest = 0;
  418 + tzp->tz_dsttime = 0;
  419 + }
  420 + return r;
  421 +#endif /* USE_CLOCK_REALTIME_FAST */
  422 +} /* redis_gettimeofday() */
  423 +
408 424 #ifdef UTIL_TEST_MAIN
409 425 #include <assert.h>
410 426
3  src/util.h
@@ -30,6 +30,8 @@
30 30 #ifndef __REDIS_UTIL_H
31 31 #define __REDIS_UTIL_H
32 32
  33 +#include <sys/time.h>
  34 +
33 35 int stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase);
34 36 int stringmatch(const char *p, const char *s, int nocase);
35 37 long long memtoll(const char *p, int *err);
@@ -37,5 +39,6 @@ int ll2string(char *s, size_t len, long long value);
37 39 int string2ll(const char *s, size_t slen, long long *value);
38 40 int string2l(const char *s, size_t slen, long *value);
39 41 int d2string(char *buf, size_t len, double value);
  42 +int redis_gettimeofday(struct timeval *tp, struct timezone *tzp);
40 43
41 44 #endif
2  src/ziplist.c
@@ -988,7 +988,7 @@ unsigned char *createIntList() {
988 988
989 989 long long usec(void) {
990 990 struct timeval tv;
991   - gettimeofday(&tv,NULL);
  991 + redis_gettimeofday(&tv,NULL);
992 992 return (((long long)tv.tv_sec)*1000000)+tv.tv_usec;
993 993 }
994 994

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.