Modified heap profiling to only dump at end of program, not after every generation.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 16 May 2021 01:07:13 +0000 (18:07 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Sun, 16 May 2021 01:07:13 +0000 (18:07 -0700)
chapter-1-experiments/ch1-breeding-numbers.go

index 35f0e20..c5baa14 100644 (file)
@@ -10,7 +10,6 @@ import (
     "os"
     "runtime/pprof"
     "sort"
-    "strconv"
 )
 
 // =============================================================================
@@ -222,7 +221,7 @@ func (s *surrealSet) remove(num surrealNumber, u surrealUniverse) {
 func main() {
     // Flags to enable various performance profiling options.
     cpuProfile := flag.String("cpuprofile", "", "Filename for saving CPU profile output.")
-    memProfilePrefix := flag.String("memprofileprefix", "", "Filename PREFIX for saving memory profile output.")
+    memProfile := flag.String("memprofile", "", "Filename for saving memory profile output.")
     suppressOutput := flag.Bool("silent", false, "Suppress printing of numberline at end of simulation. Useful when profiling.")
 
     // Obtain termination conditions from user.
@@ -266,20 +265,19 @@ func main() {
         // Attempt to add the new numbers to the universe. Any duplicates will
         // be weeded out in the attempt.
         addNumbersToUniverse(potentiallyNewNumbers, &universe)
-
-        // Setup any memory profiling requested by the user. This will snapshot
-        // the heap at the end of every generation.
-        if *memProfilePrefix != "" {
-            memProFile, err := os.Create(*memProfilePrefix + "-gen" + strconv.Itoa(generation) + ".mprof")
-            if err != nil {
-                log.Fatal("ERROR: Unable to write heap profile to disk:", err)
-            }
-            pprof.WriteHeapProfile(memProFile)
-            memProFile.Close()
-        }
     }
     fmt.Printf(".\n")
 
+    // Setup any memory profiling requested by the user. This will snapshot
+    // the heap only at this point, after all numbers were generated.
+    if *memProfile != "" {
+        memProFile, err := os.Create(*memProfile)
+        if err != nil {
+            log.Fatal("ERROR: Unable to open heap profile output file:", err)
+        }
+        pprof.WriteHeapProfile(memProFile)
+        memProFile.Close()
+    }
 
     // Print the number line with generation on the horizontal axis and
     // magnitude on the vertical axis.