Skip to content

Commit 4a2d1a9

Browse files
authored
[chore][windows/perf] Add methods for fetching raw counter values (#252)
* chore: add PDH helpers * remove println * add arm * skip autogenerated code * chore: add raw counters * expose MatchInstanceName * fix: remve AddCounter
1 parent f3ef8be commit 4a2d1a9

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

helpers/windows/pdh/pdh_query_windows.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (q *Query) AddCounter(counterPath string, instance string, format string, w
8888
var instanceName string
8989
// Extract the instance name from the counterPath.
9090
if instance == "" || wildcard {
91-
instanceName, err = matchInstanceName(counterPath)
91+
instanceName, err = MatchInstanceName(counterPath)
9292
if err != nil {
9393
return err
9494
}
@@ -203,6 +203,17 @@ func (q *Query) GetCountersAndInstances(objectName string) ([]string, []string,
203203
return UTF16ToStringArray(counters), UTF16ToStringArray(instances), nil
204204
}
205205

206+
func (q *Query) GetRawCounterValue(counterName string) (*PdhRawCounter, error) {
207+
if _, ok := q.Counters[counterName]; !ok {
208+
return nil, fmt.Errorf("%s doesn't exist in the map; call AddCounter()", counterName)
209+
}
210+
c, err := PdhGetRawCounterValue(q.Counters[counterName].handle)
211+
if err != nil {
212+
return nil, err
213+
}
214+
return c, nil
215+
}
216+
206217
// ExpandWildCardPath examines local computer and returns those counter paths that match the given counter path which contains wildcard characters.
207218
func (q *Query) ExpandWildCardPath(wildCardPath string) ([]string, error) {
208219
if wildCardPath == "" {
@@ -255,7 +266,7 @@ func (q *Query) Close() error {
255266
}
256267

257268
// matchInstanceName will check first for instance and then for any objects names.
258-
func matchInstanceName(counterPath string) (string, error) {
269+
func MatchInstanceName(counterPath string) (string, error) {
259270
matches := instanceNameRegexp.FindStringSubmatch(counterPath)
260271
if len(matches) == 2 {
261272
return returnLastInstance(matches[1]), nil

helpers/windows/pdh/pdh_query_windows_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,42 +91,42 @@ func TestSuccessfulQuery(t *testing.T) {
9191

9292
func TestMatchInstanceName(t *testing.T) {
9393
query := "\\SQLServer:Databases(*)\\Log File(s) Used Size (KB)"
94-
match, err := matchInstanceName(query)
94+
match, err := MatchInstanceName(query)
9595
assert.NoError(t, err)
9696
assert.Equal(t, match, "*")
9797

9898
query = " \\\\desktop-rfooe09\\per processor network interface card activity(3, microsoft wi-fi directvirtual (gyfyg) adapter #2)\\dpcs queued/sec"
99-
match, err = matchInstanceName(query)
99+
match, err = MatchInstanceName(query)
100100
assert.NoError(t, err)
101101
assert.Equal(t, match, "3, microsoft wi-fi directvirtual (gyfyg) adapter #2")
102102

103103
query = " \\\\desktop-rfooe09\\ (test this scenario) per processor network interface card activity(3, microsoft wi-fi directvirtual (gyfyg) adapter #2)\\dpcs queued/sec"
104-
match, err = matchInstanceName(query)
104+
match, err = MatchInstanceName(query)
105105
assert.NoError(t, err)
106106
assert.Equal(t, match, "3, microsoft wi-fi directvirtual (gyfyg) adapter #2")
107107

108108
query = "\\RAS\\Bytes Received By Disconnected Clients"
109-
match, err = matchInstanceName(query)
109+
match, err = MatchInstanceName(query)
110110
assert.NoError(t, err)
111111
assert.Equal(t, match, "RAS")
112112

113113
query = `\\Process (chrome.exe#4)\\Bytes Received By Disconnected Clients`
114-
match, err = matchInstanceName(query)
114+
match, err = MatchInstanceName(query)
115115
assert.NoError(t, err)
116116
assert.Equal(t, match, "chrome.exe#4")
117117

118118
query = "\\BranchCache\\Local Cache: Cache complete file segments"
119-
match, err = matchInstanceName(query)
119+
match, err = MatchInstanceName(query)
120120
assert.NoError(t, err)
121121
assert.Equal(t, match, "BranchCache")
122122

123123
query = `\Synchronization(*)\Exec. Resource no-Waits AcqShrdStarveExcl/sec`
124-
match, err = matchInstanceName(query)
124+
match, err = MatchInstanceName(query)
125125
assert.NoError(t, err)
126126
assert.Equal(t, match, "*")
127127

128128
query = `\.NET CLR Exceptions(test hellp (dsdsd) #rfsfs #3)\# of Finallys / sec`
129-
match, err = matchInstanceName(query)
129+
match, err = MatchInstanceName(query)
130130
assert.NoError(t, err)
131131
assert.Equal(t, match, "test hellp (dsdsd) #rfsfs #3")
132132
}

helpers/windows/pdh/pdh_windows.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ type PdhCounterValueLong struct {
101101
Pad_cgo_1 [4]byte
102102
}
103103

104+
type PdhRawCounter struct {
105+
CStatus uint32
106+
TimeStamp windows.Filetime
107+
FirstValue int64
108+
SecondValue int64
109+
MultiCount uint32
110+
}
111+
104112
// PdhOpenQuery creates a new query.
105113
func PdhOpenQuery(dataSource string, userData uintptr) (PdhQueryHandle, error) {
106114
var dataSourcePtr *uint16
@@ -198,6 +206,16 @@ func PdhGetFormattedCounterValueLong(counter PdhCounterHandle) (uint32, *PdhCoun
198206
return counterType, &value, nil
199207
}
200208

209+
// PdhGetRawCounterValue returns the raw value of a given counter.
210+
func PdhGetRawCounterValue(counter PdhCounterHandle) (*PdhRawCounter, error) {
211+
var value PdhRawCounter
212+
if err := _PdhGetRawCounter(counter, uintptr(unsafe.Pointer(&value))); err != nil {
213+
return &value, PdhErrno(err.(syscall.Errno))
214+
}
215+
216+
return &value, nil
217+
}
218+
201219
// PdhExpandWildCardPath returns counter paths that match the given counter path.
202220
func PdhExpandWildCardPath(utfPath *uint16) ([]uint16, error) {
203221
var bufferSize uint32

helpers/windows/pdh/zpdh_windows.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)