2015-04-22 15:54:31 -07:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-04-22 15:54:31 -07:00
|
|
|
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-02-02 03:45:17 -07:00
|
|
|
deadlock "github.com/sasha-s/go-deadlock"
|
2016-11-04 19:31:52 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/logger"
|
2015-04-22 15:54:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-12-17 07:37:11 -07:00
|
|
|
threshold = 100 * time.Millisecond
|
2015-10-03 08:25:21 -07:00
|
|
|
l = logger.DefaultLogger.NewFacility("sync", "Mutexes")
|
|
|
|
|
|
|
|
// We make an exception in this package and have an actual "if debug { ...
|
|
|
|
// }" variable, as it may be rather performance critical and does
|
|
|
|
// nonstandard things (from a debug logging PoV).
|
2016-11-04 19:24:53 -07:00
|
|
|
debug = strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all"
|
2018-01-15 06:33:52 -07:00
|
|
|
useDeadlock = false
|
2015-04-22 15:54:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.SetDebug("sync", strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all")
|
|
|
|
|
2018-01-15 06:33:52 -07:00
|
|
|
if n, _ := strconv.Atoi(os.Getenv("STLOCKTHRESHOLD")); n > 0 {
|
2016-11-04 19:31:52 -07:00
|
|
|
threshold = time.Duration(n) * time.Millisecond
|
|
|
|
}
|
2018-01-15 06:33:52 -07:00
|
|
|
l.Debugf("Enabling lock logging at %v threshold", threshold)
|
|
|
|
|
|
|
|
if n, _ := strconv.Atoi(os.Getenv("STDEADLOCKTIMEOUT")); n > 0 {
|
2016-11-04 19:31:52 -07:00
|
|
|
deadlock.Opts.DeadlockTimeout = time.Duration(n) * time.Second
|
2018-01-15 06:33:52 -07:00
|
|
|
l.Debugf("Enabling lock deadlocking at %v", deadlock.Opts.DeadlockTimeout)
|
|
|
|
useDeadlock = true
|
2015-04-22 15:54:31 -07:00
|
|
|
}
|
2015-10-03 08:25:21 -07:00
|
|
|
}
|