暫無描述

pprof.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // pprof is a tool for collection, manipulation and visualization
  15. // of performance profiles.
  16. package main
  17. import (
  18. "fmt"
  19. "os"
  20. "strings"
  21. "github.com/chzyer/readline"
  22. "github.com/google/pprof/driver"
  23. )
  24. func main() {
  25. if err := driver.PProf(&driver.Options{UI: newUI()}); err != nil {
  26. fmt.Fprintf(os.Stderr, "pprof: %v\n", err)
  27. os.Exit(2)
  28. }
  29. }
  30. // readlineUI implements the driver.UI interface using the
  31. // github.com/chzyer/readline library.
  32. // This is contained in pprof.go to avoid adding the readline
  33. // dependency in the vendored copy of pprof in the Go distribution,
  34. // which does not use this file.
  35. type readlineUI struct {
  36. rl *readline.Instance
  37. }
  38. func newUI() driver.UI {
  39. rl, err := readline.New("")
  40. if err != nil {
  41. fmt.Fprintf(os.Stderr, "readline: %v", err)
  42. return nil
  43. }
  44. return &readlineUI{
  45. rl: rl,
  46. }
  47. }
  48. // Read returns a line of text (a command) read from the user.
  49. // prompt is printed before reading the command.
  50. func (r *readlineUI) ReadLine(prompt string) (string, error) {
  51. r.rl.SetPrompt(prompt)
  52. return r.rl.Readline()
  53. }
  54. // Print shows a message to the user.
  55. // It is printed over stderr as stdout is reserved for regular output.
  56. func (r *readlineUI) Print(args ...interface{}) {
  57. text := fmt.Sprint(args...)
  58. if !strings.HasSuffix(text, "\n") {
  59. text += "\n"
  60. }
  61. fmt.Fprint(r.rl.Stderr(), text)
  62. }
  63. // Print shows a message to the user, colored in red for emphasis.
  64. // It is printed over stderr as stdout is reserved for regular output.
  65. func (r *readlineUI) PrintErr(args ...interface{}) {
  66. text := fmt.Sprint(args...)
  67. if !strings.HasSuffix(text, "\n") {
  68. text += "\n"
  69. }
  70. fmt.Fprint(r.rl.Stderr(), colorize(text))
  71. }
  72. // colorize the msg using ANSI color escapes.
  73. func colorize(msg string) string {
  74. var red = 31
  75. var colorEscape = fmt.Sprintf("\033[0;%dm", red)
  76. var colorResetEscape = "\033[0m"
  77. return colorEscape + msg + colorResetEscape
  78. }
  79. // IsTerminal returns whether the UI is known to be tied to an
  80. // interactive terminal (as opposed to being redirected to a file).
  81. func (r *readlineUI) IsTerminal() bool {
  82. const stdout = 1
  83. return readline.IsTerminal(stdout)
  84. }
  85. // Start a browser on interactive mode.
  86. func (r *readlineUI) WantBrowser() bool {
  87. return r.IsTerminal()
  88. }
  89. // SetAutoComplete instructs the UI to call complete(cmd) to obtain
  90. // the auto-completion of cmd, if the UI supports auto-completion at all.
  91. func (r *readlineUI) SetAutoComplete(complete func(string) string) {
  92. // TODO: Implement auto-completion support.
  93. }