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