// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/redis.c
// Function: initServer
void initServer() {
    int j;

    // 设置信号处理函数
    signal(SIGHUP, SIG_IGN);
    signal(SIGPIPE, SIG_IGN);
    setupSignalHandlers();

    // 设置 syslog
    if (server.syslog_enabled) {
        openlog(server.syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT,
            server.syslog_facility);
    }

    // 初始化并创建数据结构
    server.current_client = NULL;
    server.clients = listCreate();
    server.clients_to_close = listCreate();
    server.slaves = listCreate();
    server.monitors = listCreate();
    server.slaveseldb = -1; /* Force to emit the first SELECT command. */
    server.unblocked_clients = listCreate();
    server.ready_keys = listCreate();
    server.clients_waiting_acks = listCreate();
    server.get_ack_from_slaves = 0;
    server.clients_paused = 0;

    // 创建共享对象
    createSharedObjects();
    adjustOpenFilesLimit();
    server.el = aeCreateEventLoop(server.maxclients+REDIS_EVENTLOOP_FDSET_INCR);
    server.db = zmalloc(sizeof(redisDb)*server.dbnum);

    /* Open the TCP listening socket for the user commands. */
    // 打开 TCP 监听端口，用于等待客户端的命令请求
    if (server.port != 0 &&
        listenToPort(server.port,server.ipfd,&server.ipfd_count) == REDIS_ERR)
        exit(1);

    /* Open the listening Unix domain socket. */
    // 打开 UNIX 本地端口
    if (server.unixsocket != NULL) {
        unlink(server.unixsocket); /* don't care if this fails */
        server.sofd = anetUnixServer(server.neterr,server.unixsocket,
            server.unixsocketperm, server.tcp_backlog);
        if (server.sofd == ANET_ERR) {
            redisLog(REDIS_WARNING, "Opening socket: %s", server.neterr);
            exit(1);
        }
        anetNonBlock(NULL,server.sofd);
    }

    /* Abort if there are no listening sockets at all. */
    if (server.ipfd_count == 0 && server.sofd < 0) {
        redisLog(REDIS_WARNING, "Configured to not listen anywhere, exiting.");
        exit(1);
    }

    /* Create the Redis databases, and initialize other internal state. */
    // 创建并初始化数据库结构
    for (j = 0; j < server.dbnum; j++) {
        server.db[j].dict = dictCreate(&dbDictType,NULL);
        server.db[j].expires = dictCreate(&keyptrDictType,NULL);
        server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].ready_keys = dictCreate(&setDictType,NULL);
        server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
        server.db[j].eviction_pool = evictionPoolAlloc();
        server.db[j].id = j;
        server.db[j].avg_ttl = 0;
    }

    // 创建 PUBSUB 相关结构
    server.pubsub_channels = dictCreate(&keylistDictType,NULL);
    server.pubsub_patterns = listCreate();
    listSetFreeMethod(server.pubsub_patterns,freePubsubPattern);
    listSetMatchMethod(server.pubsub_patterns,listMatchPubsubPattern);

    server.cronloops = 0;
    server.rdb_child_pid = -1;
    server.aof_child_pid = -1;
    aofRewriteBufferReset();
    server.aof_buf = sdsempty();
    server.lastsave = time(NULL); /* At startup we consider the DB saved. */
    server.lastbgsave_try = 0;    /* At startup we never tried to BGSAVE. */
    server.rdb_save_time_last = -1;
    server.rdb_save_time_start = -1;
    server.dirty = 0;
    resetServerStats();
    /* A few stats we don't want to reset: server startup time, and peak mem. */
    server.stat_starttime = time(NULL);
    server.stat_peak_memory = 0;
    server.resident_set_size = 0;
    server.lastbgsave_status = REDIS_OK;
    server.aof_last_write_status = REDIS_OK;
    server.aof_last_write_errno = 0;
    server.repl_good_slaves_count = 0;
    updateCachedTime();

    /* Create the serverCron() time event, that's our main way to process
     * background operations. */
    // 为 serverCron() 创建时间事件
    if(aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL) == AE_ERR) {
        redisPanic("Can't create the serverCron time event.");
        exit(1);
    }

    /* Create an event handler for accepting new connections in TCP and Unix
     * domain sockets. */
    // 为 TCP 连接关联连接应答（accept）处理器
    // 用于接受并应答客户端的 connect() 调用
    for (j = 0; j < server.ipfd_count; j++) {
        if (aeCreateFileEvent(server.el, server.ipfd[j], AE_READABLE,
            acceptTcpHandler,NULL) == AE_ERR)
            {
                redisPanic(
                    "Unrecoverable error creating server.ipfd file event.");
            }
    }

    // 为本地套接字关联应答处理器
    if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
        acceptUnixHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.sofd file event.");

    /* Open the AOF file if needed. */
    // 如果 AOF 持久化功能已经打开，那么打开或创建一个 AOF 文件
    if (server.aof_state == REDIS_AOF_ON) {
        server.aof_fd = open(server.aof_filename,
                               O_WRONLY|O_APPEND|O_CREAT,0644);
        if (server.aof_fd == -1) {
            redisLog(REDIS_WARNING, "Can't open the append-only file: %s",
                strerror(errno));
            exit(1);
        }
    }

    /* 32 bit instances are limited to 4GB of address space, so if there is
     * no explicit limit in the user provided configuration we set a limit
     * at 3 GB using maxmemory with 'noeviction' policy'. This avoids
     * useless crashes of the Redis instance for out of memory. */
    // 对于 32 位实例来说，默认将最大可用内存限制在 3 GB
    if (server.arch_bits == 32 && server.maxmemory == 0) {
        redisLog(REDIS_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.");
        server.maxmemory = 3072LL*(1024*1024); /* 3 GB */
        server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
    }

    // 如果服务器以 cluster 模式打开，那么初始化 cluster
    if (server.cluster_enabled) clusterInit();

    // 初始化复制功能有关的脚本缓存
    replicationScriptCacheInit();

    // 初始化脚本系统
    scriptingInit();

    // 初始化慢查询功能
    slowlogInit();

    // 初始化 BIO 系统
    bioInit();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $0x1
popq %rdi
pushq $0x1
popq %rbx
movq %rbx, %rsi
callq 0x1f770
pushq $0xd
popq %rdi
movq %rbx, %rsi
callq 0x1f770
callq 0x24a2f
cmpl $0x0, 0xa2feb(%rip)     # 0xc7580
je 0x245ac
movq 0xa2fea(%rip), %rdi     # 0xc7588
movl 0xa2fec(%rip), %edx     # 0xc7590
pushq $0x19
popq %rsi
callq 0x1f290
andq $0x0, 0xa2bc4(%rip)     # 0xc7178
callq 0x1fd8c
movq %rax, 0xa2b98(%rip)     # 0xc7158
callq 0x1fd8c
movq %rax, 0xa2b94(%rip)     # 0xc7160
callq 0x1fd8c
movq %rax, 0xa2b90(%rip)     # 0xc7168
callq 0x1fd8c
movq %rax, 0xa2b8c(%rip)     # 0xc7170
orl $-0x1, 0xa2fa9(%rip)    # 0xc7594
callq 0x1fd8c
movq %rax, 0xa30e9(%rip)     # 0xc76e0
callq 0x1fd8c
movq %rax, 0xa30e5(%rip)     # 0xc76e8
callq 0x1fd8c
movq %rax, 0xa30a9(%rip)     # 0xc76b8
andl $0x0, 0xa30aa(%rip)     # 0xc76c0
andl $0x0, 0xa2b63(%rip)     # 0xc7180
callq 0x235f4
callq 0x2424b
movl 0xa3097(%rip), %edi     # 0xc76c4
subl $-0x80, %edi
callq 0x20120
movq %rax, 0xa2994(%rip)     # 0xc6fd0
movslq 0xa2de5(%rip), %rdi     # 0xc7428
shlq $0x6, %rdi
callq 0x2828d
movq %rax, 0xa2965(%rip)     # 0xc6fb8
movl 0xa29d7(%rip), %edi     # 0xc7030
testl %edi, %edi
je 0x24679
leaq 0xa2a68(%rip), %rsi     # 0xc70cc
leaq 0xa2aa1(%rip), %rdx     # 0xc710c
callq 0x243b0
cmpl $-0x1, %eax
je 0x24a27
movq 0xa2a40(%rip), %rdi     # 0xc70c0
testq %rdi, %rdi
je 0x246c1
callq 0x1f3a0
movq 0xa2a2f(%rip), %rsi     # 0xc70c0
movl 0xa2a31(%rip), %edx     # 0xc70c8
movl 0xa2997(%rip), %ecx     # 0xc7034
leaq 0xa2aec(%rip), %rdi     # 0xc7190
callq 0x2128a
movl %eax, 0xa2a61(%rip)     # 0xc7110
cmpl $-0x1, %eax
je 0x249dc
xorl %edi, %edi
movl %eax, %esi
callq 0x209a0
cmpl $0x0, 0xa2a44(%rip)     # 0xc710c
jne 0x246d7
cmpl $0x0, 0xa2a3f(%rip)     # 0xc7110
js 0x249b1
xorl %r13d, %r13d
leaq 0xa21ef(%rip), %rbx     # 0xc68d0
leaq 0xa2248(%rip), %r14     # 0xc6930
leaq 0xa22d1(%rip), %r15     # 0xc69c0
leaq 0xa217a(%rip), %r12     # 0xc6870
xorl %ebp, %ebp
movslq 0xa2d29(%rip), %rax     # 0xc7428
cmpq %rax, %rbp
jge 0x2479d
movq %rbx, %rdi
xorl %esi, %esi
callq 0x216f0
movq 0xa289f(%rip), %rcx     # 0xc6fb8
movq %rax, (%rcx,%r13)
movq %r14, %rdi
xorl %esi, %esi
callq 0x216f0
movq 0xa288a(%rip), %rcx     # 0xc6fb8
movq %rax, 0x8(%rcx,%r13)
movq %r15, %rdi
xorl %esi, %esi
callq 0x216f0
movq 0xa2874(%rip), %rcx     # 0xc6fb8
movq %rax, 0x10(%rcx,%r13)
movq %r12, %rdi
xorl %esi, %esi
callq 0x216f0
movq 0xa285e(%rip), %rcx     # 0xc6fb8
movq %rax, 0x18(%rcx,%r13)
movq %r15, %rdi
xorl %esi, %esi
callq 0x216f0
movq 0xa2848(%rip), %rcx     # 0xc6fb8
movq %rax, 0x20(%rcx,%r13)
callq 0x24ac4
movq 0xa2837(%rip), %rcx     # 0xc6fb8
movq %rax, 0x28(%rcx,%r13)
movl %ebp, 0x30(%rcx,%r13)
andq $0x0, 0x38(%rcx,%r13)
incq %rbp
addq $0x40, %r13
jmp 0x246f8
leaq 0xa221c(%rip), %rdi     # 0xc69c0
xorl %esi, %esi
callq 0x216f0
movq %rax, 0xa2f9e(%rip)     # 0xc7750
callq 0x1fd8c
movq %rax, 0xa2f9a(%rip)     # 0xc7758
leaq 0x231d7(%rip), %rcx     # 0x4799c
movq %rcx, 0x18(%rax)
leaq 0x231e2(%rip), %rcx     # 0x479b2
movq %rcx, 0x20(%rax)
andl $0x0, 0xa2821(%rip)     # 0xc6ffc
orl $-0x1, 0xa2d36(%rip)    # 0xc7518
orl $-0x1, 0xa2cc3(%rip)    # 0xc74ac
callq 0x459b4
callq 0x271bf
movq %rax, 0xa2cbe(%rip)     # 0xc74b8
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0xa2d38(%rip)     # 0xc7540
andq $0x0, 0xa2d38(%rip)     # 0xc7548
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, 0xa2d34(%rip)    # 0xc7550
andq $0x0, 0xa2ce4(%rip)     # 0xc7508
callq 0x2450d
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0xa2ab1(%rip)     # 0xc72e8
andq $0x0, 0xa2ae1(%rip)     # 0xc7320
andq $0x0, 0xa2b29(%rip)     # 0xc7370
andl $0x0, 0xa2d12(%rip)     # 0xc7560
andq $0x0, 0xa2ca6(%rip)     # 0xc74fc
andl $0x0, 0xa2d8b(%rip)     # 0xc75e8
callq 0x22fd5
movq 0xa2767(%rip), %rdi     # 0xc6fd0
leaq -0x187e(%rip), %rdx     # 0x22ff2
pushq $0x1
popq %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2049d
cmpq $-0x1, %rax
je 0x249c4
xorl %r14d, %r14d
leaq 0xa2717(%rip), %r15     # 0xc6fa8
leaq 0x80a9(%rip), %rbx      # 0x2c941
pushq $0x1
popq %rbp
movslq 0xa286a(%rip), %rax     # 0xc710c
cmpq %rax, %r14
jge 0x248eb
movq 0xa2722(%rip), %rdi     # 0xc6fd0
movl 0x124(%r15,%r14,4), %esi
movl %ebp, %edx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x2030c
incq %r14
cmpl $-0x1, %eax
jne 0x2489b
leaq 0x667a4(%rip), %rdi     # 0x8b076
leaq 0x71db3(%rip), %rsi     # 0x9668c
movl $0x875, %edx            # imm = 0x875
callq 0x490e8
pushq $0x1
popq %rdi
callq 0x1f3b0
movl 0xa281f(%rip), %esi     # 0xc7110
testl %esi, %esi
jle 0x24917
movq 0xa26d4(%rip), %rdi     # 0xc6fd0
leaq 0x817c(%rip), %rcx      # 0x2ca7f
pushq $0x1
popq %rdx
xorl %r8d, %r8d
callq 0x2030c
cmpl $-0x1, %eax
je 0x249ef
cmpl $0x1, 0xa2b5a(%rip)     # 0xc7478
jne 0x24947
movq 0xa2b59(%rip), %rdi     # 0xc7480
movl $0x441, %esi            # imm = 0x441
movl $0x1a4, %edx            # imm = 0x1A4
xorl %eax, %eax
callq 0x1f640
movl %eax, 0xa2b82(%rip)     # 0xc74c0
cmpl $-0x1, %eax
je 0x24a07
cmpl $0x20, 0xa26aa(%rip)    # 0xc6ff8
jne 0x24981
cmpq $0x0, 0xa2d70(%rip)     # 0xc76c8
jne 0x24981
leaq 0x667a7(%rip), %rsi     # 0x8b108
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
movl $0xc0000000, %eax       # imm = 0xC0000000
movq %rax, 0xa2d51(%rip)     # 0xc76c8
movl $0x5, 0xa2d4f(%rip)     # 0xc76d0
cmpl $0x0, 0xa2ddc(%rip)     # 0xc7764
je 0x2498f
callq 0x4c2e3
callq 0x337fe
callq 0x52ef6
callq 0x51e4d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x53e50
leaq 0x66668(%rip), %rsi     # 0x8b020
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x22613
jmp 0x24a27
leaq 0x66681(%rip), %rdi     # 0x8b04c
leaq 0x71cba(%rip), %rsi     # 0x9668c
movl $0x868, %edx            # imm = 0x868
jmp 0x248de
leaq 0x6662a(%rip), %rsi     # 0x8b00d
leaq 0xa27a6(%rip), %rdx     # 0xc7190
pushq $0x3
popq %rdi
jmp 0x24a20
leaq 0x666b7(%rip), %rdi     # 0x8b0ad
leaq 0x71c8f(%rip), %rsi     # 0x9668c
movl $0x87b, %edx            # imm = 0x87B
jmp 0x248de
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x666ca(%rip), %rsi     # 0x8b0e4
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
pushq $0x1
popq %rdi
callq 0x1f2b0