diff --git a/pkg/pillar/agentlog/agentlog_test.go b/pkg/pillar/agentlog/agentlog_test.go index add04e2496..69878466ef 100644 --- a/pkg/pillar/agentlog/agentlog_test.go +++ b/pkg/pillar/agentlog/agentlog_test.go @@ -297,8 +297,8 @@ func emulateMemoryPressureStats() (cancel context.CancelFunc, err error) { } fakePSIFileName := fakePSIFileHandler.Name() - originalPressureMemoryFile := agentlog.PressureMemoryFile - agentlog.PressureMemoryFile = fakePSIFileName + originalPressureMemoryFile := agentlog.PressureMemoryFile() + agentlog.UpdatePressureMemoryFile(fakePSIFileName) // Start a ticker to write a new line to the file every 0.5 seconds ticker := time.NewTicker(500 * time.Millisecond) @@ -342,7 +342,7 @@ func emulateMemoryPressureStats() (cancel context.CancelFunc, err error) { agentlog.PsiMutex.Lock() fakePSIFileHandler.Close() os.Remove(fakePSIFileName) - agentlog.PressureMemoryFile = originalPressureMemoryFile + agentlog.UpdatePressureMemoryFile(originalPressureMemoryFile) agentlog.PsiMutex.Unlock() // We destroy this producer, so release the mutex psiProducerMutex.Unlock() @@ -371,8 +371,8 @@ func createFakePSIStatsFile() (cleanupFunc context.CancelFunc, err error) { } fakePSIFileName := fakePSIFileHandler.Name() - originalPressureMemoryFile := agentlog.PressureMemoryFile - agentlog.PressureMemoryFile = fakePSIFileName + originalPressureMemoryFile := agentlog.PressureMemoryFile() + agentlog.UpdatePressureMemoryFile(fakePSIFileName) // Write some content to the file agentlog.PsiMutex.Lock() @@ -389,7 +389,7 @@ func createFakePSIStatsFile() (cleanupFunc context.CancelFunc, err error) { agentlog.PsiMutex.Lock() fakePSIFileHandler.Close() os.Remove(fakePSIFileName) - agentlog.PressureMemoryFile = originalPressureMemoryFile + agentlog.UpdatePressureMemoryFile(originalPressureMemoryFile) agentlog.PsiMutex.Unlock() // We destroy this producer, so release the mutex psiProducerMutex.Unlock() diff --git a/pkg/pillar/agentlog/memprofile.go b/pkg/pillar/agentlog/memprofile.go index 168164a8b3..fade482c0d 100644 --- a/pkg/pillar/agentlog/memprofile.go +++ b/pkg/pillar/agentlog/memprofile.go @@ -51,9 +51,26 @@ const ( var ( // PressureMemoryFile is the memory pressure file. It is a variable, not a constant, to allow // changing it in tests to mock the file. - PressureMemoryFile = "/proc/pressure/memory" + pressureMemoryFile = "/proc/pressure/memory" + pressureMemoryFileLock sync.RWMutex ) +// PressureMemoryFile returns the path to the memory pressure file in /proc +func PressureMemoryFile() string { + pressureMemoryFileLock.RLock() + path := pressureMemoryFile + pressureMemoryFileLock.RUnlock() + + return path +} + +// UpdatePressureMemoryFile sets the path to the memory pressure file (mostly used for tests) +func UpdatePressureMemoryFile(newpath string) { + pressureMemoryFileLock.Lock() + pressureMemoryFile = newpath + pressureMemoryFileLock.Unlock() +} + // MemAllocationSite is the return value of GetMemProfile type MemAllocationSite struct { InUseBytes int64 @@ -134,7 +151,7 @@ func GetMemAllocationSites(reportZeroInUse bool) (int, []MemAllocationSite) { var PsiMutex sync.Mutex func isPSISupported() bool { - _, err := os.Stat(PressureMemoryFile) + _, err := os.Stat(PressureMemoryFile()) if err != nil { fmt.Println("PSI is not supported. Be sure to enable CONFIG_PSI=y in your kernel configuration.") return false @@ -148,7 +165,7 @@ func collectMemoryPSI() (*PressureStallInfo, error) { if !isPSISupported() { return nil, fmt.Errorf("PSI is not supported") } - procFile, err := os.Open(PressureMemoryFile) + procFile, err := os.Open(PressureMemoryFile()) if err != nil { return nil, fmt.Errorf("open: %v", err) }