github.com/swiftstack/ProxyFS@v0.0.0-20210203235616-4017c267d62f/stats/api_internal.go (about)

     1  // Copyright (c) 2015-2021, NVIDIA CORPORATION.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  // Package stats provides a simple statsd client API.
     5  package stats
     6  
     7  import (
     8  	"sync"
     9  )
    10  
    11  func (ms MultipleStat) findStatStrings(numBytes uint64, numEntries uint64) (ops *string, bytes *string, entries *string, bbytes *string, appended *string, overwritten *string) {
    12  	switch ms {
    13  	case DirRead:
    14  		// directory read uses operations, entries and bytes stats
    15  		ops = &DirReadOps
    16  		bytes = &DirReadBytes
    17  		entries = &DirReadEntries
    18  	case FileRead:
    19  		// file read uses operations, op bucketed bytes, and bytes stats
    20  		ops = &FileReadOps
    21  		bytes = &FileReadBytes
    22  		if numBytes <= 4096 {
    23  			bbytes = &FileReadOps4K
    24  		} else if numBytes <= 8192 {
    25  			bbytes = &FileReadOps8K
    26  		} else if numBytes <= 16384 {
    27  			bbytes = &FileReadOps16K
    28  		} else if numBytes <= 32768 {
    29  			bbytes = &FileReadOps32K
    30  		} else if numBytes <= 65536 {
    31  			bbytes = &FileReadOps64K
    32  		} else {
    33  			bbytes = &FileReadOpsOver64K
    34  		}
    35  	case FileReadplan:
    36  		// file readplan uses operations, op bucketed bytes, and bytes stats
    37  		ops = &FileReadplanOps
    38  		bytes = &FileReadplanBytes
    39  		if numBytes <= 4096 {
    40  			bbytes = &FileReadplanOps4K
    41  		} else if numBytes <= 8192 {
    42  			bbytes = &FileReadplanOps8K
    43  		} else if numBytes <= 16384 {
    44  			bbytes = &FileReadplanOps16K
    45  		} else if numBytes <= 32768 {
    46  			bbytes = &FileReadplanOps32K
    47  		} else if numBytes <= 65536 {
    48  			bbytes = &FileReadplanOps64K
    49  		} else {
    50  			bbytes = &FileReadplanOpsOver64K
    51  		}
    52  		if numEntries == 1 {
    53  			entries = &FileReadplanOpsEntries1
    54  		} else if numEntries <= 4 {
    55  			entries = &FileReadplanOpsEntriesTo4
    56  		} else if numEntries <= 16 {
    57  			entries = &FileReadplanOpsEntriesTo16
    58  		} else if numEntries <= 64 {
    59  			entries = &FileReadplanOpsEntriesTo64
    60  		} else {
    61  			entries = &FileReadplanOpsEntriesOver64
    62  		}
    63  	case FileWrite:
    64  		// file write uses operations, op bucketed bytes, bytes, appended and overwritten stats
    65  		ops = &FileWriteOps
    66  		bytes = &FileWriteBytes
    67  		if numBytes <= 4096 {
    68  			bbytes = &FileWriteOps4K
    69  		} else if numBytes <= 8192 {
    70  			bbytes = &FileWriteOps8K
    71  		} else if numBytes <= 16384 {
    72  			bbytes = &FileWriteOps16K
    73  		} else if numBytes <= 32768 {
    74  			bbytes = &FileWriteOps32K
    75  		} else if numBytes <= 65536 {
    76  			bbytes = &FileWriteOps64K
    77  		} else {
    78  			bbytes = &FileWriteOpsOver64K
    79  		}
    80  		appended = &FileWriteAppended
    81  		overwritten = &FileWriteOverwritten
    82  	case FileWrote:
    83  		// file wrote uses operations, op bucketed bytes, and bytes stats
    84  		ops = &FileWroteOps
    85  		bytes = &FileWroteBytes
    86  		if numBytes <= 4096 {
    87  			bbytes = &FileWroteOps4K
    88  		} else if numBytes <= 8192 {
    89  			bbytes = &FileWroteOps8K
    90  		} else if numBytes <= 16384 {
    91  			bbytes = &FileWroteOps16K
    92  		} else if numBytes <= 32768 {
    93  			bbytes = &FileWroteOps32K
    94  		} else if numBytes <= 65536 {
    95  			bbytes = &FileWroteOps64K
    96  		} else {
    97  			bbytes = &FileWroteOpsOver64K
    98  		}
    99  	case JrpcfsIoWrite:
   100  		// jrpcfs write uses operations, op bucketed bytes, and bytes stats
   101  		ops = &JrpcfsIoWriteOps
   102  		bytes = &JrpcfsIoWriteBytes
   103  		if numBytes <= 4096 {
   104  			bbytes = &JrpcfsIoWriteOps4K
   105  		} else if numBytes <= 8192 {
   106  			bbytes = &JrpcfsIoWriteOps8K
   107  		} else if numBytes <= 16384 {
   108  			bbytes = &JrpcfsIoWriteOps16K
   109  		} else if numBytes <= 32768 {
   110  			bbytes = &JrpcfsIoWriteOps32K
   111  		} else if numBytes <= 65536 {
   112  			bbytes = &JrpcfsIoWriteOps64K
   113  		} else {
   114  			bbytes = &JrpcfsIoWriteOpsOver64K
   115  		}
   116  	case JrpcfsIoRead:
   117  		// jrpcfs read uses operations, op bucketed bytes, and bytes stats
   118  		ops = &JrpcfsIoReadOps
   119  		bytes = &JrpcfsIoReadBytes
   120  		if numBytes <= 4096 {
   121  			bbytes = &JrpcfsIoReadOps4K
   122  		} else if numBytes <= 8192 {
   123  			bbytes = &JrpcfsIoReadOps8K
   124  		} else if numBytes <= 16384 {
   125  			bbytes = &JrpcfsIoReadOps16K
   126  		} else if numBytes <= 32768 {
   127  			bbytes = &JrpcfsIoReadOps32K
   128  		} else if numBytes <= 65536 {
   129  			bbytes = &JrpcfsIoReadOps64K
   130  		} else {
   131  			bbytes = &JrpcfsIoReadOpsOver64K
   132  		}
   133  	case SwiftObjGet:
   134  		// swiftclient object-get uses operations, op bucketed bytes, and bytes stats
   135  		ops = &SwiftObjGetOps
   136  		bytes = &SwiftObjGetBytes
   137  		if numBytes <= 4096 {
   138  			bbytes = &SwiftObjGetOps4K
   139  		} else if numBytes <= 8192 {
   140  			bbytes = &SwiftObjGetOps8K
   141  		} else if numBytes <= 16384 {
   142  			bbytes = &SwiftObjGetOps16K
   143  		} else if numBytes <= 32768 {
   144  			bbytes = &SwiftObjGetOps32K
   145  		} else if numBytes <= 65536 {
   146  			bbytes = &SwiftObjGetOps64K
   147  		} else {
   148  			bbytes = &SwiftObjGetOpsOver64K
   149  		}
   150  	case SwiftObjLoad:
   151  		// swiftclient object-load uses operations, op bucketed bytes, and bytes stats
   152  		ops = &SwiftObjLoadOps
   153  		bytes = &SwiftObjLoadBytes
   154  		if numBytes <= 4096 {
   155  			bbytes = &SwiftObjLoadOps4K
   156  		} else if numBytes <= 8192 {
   157  			bbytes = &SwiftObjLoadOps8K
   158  		} else if numBytes <= 16384 {
   159  			bbytes = &SwiftObjLoadOps16K
   160  		} else if numBytes <= 32768 {
   161  			bbytes = &SwiftObjLoadOps32K
   162  		} else if numBytes <= 65536 {
   163  			bbytes = &SwiftObjLoadOps64K
   164  		} else {
   165  			bbytes = &SwiftObjLoadOpsOver64K
   166  		}
   167  	case SwiftObjRead:
   168  		// swiftclient object-read uses operations, op bucketed bytes, and bytes stats
   169  		ops = &SwiftObjReadOps
   170  		bytes = &SwiftObjReadBytes
   171  		if numBytes <= 4096 {
   172  			bbytes = &SwiftObjReadOps4K
   173  		} else if numBytes <= 8192 {
   174  			bbytes = &SwiftObjReadOps8K
   175  		} else if numBytes <= 16384 {
   176  			bbytes = &SwiftObjReadOps16K
   177  		} else if numBytes <= 32768 {
   178  			bbytes = &SwiftObjReadOps32K
   179  		} else if numBytes <= 65536 {
   180  			bbytes = &SwiftObjReadOps64K
   181  		} else {
   182  			bbytes = &SwiftObjReadOpsOver64K
   183  		}
   184  	case SwiftObjTail:
   185  		// swiftclient object-tail uses operations and bytes stats
   186  		ops = &SwiftObjTailOps
   187  		bytes = &SwiftObjTailBytes
   188  	case SwiftObjPutCtxRead:
   189  		// swiftclient object-put-context.read uses operations, op bucketed bytes, and bytes stats
   190  		ops = &SwiftObjPutCtxReadOps
   191  		bytes = &SwiftObjPutCtxReadBytes
   192  		if numBytes <= 4096 {
   193  			bbytes = &SwiftObjPutCtxReadOps4K
   194  		} else if numBytes <= 8192 {
   195  			bbytes = &SwiftObjPutCtxReadOps8K
   196  		} else if numBytes <= 16384 {
   197  			bbytes = &SwiftObjPutCtxReadOps16K
   198  		} else if numBytes <= 32768 {
   199  			bbytes = &SwiftObjPutCtxReadOps32K
   200  		} else if numBytes <= 65536 {
   201  			bbytes = &SwiftObjPutCtxReadOps64K
   202  		} else {
   203  			bbytes = &SwiftObjPutCtxReadOpsOver64K
   204  		}
   205  	case SwiftObjPutCtxSendChunk:
   206  		// swiftclient object-put-context.send-chunk uses operations, op bucketed bytes, and bytes stats
   207  		ops = &SwiftObjPutCtxSendChunkOps
   208  		bytes = &SwiftObjPutCtxSendChunkBytes
   209  		if numBytes <= 4096 {
   210  			bbytes = &SwiftObjPutCtxSendChunkOps4K
   211  		} else if numBytes <= 8192 {
   212  			bbytes = &SwiftObjPutCtxSendChunkOps8K
   213  		} else if numBytes <= 16384 {
   214  			bbytes = &SwiftObjPutCtxSendChunkOps16K
   215  		} else if numBytes <= 32768 {
   216  			bbytes = &SwiftObjPutCtxSendChunkOps32K
   217  		} else if numBytes <= 65536 {
   218  			bbytes = &SwiftObjPutCtxSendChunkOps64K
   219  		} else {
   220  			bbytes = &SwiftObjPutCtxSendChunkOpsOver64K
   221  		}
   222  	}
   223  	return
   224  }
   225  
   226  func dump() (statMap map[string]uint64) {
   227  	globals.Lock()
   228  	numStats := len(globals.statFullMap)
   229  	statMap = make(map[string]uint64, numStats)
   230  	for statKey, statValue := range globals.statFullMap {
   231  		statMap[statKey] = statValue
   232  	}
   233  	globals.Unlock()
   234  	return
   235  }
   236  
   237  var statStructPool sync.Pool = sync.Pool{
   238  	New: func() interface{} {
   239  		return &statStruct{}
   240  	},
   241  }
   242  
   243  func incrementSomething(statName *string, incBy uint64) {
   244  	if incBy == 0 {
   245  		// No point in incrementing by zero
   246  		return
   247  	}
   248  
   249  	// if stats are not enabled yet, just ignore (reduce a window while
   250  	// stats are shutting down by saving the channel to a local variable)
   251  	statChan := globals.statChan
   252  	if statChan == nil {
   253  		return
   254  	}
   255  
   256  	stat := statStructPool.Get().(*statStruct)
   257  	stat.name = statName
   258  	stat.increment = incBy
   259  	statChan <- stat
   260  }
   261  
   262  func incrementOperations(statName *string) {
   263  	incrementSomething(statName, 1)
   264  }
   265  
   266  func incrementOperationsBy(statName *string, incBy uint64) {
   267  	incrementSomething(statName, incBy)
   268  }
   269  
   270  func incrementOperationsAndBytes(stat MultipleStat, bytes uint64) {
   271  	opsStat, bytesStat, _, _, _, _ := stat.findStatStrings(bytes, 1)
   272  	incrementSomething(opsStat, 1)
   273  	incrementSomething(bytesStat, bytes)
   274  }
   275  
   276  func incrementOperationsEntriesAndBytes(stat MultipleStat, entries uint64, bytes uint64) {
   277  	opsStat, bytesStat, bentries, _, _, _ := stat.findStatStrings(bytes, 1)
   278  	incrementSomething(opsStat, 1)
   279  	incrementSomething(bentries, entries)
   280  	incrementSomething(bytesStat, bytes)
   281  }
   282  
   283  func incrementOperationsAndBucketedBytes(stat MultipleStat, bytes uint64) {
   284  	opsStat, bytesStat, _, bbytesStat, _, _ := stat.findStatStrings(bytes, 1)
   285  	incrementSomething(opsStat, 1)
   286  	incrementSomething(bytesStat, bytes)
   287  	incrementSomething(bbytesStat, 1)
   288  }
   289  
   290  func incrementOperationsBucketedEntriesAndBucketedBytes(stat MultipleStat, entries uint64, bytes uint64) {
   291  	opsStat, bytesStat, bentries, bbytesStat, _, _ := stat.findStatStrings(bytes, entries)
   292  	incrementSomething(opsStat, 1)
   293  	incrementSomething(bentries, 1)
   294  	incrementSomething(bytesStat, bytes)
   295  	incrementSomething(bbytesStat, 1)
   296  }
   297  
   298  func incrementOperationsBucketedBytesAndAppendedOverwritten(stat MultipleStat, bytes uint64, appended uint64, overwritten uint64) {
   299  	opsStat, bytesStat, _, bbytesStat, appendedStat, overwrittenStat := stat.findStatStrings(bytes, 1)
   300  	incrementSomething(opsStat, 1)
   301  	incrementSomething(bytesStat, bytes)
   302  	incrementSomething(bbytesStat, 1)
   303  	incrementSomething(appendedStat, appended)
   304  	incrementSomething(overwrittenStat, overwritten)
   305  }