perf_counter: Add ioctl for changing the sample period/frequency
authorPeter Zijlstra <a.p.zijlstra@chello.nl>
Tue, 2 Jun 2009 14:46:57 +0000 (16:46 +0200)
committerIngo Molnar <mingo@elte.hu>
Tue, 2 Jun 2009 19:45:32 +0000 (21:45 +0200)
Reported-by: Stephane Eranian <eranian@googlemail.com>
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: John Kacur <jkacur@redhat.com>
LKML-Reference: <new-submission>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
include/linux/perf_counter.h
kernel/perf_counter.c

index c046f7d97cfafce277520f591834fbef605dc900..45bdd3b95d3e7029c8a97d1115f531b30a9e4089 100644 (file)
@@ -164,10 +164,11 @@ struct perf_counter_hw_event {
 /*
  * Ioctls that can be done on a perf counter fd:
  */
-#define PERF_COUNTER_IOC_ENABLE                _IOW('$', 0, u32)
-#define PERF_COUNTER_IOC_DISABLE       _IOW('$', 1, u32)
-#define PERF_COUNTER_IOC_REFRESH       _IOW('$', 2, u32)
-#define PERF_COUNTER_IOC_RESET         _IOW('$', 3, u32)
+#define PERF_COUNTER_IOC_ENABLE                _IO ('$', 0)
+#define PERF_COUNTER_IOC_DISABLE       _IO ('$', 1)
+#define PERF_COUNTER_IOC_REFRESH       _IO ('$', 2)
+#define PERF_COUNTER_IOC_RESET         _IO ('$', 3)
+#define PERF_COUNTER_IOC_PERIOD                _IOW('$', 4, u64)
 
 enum perf_counter_ioc_flags {
        PERF_IOC_FLAG_GROUP             = 1U << 0,
index 3f11a2bc6c799268ba01fb20538e0fffc292ff00..abe2f3b6c4242aaad8d45577376b4e92545779c1 100644 (file)
@@ -1604,6 +1604,43 @@ static void perf_counter_for_each(struct perf_counter *counter,
        mutex_unlock(&counter->child_mutex);
 }
 
+static int perf_counter_period(struct perf_counter *counter, u64 __user *arg)
+{
+       struct perf_counter_context *ctx = counter->ctx;
+       unsigned long size;
+       int ret = 0;
+       u64 value;
+
+       if (!counter->hw_event.sample_period)
+               return -EINVAL;
+
+       size = copy_from_user(&value, arg, sizeof(value));
+       if (size != sizeof(value))
+               return -EFAULT;
+
+       if (!value)
+               return -EINVAL;
+
+       spin_lock_irq(&ctx->lock);
+       if (counter->hw_event.freq) {
+               if (value > sysctl_perf_counter_limit) {
+                       ret = -EINVAL;
+                       goto unlock;
+               }
+
+               counter->hw_event.sample_freq = value;
+       } else {
+               counter->hw_event.sample_period = value;
+               counter->hw.sample_period = value;
+
+               perf_log_period(counter, value);
+       }
+unlock:
+       spin_unlock_irq(&ctx->lock);
+
+       return ret;
+}
+
 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct perf_counter *counter = file->private_data;
@@ -1623,6 +1660,10 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
        case PERF_COUNTER_IOC_REFRESH:
                return perf_counter_refresh(counter, arg);
+
+       case PERF_COUNTER_IOC_PERIOD:
+               return perf_counter_period(counter, (u64 __user *)arg);
+
        default:
                return -ENOTTY;
        }