if { [get_cmd_switch --autopilot] } {
	if {[have_include "power_on/qemu"]} {
		puts "\nRun script does not support Qemu.\n"
		exit 0
	}
}

build "core init test/memcpy"

create_boot_directory

install_config {
<config>
	<parent-provides>
		<service name="ROM"/>
		<service name="IRQ"/>
		<service name="IO_MEM"/>
		<service name="IO_PORT"/>
		<service name="PD"/>
		<service name="RM"/>
		<service name="CPU"/>
		<service name="LOG"/>
	</parent-provides>

	<default-route> <any-service> <parent/> </any-service> </default-route>
	<default caps="200"/>

	<start name="test-memcpy">
		<resource name="RAM" quantum="40M"/>
		<config>
			<vfs> <dir name="dev"> <log/> <null/> </dir> </vfs>
			<libc stdout="/dev/log" stderr="/dev/log" socket="/socket"/>
		</config>
	</start>
</config>
}

build_boot_image {
	core init test-memcpy
	ld.lib.so libc.lib.so vfs.lib.so
}

append qemu_args " -nographic  "

proc run_test {name serial_id} {
	run_genode_until "start $name.*\n"    20  $serial_id
	set t1  [clock milliseconds]
	run_genode_until "finished $name.*\n" 300 $serial_id
	set t2  [clock milliseconds]
	return [expr {$t2 - $t1}]
}

run_genode_until "Memcpy testsuite started.*\n" 60
set serial_id         [output_spawn_id]
set byte_dur          [run_test "bytewise memcpy" $serial_id]
set genode_dur        [run_test "Genode memcpy"   $serial_id]
set genode_set_dur    [run_test "Genode memset"   $serial_id]
set libc_cpy_dur      [run_test "libc memcpy"     $serial_id]
set libc_set_dur      [run_test "libc memset"     $serial_id]
set genode_offset_dur [run_test "Genode memcpy"   $serial_id]
set uncached_wr_dur   [run_test "Genode memcpy"   $serial_id]
set uncached_rd_dur   [run_test "Genode memcpy"   $serial_id]
puts "bytewise:                copied 8 GB in $byte_dur milliseconds ([expr {8192000 / $byte_dur}] MiB/sec)"
puts "memcpy:                  copied 8 GB in $genode_dur milliseconds ([expr {8192000 / $genode_dur}] MiB/sec)"
puts "memset:                  copied 8 GB in $genode_set_dur milliseconds ([expr {8192000 / $genode_set_dur}] MiB/sec)"
puts "libc memcpy:             copied 8 GB in $libc_cpy_dur milliseconds ([expr {8192000 / $libc_cpy_dur}] MiB/sec)"
puts "libc memset:             copied 8 GB in $libc_set_dur milliseconds ([expr {8192000 / $libc_set_dur}] MiB/sec)"
puts "memcpy (page offset):    copied 8 GB in $genode_offset_dur milliseconds ([expr {8192000 / $genode_offset_dur}] MiB/sec)"
puts "memcpy (uncached write): copied 8 GB in $uncached_wr_dur milliseconds ([expr {8192000 / $uncached_wr_dur}] MiB/sec)"
puts "memcpy (uncached read):  copied 8 GB in $uncached_rd_dur milliseconds ([expr {8192000 / $uncached_rd_dur}] MiB/sec)"
exit 0

#
# Linux baseline measurements
#

# Zynq-7000 @ 666MHz (L2 prefetching enabled, offset=0)
# Genode memcpy:   copied 8388608 KiB in 27362177 usecs (299 MiB/sec)
# bytewise memcpy: copied 8388608 KiB in 43882888 usecs (186 MiB/sec)
# libc memcpy:     copied 8388608 KiB in 28702066 usecs (285 MiB/sec)
# libc memset:     copied 8388608 KiB in 4033019 usecs (2031 MiB/sec)

# Zynq-7000 @ 666MHz (L2 prefetching enabled, offset=4)
# Genode memcpy:   copied 8388608 KiB in 14985454 usecs (546 MiB/sec)
# bytewise memcpy: copied 8388608 KiB in 39478781 usecs (207 MiB/sec)
# libc memcpy:     copied 8388608 KiB in 28792091 usecs (284 MiB/sec)
# libc memset:     copied 8388608 KiB in 4041102 usecs (2027 MiB/sec)
