@@ -64,19 +64,63 @@ type oomReporter struct{}
6464var _ oomkiller.OOMReporter = (* oomReporter )(nil )
6565
6666func (r * oomReporter ) WriteReport (memoryUsage uint64 ) error {
67- now := time .Now ().UTC ()
67+ draftPath := filepath .Join (sWorkingPath , "oom_draft" )
68+ draftInfo , err := os .Stat (draftPath )
69+ if err != nil {
70+ if ! os .IsNotExist (err ) {
71+ return err
72+ }
73+ draftInfo = nil
74+ }
6875 reportsDir := filepath .Join (sWorkingPath , "oom_reports" )
69- err : = os .MkdirAll (reportsDir , 0o777 )
76+ err = os .MkdirAll (reportsDir , 0o777 )
7077 if err != nil {
7178 return err
7279 }
7380 chownReport (reportsDir )
7481
75- destPath , err := nextAvailableReportPath (reportsDir , now )
82+ destPath , err := nextAvailableReportPath (reportsDir , time . Now (). UTC () )
7683 if err != nil {
7784 return err
7885 }
79- err = os .MkdirAll (destPath , 0o777 )
86+ err = r .writeSnapshot (destPath , memoryUsage )
87+ if err != nil {
88+ return err
89+ }
90+ return discardDraftIfCurrent (draftPath , draftInfo )
91+ }
92+
93+ func (r * oomReporter ) WriteDraft (memoryUsage uint64 ) error {
94+ draftPath := filepath .Join (sWorkingPath , "oom_draft" )
95+ os .RemoveAll (draftPath )
96+ return r .writeSnapshot (draftPath , memoryUsage )
97+ }
98+
99+ func (r * oomReporter ) DiscardDraft () error {
100+ draftPath := filepath .Join (sWorkingPath , "oom_draft" )
101+ return os .RemoveAll (draftPath )
102+ }
103+
104+ func discardDraftIfCurrent (draftPath string , draftInfo os.FileInfo ) error {
105+ if draftInfo == nil {
106+ return nil
107+ }
108+ currentInfo , err := os .Stat (draftPath )
109+ if err != nil {
110+ if os .IsNotExist (err ) {
111+ return nil
112+ }
113+ return err
114+ }
115+ if ! os .SameFile (draftInfo , currentInfo ) {
116+ return nil
117+ }
118+ return os .RemoveAll (draftPath )
119+ }
120+
121+ func (r * oomReporter ) writeSnapshot (destPath string , memoryUsage uint64 ) error {
122+ now := time .Now ().UTC ()
123+ err := os .MkdirAll (destPath , 0o777 )
80124 if err != nil {
81125 return err
82126 }
@@ -139,3 +183,36 @@ func writeOOMProfile(destPath string, name string) {
139183 }
140184 chownReport (filePath )
141185}
186+
187+ func promoteOOMDraftAt (workingPath string ) {
188+ draftPath := filepath .Join (workingPath , "oom_draft" )
189+ info , err := os .Stat (draftPath )
190+ if err != nil || ! info .IsDir () {
191+ return
192+ }
193+ reportsDir := filepath .Join (workingPath , "oom_reports" )
194+ initReportDir (reportsDir )
195+ destPath , err := nextAvailableReportPath (reportsDir , info .ModTime ().UTC ())
196+ if err != nil {
197+ os .RemoveAll (draftPath )
198+ return
199+ }
200+ err = os .Rename (draftPath , destPath )
201+ if err != nil {
202+ os .RemoveAll (draftPath )
203+ return
204+ }
205+ chownReport (destPath )
206+ }
207+
208+ func promoteOOMDraft () {
209+ promoteOOMDraftAt (sWorkingPath )
210+ }
211+
212+ func PromoteOOMDraft () {
213+ promoteOOMDraft ()
214+ }
215+
216+ func PromoteOOMDraftAt (workingPath string ) {
217+ promoteOOMDraftAt (workingPath )
218+ }
0 commit comments