This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 1/2] Add the stopwatch.stp tapset


The stopwatch.stp tapset provides multiple, independent timers to user
scripts.  Stopwatches can be created by the user script at
anytime. The created stopwatches can be stopped and started by the
user script. The times from the stopwatches can be read in seconds,
milliseconds, microsecons, and nanoseconds.

Signed-off-by: William Cohen <wcohen@redhat.com>
---
 doc/SystemTap_Tapset_Reference/tapsets.tmpl |    1 +
 tapset/stopwatch.stp                        |  126 +++++++++++++++++++++++++++
 2 files changed, 127 insertions(+), 0 deletions(-)
 create mode 100644 tapset/stopwatch.stp

diff --git a/doc/SystemTap_Tapset_Reference/tapsets.tmpl b/doc/SystemTap_Tapset_Reference/tapsets.tmpl
index 41fe99b..4c734c4 100644
--- a/doc/SystemTap_Tapset_Reference/tapsets.tmpl
+++ b/doc/SystemTap_Tapset_Reference/tapsets.tmpl
@@ -140,6 +140,7 @@
 !Itapset/timestamp.stp
 !Itapset/timestamp_gtod.stp
 !Itapset/timestamp_monotonic.stp
+!Itapset/stopwatch.stp
   </chapter>
 
   <chapter id="ctime.stp">
diff --git a/tapset/stopwatch.stp b/tapset/stopwatch.stp
new file mode 100644
index 0000000..97cb39e
--- /dev/null
+++ b/tapset/stopwatch.stp
@@ -0,0 +1,126 @@
+// stopwatch tapset
+// Copyright (C) 2012 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.
+
+global _stopwatch_starttime, _stopwatch_acc
+
+/*
+ * Description of the stopwatch states:
+ * if (! name in _stopwatch_acc) {stopwatch doesn't exist}
+ * if (_stopwatch_starttime[name] == 0 ) {stopwatch not running}
+ * if (_stopwatch_starttime[name] != 0 ) {stopwatch running}
+ */
+
+
+/**
+ * sfunction delete_stopwatch - Remove an existing stopwatch
+ * @name: the stopwatch name
+ *
+ * Remove stopwatch @name.
+ */
+function delete_stopwatch (name:string)
+{
+	delete _stopwatch_starttime[name];
+	delete _stopwatch_acc[name];
+}
+
+
+/**
+ * sfunction start_stopwatch - Start a stopwatch 
+ * @name: the stopwatch name
+ *
+ * Start stopwatch @name.
+ * Creates stopwatch @name if it does not currently exist.
+ */
+function start_stopwatch (name:string)
+{
+	stime = _stopwatch_starttime[name]
+	if (stime == 0)
+		_stopwatch_starttime[name] = gettimeofday_ns()
+	/* make sure there is an entry in _stopwatch_acc */
+	_stopwatch_acc[name] = _stopwatch_acc[name]
+}
+
+
+/**
+ * sfunction stop_stopwatch - Stop a stopwatch 
+ * @name: the stopwatch name
+ *
+ * Stop stopwatch @name.
+ * Creates stopwatch @name if it does not currently exist.
+ */
+function stop_stopwatch (name:string)
+{
+	stime = _stopwatch_starttime[name]
+
+	if (stime != 0)
+		delta =  gettimeofday_ns() - stime
+	else
+		delta = 0
+
+	_stopwatch_starttime[name] = 0
+	_stopwatch_acc[name] = _stopwatch_acc[name] + delta
+}
+
+
+/**
+ * sfunction read_stopwatch_ns - Reads the time in nanoseconds for a stopwatch
+ * @name: stopwatch name
+ *
+ * Returns time in nanoseconds for stopwatch @name.
+ * Creates stopwatch @name if it does not currently exist.
+ */
+function read_stopwatch_ns:long (name:string)
+{
+	stime = _stopwatch_starttime[name]
+	if (stime != 0)
+		delta = gettimeofday_ns() - stime
+	else
+		delta = 0
+
+	_stopwatch_acc[name] = _stopwatch_acc[name]
+	return (_stopwatch_acc[name] + delta)
+}
+
+
+/**
+ * sfunction read_stopwatch_us - Reads the time in microseconds for a stopwatch
+ * @name: stopwatch name
+ *
+ * Returns time in microseconds for stopwatch @name.
+ * Creates stopwatch @name if it does not currently exist.
+ */
+function read_stopwatch_us:long (name:string)
+{
+	return read_stopwatch_ns(name)/1000;
+}
+
+
+/**
+ * sfunction read_stopwatch_ms - Reads the time in milliseconds for a stopwatch
+ * @name: stopwatch name
+ *
+ * Returns time in milliseconds for stopwatch @name.
+ * Creates stopwatch @name if it does not currently exist.
+ */
+function read_stopwatch_ms:long (name:string)
+{
+	return read_stopwatch_ns(name)/1000000;
+}
+
+
+/**
+ * sfunction read_stopwatch_ms - Reads the time in seconds for a stopwatch
+ * @name: stopwatch name
+ *
+ * Returns time in seconds for stopwatch @name.
+ * Creates stopwatch @name if it does not currently exist.
+ */
+function read_stopwatch_s:long (name:string)
+{
+	return read_stopwatch_ns(name)/1000000000;
+}
-- 
1.7.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]