Skip to contentSkip to conversation

PERF_COUNT_SW_RUNNABLE_TASKS: measure and act upon parallellism

RFC PATCH In review [email protected] 2 participants 10 messages Feb 7, 2010 – Feb 8, 2010

Subsystems (5)
x86 perf events MM core Scheduler +1 more subsystems
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 PERF_COUNT_SW_RUNNABLE_TASKS: measure and act upon parallellism

Hi all,

Here's an initial RFC patch for the parallallism events for perf_events.

It works with a low number of tasks, but runs into a couple of issues:
- something goes wrong when a thread exits. Apparently the count is decremented twice. I'm still figuring this one out, perhaps sync_stat is to blame.
This causes things to break with more than 3 threads if I recall correctly.
- the count is stored with the parent event only. This means the child list lock is not taken too often.
- adding threshold support to poll() breaks use with mmap. poll() returns when count < threshold although no new entries might be added to the memory map.
- min threshold is actually useless so it should be dropped.

There's definately more wrong with this early patchset and I'm doubting whether perf_events is actually fit for this purpose.
Currently, by design, it's not meant to support counters that may also decrement. Also, the foreseen use of poll to block execution of the thread doesn't seem to work together well with the mmap()-use.

Some hints, pointers and remarks are definately welcome at this stage.

Regards,
Stijn

#1 lore raw

PATCH 1/6 Move poll into perf_event and allow for wakeups when fd has not been mmap'd. This is useful when using read() to read out the current counter value.

From: Stijn Devriendt <[email protected]>

---
 include/linux/perf_event.h |    2 +-
 kernel/perf_event.c        |   30 +++++++++++++++---------------
 2 files changed, 16 insertions(+), 16 deletions(-)
