Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / ktap / samples / schedule / schedtimes.kp
1 #!/usr/vin/env ktap
2
3 #schedtimer.kp
4 #Initially inspired by Systemtap schedtimes.stp
5 #and more bugfree compare with Systemtap's version
6 #
7 #Note that the time value is associate with pid, not with execname strictly,
8 #sometime you will found there have sleep time for command "ls", the reason
9 #is that sleep time is belong to parent process bash, so clear on this.
10
11 RUNNING = 0
12 QUEUED = 1
13 SLEEPING = 2
14 DEAD = 64
15
16 run_time = {}
17 queued_time = {}
18 sleep_time = {}
19 io_wait_time = {}
20
21 pid_state = {}
22 pid_names = {}
23 prev_timestamp = {}
24 io_wait = {}
25
26 trace sched:sched_switch {
27         local prev_comm = arg1
28         local prev_pid = arg2
29         local prev_state = arg4
30         local next_comm = arg5
31         local next_pid = arg6
32         local t = gettimeofday_us()
33
34         if (pid_state[prev_pid] == nil) {
35                 #do nothing
36         } elseif (pid_state[prev_pid] == RUNNING) {
37                 run_time[prev_pid] += t - prev_timestamp[prev_pid]
38         } elseif (pid_state[prev_pid] == QUEUED) {
39                 #found this:
40                 #sched_wakeup comm=foo
41                 #sched_switch prev_comm=foo
42                 run_time[prev_pid] += t - prev_timestamp[prev_pid]
43         }
44
45         pid_names[prev_pid] = prev_comm
46         prev_timestamp[prev_pid] = t
47
48         if (prev_state == DEAD) {
49                 pid_state[prev_pid] = DEAD
50         } elseif (prev_state > 0) {
51                 if (in_iowait() == 1) {
52                         io_wait[prev_pid] = 1
53                 }
54                 pid_state[prev_pid] = SLEEPING
55         } elseif (prev_state == 0) {
56                 pid_state[prev_pid] = QUEUED
57         }
58
59         if (pid_state[next_pid] == nil) {
60                 pid_state[next_pid] = RUNNING
61         } elseif (pid_state[next_pid] == QUEUED) {
62                 queued_time[next_pid] += t - prev_timestamp[next_pid]
63                 pid_state[next_pid] = RUNNING
64         }
65
66         pid_names[next_pid] = next_comm
67         prev_timestamp[next_pid] = t
68 }
69
70 trace sched:sched_wakeup, sched:sched_wakeup_new {
71         local comm = arg1
72         local wakeup_pid = arg2
73         local success = arg4
74         local t = gettimeofday_us()
75
76         if (pid_state[wakeup_pid] == nil) {
77                 #do nothing
78         } elseif (pid_state[wakeup_pid] == SLEEPING) {
79                 local durtion = t - prev_timestamp[wakeup_pid]
80
81                 sleep_time[wakeup_pid] += durtion
82                 if (io_wait[wakeup_pid] == 1) {
83                         io_wait_time[wakeup_pid] += durtion
84                         io_wait[wakeup_pid] = 0
85                 }
86         } elseif (pid_state[wakeup_pid] == RUNNING) {
87                 return
88         }
89
90         pid_names[wakeup_pid] = comm
91         prev_timestamp[wakeup_pid] = t
92         pid_state[wakeup_pid] = QUEUED
93 }
94
95 trace_end {
96         local t = gettimeofday_us()
97
98         for (pid, state in pairs(pid_state)) {
99                 local durtion = t - prev_timestamp[pid]
100                 if (state == SLEEPING) {
101                         sleep_time[pid] += durtion
102                 } elseif (state == QUEUED) {
103                         queued_time[pid] += durtion
104                 } elseif (state == RUNNING) {
105                         run_time[pid] += durtion
106                 }
107         }
108
109         printf ("%16s: %6s %10s %10s %10s %10s %10s\n\n",
110                 "execname", "pid", "run(us)", "sleep(us)", "io_wait(us)",
111                 "queued(us)", "total(us)")
112
113         for (pid, time in pairs(run_time)) {
114                 if (sleep_time[pid] == nil) {
115                         sleep_time[pid] = 0
116                 }
117                 if (queued_time[pid] == nil) {
118                         queue_time[pid] = 0
119                 }
120                 printf("%16s: %6d %10d %10d %10d %10d %10d\n",
121                         pid_names[pid], pid, run_time[pid], sleep_time[pid],
122                         io_wait_time[pid], queued_time[pid],
123                         run_time[pid] + sleep_time[pid] + queued_time[pid]);
124         }
125 }