深入redis内部之redis启动过程之二
接上文,繼續分析代碼
1. 設置線程安全模式
zmalloc_enable_thread_safeness();/*
設置線程安全標識符為1
*/
void zmalloc_enable_thread_safeness(void) {
zmalloc_thread_safe = 1;
}
2. 內存溢出處理
zmalloc_set_oom_handler(redisOutOfMemoryHandler);/*
內存溢出的調用方法
*/
? ?void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) {
? ? ? zmalloc_oom_handler = oom_handler;
? ? ? }
//調用下一級
static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;
//最終調用
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(?)函數
頭文件:#include?<time.h>
函數定義:time_t?time(time_t?*timer)
功能描述:該函數返回從1970年1月1日00時00分00秒至今所經過的秒數。如果time_t?*timer非空指針,函數也會將返回值存到timer指針指向的內存。
返回值:成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存于errno中。
3.2?getpid(取得進程識別碼)
表頭文件 #include<unistd.h>
定義函數 pid_t getpid(void);
函數說明 getpid()用來取得目前進程的進程識別碼。
3.3?srand()函數?
void srand(unsigned seed) 初始化隨機數發生器。
3.4?gettimeofday()函數
#include<sys/time.h>
int gettimeofday(struct timeval*tv,struct timezone *tz )
gettimeofday()會把目前的時間用tv 結構體返回,當地時區的信息則放到tz所指的結構中
3.5 設置hash seed
static uint32_t dict_hash_function_seed = 5381;void dictSetHashFunctionSeed(uint32_t seed) {dict_hash_function_seed = seed; }4. 檢查是否sentime模式(集群的臨時方案)
server.sentinel_mode = checkForSentinelMode(argc,argv); //根據啟動的參數來檢查是否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; }?
轉載于:https://www.cnblogs.com/davidwang456/p/3539773.html
總結
以上是生活随笔為你收集整理的深入redis内部之redis启动过程之二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入redis内部之redis启动过程之
- 下一篇: Linux服务器性能评估与优化--转