Skip to content

Commit

Permalink
Implement standard 1GB RAM limit (aborts if over threshold, experimen…
Browse files Browse the repository at this point in the history
…tal!)
  • Loading branch information
lkarlslund committed Jun 26, 2024
1 parent 3856a0f commit aa30caf
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
cpuprofilelength := pflag.Int("cpuprofilelength", 0, "Stop profiling after N seconds, 0 to profile until program terminates")
transferstatsinterval := pflag.Int("statsinterval", 5, "Show transfer stats every N seconds, 0 to disable")
queuestatsinterval := pflag.Int("queueinterval", 30, "Show internal queue sizes every N seconds, 0 to disable")
ramlimit := pflag.Int("ramlmit", 1*1024*1024*1024, "Abort if process uses more than this amount bytes of RAM")

pflag.Parse()

Expand Down Expand Up @@ -257,6 +258,20 @@ func main() {
inodecache, directorycache, files, stack := c.Stats()
logger.Warn().Msgf("Inode cache %v, directory cache %v, file queue %v, directory queue %v",
inodecache, directorycache, files, stack)

if *ramlimit > 0 {
runtime.GC()
var m runtime.MemStats
runtime.ReadMemStats(&m)
if m.Alloc > *ramlimit {

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)

Check failure on line 266 in main.go

View workflow job for this annotation

GitHub Actions / build

invalid operation: m.Alloc > *ramlimit (mismatched types uint64 and int)
// Using more than 4GB, wooot
// Write debug memory info to file
mp, _ := os.Create("/tmp/memprofile.pprof")
pprof.WriteHeapProfile(mp)
mp.Close()
logger.Fatal().Msgf("Aborting due to absurd RAM consumption")
}
}
}
}()
}
Expand Down

0 comments on commit aa30caf

Please sign in to comment.