包含标签 redis 的文章

Redis源码分析 | ziplist

概述

Redis中的List是一个有序(按加入的时序排序)的数据结构,一般有序我们会采用数组或者是双向链表,其中双向链表由于有前后指针实际上会很浪费内存。3.2版本之前采用两种数据结构作为底层实现:

  • 压缩列表ziplist
  • 双向链表linkedlist

ziplist的结构

ziplist的数据结构及解释如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3sqBo573-1610531492142)(https://s3.ax1x.com/2021/01/10/s18atJ.png)]
ziplist的节点信息如下:

……

阅读全文

Redis源码解析 | VScode调试

环境

  • vscode- gcc5.4
  • ubuntu16.04 或者 ubuntu18.04
  • make

下载6.2版本. 我使用的是国内github镜像地址:github.com.cnpmjs.org,速度比较快。

$ git clone https://github.com.cnpmjs.org/redis/redis.git redis
$ git checkout -b 6.2 remotes/origin/6.2

vscode不参与编译,只充当可视化的调试工具

使用插件:

  • C/C++ 先编译redis
cd redis && make -j4

打开redis目录后直接按F5,会出现选择启动调试。选择c++(GDB/LLDB)
选择之后在左边的.vscode目录下会出现launch.json文件,将program修改为${workspaceFolder}/src/redis-server

……

阅读全文

VScode | 调试Redis源码,指针显示的问题

缘由

使用VScode的时候,断点看到指针显示的是一串地址,而不是指针指向的对象的值。上网找了一圈,没看到vscode有对应的插件来解决这个问题。vscode有对应的语法来解决这个问题。
网上几乎都在说在监视栏添加下面的表达式可以解决问题, 可以查看int arr_name[10]的值:

……

阅读全文

Redis源码解析 | sds

SDS头文件及作用

  • sds.h: sds声明- sdsalloc.h: 为sds分配内存 源码文件sds.h中有这样一行代码
typedef  char *sds;

很清晰、明了,sds其实就是char*
最新的6.2分支的代码:

struct __attribute__ ((__packed__)) sdshdr5 {<!-- -->
    unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr8 {<!-- -->
    uint8_t len; /* used */
    uint8_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr16 {<!-- -->
    uint16_t len; /* used */
    uint16_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr32 {<!-- -->
    uint32_t len; /* used */
    uint32_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};
struct __attribute__ ((__packed__)) sdshdr64 {<!-- -->
    uint64_t len; /* used */
    uint64_t alloc; /* excluding the header and null terminator */
    unsigned char flags; /* 3 lsb of type, 5 unused bits */
    char buf[];
};

__attribute__ ((__packed__))的设置是告诉编译器取消字节对齐,则结构体的大小就是按照结构体成员实际大小相加得到的。
Redis是在3.2版本(包括3.2)之后把sdshdr改为现在这样的。

……

阅读全文