// File: /workspace/github_cpp_makefile_star2_O2/kentcz[P]rapl-tools/AppPowerMeter.cpp
// Function: main
int main(int argc, char *argv[]) {

	Rapl *rapl = new Rapl();
	int ms_pause = 100;       // sample every 100ms
	std::ofstream outfile ("rapl.csv", std::ios::out | std::ios::trunc);

	pid_t child_pid = fork();
	if (child_pid >= 0) { //fork successful
		if (child_pid == 0) { // child process
			//printf("CHILD: child pid=%d\n", getpid());
			int exec_status = execvp(argv[1], argv+1);
			if (exec_status) {
				std::cerr << "execv failed with error " 
					<< errno << " "
					<< strerror(errno) << std::endl;
			}
		} else {              // parent process
			
			int status = 1;
			waitpid(child_pid, &status, WNOHANG);	
			while (status) {
				
				usleep(ms_pause * 1000);

				// rapl sample
				rapl->sample();
				outfile << rapl->pkg_current_power() << ","
					<< rapl->pp0_current_power() << ","
					<< rapl->pp1_current_power() << ","
					<< rapl->dram_current_power() << ","
					<< rapl->total_time() << std::endl;

				waitpid(child_pid, &status, WNOHANG);	
			}
			wait(&status); /* wait for child to exit, and store its status */
			std::cout << "PARENT: Child's exit code is: " 
				<< WEXITSTATUS(status) 
				<< std::endl;
			
			std::cout << std::endl 
				<< "\tTotal Energy:\t" << rapl->pkg_total_energy() << " J" << std::endl
				<< "\tAverage Power:\t" << rapl->pkg_average_power() << " W" << std::endl
				<< "\tTime:\t" << rapl->total_time() << " sec" << std::endl;
		}
	} else {
		std::cerr << "fork failed" << std::endl;
		return 1;
	}
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %r14
movl $0x120, %edi            # imm = 0x120
callq 0x2120
movq %rax, %rbx
movq %rax, %rdi
callq 0x263c
leaq 0xc98(%rip), %rsi       # 0x3004
leaq 0x8(%rsp), %rdi
pushq $0x30
popq %rdx
callq 0x2140
callq 0x2040
testl %eax, %eax
js 0x2564
je 0x2584
movl %eax, %ebp
leaq 0x4(%rsp), %rsi
movl $0x1, (%rsi)
pushq $0x1
popq %rdx
movl %eax, %edi
callq 0x20c0
leaq 0xc7c(%rip), %r15       # 0x3026
leaq 0x4(%rsp), %r12
pushq $0x1
popq %r13
cmpl $0x0, 0x4(%rsp)
je 0x2474
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x2180
movq %rbx, %rdi
callq 0x2944
movq %rbx, %rdi
callq 0x2aea
leaq 0x8(%rsp), %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2b5a
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2bcc
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2c3e
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2d22
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x2070
movl %ebp, %edi
movq %r12, %rsi
movl %r13d, %edx
callq 0x20c0
jmp 0x23b3
leaq 0x4(%rsp), %rdi
callq 0x2170
movq 0x2b43(%rip), %rdi      # 0x4fc8
leaq 0xb9c(%rip), %rsi       # 0x3028
callq 0x2110
movzbl 0x5(%rsp), %esi
movq %rax, %rdi
callq 0x21f0
movq %rax, %rdi
callq 0x2070
movq 0x2b1b(%rip), %rdi      # 0x4fc8
callq 0x2070
leaq 0xb8e(%rip), %rsi       # 0x3047
movq %rax, %rdi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2cf8
movq %r14, %rdi
callq 0x21d0
leaq 0xb7c(%rip), %rsi       # 0x3057
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2070
leaq 0xb68(%rip), %rsi       # 0x305a
movq %rax, %rdi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2cb0
movq %r14, %rdi
callq 0x21d0
leaq 0xb57(%rip), %rsi       # 0x306b
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2070
leaq 0xb43(%rip), %rsi       # 0x306e
movq %rax, %rdi
callq 0x2110
movq %rax, %r14
movq %rbx, %rdi
callq 0x2d22
movq %r14, %rdi
callq 0x21d0
leaq 0xb29(%rip), %rsi       # 0x3076
movq %rax, %rdi
callq 0x2110
movq %rax, %rdi
callq 0x2070
xorl %ebx, %ebx
jmp 0x25ef
movq 0x2a85(%rip), %rdi      # 0x4ff0
leaq 0xb09(%rip), %rsi       # 0x307b
callq 0x2110
movq %rax, %rdi
callq 0x2070
pushq $0x1
popq %rbx
jmp 0x25ef
movq 0x8(%r14), %rdi
addq $0x8, %r14
movq %r14, %rsi
callq 0x20b0
xorl %ebx, %ebx
testl %eax, %eax
je 0x25ef
movq 0x2a4f(%rip), %rdi      # 0x4ff0
leaq 0xa65(%rip), %rsi       # 0x300d
callq 0x2110
movq %rax, %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %esi
movq %r15, %rdi
callq 0x21f0
leaq 0xa7c(%rip), %rsi       # 0x3045
movq %rax, %rdi
callq 0x2110
movq %rax, %r15
movl (%r14), %edi
callq 0x2050
movq %r15, %rdi
movq %rax, %rsi
callq 0x2110
movq %rax, %rdi
callq 0x2070
leaq 0x8(%rsp), %rdi
callq 0x21a0
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2625
jmp 0x2625
movq %rax, %r14
movl $0x120, %esi            # imm = 0x120
movq %rbx, %rdi
callq 0x2130
jmp 0x2632
jmp 0x2625
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x21a0
movq %r14, %rdi
callq 0x2200
nop