Skip to content
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

Added basic Pdh API implementation #469

Merged
merged 1 commit into from
Jul 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Features
* [#432](https://github.com/twall/jna/pull/432): Added SetLocalTime definition to 'com.sun.jna.platform.win32.Kernel32' - [@lgoldstein](https://github.com/lgoldstein).
* [#434](https://github.com/twall/jna/pull/434): Added GetEnvironmentStrings to 'com.sun.jna.platform.win32.Kernel32' - [@lgoldstein](https://github.com/lgoldstein).
* Loosen OSGI OS name matching to accommodate Windows 8 family - Niels Bertram.
* [#436] (https://github.com/twall/jna/pull/469): Added basic Pdh API implementation to 'com.sun.jna.platform.win32' - [@lgoldstein](https://github.com/lgoldstein).

Bug Fixes
---------
Expand Down
282 changes: 282 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/Pdh.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/* Copyright (c) 2015 Goldstein Lyor, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.win32;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.BaseTSD.DWORD_PTR;
import com.sun.jna.platform.win32.WinBase.FILETIME;
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
import com.sun.jna.platform.win32.WinDef.LONGLONGByReference;
import com.sun.jna.platform.win32.WinNT.HANDLE;
import com.sun.jna.platform.win32.WinNT.HANDLEByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

/**
* Windows Performance Data Helper (a.k.a. PDH).
* @author Lyor Goldstein
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373083(v=vs.85).aspx">Performance Counters</A>
*/
public interface Pdh extends StdCallLibrary {
Pdh INSTANCE = (Pdh) Native.loadLibrary("Pdh",
Pdh.class, W32APIOptions.UNICODE_OPTIONS);


/** Maximum counter name length. */
public static final int PDH_MAX_COUNTER_NAME = 1024;
/** Maximum counter instance name length. */
public static final int PDH_MAX_INSTANCE_NAME = 1024;
/** Maximum full counter path length. */
public static final int PDH_MAX_COUNTER_PATH = 2048;
/** Maximum full counter log name length. */
public static final int PDH_MAX_DATASOURCE_PATH = 1024;

/* TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really expose those too? Crazy :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does - I copy/pasted them from the pdh.h file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a style note "public static" are superfluous on the interface constants.

On Jul 20, 2015, at 7:20 AM, Daniel Doubrovkine (dB.) @dblockdotorg [email protected] wrote:

In contrib/platform/src/com/sun/jna/platform/win32/Pdh.java:

  • */
    +public interface Pdh extends StdCallLibrary {
  • Pdh INSTANCE = (Pdh) Native.loadLibrary("Pdh",
  •        Pdh.class, W32APIOptions.UNICODE_OPTIONS);
    
  • /** Maximum counter name length. */
  • public static final int PDH_MAX_COUNTER_NAME = 1024;
  • /** Maximum counter instance name length. */
  • public static final int PDH_MAX_INSTANCE_NAME = 1024;
  • /** Maximum full counter path length. */
  • public static final int PDH_MAX_COUNTER_PATH = 2048;
  • /** Maximum full counter log name length. */
  • public static final int PDH_MAX_DATASOURCE_PATH = 1024;
  • /* TODO
    Does it really expose those too? Crazy :)


Reply to this email directly or view it on GitHub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, and I usually omit it - but I saw it in other locations in the code so I wasn't sure if it is acceptable to omit them. E.g., WinError drops them, but Advapi32 uses them. BTW, the same can be said for:

      public int RegCloseKey(HKEY hKey);   // 'public' is superfluous in an interface

If it omitting it is acceptable, I will do so in the future - although we should have a consistent style decision on this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I think if it's superfluous it should be removed everywhere, but since it's superfluous it's no big deal ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I prefer to leave them out, but don’t care so much that I’m willing to go back and edit code that doesn’t follow this convention. Please do omit for future work.

T.

On Jul 21, 2015, at 6:54 AM, Daniel Doubrovkine (dB.) @dblockdotorg [email protected] wrote:

In contrib/platform/src/com/sun/jna/platform/win32/Pdh.java:

  • */
    +public interface Pdh extends StdCallLibrary {
  • Pdh INSTANCE = (Pdh) Native.loadLibrary("Pdh",
  •        Pdh.class, W32APIOptions.UNICODE_OPTIONS);
    
  • /** Maximum counter name length. */
  • public static final int PDH_MAX_COUNTER_NAME = 1024;
  • /** Maximum counter instance name length. */
  • public static final int PDH_MAX_INSTANCE_NAME = 1024;
  • /** Maximum full counter path length. */
  • public static final int PDH_MAX_COUNTER_PATH = 2048;
  • /** Maximum full counter log name length. */
  • public static final int PDH_MAX_DATASOURCE_PATH = 1024;
  • /* TODO

FWIW I think if it's superfluous it should be removed everywhere, but since it's superfluous it's no big deal ;)


Reply to this email directly or view it on GitHub.

* LPVOID CALLBACK AllocateMemory(_In_ SIZE_T AllocSize,_In_ LPVOID pContext)
* void CALLBACK FreeMemory(LPVOID pBuffer,LPVOID pContext)
*/

/**
* Connects to the specified computer.
* @param szMachineName The name of the computer to connect to. If
* {@code null}, PDH connects to the local computer.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372578(v=vs.85).aspx">PdhConnectMachine</A>
*/
int PdhConnectMachine(String szMachineName);

// Known values for the PdhGetDllVersion result
public static final int PDH_CVERSION_WIN40 = 0x0400;
public static final int PDH_CVERSION_WIN50 = 0x0500;
// v1.1 revision of PDH -- basic log functions
// v1.2 of the PDH -- adds variable instance counters
// v1.3 of the PDH -- adds log service control & stubs for NT5/PDH v2 fn's
// v2.0 of the PDH -- is the NT v 5.0 B2 version
public static final int PDH_VERSION = PDH_CVERSION_WIN50 + 0x0003;

/**
* Returns the version of the currently installed Pdh.dll file.
* @param lpdwVersion A variable that receives the version of Pdh.dll.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372630(v=vs.85).aspx">PdhGetDllVersion</A>
*/
int PdhGetDllVersion(DWORDByReference lpdwVersion);

/**
* Creates a new query that is used to manage the collection of performance data.
* @param szDataSource The name of the log file from which to retrieve performance data.
* If {@code null}, performance data is collected from a real-time data source.
* @param dwUserData User-defined value to associate with this query.
* @param phQuery (Out) Handle to the query. You use this handle in subsequent calls.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372652(v=vs.85).aspx">PdhOpenQuery</A>
*/
int PdhOpenQuery(String szDataSource, DWORD_PTR dwUserData, HANDLEByReference phQuery);

/**
* Closes all counters contained in the specified query, closes all
* handles related to the query, and frees all memory associated with
* the query.
* @param hQuery Handle to the query to close.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372558(v=vs.85).aspx">PdhCloseQuery</A>
*/
int PdhCloseQuery(HANDLE hQuery);

/**
* Components of a counter path
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373041(v=vs.85).aspx">PDH_COUNTER_PATH_ELEMENTS</A>
* @see <A HREF="https://technet.microsoft.com/en-us/library/cc776490(v=ws.10).aspx">Windows Server 2003 Performance Counters Reference</A>
*/
public class PDH_COUNTER_PATH_ELEMENTS extends Structure {
public String szMachineName;
public String szObjectName;
public String szInstanceName;
public String szParentInstance;
public int dwInstanceIndex;
public String szCounterName;

@Override
protected List<String> getFieldOrder() {
return Arrays.asList("szMachineName", "szObjectName", "szInstanceName",
"szParentInstance", "dwInstanceIndex", "szCounterName");
}
}

// flags for the PdhMakeCounterPath
public static final int PDH_PATH_WBEM_RESULT = 0x00000001;
public static final int PDH_PATH_WBEM_INPUT = 0x00000002;

/**
* Creates a full counter path using the members specified in the
* {@link #PDH_COUNTER_PATH_ELEMENTS} structure.
* @param pCounterPathElements Structure that contains the members
* used to make up the path
* @param szFullPathBuffer Caller-allocated buffer that receives a null-terminated
* counter path. The maximum length of a counter path is PDH_MAX_COUNTER_PATH.
* Set to {@code null} if <tt>pcchBufferSize</tt> is zero.
* @param pcchBufferSize Size of the <tt>szFullPathBuffer</tt> buffer. If
* zero on input, the function returns PDH_MORE_DATA and sets this parameter
* to the required buffer size. If the buffer is larger than the required
* size, the function sets this parameter to the actual size of the buffer
* that was used.
* @param dwFlags Format of the input and output counter values.
* @return ERROR_SUCCESS (or PDH_MORE_DATA)
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372649(v=vs.85).aspx">PdhMakeCounterPath</A>
*/
int PdhMakeCounterPath(PDH_COUNTER_PATH_ELEMENTS pCounterPathElements, char[] szFullPathBuffer, DWORDByReference pcchBufferSize, int dwFlags);

/**
* Adds the specified counter to the query.
* @param hQuery Handle to the query to which you want to add the counter.
* @param szFullCounterPath String that contains the counter path.
* The maximum length of a counter path is {@link #PDH_MAX_COUNTER_PATH}.
* @param dwUserData User-defined value.
* @param phCounter (Out) Handle to the counter that was added to the query.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372204(v=vs.85).aspx">PdhAddCounter</A>
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373193(v=vs.85).aspx">Specifying a Counter Path</A>
*/
int PdhAddCounter(HANDLE hQuery, String szFullCounterPath, DWORD_PTR dwUserData, HANDLEByReference phCounter);
int PdhAddEnglishCounter(HANDLE hQuery, String szFullCounterPath, DWORD_PTR dwUserData, HANDLEByReference phCounter);

/**
* Removes a counter from a query.
* @param hCounter Handle of the counter to remove from its query.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372665(v=vs.85).aspx">PdhRemoveCounter</A>
*/
int PdhRemoveCounter(HANDLE hCounter);

/**
* The data as it was collected from the counter provider. No translation,
* formatting, or other interpretation is performed on the data.
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373060(v=vs.85).aspx">PDH_RAW_COUNTER</A>
*/
public class PDH_RAW_COUNTER extends Structure {
/** Counter status that indicates if the counter value is valid. */
public int CStatus;
/** Local time for when the data was collected */
public FILETIME TimeStamp = new FILETIME();
/** First raw counter value. */
public long FirstValue;
/** Second raw counter value. */
public long SecondValue;
/**
* If the counter type contains the PERF_MULTI_COUNTER flag,
* this member contains the additional counter data used in the
* calculation
*/
public int MultiCount;

@Override
protected List<String> getFieldOrder() {
return Arrays.asList("CStatus", "TimeStamp", "FirstValue", "SecondValue", "MultiCount");
}
}

/**
* @param hCounter Handle of the counter from which to retrieve the current raw value.
* @param lpdwType Receives the counter type - this parameter is optional
* @param pValue The {@link PDH_RAW_COUNTER} structure to receive the data
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372644(v=vs.85).aspx">PdhGetRawCounterValue</A>
*/
int PdhGetRawCounterValue(HANDLE hCounter, DWORDByReference lpdwType, PDH_RAW_COUNTER pValue);

// counter value types
public static final int PDH_FMT_RAW = 0x00000010;
public static final int PDH_FMT_ANSI = 0x00000020;
public static final int PDH_FMT_UNICODE = 0x00000040;
public static final int PDH_FMT_LONG = 0x00000100;
public static final int PDH_FMT_DOUBLE = 0x00000200;
public static final int PDH_FMT_LARGE = 0x00000400;
public static final int PDH_FMT_NOSCALE = 0x00001000;
public static final int PDH_FMT_1000 = 0x00002000;
public static final int PDH_FMT_NODATA = 0x00004000;
public static final int PDH_FMT_NOCAP100 = 0x00008000;
public static final int PERF_DETAIL_COSTLY = 0x00010000;
public static final int PERF_DETAIL_STANDARD = 0x0000FFFF;

/**
* Validates that the counter is present on the computer specified in the counter path.
* @param szFullCounterPath The counter path to validate
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372986(v=vs.85).aspx">PdhValidatePath</A>
*/
int PdhValidatePath(String szFullCounterPath);

/**
* Collects the current raw data value for all counters in the specified
* query and updates the status code of each counter.
* @param hQuery Handle to the query
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372563(v=vs.85).aspx">PdhCollectQueryData</A>
*/
int PdhCollectQueryData(HANDLE hQuery);

/**
* Uses a separate thread to collect the current raw data value for all counters
* in the specified query. The function then signals the application-defined
* event and waits the specified time interval before returning.
* @param hQuery Handle to the query
* @param dwIntervalTime Time interval to wait, in seconds.
* @param hNewDataEvent Handle to the event that you want PDH to signal after
* the time interval expires. To create an event object, call the
* {@link Kernel32#CreateEvent(com.sun.jna.platform.win32.WinBase.SECURITY_ATTRIBUTES, boolean, boolean, String)}
* function
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372566(v=vs.85).aspx">PdhCollectQueryDataEx</A>
*/
int PdhCollectQueryDataEx(HANDLE hQuery, int dwIntervalTime, HANDLE hNewDataEvent);

/**
* Collects the current raw data value for all counters in the specified
* query and updates the status code of each counter.
* @param hQuery Handle to the query
* @param pllTimeStamp Time stamp when the first counter value in the query
* was retrieved. The time is specified as {@link WinBase#FILETIME}.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372569(v=vs.85).aspx">PdhCollectQueryDataWithTime</A>
*/
int PdhCollectQueryDataWithTime(HANDLE hQuery, LONGLONGByReference pllTimeStamp);

/**
* Information on time intervals as applied to the sampling of performance data.
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa373071(v=vs.85).aspx">PDH_TIME_INFO</A>
*/
public class PDH_TIME_INFO extends Structure {
/** Starting time of the sample interval, in local FILETIME format. */
public long StartTime;
/** Ending time of the sample interval, in local FILETIME format. */
public long EndTime;
/** Number of samples collected during the interval. */
public int SampleCount;

protected List<String> getFieldOrder() {
return Arrays.asList("StartTime", "EndTime", "SampleCount");
}
}

/**
* @param hQuery Handle to the query.
* @param pInfo A {@link PDH_TIME_INFO} structure that specifies the time range.
* @return ERROR_SUCCESS if successful
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/aa372677(v=vs.85).aspx">PdhSetQueryTimeRange</A>
*/
int PdhSetQueryTimeRange(HANDLE hQuery, PDH_TIME_INFO pInfo);
}
Loading