Blame SOURCES/oprofile-rhbz1121205.patch

c532ac
commit ebde58121d34e30f57ab173bf425244ce0712d48
c532ac
Author: Maynard Johnson <maynardj@us.ibm.com>
c532ac
Date:   Wed Oct 9 13:12:21 2013 -0500
c532ac
c532ac
    Converge operf and ocount utility functions
c532ac
    
c532ac
    When the ocount tool was developed, a number of utility
c532ac
    functions were needed that were very similar to operf utility
c532ac
    functions, with just minor changes.  The decision was made at
c532ac
    the time to copy these functions into ocount and change them
c532ac
    as needed.  To avoid dual maintenance on very similar functions,
c532ac
    we should converge the two tools to use one common set of utility
c532ac
    functions. The main reason for not doing so in the first place
c532ac
    was to make it easier to review ocount patches and not have to
c532ac
    look at operf changes at the same time.
c532ac
    
c532ac
    Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
c532ac
c532ac
diff --git a/Makefile.am b/Makefile.am
c532ac
index 293114b..2fe8d2f 100644
c532ac
--- a/Makefile.am
c532ac
+++ b/Makefile.am
c532ac
@@ -19,9 +19,9 @@ SUBDIRS = \
c532ac
 	events \
c532ac
 	doc \
c532ac
 	gui \
c532ac
+	libpe_utils \
c532ac
 	libperf_events \
c532ac
 	pe_profiling \
c532ac
-	libpe_utils \
c532ac
 	pe_counting \
c532ac
 	agents
c532ac
 #### ATTENTION ####
c532ac
diff --git a/libpe_utils/op_pe_utils.cpp b/libpe_utils/op_pe_utils.cpp
c532ac
index dc9459e..b85d175 100644
c532ac
--- a/libpe_utils/op_pe_utils.cpp
c532ac
+++ b/libpe_utils/op_pe_utils.cpp
c532ac
@@ -52,7 +52,9 @@ extern op_cpu cpu_type;
c532ac
 
c532ac
 using namespace std;
c532ac
 
c532ac
-static int _op_get_next_online_cpu(DIR * dir, struct dirent *entry)
c532ac
+// Global functions
c532ac
+
c532ac
+int op_pe_utils::op_get_next_online_cpu(DIR * dir, struct dirent *entry)
c532ac
 {
c532ac
 #define OFFLINE 0x30
c532ac
 	unsigned int cpu_num;
c532ac
@@ -86,8 +88,6 @@ static int _op_get_next_online_cpu(DIR * dir, struct dirent *entry)
c532ac
 		return cpu_num;
c532ac
 }
c532ac
 
c532ac
-// Global functions
c532ac
-
c532ac
 int op_pe_utils::op_get_sys_value(const char * filename)
c532ac
 {
c532ac
 	char str[10];
c532ac
@@ -148,7 +148,7 @@ int op_pe_utils::op_get_cpu_for_perf_events_cap(void)
c532ac
 			goto error;
c532ac
 		} else {
c532ac
 			struct dirent *entry = NULL;
c532ac
-			retval = _op_get_next_online_cpu(dir, entry);
c532ac
+			retval = op_get_next_online_cpu(dir, entry);
c532ac
 			closedir(dir);
c532ac
 		}
c532ac
 	} else {
c532ac
@@ -310,40 +310,6 @@ int op_pe_utils::op_validate_app_name(char ** app, char ** save_appname)
c532ac
 
c532ac
 	out: return rc;
c532ac
 }
c532ac
-static int _get_next_online_cpu(DIR * dir, struct dirent *entry)
c532ac
-{
c532ac
-#define OFFLINE 0x30
c532ac
-	unsigned int cpu_num;
c532ac
-	char cpu_online_pathname[40];
c532ac
-	int res;
c532ac
-	FILE * online;
c532ac
-	again:
c532ac
-	do {
c532ac
-		entry = readdir(dir);
c532ac
-		if (!entry)
c532ac
-			return -1;
c532ac
-	} while (entry->d_type != DT_DIR);
c532ac
-
c532ac
-	res = sscanf(entry->d_name, "cpu%u", &cpu_num);
c532ac
-	if (res <= 0)
c532ac
-		goto again;
c532ac
-
c532ac
-	errno = 0;
c532ac
-	snprintf(cpu_online_pathname, 40, "/sys/devices/system/cpu/cpu%u/online", cpu_num);
c532ac
-	if ((online = fopen(cpu_online_pathname, "r")) == NULL) {
c532ac
-		cerr << "Unable to open " << cpu_online_pathname << endl;
c532ac
-		if (errno)
c532ac
-			cerr << strerror(errno) << endl;
c532ac
-		return -1;
c532ac
-	}
c532ac
-	res = fgetc(online);
c532ac
-	fclose(online);
c532ac
-	if (res == OFFLINE)
c532ac
-		goto again;
c532ac
-	else
c532ac
-		return cpu_num;
c532ac
-}
c532ac
-
c532ac
 
c532ac
 set<int> op_pe_utils::op_get_available_cpus(int max_num_cpus)
