Skip to content

Commit f58ac9b

Browse files
committed
feat: add env var to enable CPU profiling
1 parent 63a1f01 commit f58ac9b

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

conf/systemd/etc/systemd/system/ehfs.service

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ After=network.target
66
Type=simple
77
ExecStartPre=/sbin/setcap CAP_NET_BIND_SERVICE=+ep /usr/local/bin/ehfs
88
ExecStart=/sbin/runuser -u nobody -- /usr/local/bin/ehfs --config=/etc/ehfs.conf
9+
# ExecStart=/sbin/runuser -u nobody -- sh -c 'GHFS_CPU_PROFILE_FILE=/var/log/cpu-`date +%%F-%%T`.pprof exec /usr/local/bin/ehfs --config=/etc/ehfs.conf'
910
ExecReload=/bin/kill -s HUP $MAINPID
1011
KillSignal=SIGTERM
1112
KillMode=process

conf/systemd/etc/systemd/system/[email protected]

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ After=network.target
66
Type=simple
77
ExecStartPre=/sbin/setcap CAP_NET_BIND_SERVICE=+ep /usr/local/bin/ehfs
88
ExecStart=/sbin/runuser -u nobody -- /usr/local/bin/ehfs --config=/etc/ehfs_%I.conf
9+
# ExecStart=/sbin/runuser -u nobody -- sh -c 'GHFS_CPU_PROFILE_FILE=/var/log/cpu-`date +%%F-%%T`.pprof exec /usr/local/bin/ehfs --config=/etc/ehfs_%I.conf'
910
ExecReload=/bin/kill -s HUP $MAINPID
1011
KillSignal=SIGTERM
1112
KillMode=process

src/main.go

+32-14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"mjpclab.dev/ehfs/src/middleware"
77
"mjpclab.dev/ehfs/src/param"
88
"mjpclab.dev/ehfs/src/version"
9+
"mjpclab.dev/ghfs/src"
910
"mjpclab.dev/ghfs/src/app"
1011
"mjpclab.dev/ghfs/src/serverError"
1112
"mjpclab.dev/ghfs/src/setting"
@@ -22,7 +23,6 @@ func cleanupOnEnd(appInst *app.App) {
2223
go func() {
2324
<-chSignal
2425
appInst.Shutdown()
25-
os.Exit(0)
2626
}()
2727
}
2828

@@ -33,44 +33,62 @@ func reopenLogOnHup(appInst *app.App) {
3333
go func() {
3434
for range chSignal {
3535
errs := appInst.ReOpenLog()
36-
serverError.CheckFatal(errs...)
36+
if serverError.CheckError(errs...) {
37+
appInst.Shutdown()
38+
break
39+
}
3740
}
3841
}()
3942
}
4043

4144
func Main() {
4245
// params
4346
baseParams, params, printVersion, printHelp, errs := param.ParseFromCli()
44-
serverError.CheckFatal(errs...)
47+
if serverError.CheckError(errs...) {
48+
return
49+
}
4550
if printVersion {
4651
version.PrintVersion()
47-
os.Exit(0)
52+
return
4853
}
4954
if printHelp {
5055
param.PrintHelp()
51-
os.Exit(0)
56+
return
5257
}
5358

5459
// apply middlewares
5560
errs = middleware.ApplyMiddlewares(baseParams, params)
56-
serverError.CheckFatal(errs...)
57-
58-
// setting
59-
setting := setting.ParseFromEnv()
61+
if serverError.CheckError(errs...) {
62+
return
63+
}
6064

6165
// override default theme
6266
defaultTheme.DefaultTheme = localDefaultTheme.DefaultTheme
6367

68+
// settings
69+
settings := setting.ParseFromEnv()
70+
71+
// CPU profile
72+
if len(settings.CPUProfileFile) > 0 {
73+
cpuProfileFile, err := src.StartCPUProfile(settings.CPUProfileFile)
74+
if serverError.CheckError(err) {
75+
return
76+
}
77+
defer src.StopCPUProfile(cpuProfileFile)
78+
}
79+
6480
// app
65-
appInst, errs := app.NewApp(baseParams, setting)
66-
serverError.CheckFatal(errs...)
81+
appInst, errs := app.NewApp(baseParams, settings)
82+
if serverError.CheckError(errs...) {
83+
return
84+
}
6785
if appInst == nil {
68-
serverError.CheckFatal(errors.New("failed to create application instance"))
86+
serverError.CheckError(errors.New("failed to create application instance"))
87+
return
6988
}
7089

7190
cleanupOnEnd(appInst)
7291
reopenLogOnHup(appInst)
7392
errs = appInst.Open()
74-
serverError.CheckFatal(errs...)
75-
appInst.Shutdown()
93+
serverError.CheckError(errs...)
7694
}

0 commit comments

Comments
 (0)