github.com/rohankumardubey/proxyfs@v0.0.0-20210108201508-653efa9ab00e/stats/api_internal.go (about)

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