blob: bdf83a94792006b72a309ce9c14d7ad4306f9842 [file] [log] [blame]
Andrey Turkin41f15a72007-06-02 02:28:45 +04001/*
2 * Performance Data Helper (pdh.dll)
3 *
4 * Copyright 2007 Andrey Turkin
Hans Leidekker3ea31cb2007-07-09 20:55:33 +02005 * Copyright 2007 Hans Leidekker
Andrey Turkin41f15a72007-06-02 02:28:45 +04006 *
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 Leidekker0d516c52007-07-09 20:55:13 +020023#include <math.h>
Andrey Turkin41f15a72007-06-02 02:28:45 +040024
Hans Leidekker3f1142e2007-06-25 21:10:11 +020025#define NONAMELESSUNION
26#define NONAMELESSSTRUCT
Andrey Turkin41f15a72007-06-02 02:28:45 +040027#include "windef.h"
28#include "winbase.h"
Hans Leidekker3f1142e2007-06-25 21:10:11 +020029
30#include "pdh.h"
31#include "pdhmsg.h"
32#include "winperf.h"
33
Andrey Turkin41f15a72007-06-02 02:28:45 +040034#include "wine/debug.h"
Hans Leidekker3f1142e2007-06-25 21:10:11 +020035#include "wine/list.h"
36#include "wine/unicode.h"
Andrey Turkin41f15a72007-06-02 02:28:45 +040037
38WINE_DEFAULT_DEBUG_CHANNEL(pdh);
39
Hans Leidekker2f80bc02007-09-29 21:06:23 +020040static CRITICAL_SECTION pdh_handle_cs;
41static 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};
48static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 };
49
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +010050static inline void *heap_alloc( SIZE_T size )
Hans Leidekker3f1142e2007-06-25 21:10:11 +020051{
52 return HeapAlloc( GetProcessHeap(), 0, size );
53}
54
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +010055static inline void *heap_alloc_zero( SIZE_T size )
Hans Leidekker3f1142e2007-06-25 21:10:11 +020056{
57 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
58}
59
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +010060static inline void heap_free( LPVOID mem )
Hans Leidekker3f1142e2007-06-25 21:10:11 +020061{
62 HeapFree( GetProcessHeap(), 0, mem );
63}
64
65static inline WCHAR *pdh_strdup( const WCHAR *src )
66{
67 WCHAR *dst;
68
69 if (!src) return NULL;
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +010070 if ((dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
Hans Leidekker3f1142e2007-06-25 21:10:11 +020071 return dst;
72}
73
74static 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 );
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +010081 if ((dst = heap_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
Hans Leidekker3f1142e2007-06-25 21:10:11 +020082 return dst;
83}
84
Andrey Turkin41f15a72007-06-02 02:28:45 +040085BOOL 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 Leidekker3f1142e2007-06-25 21:10:11 +020098
Hans Leidekkera83509c2007-09-29 21:06:49 +020099union value
100{
101 LONG longvalue;
102 double doublevalue;
103 LONGLONG largevalue;
104};
105
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200106struct counter
107{
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200108 DWORD magic; /* signature */
109 struct list entry; /* list entry */
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200110 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 Leidekkera83509c2007-09-29 21:06:49 +0200120 union value one; /* first value */
121 union value two; /* second value */
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200122};
123
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200124#define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
125
Hans Leidekker0718c112007-06-25 21:10:19 +0200126static struct counter *create_counter( void )
127{
128 struct counter *counter;
129
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100130 if ((counter = heap_alloc_zero( sizeof(struct counter) )))
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200131 {
132 counter->magic = PDH_MAGIC_COUNTER;
133 return counter;
134 }
Hans Leidekker0718c112007-06-25 21:10:19 +0200135 return NULL;
136}
137
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200138static void destroy_counter( struct counter *counter )
139{
140 counter->magic = 0;
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100141 heap_free( counter->path );
142 heap_free( counter );
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200143}
144
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200145#define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
146
147struct query
148{
149 DWORD magic; /* signature */
150 DWORD_PTR user; /* user data */
Hans Leidekker0389ec62007-09-29 21:06:35 +0200151 HANDLE thread; /* collect thread */
152 DWORD interval; /* collect interval */
153 HANDLE wait; /* wait event */
154 HANDLE stop; /* stop event */
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200155 struct list counters; /* counter list */
156};
157
158static struct query *create_query( void )
159{
160 struct query *query;
161
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100162 if ((query = heap_alloc_zero( sizeof(struct query) )))
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200163 {
164 query->magic = PDH_MAGIC_QUERY;
165 list_init( &query->counters );
166 return query;
167 }
168 return NULL;
169}
170
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200171static void destroy_query( struct query *query )
172{
173 query->magic = 0;
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100174 heap_free( query );
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200175}
176
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200177struct source
178{
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200179 DWORD index; /* name index */
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200180 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 Leidekkerd95aa672007-07-21 15:07:35 +0200187static 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 Leidekkerbdb218a2007-07-09 20:54:48 +0200190static 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 Leidekkerd95aa672007-07-21 15:07:35 +0200193static 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 Leidekkerbdb218a2007-07-09 20:54:48 +0200199static void CALLBACK collect_uptime( struct counter *counter )
200{
Hans Leidekkere1125c42007-07-11 22:07:11 +0200201 counter->two.largevalue = GetTickCount64();
Hans Leidekkerbdb218a2007-07-09 20:54:48 +0200202 counter->status = PDH_CSTATUS_VALID_DATA;
203}
204
Hans Leidekkerd95aa672007-07-21 15:07:35 +0200205#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 Leidekkerbdb218a2007-07-09 20:54:48 +0200209#define TYPE_UPTIME \
210 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
211
Hans Leidekker0718c112007-06-25 21:10:19 +0200212/* counter source registry */
213static const struct source counter_sources[] =
214{
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200215 { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
216 { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 }
Hans Leidekker0718c112007-06-25 21:10:19 +0200217};
218
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200219static 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 Leidekker0718c112007-06-25 21:10:19 +0200229/***********************************************************************
230 * PdhAddCounterA (PDH.@)
231 */
232PDH_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
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100247 heap_free( pathW );
Hans Leidekker0718c112007-06-25 21:10:19 +0200248 return ret;
249}
250
251/***********************************************************************
252 * PdhAddCounterW (PDH.@)
253 */
254PDH_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 Leidekker2f80bc02007-09-29 21:06:23 +0200264
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 Leidekker0718c112007-06-25 21:10:19 +0200271
272 *hcounter = NULL;
273 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
274 {
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200275 if (pdh_match_path( counter_sources[i].path, path ))
Hans Leidekker0718c112007-06-25 21:10:19 +0200276 {
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 Leidekker0718c112007-06-25 21:10:19 +0200288 *hcounter = counter;
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200289
290 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker0718c112007-06-25 21:10:19 +0200291 return ERROR_SUCCESS;
292 }
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200293 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker0718c112007-06-25 21:10:19 +0200294 return PDH_MEMORY_ALLOCATION_FAILURE;
295 }
296 }
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200297 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker0718c112007-06-25 21:10:19 +0200298 return PDH_CSTATUS_NO_COUNTER;
299}
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200300
301/***********************************************************************
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200302 * PdhAddEnglishCounterA (PDH.@)
303 */
304PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
305 DWORD_PTR userdata, PDH_HCOUNTER *counter )
306{
Hans Leidekkercf8b16b2007-09-29 21:07:01 +0200307 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
308
309 if (!query) return PDH_INVALID_ARGUMENT;
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200310 return PdhAddCounterA( query, path, userdata, counter );
311}
312
313/***********************************************************************
314 * PdhAddEnglishCounterW (PDH.@)
315 */
316PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
317 DWORD_PTR userdata, PDH_HCOUNTER *counter )
318{
Hans Leidekkercf8b16b2007-09-29 21:07:01 +0200319 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
320
321 if (!query) return PDH_INVALID_ARGUMENT;
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200322 return PdhAddCounterW( query, path, userdata, counter );
323}
324
Hans Leidekkera83509c2007-09-29 21:06:49 +0200325/* caller must hold counter lock */
326static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
327 union value *raw2, PDH_FMT_COUNTERVALUE *value )
328{
329 LONG factor;
330
331 factor = counter->scale ? counter->scale : counter->defaultscale;
332 if (format & PDH_FMT_LONG)
333 {
334 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
335 else value->u.longValue = raw2->longvalue * pow( 10, factor );
336 }
337 else if (format & PDH_FMT_LARGE)
338 {
339 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
340 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
341 }
342 else if (format & PDH_FMT_DOUBLE)
343 {
344 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
345 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
346 }
347 else
348 {
349 WARN("unknown format %x\n", format);
350 return PDH_INVALID_ARGUMENT;
351 }
352 return ERROR_SUCCESS;
353}
354
355/***********************************************************************
356 * PdhCalculateCounterFromRawValue (PDH.@)
357 */
358PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
359 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
360 PPDH_FMT_COUNTERVALUE value )
361{
362 PDH_STATUS ret;
363 struct counter *counter = handle;
364
365 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
366
367 if (!value) return PDH_INVALID_ARGUMENT;
368
369 EnterCriticalSection( &pdh_handle_cs );
370 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
371 {
372 LeaveCriticalSection( &pdh_handle_cs );
373 return PDH_INVALID_HANDLE;
374 }
375
376 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
377 (union value *)&raw2->SecondValue, value );
378
379 LeaveCriticalSection( &pdh_handle_cs );
380 return ret;
381}
382
Hans Leidekker0389ec62007-09-29 21:06:35 +0200383
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200384/***********************************************************************
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200385 * PdhCloseQuery (PDH.@)
386 */
387PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
388{
389 struct query *query = handle;
Hans Leidekker75a91462007-07-21 15:07:50 +0200390 struct list *item, *next;
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200391
392 TRACE("%p\n", handle);
393
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200394 EnterCriticalSection( &pdh_handle_cs );
395 if (!query || query->magic != PDH_MAGIC_QUERY)
396 {
397 LeaveCriticalSection( &pdh_handle_cs );
398 return PDH_INVALID_HANDLE;
399 }
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200400
Alexandre Julliard55d274d2007-10-30 13:04:23 +0100401 if (query->thread)
402 {
403 HANDLE thread = query->thread;
404 SetEvent( query->stop );
405 LeaveCriticalSection( &pdh_handle_cs );
406
407 WaitForSingleObject( thread, INFINITE );
408
409 EnterCriticalSection( &pdh_handle_cs );
Michael Stefaniuc90560492007-10-30 21:10:09 +0100410 if (query->magic != PDH_MAGIC_QUERY)
411 {
412 LeaveCriticalSection( &pdh_handle_cs );
413 return ERROR_SUCCESS;
414 }
Alexandre Julliard55d274d2007-10-30 13:04:23 +0100415 CloseHandle( query->stop );
416 CloseHandle( query->thread );
417 query->thread = NULL;
418 }
Hans Leidekker0389ec62007-09-29 21:06:35 +0200419
Hans Leidekker75a91462007-07-21 15:07:50 +0200420 LIST_FOR_EACH_SAFE( item, next, &query->counters )
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200421 {
422 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
423
424 list_remove( &counter->entry );
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200425 destroy_counter( counter );
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200426 }
427
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200428 destroy_query( query );
Hans Leidekker75a91462007-07-21 15:07:50 +0200429
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200430 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200431 return ERROR_SUCCESS;
432}
433
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200434/* caller must hold query lock */
435static void collect_query_data( struct query *query )
Hans Leidekker4727bec2007-06-25 21:11:02 +0200436{
Hans Leidekker4727bec2007-06-25 21:11:02 +0200437 struct list *item;
438
Hans Leidekker4727bec2007-06-25 21:11:02 +0200439 LIST_FOR_EACH( item, &query->counters )
440 {
441 SYSTEMTIME time;
442 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
443
444 counter->collect( counter );
445
446 GetLocalTime( &time );
447 SystemTimeToFileTime( &time, &counter->stamp );
448 }
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200449}
450
451/***********************************************************************
452 * PdhCollectQueryData (PDH.@)
453 */
454PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
455{
456 struct query *query = handle;
457
458 TRACE("%p\n", handle);
459
460 EnterCriticalSection( &pdh_handle_cs );
461 if (!query || query->magic != PDH_MAGIC_QUERY)
462 {
463 LeaveCriticalSection( &pdh_handle_cs );
464 return PDH_INVALID_HANDLE;
465 }
466
467 if (list_empty( &query->counters ))
468 {
469 LeaveCriticalSection( &pdh_handle_cs );
470 return PDH_NO_DATA;
471 }
472
473 collect_query_data( query );
474
475 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker4727bec2007-06-25 21:11:02 +0200476 return ERROR_SUCCESS;
477}
478
Hans Leidekker0389ec62007-09-29 21:06:35 +0200479static DWORD CALLBACK collect_query_thread( void *arg )
480{
481 struct query *query = arg;
482 DWORD interval = query->interval;
483 HANDLE stop = query->stop;
484
Hans Leidekker0389ec62007-09-29 21:06:35 +0200485 for (;;)
486 {
487 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
488
489 EnterCriticalSection( &pdh_handle_cs );
Marcus Meissnerb5915392007-10-02 11:32:37 +0200490 if (query->magic != PDH_MAGIC_QUERY)
Hans Leidekker0389ec62007-09-29 21:06:35 +0200491 {
492 LeaveCriticalSection( &pdh_handle_cs );
493 ExitThread( PDH_INVALID_HANDLE );
494 }
495
496 collect_query_data( query );
497
498 if (!SetEvent( query->wait ))
499 {
500 LeaveCriticalSection( &pdh_handle_cs );
501 ExitThread( 0 );
502 }
503 LeaveCriticalSection( &pdh_handle_cs );
504 }
505}
506
507/***********************************************************************
508 * PdhCollectQueryDataEx (PDH.@)
509 */
510PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
511{
512 PDH_STATUS ret;
513 struct query *query = handle;
514
515 TRACE("%p %d %p\n", handle, interval, event);
516
517 EnterCriticalSection( &pdh_handle_cs );
518 if (!query || query->magic != PDH_MAGIC_QUERY)
519 {
520 LeaveCriticalSection( &pdh_handle_cs );
521 return PDH_INVALID_HANDLE;
522 }
523 if (list_empty( &query->counters ))
524 {
525 LeaveCriticalSection( &pdh_handle_cs );
526 return PDH_NO_DATA;
527 }
Alexandre Julliard55d274d2007-10-30 13:04:23 +0100528 if (query->thread)
529 {
530 HANDLE thread = query->thread;
531 SetEvent( query->stop );
532 LeaveCriticalSection( &pdh_handle_cs );
533
534 WaitForSingleObject( thread, INFINITE );
535
536 EnterCriticalSection( &pdh_handle_cs );
Michael Stefaniuc90560492007-10-30 21:10:09 +0100537 if (query->magic != PDH_MAGIC_QUERY)
538 {
539 LeaveCriticalSection( &pdh_handle_cs );
540 return PDH_INVALID_HANDLE;
541 }
Alexandre Julliard55d274d2007-10-30 13:04:23 +0100542 CloseHandle( query->thread );
543 query->thread = NULL;
544 }
545 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
Hans Leidekker0389ec62007-09-29 21:06:35 +0200546 {
547 ret = GetLastError();
548 LeaveCriticalSection( &pdh_handle_cs );
549 return ret;
550 }
551 query->wait = event;
552 query->interval = interval * 1000;
553 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
554 {
555 ret = GetLastError();
556 CloseHandle( query->stop );
557
558 LeaveCriticalSection( &pdh_handle_cs );
559 return ret;
560 }
Hans Leidekker0389ec62007-09-29 21:06:35 +0200561
562 LeaveCriticalSection( &pdh_handle_cs );
563 return ERROR_SUCCESS;
564}
565
Hans Leidekker4727bec2007-06-25 21:11:02 +0200566/***********************************************************************
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200567 * PdhCollectQueryDataWithTime (PDH.@)
568 */
569PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
570{
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200571 struct query *query = handle;
Hans Leidekkercf8b16b2007-09-29 21:07:01 +0200572 struct counter *counter;
573 struct list *item;
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200574
575 TRACE("%p %p\n", handle, timestamp);
576
Hans Leidekkercf8b16b2007-09-29 21:07:01 +0200577 if (!timestamp) return PDH_INVALID_ARGUMENT;
578
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200579 EnterCriticalSection( &pdh_handle_cs );
580 if (!query || query->magic != PDH_MAGIC_QUERY)
581 {
582 LeaveCriticalSection( &pdh_handle_cs );
583 return PDH_INVALID_HANDLE;
584 }
585 if (list_empty( &query->counters ))
586 {
587 LeaveCriticalSection( &pdh_handle_cs );
588 return PDH_NO_DATA;
589 }
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200590
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200591 collect_query_data( query );
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200592
Hans Leidekkercf8b16b2007-09-29 21:07:01 +0200593 item = list_head( &query->counters );
594 counter = LIST_ENTRY( item, struct counter, entry );
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200595
Hans Leidekkercf8b16b2007-09-29 21:07:01 +0200596 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
597
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200598 LeaveCriticalSection( &pdh_handle_cs );
599 return ERROR_SUCCESS;
Hans Leidekker1cec4e32007-07-21 15:08:15 +0200600}
601
602/***********************************************************************
Austin English12da91b2009-11-22 12:28:33 -0600603 * PdhExpandWildCardPathA (PDH.@)
604 */
605PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
606{
607 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
608 return PDH_NOT_IMPLEMENTED;
609}
610
611/***********************************************************************
612 * PdhExpandWildCardPathW (PDH.@)
613 */
614PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
615{
616 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
617 return PDH_NOT_IMPLEMENTED;
618}
619
620/***********************************************************************
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200621 * PdhGetCounterInfoA (PDH.@)
622 */
623PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
624{
625 struct counter *counter = handle;
626
627 TRACE("%p %d %p %p\n", handle, text, size, info);
628
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200629 EnterCriticalSection( &pdh_handle_cs );
630 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
631 {
632 LeaveCriticalSection( &pdh_handle_cs );
633 return PDH_INVALID_HANDLE;
634 }
635 if (!size)
636 {
637 LeaveCriticalSection( &pdh_handle_cs );
638 return PDH_INVALID_ARGUMENT;
639 }
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200640 if (*size < sizeof(PDH_COUNTER_INFO_A))
641 {
642 *size = sizeof(PDH_COUNTER_INFO_A);
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200643 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200644 return PDH_MORE_DATA;
645 }
646
647 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
648
649 info->dwType = counter->type;
650 info->CStatus = counter->status;
651 info->lScale = counter->scale;
652 info->lDefaultScale = counter->defaultscale;
653 info->dwUserData = counter->user;
654 info->dwQueryUserData = counter->queryuser;
655
656 *size = sizeof(PDH_COUNTER_INFO_A);
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200657
658 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200659 return ERROR_SUCCESS;
660}
661
662/***********************************************************************
663 * PdhGetCounterInfoW (PDH.@)
664 */
665PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
666{
667 struct counter *counter = handle;
668
669 TRACE("%p %d %p %p\n", handle, text, size, info);
670
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200671 EnterCriticalSection( &pdh_handle_cs );
672 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
673 {
674 LeaveCriticalSection( &pdh_handle_cs );
675 return PDH_INVALID_HANDLE;
676 }
677 if (!size)
678 {
679 LeaveCriticalSection( &pdh_handle_cs );
680 return PDH_INVALID_ARGUMENT;
681 }
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200682 if (*size < sizeof(PDH_COUNTER_INFO_W))
683 {
684 *size = sizeof(PDH_COUNTER_INFO_W);
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200685 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200686 return PDH_MORE_DATA;
687 }
688
689 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
690
691 info->dwType = counter->type;
692 info->CStatus = counter->status;
693 info->lScale = counter->scale;
694 info->lDefaultScale = counter->defaultscale;
695 info->dwUserData = counter->user;
696 info->dwQueryUserData = counter->queryuser;
697
Hans Leidekker75a91462007-07-21 15:07:50 +0200698 *size = sizeof(PDH_COUNTER_INFO_W);
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200699
700 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200701 return ERROR_SUCCESS;
702}
703
704/***********************************************************************
705 * PdhGetCounterTimeBase (PDH.@)
706 */
707PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
708{
709 struct counter *counter = handle;
710
711 TRACE("%p %p\n", handle, base);
712
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200713 if (!base) return PDH_INVALID_ARGUMENT;
714
715 EnterCriticalSection( &pdh_handle_cs );
716 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
717 {
718 LeaveCriticalSection( &pdh_handle_cs );
719 return PDH_INVALID_HANDLE;
720 }
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200721
722 *base = counter->base;
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200723
724 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker3ea31cb2007-07-09 20:55:33 +0200725 return ERROR_SUCCESS;
726}
727
728/***********************************************************************
Andrew Nguyen2da65702010-01-30 18:24:52 -0600729 * PdhGetDllVersion (PDH.@)
730 */
731PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
732{
733 if (!version)
734 return PDH_INVALID_ARGUMENT;
735
736 *version = PDH_VERSION;
737
738 return ERROR_SUCCESS;
739}
740
741/***********************************************************************
Hans Leidekker0d516c52007-07-09 20:55:13 +0200742 * PdhGetFormattedCounterValue (PDH.@)
743 */
744PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
745 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
746{
Hans Leidekkera83509c2007-09-29 21:06:49 +0200747 PDH_STATUS ret;
Hans Leidekker0d516c52007-07-09 20:55:13 +0200748 struct counter *counter = handle;
749
750 TRACE("%p %x %p %p\n", handle, format, type, value);
751
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200752 if (!value) return PDH_INVALID_ARGUMENT;
Hans Leidekker0d516c52007-07-09 20:55:13 +0200753
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200754 EnterCriticalSection( &pdh_handle_cs );
755 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
756 {
757 LeaveCriticalSection( &pdh_handle_cs );
758 return PDH_INVALID_HANDLE;
759 }
760 if (counter->status)
761 {
762 LeaveCriticalSection( &pdh_handle_cs );
763 return PDH_INVALID_DATA;
764 }
Hans Leidekkera83509c2007-09-29 21:06:49 +0200765 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
Hans Leidekker0d516c52007-07-09 20:55:13 +0200766 {
Hans Leidekkera83509c2007-09-29 21:06:49 +0200767 value->CStatus = ERROR_SUCCESS;
768 if (type) *type = counter->type;
Hans Leidekker0d516c52007-07-09 20:55:13 +0200769 }
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200770
771 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekkera83509c2007-09-29 21:06:49 +0200772 return ret;
Hans Leidekker0d516c52007-07-09 20:55:13 +0200773}
774
775/***********************************************************************
Hans Leidekker754ac8c2007-07-09 20:55:25 +0200776 * PdhGetRawCounterValue (PDH.@)
777 */
778PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
779 PPDH_RAW_COUNTER value )
780{
781 struct counter *counter = handle;
782
783 TRACE("%p %p %p\n", handle, type, value);
784
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200785 if (!value) return PDH_INVALID_ARGUMENT;
786
787 EnterCriticalSection( &pdh_handle_cs );
788 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
789 {
790 LeaveCriticalSection( &pdh_handle_cs );
791 return PDH_INVALID_HANDLE;
792 }
Hans Leidekker754ac8c2007-07-09 20:55:25 +0200793
794 value->CStatus = counter->status;
795 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
796 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
Hans Leidekkere1125c42007-07-11 22:07:11 +0200797 value->FirstValue = counter->one.largevalue;
Hans Leidekker754ac8c2007-07-09 20:55:25 +0200798 value->SecondValue = counter->two.largevalue;
799 value->MultiCount = 1; /* FIXME */
800
801 if (type) *type = counter->type;
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200802
803 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker754ac8c2007-07-09 20:55:25 +0200804 return ERROR_SUCCESS;
805}
806
807/***********************************************************************
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200808 * PdhLookupPerfIndexByNameA (PDH.@)
809 */
810PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
811{
812 PDH_STATUS ret;
Jeff Latimerc0700ee2008-02-19 21:17:19 +1100813 WCHAR *machineW = NULL;
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200814 WCHAR *nameW;
815
816 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
817
Jeff Latimerc0700ee2008-02-19 21:17:19 +1100818 if (!name) return PDH_INVALID_ARGUMENT;
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200819
Jeff Latimerc0700ee2008-02-19 21:17:19 +1100820 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
821
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200822 if (!(nameW = pdh_strdup_aw( name )))
823 return PDH_MEMORY_ALLOCATION_FAILURE;
824
Jeff Latimerc0700ee2008-02-19 21:17:19 +1100825 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200826
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100827 heap_free( nameW );
Jeff Latimerc0700ee2008-02-19 21:17:19 +1100828 heap_free( machineW );
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200829 return ret;
830}
831
832/***********************************************************************
833 * PdhLookupPerfIndexByNameW (PDH.@)
834 */
835PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
836{
837 unsigned int i;
838
839 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
840
841 if (!name || !index) return PDH_INVALID_ARGUMENT;
842
843 if (machine)
844 {
845 FIXME("remote machine not supported\n");
846 return PDH_CSTATUS_NO_MACHINE;
847 }
848 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
849 {
850 if (pdh_match_path( counter_sources[i].path, name ))
851 {
852 *index = counter_sources[i].index;
853 return ERROR_SUCCESS;
854 }
855 }
856 return PDH_STRING_NOT_FOUND;
857}
858
859/***********************************************************************
860 * PdhLookupPerfNameByIndexA (PDH.@)
861 */
862PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
863{
864 PDH_STATUS ret;
Jeff Latimer75adca52008-02-18 20:32:04 +1100865 WCHAR *machineW = NULL;
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200866 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
867 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
868
869 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
870
Jeff Latimer33c386f2008-02-18 20:31:58 +1100871 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200872
Jeff Latimer75adca52008-02-18 20:32:04 +1100873 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
874
875 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200876 {
877 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
878
879 if (size && *size < required) ret = PDH_MORE_DATA;
880 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
881 if (size) *size = required;
882 }
Jeff Latimer75adca52008-02-18 20:32:04 +1100883 heap_free( machineW );
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200884 return ret;
885}
886
887/***********************************************************************
888 * PdhLookupPerfNameByIndexW (PDH.@)
889 */
890PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
891{
892 PDH_STATUS ret;
893 unsigned int i;
894
895 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
896
897 if (machine)
898 {
899 FIXME("remote machine not supported\n");
900 return PDH_CSTATUS_NO_MACHINE;
901 }
902
Marcus Meissner22aed322008-01-26 09:43:10 +0100903 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
Hans Leidekker0fbb4612007-07-21 15:08:01 +0200904 if (!index) return ERROR_SUCCESS;
905
906 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
907 {
908 if (counter_sources[i].index == index)
909 {
910 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
911 unsigned int required = strlenW( p ) + 1;
912
913 if (*size < required) ret = PDH_MORE_DATA;
914 else
915 {
916 strcpyW( buffer, p );
917 ret = ERROR_SUCCESS;
918 }
919 *size = required;
920 return ret;
921 }
922 }
923 return PDH_INVALID_ARGUMENT;
924}
925
926/***********************************************************************
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200927 * PdhOpenQueryA (PDH.@)
928 */
929PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
930{
931 PDH_STATUS ret;
932 WCHAR *sourceW = NULL;
933
934 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
935
936 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
937
938 ret = PdhOpenQueryW( sourceW, userdata, query );
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +0100939 heap_free( sourceW );
Hans Leidekker3f1142e2007-06-25 21:10:11 +0200940
941 return ret;
942}
943
944/***********************************************************************
945 * PdhOpenQueryW (PDH.@)
946 */
947PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
948{
949 struct query *query;
950
951 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
952
953 if (!handle) return PDH_INVALID_ARGUMENT;
954
955 if (source)
956 {
957 FIXME("log file data source not supported\n");
958 return PDH_INVALID_ARGUMENT;
959 }
960 if ((query = create_query()))
961 {
962 query->user = userdata;
963 *handle = query;
964
965 return ERROR_SUCCESS;
966 }
967 return PDH_MEMORY_ALLOCATION_FAILURE;
968}
Hans Leidekker0718c112007-06-25 21:10:19 +0200969
970/***********************************************************************
971 * PdhRemoveCounter (PDH.@)
972 */
973PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
974{
975 struct counter *counter = handle;
976
977 TRACE("%p\n", handle);
978
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200979 EnterCriticalSection( &pdh_handle_cs );
980 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
981 {
982 LeaveCriticalSection( &pdh_handle_cs );
983 return PDH_INVALID_HANDLE;
984 }
Hans Leidekker0718c112007-06-25 21:10:19 +0200985
986 list_remove( &counter->entry );
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200987 destroy_counter( counter );
Hans Leidekker0718c112007-06-25 21:10:19 +0200988
Hans Leidekker2f80bc02007-09-29 21:06:23 +0200989 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker0718c112007-06-25 21:10:19 +0200990 return ERROR_SUCCESS;
991}
Hans Leidekker0d516c52007-07-09 20:55:13 +0200992
993/***********************************************************************
994 * PdhSetCounterScaleFactor (PDH.@)
995 */
996PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
997{
998 struct counter *counter = handle;
999
1000 TRACE("%p\n", handle);
1001
Hans Leidekker2f80bc02007-09-29 21:06:23 +02001002 EnterCriticalSection( &pdh_handle_cs );
1003 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1004 {
1005 LeaveCriticalSection( &pdh_handle_cs );
1006 return PDH_INVALID_HANDLE;
1007 }
1008 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1009 {
1010 LeaveCriticalSection( &pdh_handle_cs );
1011 return PDH_INVALID_ARGUMENT;
1012 }
Hans Leidekker0d516c52007-07-09 20:55:13 +02001013
1014 counter->scale = factor;
Hans Leidekker2f80bc02007-09-29 21:06:23 +02001015
1016 LeaveCriticalSection( &pdh_handle_cs );
Hans Leidekker0d516c52007-07-09 20:55:13 +02001017 return ERROR_SUCCESS;
1018}
Hans Leidekker504e7c22007-09-29 21:05:53 +02001019
1020/***********************************************************************
1021 * PdhValidatePathA (PDH.@)
1022 */
1023PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1024{
1025 PDH_STATUS ret;
1026 WCHAR *pathW;
1027
1028 TRACE("%s\n", debugstr_a(path));
1029
1030 if (!path) return PDH_INVALID_ARGUMENT;
1031 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1032
1033 ret = PdhValidatePathW( pathW );
1034
Michael Stefaniuc2c80b9d2007-12-09 16:30:15 +01001035 heap_free( pathW );
Hans Leidekker504e7c22007-09-29 21:05:53 +02001036 return ret;
1037}
1038
1039static PDH_STATUS validate_path( LPCWSTR path )
1040{
1041 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1042 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1043 return ERROR_SUCCESS;
1044 }
1045
1046/***********************************************************************
1047 * PdhValidatePathW (PDH.@)
1048 */
1049PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1050{
1051 PDH_STATUS ret;
1052 unsigned int i;
1053
1054 TRACE("%s\n", debugstr_w(path));
1055
1056 if ((ret = validate_path( path ))) return ret;
1057
1058 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1059 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1060
1061 return PDH_CSTATUS_NO_COUNTER;
1062}
1063
1064/***********************************************************************
1065 * PdhValidatePathExA (PDH.@)
1066 */
1067PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1068{
1069 TRACE("%p %s\n", source, debugstr_a(path));
1070
1071 if (source)
1072 {
1073 FIXME("log file data source not supported\n");
1074 return ERROR_SUCCESS;
1075 }
1076 return PdhValidatePathA( path );
1077}
1078
1079/***********************************************************************
1080 * PdhValidatePathExW (PDH.@)
1081 */
1082PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1083{
1084 TRACE("%p %s\n", source, debugstr_w(path));
1085
1086 if (source)
1087 {
1088 FIXME("log file data source not supported\n");
1089 return ERROR_SUCCESS;
1090 }
1091 return PdhValidatePathW( path );
1092}
Ricardo Filipecb829762009-01-27 16:39:32 +00001093
1094/***********************************************************************
Hans Leidekker754b97c2009-05-08 11:17:15 +02001095 * PdhMakeCounterPathA (PDH.@)
1096 */
1097PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1098 LPDWORD buflen, DWORD flags )
1099{
1100 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1101 PDH_COUNTER_PATH_ELEMENTS_W eW;
1102 WCHAR *bufferW;
1103 DWORD buflenW;
1104
1105 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1106
1107 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1108
1109 memset( &eW, 0, sizeof(eW) );
1110 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1111 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1112 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1113 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1114 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1115 eW.dwInstanceIndex = e->dwInstanceIndex;
1116
1117 buflenW = 0;
1118 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1119 if (ret == PDH_MORE_DATA)
1120 {
1121 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1122 {
1123 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1124 {
1125 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1126 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1127 else ret = PDH_MORE_DATA;
1128 *buflen = len;
1129 }
1130 heap_free( bufferW );
1131 }
Joris Huizer9e39e8a2009-05-10 21:27:12 +02001132 else
1133 ret = PDH_MEMORY_ALLOCATION_FAILURE;
Hans Leidekker754b97c2009-05-08 11:17:15 +02001134 }
1135
1136done:
1137 heap_free( eW.szMachineName );
1138 heap_free( eW.szObjectName );
1139 heap_free( eW.szInstanceName );
1140 heap_free( eW.szParentInstance );
1141 heap_free( eW.szCounterName );
1142 return ret;
1143}
1144
1145/***********************************************************************
1146 * PdhMakeCounterPathW (PDH.@)
1147 */
1148PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1149 LPDWORD buflen, DWORD flags )
1150{
1151 static const WCHAR bslash[] = {'\\',0};
1152 static const WCHAR fslash[] = {'/',0};
1153 static const WCHAR lparen[] = {'(',0};
1154 static const WCHAR rparen[] = {')',0};
1155 static const WCHAR fmt[] = {'#','%','u',0};
1156
1157 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1158 PDH_STATUS ret = ERROR_SUCCESS;
1159 DWORD len;
1160
1161 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1162
1163 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1164
1165 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1166 return PDH_INVALID_ARGUMENT;
1167
1168 path[0] = 0;
1169 if (e->szMachineName)
1170 {
1171 strcatW(path, bslash);
1172 strcatW(path, bslash);
1173 strcatW(path, e->szMachineName);
1174 }
1175 strcatW(path, bslash);
1176 strcatW(path, e->szObjectName);
1177 if (e->szInstanceName)
1178 {
1179 strcatW(path, lparen);
1180 if (e->szParentInstance)
1181 {
1182 strcatW(path, e->szParentInstance);
1183 strcatW(path, fslash);
1184 }
1185 strcatW(path, e->szInstanceName);
1186 sprintfW(instance, fmt, e->dwInstanceIndex);
1187 strcatW(path, instance);
1188 strcatW(path, rparen);
1189 }
1190 strcatW(path, bslash);
1191 strcatW(path, e->szCounterName);
1192
1193 len = strlenW(path) + 1;
1194 if (*buflen >= len) strcpyW(buffer, path);
1195 else ret = PDH_MORE_DATA;
1196 *buflen = len;
1197 return ret;
1198}
1199
1200/***********************************************************************
Ricardo Filipecb829762009-01-27 16:39:32 +00001201 * PdhEnumObjectItemsA (PDH.@)
1202 */
1203PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1204 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1205 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1206{
1207 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1208 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1209 pcchInstanceListLength, dwDetailLevel, dwFlags);
1210
1211 return PDH_NOT_IMPLEMENTED;
1212}
1213
1214/***********************************************************************
1215 * PdhEnumObjectItemsW (PDH.@)
1216 */
1217PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1218 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1219 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1220{
1221 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1222 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1223 pcchInstanceListLength, dwDetailLevel, dwFlags);
1224
1225 return PDH_NOT_IMPLEMENTED;
1226}
Hans Leidekker960c2082010-01-25 12:25:47 +01001227
1228/***********************************************************************
1229 * PdhSetDefaultRealTimeDataSource (PDH.@)
1230 */
1231PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1232{
1233 FIXME("%u\n", source);
1234 return ERROR_SUCCESS;
1235}