include/linux/perf_event.h +1 -1
index c66b34f..827a221 100644
@@ -538,7 +538,6 @@ struct perf_mmap_data {
 	int				writable;	/* are we writable   */
 	int				nr_locked;	/* nr pages mlocked  */
 
-	atomic_t			poll;		/* POLL_ for wakeups */
 	atomic_t			events;		/* event_id limit       */
 
 	atomic_long_t			head;		/* write position    */
@@ -639,6 +638,7 @@ struct perf_event {
 	struct perf_mmap_data		*data;
 
 	/* poll related */
+	atomic_t			poll;		/* POLL_ for wakeups */
 	wait_queue_head_t		waitq;
 	struct fasync_struct		*fasync;
 
kernel/perf_event.c +15 -15
index e0eb4a2..42dc18d 100644
@@ -1919,14 +1919,7 @@ perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 static unsigned int perf_poll(struct file *file, poll_table *wait)
 {
 	struct perf_event *event = file->private_data;
-	struct perf_mmap_data *data;
-	unsigned int events = POLL_HUP;
-
-	rcu_read_lock();
-	data = rcu_dereference(event->data);
-	if (data)
-		events = atomic_xchg(&data->poll, 0);
-	rcu_read_unlock();
+	unsigned int events = atomic_xchg(&event->poll, 0);
 
 	poll_wait(file, &event->waitq, wait);
 
@@ -2680,16 +2673,20 @@ static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
 	return true;
 }
 
-static void perf_output_wakeup(struct perf_output_handle *handle)
+static void __perf_output_wakeup(struct perf_event* event, int nmi)
 {
-	atomic_set(&handle->data->poll, POLL_IN);
+	atomic_set(&event->poll, POLLIN);
 
-	if (handle->nmi) {
-		handle->event->pending_wakeup = 1;
-		perf_pending_queue(&handle->event->pending,
-				   perf_pending_event);
+	if (nmi) {
+		event->pending_wakeup = 1;
+		perf_pending_queue(&event->pending, perf_pending_event);
 	} else
-		perf_event_wakeup(handle->event);
+		perf_event_wakeup(event);
+}
+
+static void perf_output_wakeup(struct perf_output_handle *handle)
+{
+	__perf_output_wakeup(handle->event, handle->nmi);
 }
 
 /*
@@ -3171,6 +3168,9 @@ static void perf_event_output(struct perf_event *event, int nmi,
 	struct perf_output_handle handle;
 	struct perf_event_header header;
 
+	if (!event->data)
+		return __perf_output_wakeup(event, nmi);
+
 	perf_prepare_sample(&header, data, event, regs);
 
 	if (perf_output_begin(&handle, event, header.size, nmi, 1))
Signature · 1 line
1.6.6
#2 lore raw

PATCH 2/6 Allow min/max thresholds for wakeups

From: Stijn Devriendt <[email protected]>

---
 include/linux/perf_event.h |    5 +++--
 kernel/perf_event.c        |   28 ++++++++++++++++++++++++----
 2 files changed, 27 insertions(+), 6 deletions(-)
include/linux/perf_event.h +3 -2
index 827a221..0fa235e 100644
@@ -183,6 +183,7 @@ struct perf_event_attr {
 	union {
 		__u64		sample_period;
 		__u64		sample_freq;
+		__u64           max_threshold;        /* maximum threshold     */
 	};
 
 	__u64			sample_type;
@@ -203,16 +204,16 @@ struct perf_event_attr {
 				enable_on_exec :  1, /* next exec enables     */
 				task           :  1, /* trace fork/exit       */
 				watermark      :  1, /* wakeup_watermark      */
+				threshold      :  1, /* tresholds             */
 
 				__reserved_1   : 49;
 
 	union {
 		__u32		wakeup_events;	  /* wakeup every n events */
 		__u32		wakeup_watermark; /* bytes before wakeup   */
+		__u64           min_threshold;    /* minimum threshold     */
 	};
 
-	__u32			__reserved_2;
-
 	__u64			bp_addr;
 	__u32			bp_type;
 	__u32			bp_len;
kernel/perf_event.c +24 -4
index 42dc18d..70ca6e1 100644
@@ -1401,6 +1401,8 @@ static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
 		if (!event->attr.freq || !event->attr.sample_freq)
 			continue;
 
+		if (event->attr.threshold)
+			continue;
 		/*
 		 * if the specified freq < HZ then we need to skip ticks
 		 */
@@ -1921,6 +1923,15 @@ static unsigned int perf_poll(struct file *file, poll_table *wait)
 	struct perf_event *event = file->private_data;
 	unsigned int events = atomic_xchg(&event->poll, 0);
 
+	if (event->attr.threshold)
+	{
+		u64 count = atomic64_read(&event->count);
+		if (count < event->attr.min_threshold)
+			events |= POLLIN;
+		else if (count > event->attr.max_threshold)
+			events &= ~POLLIN;
+	}
+
 	poll_wait(file, &event->waitq, wait);
 
 	return events;
@@ -1979,6 +1990,9 @@ static int perf_event_period(struct perf_event *event, u64 __user *arg)
 	if (!event->attr.sample_period)
 		return -EINVAL;
 
+	if (event->attr.threshold)
+		return -EINVAL;
+
 	size = copy_from_user(&value, arg, sizeof(value));
 	if (size != sizeof(value))
 		return -EFAULT;
@@ -2675,6 +2689,9 @@ static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
 
 static void __perf_output_wakeup(struct perf_event* event, int nmi)
 {
+	if (event->attr.threshold && atomic64_read(&event->count) > event->attr.max_threshold)
+		return;
+	
 	atomic_set(&event->poll, POLLIN);
 
 	if (nmi) {
@@ -2895,7 +2912,7 @@ void perf_output_end(struct perf_output_handle *handle)
 	struct perf_event *event = handle->event;
 	struct perf_mmap_data *data = handle->data;
 
-	int wakeup_events = event->attr.wakeup_events;
+	int wakeup_events = event->attr.thresold ? 1 : event->attr.wakeup_events;
 
 	if (handle->sample && wakeup_events) {
 		int events = atomic_inc_return(&data->events);
@@ -4445,7 +4462,7 @@ perf_event_alloc(struct perf_event_attr *attr,
 
 	hwc = &event->hw;
 	hwc->sample_period = attr->sample_period;
-	if (attr->freq && attr->sample_freq)
+	if (attr->threshold || (attr->freq && attr->sample_freq))
 		hwc->sample_period = 1;
 	hwc->last_period = hwc->sample_period;
 
@@ -4571,7 +4588,7 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
 	if (attr->type >= PERF_TYPE_MAX)
 		return -EINVAL;
 
-	if (attr->__reserved_1 || attr->__reserved_2)
+	if (attr->__reserved_1)
 		return -EINVAL;
 
 	if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
@@ -4674,6 +4691,9 @@ SYSCALL_DEFINE5(perf_event_open,
 			return -EACCES;
 	}
 
+	if (attr.threshold && (attr.freq || attr.watermark))
+		return -EINVAL;
+	
 	if (attr.freq) {
 		if (attr.sample_freq > sysctl_perf_event_sample_rate)
 			return -EINVAL;
@@ -4862,7 +4882,7 @@ inherit_event(struct perf_event *parent_event,
 	else
 		child_event->state = PERF_EVENT_STATE_OFF;
 
-	if (parent_event->attr.freq)
+	if (parent_event->attr.freq || parent_event->attr.threshold)
 		child_event->hw.sample_period = parent_event->hw.sample_period;
 
 	child_event->overflow_handler = parent_event->overflow_handler;
Signature · 1 line
1.6.6
#3 lore raw

PATCH 3/6 Add handlers for adding counter events, reading counter events, counter resets, wakeups. Renamed old read handler to update.

From: Stijn Devriendt <[email protected]>

---
 arch/x86/kernel/cpu/perf_event.c |    4 +-
 include/linux/perf_event.h       |    8 ++-
 kernel/perf_event.c              |  120 +++++++++++++++++++++++++++-----------
 3 files changed, 94 insertions(+), 38 deletions(-)
arch/x86/kernel/cpu/perf_event.c +2 -2
index c223b7e..9738f22 100644
@@ -2222,7 +2222,7 @@ void __init init_hw_perf_events(void)
 	pr_info("... event mask:             %016Lx\n", perf_event_mask);
 }
 
-static inline void x86_pmu_read(struct perf_event *event)
+static inline void x86_pmu_update(struct perf_event *event)
 {
 	x86_perf_event_update(event, &event->hw, event->hw.idx);
 }
@@ -2230,7 +2230,7 @@ static inline void x86_pmu_read(struct perf_event *event)
 static const struct pmu pmu = {
 	.enable		= x86_pmu_enable,
 	.disable	= x86_pmu_disable,
-	.read		= x86_pmu_read,
+	.update		= x86_pmu_update,
 	.unthrottle	= x86_pmu_unthrottle,
 };
 
include/linux/perf_event.h +6 -2
index 0fa235e..4f7d318 100644
@@ -513,8 +513,12 @@ struct perf_event;
 struct pmu {
 	int (*enable)			(struct perf_event *event);
 	void (*disable)			(struct perf_event *event);
-	void (*read)			(struct perf_event *event);
+	void (*update)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
+	u64 (*add)                      (struct perf_event *event, u64 count);
+	int (*reset)                    (struct perf_event *event);
+	void (*wakeup)                  (struct perf_event *event);
+	u64 (*read)                     (struct perf_event *event);
 };
 
 /**
@@ -639,7 +643,7 @@ struct perf_event {
 	struct perf_mmap_data		*data;
 
 	/* poll related */
-	atomic_t			poll;		/* POLL_ for wakeups */
+	atomic_t			poll;		/* POLLX for wakeups */
 	wait_queue_head_t		waitq;
 	struct fasync_struct		*fasync;
 
kernel/perf_event.c +86 -34

317 lines not on this page · raw message

Signature · 1 line
1.6.6
#4 lore raw

PATCH 4/6 Fix HW breakpoint handlers

From: Stijn Devriendt <[email protected]>

---
 arch/x86/include/asm/hw_breakpoint.h |    2 +-
 arch/x86/kernel/hw_breakpoint.c      |    2 +-
 kernel/hw_breakpoint.c               |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
arch/x86/include/asm/hw_breakpoint.h +1 -1
index 0675a7c..d060a5c 100644
@@ -54,7 +54,7 @@ extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
 
 int arch_install_hw_breakpoint(struct perf_event *bp);
 void arch_uninstall_hw_breakpoint(struct perf_event *bp);
-void hw_breakpoint_pmu_read(struct perf_event *bp);
+void hw_breakpoint_pmu_update(struct perf_event *bp);
 void hw_breakpoint_pmu_unthrottle(struct perf_event *bp);
 
 extern void
arch/x86/kernel/hw_breakpoint.c +1 -1
index 05d5fec..55469e0 100644
@@ -543,7 +543,7 @@ int __kprobes hw_breakpoint_exceptions_notify(
 	return hw_breakpoint_handler(data);
 }
 
-void hw_breakpoint_pmu_read(struct perf_event *bp)
+void hw_breakpoint_pmu_update(struct perf_event *bp)
 {
 	/* TODO */
 }
kernel/hw_breakpoint.c +1 -1
index dbcbf6a..16b3a9b 100644
@@ -448,6 +448,6 @@ core_initcall(init_hw_breakpoint);
 struct pmu perf_ops_bp = {
 	.enable		= arch_install_hw_breakpoint,
 	.disable	= arch_uninstall_hw_breakpoint,
-	.read		= hw_breakpoint_pmu_read,
+	.update		= hw_breakpoint_pmu_update,
 	.unthrottle	= hw_breakpoint_pmu_unthrottle
 };
Signature · 1 line
1.6.6
#5 lore raw

PATCH 5/6 Allow decrementing counters

From: Stijn Devriendt <[email protected]>

---
 include/linux/perf_event.h |    6 +++---
 kernel/perf_event.c        |   16 ++++++++--------
 2 files changed, 11 insertions(+), 11 deletions(-)
include/linux/perf_event.h +3 -3
index 4f7d318..084f322 100644
@@ -515,7 +515,7 @@ struct pmu {
 	void (*disable)			(struct perf_event *event);
 	void (*update)			(struct perf_event *event);
 	void (*unthrottle)		(struct perf_event *event);
-	u64 (*add)                      (struct perf_event *event, u64 count);
+	u64 (*add)                      (struct perf_event *event, s64 count);
 	int (*reset)                    (struct perf_event *event);
 	void (*wakeup)                  (struct perf_event *event);
 	u64 (*read)                     (struct perf_event *event);
@@ -826,10 +826,10 @@ static inline int is_software_event(struct perf_event *event)
 
 extern atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
 
-extern void __perf_sw_event(u32, u64, int, struct pt_regs *, u64);
+extern void __perf_sw_event(u32, s64, int, struct pt_regs *, u64);
 
 static inline void
-perf_sw_event(u32 event_id, u64 nr, int nmi, struct pt_regs *regs, u64 addr)
+perf_sw_event(u32 event_id, s64 nr, int nmi, struct pt_regs *regs, u64 addr)
 {
 	if (atomic_read(&perf_swevent_enabled[event_id]))
 		__perf_sw_event(event_id, nr, nmi, regs, addr);
kernel/perf_event.c +8 -8
index 724aafd..08885d0 100644
@@ -3770,12 +3770,12 @@ static void perf_event_wakeup_one(struct perf_event *event)
 	wake_up(&event->waitq);
 }
 
-static u64 __perf_event_add(struct perf_event *event, u64 count)
+static u64 __perf_event_add(struct perf_event *event, s64 count)
 {
 	return atomic64_add_return(count, &event->count);
 }
 
-static u64 perf_event_add(struct perf_event *event, u64 count)
+static u64 perf_event_add(struct perf_event *event, s64 count)
 {
 	if (event->pmu->add)
 		return event->pmu->add(event, count);
@@ -3856,7 +3856,7 @@ static void perf_swevent_unthrottle(struct perf_event *event)
 	 */
 }
 
-static void perf_swevent_add(struct perf_event *event, u64 nr,
+static void perf_swevent_add(struct perf_event *event, s64 nr,
 			       int nmi, struct perf_sample_data *data,
 			       struct pt_regs *regs)
 {
@@ -3870,10 +3870,10 @@ static void perf_swevent_add(struct perf_event *event, u64 nr,
 	if (!hwc->sample_period)
 		return;
 
-	if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
+	if (abs(nr) == 1 && hwc->sample_period == 1 && !event->attr.freq)
 		return perf_swevent_overflow(event, 1, nmi, data, regs);
 
-	if (atomic64_add_negative(nr, &hwc->period_left))
+	if (atomic64_add_negative(abs(nr), &hwc->period_left))
 		return;
 
 	perf_swevent_overflow(event, 0, nmi, data, regs);
@@ -3956,7 +3956,7 @@ static int perf_swevent_match(struct perf_event *event,
 
 static void perf_swevent_ctx_event(struct perf_event_context *ctx,
 				     enum perf_type_id type,
-				     u32 event_id, u64 nr, int nmi,
+				     u32 event_id, s64 nr, int nmi,
 				     struct perf_sample_data *data,
 				     struct pt_regs *regs)
 {
@@ -4004,7 +4004,7 @@ void perf_swevent_put_recursion_context(int rctx)
 EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
 
 static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
-				    u64 nr, int nmi,
+				    s64 nr, int nmi,
 				    struct perf_sample_data *data,
 				    struct pt_regs *regs)
 {
@@ -4025,7 +4025,7 @@ static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
 	rcu_read_unlock();
 }
 
-void __perf_sw_event(u32 event_id, u64 nr, int nmi,
+void __perf_sw_event(u32 event_id, s64 nr, int nmi,
 			    struct pt_regs *regs, u64 addr)
 {
 	struct perf_sample_data data;
Signature · 1 line
1.6.6
#6 lore raw

PATCH 6/6 Add PERF_COUNT_SW_RUNNABLE_TASKS

From: Stijn Devriendt <[email protected]>

---
 include/linux/perf_event.h |   17 ++++-
 include/linux/sched.h      |    1 +
 kernel/perf_event.c        |  180 ++++++++++++++++++++++++++++++++++++++------
 kernel/sched.c             |    7 ++
 4 files changed, 178 insertions(+), 27 deletions(-)
include/linux/perf_event.h +14 -3
index 084f322..10e56f2 100644
@@ -105,6 +105,7 @@ enum perf_sw_ids {
 	PERF_COUNT_SW_PAGE_FAULTS_MAJ		= 6,
 	PERF_COUNT_SW_ALIGNMENT_FAULTS		= 7,
 	PERF_COUNT_SW_EMULATION_FAULTS		= 8,
+	PERF_COUNT_SW_RUNNABLE_TASKS            = 9,
 
 	PERF_COUNT_SW_MAX,			/* non-ABI */
 };
@@ -456,6 +457,7 @@ enum perf_callchain_context {
 #include <linux/pid_namespace.h>
 #include <linux/workqueue.h>
 #include <asm/atomic.h>
+#include <linux/poll.h>
 
 #define PERF_MAX_STACK_DEPTH		255
 
@@ -519,6 +521,8 @@ struct pmu {
 	int (*reset)                    (struct perf_event *event);
 	void (*wakeup)                  (struct perf_event *event);
 	u64 (*read)                     (struct perf_event *event);
+	void (*init)			(struct perf_event *event);
+	unsigned int (*poll)            (struct perf_event *event, struct file* file, poll_table *wait);
 };
 
 /**
@@ -826,13 +830,20 @@ static inline int is_software_event(struct perf_event *event)
 
 extern atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
 
-extern void __perf_sw_event(u32, s64, int, struct pt_regs *, u64);
+extern void __perf_sw_event(u32, s64, int, struct pt_regs *, u64, 
+			struct task_struct* task, int cpu);
+static inline void
+perf_sw_event_target(u32 event_id, s64 nr, int nmi, struct pt_regs *regs,
+		u64 addr, struct task_struct* task, int cpu)
+{
+	if (atomic_read(&perf_swevent_enabled[event_id]))
+		__perf_sw_event(event_id, nr, nmi, regs, addr, task, cpu);
+}
 
 static inline void
 perf_sw_event(u32 event_id, s64 nr, int nmi, struct pt_regs *regs, u64 addr)
 {
-	if (atomic_read(&perf_swevent_enabled[event_id]))
-		__perf_sw_event(event_id, nr, nmi, regs, addr);
+	perf_sw_event_target(event_id, nr, nmi, regs, addr, current, smp_processor_id());
 }
 
 extern void __perf_event_mmap(struct vm_area_struct *vma);
include/linux/sched.h +1 -0
index f2f842d..dce2213 100644
@@ -138,6 +138,7 @@ extern int nr_threads;
 DECLARE_PER_CPU(unsigned long, process_counts);
 extern int nr_processes(void);
 extern unsigned long nr_running(void);
+extern unsigned long nr_running_cpu(int cpu);
 extern unsigned long nr_uninterruptible(void);
 extern unsigned long nr_iowait(void);
 extern unsigned long nr_iowait_cpu(void);
kernel/perf_event.c +156 -24

324 lines not on this page · raw message

kernel/sched.c +7 -0
index 87f1f47..53c679c 100644
@@ -1967,6 +1967,7 @@ static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
 
 	enqueue_task(rq, p, wakeup);
 	inc_nr_running(rq);
+	perf_sw_event_target(PERF_COUNT_SW_RUNNABLE_TASKS, 1, 1, task_pt_regs(p), 0, p, cpu_of(rq));
 }
 
 /*
@@ -1979,6 +1980,7 @@ static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
 
 	dequeue_task(rq, p, sleep);
 	dec_nr_running(rq);
+	perf_sw_event_target(PERF_COUNT_SW_RUNNABLE_TASKS, -1, 1, task_pt_regs(p), 0, p, cpu_of(rq));
 }
 
 /**
@@ -2932,6 +2934,11 @@ unsigned long nr_running(void)
 	return sum;
 }
 
+unsigned long nr_running_cpu(int cpu)
+{
+	return cpu_rq(cpu)->nr_running;
+}
+
 unsigned long nr_uninterruptible(void)
 {
 	unsigned long i, sum = 0;
Signature · 1 line
1.6.6
maintainer
#7 lore raw
In reply to [email protected]
Quoted 2 lines · On Sun, 2010-02-07 at 12:30 +0100, [email protected] wrote:

Here's an initial RFC patch for the parallallism
events for perf_events.

OK, so you managed to rub me totally the wrong way with posting this yesterday:
- you send me each patch twice
- you used the horrible git sendmail default of --chain-reply-to (some day I'll write a script that will detect and auto-bounce series sent to me like that)
- you failed to provide a changelog for any of the patches
- some subjects were long enough to be a changelog

Please don't do that again ;-)

Anyway, it did get me thinking, how about something like the below?

(compile tested only, we probably want a different name than CLONE_SEM, but I failed at coming up with anything better, CLONE_FRED?)

---
 include/linux/sched.h |   11 ++++++++++
 kernel/exit.c         |    5 ++++
 kernel/fork.c         |   24 ++++++++++++++++++++++
 kernel/sched.c        |   53 ++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 92 insertions(+), 1 deletions(-)
include/linux/sched.h +11 -0
index b1b8d84..580c623 100644
@@ -9,6 +9,7 @@
 #define CLONE_FS	0x00000200	/* set if fs info shared between processes */
 #define CLONE_FILES	0x00000400	/* set if open files shared between processes */
 #define CLONE_SIGHAND	0x00000800	/* set if signal handlers and blocked signals shared */
+#define CLONE_SEM	0x00001000	/* set if */
 #define CLONE_PTRACE	0x00002000	/* set if we want to let tracing continue on the child too */
 #define CLONE_VFORK	0x00004000	/* set if the parent wants the child to wake it up on mm_release */
 #define CLONE_PARENT	0x00008000	/* set if we want to have the same parent as the cloner */
@@ -1214,6 +1215,13 @@ struct sched_rt_entity {
 
 struct rcu_node;
 
+struct task_sem {
+	raw_spinlock_t		lock;
+	unsigned int		count;
+	struct list_head	wait_list;
+	atomic_t		ref;
+};
+
 struct task_struct {
 	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
 	void *stack;
@@ -1235,6 +1243,9 @@ struct task_struct {
 	struct sched_entity se;
 	struct sched_rt_entity rt;
 
+	struct task_sem *sem;
+	struct list_head sem_waiter;
+
 #ifdef CONFIG_PREEMPT_NOTIFIERS
 	/* list of struct preempt_notifier: */
 	struct hlist_head preempt_notifiers;
kernel/exit.c +5 -0
index 546774a..f8b9ab3 100644
@@ -991,6 +991,11 @@ NORET_TYPE void do_exit(long code)
 	 */
 	perf_event_exit_task(tsk);
 
+	if (unlikely(tsk->sem) && atomic_dec_and_test(&tsk->sem->ref)) {
+		kfree(tsk->sem);
+		tsk->sem = NULL;
+	}
+
 	exit_notify(tsk, group_dead);
 #ifdef CONFIG_NUMA
 	mpol_put(tsk->mempolicy);
kernel/fork.c +24 -0
index f88bd98..cea102c 100644
@@ -989,6 +989,9 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
 		return ERR_PTR(-EINVAL);
 
+	if ((clone_flags & (CLONE_VFORK|CLONE_SEM)) == (CLONE_VFORK|CLONE_SEM))
+		return ERR_PTR(-EINVAL);
+
 	/*
 	 * Thread groups must share signals as well, and detached threads
 	 * can only be started up within the thread group.
@@ -1023,6 +1026,27 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 	if (!p)
 		goto fork_out;
 
+	if (clone_flags & CLONE_SEM) {
+		INIT_LIST_HEAD(&p->sem_waiter);
+		if (!current->sem) {
+			struct task_sem *sem = 
+				kmalloc(sizeof(struct task_sem), GFP_KERNEL);
+
+			if (!sem)
+				goto bad_fork_free;
+
+			raw_spin_lock_init(&sem->lock);
+			sem->count = 0; /* current is running */
+			INIT_LIST_HEAD(&sem->wait_list);
+			atomic_set(&sem->ref, 2);
+
+			current->sem = sem;
+			p->sem = sem;
+		} else
+			atomic_inc(&current->sem->ref);
+	} else if (current->sem)
+		p->sem = NULL;
+
 	ftrace_graph_init_task(p);
 
 	rt_mutex_init_task(p);
kernel/sched.c +52 -1

73 lines not on this page · raw message

#8 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 2 lines · On Sun, 2010-02-07 at 12:30 +0100, [email protected] wrote:

Here's an initial RFC patch for the parallallism
events for perf_events.

OK, so you managed to rub me totally the wrong way with posting this yesterday:
 - you send me each patch twice
 - you used the horrible git sendmail default of --chain-reply-to (some day I'll write a script that will detect and auto-bounce series sent to me like that)
 - you failed to provide a changelog for any of the patches
 - some subjects were long enough to be a changelog

Quoted 1 line

Please don't do that again ;-)

Sorry for that. I'm still getting used to the thing called 'git'. Apart from obviously sharing its name, there's not so
much we have in common so far... VCS experiences currently don't go far beyond svn and the horrid CVS.
I just used the reply-list from the initial series about this subject, perhaps you were listed twice in that one as well.

Isn't no longer making --chain-reply-to the default an option?

Quoted 3 lines

Anyway, it did get me thinking, how about something like the below? <SNIP>

That pretty much looks like what I need at first sight. I'll definately give it a very close look and test it out.
The perf_event approach would be nice, but the implementations just seem to collide.

Thanks for taking the time anyway,
Stijn

maintainer
#9 lore raw
Quoted 1 line · Stijn Devriendt

Isn't no longer making --chain-reply-to the default an option?

That will hopefully happen on the next major git release (1.7 iirc).