// File: /workspace/github_c_makefile_star2_O3/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 0x809d1(%rip), %r15     # 0xc9fa8
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
cmpl $-0x1, 0x504(%r15)
je 0x495f6
movl %ebx, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
callq 0x2343e
movq %rax, %r14
callq 0x1fa30
testl %eax, %eax
je 0x49672
movl %eax, %ebp
callq 0x2343e
subq %r14, %rax
movq %rax, 0x380(%r15)
cmpl $-0x1, %ebp
je 0x496e0
leaq 0x51719(%rip), %rsi     # 0x9ad41
xorl %ebx, %ebx
movl $0x2, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x23223
movl $0x0, 0x500(%r15)
xorl %edi, %edi
callq 0x1fc50
movq %rax, 0x538(%r15)
movl %ebp, 0x504(%r15)
callq 0x23747
movl $0xffffffff, 0x51c(%r15) # imm = 0xFFFFFFFF
callq 0x341c0
jmp 0x495e6
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x267d6
leaq 0x51628(%rip), %rdi     # 0x9acaa
callq 0x27fbf
callq 0x1f410
leaq 0x51629(%rip), %rdx     # 0x9acbc
movq %rsp, %r14
movl $0x100, %esi            # imm = 0x100
movq %r14, %rdi
movl %eax, %ecx
xorl %eax, %eax
callq 0x1f120
movq %r14, %rdi
callq 0x4a851
movl $0x1, %ebp
testl %eax, %eax
jne 0x49709
callq 0x29cb7
testq %rax, %rax
je 0x49707
shrq $0x14, %rax
leaq 0x51609(%rip), %rsi     # 0x9acd6
xorl %ebp, %ebp
movl $0x2, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
jmp 0x49709
callq 0x1f810
movl (%rax), %edi
callq 0x1f630
leaq 0x51617(%rip), %rsi     # 0x9ad0a
movl $0x3, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x23223
jmp 0x495e6
xorl %ebp, %ebp
movl %ebp, %edi
callq 0x2349f
jmp 0x495e6