Skip to content

Commit

Permalink
Merge pull request #402 from breed808/perf_mem
Browse files Browse the repository at this point in the history
Use perflib for memory collector
  • Loading branch information
carlpett authored Oct 9, 2019
2 parents a0e5baa + 18128f4 commit 012b938
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 74 deletions.
146 changes: 72 additions & 74 deletions collector/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package collector

import (
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
Expand All @@ -15,7 +14,7 @@ func init() {
Factories["memory"] = NewMemoryCollector
}

// A MemoryCollector is a Prometheus collector for WMI Win32_PerfRawData_PerfOS_Memory metrics
// A MemoryCollector is a Prometheus collector for perflib Memory metrics
type MemoryCollector struct {
AvailableBytes *prometheus.Desc
CacheBytes *prometheus.Desc
Expand Down Expand Up @@ -257,247 +256,246 @@ func NewMemoryCollector() (Collector, error) {
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *MemoryCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) error {
if desc, err := c.collect(ch); err != nil {
if desc, err := c.collect(ctx, ch); err != nil {
log.Error("failed collecting memory metrics:", desc, err)
return err
}
return nil
}

type Win32_PerfRawData_PerfOS_Memory struct {
AvailableBytes uint64
AvailableKBytes uint64
AvailableMBytes uint64
CacheBytes uint64
CacheBytesPeak uint64
CacheFaultsPersec uint32
CommitLimit uint64
CommittedBytes uint64
DemandZeroFaultsPersec uint32
FreeAndZeroPageListBytes uint64
FreeSystemPageTableEntries uint32
ModifiedPageListBytes uint64
PageFaultsPersec uint32
PageReadsPersec uint32
PagesInputPersec uint32
PagesOutputPersec uint32
PagesPersec uint32
PageWritesPersec uint32
PoolNonpagedAllocs uint32
PoolNonpagedBytes uint64
PoolPagedAllocs uint32
PoolPagedBytes uint64
PoolPagedResidentBytes uint64
StandbyCacheCoreBytes uint64
StandbyCacheNormalPriorityBytes uint64
StandbyCacheReserveBytes uint64
SystemCacheResidentBytes uint64
SystemCodeResidentBytes uint64
SystemCodeTotalBytes uint64
SystemDriverResidentBytes uint64
SystemDriverTotalBytes uint64
TransitionFaultsPersec uint32
TransitionPagesRePurposedPersec uint32
WriteCopiesPersec uint32
type memory struct {
AvailableBytes float64 `perflib:"Available Bytes"`
AvailableKBytes float64 `perflib:"Available KBytes"`
AvailableMBytes float64 `perflib:"Available MBytes"`
CacheBytes float64 `perflib:"Cache Bytes"`
CacheBytesPeak float64 `perflib:"Cache Bytes Peak"`
CacheFaultsPersec float64 `perflib:"Cache Faults/sec"`
CommitLimit float64 `perflib:"Commit Limit"`
CommittedBytes float64 `perflib:"Committed Bytes"`
DemandZeroFaultsPersec float64 `perflib:"Demand Zero Faults/sec"`
FreeAndZeroPageListBytes float64 `perflib:"Free & Zero Page List Bytes"`
FreeSystemPageTableEntries float64 `perflib:"Free System Page Table Entries"`
ModifiedPageListBytes float64 `perflib:"Modified Page List Bytes"`
PageFaultsPersec float64 `perflib:"Page Faults/sec"`
PageReadsPersec float64 `perflib:"Page Reads/sec"`
PagesInputPersec float64 `perflib:"Pages Input/sec"`
PagesOutputPersec float64 `perflib:"Pages Output/sec"`
PagesPersec float64 `perflib:"Pages/sec"`
PageWritesPersec float64 `perflib:"Page Writes/sec"`
PoolNonpagedAllocs float64 `perflib:"Pool Nonpaged Allocs"`
PoolNonpagedBytes float64 `perflib:"Pool Nonpaged Bytes"`
PoolPagedAllocs float64 `perflib:"Pool Paged Allocs"`
PoolPagedBytes float64 `perflib:"Pool Paged Bytes"`
PoolPagedResidentBytes float64 `perflib:"Pool Paged Resident Bytes"`
StandbyCacheCoreBytes float64 `perflib:"Standby Cache Core Bytes"`
StandbyCacheNormalPriorityBytes float64 `perflib:"Standby Cache Normal Priority Bytes"`
StandbyCacheReserveBytes float64 `perflib:"Standby Cache Reserve Bytes"`
SystemCacheResidentBytes float64 `perflib:"System Cache Resident Bytes"`
SystemCodeResidentBytes float64 `perflib:"System Code Resident Bytes"`
SystemCodeTotalBytes float64 `perflib:"System Code Total Bytes"`
SystemDriverResidentBytes float64 `perflib:"System Driver Resident Bytes"`
SystemDriverTotalBytes float64 `perflib:"System Driver Total Bytes"`
TransitionFaultsPersec float64 `perflib:"Transition Faults/sec"`
TransitionPagesRePurposedPersec float64 `perflib:"Transition Pages RePurposed/sec"`
WriteCopiesPersec float64 `perflib:"Write Copies/sec"`
}

func (c *MemoryCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_PerfRawData_PerfOS_Memory
q := queryAll(&dst)
if err := wmi.Query(q, &dst); err != nil {
func (c *MemoryCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []memory
if err := unmarshalObject(ctx.perfObjects["Memory"], &dst); err != nil {
return nil, err
}

ch <- prometheus.MustNewConstMetric(
c.AvailableBytes,
prometheus.GaugeValue,
float64(dst[0].AvailableBytes),
dst[0].AvailableBytes,
)

ch <- prometheus.MustNewConstMetric(
c.CacheBytes,
prometheus.GaugeValue,
float64(dst[0].CacheBytes),
dst[0].CacheBytes,
)

ch <- prometheus.MustNewConstMetric(
c.CacheBytesPeak,
prometheus.GaugeValue,
float64(dst[0].CacheBytesPeak),
dst[0].CacheBytesPeak,
)

ch <- prometheus.MustNewConstMetric(
c.CacheFaultsTotal,
prometheus.GaugeValue,
float64(dst[0].CacheFaultsPersec),
dst[0].CacheFaultsPersec,
)

ch <- prometheus.MustNewConstMetric(
c.CommitLimit,
prometheus.GaugeValue,
float64(dst[0].CommitLimit),
dst[0].CommitLimit,
)

ch <- prometheus.MustNewConstMetric(
c.CommittedBytes,
prometheus.GaugeValue,
float64(dst[0].CommittedBytes),
dst[0].CommittedBytes,
)

ch <- prometheus.MustNewConstMetric(
c.DemandZeroFaultsTotal,
prometheus.GaugeValue,
float64(dst[0].DemandZeroFaultsPersec),
dst[0].DemandZeroFaultsPersec,
)

ch <- prometheus.MustNewConstMetric(
c.FreeAndZeroPageListBytes,
prometheus.GaugeValue,
float64(dst[0].FreeAndZeroPageListBytes),
dst[0].FreeAndZeroPageListBytes,
)

ch <- prometheus.MustNewConstMetric(
c.FreeSystemPageTableEntries,
prometheus.GaugeValue,
float64(dst[0].FreeSystemPageTableEntries),
dst[0].FreeSystemPageTableEntries,
)

ch <- prometheus.MustNewConstMetric(
c.ModifiedPageListBytes,
prometheus.GaugeValue,
float64(dst[0].ModifiedPageListBytes),
dst[0].ModifiedPageListBytes,
)

ch <- prometheus.MustNewConstMetric(
c.PageFaultsTotal,
prometheus.GaugeValue,
float64(dst[0].PageFaultsPersec),
dst[0].PageFaultsPersec,
)

ch <- prometheus.MustNewConstMetric(
c.SwapPageReadsTotal,
prometheus.GaugeValue,
float64(dst[0].PageReadsPersec),
dst[0].PageReadsPersec,
)

ch <- prometheus.MustNewConstMetric(
c.SwapPagesReadTotal,
prometheus.GaugeValue,
float64(dst[0].PagesInputPersec),
dst[0].PagesInputPersec,
)

ch <- prometheus.MustNewConstMetric(
c.SwapPagesWrittenTotal,
prometheus.GaugeValue,
float64(dst[0].PagesOutputPersec),
dst[0].PagesOutputPersec,
)

ch <- prometheus.MustNewConstMetric(
c.SwapPageOperationsTotal,
prometheus.GaugeValue,
float64(dst[0].PagesPersec),
dst[0].PagesPersec,
)

ch <- prometheus.MustNewConstMetric(
c.SwapPageWritesTotal,
prometheus.GaugeValue,
float64(dst[0].PageWritesPersec),
dst[0].PageWritesPersec,
)

ch <- prometheus.MustNewConstMetric(
c.PoolNonpagedAllocsTotal,
prometheus.GaugeValue,
float64(dst[0].PoolNonpagedAllocs),
dst[0].PoolNonpagedAllocs,
)

ch <- prometheus.MustNewConstMetric(
c.PoolNonpagedBytes,
prometheus.GaugeValue,
float64(dst[0].PoolNonpagedBytes),
dst[0].PoolNonpagedBytes,
)

ch <- prometheus.MustNewConstMetric(
c.PoolPagedAllocsTotal,
prometheus.GaugeValue,
float64(dst[0].PoolPagedAllocs),
dst[0].PoolPagedAllocs,
)

ch <- prometheus.MustNewConstMetric(
c.PoolPagedBytes,
prometheus.GaugeValue,
float64(dst[0].PoolPagedBytes),
dst[0].PoolPagedBytes,
)

ch <- prometheus.MustNewConstMetric(
c.PoolPagedResidentBytes,
prometheus.GaugeValue,
float64(dst[0].PoolPagedResidentBytes),
dst[0].PoolPagedResidentBytes,
)

ch <- prometheus.MustNewConstMetric(
c.StandbyCacheCoreBytes,
prometheus.GaugeValue,
float64(dst[0].StandbyCacheCoreBytes),
dst[0].StandbyCacheCoreBytes,
)

ch <- prometheus.MustNewConstMetric(
c.StandbyCacheNormalPriorityBytes,
prometheus.GaugeValue,
float64(dst[0].StandbyCacheNormalPriorityBytes),
dst[0].StandbyCacheNormalPriorityBytes,
)

ch <- prometheus.MustNewConstMetric(
c.StandbyCacheReserveBytes,
prometheus.GaugeValue,
float64(dst[0].StandbyCacheReserveBytes),
dst[0].StandbyCacheReserveBytes,
)

ch <- prometheus.MustNewConstMetric(
c.SystemCacheResidentBytes,
prometheus.GaugeValue,
float64(dst[0].SystemCacheResidentBytes),
dst[0].SystemCacheResidentBytes,
)

ch <- prometheus.MustNewConstMetric(
c.SystemCodeResidentBytes,
prometheus.GaugeValue,
float64(dst[0].SystemCodeResidentBytes),
dst[0].SystemCodeResidentBytes,
)

ch <- prometheus.MustNewConstMetric(
c.SystemCodeTotalBytes,
prometheus.GaugeValue,
float64(dst[0].SystemCodeTotalBytes),
dst[0].SystemCodeTotalBytes,
)

ch <- prometheus.MustNewConstMetric(
c.SystemDriverResidentBytes,
prometheus.GaugeValue,
float64(dst[0].SystemDriverResidentBytes),
dst[0].SystemDriverResidentBytes,
)

ch <- prometheus.MustNewConstMetric(
c.SystemDriverTotalBytes,
prometheus.GaugeValue,
float64(dst[0].SystemDriverTotalBytes),
dst[0].SystemDriverTotalBytes,
)

ch <- prometheus.MustNewConstMetric(
c.TransitionFaultsTotal,
prometheus.GaugeValue,
float64(dst[0].TransitionFaultsPersec),
dst[0].TransitionFaultsPersec,
)

ch <- prometheus.MustNewConstMetric(
c.TransitionPagesRepurposedTotal,
prometheus.GaugeValue,
float64(dst[0].TransitionPagesRePurposedPersec),
dst[0].TransitionPagesRePurposedPersec,
)

ch <- prometheus.MustNewConstMetric(
c.WriteCopiesTotal,
prometheus.GaugeValue,
float64(dst[0].WriteCopiesPersec),
dst[0].WriteCopiesPersec,
)

return nil, nil
Expand Down
1 change: 1 addition & 0 deletions docs/collector.memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The memory collector exposes metrics about system memory usage
|||
-|-
Metric name prefix | `memory`
Data source | Perflib
Classes | `Win32_PerfRawData_PerfOS_Memory`
Enabled by default? | Yes

Expand Down

0 comments on commit 012b938

Please sign in to comment.