#!/usr/bin/env python

import os
import subprocess
import sys

def run_command(cmd):
	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	ret = os.waitpid(p.pid, 0)[1]

	if ret != 0:
		raise Exception("Command %s failed, Error message: %s\n" % (cmd, p.stderr.read()))

	else:

		return p.stdout.read()

infile = open("part_b_output.txt", "w")
for cpu in range(0,8):
	infile.write("CPU #: " + str(cpu) + "\n")
	output = run_command(["numactl", "--membind", "0", "--physcpubind", str(cpu), "/u/csc469h/fall/pub/assgn1/bin/mccalpin-stream"])
	for line in output.splitlines():
		lst = line.split(' ')
		if (lst[0] == 'Function' or lst[0] == 'Copy:' or lst[0] == "Scale:" or lst[0] == "Add:" or lst[0] == "Triad:"):
			infile.write(line + "\n")
			
