// File: /workspace/github_c_makefile_star2_O1/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 0x804b3(%rip), %r15     # 0xc9fa8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, 0x504(%r15)
je 0x49b14
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x23280
movq %rax, %r14
callq 0x1fa30
testl %eax, %eax
je 0x49b90
movl %eax, %ebp
callq 0x23280
subq %r14, %rax
movq %rax, 0x380(%r15)
cmpl $-0x1, %ebp
je 0x49bfe
leaq 0x511b3(%rip), %rsi     # 0x9acf9
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x23064
movl $0x0, 0x500(%r15)
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0x538(%r15)
movl %ebp, 0x504(%r15)
callq 0x23589
movl $0xffffffff, 0x51c(%r15) # imm = 0xFFFFFFFF
callq 0x33d87
jmp 0x49b04
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x265b5
leaq 0x510c2(%rip), %rdi     # 0x9ac62
callq 0x27d28
callq 0x1f410
leaq 0x510c3(%rip), %rdx     # 0x9ac74
movq %rsp, %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1f120
movq %r14, %rdi
callq 0x4adf2
movl $0x1, %ebp
testl %eax, %eax
jne 0x49c27
callq 0x29a13
testq %rax, %rax
je 0x49c25
shrq $0x14, %rax
leaq 0x510a3(%rip), %rsi     # 0x9ac8e
xorl %ebp, %ebp
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
jmp 0x49c27
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x510b1(%rip), %rsi     # 0x9acc2
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23064
jmp 0x49b04
xorl %ebp, %ebp
movl %ebp, %edi
callq 0x232e1
jmp 0x49b04