1 Upstream-Status: Accepted
3 From 36028035555297695f52e856f21920012fd64f79 Mon Sep 17 00:00:00 2001
4 From: Maynard Johnson <maynardj@us.ibm.com>
5 Date: Fri, 11 Jan 2013 13:29:57 -0600
6 Subject: [PATCH] Allow ppc64 events to be specified with or without _GRP<n>
9 All events for IBM PowerPC server processors (except CYCLES) have
10 a _GRP<n> suffix. This is because the legacy opcontrol profiler
11 can only profile events in the same group (i.e., having the same
12 _GRP<n> suffix). But operf has no such restriction because it
13 can multiplex events; thus, so we should allow the user to pass
14 event names without the _GRP<n> suffix.
16 Signed-off-by: Maynard Johnson <maynardj@us.ibm.com>
18 doc/operf.1.in | 6 +++
19 doc/oprofile.xml | 12 +++++-
20 pe_profiling/operf.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++++
22 4 files changed, 127 insertions(+), 2 deletions(-)
24 diff --git a/doc/operf.1.in b/doc/operf.1.in
25 index b109324..03027ca 100644
28 @@ -110,6 +110,12 @@ be specified using the symbolic name. If no unit mask is specified, 0x0 will be
32 +On IBM PowerPC systems, events may be specified with or without the
34 +suffix. If no group number suffix is given, one will be automatically
35 +assigned; thus, OProfile post-processing tools will always show real event
36 +names that include the group number suffix.
38 When no event specification is given, the default event for the running
39 processor type will be used for profiling.
41 diff --git a/doc/oprofile.xml b/doc/oprofile.xml
42 index 0ae2b0b..0f74726 100644
43 --- a/doc/oprofile.xml
44 +++ b/doc/oprofile.xml
45 @@ -1106,10 +1106,18 @@ shown by the output of <command>ophelp</command>. Unit masks with "extra:" para
46 specified using the symbolic name.
49 -When using legacy mode <command>opcontrol</command> on PowerPC platforms, all events specified must be in the same group;
50 +When using legacy mode <command>opcontrol</command> on IBM PowerPC platforms, all events specified must be in the same group;
51 i.e., the group number appended to the event name (e.g. <constant><<emphasis>some-event-name</emphasis>>_GRP9
52 </constant>) must be the same.
56 +When profiling with <command>operf</command> on IBM PowerPC platforms, the above restriction
57 +regarding the same group number does not apply, and events may be
58 +specified with or without the group number suffix. If no group number suffix is given, one will be automatically
59 +assigned; thus, OProfile post-processing tools will always show real event
60 +names that include the group number suffix.
64 If OProfile is using timer-interrupt mode, there is no event configuration possible.
66 diff --git a/pe_profiling/operf.cpp b/pe_profiling/operf.cpp
67 index 4416b29..a776e71 100644
68 --- a/pe_profiling/operf.cpp
69 +++ b/pe_profiling/operf.cpp
70 @@ -1146,6 +1146,108 @@ static void _get_event_code(operf_event_t * event)
71 event->evt_code = config;
74 +#if (defined(__powerpc__) || defined(__powerpc64__))
75 +/* All ppc64 events (except CYCLES) have a _GRP<n> suffix. This is
76 + * because the legacy opcontrol profiler can only profile events in
77 + * the same group (i.e., having the same _GRP<n> suffix). But operf
78 + * can multiplex events, so we should allow the user to pass event
79 + * names without the _GRP<n> suffix.
81 + * If event name is not CYCLES or does not have a _GRP<n> suffix,
82 + * we'll call ophelp and scan the list of events, searching for one
83 + * that matches up to the _GRP<n> suffix. If we don't find a match,
84 + * then we'll exit with the expected error message for invalid event name.
86 +static string _handle_powerpc_event_spec(string event_spec)
89 + char line[MAX_INPUT];
91 + string evt, retval, err_msg;
92 + size_t evt_name_len;
93 + bool first_non_cyc_evt_found = false;
94 + bool event_found = false;
95 + char event_name[OP_MAX_EVT_NAME_LEN], event_spec_str[OP_MAX_EVT_NAME_LEN + 20], * count_str;
96 + string cmd = OP_BINDIR;
99 + strncpy(event_spec_str, event_spec.c_str(), event_spec.length() + 1);
101 + strncpy(event_name, strtok(event_spec_str, ":"), OP_MAX_EVT_NAME_LEN);
102 + count_str = strtok(NULL, ":");
104 + err_msg = "Invalid count for event ";
108 + if (!strcmp("CYCLES", event_name)) {
109 + event_found = true;
114 + // Need to make sure the event name truly has a _GRP<n> suffix.
115 + grp_pos = evt.rfind("_GRP");
116 + if ((grp_pos != string::npos) && ((evt = evt.substr(grp_pos, string::npos))).length() > 4) {
117 + unsigned long value;
119 + value = strtoul(evt.substr(4, string::npos).c_str(), &end, 0);
120 + if (end && (*end == '\0')) {
121 + // Valid group number found after _GRP, so we can skip to the end.
122 + event_found = true;
127 + // If we get here, it implies the user passed a non-CYCLES event without a GRP suffix.
128 + // Lets try to find a valid suffix for it.
129 + fp = popen(cmd.c_str(), "r");
131 + cerr << "Unable to execute ophelp to get info for event "
132 + << event_spec << endl;
133 + exit(EXIT_FAILURE);
135 + evt_name_len = strlen(event_name);
136 + err_msg = "Cannot find event ";
137 + while (fgets(line, MAX_INPUT, fp)) {
138 + if (!first_non_cyc_evt_found) {
139 + if (!strncmp(line, "PM_", 3))
140 + first_non_cyc_evt_found = true;
144 + if (line[0] == ' ' || line[0] == '\t')
146 + if (!strncmp(line, event_name, evt_name_len)) {
147 + // Found a potential match. Check if it's a perfect match.
148 + string save_event_name = event_name;
149 + size_t full_evt_len = index(line, ':') - line;
150 + memset(event_name, '\0', OP_MAX_EVT_NAME_LEN);
151 + strncpy(event_name, line, full_evt_len);
152 + string candidate = event_name;
153 + if (candidate.rfind("_GRP") == evt_name_len) {
154 + event_found = true;
157 + memset(event_name, '\0', OP_MAX_EVT_NAME_LEN);
158 + strncpy(event_name, save_event_name.c_str(), evt_name_len);
165 + if (!event_found) {
166 + cerr << err_msg << event_name << endl;
167 + cerr << "Error retrieving info for event "
168 + << event_spec << endl;
169 + exit(EXIT_FAILURE);
171 + retval = event_name;
172 + return retval + ":" + count_str;
176 static void _process_events_list(void)
178 string cmd = OP_BINDIR;
179 @@ -1154,6 +1256,11 @@ static void _process_events_list(void)
181 string full_cmd = cmd;
182 string event_spec = operf_options::evts[i];
184 +#if (defined(__powerpc__) || defined(__powerpc64__))
185 + event_spec = _handle_powerpc_event_spec(event_spec);
188 if (operf_options::callgraph) {
189 full_cmd += " --callgraph=1 ";
191 diff --git a/utils/ophelp.c b/utils/ophelp.c
192 index 53a5dde..63895c8 100644
195 @@ -652,6 +652,10 @@ int main(int argc, char const * argv[])
196 case CPU_PPC64_POWER7:
197 case CPU_PPC64_IBM_COMPAT_V1:
199 + "When using operf, events may be specified without a '_GRP<n>' suffix.\n"
200 + "If _GRP<n> (i.e., group number) is not specified, one will be automatically\n"
201 + "selected for use by the profiler. OProfile post-processing tools will\n"
202 + "always show real event names that include the group number suffix.\n\n"
203 "Documentation for IBM POWER7 can be obtained at:\n"
204 "http://www.power.org/events/Power7/\n"
205 "No public performance monitoring doc available for older processors.\n";