1 // lib - library routines for pstop.
3 // this file contains the library routines related to the stored state in pstop.
11 "github.com/sjmudd/pstop/lib"
12 fsbi "github.com/sjmudd/pstop/performance_schema/file_summary_by_instance"
13 "github.com/sjmudd/pstop/performance_schema/ps_table"
14 tiwsbt "github.com/sjmudd/pstop/performance_schema/table_io_waits_summary_by_table"
15 tlwsbt "github.com/sjmudd/pstop/performance_schema/table_lock_waits_summary_by_table"
16 "github.com/sjmudd/pstop/screen"
17 "github.com/sjmudd/pstop/version"
20 // what information to show
35 fsbi ps_table.Tabler // ufsbi.File_summary_by_instance
36 tiwsbt tiwsbt.Table_io_waits_summary_by_table
37 tlwsbt ps_table.Tabler // tlwsbt.Table_lock_waits_summary_by_table
38 screen screen.TermboxScreen
41 want_relative_stats bool
44 func (state *State) Setup(dbh *sql.DB) {
47 state.screen.Initialise()
49 _, variables := lib.SelectAllGlobalVariablesByVariableName(state.dbh)
50 // setup to their initial types/values
51 state.fsbi = fsbi.NewFileSummaryByInstance(variables)
52 state.tlwsbt = new(tlwsbt.Table_lock_waits_summary_by_table)
54 state.want_relative_stats = true // we show info from the point we start collecting data
55 state.fsbi.SetWantRelativeStats(state.want_relative_stats)
57 state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
59 state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
62 state.ResetDBStatistics()
65 state.show = showLatency
66 state.tiwsbt.SetWantsLatency(true)
68 _, hostname := lib.SelectGlobalVariableByVariableName(state.dbh, "HOSTNAME")
69 _, mysql_version := lib.SelectGlobalVariableByVariableName(state.dbh, "VERSION")
70 _, datadir := lib.SelectGlobalVariableByVariableName(state.dbh, "DATADIR")
71 state.SetHostname(hostname)
72 state.SetMySQLVersion(mysql_version)
73 state.SetDatadir(datadir)
76 // do a fresh collection of data and then update the initial values based on that.
77 func (state *State) ResetDBStatistics() {
79 state.UpdateInitialValues()
82 func (state *State) UpdateInitialValues() {
84 state.fsbi.UpdateInitialValues()
85 state.tlwsbt.UpdateInitialValues()
86 state.tiwsbt.UpdateInitialValues()
87 lib.Logger.Println("state.UpdateInitialValues() took", time.Duration(time.Since(start)).String())
90 func (state *State) Collect() {
92 state.fsbi.Collect(state.dbh)
93 state.tlwsbt.Collect(state.dbh)
94 state.tiwsbt.Collect(state.dbh)
95 lib.Logger.Println("state.Collect() took", time.Duration(time.Since(start)).String())
98 func (state State) MySQLVersion() string {
99 return state.mysql_version
102 func (state State) Datadir() string {
106 func (state *State) SetHelp(newHelp bool) {
113 func (state *State) SetDatadir(datadir string) {
114 state.datadir = datadir
117 func (state *State) SetMySQLVersion(mysql_version string) {
118 state.mysql_version = mysql_version
121 func (state *State) SetHostname(hostname string) {
122 state.hostname = hostname
125 func (state State) Help() bool {
129 // display the output according to the mode we are in
130 func (state *State) Display() {
132 state.screen.DisplayHelp()
134 state.displayHeading()
136 case showLatency, showOps:
137 state.displayOpsOrLatency()
146 // change to the next display mode
147 func (state *State) DisplayNext() {
148 if state.show == showLocks {
149 state.show = showLatency
153 // this needs to be done more cleanly
154 if state.show == showLatency {
155 state.tiwsbt.SetWantsLatency(true)
157 if state.show == showOps {
158 state.tiwsbt.SetWantsLatency(false)
164 func (state State) displayHeading() {
169 func (state State) displayLine0() {
170 _, uptime := lib.SelectGlobalStatusByVariableName(state.dbh, "UPTIME")
171 top_line := lib.MyName() + " " + version.Version() + " - " + now_hhmmss() + " " + state.hostname + " / " + state.mysql_version + ", up " + fmt.Sprintf("%-16s", lib.Uptime(uptime))
172 if state.want_relative_stats {
175 var initial time.Time
178 case showLatency, showOps:
179 initial = state.tiwsbt.Last()
181 initial = state.fsbi.Last()
183 initial = state.tlwsbt.Last()
185 initial = time.Now() // THIS IS WRONG !!!
188 d := now.Sub(initial)
190 top_line = top_line + " [REL] " + fmt.Sprintf("%.0f seconds", d.Seconds())
192 top_line = top_line + " [ABS] "
194 state.screen.PrintAt(0, 0, top_line)
197 func (state State) displayLine1() {
199 case showLatency, showOps:
200 state.screen.PrintAt(0, 1, state.tiwsbt.Description())
202 state.screen.PrintAt(0, 1, state.fsbi.Description())
204 state.screen.PrintAt(0, 1, state.tlwsbt.Description())
206 state.screen.PrintAt(0, 1, "UNKNOWN")
210 func (state *State) displayOpsOrLatency() {
211 state.screen.PrintAt(0, 2, state.tiwsbt.Headings())
213 max_rows := state.screen.Height() - 3
214 row_content := state.tiwsbt.RowContent(max_rows)
217 for k := range row_content {
219 state.screen.PrintAt(0, y, row_content[k])
221 // print out empty rows
222 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
224 if y < state.screen.Height()-1 {
225 state.screen.PrintAt(0, y, state.tiwsbt.EmptyRowContent())
229 // print out the totals at the bottom
230 state.screen.PrintAt(0, state.screen.Height()-1, state.tiwsbt.TotalRowContent())
233 // show actual I/O latency values
234 func (state State) displayIO() {
235 state.screen.PrintAt(0, 2, state.fsbi.Headings())
237 // print out the data
238 max_rows := state.screen.Height() - 3
239 row_content := state.fsbi.RowContent(max_rows)
242 for k := range row_content {
244 state.screen.PrintAt(0, y, row_content[k])
246 // print out empty rows
247 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
249 if y < state.screen.Height()-1 {
250 state.screen.PrintAt(0, y, state.fsbi.EmptyRowContent())
254 // print out the totals at the bottom
255 state.screen.PrintAt(0, state.screen.Height()-1, state.fsbi.TotalRowContent())
258 func (state *State) displayLocks() {
259 state.screen.PrintAt(0, 2, state.tlwsbt.Headings())
261 // print out the data
262 max_rows := state.screen.Height() - 3
263 row_content := state.tlwsbt.RowContent(max_rows)
266 for k := range row_content {
268 state.screen.PrintAt(0, y, row_content[k])
270 // print out empty rows
271 for k := len(row_content); k < (state.screen.Height() - 3); k++ {
273 if y < state.screen.Height()-1 {
274 state.screen.PrintAt(0, y, state.tlwsbt.EmptyRowContent())
278 // print out the totals at the bottom
279 state.screen.PrintAt(0, state.screen.Height()-1, state.tlwsbt.TotalRowContent())
282 // do we want to show all p_s data?
283 func (state State) WantRelativeStats() bool {
284 return state.want_relative_stats
287 // set if we want data from when we started/reset stats.
288 func (state *State) SetWantRelativeStats(want_relative_stats bool) {
289 state.want_relative_stats = want_relative_stats
291 state.fsbi.SetWantRelativeStats(want_relative_stats)
292 state.tlwsbt.SetWantRelativeStats(state.want_relative_stats)
293 state.tiwsbt.SetWantRelativeStats(state.want_relative_stats)
298 // if there's a better way of doing this do it better ...
299 func now_hhmmss() string {
301 return fmt.Sprintf("%2d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
304 // record the latest screen size
305 func (state *State) ScreenSetSize(width, height int) {
306 state.screen.SetSize(width, height)
309 // clean up screen and disconnect database
310 func (state *State) Cleanup() {
312 if state.dbh != nil {
313 _ = state.dbh.Close()