-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow mem profiling without shutting down the server
- Loading branch information
Showing
2 changed files
with
28 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,23 +130,35 @@ func NewServerCommand() *cobra.Command { | |
signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
<-osSignals | ||
s.Grpc.GracefulStop() | ||
}() | ||
|
||
if memProfilePath != "" { | ||
memProfile, err := os.Create(memProfilePath) | ||
if err != nil { | ||
logger.Error(ctx, "cannot create heap profile file", zap.Error(err), zap.String("path", memProfilePath)) | ||
} | ||
defer memProfile.Close() | ||
runtime.GC() | ||
if memProfilePath != "" { | ||
memSnapshotSignals := make(chan os.Signal, 1) | ||
signal.Notify(memSnapshotSignals, syscall.SIGUSR2) | ||
|
||
err = pprof.WriteHeapProfile(memProfile) | ||
if err != nil { | ||
logger.Error(ctx, "cannot write heap profile", zap.Error(err), zap.String("path", memProfilePath)) | ||
} | ||
} | ||
go func() { | ||
for { | ||
<-memSnapshotSignals | ||
|
||
s.Grpc.GracefulStop() | ||
}() | ||
logger.Info(ctx, "SIGUSR2 received, building heap profile", zap.String("path", memProfilePath)) | ||
|
||
memProfile, err := os.Create(memProfilePath) | ||
if err != nil { | ||
logger.Error(ctx, "cannot create heap profile file", zap.Error(err), zap.String("path", memProfilePath)) | ||
} | ||
|
||
runtime.GC() | ||
|
||
err = pprof.WriteHeapProfile(memProfile) | ||
if err != nil { | ||
logger.Error(ctx, "cannot write heap profile", zap.Error(err), zap.String("path", memProfilePath)) | ||
} | ||
|
||
memProfile.Close() | ||
} | ||
}() | ||
} | ||
|
||
logger.Info(ctx, "start fs server", key.Port.Field(port), key.Environment.Field(env.String())) | ||
return s.Serve(listen) | ||
|
@@ -171,7 +183,7 @@ func NewServerCommand() *cobra.Command { | |
flags.StringVar(&encoding, "log-encoding", "console", "Log encoding (console | json)") | ||
flags.BoolVar(&tracing, "tracing", false, "Whether tracing is enabled") | ||
flags.StringVar(&profilePath, "profile", "", "CPU profile output path (CPU profiling enabled if set)") | ||
flags.StringVar(&memProfilePath, "memprofile", "", "Memory profile output path") | ||
flags.StringVar(&memProfilePath, "memprofile", "mem.pb.gz", "Memory profile output path") | ||
|
||
flags.IntVar(&port, "port", 5051, "GRPC server port") | ||
flags.StringVar(&dbUri, "dburi", "postgres://[email protected]:5432/dl", "Postgres URI") | ||
|