// File: /workspace/github_c_makefile_star2_O2/nilyang[P]redis-tdd-annotation/src/aof.c
// Function: rewriteAppendOnlyFileBackground
int rewriteAppendOnlyFileBackground(void) {
    pid_t childpid;
    long long start;

    // 已经有进程在进行 AOF 重写了
    if (server.aof_child_pid != -1) return REDIS_ERR;

    // 记录 fork 开始前的时间，计算 fork 耗时用
    start = ustime();

    if ((childpid = fork()) == 0) {
        char tmpfile[256];

        /* Child */

        // 关闭网络连接 fd
        closeListeningSockets(0);

        // 为进程设置名字，方便记认
        redisSetProcTitle("redis-aof-rewrite");

        // 创建临时文件，并进行 AOF 重写
        snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
        if (rewriteAppendOnlyFile(tmpfile) == REDIS_OK) {
            size_t private_dirty = zmalloc_get_private_dirty();

            if (private_dirty) {
                redisLog(REDIS_NOTICE,
                    "AOF rewrite: %zu MB of memory used by copy-on-write",
                    private_dirty/(1024*1024));
            }
            // 发送重写成功信号
            exitFromChild(0);
        } else {
            // 发送重写失败信号
            exitFromChild(1);
        }
    } else {
        /* Parent */
        // 记录执行 fork 所消耗的时间
        server.stat_fork_time = ustime()-start;

        if (childpid == -1) {
            redisLog(REDIS_WARNING,
                "Can't rewrite append only file in background: fork: %s",
                strerror(errno));
            return REDIS_ERR;
        }

        redisLog(REDIS_NOTICE,
            "Background append only file rewriting started by pid %d",childpid);

        // 记录 AOF 重写的信息
        server.aof_rewrite_scheduled = 0;
        server.aof_rewrite_time_start = time(NULL);
        server.aof_child_pid = childpid;

        // 关闭字典自动 rehash
        updateDictResizePolicy();

        /* We set appendseldb to -1 in order to force the next call to the
         * feedAppendOnlyFile() to issue a SELECT command, so the differences
         * accumulated by the parent into server.aof_rewrite_buf will start
         * with a SELECT statement and it will be safe to merge. 
         *
         * 将 aof_selected_db 设为 -1 ，
         * 强制让 feedAppendOnlyFile() 下次执行时引发一个 SELECT 命令，
         * 从而确保之后新添加的命令会设置到正确的数据库中
         */
        server.aof_selected_db = -1;
        replicationScriptCacheFlush();
        return REDIS_OK;
    }
    return REDIS_OK; /* unreached */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
leaq 0x80f35(%rip), %r15     # 0xc6fa8
pushq $-0x1
popq %rbx
cmpl $-0x1, 0x504(%r15)
je 0x46090
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x22820
movq %rax, %r14
callq 0x1fa30
testl %eax, %eax
je 0x46104
movl %eax, %ebp
callq 0x22820
subq %r14, %rax
movq %rax, 0x380(%r15)
cmpl $-0x1, %ebp
je 0x4614a
leaq 0x4ac43(%rip), %rsi     # 0x90d05
xorl %ebx, %ebx
pushq $0x2
popq %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x22613
andl $0x0, 0x500(%r15)
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0x538(%r15)
movl %ebp, 0x504(%r15)
callq 0x22a8d
orl $-0x1, 0x51c(%r15)
callq 0x32011
jmp 0x46080
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x256de
leaq 0x4ab5a(%rip), %rdi     # 0x90c6e
callq 0x26d17
callq 0x1f410
leaq 0x4ab5b(%rip), %rdx     # 0x90c80
movq %rsp, %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1f120
movq %r14, %rdi
callq 0x471c8
testl %eax, %eax
je 0x4616f
pushq $0x1
popq %rbp
jmp 0x46197
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x4ab71(%rip), %rsi     # 0x90cce
pushq $0x3
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
jmp 0x46080
callq 0x28599
testq %rax, %rax
je 0x46195
shrq $0x14, %rax
leaq 0x4ab16(%rip), %rsi     # 0x90c9a
xorl %ebp, %ebp
pushq $0x2
popq %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x22613
jmp 0x46197
xorl %ebp, %ebp
movl %ebp, %edi
callq 0x22855
jmp 0x46080