From: Kan Liang <[email protected]>

The group read results from cycles/ref-cycles/TSC/ASTATE/MSTATE event
can be used to calculate the frequency, CPU Utilization and percent
performance during each sampling period.
This patch shows them in report -D.

Here is an example:

$ perf record -e
'{cycles,ref-cycles,msr/tsc/,msr/mperf/,msr/aperf/}:S' ~/tchain_edit

Here is one sample from perf report -D

1972044565107 0x3498 [0x88]: PERF_RECORD_SAMPLE(IP, 0x2): 10608/10608:
0x4005fd period: 564686 addr: 0
... sample_read:
.... group nr 5
..... id 0000000000000012, value 0000000002143901
..... id 0000000000000052, value 0000000002143896
..... id 0000000000000094, value 00000000021e443d
..... id 00000000000000d4, value 00000000021db984
..... id 0000000000000114, value 00000000021db964
..... Freq 2301 MHz
..... CPU% 98%
..... CORE_BUSY% 99%

Signed-off-by: Kan Liang <[email protected]>
---
 tools/perf/util/session.c | 41 ++++++++++++++++++++++++++++++++++-------
 tools/perf/util/session.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index ed9dc25..7971546 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -851,8 +851,16 @@ static void perf_evlist__print_tstamp(struct perf_evlist 
*evlist,
                printf("%" PRIu64 " ", sample->time);
 }
 
-static void sample_read__printf(struct perf_sample *sample, u64 read_format)
+static void sample_read__printf(struct perf_session *session,
+                               struct perf_evlist *evlist,
+                               struct perf_sample *sample,
+                               u64 read_format)
 {
+       struct perf_evsel *evsel;
+       struct perf_sample_id *sid;
+       u64 data[FREQ_PERF_MAX] = { 0 };
+       u64 cpu_max_freq = 
session->header.env.cpu_attr[PERF_HEADER_CPU_MAX_FREQ];
+
        printf("... sample_read:\n");
 
        if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
@@ -875,10 +883,26 @@ static void sample_read__printf(struct perf_sample 
*sample, u64 read_format)
                        printf("..... id %016" PRIx64
                               ", value %016" PRIx64 "\n",
                               value->id, value->value);
+
+                       sid = perf_evlist__id2sid(evlist, value->id);
+                       evsel = sid->evsel;
+                       if (evsel != NULL)
+                               
SET_FREQ_PERF_VALUE(session->header.env.msr_pmu_type,
+                                                   evsel, data, value->value);
                }
        } else
                printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
                        sample->read.one.id, sample->read.one.value);
+
+       if (HAS_FREQ(data))
+               printf("..... Freq %lu MHz\n",
+                      GET_FREQ(data, cpu_max_freq/1000));
+       if (HAS_CPU_UTIL(data))
+               printf("..... CPU%% %lu%%\n",
+                      GET_CPU_UTIL(data));
+       if (HAS_CORE_BUSY(data))
+               printf("..... CORE_BUSY%% %lu%%\n",
+                      GET_CORE_BUSY(data));
 }
 
 static void dump_event(struct perf_evlist *evlist, union perf_event *event,
@@ -899,7 +923,8 @@ static void dump_event(struct perf_evlist *evlist, union 
perf_event *event,
               event->header.size, perf_event__name(event->header.type));
 }
 
-static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
+static void dump_sample(struct perf_session *session, struct perf_evlist 
*evlist,
+                       struct perf_evsel *evsel, union perf_event *event,
                        struct perf_sample *sample)
 {
        u64 sample_type;
@@ -938,7 +963,7 @@ static void dump_sample(struct perf_evsel *evsel, union 
perf_event *event,
                printf("... transaction: %" PRIx64 "\n", sample->transaction);
 
        if (sample_type & PERF_SAMPLE_READ)
-               sample_read__printf(sample, evsel->attr.read_format);
+               sample_read__printf(session, evlist, sample, 
evsel->attr.read_format);
 }
 
 static struct machine *machines__find_for_cpumode(struct machines *machines,
@@ -1036,12 +1061,13 @@ static int
                                            &sample->read.one, machine);
 }
 
