#!/bin/csh -f -b
#
#  This script runs as much of the demo automatically as possible.
#

# [ BIG IMPORTANT NOTE: This demo is highly dependent upon the
# bandwidth, the priority of objects (both hoard priority and LRU
# priority).  I would recommend that you do exactly what I say in these
# directions and not deviate from them.  If the demoee wants to play
# around, ask that they wait until the formal demo has finished. ]

set PROG = Demo
set HOARD = /usr/coda/etc/hoard
set CFS = /usr/coda/etc/cfs
set HEAD = /usr/ucb/head
set LESS = /usr/misc/bin/less
set SLOW = /usr/coda/etc/slow
set PARTITION = /usr/coda/etc/partition
set HEAL = /usr/coda/etc/heal
set ADVICE_SRV = /coda/project/coda/alpha/bin/advice_srv
## Or, if that doesn't work, try /coda/usr/mre/src/OBJS/@sys/advice/advice_srv


if ($#argv != 1) goto usage
set HOST="$argv[1]"


#
# Make sure the DemoSetup.sh script has been run.
#
set reply
while (($reply != "y") && ($reply != "n"))
        echo -n "Have you run the DemoSetup.sh script? [yn] "
        set reply = "$<"
end

if ($reply == "n") then
	exit(-1)
endif


#
# Turn off hoard walks
#
echo ""
echo "Turning off periodic hoard walks now."
$HOARD off
echo ""
echo "While we don't normally turn off periodic hoard walks, we do so"
echo "during this demonstration.  The reason is that it allows us more"
echo "control over when hoard walks take place during the demo so that"
echo "we can better explain what Venus is doing and when."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"

#
# Setup the hoard database
#
echo ""
echo ""
echo ""
echo "Setting up the hoard database now:"
echo "  -- hoarding the Demo venus sources with priority 1000"
echo "  -- hoarding the 'less' binary with priority 1000"
echo "  -- hoarding the X-related files"
echo ""
echo "Running a hoard walk (manually) now."
echo "This may take a few minutes."
echo ""
$HOARD -f /coda/usr/mre/hoarding/demo1.hdb
$HOARD -f /coda/usr/mre/hoarding/X11.hdb
$HOARD walk
echo "[Hit return to continue.]"
set reply = "$<"

#
# Setup the random cache contents now
#
echo ""
echo ""
echo ""
echo "Now we will examine the top portion of two files."
echo "These two files represent files that just happened to be in the"
echo "cache at the point that we established the weak connection."
echo ""
echo "First, we look at /coda/usr/mre/demo.wcc/vice/srvproc.c"
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
$HEAD /coda/usr/mre/demo.wcc/vice/srvproc.c
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
echo ""
echo ""
echo ""
echo "And now /coda/usr/mre/demo.wcc/vice/codaproc.c"
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
$HEAD /coda/usr/mre/demo.wcc/vice/codaproc.c
echo ""
echo "[Hit return to continue.]"
set reply = "$<"




#
# Startup the advice monitor
#
echo ""
echo ""
echo ""
echo "Now, please start the advice monitor from the xterm on the machine's console"
echo ""
echo "Use: $ADVICE_SRV"
echo ""
echo "You should see it create a window in the upper left corner of the console."
echo "The title of this window should be 'Advice Monitor' and there should be a"
echo "menu bar with two entries ('Variables' and 'WeakMiss'), but that is all."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"



#
# Drop the bandwidth
#
echo ""
echo ""
echo ""
echo "Dropping the bandwidth to 9.6 Kb/s, using cfs."
#echo "Then, run cfs checkservers to make sure Venus adjusts to the change."
echo ""
$CFS bw 9600
#$PARTITION -h scarlatti 1361 -h $HOST 1363
#$PARTITION -h rossini 1361 -h $HOST 1363
#$PARTITION -h puccini 1361 -h $HOST 1363
#$CFS checkservers
#$HEAL -h scarlatti 1361 -h $HOST 1363
#$HEAL -h rossini 1361 -h $HOST 1363
#$HEAL -h puccini 1361 -h $HOST 1363
#$SLOW -h scarlatti 1361 9600 -h $HOST 1363 9600
#$SLOW -h rossini 1361 9600 -h $HOST 1363 9600
#$SLOW -h puccini 1361 9600 -h $HOST 1363 9600
#$CFS checkservers
echo ""
echo "[Hit return to continue.]"
set reply = "$<"


#
# Discuss the user patience threshold and coercing long fetches into misses.
#
echo ""
echo ""
echo ""
echo "When weakly connected, the performance impact of cache misses is often"
echo "too large to ignore.  For example, a cache miss on a 1 MB file at 10"
echo "Mb/s can usually be serviced in a few seconds.  At 9.6 Kb/s, the same"
echo "miss causes a delay of nearly 20 minutes."
echo ""
echo "From a user's perspective, this lack of performance transparency can"
echo "overshadow the functional transparency of caching.  The problem is"
echo "especially annoying because cache miss handling is a foreground"
echo "activity.  In most cases, a user would rather be told that a large"
echo "file is missing than be forced to wait for it to be fetched over a"
echo "weak connection."
echo ""
echo "But there are also situations where a file is so critical that a user"
echo "is willing to suffer considerable delay.  We refer to the maximum time"
echo "that a user is willing to wait for a particular file as her 'patience"
echo "threshold' for that file.  The need for user input arises because Venus"
echo "has to find out how critical a missing object is."
echo ""
echo "Since the hoarding mechanism already provided a means of factoring"
echo "user estimates of importance into cache management, it was the natural"
echo "focal point of our efforts.  Our extensions of this mechanism for weak"
echo "connectivity are in two parts: an interactive facility to help augment"
echo "the hoard database (HDB), and another to control the amount of data"
echo "fetched during hoard walks.  Together these changes have the effect of"
echo "moving many cache miss delays into the background."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
echo ""
echo ""
echo ""
echo "We begin by looking at the interactive facility to help the user"
echo "augment the hoard database."
echo ""
echo "When a miss occurs, Venus estimates the time needed to service it."
echo "This estimate is based on the size of the object (obtained from the"
echo "object's status information) and the current network bandwidth.  If"
echo "Venus does not already have status information about the object"
echo "cached, it obtains that information from the server.  The delay for"
echo "this is acceptable even on slow networks because status information is"
echo "only about 100 bytes long."
echo ""
echo "The estimated service time is then compared with the patience"
echo "threshold.  If the service time is below the threshold, Venus"
echo "transparently services the miss.  If the threshold is exceeded, Venus"
echo "returns a cache miss error and records information about the miss."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
echo ""
echo ""
echo ""
echo "Suppose that a large, but important, file"
echo "    /coda/usr/mre/demo.wcc/venus/vol_cml.c."
echo "is updated by another user.  This file is approximately 93 KB in length."
echo ""
$CFS flushobject /coda/usr/mre/demo.wcc/venus/vol_cml.c
echo ""
echo "Even though re-fetching this file over a 9.6 Kb/second connection"
echo "will take well over one minute, Venus believes the user is willing"
echo "to wait that long for this file because the user has assigned a"
echo "hoard priority of 1000 to it."
echo ""
echo "[Hit return to continue.]"
echo "[Then, hit 'q' to continue out of 'less'.]"
set reply = "$<"
$LESS /coda/usr/mre/demo.wcc/venus/vol_cml.c
echo ""
echo "Now, let us look at a large, but seemingly unimportant, uncached file:"
echo "    /coda/usr/mre/demo.wcc/vice/codaproc2.c."
echo "This file is approximately 78 KB in length."
echo ""
echo "Fetching this file will take just over 1 minute across a 9.6 Kb/second"
echo "connection -- less time than was necessary for the previous file."
echo "However, Venus coerces the request on this object to a cache miss."
echo "Since the user has not assigned a hoard priority to this file, Venus"
echo "believes the user to be unwilling to wait such a long time for a"
echo "seemingly unimportant file.  The cache miss is presented to the user"
echo "as a 'Connection timed out' message."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
$LESS /coda/usr/mre/demo.wcc/vice/codaproc2.c
echo ""
echo "But, what if the seemingly unimportant object was really important"
echo "to the user?"
echo ""
echo "[Hit return to continue.]"
set reply = "$<"

# The following interrupts the story so I've left it out...
# Note that the user patience threshold does not apply to objects that
# are already in the cache.  You can look at
#	less /coda/usr/mre/demo.wcc/vice/srvproc.c
# even though it is "unimportant" and absolutely huge!

#
# Demo the WeakMiss interface
#
echo ""
echo ""
echo ""
echo "At any time, a user can ask Venus to show her all the misses that have"
echo "occured since the previous such request.  Venus displays the pathname"
echo "of each cache miss along with the name of the binary that attempted to"
echo "reference the file.  The user can then select objects to be added to"
echo "the HDB.  This action does not immediately fetch the object; that is"
echo "deferred until a future hoard walk.  Hoard walks occur once every 10"
echo "minutes by default, or by explicit user request."
echo ""
echo "Please go to the 'WeakMiss' menu on the laptop console and select the"
echo "item labelled 'Request list of cache misses'.  This will pop-up a window"
echo "labelled 'Miss List'."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
echo ""
echo ""
echo ""
echo "For this demo, the only object listed should be"
echo "    '/coda/usr/mre/demo.wcc/vice/codaproc2.c'."
echo "The binary that attempted to reference this object should be 'less'."
echo "Following this information, there is a square button (under the column"
echo "labelled 'HDB?'.  If the user would like to add this object to the"
echo "hoard database, she can click on this button."
echo ""
echo "If the user clicks on this button, another window pops up.  This"
echo "window allows the user to specify the hoard information.  She may"
echo "edit the pathname down to the containing directory and then use"
echo "'children' or 'descendants' (and the '+').  Or, she can hoard just"
echo "this file.  To choose a priority, she can click in the slider bar"
echo "or move the slider bar with the mouse.  When she finishes, she can"
echo "click on 'Done' to commit her changes or on 'Cancel' to abort them."
echo ""
echo "For the purposes of this demo, click on the 'HDB?' button for this"
echo "item (this will require you to place the window on your screen -- use"
echo "the left mouse button).  Then, choose a lowish hoard priority (near"
echo "the left hand side of the slider bar -- I choose under the 'y' of"
echo "'Priority' and got about 250) and then click on the 'Done' button."
echo "(This will make the window go away.)  [Note that if you do the"
echo "directory thing, you'll mess up the rest of the demo.]"
echo ""
echo "[Hit return to continue.]"
set reply = "$<"
echo ""
echo ""
echo ""
echo "Now, you're back to the 'Miss List' window.  You should click on"
echo "'Done -- please walk'.  This will cause a hoard walk to be invoked and"
echo "codaproc2.c to get fetched."
echo ""
echo "[Hit return to continue (after the hoard walk completes).]"
set reply = "$<"
echo ""
echo ""
echo ""
echo "Now, we can look at the codaproc2.c file."
echo ""
echo "[Hit return to continue.]"
echo "[Then, hit 'q' to continue out of 'less'.]"
set reply = "$<"
$LESS /coda/usr/mre/demo.wcc/vice/codaproc2.c

#
# Summarize the demo so far...
#
echo ""
echo ""
echo ""
echo "As you've just seen, Venus uses the user patience threshold to decide"
echo "what uncached objects the user is likely to be willing to wait for and"
echo "what uncached objects the user would prefer to be told aren't currently"
echo "available.  If the user is told that an object is unavailable, she"
echo "can augment the hoard database interactively in order to give the"
echo "object a higher priority.  Then, after the next hoard walk (maximum"
echo "of 10 minutes), the object will be available."
echo ""
echo "The second half of this demo shows how the user can control the amount"
echo "of data fetched during hoard walks."
echo ""
set reply
echo "If the time allotted for this demo is running short, this is a good"
echo "stopping point."
echo ""
set reply
while (($reply != "y") && ($reply != "n"))
        echo -n "Would you like to stop now? [yn] "
        set reply = "$<"
end

if ($reply == "y") then
	echo "Thank you for your time."
	exit(0)
endif

#
#-------------------------------- PART II -------------------------------- 
#


#
# Introduce the second half of the demo.
#
echo ""
echo ""
echo ""
echo "Recall that the second extension of the hoarding mechanism for weak"
echo "connectivity was to allow the user to control the amount of data"
echo "fetched during hoard walks."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"


#
# Give the hoard daemon something to fetch.
#
echo ""
echo ""
echo ""
echo "Since we have recently done a hoard walk, Venus should not need to"
echo "fetch anything at the moment.  To solve this 'problem', we can either"
echo "update something on the servers or add something to the HDB.  Or, we"
echo "can cheat and tell Venus to flush perfectly valid data from its cache..."
echo "We will use the cfs utility to flush four objects from our cache."
echo "These are:  codaproc2.c, codaproc.c, srvproc.c and less"
$CFS flushobject /coda/usr/mre/demo.wcc/vice/codaproc2.c
$CFS flushobject /coda/usr/mre/demo.wcc/vice/codaproc.c
$CFS flushobject /coda/usr/mre/demo.wcc/vice/srvproc.c
$CFS flushobject /coda/misc/less/i386_mach/omega/bin/less
echo ""
echo ""
echo "We also add codaproc.c and srvproc.c to the HDB with priorities of 50."
$HOARD -f /coda/usr/mre/hoarding/demo2.hdb
echo ""
echo "[Hit return to continue.]"
set reply = "$<"


#
# Request that Venus ask for hoard walk advice
#
echo ""
echo ""
echo ""
echo "Now we must request that Venus ask for advice during hoard walks."
echo ""
echo "Please go to the 'WeakMiss' menu on the laptop console and select the"
echo "item labelled 'Hoard Walk Advice'."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"


#
#
#
echo ""
echo ""
echo ""
echo "A hoard walk is executed in two phases.  In the first phase, called"
echo "the 'status walk', Venus obtains status information for missing"
echo "objects and determines which objects, if any, should be fetched."
echo "Because of volume callbacks, the status walk usually involves little"
echo "network traffic.  During the second phase, called the 'data walk',"
echo "Venus fetches the contents of objects selected by the status walk."
echo "Even if there are only a few large objects to be fetched, this phase"
echo "can be a substantial source of network traffic."
echo ""
echo "By introducing an interactive phase between the status and data walks,"
echo "we provide users with a means of limiting the volume of data fetched"
echo "in the data walk.  Each object whose estimated service time is below"
echo "the user's patience threshold is pre-approved for fetching.  The"
echo "fetching of other objects must be explicitly approved by the user."
echo ""
echo "Forcing hoard walk now."
$HOARD walk &
echo ""
echo "[Hit return to continue.]"
set reply = "$<"


#
# What to do with the hoard walk window...
#
echo ""
echo ""
echo ""
echo "A window named 'Advice for Hoard Daemon' should popup on the console"
echo "of the laptop.  The top 4 lines provide summary data that shows the"
echo "current state of the cache.  Note that we've had one object ('less',"
echo "hoarded with priority 1000) preapproved for fetch."
echo ""
echo "The middle section of the window shows the objects for which the hoard"
echo "daemon is requesting advice.  It shows the priority of these objects"
echo "as well as the expected cost of fetching these objects.  The user has"
echo "three choices for each of the listed objects:"
echo "        1) Click on the 'Fetch?' button for this object:"
echo "           If the user chooses this action, the hoard daemon will"
echo "           fetch the object."
echo "        2) Click on the 'Stop Asking?' button for this object:"
echo "           If the user chooses this action, the hoard daemon will"
echo "           NOT fetch the object and will stop asking for advice"
echo "           about this object until a strong connection is established."
echo "        3) Do nothing for this object:"
echo "           If the user chooses this action, the hoard daemon will"
echo "           not fetch the object during this hoard walk (but will"
echo "           ask about this object again during the next hoard walk)."
echo ""
echo "The bottom section of the window shows the expected state of the cache"
echo "after the hoard walk completes.  In addition, it shows the number of"
echo "objects that will be fetched during the hoard walk and the total time"
echo "expected for the hoard walk to complete.  These values are dynamically"
echo "updated."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"


#
#  Instruct demoee in using this window...
#
echo ""
echo ""
echo ""
echo "Please select 'codaproc2.c' to be fetched by clicking on its 'Fetch?'"
echo "button.  Notice the number of objects fetched and the total expected"
echo "fetch time are updated to reflect this newly approved item."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"

echo ""
echo ""
echo ""
echo "Please select 'srvproc.c' as an item that should not be fetched until"
echo "a strong connection is established by clicking on its 'Stop Asking?'"
echo "button."
echo ""
echo "[Hit return to continue.]"
set reply = "$<"

echo ""
echo ""
echo ""
echo "Allow the hoard walk to complete by clicking on the 'Done' button."
echo "Notice that the hoard walk complains that codaproc.c and srvproc.c"
echo "are 'Missing/Invalid'.  This is because you requested that srvproc.c"
echo "not be fetched until a strong connection is established, and that"
echo "codaproc.c not be fetched during this hoard walk (implicit by the"
echo "fact that you didn't request that it be fetched)."
echo ""
echo "[Hit return after the 'Missing/Invalid' messages appear.]"
set reply = "$<"


#
# Note that it worked
#
echo ""
echo ""
echo ""
echo "Now that the hoard walk has completed, we can look at 'codaproc2.c'."
echo ""
echo "[Hit return to continue.]"
echo "[Then, hit 'q' to continue out of 'less'.]"
set reply = "$<"
$LESS /coda/usr/mre/demo.wcc/vice/codaproc2.c

echo ""
echo ""
echo ""
echo "Note that neither 'codaproc.c' nor 'srvproc.c' are available."
echo ""
$HEAD /coda/usr/mre/demo.wcc/vice/codaproc.c
echo ""
$HEAD /coda/usr/mre/demo.wcc/vice/srvproc.c
echo ""
echo "[Hit return to continue.]"
set reply = "$<"

#
#
#
echo ""
echo ""
echo ""
echo "Now we manually force another hoard walk."
$HOARD walk &
echo "Notice that 'codaproc.c' is once again on the list, because it was not"
echo "fetched during the last hoard walk (at your request).   Also notice"
echo "that 'srvproc.c' is not on the list, even though it too was not fetched."
echo "This is because you requested that this object not be fetched for the"
echo "duration of the weak connection.  Obviously, 'codaproc2.c' is not on the"
echo "list since it was fetched during the last hoard walk."
echo ""
echo "Please click on the 'Done' button to allow the hoard walk to complete."
echo "You will once again see those same two 'Missing/Invalid' message."
echo ""
echo "[Hit return after the 'Missing/Invalid' messages appear.]"
set reply = "$<"


#
# Finished!
#
echo ""
echo ""
echo ""
echo "You have now seen how we have extended the hoarding mechanism to"
echo "support weak connectivity.  Our first extension limits the amount"
echo "of time a user is forced to wait for a cache miss to be serviced,"
echo "based upon the current bandwidth and the importance of the object."
echo "It also provides an interactive facility to help the user augment"
echo "the hoard database.  Our second extension allows the user to control"
echo "the amount of data fetched during a hoard walk.  Together these two"
echo "changes have the effect of moving many cache miss delays into the"
echo "background.  In some sense, we have sacrificed some of the functional"
echo "transparency of caching in order to increase the amount of performance"
echo "transparency seen by the user."
echo ""
echo "Thank you for your time."
echo ""

$CFS bw 10485760
#$HEAL -h scarlatti 1361 -h $HOST 1363 
#$HEAL -h rossini 1361 -h $HOST 1363 
#$HEAL -h puccini 1361 -h $HOST 1363
$HOARD on

exit 0


#
#  Usage message...
#
usage:
echo "Usage: ${PROG} <hostname>"
exit 0
