| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-16 00:05:35 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"sync/atomic" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // CacheDiskStats represents cache disk statistics
 | 
					
						
							|  |  |  | // such as current disk usage and available.
 | 
					
						
							|  |  |  | type CacheDiskStats struct { | 
					
						
							| 
									
										
										
										
											2020-12-08 08:35:11 +08:00
										 |  |  | 	// used cache size
 | 
					
						
							|  |  |  | 	UsageSize uint64 | 
					
						
							|  |  |  | 	// total cache disk capacity
 | 
					
						
							|  |  |  | 	TotalCapacity uint64 | 
					
						
							| 
									
										
										
										
											2020-06-16 00:05:35 +08:00
										 |  |  | 	// indicates if usage is high or low, if high value is '1', if low its '0'
 | 
					
						
							|  |  |  | 	UsageState int32 | 
					
						
							|  |  |  | 	// indicates the current usage percentage of this cache disk
 | 
					
						
							|  |  |  | 	UsagePercent uint64 | 
					
						
							|  |  |  | 	Dir          string | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-19 12:35:38 +08:00
										 |  |  | // GetUsageLevelString gets the string representation for the usage level.
 | 
					
						
							|  |  |  | func (c *CacheDiskStats) GetUsageLevelString() (u string) { | 
					
						
							|  |  |  | 	if atomic.LoadInt32(&c.UsageState) == 0 { | 
					
						
							|  |  |  | 		return "low" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return "high" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | // CacheStats - represents bytes served from cache,
 | 
					
						
							|  |  |  | // cache hits and cache misses.
 | 
					
						
							|  |  |  | type CacheStats struct { | 
					
						
							| 
									
										
										
										
											2020-06-16 00:05:35 +08:00
										 |  |  | 	BytesServed  uint64 | 
					
						
							|  |  |  | 	Hits         uint64 | 
					
						
							|  |  |  | 	Misses       uint64 | 
					
						
							|  |  |  | 	GetDiskStats func() []CacheDiskStats | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Increase total bytes served from cache
 | 
					
						
							|  |  |  | func (s *CacheStats) incBytesServed(n int64) { | 
					
						
							| 
									
										
										
										
											2020-05-04 13:35:40 +08:00
										 |  |  | 	atomic.AddUint64(&s.BytesServed, uint64(n)) | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Increase cache hit by 1
 | 
					
						
							|  |  |  | func (s *CacheStats) incHit() { | 
					
						
							| 
									
										
										
										
											2020-05-04 13:35:40 +08:00
										 |  |  | 	atomic.AddUint64(&s.Hits, 1) | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Increase cache miss by 1
 | 
					
						
							|  |  |  | func (s *CacheStats) incMiss() { | 
					
						
							| 
									
										
										
										
											2020-05-04 13:35:40 +08:00
										 |  |  | 	atomic.AddUint64(&s.Misses, 1) | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get total bytes served
 | 
					
						
							|  |  |  | func (s *CacheStats) getBytesServed() uint64 { | 
					
						
							| 
									
										
										
										
											2020-05-04 13:35:40 +08:00
										 |  |  | 	return atomic.LoadUint64(&s.BytesServed) | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get total cache hits
 | 
					
						
							|  |  |  | func (s *CacheStats) getHits() uint64 { | 
					
						
							| 
									
										
										
										
											2020-05-04 13:35:40 +08:00
										 |  |  | 	return atomic.LoadUint64(&s.Hits) | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Get total cache misses
 | 
					
						
							|  |  |  | func (s *CacheStats) getMisses() uint64 { | 
					
						
							| 
									
										
										
										
											2020-05-04 13:35:40 +08:00
										 |  |  | 	return atomic.LoadUint64(&s.Misses) | 
					
						
							| 
									
										
										
										
											2019-12-06 15:16:06 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Prepare new CacheStats structure
 | 
					
						
							|  |  |  | func newCacheStats() *CacheStats { | 
					
						
							|  |  |  | 	return &CacheStats{} | 
					
						
							|  |  |  | } |