Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

support mac os develop #22

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ endif
REDIS_SERVER_NAME=redis-server
REDIS_SENTINEL_NAME=redis-sentinel
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.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 msqueue.o aof_buf_queue.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 notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o memcached.o memcached_parser.o redis_oplog.o
ifeq ($(uname_S),Darwin)
REDIS_SERVER_OBJ += pthread_spinlock.o
endif
REDIS_CLI_NAME=redis-cli
REDIS_CLI_OBJ=anet.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o
REDIS_BENCHMARK_NAME=redis-benchmark
Expand Down
4 changes: 4 additions & 0 deletions src/aof_buf_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
#include "msqueue.h"
#include "util.h"

#ifdef __APPLE__
#define pthread_yield sched_yield
#endif

#define REDIS_AOF_BUF_QUEUE_DEFAULT_LIMIT (1 << 30)
#define REDIS_AOF_BUF_QUEUE_MIN_LIMIT (256*1024*1024)
#define AOF_BIO_LATENCY_LOG_RATE_US (30*1000*1000) //30 seconds
Expand Down
4 changes: 4 additions & 0 deletions src/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,11 @@ void _serverAssertPrintClientInfo(const client *c) {

bugReportStart();
serverLog(LL_WARNING,"=== ASSERTION FAILED CLIENT CONTEXT ===");
#if defined(__APPLE__)
serverLog(LL_WARNING,"client->flags = %llu", c->flags);
#elif (defined(__linux__) && defined(__GLIBC__))
serverLog(LL_WARNING,"client->flags = %ld", c->flags);
#endif
serverLog(LL_WARNING,"client->fd = %d", c->fd);
serverLog(LL_WARNING,"client->argc = %d", c->argc);
for (j=0; j < c->argc; j++) {
Expand Down
2 changes: 2 additions & 0 deletions src/endianconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ uint64_t intrev64(uint64_t v);
#define intrev16ifbe(v) (v)
#define intrev32ifbe(v) (v)
#define intrev64ifbe(v) (v)
#ifndef __APPLE__
#define ntohll(v) intrev64(v)
#define htonll(v) intrev64(v)
#endif
#else
#define memrev16ifbe(p) memrev16(p)
#define memrev32ifbe(p) memrev32(p)
Expand Down
15 changes: 15 additions & 0 deletions src/memcached_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
#include "server.h"
#include "endianconv.h"

#ifdef __APPLE__
void *memrchr(char *s,int c,int n) {
char *end = s + n;

do {
--end;
if (c == (int)(*end)) {
return end;
}
} while(s != end);

return NULL;
}
#endif

extern void addCommandStats(client *c);
extern void setProtocolError(const char *errstr, client *c, int pos);
extern int time_independent_strcmp(char *a, char *b);
Expand Down
3 changes: 3 additions & 0 deletions src/msqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#ifndef __MSQUEUE_H__
#define __MSQUEUE_H__
#include <pthread.h>
#ifdef __APPLE__
#include "pthread_spinlock.h"
#endif

typedef struct queueNode {
struct queueNode *next;
Expand Down
72 changes: 72 additions & 0 deletions src/pthread_spinlock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* pthread spinklock macos implementation
*
* Copyright (c) 2019-2021, wei.kukey <wei.kukey at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "pthread_spinlock.h"
#include <errno.h>
#include <sched.h>

#define UNUSED(V) ((void) V)

int pthread_spin_init(pthread_spinlock_t *lock, int pshared) {
__asm__ __volatile__ ("" ::: "memory");
UNUSED(pshared);
*lock = 0;
return 0;
}

int pthread_spin_destroy(pthread_spinlock_t *lock) {
UNUSED(lock);
return 0;
}

int pthread_spin_lock(pthread_spinlock_t *lock) {
while (1) {
int i;
for (i=0; i < 10000; i++) {
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
return 0;
}
}
sched_yield();
}
}

int pthread_spin_trylock(pthread_spinlock_t *lock) {
if (__sync_bool_compare_and_swap(lock, 0, 1)) {
return 0;
}
return EBUSY;
}

int pthread_spin_unlock(pthread_spinlock_t *lock) {
__asm__ __volatile__ ("" ::: "memory");
*lock = 0;
return 0;
}

41 changes: 41 additions & 0 deletions src/pthread_spinlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* pthread spinklock macos implementation
*
* Copyright (c) 2019-2021, wei.kukey <wei.kukey at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef KCC_REDIS_PTHREAD_SPINLOCK_H
#define KCC_REDIS_PTHREAD_SPINLOCK_H

typedef int pthread_spinlock_t;

int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
int pthread_spin_destroy(pthread_spinlock_t *lock);
int pthread_spin_lock(pthread_spinlock_t *lock);
int pthread_spin_trylock(pthread_spinlock_t *lock);
int pthread_spin_unlock(pthread_spinlock_t *lock);

#endif //KCC_REDIS_PTHREAD_SPINLOCK_H
34 changes: 32 additions & 2 deletions src/redis_oplog.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,13 @@ void cronBgsave(void) {
if (growth >= server.cron_bgsave_rewrite_perc ||
(server.maxmemory && (unsigned)server.aof_inc_from_last_cron_bgsave
> server.maxmemory * server.cron_bgsave_rewrite_perc / 100)) {
#if defined(__APPLE__)
serverLog(LL_NOTICE,"Starting cron bgsave on %lld%% (%lld/%lld) aof growth",
growth, server.aof_inc_from_last_cron_bgsave, base);
#elif (defined(__linux__) && defined(__GLIBC__))
serverLog(LL_NOTICE,"Starting cron bgsave on %lld%% (%ld/%lld) aof growth",
growth, server.aof_inc_from_last_cron_bgsave, base);
#endif
rdbSaveInfo rsi, *rsiptr;
rsiptr = rdbPopulateSaveInfo(&rsi);
rdbSaveBackground(server.rdb_filename, rsiptr);
Expand Down Expand Up @@ -521,8 +526,13 @@ long long getAofFirstOpid(char *filename) {
if (!strcasecmp("opinfo", argv[0])) {
redisOplogHeader *loaded_header = (redisOplogHeader *)argv[1];
long long tmp_opid = loaded_header->opid;
#if defined(__APPLE__)
serverLog(LL_VERBOSE, "getAofFirstOpid: parsed first opid %lld in %s",
loaded_header->opid, filename);
#elif (defined(__linux__) && defined(__GLIBC__))
serverLog(LL_VERBOSE, "getAofFirstOpid: parsed first opid %ld in %s",
loaded_header->opid, filename);
#endif
cleaningOfGetAofFirstOpid(argv, fp, argc);
return tmp_opid;
} else {
Expand Down Expand Up @@ -1634,19 +1644,35 @@ int writeAndFlushTmpRdbIndex(FILE *fp_tmp_rdb_index, char *filename) {
}

if(server.aof_state != AOF_OFF) {
#if defined(__APPLE__)
if(fprintf(fp_tmp_rdb_index, "%s %s %lld %lld\n", \
filename, server.aof_filename, \
server.aof_current_size,
(server.next_opid < 1 ? 0 : server.next_opid)) < 0)
#elif (defined(__linux__) && defined(__GLIBC__))
if(fprintf(fp_tmp_rdb_index, "%s %s %ld %lld\n", \
filename, server.aof_filename, \
server.aof_current_size,
(server.next_opid < 1 ? 0 : server.next_opid)) < 0) {
(server.next_opid < 1 ? 0 : server.next_opid)) < 0)
#endif
{
serverLog(LL_WARNING, "Writing %s error: %s",
REDIS_RDB_INDEX_TMP_FILENAME, strerror(errno));
return C_ERR;
}
#if defined(__APPLE__)
serverLog(LL_NOTICE,
"BGSAVE done, write rdb.index, rdb name: %s, aof name: %s, "
"aof offset: %lld, next opid: %lld",
filename, server.aof_filename, \
server.aof_current_size, server.next_opid);
#elif (defined(__linux__) && defined(__GLIBC__))
serverLog(LL_NOTICE,
"BGSAVE done, write rdb.index, rdb name: %s, aof name: %s, "
"aof offset: %ld, next opid: %lld",
filename, server.aof_filename, \
server.aof_current_size, server.next_opid);
#endif
if (rdbSaveAppliedInfoToRdbIndex(fp_tmp_rdb_index) != C_OK) {
return C_ERR;
}
Expand Down Expand Up @@ -1813,7 +1839,11 @@ sds catExtraReplInfo(sds info) {
info = sdscatprintf(info,
"aof_psyncing_state:%d\r\n"
"aof_psync_reading_filename:%s\r\n"
#if defined(__APPLE__)
"aof_psync_reading_offset:%lld\r\n"
#elif (defined(__linux__) && defined(__GLIBC__))
"aof_psync_reading_offset:%ld\r\n"
#endif
"next_opid:%lld\r\n"
"second_replid_opid:%lld\r\n",
(slaveCheckAofPsyncingState() == C_OK ? 1 : 0),
Expand Down Expand Up @@ -2453,7 +2483,7 @@ void doSendAofToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {

/* Align IOBUF to multiple of 1024 */
const int IOBUF_LEN = PROTO_IOBUF_LEN;
char buf[IOBUF_LEN];
char buf[PROTO_IOBUF_LEN];
ssize_t nwritten, buflen;

lseek(aof_psync_reading_fd, slave->repldboff, SEEK_SET);
Expand Down