]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
perfcounters: add context switch counter
authorIngo Molnar <mingo@elte.hu>
Sun, 14 Dec 2008 11:28:33 +0000 (12:28 +0100)
committerIngo Molnar <mingo@elte.hu>
Sun, 14 Dec 2008 19:31:23 +0000 (20:31 +0100)
Impact: add new feature, new sw counter

Add a counter that counts the number of context-switches a task
is doing.

Signed-off-by: Ingo Molnar <mingo@elte.hu>
include/linux/perf_counter.h
kernel/perf_counter.c

index e5d25bf8f74e869f01ce08eaefe62545275c1e21..d2a16563415fe88b68337ad017a9c94404167c10 100644 (file)
@@ -53,8 +53,8 @@ enum hw_event_types {
        /*
         * Future software events:
         */
-       /* PERF_COUNT_PAGE_FAULTS       = -3,
-          PERF_COUNT_CONTEXT_SWITCHES  = -4, */
+       PERF_COUNT_PAGE_FAULTS          = -3,
+       PERF_COUNT_CONTEXT_SWITCHES     = -4,
 };
 
 /*
index 1f81cde0dc43032fcab08a087be844733afca48f..09287091c5266ff1f8c7042bd98d572eb87e95fa 100644 (file)
@@ -888,6 +888,54 @@ static const struct hw_perf_counter_ops perf_ops_task_clock = {
        .hw_perf_counter_read           = task_clock_perf_counter_read,
 };
 
+static u64 get_context_switches(void)
+{
+       struct task_struct *curr = current;
+
+       return curr->nvcsw + curr->nivcsw;
+}
+
+static void context_switches_perf_counter_update(struct perf_counter *counter)
+{
+       u64 prev, now;
+       s64 delta;
+
+       prev = atomic64_read(&counter->hw.prev_count);
+       now = get_context_switches();
+
+       atomic64_set(&counter->hw.prev_count, now);
+
+       delta = now - prev;
+       if (WARN_ON_ONCE(delta < 0))
+               delta = 0;
+
+       atomic64_add(delta, &counter->count);
+}
+
+static void context_switches_perf_counter_read(struct perf_counter *counter)
+{
+       context_switches_perf_counter_update(counter);
+}
+
+static void context_switches_perf_counter_enable(struct perf_counter *counter)
+{
+       /*
+        * ->nvcsw + curr->nivcsw is a per-task value already,
+        * so we dont have to clear it on switch-in.
+        */
+}
+
+static void context_switches_perf_counter_disable(struct perf_counter *counter)
+{
+       context_switches_perf_counter_update(counter);
+}
+
+static const struct hw_perf_counter_ops perf_ops_context_switches = {
+       .hw_perf_counter_enable         = context_switches_perf_counter_enable,
+       .hw_perf_counter_disable        = context_switches_perf_counter_disable,
+       .hw_perf_counter_read           = context_switches_perf_counter_read,
+};
+
 static const struct hw_perf_counter_ops *
 sw_perf_counter_init(struct perf_counter *counter)
 {
@@ -900,6 +948,9 @@ sw_perf_counter_init(struct perf_counter *counter)
        case PERF_COUNT_TASK_CLOCK:
                hw_ops = &perf_ops_task_clock;
                break;
+       case PERF_COUNT_CONTEXT_SWITCHES:
+               hw_ops = &perf_ops_context_switches;
+               break;
        default:
                break;
        }