Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Performance Data Helper (pdh.dll) |
| 3 | * |
| 4 | * Copyright 2007 Andrey Turkin |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 5 | * Copyright 2007 Hans Leidekker |
Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation; either |
| 10 | * version 2.1 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
| 20 | */ |
| 21 | |
| 22 | #include <stdarg.h> |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 23 | #include <math.h> |
Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 24 | |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 25 | #define NONAMELESSUNION |
| 26 | #define NONAMELESSSTRUCT |
Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 27 | #include "windef.h" |
| 28 | #include "winbase.h" |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 29 | |
| 30 | #include "pdh.h" |
| 31 | #include "pdhmsg.h" |
| 32 | #include "winperf.h" |
| 33 | |
Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 34 | #include "wine/debug.h" |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 35 | #include "wine/list.h" |
| 36 | #include "wine/unicode.h" |
Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 37 | |
| 38 | WINE_DEFAULT_DEBUG_CHANNEL(pdh); |
| 39 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 40 | static CRITICAL_SECTION pdh_handle_cs; |
| 41 | static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug = |
| 42 | { |
| 43 | 0, 0, &pdh_handle_cs, |
| 44 | { &pdh_handle_cs_debug.ProcessLocksList, |
| 45 | &pdh_handle_cs_debug.ProcessLocksList }, |
| 46 | 0, 0, { (DWORD_PTR)(__FILE__ ": pdh_handle_cs") } |
| 47 | }; |
| 48 | static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 }; |
| 49 | |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 50 | static inline void *pdh_alloc( SIZE_T size ) |
| 51 | { |
| 52 | return HeapAlloc( GetProcessHeap(), 0, size ); |
| 53 | } |
| 54 | |
| 55 | static inline void *pdh_alloc_zero( SIZE_T size ) |
| 56 | { |
| 57 | return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size ); |
| 58 | } |
| 59 | |
| 60 | static inline void pdh_free( LPVOID mem ) |
| 61 | { |
| 62 | HeapFree( GetProcessHeap(), 0, mem ); |
| 63 | } |
| 64 | |
| 65 | static inline WCHAR *pdh_strdup( const WCHAR *src ) |
| 66 | { |
| 67 | WCHAR *dst; |
| 68 | |
| 69 | if (!src) return NULL; |
| 70 | if ((dst = pdh_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src ); |
| 71 | return dst; |
| 72 | } |
| 73 | |
| 74 | static inline WCHAR *pdh_strdup_aw( const char *src ) |
| 75 | { |
| 76 | int len; |
| 77 | WCHAR *dst; |
| 78 | |
| 79 | if (!src) return NULL; |
| 80 | len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 ); |
| 81 | if ((dst = pdh_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len ); |
| 82 | return dst; |
| 83 | } |
| 84 | |
Andrey Turkin | 41f15a7 | 2007-06-02 02:28:45 +0400 | [diff] [blame] | 85 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) |
| 86 | { |
| 87 | TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved); |
| 88 | |
| 89 | if (fdwReason == DLL_WINE_PREATTACH) return FALSE; /* prefer native version */ |
| 90 | |
| 91 | if (fdwReason == DLL_PROCESS_ATTACH) |
| 92 | { |
| 93 | DisableThreadLibraryCalls( hinstDLL ); |
| 94 | } |
| 95 | |
| 96 | return TRUE; |
| 97 | } |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 98 | |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 99 | union value |
| 100 | { |
| 101 | LONG longvalue; |
| 102 | double doublevalue; |
| 103 | LONGLONG largevalue; |
| 104 | }; |
| 105 | |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 106 | struct counter |
| 107 | { |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 108 | DWORD magic; /* signature */ |
| 109 | struct list entry; /* list entry */ |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 110 | WCHAR *path; /* identifier */ |
| 111 | DWORD type; /* counter type */ |
| 112 | DWORD status; /* update status */ |
| 113 | LONG scale; /* scale factor */ |
| 114 | LONG defaultscale; /* default scale factor */ |
| 115 | DWORD_PTR user; /* user data */ |
| 116 | DWORD_PTR queryuser; /* query user data */ |
| 117 | LONGLONG base; /* samples per second */ |
| 118 | FILETIME stamp; /* time stamp */ |
| 119 | void (CALLBACK *collect)( struct counter * ); /* collect callback */ |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 120 | union value one; /* first value */ |
| 121 | union value two; /* second value */ |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 124 | #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */ |
| 125 | |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 126 | static struct counter *create_counter( void ) |
| 127 | { |
| 128 | struct counter *counter; |
| 129 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 130 | if ((counter = pdh_alloc_zero( sizeof(struct counter) ))) |
| 131 | { |
| 132 | counter->magic = PDH_MAGIC_COUNTER; |
| 133 | return counter; |
| 134 | } |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 135 | return NULL; |
| 136 | } |
| 137 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 138 | static void destroy_counter( struct counter *counter ) |
| 139 | { |
| 140 | counter->magic = 0; |
| 141 | pdh_free( counter->path ); |
| 142 | pdh_free( counter ); |
| 143 | } |
| 144 | |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 145 | #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */ |
| 146 | |
| 147 | struct query |
| 148 | { |
| 149 | DWORD magic; /* signature */ |
| 150 | DWORD_PTR user; /* user data */ |
Hans Leidekker | 0389ec6 | 2007-09-29 21:06:35 +0200 | [diff] [blame] | 151 | HANDLE thread; /* collect thread */ |
| 152 | DWORD interval; /* collect interval */ |
| 153 | HANDLE wait; /* wait event */ |
| 154 | HANDLE stop; /* stop event */ |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 155 | struct list counters; /* counter list */ |
| 156 | }; |
| 157 | |
| 158 | static struct query *create_query( void ) |
| 159 | { |
| 160 | struct query *query; |
| 161 | |
| 162 | if ((query = pdh_alloc_zero( sizeof(struct query) ))) |
| 163 | { |
| 164 | query->magic = PDH_MAGIC_QUERY; |
| 165 | list_init( &query->counters ); |
| 166 | return query; |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 171 | static void destroy_query( struct query *query ) |
| 172 | { |
| 173 | query->magic = 0; |
| 174 | pdh_free( query ); |
| 175 | } |
| 176 | |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 177 | struct source |
| 178 | { |
Hans Leidekker | 0fbb461 | 2007-07-21 15:08:01 +0200 | [diff] [blame] | 179 | DWORD index; /* name index */ |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 180 | const WCHAR *path; /* identifier */ |
| 181 | void (CALLBACK *collect)( struct counter * ); /* collect callback */ |
| 182 | DWORD type; /* counter type */ |
| 183 | LONG scale; /* default scale factor */ |
| 184 | LONGLONG base; /* samples per second */ |
| 185 | }; |
| 186 | |
Hans Leidekker | d95aa67 | 2007-07-21 15:07:35 +0200 | [diff] [blame] | 187 | static const WCHAR path_processor_time[] = |
| 188 | {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')', |
| 189 | '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0}; |
Hans Leidekker | bdb218a | 2007-07-09 20:54:48 +0200 | [diff] [blame] | 190 | static const WCHAR path_uptime[] = |
| 191 | {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0}; |
| 192 | |
Hans Leidekker | d95aa67 | 2007-07-21 15:07:35 +0200 | [diff] [blame] | 193 | static void CALLBACK collect_processor_time( struct counter *counter ) |
| 194 | { |
| 195 | counter->two.largevalue = 500000; /* FIXME */ |
| 196 | counter->status = PDH_CSTATUS_VALID_DATA; |
| 197 | } |
| 198 | |
Hans Leidekker | bdb218a | 2007-07-09 20:54:48 +0200 | [diff] [blame] | 199 | static void CALLBACK collect_uptime( struct counter *counter ) |
| 200 | { |
Hans Leidekker | e1125c4 | 2007-07-11 22:07:11 +0200 | [diff] [blame] | 201 | counter->two.largevalue = GetTickCount64(); |
Hans Leidekker | bdb218a | 2007-07-09 20:54:48 +0200 | [diff] [blame] | 202 | counter->status = PDH_CSTATUS_VALID_DATA; |
| 203 | } |
| 204 | |
Hans Leidekker | d95aa67 | 2007-07-21 15:07:35 +0200 | [diff] [blame] | 205 | #define TYPE_PROCESSOR_TIME \ |
| 206 | (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \ |
| 207 | PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT) |
| 208 | |
Hans Leidekker | bdb218a | 2007-07-09 20:54:48 +0200 | [diff] [blame] | 209 | #define TYPE_UPTIME \ |
| 210 | (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS) |
| 211 | |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 212 | /* counter source registry */ |
| 213 | static const struct source counter_sources[] = |
| 214 | { |
Hans Leidekker | 0fbb461 | 2007-07-21 15:08:01 +0200 | [diff] [blame] | 215 | { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 }, |
| 216 | { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 } |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
Hans Leidekker | 0fbb461 | 2007-07-21 15:08:01 +0200 | [diff] [blame] | 219 | static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path ) |
| 220 | { |
| 221 | const WCHAR *p; |
| 222 | |
| 223 | if (strchrW( path, '\\')) p = fullpath; |
| 224 | else p = strrchrW( fullpath, '\\' ) + 1; |
| 225 | if (strcmpW( p, path )) return FALSE; |
| 226 | return TRUE; |
| 227 | } |
| 228 | |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 229 | /*********************************************************************** |
| 230 | * PdhAddCounterA (PDH.@) |
| 231 | */ |
| 232 | PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path, |
| 233 | DWORD_PTR userdata, PDH_HCOUNTER *counter ) |
| 234 | { |
| 235 | PDH_STATUS ret; |
| 236 | WCHAR *pathW; |
| 237 | |
| 238 | TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter); |
| 239 | |
| 240 | if (!path) return PDH_INVALID_ARGUMENT; |
| 241 | |
| 242 | if (!(pathW = pdh_strdup_aw( path ))) |
| 243 | return PDH_MEMORY_ALLOCATION_FAILURE; |
| 244 | |
| 245 | ret = PdhAddCounterW( query, pathW, userdata, counter ); |
| 246 | |
| 247 | pdh_free( pathW ); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | /*********************************************************************** |
| 252 | * PdhAddCounterW (PDH.@) |
| 253 | */ |
| 254 | PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path, |
| 255 | DWORD_PTR userdata, PDH_HCOUNTER *hcounter ) |
| 256 | { |
| 257 | struct query *query = hquery; |
| 258 | struct counter *counter; |
| 259 | unsigned int i; |
| 260 | |
| 261 | TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter); |
| 262 | |
| 263 | if (!path || !hcounter) return PDH_INVALID_ARGUMENT; |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 264 | |
| 265 | EnterCriticalSection( &pdh_handle_cs ); |
| 266 | if (!query || query->magic != PDH_MAGIC_QUERY) |
| 267 | { |
| 268 | LeaveCriticalSection( &pdh_handle_cs ); |
| 269 | return PDH_INVALID_HANDLE; |
| 270 | } |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 271 | |
| 272 | *hcounter = NULL; |
| 273 | for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++) |
| 274 | { |
Hans Leidekker | 0fbb461 | 2007-07-21 15:08:01 +0200 | [diff] [blame] | 275 | if (pdh_match_path( counter_sources[i].path, path )) |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 276 | { |
| 277 | if ((counter = create_counter())) |
| 278 | { |
| 279 | counter->path = pdh_strdup( counter_sources[i].path ); |
| 280 | counter->collect = counter_sources[i].collect; |
| 281 | counter->type = counter_sources[i].type; |
| 282 | counter->defaultscale = counter_sources[i].scale; |
| 283 | counter->base = counter_sources[i].base; |
| 284 | counter->queryuser = query->user; |
| 285 | counter->user = userdata; |
| 286 | |
| 287 | list_add_tail( &query->counters, &counter->entry ); |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 288 | *hcounter = counter; |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 289 | |
| 290 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 291 | return ERROR_SUCCESS; |
| 292 | } |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 293 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 294 | return PDH_MEMORY_ALLOCATION_FAILURE; |
| 295 | } |
| 296 | } |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 297 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 298 | return PDH_CSTATUS_NO_COUNTER; |
| 299 | } |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 300 | |
| 301 | /*********************************************************************** |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 302 | * PdhAddEnglishCounterA (PDH.@) |
| 303 | */ |
| 304 | PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path, |
| 305 | DWORD_PTR userdata, PDH_HCOUNTER *counter ) |
| 306 | { |
| 307 | return PdhAddCounterA( query, path, userdata, counter ); |
| 308 | } |
| 309 | |
| 310 | /*********************************************************************** |
| 311 | * PdhAddEnglishCounterW (PDH.@) |
| 312 | */ |
| 313 | PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path, |
| 314 | DWORD_PTR userdata, PDH_HCOUNTER *counter ) |
| 315 | { |
| 316 | return PdhAddCounterW( query, path, userdata, counter ); |
| 317 | } |
| 318 | |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 319 | /* caller must hold counter lock */ |
| 320 | static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1, |
| 321 | union value *raw2, PDH_FMT_COUNTERVALUE *value ) |
| 322 | { |
| 323 | LONG factor; |
| 324 | |
| 325 | factor = counter->scale ? counter->scale : counter->defaultscale; |
| 326 | if (format & PDH_FMT_LONG) |
| 327 | { |
| 328 | if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000; |
| 329 | else value->u.longValue = raw2->longvalue * pow( 10, factor ); |
| 330 | } |
| 331 | else if (format & PDH_FMT_LARGE) |
| 332 | { |
| 333 | if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000; |
| 334 | else value->u.largeValue = raw2->largevalue * pow( 10, factor ); |
| 335 | } |
| 336 | else if (format & PDH_FMT_DOUBLE) |
| 337 | { |
| 338 | if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000; |
| 339 | else value->u.doubleValue = raw2->doublevalue * pow( 10, factor ); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | WARN("unknown format %x\n", format); |
| 344 | return PDH_INVALID_ARGUMENT; |
| 345 | } |
| 346 | return ERROR_SUCCESS; |
| 347 | } |
| 348 | |
| 349 | /*********************************************************************** |
| 350 | * PdhCalculateCounterFromRawValue (PDH.@) |
| 351 | */ |
| 352 | PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format, |
| 353 | PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2, |
| 354 | PPDH_FMT_COUNTERVALUE value ) |
| 355 | { |
| 356 | PDH_STATUS ret; |
| 357 | struct counter *counter = handle; |
| 358 | |
| 359 | TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value); |
| 360 | |
| 361 | if (!value) return PDH_INVALID_ARGUMENT; |
| 362 | |
| 363 | EnterCriticalSection( &pdh_handle_cs ); |
| 364 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 365 | { |
| 366 | LeaveCriticalSection( &pdh_handle_cs ); |
| 367 | return PDH_INVALID_HANDLE; |
| 368 | } |
| 369 | |
| 370 | ret = format_value( counter, format, (union value *)&raw1->SecondValue, |
| 371 | (union value *)&raw2->SecondValue, value ); |
| 372 | |
| 373 | LeaveCriticalSection( &pdh_handle_cs ); |
| 374 | return ret; |
| 375 | } |
| 376 | |
Hans Leidekker | 0389ec6 | 2007-09-29 21:06:35 +0200 | [diff] [blame] | 377 | /* caller must hold query lock */ |
| 378 | static void shutdown_query_thread( struct query *query ) |
| 379 | { |
| 380 | SetEvent( query->stop ); |
| 381 | WaitForSingleObject( query->thread, INFINITE ); |
| 382 | |
| 383 | CloseHandle( query->stop ); |
| 384 | CloseHandle( query->thread ); |
| 385 | |
| 386 | query->thread = NULL; |
| 387 | } |
| 388 | |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 389 | /*********************************************************************** |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 390 | * PdhCloseQuery (PDH.@) |
| 391 | */ |
| 392 | PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle ) |
| 393 | { |
| 394 | struct query *query = handle; |
Hans Leidekker | 75a9146 | 2007-07-21 15:07:50 +0200 | [diff] [blame] | 395 | struct list *item, *next; |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 396 | |
| 397 | TRACE("%p\n", handle); |
| 398 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 399 | EnterCriticalSection( &pdh_handle_cs ); |
| 400 | if (!query || query->magic != PDH_MAGIC_QUERY) |
| 401 | { |
| 402 | LeaveCriticalSection( &pdh_handle_cs ); |
| 403 | return PDH_INVALID_HANDLE; |
| 404 | } |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 405 | |
Hans Leidekker | 0389ec6 | 2007-09-29 21:06:35 +0200 | [diff] [blame] | 406 | if (query->thread) shutdown_query_thread( query ); |
| 407 | |
Hans Leidekker | 75a9146 | 2007-07-21 15:07:50 +0200 | [diff] [blame] | 408 | LIST_FOR_EACH_SAFE( item, next, &query->counters ) |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 409 | { |
| 410 | struct counter *counter = LIST_ENTRY( item, struct counter, entry ); |
| 411 | |
| 412 | list_remove( &counter->entry ); |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 413 | destroy_counter( counter ); |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 416 | destroy_query( query ); |
Hans Leidekker | 75a9146 | 2007-07-21 15:07:50 +0200 | [diff] [blame] | 417 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 418 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 419 | return ERROR_SUCCESS; |
| 420 | } |
| 421 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 422 | /* caller must hold query lock */ |
| 423 | static void collect_query_data( struct query *query ) |
Hans Leidekker | 4727bec | 2007-06-25 21:11:02 +0200 | [diff] [blame] | 424 | { |
Hans Leidekker | 4727bec | 2007-06-25 21:11:02 +0200 | [diff] [blame] | 425 | struct list *item; |
| 426 | |
Hans Leidekker | 4727bec | 2007-06-25 21:11:02 +0200 | [diff] [blame] | 427 | LIST_FOR_EACH( item, &query->counters ) |
| 428 | { |
| 429 | SYSTEMTIME time; |
| 430 | struct counter *counter = LIST_ENTRY( item, struct counter, entry ); |
| 431 | |
| 432 | counter->collect( counter ); |
| 433 | |
| 434 | GetLocalTime( &time ); |
| 435 | SystemTimeToFileTime( &time, &counter->stamp ); |
| 436 | } |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /*********************************************************************** |
| 440 | * PdhCollectQueryData (PDH.@) |
| 441 | */ |
| 442 | PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle ) |
| 443 | { |
| 444 | struct query *query = handle; |
| 445 | |
| 446 | TRACE("%p\n", handle); |
| 447 | |
| 448 | EnterCriticalSection( &pdh_handle_cs ); |
| 449 | if (!query || query->magic != PDH_MAGIC_QUERY) |
| 450 | { |
| 451 | LeaveCriticalSection( &pdh_handle_cs ); |
| 452 | return PDH_INVALID_HANDLE; |
| 453 | } |
| 454 | |
| 455 | if (list_empty( &query->counters )) |
| 456 | { |
| 457 | LeaveCriticalSection( &pdh_handle_cs ); |
| 458 | return PDH_NO_DATA; |
| 459 | } |
| 460 | |
| 461 | collect_query_data( query ); |
| 462 | |
| 463 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 4727bec | 2007-06-25 21:11:02 +0200 | [diff] [blame] | 464 | return ERROR_SUCCESS; |
| 465 | } |
| 466 | |
Hans Leidekker | 0389ec6 | 2007-09-29 21:06:35 +0200 | [diff] [blame] | 467 | static DWORD CALLBACK collect_query_thread( void *arg ) |
| 468 | { |
| 469 | struct query *query = arg; |
| 470 | DWORD interval = query->interval; |
| 471 | HANDLE stop = query->stop; |
| 472 | |
| 473 | SetEvent( stop ); |
| 474 | for (;;) |
| 475 | { |
| 476 | if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 ); |
| 477 | |
| 478 | EnterCriticalSection( &pdh_handle_cs ); |
| 479 | if (!query || query->magic != PDH_MAGIC_QUERY) |
| 480 | { |
| 481 | LeaveCriticalSection( &pdh_handle_cs ); |
| 482 | ExitThread( PDH_INVALID_HANDLE ); |
| 483 | } |
| 484 | |
| 485 | collect_query_data( query ); |
| 486 | |
| 487 | if (!SetEvent( query->wait )) |
| 488 | { |
| 489 | LeaveCriticalSection( &pdh_handle_cs ); |
| 490 | ExitThread( 0 ); |
| 491 | } |
| 492 | LeaveCriticalSection( &pdh_handle_cs ); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /*********************************************************************** |
| 497 | * PdhCollectQueryDataEx (PDH.@) |
| 498 | */ |
| 499 | PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event ) |
| 500 | { |
| 501 | PDH_STATUS ret; |
| 502 | struct query *query = handle; |
| 503 | |
| 504 | TRACE("%p %d %p\n", handle, interval, event); |
| 505 | |
| 506 | EnterCriticalSection( &pdh_handle_cs ); |
| 507 | if (!query || query->magic != PDH_MAGIC_QUERY) |
| 508 | { |
| 509 | LeaveCriticalSection( &pdh_handle_cs ); |
| 510 | return PDH_INVALID_HANDLE; |
| 511 | } |
| 512 | if (list_empty( &query->counters )) |
| 513 | { |
| 514 | LeaveCriticalSection( &pdh_handle_cs ); |
| 515 | return PDH_NO_DATA; |
| 516 | } |
| 517 | if (query->thread) shutdown_query_thread( query ); |
| 518 | if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL ))) |
| 519 | { |
| 520 | ret = GetLastError(); |
| 521 | LeaveCriticalSection( &pdh_handle_cs ); |
| 522 | return ret; |
| 523 | } |
| 524 | query->wait = event; |
| 525 | query->interval = interval * 1000; |
| 526 | if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL ))) |
| 527 | { |
| 528 | ret = GetLastError(); |
| 529 | CloseHandle( query->stop ); |
| 530 | |
| 531 | LeaveCriticalSection( &pdh_handle_cs ); |
| 532 | return ret; |
| 533 | } |
| 534 | WaitForSingleObject( query->stop, INFINITE ); |
| 535 | |
| 536 | LeaveCriticalSection( &pdh_handle_cs ); |
| 537 | return ERROR_SUCCESS; |
| 538 | } |
| 539 | |
Hans Leidekker | 4727bec | 2007-06-25 21:11:02 +0200 | [diff] [blame] | 540 | /*********************************************************************** |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 541 | * PdhCollectQueryDataWithTime (PDH.@) |
| 542 | */ |
| 543 | PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp ) |
| 544 | { |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 545 | struct query *query = handle; |
| 546 | |
| 547 | TRACE("%p %p\n", handle, timestamp); |
| 548 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 549 | EnterCriticalSection( &pdh_handle_cs ); |
| 550 | if (!query || query->magic != PDH_MAGIC_QUERY) |
| 551 | { |
| 552 | LeaveCriticalSection( &pdh_handle_cs ); |
| 553 | return PDH_INVALID_HANDLE; |
| 554 | } |
| 555 | if (list_empty( &query->counters )) |
| 556 | { |
| 557 | LeaveCriticalSection( &pdh_handle_cs ); |
| 558 | return PDH_NO_DATA; |
| 559 | } |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 560 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 561 | collect_query_data( query ); |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 562 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 563 | if (timestamp) |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 564 | { |
| 565 | struct list *item = list_head( &query->counters ); |
| 566 | struct counter *counter = LIST_ENTRY( item, struct counter, entry ); |
| 567 | |
| 568 | *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime; |
| 569 | } |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 570 | LeaveCriticalSection( &pdh_handle_cs ); |
| 571 | return ERROR_SUCCESS; |
Hans Leidekker | 1cec4e3 | 2007-07-21 15:08:15 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /*********************************************************************** |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 575 | * PdhGetCounterInfoA (PDH.@) |
| 576 | */ |
| 577 | PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info ) |
| 578 | { |
| 579 | struct counter *counter = handle; |
| 580 | |
| 581 | TRACE("%p %d %p %p\n", handle, text, size, info); |
| 582 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 583 | EnterCriticalSection( &pdh_handle_cs ); |
| 584 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 585 | { |
| 586 | LeaveCriticalSection( &pdh_handle_cs ); |
| 587 | return PDH_INVALID_HANDLE; |
| 588 | } |
| 589 | if (!size) |
| 590 | { |
| 591 | LeaveCriticalSection( &pdh_handle_cs ); |
| 592 | return PDH_INVALID_ARGUMENT; |
| 593 | } |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 594 | if (*size < sizeof(PDH_COUNTER_INFO_A)) |
| 595 | { |
| 596 | *size = sizeof(PDH_COUNTER_INFO_A); |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 597 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 598 | return PDH_MORE_DATA; |
| 599 | } |
| 600 | |
| 601 | memset( info, 0, sizeof(PDH_COUNTER_INFO_A) ); |
| 602 | |
| 603 | info->dwType = counter->type; |
| 604 | info->CStatus = counter->status; |
| 605 | info->lScale = counter->scale; |
| 606 | info->lDefaultScale = counter->defaultscale; |
| 607 | info->dwUserData = counter->user; |
| 608 | info->dwQueryUserData = counter->queryuser; |
| 609 | |
| 610 | *size = sizeof(PDH_COUNTER_INFO_A); |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 611 | |
| 612 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 613 | return ERROR_SUCCESS; |
| 614 | } |
| 615 | |
| 616 | /*********************************************************************** |
| 617 | * PdhGetCounterInfoW (PDH.@) |
| 618 | */ |
| 619 | PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info ) |
| 620 | { |
| 621 | struct counter *counter = handle; |
| 622 | |
| 623 | TRACE("%p %d %p %p\n", handle, text, size, info); |
| 624 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 625 | EnterCriticalSection( &pdh_handle_cs ); |
| 626 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 627 | { |
| 628 | LeaveCriticalSection( &pdh_handle_cs ); |
| 629 | return PDH_INVALID_HANDLE; |
| 630 | } |
| 631 | if (!size) |
| 632 | { |
| 633 | LeaveCriticalSection( &pdh_handle_cs ); |
| 634 | return PDH_INVALID_ARGUMENT; |
| 635 | } |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 636 | if (*size < sizeof(PDH_COUNTER_INFO_W)) |
| 637 | { |
| 638 | *size = sizeof(PDH_COUNTER_INFO_W); |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 639 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 640 | return PDH_MORE_DATA; |
| 641 | } |
| 642 | |
| 643 | memset( info, 0, sizeof(PDH_COUNTER_INFO_W) ); |
| 644 | |
| 645 | info->dwType = counter->type; |
| 646 | info->CStatus = counter->status; |
| 647 | info->lScale = counter->scale; |
| 648 | info->lDefaultScale = counter->defaultscale; |
| 649 | info->dwUserData = counter->user; |
| 650 | info->dwQueryUserData = counter->queryuser; |
| 651 | |
Hans Leidekker | 75a9146 | 2007-07-21 15:07:50 +0200 | [diff] [blame] | 652 | *size = sizeof(PDH_COUNTER_INFO_W); |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 653 | |
| 654 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 655 | return ERROR_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | /*********************************************************************** |
| 659 | * PdhGetCounterTimeBase (PDH.@) |
| 660 | */ |
| 661 | PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base ) |
| 662 | { |
| 663 | struct counter *counter = handle; |
| 664 | |
| 665 | TRACE("%p %p\n", handle, base); |
| 666 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 667 | if (!base) return PDH_INVALID_ARGUMENT; |
| 668 | |
| 669 | EnterCriticalSection( &pdh_handle_cs ); |
| 670 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 671 | { |
| 672 | LeaveCriticalSection( &pdh_handle_cs ); |
| 673 | return PDH_INVALID_HANDLE; |
| 674 | } |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 675 | |
| 676 | *base = counter->base; |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 677 | |
| 678 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 3ea31cb | 2007-07-09 20:55:33 +0200 | [diff] [blame] | 679 | return ERROR_SUCCESS; |
| 680 | } |
| 681 | |
| 682 | /*********************************************************************** |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 683 | * PdhGetFormattedCounterValue (PDH.@) |
| 684 | */ |
| 685 | PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format, |
| 686 | LPDWORD type, PPDH_FMT_COUNTERVALUE value ) |
| 687 | { |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 688 | PDH_STATUS ret; |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 689 | struct counter *counter = handle; |
| 690 | |
| 691 | TRACE("%p %x %p %p\n", handle, format, type, value); |
| 692 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 693 | if (!value) return PDH_INVALID_ARGUMENT; |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 694 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 695 | EnterCriticalSection( &pdh_handle_cs ); |
| 696 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 697 | { |
| 698 | LeaveCriticalSection( &pdh_handle_cs ); |
| 699 | return PDH_INVALID_HANDLE; |
| 700 | } |
| 701 | if (counter->status) |
| 702 | { |
| 703 | LeaveCriticalSection( &pdh_handle_cs ); |
| 704 | return PDH_INVALID_DATA; |
| 705 | } |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 706 | if (!(ret = format_value( counter, format, &counter->one, &counter->two, value ))) |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 707 | { |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 708 | value->CStatus = ERROR_SUCCESS; |
| 709 | if (type) *type = counter->type; |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 710 | } |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 711 | |
| 712 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | a83509c | 2007-09-29 21:06:49 +0200 | [diff] [blame^] | 713 | return ret; |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /*********************************************************************** |
Hans Leidekker | 754ac8c | 2007-07-09 20:55:25 +0200 | [diff] [blame] | 717 | * PdhGetRawCounterValue (PDH.@) |
| 718 | */ |
| 719 | PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type, |
| 720 | PPDH_RAW_COUNTER value ) |
| 721 | { |
| 722 | struct counter *counter = handle; |
| 723 | |
| 724 | TRACE("%p %p %p\n", handle, type, value); |
| 725 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 726 | if (!value) return PDH_INVALID_ARGUMENT; |
| 727 | |
| 728 | EnterCriticalSection( &pdh_handle_cs ); |
| 729 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 730 | { |
| 731 | LeaveCriticalSection( &pdh_handle_cs ); |
| 732 | return PDH_INVALID_HANDLE; |
| 733 | } |
Hans Leidekker | 754ac8c | 2007-07-09 20:55:25 +0200 | [diff] [blame] | 734 | |
| 735 | value->CStatus = counter->status; |
| 736 | value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime; |
| 737 | value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime; |
Hans Leidekker | e1125c4 | 2007-07-11 22:07:11 +0200 | [diff] [blame] | 738 | value->FirstValue = counter->one.largevalue; |
Hans Leidekker | 754ac8c | 2007-07-09 20:55:25 +0200 | [diff] [blame] | 739 | value->SecondValue = counter->two.largevalue; |
| 740 | value->MultiCount = 1; /* FIXME */ |
| 741 | |
| 742 | if (type) *type = counter->type; |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 743 | |
| 744 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 754ac8c | 2007-07-09 20:55:25 +0200 | [diff] [blame] | 745 | return ERROR_SUCCESS; |
| 746 | } |
| 747 | |
| 748 | /*********************************************************************** |
Hans Leidekker | 0fbb461 | 2007-07-21 15:08:01 +0200 | [diff] [blame] | 749 | * PdhLookupPerfIndexByNameA (PDH.@) |
| 750 | */ |
| 751 | PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index ) |
| 752 | { |
| 753 | PDH_STATUS ret; |
| 754 | WCHAR *nameW; |
| 755 | |
| 756 | TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index); |
| 757 | |
| 758 | if (!name || !index) return PDH_INVALID_ARGUMENT; |
| 759 | |
| 760 | if (machine) |
| 761 | { |
| 762 | FIXME("remote machine not supported\n"); |
| 763 | return PDH_CSTATUS_NO_MACHINE; |
| 764 | } |
| 765 | if (!(nameW = pdh_strdup_aw( name ))) |
| 766 | return PDH_MEMORY_ALLOCATION_FAILURE; |
| 767 | |
| 768 | ret = PdhLookupPerfIndexByNameW( NULL, nameW, index ); |
| 769 | |
| 770 | pdh_free( nameW ); |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | /*********************************************************************** |
| 775 | * PdhLookupPerfIndexByNameW (PDH.@) |
| 776 | */ |
| 777 | PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index ) |
| 778 | { |
| 779 | unsigned int i; |
| 780 | |
| 781 | TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index); |
| 782 | |
| 783 | if (!name || !index) return PDH_INVALID_ARGUMENT; |
| 784 | |
| 785 | if (machine) |
| 786 | { |
| 787 | FIXME("remote machine not supported\n"); |
| 788 | return PDH_CSTATUS_NO_MACHINE; |
| 789 | } |
| 790 | for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++) |
| 791 | { |
| 792 | if (pdh_match_path( counter_sources[i].path, name )) |
| 793 | { |
| 794 | *index = counter_sources[i].index; |
| 795 | return ERROR_SUCCESS; |
| 796 | } |
| 797 | } |
| 798 | return PDH_STRING_NOT_FOUND; |
| 799 | } |
| 800 | |
| 801 | /*********************************************************************** |
| 802 | * PdhLookupPerfNameByIndexA (PDH.@) |
| 803 | */ |
| 804 | PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size ) |
| 805 | { |
| 806 | PDH_STATUS ret; |
| 807 | WCHAR bufferW[PDH_MAX_COUNTER_NAME]; |
| 808 | DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR); |
| 809 | |
| 810 | TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size); |
| 811 | |
| 812 | if (machine) |
| 813 | { |
| 814 | FIXME("remote machine not supported\n"); |
| 815 | return PDH_CSTATUS_NO_MACHINE; |
| 816 | } |
| 817 | |
| 818 | if (!buffer && !size) return PDH_INVALID_ARGUMENT; |
| 819 | if (!index) return ERROR_SUCCESS; |
| 820 | |
| 821 | if (!(ret = PdhLookupPerfNameByIndexW( NULL, index, bufferW, &sizeW ))) |
| 822 | { |
| 823 | int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL ); |
| 824 | |
| 825 | if (size && *size < required) ret = PDH_MORE_DATA; |
| 826 | else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL ); |
| 827 | if (size) *size = required; |
| 828 | } |
| 829 | return ret; |
| 830 | } |
| 831 | |
| 832 | /*********************************************************************** |
| 833 | * PdhLookupPerfNameByIndexW (PDH.@) |
| 834 | */ |
| 835 | PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size ) |
| 836 | { |
| 837 | PDH_STATUS ret; |
| 838 | unsigned int i; |
| 839 | |
| 840 | TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size); |
| 841 | |
| 842 | if (machine) |
| 843 | { |
| 844 | FIXME("remote machine not supported\n"); |
| 845 | return PDH_CSTATUS_NO_MACHINE; |
| 846 | } |
| 847 | |
| 848 | if (!buffer && !size) return PDH_INVALID_ARGUMENT; |
| 849 | if (!index) return ERROR_SUCCESS; |
| 850 | |
| 851 | for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++) |
| 852 | { |
| 853 | if (counter_sources[i].index == index) |
| 854 | { |
| 855 | WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1; |
| 856 | unsigned int required = strlenW( p ) + 1; |
| 857 | |
| 858 | if (*size < required) ret = PDH_MORE_DATA; |
| 859 | else |
| 860 | { |
| 861 | strcpyW( buffer, p ); |
| 862 | ret = ERROR_SUCCESS; |
| 863 | } |
| 864 | *size = required; |
| 865 | return ret; |
| 866 | } |
| 867 | } |
| 868 | return PDH_INVALID_ARGUMENT; |
| 869 | } |
| 870 | |
| 871 | /*********************************************************************** |
Hans Leidekker | 3f1142e | 2007-06-25 21:10:11 +0200 | [diff] [blame] | 872 | * PdhOpenQueryA (PDH.@) |
| 873 | */ |
| 874 | PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query ) |
| 875 | { |
| 876 | PDH_STATUS ret; |
| 877 | WCHAR *sourceW = NULL; |
| 878 | |
| 879 | TRACE("%s %lx %p\n", debugstr_a(source), userdata, query); |
| 880 | |
| 881 | if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE; |
| 882 | |
| 883 | ret = PdhOpenQueryW( sourceW, userdata, query ); |
| 884 | pdh_free( sourceW ); |
| 885 | |
| 886 | return ret; |
| 887 | } |
| 888 | |
| 889 | /*********************************************************************** |
| 890 | * PdhOpenQueryW (PDH.@) |
| 891 | */ |
| 892 | PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle ) |
| 893 | { |
| 894 | struct query *query; |
| 895 | |
| 896 | TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle); |
| 897 | |
| 898 | if (!handle) return PDH_INVALID_ARGUMENT; |
| 899 | |
| 900 | if (source) |
| 901 | { |
| 902 | FIXME("log file data source not supported\n"); |
| 903 | return PDH_INVALID_ARGUMENT; |
| 904 | } |
| 905 | if ((query = create_query())) |
| 906 | { |
| 907 | query->user = userdata; |
| 908 | *handle = query; |
| 909 | |
| 910 | return ERROR_SUCCESS; |
| 911 | } |
| 912 | return PDH_MEMORY_ALLOCATION_FAILURE; |
| 913 | } |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 914 | |
| 915 | /*********************************************************************** |
| 916 | * PdhRemoveCounter (PDH.@) |
| 917 | */ |
| 918 | PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle ) |
| 919 | { |
| 920 | struct counter *counter = handle; |
| 921 | |
| 922 | TRACE("%p\n", handle); |
| 923 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 924 | EnterCriticalSection( &pdh_handle_cs ); |
| 925 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 926 | { |
| 927 | LeaveCriticalSection( &pdh_handle_cs ); |
| 928 | return PDH_INVALID_HANDLE; |
| 929 | } |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 930 | |
| 931 | list_remove( &counter->entry ); |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 932 | destroy_counter( counter ); |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 933 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 934 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 0718c11 | 2007-06-25 21:10:19 +0200 | [diff] [blame] | 935 | return ERROR_SUCCESS; |
| 936 | } |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 937 | |
| 938 | /*********************************************************************** |
| 939 | * PdhSetCounterScaleFactor (PDH.@) |
| 940 | */ |
| 941 | PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor ) |
| 942 | { |
| 943 | struct counter *counter = handle; |
| 944 | |
| 945 | TRACE("%p\n", handle); |
| 946 | |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 947 | EnterCriticalSection( &pdh_handle_cs ); |
| 948 | if (!counter || counter->magic != PDH_MAGIC_COUNTER) |
| 949 | { |
| 950 | LeaveCriticalSection( &pdh_handle_cs ); |
| 951 | return PDH_INVALID_HANDLE; |
| 952 | } |
| 953 | if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE) |
| 954 | { |
| 955 | LeaveCriticalSection( &pdh_handle_cs ); |
| 956 | return PDH_INVALID_ARGUMENT; |
| 957 | } |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 958 | |
| 959 | counter->scale = factor; |
Hans Leidekker | 2f80bc0 | 2007-09-29 21:06:23 +0200 | [diff] [blame] | 960 | |
| 961 | LeaveCriticalSection( &pdh_handle_cs ); |
Hans Leidekker | 0d516c5 | 2007-07-09 20:55:13 +0200 | [diff] [blame] | 962 | return ERROR_SUCCESS; |
| 963 | } |
Hans Leidekker | 504e7c2 | 2007-09-29 21:05:53 +0200 | [diff] [blame] | 964 | |
| 965 | /*********************************************************************** |
| 966 | * PdhValidatePathA (PDH.@) |
| 967 | */ |
| 968 | PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path ) |
| 969 | { |
| 970 | PDH_STATUS ret; |
| 971 | WCHAR *pathW; |
| 972 | |
| 973 | TRACE("%s\n", debugstr_a(path)); |
| 974 | |
| 975 | if (!path) return PDH_INVALID_ARGUMENT; |
| 976 | if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE; |
| 977 | |
| 978 | ret = PdhValidatePathW( pathW ); |
| 979 | |
| 980 | pdh_free( pathW ); |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | static PDH_STATUS validate_path( LPCWSTR path ) |
| 985 | { |
| 986 | if (!path || !*path) return PDH_INVALID_ARGUMENT; |
| 987 | if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME; |
| 988 | return ERROR_SUCCESS; |
| 989 | } |
| 990 | |
| 991 | /*********************************************************************** |
| 992 | * PdhValidatePathW (PDH.@) |
| 993 | */ |
| 994 | PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path ) |
| 995 | { |
| 996 | PDH_STATUS ret; |
| 997 | unsigned int i; |
| 998 | |
| 999 | TRACE("%s\n", debugstr_w(path)); |
| 1000 | |
| 1001 | if ((ret = validate_path( path ))) return ret; |
| 1002 | |
| 1003 | for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++) |
| 1004 | if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS; |
| 1005 | |
| 1006 | return PDH_CSTATUS_NO_COUNTER; |
| 1007 | } |
| 1008 | |
| 1009 | /*********************************************************************** |
| 1010 | * PdhValidatePathExA (PDH.@) |
| 1011 | */ |
| 1012 | PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path ) |
| 1013 | { |
| 1014 | TRACE("%p %s\n", source, debugstr_a(path)); |
| 1015 | |
| 1016 | if (source) |
| 1017 | { |
| 1018 | FIXME("log file data source not supported\n"); |
| 1019 | return ERROR_SUCCESS; |
| 1020 | } |
| 1021 | return PdhValidatePathA( path ); |
| 1022 | } |
| 1023 | |
| 1024 | /*********************************************************************** |
| 1025 | * PdhValidatePathExW (PDH.@) |
| 1026 | */ |
| 1027 | PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path ) |
| 1028 | { |
| 1029 | TRACE("%p %s\n", source, debugstr_w(path)); |
| 1030 | |
| 1031 | if (source) |
| 1032 | { |
| 1033 | FIXME("log file data source not supported\n"); |
| 1034 | return ERROR_SUCCESS; |
| 1035 | } |
| 1036 | return PdhValidatePathW( path ); |
| 1037 | } |