// File: lega911[P]ijson/src/service.cpp
// Function: Service::_start()
void Service::_start() {
    int threads = server->threads;
    bool log = server->log & 64;
    u64 *used = new u64[threads]();
    int *cpu = new int[threads]();
    u64 prev_time = get_ntime();
    i16 clean_tasks = 2;

    while(true) {
        usleep(500'000);  // 500ms
        _free_memory();

        if(--clean_tasks <= 0) {
            clean_tasks = 2;
            _clean_dead_tasks();
        }

        #ifndef _NOBALANCER
        if(threads > 1) {
            // balance
            u64 now = get_ntime();
            u64 frame = now - prev_time;
            prev_time = now;

            struct timespec thread_time;
            clockid_t threadClockId;

            if(log) std::cout << "cpu: ";
            u64 total = 0;
            for(int i=0; i<threads; i++) {
                auto id = server->loops[i]->get_id();
                pthread_getcpuclockid(id, &threadClockId);
                clock_gettime(threadClockId, &thread_time);
                u64 usage = thread_time.tv_sec * 1'000'000'000 + thread_time.tv_nsec;
                cpu[i] = (usage - used[i]) * 100 / frame;
                used[i] = usage;

                if(log) std::cout << cpu[i] << "% ";
            }
            if(log) std::cout << std::endl;

            int active = -1;
            int min = 0;
            for(int i=0;i<threads;i++) {
                if(cpu[i] < cpu[min]) min = i;
                if(cpu[i] > 80) continue;
                active = i;
                break;
            }

            if(active == -1) active = min;
            if(server->active_loop != active) {
                server->active_loop = active;
                if(log) std::cout << "Active loop: " << active << std::endl;
            }
        }
        #endif
    };

    delete[] used;
    delete[] cpu;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq 0x8(%rax), %rcx
movl 0x28(%rcx), %ecx
movl %ecx, -0xc(%rbp)
movq 0x8(%rax), %rax
movl 0x20(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0xd(%rbp)
movslq -0xc(%rbp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0xa8(%rbp)
callq 0x19030
movq -0xa8(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0xa0(%rbp)
xorl %esi, %esi
callq 0x191b0
movq -0xa0(%rbp), %rax
movq %rax, -0x18(%rbp)
movslq -0xc(%rbp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
movq %rdi, -0x98(%rbp)
callq 0x19030
movq -0x98(%rbp), %rdx
movq %rax, %rdi
movq %rdi, -0x90(%rbp)
xorl %esi, %esi
callq 0x191b0
movq -0x90(%rbp), %rax
movq %rax, -0x20(%rbp)
callq 0x35770
movq %rax, -0x28(%rbp)
movw $0x2, -0x2a(%rbp)
movl $0x7a120, %edi          # imm = 0x7A120
callq 0x193d0
movq -0xb0(%rbp), %rdi
callq 0x357a0
movw -0x2a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x2a(%rbp)
cwtl
cmpl $0x0, %eax
jg 0x35550
movq -0xb0(%rbp), %rdi
movw $0x2, -0x2a(%rbp)
callq 0x358c0
cmpl $0x1, -0xc(%rbp)
jle 0x35762
callq 0x35770
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
subq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x28(%rbp)
testb $0x1, -0xd(%rbp)
je 0x35590
movq 0x11a4c(%rip), %rdi     # 0x46fd0
leaq 0x41ff(%rip), %rsi      # 0x3978a
callq 0x192e0
movq $0x0, -0x60(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x3566c
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20050(%rax), %rax
movslq -0x64(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
callq 0x35fa0
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
leaq -0x54(%rbp), %rsi
callq 0x19110
movl -0x54(%rbp), %edi
leaq -0x50(%rbp), %rsi
callq 0x192b0
imulq $0x3b9aca00, -0x50(%rbp), %rax # imm = 0x3B9ACA00
addq -0x48(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
movq -0x18(%rbp), %rcx
movslq -0x64(%rbp), %rdx
subq (%rcx,%rdx,8), %rax
imulq $0x64, %rax, %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x40(%rbp)
movl %eax, %edx
movq -0x20(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movq -0x78(%rbp), %rdx
movq -0x18(%rbp), %rax
movslq -0x64(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
testb $0x1, -0xd(%rbp)
je 0x3565c
movq -0x20(%rbp), %rax
movslq -0x64(%rbp), %rcx
movl (%rax,%rcx,4), %esi
movq 0x11988(%rip), %rdi     # 0x46fd0
callq 0x19490
movq %rax, %rdi
leaq 0x4139(%rip), %rsi      # 0x39790
callq 0x192e0
jmp 0x3565e
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x3559f
testb $0x1, -0xd(%rbp)
je 0x35685
movq 0x11957(%rip), %rdi     # 0x46fd0
movq 0x11930(%rip), %rsi     # 0x46fb0
callq 0x19320
movl $0xffffffff, -0x7c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x356fb
movq -0x20(%rbp), %rax
movslq -0x84(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movq -0x20(%rbp), %rcx
movslq -0x80(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x356cc
movl -0x84(%rbp), %eax
movl %eax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movslq -0x84(%rbp), %rcx
cmpl $0x50, (%rax,%rcx,4)
jle 0x356df
jmp 0x356ea
movl -0x84(%rbp), %eax
movl %eax, -0x7c(%rbp)
jmp 0x356fb
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0x3569d
cmpl $-0x1, -0x7c(%rbp)
jne 0x35707
movl -0x80(%rbp), %eax
movl %eax, -0x7c(%rbp)
movq -0xb0(%rbp), %rax
movq 0x8(%rax), %rax
movl 0x4(%rax), %eax
cmpl -0x7c(%rbp), %eax
je 0x35760
movq -0xb0(%rbp), %rax
movl -0x7c(%rbp), %ecx
movq 0x8(%rax), %rax
movl %ecx, 0x4(%rax)
testb $0x1, -0xd(%rbp)
je 0x3575e
movq 0x11898(%rip), %rdi     # 0x46fd0
leaq 0x4054(%rip), %rsi      # 0x39793
callq 0x192e0
movq %rax, %rdi
movl -0x7c(%rbp), %esi
callq 0x19490
movq %rax, %rdi
movq 0x11857(%rip), %rsi     # 0x46fb0
callq 0x19320
jmp 0x35760
jmp 0x35762
jmp 0x35516
nopw (%rax,%rax)