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

	// Default settings
	int runtime = 60;         // run for 60 seconds	
	int ms_pause = 100;       // sample every 100ms
	bool use_outfile = false; // no output file
	ofstream outfile;

	/**
	 *  Read Commandline Parameters
	 *
	 *  -p -- pause duration (in milliseconds)
	 *  -o -- write output to a file
	 *  -t -- run time (in sec) 
	 *
	 *  Example: ./a.out -p 500 -o outfile.csv
	 *
	 **/
	char input_char;
	while ((input_char = getopt(argc, argv, "p:o:")) != -1) {
		switch (input_char) {
		case 'p':
			ms_pause = atoi(optarg);
			break;
		case 't':
			runtime = atoi(optarg);
			break;
		case 'o':
			printf("ouput file:%s\n",optarg);
			use_outfile = true;
			outfile.open(optarg, ios::out | ios::trunc);
			break;
		default:
			abort();
		}
	}

	Rapl *rapl = new Rapl();
	while (rapl->total_time() < runtime) {
		usleep(1000 * ms_pause);
		rapl->sample();

		// Write sample to outfile
		if (use_outfile) {
			outfile << rapl->pkg_current_power() << ","
					<< rapl->pp0_current_power() << ","
					<< rapl->pp1_current_power() << ","
					<< rapl->dram_current_power() << ","
					<< rapl->total_time() << endl;
		}

		// Write sample to terminal
		cout << "\33[2K\r" // clear line
				<< "power=" << rapl->pkg_current_power()
				<< "\tTime=" << rapl->current_time();
		cout.flush();
	}

	// Print totals
	cout << endl 
		<< "\tTotal Energy:\t" << rapl->pkg_total_energy() << " J" << endl
		<< "\tAverage Power:\t" << rapl->pkg_average_power() << " W" << endl
		<< "\tTime:\t" << rapl->total_time() << " sec" << endl;

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x208, %rsp            # imm = 0x208
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x8(%rsp), %rdi
callq 0x2220
leaq 0x1bdd(%rip), %rdx      # 0x4010
movl %ebp, %edi
movq %rbx, %rsi
callq 0x2070
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x2461
movsd 0x1bb9(%rip), %xmm0     # 0x4008
movsd %xmm0, (%rsp)
movl $0x186a0, %ebp          # imm = 0x186A0
xorl %r13d, %r13d
jmp 0x2501
movl $0x64, %r14d
movl $0x3c, (%rsp)
xorl %r13d, %r13d
movq 0x3b40(%rip), %r12      # 0x5fb8
leaq 0x1b91(%rip), %r15      # 0x4010
sarl $0x18, %eax
cmpl $0x6f, %eax
je 0x24b1
cmpl $0x74, %eax
je 0x24a3
cmpl $0x70, %eax
jne 0x2828
movq (%r12), %rdi
callq 0x21e0
movl %eax, %r14d
jmp 0x24d9
movq (%r12), %rdi
callq 0x21e0
movl %eax, (%rsp)
jmp 0x24d9
movq (%r12), %rsi
leaq 0x1b59(%rip), %rdi      # 0x4015
xorl %eax, %eax
callq 0x2050
movq (%r12), %rsi
movb $0x1, %r13b
leaq 0x8(%rsp), %rdi
movl $0x30, %edx
callq 0x2060
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x2070
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
jne 0x247f
cvtsi2sdl (%rsp), %xmm0
movsd %xmm0, (%rsp)
imull $0x3e8, %r14d, %ebp     # imm = 0x3E8
movl $0x120, %edi            # imm = 0x120
callq 0x2150
movq %rax, %rbx
movq %rax, %rdi
callq 0x2858
leaq 0x1b07(%rip), %r12      # 0x4024
movq 0x3aac(%rip), %r14      # 0x5fd0
movq %rbx, %rdi
callq 0x2f96
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2688
movl %ebp, %edi
callq 0x21a0
movq %rbx, %rdi
callq 0x2b76
testb $0x1, %r13b
je 0x261c
movq %rbx, %rdi
callq 0x2d1e
leaq 0x8(%rsp), %rdi
callq 0x2200
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2d98
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2e14
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2e90
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x2170
movq %rbx, %rdi
callq 0x2f96
movq %r15, %rdi
callq 0x2200
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movl $0x5, %edx
movq %r14, %rdi
leaq 0x19fb(%rip), %rsi      # 0x4026
callq 0x2170
movl $0x6, %edx
movq %r14, %rdi
leaq 0x19ed(%rip), %rsi      # 0x402c
callq 0x2170
movq %rbx, %rdi
callq 0x2d1e
movq %r14, %rdi
callq 0x2200
movq %rax, %r15
movl $0x6, %edx
movq %rax, %rdi
leaq 0x19cd(%rip), %rsi      # 0x4033
callq 0x2170
movq %rbx, %rdi
callq 0x3166
movq %r15, %rdi
callq 0x2200
movq %r14, %rdi
callq 0x20b0
jmp 0x2524
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq 0x392a(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq %rax, %r14
leaq 0x197d(%rip), %rsi      # 0x403a
movl $0xf, %edx
movq %rax, %rdi
callq 0x2170
movq %rbx, %rdi
callq 0x2f6c
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
leaq 0x1966(%rip), %rsi      # 0x404a
movl $0x2, %edx
movq %rax, %rdi
callq 0x2170
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq %rax, %r14
leaq 0x192b(%rip), %rsi      # 0x404d
movl $0x10, %edx
movq %rax, %rdi
callq 0x2170
movq %rbx, %rdi
callq 0x2f0c
movq %r14, %rdi
callq 0x2200
movq %rax, %r14
leaq 0x1915(%rip), %rsi      # 0x405e
movl $0x2, %edx
movq %rax, %rdi
callq 0x2170
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq %rax, %r14
leaq 0x18da(%rip), %rsi      # 0x4061
movl $0x7, %edx
movq %rax, %rdi
callq 0x2170
movq %rbx, %rdi
callq 0x2f96
movq %r14, %rdi
callq 0x2200
movq %rax, %rbx
leaq 0x18bb(%rip), %rsi      # 0x4069
movl $0x4, %edx
movq %rax, %rdi
callq 0x2170
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2130
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x20b0
movq 0x37df(%rip), %rax      # 0x5fc8
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 0x2100
leaq 0x100(%rsp), %rdi
callq 0x2080
xorl %eax, %eax
addq $0x208, %rsp            # imm = 0x208
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x20a0
movq %rax, %r14
movl $0x120, %esi            # imm = 0x120
movq %rbx, %rdi
callq 0x2160
jmp 0x2846
jmp 0x2843
jmp 0x2843
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x21c0
movq %r14, %rdi
callq 0x2240