...

Source file src/github.com/ory/x/metricsx/metrics.go

Documentation: github.com/ory/x/metricsx

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   */
    20  
    21  package metricsx
    22  
    23  import (
    24  	"runtime"
    25  	"sync"
    26  )
    27  
    28  // MemoryStatistics is a JSON-able version of runtime.MemStats
    29  type MemoryStatistics struct {
    30  	sync.Mutex
    31  	// Alloc is bytes of allocated heap objects.
    32  	Alloc uint64 `json:"alloc"`
    33  	// TotalAlloc is cumulative bytes allocated for heap objects.
    34  	TotalAlloc uint64 `json:"totalAlloc"`
    35  	// Sys is the total bytes of memory obtained from the OS.
    36  	Sys uint64 `json:"sys"`
    37  	// Lookups is the number of pointer lookups performed by the
    38  	// runtime.
    39  	Lookups uint64 `json:"lookups"`
    40  	// Mallocs is the cumulative count of heap objects allocated.
    41  	// The number of live objects is Mallocs - Frees.
    42  	Mallocs uint64 `json:"mallocs"`
    43  	// Frees is the cumulative count of heap objects freed.
    44  	Frees uint64 `json:"frees"`
    45  	// HeapAlloc is bytes of allocated heap objects.
    46  	HeapAlloc uint64 `json:"heapAlloc"`
    47  	// HeapSys is bytes of heap memory obtained from the OS.
    48  	HeapSys uint64 `json:"heapSys"`
    49  	// HeapIdle is bytes in idle (unused) spans.
    50  	HeapIdle uint64 `json:"heapIdle"`
    51  	// HeapInuse is bytes in in-use spans.
    52  	HeapInuse uint64 `json:"heapInuse"`
    53  	// HeapReleased is bytes of physical memory returned to the OS.
    54  	HeapReleased uint64 `json:"heapReleased"`
    55  	// HeapObjects is the number of allocated heap objects.
    56  	HeapObjects uint64 `json:"heapObjects"`
    57  	// NumGC is the number of completed GC cycles.
    58  	NumGC uint32 `json:"numGC"`
    59  }
    60  
    61  // ToMap converts to a map[string]interface{}.
    62  func (ms *MemoryStatistics) ToMap() map[string]interface{} {
    63  	return map[string]interface{}{
    64  		"alloc":          ms.Alloc,
    65  		"totalAlloc":     ms.TotalAlloc,
    66  		"sys":            ms.Sys,
    67  		"lookups":        ms.Lookups,
    68  		"mallocs":        ms.Mallocs,
    69  		"frees":          ms.Frees,
    70  		"heapAlloc":      ms.HeapAlloc,
    71  		"heapSys":        ms.HeapSys,
    72  		"heapIdle":       ms.HeapIdle,
    73  		"heapInuse":      ms.HeapInuse,
    74  		"heapReleased":   ms.HeapReleased,
    75  		"heapObjects":    ms.HeapObjects,
    76  		"numGC":          ms.NumGC,
    77  		"nonInteraction": 1,
    78  	}
    79  }
    80  
    81  // Update takes the most recent stats from runtime.
    82  func (ms *MemoryStatistics) Update() {
    83  	var m runtime.MemStats
    84  	runtime.ReadMemStats(&m)
    85  
    86  	ms.Lock()
    87  	defer ms.Unlock()
    88  	ms.Alloc = m.Alloc
    89  	ms.TotalAlloc = m.TotalAlloc
    90  	ms.Sys = m.Sys
    91  	ms.Lookups = m.Lookups
    92  	ms.Mallocs = m.Mallocs
    93  	ms.Frees = m.Frees
    94  	ms.HeapAlloc = m.HeapAlloc
    95  	ms.HeapSys = m.HeapSys
    96  	ms.HeapIdle = m.HeapIdle
    97  	ms.HeapInuse = m.HeapInuse
    98  	ms.HeapReleased = m.HeapReleased
    99  	ms.HeapObjects = m.HeapObjects
   100  	ms.NumGC = m.NumGC
   101  }
   102  

View as plain text