From 5780cc7f8208ebca519d6e076fe403b1220ab865 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 23 Feb 2009 14:22:38 -0800 Subject: Add selective use of _stp_time_init/kill (PR9822) Our gettimeofday runtime has frequent wakeups to stay in sync with kernel time, but this is wasted effort if gettimeofday is not used in the script. This patch moves the calls to _stp_time_init and _stp_time_kill into begin and end/error probes, which only get pulled in if one of the gettimeofday variants is called. --- runtime/time.c | 8 ++++- runtime/transport/transport.c | 6 ---- tapset/timestamp.stp | 43 --------------------------- tapset/timestamp_gtod.stp | 68 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 tapset/timestamp_gtod.stp diff --git a/runtime/time.c b/runtime/time.c index ad7cef9d..58c23e57 100644 --- a/runtime/time.c +++ b/runtime/time.c @@ -223,6 +223,7 @@ _stp_kill_time(void) #endif _stp_free_percpu(stp_time); + stp_time = NULL; } } @@ -232,6 +233,8 @@ _stp_init_time(void) { int ret = 0; + _stp_kill_time(); + stp_time = _stp_alloc_percpu(sizeof(stp_time_t)); if (unlikely(stp_time == 0)) return -1; @@ -263,7 +266,7 @@ _stp_init_time(void) } } #endif - if (ret) + if (ret) _stp_kill_time(); return ret; } @@ -278,6 +281,9 @@ _stp_gettimeofday_ns(void) stp_time_t *time; int i = 0; + if (!stp_time) + return -1; + preempt_disable(); time = per_cpu_ptr(stp_time, smp_processor_id()); diff --git a/runtime/transport/transport.c b/runtime/transport/transport.c index 0755781e..7fcebd42 100644 --- a/runtime/transport/transport.c +++ b/runtime/transport/transport.c @@ -188,7 +188,6 @@ static void _stp_transport_close() _stp_unregister_ctl_channel(); if (_stp_utt) utt_trace_remove(_stp_utt); - _stp_kill_time(); /* Go to a beach. Drink a beer. */ _stp_print_cleanup(); /* free print buffers */ _stp_mem_debug_done(); dbug_trans(1, "---- CLOSED ----\n"); @@ -244,10 +243,6 @@ static int _stp_transport_init(void) dbug_trans(1, "Using %d subbufs of size %d\n", _stp_nsubbufs, _stp_subbuf_size); } - /* initialize timer code */ - if (_stp_init_time()) - return -1; - #if !defined (STP_OLD_TRANSPORT) || defined (STP_BULKMODE) /* open utt (relayfs) channel to send data to userspace */ _stp_utt = _stp_utt_open(); @@ -286,7 +281,6 @@ err1: if (_stp_utt) utt_trace_remove(_stp_utt); err0: - _stp_kill_time(); return -1; } diff --git a/tapset/timestamp.stp b/tapset/timestamp.stp index 29763cb9..ce8f7558 100644 --- a/tapset/timestamp.stp +++ b/tapset/timestamp.stp @@ -7,10 +7,6 @@ // Public License (GPL); either version 2, or (at your option) any // later version. -%{ -#include -%} - /** * sfunction get_cycles - Processor cycle count. * @@ -21,43 +17,4 @@ function get_cycles:long () %{ /* pure */ THIS->__retvalue = (int64_t) c; %} - -/** - * sfunction gettimeofday_ns - Number of nanoseconds since UNIX epoch. - * - * Return the number of nanoseconds since the UNIX epoch. - */ -function gettimeofday_ns:long () %{ /* pure */ - /* NOTE: we can't use do_gettimeofday because we could be called from a - * context where xtime_lock is already held. See bug #2525. */ - THIS->__retvalue = _stp_gettimeofday_ns(); -%} - -/** - * sfunction gettimeofday_us - Number of microseconds since UNIX epoch. - * - * Return the number of microseconds since the UNIX epoch. - */ -function gettimeofday_us:long () { - return gettimeofday_ns() / 1000; -} - -/** - * sfunction gettimeofday_ms - Number of milliseconds since UNIX epoch. - * - * Return the number of milliseconds since the UNIX epoch. - */ -function gettimeofday_ms:long () { - return gettimeofday_ns() / 1000000; -} - -/** - * sfunction gettimeofday_s - Number of seconds since UNIX epoch. - * - * Return the number of seconds since the UNIX epoch. - */ -function gettimeofday_s:long () { - return gettimeofday_ns() / 1000000000; -} - // likewise jiffies, monotonic_clock ... diff --git a/tapset/timestamp_gtod.stp b/tapset/timestamp_gtod.stp new file mode 100644 index 00000000..43b127dc --- /dev/null +++ b/tapset/timestamp_gtod.stp @@ -0,0 +1,68 @@ +// timestamp tapset -- gettimeofday variants +// Copyright (C) 2005-2009 Red Hat Inc. +// Copyright (C) 2006 Intel Corporation. +// +// 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. + +function _gettimeofday_init:long() %{ + THIS->__retvalue = _stp_init_time(); /* Kick off the Big Bang. */ +%} + +probe begin(-0x8000000000000000) { + if (_gettimeofday_init() != 0) + error("couldn't initialize gettimeofday") +} + +function _gettimeofday_kill() %{ + _stp_kill_time(); /* Go to a beach. Drink a beer. */ +%} + +probe end(0x7FFFFFFFFFFFFFFF), error(0x7FFFFFFFFFFFFFFF) { + _gettimeofday_kill() +} + + +/** + * sfunction gettimeofday_ns - Number of nanoseconds since UNIX epoch. + * + * Return the number of nanoseconds since the UNIX epoch. + */ +function gettimeofday_ns:long () %{ /* pure */ + /* NOTE: we can't use do_gettimeofday because we could be called from a + * context where xtime_lock is already held. See bug #2525. */ + THIS->__retvalue = _stp_gettimeofday_ns(); + if (THIS->__retvalue < 0) + CONTEXT->last_error = "gettimeofday not initialized"; +%} + +/** + * sfunction gettimeofday_us - Number of microseconds since UNIX epoch. + * + * Return the number of microseconds since the UNIX epoch. + */ +function gettimeofday_us:long () { + return gettimeofday_ns() / 1000; +} + +/** + * sfunction gettimeofday_ms - Number of milliseconds since UNIX epoch. + * + * Return the number of milliseconds since the UNIX epoch. + */ +function gettimeofday_ms:long () { + return gettimeofday_ns() / 1000000; +} + +/** + * sfunction gettimeofday_s - Number of seconds since UNIX epoch. + * + * Return the number of seconds since the UNIX epoch. + */ +function gettimeofday_s:long () { + return gettimeofday_ns() / 1000000000; +} + +// likewise jiffies, monotonic_clock ... -- cgit From bf2a0399419306bf26cd57236822c51be893dfb0 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 23 Feb 2009 15:08:02 -0800 Subject: Add a testcase for gtod init/kill This test simply checks that _gettimeofday_init and _gettimeofday_kill are pulled in when gettimeofday is used, and also that they are NOT pulled in when gettimeofday is not used. --- testsuite/systemtap.base/gtod_init.exp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 testsuite/systemtap.base/gtod_init.exp diff --git a/testsuite/systemtap.base/gtod_init.exp b/testsuite/systemtap.base/gtod_init.exp new file mode 100644 index 00000000..48616b1f --- /dev/null +++ b/testsuite/systemtap.base/gtod_init.exp @@ -0,0 +1,29 @@ +# test for checking initialization of the time subsystem +set test "gtod_init" + +# check that init and kill are both present with a gettimeofday +set time_init 0 +set time_kill 0 +spawn stap -p2 -e {probe begin { println(gettimeofday_s()) }} +expect { + -timeout 120 + -re {\n_gettimeofday_init:} { incr time_init; exp_continue } + -re {\n_gettimeofday_kill:} { incr time_kill; exp_continue } + timeout { fail "$test (timeout)" } + eof { + if {$time_init == 1} { pass "$test (init)" } { fail "$test (init $time_init)" } + if {$time_kill == 1} { pass "$test (kill)" } { fail "$test (kill $time_kill)" } + } +} +wait + +# check that init and kill are both NOT present without a gettimeofday +spawn stap -p2 -e {probe begin { println(get_cycles()) }} +expect { + -timeout 120 + -re {\n_gettimeofday_init:} { fail "$test (bad init)" } + -re {\n_gettimeofday_kill:} { fail "$test (bad kill)" } + timeout { fail "$test (timeout)" } + eof { pass "$test (no init/kill)" } +} +wait -- cgit