parlib: Move the provisioning of cores to a PID

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
diff --git a/tools/dev-util/perf/perf.c b/tools/dev-util/perf/perf.c
index b018f9f..6116af3 100644
--- a/tools/dev-util/perf/perf.c
+++ b/tools/dev-util/perf/perf.c
@@ -534,8 +534,6 @@
 								 const struct core_set *cores)
 {
 	int pid, status;
-	size_t max_cores = parlib_nr_total_cores();
-	struct core_set pvcores;
 
 	pid = create_child_with_stdfds(argv[0], argc, argv, environ);
 	if (pid < 0) {
@@ -544,19 +542,12 @@
 		exit(1);
 	}
 	if (cores) {
-		parlib_get_ll_core_set(&pvcores);
-		parlib_not_core_set(&pvcores);
-		parlib_and_core_sets(&pvcores, cores);
-		for (size_t i = 0; i < max_cores; i++) {
-			if (parlib_get_core(&pvcores, i)) {
-				if (sys_provision(pid, RES_CORES, i)) {
-					fprintf(stderr,
-							"Unable to provision CPU %lu to PID %d: cmd='%s'\n",
-							i, pid, argv[0]);
-					sys_proc_destroy(pid, -1);
-					exit(1);
-				}
-			}
+		if (provision_core_set(pid, cores)) {
+			fprintf(stderr,
+					"Unable to provision all cores to PID %d: cmd='%s'\n",
+					pid, argv[0]);
+			sys_proc_destroy(pid, -1);
+			exit(1);
 		}
 	}
 	sys_proc_run(pid);
diff --git a/user/parlib/include/parlib/parlib.h b/user/parlib/include/parlib/parlib.h
index e1589e4..447b4c2 100644
--- a/user/parlib/include/parlib/parlib.h
+++ b/user/parlib/include/parlib/parlib.h
@@ -10,6 +10,7 @@
 
 #include <parlib/common.h>
 #include <parlib/vcore.h>
+#include <parlib/core_set.h>
 #include <ros/memlayout.h>
 #include <ros/syscall.h>
 #include <ros/procinfo.h>
@@ -72,6 +73,7 @@
                    char *const envp[]);
 pid_t create_child_with_stdfds(const char *exe, int argc, char *const argv[],
                                char *const envp[]);
+int provision_core_set(pid_t pid, const struct core_set *cores);
 
 /* Once */
 typedef struct {
diff --git a/user/parlib/parlib.c b/user/parlib/parlib.c
index ce39cd7..d4dd71a 100644
--- a/user/parlib/parlib.c
+++ b/user/parlib/parlib.c
@@ -3,6 +3,7 @@
  * See LICENSE for details. */
 
 #include <parlib/parlib.h>
+#include <parlib/core_set.h>
 #include <parlib/ros_debug.h>
 #include <stdlib.h>
 
@@ -89,3 +90,21 @@
 	}
 	return kid;
 }
+
+/* Provisions the CG cores to PID.  Returns -1 if any of them fail. */
+int provision_core_set(pid_t pid, const struct core_set *cores)
+{
+	struct core_set pvcores;
+	size_t max_cores = parlib_nr_total_cores();
+
+	parlib_get_ll_core_set(&pvcores);
+	parlib_not_core_set(&pvcores);
+	parlib_and_core_sets(&pvcores, cores);
+	for (size_t i = 0; i < max_cores; i++) {
+		if (parlib_get_core(&pvcores, i)) {
+			if (sys_provision(pid, RES_CORES, i))
+				return -1;
+		}
+	}
+	return 0;
+}