-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cpu/windows] Switch to performance counters (#192)
Add conditional switching to performance counters for `cpu`/`core` metricset collections This PR add an option `WithPerformanceCounter`, which uses performance counter for CPU data collection, when enabled. Default behaviour will be the existing implementation. ### How to use performance counters? It's simple. Something like following: ```go cpu, _ := metrics.New(WithPerformanceCounter()) data, err := cpu.Fetch() ``` ### Why performance counters? From elastic/beats#40926, > On Windows, Metricbeat measures CPU use via the Windows API call GetSystemTimes. Each metrics interval, it fetches the CPU numbers, and compares them to the previous measurement to determine CPU load during that interval. On most systems this includes CPU time ["including all threads in all processes, on all processors"](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes). However, on systems with more than 64 cores, it returns only the data for the [current processor group](https://learn.microsoft.com/en-us/windows/win32/procthread/processor-groups) of up to 64 cores. This is a major limitation for such systems and it leads to inconsistent data. ### Testing Test results are reported on the linked issue. Fixes elastic/beats#40926 ### **Note:** `WithPerformanceCounter` option is only effective for windows and is ineffective if used by other OS'.
- Loading branch information
1 parent
1ac0416
commit 80aacba
Showing
11 changed files
with
142 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you 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. | ||
|
||
//go:build windows | ||
|
||
package cpu | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/elastic/elastic-agent-system-metrics/dev-tools/systemtests" | ||
) | ||
|
||
func TestCounterLength(t *testing.T) { | ||
monitor, err := New(systemtests.DockerTestResolver()) | ||
require.NoError(t, err) | ||
require.NoError(t, monitor.query.CollectData()) | ||
|
||
query := monitor.query | ||
kernelRawData, err := query.GetRawCounterArray(totalKernelTimeCounter, true) | ||
require.NoError(t, err) | ||
|
||
idleRawData, err := query.GetRawCounterArray(totalIdleTimeCounter, true) | ||
require.NoError(t, err) | ||
|
||
userRawData, err := query.GetRawCounterArray(totalUserTimeCounter, true) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, len(kernelRawData), len(idleRawData)) | ||
require.Equal(t, len(userRawData), len(idleRawData)) | ||
|
||
for i := 0; i < len(userRawData); i++ { | ||
require.Equal(t, userRawData[i].InstanceName, kernelRawData[i].InstanceName, "InstanceName should be equal") | ||
} | ||
for i := 0; i < len(kernelRawData); i++ { | ||
require.Equal(t, kernelRawData[i].InstanceName, idleRawData[i].InstanceName, "InstanceName should be equal") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters