Merge branch 'timers-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[safe/jmp/linux-2.6] / kernel / trace / trace_workqueue.c
index 4664990..40cafb0 100644 (file)
@@ -6,9 +6,10 @@
  */
 
 
-#include <trace/workqueue.h>
+#include <trace/events/workqueue.h>
 #include <linux/list.h>
 #include <linux/percpu.h>
+#include <linux/kref.h>
 #include "trace_stat.h"
 #include "trace.h"
 
 /* A cpu workqueue thread */
 struct cpu_workqueue_stats {
        struct list_head            list;
-/* Useful to know if we print the cpu headers */
-       bool                        first_entry;
+       struct kref                 kref;
        int                         cpu;
-       pid_t                       pid;
+       pid_t                       pid;
 /* Can be inserted from interrupt or user context, need to be atomic */
-       atomic_t                    inserted;
+       atomic_t                    inserted;
 /*
  *  Don't need to be atomic, works are serialized in a single workqueue thread
  *  on a single CPU.
  */
-       unsigned int                executed;
+       unsigned int                executed;
 };
 
 /* List of workqueue threads on one cpu */
@@ -41,18 +41,22 @@ struct workqueue_global_stats {
 static DEFINE_PER_CPU(struct workqueue_global_stats, all_workqueue_stat);
 #define workqueue_cpu_stat(cpu) (&per_cpu(all_workqueue_stat, cpu))
 
+static void cpu_workqueue_stat_free(struct kref *kref)
+{
+       kfree(container_of(kref, struct cpu_workqueue_stats, kref));
+}
+
 /* Insertion of a work */
 static void
 probe_workqueue_insertion(struct task_struct *wq_thread,
                          struct work_struct *work)
 {
        int cpu = cpumask_first(&wq_thread->cpus_allowed);
-       struct cpu_workqueue_stats *node, *next;
+       struct cpu_workqueue_stats *node;
        unsigned long flags;
 
        spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
-       list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
-                                                       list) {
+       list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
                if (node->pid == wq_thread->pid) {
                        atomic_inc(&node->inserted);
                        goto found;
@@ -69,12 +73,11 @@ probe_workqueue_execution(struct task_struct *wq_thread,
                          struct work_struct *work)
 {
        int cpu = cpumask_first(&wq_thread->cpus_allowed);
-       struct cpu_workqueue_stats *node, *next;
+       struct cpu_workqueue_stats *node;
        unsigned long flags;
 
        spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
-       list_for_each_entry_safe(node, next, &workqueue_cpu_stat(cpu)->list,
-                                                       list) {
+       list_for_each_entry(node, &workqueue_cpu_stat(cpu)->list, list) {
                if (node->pid == wq_thread->pid) {
                        node->executed++;
                        goto found;
@@ -91,7 +94,7 @@ static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu)
        struct cpu_workqueue_stats *cws;
        unsigned long flags;
 
-       WARN_ON(cpu < 0 || cpu >= num_possible_cpus());
+       WARN_ON(cpu < 0);
 
        /* Workqueues are sometimes created in atomic context */
        cws = kzalloc(sizeof(struct cpu_workqueue_stats), GFP_ATOMIC);
@@ -99,16 +102,12 @@ static void probe_workqueue_creation(struct task_struct *wq_thread, int cpu)
                pr_warning("trace_workqueue: not enough memory\n");
                return;
        }
-       tracing_record_cmdline(wq_thread);
-
        INIT_LIST_HEAD(&cws->list);
+       kref_init(&cws->kref);
        cws->cpu = cpu;
-
        cws->pid = wq_thread->pid;
 
        spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
-       if (list_empty(&workqueue_cpu_stat(cpu)->list))
-               cws->first_entry = true;
        list_add_tail(&cws->list, &workqueue_cpu_stat(cpu)->list);
        spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
 }
@@ -126,7 +125,7 @@ static void probe_workqueue_destruction(struct task_struct *wq_thread)
                                                        list) {
                if (node->pid == wq_thread->pid) {
                        list_del(&node->list);
-                       kfree(node);
+                       kref_put(&node->kref, cpu_workqueue_stat_free);
                        goto found;
                }
        }
@@ -145,16 +144,18 @@ static struct cpu_workqueue_stats *workqueue_stat_start_cpu(int cpu)
 
        spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
 
-       if (!list_empty(&workqueue_cpu_stat(cpu)->list))
+       if (!list_empty(&workqueue_cpu_stat(cpu)->list)) {
                ret = list_entry(workqueue_cpu_stat(cpu)->list.next,
                                 struct cpu_workqueue_stats, list);
+               kref_get(&ret->kref);
+       }
 
        spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
 
        return ret;
 }
 
-static void *workqueue_stat_start(void)
+static void *workqueue_stat_start(struct tracer_stat *trace)
 {
        int cpu;
        void *ret = NULL;
@@ -170,49 +171,61 @@ static void *workqueue_stat_start(void)
 static void *workqueue_stat_next(void *prev, int idx)
 {
        struct cpu_workqueue_stats *prev_cws = prev;
+       struct cpu_workqueue_stats *ret;
        int cpu = prev_cws->cpu;
        unsigned long flags;
-       void *ret = NULL;
 
        spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
        if (list_is_last(&prev_cws->list, &workqueue_cpu_stat(cpu)->list)) {
                spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
-               for (++cpu ; cpu < num_possible_cpus(); cpu++) {
-                       ret = workqueue_stat_start_cpu(cpu);
-                       if (ret)
-                               return ret;
-               }
-               return NULL;
+               do {
+                       cpu = cpumask_next(cpu, cpu_possible_mask);
+                       if (cpu >= nr_cpu_ids)
+                               return NULL;
+               } while (!(ret = workqueue_stat_start_cpu(cpu)));
+               return ret;
+       } else {
+               ret = list_entry(prev_cws->list.next,
+                                struct cpu_workqueue_stats, list);
+               kref_get(&ret->kref);
        }
        spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
 
-       return list_entry(prev_cws->list.next, struct cpu_workqueue_stats,
-                         list);
+       return ret;
 }
 
 static int workqueue_stat_show(struct seq_file *s, void *p)
 {
        struct cpu_workqueue_stats *cws = p;
-       unsigned long flags;
-       int cpu = cws->cpu;
+       struct pid *pid;
+       struct task_struct *tsk;
+
+       pid = find_get_pid(cws->pid);
+       if (pid) {
+               tsk = get_pid_task(pid, PIDTYPE_PID);
+               if (tsk) {
+                       seq_printf(s, "%3d %6d     %6u       %s\n", cws->cpu,
+                                  atomic_read(&cws->inserted), cws->executed,
+                                  tsk->comm);
+                       put_task_struct(tsk);
+               }
+               put_pid(pid);
+       }
 
-       seq_printf(s, "%3d %6d     %6u       %s\n", cws->cpu,
-                  atomic_read(&cws->inserted),
-                  cws->executed,
-                  trace_find_cmdline(cws->pid));
+       return 0;
+}
 
-       spin_lock_irqsave(&workqueue_cpu_stat(cpu)->lock, flags);
-       if (&cws->list == workqueue_cpu_stat(cpu)->list.next)
-               seq_printf(s, "\n");
-       spin_unlock_irqrestore(&workqueue_cpu_stat(cpu)->lock, flags);
+static void workqueue_stat_release(void *stat)
+{
+       struct cpu_workqueue_stats *node = stat;
 
-       return 0;
+       kref_put(&node->kref, cpu_workqueue_stat_free);
 }
 
 static int workqueue_stat_headers(struct seq_file *s)
 {
        seq_printf(s, "# CPU  INSERTED  EXECUTED   NAME\n");
-       seq_printf(s, "# |      |         |          |\n\n");
+       seq_printf(s, "# |      |         |          |\n");
        return 0;
 }
 
@@ -221,6 +234,7 @@ struct tracer_stat workqueue_stats __read_mostly = {
        .stat_start = workqueue_stat_start,
        .stat_next = workqueue_stat_next,
        .stat_show = workqueue_stat_show,
+       .stat_release = workqueue_stat_release,
        .stat_headers = workqueue_stat_headers
 };