Blame SOURCES/oprofile-rhbz1121205.patch

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