Debian Codegraph @

tapset/linux/timestamp_monotonic.stp

raw
1 // timestamp tapset -- monotonic clock variants
2 // Copyright (C) 2011 Red Hat Inc.
3 //
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
7 // later version.
8
9 %{
10 #include <linux/sched.h>
11 #if defined(STAPCONF_LINUX_SCHED_HEADERS)
12 #include <linux/sched/clock.h>
13 #endif
14
15 // Since scripts can run from quite arbitrary contexts, we can only use
16 // cpu_clock once it was made NMI safe. This was in commit def0a9b2 for
17 // HAVE_UNSTABLE_SCHED_CLOCK archs (included in 2.6.32), and commit b9f8fcd5
18 // for the rest (included in 2.6.33). So before that, we'll just pretend that
19 // cpu_clock and local_clock don't exist.
20 #if (!defined (CONFIG_HAVE_UNSTABLE_SCHED_CLOCK) \
21 && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33)))
22 #undef STAPCONF_CPU_CLOCK
23 #undef STAPCONF_LOCAL_CLOCK
24 #endif
25
26 // If we're falling back on gettimeofday, that machinery needs to be started.
27 #if !defined (STAPCONF_CPU_CLOCK)
28 #define STAP_NEED_GETTIMEOFDAY 1
29 #endif
30 %}
31
32
33 /**
34 * sfunction cpu_clock_ns - Number of nanoseconds on the given cpu's clock
35 * @cpu: Which processor's clock to read
36 *
37 * Description: This function returns the number of nanoseconds on the given
38 * cpu's clock. This is always monotonic comparing on the same cpu, but may
39 * have some drift between cpus (within about a jiffy).
40 */
41 function cpu_clock_ns:long (cpu:long) %{ /* pure */ /* unprivileged */
42 #if defined (STAPCONF_CPU_CLOCK)
43 if (likely(STAP_ARG_cpu >= 0 && STAP_ARG_cpu < NR_CPUS && cpu_online(STAP_ARG_cpu)))
44 STAP_RETVALUE = cpu_clock(STAP_ARG_cpu);
45 else
46 {
47 snprintf(CONTEXT->error_buffer, sizeof(CONTEXT->error_buffer),
48 "cpu %lld is not online", STAP_ARG_cpu);
49 CONTEXT->last_error = CONTEXT->error_buffer;
50 }
51 #else
52 /* NOTE: we can't use do_gettimeofday because we could be called from a
53 * context where xtime_lock is already held. See bug #2525. */
54 STAP_RETVALUE = _stp_gettimeofday_ns();
55 if (STAP_RETVALUE < 0)
56 CONTEXT->last_error = "gettimeofday not initialized";
57 #endif
58 %}
59
60 /**
61 * sfunction cpu_clock_us - Number of microseconds on the given cpu's clock
62 * @cpu: Which processor's clock to read
63 *
64 * Description: This function returns the number of microseconds on the given
65 * cpu's clock. This is always monotonic comparing on the same cpu, but may
66 * have some drift between cpus (within about a jiffy).
67 */
68 function cpu_clock_us:long (cpu:long) {
69 return cpu_clock_ns(cpu) / 1000;
70 }
71
72 /**
73 * sfunction cpu_clock_ms - Number of milliseconds on the given cpu's clock
74 * @cpu: Which processor's clock to read
75 *
76 * Description: This function returns the number of milliseconds on the given
77 * cpu's clock. This is always monotonic comparing on the same cpu, but may
78 * have some drift between cpus (within about a jiffy).
79 */
80 function cpu_clock_ms:long (cpu:long) {
81 return cpu_clock_ns(cpu) / 1000000;
82 }
83
84 /**
85 * sfunction cpu_clock_s - Number of seconds on the given cpu's clock
86 * @cpu: Which processor's clock to read
87 *
88 * Description: This function returns the number of seconds on the given cpu's
89 * clock. This is always monotonic comparing on the same cpu, but may have
90 * some drift between cpus (within about a jiffy).
91 */
92 function cpu_clock_s:long (cpu:long) {
93 return cpu_clock_ns(cpu) / 1000000000;
94 }
95
96
97 /**
98 * sfunction local_clock_ns - Number of nanoseconds on the local cpu's clock
99 *
100 * Description: This function returns the number of nanoseconds on the local
101 * cpu's clock. This is always monotonic comparing on the same cpu, but may
102 * have some drift between cpus (within about a jiffy).
103 */
104 function local_clock_ns:long () %{ /* pure */ /* unprivileged */
105 #if defined (STAPCONF_LOCAL_CLOCK)
106 STAP_RETVALUE = local_clock();
107 #elif defined (STAPCONF_CPU_CLOCK)
108 STAP_RETVALUE = cpu_clock(smp_processor_id());
109 #else
110 /* NOTE: we can't use do_gettimeofday because we could be called from a
111 * context where xtime_lock is already held. See bug #2525. */
112 STAP_RETVALUE = _stp_gettimeofday_ns();
113 if (STAP_RETVALUE < 0)
114 CONTEXT->last_error = "gettimeofday not initialized";
115 #endif
116 %}
117
118 /**
119 * sfunction local_clock_us - Number of microseconds on the local cpu's clock
120 *
121 * Description: This function returns the number of microseconds on the local
122 * cpu's clock. This is always monotonic comparing on the same cpu, but may
123 * have some drift between cpus (within about a jiffy).
124 */
125 function local_clock_us:long () {
126 return local_clock_ns() / 1000;
127 }
128
129 /**
130 * sfunction local_clock_ms - Number of milliseconds on the local cpu's clock
131 *
132 * Description: This function returns the number of milliseconds on the local
133 * cpu's clock. This is always monotonic comparing on the same cpu, but may
134 * have some drift between cpus (within about a jiffy).
135 */
136 function local_clock_ms:long () {
137 return local_clock_ns() / 1000000;
138 }
139
140 /**
141 * sfunction local_clock_s - Number of seconds on the local cpu's clock
142 *
143 * Description: This function returns the number of seconds on the local cpu's
144 * clock. This is always monotonic comparing on the same cpu, but may have
145 * some drift between cpus (within about a jiffy).
146 */
147 function local_clock_s:long () {
148 return local_clock_ns() / 1000000000;
149 }
150
151
152 /**
153 * sfunction ktime_get_ns - Number of nanoseconds since boot
154 *
155 * Description: This function returns the system ktime.
156 */
157 function ktime_get_ns:long () %{
158 /* pure */ /* unprivileged */
159 STAP_RETURN ((int64_t) ktime_get_ns());
160 %}