File: task_time.stp

package info (click to toggle)
systemtap 5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 47,556 kB
  • sloc: cpp: 81,117; ansic: 54,933; xml: 49,795; exp: 43,595; sh: 11,526; python: 5,003; perl: 2,252; tcl: 1,312; makefile: 1,006; javascript: 149; lisp: 105; awk: 101; asm: 91; java: 70; sed: 16
file content (260 lines) | stat: -rw-r--r-- 8,013 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Task time query and utility functions.
// Copyright (C) 2009, 2014 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

// <tapsetdescription>
// Task time query and utility functions provide information about
// the time resource usage of the current task. These functions provide
// information about the user time and system time of the current
// task. And provide utility functions to turn the reported times
// into miliseconds and create human readable string representations
// of task time used. The reported times are approximates and should
// be used for "coarse grained" measurements only. The reported user
// and system time are only for the current task, not for the process
// as a whole nor of any time spend by children of the current task.
// </tapsetdescription>

%{
#include <linux/sched.h>		/* includes asm/cputime.h */
#include <linux/time.h>

/* Kernels since 2.6.37 generally have cputime_to_usecs, but not msecs.
 * (ref: kernel commit d57af9b2142f31a39dcfdeb30776baadfc802827)
 * Yet note some kernels (RHEL6) may already have both...  */
#if defined(cputime_to_usecs)
#if !defined(cputime_to_msecs)
/* On some kernels (android 3.0 kernels), the cputime_to_usecs() macro
 * definition inadvertently had a semicolon on the end. The following
 * should handle versions of that macro with and without the extra
 * semi-colon. */
#define cputime_to_msecs(__ct)					\
	_stp_div64(NULL, ({cputime_to_usecs(__ct);}), 1000ULL)
#endif

/* Kernels before 2.6.37 have cputime_to_msecs, but not usecs. */
#elif defined(cputime_to_msecs)
#define cputime_to_usecs(__ct)		(cputime_to_msecs(__ct) * 1000ULL)

/* RHEL4 (2.6.9) kernels have neither, but it's just jiffies anyway. */
#else
#define cputime_to_msecs(__ct)		jiffies_to_msecs(__ct)
#define cputime_to_usecs(__ct)		jiffies_to_usecs(__ct)
#endif
%}

/**
 * sfunction task_utime - User time of the task
 *
 * Description: Returns the user time of the current task in cputime.
 * Does not include any time used by other tasks in this process, nor
 * does it include any time of the children of this task.
 */
function task_utime:long ()
{
	return @const("current->utime")
}

/**
 * sfunction task_utime - User time of the task
 *
 * @tid: Thread id of the given task
 *
 * Description: Returns the user time of the given task in cputime,
 * or zero if the task doesn't exist.
 * Does not include any time used by other tasks in this process, nor
 * does it include any time of the children of this task.
 */
function task_utime:long(tid:long)
{
  task = pid2task(tid);
  if (task != 0)
    return task->utime;
  else
    return 0;
}
function task_utime_tid:long(tid:long)
{
  return task_utime(tid);
}

/**
 * sfunction task_stime - System time of the task
 *
 * Description: Returns the system time of the current task in cputime.
 * Does not include any time used by other tasks in this process, nor
 * does it include any time of the children of this task.
 */
function task_stime:long ()
{
	return @const("current->stime")
}

/**
 * sfunction task_stime - System time of the task
 *
 * @tid: Thread id of the given task
 *
 * Description: Returns the system time of the given task in cputime,
 * or zero if the task doesn't exist.
 * Does not include any time used by other tasks in this process, nor
 * does it include any time of the children of this task.
 */
function task_stime:long(tid:long)
{
  task = pid2task(tid);
  if (task != 0)
    return task->stime;
  else
    return 0;
}
function task_stime_tid:long(tid:long)
{
  return task_stime(tid);
}

/**
 * sfunction task_start_time - Start time of the given task
 *
 * @tid: Thread id of the given task
 *
 * Description: Returns the start time of the given task in nanoseconds since
 * boot time or 0 if the task does not exist.
 */
