1 /** 2 * Oshi (https://github.com/dblock/oshi) 3 * 4 * Copyright (c) 2010 - 2015 The Oshi Project Team 5 * 6 * All rights reserved. This program and the accompanying materials 7 * are made available under the terms of the Eclipse Public License v1.0 8 * which accompanies this distribution, and is available at 9 * http://www.eclipse.org/legal/epl-v10.html 10 * 11 * Contributors: 12 * dblock[at]dblock[dot]org 13 * alessandro[at]perucchi[dot]org 14 * widdis[at]gmail[dot]com 15 * https://github.com/dblock/oshi/graphs/contributors 16 */ 17 package oshi.software.os.windows.nt; 18 19 import java.util.Arrays; 20 import java.util.List; 21 22 import com.sun.jna.Library; 23 import com.sun.jna.Native; 24 import com.sun.jna.Pointer; 25 import com.sun.jna.Structure; 26 import com.sun.jna.Union; 27 import com.sun.jna.WString; 28 import com.sun.jna.ptr.IntByReference; 29 import com.sun.jna.ptr.PointerByReference; 30 31 /** 32 * Windows Performance Data Helper 33 * 34 * @author widdis[at]gmail[dot]com 35 */ 36 public interface Pdh extends Library { 37 Pdh INSTANCE = (Pdh) Native.loadLibrary("Pdh", Pdh.class); 38 39 // Counter return types 40 public static final int PDH_FMT_LONG = 0x00000100; // Native Long 41 public static final int PDH_FMT_DOUBLE = 0x00000200; // double 42 public static final int PDH_FMT_LARGE = 0x00000400; // 64 bit long 43 // These can be combined with above types with bitwise OR 44 public static final int PDH_FMT_NOSCALE = 0x00001000; // don't scale 45 public static final int PDH_FMT_1000 = 0x00002000; // multiply by 1000 46 public static final int PDH_FMT_NOCAP100 = 0x00008000; // don't cap at 100 47 48 /** 49 * Union included in return value of {@link PdhFmtCounterValue} 50 */ 51 public static class ValueUnion extends Union { 52 public int longValue; 53 public double doubleValue; 54 public long largeValue; 55 public String AnsiStringValue; 56 public WString WideStringValue; 57 } 58 59 /** 60 * Holds the return value of a formatted data query. 61 */ 62 public static class PdhFmtCounterValue extends Structure { 63 public int cStatus; 64 public ValueUnion value; 65 66 @Override 67 protected List<String> getFieldOrder() { 68 return Arrays.asList(new String[] { "cStatus", "value" }); 69 } 70 } 71 72 /** 73 * Creates a new query that is used to manage the collection of performance 74 * data. 75 * 76 * @param szDataSource 77 * Null-terminated string that specifies the name of the log file 78 * from which to retrieve performance data. If NULL, performance 79 * data is collected from a real-time data source. 80 * @param dwUserData 81 * User-defined value to associate with this query. 82 * @param phQuery 83 * Handle to the query. You use this handle in subsequent calls. 84 * @return If the function succeeds, the return value is zero. If the 85 * function fails, the return value is a system error code or a PDH 86 * error code. 87 */ 88 int PdhOpenQuery(String szDataSource, IntByReference dwUserData, 89 PointerByReference phQuery); 90 91 /** 92 * Adds the specified language-neutral counter to the query. 93 * 94 * @param pointer 95 * Handle to the query to which you want to add the counter. This 96 * handle is returned by the {@link #PdhOpenQuery()} function. 97 * @param counterPath 98 * Null-terminated string that contains the counter path. 99 * @param dwUserData 100 * User-defined value. 101 * @param phCounter 102 * Handle to the counter that was added to the query. You may 103 * need to reference this handle in subsequent calls. 104 * @return If the function succeeds, the return value is zero. If the 105 * function fails, the return value is a system error code or a PDH 106 * error code. 107 */ 108 int PdhAddEnglishCounterA(Pointer pointer, String counterPath, 109 IntByReference dwUserData, PointerByReference phCounter); 110 111 /** 112 * Collects the current raw data value for all counters in the specified 113 * query and updates the status code of each counter. 114 * 115 * @param pointer 116 * Handle of the query for which you want to collect data. The 117 * {@link #PdhOpenQuery()} function returns this handle. 118 * @return If the function succeeds, the return value is nonzero. If the 119 * function fails, the return value is zero and errno is set. 120 */ 121 int PdhCollectQueryData(Pointer pointer); 122 123 /** 124 * Computes a displayable value for the specified counter. 125 * 126 * @param pointer 127 * Handle of the counter for which you want to compute a 128 * displayable value. The {@link #PdhAddEnglishCounter()} 129 * function returns this handle. 130 * @param dwFormat 131 * Determines the data type of the formatted value. 132 * @param lpdwType 133 * Receives the counter type. This parameter is optional. 134 * @param pValue 135 * A {@link PdhFmtCounterValue} structure that receives the 136 * counter value. 137 * @return If the function succeeds, the return value is zero. If the 138 * function fails, the return value is a system error code or a PDH 139 * error code. 140 */ 141 int PdhGetFormattedCounterValue(Pointer pointer, int dwFormat, 142 IntByReference lpdwType, PdhFmtCounterValue pValue); 143 144 /** 145 * Closes all counters contained in the specified query, closes all handles 146 * related to the query, and frees all memory associated with the query. 147 * 148 * @param pointer 149 * Handle to the query to close. This handle is returned by the 150 * {@link #PdhOpenQuery()} function. 151 * @return If the function succeeds, the return value is zero. If the 152 * function fails, the return value is a system error code or a PDH 153 * error code. 154 */ 155 int PdhCloseQuery(Pointer pointer); 156 }