public inbox for systemtap@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add the stopwatch.stp tapset
@ 2012-01-26 20:37 William Cohen
  2012-01-27  0:58 ` Frank Ch. Eigler
  0 siblings, 1 reply; 4+ messages in thread
From: William Cohen @ 2012-01-26 20:37 UTC (permalink / raw)
  To: systemtap; +Cc: William Cohen

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add the stopwatch.stp tapset
  2012-01-26 20:37 [PATCH] Add the stopwatch.stp tapset William Cohen
@ 2012-01-27  0:58 ` Frank Ch. Eigler
  2012-01-30 17:53   ` William Cohen
  0 siblings, 1 reply; 4+ messages in thread
From: Frank Ch. Eigler @ 2012-01-27  0:58 UTC (permalink / raw)
  To: William Cohen; +Cc: systemtap

William Cohen <wcohen@redhat.com> writes:

> The stopwatch.stp tapset provides multiple, independent timers to user
> scripts.  Stopwatches can be created by the user script at
> anytime. [...]

Might you consider a somewhat simpler API?

function start_stopwatch (name:string) { }
function stop_stopwatch (name:string) { }
function read_stopwatch_ns:long (name:string) { }
function read_stopwatch_us:long (name:string) { }
function read_stopwatch_ms:long (name:string) { }
function read_stopwatch_s:long (name:string) { }

i.e., no explicit create (the tapset can create it on first encounter
of a new name), no enums (encode start/stop as distinct functions),
and explicit reader functions.

- FChE

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add the stopwatch.stp tapset
  2012-01-27  0:58 ` Frank Ch. Eigler
@ 2012-01-30 17:53   ` William Cohen
  2012-01-30 18:13     ` Frank Ch. Eigler
  0 siblings, 1 reply; 4+ messages in thread
From: William Cohen @ 2012-01-30 17:53 UTC (permalink / raw)
  To: Frank Ch. Eigler; +Cc: systemtap

On 01/26/2012 07:58 PM, Frank Ch. Eigler wrote:
> William Cohen <wcohen@redhat.com> writes:
> 
>> The stopwatch.stp tapset provides multiple, independent timers to user
>> scripts.  Stopwatches can be created by the user script at
>> anytime. [...]
> 
> Might you consider a somewhat simpler API?
> 
> function start_stopwatch (name:string) { }
> function stop_stopwatch (name:string) { }
> function read_stopwatch_ns:long (name:string) { }
> function read_stopwatch_us:long (name:string) { }
> function read_stopwatch_ms:long (name:string) { }
> function read_stopwatch_s:long (name:string) { }
> 
> i.e., no explicit create (the tapset can create it on first encounter
> of a new name), no enums (encode start/stop as distinct functions),
> and explicit reader functions.
> 
> - FChE


Hi Frank,

One thing missing from the simpler API is a way to clean things up.  If a script has many stopwatches of limited lifetime, it would be possible for the stopwatch tapset to cause the associative arrays to fill up.  There needs to be a:

function delete_stopwatch (name:string) {}

probably should have matching optional that can be explicitly called if desired:

function create_stopwatch (name:string) {}

Going through and trying to code up this API this afternoon.

-Will

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add the stopwatch.stp tapset
  2012-01-30 17:53   ` William Cohen
@ 2012-01-30 18:13     ` Frank Ch. Eigler
  0 siblings, 0 replies; 4+ messages in thread
From: Frank Ch. Eigler @ 2012-01-30 18:13 UTC (permalink / raw)
  To: William Cohen; +Cc: systemtap

Hi, Will -

> One thing missing from the simpler API is a way to clean things up.  [...]
> function delete_stopwatch (name:string) {}

OK, good idea.

> probably should have matching optional that can be explicitly called [...]
> function create_stopwatch (name:string) {}

This one sounds like make-work.  It could be that exact empty function, no?

- FChE

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-01-30 18:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-26 20:37 [PATCH] Add the stopwatch.stp tapset William Cohen
2012-01-27  0:58 ` Frank Ch. Eigler
2012-01-30 17:53   ` William Cohen
2012-01-30 18:13     ` Frank Ch. Eigler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).