// File: /workspace/github_c_makefile_star2_O3/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 0x1db82
movq %rax, %r13
leaq 0x3f02e(%rip), %rax     # 0x6f088
movq 0x678(%rax), %rdi
movq 0x28(%rdi), %rcx
addq 0x48(%rdi), %rcx
jne 0x30081
cmpq $0x0, 0x670(%rax)
jne 0x30081
leaq 0x28044(%rip), %rsi     # 0x580c0
jmp 0x30243
callq 0x18ec7
movq %rax, %r12
movq %rax, %rdi
callq 0x18ef7
testq %rax, %rax
je 0x30234
xorl %ebp, %ebp
xorl %ebx, %ebx
movl $0x0, 0xc(%rsp)
xorl %r15d, %r15d
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
movq 0x8(%rax), %r12
testq %r12, %r12
je 0x301c4
movl %r14d, 0x10(%rsp)
movq (%rax), %r14
testl %ebp, %ebp
movl %ebx, 0x14(%rsp)
jne 0x300e0
movq %r13, %rdi
leaq 0x28109(%rip), %rsi     # 0x581e1
callq 0x1e495
movq %rax, %r13
incl %ebp
movq %r14, %rdi
leaq 0x28(%rsp), %rsi
callq 0x2fefd
movl 0x3c(%rsp), %r8d
movl 0x2c(%rsp), %r9d
movl 0x38(%rsp), %r10d
cvtsi2sdq 0x40(%rsp), %xmm0
cvtsi2sd %r8, %xmm1
divsd %xmm1, %xmm0
movl 0x4(%r12), %r11d
movq %r13, %rdi
leaq 0x28132(%rip), %rsi     # 0x5824f
movl %ebp, %edx
movq %r14, %rcx
movb $0x1, %al
pushq %r11
pushq %r10
callq 0x1e826
addq $0x10, %rsp
movq %rax, 0x20(%rsp)
movq %r14, %rdi
leaq 0x22ffa(%rip), %rsi     # 0x5313a
callq 0x12090
leal 0x2(%r15), %r12d
testl %eax, %eax
cmovnel %r15d, %r12d
movq %r14, %rdi
leaq 0x22f97(%rip), %rsi     # 0x530f0
callq 0x12090
xorl %r15d, %r15d
testl %eax, %eax
sete %r15b
movl 0xc(%rsp), %eax
movl %ebp, %r13d
movl $0x1, %ebp
cmovel %ebp, %eax
movl %eax, 0xc(%rsp)
leal (%r12,%r15), %ebx
movq %r14, %rdi
leaq 0x28135(%rip), %rsi     # 0x582bd
callq 0x12090
testl %eax, %eax
leal 0x2(%r12,%r15), %r15d
movl 0x10(%rsp), %r14d
cmovel %ebp, %r14d
cmovnel %ebx, %r15d
movl 0x14(%rsp), %ebx
cmovel %ebp, %ebx
movl %r13d, %ebp
movl $0x1, %edx
movq 0x20(%rsp), %rdi
leaq 0x23c1c(%rip), %rsi     # 0x53dd8
callq 0x1e3ca
movq %rax, %r13
movq 0x18(%rsp), %r12
movq %r12, %rdi
callq 0x18ef7
testq %rax, %rax
jne 0x300b1
movq %r12, %rdi
callq 0x18fbf
testl %ebp, %ebp
je 0x3023c
testl %r15d, %r15d
je 0x30259
leaq 0x28253(%rip), %rsi     # 0x58445
movq %r13, %rdi
callq 0x1e495
cmpl $0x0, 0xc(%rsp)
je 0x30210
leaq 0x2825e(%rip), %rsi     # 0x58466
movq %rax, %rdi
callq 0x1e495
testl %r14d, %r14d
je 0x30224
leaq 0x284b9(%rip), %rsi     # 0x586d5
movq %rax, %rdi
callq 0x1e495
testl %ebx, %ebx
je 0x30262
leaq 0x28533(%rip), %rsi     # 0x58762
movq %rax, %rdi
jmp 0x30246
movq %r12, %rdi
callq 0x18fbf
leaq 0x2808f(%rip), %rsi     # 0x582d2
movq %r13, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1e495
leaq 0x28138(%rip), %rsi     # 0x58398
jmp 0x30243
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq