// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x80, %rsp
movl %edi, -0x4(%rbp)
movl $0x0, -0xc(%rbp)
movl $0x10, -0x10(%rbp)
callq 0x2ff30
movq %rax, -0x18(%rbp)
callq 0x454e0
cmpl $0x0, %eax
je 0x306f4
jmp 0x309f2
cmpl $0x1, -0x4(%rbp)
jne 0x3072f
cmpl $0x0, 0x14852b(%rip)    # 0x178c2c
jne 0x30708
jmp 0x309f2
movq -0x18(%rbp), %rax
movq 0x14851d(%rip), %rcx    # 0x178c30
addq $0x7d0, %rcx            # imm = 0x7D0
cmpq %rcx, %rax
jge 0x30724
jmp 0x309f2
movq -0x18(%rbp), %rax
movq %rax, 0x148501(%rip)    # 0x178c30
movl -0x10(%rbp), %eax
cmpl 0x1480b0(%rip), %eax    # 0x1787e8
jg 0x30743
cmpl $0x0, 0x1484eb(%rip)    # 0x178c2c
je 0x3074c
movl 0x14809f(%rip), %eax    # 0x1787e8
movl %eax, -0x10(%rbp)
movl $0x17d7840, %eax        # imm = 0x17D7840
cltd
idivl 0x147aa0(%rip)          # 0x1781f8
movl $0x64, %ecx
cltd
idivl %ecx
cltq
movq %rax, -0x20(%rbp)
movl $0x0, 0x1484bc(%rip)    # 0x178c2c
cmpq $0x0, -0x20(%rbp)
jg 0x3077f
movq $0x1, -0x20(%rbp)
cmpl $0x1, -0x4(%rbp)
jne 0x3078d
movq $0x3e8, -0x20(%rbp)     # imm = 0x3E8
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x309f2
movq 0x147a59(%rip), %rax    # 0x178200
movq %rax, -0x80(%rbp)
movl 0x148477(%rip), %eax    # 0x178c28
xorl %edx, %edx
divl 0x14802f(%rip)          # 0x1787e8
movq -0x80(%rbp), %rax
movl %edx, %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl 0x148458(%rip), %eax    # 0x178c28
addl $0x1, %eax
movl %eax, 0x14844f(%rip)    # 0x178c28
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x28(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
addq 0x48(%rcx), %rax
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x3080c
movq -0x30(%rbp), %rax
movq $0x0, 0x38(%rax)
jmp 0x309e2
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rax
movq -0x30(%rbp), %rcx
movq 0x8(%rcx), %rcx
addq 0x38(%rcx), %rax
movq %rax, -0x40(%rbp)
callq 0x2ff70
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x38(%rbp)
je 0x30857
cmpq $0x4, -0x40(%rbp)
jbe 0x30857
imulq $0x64, -0x38(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x40(%rbp)
cmpq $0x1, %rax
jae 0x30857
jmp 0x309e2
movl $0x0, -0x24(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
cmpq $0x14, -0x38(%rbp)
jbe 0x3087c
movq $0x14, -0x38(%rbp)
jmp 0x3087e
movq -0x38(%rbp), %rax
movq %rax, %rcx
addq $-0x1, %rcx
movq %rcx, -0x38(%rbp)
cmpq $0x0, %rax
je 0x308f9
movq -0x30(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2e6a0
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
jne 0x308ac
jmp 0x308f9
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
subq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x30(%rbp), %rdi
movq -0x60(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x305f0
cmpl $0x0, %eax
je 0x308db
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
cmpq $0x0, -0x68(%rbp)
jle 0x308f7
movq -0x68(%rbp), %rax
addq -0x50(%rbp), %rax
movq %rax, -0x50(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x3087e
cmpl $0x0, -0x54(%rbp)
je 0x30956
movq -0x50(%rbp), %rax
movslq -0x54(%rbp), %rcx
cqto
idivq %rcx
movq %rax, -0x70(%rbp)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0x30927
movq -0x70(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movl $0x32, %ecx
cqto
idivq %rcx
imulq $0x31, %rax, %rcx
movq -0x70(%rbp), %rax
movl $0x32, %esi
cqto
idivq %rsi
addq %rax, %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x38(%rax)
movl -0xc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc(%rbp)
movl -0xc(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
jne 0x309cb
callq 0x2ff30
subq -0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, 0x148271(%rip)    # 0x178bf0
je 0x309b5
movq -0x78(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
cmpq 0x14825a(%rip), %rax    # 0x178bf0
jl 0x309b5
movq -0x78(%rbp), %rax
movl $0x3e8, %ecx            # imm = 0x3E8
cqto
idivq %rcx
movq %rax, %rsi
leaq 0x106ff3(%rip), %rdi    # 0x1379a3
callq 0xa1210
movq -0x78(%rbp), %rax
cmpq -0x20(%rbp), %rax
jle 0x309c9
movl $0x1, 0x148263(%rip)    # 0x178c2c
jmp 0x309cb
cmpl $0x0, 0x14825a(%rip)    # 0x178c2c
je 0x309d6
jmp 0x309f2
jmp 0x309d8
cmpl $0x5, -0x24(%rbp)
jg 0x307d9
jmp 0x309e4
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x30794
addq $0x80, %rsp
popq %rbp
retq
nopl (%rax,%rax)