深入redis内部之redis启动过程之二
接上文,繼續(xù)分析代碼
1. 設(shè)置線程安全模式
zmalloc_enable_thread_safeness();/*
設(shè)置線程安全標(biāo)識(shí)符為1
*/
void zmalloc_enable_thread_safeness(void) {
zmalloc_thread_safe = 1;
}
2. 內(nèi)存溢出處理
zmalloc_set_oom_handler(redisOutOfMemoryHandler);/*
內(nèi)存溢出的調(diào)用方法
*/
? ?void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) {
? ? ? zmalloc_oom_handler = oom_handler;
? ? ? }
//調(diào)用下一級(jí)
static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;
//最終調(diào)用
static void zmalloc_default_oom(size_t size) {
fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",size);
fflush(stderr);
abort();
}
?
3.生成hash seed
srand(time(NULL)^getpid());gettimeofday(&tv,NULL);dictSetHashFunctionSeed(tv.tv_sec^tv.tv_usec^getpid());?3.1?time(?)函數(shù)
頭文件:#include?<time.h>
函數(shù)定義:time_t?time(time_t?*timer)
功能描述:該函數(shù)返回從1970年1月1日00時(shí)00分00秒至今所經(jīng)過(guò)的秒數(shù)。如果time_t?*timer非空指針,函數(shù)也會(huì)將返回值存到timer指針指向的內(nèi)存。
返回值:成功則返回秒數(shù),失敗則返回((time_t)-1)值,錯(cuò)誤原因存于errno中。
3.2?getpid(取得進(jìn)程識(shí)別碼)
表頭文件 #include<unistd.h>
定義函數(shù) pid_t getpid(void);
函數(shù)說(shuō)明 getpid()用來(lái)取得目前進(jìn)程的進(jìn)程識(shí)別碼。
3.3?srand()函數(shù)?
void srand(unsigned seed) 初始化隨機(jī)數(shù)發(fā)生器。
3.4?gettimeofday()函數(shù)
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
gettimeofday()會(huì)把目前的時(shí)間用tv 結(jié)構(gòu)體返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中
3.5 設(shè)置hash seed
static uint32_t dict_hash_function_seed = 5381;void dictSetHashFunctionSeed(uint32_t seed) {dict_hash_function_seed = seed; }4. 檢查是否sentime模式(集群的臨時(shí)方案)
server.sentinel_mode = checkForSentinelMode(argc,argv); //根據(jù)啟動(dòng)的參數(shù)來(lái)檢查是否sentinel模式 /* Returns 1 if there is --sentinel among the arguments or if* argv[0] is exactly "redis-sentinel". */ int checkForSentinelMode(int argc, char **argv) {int j;if (strstr(argv[0],"redis-sentinel") != NULL) return 1;for (j = 1; j < argc; j++)if (!strcmp(argv[j],"--sentinel")) return 1;return 0; }?
轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/3539773.html
總結(jié)
以上是生活随笔為你收集整理的深入redis内部之redis启动过程之二的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 深入redis内部之redis启动过程之
- 下一篇: keepalive学习之软件设计