今天看了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源码。