c532ac
 {
c532ac
@@ -392,7 +358,7 @@ set<int> op_pe_utils::op_get_available_cpus(int max_num_cpus)
c532ac
 		if (all_cpus_avail) {
c532ac
 			available_cpus.insert(cpu);
c532ac
 		} else {
c532ac
-			real_cpu = _get_next_online_cpu(dir, entry);
c532ac
+			real_cpu = op_get_next_online_cpu(dir, entry);
c532ac
 			if (real_cpu < 0) {
c532ac
 				err_msg = "Internal Error: Number of online cpus cannot be determined.";
c532ac
 				rc = -1;
c532ac
@@ -803,7 +769,8 @@ static bool convert_event_vals(vector<operf_event_t> * evt_vec)
c532ac
 
c532ac
 
c532ac
 
c532ac
-void op_pe_utils::op_process_events_list(vector<string> & passed_evts)
c532ac
+void op_pe_utils::op_process_events_list(vector<string> & passed_evts,
c532ac
+                                         bool do_profiling, bool do_callgraph)
c532ac
 {
c532ac
 	string cmd = OP_BINDIR;
c532ac
 
c532ac
@@ -812,7 +779,9 @@ void op_pe_utils::op_process_events_list(vector<string> & passed_evts)
c532ac
 		     << OP_MAX_EVENTS << "." << endl;
c532ac
 		exit(EXIT_FAILURE);
c532ac
 	}
c532ac
-	cmd += "/ophelp --check-events --ignore-count ";
c532ac
+	cmd += "/ophelp --check-events ";
c532ac
+	if (!do_profiling)
c532ac
+		cmd += "--ignore-count ";
c532ac
 	for (unsigned int i = 0; i <  passed_evts.size(); i++) {
c532ac
 		FILE * fp;
c532ac
 		string full_cmd = cmd;
c532ac
@@ -825,6 +794,8 @@ void op_pe_utils::op_process_events_list(vector<string> & passed_evts)
c532ac
 			event_spec = _handle_powerpc_event_spec(event_spec);
c532ac
 #endif
c532ac
 
c532ac
+		if (do_callgraph)
c532ac
+			full_cmd += " --callgraph=1 ";
c532ac
 		full_cmd += event_spec;
c532ac
 		fp = popen(full_cmd.c_str(), "r");
c532ac
 		if (fp == NULL) {
c532ac
@@ -836,14 +807,21 @@ void op_pe_utils::op_process_events_list(vector<string> & passed_evts)
c532ac
 			pclose(fp);
c532ac
 			cerr << "Error retrieving info for event "
c532ac
 			     << event_spec << endl;
c532ac
+			if (do_callgraph)
c532ac
+				cerr << "Note: When doing callgraph profiling, the sample count must be"
c532ac
+				     << endl << "15 times the minimum count value for the event."  << endl;
c532ac
 			exit(EXIT_FAILURE);
c532ac
 		}
c532ac
 		pclose(fp);
c532ac
 		char * event_str = op_xstrndup(event_spec.c_str(), event_spec.length());
c532ac
 		operf_event_t event;
c532ac
 		strncpy(event.name, strtok(event_str, ":"), OP_MAX_EVT_NAME_LEN - 1);
c532ac
+		if (do_profiling)
c532ac
+			event.count = atoi(strtok(NULL, ":"));
c532ac
+		else
c532ac
+			event.count = 0UL;
c532ac
 		/* Event name is required in the event spec in order for
c532ac
-		 * 'ophelp --check-events --ignore-count' to pass.  But since unit mask
c532ac
+		 * 'ophelp --check-events' to pass.  But since unit mask
c532ac
 		 *  and domain control bits are optional, we need to ensure the result of
c532ac
 		 *  strtok is valid.
c532ac
 		 */
c532ac
@@ -854,7 +832,6 @@ void op_pe_utils::op_process_events_list(vector<string> & passed_evts)
c532ac
 		int place =  _OP_UM;
c532ac
 		char * endptr = NULL;
c532ac
 		event.evt_um = 0UL;
c532ac
-		event.count = 0UL;
c532ac
 		event.no_kernel = 0;
c532ac
 		event.no_user = 0;
c532ac
 		event.throttled = false;
c532ac
@@ -904,7 +881,7 @@ void op_pe_utils::op_process_events_list(vector<string> & passed_evts)
c532ac
 #endif
c532ac
 }
c532ac
 
c532ac
-void op_pe_utils::op_get_default_event(void)
c532ac
+void op_pe_utils::op_get_default_event(bool do_callgraph)
c532ac
 {
c532ac
 	operf_event_t dft_evt;
c532ac
 	struct op_default_event_descr descr;
c532ac
@@ -918,7 +895,18 @@ void op_pe_utils::op_get_default_event(void)
c532ac
 	}
c532ac
 
c532ac
 	memset(&dft_evt, 0, sizeof(dft_evt));
c532ac
-	dft_evt.count = descr.count;
c532ac
+	if (do_callgraph) {
c532ac
+		struct op_event * _event;
c532ac
+		op_events(cpu_type);
c532ac
+		if ((_event = find_event_by_name(descr.name, 0, 0))) {
c532ac
+			dft_evt.count = _event->min_count * CALLGRAPH_MIN_COUNT_SCALE;
c532ac
+		} else {
c532ac
+			cerr << "Error getting event info for " << descr.name << endl;
c532ac
+			exit(EXIT_FAILURE);
c532ac
+		}
c532ac
+	} else {
c532ac
+		dft_evt.count = descr.count;
c532ac
+	}
c532ac
 	dft_evt.evt_um = descr.um;
c532ac
 	strncpy(dft_evt.name, descr.name, OP_MAX_EVT_NAME_LEN - 1);
c532ac
 	_get_event_code(&dft_evt, cpu_type);
c532ac
diff --git a/libpe_utils/op_pe_utils.h b/libpe_utils/op_pe_utils.h
c532ac
index 400eed3..08b6fae 100644
c532ac
--- a/libpe_utils/op_pe_utils.h
c532ac
+++ b/libpe_utils/op_pe_utils.h
c532ac
@@ -18,11 +18,13 @@
c532ac
 #include <dirent.h>
c532ac
 
c532ac
 #include <vector>
c532ac
+#include <set>
c532ac
 
c532ac
 #include "op_cpu_type.h"
c532ac
 
c532ac
 #define OP_APPNAME_LEN 1024
c532ac
 #define OP_MAX_EVENTS 24
c532ac
+#define CALLGRAPH_MIN_COUNT_SCALE 15
c532ac
 
c532ac
 /* A macro to be used for ppc64 architecture-specific code.  The '__powerpc__' macro
c532ac
  * is defined for both ppc64 and ppc32 architectures, so we must further qualify by
c532ac
@@ -38,8 +40,10 @@ extern int op_check_perf_events_cap(bool use_cpu_minus_one);
c532ac
 extern int op_get_sys_value(const char * filename);
c532ac
 extern int op_get_cpu_for_perf_events_cap(void);
c532ac
 extern int op_validate_app_name(char ** app, char ** save_appname);
c532ac
-extern void op_get_default_event(void);
c532ac
-extern void op_process_events_list(std::vector<std::string> & passed_evts);
c532ac
+extern void op_get_default_event(bool do_callgraph);
c532ac
+extern void op_process_events_list(std::vector<std::string> & passed_evts,
c532ac
+                                   bool do_profiling, bool do_callgraph);
c532ac
+extern int op_get_next_online_cpu(DIR * dir, struct dirent *entry);
c532ac
 extern std::set<int> op_get_available_cpus(int max_num_cpus);
c532ac
 }
c532ac
 
c532ac
diff --git a/libperf_events/Makefile.am b/libperf_events/Makefile.am
c532ac
index 7163610..cf5f434 100644
c532ac
--- a/libperf_events/Makefile.am
c532ac
+++ b/libperf_events/Makefile.am
c532ac
@@ -7,6 +7,7 @@ AM_CPPFLAGS = \
c532ac
 	-I ${top_srcdir}/libop \
c532ac
 	-I ${top_srcdir}/libdb \
c532ac
 	-I ${top_srcdir}/libperf_events \
c532ac
+	-I ${top_srcdir}/libpe_utils \
c532ac
 	@PERF_EVENT_FLAGS@ \
c532ac
 	@OP_CPPFLAGS@
c532ac
 
c532ac
diff --git a/libperf_events/operf_counter.cpp b/libperf_events/operf_counter.cpp
c532ac
index b4cceaa..319e859 100644
c532ac
--- a/libperf_events/operf_counter.cpp
c532ac
+++ b/libperf_events/operf_counter.cpp
c532ac
@@ -31,6 +31,7 @@
c532ac
 #include "operf_process_info.h"
c532ac
 #include "op_libiberty.h"
c532ac
 #include "operf_stats.h"
c532ac
+#include "op_pe_utils.h"
c532ac
 
c532ac
 
c532ac
 using namespace std;
c532ac
@@ -645,7 +646,7 @@ void operf_record::setup()
c532ac
 		} else if (all_cpus_avail) {
c532ac
 			real_cpu = cpu;
c532ac
 		} else {
c532ac
-			real_cpu = op_get_next_online_cpu(dir, entry);
c532ac
+			real_cpu = op_pe_utils::op_get_next_online_cpu(dir, entry);
c532ac
 			if (real_cpu < 0) {
c532ac
 				err_msg = "Internal Error: Number of online cpus cannot be determined.";
c532ac
 				rc = -1;
c532ac
diff --git a/libperf_events/operf_utils.cpp b/libperf_events/operf_utils.cpp
c532ac
index 30e64d8..faed9a6 100644
c532ac
--- a/libperf_events/operf_utils.cpp
c532ac
+++ b/libperf_events/operf_utils.cpp
c532ac
@@ -65,161 +65,6 @@ static list<event_t *> unresolved_events;
c532ac
 static struct operf_transient trans;
c532ac
 static bool sfile_init_done;
c532ac
 
c532ac
-/* Some architectures (e.g., ppc64) do not use the same event value (code) for oprofile
c532ac
- * and for perf_events.  The operf-record process requires event values that perf_events
c532ac
- * understands, but the operf-read process requires oprofile event values.  The purpose of
c532ac
- * the following method is to map the operf-record event value to a value that
c532ac
- * opreport can understand.
c532ac
- */
c532ac
-#if PPC64_ARCH
c532ac
-extern op_cpu cpu_type;
c532ac
-#define NIL_CODE ~0U
c532ac
-
c532ac
-#if HAVE_LIBPFM3
c532ac
-static bool _get_codes_for_match(unsigned int pfm_idx, const char name[],
c532ac
-                                 vector<operf_event_t> * evt_vec)
c532ac
-{
c532ac
-	unsigned int num_events = evt_vec->size();
c532ac
-	int tmp_code, ret;
c532ac
-	char evt_name[OP_MAX_EVT_NAME_LEN];
c532ac
-	unsigned int events_converted = 0;
c532ac
-	for (unsigned int i = 0; i < num_events; i++) {
c532ac
-		operf_event_t event = (*evt_vec)[i];
c532ac
-		if (event.evt_code != NIL_CODE) {
c532ac
-			events_converted++;
c532ac
-			continue;
c532ac
-		}
c532ac
-		memset(evt_name, 0, OP_MAX_EVT_NAME_LEN);
c532ac
-		if (!strcmp(event.name, "CYCLES")) {
c532ac
-			strcpy(evt_name ,"PM_CYC") ;
c532ac
-		} else if (strstr(event.name, "_GRP")) {
c532ac
-			string str = event.name;
c532ac
-			strncpy(evt_name, event.name, str.rfind("_GRP"));
c532ac
-		} else {
c532ac
-			strncpy(evt_name, event.name, strlen(event.name));
c532ac
-		}
c532ac
-		if (strncmp(name, evt_name, OP_MAX_EVT_NAME_LEN))
c532ac
-			continue;
c532ac
-		ret = pfm_get_event_code(pfm_idx, &tmp_code);
c532ac
-		if (ret != PFMLIB_SUCCESS) {
c532ac
-			string evt_name_str = event.name;
c532ac
-			string msg = "libpfm cannot find event code for " + evt_name_str +
c532ac
-					"; cannot continue";
c532ac
-			throw runtime_error(msg);
c532ac
-		}
c532ac
-		event.evt_code = tmp_code;
c532ac
-		(*evt_vec)[i] = event;
c532ac
-		events_converted++;
c532ac
-		cverb << vrecord << "Successfully converted " << event.name << " to perf_event code "
c532ac
-		      << hex << tmp_code << endl;
c532ac
-	}
c532ac
-	return (events_converted == num_events);
c532ac
-}
c532ac
-#else
c532ac
-static bool _op_get_event_codes(vector<operf_event_t> * evt_vec)
c532ac
-{
c532ac
-	int ret, i;
c532ac
-	unsigned int num_events = evt_vec->size();
c532ac
-	char evt_name[OP_MAX_EVT_NAME_LEN];
c532ac
-	unsigned int events_converted = 0;
c532ac
-	uint64_t code[1];
c532ac
-
c532ac
-	typedef struct {
c532ac
-		uint64_t    *codes;
c532ac
-		char        **fstr;
c532ac
-		size_t      size;
c532ac
-		int         count;
c532ac
-		int         idx;
c532ac
-	} pfm_raw_pmu_encode_t;
c532ac
-
c532ac
-	pfm_raw_pmu_encode_t raw;
c532ac
-	raw.codes = code;
c532ac
-	raw.count = 1;
c532ac
-	raw.fstr = NULL;
c532ac
-
c532ac
-	if (pfm_initialize() != PFM_SUCCESS)
c532ac
-		throw runtime_error("Unable to initialize libpfm; cannot continue");
c532ac
-
c532ac
-	for (unsigned int i = 0; i < num_events; i++) {
c532ac
-		operf_event_t event = (*evt_vec)[i];
c532ac
-		if (event.evt_code != NIL_CODE) {
c532ac
-			events_converted++;
c532ac
-			continue;
c532ac
-		}
c532ac
-		memset(evt_name, 0, OP_MAX_EVT_NAME_LEN);
c532ac
-		if (!strcmp(event.name, "CYCLES")) {
c532ac
-			strcpy(evt_name ,"PM_CYC") ;
c532ac
-		} else if (strstr(event.name, "_GRP")) {
c532ac
-			string str = event.name;
c532ac
-			strncpy(evt_name, event.name, str.rfind("_GRP"));
c532ac
-		} else {
c532ac
-			strncpy(evt_name, event.name, strlen(event.name));
c532ac
-		}
c532ac
-
c532ac
-		memset(&raw, 0, sizeof(raw));
c532ac
-		ret = pfm_get_os_event_encoding(evt_name, PFM_PLM3, PFM_OS_NONE, &raw;;
c532ac
-		if (ret != PFM_SUCCESS) {
c532ac
-			string evt_name_str = event.name;
c532ac
-			string msg = "libpfm cannot find event code for " + evt_name_str +
c532ac
-					"; cannot continue";
c532ac
-			throw runtime_error(msg);
c532ac
-		}
c532ac
-
c532ac
-		event.evt_code = raw.codes[0];
c532ac
-		(*evt_vec)[i] = event;
c532ac
-		events_converted++;
c532ac
-		cverb << vrecord << "Successfully converted " << event.name << " to perf_event code "
c532ac
-		      << hex << event.evt_code << endl;
c532ac
-	}
c532ac
-	return (events_converted == num_events);
c532ac
-}
c532ac
-#endif
c532ac
-
c532ac
-bool OP_perf_utils::op_convert_event_vals(vector<operf_event_t> * evt_vec)
c532ac
-{
c532ac
-	unsigned int i, count;
c532ac
-	char name[256];
c532ac
-	int ret;
c532ac
-	for (unsigned int i = 0; i < evt_vec->size(); i++) {
c532ac
-		operf_event_t event = (*evt_vec)[i];
c532ac
-		if (cpu_type == CPU_PPC64_POWER7) {
c532ac
-			if (!strncmp(event.name, "PM_RUN_CYC", strlen("PM_RUN_CYC"))) {
c532ac
-				event.evt_code = 0x600f4;
c532ac
-			} else if (!strncmp(event.name, "PM_RUN_INST_CMPL", strlen("PM_RUN_INST_CMPL"))) {
c532ac
-				event.evt_code = 0x500fa;
c532ac
-			} else {
c532ac
-				event.evt_code = NIL_CODE;
c532ac
-			}
c532ac
-		} else {
c532ac
-			event.evt_code = NIL_CODE;
c532ac
-		}
c532ac
-		(*evt_vec)[i] = event;
c532ac
-	}
c532ac
-
c532ac
-#if HAVE_LIBPFM3
c532ac
-	if (pfm_initialize() != PFMLIB_SUCCESS)
c532ac
-		throw runtime_error("Unable to initialize libpfm; cannot continue");
c532ac
-
c532ac
-	ret = pfm_get_num_events(&count);
c532ac
-	if (ret != PFMLIB_SUCCESS)
c532ac
-		throw runtime_error("Unable to use libpfm to obtain event code; cannot continue");
c532ac
-	for(i =0 ; i < count; i++)
c532ac
-	{
c532ac
-		ret = pfm_get_event_name(i, name, 256);
c532ac
-		if (ret != PFMLIB_SUCCESS)
c532ac
-			continue;
c532ac
-		if (_get_codes_for_match(i, name, evt_vec))
c532ac
-			break;
c532ac
-	}
c532ac
-	return (i != count);
c532ac
-#else
c532ac
-	return _op_get_event_codes(evt_vec);
c532ac
-#endif
c532ac
-}
c532ac
-
c532ac
-#endif // PPC64_ARCH
c532ac
-
c532ac
-
c532ac
 static inline void update_trans_last(struct operf_transient * trans)
c532ac
 {
c532ac
 	trans->last = trans->current;
c532ac
@@ -1465,38 +1310,3 @@ void OP_perf_utils::op_get_kernel_event_data(struct mmap_data *md, operf_record
c532ac
 	md->prev = old;
c532ac
 	pc->data_tail = old;
c532ac
 }
c532ac
-
c532ac
-
c532ac
-int OP_perf_utils::op_get_next_online_cpu(DIR * dir, struct dirent *entry)
c532ac
-{
c532ac
-#define OFFLINE 0x30
c532ac
-	unsigned int cpu_num;
c532ac
-	char cpu_online_pathname[40];
c532ac
-	int res;
c532ac
-	FILE * online;
c532ac
-	again:
c532ac
-	do {
c532ac
-		entry = readdir(dir);
c532ac
-		if (!entry)
c532ac
-			return -1;
c532ac
-	} while (entry->d_type != DT_DIR);
c532ac
-
c532ac
-	res = sscanf(entry->d_name, "cpu%u", &cpu_num);
c532ac
-	if (res <= 0)
c532ac
-		goto again;
c532ac
-
c532ac
-	errno = 0;
c532ac
-	snprintf(cpu_online_pathname, 40, "/sys/devices/system/cpu/cpu%u/online", cpu_num);
c532ac
-	if ((online = fopen(cpu_online_pathname, "r")) == NULL) {
c532ac
-		cerr << "Unable to open " << cpu_online_pathname << endl;
c532ac
-		if (errno)
c532ac
-			cerr << strerror(errno) << endl;
c532ac
-		return -1;
c532ac
-	}
c532ac
-	res = fgetc(online);
c532ac
-	fclose(online);
c532ac
-	if (res == OFFLINE)
c532ac
-		goto again;
c532ac
-	else
c532ac
-		return cpu_num;
c532ac
-}
c532ac
diff --git a/libperf_events/operf_utils.h b/libperf_events/operf_utils.h
c532ac
index 4c191fe..2a979e3 100644
c532ac
--- a/libperf_events/operf_utils.h
c532ac
+++ b/libperf_events/operf_utils.h
c532ac
@@ -87,8 +87,6 @@ int op_write_output(int output, void *buf, size_t size);
c532ac
 int op_write_event(event_t * event, u64 sample_type);
c532ac
 int op_read_from_stream(std::ifstream & is, char * buf, std::streamsize sz);
c532ac
 int op_mmap_trace_file(struct mmap_info & info, bool init);
c532ac
-int op_get_next_online_cpu(DIR * dir, struct dirent *entry);
c532ac
-bool op_convert_event_vals(std::vector<operf_event_t> * evt_vec);
c532ac
 void op_reprocess_unresolved_events(u64 sample_type, bool print_progress);
c532ac
 void op_release_resources(void);
c532ac
 }
c532ac
diff --git a/pe_counting/ocount.cpp b/pe_counting/ocount.cpp
c532ac
index 5a85c3f..db847ea 100644
c532ac
--- a/pe_counting/ocount.cpp
c532ac
+++ b/pe_counting/ocount.cpp
c532ac
@@ -720,9 +720,9 @@ static void process_args(int argc, char * const argv[])
c532ac
 
c532ac
 	if (ocount_options::evts.empty()) {
c532ac
 		// Use default event
c532ac
-		op_pe_utils::op_get_default_event();
c532ac
+		op_pe_utils::op_get_default_event(false);
c532ac
 	} else  {
c532ac
-		op_pe_utils::op_process_events_list(ocount_options::evts);
c532ac
+		op_pe_utils::op_process_events_list(ocount_options::evts, false, false);
c532ac
 	}
c532ac
 	cverb << vdebug << "Number of events passed is " << events.size() << endl;
c532ac
 	return;
c532ac
diff --git a/pe_profiling/Makefile.am b/pe_profiling/Makefile.am
c532ac
index b27cbc7..8c232c4 100644
c532ac
--- a/pe_profiling/Makefile.am
c532ac
+++ b/pe_profiling/Makefile.am
c532ac
@@ -6,6 +6,7 @@ AM_CPPFLAGS = \
c532ac
 	-I ${top_srcdir}/libop \
c532ac
 	-I ${top_srcdir}/libutil++ \
c532ac
 	-I ${top_srcdir}/libperf_events \
c532ac
+	-I ${top_srcdir}/libpe_utils \
c532ac
 	@PERF_EVENT_FLAGS@ \
c532ac
 	@OP_CPPFLAGS@
c532ac
 
c532ac
@@ -15,7 +16,8 @@ AM_CXXFLAGS = @OP_CXXFLAGS@
c532ac
 AM_LDFLAGS = @OP_LDFLAGS@
c532ac
 
c532ac
 bin_PROGRAMS = operf
c532ac
-operf_LDADD =	../libperf_events/libperf_events.a \
c532ac
+operf_LDADD = ../libperf_events/libperf_events.a \
c532ac
+	../libpe_utils/libpe_utils.a \
c532ac
 	../libutil++/libutil++.a \
c532ac
 	../libdb/libodb.a \
c532ac
 	../libop/libop.a \
c532ac
diff --git a/pe_profiling/operf.cpp b/pe_profiling/operf.cpp
c532ac
index 3fec123..89e9c4b 100644
c532ac
--- a/pe_profiling/operf.cpp
c532ac
+++ b/pe_profiling/operf.cpp
c532ac
@@ -35,6 +35,7 @@
c532ac
 #include <getopt.h>
c532ac
 #include <iostream>
c532ac
 #include "operf_utils.h"
c532ac
+#include "op_pe_utils.h"
c532ac
 #include "op_libiberty.h"
c532ac
 #include "string_manip.h"
c532ac
 #include "cverb.h"
c532ac
@@ -50,6 +51,7 @@
c532ac
 #include "op_netburst.h"
c532ac
 
c532ac
 using namespace std;
c532ac
+using namespace op_pe_utils;
c532ac
 
c532ac
 typedef enum END_CODE {
c532ac
 	ALL_OK = 0,
c532ac
@@ -73,11 +75,11 @@ uid_t my_uid;
c532ac
 bool no_vmlinux;
c532ac
 int kptr_restrict;
c532ac
 char * start_time_human_readable;
c532ac
+std::vector<operf_event_t> events;
c532ac
+
c532ac
 
c532ac
 #define DEFAULT_OPERF_OUTFILE "operf.data"
c532ac
-#define CALLGRAPH_MIN_COUNT_SCALE 15
c532ac
 
c532ac
-static char full_pathname[PATH_MAX];
c532ac
 static char * app_name_SAVE = NULL;
c532ac
 static char ** app_args = NULL;
c532ac
 static 	pid_t jitconv_pid = -1;
c532ac
@@ -88,7 +90,6 @@ static string samples_dir;
c532ac
 static bool startApp;
c532ac
 static string outputfile;
c532ac
 static char start_time_str[32];
c532ac
-static vector<operf_event_t> events;
c532ac
 static bool jit_conversion_running;
c532ac
 static void convert_sample_data(void);
c532ac
 static int sample_data_pipe[2];
c532ac
@@ -948,517 +949,6 @@ out:
c532ac
 }
c532ac
 
c532ac
 
c532ac
-static int find_app_file_in_dir(const struct dirent * d)
c532ac
-{
c532ac
-	if (!strcmp(d->d_name, app_name))
c532ac
-		return 1;
c532ac
-	else
c532ac
-		return 0;
c532ac
-}
c532ac
-
c532ac
-static int get_PATH_based_pathname(char * path_holder, size_t n)
c532ac
-{
c532ac
-	int retval = -1;
c532ac
-
c532ac
-	char * real_path = getenv("PATH");
c532ac
-	char * path = (char *) xstrdup(real_path);
c532ac
-	char * segment = strtok(path, ":");
c532ac
-	while (segment) {
c532ac
-		struct dirent ** namelist;
c532ac
-		int rc = scandir(segment, &namelist, find_app_file_in_dir, NULL);
c532ac
-		if (rc < 0) {
c532ac
-			if (errno != ENOENT) {
c532ac
-				cerr << strerror(errno) << endl;
c532ac
-				cerr << app_name << " cannot be found in your PATH." << endl;
c532ac
-				break;
c532ac
-			}
c532ac
-		} else if (rc == 1) {
c532ac
-			size_t applen = strlen(app_name);
c532ac
-			size_t dirlen = strlen(segment);
c532ac
-
c532ac
-			if (applen + dirlen + 2 > n) {
c532ac
-				cerr << "Path segment " << segment
c532ac
-				     << " prepended to the passed app name is too long"
c532ac
-				     << endl;
c532ac
-				retval = -1;
c532ac
-				break;
c532ac
-			}
c532ac
-
c532ac
-			if (!strcmp(segment, ".")) {
c532ac
-				if (getcwd(path_holder, PATH_MAX) == NULL) {
c532ac
-					retval = -1;
c532ac
-					cerr << "getcwd [3] failed when processing <cur-dir>/" << app_name << " found via PATH. Aborting."
c532ac
-							<< endl;
c532ac
-					break;
c532ac
-				}
c532ac
-			} else {
c532ac
-				strncpy(path_holder, segment, dirlen);
c532ac
-			}
c532ac
-			strcat(path_holder, "/");
c532ac
-			strncat(path_holder, app_name, applen);
c532ac
-			retval = 0;
c532ac
-			free(namelist[0]);
c532ac
-			free(namelist);
c532ac
-
c532ac
-			break;
c532ac
-		}
c532ac
-		segment = strtok(NULL, ":");
c532ac
-	}
c532ac
-	free(path);
c532ac
-	return retval;
c532ac
-}
c532ac
-int validate_app_name(void)
c532ac
-{
c532ac
-	int rc = 0;
c532ac
-	struct stat filestat;
c532ac
-	size_t len = strlen(app_name);
c532ac
-
c532ac
-	if (len > (size_t) (OP_APPNAME_LEN - 1)) {
c532ac
-		cerr << "app name longer than max allowed (" << OP_APPNAME_LEN
c532ac
-		     << " chars)\n";
c532ac
-		cerr << app_name << endl;
c532ac
-		rc = -1;
c532ac
-		goto out;
c532ac
-	}
c532ac
-
c532ac
-	if (index(app_name, '/') == app_name) {
c532ac
-		// Full pathname of app was specified, starting with "/".
c532ac
-		strncpy(full_pathname, app_name, len);
c532ac
-	} else if ((app_name[0] == '.') && (app_name[1] == '/')) {
c532ac
-		// Passed app is in current directory; e.g., "./myApp"
c532ac
-		if (getcwd(full_pathname, PATH_MAX) == NULL) {
c532ac
-			rc = -1;
c532ac
-			cerr << "getcwd [1] failed when trying to find app name " << app_name << ". Aborting."
c532ac
-			     << endl;
c532ac
-			goto out;
c532ac
-		}
c532ac
-		strcat(full_pathname, "/");
c532ac
-		if ((strlen(full_pathname) + strlen(app_name + 2) + 1) > PATH_MAX) {
c532ac
-			rc = -1;
c532ac
-			cerr << "Length of current dir (" << full_pathname << ") and app name ("
c532ac
-			     << (app_name + 2) << ") exceeds max allowed (" << PATH_MAX << "). Aborting."
c532ac
-			     << endl;
c532ac
-			goto out;
c532ac
-		}
c532ac
-		strcat(full_pathname, (app_name + 2));
c532ac
-	} else if (index(app_name, '/')) {
c532ac
-		// Passed app is in a subdirectory of cur dir; e.g., "test-stuff/myApp"
c532ac
-		if (getcwd(full_pathname, PATH_MAX) == NULL) {
c532ac
-			rc = -1;
c532ac
-			cerr << "getcwd [2] failed when trying to find app name " << app_name << ". Aborting."
c532ac
-			     << endl;
c532ac
-			goto out;
c532ac
-		}
c532ac
-		strcat(full_pathname, "/");
c532ac
-		strcat(full_pathname, app_name);
c532ac
-	} else {
c532ac
-		// Passed app name, at this point, MUST be found in PATH
c532ac
-		rc = get_PATH_based_pathname(full_pathname, PATH_MAX);
c532ac
-	}
c532ac
-
c532ac
-	if (rc) {
c532ac
-		cerr << "Problem finding app name " << app_name << ". Aborting."
c532ac
-		     << endl;
c532ac
-		goto out;
c532ac
-	}
c532ac
-	app_name_SAVE = app_name;
c532ac
-	app_name = full_pathname;
c532ac
-	if (stat(app_name, &filestat)) {
c532ac
-		char msg[OP_APPNAME_LEN + 50];
c532ac
-		snprintf(msg, OP_APPNAME_LEN + 50, "Non-existent app name \"%s\"",
c532ac
-		         app_name);
c532ac
-		perror(msg);
c532ac
-		rc = -1;
c532ac
-	}
c532ac
-
c532ac
-	out: return rc;
c532ac
-}
c532ac
-
c532ac
-static void _get_event_code(operf_event_t * event)
c532ac
-{
c532ac
-	FILE * fp;
c532ac
-	char oprof_event_code[9];
c532ac
-	string command;
c532ac
-	u64 base_code, config;
c532ac
-	char buf[20];
c532ac
-	if ((snprintf(buf, 20, "%lu", event->count)) < 0) {
c532ac
-		cerr << "Error parsing event count of " << event->count << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-
c532ac
-	base_code = config = 0ULL;
c532ac
-
c532ac
-	command = OP_BINDIR;
c532ac
-	command += "ophelp ";
c532ac
-	command += event->name;
c532ac
-
c532ac
-	fp = popen(command.c_str(), "r");
c532ac
-	if (fp == NULL) {
c532ac
-		cerr << "Unable to execute ophelp to get info for event "
c532ac
-		     << event->name << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-	if (fgets(oprof_event_code, sizeof(oprof_event_code), fp) == NULL) {
c532ac
-		pclose(fp);
c532ac
-		cerr << "Unable to find info for event "
c532ac
-		     << event->name << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-
c532ac
-	pclose(fp);
c532ac
-
c532ac
-	base_code = strtoull(oprof_event_code, (char **) NULL, 10);
c532ac
-
c532ac
-
c532ac
-#if defined(__i386__) || defined(__x86_64__)
c532ac
-	// Setup EventSelct[11:8] field for AMD
c532ac
-	char mask[12];
c532ac
-	const char * vendor_AMD = "AuthenticAMD";
c532ac
-	if (op_is_cpu_vendor((char *)vendor_AMD)) {
c532ac
-		config = base_code & 0xF00ULL;
c532ac
-		config = config << 32;
c532ac
-	}
c532ac
-
c532ac
-	// Setup EventSelct[7:0] field
c532ac
-	config |= base_code & 0xFFULL;
c532ac
-
c532ac
-	// Setup unitmask field
c532ac
-handle_named_um:
c532ac
-	if (event->um_name[0]) {
c532ac
-		command = OP_BINDIR;
c532ac
-		command += "ophelp ";
c532ac
-		command += "--extra-mask ";
c532ac
-		command += event->name;
c532ac
-		command += ":";
c532ac
-		command += buf;
c532ac
-		command += ":";
c532ac
-		command += event->um_name;
c532ac
-		fp = popen(command.c_str(), "r");
c532ac
-		if (fp == NULL) {
c532ac
-			cerr << "Unable to execute ophelp to get info for event "
c532ac
-			     << event->name << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-		if (fgets(mask, sizeof(mask), fp) == NULL) {
c532ac
-			pclose(fp);
c532ac
-			cerr << "Unable to find unit mask info for " << event->um_name << " for event "
c532ac
-			     << event->name << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-		pclose(fp);
c532ac
-		// FIXME:  The mask value here is the extra bits from the named unit mask.  It's not
c532ac
-		// ideal to put that value into the UM's mask, since that's what will show up in
c532ac
-		// opreport.  It would be better if we could somehow have the unit mask name that the
c532ac
-		// user passed to us show up in opreort.
c532ac
-		event->evt_um = strtoull(mask, (char **) NULL, 10);
c532ac
-		/* A value >= EXTRA_MIN_VAL returned by 'ophelp --extra-mask' is interpreted as a
c532ac
-		 * valid extra value; otherwise we interpret it as a simple unit mask value
c532ac
-		 * for a named unit mask with EXTRA_NONE.
c532ac
-		 */
c532ac
-		if (event->evt_um >= EXTRA_MIN_VAL)
c532ac
-			config |= event->evt_um;
c532ac
-		else
c532ac
-			config |= ((event->evt_um & 0xFFULL) << 8);
c532ac
-	} else if (!event->evt_um) {
c532ac
-		char * endptr;
c532ac
-		command.clear();
c532ac
-		command = OP_BINDIR;
c532ac
-		command += "ophelp ";
c532ac
-		command += "--unit-mask ";
c532ac
-		command += event->name;
c532ac
-		command += ":";
c532ac
-		command += buf;
c532ac
-		fp = popen(command.c_str(), "r");
c532ac
-		if (fp == NULL) {
c532ac
-			cerr << "Unable to execute ophelp to get unit mask for event "
c532ac
-			     << event->name << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-		if (fgets(mask, sizeof(mask), fp) == NULL) {
c532ac
-			pclose(fp);
c532ac
-			cerr << "Unable to find unit mask info for event " << event->name << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-		pclose(fp);
c532ac
-		event->evt_um = strtoull(mask, &endptr, 10);
c532ac
-		if ((endptr >= mask) &&
c532ac
-				(endptr <= (mask + strlen(mask) - 1))) {
c532ac
-			// Must be a default named unit mask
c532ac
-			strncpy(event->um_name, mask, OP_MAX_UM_NAME_LEN);
c532ac
-			goto handle_named_um;
c532ac
-		}
c532ac
-		config |= ((event->evt_um & 0xFFULL) << 8);
c532ac
-	} else {
c532ac
-		config |= ((event->evt_um & 0xFFULL) << 8);
c532ac
-	}
c532ac
-#else
c532ac
-	config = base_code;
c532ac
-#endif
c532ac
-
c532ac
-	event->op_evt_code = base_code;
c532ac
-	if (cpu_type == CPU_P4 || cpu_type == CPU_P4_HT2) {
c532ac
-		if (op_netburst_get_perf_encoding(event->name, event->evt_um, 1, 1, &config)) {
c532ac
-			cerr << "Unable to get event encoding for " << event->name << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-	}
c532ac
-	event->evt_code = config;
c532ac
-}
c532ac
-
c532ac
-#if PPC64_ARCH
c532ac
-/* All ppc64 events (except CYCLES) have a _GRP<n> suffix.  This is
c532ac
- * because the legacy opcontrol profiler can only profile events in
c532ac
- * the same group (i.e., having the same _GRP<n> suffix).  But operf
c532ac
- * can multiplex events, so we should allow the user to pass event
c532ac
- * names without the _GRP<n> suffix.
c532ac
- *
c532ac
- * If event name is not CYCLES or does not have a _GRP<n> suffix,
c532ac
- * we'll call ophelp and scan the list of events, searching for one
c532ac
- * that matches up to the _GRP<n> suffix.  If we don't find a match,
c532ac
- * then we'll exit with the expected error message for invalid event name.
c532ac
- */
c532ac
-static string _handle_powerpc_event_spec(string event_spec)
c532ac
-{
c532ac
-	FILE * fp;
c532ac
-	char line[MAX_INPUT];
c532ac
-	size_t grp_pos;
c532ac
-	string evt, retval, err_msg;
c532ac
-	size_t evt_name_len;
c532ac
-	bool first_non_cyc_evt_found = false;
c532ac
-	bool event_found = false;
c532ac
-	char event_name[OP_MAX_EVT_NAME_LEN], event_spec_str[OP_MAX_EVT_NAME_LEN + 20], * count_str;
c532ac
-	string cmd = OP_BINDIR;
c532ac
-	cmd += "/ophelp";
c532ac
-
c532ac
-	strncpy(event_spec_str, event_spec.c_str(), event_spec.length() + 1);
c532ac
-
c532ac
-	strncpy(event_name, strtok(event_spec_str, ":"), OP_MAX_EVT_NAME_LEN);
c532ac
-	count_str = strtok(NULL, ":");
c532ac
-	if (!count_str) {
c532ac
-		err_msg = "Invalid count for event ";
c532ac
-		goto out;
c532ac
-	}
c532ac
-
c532ac
-	if (!strcmp("CYCLES", event_name)) {
c532ac
-		event_found = true;
c532ac
-		goto out;
c532ac
-	}
c532ac
-
c532ac
-	evt = event_name;
c532ac
-	// Need to make sure the event name truly has a _GRP<n> suffix.
c532ac
-	grp_pos = evt.rfind("_GRP");
c532ac
-	if ((grp_pos != string::npos) && ((evt = evt.substr(grp_pos, string::npos))).length() > 4) {
c532ac
-		char * end;
c532ac
-		strtoul(evt.substr(4, string::npos).c_str(), &end, 0);
c532ac
-		if (end && (*end == '\0')) {
c532ac
-		// Valid group number found after _GRP, so we can skip to the end.
c532ac
-			event_found = true;
c532ac
-			goto out;
c532ac
-		}
c532ac
-	}
c532ac
-
c532ac
-	// If we get here, it implies the user passed a non-CYCLES event without a GRP suffix.
c532ac
-	// Lets try to find a valid suffix for it.
c532ac
-	fp = popen(cmd.c_str(), "r");
c532ac
-	if (fp == NULL) {
c532ac
-		cerr << "Unable to execute ophelp to get info for event "
c532ac
-		     << event_spec << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-	evt_name_len = strlen(event_name);
c532ac
-	err_msg = "Cannot find event ";
c532ac
-	while (fgets(line, MAX_INPUT, fp)) {
c532ac
-		if (!first_non_cyc_evt_found) {
c532ac
-			if (!strncmp(line, "PM_", 3))
c532ac
-				first_non_cyc_evt_found = true;
c532ac
-			else
c532ac
-				continue;
c532ac
-		}
c532ac
-		if (line[0] == ' ' || line[0] == '\t')
c532ac
-			continue;
c532ac
-		if (!strncmp(line, event_name, evt_name_len)) {
c532ac
-			// Found a potential match.  Check if it's a perfect match.
c532ac
-			string save_event_name = event_name;
c532ac
-			size_t full_evt_len = index(line, ':') - line;
c532ac
-			memset(event_name, '\0', OP_MAX_EVT_NAME_LEN);
c532ac
-			strncpy(event_name, line, full_evt_len);
c532ac
-			string candidate = event_name;
c532ac
-			if (candidate.rfind("_GRP") == evt_name_len) {
c532ac
-				event_found = true;
c532ac
-				break;
c532ac
-			} else {
c532ac
-				memset(event_name, '\0', OP_MAX_EVT_NAME_LEN);
c532ac
-				strncpy(event_name, save_event_name.c_str(), evt_name_len);
c532ac
-			}
c532ac
-		}
c532ac
-	}
c532ac
-	pclose(fp);
c532ac
-
c532ac
-out:
c532ac
-	if (!event_found) {
c532ac
-		cerr << err_msg << event_name << endl;
c532ac
-		cerr << "Error retrieving info for event "
c532ac
-				<< event_spec << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-	retval = event_name;
c532ac
-	return retval + ":" + count_str;
c532ac
-}
c532ac
-#endif
c532ac
-
c532ac
-static void _process_events_list(void)
c532ac
-{
c532ac
-	string cmd = OP_BINDIR;
c532ac
-	if (operf_options::evts.size() > OP_MAX_EVENTS) {
c532ac
-		cerr << "Number of events specified is greater than allowed maximum of "
c532ac
-		     << OP_MAX_EVENTS << "." << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-	cmd += "/ophelp --check-events ";
c532ac
-	for (unsigned int i = 0; i <  operf_options::evts.size(); i++) {
c532ac
-		FILE * fp;
c532ac
-		string full_cmd = cmd;
c532ac
-		string event_spec = operf_options::evts[i];
c532ac
-
c532ac
-#if PPC64_ARCH
c532ac
-		// Starting with CPU_PPC64_ARCH_V1, ppc64 events files are formatted like
c532ac
-		// other architectures, so no special handling is needed.
c532ac
-		if (cpu_type < CPU_PPC64_ARCH_V1)
c532ac
-			event_spec = _handle_powerpc_event_spec(event_spec);
c532ac
-#endif
c532ac
-
c532ac
-		if (operf_options::callgraph) {
c532ac
-			full_cmd += " --callgraph=1 ";
c532ac
-		}
c532ac
-		full_cmd += event_spec;
c532ac
-		fp = popen(full_cmd.c_str(), "r");
c532ac
-		if (fp == NULL) {
c532ac
-			cerr << "Unable to execute ophelp to get info for event "
c532ac
-			     << event_spec << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-		if (fgetc(fp) == EOF) {
c532ac
-			pclose(fp);
c532ac
-			cerr << "Error retrieving info for event "
c532ac
-			     << event_spec << endl;
c532ac
-			if (operf_options::callgraph)
c532ac
-				cerr << "Note: When doing callgraph profiling, the sample count must be"
c532ac
-				     << endl << "15 times the minimum count value for the event."  << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-		pclose(fp);
c532ac
-		char * event_str = op_xstrndup(event_spec.c_str(), event_spec.length());
c532ac
-		operf_event_t event;
c532ac
-		strncpy(event.name, strtok(event_str, ":"), OP_MAX_EVT_NAME_LEN - 1);
c532ac
-		event.count = atoi(strtok(NULL, ":"));
c532ac
-		/* Name and count are required in the event spec in order for
c532ac
-		 * 'ophelp --check-events' to pass.  But since unit mask and domain
c532ac
-		 * control bits are optional, we need to ensure the result of strtok
c532ac
-		 * is valid.
c532ac
-		 */
c532ac
-		char * info;
c532ac
-#define	_OP_UM 1
c532ac
-#define	_OP_KERNEL 2
c532ac
-#define	_OP_USER 3
c532ac
-		int place =  _OP_UM;
c532ac
-		char * endptr = NULL;
c532ac
-		event.evt_um = 0ULL;
c532ac
-		event.no_kernel = 0;
c532ac
-		event.no_user = 0;
c532ac
-		event.throttled = false;
c532ac
-		memset(event.um_name, '\0', OP_MAX_UM_NAME_LEN);
c532ac
-		while ((info = strtok(NULL, ":"))) {
c532ac
-			switch (place) {
c532ac
-			case _OP_UM:
c532ac
-				event.evt_um = strtoul(info, &endptr, 0);
c532ac
-				// If any of the UM part is not a number, then we
c532ac
-				// consider the entire part a string.
c532ac
-				if (*endptr) {
c532ac
-					event.evt_um = 0;
c532ac
-					strncpy(event.um_name, info, OP_MAX_UM_NAME_LEN - 1);
c532ac
-				}
c532ac
-				break;
c532ac
-			case _OP_KERNEL:
c532ac
-				if (atoi(info) == 0)
c532ac
-					event.no_kernel = 1;
c532ac
-				break;
c532ac
-			case _OP_USER:
c532ac
-				if (atoi(info) == 0)
c532ac
-					event.no_user = 1;
c532ac
-				break;
c532ac
-			}
c532ac
-			place++;
c532ac
-		}
c532ac
-		free(event_str);
c532ac
-		_get_event_code(&event);
c532ac
-		events.push_back(event);
c532ac
-	}
c532ac
-#if PPC64_ARCH
c532ac
-	{
c532ac
-		/* For ppc64 architecture processors prior to the introduction of
c532ac
-		 * architected_events_v1, the oprofile event code needs to be converted
c532ac
-		 * to the appropriate event code to pass to the perf_event_open syscall.
c532ac
-		 * But as of the introduction of architected_events_v1, the events
c532ac
-		 * file contains the necessary event code information, so this conversion
c532ac
-		 * step is no longer needed.
c532ac
-		 */
c532ac
-
c532ac
-		using namespace OP_perf_utils;
c532ac
-		if ((cpu_type < CPU_PPC64_ARCH_V1) && !op_convert_event_vals(&events)) {
c532ac
-			cerr << "Unable to convert all oprofile event values to perf_event values" << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-	}
c532ac
-#endif
c532ac
-}
c532ac
-
c532ac
-static void get_default_event(void)
c532ac
-{
c532ac
-	operf_event_t dft_evt;
c532ac
-	struct op_default_event_descr descr;
c532ac
-	vector<operf_event_t> tmp_events;
c532ac
-
c532ac
-
c532ac
-	op_default_event(cpu_type, &descr);
c532ac
-	if (descr.name[0] == '\0') {
c532ac
-		cerr << "Unable to find default event" << endl;
c532ac
-		exit(EXIT_FAILURE);
c532ac
-	}
c532ac
-
c532ac
-	memset(&dft_evt, 0, sizeof(dft_evt));
c532ac
-	if (operf_options::callgraph) {
c532ac
-		struct op_event * _event;
c532ac
-		op_events(cpu_type);
c532ac
-		if ((_event = find_event_by_name(descr.name, 0, 0))) {
c532ac
-			dft_evt.count = _event->min_count * CALLGRAPH_MIN_COUNT_SCALE;
c532ac
-		} else {
c532ac
-			cerr << "Error getting event info for " << descr.name << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-	} else {
c532ac
-		dft_evt.count = descr.count;
c532ac
-	}
c532ac
-	dft_evt.evt_um = descr.um;
c532ac
-	strncpy(dft_evt.name, descr.name, OP_MAX_EVT_NAME_LEN - 1);
c532ac
-	_get_event_code(&dft_evt);
c532ac
-	events.push_back(dft_evt);
c532ac
-
c532ac
-#if PPC64_ARCH
c532ac
-	{
c532ac
-		/* This section of code is for architectures such as ppc[64] for which
c532ac
-		 * the oprofile event code needs to be converted to the appropriate event
c532ac
-		 * code to pass to the perf_event_open syscall.
c532ac
-		 */
c532ac
-
c532ac
-		using namespace OP_perf_utils;
c532ac
-		if ((cpu_type < CPU_PPC64_ARCH_V1) && !op_convert_event_vals(&events)) {
c532ac
-			cerr << "Unable to convert all oprofile event values to perf_event values" << endl;
c532ac
-			exit(EXIT_FAILURE);
c532ac
-		}
c532ac
-	}
c532ac
-#endif
c532ac
-}
c532ac
-
c532ac
 static void _process_session_dir(void)
c532ac
 {
c532ac
 	if (operf_options::session_dir.empty()) {
c532ac
@@ -1752,7 +1242,7 @@ static void process_args(int argc, char * const argv[])
c532ac
 			app_args = (char **) xmalloc((sizeof *app_args) * 2);
c532ac
 			app_args[1] = NULL;
c532ac
 		}
c532ac
-		if (validate_app_name() < 0) {
c532ac
+		if (op_validate_app_name(&app_name, &app_name_SAVE) < 0) {
c532ac
 			__print_usage_and_exit(NULL);
c532ac
 		}
c532ac
 	} else {  // non_options_idx == 0
c532ac
@@ -1783,9 +1273,9 @@ static void process_args(int argc, char * const argv[])
c532ac
 
c532ac
 	if (operf_options::evts.empty()) {
c532ac
 		// Use default event
c532ac
-		get_default_event();
c532ac
+		op_get_default_event(operf_options::callgraph);
c532ac
 	} else  {
c532ac
-		_process_events_list();
c532ac
+		op_process_events_list(operf_options::evts, true, operf_options::callgraph);
c532ac
 	}
c532ac
 	op_nr_events = events.size();
c532ac
 
c532ac
@@ -1800,87 +1290,6 @@ static void process_args(int argc, char * const argv[])
c532ac
 	return;
c532ac
 }
c532ac
 
c532ac
-static int _get_cpu_for_perf_events_cap(void)
c532ac
-{
c532ac
-	int retval;
c532ac
-	string err_msg;
c532ac
-	char cpus_online[257];
c532ac
-	FILE * online_cpus;
c532ac
-	DIR *dir = NULL;
c532ac
-
c532ac
-	int total_cpus = sysconf(_SC_NPROCESSORS_ONLN);
c532ac
-	if (!total_cpus) {
c532ac
-		err_msg = "Internal Error (1): Number of online cpus cannot be determined.";
c532ac
-		retval = -1;
c532ac
-		goto error;
c532ac
-	}
c532ac
-
c532ac
-	online_cpus = fopen("/sys/devices/system/cpu/online", "r");
c532ac
-	if (!online_cpus) {
c532ac
-		err_msg = "Internal Error (2): Number of online cpus cannot be determined.";
c532ac
-		retval = -1;
c532ac
-		goto error;
c532ac
-	}
c532ac
-	memset(cpus_online, 0, sizeof(cpus_online));
c532ac
-
c532ac
-	if ( fgets(cpus_online, sizeof(cpus_online), online_cpus) == NULL) {
c532ac
-		fclose(online_cpus);
c532ac
-		err_msg = "Internal Error (3): Number of online cpus cannot be determined.";
c532ac
-		retval = -1;
c532ac
-		goto error;
c532ac
-	}
c532ac
-
c532ac
-	if (!cpus_online[0]) {
c532ac
-		fclose(online_cpus);
c532ac
-		err_msg = "Internal Error (4): Number of online cpus cannot be determined.";
c532ac
-		retval = -1;
c532ac
-		goto error;
c532ac
-
c532ac
-	}
c532ac
-	if (index(cpus_online, ',') || cpus_online[0] != '0') {
c532ac
-		// A comma in cpus_online implies a gap, which in turn implies that not all
c532ac
-		// CPUs are online.
c532ac
-		if ((dir = opendir("/sys/devices/system/cpu")) == NULL) {
c532ac
-			fclose(online_cpus);
c532ac
-			err_msg = "Internal Error (5): Number of online cpus cannot be determined.";
c532ac
-			retval = -1;
c532ac
-			goto error;
c532ac
-		} else {
c532ac
-			struct dirent *entry = NULL;
c532ac
-			retval = OP_perf_utils::op_get_next_online_cpu(dir, entry);
c532ac
-			closedir(dir);
c532ac
-		}
c532ac
-	} else {
c532ac
-		// All CPUs are available, so we just arbitrarily choose CPU 0.
c532ac
-		retval = 0;
c532ac
-	}
c532ac
-	fclose(online_cpus);
c532ac
-error:
c532ac
-	return retval;
c532ac
-}
c532ac
-
c532ac
-
c532ac
-static int _check_perf_events_cap(bool use_cpu_minus_one)
c532ac
-{
c532ac
-	/* If perf_events syscall is not implemented, the syscall below will fail
c532ac
-	 * with ENOSYS (38).  If implemented, but the processor type on which this
c532ac
-	 * program is running is not supported by perf_events, the syscall returns
c532ac
-	 * ENOENT (2).
c532ac
-	 */
c532ac
-	struct perf_event_attr attr;
c532ac
-	pid_t pid ;
c532ac
-	int cpu_to_try = use_cpu_minus_one ? -1 : _get_cpu_for_perf_events_cap();
c532ac
-	errno = 0;
c532ac
-        memset(&attr, 0, sizeof(attr));
c532ac
-        attr.size = sizeof(attr);
c532ac
-        attr.sample_type = PERF_SAMPLE_IP;
c532ac
-
c532ac
-	pid = getpid();
c532ac
-	syscall(__NR_perf_event_open, &attr, pid, cpu_to_try, -1, 0);
c532ac
-	return errno;
c532ac
-
c532ac
-}
c532ac
-
c532ac
 static void _precheck_permissions_to_samplesdir(string sampledir, bool for_current)
c532ac
 {
c532ac
 	/* Pre-check to make sure we have permission to remove old sample data
c532ac
@@ -1911,28 +1320,14 @@ static void _precheck_permissions_to_samplesdir(string sampledir, bool for_curre
c532ac
 
c532ac
 }
c532ac
 
c532ac
-static int _get_sys_value(const char * filename)
c532ac
-{
c532ac
-	char str[10];
c532ac
-	int _val = -999;
c532ac
-	FILE * fp = fopen(filename, "r");
c532ac
-	if (fp == NULL)
c532ac
-		return _val;
c532ac
-	if (fgets(str, 9, fp))
c532ac
-		sscanf(str, "%d", &_val);
c532ac
-	fclose(fp);
c532ac
-	return _val;
c532ac
-}
c532ac
-
c532ac
-
c532ac
 int main(int argc, char * const argv[])
c532ac
 {
c532ac
 	int rc;
c532ac
-	int perf_event_paranoid = _get_sys_value("/proc/sys/kernel/perf_event_paranoid");
c532ac
+	int perf_event_paranoid = op_get_sys_value("/proc/sys/kernel/perf_event_paranoid");
c532ac
 
c532ac
 	my_uid = geteuid();
c532ac
 	throttled = false;
c532ac
-	rc = _check_perf_events_cap(use_cpu_minus_one);
c532ac
+	rc = op_check_perf_events_cap(use_cpu_minus_one);
c532ac
 	if (rc == EACCES) {
c532ac
 		/* Early perf_events kernels required the cpu argument to perf_event_open
c532ac
 		 * to be '-1' when setting up to profile a single process if 1) the user is
c532ac
@@ -1948,7 +1343,7 @@ int main(int argc, char * const argv[])
c532ac
 		 */
c532ac
 		if (my_uid != 0 && perf_event_paranoid > 0) {
c532ac
 			use_cpu_minus_one = true;
c532ac
-			rc = _check_perf_events_cap(use_cpu_minus_one);
c532ac
+			rc = op_check_perf_events_cap(use_cpu_minus_one);
c532ac
 		}
c532ac
 	}
c532ac
 	if (rc == EBUSY) {
c532ac
@@ -1996,7 +1391,7 @@ int main(int argc, char * const argv[])
c532ac
 			_precheck_permissions_to_samplesdir(previous_sampledir, for_current);
c532ac
 		}
c532ac
 	}
c532ac
-	kptr_restrict = _get_sys_value("/proc/sys/kernel/kptr_restrict");
c532ac
+	kptr_restrict = op_get_sys_value("/proc/sys/kernel/kptr_restrict");
c532ac
 	end_code_t run_result;
c532ac
 	if ((run_result = _run())) {
c532ac
 		if (startApp && app_started && (run_result != APP_ABNORMAL_END)) {