1 diff --git a/arch/Kconfig b/arch/Kconfig
2 index 4e312ff..ef6a721 100644
5 @@ -43,6 +43,14 @@ config OPROFILE_EVENT_MULTIPLEX
10 + bool "Chopstix (PlanetLab)"
11 + depends on MODULES && OPROFILE
13 + Chopstix allows you to monitor various events by summarizing them
14 + in lossy data structures and transferring these data structures
15 + into user space. If in doubt, say "N".
20 diff --git a/arch/x86/kernel/asm-offsets_32.c b/arch/x86/kernel/asm-offsets_32.c
21 index dfdbf64..29c79b8 100644
22 --- a/arch/x86/kernel/asm-offsets_32.c
23 +++ b/arch/x86/kernel/asm-offsets_32.c
25 #include <linux/signal.h>
26 #include <linux/personality.h>
27 #include <linux/suspend.h>
28 +#include <linux/arrays.h>
29 #include <linux/kbuild.h>
30 #include <asm/ucontext.h>
31 #include <asm/sigframe.h>
33 #include <linux/lguest.h>
34 #include "../../../drivers/lguest/lg.h"
36 +#ifdef CONFIG_CHOPSTIX
37 +#define STACKOFFSET(sym, str, mem) \
38 + DEFINE(sym, offsetof(struct str, mem)-sizeof(struct str));
42 + unsigned long dcookie;
44 + unsigned int number;
48 /* workaround for a warning with -Wmissing-prototypes */
51 @@ -51,6 +64,18 @@ void foo(void)
52 OFFSET(CPUINFO_x86_vendor_id, cpuinfo_x86, x86_vendor_id);
55 +#ifdef CONFIG_CHOPSTIX
56 + STACKOFFSET(TASK_thread, task_struct, thread);
57 + STACKOFFSET(THREAD_esp, thread_struct, sp);
58 + STACKOFFSET(EVENT_event_data, event, event_data);
59 + STACKOFFSET(EVENT_task, event, task);
60 + STACKOFFSET(EVENT_event_type, event, event_type);
61 + STACKOFFSET(SPEC_number, event_spec, number);
62 + DEFINE(EVENT_SIZE, sizeof(struct event));
63 + DEFINE(SPEC_SIZE, sizeof(struct event_spec));
64 + DEFINE(SPEC_EVENT_SIZE, sizeof(struct event_spec)+sizeof(struct event));
67 OFFSET(TI_task, thread_info, task);
68 OFFSET(TI_exec_domain, thread_info, exec_domain);
69 OFFSET(TI_flags, thread_info, flags);
70 diff --git a/arch/x86/kernel/entry_32.S b/arch/x86/kernel/entry_32.S
71 index c097e7d..8eff053 100644
72 --- a/arch/x86/kernel/entry_32.S
73 +++ b/arch/x86/kernel/entry_32.S
74 @@ -526,6 +526,34 @@ ENTRY(system_call)
75 cmpl $(nr_syscalls), %eax
78 +#ifdef CONFIG_CHOPSTIX
79 + /* Move Chopstix syscall probe here */
80 + /* Save and clobber: eax, ecx, ebp */
85 + subl $SPEC_EVENT_SIZE, %esp
86 + movl rec_event, %ecx
89 + # struct event is first, just below %ebp
90 + movl %eax, (SPEC_number-EVENT_SIZE)(%ebp)
91 + leal -SPEC_EVENT_SIZE(%ebp), %eax
92 + movl %eax, EVENT_event_data(%ebp)
93 + movl $7, EVENT_event_type(%ebp)
94 + movl rec_event, %edx
96 + leal -EVENT_SIZE(%ebp), %eax
100 + addl $SPEC_EVENT_SIZE, %esp
106 call *sys_call_table(,%eax,4)
107 movl %eax,PT_EAX(%esp) # store the return value
109 diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
110 index 4302583..85bf9f2 100644
111 --- a/arch/x86/mm/fault.c
112 +++ b/arch/x86/mm/fault.c
113 @@ -62,6 +62,16 @@ static inline int notify_page_fault(struct pt_regs *regs)
117 +#ifdef CONFIG_CHOPSTIX
118 +extern void (*rec_event)(void *,unsigned int);
121 + unsigned long dcookie;
123 + unsigned char reason;
130 diff --git a/block/blk-core.c b/block/blk-core.c
131 index 71da511..1cefcaa 100644
132 --- a/block/blk-core.c
133 +++ b/block/blk-core.c
135 #include <linux/writeback.h>
136 #include <linux/task_io_accounting_ops.h>
137 #include <linux/fault-inject.h>
138 +#include <linux/arrays.h>
140 #define CREATE_TRACE_POINTS
141 #include <trace/events/block.h>
145 +#ifdef CONFIG_CHOPSTIX
146 +extern void (*rec_event)(void *,unsigned int);
149 + unsigned long dcookie;
151 + unsigned char reason;
155 EXPORT_TRACEPOINT_SYMBOL_GPL(block_remap);
156 EXPORT_TRACEPOINT_SYMBOL_GPL(block_rq_remap);
157 EXPORT_TRACEPOINT_SYMBOL_GPL(block_bio_complete);
158 @@ -1478,6 +1489,24 @@ static inline void __generic_make_request(struct bio *bio)
160 trace_block_bio_queue(q, bio);
162 +#ifdef CONFIG_CHOPSTIX
164 + struct event event;
165 + struct event_spec espec;
168 + espec.reason = 0;/*request */
170 + eip = bio->bi_end_io;
171 + event.event_data=&espec;
173 + event.event_type=3;
174 + /* index in the event array currently set up */
175 + /* make sure the counters are loaded in the order we want them to show up*/
176 + (*rec_event)(&event, bio->bi_size);
180 ret = q->make_request_fn(q, bio);
183 diff --git a/drivers/oprofile/cpu_buffer.c b/drivers/oprofile/cpu_buffer.c
184 index a7aae24..9817d91 100644
185 --- a/drivers/oprofile/cpu_buffer.c
186 +++ b/drivers/oprofile/cpu_buffer.c
188 #include <linux/sched.h>
189 #include <linux/oprofile.h>
190 #include <linux/errno.h>
191 +#include <linux/arrays.h>
193 #include "event_buffer.h"
194 #include "cpu_buffer.h"
195 @@ -326,6 +327,17 @@ static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
196 cpu_buf->tracing = 0;
199 +#ifdef CONFIG_CHOPSTIX
203 + unsigned long dcookie;
207 +extern void (*rec_event)(void *,unsigned int);
211 __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
212 unsigned long event, int is_kernel)
213 @@ -360,7 +372,25 @@ void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
214 int is_kernel = !user_mode(regs);
215 unsigned long pc = profile_pc(regs);
217 +#ifdef CONFIG_CHOPSTIX
220 + struct event_spec espec;
221 + esig.task = current;
224 + esig.event_data = &espec;
225 + esig.event_type = event; /* index in the event array currently set up */
226 + /* make sure the counters are loaded in the order we want them to show up*/
227 + (*rec_event)(&esig, 1);
230 + __oprofile_add_ext_sample(pc, regs, event, is_kernel);
233 __oprofile_add_ext_sample(pc, regs, event, is_kernel);
239 diff --git a/fs/bio.c b/fs/bio.c
240 index e0c9e71..796767d 100644
244 #include <linux/mempool.h>
245 #include <linux/workqueue.h>
246 #include <scsi/sg.h> /* for struct sg_iovec */
247 +#include <linux/arrays.h>
249 #include <trace/events/block.h>
251 @@ -48,6 +49,7 @@ struct biovec_slab bvec_slabs[BIOVEC_NR_POOLS] __read_mostly = {
257 * fs_bio_set is the bio_set containing bio and iovec memory pools used by
258 * IO code that does not need private memory pools.
259 @@ -1398,6 +1400,17 @@ void bio_check_pages_dirty(struct bio *bio)
263 +#ifdef CONFIG_CHOPSTIX
266 + unsigned long dcookie;
268 + unsigned char reason;
271 +extern void (*rec_event)(void *,unsigned int);
275 * bio_endio - end I/O on a bio
277 @@ -1419,6 +1432,24 @@ void bio_endio(struct bio *bio, int error)
278 else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
281 +#ifdef CONFIG_CHOPSTIX
283 + struct event event;
284 + struct event_spec espec;
287 + espec.reason = 1;/*response */
289 + eip = bio->bi_end_io;
290 + event.event_data=&espec;
292 + event.event_type=3;
293 + /* index in the event array currently set up */
294 + /* make sure the counters are loaded in the order we want them to show up*/
295 + (*rec_event)(&event, bio->bi_size);
300 bio->bi_end_io(bio, error);
302 diff --git a/fs/exec.c b/fs/exec.c
303 index 0a049b8..6c6bcc5 100644
307 #include <linux/fdtable.h>
308 #include <linux/mm.h>
309 #include <linux/stat.h>
310 +#include <linux/dcookies.h>
311 #include <linux/fcntl.h>
312 #include <linux/smp_lock.h>
313 #include <linux/swap.h>
314 @@ -673,6 +674,13 @@ struct file *open_exec(const char *name)
318 +#ifdef CONFIG_CHOPSTIX
319 + unsigned long cookie;
320 + extern void (*rec_event)(void *, unsigned int);
321 + if (rec_event && !nd.path.dentry->d_cookie)
322 + get_dcookie(&nd.path, &cookie);
328 diff --git a/include/linux/arrays.h b/include/linux/arrays.h
330 index 0000000..7641a3c
332 +++ b/include/linux/arrays.h
334 +#ifndef __ARRAYS_H__
335 +#define __ARRAYS_H__
336 +#include <linux/list.h>
338 +#define SAMPLING_METHOD_DEFAULT 0
339 +#define SAMPLING_METHOD_LOG 1
341 +#define DEFAULT_ARRAY_SIZE 2048
343 +/* Every probe has an array handler */
345 +/* XXX - Optimize this structure */
347 +extern void (*rec_event)(void *,unsigned int);
348 +struct array_handler {
349 + struct list_head link;
350 + unsigned int (*hash_func)(void *);
351 + unsigned int (*sampling_func)(void *,int,void *);
352 + unsigned short size;
353 + unsigned int threshold;
354 + unsigned char **expcount;
355 + unsigned int sampling_method;
356 + unsigned int **arrays;
357 + unsigned int arraysize;
358 + unsigned int num_samples[2];
359 + void **epoch_samples; /* size-sized lists of samples */
360 + unsigned int (*serialize)(void *, void *);
361 + unsigned char code[5];
362 + unsigned int last_threshold;
366 + struct list_head link;
368 + unsigned int count;
369 + unsigned int event_type;
370 + struct task_struct *task;
373 diff --git a/include/linux/mutex.h b/include/linux/mutex.h
374 index 878cab4..8bac64d 100644
375 --- a/include/linux/mutex.h
376 +++ b/include/linux/mutex.h
377 @@ -57,6 +57,9 @@ struct mutex {
381 +#ifdef CONFIG_CHOPSTIX
382 + struct thread_info *owner;
384 #ifdef CONFIG_DEBUG_LOCK_ALLOC
385 struct lockdep_map dep_map;
387 diff --git a/include/linux/sched.h b/include/linux/sched.h
388 index c9d3cae..dd62888 100644
389 --- a/include/linux/sched.h
390 +++ b/include/linux/sched.h
391 @@ -1349,6 +1349,11 @@ struct task_struct {
392 cputime_t utime, stime, utimescaled, stimescaled;
394 cputime_t prev_utime, prev_stime;
396 + #ifdef CONFIG_CHOPSTIX
397 + unsigned long last_interrupted, last_ran_j;
400 unsigned long nvcsw, nivcsw; /* context switch counts */
401 struct timespec start_time; /* monotonic time */
402 struct timespec real_start_time; /* boot based time */
403 diff --git a/kernel/mutex.c b/kernel/mutex.c
404 index 947b3ad..ae1dc67 100644
408 #include <linux/spinlock.h>
409 #include <linux/interrupt.h>
410 #include <linux/debug_locks.h>
411 +#include <linux/arrays.h>
413 +#ifdef CONFIG_CHOPSTIX
416 + unsigned long dcookie;
418 + unsigned char reason;
423 * In the DEBUG case we are using the "NULL fastpath" for mutexes,
424 @@ -49,6 +59,9 @@ void
425 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
427 atomic_set(&lock->count, 1);
428 +#ifdef CONFIG_CHOPSTIX
429 + lock->owner = NULL;
431 spin_lock_init(&lock->wait_lock);
432 INIT_LIST_HEAD(&lock->wait_list);
433 mutex_clear_owner(lock);
434 @@ -247,6 +260,25 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
436 __set_task_state(task, state);
438 +#if 0 && CONFIG_CHOPSTIX
441 + struct event event;
442 + struct event_spec espec;
443 + struct task_struct *p = lock->owner->task;
445 + espec.reason = 0; /* lock */
446 + event.event_data = &espec;
449 + event.event_type = 5;
450 + (*rec_event)(&event, 1);
457 /* didnt get the lock, go to sleep: */
458 spin_unlock_mutex(&lock->wait_lock, flags);
459 preempt_enable_no_resched();
460 @@ -261,6 +293,10 @@ done:
461 mutex_remove_waiter(lock, &waiter, current_thread_info());
462 mutex_set_owner(lock);
464 +#ifdef CONFIG_CHOPSTIX
465 + lock->owner = task_thread_info(task);
468 /* set it to 0 if there are no waiters left: */
469 if (likely(list_empty(&lock->wait_list)))
470 atomic_set(&lock->count, 0);
471 @@ -331,6 +367,25 @@ __mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
473 debug_mutex_wake_waiter(lock, waiter);
475 +#if 0 && CONFIG_CHOPSTIX
478 + struct event event;
479 + struct event_spec espec;
480 + struct task_struct *p = lock->owner->task;
482 + espec.reason = 1; /* unlock */
483 + event.event_data = &espec;
486 + event.event_type = 5;
487 + (*rec_event)(&event, 1);
494 wake_up_process(waiter->task);
497 diff --git a/kernel/sched.c b/kernel/sched.c
498 index 90b63b8..43b728e 100644
502 * 1998-11-19 Implemented schedule_timeout() and related stuff
503 * by Andrea Arcangeli
504 * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
505 - * hybrid priority-list and round-robin design with
506 + * hybrid priority-list and round-robin deventn with
507 * an array-switch method of distributing timeslices
508 * and per-CPU runqueues. Cleanups and useful suggestions
509 * by Davide Libenzi, preemptible kernel bits by Robert Love.
511 #include <linux/ftrace.h>
512 #include <linux/vs_sched.h>
513 #include <linux/vs_cvirt.h>
514 +#include <linux/arrays.h>
517 #include <asm/irq_regs.h>
519 #include "sched_cpupri.h"
521 +#define INTERRUPTIBLE -1
524 #define CREATE_TRACE_POINTS
525 #include <trace/events/sched.h>
527 @@ -2742,6 +2746,10 @@ static void __sched_fork(struct task_struct *p)
528 INIT_HLIST_HEAD(&p->preempt_notifiers);
531 +#ifdef CONFIG_CHOPSTIX
532 + p->last_ran_j = jiffies;
533 + p->last_interrupted = INTERRUPTIBLE;
536 * We mark the process as running here, but have not actually
537 * inserted it onto the runqueue yet. This guarantees that
538 @@ -5659,6 +5667,30 @@ pick_next_task(struct rq *rq)
542 +#ifdef CONFIG_CHOPSTIX
543 +void (*rec_event)(void *,unsigned int) = NULL;
544 +EXPORT_SYMBOL(rec_event);
545 +EXPORT_SYMBOL(in_sched_functions);
549 + unsigned long dcookie;
550 + unsigned int count;
551 + unsigned int reason;
554 +/* To support safe calling from asm */
555 +asmlinkage void rec_event_asm (struct event *event_signature_in, unsigned int count) {
556 + struct pt_regs *regs;
557 + struct event_spec *es = event_signature_in->event_data;
558 + regs = task_pt_regs(current);
559 + event_signature_in->task=current;
561 + event_signature_in->count=1;
562 + (*rec_event)(event_signature_in, count);
567 * schedule() is the main scheduler function.
569 @@ -5706,6 +5738,54 @@ need_resched_nonpreemptible:
570 next = pick_next_task(rq);
572 if (likely(prev != next)) {
574 +#ifdef CONFIG_CHOPSTIX
575 + /* Run only if the Chopstix module so decrees it */
577 + unsigned long diff;
578 + int sampling_reason;
579 + prev->last_ran_j = jiffies;
580 + if (next->last_interrupted!=INTERRUPTIBLE) {
581 + if (next->last_interrupted!=RUNNING) {
582 + diff = (jiffies-next->last_interrupted);
583 + sampling_reason = 0;/* BLOCKING */
586 + diff = jiffies-next->last_ran_j;
587 + sampling_reason = 1;/* PREEMPTION */
590 + if (diff >= HZ/10) {
591 + struct event event;
592 + struct event_spec espec;
593 + struct pt_regs *regs;
594 + regs = task_pt_regs(current);
596 + espec.reason = sampling_reason;
597 + event.event_data=&espec;
600 + event.event_type=2;
601 + /* index in the event array currently set up */
602 + /* make sure the counters are loaded in the order we want them to show up*/
603 + (*rec_event)(&event, diff);
606 + /* next has been elected to run */
607 + next->last_interrupted=0;
609 + /* An uninterruptible process just yielded. Record the current jiffy */
610 + if (prev->state & TASK_UNINTERRUPTIBLE) {
611 + prev->last_interrupted=jiffies;
613 + /* An interruptible process just yielded, or it got preempted.
614 + * Mark it as interruptible */
615 + else if (prev->state & TASK_INTERRUPTIBLE) {
616 + prev->last_interrupted=INTERRUPTIBLE;
621 sched_info_switch(prev, next);
622 perf_event_task_sched_out(prev, next, cpu);
624 diff --git a/mm/memory.c b/mm/memory.c
625 index e828063..6e88fed 100644
629 #include <linux/swapops.h>
630 #include <linux/elf.h>
631 // #include <linux/vs_memory.h>
632 +#include <linux/arrays.h>
635 #include <asm/pgalloc.h>
636 @@ -3070,6 +3071,16 @@ out:
640 +#ifdef CONFIG_CHOPSTIX
641 +extern void (*rec_event)(void *,unsigned int);
644 + unsigned long dcookie;
646 + unsigned char reason;
651 * By the time we get here, we already hold the mm semaphore
653 @@ -3115,6 +3126,24 @@ int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
657 +#ifdef CONFIG_CHOPSTIX
659 + struct event event;
660 + struct event_spec espec;
661 + struct pt_regs *regs;
663 + regs = task_pt_regs(current);
664 + pc = regs->ip & (unsigned int) ~4095;
666 + espec.reason = 0; /* alloc */
667 + event.event_data=&espec;
668 + event.task = current;
670 + event.event_type = 6;
671 + (*rec_event)(&event, 1);
675 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
678 diff --git a/mm/slab.c b/mm/slab.c
679 index ad2828e..5acdf6c 100644
683 #include <linux/fault-inject.h>
684 #include <linux/rtmutex.h>
685 #include <linux/reciprocal_div.h>
686 +#include <linux/arrays.h>
687 #include <linux/debugobjects.h>
688 #include <linux/kmemcheck.h>
690 @@ -252,6 +253,16 @@ struct slab_rcu {
694 +#ifdef CONFIG_CHOPSTIX
695 +extern void (*rec_event)(void *,unsigned int);
698 + unsigned long dcookie;
700 + unsigned char reason;
707 @@ -3400,6 +3411,19 @@ __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
708 kmemleak_alloc_recursive(objp, obj_size(cachep), 1, cachep->flags,
711 +#ifdef CONFIG_CHOPSTIX
712 + if (rec_event && objp) {
713 + struct event event;
714 + struct event_spec espec;
716 + espec.reason = 0; /* alloc */
717 + event.event_data=&espec;
718 + event.task = current;
720 + event.event_type=4;
721 + (*rec_event)(&event, cachep->buffer_size);
726 kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
727 @@ -3512,13 +3536,28 @@ free_done:
728 * Release an obj back to its cache. If the obj has a constructed state, it must
729 * be in this state _before_ it is released. Called with disabled ints.
731 -static inline void __cache_free(struct kmem_cache *cachep, void *objp)
732 +static inline void __cache_free(struct kmem_cache *cachep, void *objp, void *caller)
734 struct array_cache *ac = cpu_cache_get(cachep);
737 kmemleak_free_recursive(objp, cachep->flags);
738 - objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
739 + objp = cache_free_debugcheck(cachep, objp, caller);
741 +#ifdef CONFIG_CHOPSTIX
742 + if (rec_event && objp) {
743 + struct event event;
744 + struct event_spec espec;
746 + espec.reason = 1; /* free */
747 + event.event_data = &espec;
748 + event.task = current;
750 + event.event_type = 4;
751 + (*rec_event)(&event, cachep->buffer_size);
755 vx_slab_free(cachep);
757 kmemcheck_slab_free(cachep, objp, obj_size(cachep));
758 @@ -3720,10 +3759,17 @@ void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
759 EXPORT_SYMBOL(__kmalloc_track_caller);
762 +#ifdef CONFIG_CHOPSTIX
763 +void *__kmalloc(size_t size, gfp_t flags)
765 + return __do_kmalloc(size, flags, __builtin_return_address(0));
768 void *__kmalloc(size_t size, gfp_t flags)
770 return __do_kmalloc(size, flags, NULL);
773 EXPORT_SYMBOL(__kmalloc);
776 @@ -3743,7 +3789,7 @@ void kmem_cache_free(struct kmem_cache *cachep, void *objp)
777 debug_check_no_locks_freed(objp, obj_size(cachep));
778 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
779 debug_check_no_obj_freed(objp, obj_size(cachep));
780 - __cache_free(cachep, objp);
781 + __cache_free(cachep, objp,__builtin_return_address(0));
782 local_irq_restore(flags);
784 trace_kmem_cache_free(_RET_IP_, objp);
785 @@ -3773,7 +3819,7 @@ void kfree(const void *objp)
786 c = virt_to_cache(objp);
787 debug_check_no_locks_freed(objp, obj_size(c));
788 debug_check_no_obj_freed(objp, obj_size(c));
789 - __cache_free(c, (void *)objp);
790 + __cache_free(c, (void *)objp,__builtin_return_address(0));
791 local_irq_restore(flags);
793 EXPORT_SYMBOL(kfree);