perf/x86: add Intel RAPL PMU support
Subsystems (2)
v5 cover perf/x86: add Intel RAPL PMU support
This patch adds a new uncore PMU to expose the Intel RAPL (Running Average Power Limit) energy consumption counters. Up to 3 counters, each counting a single RAPL event are exposed.
The RAPL counters are available on Intel SandyBridge, IvyBridge, Haswell. The server processors add a 3rd counter to measure DRAM power consumption.
The following events are available and exposed in sysfs:
- power/energy-cores: power consumption of all cores on socket
- power/energy-pkg : power consumption of all cores + LLC cache
- power/energy-dram : power consumption of DRAM (servers only)
The RAPL PMU is uncore by nature and is implemented such that it only works in system-wide mode. Measuring only one CPU per socket is sufficient.
The counters all count in the same unit. The perf_events API exposes all RAPL counters as 64-bit integers counting in unit of 1/2^32 Joules (about 0.23 nJ). User level tools must convert the counts by multiplying them by the scaling factor exposed in the correponding event .scale file in sysfs to obtain a value expressed in Joules. The reason for this approach is that the kernel avoids doing floating point math whenever possible because it is expensive (user floating-point state must be saved). The method used avoids kernel floating-point and does not incur any precision loss. Thanks to PeterZ for suggesting this approach.
To convert the raw count in Watts: W = C * 2.3 / (1e10 * time)
The kernel exposes both the scaling factor and the unit (Joules) in sysfs:
$ ls -1 /sys/devices/power/events/energy-* /sys/devices/power/events/energy-cores /sys/devices/power/events/energy-cores.scale /sys/devices/power/events/energy-cores.unit /sys/devices/power/events/energy-pkg /sys/devices/power/events/energy-pkg.scale /sys/devices/power/events/energy-pkg.unit
$ cat /sys/devices/power/events/energy-cores.scale 2.3283064365386962890625e-10
$ cat cat /sys/devices/power/events/energy-cores.unit Joules
RAPL PMU is a new standalone PMU which registers with the perf_event core subsystem. The PMU type (attr->type) is dynamically allocated and is available from /sys/device/rapl/type.
Sampling is not supported by the RAPL PMU. There is no privilege level filtering either.
The PMU exports a cpumask in /sys/devices/power/cpumask. It is used by perf to ensure only one instance of each RAPL event is measured per processor socket. Hotplug CPU is also supported.
The perf stat infrastrructure is enhanced to show events units. It also applies the scaling factor. As such, perf stat prints RAPL events in Joules (and not increments of 0.23 nJ):
# perf stat -a -e power/energy-pkg/,power/energy-cores/,cycles -I 1000 sleep 1000
# time counts unit events
1.000282860 2.51 Joules power/energy-pkg/
1.000282860 0.31 Joules power/energy-cores/
1.000282860 37765378 cyclesThe patch adds a hrtimer to poll the counters given that they do no interrupt on overflow. Hardware counters are 32-bit wide.
In v2, we add the locking necesarry to protect the rapl_pmu struct. We also add a description at the top of the file. We check for Intel only processor. We improved the data layout of the rapl_pmu struct. We also lifted the restriction of the number of instances of RAPL counters that can be active at the same time. RAPL is free running counters, so ought to be able to measure events as many times as necessary in parallel via multiple tools. There is never multiplexing among RAPL events.
In v3, we have renamed the event to be more generic power/* instead of rapl/*. We have modified perf stat to print the event with the unit and scaling factors.
In v4, we integrate the feedback from Jiri and rebase to 3.12-rc7+ from tip.git.
In v5, we export the full scaling factor to increase prescision. In the perf tool, we changed the way the .unit and .scale syfs entries are parsed. Thank to Jiri for this contribution on this. We also fix a couple of printf() issues with perf stat and units. Now, we print no unit symbol when the event has no unit (was ? before). Patch is relative to 3.12 from tip.git.
Thanks to all contributors to this patch series: PeterZ, Jiri, Maria, Arnaldo, Andi, Ingo.
Supported CPUs: SandyBridge, IvyBridge, Haswell.
Stephane Eranian (4):
perf: add active_entry list head to struct perf_event perf stat: add event unit and scale support perf,x86: add Intel RAPL PMU support perf,x86: add RAPL hrtimer support
arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/perf_event_intel_rapl.c | 721 +++++++++++++++++++++++++++ include/linux/perf_event.h | 5 +- kernel/events/core.c | 1 + tools/perf/builtin-stat.c | 114 +++-- tools/perf/util/evsel.c | 2 + tools/perf/util/evsel.h | 3 + tools/perf/util/parse-events.c | 28 +- tools/perf/util/pmu.c | 144 +++++- tools/perf/util/pmu.h | 3 +- 10 files changed, 978 insertions(+), 45 deletions(-) create mode 100644 arch/x86/kernel/cpu/perf_event_intel_rapl.c
Signature · 1 line
1.7.9.5
PATCH v5 1/4 perf: add active_entry list head to struct perf_event
This patch adds a new field to the struct perf_event. It is intended to be used to chain events which are active (enabled). It helps in the hardware layer for PMUs which do not have actual counter restrictions, i.e., free running read-only counters. Active events are chained as opposed to being tracked via the counter they use.
To save space we use a union with hlist_entry as both are mutually exclusive (suggested by Jiri Olsa).
--- include/linux/perf_event.h | 5 ++++- kernel/events/core.c | 1 + 2 files changed, 5 insertions(+), 1 deletion(-)
include/linux/perf_event.h +4 -1
@@ -319,7 +319,10 @@ struct perf_event {*/ struct list_head migrate_entry; -struct hlist_node hlist_entry; +union { +struct hlist_node hlist_entry; +struct list_head active_entry; + };int nr_siblings;int group_flags;struct perf_event *group_leader;
kernel/events/core.c +1 -0
@@ -6663,6 +6663,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,INIT_LIST_HEAD (&event->event_entry);INIT_LIST_HEAD (&event->sibling_list);INIT_LIST_HEAD (&event->rb_entry); +INIT_LIST_HEAD (&event->active_entry);init_waitqueue_head (&event->waitq);init_irq_work (&event->pending, perf_pending_event);
Signature · 1 line
1.7.9.5
PATCH v5 2/4 perf stat: add event unit and scale support
In reply to Stephane EranianThis patch adds perf stat support for handling event units and scales as exported by the kernel.
The kernel can export PMU events actual unit and scaling factor via sysfs:
$ ls -1 /sys/devices/power/events/energy-* /sys/devices/power/events/energy-cores /sys/devices/power/events/energy-cores.scale /sys/devices/power/events/energy-cores.unit /sys/devices/power/events/energy-pkg /sys/devices/power/events/energy-pkg.scale /sys/devices/power/events/energy-pkg.unit $ cat /sys/devices/power/events/energy-cores.scale 2.3283064365386962890625e-10 $ cat cat /sys/devices/power/events/energy-cores.unit Joules
This patch modifies the pmu event alias code to check for the presence of the .unit and .scale files to load the corresponding values. They are then used by perf stat transparently:
# perf stat -a -e power/energy-pkg/,power/energy-cores/,cycles -I 1000 sleep 1000
# time counts unit events
1.000214717 3.07 Joules power/energy-pkg/ [100.00%]
1.000214717 0.53 Joules power/energy-cores/
1.000214717 12965028 cycles [100.00%]
2.000749289 3.01 Joules power/energy-pkg/
2.000749289 0.52 Joules power/energy-cores/
2.000749289 15817043 cyclesWhen the event does not have an explicit unit exported by the kernel, nothing is printed. In csv output mode, there will be an empty field.
Special thanks to Jiri for providing the supporting code in the parser to trigger reading of the scale and unit files.
--- tools/perf/builtin-stat.c | 114 ++++++++++++++++++++++--------- tools/perf/util/evsel.c | 2 + tools/perf/util/evsel.h | 3 + tools/perf/util/parse-events.c | 28 +++++--- tools/perf/util/pmu.c | 144 +++++++++++++++++++++++++++++++++++++++- tools/perf/util/pmu.h | 3 +- 6 files changed, 251 insertions(+), 43 deletions(-)
tools/perf/builtin-stat.c +84 -30
@@ -138,6 +138,7 @@ static const char *post_cmd = NULL;static bool sync_run = false;static unsigned int interval =0 ;static unsigned int initial_delay =0 ; +static unsigned int unit_width =4 ;/* strlen("unit") */ static bool forever = false;static struct timespec ref_time;static struct cpu_map *aggr_map;
@@ -462,17 +463,17 @@ static void print_interval(void)if (num_print_interval ==0 && !csv_output) {switch (aggr_mode) {case AGGR_SOCKET : -fprintf (output,"# time socket cpus counts events\n" ); +fprintf (output,"# time socket cpus counts %*s events\n" , unit_width,"unit" );break ;case AGGR_CORE : -fprintf (output,"# time core cpus counts events\n" ); +fprintf (output,"# time core cpus counts %*s events\n" , unit_width,"unit" );break ;case AGGR_NONE : -fprintf (output,"# time CPU counts events\n" ); +fprintf (output,"# time CPU counts %*s events\n" , unit_width,"unit" );break ;case AGGR_GLOBAL :default : -fprintf (output,"# time counts events\n" ); +fprintf (output,"# time counts %*s events\n" , unit_width,"unit" ); } }
@@ -517,6 +518,7 @@ static int __run_perf_stat(int argc, const char **argv)unsigned long long t0, t1;struct perf_evsel *counter;struct timespec ts; +size_t l;int status =0 ;const bool forks = (argc >0 );
@@ -566,6 +568,10 @@ static int __run_perf_stat(int argc, const char **argv)return -1 ; } counter->supported = true; + + l =strlen (counter->unit); +if (l > unit_width) + unit_width = l; }if (perf_evlist__apply_filters (evsel_list)) {
@@ -705,14 +711,25 @@ static void aggr_printout(struct perf_evsel *evsel, int id, int nr)static void nsec_printout (int cpu,int nr,struct perf_evsel *evsel,double avg) {double msecs = avg /1e6 ; -const char *fmt = csv_output ?"%.6f%s%s" :"%18.6f%s%-25s" ; +const char *fmt_v, *fmt_n;char name[25 ]; + fmt_v = csv_output ?"%.6f%s" :"%18.6f%s" ; + fmt_n = csv_output ?"%s" :"%-25s" ; +aggr_printout (evsel, cpu, nr);scnprintf (name,sizeof (name),"%s%s" ,perf_evsel__name (evsel), csv_output ?"" :" (msec)" ); -fprintf (output, fmt, msecs, csv_sep, name); + +fprintf (output, fmt_v, msecs, csv_sep); + +if (csv_output) +fprintf (output,"%s%s" , evsel->unit, csv_sep); +else +fprintf (output,"%-*s%s" , unit_width, evsel->unit, csv_sep); + +fprintf (output, fmt_n, name);if (evsel->cgrp)fprintf (output,"%s%s" , csv_sep, evsel->cgrp->name);
@@ -909,21 +926,31 @@ static void print_ll_cache_misses(int cpu,static void abs_printout (int cpu,int nr,struct perf_evsel *evsel,double avg) {double total, ratio =0.0 , total2; +double sc = evsel->scale;const char *fmt; -if (csv_output) - fmt ="%.0f%s%s" ; -else if (big_num) - fmt ="%'18.0f%s%-25s" ; -else - fmt ="%18.0f%s%-25s" ; +if (csv_output) { + fmt = sc !=1.0 ?"%.2f%s" :"%.0f%s" ; + }else { +if (big_num) + fmt = sc !=1.0 ?"%'18.2f%s" :"%'18.0f%s" ; +else + fmt = sc !=1.0 ?"%18.2f%s" :"%18.0f%s" ; + }aggr_printout (evsel, cpu, nr);if (aggr_mode ==AGGR_GLOBAL ) cpu =0 ; -fprintf (output, fmt, avg, csv_sep,perf_evsel__name (evsel)); +fprintf (output, fmt, avg, csv_sep); + +if (evsel->unit) +fprintf (output,"%-*s%s" , + csv_output ?0 : unit_width, + evsel->unit, csv_sep); + +fprintf (output,"%-*s" , csv_output ?0 :25 ,perf_evsel__name (evsel));if (evsel->cgrp)fprintf (output,"%s%s" , csv_sep, evsel->cgrp->name);
@@ -942,7 +969,10 @@ static void abs_printout(int cpu, int nr, struct perf_evsel *evsel, double avg)if (total && avg) { ratio = total / avg; -fprintf (output,"\n # %5.2f stalled cycles per insn" , ratio); +fprintf (output,"\n" ); +if (aggr_mode ==AGGR_NONE ) +fprintf (output," " ); +fprintf (output," # %5.2f stalled cycles per insn" , ratio); } } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
@@ -1062,6 +1092,7 @@ static void print_aggr(char *prefix) {struct perf_evsel *counter;int cpu, cpu2, s, s2, id, nr; +double uval;u64 ena, run, val;if (!(aggr_map || aggr_get_id))
@@ -1088,11 +1119,17 @@ static void print_aggr(char *prefix)if (run ==0 || ena ==0 ) {aggr_printout (counter, id, nr); -fprintf (output,"%*s%s%*s" , +fprintf (output,"%*s%s" , csv_output ?0 :18 , counter->supported ?CNTR_NOT_COUNTED :CNTR_NOT_SUPPORTED , - csv_sep, - csv_output ?0 :-24 , + csv_sep); + +fprintf (output,"%-*s%s" , + csv_output ?0 : unit_width, + counter->unit, csv_sep); + +fprintf (output,"%*s" , + csv_output ?0 :-25 ,perf_evsel__name (counter));if (counter->cgrp)
@@ -1102,11 +1139,12 @@ static void print_aggr(char *prefix)fputc ('\n' , output);continue ; } + uval = val * counter->scale;if (nsec_counter (counter)) -nsec_printout (id, nr, counter, val); +nsec_printout (id, nr, counter, uval);else -abs_printout (id, nr, counter, val); +abs_printout (id, nr, counter, uval);if (!csv_output) {print_noise (counter,1.0 );
@@ -1129,16 +1167,21 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)struct perf_stat *ps = counter->priv;double avg =avg_stats (&ps->res_stats[0 ]);int scaled = counter->counts->scaled; +double uval;if (prefix)fprintf (output,"%s" , prefix);if (scaled ==-1 ) { -fprintf (output,"%*s%s%*s" , +fprintf (output,"%*s%s" , csv_output ?0 :18 , counter->supported ?CNTR_NOT_COUNTED :CNTR_NOT_SUPPORTED , - csv_sep, - csv_output ?0 :-24 , + csv_sep); +fprintf (output,"%-*s%s" , + csv_output ?0 : unit_width, + counter->unit, csv_sep); +fprintf (output,"%*s" , + csv_output ?0 :-25 ,perf_evsel__name (counter));if (counter->cgrp)
@@ -1148,10 +1191,12 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)return ; } + uval = avg * counter->scale; +if (nsec_counter (counter)) -nsec_printout (-1 ,0 , counter, avg); +nsec_printout (-1 ,0 , counter, uval);else -abs_printout (-1 ,0 , counter, avg); +abs_printout (-1 ,0 , counter, uval);print_noise (counter, avg);
@@ -1178,6 +1223,7 @@ static void print_counter_aggr(struct perf_evsel *counter, char *prefix)static void print_counter (struct perf_evsel *counter,char *prefix) {u64 ena, run, val; +double uval;int cpu;for (cpu =0 ; cpu <perf_evsel__nr_cpus (counter); cpu++) {
@@ -1189,14 +1235,20 @@ static void print_counter(struct perf_evsel *counter, char *prefix)fprintf (output,"%s" , prefix);if (run ==0 || ena ==0 ) { -fprintf (output,"CPU%*d%s%*s%s%*s" , +fprintf (output,"CPU%*d%s%*s%s" , csv_output ?0 :-4 ,perf_evsel__cpus (counter)->map[cpu], csv_sep, csv_output ?0 :18 , counter->supported ?CNTR_NOT_COUNTED :CNTR_NOT_SUPPORTED , - csv_sep, - csv_output ?0 :-24 , -perf_evsel__name (counter)); + csv_sep); + +fprintf (output,"%-*s%s" , + csv_output ?0 : unit_width, + counter->unit, csv_sep); + +fprintf (output,"%*s" , + csv_output ?0 :-25 , +perf_evsel__name (counter));if (counter->cgrp) fprintf(output, "%s%s",
@@ -1206,10 +1258,12 @@ static void print_counter(struct perf_evsel *counter, char *prefix)continue ; } + uval = val * counter->scale; +if (nsec_counter (counter)) -nsec_printout (cpu,0 , counter, val); +nsec_printout (cpu,0 , counter, uval);else -abs_printout (cpu,0 , counter, val); +abs_printout (cpu,0 , counter, uval);if (!csv_output) {print_noise (counter,1.0 );
tools/perf/util/evsel.c +2 -0
@@ -162,6 +162,8 @@ void perf_evsel__init(struct perf_evsel *evsel, evsel->idx = idx; evsel->attr = *attr; evsel->leader = evsel; + evsel->unit ="" ; + evsel->scale =1.0 ;INIT_LIST_HEAD (&evsel->node);hists__init (&evsel->hists); evsel->sample_size =__perf_evsel__sample_size (attr->sample_type);
tools/perf/util/evsel.h +3 -0
@@ -68,6 +68,8 @@ struct perf_evsel {u32 ids;struct hists hists;char *name; +double scale; +const char *unit;struct event_format *tp_format;union {void *priv;
@@ -130,6 +132,7 @@ extern const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX];int __perf_evsel__hw_cache_type_op_res_name (u8 type,u8 op,u8 result,char *bf,size_t size);const char *perf_evsel__name (struct perf_evsel *evsel); +const char *perf_evsel__group_name (struct perf_evsel *evsel);int perf_evsel__group_desc (struct perf_evsel *evsel,char *buf,size_t size);
tools/perf/util/parse-events.c +19 -9
@@ -269,9 +269,10 @@ const char *event_type(int type) -static int __add_event (struct list_head *list,int *idx, -struct perf_event_attr *attr, -char *name,struct cpu_map *cpus) +static struct perf_evsel * +__add_event (struct list_head *list,int *idx, +struct perf_event_attr *attr, +char *name,struct cpu_map *cpus) {struct perf_evsel *evsel;
@@ -279,19 +280,19 @@ static int __add_event(struct list_head *list, int *idx, evsel =perf_evsel__new (attr, (*idx)++);if (!evsel) -return -ENOMEM ; +return NULL ; evsel->cpus = cpus;if (name) evsel->name =strdup (name);list_add_tail (&evsel->node, list); -return 0 ; +return evsel; }static int add_event (struct list_head *list,int *idx,struct perf_event_attr *attr,char *name) { -return __add_event (list, idx, attr, name,NULL ); +return __add_event (list, idx, attr, name,NULL ) ?0 : -ENOMEM ; }static int parse_aliases (char *str,const char *names[][PERF_EVSEL__MAX_ALIASES ],int size)
@@ -633,6 +634,9 @@ int parse_events_add_pmu(struct list_head *list, int *idx, {struct perf_event_attr attr;struct perf_pmu *pmu; +struct perf_evsel *evsel; +char *unit; +double scale; pmu =perf_pmu__find (name);if (!pmu)
@@ -640,7 +644,7 @@ int parse_events_add_pmu(struct list_head *list, int *idx,memset (&attr,0 ,sizeof (attr)); -if (perf_pmu__check_alias (pmu, head_config)) +if (perf_pmu__check_alias (pmu, head_config, &unit, &scale))return -EINVAL ;/*
@@ -652,8 +656,14 @@ int parse_events_add_pmu(struct list_head *list, int *idx,if (perf_pmu__config (pmu, &attr, head_config))return -EINVAL ; -return __add_event (list, idx, &attr,pmu_event_name (head_config), - pmu->cpus); + evsel =__add_event (list, idx, &attr,pmu_event_name (head_config), + pmu->cpus); +if (evsel) { + evsel->unit = unit; + evsel->scale = scale; + } + +return evsel ?0 : -ENOMEM ; } int parse_events__modifier_group(struct list_head *list,
tools/perf/util/pmu.c +141 -3
214 lines not on this page · raw message
tools/perf/util/pmu.h +2 -1
@@ -28,7 +28,8 @@ int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,int perf_pmu__config_terms (struct list_head *formats,struct perf_event_attr *attr,struct list_head *head_terms); -int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms); +int perf_pmu__check_alias (struct perf_pmu *pmu,struct list_head *head_terms, +char **unit,double *scale);struct list_head *perf_pmu__alias (struct perf_pmu *pmu,struct list_head *head_terms);int perf_pmu_wrap (void );
Signature · 1 line
1.7.9.5
PATCH v5 3/4 perf,x86: add Intel RAPL PMU support
In reply to Stephane EranianThis patch adds a new uncore PMU to expose the Intel RAPL energy consumption counters. Up to 3 counters, each counting a particular RAPL event are exposed.
The RAPL counters are available on Intel SandyBridge, IvyBridge, Haswell. The server skus add a 3rd counter.
The following events are available and exposed in sysfs:
- power/energy-cores: power consumption of all cores on socket
- power/energy-pkg: power consumption of all cores + LLc cache
- power/energy-dram: power consumption of DRAM (servers only)
For each event both the unit (Joules) and scale (2^-32 J) is exposed in sysfs for use by perf stat and other tools. Files are:
/sys/devices/power/events/energy-*.unit /sys/devices/power/events/energy-*.scale
The RAPL PMU is uncore by nature and is implemented such that it only works in system-wide mode. Measuring only one CPU per socket is sufficient. The /sys/devices/power/cpumask file can be used by tools to figure out which CPUs to monitor by default. For instance, on a 2-socket system, 2 CPUs (one on each socket) will be shown.
All the counters measure in the same unit (exposed via sysfs). The perf_events API exposes all RAPL counters as 64-bit integers counting in unit of 1/2^32 Joules (about 0.23 nJ). User level tools must convert the counts by multiplying them by 2^-32 to obtain Joules. The reason for this is that the kernel avoids doing floating point math whenever possible because it is expensive (user floating-point state must be saved). The method used avoids kernel floating-point usage. There is no loss of precision. Thanks to PeterZ for suggesting this approach.
To convert the raw count in Watts:
W = C * 2.3 / (1e10 * time)
or ldexp(C, -32).
RAPL PMU is a new standalone PMU which registers with the perf_event core subsystem. The PMU type (attr->type) is dynamically allocated and is available from /sys/device/power/type.
Sampling is not supported by the RAPL PMU. There is no privilege level filtering either.
Thanks to Maria Dimakopoulou for her contributions to this patch especially on the floating-point aspects.
--- arch/x86/kernel/cpu/Makefile | 2 +- arch/x86/kernel/cpu/perf_event_intel_rapl.c | 655 +++++++++++++++++++++++++++ 2 files changed, 656 insertions(+), 1 deletion(-) create mode 100644 arch/x86/kernel/cpu/perf_event_intel_rapl.c
arch/x86/kernel/cpu/Makefile +1 -1
@@ -36,7 +36,7 @@ obj-$(CONFIG_CPU_SUP_AMD) += perf_event_amd_iommu.o endif obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_p6.o perf_event_knc.o perf_event_p4.o obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o -obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_uncore.o +obj-$(CONFIG_CPU_SUP_INTEL) += perf_event_intel_uncore.o perf_event_intel_rapl.o endif
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +655 -0
655 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Re: [PATCH v5 3/4] perf,x86: add Intel RAPL PMU support
Quoted 16 lines · Stephane Eranian
(quoted diff) +16
-0+static int rapl_cpu_dying(int cpu) +{ + struct rapl_pmu *pmu = per_cpu(rapl_pmu, cpu); + struct perf_event *event, *tmp; + + if (!pmu) + return 0; + + spin_lock(&rapl_hotplug_lock); + + /* + * stop all syswide RAPL events on that CPU + * as a consequence also stops the hrtimer + */ + list_for_each_entry_safe(event, tmp, &pmu->active_list, active_entry) { + rapl_pmu_event_stop(event, PERF_EF_UPDATE);
Review
Quoted 10 lines
(quoted diff) +10
-0+ } + + per_cpu(rapl_pmu, cpu) = NULL; + + if (atomic_dec_and_test(&pmu->refcnt)) + kfree(pmu); + + spin_unlock(&rapl_hotplug_lock); + return 0; +}
Could you do an add-on patch similar to the below -- no need to respin the entire series once again for this.
---
commit 22cc4ccf63e10e361531bf61e6e6c96c53a2f665
Author: Yan, Zheng <[email protected]> Date: Tue Apr 16 19:51:05 2013 +0800
perf/x86: Avoid kfree() in CPU_{STARTING,DYING}
On -rt kfree() can schedule, but CPU_{STARTING,DYING} should be atomic. So use a list to defer kfree until CPU_{ONLINE,DEAD}.
arch/x86/kernel/cpu/perf_event_intel_uncore.c +25 -3
@@ -2622,6 +2622,21 @@ static void __init uncore_pci_exit(void) } } +/* CPU hot plug/unplug are serialized by cpu_add_remove_lock mutex */ +static LIST_HEAD (boxes_to_free); + +static void __cpuinit uncore_kfree_boxes (void ) +{ +struct intel_uncore_box *box; + +while (!list_empty (&boxes_to_free)) { + box =list_entry (boxes_to_free.next, + struct intel_uncore_box, list); +list_del (&box->list); +kfree (box); + } +} +static void __cpuinit uncore_cpu_dying (int cpu) {struct intel_uncore_type *type;
@@ -2636,7 +2651,7 @@ static void __cpuinit uncore_cpu_dying(int cpu) box = *per_cpu_ptr (pmu->box, cpu); *per_cpu_ptr (pmu->box, cpu) =NULL ;if (box &&atomic_dec_and_test (&box->refcnt)) -kfree (box); +list_add (&box->list, &boxes_to_free); } } }
@@ -2666,8 +2681,11 @@ static int __cpuinit uncore_cpu_starting(int cpu)if (exist && exist->phys_id == phys_id) {atomic_inc (&exist->refcnt); *per_cpu_ptr (pmu->box, cpu) = exist; -kfree (box); - box =NULL ; +if (box) { +list_add (&box->list, + &boxes_to_free); + box =NULL ; + }break ; } }
@@ -2806,6 +2824,10 @@ static intcase CPU_DYING :uncore_cpu_dying (cpu);break ; +case CPU_ONLINE : +case CPU_DEAD : +uncore_kfree_boxes (); +break ;default :break ; }
Peter Zijlstra
maintainerRe: [PATCH v5 3/4] perf,x86: add Intel RAPL PMU support
In reply to Stephane EranianQuoted 82 lines · Stephane Eranian
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +76
-0+static DEFINE_SPINLOCK (rapl_hotplug_lock); +static void rapl_exit_cpu (int cpu) +{ +int i, phys_id =topology_physical_package_id (cpu); + +spin_lock (&rapl_hotplug_lock); +spin_unlock (&rapl_hotplug_lock); +} + +static void rapl_init_cpu (int cpu) +{ +int i, phys_id =topology_physical_package_id (cpu); + +spin_lock (&rapl_hotplug_lock); +spin_unlock (&rapl_hotplug_lock); +} +static int rapl_cpu_starting (int cpu) +{ +struct rapl_pmu *pmu2; +struct rapl_pmu *pmu1 =per_cpu (rapl_pmu, cpu); +int i, phys_id =topology_physical_package_id (cpu); + +if (pmu1) +return 0 ; + +spin_lock (&rapl_hotplug_lock); +spin_unlock (&rapl_hotplug_lock); +return 0 ; +} + +static int rapl_cpu_dying (int cpu) +{ +struct rapl_pmu *pmu =per_cpu (rapl_pmu, cpu); +struct perf_event *event, *tmp; + +if (!pmu) +return 0 ; + +spin_lock (&rapl_hotplug_lock); +spin_unlock (&rapl_hotplug_lock); +return 0 ; +} + +static int rapl_cpu_notifier (struct notifier_block *self, +unsigned long action,void *hcpu) +{ +unsigned int cpu = (long )hcpu; + +/* allocate/free data structure for uncore box */ +switch (action & ~CPU_TASKS_FROZEN ) { +case CPU_UP_PREPARE : +rapl_cpu_prepare (cpu); +break ; +case CPU_STARTING : +rapl_cpu_starting (cpu); +break ; +case CPU_UP_CANCELED : +case CPU_DYING : +rapl_cpu_dying (cpu); +break ; +case CPU_ONLINE : +kfree (per_cpu (rapl_pmu_kfree, cpu)); +per_cpu (rapl_pmu_kfree, cpu) =NULL ; +break ; +case CPU_DEAD : +per_cpu (rapl_pmu, cpu) =NULL ; +break ; +default : +break ; + } + +/* select the cpu that collects uncore events */ +switch (action & ~CPU_TASKS_FROZEN ) { +case CPU_DOWN_FAILED : +case CPU_STARTING : +rapl_init_cpu (cpu);
arch/x86/kernel/cpu/perf_event_intel_rapl.c line 575
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +10
-0+break ; +case CPU_DOWN_PREPARE : +rapl_exit_cpu (cpu); +break ; +default : +break ; + } + +return NOTIFY_OK ; +}
What's the point of that rapl_hotplug_lock? It appears all methods its used from are called from hotplug notifiers and those are fully serialized by cpu_add_remove_lock.
Peter Zijlstra
maintainerRe: [PATCH v5 3/4] perf,x86: add Intel RAPL PMU support
In reply to Stephane EranianQuoted 34 lines · Stephane Eranian
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +32
-0+static void rapl_exit_cpu (int cpu) +{ +int i, phys_id =topology_physical_package_id (cpu); + +spin_lock (&rapl_hotplug_lock); + +/* if CPU not in RAPL mask, nothing to do */ +if (!cpumask_test_and_clear_cpu (cpu, &rapl_cpu_mask)) + goto skip; + +/* find a new cpu on same package */ +for_each_online_cpu (i) { +if (i == cpu || i ==0 ) +continue ; +if (phys_id ==topology_physical_package_id (i)) { +cpumask_set_cpu (i, &rapl_cpu_mask); +break ; + } + } + +WARN_ON (cpumask_empty (&rapl_cpu_mask)); +skip: +spin_unlock (&rapl_hotplug_lock); +} +static int rapl_cpu_notifier (struct notifier_block *self, +unsigned long action,void *hcpu) +{ +/* select the cpu that collects uncore events */ +switch (action & ~CPU_TASKS_FROZEN ) { +case CPU_DOWN_FAILED : +case CPU_STARTING : +rapl_init_cpu (cpu);
arch/x86/kernel/cpu/perf_event_intel_rapl.c line 476
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +10
-0+break ; +case CPU_DOWN_PREPARE : +rapl_exit_cpu (cpu); +break ; +default : +break ; + } + +return NOTIFY_OK ; +}
All other uncore drivers call perf_pmu_migrate_context() when the CPU the events are registered on goes away.. ?
Re: [PATCH v5 3/4] perf,x86: add Intel RAPL PMU support
Peter,
Working on it. Found other bugs and cleanups, so I will respin the entire series.
It will be easier that way.
Quoted 48 lines · Peter Zijlstra
Quoted 34 lines · Stephane Eranian
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +32
-0+static void rapl_exit_cpu (int cpu) +{ +int i, phys_id =topology_physical_package_id (cpu); + +spin_lock (&rapl_hotplug_lock); + +/* if CPU not in RAPL mask, nothing to do */ +if (!cpumask_test_and_clear_cpu (cpu, &rapl_cpu_mask)) + goto skip; + +/* find a new cpu on same package */ +for_each_online_cpu (i) { +if (i == cpu || i ==0 ) +continue ; +if (phys_id ==topology_physical_package_id (i)) { +cpumask_set_cpu (i, &rapl_cpu_mask); +break ; + } + } + +WARN_ON (cpumask_empty (&rapl_cpu_mask)); +skip: +spin_unlock (&rapl_hotplug_lock); +} +static int rapl_cpu_notifier (struct notifier_block *self, +unsigned long action,void *hcpu) +{ +/* select the cpu that collects uncore events */ +switch (action & ~CPU_TASKS_FROZEN ) { +case CPU_DOWN_FAILED : +case CPU_STARTING : +rapl_init_cpu (cpu);arch/x86/kernel/cpu/perf_event_intel_rapl.c line 476
Quoted 10 lines
arch/x86/kernel/cpu/perf_event_intel_rapl.c new file +10
-0+break ; +case CPU_DOWN_PREPARE : +rapl_exit_cpu (cpu); +break ; +default : +break ; + } + +return NOTIFY_OK ; +}All other uncore drivers call
perf_pmu_migrate_context()when the CPU the events are registered on goes away.. ?
PATCH v5 4/4 perf,x86: add RAPL hrtimer support
In reply to Stephane EranianThe RAPL PMU counters do not interrupt on overflow. Therefore, the kernel needs to poll the counters to avoid missing an overflow. This patch adds the hrtimer code to do this.
The timer internval is calculated at boot time based on the power unit used by the HW.
--- arch/x86/kernel/cpu/perf_event_intel_rapl.c | 75 +++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/perf_event_intel_rapl.c +70 -5
@@ -92,11 +92,13 @@ static struct kobj_attribute format_attr_##_var = \struct rapl_pmu {spinlock_t lock; -atomic_t refcnt;int hw_unit;/* 1/2^hw_unit Joule */ -int phys_id; -int n_active;/* number of active events */ +struct hrtimer hrtimer;struct list_head active_list; +ktime_t timer_interval;/* in ktime_t unit */ +int n_active;/* number of active events */ +int phys_id; +atomic_t refcnt; };static struct pmu rapl_pmu_class;
@@ -161,6 +163,47 @@ static u64 rapl_event_update(struct perf_event *event)return new_raw_count; } +static void rapl_start_hrtimer (struct rapl_pmu *pmu) +{ +__hrtimer_start_range_ns (&pmu->hrtimer, + pmu->timer_interval,0 , +HRTIMER_MODE_REL_PINNED ,0 ); +} + +static void rapl_stop_hrtimer (struct rapl_pmu *pmu) +{ +hrtimer_cancel (&pmu->hrtimer); +} + +static enum hrtimer_restart rapl_hrtimer_handle (struct hrtimer *hrtimer) +{ + struct rapl_pmu *pmu = container_of(hrtimer, struct rapl_pmu, hrtimer); +struct perf_event *event; +unsigned long flags; + +if (!pmu->n_active) +return HRTIMER_NORESTART ; + +spin_lock_irqsave (&pmu->lock, flags); + +list_for_each_entry (event, &pmu->active_list, active_entry) { +rapl_event_update (event); + } + +spin_unlock_irqrestore (&pmu->lock, flags); + +hrtimer_forward_now (&pmu->hrtimer, pmu->timer_interval); + +return HRTIMER_RESTART ; +} + +static void rapl_hrtimer_init (struct rapl_pmu *pmu) +{ +hrtimer_init (&pmu->hrtimer,CLOCK_MONOTONIC ,HRTIMER_MODE_REL ); + pmu->hrtimer.function = rapl_hrtimer_handle; +} + +static void __rapl_pmu_event_start (struct rapl_pmu *pmu,struct perf_event *event) {
@@ -174,6 +217,8 @@ static void __rapl_pmu_event_start(struct rapl_pmu *pmu,local64_set (&event->hw.prev_count,rapl_read_counter (event)); pmu->n_active++; +if (pmu->n_active ==1 ) +rapl_start_hrtimer (pmu); }static void rapl_pmu_event_start (struct perf_event *event,int mode)
@@ -198,6 +243,8 @@ static void rapl_pmu_event_stop(struct perf_event *event, int mode)if (!(hwc->state &PERF_HES_STOPPED )) {WARN_ON_ONCE (pmu->n_active <=0 ); pmu->n_active--; +if (pmu->n_active ==0 ) +rapl_stop_hrtimer (pmu);list_del (&event->active_entry);
@@ -443,6 +490,7 @@ static int rapl_cpu_prepare(int cpu) {struct rapl_pmu *pmu =per_cpu (rapl_pmu, cpu);int phys_id =topology_physical_package_id (cpu); +u64 ms;if (pmu)return 0 ;
@@ -468,6 +516,20 @@ static int rapl_cpu_prepare(int cpu)rdmsrl (MSR_RAPL_POWER_UNIT , pmu->hw_unit); pmu->hw_unit = (pmu->hw_unit >>8 ) &0x1FULL ; +/* +* use reference of 200W for scaling the timeout +* to avoid missing counter overflows. +* 200W = 200 Joules/sec +* divide interval by 2 to avoid lockstep (2 * 100) +* if hw unit is 32, then we use 2 ms 1/200/2 +*/ +if (pmu->hw_unit <32 ) + ms =1000 * (1ULL << (32 - pmu->hw_unit -1 )) / (2 *100 ); +else + ms =2 ; + + pmu->timer_interval =ms_to_ktime (ms); +/* set RAPL pmu for this cpu for now */ per_cpu (rapl_pmu_kfree, cpu) =NULL ;per_cpu (rapl_pmu, cpu) = pmu;
@@ -629,6 +691,7 @@ static int __init rapl_pmu_init(void) }rapl_cpu_prepare (cpu);cpumask_set_cpu (cpu, &rapl_cpu_mask); +rapl_hrtimer_init (per_cpu (rapl_pmu, cpu)); }perf_cpu_notifier (rapl_cpu_notifier);
@@ -644,9 +707,11 @@ static int __init rapl_pmu_init(void)pr_info ("RAPL PMU detected, hw unit 2^-%d Joules," " API unit is 2^-32 Joules," -" %d fixed counters\n" , +" %d fixed counters" +" %llu ms ovfl timer\n" , pmu->hw_unit, -hweight32 (rapl_cntr_mask)); +hweight32 (rapl_cntr_mask), +ktime_to_ms (pmu->timer_interval));put_online_cpus ();
Signature · 1 line
1.7.9.5