| 1 |
// stopwatch tapset |
| 2 |
// Copyright (C) 2012 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 |
@__private30 global _stopwatch_starttime, _stopwatch_acc |
| 10 |
|
| 11 |
/* |
| 12 |
* Description of the stopwatch states: |
| 13 |
* if (! name in _stopwatch_acc) {stopwatch doesn't exist} |
| 14 |
* if (_stopwatch_starttime[name] == 0 ) {stopwatch not running} |
| 15 |
* if (_stopwatch_starttime[name] != 0 ) {stopwatch running} |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* sfunction delete_stopwatch - Remove an existing stopwatch |
| 21 |
* @name: the stopwatch name |
| 22 |
* |
| 23 |
* Remove stopwatch @name. |
| 24 |
*/ |
| 25 |
function delete_stopwatch (name:string) |
| 26 |
{ |
| 27 |
delete _stopwatch_starttime[name]; |
| 28 |
delete _stopwatch_acc[name]; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* sfunction start_stopwatch - Start a stopwatch |
| 34 |
* @name: the stopwatch name |
| 35 |
* |
| 36 |
* Start stopwatch @name. |
| 37 |
* Creates stopwatch @name if it does not currently exist. |
| 38 |
*/ |
| 39 |
function start_stopwatch (name:string) |
| 40 |
{ |
| 41 |
stime = _stopwatch_starttime[name] |
| 42 |
if (stime == 0) |
| 43 |
_stopwatch_starttime[name] = gettimeofday_ns() |
| 44 |
/* make sure there is an entry in _stopwatch_acc */ |
| 45 |
_stopwatch_acc[name] = _stopwatch_acc[name] |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* sfunction stop_stopwatch - Stop a stopwatch |
| 51 |
* @name: the stopwatch name |
| 52 |
* |
| 53 |
* Stop stopwatch @name. |
| 54 |
* Creates stopwatch @name if it does not currently exist. |
| 55 |
*/ |
| 56 |
function stop_stopwatch (name:string) |
| 57 |
{ |
| 58 |
stime = _stopwatch_starttime[name] |
| 59 |
|
| 60 |
if (stime != 0) |
| 61 |
delta = gettimeofday_ns() - stime |
| 62 |
else |
| 63 |
delta = 0 |
| 64 |
|
| 65 |
_stopwatch_starttime[name] = 0 |
| 66 |
_stopwatch_acc[name] = _stopwatch_acc[name] + delta |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* sfunction read_stopwatch_ns - Reads the time in nanoseconds for a stopwatch |
| 72 |
* @name: stopwatch name |
| 73 |
* |
| 74 |
* Returns time in nanoseconds for stopwatch @name. |
| 75 |
* Creates stopwatch @name if it does not currently exist. |
| 76 |
*/ |
| 77 |
function read_stopwatch_ns:long (name:string) |
| 78 |
{ |
| 79 |
stime = _stopwatch_starttime[name] |
| 80 |
if (stime != 0) |
| 81 |
delta = gettimeofday_ns() - stime |
| 82 |
else |
| 83 |
delta = 0 |
| 84 |
|
| 85 |
_stopwatch_acc[name] = _stopwatch_acc[name] |
| 86 |
return (_stopwatch_acc[name] + delta) |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* sfunction read_stopwatch_us - Reads the time in microseconds for a stopwatch |
| 92 |
* @name: stopwatch name |
| 93 |
* |
| 94 |
* Returns time in microseconds for stopwatch @name. |
| 95 |
* Creates stopwatch @name if it does not currently exist. |
| 96 |
*/ |
| 97 |
function read_stopwatch_us:long (name:string) |
| 98 |
{ |
| 99 |
return read_stopwatch_ns(name)/1000; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* sfunction read_stopwatch_ms - Reads the time in milliseconds for a stopwatch |
| 105 |
* @name: stopwatch name |
| 106 |
* |
| 107 |
* Returns time in milliseconds for stopwatch @name. |
| 108 |
* Creates stopwatch @name if it does not currently exist. |
| 109 |
*/ |
| 110 |
function read_stopwatch_ms:long (name:string) |
| 111 |
{ |
| 112 |
return read_stopwatch_ns(name)/1000000; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* sfunction read_stopwatch_s - Reads the time in seconds for a stopwatch |
| 118 |
* @name: stopwatch name |
| 119 |
* |
| 120 |
* Returns time in seconds for stopwatch @name. |
| 121 |
* Creates stopwatch @name if it does not currently exist. |
| 122 |
*/ |
| 123 |
function read_stopwatch_s:long (name:string) |
| 124 |
{ |
| 125 |
return read_stopwatch_ns(name)/1000000000; |
| 126 |
} |