-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[windows] - Introduce PdhGetRawCounterArrayW #254
Changes from all commits
e56f0c1
fd8d0df
70cb699
22c04ae
d1bdb5e
0d290c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,9 @@ | |
package pdh | ||
|
||
import ( | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"unicode/utf16" | ||
"unsafe" | ||
|
@@ -108,6 +110,31 @@ type PdhRawCounter struct { | |
SecondValue int64 | ||
MultiCount uint32 | ||
} | ||
type pdhRawCounterItem struct { | ||
// Pointer to a null-terminated string that specifies the instance name of the counter. The string is appended to the end of this structure. | ||
SzName *uint16 | ||
//A pdhRawCounter structure that contains the raw counter value of the instance | ||
RawValue PdhRawCounter | ||
} | ||
|
||
type PdhRawCounterItem struct { | ||
InstanceName string | ||
RawValue PdhRawCounter | ||
} | ||
|
||
type RawCounterArray []PdhRawCounterItem | ||
|
||
func (a RawCounterArray) Len() int { | ||
return len(a) | ||
} | ||
|
||
func (a RawCounterArray) Swap(i, j int) { | ||
a[i], a[j] = a[j], a[i] | ||
} | ||
|
||
func (a RawCounterArray) Less(i, j int) bool { | ||
return a[i].InstanceName < a[j].InstanceName | ||
} | ||
|
||
// PdhOpenQuery creates a new query. | ||
func PdhOpenQuery(dataSource string, userData uintptr) (PdhQueryHandle, error) { | ||
|
@@ -207,13 +234,43 @@ func PdhGetFormattedCounterValueLong(counter PdhCounterHandle) (uint32, *PdhCoun | |
} | ||
|
||
// PdhGetRawCounterValue returns the raw value of a given counter. | ||
func PdhGetRawCounterValue(counter PdhCounterHandle) (*PdhRawCounter, error) { | ||
func PdhGetRawCounterValue(counter PdhCounterHandle) (PdhRawCounter, error) { | ||
leehinman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var value PdhRawCounter | ||
if err := _PdhGetRawCounter(counter, uintptr(unsafe.Pointer(&value))); err != nil { | ||
return &value, PdhErrno(err.(syscall.Errno)) | ||
return value, PdhErrno(err.(syscall.Errno)) | ||
} | ||
|
||
return &value, nil | ||
return value, nil | ||
} | ||
|
||
func PdhGetRawCounterArray(counter PdhCounterHandle, filterTotal bool) (RawCounterArray, error) { | ||
var bufferSize, itemCount uint32 | ||
if err := _PdhGetRawCounterArray(counter, &bufferSize, &itemCount, nil); err != nil { | ||
if PdhErrno(err.(syscall.Errno)) != PDH_MORE_DATA { | ||
return nil, PdhErrno(err.(syscall.Errno)) | ||
} | ||
buf := make([]byte, bufferSize) | ||
if err := _PdhGetRawCounterArray(counter, &bufferSize, &itemCount, &buf[0]); err != nil { | ||
return nil, PdhErrno(err.(syscall.Errno)) | ||
} | ||
items := unsafe.Slice((*pdhRawCounterItem)(unsafe.Pointer(&buf[0])), itemCount) | ||
ret := make([]PdhRawCounterItem, 0, len(items)) | ||
for _, item := range items { | ||
instance := windows.UTF16PtrToString(item.SzName) | ||
if filterTotal && strings.Contains(instance, "_Total") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to worry if this is a non-English Windows system? Just wondering if we might have something besides "_Total". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For instance name? Nope. The name remains For counter name? yes, the language makes a lot of change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For this specific piece of code? Nope. |
||
continue | ||
} | ||
ret = append(ret, PdhRawCounterItem{ | ||
RawValue: item.RawValue, | ||
InstanceName: instance, | ||
}) | ||
} | ||
// we sort the array by the instance name to ensure that each index in the final array corresponds to a specific core | ||
// This is important because we will be collecting three different types of counters, and sorting ensures that each index in each counter aligns with the correct core. | ||
sort.Sort(RawCounterArray(ret)) | ||
return ret, nil | ||
} | ||
return nil, PdhErrno(syscall.ERROR_NOT_FOUND) | ||
} | ||
|
||
// PdhExpandWildCardPath returns counter paths that match the given counter path. | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leehinman These are two different API calls.
PdhAddCounter is language specific.
PdhAddEnglishCounter is language neutral.