diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 6997f1d85..7b7042385 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -704,6 +704,7 @@ func statsFor(days int) sessions.Stats { func statsForGen(days int) (sessions.Stats, uint64) { days = max(0, days) + keepStatsWhileAsked() statsMemo.Lock() if k := statsMemo.m[days]; k != nil { statsMemo.asks++ @@ -764,6 +765,62 @@ func statsForGen(days int) (sessions.Stats, uint64) { return st, gen } +// statsIdle is how long the Sessions page's stats are kept after the last +// ask. The page polls every 15 s while it is open; once it has been shut +// for this long, every range's Stats (a Summary for each session ever: +// ~15 MB of a 14k-session history), the overviews and the answers' tags +// go. The parsed session files stay, so the next ask sums them up again +// (~0.1 s a range) without reading a file. +const statsIdle = 2 * time.Minute + +var statsIdleTimer struct { + sync.Mutex + t *time.Timer + // off: tests, which ask for stats over minutes, have it armed only + // when they ask for it + off bool +} + +func init() { statsIdleTimer.off = testing.Testing() } + +// keepStatsWhileAsked puts off forgetIdleStats for statsIdle. +func keepStatsWhileAsked() { + statsIdleTimer.Lock() + defer statsIdleTimer.Unlock() + if statsIdleTimer.off { + return + } + if statsIdleTimer.t != nil { + statsIdleTimer.t.Reset(statsIdle) + return + } + statsIdleTimer.t = time.AfterFunc(statsIdle, forgetIdleStats) +} + +// forgetIdleStats lets go of what statsForGen and writeOverview kept, and +// hands the pages back to the system at once; a range being read again +// puts it off. +func forgetIdleStats() { + statsMemo.Lock() + for _, k := range statsMemo.m { + if k.busy { + statsMemo.Unlock() + keepStatsWhileAsked() + return + } + } + statsMemo.m = nil + statsMemo.Unlock() + overviews.Lock() + overviews.m = nil + overviews.Unlock() + statsTags.Lock() + statsTags.m = nil + statsTags.Unlock() + sessions.ForgetKeptRanges() + debug.FreeOSMemory() +} + func combinedSessionStats(days int) sessions.Stats { return combinedSessionStatsAll([]int{days})[0] } diff --git a/internal/gui/sessions_stats_idle_test.go b/internal/gui/sessions_stats_idle_test.go new file mode 100644 index 000000000..7a98b3dd5 --- /dev/null +++ b/internal/gui/sessions_stats_idle_test.go @@ -0,0 +1,90 @@ +package gui + +import ( + "net/http/httptest" + "testing" +) + +// Once the Sessions page has gone unasked for statsIdle, every range's +// Stats, the overviews and the answers' tags are let go: a long history's +// were ~15 MB held for good by a window that showed another page, or none. +// The next ask is answered as before, from a new read. +func TestIdleStatsLetGo(t *testing.T) { + forgetStats() + t.Cleanup(forgetStats) + statsIdleTimer.Lock() + statsIdleTimer.off = false + statsIdleTimer.Unlock() + t.Cleanup(func() { + statsIdleTimer.Lock() + if statsIdleTimer.t != nil { + statsIdleTimer.t.Stop() + statsIdleTimer.t = nil + } + statsIdleTimer.off = true + statsIdleTimer.Unlock() + }) + + before, gen := statsForGen(0) + statsFor(30) + writeOverview(httptest.NewRecorder(), 0, "", "", "") + overviews.Lock() + had := len(overviews.m) + overviews.Unlock() + if had == 0 { + t.Fatal("the overview asked for was not kept") + } + statsIdleTimer.Lock() + armed := statsIdleTimer.t != nil + statsIdleTimer.Unlock() + if !armed { + t.Fatal("an ask for stats armed no idle timer") + } + + forgetIdleStats() + statsMemo.Lock() + n := len(statsMemo.m) + statsMemo.Unlock() + overviews.Lock() + o := len(overviews.m) + overviews.Unlock() + statsTags.Lock() + tags := len(statsTags.m) + statsTags.Unlock() + if n != 0 || o != 0 || tags != 0 { + t.Fatalf("after the idle timer: %d ranges, %d overviews, %d tags kept; want none", n, o, tags) + } + + after, gen2 := statsForGen(0) + if gen2 == gen { + t.Fatal("the range asked after the idle timer was answered from what it let go") + } + if !sameStats(before, after) { + t.Fatal("the range read again differs from the one let go") + } +} + +// A range being read again behind the page is not let go under it: the +// read would put back a range the memo no longer has. +func TestIdleStatsWaitForABusyRead(t *testing.T) { + forgetStats() + t.Cleanup(forgetStats) + statsFor(0) + statsMemo.Lock() + statsMemo.m[0].busy = true + statsMemo.Unlock() + t.Cleanup(func() { + statsMemo.Lock() + if k := statsMemo.m[0]; k != nil { + k.busy = false + } + statsMemo.Unlock() + }) + forgetIdleStats() + statsMemo.Lock() + kept := statsMemo.m[0] != nil + statsMemo.Unlock() + if !kept { + t.Fatal("a range being read again was let go") + } +} diff --git a/internal/sessions/stats_kept.go b/internal/sessions/stats_kept.go index 39336e209..3d68d574f 100644 --- a/internal/sessions/stats_kept.go +++ b/internal/sessions/stats_kept.go @@ -204,3 +204,13 @@ func forgetKeptStats() { keptStats.files, keptStats.ranges, keptStats.native = nil, nil, nil keptStats.prices, keptStats.titles, keptStats.desktop = nil, titleAnswers{}, nil } + +// ForgetKeptRanges lets go of the ranges' Stats kept for the next read, +// and what they were checked against, once no page has asked for a while. +// The parsed files stay: a page that asks again sums them up again +// (~0.1 s for a long history) and reads no file. +func ForgetKeptRanges() { + mu.Lock() + defer mu.Unlock() + forgetKeptStats() +}