Fix diff case
[lightning.git] / profile.go
1 // Copyright (C) The Lightning Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package lightning
6
7 import (
8         "os"
9         "runtime"
10         "runtime/pprof"
11         "time"
12
13         log "github.com/sirupsen/logrus"
14 )
15
16 func writeProfilesPeriodically(outdir string) {
17         for range time.NewTicker(time.Minute).C {
18                 writeMemProfile(outdir)
19                 writeCPUProfile(outdir)
20         }
21 }
22
23 func writeCPUProfile(outdir string) {
24         f, err := os.OpenFile(outdir+"/cpu.prof~", os.O_CREATE|os.O_WRONLY, 0666)
25         if err != nil {
26                 log.Print(err)
27                 return
28         }
29         defer f.Close()
30         runtime.GC()
31         if err := pprof.StartCPUProfile(f); err != nil {
32                 log.Print(err)
33                 return
34         }
35         time.Sleep(time.Second)
36         pprof.StopCPUProfile()
37         err = f.Close()
38         if err != nil {
39                 log.Print(err)
40                 return
41         }
42         err = os.Rename(outdir+"/cpu.prof~", outdir+"/cpu.prof")
43         if err != nil {
44                 log.Print(err)
45         }
46         return
47 }
48
49 func writeMemProfile(outdir string) {
50         f, err := os.OpenFile(outdir+"/mem.prof~", os.O_CREATE|os.O_WRONLY, 0666)
51         if err != nil {
52                 log.Print(err)
53                 return
54         }
55         defer f.Close()
56         runtime.GC()
57         if err := pprof.WriteHeapProfile(f); err != nil {
58                 log.Print(err)
59                 return
60         }
61         err = f.Close()
62         if err != nil {
63                 log.Print(err)
64                 return
65         }
66         err = os.Rename(outdir+"/mem.prof~", outdir+"/mem.prof")
67         if err != nil {
68                 log.Print(err)
69         }
70         return
71 }