@@ -311,6 +311,72 @@ static void perf_baseline(void)
maxcycles, duration);
}
+static void perf_cost_load_worker(void *opaque)
+{
+ int fd;
+
+ fd = open("/proc/self/exe", O_RDONLY);
+ assert(fd >= 0);
+ close(fd);
+}
+
+static __attribute__((noinline)) void perf_cost_load_func(void *opaque)
+{
+ perf_cost_load_worker(opaque);
+ qemu_coroutine_yield();
+}
+
+static double perf_cost_load(unsigned long maxcycles, bool use_co)
+{
+ unsigned long i = 0;
+ double duration;
+
+ g_test_timer_start();
+ if (use_co) {
+ Coroutine *co;
+ while (i++ < maxcycles) {
+ co = qemu_coroutine_create(perf_cost_load_func);
+ qemu_coroutine_enter(co, &i);
+ qemu_coroutine_enter(co, NULL);
+ }
+ } else {
+ while (i++ < maxcycles) {
+ perf_cost_load_worker(&i);
+ }
+ }
+ duration = g_test_timer_elapsed();
+
+ return duration;
+}
+
+static void perf_cost_with_load(void)
+{
+ const unsigned long maxcycles = 1000000;
+ double duration;
+ unsigned long ops;
+ unsigned long cost_co, cost;
+
+ duration = perf_cost_load(maxcycles, false);
+ ops = (long)(maxcycles / (duration * 1000));
+ cost = (unsigned long)(1000000000.0 * duration / maxcycles);
+ g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
+ "%luns per operation without using coroutine",
+ maxcycles,
+ duration, ops,
+ cost);
+
+ duration = perf_cost_load(maxcycles, true);
+ ops = (long)(maxcycles / (duration * 1000));
+ cost_co = (unsigned long)(1000000000.0 * duration / maxcycles);
+ g_test_message("Run operation %lu iterations %f s, %luK operations/s, "
+ "%luns per operation, "
+ "%luns(cost introduced by coroutine) per operation "
+ "with using coroutine",
+ maxcycles,
+ duration, ops,
+ cost_co, cost_co - cost);
+}
+
static __attribute__((noinline)) void perf_cost_func(void *opaque)
{
qemu_coroutine_yield();
@@ -355,6 +421,7 @@ int main(int argc, char **argv)
g_test_add_func("/perf/yield", perf_yield);
g_test_add_func("/perf/function-call", perf_baseline);
g_test_add_func("/perf/cost", perf_cost);
+ g_test_add_func("/perf/cost-with-load", perf_cost_with_load);
}
return g_test_run();
}