// File: /workspace/github_c_makefile_star2_O2/erikdubbelboer[P]discnt/src/latency.c
// Function: createLatencyReport
sds createLatencyReport(void) {
    sds report = sdsempty();
    int advise_disk_contention = 0; /* Try to lower disk contention. */
    int advise_scheduler = 0;       /* Intrinsic latency. */
    int advise_local_disk = 0;      /* Avoid remote disks. */
    int advise_ssd = 0;             /* Use an SSD drive. */
    int advise_hz = 0;              /* Use higher HZ. */
    int advices = 0;

    /* Return ASAP if the latency engine is disabled and it looks like it
     * was never enabled so far. */
    if (dictSize(server.latency_events) == 0 &&
        server.latency_monitor_threshold == 0)
    {
        report = sdscat(report,"I'm sorry, Dave, I can't do that. Latency monitoring is disabled in this Discnt instance. You may use \"CONFIG SET latency-monitor-threshold <milliseconds>.\" in order to enable it. If we weren't in a deep space mission I'd suggest to take a look at http://redis.io/topics/latency-monitor.\n");
        return report;
    }

    /* Show all the events stats and add for each event some event-related
     * comment depending on the values. */
    dictIterator *di;
    dictEntry *de;
    int eventnum = 0;

    di = dictGetSafeIterator(server.latency_events);
    while((de = dictNext(di)) != NULL) {
        char *event = dictGetKey(de);
        struct latencyTimeSeries *ts = dictGetVal(de);
        struct latencyStats ls;

        if (ts == NULL) continue;
        eventnum++;
        if (eventnum == 1) {
            report = sdscat(report,"Dave, I have observed latency spikes in this Discnt instance. You don't mind talking about it, do you Dave?\n\n");
        }
        analyzeLatencyForEvent(event,&ls);

        report = sdscatprintf(report,
            "%d. %s: %d latency spikes (average %lums, mean deviation %lums, period %.2f sec). Worst all time event %lums.",
            eventnum, event,
            ls.samples,
            (unsigned long) ls.avg,
            (unsigned long) ls.mad,
            (double) ls.period/ls.samples,
            (unsigned long) ts->max);

        /* Potentially commands. */
        if (!strcasecmp(event,"command")) {
            advices += 2;
        }

        /* fast-command. */
        if (!strcasecmp(event,"fast-command")) {
            advise_scheduler = 1;
            advices++;
        }

        if (!strcasecmp(event,"ddb-unlink-temp-file")) {
            advise_disk_contention = 1;
            advise_local_disk = 1;
            advices += 2;
        }

        report = sdscatlen(report,"\n",1);
    }
    dictReleaseIterator(di);

    if (eventnum == 0) {
        report = sdscat(report,"Dave, no latency spike was observed during the lifetime of this Discnt instance, not in the slightest bit. I honestly think you ought to sit down calmly, take a stress pill, and think things over.\n");
    } else if (advices == 0) {
        report = sdscat(report,"\nWhile there are latency events logged, I'm not able to suggest any easy fix. Please use the Discnt community to get some help, providing this report in your help request.\n");
    } else {
        /* Add all the suggestions accumulated so far. */

        report = sdscat(report,"\nI have a few advices for you:\n\n");

        /* Intrinsic latency. */
        if (advise_scheduler) {
            report = sdscat(report,"- The system is slow to execute Discnt code paths not containing system calls. This usually means the system does not provide Discnt CPU time to run for long periods. You should try to:\n"
            "  1) Lower the system load.\n"
            "  2) Use a computer / VM just for Discnt if you are running other softawre in the same system.\n"
            "  3) Check if you have a \"noisy neighbour\" problem.\n"
            "  4) Check with 'discnt-cli --intrinsic-latency 100' what is the intrinsic latency in your system.\n"
            "  5) Check if the problem is allocator-related by recompiling Discnt with MALLOC=libc, if you are using Jemalloc. However this may create fragmentation problems.\n");
        }

        /* Disk latency. */
        if (advise_local_disk) {
            report = sdscat(report,"- It is strongly advised to use local disks for persistence. Remote disks provided by platform-as-a-service providers are known to be slow.\n");
        }

        if (advise_ssd) {
            report = sdscat(report,"- SSD disks are able to reduce fsync latency, and total time needed for snapshotting. With extremely high write load SSD disks can be a good option. However Discnt should perform reasonably with high load using normal disks. Use this advice as a last resort.\n");
        }

        if (advise_disk_contention) {
            report = sdscat(report,"- Try to lower the disk contention. This is often caused by other disk intensive processes running in the same computer (including other Redis instances).\n");
        }

        if (advise_hz && server.hz < 100) {
            report = sdscat(report,"- In order to make the Discnt internal processes more incremental, try to set the 'hz' configuration parameter to 100 using 'CONFIG SET hz 100'.\n");
        }
    }

    return report;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
callq 0x194cc
movq %rax, %rbp
leaq 0x40ebb(%rip), %rax     # 0x6a088
movq 0x678(%rax), %rdi
movq 0x28(%rdi), %rcx
addq 0x48(%rdi), %rcx
jne 0x291f4
cmpq $0x0, 0x670(%rax)
jne 0x291f4
leaq 0x285c1(%rip), %rsi     # 0x517b0
jmp 0x293a1
callq 0x14f4c
movq %rax, 0x8(%rsp)
movl $0x0, (%rsp)
xorl %r13d, %r13d
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x8(%rsp), %rdi
callq 0x14f5b
testq %rax, %rax
je 0x29334
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x29211
movl %r14d, 0x4(%rsp)
movq (%rax), %r14
testl %r12d, %r12d
jne 0x2924c
movq %rbp, %rdi
leaq 0x2868d(%rip), %rsi     # 0x518d1
callq 0x19b37
movq %rax, %rbp
incl %r12d
movq %r14, %rdi
leaq 0x18(%rsp), %rsi
callq 0x2906c
movl 0x2c(%rsp), %r8d
movl 0x1c(%rsp), %r9d
movl 0x28(%rsp), %r10d
cvtsi2sdq 0x30(%rsp), %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
movl 0x4(%rbx), %r11d
movq %rbp, %rdi
leaq 0x286b6(%rip), %rsi     # 0x5193f
movl %r12d, %edx
movq %r14, %rcx
movb $0x1, %al
pushq %r11
pushq %r10
callq 0x19db8
popq %rcx
popq %rdx
movq %rax, 0x10(%rsp)
movq %r14, %rdi
leaq 0x23daf(%rip), %rsi     # 0x4d05a
callq 0x12090
leal 0x2(%r13), %ebx
testl %eax, %eax
cmovnel %r13d, %ebx
movq %r14, %rdi
leaq 0x23d4c(%rip), %rsi     # 0x4d010
callq 0x12090
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
pushq $0x1
popq %rax
cmovel %eax, %r15d
movl %r12d, %ebp
movl %r15d, %r12d
leal (%rbx,%r13), %r15d
movq %r14, %rdi
leaq 0x286c0(%rip), %rsi     # 0x519ad
callq 0x12090
testl %eax, %eax
leal 0x2(%rbx,%r13), %r13d
movl (%rsp), %eax
pushq $0x1
popq %rcx
cmovel %ecx, %eax
movl %eax, (%rsp)
cmovnel %r15d, %r13d
movl %r12d, %r15d
movl %ebp, %r12d
movl 0x4(%rsp), %r14d
cmovel %ecx, %r14d
movq 0x10(%rsp), %rdi
leaq 0x249d4(%rip), %rsi     # 0x4dcf8
pushq $0x1
popq %rdx
callq 0x19ad8
movq %rax, %rbp
jmp 0x29211
movq 0x8(%rsp), %rdi
callq 0x1501c
testl %r12d, %r12d
je 0x29391
testl %r13d, %r13d
je 0x2939a
leaq 0x287e6(%rip), %rsi     # 0x51b35
movq %rbp, %rdi
callq 0x19b37
testl %r15d, %r15d
je 0x2936b
leaq 0x287f3(%rip), %rsi     # 0x51b56
movq %rax, %rdi
callq 0x19b37
cmpl $0x0, (%rsp)
je 0x29380
leaq 0x28a4d(%rip), %rsi     # 0x51dc5
movq %rax, %rdi
callq 0x19b37
testl %r14d, %r14d
je 0x293b7
leaq 0x28ac6(%rip), %rsi     # 0x51e52
movq %rax, %rdi
jmp 0x293a4
leaq 0x2862a(%rip), %rsi     # 0x519c2
jmp 0x293a1
leaq 0x286e7(%rip), %rsi     # 0x51a88
movq %rbp, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x19b37
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq