#! /bin/sh
#
# Copyright 1997,2003 Silicon Graphics, Inc.
# ALL RIGHTS RESERVED
# 
# UNPUBLISHED -- Rights reserved under the copyright laws of the United
# States.   Use of a copyright notice is precautionary only and does not
# imply publication or disclosure.
# 
# U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
# in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
# in similar or successor clauses in the FAR, or the DOD or NASA FAR
# Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
# 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
# 
# THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
# INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
# DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
# PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
# GRAPHICS, INC.
#
# micky mouse transaction load generator ...
#

# Get standard environment
. /etc/pcp.env


NUMTX=-1
while getopts n:\? c
do
    case $c
    in
	n)	NUMTX=$OPTARG;;
	\?)	echo "Usage: genload [-n numtx] [tps [avg_serv]]"
		exit 1
		;;
    esac
done
shift `expr $OPTIND - 1`

# target TPS [default 10]
#
TPS=${1-10}

# mean of the mean service times [default 10 seconds]
#
AVG=${2-10}

if [ ! -x ./txrecord ]
then
    echo 'Error: no txrecord binary here ... try "make txrecord"?'
    exit 1
fi

# get tx types from txmon PMDA via pmcd on the localhost, then choose tx
# type with Zipfian distribution, and artifically chosen service times
# ... pipe them to a record-then-sleep droid in the while loop at the
# end ...  rate throttling relies on O/S pipe-full synchronization
# (crude, but simple and effective)
#
pminfo -f txmon.count \
| tr -d '"\]\[' \
| $PCP_AWK_PROG '
BEGIN	{ i = 0; w = 2; s = 0; numtx='$NUMTX' }
/value/	{ name[i] = $4; zipf[i] = s + 1 / w; s = zipf[i]
	  i++; w *= 2
	}
END	{ if (i == 0) {
	    print "e Cannot determine transaction type names ... is the txmon PMDA running locally?"
	    exit
	  }
	  # compute mean, drawn from normal distn with mean and std dev AVG
	  for (j=0; j<i; j++) {
	    while (1) {		# the csw method
		y = rand()
		d = 1 - y
		h = 0.14 / d
		x = 1.22 * y * (1.0 + h)
		t = log(rand() / (1.0 + h/d)) + 0.5 * x*x + 0.1576
		if (t <= 0) {
		    if (t >= -0.69314706) x = -x
		    m[j] = '$AVG' + '$AVG' * x
		    if (m[j] >= 0)
			break
		}
	    }
	    print "i Mean service time for " name[j] " tx: " m[j]
	  }
	  zipf[i-1] = 1			# force upper bound of last interval
	  while (numtx != 0) {
	    printf "w "
	    for (k=0; k < '$TPS' && numtx != 0; k++) {
		r = rand()
		for (j=0; j<i; j++) {
		    if (r <= zipf[j])
			break
		}
		printf " %s %f",name[j],rand() * m[j]
		if (numtx > 0)
		    numtx--
	    }
	    print ""
	    print "s"
	  }
	}' \
| while read action arg
do
    case $action
    in
	i)	echo "$arg"
		;;
	e)      echo "Error: $arg"
		exit 1
		;;
	s)	sleep 1
		;;
	w)	./txrecord $arg
		;;
    esac
done


