| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2017 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | import "github.com/minio/minio/pkg/sys" | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | func setMaxResources() (err error) { | 
					
						
							|  |  |  | 	var maxLimit uint64 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Set open files limit to maximum.
 | 
					
						
							|  |  |  | 	if _, maxLimit, err = sys.GetMaxOpenFileLimit(); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err = sys.SetMaxOpenFileLimit(maxLimit, maxLimit); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Set max memory limit as current memory limit.
 | 
					
						
							|  |  |  | 	if _, maxLimit, err = sys.GetMaxMemoryLimit(); err != nil { | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | 	err = sys.SetMaxMemoryLimit(maxLimit, maxLimit) | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getMaxCacheSize(curLimit, totalRAM uint64) (cacheSize uint64) { | 
					
						
							|  |  |  | 	// Return zero if current limit or totalTAM is less than minRAMSize.
 | 
					
						
							|  |  |  | 	if curLimit < minRAMSize || totalRAM < minRAMSize { | 
					
						
							|  |  |  | 		return cacheSize | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return 50% of current rlimit or total RAM as cache size.
 | 
					
						
							|  |  |  | 	if curLimit < totalRAM { | 
					
						
							|  |  |  | 		cacheSize = curLimit / 2 | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		cacheSize = totalRAM / 2 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return cacheSize | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | // GetMaxCacheSize returns maximum cache size based on current RAM size and memory limit.
 | 
					
						
							|  |  |  | func GetMaxCacheSize() (cacheSize uint64, err error) { | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | 	// Get max memory limit
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | 	var curLimit uint64 | 
					
						
							|  |  |  | 	if curLimit, _, err = sys.GetMaxMemoryLimit(); err != nil { | 
					
						
							|  |  |  | 		return cacheSize, err | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Get total RAM.
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | 	var stats sys.Stats | 
					
						
							|  |  |  | 	if stats, err = sys.GetStats(); err != nil { | 
					
						
							|  |  |  | 		return cacheSize, err | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// In some OS like windows, maxLimit is zero.  Set total RAM as maxLimit.
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | 	if curLimit == 0 { | 
					
						
							|  |  |  | 		curLimit = stats.TotalRAM | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 02:34:37 +08:00
										 |  |  | 	cacheSize = getMaxCacheSize(curLimit, stats.TotalRAM) | 
					
						
							|  |  |  | 	return cacheSize, err | 
					
						
							| 
									
										
										
										
											2017-03-02 13:51:57 +08:00
										 |  |  | } |