From dc0c46f5f74a9e0468236b31c8d9364eb24c3624 Mon Sep 17 00:00:00 2001 From: Charley Wang Date: Wed, 23 Sep 2009 14:00:33 -0400 Subject: PR1062: runtime function * arith.c (_stp_random_pm_u): New function. --- runtime/arith.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/runtime/arith.c b/runtime/arith.c index d1d0da29..4b0e6c9e 100644 --- a/runtime/arith.c +++ b/runtime/arith.c @@ -106,6 +106,28 @@ static int _stp_random_pm (int n) #endif /* _STP_TEST_ */ +#ifndef _STP_TEST_ +/** 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_u (int 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); +} +#endif /* _STP_TEST_ */ + + #if defined (__i386__) || defined (__arm__) /* 64-bit division functions extracted from libgcc */ -- cgit From 09846ceb72c27dfe87f0b5c8d0a6296fc09bbc36 Mon Sep 17 00:00:00 2001 From: Roland Grunberg Date: Wed, 23 Sep 2009 13:46:05 -0400 Subject: PR10632: tapset: randint() function + tests --- tapset/random.stp | 18 +++++++++++++++++ testsuite/systemtap.base/rand.exp | 3 +++ testsuite/systemtap.base/rand.stp | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 tapset/random.stp create mode 100644 testsuite/systemtap.base/rand.exp create mode 100644 testsuite/systemtap.base/rand.stp diff --git a/tapset/random.stp b/tapset/random.stp new file mode 100644 index 00000000..e0a89d19 --- /dev/null +++ b/tapset/random.stp @@ -0,0 +1,18 @@ +/** @addtogroup library +* The library tapset is a collection of standard functions. +* @{ +*/ + +function randint:long(min:long, max: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) ); + +%} + +/** @} */ diff --git a/testsuite/systemtap.base/rand.exp b/testsuite/systemtap.base/rand.exp new file mode 100644 index 00000000..9b372e18 --- /dev/null +++ b/testsuite/systemtap.base/rand.exp @@ -0,0 +1,3 @@ +set test "rand" +set ::result_string {PASS} +stap_run2 $srcdir/$subdir/$test.stp diff --git a/testsuite/systemtap.base/rand.stp b/testsuite/systemtap.base/rand.stp new file mode 100644 index 00000000..aca80877 --- /dev/null +++ b/testsuite/systemtap.base/rand.stp @@ -0,0 +1,41 @@ +function checkStatus(status:long){ + if (status == 1){ + printf("%s\n","FAIL") + }else{ + printf("%s\n","PASS") + } +} + +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") + } + } + + 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) + + exit() +} -- cgit From d098276239dd9f2e1ad031b48519a5a21779c369 Mon Sep 17 00:00:00 2001 From: "Frank Ch. Eigler" Date: Wed, 23 Sep 2009 16:41:40 -0400 Subject: PR10632: simplify randint() implementation * tapset/random.stp (randint): Make it 1-arity (imply min=0). Document with kerneldoc. * doc/Systemtap_Tapset_Reference/tapsets.tmpl: Extract the docs. * runtime/arith.c (_stp_random_pm_u): Rename without _pm. (_stp_random_pm): Rewrite in terms of ..._u. * testsuite/random.stp: Adapt & simplify. --- doc/SystemTap_Tapset_Reference/tapsets.tmpl | 7 ++++++ runtime/arith.c | 34 +++++++++-------------------- tapset/random.stp | 28 ++++++++++-------------- 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 @@ !Itapset/logging.stp + + Random functions Tapset + + These functions deal with random number generation. + +!Itapset/random.stp + 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() } -- cgit From 243a16e69fa639092a58df581b1aba91269c8fba Mon Sep 17 00:00:00 2001 From: "Frank Ch. Eigler" Date: Wed, 23 Sep 2009 22:36:48 -0400 Subject: PR10632: make randint() unprivileged --- tapset/random.stp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tapset/random.stp b/tapset/random.stp index ada94216..9b2fdc70 100644 --- a/tapset/random.stp +++ b/tapset/random.stp @@ -3,7 +3,7 @@ * @n: Number past upper limit of range, not larger than 2**20. */ function randint:long(n:long) -%{ +%{ /* unprivileged */ #define RANDMAX (1024*1024) if (THIS->n > RANDMAX) CONTEXT->last_error = "range too wide"; -- cgit