redis源码之main()函数剖析
生活随笔
收集整理的這篇文章主要介紹了
redis源码之main()函数剖析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天看了redis的源碼之中的main()函數,將大概的流程進行梳理。
在代碼中進行了相應的注釋,便于閱讀者理解:
int main(int argc, char **argv) {struct timeval tv;int j;#ifdef REDIS_TESTif (argc == 3 && !strcasecmp(argv[1], "test")) {if (!strcasecmp(argv[2], "ziplist")) {return ziplistTest(argc, argv);} else if (!strcasecmp(argv[2], "quicklist")) {quicklistTest(argc, argv);} else if (!strcasecmp(argv[2], "intset")) {return intsetTest(argc, argv);} else if (!strcasecmp(argv[2], "zipmap")) {return zipmapTest(argc, argv);} else if (!strcasecmp(argv[2], "sha1test")) {return sha1Test(argc, argv);} else if (!strcasecmp(argv[2], "util")) {return utilTest(argc, argv);} else if (!strcasecmp(argv[2], "sds")) {return sdsTest(argc, argv);} else if (!strcasecmp(argv[2], "endianconv")) {return endianconvTest(argc, argv);} else if (!strcasecmp(argv[2], "crc64")) {return crc64Test(argc, argv);} else if (!strcasecmp(argv[2], "zmalloc")) {return zmalloc_test(argc, argv);}return -1; /* test not found */} #endif/* We need to initialize our libraries, and the server configuration. */ #ifdef INIT_SETPROCTITLE_REPLACEMENTspt_init(argc, argv); #endifsetlocale(LC_COLLATE,""); //更改字符編碼tzset(); /* Populates 'timezone' global. */// oom時的處理,主要是內存不足時,將需要的memory的值打印出來zmalloc_set_oom_handler(redisOutOfMemoryHandler);// 根據當前時間和pid獲取隨機值的srand(time(NULL)^getpid());gettimeofday(&tv,NULL); // 1970年1月1日到現在的時間char hashseed[16];//獲取哈希種子getRandomHexChars(hashseed,sizeof(hashseed));dictSetHashFunctionSeed((uint8_t*)hashseed);// 服務器的啟動模式:單機模式、Cluster模式、sentinel模式server.sentinel_mode = checkForSentinelMode(argc,argv);// 服務器端的配置initServerConfig();ACLInit(); /* The ACL subsystem must be initialized ASAP because thebasic networking code and client creation depends on it. */moduleInitModulesSystem();/* Store the executable path and arguments in a safe place in order* to be able to restart the server later. */server.executable = getAbsolutePath(argv[0]);server.exec_argv = zmalloc(sizeof(char*)*(argc+1));server.exec_argv[argc] = NULL;for (j = 0; j < argc; j++) server.exec_argv[j] = zstrdup(argv[j]);/* We need to init sentinel right now as parsing the configuration file* in sentinel mode will have the effect of populating the sentinel* data structures with master nodes to monitor. */// sentinel模式下的配置if (server.sentinel_mode) {initSentinelConfig();initSentinel();}/* Check if we need to start in redis-check-rdb/aof mode. We just execute* the program main. However the program is part of the Redis executable* so that we can easily execute an RDB check on loading errors. */// 持久化的兩種方式if (strstr(argv[0],"redis-check-rdb") != NULL)// 定期的將數據dump到磁盤中redis_check_rdb_main(argc,argv,NULL);else if (strstr(argv[0],"redis-check-aof") != NULL)// 通過aof記錄事務執行的每個命令,便于數據的恢復redis_check_aof_main(argc,argv);// 幫助信息if (argc >= 2) {j = 1; /* First option to parse in argv[] */sds options = sdsempty();char *configfile = NULL;/* Handle special options --help and --version */if (strcmp(argv[1], "-v") == 0 ||strcmp(argv[1], "--version") == 0) version();if (strcmp(argv[1], "--help") == 0 ||strcmp(argv[1], "-h") == 0) usage();if (strcmp(argv[1], "--test-memory") == 0) {if (argc == 3) {memtest(atoi(argv[2]),50);exit(0);} else {fprintf(stderr,"Please specify the amount of memory to test in megabytes.\n");fprintf(stderr,"Example: ./redis-server --test-memory 4096\n\n");exit(1);}}/* First argument is the config file name? */if (argv[j][0] != '-' || argv[j][1] != '-') {configfile = argv[j];server.configfile = getAbsolutePath(configfile);/* Replace the config file in server.exec_argv with* its absolute path. */zfree(server.exec_argv[j]);server.exec_argv[j] = zstrdup(server.configfile);j++;}/* All the other options are parsed and conceptually appended to the* configuration file. For instance --port 6380 will generate the* string "port 6380\n" to be parsed after the actual file name* is parsed, if any. */while(j != argc) {if (argv[j][0] == '-' && argv[j][1] == '-') {/* Option name */if (!strcmp(argv[j], "--check-rdb")) {/* Argument has no options, need to skip for parsing. */j++;continue;}if (sdslen(options)) options = sdscat(options,"\n");options = sdscat(options,argv[j]+2);options = sdscat(options," ");} else {/* Option argument */options = sdscatrepr(options,argv[j],strlen(argv[j]));options = sdscat(options," ");}j++;}if (server.sentinel_mode && configfile && *configfile == '-') {serverLog(LL_WARNING,"Sentinel config from STDIN not allowed.");serverLog(LL_WARNING,"Sentinel needs config file on disk to save state. Exiting...");exit(1);}// 釋放內存 resetServerSaveParams();loadServerConfig(configfile,options);sdsfree(options);}serverLog(LL_WARNING, "oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo");serverLog(LL_WARNING,"Redis version=%s, bits=%d, commit=%s, modified=%d, pid=%d, just started",REDIS_VERSION,(sizeof(long) == 8) ? 64 : 32,redisGitSHA1(),strtol(redisGitDirty(),NULL,10) > 0,(int)getpid());if (argc == 1) {serverLog(LL_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");} else {serverLog(LL_WARNING, "Configuration loaded");}server.supervised = redisIsSupervised(server.supervised_mode);int background = server.daemonize && !server.supervised;if (background) daemonize();//sever端的初始化initServer();if (background || server.pidfile) createPidFile();// 設置進程的titleredisSetProcTitle(argv[0]);redisAsciiArt();checkTcpBacklogSettings();// 非sentinel模式if (!server.sentinel_mode) {/* Things not needed when running in Sentinel mode. */serverLog(LL_WARNING,"Server initialized");#ifdef __linux__linuxMemoryWarnings();#endifmoduleLoadFromQueue();ACLLoadUsersAtStartup();// 從磁盤獲取已經持久化的數據loadDataFromDisk();if (server.cluster_enabled) {if (verifyClusterConfigWithData() == C_ERR) {serverLog(LL_WARNING,"You can't have keys in a DB different than DB 0 when in ""Cluster mode. Exiting.");exit(1);}}if (server.ipfd_count > 0)serverLog(LL_NOTICE,"Ready to accept connections");if (server.sofd > 0)serverLog(LL_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);} else {// 檢測sentinel的配置是否有效sentinelIsRunning();}/* Warning the user about suspicious maxmemory setting. */if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {serverLog(LL_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);}// 設置服務器sleep之前的函數調用aeSetBeforeSleepProc(server.el,beforeSleep);// 設置服務器sleep后的函數調用aeSetAfterSleepProc(server.el,afterSleep);// 主函數事件驅動aeMain(server.el);// 刪除事件驅動aeDeleteEventLoop(server.el);return 0; }閱讀了main()函數中的源碼之后發現redis居然有OOM機制,內存分配不出來時,將申請的內存打印出來等等。由于本人能力有限只能理解到這里。后續繼續分析redis源碼。
總結
以上是生活随笔為你收集整理的redis源码之main()函数剖析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《鸟哥Linux私房菜》系列
- 下一篇: DFA敏感词过滤算法详解