forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
overlord/state, tests: measure state lock (canonical#14874)
* spike to measure locking time os snapd state file * Changes required to collect the state locks for the whole run * fix shellcheck error * measure the lock time after the lock is done * Save unlock times before the unlock This will prevent raises. * Added wait time and several improvements * update unit test a minor fix on comments * fix flock unit test * osutil: tweak env vars for state lock tracing, improve tracing Signed-off-by: Maciej Borzecki <[email protected]> * tests: update for new env varibles names Signed-off-by: Maciej Borzecki <[email protected]> * osutil: simplify init, tweak naming and lock tracking API Signed-off-by: Maciej Borzecki <[email protected]> * overlord/state: tweak naming and match lock tracing API Signed-off-by: Maciej Borzecki <[email protected]> * overlord/state: move state lock tracing, unexport functions, change build tag Move state lock tracing from osutil into overlord/state. Tweak the code to not export functions since there aren't any external consumers. Signed-off-by: Maciej Borzecki <[email protected]> * many: update state lock trace build tag use locations Signed-off-by: Maciej Borzecki <[email protected]> * overlord/state: take timestamp before unlocking Signed-off-by: Maciej Borzecki <[email protected]> * github: do not collect coverage for statelocktrace tag The code is already executed in spread tests. Signed-off-by: Maciej Borzecki <[email protected]> --------- Signed-off-by: Maciej Borzecki <[email protected]> Co-authored-by: Maciej Borzecki <[email protected]>
- Loading branch information
1 parent
eb821e9
commit 953a5d8
Showing
12 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
//go:build statelocktrace | ||
|
||
/* | ||
* Copyright (C) 2021 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* 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 state | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/snapcore/snapd/osutil" | ||
) | ||
|
||
var ( | ||
traceStateLock = false | ||
|
||
traceThreshold = int64(0) | ||
traceFilePath = "" | ||
) | ||
|
||
func init() { | ||
if !osutil.GetenvBool("SNAPPY_TESTING") { | ||
return | ||
} | ||
|
||
threshold := osutil.GetenvInt64("SNAPD_STATE_LOCK_TRACE_THRESHOLD_MS") | ||
logFilePath := os.Getenv("SNAPD_STATE_LOCK_TRACE_FILE") | ||
|
||
if threshold <= 0 || logFilePath == "" { | ||
return | ||
} | ||
|
||
traceThreshold = threshold | ||
traceFilePath = logFilePath | ||
traceStateLock = true | ||
} | ||
|
||
func traceCallers(ts, heldMs, waitMs int64) error { | ||
if traceFilePath == "" { | ||
return fmt.Errorf("internal error: trace file path unset") | ||
} | ||
|
||
logFile, err := os.OpenFile(traceFilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||
if err != nil { | ||
return fmt.Errorf("cannot not open/create log trace file: %v", err) | ||
} | ||
lockFile := osutil.NewFileLockWithFile(logFile) | ||
defer lockFile.Close() | ||
|
||
if err := lockFile.Lock(); err != nil { | ||
return fmt.Errorf("cannot take file lock: %v", err) | ||
} | ||
|
||
pc := make([]uintptr, 10) | ||
// avoid 3 first callers on the stack: runtime.Callers(), this function and the parent | ||
n := runtime.Callers(3, pc) | ||
frames := runtime.CallersFrames(pc[:n]) | ||
|
||
_, err = fmt.Fprintf(logFile, "### %s lock: held: %d ms wait %d ms\n", | ||
time.UnixMilli(ts), | ||
heldMs, waitMs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for { | ||
frame, more := frames.Next() | ||
_, err := fmt.Fprintf(logFile, "%s:%d %s\n", frame.File, frame.Line, frame.Function) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !more { | ||
break | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func lockTimestamp() int64 { | ||
if !traceStateLock { | ||
return 0 | ||
} | ||
|
||
return time.Now().UnixMilli() | ||
} | ||
|
||
// maybeSaveLockTime allows to save lock times when this overpass the threshold | ||
// defined by through the SNAPD_STATE_LOCK_THRESHOLD_MS environment settings. | ||
func maybeSaveLockTime(lockWaitStart, lockHoldStart, now int64) { | ||
if !traceStateLock { | ||
return | ||
} | ||
|
||
heldMs := now - lockHoldStart | ||
waitMs := lockHoldStart - lockWaitStart | ||
if heldMs > traceThreshold || waitMs > traceThreshold { | ||
if err := traceCallers(now, heldMs, waitMs); err != nil { | ||
fmt.Fprintf(os.Stderr, "could write state lock trace: %v\n", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
//go:build !statelocktrace | ||
|
||
/* | ||
* Copyright (C) 2021 Canonical Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* 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 state | ||
|
||
func lockTimestamp() int64 { | ||
return int64(0) | ||
} | ||
|
||
func maybeSaveLockTime(lockWaitStart, lockHoldStart, now int64) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
summary: smoke test used to retrieve the lock state times | ||
|
||
details: | | ||
Test used to collect artifacts | ||
priority: -1 | ||
|
||
artifacts: | ||
- snapd_lock_traces | ||
|
||
execute: | | ||
if [ -f "$TESTSTMP"/snapd_lock_traces ]; then | ||
cp -f "$TESTSTMP"/snapd_lock_traces . | ||
else | ||
touch snapd_lock_traces | ||
fi |