github.com/llir/llvm@v0.3.6/cmd/l-tm/main.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"log"
     7  	"os"
     8  	"runtime"
     9  	"runtime/pprof"
    10  	"time"
    11  
    12  	"github.com/llir/llvm/asm"
    13  )
    14  
    15  func main() {
    16  	var (
    17  		cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
    18  		memprofile = flag.String("memprofile", "", "write mem profile to file")
    19  		verbose    = flag.Bool("v", false, "verbose output")
    20  	)
    21  	flag.Parse()
    22  
    23  	if *cpuprofile != "" {
    24  		fd, err := os.Create(*cpuprofile)
    25  		if err != nil {
    26  			log.Fatalf("%+v", err)
    27  		}
    28  		pprof.StartCPUProfile(fd)
    29  		defer pprof.StopCPUProfile()
    30  	}
    31  
    32  	if *memprofile != "" {
    33  		runtime.MemProfileRate = 1
    34  	}
    35  
    36  	for _, llPath := range flag.Args() {
    37  		fmt.Fprintf(os.Stderr, "=== [ %v ] =======================\n", llPath)
    38  		fmt.Fprintln(os.Stderr)
    39  		fileStart := time.Now()
    40  		m, err := asm.ParseFile(llPath)
    41  		if err != nil {
    42  			log.Fatalf("%q: %+v", llPath, err)
    43  		}
    44  		fmt.Fprintf(os.Stderr, "total time for file %q: %v\n", llPath, time.Since(fileStart))
    45  		_ = m
    46  		if *verbose {
    47  			fmt.Fprint(os.Stdout, m)
    48  		}
    49  		//pretty.Println(m)
    50  	}
    51  
    52  	if *memprofile != "" {
    53  		fd, err := os.Create(*memprofile)
    54  		if err != nil {
    55  			log.Fatalf("%+v", err)
    56  		}
    57  		runtime.GC()
    58  		if err := pprof.WriteHeapProfile(fd); err != nil {
    59  			log.Fatalf("WriteHeapProfile: %v", err)
    60  		}
    61  	}
    62  }