| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright Copyright (c) 2017, Matias De lellis <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Branko Kokanovic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | namespace OCA\FaceRecognition\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * Tries to get total amount of memory on the host, given to PHP. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class MemoryLimits { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * Tries to get memory available to PHP. This is highly speculative business. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * It will first try reading value of "memory_limit" and if it is -1, it will | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * try to get 1/2 memory of host system. In case of any error, it will return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * negative value. Note that negative here doesn't mean "unlimited"! This | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * function doesn't care if PHP is being used in CLI or FPM mode. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @return int Total memory available to PHP, in bytes, or negative if | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * we don't know any better | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  | 	public static function getAvailableMemory(): int { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		// Try first to get from php.ini | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 			$value = MemoryLimits::returnBytes(ini_get('memory_limit')); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 			if ($value > 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 				return $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 		} catch (\Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 			return -1; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | 		// php.ini says that memory_limit is -1, which means unlimited. | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  | 		//We need to get memory from system (if system is supported here). | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  | 		// Only linux is currently supported. | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | 		if (php_uname("s") === "Linux") { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | 			$linuxMemory = MemoryLimits::getTotalMemoryLinux(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  | 			if ($linuxMemory <= 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  | 				return -3; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  | 			return intval($linuxMemory / 2); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  | 		} else { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  | 			return -2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	private static function getTotalMemoryLinux(): int { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		$fh = fopen('/proc/meminfo','r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$mem = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		while ($line = fgets($fh)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 			$pieces = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			if (preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 				$mem = $pieces[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		fclose($fh); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$memKb = intval($mem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		return $memKb * 1024; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 * Converts shorthand memory notation value to bytes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	 * From http://php.net/manual/en/function.ini-get.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * @param string val Memory size shorthand notation string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * @return int Value in integers (bytes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	private static function returnBytes(string $val): int { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		$val = trim($val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		if ($val === "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 			return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		$last = strtolower($val[strlen($val)-1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		switch($last) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 			// Fallthrough on purpose | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			case 'g': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 				$val *= 1024; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			case 'm': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 				$val *= 1024; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			case 'k': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 				$val *= 1024; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		return $val; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 108 |  |  | } |