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