-
Notifications
You must be signed in to change notification settings - Fork 587
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
Open
sergiocazzolato
wants to merge
3
commits into
canonical:master
Choose a base branch
from
sergiocazzolato:tests-measure-state-lock
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
tests: measure state lock #14874
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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") | ||
if lockfilePath == "" { | ||
fmt.Fprintf(os.Stderr, "could not retrieve log file, SNAPD_STATE_LOCK_FILE env var required") | ||
} | ||
|
||
lockfile, err := os.OpenFile(lockfilePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||
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) | ||
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]) | ||
formattedLine = fmt.Sprintf("%s:%d %s\n", file, line, f.Name()) | ||
if _, err = lockfile.WriteString(formattedLine); err != nil { | ||
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) { | ||
lockEnd := time.Now().UnixNano() / int64(time.Millisecond) | ||
|
||
if !GetenvBool("SNAPPY_TESTING") { | ||
return | ||
} | ||
threshold := GetenvInt64("SNAPD_STATE_LOCK_THRESHOLD_MS") | ||
if threshold <= 0 { | ||
return | ||
} | ||
|
||
elapsedMilliseconds := lockEnd - lockStart | ||
if elapsedMilliseconds > threshold { | ||
formattedLine := fmt.Sprintf("Elapsed Time: %d milliseconds", elapsedMilliseconds) | ||
traceCallers(formattedLine) | ||
} | ||
} |
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 !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) { | ||
} |
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
shouldn't this be done with the lock taken? otherwise just the tentative themselves to lock will race/dirty lockStart