#!/bin/ksh
# 
# $Copyright
# Copyright 1993, 1994 , 1995 Intel Corporation
# INTEL CONFIDENTIAL
# The technical data and computer software contained herein are subject
# to the copyright notices; trademarks; and use and disclosure
# restrictions identified in the file located in /etc/copyright on
# this system.
# Copyright$
# 
 
#
# Copyright (c) 1992-1995, Locus Computing Corporation
# All rights reserved
#
#
# HISTORY
# $Log: artemis,v $
# Revision 1.6  1995/02/02  00:17:27  bolsen
#  Reviewer(s): Jerry Toman
#  Risk: Medium (lots of files)
#  Module(s): Too many to list
#  Configurations built: STD, LITE, & RAMDISK
#
#  Added or Updated the Locus Copyright message.
#
# Revision 1.5  1994/11/18  20:57:08  mtm
# Copyright additions/changes
#
# Revision 1.4  1993/07/14  18:53:59  cfj
# OSF/1 AD 1.0.4 code drop from Locus.
#
# Revision 1.3  1992/12/11  03:09:33  cfj
# Merged 12-1-92 bug drop from Locus.
#
# Revision 1.2  1992/11/30  23:04:25  dleslie
# Copy of NX branch back into main trunk
#
# Revision 1.1.2.1  1992/11/06  00:14:58  dleslie
# Local changes for NX through noon, November 5, 1992.
#
# Revision 1.1.1.2  1993/07/01  21:21:34  cfj
# Adding new code from vendor
#
# Revision 3.1  92/11/23  11:50:15  chrisp
# Modify to use functions from stresslib.
# 
# Revision 3.0  92/07/21  12:06:00  chrisp
# First release
# 
# 
#

. stresslib

DELAY=30		# default working time on each node

#
# Function to look around for a tasty meal
#
hunt() {
	ps a | { read header
		while read pid etc; do
			set -- ${etc##*:*([0-9. ])}
			if [[ "$1 $2" = *+($PREY)* ]]; then
				if kill -9 $pid >/dev/null 2>&1; then
					log "$1 $2... [$pid] killed"
					return 0
				else
					log "$1 $2... [$pid] escaped"
				fi
			fi
		done
		return 1
	}
}


while getopts ":L:D:d:Nnp:r:t:q" opt; do
	case "$opt" in
	    L)	LOGFILE="$OPTARG";;
	    N)	RANDOMIZING_NODES=;;
	    n)	RANDOMIZING_NODES=true;;
	    D)	DELAY=$OPTARG;;
	    d)	DELAY=$OPTARG; randomizing_delay=true;;
	    p)  PREY=$OPTARG;;
	    q)	QUIET=please;;
	    r)	RANDOM=$OPTARG;;
	    t)	TIME=$OPTARG;;
	    :)	echo "$NAME: $OPTARG requires a value"
		exit 2;;
	   \?)	echo "$NAME: unknown option $OPTARG"
		echo "usage: $NAME -n -t <for how long> -e <#secs to eat> <node_list>"
		exit 2;;
	esac
done
shift OPTIND-1
node_list="$*"

#
# Prompt for all the important stuff if not given
# 
[ "$node_list" ] || read node_list?"Node list? "
[ "$node_list" ] || node_list="$(node_self)"
[ "$PREY" ]      || PREY"What prey? "
[ "$PREY" ]      || exit 2

#
# Turn space-separated list of prey into pattern matching string
#
set -- $PREY
PREY=$1
shift
while [ "$1" ]; do
	PREY="$PREY|$1"
	shift
done

declare_nodes $node_list

trap 'stop_eating; stop_timing; exit' INT KILL TERM
if [ "$TIME" ]; then
	CYCLING_NODES=true
	start_timing $TIME
	log "[$timer] hunting for $TIME seconds..."
else
	CYCLING_NODES=true
fi
		
#
# Take a random walk through the nodes,
#	pausing to do a random amount of work on each.
#
let "delay_time = DELAY"
while timing; do
	#
	# Make a random selection from the list of nodes
	#	and migrate there
	#
	next_node
	[ "$NODE" ] || break
	log "stalking off to node $NODE"
	migrate_to_node $NODE

	[ $DELAY -eq 0 ] && continue

	#
	# Decide time to eat cpu or sleep
	#
	if [ "$randomizing_delay" ]; then
		let "delay_time = (RANDOM % DELAY) + 1"
	fi

	eat_or_sleep=sleep
	hunt && eat_or_sleep=eat

	log "${eat_or_sleep}ing on node $(node_self) for $delay_time secs"
	$eat_or_sleep $delay_time
done

log "...returning home from node $(node_self)"
exit 0
