| 1 |
// Task time query and utility functions. |
| 2 |
// Copyright (C) 2009, 2014 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 |
// <tapsetdescription> |
| 10 |
// Task time query and utility functions provide information about |
| 11 |
// the time resource usage of the current task. These functions provide |
| 12 |
// information about the user time and system time of the current |
| 13 |
// task. And provide utility functions to turn the reported times |
| 14 |
// into miliseconds and create human readable string representations |
| 15 |
// of task time used. The reported times are approximates and should |
| 16 |
// be used for "coarse grained" measurements only. The reported user |
| 17 |
// and system time are only for the current task, not for the process |
| 18 |
// as a whole nor of any time spend by children of the current task. |
| 19 |
// </tapsetdescription> |
| 20 |
|
| 21 |
%{ |
| 22 |
#include <linux/sched.h> /* includes asm/cputime.h */ |
| 23 |
#include <linux/time.h> |
| 24 |
|
| 25 |
/* Kernels since 2.6.37 generally have cputime_to_usecs, but not msecs. |
| 26 |
* (ref: kernel commit d57af9b2142f31a39dcfdeb30776baadfc802827) |
| 27 |
* Yet note some kernels (RHEL6) may already have both... */ |
| 28 |
#if defined(cputime_to_usecs) |
| 29 |
#if !defined(cputime_to_msecs) |
| 30 |
/* On some kernels (android 3.0 kernels), the cputime_to_usecs() macro |
| 31 |
* definition inadvertently had a semicolon on the end. The following |
| 32 |
* should handle versions of that macro with and without the extra |
| 33 |
* semi-colon. */ |
| 34 |
#define cputime_to_msecs(__ct) \ |
| 35 |
_stp_div64(NULL, ({cputime_to_usecs(__ct);}), 1000ULL) |
| 36 |
#endif |
| 37 |
|
| 38 |
/* Kernels before 2.6.37 have cputime_to_msecs, but not usecs. */ |
| 39 |
#elif defined(cputime_to_msecs) |
| 40 |
#define cputime_to_usecs(__ct) (cputime_to_msecs(__ct) * 1000ULL) |
| 41 |
|
| 42 |
/* RHEL4 (2.6.9) kernels have neither, but it's just jiffies anyway. */ |
| 43 |
#else |
| 44 |
#define cputime_to_msecs(__ct) jiffies_to_msecs(__ct) |
| 45 |
#define cputime_to_usecs(__ct) jiffies_to_usecs(__ct) |
| 46 |
#endif |
| 47 |
%} |
| 48 |
|
| 49 |
/** |
| 50 |
* sfunction task_utime - User time of the task |
| 51 |
* |
| 52 |
* Description: Returns the user time of the current task in cputime. |
| 53 |
* Does not include any time used by other tasks in this process, nor |
| 54 |
* does it include any time of the children of this task. |
| 55 |
*/ |
| 56 |
function task_utime:long () |
| 57 |
{ |
| 58 |
return @const("current->utime") |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* sfunction task_utime - User time of the task |
| 63 |
* |
| 64 |
* @tid: Thread id of the given task |
| 65 |
* |
| 66 |
* Description: Returns the user time of the given task in cputime, |
| 67 |
* or zero if the task doesn't exist. |
| 68 |
* Does not include any time used by other tasks in this process, nor |
| 69 |
* does it include any time of the children of this task. |
| 70 |
*/ |
| 71 |
function task_utime:long(tid:long) |
| 72 |
{ |
| 73 |
task = pid2task(tid); |
| 74 |
if (task != 0) |
| 75 |
return task->utime; |
| 76 |
else |
| 77 |
return 0; |
| 78 |
} |
| 79 |
function task_utime_tid:long(tid:long) |
| 80 |
{ |
| 81 |
return task_utime(tid); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* sfunction task_stime - System time of the task |
| 86 |
* |
| 87 |
* Description: Returns the system time of the current task in cputime. |
| 88 |
* Does not include any time used by other tasks in this process, nor |
| 89 |
* does it include any time of the children of this task. |
| 90 |
*/ |
| 91 |
function task_stime:long () |
| 92 |
{ |
| 93 |
return @const("current->stime") |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* sfunction task_stime - System time of the task |
| 98 |
* |
| 99 |
* @tid: Thread id of the given task |
| 100 |
* |
| 101 |
* Description: Returns the system time of the given task in cputime, |
| 102 |
* or zero if the task doesn't exist. |
| 103 |
* Does not include any time used by other tasks in this process, nor |
| 104 |
* does it include any time of the children of this task. |
| 105 |
*/ |
| 106 |
function task_stime:long(tid:long) |
| 107 |
{ |
| 108 |
task = pid2task(tid); |
| 109 |
if (task != 0) |
| 110 |
return task->stime; |
| 111 |
else |
| 112 |
return 0; |
| 113 |
} |
| 114 |
function task_stime_tid:long(tid:long) |
| 115 |
{ |
| 116 |
return task_stime(tid); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* sfunction task_start_time - Start time of the given task |
| 121 |
* |
| 122 |
* @tid: Thread id of the given task |
| 123 |
* |
| 124 |
* Description: Returns the start time of the given task in nanoseconds since |
| 125 |
* boot time or 0 if the task does not exist. |
| 126 |
*/ |
| 127 |
function task_start_time:long (tid:long) { |
| 128 |
task = pid2task(tid); |
| 129 |
if (task != 0) { |
| 130 |
if (@defined(@task(0)->real_start_time->tv_sec)) { |
| 131 |
time_sec = task->real_start_time->tv_sec |
| 132 |
time_nsec = task->real_start_time->tv_nsec |
| 133 |
return time_sec * 1000000000 + time_nsec |
| 134 |
} else if (@defined(@task(0)->start_boottime)) { |
| 135 |
// renamed to start_boottime kernel commit cf25e24db61cc9d |
| 136 |
return task->start_boottime |
| 137 |
} else { |
| 138 |
// simple u64 in ns since kernel commit 57e0be041d9e2 |
| 139 |
return task->real_start_time |
| 140 |
} |
| 141 |
} else return 0 |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* sfunction cputime_to_msecs - Translates the given cputime into milliseconds |
| 146 |
* @cputime: Time to convert to milliseconds. |
| 147 |
*/ |
| 148 |
function cputime_to_msecs:long (cputime:long) |
| 149 |
%{ /* pure */ /* unprivileged */ |
| 150 |
STAP_RETVALUE = cputime_to_msecs (STAP_ARG_cputime); |
| 151 |
%} |
| 152 |
|
| 153 |
/** |
| 154 |
* sfunction cputime_to_usecs - Translates the given cputime into microseconds |
| 155 |
* @cputime: Time to convert to microseconds. |
| 156 |
*/ |
| 157 |
function cputime_to_usecs:long (cputime:long) |
| 158 |
%{ /* pure */ /* unprivileged */ |
| 159 |
STAP_RETVALUE = cputime_to_usecs (STAP_ARG_cputime); |
| 160 |
%} |
| 161 |
|
| 162 |
/** |
| 163 |
* sfunction msecs_to_string - Human readable string for given milliseconds |
| 164 |
* |
| 165 |
* @msecs: Number of milliseconds to translate. |
| 166 |
* |
| 167 |
* Description: Returns a string representing the number of |
| 168 |
* milliseconds as a human readable string consisting of "XmY.ZZZs", |
| 169 |
* where X is the number of minutes, Y is the number of seconds and |
| 170 |
* ZZZ is the number of milliseconds. |
| 171 |
*/ |
| 172 |
function msecs_to_string:string (msecs:long) |
| 173 |
{ |
| 174 |
ms = msecs % 1000; |
| 175 |
secs = msecs / 1000; |
| 176 |
mins = secs / 60; |
| 177 |
secs = secs % 60; |
| 178 |
return sprintf("%dm%d.%.3ds", mins, secs, ms); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* sfunction usecs_to_string - Human readable string for given microseconds |
| 183 |
* |
| 184 |
* @usecs: Number of microseconds to translate. |
| 185 |
* |
| 186 |
* Description: Returns a string representing the number of |
| 187 |
* microseconds as a human readable string consisting of "XmY.ZZZZZZs", |
| 188 |
* where X is the number of minutes, Y is the number of seconds and |
| 189 |
* ZZZZZZ is the number of microseconds. |
| 190 |
*/ |
| 191 |
function usecs_to_string:string (usecs:long) |
| 192 |
{ |
| 193 |
us = usecs % 1000000; |
| 194 |
secs = usecs / 1000000; |
| 195 |
mins = secs / 60; |
| 196 |
secs = secs % 60; |
| 197 |
return sprintf("%dm%d.%.6ds", mins, secs, us); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* sfunction nsecs_to_string - Human readable string for given nanoseconds |
| 202 |
* |
| 203 |
* @nsecs: Number of nanoseconds to translate. |
| 204 |
* |
| 205 |
* Description: Returns a string representing the number of |
| 206 |
* nanoseconds as a human readable string consisting of "XmY.ZZZZZZs", |
| 207 |
* where X is the number of minutes, Y is the number of seconds and |
| 208 |
* ZZZZZZZZZ is the number of nanoseconds. |
| 209 |
*/ |
| 210 |
function nsecs_to_string:string (nsecs:long) |
| 211 |
{ |
| 212 |
ns = nsecs % 1000000000; |
| 213 |
secs = nsecs / 1000000000; |
| 214 |
mins = secs / 60; |
| 215 |
secs = secs % 60; |
| 216 |
return sprintf("%dm%d.%.9ds", mins, secs, ns); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* sfunction cputime_to_string - Human readable string for given cputime |
| 221 |
* |
| 222 |
* @cputime: Time to translate. |
| 223 |
* |
| 224 |
* Description: Equivalent to calling: |
| 225 |
* msec_to_string (cputime_to_msecs (cputime). |
| 226 |
*/ |
| 227 |
function cputime_to_string:string (cputime:long) |
| 228 |
{ |
| 229 |
return msecs_to_string (cputime_to_msecs (cputime)); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* sfunction task_time_string - Human readable string of task time usage |
| 234 |
* |
| 235 |
* Description: Returns a human readable string showing the user and |
| 236 |
* system time the current task has used up to now. For example |
| 237 |
* "usr: 0m12.908s, sys: 1m6.851s". |
| 238 |
*/ |
| 239 |
function task_time_string:string () |
| 240 |
{ |
| 241 |
return sprintf ("usr: %s, sys: %s", |
| 242 |
cputime_to_string (task_utime()), |
| 243 |
cputime_to_string (task_stime())); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* sfunction task_time_string_tid - Human readable string of task time usage |
| 248 |
* |
| 249 |
* @tid: Thread id of the given task |
| 250 |
* |
| 251 |
* Description: Returns a human readable string showing the user and |
| 252 |
* system time the given task has used up to now. For example |
| 253 |
* "usr: 0m12.908s, sys: 1m6.851s". |
| 254 |
*/ |
| 255 |
function task_time_string_tid:string (tid:long) |
| 256 |
{ |
| 257 |
return sprintf ("usr: %s, sys: %s", |
| 258 |
cputime_to_string (task_utime(tid)), |
| 259 |
cputime_to_string (task_stime(tid))); |
| 260 |
} |