#!/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: stresslib,v $
# Revision 1.4  1995/02/02  00:20:14  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.3  1994/11/18  20:57:22  mtm
# Copyright additions/changes
#
# Revision 1.2  1993/07/14  18:54:28  cfj
# OSF/1 AD 1.0.4 code drop from Locus.
#
# Revision 1.1  1992/12/02  21:34:03  dleslie
# New bug drop from Locus, December 2, 1992
#
# Revision 1.1.1.2  1993/07/01  21:22:14  cfj
# Adding new code from vendor
#
# Revision 3.0  92/11/23  11:44:43  chrisp
# First appearance.
# /
# 
#

RANDOM=$$		# default random seed
LOGFILE=/dev/tty	# log to stdout by default
NAME=${0##*/}

#
# Function to check that required commands are in path.
#
required() {
	for cmd in $*; do
		[ "$(whence $cmd)" ] ||
			{ echo "$NAME: can't find $cmd"; exit 2; }
	done
}


#
# Function to create node array from list of nodes
#
declare_nodes() {
	let "num_nodes = 0"
	for node in $*; do
		case $node in
		    *[!0-9.]*)
			log "bad node number specified"
			exit 2
			;;
		    *..*)
			lo_range=${node%%..*}
			hi_range=${node##*..}
			while ((lo_range <= hi_range)); do
				let "NODES[num_nodes]=lo_range"
				let "lo_range += 1"
				let "num_nodes += 1"
			done
			;;
		    [!0-9])
			log "bad node number specified"
			exit 2
			;;
		    *)
			let "NODES[num_nodes]=node"
			let "num_nodes += 1"
			;;
		esac
	done
	let "node_index = -1"
}

#
# Function to select the next node from a list
#	either randomly or cyclically
#
next_node() {
	if [ "$RANDOMIZING_NODES" ]; then
		let "node_index = RANDOM % num_nodes"
	else
		let "node_index += 1"
		if [ "$CYCLING_NODES" ]; then
			let "node_index = node_index % num_nodes"
		fi
	fi
	NODE=${NODES[node_index]}
}

#
# Function to migrate a process (default ourself) to a new node
#
migrate_to_node() {
	node_to=$1
	process=${2-$$}
	node_from=$(node_self)
	if [ $node_to != $node_from ]; then
		kill3 -SIGMIGRATE $node_to $process 1>/dev/null 2>&1
	fi
}

#
# Function to put out progress announcements:
#
log() {
	text="$1"
	set `date`
	time=$4
	[ "$QUIET" ] || echo "$time [$(node_self).$$] $NAME: $text" >>$LOGFILE
}

#
# Functions to start, stop and test for outstanding timer
#
start_timing() {
	sleep $1 &
	TIMER=$!
}
stop_timing() {
	kill $TIMER 1>/dev/null 2>&1
}
timing() {
	if [ "$TIMER" ]; then
		kill -0 $TIMER 1>/dev/null 2>&1
		return $?
	else
		return 0
	fi
}

#
# Function to eat cpu for a given time
#
eat() {
	( while : ; do
		:
	  done
	) &
	EATER=$!
	sleep $1
	kill $EATER
}
stop_eating() {
	kill $EATER 1>/dev/null 2>&1
}


required kill3 node_self
