// File: /workspace/github_c_makefile_star2_O2/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, %ebp
callq 0x2b88b
movq %rax, 0x8(%rsp)
callq 0x38320
testl %eax, %eax
je 0x2bc3f
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb 0xce6b7(%rip), %cl      # 0xfa2fc
cmpl $0x1, %ebp
jne 0x2bc6e
testb $0x1, %cl
je 0x2bc30
movl $0x7d0, %eax            # imm = 0x7D0
addq 0xce6a5(%rip), %rax     # 0xfa300
cmpq %rax, 0x8(%rsp)
jl 0x2bc30
movq 0x8(%rsp), %rax
movq %rax, 0xce692(%rip)     # 0xfa300
movl 0xce244(%rip), %esi     # 0xf9eb8
testl %esi, %esi
movl $0x0, %edi
cmovgl %esi, %edi
movl $0x17d7840, %eax        # imm = 0x17D7840
xorl %edx, %edx
idivl 0xcdc3d(%rip)           # 0xf98c8
pushq $0x64
popq %r8
cltd
idivl %r8d
cmpl $0x2, %eax
pushq $0x1
popq %rdx
cmovgel %eax, %edx
cmpl $0x1, %ebp
movl $0x3e8, %eax            # imm = 0x3E8
cmovnel %edx, %eax
movq %rax, 0x30(%rsp)
testb $0x1, %cl
pushq $0x10
popq %rax
cmovnel %edi, %eax
cmpl $0x10, %esi
movb $0x0, 0xce63d(%rip)     # 0xfa2fc
cmovll %edi, %eax
movl %eax, 0x1c(%rsp)
xorl %r15d, %r15d
xorl %eax, %eax
movl %eax, 0x20(%rsp)
cmpl 0x1c(%rsp), %eax
je 0x2bc30
movl 0xce619(%rip), %ecx     # 0xfa2f8
movl %ecx, %eax
xorl %edx, %edx
divl 0xce1cf(%rip)           # 0xf9eb8
movl %edx, %ebx
shlq $0x6, %rbx
addq 0xcdbda(%rip), %rbx     # 0xf98d0
incl %ecx
movl %ecx, 0xce5fa(%rip)     # 0xfa2f8
movq %rbx, 0x28(%rsp)
movq 0x8(%rbx), %rax
movq 0x48(%rax), %rbp
addq 0x28(%rax), %rbp
je 0x2be4c
movq 0x38(%rax), %r14
addq 0x18(%rax), %r14
callq 0x2b8ae
movq %rax, %r12
cmpq $0x5, %r14
jb 0x2bd38
imulq $0x64, %rbp, %rax
cmpq %rax, %r14
ja 0x2be56
movl %r15d, 0x24(%rsp)
cmpq $0x14, %rbp
pushq $0x14
popq %rax
cmovaeq %rax, %rbp
movq $0x0, 0x10(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
subq $0x1, %rbp
movq 0x28(%rsp), %rbx
jb 0x2bdaa
movq 0x8(%rbx), %rdi
callq 0x2aaf6
testq %rax, %rax
je 0x2bdaa
movq 0x8(%rax), %r15
subq %r12, %r15
movq %rbx, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x2bb9e
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
addl %eax, %r14d
jmp 0x2bd57
testl %r14d, %r14d
je 0x2bde3
movl %r14d, %ecx
movq %r13, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %rcx
movq 0x38(%rbx), %rax
testq %rax, %rax
cmoveq %rcx, %rax
cqto
pushq $0x32
popq %rdi
idivq %rdi
imulq $0x31, %rax, %rsi
movq %rcx, %rax
xorl %edx, %edx
divq %rdi
addq %rsi, %rax
movq %rax, 0x38(%rbx)
movl 0x24(%rsp), %r15d
incl %r15d
testb $0xf, %r15b
jne 0x2be32
callq 0x2b88b
movq %rax, %r12
subq 0x8(%rsp), %r12
movq 0xce4bb(%rip), %rcx     # 0xfa2c0
testq %rcx, %rcx
je 0x2be2b
movq %r12, %rax
cqto
movl $0x3e8, %esi            # imm = 0x3E8
idivq %rsi
cmpq %rcx, %rax
jl 0x2be2b
leaq 0x82b00(%rip), %rdi     # 0xae923
movq %rax, %rsi
callq 0x71e85
cmpq 0x30(%rsp), %r12
jg 0x2be61
cmpb $0x0, 0xce4c3(%rip)     # 0xfa2fc
jne 0x2bc30
cmpl $0x5, 0x10(%rsp)
ja 0x2bd03
jmp 0x2be56
movq 0x28(%rsp), %rax
andq $0x0, 0x38(%rax)
movl 0x20(%rsp), %eax
incl %eax
jmp 0x2bccb
movb $0x1, 0xce494(%rip)     # 0xfa2fc
jmp 0x2bc30