Back to home page

LXR

 
 

    


0001 /*
0002  * Copyright 2010 ARM Ltd.
0003  * Copyright 2012 Advanced Micro Devices, Inc., Robert Richter
0004  *
0005  * Perf-events backend for OProfile.
0006  */
0007 #include <linux/perf_event.h>
0008 #include <linux/platform_device.h>
0009 #include <linux/oprofile.h>
0010 #include <linux/slab.h>
0011 
0012 /*
0013  * Per performance monitor configuration as set via oprofilefs.
0014  */
0015 struct op_counter_config {
0016     unsigned long count;
0017     unsigned long enabled;
0018     unsigned long event;
0019     unsigned long unit_mask;
0020     unsigned long kernel;
0021     unsigned long user;
0022     struct perf_event_attr attr;
0023 };
0024 
0025 static int oprofile_perf_enabled;
0026 static DEFINE_MUTEX(oprofile_perf_mutex);
0027 
0028 static struct op_counter_config *counter_config;
0029 static DEFINE_PER_CPU(struct perf_event **, perf_events);
0030 static int num_counters;
0031 
0032 /*
0033  * Overflow callback for oprofile.
0034  */
0035 static void op_overflow_handler(struct perf_event *event,
0036             struct perf_sample_data *data, struct pt_regs *regs)
0037 {
0038     int id;
0039     u32 cpu = smp_processor_id();
0040 
0041     for (id = 0; id < num_counters; ++id)
0042         if (per_cpu(perf_events, cpu)[id] == event)
0043             break;
0044 
0045     if (id != num_counters)
0046         oprofile_add_sample(regs, id);
0047     else
0048         pr_warning("oprofile: ignoring spurious overflow "
0049                 "on cpu %u\n", cpu);
0050 }
0051 
0052 /*
0053  * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
0054  * settings in counter_config. Attributes are created as `pinned' events and
0055  * so are permanently scheduled on the PMU.
0056  */
0057 static void op_perf_setup(void)
0058 {
0059     int i;
0060     u32 size = sizeof(struct perf_event_attr);
0061     struct perf_event_attr *attr;
0062 
0063     for (i = 0; i < num_counters; ++i) {
0064         attr = &counter_config[i].attr;
0065         memset(attr, 0, size);
0066         attr->type      = PERF_TYPE_RAW;
0067         attr->size      = size;
0068         attr->config        = counter_config[i].event;
0069         attr->sample_period = counter_config[i].count;
0070         attr->pinned        = 1;
0071     }
0072 }
0073 
0074 static int op_create_counter(int cpu, int event)
0075 {
0076     struct perf_event *pevent;
0077 
0078     if (!counter_config[event].enabled || per_cpu(perf_events, cpu)[event])
0079         return 0;
0080 
0081     pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
0082                           cpu, NULL,
0083                           op_overflow_handler, NULL);
0084 
0085     if (IS_ERR(pevent))
0086         return PTR_ERR(pevent);
0087 
0088     if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
0089         perf_event_release_kernel(pevent);
0090         pr_warning("oprofile: failed to enable event %d "
0091                 "on CPU %d\n", event, cpu);
0092         return -EBUSY;
0093     }
0094 
0095     per_cpu(perf_events, cpu)[event] = pevent;
0096 
0097     return 0;
0098 }
0099 
0100 static void op_destroy_counter(int cpu, int event)
0101 {
0102     struct perf_event *pevent = per_cpu(perf_events, cpu)[event];
0103 
0104     if (pevent) {
0105         perf_event_release_kernel(pevent);
0106         per_cpu(perf_events, cpu)[event] = NULL;
0107     }
0108 }
0109 
0110 /*
0111  * Called by oprofile_perf_start to create active perf events based on the
0112  * perviously configured attributes.
0113  */
0114 static int op_perf_start(void)
0115 {
0116     int cpu, event, ret = 0;
0117 
0118     for_each_online_cpu(cpu) {
0119         for (event = 0; event < num_counters; ++event) {
0120             ret = op_create_counter(cpu, event);
0121             if (ret)
0122                 return ret;
0123         }
0124     }
0125 
0126     return ret;
0127 }
0128 
0129 /*
0130  * Called by oprofile_perf_stop at the end of a profiling run.
0131  */
0132 static void op_perf_stop(void)
0133 {
0134     int cpu, event;
0135 
0136     for_each_online_cpu(cpu)
0137         for (event = 0; event < num_counters; ++event)
0138             op_destroy_counter(cpu, event);
0139 }
0140 
0141 static int oprofile_perf_create_files(struct dentry *root)
0142 {
0143     unsigned int i;
0144 
0145     for (i = 0; i < num_counters; i++) {
0146         struct dentry *dir;
0147         char buf[4];
0148 
0149         snprintf(buf, sizeof buf, "%d", i);
0150         dir = oprofilefs_mkdir(root, buf);
0151         oprofilefs_create_ulong(dir, "enabled", &counter_config[i].enabled);
0152         oprofilefs_create_ulong(dir, "event", &counter_config[i].event);
0153         oprofilefs_create_ulong(dir, "count", &counter_config[i].count);
0154         oprofilefs_create_ulong(dir, "unit_mask", &counter_config[i].unit_mask);
0155         oprofilefs_create_ulong(dir, "kernel", &counter_config[i].kernel);
0156         oprofilefs_create_ulong(dir, "user", &counter_config[i].user);
0157     }
0158 
0159     return 0;
0160 }
0161 
0162 static int oprofile_perf_setup(void)
0163 {
0164     raw_spin_lock(&oprofilefs_lock);
0165     op_perf_setup();
0166     raw_spin_unlock(&oprofilefs_lock);
0167     return 0;
0168 }
0169 
0170 static int oprofile_perf_start(void)
0171 {
0172     int ret = -EBUSY;
0173 
0174     mutex_lock(&oprofile_perf_mutex);
0175     if (!oprofile_perf_enabled) {
0176         ret = 0;
0177         op_perf_start();
0178         oprofile_perf_enabled = 1;
0179     }
0180     mutex_unlock(&oprofile_perf_mutex);
0181     return ret;
0182 }
0183 
0184 static void oprofile_perf_stop(void)
0185 {
0186     mutex_lock(&oprofile_perf_mutex);
0187     if (oprofile_perf_enabled)
0188         op_perf_stop();
0189     oprofile_perf_enabled = 0;
0190     mutex_unlock(&oprofile_perf_mutex);
0191 }
0192 
0193 #ifdef CONFIG_PM
0194 
0195 static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
0196 {
0197     mutex_lock(&oprofile_perf_mutex);
0198     if (oprofile_perf_enabled)
0199         op_perf_stop();
0200     mutex_unlock(&oprofile_perf_mutex);
0201     return 0;
0202 }
0203 
0204 static int oprofile_perf_resume(struct platform_device *dev)
0205 {
0206     mutex_lock(&oprofile_perf_mutex);
0207     if (oprofile_perf_enabled && op_perf_start())
0208         oprofile_perf_enabled = 0;
0209     mutex_unlock(&oprofile_perf_mutex);
0210     return 0;
0211 }
0212 
0213 static struct platform_driver oprofile_driver = {
0214     .driver     = {
0215         .name       = "oprofile-perf",
0216     },
0217     .resume     = oprofile_perf_resume,
0218     .suspend    = oprofile_perf_suspend,
0219 };
0220 
0221 static struct platform_device *oprofile_pdev;
0222 
0223 static int __init init_driverfs(void)
0224 {
0225     int ret;
0226 
0227     ret = platform_driver_register(&oprofile_driver);
0228     if (ret)
0229         return ret;
0230 
0231     oprofile_pdev = platform_device_register_simple(
0232                 oprofile_driver.driver.name, 0, NULL, 0);
0233     if (IS_ERR(oprofile_pdev)) {
0234         ret = PTR_ERR(oprofile_pdev);
0235         platform_driver_unregister(&oprofile_driver);
0236     }
0237 
0238     return ret;
0239 }
0240 
0241 static void exit_driverfs(void)
0242 {
0243     platform_device_unregister(oprofile_pdev);
0244     platform_driver_unregister(&oprofile_driver);
0245 }
0246 
0247 #else
0248 
0249 static inline int  init_driverfs(void) { return 0; }
0250 static inline void exit_driverfs(void) { }
0251 
0252 #endif /* CONFIG_PM */
0253 
0254 void oprofile_perf_exit(void)
0255 {
0256     int cpu, id;
0257     struct perf_event *event;
0258 
0259     for_each_possible_cpu(cpu) {
0260         for (id = 0; id < num_counters; ++id) {
0261             event = per_cpu(perf_events, cpu)[id];
0262             if (event)
0263                 perf_event_release_kernel(event);
0264         }
0265 
0266         kfree(per_cpu(perf_events, cpu));
0267     }
0268 
0269     kfree(counter_config);
0270     exit_driverfs();
0271 }
0272 
0273 int __init oprofile_perf_init(struct oprofile_operations *ops)
0274 {
0275     int cpu, ret = 0;
0276 
0277     ret = init_driverfs();
0278     if (ret)
0279         return ret;
0280 
0281     num_counters = perf_num_counters();
0282     if (num_counters <= 0) {
0283         pr_info("oprofile: no performance counters\n");
0284         ret = -ENODEV;
0285         goto out;
0286     }
0287 
0288     counter_config = kcalloc(num_counters,
0289             sizeof(struct op_counter_config), GFP_KERNEL);
0290 
0291     if (!counter_config) {
0292         pr_info("oprofile: failed to allocate %d "
0293                 "counters\n", num_counters);
0294         ret = -ENOMEM;
0295         num_counters = 0;
0296         goto out;
0297     }
0298 
0299     for_each_possible_cpu(cpu) {
0300         per_cpu(perf_events, cpu) = kcalloc(num_counters,
0301                 sizeof(struct perf_event *), GFP_KERNEL);
0302         if (!per_cpu(perf_events, cpu)) {
0303             pr_info("oprofile: failed to allocate %d perf events "
0304                     "for cpu %d\n", num_counters, cpu);
0305             ret = -ENOMEM;
0306             goto out;
0307         }
0308     }
0309 
0310     ops->create_files   = oprofile_perf_create_files;
0311     ops->setup      = oprofile_perf_setup;
0312     ops->start      = oprofile_perf_start;
0313     ops->stop       = oprofile_perf_stop;
0314     ops->shutdown       = oprofile_perf_stop;
0315     ops->cpu_type       = op_name_from_perf_id();
0316 
0317     if (!ops->cpu_type)
0318         ret = -ENODEV;
0319     else
0320         pr_info("oprofile: using %s\n", ops->cpu_type);
0321 
0322 out:
0323     if (ret)
0324         oprofile_perf_exit();
0325 
0326     return ret;
0327 }