From 0e78ed63a62fe17f0228c3dc39775127710edb0a Mon Sep 17 00:00:00 2001 From: Pavel Borzenkov Date: Wed, 11 Nov 2015 16:02:41 +0300 Subject: [PATCH] Add new collector exposing 'ksmd' stats Add new collector which exposes the content of /sys/kernel/mm/ksm directory. This directory contains control and statistics files for Kernel Samepage Merging daemon. The collector is not enabled by default. Signed-off-by: Pavel Borzenkov --- collector/fixtures/e2e-output.txt | 27 ++++++ .../fixtures/sys/kernel/mm/ksm/full_scans | 1 + .../sys/kernel/mm/ksm/merge_across_nodes | 1 + .../fixtures/sys/kernel/mm/ksm/pages_shared | 1 + .../fixtures/sys/kernel/mm/ksm/pages_sharing | 1 + .../fixtures/sys/kernel/mm/ksm/pages_to_scan | 1 + .../fixtures/sys/kernel/mm/ksm/pages_unshared | 1 + .../fixtures/sys/kernel/mm/ksm/pages_volatile | 1 + collector/fixtures/sys/kernel/mm/ksm/run | 1 + .../sys/kernel/mm/ksm/sleep_millisecs | 1 + collector/ksmd_linux.go | 83 +++++++++++++++++++ end-to-end-test.sh | 1 + 12 files changed, 120 insertions(+) create mode 100644 collector/fixtures/sys/kernel/mm/ksm/full_scans create mode 100644 collector/fixtures/sys/kernel/mm/ksm/merge_across_nodes create mode 100644 collector/fixtures/sys/kernel/mm/ksm/pages_shared create mode 100644 collector/fixtures/sys/kernel/mm/ksm/pages_sharing create mode 100644 collector/fixtures/sys/kernel/mm/ksm/pages_to_scan create mode 100644 collector/fixtures/sys/kernel/mm/ksm/pages_unshared create mode 100644 collector/fixtures/sys/kernel/mm/ksm/pages_volatile create mode 100644 collector/fixtures/sys/kernel/mm/ksm/run create mode 100644 collector/fixtures/sys/kernel/mm/ksm/sleep_millisecs create mode 100644 collector/ksmd_linux.go diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 4801347e5b..1ab0356fa9 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -444,6 +444,33 @@ node_forks 26442 # HELP node_intr Total number of interrupts serviced. # TYPE node_intr counter node_intr 8.885917e+06 +# HELP node_ksmd_full_scans_total ksmd 'full_scans' file. +# TYPE node_ksmd_full_scans_total counter +node_ksmd_full_scans_total 323 +# HELP node_ksmd_merge_across_nodes ksmd 'merge_across_nodes' file. +# TYPE node_ksmd_merge_across_nodes gauge +node_ksmd_merge_across_nodes 1 +# HELP node_ksmd_pages_shared ksmd 'pages_shared' file. +# TYPE node_ksmd_pages_shared gauge +node_ksmd_pages_shared 1 +# HELP node_ksmd_pages_sharing ksmd 'pages_sharing' file. +# TYPE node_ksmd_pages_sharing gauge +node_ksmd_pages_sharing 255 +# HELP node_ksmd_pages_to_scan ksmd 'pages_to_scan' file. +# TYPE node_ksmd_pages_to_scan gauge +node_ksmd_pages_to_scan 100 +# HELP node_ksmd_pages_unshared ksmd 'pages_unshared' file. +# TYPE node_ksmd_pages_unshared gauge +node_ksmd_pages_unshared 0 +# HELP node_ksmd_pages_volatile ksmd 'pages_volatile' file. +# TYPE node_ksmd_pages_volatile gauge +node_ksmd_pages_volatile 0 +# HELP node_ksmd_run ksmd 'run' file. +# TYPE node_ksmd_run gauge +node_ksmd_run 1 +# HELP node_ksmd_sleep_seconds ksmd 'sleep_millisecs' file. +# TYPE node_ksmd_sleep_seconds gauge +node_ksmd_sleep_seconds 0.02 # HELP node_load1 1m load average. # TYPE node_load1 gauge node_load1 0.21 diff --git a/collector/fixtures/sys/kernel/mm/ksm/full_scans b/collector/fixtures/sys/kernel/mm/ksm/full_scans new file mode 100644 index 0000000000..3860ed9137 --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/full_scans @@ -0,0 +1 @@ +323 diff --git a/collector/fixtures/sys/kernel/mm/ksm/merge_across_nodes b/collector/fixtures/sys/kernel/mm/ksm/merge_across_nodes new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/merge_across_nodes @@ -0,0 +1 @@ +1 diff --git a/collector/fixtures/sys/kernel/mm/ksm/pages_shared b/collector/fixtures/sys/kernel/mm/ksm/pages_shared new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/pages_shared @@ -0,0 +1 @@ +1 diff --git a/collector/fixtures/sys/kernel/mm/ksm/pages_sharing b/collector/fixtures/sys/kernel/mm/ksm/pages_sharing new file mode 100644 index 0000000000..ace9d03621 --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/pages_sharing @@ -0,0 +1 @@ +255 diff --git a/collector/fixtures/sys/kernel/mm/ksm/pages_to_scan b/collector/fixtures/sys/kernel/mm/ksm/pages_to_scan new file mode 100644 index 0000000000..29d6383b52 --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/pages_to_scan @@ -0,0 +1 @@ +100 diff --git a/collector/fixtures/sys/kernel/mm/ksm/pages_unshared b/collector/fixtures/sys/kernel/mm/ksm/pages_unshared new file mode 100644 index 0000000000..573541ac97 --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/pages_unshared @@ -0,0 +1 @@ +0 diff --git a/collector/fixtures/sys/kernel/mm/ksm/pages_volatile b/collector/fixtures/sys/kernel/mm/ksm/pages_volatile new file mode 100644 index 0000000000..573541ac97 --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/pages_volatile @@ -0,0 +1 @@ +0 diff --git a/collector/fixtures/sys/kernel/mm/ksm/run b/collector/fixtures/sys/kernel/mm/ksm/run new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/run @@ -0,0 +1 @@ +1 diff --git a/collector/fixtures/sys/kernel/mm/ksm/sleep_millisecs b/collector/fixtures/sys/kernel/mm/ksm/sleep_millisecs new file mode 100644 index 0000000000..209e3ef4b6 --- /dev/null +++ b/collector/fixtures/sys/kernel/mm/ksm/sleep_millisecs @@ -0,0 +1 @@ +20 diff --git a/collector/ksmd_linux.go b/collector/ksmd_linux.go new file mode 100644 index 0000000000..198024e9a0 --- /dev/null +++ b/collector/ksmd_linux.go @@ -0,0 +1,83 @@ +// Copyright 2015 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !noksmd + +package collector + +import ( + "fmt" + "path" + + "github.com/prometheus/client_golang/prometheus" +) + +var ( + ksmdFiles = []string{"full_scans", "merge_across_nodes", "pages_shared", "pages_sharing", + "pages_to_scan", "pages_unshared", "pages_volatile", "run", "sleep_millisecs"} +) + +type ksmdCollector struct { + metricDescs map[string]*prometheus.Desc +} + +func init() { + Factories["ksmd"] = NewKsmdCollector +} + +func getCanonicalMetricName(filename string) string { + switch filename { + case "full_scans": + return filename + "_total" + case "sleep_millisecs": + return "sleep_seconds" + default: + return filename + } +} + +// Takes a prometheus registry and returns a new Collector exposing +// kernel/system statistics. +func NewKsmdCollector() (Collector, error) { + subsystem := "ksmd" + descs := make(map[string]*prometheus.Desc) + + for _, n := range ksmdFiles { + descs[n] = prometheus.NewDesc( + prometheus.BuildFQName(Namespace, subsystem, getCanonicalMetricName(n)), + fmt.Sprintf("ksmd '%s' file.", n), nil, nil) + } + return &ksmdCollector{descs}, nil +} + +// Expose kernel and system statistics. +func (c *ksmdCollector) Update(ch chan<- prometheus.Metric) (err error) { + for _, n := range ksmdFiles { + val, err := readUintFromFile(sysFilePath(path.Join("kernel/mm/ksm", n))) + if err != nil { + return err + } + + t := prometheus.GaugeValue + v := float64(val) + switch n { + case "full_scans": + t = prometheus.CounterValue + case "sleep_millisecs": + v /= 1000 + } + ch <- prometheus.MustNewConstMetric(c.metricDescs[n], t, v) + } + + return nil +} diff --git a/end-to-end-test.sh b/end-to-end-test.sh index 605794622f..89f799f692 100755 --- a/end-to-end-test.sh +++ b/end-to-end-test.sh @@ -7,6 +7,7 @@ collectors=$(cat << COLLECTORS diskstats entropy filefd + ksmd loadavg mdadm meminfo