go-hep.org/x/hep@v0.38.1/fwk/cmd/fwk-cpu-cruncher/main.go (about)

     1  // Copyright ©2017 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"log"
    11  	"os"
    12  	"runtime/pprof"
    13  	"time"
    14  
    15  	"go-hep.org/x/hep/fwk/job"
    16  )
    17  
    18  var (
    19  	lvl     = flag.String("l", "INFO", "log level (DEBUG|INFO|WARN|ERROR)")
    20  	evtmax  = flag.Int("evtmax", 10, "number of events to process")
    21  	nprocs  = flag.Int("nprocs", 0, "number of concurrent events to process")
    22  	dotfile = flag.String("dotfile", "", "path to dotfile for dumping dataflow graph")
    23  	cpu     = flag.Bool("cpu-prof", false, "enable CPU profiling")
    24  )
    25  
    26  func main() {
    27  	flag.Usage = func() {
    28  		fmt.Fprintf(os.Stderr, `Usage: fwk-cpu-cruncher [options] <config-file>
    29  
    30  ex:
    31   $ fwk-cpu-cruncher -l=INFO -evtmax=100 ./testdata/athena.json
    32  
    33  options:
    34  `,
    35  		)
    36  		flag.PrintDefaults()
    37  	}
    38  
    39  	flag.Parse()
    40  
    41  	if flag.NArg() <= 0 {
    42  		fmt.Fprintf(os.Stderr, "** error: needs an input cpu-cruncher configuration file\n")
    43  		flag.Usage()
    44  		os.Exit(1)
    45  	}
    46  	fname := flag.Arg(0)
    47  
    48  	start := time.Now()
    49  
    50  	fmt.Printf("::: fwk-cpu-cruncher...\n")
    51  	if *cpu {
    52  		f, err := os.Create("cpu.prof")
    53  		if err != nil {
    54  			panic(err)
    55  		}
    56  		defer f.Close()
    57  		err = pprof.StartCPUProfile(f)
    58  		if err != nil {
    59  			log.Fatalf("could not start CPU profile: %+v", err)
    60  		}
    61  		defer pprof.StopCPUProfile()
    62  	}
    63  
    64  	app := job.New(job.P{
    65  		"EvtMax":   int64(*evtmax),
    66  		"NProcs":   *nprocs,
    67  		"MsgLevel": job.MsgLevel(*lvl),
    68  	})
    69  
    70  	if *dotfile != "" {
    71  		dflow := app.App().GetSvc("dataflow")
    72  		if dflow == nil {
    73  			panic(fmt.Errorf("could not retrieve dataflow service"))
    74  		}
    75  
    76  		app.SetProp(dflow, "DotFile", *dotfile)
    77  	}
    78  
    79  	loadConfig(fname, app)
    80  
    81  	app.Run()
    82  	fmt.Printf("::: fwk-cpu-cruncher... [done] (time=%v)\n", time.Since(start))
    83  }