function task_start_time:long (tid:long) {
  task = pid2task(tid);
  if (task != 0) {
    if (@defined(@task(0)->real_start_time->tv_sec)) {
      time_sec = task->real_start_time->tv_sec
      time_nsec = task->real_start_time->tv_nsec
      return time_sec * 1000000000 + time_nsec
    } else if (@defined(@task(0)->start_boottime)) {
      // renamed to start_boottime kernel commit cf25e24db61cc9d
      return task->start_boottime
    } else {
      // simple u64 in ns since kernel commit 57e0be041d9e2
      return task->real_start_time
    }
  } else return 0
}

/**
 * sfunction cputime_to_msecs - Translates the given cputime into milliseconds
 * @cputime: Time to convert to milliseconds.
 */
function cputime_to_msecs:long (cputime:long)
%{ /* pure */ /* unprivileged */
  STAP_RETVALUE = cputime_to_msecs (STAP_ARG_cputime);
%}

/**
 * sfunction cputime_to_usecs - Translates the given cputime into microseconds
 * @cputime: Time to convert to microseconds.
 */
function cputime_to_usecs:long (cputime:long)
%{ /* pure */ /* unprivileged */
  STAP_RETVALUE = cputime_to_usecs (STAP_ARG_cputime);
%}

/**
 * sfunction msecs_to_string - Human readable string for given milliseconds
 *
 * @msecs: Number of milliseconds to translate.
 *
 * Description: Returns a string representing the number of
 * milliseconds as a human readable string consisting of "XmY.ZZZs",
 * where X is the number of minutes, Y is the number of seconds and
 * ZZZ is the number of milliseconds.
 */
function msecs_to_string:string (msecs:long)
{
  ms = msecs % 1000;
  secs = msecs / 1000;
  mins = secs / 60;
  secs = secs % 60;
  return sprintf("%dm%d.%.3ds", mins, secs, ms);
}

/**
 * sfunction usecs_to_string - Human readable string for given microseconds
 *
 * @usecs: Number of microseconds to translate.
 *
 * Description: Returns a string representing the number of
 * microseconds as a human readable string consisting of "XmY.ZZZZZZs",
 * where X is the number of minutes, Y is the number of seconds and
 * ZZZZZZ is the number of microseconds.
 */
function usecs_to_string:string (usecs:long)
{
  us = usecs % 1000000;
  secs = usecs / 1000000;
  mins = secs / 60;
  secs = secs % 60;
  return sprintf("%dm%d.%.6ds", mins, secs, us);
}

/**
 * sfunction nsecs_to_string - Human readable string for given nanoseconds
 *
 * @nsecs: Number of nanoseconds to translate.
 *
 * Description: Returns a string representing the number of
 * nanoseconds as a human readable string consisting of "XmY.ZZZZZZs",
 * where X is the number of minutes, Y is the number of seconds and
 * ZZZZZZZZZ is the number of nanoseconds.
 */
function nsecs_to_string:string (nsecs:long)
{
  ns = nsecs % 1000000000;
  secs = nsecs / 1000000000;
  mins = secs / 60;
  secs = secs % 60;
  return sprintf("%dm%d.%.9ds", mins, secs, ns);
}

/**
 * sfunction cputime_to_string - Human readable string for given cputime
 *
 * @cputime: Time to translate.
 *
 * Description: Equivalent to calling:
 * msec_to_string (cputime_to_msecs (cputime).
 */
function cputime_to_string:string (cputime:long)
{
  return msecs_to_string (cputime_to_msecs (cputime));
}

/**
 * sfunction task_time_string - Human readable string of task time usage
 *
 * Description: Returns a human readable string showing the user and
 * system time the current task has used up to now.  For example
 * "usr: 0m12.908s, sys: 1m6.851s".
 */
function task_time_string:string ()
{
  return sprintf ("usr: %s, sys: %s",
                  cputime_to_string (task_utime()),
                  cputime_to_string (task_stime()));
}

/**
 * sfunction task_time_string_tid - Human readable string of task time usage
 *
 * @tid: Thread id of the given task
 *
 * Description: Returns a human readable string showing the user and
 * system time the given task has used up to now.  For example
 * "usr: 0m12.908s, sys: 1m6.851s".
 */
function task_time_string_tid:string (tid:long)
{
  return sprintf ("usr: %s, sys: %s",
                  cputime_to_string (task_utime(tid)),
                  cputime_to_string (task_stime(tid)));
}