Skip to content

Commit 9f094c2

Browse files
committed
Merge pull request #469 from lgoldstein/pdh-request
Added basic Pdh API implementation
2 parents 882d19f + 3788c5d commit 9f094c2

6 files changed

Lines changed: 1101 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Features
4747
* [#432](https://github.com/twall/jna/pull/432): Added SetLocalTime definition to 'com.sun.jna.platform.win32.Kernel32' - [@lgoldstein](https://github.com/lgoldstein).
4848
* [#434](https://github.com/twall/jna/pull/434): Added GetEnvironmentStrings to 'com.sun.jna.platform.win32.Kernel32' - [@lgoldstein](https://github.com/lgoldstein).
4949
* Loosen OSGI OS name matching to accommodate Windows 8 family - Niels Bertram.
50+
* [#436] (https://github.com/twall/jna/pull/469): Added basic Pdh API implementation to 'com.sun.jna.platform.win32' - [@lgoldstein](https://github.com/lgoldstein).
5051

5152
Bug Fixes
5253
---------
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
2+
*
3+
* This library is free software; you can redistribute it and/or
4+
* modify it under the terms of the GNU Lesser General Public
5+
* License as published by the Free Software Foundation; either
6+
* version 2.1 of the License, or (at your option) any later version.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*/
13+
package com.sun.jna.platform.win32;
14+
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
import com.sun.jna.Native;
19+
import com.sun.jna.Structure;
20+
import com.sun.jna.platform.win32.BaseTSD.DWORD_PTR;
21+
import com.sun.jna.platform.win32.WinBase.FILETIME;
22+
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
23+
import com.sun.jna.platform.win32.WinDef.LONGLONGByReference;
24+
import com.sun.jna.platform.win32.WinNT.HANDLE;
25+
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
26+
import com.sun.jna.win32.StdCallLibrary;
27+
import com.sun.jna.win32.W32APIOptions;
28+
29+
/**
30+
* Windows Performance Data Helper (a.k.a. PDH).
31+
* @author Lyor Goldstein
32+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373083(v=vs.85).aspx">Performance Counters</A>
33+
*/
34+
public interface Pdh extends StdCallLibrary {
35+
Pdh INSTANCE = (Pdh) Native.loadLibrary("Pdh",
36+
Pdh.class, W32APIOptions.UNICODE_OPTIONS);
37+
38+
39+
/** Maximum counter name length. */
40+
public static final int PDH_MAX_COUNTER_NAME = 1024;
41+
/** Maximum counter instance name length. */
42+
public static final int PDH_MAX_INSTANCE_NAME = 1024;
43+
/** Maximum full counter path length. */
44+
public static final int PDH_MAX_COUNTER_PATH = 2048;
45+
/** Maximum full counter log name length. */
46+
public static final int PDH_MAX_DATASOURCE_PATH = 1024;
47+
48+
/* TODO
49+
* LPVOID CALLBACK AllocateMemory(_In_ SIZE_T AllocSize,_In_ LPVOID pContext)
50+
* void CALLBACK FreeMemory(LPVOID pBuffer,LPVOID pContext)
51+
*/
52+
53+
/**
54+
* Connects to the specified computer.
55+
* @param szMachineName The name of the computer to connect to. If
56+
* {@code null}, PDH connects to the local computer.
57+
* @return ERROR_SUCCESS if successful
58+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372578(v=vs.85).aspx">PdhConnectMachine</A>
59+
*/
60+
int PdhConnectMachine(String szMachineName);
61+
62+
// Known values for the PdhGetDllVersion result
63+
public static final int PDH_CVERSION_WIN40 = 0x0400;
64+
public static final int PDH_CVERSION_WIN50 = 0x0500;
65+
// v1.1 revision of PDH -- basic log functions
66+
// v1.2 of the PDH -- adds variable instance counters
67+
// v1.3 of the PDH -- adds log service control & stubs for NT5/PDH v2 fn's
68+
// v2.0 of the PDH -- is the NT v 5.0 B2 version
69+
public static final int PDH_VERSION = PDH_CVERSION_WIN50 + 0x0003;
70+
71+
/**
72+
* Returns the version of the currently installed Pdh.dll file.
73+
* @param lpdwVersion A variable that receives the version of Pdh.dll.
74+
* @return ERROR_SUCCESS if successful
75+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372630(v=vs.85).aspx">PdhGetDllVersion</A>
76+
*/
77+
int PdhGetDllVersion(DWORDByReference lpdwVersion);
78+
79+
/**
80+
* Creates a new query that is used to manage the collection of performance data.
81+
* @param szDataSource The name of the log file from which to retrieve performance data.
82+
* If {@code null}, performance data is collected from a real-time data source.
83+
* @param dwUserData User-defined value to associate with this query.
84+
* @param phQuery (Out) Handle to the query. You use this handle in subsequent calls.
85+
* @return ERROR_SUCCESS if successful
86+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372652(v=vs.85).aspx">PdhOpenQuery</A>
87+
*/
88+
int PdhOpenQuery(String szDataSource, DWORD_PTR dwUserData, HANDLEByReference phQuery);
89+
90+
/**
91+
* Closes all counters contained in the specified query, closes all
92+
* handles related to the query, and frees all memory associated with
93+
* the query.
94+
* @param hQuery Handle to the query to close.
95+
* @return ERROR_SUCCESS if successful
96+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372558(v=vs.85).aspx">PdhCloseQuery</A>
97+
*/
98+
int PdhCloseQuery(HANDLE hQuery);
99+
100+
/**
101+
* Components of a counter path
102+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373041(v=vs.85).aspx">PDH_COUNTER_PATH_ELEMENTS</A>
103+
* @see <A HREF="https://technet.microsoft.com/en-us/library/cc776490(v=ws.10).aspx">Windows Server 2003 Performance Counters Reference</A>
104+
*/
105+
public class PDH_COUNTER_PATH_ELEMENTS extends Structure {
106+
public String szMachineName;
107+
public String szObjectName;
108+
public String szInstanceName;
109+
public String szParentInstance;
110+
public int dwInstanceIndex;
111+
public String szCounterName;
112+
113+
@Override
114+
protected List<String> getFieldOrder() {
115+
return Arrays.asList("szMachineName", "szObjectName", "szInstanceName",
116+
"szParentInstance", "dwInstanceIndex", "szCounterName");
117+
}
118+
}
119+
120+
// flags for the PdhMakeCounterPath
121+
public static final int PDH_PATH_WBEM_RESULT = 0x00000001;
122+
public static final int PDH_PATH_WBEM_INPUT = 0x00000002;
123+
124+
/**
125+
* Creates a full counter path using the members specified in the
126+
* {@link #PDH_COUNTER_PATH_ELEMENTS} structure.
127+
* @param pCounterPathElements Structure that contains the members
128+
* used to make up the path
129+
* @param szFullPathBuffer Caller-allocated buffer that receives a null-terminated
130+
* counter path. The maximum length of a counter path is PDH_MAX_COUNTER_PATH.
131+
* Set to {@code null} if <tt>pcchBufferSize</tt> is zero.
132+
* @param pcchBufferSize Size of the <tt>szFullPathBuffer</tt> buffer. If
133+
* zero on input, the function returns PDH_MORE_DATA and sets this parameter
134+
* to the required buffer size. If the buffer is larger than the required
135+
* size, the function sets this parameter to the actual size of the buffer
136+
* that was used.
137+
* @param dwFlags Format of the input and output counter values.
138+
* @return ERROR_SUCCESS (or PDH_MORE_DATA)
139+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372649(v=vs.85).aspx">PdhMakeCounterPath</A>
140+
*/
141+
int PdhMakeCounterPath(PDH_COUNTER_PATH_ELEMENTS pCounterPathElements, char[] szFullPathBuffer, DWORDByReference pcchBufferSize, int dwFlags);
142+
143+
/**
144+
* Adds the specified counter to the query.
145+
* @param hQuery Handle to the query to which you want to add the counter.
146+
* @param szFullCounterPath String that contains the counter path.
147+
* The maximum length of a counter path is {@link #PDH_MAX_COUNTER_PATH}.
148+
* @param dwUserData User-defined value.
149+
* @param phCounter (Out) Handle to the counter that was added to the query.
150+
* @return ERROR_SUCCESS if successful
151+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372204(v=vs.85).aspx">PdhAddCounter</A>
152+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373193(v=vs.85).aspx">Specifying a Counter Path</A>
153+
*/
154+
int PdhAddCounter(HANDLE hQuery, String szFullCounterPath, DWORD_PTR dwUserData, HANDLEByReference phCounter);
155+
int PdhAddEnglishCounter(HANDLE hQuery, String szFullCounterPath, DWORD_PTR dwUserData, HANDLEByReference phCounter);
156+
157+
/**
158+
* Removes a counter from a query.
159+
* @param hCounter Handle of the counter to remove from its query.
160+
* @return ERROR_SUCCESS if successful
161+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372665(v=vs.85).aspx">PdhRemoveCounter</A>
162+
*/
163+
int PdhRemoveCounter(HANDLE hCounter);
164+
165+
/**
166+
* The data as it was collected from the counter provider. No translation,
167+
* formatting, or other interpretation is performed on the data.
168+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373060(v=vs.85).aspx">PDH_RAW_COUNTER</A>
169+
*/
170+
public class PDH_RAW_COUNTER extends Structure {
171+
/** Counter status that indicates if the counter value is valid. */
172+
public int CStatus;
173+
/** Local time for when the data was collected */
174+
public FILETIME TimeStamp = new FILETIME();
175+
/** First raw counter value. */
176+
public long FirstValue;
177+
/** Second raw counter value. */
178+
public long SecondValue;
179+
/**
180+
* If the counter type contains the PERF_MULTI_COUNTER flag,
181+
* this member contains the additional counter data used in the
182+
* calculation
183+
*/
184+
public int MultiCount;
185+
186+
@Override
187+
protected List<String> getFieldOrder() {
188+
return Arrays.asList("CStatus", "TimeStamp", "FirstValue", "SecondValue", "MultiCount");
189+
}
190+
}
191+
192+
/**
193+
* @param hCounter Handle of the counter from which to retrieve the current raw value.
194+
* @param lpdwType Receives the counter type - this parameter is optional
195+
* @param pValue The {@link PDH_RAW_COUNTER} structure to receive the data
196+
* @return ERROR_SUCCESS if successful
197+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372644(v=vs.85).aspx">PdhGetRawCounterValue</A>
198+
*/
199+
int PdhGetRawCounterValue(HANDLE hCounter, DWORDByReference lpdwType, PDH_RAW_COUNTER pValue);
200+
201+
// counter value types
202+
public static final int PDH_FMT_RAW = 0x00000010;
203+
public static final int PDH_FMT_ANSI = 0x00000020;
204+
public static final int PDH_FMT_UNICODE = 0x00000040;
205+
public static final int PDH_FMT_LONG = 0x00000100;
206+
public static final int PDH_FMT_DOUBLE = 0x00000200;
207+
public static final int PDH_FMT_LARGE = 0x00000400;
208+
public static final int PDH_FMT_NOSCALE = 0x00001000;
209+
public static final int PDH_FMT_1000 = 0x00002000;
210+
public static final int PDH_FMT_NODATA = 0x00004000;
211+
public static final int PDH_FMT_NOCAP100 = 0x00008000;
212+
public static final int PERF_DETAIL_COSTLY = 0x00010000;
213+
public static final int PERF_DETAIL_STANDARD = 0x0000FFFF;
214+
215+
/**
216+
* Validates that the counter is present on the computer specified in the counter path.
217+
* @param szFullCounterPath The counter path to validate
218+
* @return ERROR_SUCCESS if successful
219+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372986(v=vs.85).aspx">PdhValidatePath</A>
220+
*/
221+
int PdhValidatePath(String szFullCounterPath);
222+
223+
/**
224+
* Collects the current raw data value for all counters in the specified
225+
* query and updates the status code of each counter.
226+
* @param hQuery Handle to the query
227+
* @return ERROR_SUCCESS if successful
228+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372563(v=vs.85).aspx">PdhCollectQueryData</A>
229+
*/
230+
int PdhCollectQueryData(HANDLE hQuery);
231+
232+
/**
233+
* Uses a separate thread to collect the current raw data value for all counters
234+
* in the specified query. The function then signals the application-defined
235+
* event and waits the specified time interval before returning.
236+
* @param hQuery Handle to the query
237+
* @param dwIntervalTime Time interval to wait, in seconds.
238+
* @param hNewDataEvent Handle to the event that you want PDH to signal after
239+
* the time interval expires. To create an event object, call the
240+
* {@link Kernel32#CreateEvent(com.sun.jna.platform.win32.WinBase.SECURITY_ATTRIBUTES, boolean, boolean, String)}
241+
* function
242+
* @return ERROR_SUCCESS if successful
243+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372566(v=vs.85).aspx">PdhCollectQueryDataEx</A>
244+
*/
245+
int PdhCollectQueryDataEx(HANDLE hQuery, int dwIntervalTime, HANDLE hNewDataEvent);
246+
247+
/**
248+
* Collects the current raw data value for all counters in the specified
249+
* query and updates the status code of each counter.
250+
* @param hQuery Handle to the query
251+
* @param pllTimeStamp Time stamp when the first counter value in the query
252+
* was retrieved. The time is specified as {@link WinBase#FILETIME}.
253+
* @return ERROR_SUCCESS if successful
254+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372569(v=vs.85).aspx">PdhCollectQueryDataWithTime</A>
255+
*/
256+
int PdhCollectQueryDataWithTime(HANDLE hQuery, LONGLONGByReference pllTimeStamp);
257+
258+
/**
259+
* Information on time intervals as applied to the sampling of performance data.
260+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373071(v=vs.85).aspx">PDH_TIME_INFO</A>
261+
*/
262+
public class PDH_TIME_INFO extends Structure {
263+
/** Starting time of the sample interval, in local FILETIME format. */
264+
public long StartTime;
265+
/** Ending time of the sample interval, in local FILETIME format. */
266+
public long EndTime;
267+
/** Number of samples collected during the interval. */
268+
public int SampleCount;
269+
270+
protected List<String> getFieldOrder() {
271+
return Arrays.asList("StartTime", "EndTime", "SampleCount");
272+
}
273+
}
274+
275+
/**
276+
* @param hQuery Handle to the query.
277+
* @param pInfo A {@link PDH_TIME_INFO} structure that specifies the time range.
278+
* @return ERROR_SUCCESS if successful
279+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372677(v=vs.85).aspx">PdhSetQueryTimeRange</A>
280+
*/
281+
int PdhSetQueryTimeRange(HANDLE hQuery, PDH_TIME_INFO pInfo);
282+
}

0 commit comments

Comments
 (0)