build "core init timer server/cpu_balancer app/cpu_burner app/top"

if {![have_include "power_on/qemu"]} {
	puts "Run script is not supported on this platform"
	exit 0
}
if {[have_spec foc] && ([have_board pbxa9] || [have_board rpi3])} {
	# foc kernel does detect solely 1 CPU */
	puts "Run script is not supported on this platform"
	exit 0
}
if {![have_spec nova] && ![have_spec foc] && ![have_spec sel4]} {
	puts "Run script is not supported on this platform"
	exit 0
}

set cpu_width  4
set cpu_height 1
set report_config "yes"
set use_trace "yes"

create_boot_directory

import_from_depot [depot_user]/src/report_rom \
                  [depot_user]/src/shim

append config {
<config prio_levels="2" verbose="yes">
	<affinity-space width="} $cpu_width {" height="} $cpu_height {"/>
	<parent-provides>
		<service name="LOG"/>
		<service name="CPU"/>
		<service name="ROM"/>
		<service name="PD"/>
		<service name="IO_PORT"/> <!-- timer on some kernels -->
		<service name="IRQ"/>     <!-- timer on some kernels -->
		<service name="TRACE"/>
	</parent-provides>

	<default-route>
		<service name="LOG"> <parent/> </service>
		<service name="PD"> <parent/> </service>
		<service name="ROM"> <parent/> </service>
	</default-route>
	<default caps="100"/>

	<start name="timer">
		<resource name="RAM" quantum="1M"/>
		<provides><service name="Timer"/></provides>
		<route>
			<any-service> <parent/> </any-service>
		</route>
	</start>}

append_if [expr $report_config eq "yes"] config {
	<start name="report_rom">
		<binary name="report_rom"/>
		<resource name="RAM" quantum="1M"/>
		<provides> <service name="Report"/> <service name="ROM"/> </provides>
		<config verbose="yes"/>
		<route>
			<any-service> <parent/> </any-service>
		</route>
	</start>}

append config {
	<start name="cpu_balancer">
		<resource name="RAM" quantum="2M"/>
		<provides>
			<service name="PD"/>
			<service name="CPU"/>
		</provides>
		<config interval_us="2000000"
		        report="} $report_config {"
		        trace="} $use_trace {"
		        verbose="no">
			<component label="cpu_burner -> " default_policy="none">
<!--
				<thread name="signal handler" policy="pin" xpos="1" ypos="0"/>
-->
				<thread name="signal handler" policy="max-utilize"/>
				<thread name="burn_0x0"       policy="round-robin"/>
				<thread name="burn_1x0"       policy="round-robin"/>
			</component>
		</config>
		<route>
			<service name="Timer">  <child name="timer"/> </service>
			<service name="Report"> <child name="report_rom"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>

	<start name="cpu_burner" priority="-1" caps="120">
		<binary name="shim"/>
		<affinity xpos="1" ypos="0" width="2" height="1"/>
		<resource name="RAM" quantum="3M"/>
		<route>

			<!-- by shim binary -->
			<service name="PD"  unscoped_label="cpu_burner"> <parent/> </service>
			<service name="CPU" unscoped_label="cpu_burner"> <parent/> </service>

			<!-- by child of shim -->
			<service name="PD">  <child name="cpu_balancer"/> </service>
			<service name="CPU"> <child name="cpu_balancer"/> </service>

			<service name="ROM" label="binary"> <parent label="cpu_burner"/> </service>

			<service name="Timer"> <child name="timer"/> </service>
			<service name="LOG"> <parent/> </service>
			<service name="ROM"> <parent/> </service>
		</route>
	</start>
<!--
	<start name="top">
		<resource name="RAM" quantum="2M"/>
		<route>
			<service name="Timer"> <child name="timer"/> </service>
			<service name="CPU"> <child name="cpu_balancer"/> </service>
			<any-service> <parent/> </any-service>
		</route>
	</start>
-->
</config>}

install_config $config

build_boot_image { core ld.lib.so init timer cpu_balancer cpu_burner top }

append qemu_args " -nographic"
append qemu_args " -smp [expr $cpu_width * $cpu_height],cores=$cpu_width,threads=$cpu_height"

run_genode_until {.*thread xpos="1" ypos="0" name="signal handler" policy="max-utilize".*\n} 60