// File: /workspace/github_cpp_makefile_star2_O1/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 0x2160
movq %rax, %rbx
movq %rax, %rdi
callq 0x28ec
leaq 0x1ba8(%rip), %rsi      # 0x4004
leaq 0x8(%rsp), %rdi
movl $0x30, %edx
callq 0x2180
callq 0x2050
testl %eax, %eax
js 0x2760
je 0x27b0
movl %eax, %ebp
leaq 0x4(%rsp), %rsi
movl $0x1, (%rsi)
movl %eax, %edi
movl $0x1, %edx
callq 0x20f0
leaq 0x8(%rsp), %r14
leaq 0x1b83(%rip), %r15      # 0x4026
leaq 0x4(%rsp), %r12
cmpl $0x0, 0x4(%rsp)
je 0x259f
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x21d0
movq %rbx, %rdi
callq 0x2c06
movq %rbx, %rdi
callq 0x2d80
movq %r14, %rdi
callq 0x2220
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x2dfa
movq %r13, %rdi
callq 0x2220
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x2e76
movq %r13, %rdi
callq 0x2220
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x2ef2
movq %r13, %rdi
callq 0x2220
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %r15, %rsi
callq 0x2190
movq %rbx, %rdi
callq 0x2ff8
movq %r13, %rdi
callq 0x2220
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movl %ebp, %edi
movq %r12, %rsi
movl $0x1, %edx
callq 0x20f0
jmp 0x24a8
leaq 0x4(%rsp), %rdi
callq 0x21c0
movq 0x3a18(%rip), %rdi      # 0x5fc8
leaq 0x1a71(%rip), %rsi      # 0x4028
movl $0x1e, %edx
callq 0x2190
movzbl 0x5(%rsp), %esi
movq 0x39fb(%rip), %r14      # 0x5fc8
movq %r14, %rdi
callq 0x2250
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq 0x39ab(%rip), %rdi      # 0x5fc8
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %rax, %r14
leaq 0x1a13(%rip), %rsi      # 0x4047
movl $0xf, %edx
movq %rax, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2fce
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
leaq 0x19fc(%rip), %rsi      # 0x4057
movl $0x2, %edx
movq %rax, %rdi
callq 0x2190
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %rax, %r14
leaq 0x19c1(%rip), %rsi      # 0x405a
movl $0x10, %edx
movq %rax, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2f6e
movq %r14, %rdi
callq 0x2220
movq %rax, %r14
leaq 0x19ab(%rip), %rsi      # 0x406b
movl $0x2, %edx
movq %rax, %rdi
callq 0x2190
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq %rax, %r14
leaq 0x1970(%rip), %rsi      # 0x406e
movl $0x7, %edx
movq %rax, %rdi
callq 0x2190
movq %rbx, %rdi
callq 0x2ff8
movq %r14, %rdi
callq 0x2220
movq %rax, %rbx
leaq 0x1951(%rip), %rsi      # 0x4076
movl $0x4, %edx
movq %rax, %rdi
callq 0x2190
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
xorl %ebx, %ebx
jmp 0x2878
movq 0x3889(%rip), %rbx      # 0x5ff0
leaq 0x190d(%rip), %rsi      # 0x407b
movl $0xb, %edx
movq %rbx, %rdi
callq 0x2190
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq 0x3857(%rip), %rdi      # 0x5ff0
callq 0x2030
movl $0x1, %ebx
movq %rax, %rdi
callq 0x20a0
jmp 0x2878
movq 0x8(%r14), %rdi
addq $0x8, %r14
movq %r14, %rsi
callq 0x20e0
xorl %ebx, %ebx
testl %eax, %eax
je 0x2878
movq 0x381f(%rip), %rdi      # 0x5ff0
leaq 0x1835(%rip), %rsi      # 0x400d
movl $0x18, %edx
callq 0x2190
callq 0x2040
movq %rax, %r15
movl (%rax), %esi
movq 0x37fd(%rip), %rdi      # 0x5ff0
callq 0x2250
movq %rax, %r14
leaq 0x1843(%rip), %rsi      # 0x4045
movl $0x1, %edx
movq %rax, %rdi
callq 0x2190
movl (%r15), %edi
callq 0x2070
testq %rax, %rax
je 0x2837
movq %rax, %r15
movq %rax, %rdi
callq 0x2080
movq %r14, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2190
jmp 0x2851
movq (%r14), %rax
movq -0x18(%rax), %rax
movq %r14, %rdi
addq %rax, %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x2240
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2140
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20a0
movq 0x3741(%rip), %rax      # 0x5fc0
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq 0x10(%rsp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, 0x8(%rsp,%rcx)
callq 0x2110
leaq 0x100(%rsp), %rdi
callq 0x2060
movl %ebx, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r14
movl $0x120, %esi            # imm = 0x120
movq %rbx, %rdi
callq 0x2170
jmp 0x28e3
jmp 0x28d6
jmp 0x28d6
jmp 0x28d6
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x21f0
movq %r14, %rdi
callq 0x2260
nop