1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318:
<?php
/**
* @author: stev leibelt <artodeto@bazzline.net>
* @since: 2014-07-27
*/
namespace Net\Bazzline\Component\MemoryLimitManager;
/**
* Class MemoryLimitManager
* @package Net\Bazzline\Component\Fork
*/
class MemoryLimitManager
{
/**
* @var int
*/
private $bufferInBytes;
/**
* @var int
*/
private $limitFromIniInBytes;
/**
* @var int
*/
private $limitInBytes;
public function __construct()
{
$currentMemoryLimitFromIni = trim(ini_get('memory_limit'));
if (((int) $currentMemoryLimitFromIni) > 0) {
$unitIdentifier = strtolower($currentMemoryLimitFromIni[strlen($currentMemoryLimitFromIni)-1]);
$value = (int) substr($currentMemoryLimitFromIni, 0, -1);
switch ($unitIdentifier) {
case 'g':
$this->limitFromIniInBytes = ($this->gigaBytesInBytes($value));
break;
case 'm':
$this->limitFromIniInBytes = ($this->megaBytesInBytes($value));
break;
case 'k':
$this->limitFromIniInBytes = ($this->kiloBytesInBytes($value));
break;
default:
$this->limitFromIniInBytes = $value;
}
} else {
$this->limitFromIniInBytes = 0;
}
$this->limitInBytes = $this->limitFromIniInBytes;
}
/**
* @param int $bytes
*/
public function setBufferInBytes($bytes)
{
$this->bufferInBytes = (int) $bytes;
}
/**
* @param int $kiloBytes
*/
public function setBufferInKiloBytes($kiloBytes)
{
$this->setBufferInBytes($this->kiloBytesInBytes($kiloBytes));
}
/**
* @param int $megaBytes
*/
public function setBufferInMegaBytes($megaBytes)
{
$this->setBufferInBytes($this->megaBytesInBytes($megaBytes));
}
/**
* @param int $gigaBytes
*/
public function setBufferInGigaBytes($gigaBytes)
{
$this->setBufferInBytes($this->gigaBytesInBytes($gigaBytes));
}
/**
* @return int
*/
public function getBufferInBytes()
{
return $this->bufferInBytes;
}
/**
* @return int
*/
public function getBufferInKiloBytes()
{
return $this->bytesInKiloBytes($this->bufferInBytes);
}
/**
* @return int
*/
public function getBufferInMegaBytes()
{
return $this->bytesInMegaBytes($this->bufferInBytes);
}
/**
* @return int
*/
public function getBufferInGigaBytes()
{
return $this->bytesInGigaBytes($this->bufferInBytes);
}
/**
* @param int $bytes
* @throws InvalidArgumentException
*/
public function setLimitInBytes($bytes)
{
if ($this->limitFromIniInBytes > 0) {
if ($bytes > $this->limitFromIniInBytes) {
throw new InvalidArgumentException(
'provided limit (' . $bytes .
') is above ini limit (' .
$this->limitFromIniInBytes . ')',
1
);
}
}
$this->limitInBytes = (int) $bytes;
}
/**
* @param int $kiloBytes
* @throws InvalidArgumentException
*/
public function setLimitInKiloBytes($kiloBytes)
{
$this->setLimitInBytes($this->kiloBytesInBytes($kiloBytes));
}
/**
* @param int $megaBytes
* @throws InvalidArgumentException
*/
public function setLimitInMegaBytes($megaBytes)
{
$this->setLimitInBytes($this->megaBytesInBytes($megaBytes));
}
/**
* @param int $gigaBytes
* @throws InvalidArgumentException
*/
public function setLimitInGigaBytes($gigaBytes)
{
$this->setLimitInBytes($this->gigaBytesInBytes($gigaBytes));
}
/**
* @return int
*/
public function getLimitInBytes()
{
return $this->limitInBytes;
}
/**
* @return int
*/
public function getLimitInKiloBytes()
{
return $this->bytesInKiloBytes($this->limitInBytes);
}
/**
* @return int
*/
public function getLimitInMegaBytes()
{
return $this->bytesInMegaBytes($this->limitInBytes);
}
/**
* @return int
*/
public function getLimitInGigaBytes()
{
return $this->bytesInGigaBytes($this->limitInBytes);
}
/**
* @param array $processIds
* @return int
*/
public function getCurrentUsageInBytes(array $processIds = array())
{
$currentUsageInBytes = memory_get_usage(true);
foreach ($processIds as $processId) {
$return = 0;
exec('ps -p ' . $processId . ' --no-headers -o rss', $return);
if (isset($return[0])) {
//non-swapped physical memory in kilo bytes
$usageInBytes = (int) $return[0];
$currentUsageInBytes += ($usageInBytes * 1024);
}
}
return $currentUsageInBytes;
}
/**
* @param array $processIds
* @return int
*/
public function getCurrentUsageInKiloBytes(array $processIds = array())
{
return $this->bytesInKiloBytes($this->getCurrentUsageInBytes($processIds));
}
/**
* @param array $processIds
* @return int
*/
public function getCurrentUsageInMegaBytes(array $processIds = array())
{
return $this->bytesInMegaBytes($this->getCurrentUsageInBytes($processIds));
}
/**
* @param array $processIds
* @return int
*/
public function getCurrentUsageInGigaBytes(array $processIds = array())
{
return $this->bytesInGigaBytes($this->getCurrentUsageInBytes($processIds));
}
/**
* @param array $processIds
* @return bool
*/
public function isLimitReached(array $processIds = array())
{
$currentUsageInBytes = $this->getCurrentUsageInBytes($processIds);
$currentUsageWithBufferInBytes = $currentUsageInBytes + $this->bufferInBytes;
$isReached = ($currentUsageWithBufferInBytes >= $this->limitInBytes);
return $isReached;
}
/**
* @param int $bytes
* @return int
*/
private function bytesInKiloBytes($bytes)
{
return ((int) ($bytes / 1024));
}
/**
* @param int $bytes
* @return int
*/
private function bytesInMegaBytes($bytes)
{
return ((int) ($bytes / 1048576)); //1048576 = 1024 * 1024
}
/**
* @param int $bytes
* @return int
*/
private function bytesInGigaBytes($bytes)
{
return ((int) ($bytes / 1073741824)); //1073741824 = 1024 * 1024 * 1024
}
/**
* @param int $kiloBytes
* @return int
*/
private function kiloBytesInBytes($kiloBytes)
{
return ((int) ($kiloBytes * 1024));
}
/**
* @param int $megaBytes
* @return int
*/
private function megaBytesInBytes($megaBytes)
{
return ((int) ($megaBytes * 1048576)); //1048576 = 1024 * 1024
}
/**
* @param int $gigaBytes
* @return int
*/
private function gigaBytesInBytes($gigaBytes)
{
return ((int) ($gigaBytes * 1073741824)); //1073741824 = 1024 * 1024 * 1024
}
}