REDIS源码中一些值得学习的技术细节01
2015-11-23 20:56
585 查看
redis.c/exitFromChild函数:
分了两种情况考虑,如果是执行完RDB dump, AOF rewrite这类退出操作,为防止影响到父进程文件(父进程子进程共享文件描述符, exit函数会刷新子进程拷贝的stdio缓冲区副本),所以调用_exit()函数
但如果是在做coverage test, 为了得到正确的coverage信息,那就应该调用exit()来先执行退出处理程序,刷新stdio流缓冲区,再终止进程
关于exit()与_exit()区别,更具体的信息可参考《The Linux Programming Interface》chapter25.4 以及这个stackoverflow上的问题
http://stackoverflow.com/questions/5422831/what-is-the-difference-between-using-exit-exit-in-a-conventional-linux-fo
redis.c/activeExpireCycle函数:
此函数作为cron函数,按照源码设定,需要在0.0025秒的时间内尽可能多的清除过期键,所以函数执行的效率极为关键。
这里面,如第64行所示, 对每个数据库检查过期键,在每经过16次循环后,需要检查时间是否超时,设iteration为循环计数
我的第一想法,是if(iteration%16 == 0), 但redis却使用了 if((iteration & 0xf) == 0) 的位运算方法,为什么要用这么“装逼”的写法呢?
究其原因,是因为位运算远快于取余运算,在处理高并发场景时,函数执行的时间尤为关键,而此函数作为cron函数,需要在0.0025秒的时间内尽可能多的清除过期键,
采取这种位运算方式,自然节省了大量时间用于查找和清除过期键。这也提示我们,在对运行时间要求十分苛刻的场合,要善用位运算。
void exitFromChild(int retcode) { #ifdef COVERAGE_TEST exit(retcode); #else _exit(retcode); #endif }
分了两种情况考虑,如果是执行完RDB dump, AOF rewrite这类退出操作,为防止影响到父进程文件(父进程子进程共享文件描述符, exit函数会刷新子进程拷贝的stdio缓冲区副本),所以调用_exit()函数
但如果是在做coverage test, 为了得到正确的coverage信息,那就应该调用exit()来先执行退出处理程序,刷新stdio流缓冲区,再终止进程
关于exit()与_exit()区别,更具体的信息可参考《The Linux Programming Interface》chapter25.4 以及这个stackoverflow上的问题
http://stackoverflow.com/questions/5422831/what-is-the-difference-between-using-exit-exit-in-a-conventional-linux-fo
redis.c/activeExpireCycle函数:
void activeExpireCycle(void) { int j, iteration = 0; long long start = ustime(), timelimit; /* We can use at max REDIS_EXPIRELOOKUPS_TIME_PERC percentage of CPU time * per iteration. Since this function gets called with a frequency of * REDIS_HZ times per second, the following is the max amount of * microseconds we can spend in this function. */ timelimit = 1000000*REDIS_EXPIRELOOKUPS_TIME_PERC/REDIS_HZ/100; if (timelimit <= 0) timelimit = 1; for (j = 0; j < server.dbnum; j++) { int expired; redisDb *db = server.db+j; /* Continue to expire if at the end of the cycle more than 25% * of the keys were expired. */ do { unsigned long num = dictSize(db->expires); unsigned long slots = dictSlots(db->expires); long long now = mstime(); /* When there are less than 1% filled slots getting random * keys is expensive, so stop here waiting for better times... * The dictionary will be resized asap. */ // 过期字典里只有 %1 位置被占用,调用随机 key 的消耗比较高 // 等 key 多一点再来 if (num && slots > DICT_HT_INITIAL_SIZE && (num*100/slots < 1)) break; /* The main collection cycle. Sample random keys among keys * with an expire set, checking for expired ones. */ // 从过期字典中随机取出 key ,检查它是否过期 expired = 0; // 被删除 key 计数 if (num > REDIS_EXPIRELOOKUPS_PER_CRON) // 最多每次可查找的次数 num = REDIS_EXPIRELOOKUPS_PER_CRON; while (num--) { dictEntry *de; long long t; // 随机查找带有 TTL 的 key ,看它是否过期 // 如果数据库为空,跳出 if ((de = dictGetRandomKey(db->expires)) == NULL) break; t = dictGetSignedIntegerVal(de); if (now > t) { // 已过期 sds key = dictGetKey(de); robj *keyobj = createStringObject(key,sdslen(key)); propagateExpire(db,keyobj); dbDelete(db,keyobj); decrRefCount(keyobj); expired++; server.stat_expiredkeys++; } } /* We can't block forever here even if there are many keys to * expire. So after a given amount of milliseconds return to the * caller waiting for the other active expire cycle. */ // 每次进行 16 次循环之后,检查时间是否超过,如果超过,则退出 iteration++; if ((iteration & 0xf) == 0 && /* check once every 16 cycles. */ (ustime()-start) > timelimit) return; } while (expired > REDIS_EXPIRELOOKUPS_PER_CRON/4); } }
此函数作为cron函数,按照源码设定,需要在0.0025秒的时间内尽可能多的清除过期键,所以函数执行的效率极为关键。
这里面,如第64行所示, 对每个数据库检查过期键,在每经过16次循环后,需要检查时间是否超时,设iteration为循环计数
我的第一想法,是if(iteration%16 == 0), 但redis却使用了 if((iteration & 0xf) == 0) 的位运算方法,为什么要用这么“装逼”的写法呢?
究其原因,是因为位运算远快于取余运算,在处理高并发场景时,函数执行的时间尤为关键,而此函数作为cron函数,需要在0.0025秒的时间内尽可能多的清除过期键,
采取这种位运算方式,自然节省了大量时间用于查找和清除过期键。这也提示我们,在对运行时间要求十分苛刻的场合,要善用位运算。
相关文章推荐
- Redis内存回收:LRU算法
- linux下redis下载安装
- redis 持久化与备份策略
- 用redis做超时判断 感觉写的很有意思
- Redis 起步
- Redis介绍
- Redis集群方案(codis)
- Redis简介
- Redis学习笔记3-本机java程序调用虚拟机redis
- redis cluster 集群 安装 配置 详解
- 本地电脑安装和配置Redis操作客户端
- 用Redis bitmap统计活跃用户、留存
- 15天玩转redis —— 第七篇 同事的一次缓存操作引起对慢查询的认识
- redis的主从复制配置
- spring + redis 实现数据的缓存
- redis
- redis密码管理
- 几款开源的图形化Redis客户端管理软件推荐
- redis 详解及适用场合
- Redis