-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests: measure state lock #14874
base: master
Are you sure you want to change the base?
tests: measure state lock #14874
Changes from 4 commits
6b28c3a
6b3d5b8
f46e537
30de5db
42430f7
af6f416
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,83 @@ | ||||||||||||||||||||||||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||||||||||||||||||||||||
//go:build statelock | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
/* | ||||||||||||||||||||||||
* 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 osutil | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||
"os" | ||||||||||||||||||||||||
"runtime" | ||||||||||||||||||||||||
"time" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func traceCallers(description string) { | ||||||||||||||||||||||||
lockfilePath := os.Getenv("SNAPD_STATE_LOCK_FILE") | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd avoid using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||||||||||||||||||||||||
if lockfilePath == "" { | ||||||||||||||||||||||||
fmt.Fprintf(os.Stderr, "could not retrieve log file, SNAPD_STATE_LOCK_FILE env var required") | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
lockfile, err := os.OpenFile(lockfilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could flock the file after opening it, so that we serialize the writes but the state lock could be unlocked earlier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
fmt.Fprintf(os.Stderr, "could not open/create log traces file: %v", err) | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
defer lockfile.Close() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
pc := make([]uintptr, 10) | ||||||||||||||||||||||||
n := runtime.Callers(0, pc) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. skip should likely be set to 2, to skip There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure |
||||||||||||||||||||||||
formattedLine := fmt.Sprintf("##%s\n", description) | ||||||||||||||||||||||||
if _, err = lockfile.WriteString(formattedLine); err != nil { | ||||||||||||||||||||||||
fmt.Fprintf(os.Stderr, "internal error: could not write trace callers header to tmp file: %v", err) | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
for i := 0; i < n; i++ { | ||||||||||||||||||||||||
f := runtime.FuncForPC(pc[i]) | ||||||||||||||||||||||||
file, line := f.FileLine(pc[i]) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. per docs of https://pkg.go.dev/runtime?utm_source=godoc#Callers, this should use runtime.CallersFrames() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||||||||||||||||||||||||
formattedLine = fmt.Sprintf("%s:%d %s\n", file, line, f.Name()) | ||||||||||||||||||||||||
if _, err = lockfile.WriteString(formattedLine); err != nil { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we could maybe use |
||||||||||||||||||||||||
fmt.Fprintf(os.Stderr, "internal error: could not write trace callers to tmp file: %v", err) | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func GetLockStart() int64 { | ||||||||||||||||||||||||
return time.Now().UnixNano() / int64(time.Millisecond) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// MaybeSaveLockTime allows to save lock times when this overpass the threshold | ||||||||||||||||||||||||
// defined by through the SNAPD_STATE_LOCK_THRESHOLD_MS environment settings. | ||||||||||||||||||||||||
func MaybeSaveLockTime(lockStart int64) { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||||||||||||||
lockEnd := time.Now().UnixNano() / int64(time.Millisecond) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can prob be done after both getenvs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. update |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
if !GetenvBool("SNAPPY_TESTING") { | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
threshold := GetenvInt64("SNAPD_STATE_LOCK_THRESHOLD_MS") | ||||||||||||||||||||||||
if threshold <= 0 { | ||||||||||||||||||||||||
return | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "SNAPD_STATE_LOCK_FILE" could be tested here |
||||||||||||||||||||||||
elapsedMilliseconds := lockEnd - lockStart | ||||||||||||||||||||||||
if elapsedMilliseconds > threshold { | ||||||||||||||||||||||||
formattedLine := fmt.Sprintf("Elapsed Time: %d milliseconds", elapsedMilliseconds) | ||||||||||||||||||||||||
traceCallers(formattedLine) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
//go:build !statelock | ||
|
||
/* | ||
* 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 osutil | ||
|
||
func GetLockStart() int64 { | ||
return int64(0) | ||
} | ||
|
||
func MaybeSaveLockTime(lockStart int64) { | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,6 +32,7 @@ import ( | |||||||||||||||||
"time" | ||||||||||||||||||
|
||||||||||||||||||
"github.com/snapcore/snapd/logger" | ||||||||||||||||||
"github.com/snapcore/snapd/osutil" | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
// A Backend is used by State to checkpoint on every unlock operation | ||||||||||||||||||
|
@@ -111,6 +112,8 @@ type State struct { | |||||||||||||||||
// task/changes observing | ||||||||||||||||||
taskHandlers map[int]func(t *Task, old, new Status) (remove bool) | ||||||||||||||||||
changeHandlers map[int]func(chg *Change, old, new Status) | ||||||||||||||||||
|
||||||||||||||||||
lockStart int64 | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// New returns a new empty state. | ||||||||||||||||||
|
@@ -141,6 +144,7 @@ func (s *State) Modified() bool { | |||||||||||||||||
func (s *State) Lock() { | ||||||||||||||||||
s.mu.Lock() | ||||||||||||||||||
atomic.AddInt32(&s.muC, 1) | ||||||||||||||||||
s.lockStart = osutil.GetLockStart() | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
func (s *State) reading() { | ||||||||||||||||||
|
@@ -159,6 +163,7 @@ func (s *State) writing() { | |||||||||||||||||
func (s *State) unlock() { | ||||||||||||||||||
atomic.AddInt32(&s.muC, -1) | ||||||||||||||||||
s.mu.Unlock() | ||||||||||||||||||
osutil.MaybeSaveLockTime(s.lockStart) | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this only needs to happen with the lock taken I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may be ok to do with the state lock released if we flock the log file. With the state lock held, there would likely be no contention for flock on the log file. OTOH with the lock released, we'd rely on flock to serialize writes, but this would not keep the state lock unnecessarily held. FWIW, unlocking may reschedule, so assuming we use flock for serializing writes, the timing may end up being incorrect given that the time is captured inside MaybeSaveLockTime(). The end time will likely need to be captured like so:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note, I missed that s.lockStart is used after releasing the lock, it should ofc be read into a variable before the lock is released There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. udpated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
type marshalledState struct { | ||||||||||||||||||
|
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and then you can update MaybeSave to do:
and change
fmt.Fprintf()
to usereturn fmt.Errorf("cannot this or that: %w", err)