Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions cmd/cog/cog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@ package main
import (
"github.com/replicate/cog/pkg/cli"
"github.com/replicate/cog/pkg/console"
"github.com/replicate/cog/pkg/global"
)

func main() {
cmd, err := cli.NewRootCommand()
if err != nil {
console.Fatalf("%f", err)
}
defer func() {
if global.Profiler != nil {
global.Profiler.Stop()
}
}()

if err = cmd.Execute(); err != nil {
console.Fatalf("%s", err)
Expand Down
4 changes: 0 additions & 4 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"regexp"

"github.com/pkg/profile"
"github.com/spf13/cobra"

"github.com/replicate/cog/pkg/console"
Expand All @@ -29,9 +28,6 @@ func NewRootCommand() (*cobra.Command, error) {
if global.Verbose {
console.SetLevel(console.DebugLevel)
}
if global.ProfilingEnabled {
global.Profiler = profile.Start(profile.MemProfile)
}
cmd.SilenceUsage = true
},
SilenceErrors: true,
Expand Down
15 changes: 7 additions & 8 deletions pkg/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
)

var (
Version = "0.0.1"
BuildTime = "none"
Verbose = false
ProfilingEnabled = false
Profiler interface{ Stop() } = nil
StartupTimeout = 5 * time.Minute
ConfigFilename = "cog.yaml"
CogServerAddress = "http://cog.replicate.ai" // TODO(andreas): https
Version = "0.0.1"
BuildTime = "none"
Verbose = false
ProfilingEnabled = false
StartupTimeout = 5 * time.Minute
ConfigFilename = "cog.yaml"
CogServerAddress = "http://cog.replicate.ai" // TODO(andreas): https
)
73 changes: 73 additions & 0 deletions pkg/server/profiling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package server

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/pkg/profile"
)

// Profile responds with the pprof-formatted memory profile.
// Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified.
// The package initialization registers it as /debug/pprof/profile.
func profileMemory(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Content-Type-Options", "nosniff")
sec, err := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
if sec <= 0 || err != nil {
sec = 30
}

if durationExceedsWriteTimeout(r, float64(sec)) {
pprofServeError(w, http.StatusBadRequest, "profile duration exceeds server's WriteTimeout")
return
}

w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", `attachment; filename="profile"`)
profileDir, err := os.MkdirTemp("", "cog-mem-profile")
if err != nil {
pprofServeError(w, http.StatusInternalServerError,
fmt.Sprintf("Failed to create temp directory: %s", err))
return
}
profiler := profile.Start(profile.ProfilePath(profileDir), profile.MemProfile)

pprofSleep(r, time.Duration(sec)*time.Second)
profiler.Stop()

file, err := os.Open(filepath.Join(profileDir, "mem.pprof"))
if err != nil {
pprofServeError(w, http.StatusInternalServerError,
fmt.Sprintf("Failed to create temp directory: %s", err))
}
defer file.Close()
if _, err := io.Copy(w, file); err != nil {
pprofServeError(w, http.StatusInternalServerError,
fmt.Sprintf("Failed to copy memory profile contents: %s", err))
}
}

func durationExceedsWriteTimeout(r *http.Request, seconds float64) bool {
srv, ok := r.Context().Value(http.ServerContextKey).(*http.Server)
return ok && srv.WriteTimeout != 0 && seconds >= srv.WriteTimeout.Seconds()
}

func pprofServeError(w http.ResponseWriter, status int, txt string) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Go-Pprof", "1")
w.Header().Del("Content-Disposition")
w.WriteHeader(status)
fmt.Fprintln(w, txt)
}

func pprofSleep(r *http.Request, d time.Duration) {
select {
case <-time.After(d):
case <-r.Context().Done():
}
}
14 changes: 14 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"net/http"
"net/http/pprof"
"os"

"github.com/gorilla/handlers"
Expand All @@ -11,6 +12,7 @@ import (
"github.com/replicate/cog/pkg/console"
"github.com/replicate/cog/pkg/database"
"github.com/replicate/cog/pkg/docker"
"github.com/replicate/cog/pkg/global"
"github.com/replicate/cog/pkg/serving"
"github.com/replicate/cog/pkg/storage"
)
Expand Down Expand Up @@ -90,6 +92,18 @@ func (s *Server) Start() error {
router.Path("/v1/repos/{user}/{name}/check-read").
Methods(http.MethodGet).
HandlerFunc(s.checkReadAccess(nil))

if global.ProfilingEnabled {
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/profile-mem", profileMemory)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
router.Handle("/debug/pprof/allocs", pprof.Handler("allocs"))
router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
}

console.Infof("Server running on 0.0.0.0:%d", s.port)

loggedRouter := handlers.LoggingHandler(os.Stdout, router)
Expand Down