Skip to content

Commit

Permalink
enable logging control
Browse files Browse the repository at this point in the history
  • Loading branch information
lezhnev74 committed Oct 26, 2024
1 parent 607d685 commit 139b843
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
14 changes: 8 additions & 6 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ func PrintMem(db *sql.DB) (rss uint64) {
if totalBlocks > 0 {
freeBlocksPct = int(float64(freeBlocks) / float64(totalBlocks) * 100)
}
log.Printf("DuckDB: fileSize:%s, walSize:%s, mem:%s/%s, Blcs[total/used/free/freePcs]:%d,%d,%d,%d%% ",
Out("DuckDB: fileSize:%s, walSize:%s, mem:%s/%s, Blcs[total/used/free/freePcs]:%d,%d,%d,%d%% ",
dbSize, walSize, memSize, memLimit, totalBlocks, usedBlocks, freeBlocks, freeBlocksPct)

var m runtime.MemStats
runtime.ReadMemStats(&m)

log.Printf(
Out(
"System: %s, %s",
fmt.Sprintf("RSS:%dMiB", m.Sys/1024/1024), // total virtual memory reserved from OS
fmt.Sprintf("HeapAlloc:%dMiB", m.HeapAlloc/1024/1024), // HeapAlloc is bytes of allocated heap objects.
Expand All @@ -124,8 +124,10 @@ func PrintMem(db *sql.DB) (rss uint64) {

var EnableLogging bool

func Log(message string) {
fmt.Println(message)
func Out(pattern string, args ...any) {
if EnableLogging {
log.Printf(pattern, args...)
}
}

//func CleanMem() {
Expand All @@ -136,9 +138,9 @@ func Log(message string) {
// ctx := context.Background()
// cmd := exec.CommandContext(ctx, "bash", "-c", `echo 3 > /proc/sys/vm/drop_caches`)
// out, err := cmd.CombinedOutput()
// log.Printf("cleanmem: %s", out)
// Out("cleanmem: %s", out)
// if err != nil {
// log.Printf("cleanmem error: %s", err)
// Out("cleanmem error: %s", err)
// return
// }
//}
3 changes: 2 additions & 1 deletion ingest/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ingest

import (
"golang.org/x/xerrors"
"heaplog_2024/common"
"heaplog_2024/db"
"log"
"path/filepath"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (d *Discover) DiscoverFiles() (news, obsoletes []string, err error) {
for i, _ := range obsoletes {
report += "- " + obsoletes[i] + "\n"
}
log.Printf(report)
common.Out(report)
}

return
Expand Down
5 changes: 2 additions & 3 deletions ingest/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"heaplog_2024/db"
"heaplog_2024/scanner"
"io"
"log"
"os"
"slices"
"time"
Expand Down Expand Up @@ -223,7 +222,7 @@ func (ing *Ingest) saveBatch(file string, messages <-chan *ScannedTokenizedMessa

// Report
if curSegmentMessages > 0 {
log.Printf("indexed %s[%d:%d]: %d messages, %d terms in %s", file, curSegment.Loc.From, curSegment.Loc.To, curSegmentMessages, len(segmentTerms), time.Now().Sub(t0).String())
common.Out("indexed %s[%d:%d]: %d messages, %d terms in %s", file, curSegment.Loc.From, curSegment.Loc.To, curSegmentMessages, len(segmentTerms), time.Now().Sub(t0).String())
}

// Cleanup
Expand Down Expand Up @@ -330,7 +329,7 @@ func (ing *Ingest) readMessagesInStream(name string, stream io.ReaderAt, message

messageLen := layout.To - layout.From
if messageLen > maxIndexableSize {
log.Printf("big message %dMiB at %s:%d", messageLen/1024/1024, name, layout.From)
common.Out("big message %dMiB at %s:%d", messageLen/1024/1024, name, layout.From)

// Index only the beginning and the end of the big message.
halfSize := maxIndexableSize / 2
Expand Down
3 changes: 2 additions & 1 deletion test_util/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"fmt"
"github.com/prometheus/procfs"
"heaplog_2024/common"
"log"
"os"
"path"
Expand Down Expand Up @@ -107,5 +108,5 @@ func ProcStat() {
if err != nil {
log.Fatalf("could not get process stat: %s", err)
}
log.Printf("RSS: %dMb\n", stat.ResidentMemory()/1024/1024)
common.Out("RSS: %dMb\n", stat.ResidentMemory()/1024/1024)
}
14 changes: 10 additions & 4 deletions ui/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"heaplog_2024/common"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -38,6 +39,7 @@ func overrideConfig(cfg Config, ctx *cli.Context) Config {
if ctx.Int("DuckdbMaxMemMb") != 0 {
cfg.DuckdbMaxMemMb = uint(ctx.Int("DuckdbMaxMemMb"))
}
cfg.EnableLogging = ctx.Bool("Verbose")

return cfg
}
Expand All @@ -47,6 +49,9 @@ func PrepareConsoleApp() (app *cli.App) {
prepareCfg := func(ctx *cli.Context) (Config, error) {
cfg, _ := LoadConfig(true)
cfg = overrideConfig(cfg, ctx)

common.EnableLogging = cfg.EnableLogging

return cfg, cfg.Validate()
}

Expand Down Expand Up @@ -92,8 +97,9 @@ func PrepareConsoleApp() (app *cli.App) {
Usage: "Max memory the duckdb instance is allowed to allocate (Mb)",
},
&cli.BoolFlag{
Name: "Verbose",
Aliases: []string{"v"},
Usage: "Show extra details about what the service does",
Usage: "Show extra details about what the service is doing",
},
}

Expand Down Expand Up @@ -122,18 +128,18 @@ func PrepareConsoleApp() (app *cli.App) {
httpApp := makeHttpApp(happ, "")

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
<-sigs
cancel() // stop the program
log.Printf("Stopping heaplog...")

t := time.Second * 10 // the same as the docker's timeout for "docker stop"
t := time.Second * 3
time.Sleep(t)

err := httpApp.Shutdown()
if err != nil {
log.Printf("%s", err)
common.Out("%s", err)
}
}()

Expand Down

0 comments on commit 139b843

Please sign in to comment.