-static int machines__deliver_event(struct machines *machines,
+static int machines__deliver_event(struct perf_session *session,
                                   struct perf_evlist *evlist,
                                   union perf_event *event,
                                   struct perf_sample *sample,
                                   struct perf_tool *tool, u64 file_offset)
 {
+       struct machines *machines = &session->machines;
        struct perf_evsel *evsel;
        struct machine *machine;
 
@@ -1053,11 +1079,12 @@ static int machines__deliver_event(struct machines 
*machines,
 
        switch (event->header.type) {
        case PERF_RECORD_SAMPLE:
-               dump_sample(evsel, event, sample);
                if (evsel == NULL) {
                        ++evlist->stats.nr_unknown_id;
                        return 0;
                }
+               dump_sample(session, evlist, evsel, event, sample);
+
                if (machine == NULL) {
                        ++evlist->stats.nr_unprocessable_samples;
                        return 0;
@@ -1113,7 +1140,7 @@ static int perf_session__deliver_event(struct 
perf_session *session,
        if (ret > 0)
                return 0;
 
-       return machines__deliver_event(&session->machines, session->evlist,
+       return machines__deliver_event(session, session->evlist,
                                       event, sample, tool, file_offset);
 }
 
@@ -1179,7 +1206,7 @@ int perf_session__deliver_synth_event(struct perf_session 
*session,
        if (event->header.type >= PERF_RECORD_USER_TYPE_START)
                return perf_session__process_user_event(session, event, 0);
 
-       return machines__deliver_event(&session->machines, evlist, event, 
sample, tool, 0);
+       return machines__deliver_event(session, evlist, event, sample, tool, 0);
 }
 
 static void event_swap(union perf_event *event, bool sample_id_all)
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index b44afc7..e6e408b 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -42,6 +42,53 @@ struct perf_session {
 #define PRINT_IP_OPT_ONELINE   (1<<4)
 #define PRINT_IP_OPT_SRCLINE   (1<<5)
 
+#define PERF_MSR_TSC           0
+#define PERF_MSR_APERF         1
+#define PERF_MSR_MPERF         2
+
+enum perf_freq_perf_index {
+       FREQ_PERF_TSC           = 0,
+       FREQ_PERF_APERF         = 1,
+       FREQ_PERF_MPERF         = 2,
+       FREQ_PERF_CYCLES        = 3,
+       FREQ_PERF_REF_CYCLES    = 4,
+
+       FREQ_PERF_MAX
+};
+
+#define SET_FREQ_PERF_VALUE(msr_pmu_type, event, array, value)         \
+{                                                                      \
+       if (event->attr.type == msr_pmu_type) {                         \
+               if (event->attr.config == PERF_MSR_TSC)                 \
+                       array[FREQ_PERF_TSC] = value;                   \
+               if (event->attr.config == PERF_MSR_APERF)               \
+                       array[FREQ_PERF_APERF] = value;                 \
+               if (event->attr.config == PERF_MSR_MPERF)               \
+                       array[FREQ_PERF_MPERF] = value;                 \
+       }                                                               \
+       if (event->attr.type == PERF_TYPE_HARDWARE) {                   \
+               if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES)     \
+                       array[FREQ_PERF_CYCLES] = value;                \
+               if (event->attr.config == PERF_COUNT_HW_REF_CPU_CYCLES) \
+                       array[FREQ_PERF_REF_CYCLES] = value;            \
+       }                                                               \
+}
+
+#define HAS_FREQ(array)                                                        
\
+       ((array[FREQ_PERF_CYCLES] > 0) && (array[FREQ_PERF_REF_CYCLES] > 0))
+#define GET_FREQ(array, cpu_max_freq)                                  \
+       ((array[FREQ_PERF_CYCLES] * cpu_max_freq) / array[FREQ_PERF_REF_CYCLES])
+
+#define HAS_CPU_UTIL(array)                                            \
+       ((array[FREQ_PERF_TSC] > 0) && (array[FREQ_PERF_REF_CYCLES] > 0))
+#define GET_CPU_UTIL(array)                                            \
+       ((100 * array[FREQ_PERF_REF_CYCLES]) / array[FREQ_PERF_TSC])
+
+#define HAS_CORE_BUSY(array)                                           \
+       ((array[FREQ_PERF_APERF] > 0) && (array[FREQ_PERF_MPERF] > 0))
+#define GET_CORE_BUSY(array)                                           \
+       ((100 * array[FREQ_PERF_APERF]) / array[FREQ_PERF_MPERF])
+
 struct perf_tool;
 
 struct perf_session *perf_session__new(struct perf_data_file *file,
-- 
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to