diff options
-rw-r--r-- | doc/SystemTap_Tapset_Reference/tapsets.tmpl | 7 | ||||
-rw-r--r-- | runtime/arith.c | 34 | ||||
-rw-r--r-- | tapset/random.stp | 28 | ||||
-rw-r--r-- | testsuite/systemtap.base/rand.stp | 32 |
4 files changed, 34 insertions, 67 deletions
diff --git a/doc/SystemTap_Tapset_Reference/tapsets.tmpl b/doc/SystemTap_Tapset_Reference/tapsets.tmpl index fcfb1e85..99f72727 100644 --- a/doc/SystemTap_Tapset_Reference/tapsets.tmpl +++ b/doc/SystemTap_Tapset_Reference/tapsets.tmpl @@ -214,4 +214,11 @@ </para> !Itapset/logging.stp </chapter> + <chapter id="random.stp"> + <title>Random functions Tapset</title> + <para> + These functions deal with random number generation. + </para> +!Itapset/random.stp + </chapter> </book> diff --git a/runtime/arith.c b/runtime/arith.c index 4b0e6c9e..4c818a76 100644 --- a/runtime/arith.c +++ b/runtime/arith.c @@ -84,48 +84,34 @@ static int64_t _stp_mod64 (const char **error, int64_t x, int64_t y) } -#ifndef _STP_TEST_ -/** Return a random integer between -n and n. +/** Return a random integer between 0 and n - 1. * @param n how far from zero to go. Make it positive but less than a million or so. */ -static int _stp_random_pm (int n) +static unsigned long _stp_random_u (unsigned long n) { static unsigned long seed; static int initialized_p = 0; - + if (unlikely (! initialized_p)) { seed = (unsigned long) jiffies; initialized_p = 1; } - + /* from glibc rand man page */ seed = seed * 1103515245 + 12345; - - return (seed % (2*n+1)-n); + + return (n == 0 ? 0 : seed % n); } -#endif /* _STP_TEST_ */ -#ifndef _STP_TEST_ -/** Return a random integer between 0 and n - 1. +/** Return a random integer between -n and n. * @param n how far from zero to go. Make it positive but less than a million or so. */ -static int _stp_random_pm_u (int n) +static int _stp_random_pm (unsigned n) { - static unsigned long seed; - static int initialized_p = 0; - - if (unlikely (! initialized_p)) { - seed = (unsigned long) jiffies; - initialized_p = 1; - } - - /* from glibc rand man page */ - seed = seed * 1103515245 + 12345; - - return (seed % n); + return -(int)n + (int)_stp_random_u (2*n + 1); } -#endif /* _STP_TEST_ */ + #if defined (__i386__) || defined (__arm__) diff --git a/tapset/random.stp b/tapset/random.stp index e0a89d19..ada94216 100644 --- a/tapset/random.stp +++ b/tapset/random.stp @@ -1,18 +1,14 @@ -/** @addtogroup library -* The library tapset is a collection of standard functions. -* @{ -*/ - -function randint:long(min:long, max:long) +/** + * sfunction randint - Return a random number between [0,n) + * @n: Number past upper limit of range, not larger than 2**20. + */ +function randint:long(n:long) %{ - - unsigned long difference = (unsigned long)(THIS->max - THIS->min); - - if ( THIS->min >= THIS->max || (THIS->max -THIS->min) > (1024*1024) ){ - CONTEXT->last_error = "either first argument was not strictly less than the second argument, or their difference was greater than (1024*1024)"; - } - THIS->__retvalue = THIS->min + ( _stp_random_pm_u(2147483646) % (difference + 1) ); - +#define RANDMAX (1024*1024) + if (THIS->n > RANDMAX) + CONTEXT->last_error = "range too wide"; + else { + THIS->__retvalue = (uint64_t) _stp_random_u((unsigned long) THIS->n); + } +#undef RANDMAX %} - -/** @} */ diff --git a/testsuite/systemtap.base/rand.stp b/testsuite/systemtap.base/rand.stp index aca80877..d584e5f7 100644 --- a/testsuite/systemtap.base/rand.stp +++ b/testsuite/systemtap.base/rand.stp @@ -8,34 +8,12 @@ function checkStatus(status:long){ probe begin { - - for (i = 0; i < 100; i ++) { - num = randint(-5, 5) - if (num > 5) { - printf("NUMBER TOO HIGH\n") - } - if (num < -5) { - printf("NUMBER TOO LOW\n") + status = 0 + for (i=1; i <= 100; i++){ + if (randint(i) < 0 || randint(i) > i) { + status = 1 } } - - for (i = 0; i < 500; i ++) { - num = randint(-3, 3) - if (num > 3) { - printf("NUMBER TOO HIGH\n") - } - if (num < -3) { - printf("NUMBER TOO LOW\n") - } - } - - status = 0 - for (i=1; i <= 100; i++){ - if ( randint(-1*i,i) < (-1*i) || randint(-1*i,i) > i ){ - status = 1 - } - } - checkStatus(status) - + checkStatus(status) exit() } |