// File: /workspace/github_c_makefile_star2_O3/gplessis[P]dotdeb-redis/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;

    /* When clients are paused the dataset should be static not just from the
     * POV of clients not being able to write, but also from the POV of
     * expires and evictions of keys not being performed. */
     if (clientsArePaused()) return;

    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 $0x38, %rsp
movl %edi, %ebx
leaq 0x18(%rsp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, (%r14), %rax  # imm = 0xF4240
addq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
callq 0x3e869
testl %eax, %eax
je 0x3052c
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb 0xd1dca(%rip), %al      # 0x1022fc
cmpl $0x1, %ebx
jne 0x3055a
testb $0x1, %al
je 0x3051d
movl $0x7d0, %ecx            # imm = 0x7D0
addq 0xd1db9(%rip), %rcx     # 0x102300
cmpq %rcx, 0x8(%rsp)
jl 0x3051d
movq 0x8(%rsp), %rcx
movq %rcx, 0xd1da6(%rip)     # 0x102300
movl 0xd1958(%rip), %ecx     # 0x101eb8
testb $0x1, %al
movl $0x10, %esi
cmovnel %ecx, %esi
cmpl $0x10, %ecx
cmovll %ecx, %esi
movl $0x17d7840, %eax        # imm = 0x17D7840
xorl %edx, %edx
idivl 0xd134b(%rip)           # 0x1018c8
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, 0xd1d54(%rip)     # 0x1022fc
movl $0x3e8, %eax            # imm = 0x3E8
cmovnel %ecx, %eax
movq %rax, 0x28(%rsp)
movl %esi, 0x10(%rsp)
testl %esi, %esi
jle 0x3051d
movl $0x0, 0x4(%rsp)
xorl %r15d, %r15d
movl 0xd1d26(%rip), %ecx     # 0x1022f8
movl %ecx, %eax
xorl %edx, %edx
divl 0xd18dc(%rip)           # 0x101eb8
movl %edx, %ebx
shlq $0x6, %rbx
addq 0xd12e7(%rip), %rbx     # 0x1018d0
incl %ecx
movl %ecx, 0xd1d07(%rip)     # 0x1022f8
movq %rbx, %r12
movq 0x8(%rbx), %rax
movq 0x48(%rax), %rbx
addq 0x28(%rax), %rbx
je 0x307a0
movq 0x38(%rax), %r14
addq 0x18(%rax), %r14
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, 0x18(%rsp), %rax # imm = 0xF4240
addq 0x20(%rsp), %rax
movabsq $0x20c49ba5e353f7cf, %rcx # imm = 0x20C49BA5E353F7CF
imulq %rcx
cmpq $0x5, %r14
jb 0x30648
imulq $0x64, %rbx, %rax
cmpq %rax, %r14
ja 0x307a9
movl %r15d, 0x14(%rsp)
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
movq %rdx, 0x30(%rsp)
cmpq $0x14, %rbx
movl $0x14, %eax
cmovaeq %rax, %rbx
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %ebp, %ebp
movq 0x8(%r12), %rdi
callq 0x2f141
testq %rax, %rax
je 0x306b9
movq 0x8(%rax), %r15
movq 0x30(%rsp), %rdx
subq %rdx, %r15
movq %r12, %rdi
movq %rax, %rsi
callq 0x30442
addl %eax, %ebp
xorl %eax, %eax
testq %r15, %r15
movl $0x0, %ecx
cmovleq %rcx, %r15
setg %al
addq %r15, %r13
addl %eax, %r14d
decq %rbx
jne 0x30675
testl %r14d, %r14d
movq %r12, %rbx
je 0x30713
movl %r14d, %ecx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
cmoveq %rax, %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
shrq %rcx
movq %rcx, %rax
mulq %rdi
shrq $0x4, %rdx
addq %rsi, %rdx
movq %rdx, 0x38(%rbx)
movl 0x14(%rsp), %r15d
incl %r15d
testb $0xf, %r15b
jne 0x30788
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
callq 0x26300
imulq $0xf4240, 0x18(%rsp), %rax # imm = 0xF4240
movq 0x20(%rsp), %r12
subq 0x8(%rsp), %r12
addq %rax, %r12
movq 0xd1b76(%rip), %rcx     # 0x1022c0
testq %rcx, %rcx
je 0x30781
movq %r12, %rax
movabsq $0x20c49ba5e353f7cf, %rdx # imm = 0x20C49BA5E353F7CF
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x7, %rdx
addq %rax, %rdx
cmpq %rcx, %rdx
jl 0x30781
leaq 0x8c2ba(%rip), %rdi     # 0xbca33
movq %rdx, %rsi
callq 0x7eb75
cmpq 0x28(%rsp), %r12
jg 0x307c2
cmpb $0x0, 0xd1b6d(%rip)     # 0x1022fc
jne 0x3051d
cmpl $0x5, %ebp
ja 0x305f1
jmp 0x307a9
movq $0x0, 0x38(%r12)
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
cmpl 0x10(%rsp), %eax
jne 0x305cc
jmp 0x3051d
movb $0x1, 0xd1b33(%rip)     # 0x1022fc
jmp 0x3051d