-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes required to collect the state locks for the whole run
- Loading branch information
1 parent
2a6abcc
commit 4186d11
Showing
10 changed files
with
174 additions
and
38 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
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 |