fix: Set globalMaxCacheSize to allowable value. (#3816)

If memory resource limit and total RAM are more than 8GiB, either 50%
of memory resource limit or total RAM is set to globalMaxCacheSize.
This commit is contained in:
Bala FA 2017-03-01 06:21:52 +05:30 committed by Harshavardhana
parent fcad4a44fd
commit 097cec676a
3 changed files with 52 additions and 26 deletions

View File

@ -59,25 +59,35 @@ func setMaxMemory() error {
if err != nil { if err != nil {
return err return err
} }
err = syscall.Getrlimit(syscall.RLIMIT_AS, &rLimit) err = syscall.Getrlimit(syscall.RLIMIT_AS, &rLimit)
if err != nil { if err != nil {
return err return err
} }
// Validate if rlimit memory is set to lower // If current rlimit is less than minRAMSize, do not set globalMaxCacheSize.
// than max cache size. Then we should use such value. if rLimit.Cur < minRAMSize {
if uint64(rLimit.Cur) < globalMaxCacheSize { return nil
globalMaxCacheSize = uint64(float64(50*rLimit.Cur) / 100)
} }
// Make sure globalMaxCacheSize is less than RAM size. // Get total RAM.
stats, err := sys.GetStats() stats, err := sys.GetStats()
if err != nil && err != sys.ErrNotImplemented { if err != nil {
// Ignore sys.ErrNotImplemented error.
if err == sys.ErrNotImplemented {
err = nil
}
return err return err
} }
// If TotalRAM is >= minRAMSize we proceed to enable cache.
// cache is always 50% of the totalRAM. // Set 50% of current rlimit or total RAM to globalMaxCacheSize.
if err == nil && stats.TotalRAM >= minRAMSize { if stats.TotalRAM >= minRAMSize {
globalMaxCacheSize = uint64(float64(50*stats.TotalRAM) / 100) if rLimit.Cur < stats.TotalRAM {
globalMaxCacheSize = rLimit.Cur / 2
} else {
globalMaxCacheSize = stats.TotalRAM / 2
} }
}
return nil return nil
} }

View File

@ -59,25 +59,35 @@ func setMaxMemory() error {
if err != nil { if err != nil {
return err return err
} }
err = syscall.Getrlimit(syscall.RLIMIT_DATA, &rLimit) err = syscall.Getrlimit(syscall.RLIMIT_DATA, &rLimit)
if err != nil { if err != nil {
return err return err
} }
// Validate if rlimit memory is set to lower // If current rlimit is less than minRAMSize, do not set globalMaxCacheSize.
// than max cache size. Then we should use such value. if rLimit.Cur < minRAMSize {
if uint64(rLimit.Cur) < globalMaxCacheSize { return nil
globalMaxCacheSize = uint64(float64(50*rLimit.Cur) / 100)
} }
// Make sure globalMaxCacheSize is less than RAM size. // Get total RAM.
stats, err := sys.GetStats() stats, err := sys.GetStats()
if err != nil && err != sys.ErrNotImplemented { if err != nil {
// Ignore sys.ErrNotImplemented error.
if err == sys.ErrNotImplemented {
err = nil
}
return err return err
} }
// If TotalRAM is >= minRAMSize we proceed to enable cache.
// cache is always 50% of the totalRAM. // Set 50% of current rlimit or total RAM to globalMaxCacheSize.
if err == nil && stats.TotalRAM >= minRAMSize { if stats.TotalRAM >= minRAMSize {
globalMaxCacheSize = uint64(float64(50*stats.TotalRAM) / 100) if rLimit.Cur < stats.TotalRAM {
globalMaxCacheSize = rLimit.Cur / 2
} else {
globalMaxCacheSize = stats.TotalRAM / 2
} }
}
return nil return nil
} }

View File

@ -28,15 +28,21 @@ func setMaxOpenFiles() error {
} }
func setMaxMemory() error { func setMaxMemory() error {
// Make sure globalMaxCacheSize is less than RAM size. // Get total RAM.
stats, err := sys.GetStats() stats, err := sys.GetStats()
if err != nil && err != sys.ErrNotImplemented { if err != nil {
// Ignore sys.ErrNotImplemented error.
if err == sys.ErrNotImplemented {
err = nil
}
return err return err
} }
// If TotalRAM is <= minRAMSize we proceed to enable cache.
// cache is always 50% of the totalRAM. // Set 50% of total RAM to globalMaxCacheSize.
if err == nil && stats.TotalRAM >= minRAMSize { if stats.TotalRAM >= minRAMSize {
globalMaxCacheSize = uint64(float64(50*stats.TotalRAM) / 100) globalMaxCacheSize = stats.TotalRAM / 2
} }
return nil return nil
} }