今天看了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()函数剖析相关推荐

  1. x265源码分析 main函数 x265.cpp

    图片转载于x265源码流程分析_Dillon2015的博客-CSDN博客_x265编码流程 cliopt.prase main ()函数--解析函数参数并进行编码准备工作:x265.cpp (1)Ge ...

  2. RT-Thread源码-4-线程函数剖析

    前言 TCB 线程控制块 RT-Thread中每个线程的信息用线程控制块(Thread Control-Block,缩写为TCB)表示,它是定义在rtdef.h中的struct结构体,用来描述一个线程 ...

  3. Redis源码分析--lookupKey函数查看value值

    lookupKey函数查看value值 robj *lookupKey(redisDb *db, robj *key) {dictEntry *de = dictFind(db->dict,ke ...

  4. Redis源码-BFS方式浏览main函数

    文章目录 前言 看代码的方式 Redis 服务器的 main 函数 main 函数分段解释 函数名及参数 启动测试程序 程序环境初始化 初始化配置信息 存储参数信息 根据参数确定启动方式 处理并加载命 ...

  5. 【Redis源码剖析】 - Redis内置数据结构之压缩列表ziplist

    在前面的一篇文章[Redis源码剖析] - Redis内置数据结构之双向链表中,我们介绍了Redis封装的一种"传统"双向链表list,分别使用prev.next指针来指向当前节点 ...

  6. Redis源码剖析和注释(十六)---- Redis输入输出的抽象(rio)

    Redis源码剖析和注释(十六)---- Redis输入输出的抽象(rio) . https://blog.csdn.net/men_wen/article/details/71131550 Redi ...

  7. 【Redis源码剖析】 - Redis IO操作之rio

    原创作品,转载请标明:http://blog.csdn.net/xiejingfa/article/details/51433696 Redis源码剖析系列文章汇总:传送门 Reids内部封装了一个I ...

  8. Redis源码剖析之GEO——Redis是如何高效检索地理位置的?

    Redis GEO 用做存储地理位置信息,并对存储的信息进行操作.通过geo相关的命令,可以很容易在redis中存储和使用经纬度坐标信息.Redis中提供的Geo命令有如下几个: geoadd:添加经 ...

  9. 【Redis源码剖析】 - Redis持久化之RDB

    原创作品,转载请标明:http://blog.csdn.net/xiejingfa/article/details/51553370 Redis源码剖析系列文章汇总:传送门 Redis是一个高效的内存 ...

最新文章

  1. java 调用webservice的各种方法总结
  2. 设计模式 — 创建型模式 — 工厂模式
  3. Unknown column 'subject_1.pid' in 'field list') [SQL: 'SELECT anon_1.screen_id AS anon_1_screen_i
  4. 小程序开发系类之基础部分-开发工具
  5. DataSourceUtils(使用C3P0连接池的工具类)
  6. ubuntu16.04下安装opencv3.2版本
  7. 详细设计 英文_官宣 | 闽江学院官方文创产品设计稿征集
  8. Entity framework 配置文件,实现类,测试类
  9. 末转变者登录服务器一直在排队,魔兽世界:国服神级服务器,排队持续两年,哈霍兰有何特别之处?...
  10. opencv-python学习一--人脸检测
  11. 2016年最新J2EE基础入门教程目录(完结版)
  12. 《社会调查数据管理——基于Stata 14管理CGSS数据》一1.3 数据管理工作主体不明...
  13. C语言实现24点游戏算法
  14. 想买个吉他英雄3的正版
  15. Visual studio 2015(VS2015)的下载和安装,以及安装VS2015中的C++
  16. 记一次 unicode-escape 和 utf-8 编码的互解
  17. 音乐 组件 (音频, 视频)
  18. Wordpress限制游客访问权限(免插件) 实现禁止游客访问功能
  19. 靠“吃利息”每月5000元,需要银行存多少本金?
  20. EXT前端数据传不到后台

热门文章

  1. 2013腾讯编程马拉松初赛(3月20日)
  2. java 程序中打开文件和文件夹
  3. 学成在线--0.项目概述
  4. 清华大学《操作系统》(二十二):文件系统
  5. 计算机与广播电视论文,浅谈广播电视中计算机技术的作用论文.pdf
  6. 哈希表思路图解和代码实现
  7. tornado学习笔记day06-应用安全
  8. HTML段落,换行,字符实体
  9. 【Python基础入门系列】第03天:Python 变量与数据类型
  10. 华为消息推送 有透传通道吗_求解在推送用透传消息-使用厂商通道(我用的是基座测试) 用手机测试出现了“clientId离线”问题...