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] 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                        |  149 +++++++++++++++++++++++++++
 2 files changed, 150 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..88eb807
--- /dev/null
+++ b/tapset/stopwatch.stp
@@ -0,0 +1,149 @@
+// 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_id, _stopwatch_starttime, _stopwatch_acc
+global STOPWATCH_STOP = 0, STOPWATCH_START = 1
+
+/**
+ * sfunction create_stopwatch - Creates a running stopwatch
+ *
+ * Returns a handle to identify the stopwatch.
+ */
+function create_stopwatch:long ()
+{
+	_stopwatch_id += 1
+	_stopwatch_starttime[_stopwatch_id] = gettimeofday_ns()
+	return _stopwatch_id
+}
+
+/**
+ * sfunction read_stopwatch_ns - Reads the time in nanoseconds for a stopwatch
+ * @id: handle for the stopwatch
+ * @state: stop (STOPWATCH_STOP) or start (STOPWATCH_START) the stopwatch
+ *
+ * Returns time in nanoseconds for stopwatch @id.
+ */
+function read_stopwatch_ns:long (id:long, state:long)
+{
+	t = gettimeofday_ns()
+	stime = _stopwatch_starttime[id]
+	if (stime != 0)
+		delta = t - stime
+	else
+		delta = 0
+
+	elapsed = _stopwatch_acc[id] + delta
+	if (state == STOPWATCH_STOP) {
+		delete _stopwatch_starttime[id]
+		_stopwatch_acc[id] = elapsed
+	} else {
+		if (stime == 0)
+			_stopwatch_starttime[id] = t
+	}
+	return elapsed
+}
+
+/**
+ * sfunction read_stopwatch_us - Reads the time in microseconds for a stopwatch
+ * @id: handle for the stopwatch
+ * @state: stop (STOPWATCH_STOP) or start (STOPWATCH_START) the stopwatch
+ *
+ * Returns time in microseconds for stopwatch @id.
+ */
+function read_stopwatch_us:long (id:long, state:long)
+{
+	return read_stopwatch_ns(id, state)/1000;
+}
+
+/**
+ * sfunction read_stopwatch_ms - Reads the time in milliseconds for a stopwatch
+ * @id: handle for the stopwatch
+ * @state: stop (STOPWATCH_STOP) or start (STOPWATCH_START) the stopwatch
+ *
+ * Returns time in milliseconds for stopwatch @id.
+ */
+function read_stopwatch_ms:long (id:long, state:long)
+{
+	return read_stopwatch_ns(id, state)/1000000;
+}
+
+/**
+ * sfunction read_stopwatch_ms - Reads the time in milliseconds for a stopwatch
+ * @id: handle for the stopwatch
+ * @state: stop (STOPWATCH_STOP) or start (STOPWATCH_START) the stopwatch
+ *
+ * Returns time in seconds for stopwatch @id.
+ */
+function read_stopwatch_s:long (id:long, state:long)
+{
+	return read_stopwatch_ns(id, state)/1000000000;
+}
+
+/**
+ * sfunction delete_stopwatch - Remove a stopwatch
+ * @id: handle for the stopwatch
+ *
+ * Remove stopwatch @id.
+ */
+function destroy_stopwatch (id:long)
+{
+	delete _stopwatch_starttime[id];
+	delete _stopwatch_acc[id];
+}
+
+/**
+ * sfunction delete_stopwatch_ns - Return time in ns on stopwatch then remove
+ * @id: handle for the stopwatch
+ *
+ * Remove stopwatch @id.
+ */
+function destroy_stopwatch_ns:long (id:long)
+{
+	tmp = read_stopwatch_ns(id, STOPWATCH_START);
+	destroy_stopwatch(id);
+	return tmp;
+}
+
+/**
+ * sfunction delete_stopwatch_us - Return time in microseconds on stopwatch then remove
+ * @id: handle for the stopwatch
+ *
+ * Remove stopwatch @id.
+ */
+function destroy_stopwatch_us:long (id:long)
+{
+	tmp = read_stopwatch_us(id, STOPWATCH_START);
+	destroy_stopwatch(id);
+	return tmp;
+}
+
+/**
+ * sfunction delete_stopwatch_ms - Return time in milliseconds on stopwatch then remove
+ * @id: handle for the stopwatch
+ *
+ * Remove stopwatch @id.
+ */
+function destroy_stopwatch_ms:long (id:long)
+{
+	tmp = read_stopwatch_ms(id, STOPWATCH_START);
+	destroy_stopwatch(id);
+	return tmp;
+}
+
+/**
+ * sfunction delete_stopwatch_s - Return time in seconds on stopwatch then remove
+ * @id: handle for the stopwatch
+ *
+ * Remove stopwatch @id.
+ */
+function destroy_stopwatch_s:long (id:long)
+{
+	tmp = read_stopwatch_s(id, STOPWATCH_START);
+	destroy_stopwatch(id);
+	return tmp;
+}
-- 
1.7.1


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