示例#1
0
文件: app.go 项目: ChengShuY/ps-top
// Run runs the application in a loop until we're ready to finish
func (app *App) Run() {
	logger.Println("app.Run()")

	app.sigChan = make(chan os.Signal, 10) // 10 entries
	signal.Notify(app.sigChan, syscall.SIGINT, syscall.SIGTERM)

	eventChan := app.display.EventChan()

	for !app.Finished() {
		select {
		case sig := <-app.sigChan:
			fmt.Println("Caught signal: ", sig)
			app.finished = true
		case <-app.wi.WaitNextPeriod():
			app.Collect()
			app.Display()
			if app.stdout {
				app.setInitialFromCurrent()
			}
		case inputEvent := <-eventChan:
			switch inputEvent.Type {
			case event.EventAnonymise:
				anonymiser.Enable(!anonymiser.Enabled()) // toggle current behaviour
			case event.EventFinished:
				app.finished = true
			case event.EventViewNext:
				app.displayNext()
			case event.EventViewPrev:
				app.displayPrevious()
			case event.EventDecreasePollTime:
				if app.wi.WaitInterval() > time.Second {
					app.wi.SetWaitInterval(app.wi.WaitInterval() - time.Second)
				}
			case event.EventIncreasePollTime:
				app.wi.SetWaitInterval(app.wi.WaitInterval() + time.Second)
			case event.EventHelp:
				app.SetHelp(!app.Help())
			case event.EventToggleWantRelative:
				app.ctx.SetWantRelativeStats(!app.ctx.WantRelativeStats())
				app.Display()
			case event.EventResetStatistics:
				app.resetDBStatistics()
				app.Display()
			case event.EventResizeScreen:
				width, height := inputEvent.Width, inputEvent.Height
				app.display.Resize(width, height)
				app.Display()
			case event.EventError:
				log.Fatalf("Quitting because of EventError error")
			}
		}
		// provide a hook to stop the application if the counter goes down to zero
		if app.stdout && app.count > 0 {
			app.count--
			if app.count == 0 {
				app.finished = true
			}
		}
	}
}
示例#2
0
文件: app.go 项目: ChengShuY/ps-top
// NewApp sets up the application given various parameters.
func NewApp(settings Settings) *App {
	logger.Println("app.NewApp()")
	app := new(App)

	anonymiser.Enable(settings.Anonymise) // not dynamic at the moment
	app.dbh = settings.Conn.Handle()

	status := global.NewStatus(app.dbh)
	variables := global.NewVariables(app.dbh)
	// Prior to setting up screen check that performance_schema is enabled.
	// On MariaDB this is not the default setting so it will confuse people.
	ensurePerformanceSchemaEnabled(variables)

	app.ctx = context.NewContext(status, variables)
	app.ctx.SetWantRelativeStats(true)
	app.count = settings.Count
	app.finished = false

	app.stdout = settings.Stdout
	app.display = settings.Disp
	app.display.SetContext(app.ctx)
	app.SetHelp(false)

	if err := view.ValidateViews(app.dbh); err != nil {
		log.Fatal(err)
	}

	logger.Println("app.Setup() Setting the default view to:", settings.View)
	app.currentView.SetByName(settings.View) // if empty will use the default

	app.setupInstruments = setup_instruments.NewSetupInstruments(app.dbh)
	app.setupInstruments.EnableMonitoring()

	app.wi.SetWaitInterval(time.Second * time.Duration(settings.Interval))

	// setup to their initial types/values
	logger.Println("app.NewApp() Setup models")
	app.fsbi = fsbi.NewFileSummaryByInstance(app.ctx)
	app.tiwsbt = tiwsbt.NewTableIoLatency(app.ctx)
	app.tlwsbt = tlwsbt.NewTableLockLatency(app.ctx)
	app.ewsgben = ewsgben.NewMutexLatency(app.ctx)
	app.essgben = essgben.NewStagesLatency(app.ctx)
	app.memory = memory_usage.NewMemoryUsage(app.ctx)
	app.users = user_latency.NewUserLatency(app.ctx)
	logger.Println("app.NewApp() Finished initialising models")

	logger.Println("app.NewApp() fixLatencySetting()")
	app.fixLatencySetting() // adjust to see ops/latency

	logger.Println("app.NewApp() resetDBStatistics()")
	app.resetDBStatistics()

	logger.Println("app.NewApp() finishes")
	return app
}