// File: /workspace/github_c_makefile_star2_O1/zhou-yuxin[P]redis-aof-no-cow/src/server.c
// Function: activeExpireCycle
void activeExpireCycle(int type) {
    /* This function has some global state in order to continue the work
     * incrementally across calls. */
    static unsigned int current_db = 0; /* Last DB tested. */
    static int timelimit_exit = 0;      /* Time limit hit in previous call? */
    static long long last_fast_cycle = 0; /* When last fast cycle ran. */

    int j, iteration = 0;
    int dbs_per_call = CRON_DBS_PER_CALL;
    long long start = ustime(), timelimit;

    if (type == ACTIVE_EXPIRE_CYCLE_FAST) {
        /* Don't start a fast cycle if the previous cycle did not exited
         * for time limt. Also don't repeat a fast cycle for the same period
         * as the fast cycle total duration itself. */
        if (!timelimit_exit) return;
        if (start < last_fast_cycle + ACTIVE_EXPIRE_CYCLE_FAST_DURATION*2) return;
        last_fast_cycle = start;
    }

    /* We usually should test CRON_DBS_PER_CALL per iteration, with
     * two exceptions:
     *
     * 1) Don't test more DBs than we have.
     * 2) If last time we hit the time limit, we want to scan all DBs
     * in this iteration, as there is work to do in some DB and we don't want
     * expired keys to use memory for too much time. */
    if (dbs_per_call > server.dbnum || timelimit_exit)
        dbs_per_call = server.dbnum;

    /* We can use at max ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC percentage of CPU time
     * per iteration. Since this function gets called with a frequency of
     * server.hz times per second, the following is the max amount of
     * microseconds we can spend in this function. */
    timelimit = 1000000*ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC/server.hz/100;
    timelimit_exit = 0;
    if (timelimit <= 0) timelimit = 1;

    if (type == ACTIVE_EXPIRE_CYCLE_FAST)
        timelimit = ACTIVE_EXPIRE_CYCLE_FAST_DURATION; /* in microseconds. */

    for (j = 0; j < dbs_per_call; j++) {
        int expired;
        redisDb *db = server.db+(current_db % server.dbnum);

        /* Increment the DB now so we are sure if we run out of time
         * in the current DB we'll restart from the next. This allows to
         * distribute the time evenly across DBs. */
        current_db++;

        /* Continue to expire if at the end of the cycle more than 25%
         * of the keys were expired. */
        do {
            unsigned long num, slots;
            long long now, ttl_sum;
            int ttl_samples;

            /* If there is nothing to expire try next DB ASAP. */
            if ((num = dictSize(db->expires)) == 0) {
                db->avg_ttl = 0;
                break;
            }
            slots = dictSlots(db->expires);
            now = mstime();

            /* When there are less than 1% filled slots getting random
             * keys is expensive, so stop here waiting for better times...
             * The dictionary will be resized asap. */
            if (num && slots > DICT_HT_INITIAL_SIZE &&
                (num*100/slots < 1)) break;

            /* The main collection cycle. Sample random keys among keys
             * with an expire set, checking for expired ones. */
            expired = 0;
            ttl_sum = 0;
            ttl_samples = 0;

            if (num > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP)
                num = ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP;

            while (num--) {
                dictEntry *de;
                long long ttl;

                if ((de = dictGetRandomKey(db->expires)) == NULL) break;
                ttl = dictGetSignedIntegerVal(de)-now;
                if (activeExpireCycleTryExpire(db,de,now)) expired++;
                if (ttl > 0) {
                    /* We want the average TTL of keys yet not expired. */
                    ttl_sum += ttl;
                    ttl_samples++;
                }
            }

            /* Update the average TTL stats for this database. */
            if (ttl_samples) {
                long long avg_ttl = ttl_sum/ttl_samples;

                /* Do a simple running average with a few samples.
                 * We just use the current estimate with a weight of 2%
                 * and the previous estimate with a weight of 98%. */
                if (db->avg_ttl == 0) db->avg_ttl = avg_ttl;
                db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50);
            }

            /* We can't block forever here even if there are many keys to
             * expire. So after a given amount of milliseconds return to the
             * caller waiting for the other active expire cycle. */
            iteration++;
            if ((iteration & 0xf) == 0) { /* check once every 16 iterations. */
                long long elapsed = ustime()-start;

                latencyAddSampleIfNeeded("expire-cycle",elapsed/1000);
                if (elapsed > timelimit) timelimit_exit = 1;
            }
            if (timelimit_exit) return;
            /* We don't repeat the cycle if there are less than 25% of keys
             * found expired in the current DB. */
        } while (expired > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP/4);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %edi, %ebx
leaq 0x30(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, (%r14), %rax  # imm = 0xF4240
addq 0x8(%r14), %rax
movq %rax, 0x20(%rsp)
cmpl $0x1, %ebx
jne 0x33643
cmpb $0x1, 0xd3c82(%rip)     # 0x10729c
jne 0x33905
movl $0x7d0, %eax            # imm = 0x7D0
addq 0xd3c74(%rip), %rax     # 0x1072a0
cmpq %rax, 0x20(%rsp)
jl 0x33905
movq 0x20(%rsp), %rax
movq %rax, 0xd3c5d(%rip)     # 0x1072a0
movl 0xd380f(%rip), %eax     # 0x106e58
testb $0x1, 0xd3c4c(%rip)     # 0x10729c
movl $0x10, %esi
cmovnel %eax, %esi
cmpl $0x10, %eax
cmovll %eax, %esi
movl $0x17d7840, %eax        # imm = 0x17D7840
xorl %edx, %edx
idivl 0xd320d(%rip)           # 0x106878
cltq
imulq $0x51eb851f, %rax, %rax # imm = 0x51EB851F
movq %rax, %rcx
shrq $0x3f, %rcx
sarq $0x25, %rax
addl %ecx, %eax
cmpl $0x2, %eax
movl $0x1, %ecx
cmovgel %eax, %ecx
cmpl $0x1, %ebx
movb $0x0, 0xd3c06(%rip)     # 0x10729c
movl $0x3e8, %eax            # imm = 0x3E8
cmovnel %ecx, %eax
movq %rax, 0x40(%rsp)
movl %esi, 0x2c(%rsp)
testl %esi, %esi
jle 0x33905
leaq 0x30(%rsp), %r15
movabsq $0x20c49ba5e353f7cf, %r13 # imm = 0x20C49BA5E353F7CF
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movl 0xd3bc5(%rip), %ecx     # 0x107298
movl %ecx, %eax
xorl %edx, %edx
divl 0xd377b(%rip)           # 0x106e58
shlq $0x6, %rdx
addq 0xd3198(%rip), %rdx     # 0x106880
movq %rdx, 0x18(%rsp)
incl %ecx
movl %ecx, 0xd3ba3(%rip)     # 0x107298
movl %ebx, 0xc(%rsp)
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x48(%rax), %r14
addq 0x28(%rax), %r14
je 0x3374f
movq 0x38(%rax), %rbx
addq 0x18(%rax), %rbx
movq %r15, %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, 0x30(%rsp), %rax # imm = 0xF4240
addq 0x38(%rsp), %rax
imulq %r13
movq %rdx, %r12
cmpq $0x5, %rbx
jb 0x33761
imulq $0x64, %r14, %rcx
movl $0x5, %eax
cmpq %rcx, %rbx
jbe 0x33761
movl 0xc(%rsp), %ebx
jmp 0x338d9
movq $0x0, 0x38(%rcx)
movl $0x5, %eax
jmp 0x338d9
movq %r12, %rax
shrq $0x3f, %rax
sarq $0x7, %r12
addq %rax, %r12
cmpq $0x14, %r14
movl $0x14, %eax
cmovaeq %rax, %r14
incq %r14
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %ebx, %ebx
movq 0x18(%rsp), %r15
decq %r14
je 0x337eb
movq 0x8(%r15), %rdi
callq 0x32237
movq %rax, %rbp
testq %rax, %rax
je 0x337e6
movq 0x8(%rbp), %r15
subq %r12, %r15
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x3353c
movq 0x10(%rsp), %rcx
addl %eax, %ecx
movq %rcx, 0x10(%rsp)
xorl %eax, %eax
testq %r15, %r15
setg %al
movl $0x0, %ecx
cmovleq %rcx, %r15
addq %r15, %r13
movq 0x18(%rsp), %r15
addl %eax, %ebx
testq %rbp, %rbp
jne 0x33792
testl %ebx, %ebx
je 0x3384f
movslq %ebx, %rcx
movq %r13, %rax
cqto
idivq %rcx
movq %rax, %rcx
cmpq $0x0, 0x38(%r15)
jne 0x33808
movq %rcx, 0x38(%r15)
movq 0x38(%r15), %rsi
movq %rsi, %rax
movabsq $-0x5c28f5c28f5c28f5, %rdi # imm = 0xA3D70A3D70A3D70B
imulq %rdi
addq %rsi, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x5, %rdx
addq %rax, %rdx
imulq $0x31, %rdx, %rsi
movq %rcx, %rax
imulq %rdi
addq %rcx, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x5, %rdx
addq %rax, %rdx
addq %rsi, %rdx
movq %rdx, 0x38(%r15)
movl 0x8(%rsp), %eax
incl %eax
movl %eax, 0x8(%rsp)
testb $0xf, %al
leaq 0x30(%rsp), %r15
movabsq $0x20c49ba5e353f7cf, %r13 # imm = 0x20C49BA5E353F7CF
movl 0xc(%rsp), %ebx
jne 0x338d2
movq %r15, %rdi
xorl %esi, %esi
callq 0x26310
imulq $0xf4240, 0x30(%rsp), %rax # imm = 0xF4240
movq 0x38(%rsp), %r12
subq 0x20(%rsp), %r12
addq %rax, %r12
movq 0xd39c9(%rip), %rcx     # 0x107260
testq %rcx, %rcx
je 0x338c4
movq %r12, %rax
imulq %r13
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jl 0x338c4
leaq 0x8c105(%rip), %rdi     # 0xbf9c1
movq %rdx, %rsi
callq 0x825dd
cmpq 0x40(%rsp), %r12
jle 0x338d2
movb $0x1, 0xd39ca(%rip)     # 0x10729c
movzbl 0xd39c3(%rip), %eax     # 0x10729c
testl %eax, %eax
jne 0x338ea
cmpl $0x5, 0x10(%rsp)
jg 0x336f9
jmp 0x338ef
cmpl $0x5, %eax
jne 0x338f3
xorl %eax, %eax
jmp 0x338f5
movb $0x1, %al
testb %al, %al
jne 0x33905
incl %ebx
cmpl 0x2c(%rsp), %ebx
jne 0x336cd
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq