Lost the syscall hunk in my last checkin. This version includes it.
[linux-2.6.git] / linux-2.6-590-chopstix-intern.patch
1 diff -Nurb linux-2.6.22-580/arch/i386/Kconfig linux-2.6.22-590/arch/i386/Kconfig
2 --- linux-2.6.22-580/arch/i386/Kconfig  2009-04-08 16:36:16.000000000 -0400
3 +++ linux-2.6.22-590/arch/i386/Kconfig  2009-04-08 16:37:10.000000000 -0400
4 @@ -1217,6 +1217,14 @@
5  
6  source "arch/i386/oprofile/Kconfig"
7  
8 +config CHOPSTIX
9 +       bool "Chopstix (PlanetLab)"
10 +       depends on MODULES && OPROFILE
11 +       help
12 +         Chopstix allows you to monitor various events by summarizing them
13 +         in lossy data structures and transferring these data structures
14 +         into user space. If in doubt, say "N".
15 +
16  config KPROBES
17         bool "Kprobes (EXPERIMENTAL)"
18         depends on KALLSYMS && EXPERIMENTAL && MODULES
19 diff -Nurb linux-2.6.22-580/arch/i386/kernel/asm-offsets.c linux-2.6.22-590/arch/i386/kernel/asm-offsets.c
20 --- linux-2.6.22-580/arch/i386/kernel/asm-offsets.c     2007-07-08 19:32:17.000000000 -0400
21 +++ linux-2.6.22-590/arch/i386/kernel/asm-offsets.c     2009-04-08 16:37:10.000000000 -0400
22 @@ -9,6 +9,7 @@
23  #include <linux/signal.h>
24  #include <linux/personality.h>
25  #include <linux/suspend.h>
26 +#include <linux/arrays.h>
27  #include <asm/ucontext.h>
28  #include "sigframe.h"
29  #include <asm/pgtable.h>
30 @@ -25,9 +26,19 @@
31  #define OFFSET(sym, str, mem) \
32         DEFINE(sym, offsetof(struct str, mem));
33  
34 +#define STACKOFFSET(sym, str, mem) \
35 +       DEFINE(sym, offsetof(struct str, mem)-sizeof(struct str));
36 +
37  /* workaround for a warning with -Wmissing-prototypes */
38  void foo(void);
39  
40 +struct event_spec {
41 +       unsigned long pc;
42 +       unsigned long dcookie;
43 +       unsigned count;
44 +       unsigned int number;
45 +};
46 +
47  void foo(void)
48  {
49         OFFSET(SIGCONTEXT_eax, sigcontext, eax);
50 @@ -51,7 +62,16 @@
51         OFFSET(CPUINFO_x86_vendor_id, cpuinfo_x86, x86_vendor_id);
52         BLANK();
53  
54 -       OFFSET(TI_task, thread_info, task);
55 +    STACKOFFSET(TASK_thread, task_struct, thread);
56 +    STACKOFFSET(THREAD_esp, thread_struct, esp);
57 +    STACKOFFSET(EVENT_event_data, event, event_data);
58 +    STACKOFFSET(EVENT_task, event, task);
59 +    STACKOFFSET(EVENT_event_type, event, event_type);
60 +    STACKOFFSET(SPEC_number, event_spec, number);
61 +    DEFINE(EVENT_SIZE, sizeof(struct event));
62 +    DEFINE(SPEC_SIZE, sizeof(struct event_spec));
63 +    DEFINE(SPEC_EVENT_SIZE, sizeof(struct event_spec)+sizeof(struct event));
64 +
65         OFFSET(TI_exec_domain, thread_info, exec_domain);
66         OFFSET(TI_flags, thread_info, flags);
67         OFFSET(TI_status, thread_info, status);
68 diff -Nurb linux-2.6.22-580/arch/i386/mm/fault.c linux-2.6.22-590/arch/i386/mm/fault.c
69 --- linux-2.6.22-580/arch/i386/mm/fault.c       2009-04-08 16:36:16.000000000 -0400
70 +++ linux-2.6.22-590/arch/i386/mm/fault.c       2009-04-08 16:37:10.000000000 -0400
71 @@ -60,6 +60,15 @@
72                                           DIE_PAGE_FAULT, &args);
73  }
74  
75 +
76 +extern void (*rec_event)(void *,unsigned int);
77 +struct event_spec {
78 +       unsigned long pc;
79 +       unsigned long dcookie; 
80 +       unsigned count;
81 +       unsigned char reason;
82 +};
83 +
84  /*
85   * Return EIP plus the CS segment base.  The segment limit is also
86   * adjusted, clamped to the kernel/user address space (whichever is
87 @@ -296,6 +305,8 @@
88   *     bit 3 == 1 means use of reserved bit detected
89   *     bit 4 == 1 means fault was an instruction fetch
90   */
91 +
92 +
93  fastcall void __kprobes do_page_fault(struct pt_regs *regs,
94                                       unsigned long error_code)
95  {
96 diff -Nurb linux-2.6.22-580/block/ll_rw_blk.c linux-2.6.22-590/block/ll_rw_blk.c
97 --- linux-2.6.22-580/block/ll_rw_blk.c  2009-04-08 16:36:09.000000000 -0400
98 +++ linux-2.6.22-590/block/ll_rw_blk.c  2009-04-08 16:37:10.000000000 -0400
99 @@ -30,6 +30,7 @@
100  #include <linux/cpu.h>
101  #include <linux/blktrace_api.h>
102  #include <linux/fault-inject.h>
103 +#include <linux/arrays.h>
104  
105  /*
106   * for max sense size
107 @@ -3102,6 +3103,13 @@
108  
109  #endif /* CONFIG_FAIL_MAKE_REQUEST */
110  
111 +extern void (*rec_event)(void *,unsigned int);
112 +struct event_spec {
113 +       unsigned long pc;
114 +       unsigned long dcookie;
115 +       unsigned count;
116 +       unsigned char reason;
117 +};
118  /**
119   * generic_make_request: hand a buffer to its device driver for I/O
120   * @bio:  The bio describing the location in memory and on the device.
121 @@ -3220,7 +3228,23 @@
122                                 goto end_io;
123                         }
124                 }
125 -
126 +#ifdef CONFIG_CHOPSTIX
127 +               if (rec_event) {
128 +                       struct event event;
129 +                       struct event_spec espec;
130 +                       unsigned long eip;
131 +                       
132 +                       espec.reason = 0;/*request */
133 +
134 +                       eip = bio->bi_end_io;
135 +                       event.event_data=&espec;
136 +                       espec.pc=eip;
137 +                       event.event_type=3; 
138 +                       /* index in the event array currently set up */
139 +                       /* make sure the counters are loaded in the order we want them to show up*/ 
140 +                       (*rec_event)(&event, bio->bi_size);
141 +               }
142 +#endif
143                 ret = q->make_request_fn(q, bio);
144         } while (ret);
145  }
146 diff -Nurb linux-2.6.22-580/drivers/oprofile/cpu_buffer.c linux-2.6.22-590/drivers/oprofile/cpu_buffer.c
147 --- linux-2.6.22-580/drivers/oprofile/cpu_buffer.c      2007-07-08 19:32:17.000000000 -0400
148 +++ linux-2.6.22-590/drivers/oprofile/cpu_buffer.c      2009-04-08 16:37:10.000000000 -0400
149 @@ -21,6 +21,7 @@
150  #include <linux/oprofile.h>
151  #include <linux/vmalloc.h>
152  #include <linux/errno.h>
153 +#include <linux/arrays.h>
154   
155  #include "event_buffer.h"
156  #include "cpu_buffer.h"
157 @@ -143,6 +144,17 @@
158                 b->head_pos = 0;
159  }
160  
161 +#ifdef CONFIG_CHOPSTIX
162 +
163 +struct event_spec {
164 +       unsigned int pc;
165 +       unsigned long dcookie;
166 +       unsigned count;
167 +};
168 +
169 +extern void (*rec_event)(void *,unsigned int);
170 +#endif
171 +
172  static inline void
173  add_sample(struct oprofile_cpu_buffer * cpu_buf,
174             unsigned long pc, unsigned long event)
175 @@ -151,6 +163,7 @@
176         entry->eip = pc;
177         entry->event = event;
178         increment_head(cpu_buf);
179 +
180  }
181  
182  static inline void
183 @@ -241,8 +254,28 @@
184  {
185         int is_kernel = !user_mode(regs);
186         unsigned long pc = profile_pc(regs);
187 +       int res=0;
188  
189 +#ifdef CONFIG_CHOPSTIX
190 +       if (rec_event) {
191 +               struct event esig;
192 +               struct event_spec espec;
193 +               esig.task = current;
194 +               espec.pc=pc;
195 +               espec.count=1;
196 +               esig.event_data=&espec;
197 +               esig.event_type=event; /* index in the event array currently set up */
198 +                                       /* make sure the counters are loaded in the order we want them to show up*/ 
199 +               (*rec_event)(&esig, 1);
200 +       }
201 +       else {
202         oprofile_add_ext_sample(pc, regs, event, is_kernel);
203 +       }
204 +#else
205 +       oprofile_add_ext_sample(pc, regs, event, is_kernel);
206 +#endif
207 +
208 +
209  }
210  
211  void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
212 diff -Nurb linux-2.6.22-580/fs/bio.c linux-2.6.22-590/fs/bio.c
213 --- linux-2.6.22-580/fs/bio.c   2007-07-08 19:32:17.000000000 -0400
214 +++ linux-2.6.22-590/fs/bio.c   2009-04-08 16:37:10.000000000 -0400
215 @@ -27,6 +27,7 @@
216  #include <linux/workqueue.h>
217  #include <linux/blktrace_api.h>
218  #include <scsi/sg.h>           /* for struct sg_iovec */
219 +#include <linux/arrays.h>
220  
221  #define BIO_POOL_SIZE 2
222  
223 @@ -47,6 +48,7 @@
224         struct kmem_cache *slab;
225  };
226  
227 +
228  /*
229   * if you change this list, also change bvec_alloc or things will
230   * break badly! cannot be bigger than what you can fit into an
231 @@ -999,6 +1001,14 @@
232         }
233  }
234  
235 +struct event_spec {
236 +       unsigned long pc;
237 +       unsigned long dcookie;
238 +       unsigned count;
239 +       unsigned char reason;
240 +};
241 +
242 +extern void (*rec_event)(void *,unsigned int);
243  /**
244   * bio_endio - end I/O on a bio
245   * @bio:       bio
246 @@ -1028,6 +1038,24 @@
247         bio->bi_size -= bytes_done;
248         bio->bi_sector += (bytes_done >> 9);
249  
250 +#ifdef CONFIG_CHOPSTIX
251 +               if (rec_event) {
252 +                       struct event event;
253 +                       struct event_spec espec;
254 +                       unsigned long eip;
255 +                       
256 +                       espec.reason = 1;/*response */
257 +
258 +                       eip = bio->bi_end_io;
259 +                       event.event_data=&espec;
260 +                       espec.pc=eip;
261 +                       event.event_type=3; 
262 +                       /* index in the event array currently set up */
263 +                       /* make sure the counters are loaded in the order we want them to show up*/ 
264 +                       (*rec_event)(&event, bytes_done);
265 +               }
266 +#endif
267 +
268         if (bio->bi_end_io)
269                 bio->bi_end_io(bio, bytes_done, error);
270  }
271 diff -Nurb linux-2.6.22-580/fs/exec.c linux-2.6.22-590/fs/exec.c
272 --- linux-2.6.22-580/fs/exec.c  2009-04-08 16:36:16.000000000 -0400
273 +++ linux-2.6.22-590/fs/exec.c  2009-04-08 16:40:34.000000000 -0400
274 @@ -27,6 +27,7 @@
275  #include <linux/mman.h>
276  #include <linux/a.out.h>
277  #include <linux/stat.h>
278 +#include <linux/dcookies.h>
279  #include <linux/fcntl.h>
280  #include <linux/smp_lock.h>
281  #include <linux/init.h>
282 @@ -38,7 +39,7 @@
283  #include <linux/binfmts.h>
284  #include <linux/swap.h>
285  #include <linux/utsname.h>
286 -#include <linux/pid_namespace.h>
287 +/*#include <linux/pid_namespace.h>*/
288  #include <linux/module.h>
289  #include <linux/namei.h>
290  #include <linux/proc_fs.h>
291 @@ -488,6 +489,13 @@
292  
293         if (!err) {
294                 struct inode *inode = nd.dentry->d_inode;
295 +#ifdef CONFIG_CHOPSTIX
296 +               unsigned long cookie;
297 +        extern void (*rec_event)(void *, unsigned int);
298 +               if (rec_event && !nd.dentry->d_cookie)
299 +                       get_dcookie(nd.dentry, nd.mnt, &cookie);
300 +#endif
301 +
302                 file = ERR_PTR(-EACCES);
303                 if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
304                     S_ISREG(inode->i_mode)) {
305 @@ -627,8 +635,10 @@
306          * Reparenting needs write_lock on tasklist_lock,
307          * so it is safe to do it under read_lock.
308          */
309 +       /*
310         if (unlikely(tsk->group_leader == child_reaper(tsk)))
311                 tsk->nsproxy->pid_ns->child_reaper = tsk;
312 +               */
313  
314         zap_other_threads(tsk);
315         read_unlock(&tasklist_lock);
316 diff -Nurb linux-2.6.22-580/include/linux/arrays.h linux-2.6.22-590/include/linux/arrays.h
317 --- linux-2.6.22-580/include/linux/arrays.h     1969-12-31 19:00:00.000000000 -0500
318 +++ linux-2.6.22-590/include/linux/arrays.h     2009-04-08 16:37:10.000000000 -0400
319 @@ -0,0 +1,36 @@
320 +#ifndef __ARRAYS_H__
321 +#define __ARRAYS_H__
322 +#include <linux/list.h>
323 +
324 +#define SAMPLING_METHOD_DEFAULT 0
325 +#define SAMPLING_METHOD_LOG 1
326 +
327 +/* Every probe has an array handler */
328 +
329 +/* XXX - Optimize this structure */
330 +
331 +extern void (*rec_event)(void *,unsigned int);
332 +struct array_handler {
333 +       struct list_head link;
334 +       unsigned int (*hash_func)(void *);
335 +       unsigned int (*sampling_func)(void *,int,void *);
336 +       unsigned short size;
337 +       unsigned int threshold;
338 +       unsigned char **expcount;
339 +       unsigned int sampling_method;
340 +       unsigned int **arrays;
341 +       unsigned int arraysize;
342 +       unsigned int num_samples[2];
343 +       void **epoch_samples; /* size-sized lists of samples */
344 +       unsigned int (*serialize)(void *, void *);
345 +       unsigned char code[5];
346 +};
347 +
348 +struct event {
349 +       struct list_head link;
350 +       void *event_data;
351 +       unsigned int count;
352 +       unsigned int event_type;
353 +       struct task_struct *task;
354 +};
355 +#endif
356 diff -Nurb linux-2.6.22-580/include/linux/mutex.h linux-2.6.22-590/include/linux/mutex.h
357 --- linux-2.6.22-580/include/linux/mutex.h      2007-07-08 19:32:17.000000000 -0400
358 +++ linux-2.6.22-590/include/linux/mutex.h      2009-04-08 16:37:10.000000000 -0400
359 @@ -53,6 +53,10 @@
360         struct thread_info      *owner;
361         const char              *name;
362         void                    *magic;
363 +#else
364 +#ifdef CONFIG_CHOPSTIX
365 +       struct thread_info      *owner;
366 +#endif
367  #endif
368  #ifdef CONFIG_DEBUG_LOCK_ALLOC
369         struct lockdep_map      dep_map;
370 diff -Nurb linux-2.6.22-580/include/linux/sched.h linux-2.6.22-590/include/linux/sched.h
371 --- linux-2.6.22-580/include/linux/sched.h      2009-04-08 16:36:17.000000000 -0400
372 +++ linux-2.6.22-590/include/linux/sched.h      2009-04-08 16:37:10.000000000 -0400
373 @@ -850,6 +850,10 @@
374  #endif
375         unsigned long sleep_avg;
376         unsigned long long timestamp, last_ran;
377 +#ifdef CONFIG_CHOPSTIX
378 +       unsigned long last_interrupted, last_ran_j;
379 +#endif
380 +
381         unsigned long long sched_time; /* sched_clock time spent running */
382         enum sleep_type sleep_type;
383  
384 diff -Nurb linux-2.6.22-580/kernel/mutex.c linux-2.6.22-590/kernel/mutex.c
385 --- linux-2.6.22-580/kernel/mutex.c     2007-07-08 19:32:17.000000000 -0400
386 +++ linux-2.6.22-590/kernel/mutex.c     2009-04-08 16:37:10.000000000 -0400
387 @@ -18,6 +18,17 @@
388  #include <linux/spinlock.h>
389  #include <linux/interrupt.h>
390  #include <linux/debug_locks.h>
391 +#include <linux/arrays.h>
392 +
393 +#undef CONFIG_CHOPSTIX
394 +#ifdef CONFIG_CHOPSTIX
395 +struct event_spec {
396 +       unsigned long pc;
397 +       unsigned long dcookie;
398 +       unsigned count;
399 +       unsigned char reason;
400 +};
401 +#endif
402  
403  /*
404   * In the DEBUG case we are using the "NULL fastpath" for mutexes,
405 @@ -43,6 +54,9 @@
406  __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
407  {
408         atomic_set(&lock->count, 1);
409 +#ifdef CONFIG_CHOPSTIX
410 +       lock->owner=NULL;
411 +#endif
412         spin_lock_init(&lock->wait_lock);
413         INIT_LIST_HEAD(&lock->wait_list);
414  
415 @@ -88,6 +102,7 @@
416          * The locking fastpath is the 1->0 transition from
417          * 'unlocked' into 'locked' state.
418          */
419 +
420         __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
421  }
422  
423 @@ -168,6 +183,27 @@
424                 }
425                 __set_task_state(task, state);
426  
427 +#ifdef CONFIG_CHOPSTIX
428 +               if (rec_event) {
429 +                       if (lock->owner) {
430 +                               struct event event;
431 +                               struct event_spec espec;
432 +                               struct task_struct *p = lock->owner->task;
433 +                               /*spin_lock(&p->alloc_lock);*/
434 +                               espec.reason = 0; /* lock */
435 +                               event.event_data=&espec;
436 +                               event.task = p;
437 +                               espec.pc=lock;
438 +                               event.event_type=5; 
439 +                               (*rec_event)(&event, 1);
440 +                               /*spin_unlock(&p->alloc_lock);*/
441 +
442 +                       }
443 +                       else 
444 +                               BUG();
445 +               }
446 +#endif
447 +
448                 /* didnt get the lock, go to sleep: */
449                 spin_unlock_mutex(&lock->wait_lock, flags);
450                 schedule();
451 @@ -177,6 +213,9 @@
452         /* got the lock - rejoice! */
453         mutex_remove_waiter(lock, &waiter, task_thread_info(task));
454         debug_mutex_set_owner(lock, task_thread_info(task));
455 +#ifdef CONFIG_CHOPSTIX
456 +       lock->owner = task_thread_info(task);
457 +#endif
458  
459         /* set it to 0 if there are no waiters left: */
460         if (likely(list_empty(&lock->wait_list)))
461 @@ -202,6 +241,7 @@
462  mutex_lock_nested(struct mutex *lock, unsigned int subclass)
463  {
464         might_sleep();
465 +
466         __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass);
467  }
468  
469 @@ -211,6 +251,7 @@
470  mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
471  {
472         might_sleep();
473 +
474         return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, subclass);
475  }
476  
477 @@ -246,6 +287,23 @@
478  
479                 debug_mutex_wake_waiter(lock, waiter);
480  
481 +#ifdef CONFIG_CHOPSTIX
482 +               if (rec_event) {
483 +                       if (lock->owner) {
484 +                               struct event event;
485 +                               struct event_spec espec;
486 +
487 +                               espec.reason = 1; /* unlock */
488 +                               event.event_data=&espec;
489 +                               event.task = lock->owner->task;
490 +                               espec.pc=lock;
491 +                               event.event_type=5; 
492 +                               (*rec_event)(&event, 1);
493 +                       }
494 +                       else 
495 +                               BUG();
496 +               }
497 +#endif
498                 wake_up_process(waiter->task);
499         }
500  
501 diff -Nurb linux-2.6.22-580/kernel/sched.c linux-2.6.22-590/kernel/sched.c
502 --- linux-2.6.22-580/kernel/sched.c     2009-04-08 16:36:17.000000000 -0400
503 +++ linux-2.6.22-590/kernel/sched.c     2009-04-08 16:37:10.000000000 -0400
504 @@ -10,7 +10,7 @@
505   *  1998-11-19 Implemented schedule_timeout() and related stuff
506   *             by Andrea Arcangeli
507   *  2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
508 - *             hybrid priority-list and round-robin design with
509 + *             hybrid priority-list and round-robin deventn with
510   *             an array-switch method of distributing timeslices
511   *             and per-CPU runqueues.  Cleanups and useful suggestions
512   *             by Davide Libenzi, preemptible kernel bits by Robert Love.
513 @@ -23,6 +23,7 @@
514  #include <linux/nmi.h>
515  #include <linux/init.h>
516  #include <asm/uaccess.h>
517 +#include <linux/arrays.h>
518  #include <linux/highmem.h>
519  #include <linux/smp_lock.h>
520  #include <asm/mmu_context.h>
521 @@ -59,6 +60,9 @@
522  #include <linux/vs_sched.h>
523  #include <linux/vs_cvirt.h>
524  
525 +#define INTERRUPTIBLE   -1
526 +#define RUNNING         0
527 +
528  /*
529   * Scheduler clock - returns current time in nanosec units.
530   * This is default implementation.
531 @@ -431,6 +435,7 @@
532  
533  repeat_lock_task:
534         rq = task_rq(p);
535 +
536         spin_lock(&rq->lock);
537         if (unlikely(rq != task_rq(p))) {
538                 spin_unlock(&rq->lock);
539 @@ -1741,6 +1746,21 @@
540          * event cannot wake it up and insert it on the runqueue either.
541          */
542         p->state = TASK_RUNNING;
543 +#ifdef CONFIG_CHOPSTIX
544 +    /* The jiffy of last interruption */
545 +    if (p->state & TASK_UNINTERRUPTIBLE) {
546 +                               p->last_interrupted=jiffies;
547 +       }
548 +    else
549 +    if (p->state & TASK_INTERRUPTIBLE) {
550 +                               p->last_interrupted=INTERRUPTIBLE;
551 +       }
552 +    else
553 +           p->last_interrupted=RUNNING;
554 +
555 +    /* The jiffy of last execution */ 
556 +       p->last_ran_j=jiffies;
557 +#endif
558  
559         /*
560          * Make sure we do not leak PI boosting priority to the child:
561 @@ -3608,6 +3628,7 @@
562  
563  #endif
564  
565 +
566  static inline int interactive_sleep(enum sleep_type sleep_type)
567  {
568         return (sleep_type == SLEEP_INTERACTIVE ||
569 @@ -3617,16 +3638,28 @@
570  /*
571   * schedule() is the main scheduler function.
572   */
573 +
574 +#ifdef CONFIG_CHOPSTIX
575 +extern void (*rec_event)(void *,unsigned int);
576 +struct event_spec {
577 +       unsigned long pc;
578 +       unsigned long dcookie;
579 +       unsigned int count;
580 +       unsigned int reason;
581 +};
582 +#endif
583 +
584  asmlinkage void __sched schedule(void)
585  {
586         struct task_struct *prev, *next;
587         struct prio_array *array;
588         struct list_head *queue;
589         unsigned long long now;
590 -       unsigned long run_time;
591 +       unsigned long run_time, diff;
592         int cpu, idx, new_prio;
593         long *switch_count;
594         struct rq *rq;
595 +       int sampling_reason;
596  
597         /*
598          * Test if we are atomic.  Since do_exit() needs to call into
599 @@ -3680,6 +3713,7 @@
600         switch_count = &prev->nivcsw;
601         if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
602                 switch_count = &prev->nvcsw;
603 +
604                 if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
605                                 unlikely(signal_pending(prev))))
606                         prev->state = TASK_RUNNING;
607 @@ -3689,6 +3723,17 @@
608                                 vx_uninterruptible_inc(prev);
609                         }
610                         deactivate_task(prev, rq);
611 +#ifdef CONFIG_CHOPSTIX
612 +            /* An uninterruptible process just yielded. Record the current jiffie */
613 +                       if (prev->state & TASK_UNINTERRUPTIBLE) {
614 +                               prev->last_interrupted=jiffies;
615 +                       }
616 +            /* An interruptible process just yielded, or it got preempted. 
617 +             * Mark it as interruptible */
618 +                       else if (prev->state & TASK_INTERRUPTIBLE) {
619 +                               prev->last_interrupted=INTERRUPTIBLE;
620 +                       }
621 +#endif
622                 }
623         }
624  
625 @@ -3765,6 +3810,40 @@
626                 prev->sleep_avg = 0;
627         prev->timestamp = prev->last_ran = now;
628  
629 +#ifdef CONFIG_CHOPSTIX
630 +       /* Run only if the Chopstix module so decrees it */
631 +       if (rec_event) {
632 +               prev->last_ran_j = jiffies;
633 +               if (next->last_interrupted!=INTERRUPTIBLE) {
634 +                       if (next->last_interrupted!=RUNNING) {
635 +                               diff = (jiffies-next->last_interrupted);
636 +                               sampling_reason = 0;/* BLOCKING */
637 +                       }
638 +                       else {
639 +                               diff = jiffies-next->last_ran_j; 
640 +                               sampling_reason = 1;/* PREEMPTION */
641 +                       }
642 +
643 +                       if (diff >= HZ/10) {
644 +                               struct event event;
645 +                               struct event_spec espec;
646 +                struct pt_regs *regs;
647 +                regs = task_pt_regs(current);
648 +
649 +                               espec.reason = sampling_reason;
650 +                               event.event_data=&espec;
651 +                               event.task=next;
652 +                               espec.pc=regs->eip;
653 +                               event.event_type=2; 
654 +                               /* index in the event array currently set up */
655 +                               /* make sure the counters are loaded in the order we want them to show up*/ 
656 +                               (*rec_event)(&event, diff);
657 +                       }
658 +               }
659 +        /* next has been elected to run */
660 +               next->last_interrupted=0;
661 +       }
662 +#endif
663         sched_info_switch(prev, next);
664         if (likely(prev != next)) {
665                 next->timestamp = next->last_ran = now;
666 @@ -4664,6 +4743,7 @@
667         get_task_struct(p);
668         read_unlock(&tasklist_lock);
669  
670 +
671         retval = -EPERM;
672         if ((current->euid != p->euid) && (current->euid != p->uid) &&
673                         !capable(CAP_SYS_NICE))
674 @@ -5032,6 +5112,7 @@
675         jiffies_to_timespec(p->policy == SCHED_FIFO ?
676                                 0 : task_timeslice(p), &t);
677         read_unlock(&tasklist_lock);
678 +
679         retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
680  out_nounlock:
681         return retval;
682 @@ -7275,3 +7356,20 @@
683  }
684  
685  #endif
686 +
687 +#ifdef CONFIG_CHOPSTIX
688 +void (*rec_event)(void *,unsigned int) = NULL;
689 +
690 +/* To support safe calling from asm */
691 +asmlinkage void rec_event_asm (struct event *event_signature_in, unsigned int count) {
692 +    struct pt_regs *regs;
693 +    struct event_spec *es = event_signature_in->event_data;
694 +    regs = task_pt_regs(current);
695 +       event_signature_in->task=current;
696 +       es->pc=regs->eip;
697 +    event_signature_in->count=1;
698 +    (*rec_event)(event_signature_in, count);
699 +}
700 +EXPORT_SYMBOL(rec_event);
701 +EXPORT_SYMBOL(in_sched_functions);
702 +#endif
703 diff -Nurb linux-2.6.22-580/mm/memory.c linux-2.6.22-590/mm/memory.c
704 --- linux-2.6.22-580/mm/memory.c        2009-04-08 16:36:17.000000000 -0400
705 +++ linux-2.6.22-590/mm/memory.c        2009-04-08 16:37:10.000000000 -0400
706 @@ -59,6 +59,7 @@
707  
708  #include <linux/swapops.h>
709  #include <linux/elf.h>
710 +#include <linux/arrays.h>
711  
712  #ifndef CONFIG_NEED_MULTIPLE_NODES
713  /* use the per-pgdat data instead for discontigmem - mbligh */
714 @@ -2601,6 +2602,15 @@
715         return ret;
716  }
717  
718 +extern void (*rec_event)(void *,unsigned int);
719 +struct event_spec {
720 +       unsigned long pc;
721 +       unsigned long dcookie; 
722 +       unsigned count;
723 +       unsigned char reason;
724 +};
725 +
726 +
727  /*
728   * By the time we get here, we already hold the mm semaphore
729   */
730 @@ -2630,6 +2640,24 @@
731         if (!pte)
732                 return VM_FAULT_OOM;
733  
734 +#ifdef CONFIG_CHOPSTIX
735 +       if (rec_event) {
736 +               struct event event;
737 +               struct event_spec espec;
738 +        struct pt_regs *regs;
739 +        unsigned int pc;
740 +        regs = task_pt_regs(current);
741 +        pc = regs->eip & (unsigned int) ~4095;
742 +
743 +               espec.reason = 0; /* alloc */
744 +               event.event_data=&espec;
745 +               event.task = current;
746 +               espec.pc=pc;
747 +               event.event_type=5; 
748 +               (*rec_event)(&event, 1);
749 +       }
750 +#endif
751 +
752         return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
753  }
754  
755 diff -Nurb linux-2.6.22-580/mm/slab.c linux-2.6.22-590/mm/slab.c
756 --- linux-2.6.22-580/mm/slab.c  2009-04-08 16:36:17.000000000 -0400
757 +++ linux-2.6.22-590/mm/slab.c  2009-04-08 16:37:10.000000000 -0400
758 @@ -110,11 +110,13 @@
759  #include       <linux/fault-inject.h>
760  #include       <linux/rtmutex.h>
761  #include       <linux/reciprocal_div.h>
762 +#include <linux/arrays.h>
763  
764  #include       <asm/cacheflush.h>
765  #include       <asm/tlbflush.h>
766  #include       <asm/page.h>
767  
768 +
769  /*
770   * DEBUG       - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
771   *               0 for faster, smaller code (especially in the critical paths).
772 @@ -249,6 +251,14 @@
773         void *addr;
774  };
775  
776 +extern void (*rec_event)(void *,unsigned int);
777 +struct event_spec {
778 +       unsigned long pc;
779 +       unsigned long dcookie; 
780 +       unsigned count;
781 +       unsigned char reason;
782 +};
783 +
784  /*
785   * struct array_cache
786   *
787 @@ -3443,6 +3453,19 @@
788         local_irq_restore(save_flags);
789         objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
790         prefetchw(objp);
791 +#ifdef CONFIG_CHOPSTIX
792 +       if (rec_event && objp) {
793 +               struct event event;
794 +               struct event_spec espec;
795 +
796 +               espec.reason = 0; /* alloc */
797 +               event.event_data=&espec;
798 +               event.task = current;
799 +               espec.pc=caller;
800 +               event.event_type=5; 
801 +               (*rec_event)(&event, cachep->buffer_size);
802 +       }
803 +#endif
804  
805         return objp;
806  }
807 @@ -3549,12 +3572,26 @@
808   * Release an obj back to its cache. If the obj has a constructed state, it must
809   * be in this state _before_ it is released.  Called with disabled ints.
810   */
811 -static inline void __cache_free(struct kmem_cache *cachep, void *objp)
812 +static inline void __cache_free(struct kmem_cache *cachep, void *objp, void *caller)
813  {
814         struct array_cache *ac = cpu_cache_get(cachep);
815  
816         check_irq_off();
817 -       objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
818 +       objp = cache_free_debugcheck(cachep, objp, caller);
819 + #ifdef CONFIG_CHOPSTIX
820 +       if (rec_event && objp) {
821 +               struct event event;
822 +               struct event_spec espec;
823 +  
824 +               espec.reason = 1; /* free */
825 +               event.event_data=&espec;
826 +               event.task = current;
827 +               espec.pc=caller;
828 +               event.event_type=4; 
829 +               (*rec_event)(&event, cachep->buffer_size);
830 +       }
831 + #endif
832 +
833         vx_slab_free(cachep);
834  
835         if (cache_free_alien(cachep, objp))
836 @@ -3651,16 +3688,19 @@
837                         __builtin_return_address(0));
838  }
839  EXPORT_SYMBOL(kmem_cache_alloc_node);
840 -
841  static __always_inline void *
842  __do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
843  {
844         struct kmem_cache *cachep;
845 +       void *ret;
846 +
847  
848         cachep = kmem_find_general_cachep(size, flags);
849         if (unlikely(cachep == NULL))
850                 return NULL;
851 -       return kmem_cache_alloc_node(cachep, flags, node);
852 +       ret = kmem_cache_alloc_node(cachep, flags, node);
853 +       
854 +       return ret;
855  }
856  
857  #ifdef CONFIG_DEBUG_SLAB
858 @@ -3696,6 +3736,7 @@
859                                           void *caller)
860  {
861         struct kmem_cache *cachep;
862 +       void *ret;
863  
864         /* If you want to save a few bytes .text space: replace
865          * __ with kmem_.
866 @@ -3705,9 +3746,10 @@
867         cachep = __find_general_cachep(size, flags);
868         if (unlikely(cachep == NULL))
869                 return NULL;
870 -       return __cache_alloc(cachep, flags, caller);
871 -}
872 +       ret = __cache_alloc(cachep, flags, caller);
873  
874 +       return ret;
875 +}
876  
877  #ifdef CONFIG_DEBUG_SLAB
878  void *__kmalloc(size_t size, gfp_t flags)
879 @@ -3723,10 +3765,17 @@
880  EXPORT_SYMBOL(__kmalloc_track_caller);
881  
882  #else
883 +#ifdef CONFIG_CHOPSTIX
884 +void *__kmalloc(size_t size, gfp_t flags)
885 +{
886 +       return __do_kmalloc(size, flags, __builtin_return_address(0));
887 +}
888 +#else
889  void *__kmalloc(size_t size, gfp_t flags)
890  {
891         return __do_kmalloc(size, flags, NULL);
892  }
893 +#endif
894  EXPORT_SYMBOL(__kmalloc);
895  #endif
896  
897 @@ -3792,7 +3841,7 @@
898  
899         local_irq_save(flags);
900         debug_check_no_locks_freed(objp, obj_size(cachep));
901 -       __cache_free(cachep, objp);
902 +       __cache_free(cachep, objp,__builtin_return_address(0));
903         local_irq_restore(flags);
904  }
905  EXPORT_SYMBOL(kmem_cache_free);
906 @@ -3817,7 +3866,7 @@
907         kfree_debugcheck(objp);
908         c = virt_to_cache(objp);
909         debug_check_no_locks_freed(objp, obj_size(c));
910 -       __cache_free(c, (void *)objp);
911 +       __cache_free(c, (void *)objp,__builtin_return_address(0));
912         local_irq_restore(flags);
913  }
914  EXPORT_SYMBOL(kfree);