Closed mozartfish closed 1 year ago
I am very happy that you are contributing to this project.
After applying the following patch to the main branch, the benchmark runs and outputs example-cpu.prof. This seems like a good starting point.
diff --git a/machine/machine_test.go b/machine/machine_test.go
index f1d7d600a835..6ae44dc70fe7 100644
--- a/machine/machine_test.go
+++ b/machine/machine_test.go
@@ -9,6 +9,7 @@ import (
"syscall"
"testing"
"time"
+ "runtime/pprof"
"github.com/bobuhiro11/gokvm/kvm"
"github.com/bobuhiro11/gokvm/machine"
@@ -95,6 +96,48 @@ func testNewAndLoadLinux(t *testing.T, kernel, tap, guestIPv4, hostIPv4, prefixL
}
}
+func BenchmarkNewAndLoadLinuxWithBzImage(b *testing.B) { // nolint:paralleltest
+ cpuProfile, err := os.Create("../example-cpu.prof")
+ if err != nil {
+ b.Fatal(err)
+ }
+ pprof.StartCPUProfile(cpuProfile)
+ defer pprof.StopCPUProfile()
+
+ for i := 0; i < b.N; i++ {
+ m, err := machine.New("/dev/kvm", 1, 1<<29)
+ if err != nil {
+ b.Fatal(err)
+ }
+
+ param := fmt.Sprintf(`console=ttyS0 earlyprintk=serial noapic noacpi notsc `+
+ `lapic tsc_early_khz=2000 pci=realloc=off virtio_pci.force_legacy=1 `+
+ `rdinit=/init init=/init`)
+
+ kern, err := os.Open("../bzImage")
+ if err != nil {
+ b.Fatal(err)
+ }
+
+ initrd, err := os.Open("../initrd")
+ if err != nil {
+ b.Fatal(err)
+ }
+
+ if err = m.LoadLinux(kern, initrd, param); err != nil {
+ b.Fatal(err)
+ }
+
+ go func() {
+ if err = m.RunInfiniteLoop(0); err != nil {
+ panic(err)
+ }
+ }()
+
+ time.Sleep(10 * time.Second)
+ }
+}
+
func TestNewAndLoadLinuxWithBzImage(t *testing.T) { // nolint:paralleltest
testNewAndLoadLinux(t, "../bzImage", "tap0", "192.168.20.1", "192.168.20.2", "24")
}
$ go test -bench=. ./machine/... -run BenchmarkNewAndLoadLinuxWithBzImage
Here are some other points of interest.
main.go is the entry point for the CLI, so I don't want to touch it here if possible. Instead, machine_test.go, for example, is for testing purposes, so I feel it would be appropriate to implement profiles and benchmarks in this file.
Proposed changes here is waiting for input from the user (e.g. b, err := in.ReadByte()
), so maybe it is not terminating normally. If so, example-cpu.prof might not be generated. These are my predictions.
The scope of the benchmark includes loading the kernel image, so it would be good to limit it to what you are interested in.
I'm attempting to create a cpu.prof file using the following strategies
I'm producing an empty cpu.prof file and I can't figure out why this is happening and wondering if anyone has any suggestions for what might be causing this issue because I have been able to get smaller programs such as fibonacci program working.
Thank you