// File: /workspace/github_c_makefile_star2_O1/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 $0x48, %rsp
callq 0x1de3f
movq %rax, %r14
leaq 0x3eaa3(%rip), %rax     # 0x6f088
movq 0x678(%rax), %rdi
movq 0x28(%rdi), %rcx
addq 0x48(%rdi), %rcx
jne 0x3060c
cmpq $0x0, 0x670(%rax)
jne 0x3060c
leaq 0x27ac9(%rip), %rsi     # 0x580d0
jmp 0x307bc
callq 0x1926c
movq %rax, 0x18(%rsp)
movq %rax, %rdi
callq 0x1929d
testq %rax, %rax
je 0x3078b
xorl %ebp, %ebp
xorl %r15d, %r15d
movl $0x0, 0x10(%rsp)
xorl %r13d, %r13d
movl $0x0, 0xc(%rsp)
movq 0x8(%rax), %rbx
testq %rbx, %rbx
je 0x30751
movq (%rax), %r12
testl %ebp, %ebp
movl %r15d, 0x14(%rsp)
jne 0x3066a
movq %r14, %rdi
leaq 0x27b8f(%rip), %rsi     # 0x581f1
callq 0x1e73d
movq %rax, %r14
incl %ebp
movq %r12, %rdi
leaq 0x28(%rsp), %rsi
callq 0x304ad
movl 0x3c(%rsp), %r8d
movl 0x2c(%rsp), %r9d
movl 0x38(%rsp), %r10d
cvtsi2sdq 0x40(%rsp), %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
movl 0x4(%rbx), %r11d
movq %r14, %rdi
leaq 0x27bb9(%rip), %rsi     # 0x5825f
movl %ebp, %edx
movq %r12, %rcx
movb $0x1, %al
pushq %r11
pushq %r10
callq 0x1eace
addq $0x10, %rsp
movq %rax, 0x20(%rsp)
movq %r12, %rdi
leaq 0x22a21(%rip), %rsi     # 0x530ea
callq 0x12090
leal 0x2(%r13), %ebx
testl %eax, %eax
cmovnel %r13d, %ebx
movq %r12, %rdi
leaq 0x229be(%rip), %rsi     # 0x530a0
callq 0x12090
xorl %r13d, %r13d
testl %eax, %eax
sete %r13b
movl %ebp, %r14d
movl $0x1, %ebp
movl 0x10(%rsp), %eax
cmovel %ebp, %eax
movl %eax, 0x10(%rsp)
leal (%rbx,%r13), %r15d
movq %r12, %rdi
leaq 0x27bbc(%rip), %rsi     # 0x582cd
callq 0x12090
testl %eax, %eax
leal 0x2(%rbx,%r13), %r13d
movl 0xc(%rsp), %eax
cmovel %ebp, %eax
movl %eax, 0xc(%rsp)
cmovnel %r15d, %r13d
movl 0x14(%rsp), %r15d
cmovel %ebp, %r15d
movl %r14d, %ebp
movl $0x1, %edx
movq 0x20(%rsp), %rdi
leaq 0x2363f(%rip), %rsi     # 0x53d88
callq 0x1e66d
movq %rax, %r14
movq 0x18(%rsp), %rdi
callq 0x1929d
testq %rax, %rax
jne 0x3063f
testl %ebp, %ebp
sete %bl
testl %r13d, %r13d
sete %r13b
cmpl $0x0, 0x10(%rsp)
sete %r12b
cmpl $0x0, 0xc(%rsp)
sete %bpl
testl %r15d, %r15d
sete %r15b
jmp 0x30799
movb $0x1, %bpl
movb $0x1, %r13b
movb $0x1, %r12b
movb $0x1, %r15b
movb $0x1, %bl
movq 0x18(%rsp), %rdi
callq 0x1935a
testb %bl, %bl
je 0x307b0
leaq 0x27b34(%rip), %rsi     # 0x582e2
jmp 0x307bc
testb %r13b, %r13b
je 0x307d2
leaq 0x27bec(%rip), %rsi     # 0x583a8
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1e73d
leaq 0x27c7c(%rip), %rsi     # 0x58455
movq %r14, %rdi
callq 0x1e73d
testb %r12b, %r12b
jne 0x307f5
leaq 0x27c89(%rip), %rsi     # 0x58476
movq %rax, %rdi
callq 0x1e73d
testb %bpl, %bpl
jne 0x30809
leaq 0x27ee4(%rip), %rsi     # 0x586e5
movq %rax, %rdi
callq 0x1e73d
testb %r15b, %r15b
je 0x3081d
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x27f4e(%rip), %rsi     # 0x58772
movq %rax, %rdi
jmp 0x307bf