pending sched and perf_counter patches
Subsystems (2)
cover pending sched and perf_counter patches
PATCH 1/7 sched: rt: document the risk of small values in the bandwidth settings
Thomas noted that we should disallow sysctl_sched_rt_runtime == 0 for (! RT_GROUP) since the root group always has some RT tasks in it.
Further, update the documentation to inspire clue.
--- Documentation/scheduler/sched-rt-group.txt | 18 ++++++++++++++++++ kernel/sched.c | 7 +++++++ 2 files changed, 25 insertions(+)
Documentation/scheduler/sched-rt-group.txt +18 -0
@@ -4,6 +4,7 @@ CONTENTS ======== +0. WARNING 1. Overview 1.1 The problem 1.2 The solution
@@ -14,6 +15,23 @@ CONTENTS 3. Future plans +0. WARNING +========== + + Fiddling with these settings can result in an unstable system, the knobs are + root only and assumes root knows what he is doing. + +Most notable: + + * very small values in sched_rt_period_us can result in an unstable + system when the period is smaller than either the available hrtimer + resolution, or the time it takes to handle the budget refresh itself. + + * very small values in sched_rt_runtime_us can result in an unstable + system when the runtime is so small the system has difficulty making + forward progress (NOTE: the migration thread and kstopmachine both + are real-time processes). + 1. Overview ===========
kernel/sched.c +7 -0
@@ -10024,6 +10024,13 @@ static int sched_rt_global_constraints(vif (sysctl_sched_rt_period <=0 )return -EINVAL ; +/* +* There's always some RT tasks in the root group +* -- migration, kstopmachine etc.. +*/ +if (sysctl_sched_rt_runtime ==0 ) +return -EBUSY ; +spin_lock_irqsave (&def_rt_bandwidth.rt_runtime_lock, flags);for_each_possible_cpu (i) {struct rt_rq *rt_rq = &cpu_rq (i)->rt;
[tip:sched/core] sched: rt: document the risk of small values in the bandwidth settings
Commit-ID: 60aa605dfce2976e54fa76e805ab0f221372d4d9 Gitweb: http://git.kernel.org/tip/60aa605dfce2976e54fa76e805ab0f221372d4d9 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:21 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:07:57 +0200
sched: rt: document the risk of small values in the bandwidth settings
Thomas noted that we should disallow sysctl_sched_rt_runtime == 0 for (!RT_GROUP) since the root group always has some RT tasks in it.
Further, update the documentation to inspire clue.
[ Impact: exclude corner-case sysctl_sched_rt_runtime value ]
LKML-Reference: <[email protected]>
--- Documentation/scheduler/sched-rt-group.txt | 18 ++++++++++++++++++ kernel/sched.c | 7 +++++++ 2 files changed, 25 insertions(+), 0 deletions(-)
Documentation/scheduler/sched-rt-group.txt +18 -0
@@ -4,6 +4,7 @@ CONTENTS ======== +0. WARNING 1. Overview 1.1 The problem 1.2 The solution
@@ -14,6 +15,23 @@ CONTENTS 3. Future plans +0. WARNING +========== + + Fiddling with these settings can result in an unstable system, the knobs are + root only and assumes root knows what he is doing. + +Most notable: + + * very small values in sched_rt_period_us can result in an unstable + system when the period is smaller than either the available hrtimer + resolution, or the time it takes to handle the budget refresh itself. + + * very small values in sched_rt_runtime_us can result in an unstable + system when the runtime is so small the system has difficulty making + forward progress (NOTE: the migration thread and kstopmachine both + are real-time processes). + 1. Overview ===========
kernel/sched.c +7 -0
@@ -9917,6 +9917,13 @@ static int sched_rt_global_constraints(void)if (sysctl_sched_rt_period <=0 )return -EINVAL ; +/* +* There's always some RT tasks in the root group +* -- migration, kstopmachine etc.. +*/ +if (sysctl_sched_rt_runtime ==0 ) +return -EBUSY ; +spin_lock_irqsave (&def_rt_bandwidth.rt_runtime_lock, flags);for_each_possible_cpu (i) {struct rt_rq *rt_rq = &cpu_rq (i)->rt;
PATCH 2/7 perf_counter: uncouple data_head updates from wakeups
In reply to Peter ZijlstraKeep data_head up-to-date irrespective of notifications, this fixes the case where you disable a counter and don't get a notification for the last few pending events, and also allows polling usage.
--- include/linux/perf_counter.h | 4 +++- kernel/perf_counter.c | 20 +++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-)
kernel/perf_counter.c +9 -11
@@ -1696,7 +1696,6 @@ struct perf_output_handle {struct perf_mmap_data *data;unsigned int offset;unsigned int head; -int wakeup;int nmi;int overflow;int locked;
@@ -1752,8 +1751,7 @@ static void perf_output_unlock(struct pestruct perf_mmap_data *data = handle->data;int head, cpu; -if (handle->wakeup) - data->wakeup_head = data->head; + data->done_head = data->head;if (!handle->locked) goto out;
@@ -1764,13 +1762,11 @@ again:* before we publish the new head, matched by a rmb() in userspace when * reading this position. */ -while ((head =atomic_xchg (&data->wakeup_head,0 ))) { +while ((head =atomic_xchg (&data->done_head,0 ))) data->user_page->data_head = head; - handle->wakeup =1 ; - }/* -* NMI can happen here, which means we can miss a wakeup_head update. +* NMI can happen here, which means we can miss a done_head update. */ cpu =atomic_xchg (&data->lock,0 );
@@ -1779,7 +1775,7 @@ again:/* * Therefore we have to validate we did not indeed do so. */ -if (unlikely (atomic_read (&data->wakeup_head))) { +if (unlikely (atomic_read (&data->done_head))) {/* * Since we had it locked, we can lock it again. */
@@ -1789,7 +1785,7 @@ again: goto again; } -if (handle->wakeup) +if (atomic_xchg (&data->wakeup,0 ))perf_output_wakeup (handle); out:local_irq_restore (handle->flags);
@@ -1824,7 +1820,9 @@ static int perf_output_begin(struct perf handle->offset = offset; handle->head = head; - handle->wakeup = (offset >>PAGE_SHIFT ) != (head >>PAGE_SHIFT ); + +if ((offset >>PAGE_SHIFT ) != (head >>PAGE_SHIFT )) +atomic_set (&data->wakeup,1 );return 0 ;
@@ -1882,7 +1880,7 @@ static void perf_output_end(struct perf_int events =atomic_inc_return (&data->events);if (events >= wakeup_events) {atomic_sub (wakeup_events, &data->events); - handle->wakeup =1 ; +atomic_set (&data->wakeup,1 ); } }
include/linux/perf_counter.h +3 -1
@@ -362,9 +362,11 @@ struct perf_mmap_data {atomic_t head;/* write position */ atomic_t events;/* event limit */ -atomic_t wakeup_head;/* completed head */ +atomic_t done_head;/* completed head */ atomic_t lock;/* concurrent writes */ +atomic_t wakeup;/* needs a wakeup */ +struct perf_counter_mmap_page *user_page;void *data_pages[0 ]; };
[tip:perfcounters/core] perf_counter: uncouple data_head updates from wakeups
Commit-ID: c66de4a5be7913247bd83d79168f8e4420c9cfbc Gitweb: http://git.kernel.org/tip/c66de4a5be7913247bd83d79168f8e4420c9cfbc Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:22 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:18:30 +0200
perf_counter: uncouple data_head updates from wakeups
Keep data_head up-to-date irrespective of notifications. This fixes the case where you disable a counter and don't get a notification for the last few pending events, and it also allows polling usage.
[ Impact: increase precision of perfcounter mmap-ed fields ]
LKML-Reference: <[email protected]>
--- include/linux/perf_counter.h | 4 +++- kernel/perf_counter.c | 20 +++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-)
include/linux/perf_counter.h +3 -1
@@ -362,9 +362,11 @@ struct perf_mmap_data {atomic_t head;/* write position */ atomic_t events;/* event limit */ -atomic_t wakeup_head;/* completed head */ +atomic_t done_head;/* completed head */ atomic_t lock;/* concurrent writes */ +atomic_t wakeup;/* needs a wakeup */ +struct perf_counter_mmap_page *user_page;void *data_pages[0 ]; };
kernel/perf_counter.c +9 -11
@@ -1696,7 +1696,6 @@ struct perf_output_handle {struct perf_mmap_data *data;unsigned int offset;unsigned int head; -int wakeup;int nmi;int overflow;int locked;
@@ -1752,8 +1751,7 @@ static void perf_output_unlock(struct perf_output_handle *handle)struct perf_mmap_data *data = handle->data;int head, cpu; -if (handle->wakeup) - data->wakeup_head = data->head; + data->done_head = data->head;if (!handle->locked) goto out;
@@ -1764,13 +1762,11 @@ again:* before we publish the new head, matched by a rmb() in userspace when * reading this position. */ -while ((head =atomic_xchg (&data->wakeup_head,0 ))) { +while ((head =atomic_xchg (&data->done_head,0 ))) data->user_page->data_head = head; - handle->wakeup =1 ; - }/* -* NMI can happen here, which means we can miss a wakeup_head update. +* NMI can happen here, which means we can miss a done_head update. */ cpu =atomic_xchg (&data->lock,0 );
@@ -1779,7 +1775,7 @@ again:/* * Therefore we have to validate we did not indeed do so. */ -if (unlikely (atomic_read (&data->wakeup_head))) { +if (unlikely (atomic_read (&data->done_head))) {/* * Since we had it locked, we can lock it again. */
@@ -1789,7 +1785,7 @@ again: goto again; } -if (handle->wakeup) +if (atomic_xchg (&data->wakeup,0 ))perf_output_wakeup (handle); out:local_irq_restore (handle->flags);
@@ -1824,7 +1820,9 @@ static int perf_output_begin(struct perf_output_handle *handle, handle->offset = offset; handle->head = head; - handle->wakeup = (offset >>PAGE_SHIFT ) != (head >>PAGE_SHIFT ); + +if ((offset >>PAGE_SHIFT ) != (head >>PAGE_SHIFT )) +atomic_set (&data->wakeup,1 );return 0 ;
@@ -1882,7 +1880,7 @@ static void perf_output_end(struct perf_output_handle *handle)int events =atomic_inc_return (&data->events);if (events >= wakeup_events) {atomic_sub (wakeup_events, &data->events); - handle->wakeup =1 ; +atomic_set (&data->wakeup,1 ); } }
PATCH 3/7 perf_counter: ioctl(PERF_COUNTER_IOC_RESET)
In reply to Peter ZijlstraProvide a way to reset an existing counter.
Similar to read() it doesn't collapse pending child counters.
--- include/linux/perf_counter.h | 1 + kernel/perf_counter.c | 8 ++++++++ 2 files changed, 9 insertions(+)
include/linux/perf_counter.h +1 -0
@@ -160,6 +160,7 @@ struct perf_counter_hw_event {#define PERF_COUNTER_IOC_ENABLE _IO ('$', 0)#define PERF_COUNTER_IOC_DISABLE _IO ('$', 1)#define PERF_COUNTER_IOC_REFRESH _IOW('$', 2, u32) +#define PERF_COUNTER_IOC_RESET _IO ('$', 3)/* * Structure of the page that can be mapped via mmap
kernel/perf_counter.c +8 -0
@@ -1288,6 +1288,11 @@ static unsigned int perf_poll(struct filreturn events; } +static void perf_counter_reset (struct perf_counter *counter) +{ +atomic_set (&counter->count,0 ); +} +static long perf_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {struct perf_counter *counter = file->private_data;
@@ -1303,6 +1308,9 @@ static long perf_ioctl(struct file *filecase PERF_COUNTER_IOC_REFRESH :perf_counter_refresh (counter, arg);break ; +case PERF_COUNTER_IOC_RESET : +perf_counter_reset (counter); +break ;default : err = -ENOTTY ; }
Re: [PATCH 3/7] perf_counter: ioctl(PERF_COUNTER_IOC_RESET)
Hi Peter,
kernel/perf_counter.c static unsigned int perf_poll(struct fil · line 1295
Quoted 5 lines · Peter Zijlstra
kernel/perf_counter.c +5
-0+static void perf_counter_reset (struct perf_counter *counter) +{ +atomic_set (&counter->count,0 ); +} +
Thanks for posting a patch for this issue.
As Ingo said, I think the hardware counter needs to be reset as well as the value saved in the perf_counter struct.
Regards,
- Corey
Corey Ashford
Software Engineer
IBM Linux Technology Center, Linux Toolchain
[email protected]
Re: [PATCH 3/7] perf_counter: ioctl(PERF_COUNTER_IOC_RESET)
Quoted 13 lines · Corey Ashford
Hi Peter,
Review
Quoted 5 lines · Peter Zijlstra
(quoted diff) +5
-0+static void perf_counter_reset(struct perf_counter *counter) +{ + atomic_set(&counter->count, 0); +} +Thanks for posting a patch for this issue.
Quoted 3 lines
As Ingo said, I think the hardware counter needs to be reset as well as the value saved in the
perf_counterstruct.
I don't think that's needed, we calculate a delta between prev_count and the current read and use that to increment counter->count. Therefore when we reset counter->count we should not need to touch the hardware counter.
However, I do think we need the below, first read the hardware counter to ensure that delta spoken of above is as close to zero as possible when we reset.
And update the user-page bits.
---
kernel/perf_counter.c +2 -0
@@ -1299,7 +1299,9 @@ static unsigned int perf_poll(struct filstatic void perf_counter_reset (struct perf_counter *counter) { + (void )perf_counter_read (counter);atomic_set (&counter->count,0 ); +perf_counter_update_userpage (counter); }static long perf_ioctl (struct file *file,unsigned int cmd,unsigned long arg)
Re: [PATCH 3/7] perf_counter: ioctl(PERF_COUNTER_IOC_RESET)
Quoted 35 lines · Peter Zijlstra
Quoted 13 lines · Corey Ashford
Hi Peter,
Review
Quoted 5 lines · Peter Zijlstra
(quoted diff) +5
-0+static void perf_counter_reset(struct perf_counter *counter) +{ + atomic_set(&counter->count, 0); +} +Thanks for posting a patch for this issue.
Quoted 3 lines
As Ingo said, I think the hardware counter needs to be reset as well as the value saved in the
perf_counterstruct.I don't think that's needed, we calculate a delta between
prev_countand the current read and use that to incrementcounter->count. Therefore when we resetcounter->countwe should not need to touch the hardware counter.However, I do think we need the below, first read the hardware counter to ensure that delta spoken of above is as close to zero as possible when we reset.
And update the user-page bits.
---
Index:linux-2.6/kernel/perf_counter.c
===================================================================
kernel/perf_counter.c static unsigned int perf_poll(struct fil · line 1305
Quoted 7 lines
kernel/perf_counter.c +2
-0@@ -1299,7 +1299,9 @@ static unsigned int perf_poll(struct filstatic void perf_counter_reset (struct perf_counter *counter) { + (void )perf_counter_read (counter);atomic_set (&counter->count,0 ); +perf_counter_update_userpage (counter); }
Mind sending a changelogged patch? Thanks,
Ingo
[tip:perfcounters/core] perf_counter: add ioctl(PERF_COUNTER_IOC_RESET)
In reply to Peter ZijlstraCommit-ID: 6de6a7b95705b859b61430fa3afa1403034eb3e6 Gitweb: http://git.kernel.org/tip/6de6a7b95705b859b61430fa3afa1403034eb3e6 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:23 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:18:31 +0200
perf_counter: add ioctl(PERF_COUNTER_IOC_RESET)
Provide a way to reset an existing counter - this eases PAPI libraries around perfcounters.
Similar to read() it doesn't collapse pending child counters.
[ Impact: new perfcounter fd ioctl method to reset counters ]
LKML-Reference: <[email protected]>
--- include/linux/perf_counter.h | 1 + kernel/perf_counter.c | 8 ++++++++ 2 files changed, 9 insertions(+), 0 deletions(-)
include/linux/perf_counter.h +1 -0
@@ -160,6 +160,7 @@ struct perf_counter_hw_event {#define PERF_COUNTER_IOC_ENABLE _IO ('$', 0)#define PERF_COUNTER_IOC_DISABLE _IO ('$', 1)#define PERF_COUNTER_IOC_REFRESH _IOW('$', 2, u32) +#define PERF_COUNTER_IOC_RESET _IO ('$', 3)/* * Structure of the page that can be mapped via mmap
kernel/perf_counter.c +8 -0
@@ -1288,6 +1288,11 @@ static unsigned int perf_poll(struct file *file, poll_table *wait)return events; } +static void perf_counter_reset (struct perf_counter *counter) +{ +atomic_set (&counter->count,0 ); +} +static long perf_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {struct perf_counter *counter = file->private_data;
@@ -1303,6 +1308,9 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)case PERF_COUNTER_IOC_REFRESH :perf_counter_refresh (counter, arg);break ; +case PERF_COUNTER_IOC_RESET : +perf_counter_reset (counter); +break ;default : err = -ENOTTY ; }
PATCH 4/7 perf_counter: provide an mlock threshold
In reply to Peter ZijlstraProvide a threshold to relax the mlock accounting, increasing usability.
Each counter gets perf_counter_mlock_kb for free.
--- include/linux/perf_counter.h | 2 ++ kernel/perf_counter.c | 15 +++++++++++---- kernel/sysctl.c | 8 ++++++++ 3 files changed, 21 insertions(+), 4 deletions(-)
include/linux/perf_counter.h +2 -0
@@ -358,6 +358,7 @@ struct file;struct perf_mmap_data {struct rcu_head rcu_head;int nr_pages;/* nr of data pages */ +int nr_locked;/* nr pages mlocked */ atomic_t poll;/* POLL_ for wakeups */ atomic_t head;/* write position */
@@ -575,6 +576,7 @@ struct perf_callchain_entry {extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs);extern int sysctl_perf_counter_priv; +extern int sysctl_perf_counter_mlock;extern void perf_counter_init (void );
kernel/perf_counter.c +11 -4
@@ -44,6 +44,7 @@ static atomic_t nr_munmap_tracking __reastatic atomic_t nr_comm_tracking__read_mostly ;int sysctl_perf_counter_priv__read_mostly ;/* do we need to be privileged */ +int sysctl_perf_counter_mlock__read_mostly =128 ;/* 'free' kb per counter */ /* * Lock for (sysadmin-configurable) counter reservations:
@@ -1461,7 +1462,7 @@ static void perf_mmap_close(struct vm_arif (atomic_dec_and_mutex_lock (&counter->mmap_count, &counter->mmap_mutex)) { - vma->vm_mm->locked_vm -= counter->data->nr_pages +1 ; + vma->vm_mm->locked_vm -= counter->data->nr_locked;perf_mmap_data_free (counter);mutex_unlock (&counter->mmap_mutex); }
@@ -1480,6 +1481,7 @@ static int perf_mmap(struct file *file,unsigned long nr_pages;unsigned long locked, lock_limit;int ret =0 ; +long extra;if (!(vma->vm_flags &VM_SHARED ) || (vma->vm_flags &VM_WRITE ))return -EINVAL ;
@@ -1507,8 +1509,12 @@ static int perf_mmap(struct file *file, goto unlock; } - locked = vma->vm_mm->locked_vm; - locked += nr_pages +1 ; + extra = nr_pages/* + 1 only account the data pages */ ; + extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT -10 ); +if (extra <0 ) + extra =0 ; + + locked = vma->vm_mm->locked_vm + extra; lock_limit = current->signal->rlim[RLIMIT_MEMLOCK ].rlim_cur; lock_limit >>=PAGE_SHIFT ;
@@ -1524,7 +1530,8 @@ static int perf_mmap(struct file *file, goto unlock;atomic_set (&counter->mmap_count,1 ); - vma->vm_mm->locked_vm += nr_pages +1 ; + vma->vm_mm->locked_vm += extra; + counter->data->nr_locked = extra; unlock:mutex_unlock (&counter->mmap_mutex);
kernel/sysctl.c +8 -0
@@ -924,6 +924,14 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = &proc_dointvec, }, + { + .ctl_name = CTL_UNNUMBERED, + .procname = "perf_counter_mlock_kb", + .data = &sysctl_perf_counter_mlock, + .maxlen = sizeof(sysctl_perf_counter_mlock), + .mode = 0644, + .proc_handler = &proc_dointvec, + },#endif /* * NOTE: do not add new entries to this table unless you have read
[tip:perfcounters/core] perf_counter: provide an mlock threshold
Commit-ID: c5078f78b455fbf67ea71442c7e7ca8acf9ff095 Gitweb: http://git.kernel.org/tip/c5078f78b455fbf67ea71442c7e7ca8acf9ff095 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:24 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:18:32 +0200
perf_counter: provide an mlock threshold
Provide a threshold to relax the mlock accounting, increasing usability.
Each counter gets perf_counter_mlock_kb for free.
[ Impact: allow more mmap buffering ]
LKML-Reference: <[email protected]>
--- include/linux/perf_counter.h | 2 ++ kernel/perf_counter.c | 15 +++++++++++---- kernel/sysctl.c | 8 ++++++++ 3 files changed, 21 insertions(+), 4 deletions(-)
include/linux/perf_counter.h +2 -0
@@ -358,6 +358,7 @@ struct file;struct perf_mmap_data {struct rcu_head rcu_head;int nr_pages;/* nr of data pages */ +int nr_locked;/* nr pages mlocked */ atomic_t poll;/* POLL_ for wakeups */ atomic_t head;/* write position */
@@ -575,6 +576,7 @@ struct perf_callchain_entry {extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs);extern int sysctl_perf_counter_priv; +extern int sysctl_perf_counter_mlock;extern void perf_counter_init (void );
kernel/perf_counter.c +11 -4
@@ -44,6 +44,7 @@ static atomic_t nr_munmap_tracking __read_mostly;static atomic_t nr_comm_tracking__read_mostly ;int sysctl_perf_counter_priv__read_mostly ;/* do we need to be privileged */ +int sysctl_perf_counter_mlock__read_mostly =128 ;/* 'free' kb per counter */ /* * Lock for (sysadmin-configurable) counter reservations:
@@ -1461,7 +1462,7 @@ static void perf_mmap_close(struct vm_area_struct *vma)if (atomic_dec_and_mutex_lock (&counter->mmap_count, &counter->mmap_mutex)) { - vma->vm_mm->locked_vm -= counter->data->nr_pages +1 ; + vma->vm_mm->locked_vm -= counter->data->nr_locked;perf_mmap_data_free (counter);mutex_unlock (&counter->mmap_mutex); }
@@ -1480,6 +1481,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)unsigned long nr_pages;unsigned long locked, lock_limit;int ret =0 ; +long extra;if (!(vma->vm_flags &VM_SHARED ) || (vma->vm_flags &VM_WRITE ))return -EINVAL ;
@@ -1507,8 +1509,12 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) goto unlock; } - locked = vma->vm_mm->locked_vm; - locked += nr_pages +1 ; + extra = nr_pages/* + 1 only account the data pages */ ; + extra -= sysctl_perf_counter_mlock >> (PAGE_SHIFT -10 ); +if (extra <0 ) + extra =0 ; + + locked = vma->vm_mm->locked_vm + extra; lock_limit = current->signal->rlim[RLIMIT_MEMLOCK ].rlim_cur; lock_limit >>=PAGE_SHIFT ;
@@ -1524,7 +1530,8 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma) goto unlock;atomic_set (&counter->mmap_count,1 ); - vma->vm_mm->locked_vm += nr_pages +1 ; + vma->vm_mm->locked_vm += extra; + counter->data->nr_locked = extra; unlock:mutex_unlock (&counter->mmap_mutex);
kernel/sysctl.c +8 -0
@@ -920,6 +920,14 @@ static struct ctl_table kern_table[] = { .mode = 0644, .proc_handler = &proc_dointvec, }, + { + .ctl_name = CTL_UNNUMBERED, + .procname = "perf_counter_mlock_kb", + .data = &sysctl_perf_counter_mlock, + .maxlen = sizeof(sysctl_perf_counter_mlock), + .mode = 0644, + .proc_handler = &proc_dointvec, + },#endif /* * NOTE: do not add new entries to this table unless you have read
PATCH 5/7 perf_counter: fix the output lock
In reply to Peter ZijlstraUse -1 instead of 0 as unlocked, since 0 is a valid cpu number.
--- kernel/perf_counter.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-)
kernel/perf_counter.c +4 -3
@@ -1409,6 +1417,7 @@ static int perf_mmap_data_alloc(struct p } data->nr_pages = nr_pages; +atomic_set (&data->lock,-1 );rcu_assign_pointer (counter->data, data);
@@ -1755,7 +1764,7 @@ static void perf_output_lock(struct perfif (in_nmi () &&atomic_read (&data->lock) == cpu)return ; -while (atomic_cmpxchg (&data->lock,0 , cpu) !=0 ) +while (atomic_cmpxchg (&data->lock,-1 , cpu) !=-1 )cpu_relax (); handle->locked =1 ;
@@ -1784,7 +1793,7 @@ again:* NMI can happen here, which means we can miss a done_head update. */ - cpu =atomic_xchg (&data->lock,0 ); + cpu =atomic_xchg (&data->lock,-1 );WARN_ON_ONCE (cpu !=smp_processor_id ());/*
@@ -1794,7 +1803,7 @@ again:/* * Since we had it locked, we can lock it again. */ -while (atomic_cmpxchg (&data->lock,0 , cpu) !=0 ) +while (atomic_cmpxchg (&data->lock,-1 , cpu) !=-1 )cpu_relax (); goto again;
[tip:perfcounters/core] perf_counter: fix the output lock
Commit-ID: 22c1558e51c210787c6cf75d8905246fc91ec030 Gitweb: http://git.kernel.org/tip/22c1558e51c210787c6cf75d8905246fc91ec030 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:25 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:18:32 +0200
perf_counter: fix the output lock
Use -1 instead of 0 as unlocked, since 0 is a valid cpu number.
( This is not an issue right now but will be once we allow multiple
counters to output to the same mmap area. )
[ Impact: prepare code for multi-counter profile output ]
LKML-Reference: <[email protected]>
--- kernel/perf_counter.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
kernel/perf_counter.c +4 -3
@@ -1409,6 +1409,7 @@ static int perf_mmap_data_alloc(struct perf_counter *counter, int nr_pages) } data->nr_pages = nr_pages; +atomic_set (&data->lock,-1 );rcu_assign_pointer (counter->data, data);
@@ -1755,7 +1756,7 @@ static void perf_output_lock(struct perf_output_handle *handle)if (in_nmi () &&atomic_read (&data->lock) == cpu)return ; -while (atomic_cmpxchg (&data->lock,0 , cpu) !=0 ) +while (atomic_cmpxchg (&data->lock,-1 , cpu) !=-1 )cpu_relax (); handle->locked =1 ;
@@ -1784,7 +1785,7 @@ again:* NMI can happen here, which means we can miss a done_head update. */ - cpu =atomic_xchg (&data->lock,0 ); + cpu =atomic_xchg (&data->lock,-1 );WARN_ON_ONCE (cpu !=smp_processor_id ());/*
@@ -1794,7 +1795,7 @@ again:/* * Since we had it locked, we can lock it again. */ -while (atomic_cmpxchg (&data->lock,0 , cpu) !=0 ) +while (atomic_cmpxchg (&data->lock,-1 , cpu) !=-1 )cpu_relax (); goto again;
PATCH 6/7 perf_counter: inheritable sample counters
In reply to Peter ZijlstraRedirect the output to the parent counter and put in some sanity checks.
--- kernel/perf_counter.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
kernel/perf_counter.c +30 -2
@@ -738,10 +738,18 @@ static void perf_counter_enable(struct pspin_unlock_irq (&ctx->lock); } -static void perf_counter_refresh (struct perf_counter *counter,int refresh) +static int perf_counter_refresh (struct perf_counter *counter,int refresh) { +/* +* not supported on inherited counters +*/ +if (counter->hw_event.inherit) +return -EINVAL ; +atomic_add (refresh, &counter->event_limit);perf_counter_enable (counter); + +return 0 ; }/*
@@ -1307,7 +1315,7 @@ static long perf_ioctl(struct file *fileperf_counter_disable_family (counter);break ;case PERF_COUNTER_IOC_REFRESH : -perf_counter_refresh (counter, arg); + err =perf_counter_refresh (counter, arg);break ;case PERF_COUNTER_IOC_RESET :perf_counter_reset (counter);
@@ -1814,6 +1822,12 @@ static int perf_output_begin(struct perfstruct perf_mmap_data *data;unsigned int offset, head; +/* +* For inherited counters we send all the output towards the parent. +*/ +if (counter->parent) + counter = counter->parent; +rcu_read_lock (); data =rcu_dereference (counter->data);if (!data)
@@ -1995,6 +2009,9 @@ static void perf_counter_output(struct pif (record_type &PERF_RECORD_ADDR )perf_output_put (&handle, addr); +/* +* XXX PERF_RECORD_GROUP vs inherited counters seems difficult. +*/ if (record_type &PERF_RECORD_GROUP ) {struct perf_counter *leader, *sub;u64 nr = counter->nr_siblings;
@@ -2281,6 +2298,11 @@ int perf_counter_overflow(struct perf_coint events =atomic_read (&counter->event_limit);int ret =0 ; +/* +* XXX event_limit might not quite work as expected on inherited +* counters +*/ + counter->pending_kill =POLL_IN ;if (events &&atomic_dec_and_test (&counter->event_limit)) { ret =1 ;
@@ -2801,6 +2823,12 @@ perf_counter_alloc(struct perf_counter_h pmu =NULL ; +/* +* we currently do not support PERF_RECORD_GROUP on inherited counters +*/ +if (hw_event->inherit && (hw_event->record_type &PERF_RECORD_GROUP )) + goto done; +if (perf_event_raw (hw_event)) { pmu =hw_perf_counter_init (counter); goto done;
[tip:perfcounters/core] perf_counter: inheritable sample counters
Commit-ID: 2023b359214bbc5bad31571cf50d7fb83b535c0a Gitweb: http://git.kernel.org/tip/2023b359214bbc5bad31571cf50d7fb83b535c0a Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:26 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:18:33 +0200
perf_counter: inheritable sample counters
Redirect the output to the parent counter and put in some sanity checks.
[ Impact: new perfcounter feature - inherited sampling counters ]
LKML-Reference: <[email protected]>
--- kernel/perf_counter.c | 32 ++++++++++++++++++++++++++++++-- 1 files changed, 30 insertions(+), 2 deletions(-)
kernel/perf_counter.c +30 -2
@@ -738,10 +738,18 @@ static void perf_counter_enable(struct perf_counter *counter)spin_unlock_irq (&ctx->lock); } -static void perf_counter_refresh (struct perf_counter *counter,int refresh) +static int perf_counter_refresh (struct perf_counter *counter,int refresh) { +/* +* not supported on inherited counters +*/ +if (counter->hw_event.inherit) +return -EINVAL ; +atomic_add (refresh, &counter->event_limit);perf_counter_enable (counter); + +return 0 ; }/*
@@ -1307,7 +1315,7 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)perf_counter_disable_family (counter);break ;case PERF_COUNTER_IOC_REFRESH : -perf_counter_refresh (counter, arg); + err =perf_counter_refresh (counter, arg);break ;case PERF_COUNTER_IOC_RESET :perf_counter_reset (counter);
@@ -1814,6 +1822,12 @@ static int perf_output_begin(struct perf_output_handle *handle,struct perf_mmap_data *data;unsigned int offset, head; +/* +* For inherited counters we send all the output towards the parent. +*/ +if (counter->parent) + counter = counter->parent; +rcu_read_lock (); data =rcu_dereference (counter->data);if (!data)
@@ -1995,6 +2009,9 @@ static void perf_counter_output(struct perf_counter *counter,if (record_type &PERF_RECORD_ADDR )perf_output_put (&handle, addr); +/* +* XXX PERF_RECORD_GROUP vs inherited counters seems difficult. +*/ if (record_type &PERF_RECORD_GROUP ) {struct perf_counter *leader, *sub;u64 nr = counter->nr_siblings;
@@ -2281,6 +2298,11 @@ int perf_counter_overflow(struct perf_counter *counter,int events =atomic_read (&counter->event_limit);int ret =0 ; +/* +* XXX event_limit might not quite work as expected on inherited +* counters +*/ + counter->pending_kill =POLL_IN ;if (events &&atomic_dec_and_test (&counter->event_limit)) { ret =1 ;
@@ -2801,6 +2823,12 @@ perf_counter_alloc(struct perf_counter_hw_event *hw_event, pmu =NULL ; +/* +* we currently do not support PERF_RECORD_GROUP on inherited counters +*/ +if (hw_event->inherit && (hw_event->record_type &PERF_RECORD_GROUP )) + goto done; +if (perf_event_raw (hw_event)) { pmu =hw_perf_counter_init (counter); goto done;
PATCH 7/7 perf_counter: tools: update the tools to support process and inherited counters
In reply to Peter Zijlstrarecord:
- per task counter
- inherit switch
- nmi switch
report:
- userspace/kernel filter
stat:
- userspace/kernel filter
--- Documentation/perf_counter/builtin-record.c | 155 +++++++++++++++++----------- Documentation/perf_counter/builtin-stat.c | 24 +++- Documentation/perf_counter/perf-report.cc | 27 +++- 3 files changed, 140 insertions(+), 66 deletions(-)
Documentation/perf_counter/builtin-record.c +96 -59
@@ -45,7 +45,10 @@ static unsigned int mmap_pages = 16;static int output;static char *output_name ="output.perf" ;static int group =0 ; -static unsigned int realtime_prio =0 ; +static unsigned int realtime_prio =0 ; +static int system_wide =0 ; +static int inherit =1 ; +static int nmi =1 ;const unsigned int default_count[] = {1000000 ,
@@ -167,7 +170,7 @@ static void display_events_help(void)static void display_help (void ) {printf ( -"Usage: perf-record [<options>]\n" +"Usage: perf-record [<options>] <cmd>\n" "perf-record Options (up to %d event types can be specified at once):\n\n" ,MAX_COUNTERS );
@@ -178,12 +181,13 @@ static void display_help(void)" -m pages --mmap_pages=<pages> # number of mmap data pages\n" " -o file --output=<file> # output file\n" " -r prio --realtime=<prio> # use RT prio\n" +" -s --system # system wide profiling\n" );exit (0 ); } -static void process_options (int argc,char *argv[]) +static void process_options (int argc,const char *argv[]) {int error =0 , counter;
@@ -196,9 +200,12 @@ static void process_options(int argc, ch {"mmap_pages", required_argument, NULL, 'm'}, {"output", required_argument, NULL, 'o'}, {"realtime", required_argument, NULL, 'r'}, + {"system", no_argument, NULL, 's'}, + {"inherit", no_argument, NULL, 'i'}, + {"nmi", no_argument, NULL, 'n'}, {NULL, 0, NULL, 0 } }; -int c =getopt_long (argc, argv,"+:c:e:m:o:r:" , +int c =getopt_long (argc, argv,"+:c:e:m:o:r:sin" , long_options, &option_index);if (c ==-1 )break ;
@@ -209,9 +216,16 @@ static void process_options(int argc, chcase 'm' : mmap_pages =atoi (optarg);break ;case 'o' : output_name =strdup (optarg);break ;case 'r' : realtime_prio =atoi (optarg);break ; +case 's' : system_wide ^=1 ;break ; +case 'i' : inherit ^=1 ;break ; +case 'n' : nmi ^=1 ;break ;default : error =1 ;break ; } } + +if (argc - optind ==0 ) + error =1 ; +if (error)display_help ();
@@ -325,18 +339,82 @@ static void mmap_read(struct mmap_data *static volatile int done =0 ; -static void sigchld_handler (int sig) +static void sig_handler (int sig) { -if (sig ==SIGCHLD ) - done =1 ; + done =1 ; } -int cmd_record (int argc,char **argv) +static struct pollfd event_array[MAX_NR_CPUS *MAX_COUNTERS ]; +static struct mmap_data mmap_array[MAX_NR_CPUS ][MAX_COUNTERS ]; + +static int nr_poll; +static int nr_cpu; + +static void open_counters (int cpu) { -struct pollfd event_array[MAX_NR_CPUS *MAX_COUNTERS ]; -struct mmap_data mmap_array[MAX_NR_CPUS ][MAX_COUNTERS ];struct perf_counter_hw_event hw_event; -int i, counter, group_fd, nr_poll =0 ; +int counter, group_fd; +int track =1 ; +pid_t pid =-1 ; + +if (cpu <0 ) + pid =0 ; + + group_fd =-1 ; +for (counter =0 ; counter < nr_counters; counter++) { + +memset (&hw_event,0 ,sizeof (hw_event)); + hw_event.config = event_id[counter]; + hw_event.irq_period = event_count[counter]; + hw_event.record_type =PERF_RECORD_IP |PERF_RECORD_TID ; + hw_event.nmi = nmi; + hw_event.mmap = track; + hw_event.comm = track; + hw_event.inherit = (cpu <0 ) && inherit; + + track =0 ;// only the first counter needs these + + fd[nr_cpu][counter] = +sys_perf_counter_open (&hw_event, pid, cpu, group_fd,0 ); + +if (fd[nr_cpu][counter] <0 ) { +int err = errno; +printf ("kerneltop error: syscall returned with %d (%s)\n" , + fd[nr_cpu][counter],strerror (err)); +if (err ==EPERM ) +printf ("Are you root?\n" ); +exit (-1 ); + } +assert (fd[nr_cpu][counter] >=0 ); +fcntl (fd[nr_cpu][counter],F_SETFL ,O_NONBLOCK ); + +/* +* First counter acts as the group leader: +*/ +if (group && group_fd ==-1 ) + group_fd = fd[nr_cpu][counter]; + + event_array[nr_poll].fd = fd[nr_cpu][counter]; + event_array[nr_poll].events =POLLIN ; + nr_poll++; + + mmap_array[nr_cpu][counter].counter = counter; + mmap_array[nr_cpu][counter].prev =0 ; + mmap_array[nr_cpu][counter].mask = mmap_pages*page_size -1 ; + mmap_array[nr_cpu][counter].base =mmap (NULL , (mmap_pages+1 )*page_size, +PROT_READ ,MAP_SHARED , fd[nr_cpu][counter],0 ); +if (mmap_array[nr_cpu][counter].base ==MAP_FAILED ) { +printf ("kerneltop error: failed to mmap with %d (%s)\n" , + errno,strerror (errno)); +exit (-1 ); + } + } + nr_cpu++; +} + +int cmd_record (int argc,const char **argv) +{ +int i, counter;pid_t pid;int ret;
@@ -357,54 +435,13 @@ int cmd_record(int argc, char **argv) argc -= optind; argv += optind; -for (i =0 ; i < nr_cpus; i++) { - group_fd =-1 ; -for (counter =0 ; counter < nr_counters; counter++) { - -memset (&hw_event,0 ,sizeof (hw_event)); - hw_event.config = event_id[counter]; - hw_event.irq_period = event_count[counter]; - hw_event.record_type =PERF_RECORD_IP |PERF_RECORD_TID ; - hw_event.nmi =1 ; - hw_event.mmap =1 ; - hw_event.comm =1 ; - - fd[i][counter] =sys_perf_counter_open (&hw_event,-1 , i, group_fd,0 ); -if (fd[i][counter] <0 ) { -int err = errno; -printf ("kerneltop error: syscall returned with %d (%s)\n" , - fd[i][counter],strerror (err)); -if (err ==EPERM ) -printf ("Are you root?\n" ); -exit (-1 ); - } -assert (fd[i][counter] >=0 ); -fcntl (fd[i][counter],F_SETFL ,O_NONBLOCK ); - -/* -* First counter acts as the group leader: -*/ -if (group && group_fd ==-1 ) - group_fd = fd[i][counter]; - - event_array[nr_poll].fd = fd[i][counter]; - event_array[nr_poll].events =POLLIN ; - nr_poll++; - - mmap_array[i][counter].counter = counter; - mmap_array[i][counter].prev =0 ; - mmap_array[i][counter].mask = mmap_pages*page_size -1 ; - mmap_array[i][counter].base =mmap (NULL , (mmap_pages+1 )*page_size, -PROT_READ ,MAP_SHARED , fd[i][counter],0 ); -if (mmap_array[i][counter].base ==MAP_FAILED ) { -printf ("kerneltop error: failed to mmap with %d (%s)\n" , - errno,strerror (errno)); -exit (-1 ); - } - } - } +if (!system_wide) +open_counters (-1 ); +else for (i =0 ; i < nr_cpus; i++) +open_counters (i); -signal (SIGCHLD , sigchld_handler); +signal (SIGCHLD , sig_handler); +signal (SIGINT , sig_handler); pid =fork ();if (pid <0 )
@@ -434,7 +471,7 @@ int cmd_record(int argc, char **argv)while (!done) {int hits = events; -for (i =0 ; i < nr_cpus; i++) { +for (i =0 ; i < nr_cpu; i++) {for (counter =0 ; counter < nr_counters; counter++)mmap_read (&mmap_array[i][counter]); }
Documentation/perf_counter/builtin-stat.c +22 -2
@@ -87,6 +87,9 @@#include "perf.h" +#define EVENT_MASK_KERNEL 1 +#define EVENT_MASK_USER 2 +static int system_wide =0 ;static int nr_counters =0 ;
@@ -104,6 +107,7 @@ static __u64 event_id[MAX_COUNTERS] =static int default_interval =100000 ;static int event_count[MAX_COUNTERS ];static int fd[MAX_NR_CPUS ][MAX_COUNTERS ]; +static int event_mask[MAX_COUNTERS ];static int tid =-1 ;static int profile_cpu =-1 ;
@@ -258,12 +262,23 @@ static __u64 match_event_symbols(char *s__u64 config, id;int type;unsigned int i; +char mask_str[4 ];if (sscanf (str,"r%llx" , &config) ==1 )return config |PERF_COUNTER_RAW_MASK ; -if (sscanf (str,"%d:%llu" , &type, &id) ==2 ) -return EID (type, id); +switch (sscanf (str,"%d:%llu:%2s" , &type, &id, mask_str)) { +case 3 : +if (strchr (mask_str,'u' )) + event_mask[nr_counters] |=EVENT_MASK_USER ; +if (strchr (mask_str,'k' )) + event_mask[nr_counters] |=EVENT_MASK_KERNEL ; +case 2 : +return EID (type, id); + +default : +break ; + } for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { if (!strncmp(str, event_symbols[i].symbol,
@@ -313,6 +328,11 @@ static void create_perfstat_counter(int hw_event.config = event_id[counter]; hw_event.record_type =0 ; hw_event.nmi =0 ; + hw_event.exclude_kernel = event_mask[counter] &EVENT_MASK_KERNEL ; + hw_event.exclude_user = event_mask[counter] &EVENT_MASK_USER ; + +printf ("exclude: %d\n" , event_mask[counter]); +if (scale) hw_event.read_format =PERF_FORMAT_TOTAL_TIME_ENABLED |PERF_FORMAT_TOTAL_TIME_RUNNING ;
Documentation/perf_counter/perf-report.cc +22 -5
@@ -33,8 +33,13 @@ #include <string> +#define SHOW_KERNEL 1 +#define SHOW_USER 2 +#define SHOW_HV 4 + static char const *input_name = "output.perf"; static int input; +static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; static unsigned long page_size; static unsigned long mmap_window = 32;
@@ -359,15 +364,21 @@ static void process_options(int argc, ch /** Options for getopt */ static struct option long_options[] = { {"input", required_argument, NULL, 'i'}, + {"no-user", no_argument, NULL, 'u'}, + {"no-kernel", no_argument, NULL, 'k'}, + {"no-hv", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0 } }; - int c = getopt_long(argc, argv, "+:i:", + int c = getopt_long(argc, argv, "+:i:kuh", long_options, &option_index); if (c == -1) break; switch (c) { case 'i': input_name = strdup(optarg); break; + case 'k': show_mask &= ~SHOW_KERNEL; break; + case 'u': show_mask &= ~SHOW_USER; break; + case 'h': show_mask &= ~SHOW_HV; break; default: error = 1; break; } }
@@ -443,22 +454,28 @@ more: if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { std::string comm, sym, level; + int show = 0; char output[1024]; if (event->header.misc & PERF_EVENT_MISC_KERNEL) { + show |= SHOW_KERNEL; level = " [k] "; sym = resolve_kernel_symbol(event->ip.ip); } else if (event->header.misc & PERF_EVENT_MISC_USER) { + show |= SHOW_USER; level = " [.] "; sym = resolve_user_symbol(event->ip.pid, event->ip.ip); } else { + show |= SHOW_HV; level = " [H] "; } - comm = resolve_comm(event->ip.pid); - snprintf(output, sizeof(output), "%16s %s %s", - comm.c_str(), level.c_str(), sym.c_str()); - hist[output]++; + if (show & show_mask) { + comm = resolve_comm(event->ip.pid); + snprintf(output, sizeof(output), "%16s %s %s", + comm.c_str(), level.c_str(), sym.c_str()); + hist[output]++; + } total++;
[tip:perfcounters/core] perf_counter: tools: update the tools to support process and inherited counters
Commit-ID: 16c8a10932aef971292c9570eb5f60b5d4e83ed2 Gitweb: http://git.kernel.org/tip/16c8a10932aef971292c9570eb5f60b5d4e83ed2 Author: Peter Zijlstra <[email protected]> AuthorDate: Tue, 5 May 2009 17:50:27 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Tue, 5 May 2009 20:18:33 +0200
perf_counter: tools: update the tools to support process and inherited counters
"perf record":
- per task counter
- inherit switch
- nmi switch
"perf report":
- userspace/kernel filter
"perf stat":
- userspace/kernel filter
LKML-Reference: <[email protected]>
--- Documentation/perf_counter/builtin-record.c | 155 +++++++++++++++++---------- Documentation/perf_counter/builtin-stat.c | 24 ++++- Documentation/perf_counter/perf-report.cc | 27 ++++- 3 files changed, 140 insertions(+), 66 deletions(-)
Documentation/perf_counter/builtin-record.c +96 -59
@@ -45,7 +45,10 @@ static unsigned int mmap_pages = 16;static int output;static char *output_name ="output.perf" ;static int group =0 ; -static unsigned int realtime_prio =0 ; +static unsigned int realtime_prio =0 ; +static int system_wide =0 ; +static int inherit =1 ; +static int nmi =1 ;const unsigned int default_count[] = {1000000 ,
@@ -167,7 +170,7 @@ static void display_events_help(void)static void display_help (void ) {printf ( -"Usage: perf-record [<options>]\n" +"Usage: perf-record [<options>] <cmd>\n" "perf-record Options (up to %d event types can be specified at once):\n\n" ,MAX_COUNTERS );
@@ -178,12 +181,13 @@ static void display_help(void)" -m pages --mmap_pages=<pages> # number of mmap data pages\n" " -o file --output=<file> # output file\n" " -r prio --realtime=<prio> # use RT prio\n" +" -s --system # system wide profiling\n" );exit (0 ); } -static void process_options (int argc,char *argv[]) +static void process_options (int argc,const char *argv[]) {int error =0 , counter;
@@ -196,9 +200,12 @@ static void process_options(int argc, char *argv[]) {"mmap_pages", required_argument, NULL, 'm'}, {"output", required_argument, NULL, 'o'}, {"realtime", required_argument, NULL, 'r'}, + {"system", no_argument, NULL, 's'}, + {"inherit", no_argument, NULL, 'i'}, + {"nmi", no_argument, NULL, 'n'}, {NULL, 0, NULL, 0 } }; -int c =getopt_long (argc, argv,"+:c:e:m:o:r:" , +int c =getopt_long (argc, argv,"+:c:e:m:o:r:sin" , long_options, &option_index);if (c ==-1 )break ;
@@ -209,9 +216,16 @@ static void process_options(int argc, char *argv[])case 'm' : mmap_pages =atoi (optarg);break ;case 'o' : output_name =strdup (optarg);break ;case 'r' : realtime_prio =atoi (optarg);break ; +case 's' : system_wide ^=1 ;break ; +case 'i' : inherit ^=1 ;break ; +case 'n' : nmi ^=1 ;break ;default : error =1 ;break ; } } + +if (argc - optind ==0 ) + error =1 ; +if (error)display_help ();
@@ -325,18 +339,82 @@ static void mmap_read(struct mmap_data *md)static volatile int done =0 ; -static void sigchld_handler (int sig) +static void sig_handler (int sig) { -if (sig ==SIGCHLD ) - done =1 ; + done =1 ; } -int cmd_record (int argc,char **argv) +static struct pollfd event_array[MAX_NR_CPUS *MAX_COUNTERS ]; +static struct mmap_data mmap_array[MAX_NR_CPUS ][MAX_COUNTERS ]; + +static int nr_poll; +static int nr_cpu; + +static void open_counters (int cpu) { -struct pollfd event_array[MAX_NR_CPUS *MAX_COUNTERS ]; -struct mmap_data mmap_array[MAX_NR_CPUS ][MAX_COUNTERS ];struct perf_counter_hw_event hw_event; -int i, counter, group_fd, nr_poll =0 ; +int counter, group_fd; +int track =1 ; +pid_t pid =-1 ; + +if (cpu <0 ) + pid =0 ; + + group_fd =-1 ; +for (counter =0 ; counter < nr_counters; counter++) { + +memset (&hw_event,0 ,sizeof (hw_event)); + hw_event.config = event_id[counter]; + hw_event.irq_period = event_count[counter]; + hw_event.record_type =PERF_RECORD_IP |PERF_RECORD_TID ; + hw_event.nmi = nmi; + hw_event.mmap = track; + hw_event.comm = track; + hw_event.inherit = (cpu <0 ) && inherit; + + track =0 ;// only the first counter needs these + + fd[nr_cpu][counter] = +sys_perf_counter_open (&hw_event, pid, cpu, group_fd,0 ); + +if (fd[nr_cpu][counter] <0 ) { +int err = errno; +printf ("kerneltop error: syscall returned with %d (%s)\n" , + fd[nr_cpu][counter],strerror (err)); +if (err ==EPERM ) +printf ("Are you root?\n" ); +exit (-1 ); + } +assert (fd[nr_cpu][counter] >=0 ); +fcntl (fd[nr_cpu][counter],F_SETFL ,O_NONBLOCK ); + +/* +* First counter acts as the group leader: +*/ +if (group && group_fd ==-1 ) + group_fd = fd[nr_cpu][counter]; + + event_array[nr_poll].fd = fd[nr_cpu][counter]; + event_array[nr_poll].events =POLLIN ; + nr_poll++; + + mmap_array[nr_cpu][counter].counter = counter; + mmap_array[nr_cpu][counter].prev =0 ; + mmap_array[nr_cpu][counter].mask = mmap_pages*page_size -1 ; + mmap_array[nr_cpu][counter].base =mmap (NULL , (mmap_pages+1 )*page_size, +PROT_READ ,MAP_SHARED , fd[nr_cpu][counter],0 ); +if (mmap_array[nr_cpu][counter].base ==MAP_FAILED ) { +printf ("kerneltop error: failed to mmap with %d (%s)\n" , + errno,strerror (errno)); +exit (-1 ); + } + } + nr_cpu++; +} + +int cmd_record (int argc,const char **argv) +{ +int i, counter;pid_t pid;int ret;
@@ -357,54 +435,13 @@ int cmd_record(int argc, char **argv) argc -= optind; argv += optind; -for (i =0 ; i < nr_cpus; i++) { - group_fd =-1 ; -for (counter =0 ; counter < nr_counters; counter++) { - -memset (&hw_event,0 ,sizeof (hw_event)); - hw_event.config = event_id[counter]; - hw_event.irq_period = event_count[counter]; - hw_event.record_type =PERF_RECORD_IP |PERF_RECORD_TID ; - hw_event.nmi =1 ; - hw_event.mmap =1 ; - hw_event.comm =1 ; - - fd[i][counter] =sys_perf_counter_open (&hw_event,-1 , i, group_fd,0 ); -if (fd[i][counter] <0 ) { -int err = errno; -printf ("kerneltop error: syscall returned with %d (%s)\n" , - fd[i][counter],strerror (err)); -if (err ==EPERM ) -printf ("Are you root?\n" ); -exit (-1 ); - } -assert (fd[i][counter] >=0 ); -fcntl (fd[i][counter],F_SETFL ,O_NONBLOCK ); - -/* -* First counter acts as the group leader: -*/ -if (group && group_fd ==-1 ) - group_fd = fd[i][counter]; - - event_array[nr_poll].fd = fd[i][counter]; - event_array[nr_poll].events =POLLIN ; - nr_poll++; - - mmap_array[i][counter].counter = counter; - mmap_array[i][counter].prev =0 ; - mmap_array[i][counter].mask = mmap_pages*page_size -1 ; - mmap_array[i][counter].base =mmap (NULL , (mmap_pages+1 )*page_size, -PROT_READ ,MAP_SHARED , fd[i][counter],0 ); -if (mmap_array[i][counter].base ==MAP_FAILED ) { -printf ("kerneltop error: failed to mmap with %d (%s)\n" , - errno,strerror (errno)); -exit (-1 ); - } - } - } +if (!system_wide) +open_counters (-1 ); +else for (i =0 ; i < nr_cpus; i++) +open_counters (i); -signal (SIGCHLD , sigchld_handler); +signal (SIGCHLD , sig_handler); +signal (SIGINT , sig_handler); pid =fork ();if (pid <0 )
@@ -434,7 +471,7 @@ int cmd_record(int argc, char **argv)while (!done) {int hits = events; -for (i =0 ; i < nr_cpus; i++) { +for (i =0 ; i < nr_cpu; i++) {for (counter =0 ; counter < nr_counters; counter++)mmap_read (&mmap_array[i][counter]); }
Documentation/perf_counter/builtin-stat.c +22 -2
@@ -87,6 +87,9 @@#include "perf.h" +#define EVENT_MASK_KERNEL 1 +#define EVENT_MASK_USER 2 +static int system_wide =0 ;static int nr_counters =0 ;
@@ -104,6 +107,7 @@ static __u64 event_id[MAX_COUNTERS] = {static int default_interval =100000 ;static int event_count[MAX_COUNTERS ];static int fd[MAX_NR_CPUS ][MAX_COUNTERS ]; +static int event_mask[MAX_COUNTERS ];static int tid =-1 ;static int profile_cpu =-1 ;
@@ -258,12 +262,23 @@ static __u64 match_event_symbols(char *str)__u64 config, id;int type;unsigned int i; +char mask_str[4 ];if (sscanf (str,"r%llx" , &config) ==1 )return config |PERF_COUNTER_RAW_MASK ; -if (sscanf (str,"%d:%llu" , &type, &id) ==2 ) -return EID (type, id); +switch (sscanf (str,"%d:%llu:%2s" , &type, &id, mask_str)) { +case 3 : +if (strchr (mask_str,'u' )) + event_mask[nr_counters] |=EVENT_MASK_USER ; +if (strchr (mask_str,'k' )) + event_mask[nr_counters] |=EVENT_MASK_KERNEL ; +case 2 : +return EID (type, id); + +default : +break ; + } for (i = 0; i < ARRAY_SIZE(event_symbols); i++) { if (!strncmp(str, event_symbols[i].symbol,
@@ -313,6 +328,11 @@ static void create_perfstat_counter(int counter) hw_event.config = event_id[counter]; hw_event.record_type =0 ; hw_event.nmi =0 ; + hw_event.exclude_kernel = event_mask[counter] &EVENT_MASK_KERNEL ; + hw_event.exclude_user = event_mask[counter] &EVENT_MASK_USER ; + +printf ("exclude: %d\n" , event_mask[counter]); +if (scale) hw_event.read_format =PERF_FORMAT_TOTAL_TIME_ENABLED |PERF_FORMAT_TOTAL_TIME_RUNNING ;
Documentation/perf_counter/perf-report.cc +22 -5
@@ -33,8 +33,13 @@ #include <string> +#define SHOW_KERNEL 1 +#define SHOW_USER 2 +#define SHOW_HV 4 + static char const *input_name = "output.perf"; static int input; +static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV; static unsigned long page_size; static unsigned long mmap_window = 32;
@@ -359,15 +364,21 @@ static void process_options(int argc, char *argv[]) /** Options for getopt */ static struct option long_options[] = { {"input", required_argument, NULL, 'i'}, + {"no-user", no_argument, NULL, 'u'}, + {"no-kernel", no_argument, NULL, 'k'}, + {"no-hv", no_argument, NULL, 'h'}, {NULL, 0, NULL, 0 } }; - int c = getopt_long(argc, argv, "+:i:", + int c = getopt_long(argc, argv, "+:i:kuh", long_options, &option_index); if (c == -1) break; switch (c) { case 'i': input_name = strdup(optarg); break; + case 'k': show_mask &= ~SHOW_KERNEL; break; + case 'u': show_mask &= ~SHOW_USER; break; + case 'h': show_mask &= ~SHOW_HV; break; default: error = 1; break; } }
@@ -443,22 +454,28 @@ more: if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) { std::string comm, sym, level; + int show = 0; char output[1024]; if (event->header.misc & PERF_EVENT_MISC_KERNEL) { + show |= SHOW_KERNEL; level = " [k] "; sym = resolve_kernel_symbol(event->ip.ip); } else if (event->header.misc & PERF_EVENT_MISC_USER) { + show |= SHOW_USER; level = " [.] "; sym = resolve_user_symbol(event->ip.pid, event->ip.ip); } else { + show |= SHOW_HV; level = " [H] "; } - comm = resolve_comm(event->ip.pid); - snprintf(output, sizeof(output), "%16s %s %s", - comm.c_str(), level.c_str(), sym.c_str()); - hist[output]++; + if (show & show_mask) { + comm = resolve_comm(event->ip.pid); + snprintf(output, sizeof(output), "%16s %s %s", + comm.c_str(), level.c_str(), sym.c_str()); + hist[output]++; + } total++;