| 52 | } |
| 53 | |
| 54 | func (srv *Server) runCPUProfile(args string) (io.Reader, error) { |
| 55 | duration := defaultCPUProfileDuration |
| 56 | |
| 57 | var err error |
| 58 | var blockProfile, useGzip bool |
| 59 | if args != "" { |
| 60 | s := strings.Split(args, ",") |
| 61 | // a duration string must be the 1st field, if any |
| 62 | if duration, err = time.ParseDuration(s[0]); err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | for _, arg := range s[1:] { |
| 66 | switch arg { |
| 67 | case "block", "blocked", "blocking": |
| 68 | blockProfile = true |
| 69 | case "gzip": |
| 70 | useGzip = true |
| 71 | default: |
| 72 | return nil, ErrCPUProfilingBadOption |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if atomic.LoadInt64(&srv.isCPUProfiling) > 0 { |
| 78 | return nil, ErrCPUProfilingInProgress |
| 79 | } |
| 80 | atomic.StoreInt64(&srv.isCPUProfiling, 1) |
| 81 | defer atomic.StoreInt64(&srv.isCPUProfiling, 0) |
| 82 | |
| 83 | var buf bytes.Buffer |
| 84 | var writer io.Writer = &buf |
| 85 | if blockProfile { |
| 86 | runtime.SetBlockProfileRate(1) |
| 87 | defer runtime.SetBlockProfileRate(0) |
| 88 | } |
| 89 | if useGzip { |
| 90 | writer = gzip.NewWriter(writer) |
| 91 | } |
| 92 | if err = pprof.StartCPUProfile(writer); err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | time.Sleep(duration) |
| 97 | pprof.StopCPUProfile() |
| 98 | srv.migrationContext.Log.Infof("Captured %d byte runtime/pprof CPU profile (gzip=%v)", buf.Len(), useGzip) |
| 99 | return &buf, nil |
| 100 | } |
| 101 | |
| 102 | func (srv *Server) createPostponeCutOverFlagFile(filePath string) (err error) { |
| 103 | if !base.FileExists(filePath) { |