| 1 |
// Copyright 2019 The Prometheus Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package prometheus |
| 15 |
|
| 16 |
import ( |
| 17 |
"syscall" |
| 18 |
"unsafe" |
| 19 |
|
| 20 |
"golang.org/x/sys/windows" |
| 21 |
) |
| 22 |
|
| 23 |
func canCollectProcess() bool { |
| 24 |
return true |
| 25 |
} |
| 26 |
|
| 27 |
var ( |
| 28 |
modpsapi = syscall.NewLazyDLL("psapi.dll") |
| 29 |
modkernel32 = syscall.NewLazyDLL("kernel32.dll") |
| 30 |
|
| 31 |
procGetProcessMemoryInfo = modpsapi.NewProc("GetProcessMemoryInfo") |
| 32 |
procGetProcessHandleCount = modkernel32.NewProc("GetProcessHandleCount") |
| 33 |
) |
| 34 |
|
| 35 |
type processMemoryCounters struct { |
| 36 |
// System interface description |
| 37 |
// https://docs.microsoft.com/en-us/windows/desktop/api/psapi/ns-psapi-process_memory_counters_ex |
| 38 |
|
| 39 |
// Refer to the Golang internal implementation |
| 40 |
// https://golang.org/src/internal/syscall/windows/psapi_windows.go |
| 41 |
_ uint32 |
| 42 |
PageFaultCount uint32 |
| 43 |
PeakWorkingSetSize uintptr |
| 44 |
WorkingSetSize uintptr |
| 45 |
QuotaPeakPagedPoolUsage uintptr |
| 46 |
QuotaPagedPoolUsage uintptr |
| 47 |
QuotaPeakNonPagedPoolUsage uintptr |
| 48 |
QuotaNonPagedPoolUsage uintptr |
| 49 |
PagefileUsage uintptr |
| 50 |
PeakPagefileUsage uintptr |
| 51 |
PrivateUsage uintptr |
| 52 |
} |
| 53 |
|
| 54 |
func getProcessMemoryInfo(handle windows.Handle) (processMemoryCounters, error) { |
| 55 |
mem := processMemoryCounters{} |
| 56 |
r1, _, err := procGetProcessMemoryInfo.Call( |
| 57 |
uintptr(handle), |
| 58 |
uintptr(unsafe.Pointer(&mem)), |
| 59 |
uintptr(unsafe.Sizeof(mem)), |
| 60 |
) |
| 61 |
if r1 != 1 { |
| 62 |
return mem, err |
| 63 |
} else { |
| 64 |
return mem, nil |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func getProcessHandleCount(handle windows.Handle) (uint32, error) { |
| 69 |
var count uint32 |
| 70 |
r1, _, err := procGetProcessHandleCount.Call( |
| 71 |
uintptr(handle), |
| 72 |
uintptr(unsafe.Pointer(&count)), |
| 73 |
) |
| 74 |
if r1 != 1 { |
| 75 |
return 0, err |
| 76 |
} else { |
| 77 |
return count, nil |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func (c *processCollector) processCollect(ch chan<- Metric) { |
| 82 |
h := windows.CurrentProcess() |
| 83 |
|
| 84 |
var startTime, exitTime, kernelTime, userTime windows.Filetime |
| 85 |
err := windows.GetProcessTimes(h, &startTime, &exitTime, &kernelTime, &userTime) |
| 86 |
if err != nil { |
| 87 |
c.reportError(ch, nil, err) |
| 88 |
return |
| 89 |
} |
| 90 |
ch <- MustNewConstMetric(c.startTime, GaugeValue, float64(startTime.Nanoseconds()/1e9)) |
| 91 |
ch <- MustNewConstMetric(c.cpuTotal, CounterValue, fileTimeToSeconds(kernelTime)+fileTimeToSeconds(userTime)) |
| 92 |
|
| 93 |
mem, err := getProcessMemoryInfo(h) |
| 94 |
if err != nil { |
| 95 |
c.reportError(ch, nil, err) |
| 96 |
return |
| 97 |
} |
| 98 |
ch <- MustNewConstMetric(c.vsize, GaugeValue, float64(mem.PrivateUsage)) |
| 99 |
ch <- MustNewConstMetric(c.rss, GaugeValue, float64(mem.WorkingSetSize)) |
| 100 |
|
| 101 |
handles, err := getProcessHandleCount(h) |
| 102 |
if err != nil { |
| 103 |
c.reportError(ch, nil, err) |
| 104 |
return |
| 105 |
} |
| 106 |
ch <- MustNewConstMetric(c.openFDs, GaugeValue, float64(handles)) |
| 107 |
ch <- MustNewConstMetric(c.maxFDs, GaugeValue, float64(16*1024*1024)) // Windows has a hard-coded max limit, not per-process. |
| 108 |
} |
| 109 |
|
| 110 |
// describe returns all descriptions of the collector for windows. |
| 111 |
// Ensure that this list of descriptors is kept in sync with the metrics collected |
| 112 |
// in the processCollect method. Any changes to the metrics in processCollect |
| 113 |
// (such as adding or removing metrics) should be reflected in this list of descriptors. |
| 114 |
func (c *processCollector) describe(ch chan<- *Desc) { |
| 115 |
ch <- c.cpuTotal |
| 116 |
ch <- c.openFDs |
| 117 |
ch <- c.maxFDs |
| 118 |
ch <- c.vsize |
| 119 |
ch <- c.rss |
| 120 |
ch <- c.startTime |
| 121 |
} |
| 122 |
|
| 123 |
func fileTimeToSeconds(ft windows.Filetime) float64 { |
| 124 |
return float64(uint64(ft.HighDateTime)<<32+uint64(ft.LowDateTime)) / 1e7 |
| 125 |
} |