From fe3f67f712a7b0c663ecb8b18403ce9b857690db Mon Sep 17 00:00:00 2001 From: Stefan Kalkowski Date: Tue, 30 Oct 2018 14:40:20 +0100 Subject: [PATCH] test: add simple cpu benchmark test (fix #3026) --- repos/os/run/cpu_bench.run | 56 ++++++++++++++++++++++ repos/os/src/test/cpu_bench/bogomips.h | 8 ++++ repos/os/src/test/cpu_bench/linux/Makefile | 7 +++ repos/os/src/test/cpu_bench/linux/main.cc | 48 +++++++++++++++++++ repos/os/src/test/cpu_bench/main.cc | 30 ++++++++++++ repos/os/src/test/cpu_bench/target.mk | 4 ++ 6 files changed, 153 insertions(+) create mode 100644 repos/os/run/cpu_bench.run create mode 100644 repos/os/src/test/cpu_bench/bogomips.h create mode 100644 repos/os/src/test/cpu_bench/linux/Makefile create mode 100644 repos/os/src/test/cpu_bench/linux/main.cc create mode 100644 repos/os/src/test/cpu_bench/main.cc create mode 100644 repos/os/src/test/cpu_bench/target.mk diff --git a/repos/os/run/cpu_bench.run b/repos/os/run/cpu_bench.run new file mode 100644 index 000000000..d33d1f2a7 --- /dev/null +++ b/repos/os/run/cpu_bench.run @@ -0,0 +1,56 @@ +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/cpu_bench" + +create_boot_directory + +install_config { + + + + + + + + + + + + + + + + + + + + + + + +} + +build_boot_image { + core init test-cpu ld.lib.so timer +} + +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" 200 $serial_id + set t2 [clock milliseconds] + return [expr {$t2 - $t1}] +} + +run_genode_until "Cpu testsuite started.*\n" 60 +set serial_id [output_spawn_id] +set bogomips [run_test "bogomips" $serial_id] +puts "bogomips: 2G Bogus instructions in $bogomips milliseconds ([expr {2000000.0 / $bogomips}] BogoMIPS)" +exit 0 diff --git a/repos/os/src/test/cpu_bench/bogomips.h b/repos/os/src/test/cpu_bench/bogomips.h new file mode 100644 index 000000000..d709d8e1b --- /dev/null +++ b/repos/os/src/test/cpu_bench/bogomips.h @@ -0,0 +1,8 @@ + +void bogomips() __attribute__((optimize("O0"))); + +void bogomips() +{ + for (register unsigned i = 0; i < 1000000000; i++) ; +}; + diff --git a/repos/os/src/test/cpu_bench/linux/Makefile b/repos/os/src/test/cpu_bench/linux/Makefile new file mode 100644 index 000000000..9490900b3 --- /dev/null +++ b/repos/os/src/test/cpu_bench/linux/Makefile @@ -0,0 +1,7 @@ +INC_DIR = $(PWD)/.. + +cpu_bench: main.cc $(INC_DIR)/bogomips.h + g++ -I$(INC_DIR) -O2 -Wall -Wextra -Weffc++ -std=gnu++11 $< -o $@ + +clean: + rm -f *~ cpu_bench diff --git a/repos/os/src/test/cpu_bench/linux/main.cc b/repos/os/src/test/cpu_bench/linux/main.cc new file mode 100644 index 000000000..9e98f30f8 --- /dev/null +++ b/repos/os/src/test/cpu_bench/linux/main.cc @@ -0,0 +1,48 @@ +#include +#include + +#include + +struct Duration { unsigned long usecs; }; + + +struct Time +{ + timespec _timespec { 0, 0 }; + + Time() + { + clock_gettime(CLOCK_REALTIME, &_timespec); + } + + Time(timespec timespec) : _timespec(timespec) { } + + void print() const + { + printf("secs=%ld nsecs=%ld\n", + (long)_timespec.tv_sec, (long)_timespec.tv_nsec); + } + + static Duration duration(Time t1, Time t2) + { + auto usecs = [&] (timespec ts) { + return 1000UL*1000UL*((unsigned long)ts.tv_sec % 1000UL) + + (unsigned long)ts.tv_nsec/1000UL; }; + + return Duration { usecs(t2._timespec) - usecs(t1._timespec) }; + } +}; + + +int main(int, char**) +{ + printf("bogomips test:\n"); + Time s; + bogomips(); + { + Time e; + Duration duration = Time::duration(s, e); + printf("2G bogus instructions in %ld msecs (%f BogoMIPS)\n", + duration.usecs/1000, 2000000000 / (float)duration.usecs); + } +} diff --git a/repos/os/src/test/cpu_bench/main.cc b/repos/os/src/test/cpu_bench/main.cc new file mode 100644 index 000000000..05679330d --- /dev/null +++ b/repos/os/src/test/cpu_bench/main.cc @@ -0,0 +1,30 @@ +/* + * \brief Testing CPU performance + * \author Stefan Kalkowski + * \date 2018-10-22 + * + */ + +/* + * Copyright (C) 2018 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#include +#include +#include + +#include + +void Component::construct(Genode::Env &env) +{ + Timer::Connection timer(env); + timer.msleep(2000); + + Genode::log("Cpu testsuite started"); + Genode::log("start bogomips"); + bogomips(); + Genode::log("finished bogomips"); +}; diff --git a/repos/os/src/test/cpu_bench/target.mk b/repos/os/src/test/cpu_bench/target.mk new file mode 100644 index 000000000..a61148ad8 --- /dev/null +++ b/repos/os/src/test/cpu_bench/target.mk @@ -0,0 +1,4 @@ +TARGET = test-cpu +SRC_CC = main.cc +INC_DIR = $(PRG_DIR) +LIBS = base