Browse Source

Enable pprof http server via build tag

- Remove obsolete ConsoleClient "profile"
  command-line flag

- Intended for debug builds only
Rod Hynes 7 years ago
parent
commit
71259d937a
4 changed files with 66 additions and 16 deletions
  1. 0 16
      ConsoleClient/main.go
  2. 2 0
      psiphon/controller.go
  3. 39 0
      psiphon/pprof.go
  4. 25 0
      psiphon/pprof_disabled.go

+ 0 - 16
ConsoleClient/main.go

@@ -29,7 +29,6 @@ import (
 	"io/ioutil"
 	"os"
 	"os/signal"
-	"runtime/pprof"
 	"sort"
 	"sync"
 
@@ -52,9 +51,6 @@ func main() {
 	var formatNotices bool
 	flag.BoolVar(&formatNotices, "formatNotices", false, "emit notices in human-readable format")
 
-	var profileFilename string
-	flag.StringVar(&profileFilename, "profile", "", "CPU profile output file")
-
 	var interfaceName string
 	flag.StringVar(&interfaceName, "listenInterface", "", "bind local proxies to specified interface")
 
@@ -216,18 +212,6 @@ func main() {
 
 	psiphon.NoticeBuildInfo()
 
-	// Handle optional profiling parameter
-
-	if profileFilename != "" {
-		profileFile, err := os.Create(profileFilename)
-		if err != nil {
-			psiphon.NoticeError("error opening profile file: %s", err)
-			os.Exit(1)
-		}
-		pprof.StartCPUProfile(profileFile)
-		defer pprof.StopCPUProfile()
-	}
-
 	// Initialize data store
 
 	err = psiphon.OpenDataStore(config)

+ 2 - 0
psiphon/controller.go

@@ -156,6 +156,8 @@ func NewController(config *Config) (controller *Controller, err error) {
 // component fails or the parent context is canceled.
 func (controller *Controller) Run(ctx context.Context) {
 
+	pprofRun()
+
 	// Ensure fresh repetitive notice state for each run, so the
 	// client will always get an AvailableEgressRegions notice,
 	// an initial instance of any repetitive error notice, etc.

+ 39 - 0
psiphon/pprof.go

@@ -0,0 +1,39 @@
+// +build RUN_PPROF
+
+/*
+ * Copyright (c) 2018, Psiphon Inc.
+ * All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package psiphon
+
+import (
+	"net/http"
+	_ "net/http/pprof"
+	"sync"
+)
+
+var pprofRunOnce sync.Once
+
+func pprofRun() {
+	pprofRunOnce.Do(func() {
+		go func() {
+			NoticeInfo("Running http://localhost:6060/debug/pprof/")
+			NoticeInfo("pprofRun: %s", http.ListenAndServe("localhost:6060", nil))
+		}()
+	})
+}

+ 25 - 0
psiphon/pprof_disabled.go

@@ -0,0 +1,25 @@
+// +build !RUN_PPROF
+
+/*
+ * Copyright (c) 2018, Psiphon Inc.
+ * All rights reserved.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package psiphon
+
+func pprofRun() {
+}