// File: /workspace/github_cpp_makefile_star2_O2/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 $0x218, %rsp            # imm = 0x218
movq %rsi, %rbx
movl %edi, %r15d
leaq 0x18(%rsp), %rdi
callq 0x21f0
pushq $0x3c
popq %rbp
pushq $0x64
popq %r14
movl $0x0, 0xc(%rsp)
leaq 0xc81(%rip), %r13       # 0x3004
movq 0x2c36(%rip), %r12      # 0x4fc0
movl %r15d, %edi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x2060
shll $0x18, %eax
cmpl $0xff000000, %eax       # imm = 0xFF000000
je 0x23fe
sarl $0x18, %eax
cmpl $0x74, %eax
jne 0x23b7
movq (%r12), %rdi
callq 0x21b0
movl %eax, %ebp
jmp 0x238a
cmpl $0x6f, %eax
je 0x23d3
cmpl $0x70, %eax
jne 0x262e
movq (%r12), %rdi
callq 0x21b0
movl %eax, %r14d
jmp 0x238a
movq (%r12), %rsi
leaq 0xc2b(%rip), %rdi       # 0x3009
xorl %eax, %eax
callq 0x2040
movq (%r12), %rsi
movb $0x1, %al
movl %eax, 0xc(%rsp)
leaq 0x18(%rsp), %rdi
pushq $0x30
popq %rdx
callq 0x2050
jmp 0x238a
movl $0x120, %edi            # imm = 0x120
callq 0x2130
movq %rax, %rbx
movq %rax, %rdi
callq 0x2660
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x10(%rsp)
imull $0x3e8, %r14d, %ebp     # imm = 0x3E8
leaq 0xbed(%rip), %r15       # 0x3018
movq 0x2b9e(%rip), %r12      # 0x4fd0
leaq 0xbee(%rip), %r13       # 0x3027
movq %rbx, %rdi
callq 0x2d46
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2559
movl %ebp, %edi
callq 0x2170
movq %rbx, %rdi
callq 0x2968
testb $0x1, 0xc(%rsp)
je 0x24fd
movq %rbx, %rdi
callq 0x2b0e
leaq 0x18(%rsp), %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2b7e
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2bf0
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2c62
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r15, %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2d46
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
callq 0x2080
movq %r12, %rdi
leaq 0xb13(%rip), %rsi       # 0x301a
callq 0x2120
movq %rax, %rdi
leaq 0xb0a(%rip), %rsi       # 0x3020
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2b0e
movq %r14, %rdi
callq 0x21d0
movq %rax, %rdi
movq %r13, %rsi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2eb0
movq %r14, %rdi
callq 0x21d0
movq %r12, %rdi
callq 0x20a0
jmp 0x2439
movq 0x2a70(%rip), %rdi      # 0x4fd0
callq 0x2080
leaq 0xac2(%rip), %rsi       # 0x302e
movq %rax, %rdi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2d1c
movq %r14, %rdi
callq 0x21d0
leaq 0xab0(%rip), %rsi       # 0x303e
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x2080
leaq 0xa9c(%rip), %rsi       # 0x3041
movq %rax, %rdi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2cd4
movq %r14, %rdi
callq 0x21d0
leaq 0xa8b(%rip), %rsi       # 0x3052
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x2080
leaq 0xa77(%rip), %rsi       # 0x3055
movq %rax, %rdi
callq 0x2120
movq %rax, %r14
movq %rbx, %rdi
callq 0x2d46
movq %r14, %rdi
callq 0x21d0
leaq 0xa5d(%rip), %rsi       # 0x305d
movq %rax, %rdi
callq 0x2120
movq %rax, %rdi
callq 0x2080
leaq 0x18(%rsp), %rdi
callq 0x2190
xorl %eax, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x2090
jmp 0x2649
movq %rax, %r14
movl $0x120, %esi            # imm = 0x120
movq %rbx, %rdi
callq 0x2140
jmp 0x264c
jmp 0x2649
movq %rax, %r14
leaq 0x18(%rsp), %rdi
callq 0x2190
movq %r14, %rdi
callq 0x2210
nop