Commit 572491fe authored by Aaron Jacobs's avatar Aaron Jacobs

commonOp.maybeTraceByPID

parent 2783a716
...@@ -16,6 +16,7 @@ package fuseops ...@@ -16,6 +16,7 @@ package fuseops
import ( import (
"flag" "flag"
"fmt"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
...@@ -60,15 +61,44 @@ func describeOpType(t reflect.Type) (desc string) { ...@@ -60,15 +61,44 @@ func describeOpType(t reflect.Type) (desc string) {
return return
} }
func (o *commonOp) maybeTraceByPID(in context.Context) (out context.Context) { var gPIDMapMu sync.Mutex
// A map from PID to a traced context for that PID.
//
// GUARDED_BY(gPIDMapMu)
var gPIDMap = make(map[int]context.Context)
func reportWhenPIDGone(
pid int,
ctx context.Context,
report reqtrace.ReportFunc)
func (o *commonOp) maybeTraceByPID(
in context.Context,
pid int) (out context.Context) {
// Is there anything to do? // Is there anything to do?
if !*fTraceByPID { if !reqtrace.Enabled() || !*fTraceByPID {
out = in out = in
return return
} }
// TODO(jacobsa): Do something interesting. gPIDMapMu.Lock()
out = in defer gPIDMapMu.Unlock()
// Do we already have a traced context for this PID?
if existing, ok := gPIDMap[pid]; ok {
out = existing
return
}
// Set up a new one and stick it in the map.
var report reqtrace.ReportFunc
out, report = reqtrace.Trace(in, fmt.Sprintf("PID %v", pid))
gPIDMap[pid] = out
// Ensure we close the trace and remove it from the map eventually.
go reportWhenPIDGone(pid, out, report)
return return
} }
...@@ -79,7 +109,7 @@ func (o *commonOp) init( ...@@ -79,7 +109,7 @@ func (o *commonOp) init(
log func(int, string, ...interface{}), log func(int, string, ...interface{}),
opsInFlight *sync.WaitGroup) { opsInFlight *sync.WaitGroup) {
// Set up a context that reflects per-PID tracing if appropriate. // Set up a context that reflects per-PID tracing if appropriate.
ctx = o.maybeTraceByPID(ctx) ctx = o.maybeTraceByPID(ctx, int(r.Hdr().Pid))
// Initialize basic fields. // Initialize basic fields.
o.opType = describeOpType(opType) o.opType = describeOpType(opType)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment