Blame SOURCES/oprofile-rhbz1121205.patch

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