Killed a few stale features and removed a global debug flag.

For GoogleCloudPlatform/gcsfuse#89.
geesefs-0-30-9
Aaron Jacobs 2015-07-16 10:55:54 +10:00
commit 1a3bd0eae3
8 changed files with 25 additions and 167 deletions

View File

@ -15,29 +15,18 @@
package fuse
import (
"flag"
"fmt"
"log"
"path"
"runtime"
"sync"
"time"
"golang.org/x/net/context"
"golang.org/x/sys/unix"
"github.com/jacobsa/bazilfuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/reqtrace"
)
var fTraceByPID = flag.Bool(
"fuse.trace_by_pid",
false,
"Enable a hacky mode that uses reqtrace to group all ops from each "+
"individual PID. Not a good idea to use in production; races, bugs, and "+
"resource leaks likely lurk.")
// A connection to the fuse kernel process.
type Connection struct {
debugLogger *log.Logger
@ -57,11 +46,6 @@ type Connection struct {
//
// GUARDED_BY(mu)
cancelFuncs map[bazilfuse.RequestID]func()
// A map from PID to a traced context for that PID.
//
// GUARDED_BY(mu)
pidMap map[int]context.Context
}
// Responsibility for closing the wrapped connection is transferred to the
@ -77,7 +61,6 @@ func newConnection(
wrapped: wrapped,
parentCtx: parentCtx,
cancelFuncs: make(map[bazilfuse.RequestID]func()),
pidMap: make(map[int]context.Context),
}
return
@ -127,85 +110,6 @@ func (c *Connection) recordCancelFunc(
c.cancelFuncs[reqID] = f
}
// Wait until the process completes, then close off the trace and remove the
// context from the map.
//
// LOCKS_EXCLUDED(c.mu)
func (c *Connection) reportWhenPIDGone(
pid int,
ctx context.Context,
report reqtrace.ReportFunc) {
// HACK(jacobsa): Poll until the process no longer exists.
const pollPeriod = 50 * time.Millisecond
for {
// The man page for kill(2) says that if the signal is zero, then "no
// signal is sent, but error checking is still performed; this can be used
// to check for the existence of a process ID".
err := unix.Kill(pid, 0)
// ESRCH means the process is gone.
if err == unix.ESRCH {
break
}
// If we receive EPERM, we're not going to be able to do what we want. We
// don't really have any choice but to print info and leak.
if err == unix.EPERM {
log.Printf("Failed to kill(2) PID %v; no permissions. Leaking trace.", pid)
return
}
// Otherwise, panic.
if err != nil {
panic(fmt.Errorf("Kill(%v): %v", pid, err))
}
time.Sleep(pollPeriod)
}
// Finish up.
report(nil)
c.mu.Lock()
delete(c.pidMap, pid)
c.mu.Unlock()
}
// Set up a hacky per-PID trace context, if enabled. Either way, return a
// context from which an operation should inherit.
//
// See notes on fTraceByPID.
//
// LOCKS_EXCLUDED(c.mu)
func (c *Connection) maybeTraceByPID(
pid int) (ctx context.Context) {
ctx = c.parentCtx
// Is there anything to do?
if !reqtrace.Enabled() || !*fTraceByPID {
return
}
c.mu.Lock()
defer c.mu.Unlock()
// Do we already have a traced context for this PID?
if existing, ok := c.pidMap[pid]; ok {
ctx = existing
return
}
// Set up a new one and stick it in the map.
var report reqtrace.ReportFunc
ctx, report = reqtrace.Trace(ctx, fmt.Sprintf("Requests from PID %v", pid))
c.pidMap[pid] = ctx
// Ensure we close the trace and remove it from the map eventually.
go c.reportWhenPIDGone(pid, ctx, report)
return
}
// Set up state for an op that is about to be returned to the user, given its
// underlying bazilfuse request.
//
@ -216,8 +120,8 @@ func (c *Connection) beginOp(
bfReq bazilfuse.Request) (ctx context.Context) {
reqID := bfReq.Hdr().ID
// Choose a parent context.
ctx = c.maybeTraceByPID(int(bfReq.Hdr().Pid))
// Start with the parent context.
ctx = c.parentCtx
// Set up a cancellation function.
//

View File

@ -1,51 +0,0 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fuse
import (
"flag"
"io"
"io/ioutil"
"log"
"os"
"sync"
)
var fEnableDebug = flag.Bool(
"fuse.debug",
false,
"Write FUSE debugging messages to stderr.")
var gDebugLogger *log.Logger
var gDebugLoggerOnce sync.Once
func initDebugDebugLogger() {
if !flag.Parsed() {
panic("initDebugDebugLogger called before flags available.")
}
var writer io.Writer = ioutil.Discard
if *fEnableDebug {
writer = os.Stderr
}
const flags = log.Ldate | log.Ltime | log.Lmicroseconds
gDebugLogger = log.New(writer, "", flags)
}
func getDebugLogger() *log.Logger {
gDebugLoggerOnce.Do(initDebugDebugLogger)
return gDebugLogger
}

View File

@ -49,7 +49,7 @@ type Op interface {
// Log information tied to this operation, with semantics equivalent to
// log.Printf, except that the format is different and logging is suppressed
// if --fuse.debug is not set.
// if no debug logger was set when mounting.
Logf(format string, v ...interface{})
}

View File

@ -15,20 +15,13 @@
package fuseutil
import (
"flag"
"io"
"math/rand"
"sync"
"time"
"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
)
var fRandomDelays = flag.Bool(
"fuseutil.random_delays", false,
"If set, randomly delay each op received, to help expose concurrency issues.")
// An interface with a method for each op type in the fuseops package. This can
// be used in conjunction with NewFileSystemServer to avoid writing a "dispatch
// loop" that switches on op types, instead receiving typed method calls
@ -115,13 +108,6 @@ func (s *fileSystemServer) ServeOps(c *fuse.Connection) {
func (s *fileSystemServer) handleOp(op fuseops.Op) {
defer s.opsInFlight.Done()
// Delay if requested.
if *fRandomDelays {
const delayLimit = 100 * time.Microsecond
delay := time.Duration(rand.Int63n(int64(delayLimit)))
time.Sleep(delay)
}
// Dispatch to the appropriate method.
var err error
switch typed := op.(type) {

View File

@ -84,6 +84,10 @@ type MountConfig struct {
// logging is performed.
ErrorLogger *log.Logger
// A logger to use for logging debug information. If nil, no debug logging is
// performed.
DebugLogger *log.Logger
// OS X only.
//
// Normally on OS X we mount with the novncache option
@ -189,7 +193,11 @@ func Mount(
dir string,
server Server,
config *MountConfig) (mfs *MountedFileSystem, err error) {
debugLogger := getDebugLogger()
// Arrange for a non-nil debug logger.
debugLogger := config.DebugLogger
if debugLogger == nil {
debugLogger = log.New(ioutil.Discard, "", 0)
}
// Initialize the struct.
mfs = &MountedFileSystem{

View File

@ -18,6 +18,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"time"
@ -59,7 +60,12 @@ type SampleTest struct {
//
// REQUIRES: t.Server has been set.
func (t *SampleTest) SetUp(ti *ogletest.TestInfo) {
err := t.initialize(ti.Ctx, t.Server, &t.MountConfig)
cfg := t.MountConfig
if *fDebug {
cfg.DebugLogger = log.New(os.Stderr, "fuse: ", 0)
}
err := t.initialize(ti.Ctx, t.Server, &cfg)
if err != nil {
panic(err)
}

View File

@ -40,6 +40,7 @@ var fFlushError = flag.Int("flushfs.flush_error", 0, "")
var fFsyncError = flag.Int("flushfs.fsync_error", 0, "")
var fReadOnly = flag.Bool("read_only", false, "Mount in read-only mode.")
var fDebug = flag.Bool("debug", false, "Enable debug logging.")
func makeFlushFS() (server fuse.Server, err error) {
// Check the flags.
@ -140,6 +141,10 @@ func main() {
ReadOnly: *fReadOnly,
}
if *fDebug {
cfg.DebugLogger = log.New(os.Stderr, "fuse: ", 0)
}
mfs, err := fuse.Mount(*fMountPoint, server, cfg)
if err != nil {
log.Fatalf("Mount: %v", err)

View File

@ -276,7 +276,7 @@ func (t *SubprocessTest) initialize(ctx context.Context) (err error) {
// Handle debug mode.
if *fDebug {
mountCmd.Stderr = os.Stderr
mountCmd.Args = append(mountCmd.Args, "--fuse.debug")
mountCmd.Args = append(mountCmd.Args, "--debug")
}
// Start the command.