# --------------------------------------------------------------- #
# -- Copyright (c) 1994, 1995 Computer Sciences Department,    -- #
# -- University of Wisconsin-Madison, subject to the terms     -- #
# -- and conditions given in the file COPYRIGHT.  All Rights   -- #
# -- Reserved.                                                 -- #
# --------------------------------------------------------------- #

#
#	Time writing with append_file_i
#   Parameters: 
#   	        size of objects written 
#		(write size== whole object)
#		logging on or off
#
#
# NB: the first time you run this on a new volume, you will get some 
# multiple of 100 objects created; the transaction creating the last 100
# will run out of disk space and abort.  Thereafter, you must set
# the value of fid" to indicate that the file already exists.
#
#

set nobjs 100000
# set nobjs 10

set STARTOVER 0

if [is_set STARTOVER] {
    set fid 0
} else {
    set dont_force_reformat 1
    set fid s(1.4)
}

source $script_dir/vol.init


# set objsize [expr {8192 * 5}]
set objsize 200

#ignored for now
set logging 0

set config [sm config_info]

if {$volid == 0} {
    foreach i $ssh_device_list {
	set dev  [lindex $i 0]
	set size [lindex $i 1]
	set vol  [lindex $i 2]

	sm mount_dev $dev $vol

	set vol_list [sm list_volumes $dev]
	if { [llength $vol_list] == 0 } {
	    set quota [sm get_device_quota $dev]
	    echo creating volume $vol size $size on $dev
	    sm create_vol $dev $vol $size
	}

	set quota [sm get_device_quota $dev]
	set quota [sm get_volume_quota $vol]

	set vol_list [sm list_volumes $dev]
	assert {[expr [llength $vol_list] == 1]}
	assert {[expr [string compare [lindex $vol_list 0] $vol.0.0.0:0] == 0]}
    }
}
if {$volid == 0} {
    set volid [lindex [lindex $ssh_device_list 0] 2]
}

echo mounted everything....

set volsize [lindex [sm get_volume_quota $volid] 0]
set used [lindex [sm get_volume_quota $volid] 1]
set volsize [expr {$volsize - $used} ]
# set objksize [expr {$objsize / 1024} ]
# set nobjs [expr {$volsize / $objksize} ]
# set nobjs [expr { 200000000 / $objsize } ]
#unset objksize

set hdr "" 
set body [format "%0*s" $objsize a]


# 
# create a huge file
#
if {$fid==0} {
    sm begin_xct
    set fid [sm create_file $volid tmp ]
    echo fid=$fid
    set scan [sm scan_file_create $fid t_cc_append]
    set dummy [sm gather_stats reset]
    echo creating $nobjs objects of size $objsize 
    set total 0
    set wtime [ time {
	sm multi_file_append $scan $hdr $objsize $body $nobjs
    } 1 ]
    sm commit_xct
    echo done creating...
    pstats
    sm scan_file_destroy $scan
} 

set dummy [sm gather_stats reset]
#   clear the buffer pool, invalidate 
sm begin_xct
set ftime [ time {
sm force_buffers true
} 1 ]
sm commit_xct
pstats

echo
echo Write time = $wtime  Force time = $ftime
echo
