Sync with trunk.
[reactos.git] / dll / win32 / 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 #define WIN32_NO_STATUS
23
24 #include <stdarg.h>
25 #include <math.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include <windef.h>
30 #include <winbase.h>
31
32 #include <pdh.h>
33 #include <pdhmsg.h>
34 //#include "winperf.h"
35
36 #include <wine/debug.h>
37 #include <wine/list.h>
38 #include <wine/unicode.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(pdh);
41
42 static CRITICAL_SECTION pdh_handle_cs;
43 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug =
44 {
45 0, 0, &pdh_handle_cs,
46 { &pdh_handle_cs_debug.ProcessLocksList,
47 &pdh_handle_cs_debug.ProcessLocksList },
48 0, 0, { (DWORD_PTR)(__FILE__ ": pdh_handle_cs") }
49 };
50 static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 };
51
52 static inline void *heap_alloc( SIZE_T size )
53 {
54 return HeapAlloc( GetProcessHeap(), 0, size );
55 }
56
57 static inline void *heap_alloc_zero( SIZE_T size )
58 {
59 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
60 }
61
62 static inline void heap_free( LPVOID mem )
63 {
64 HeapFree( GetProcessHeap(), 0, mem );
65 }
66
67 static inline WCHAR *pdh_strdup( const WCHAR *src )
68 {
69 WCHAR *dst;
70
71 if (!src) return NULL;
72 if ((dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
73 return dst;
74 }
75
76 static inline WCHAR *pdh_strdup_aw( const char *src )
77 {
78 int len;
79 WCHAR *dst;
80
81 if (!src) return NULL;
82 len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
83 if ((dst = heap_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
84 return dst;
85 }
86
87 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
88 {
89 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
90
91 if (fdwReason == DLL_WINE_PREATTACH) return FALSE; /* prefer native version */
92
93 if (fdwReason == DLL_PROCESS_ATTACH)
94 {
95 DisableThreadLibraryCalls( hinstDLL );
96 }
97
98 return TRUE;
99 }
100
101 union value
102 {
103 LONG longvalue;
104 double doublevalue;
105 LONGLONG largevalue;
106 };
107
108 struct counter
109 {
110 DWORD magic; /* signature */
111 struct list entry; /* list entry */
112 WCHAR *path; /* identifier */
113 DWORD type; /* counter type */
114 DWORD status; /* update status */
115 LONG scale; /* scale factor */
116 LONG defaultscale; /* default scale factor */
117 DWORD_PTR user; /* user data */
118 DWORD_PTR queryuser; /* query user data */
119 LONGLONG base; /* samples per second */
120 FILETIME stamp; /* time stamp */
121 void (CALLBACK *collect)( struct counter * ); /* collect callback */
122 union value one; /* first value */
123 union value two; /* second value */
124 };
125
126 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
127
128 static struct counter *create_counter( void )
129 {
130 struct counter *counter;
131
132 if ((counter = heap_alloc_zero( sizeof(struct counter) )))
133 {
134 counter->magic = PDH_MAGIC_COUNTER;
135 return counter;
136 }
137 return NULL;
138 }
139
140 static void destroy_counter( struct counter *counter )
141 {
142 counter->magic = 0;
143 heap_free( counter->path );
144 heap_free( counter );
145 }
146
147 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
148
149 struct query
150 {
151 DWORD magic; /* signature */
152 DWORD_PTR user; /* user data */
153 HANDLE thread; /* collect thread */
154 DWORD interval; /* collect interval */
155 HANDLE wait; /* wait event */
156 HANDLE stop; /* stop event */
157 struct list counters; /* counter list */
158 };
159
160 static struct query *create_query( void )
161 {
162 struct query *query;
163
164 if ((query = heap_alloc_zero( sizeof(struct query) )))
165 {
166 query->magic = PDH_MAGIC_QUERY;
167 list_init( &query->counters );
168 return query;
169 }
170 return NULL;
171 }
172
173 static void destroy_query( struct query *query )
174 {
175 query->magic = 0;
176 heap_free( query );
177 }
178
179 struct source
180 {
181 DWORD index; /* name index */
182 const WCHAR *path; /* identifier */
183 void (CALLBACK *collect)( struct counter * ); /* collect callback */
184 DWORD type; /* counter type */
185 LONG scale; /* default scale factor */
186 LONGLONG base; /* samples per second */
187 };
188
189 static const WCHAR path_processor_time[] =
190 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
191 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
192 static const WCHAR path_uptime[] =
193 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
194
195 static void CALLBACK collect_processor_time( struct counter *counter )
196 {
197 counter->two.largevalue = 500000; /* FIXME */
198 counter->status = PDH_CSTATUS_VALID_DATA;
199 }
200
201 static void CALLBACK collect_uptime( struct counter *counter )
202 {
203 counter->two.largevalue = GetTickCount64();
204 counter->status = PDH_CSTATUS_VALID_DATA;
205 }
206
207 #define TYPE_PROCESSOR_TIME \
208 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
209 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
210
211 #define TYPE_UPTIME \
212 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
213
214 /* counter source registry */
215 static const struct source counter_sources[] =
216 {
217 { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
218 { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 }
219 };
220
221 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
222 {
223 const WCHAR *p;
224
225 if (strchrW( path, '\\')) p = fullpath;
226 else p = strrchrW( fullpath, '\\' ) + 1;
227 if (strcmpW( p, path )) return FALSE;
228 return TRUE;
229 }
230
231 /***********************************************************************
232 * PdhAddCounterA (PDH.@)
233 */
234 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
235 DWORD_PTR userdata, PDH_HCOUNTER *counter )
236 {
237 PDH_STATUS ret;
238 WCHAR *pathW;
239
240 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
241
242 if (!path) return PDH_INVALID_ARGUMENT;
243
244 if (!(pathW = pdh_strdup_aw( path )))
245 return PDH_MEMORY_ALLOCATION_FAILURE;
246
247 ret = PdhAddCounterW( query, pathW, userdata, counter );
248
249 heap_free( pathW );
250 return ret;
251 }
252
253 /***********************************************************************
254 * PdhAddCounterW (PDH.@)
255 */
256 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
257 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
258 {
259 struct query *query = hquery;
260 struct counter *counter;
261 unsigned int i;
262
263 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
264
265 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
266
267 EnterCriticalSection( &pdh_handle_cs );
268 if (!query || query->magic != PDH_MAGIC_QUERY)
269 {
270 LeaveCriticalSection( &pdh_handle_cs );
271 return PDH_INVALID_HANDLE;
272 }
273
274 *hcounter = NULL;
275 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
276 {
277 if (pdh_match_path( counter_sources[i].path, path ))
278 {
279 if ((counter = create_counter()))
280 {
281 counter->path = pdh_strdup( counter_sources[i].path );
282 counter->collect = counter_sources[i].collect;
283 counter->type = counter_sources[i].type;
284 counter->defaultscale = counter_sources[i].scale;
285 counter->base = counter_sources[i].base;
286 counter->queryuser = query->user;
287 counter->user = userdata;
288
289 list_add_tail( &query->counters, &counter->entry );
290 *hcounter = counter;
291
292 LeaveCriticalSection( &pdh_handle_cs );
293 return ERROR_SUCCESS;
294 }
295 LeaveCriticalSection( &pdh_handle_cs );
296 return PDH_MEMORY_ALLOCATION_FAILURE;
297 }
298 }
299 LeaveCriticalSection( &pdh_handle_cs );
300 return PDH_CSTATUS_NO_COUNTER;
301 }
302
303 /***********************************************************************
304 * PdhAddEnglishCounterA (PDH.@)
305 */
306 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
307 DWORD_PTR userdata, PDH_HCOUNTER *counter )
308 {
309 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
310
311 if (!query) return PDH_INVALID_ARGUMENT;
312 return PdhAddCounterA( query, path, userdata, counter );
313 }
314
315 /***********************************************************************
316 * PdhAddEnglishCounterW (PDH.@)
317 */
318 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
319 DWORD_PTR userdata, PDH_HCOUNTER *counter )
320 {
321 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
322
323 if (!query) return PDH_INVALID_ARGUMENT;
324 return PdhAddCounterW( query, path, userdata, counter );
325 }
326
327 /* caller must hold counter lock */
328 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
329 union value *raw2, PDH_FMT_COUNTERVALUE *value )
330 {
331 LONG factor;
332
333 factor = counter->scale ? counter->scale : counter->defaultscale;
334 if (format & PDH_FMT_LONG)
335 {
336 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
337 else value->u.longValue = raw2->longvalue * pow( 10, factor );
338 }
339 else if (format & PDH_FMT_LARGE)
340 {
341 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
342 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
343 }
344 else if (format & PDH_FMT_DOUBLE)
345 {
346 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
347 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
348 }
349 else
350 {
351 WARN("unknown format %x\n", format);
352 return PDH_INVALID_ARGUMENT;
353 }
354 return ERROR_SUCCESS;
355 }
356
357 /***********************************************************************
358 * PdhCalculateCounterFromRawValue (PDH.@)
359 */
360 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
361 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
362 PPDH_FMT_COUNTERVALUE value )
363 {
364 PDH_STATUS ret;
365 struct counter *counter = handle;
366
367 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
368
369 if (!value) return PDH_INVALID_ARGUMENT;
370
371 EnterCriticalSection( &pdh_handle_cs );
372 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
373 {
374 LeaveCriticalSection( &pdh_handle_cs );
375 return PDH_INVALID_HANDLE;
376 }
377
378 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
379 (union value *)&raw2->SecondValue, value );
380
381 LeaveCriticalSection( &pdh_handle_cs );
382 return ret;
383 }
384
385
386 /***********************************************************************
387 * PdhCloseQuery (PDH.@)
388 */
389 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
390 {
391 struct query *query = handle;
392 struct list *item, *next;
393
394 TRACE("%p\n", handle);
395
396 EnterCriticalSection( &pdh_handle_cs );
397 if (!query || query->magic != PDH_MAGIC_QUERY)
398 {
399 LeaveCriticalSection( &pdh_handle_cs );
400 return PDH_INVALID_HANDLE;
401 }
402
403 if (query->thread)
404 {
405 HANDLE thread = query->thread;
406 SetEvent( query->stop );
407 LeaveCriticalSection( &pdh_handle_cs );
408
409 WaitForSingleObject( thread, INFINITE );
410
411 EnterCriticalSection( &pdh_handle_cs );
412 if (query->magic != PDH_MAGIC_QUERY)
413 {
414 LeaveCriticalSection( &pdh_handle_cs );
415 return ERROR_SUCCESS;
416 }
417 CloseHandle( query->stop );
418 CloseHandle( query->thread );
419 query->thread = NULL;
420 }
421
422 LIST_FOR_EACH_SAFE( item, next, &query->counters )
423 {
424 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
425
426 list_remove( &counter->entry );
427 destroy_counter( counter );
428 }
429
430 destroy_query( query );
431
432 LeaveCriticalSection( &pdh_handle_cs );
433 return ERROR_SUCCESS;
434 }
435
436 /* caller must hold query lock */
437 static void collect_query_data( struct query *query )
438 {
439 struct list *item;
440
441 LIST_FOR_EACH( item, &query->counters )
442 {
443 SYSTEMTIME time;
444 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
445
446 counter->collect( counter );
447
448 GetLocalTime( &time );
449 SystemTimeToFileTime( &time, &counter->stamp );
450 }
451 }
452
453 /***********************************************************************
454 * PdhCollectQueryData (PDH.@)
455 */
456 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
457 {
458 struct query *query = handle;
459
460 TRACE("%p\n", handle);
461
462 EnterCriticalSection( &pdh_handle_cs );
463 if (!query || query->magic != PDH_MAGIC_QUERY)
464 {
465 LeaveCriticalSection( &pdh_handle_cs );
466 return PDH_INVALID_HANDLE;
467 }
468
469 if (list_empty( &query->counters ))
470 {
471 LeaveCriticalSection( &pdh_handle_cs );
472 return PDH_NO_DATA;
473 }
474
475 collect_query_data( query );
476
477 LeaveCriticalSection( &pdh_handle_cs );
478 return ERROR_SUCCESS;
479 }
480
481 static DWORD CALLBACK collect_query_thread( void *arg )
482 {
483 struct query *query = arg;
484 DWORD interval = query->interval;
485 HANDLE stop = query->stop;
486
487 for (;;)
488 {
489 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
490
491 EnterCriticalSection( &pdh_handle_cs );
492 if (query->magic != PDH_MAGIC_QUERY)
493 {
494 LeaveCriticalSection( &pdh_handle_cs );
495 ExitThread( PDH_INVALID_HANDLE );
496 }
497
498 collect_query_data( query );
499
500 if (!SetEvent( query->wait ))
501 {
502 LeaveCriticalSection( &pdh_handle_cs );
503 ExitThread( 0 );
504 }
505 LeaveCriticalSection( &pdh_handle_cs );
506 }
507 }
508
509 /***********************************************************************
510 * PdhCollectQueryDataEx (PDH.@)
511 */
512 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
513 {
514 PDH_STATUS ret;
515 struct query *query = handle;
516
517 TRACE("%p %d %p\n", handle, interval, event);
518
519 EnterCriticalSection( &pdh_handle_cs );
520 if (!query || query->magic != PDH_MAGIC_QUERY)
521 {
522 LeaveCriticalSection( &pdh_handle_cs );
523 return PDH_INVALID_HANDLE;
524 }
525 if (list_empty( &query->counters ))
526 {
527 LeaveCriticalSection( &pdh_handle_cs );
528 return PDH_NO_DATA;
529 }
530 if (query->thread)
531 {
532 HANDLE thread = query->thread;
533 SetEvent( query->stop );
534 LeaveCriticalSection( &pdh_handle_cs );
535
536 WaitForSingleObject( thread, INFINITE );
537
538 EnterCriticalSection( &pdh_handle_cs );
539 if (query->magic != PDH_MAGIC_QUERY)
540 {
541 LeaveCriticalSection( &pdh_handle_cs );
542 return PDH_INVALID_HANDLE;
543 }
544 CloseHandle( query->thread );
545 query->thread = NULL;
546 }
547 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
548 {
549 ret = GetLastError();
550 LeaveCriticalSection( &pdh_handle_cs );
551 return ret;
552 }
553 query->wait = event;
554 query->interval = interval * 1000;
555 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
556 {
557 ret = GetLastError();
558 CloseHandle( query->stop );
559
560 LeaveCriticalSection( &pdh_handle_cs );
561 return ret;
562 }
563
564 LeaveCriticalSection( &pdh_handle_cs );
565 return ERROR_SUCCESS;
566 }
567
568 /***********************************************************************
569 * PdhCollectQueryDataWithTime (PDH.@)
570 */
571 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
572 {
573 struct query *query = handle;
574 struct counter *counter;
575 struct list *item;
576
577 TRACE("%p %p\n", handle, timestamp);
578
579 if (!timestamp) return PDH_INVALID_ARGUMENT;
580
581 EnterCriticalSection( &pdh_handle_cs );
582 if (!query || query->magic != PDH_MAGIC_QUERY)
583 {
584 LeaveCriticalSection( &pdh_handle_cs );
585 return PDH_INVALID_HANDLE;
586 }
587 if (list_empty( &query->counters ))
588 {
589 LeaveCriticalSection( &pdh_handle_cs );
590 return PDH_NO_DATA;
591 }
592
593 collect_query_data( query );
594
595 item = list_head( &query->counters );
596 counter = LIST_ENTRY( item, struct counter, entry );
597
598 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
599
600 LeaveCriticalSection( &pdh_handle_cs );
601 return ERROR_SUCCESS;
602 }
603
604 /***********************************************************************
605 * PdhExpandWildCardPathA (PDH.@)
606 */
607 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
608 {
609 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
610 return PDH_NOT_IMPLEMENTED;
611 }
612
613 /***********************************************************************
614 * PdhExpandWildCardPathW (PDH.@)
615 */
616 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
617 {
618 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
619 return PDH_NOT_IMPLEMENTED;
620 }
621
622 /***********************************************************************
623 * PdhGetCounterInfoA (PDH.@)
624 */
625 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
626 {
627 struct counter *counter = handle;
628
629 TRACE("%p %d %p %p\n", handle, text, size, info);
630
631 EnterCriticalSection( &pdh_handle_cs );
632 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
633 {
634 LeaveCriticalSection( &pdh_handle_cs );
635 return PDH_INVALID_HANDLE;
636 }
637 if (!size)
638 {
639 LeaveCriticalSection( &pdh_handle_cs );
640 return PDH_INVALID_ARGUMENT;
641 }
642 if (*size < sizeof(PDH_COUNTER_INFO_A))
643 {
644 *size = sizeof(PDH_COUNTER_INFO_A);
645 LeaveCriticalSection( &pdh_handle_cs );
646 return PDH_MORE_DATA;
647 }
648
649 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
650
651 info->dwType = counter->type;
652 info->CStatus = counter->status;
653 info->lScale = counter->scale;
654 info->lDefaultScale = counter->defaultscale;
655 info->dwUserData = counter->user;
656 info->dwQueryUserData = counter->queryuser;
657
658 *size = sizeof(PDH_COUNTER_INFO_A);
659
660 LeaveCriticalSection( &pdh_handle_cs );
661 return ERROR_SUCCESS;
662 }
663
664 /***********************************************************************
665 * PdhGetCounterInfoW (PDH.@)
666 */
667 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
668 {
669 struct counter *counter = handle;
670
671 TRACE("%p %d %p %p\n", handle, text, size, info);
672
673 EnterCriticalSection( &pdh_handle_cs );
674 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
675 {
676 LeaveCriticalSection( &pdh_handle_cs );
677 return PDH_INVALID_HANDLE;
678 }
679 if (!size)
680 {
681 LeaveCriticalSection( &pdh_handle_cs );
682 return PDH_INVALID_ARGUMENT;
683 }
684 if (*size < sizeof(PDH_COUNTER_INFO_W))
685 {
686 *size = sizeof(PDH_COUNTER_INFO_W);
687 LeaveCriticalSection( &pdh_handle_cs );
688 return PDH_MORE_DATA;
689 }
690
691 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
692
693 info->dwType = counter->type;
694 info->CStatus = counter->status;
695 info->lScale = counter->scale;
696 info->lDefaultScale = counter->defaultscale;
697 info->dwUserData = counter->user;
698 info->dwQueryUserData = counter->queryuser;
699
700 *size = sizeof(PDH_COUNTER_INFO_W);
701
702 LeaveCriticalSection( &pdh_handle_cs );
703 return ERROR_SUCCESS;
704 }
705
706 /***********************************************************************
707 * PdhGetCounterTimeBase (PDH.@)
708 */
709 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
710 {
711 struct counter *counter = handle;
712
713 TRACE("%p %p\n", handle, base);
714
715 if (!base) return PDH_INVALID_ARGUMENT;
716
717 EnterCriticalSection( &pdh_handle_cs );
718 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
719 {
720 LeaveCriticalSection( &pdh_handle_cs );
721 return PDH_INVALID_HANDLE;
722 }
723
724 *base = counter->base;
725
726 LeaveCriticalSection( &pdh_handle_cs );
727 return ERROR_SUCCESS;
728 }
729
730 /***********************************************************************
731 * PdhGetDllVersion (PDH.@)
732 */
733 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
734 {
735 if (!version)
736 return PDH_INVALID_ARGUMENT;
737
738 *version = PDH_VERSION;
739
740 return ERROR_SUCCESS;
741 }
742
743 /***********************************************************************
744 * PdhGetFormattedCounterValue (PDH.@)
745 */
746 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
747 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
748 {
749 PDH_STATUS ret;
750 struct counter *counter = handle;
751
752 TRACE("%p %x %p %p\n", handle, format, type, value);
753
754 if (!value) return PDH_INVALID_ARGUMENT;
755
756 EnterCriticalSection( &pdh_handle_cs );
757 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
758 {
759 LeaveCriticalSection( &pdh_handle_cs );
760 return PDH_INVALID_HANDLE;
761 }
762 if (counter->status)
763 {
764 LeaveCriticalSection( &pdh_handle_cs );
765 return PDH_INVALID_DATA;
766 }
767 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
768 {
769 value->CStatus = ERROR_SUCCESS;
770 if (type) *type = counter->type;
771 }
772
773 LeaveCriticalSection( &pdh_handle_cs );
774 return ret;
775 }
776
777 /***********************************************************************
778 * PdhGetRawCounterValue (PDH.@)
779 */
780 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
781 PPDH_RAW_COUNTER value )
782 {
783 struct counter *counter = handle;
784
785 TRACE("%p %p %p\n", handle, type, value);
786
787 if (!value) return PDH_INVALID_ARGUMENT;
788
789 EnterCriticalSection( &pdh_handle_cs );
790 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
791 {
792 LeaveCriticalSection( &pdh_handle_cs );
793 return PDH_INVALID_HANDLE;
794 }
795
796 value->CStatus = counter->status;
797 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
798 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
799 value->FirstValue = counter->one.largevalue;
800 value->SecondValue = counter->two.largevalue;
801 value->MultiCount = 1; /* FIXME */
802
803 if (type) *type = counter->type;
804
805 LeaveCriticalSection( &pdh_handle_cs );
806 return ERROR_SUCCESS;
807 }
808
809 /***********************************************************************
810 * PdhLookupPerfIndexByNameA (PDH.@)
811 */
812 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
813 {
814 PDH_STATUS ret;
815 WCHAR *machineW = NULL;
816 WCHAR *nameW;
817
818 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
819
820 if (!name) return PDH_INVALID_ARGUMENT;
821
822 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
823
824 if (!(nameW = pdh_strdup_aw( name )))
825 return PDH_MEMORY_ALLOCATION_FAILURE;
826
827 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
828
829 heap_free( nameW );
830 heap_free( machineW );
831 return ret;
832 }
833
834 /***********************************************************************
835 * PdhLookupPerfIndexByNameW (PDH.@)
836 */
837 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
838 {
839 unsigned int i;
840
841 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
842
843 if (!name || !index) return PDH_INVALID_ARGUMENT;
844
845 if (machine)
846 {
847 FIXME("remote machine not supported\n");
848 return PDH_CSTATUS_NO_MACHINE;
849 }
850 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
851 {
852 if (pdh_match_path( counter_sources[i].path, name ))
853 {
854 *index = counter_sources[i].index;
855 return ERROR_SUCCESS;
856 }
857 }
858 return PDH_STRING_NOT_FOUND;
859 }
860
861 /***********************************************************************
862 * PdhLookupPerfNameByIndexA (PDH.@)
863 */
864 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
865 {
866 PDH_STATUS ret;
867 WCHAR *machineW = NULL;
868 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
869 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
870
871 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
872
873 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
874
875 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
876
877 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
878 {
879 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
880
881 if (size && *size < required) ret = PDH_MORE_DATA;
882 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
883 if (size) *size = required;
884 }
885 heap_free( machineW );
886 return ret;
887 }
888
889 /***********************************************************************
890 * PdhLookupPerfNameByIndexW (PDH.@)
891 */
892 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
893 {
894 PDH_STATUS ret;
895 unsigned int i;
896
897 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
898
899 if (machine)
900 {
901 FIXME("remote machine not supported\n");
902 return PDH_CSTATUS_NO_MACHINE;
903 }
904
905 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
906 if (!index) return ERROR_SUCCESS;
907
908 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
909 {
910 if (counter_sources[i].index == index)
911 {
912 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
913 unsigned int required = strlenW( p ) + 1;
914
915 if (*size < required) ret = PDH_MORE_DATA;
916 else
917 {
918 strcpyW( buffer, p );
919 ret = ERROR_SUCCESS;
920 }
921 *size = required;
922 return ret;
923 }
924 }
925 return PDH_INVALID_ARGUMENT;
926 }
927
928 /***********************************************************************
929 * PdhOpenQueryA (PDH.@)
930 */
931 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
932 {
933 PDH_STATUS ret;
934 WCHAR *sourceW = NULL;
935
936 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
937
938 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
939
940 ret = PdhOpenQueryW( sourceW, userdata, query );
941 heap_free( sourceW );
942
943 return ret;
944 }
945
946 /***********************************************************************
947 * PdhOpenQueryW (PDH.@)
948 */
949 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
950 {
951 struct query *query;
952
953 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
954
955 if (!handle) return PDH_INVALID_ARGUMENT;
956
957 if (source)
958 {
959 FIXME("log file data source not supported\n");
960 return PDH_INVALID_ARGUMENT;
961 }
962 if ((query = create_query()))
963 {
964 query->user = userdata;
965 *handle = query;
966
967 return ERROR_SUCCESS;
968 }
969 return PDH_MEMORY_ALLOCATION_FAILURE;
970 }
971
972 /***********************************************************************
973 * PdhRemoveCounter (PDH.@)
974 */
975 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
976 {
977 struct counter *counter = handle;
978
979 TRACE("%p\n", handle);
980
981 EnterCriticalSection( &pdh_handle_cs );
982 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
983 {
984 LeaveCriticalSection( &pdh_handle_cs );
985 return PDH_INVALID_HANDLE;
986 }
987
988 list_remove( &counter->entry );
989 destroy_counter( counter );
990
991 LeaveCriticalSection( &pdh_handle_cs );
992 return ERROR_SUCCESS;
993 }
994
995 /***********************************************************************
996 * PdhSetCounterScaleFactor (PDH.@)
997 */
998 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
999 {
1000 struct counter *counter = handle;
1001
1002 TRACE("%p\n", handle);
1003
1004 EnterCriticalSection( &pdh_handle_cs );
1005 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1006 {
1007 LeaveCriticalSection( &pdh_handle_cs );
1008 return PDH_INVALID_HANDLE;
1009 }
1010 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1011 {
1012 LeaveCriticalSection( &pdh_handle_cs );
1013 return PDH_INVALID_ARGUMENT;
1014 }
1015
1016 counter->scale = factor;
1017
1018 LeaveCriticalSection( &pdh_handle_cs );
1019 return ERROR_SUCCESS;
1020 }
1021
1022 /***********************************************************************
1023 * PdhValidatePathA (PDH.@)
1024 */
1025 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1026 {
1027 PDH_STATUS ret;
1028 WCHAR *pathW;
1029
1030 TRACE("%s\n", debugstr_a(path));
1031
1032 if (!path) return PDH_INVALID_ARGUMENT;
1033 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1034
1035 ret = PdhValidatePathW( pathW );
1036
1037 heap_free( pathW );
1038 return ret;
1039 }
1040
1041 static PDH_STATUS validate_path( LPCWSTR path )
1042 {
1043 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1044 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1045 return ERROR_SUCCESS;
1046 }
1047
1048 /***********************************************************************
1049 * PdhValidatePathW (PDH.@)
1050 */
1051 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1052 {
1053 PDH_STATUS ret;
1054 unsigned int i;
1055
1056 TRACE("%s\n", debugstr_w(path));
1057
1058 if ((ret = validate_path( path ))) return ret;
1059
1060 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1061 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1062
1063 return PDH_CSTATUS_NO_COUNTER;
1064 }
1065
1066 /***********************************************************************
1067 * PdhValidatePathExA (PDH.@)
1068 */
1069 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1070 {
1071 TRACE("%p %s\n", source, debugstr_a(path));
1072
1073 if (source)
1074 {
1075 FIXME("log file data source not supported\n");
1076 return ERROR_SUCCESS;
1077 }
1078 return PdhValidatePathA( path );
1079 }
1080
1081 /***********************************************************************
1082 * PdhValidatePathExW (PDH.@)
1083 */
1084 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1085 {
1086 TRACE("%p %s\n", source, debugstr_w(path));
1087
1088 if (source)
1089 {
1090 FIXME("log file data source not supported\n");
1091 return ERROR_SUCCESS;
1092 }
1093 return PdhValidatePathW( path );
1094 }
1095
1096 /***********************************************************************
1097 * PdhMakeCounterPathA (PDH.@)
1098 */
1099 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1100 LPDWORD buflen, DWORD flags )
1101 {
1102 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1103 PDH_COUNTER_PATH_ELEMENTS_W eW;
1104 WCHAR *bufferW;
1105 DWORD buflenW;
1106
1107 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1108
1109 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1110
1111 memset( &eW, 0, sizeof(eW) );
1112 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1113 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1114 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1115 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1116 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1117 eW.dwInstanceIndex = e->dwInstanceIndex;
1118
1119 buflenW = 0;
1120 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1121 if (ret == PDH_MORE_DATA)
1122 {
1123 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1124 {
1125 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1126 {
1127 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1128 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1129 else ret = PDH_MORE_DATA;
1130 *buflen = len;
1131 }
1132 heap_free( bufferW );
1133 }
1134 else
1135 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1136 }
1137
1138 done:
1139 heap_free( eW.szMachineName );
1140 heap_free( eW.szObjectName );
1141 heap_free( eW.szInstanceName );
1142 heap_free( eW.szParentInstance );
1143 heap_free( eW.szCounterName );
1144 return ret;
1145 }
1146
1147 /***********************************************************************
1148 * PdhMakeCounterPathW (PDH.@)
1149 */
1150 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1151 LPDWORD buflen, DWORD flags )
1152 {
1153 static const WCHAR bslash[] = {'\\',0};
1154 static const WCHAR fslash[] = {'/',0};
1155 static const WCHAR lparen[] = {'(',0};
1156 static const WCHAR rparen[] = {')',0};
1157 static const WCHAR fmt[] = {'#','%','u',0};
1158
1159 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1160 PDH_STATUS ret = ERROR_SUCCESS;
1161 DWORD len;
1162
1163 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1164
1165 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1166
1167 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1168 return PDH_INVALID_ARGUMENT;
1169
1170 path[0] = 0;
1171 if (e->szMachineName)
1172 {
1173 strcatW(path, bslash);
1174 strcatW(path, bslash);
1175 strcatW(path, e->szMachineName);
1176 }
1177 strcatW(path, bslash);
1178 strcatW(path, e->szObjectName);
1179 if (e->szInstanceName)
1180 {
1181 strcatW(path, lparen);
1182 if (e->szParentInstance)
1183 {
1184 strcatW(path, e->szParentInstance);
1185 strcatW(path, fslash);
1186 }
1187 strcatW(path, e->szInstanceName);
1188 sprintfW(instance, fmt, e->dwInstanceIndex);
1189 strcatW(path, instance);
1190 strcatW(path, rparen);
1191 }
1192 strcatW(path, bslash);
1193 strcatW(path, e->szCounterName);
1194
1195 len = strlenW(path) + 1;
1196 if (*buflen >= len) strcpyW(buffer, path);
1197 else ret = PDH_MORE_DATA;
1198 *buflen = len;
1199 return ret;
1200 }
1201
1202 /***********************************************************************
1203 * PdhEnumObjectItemsA (PDH.@)
1204 */
1205 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1206 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1207 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1208 {
1209 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1210 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1211 pcchInstanceListLength, dwDetailLevel, dwFlags);
1212
1213 return PDH_NOT_IMPLEMENTED;
1214 }
1215
1216 /***********************************************************************
1217 * PdhEnumObjectItemsW (PDH.@)
1218 */
1219 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1220 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1221 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1222 {
1223 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1224 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1225 pcchInstanceListLength, dwDetailLevel, dwFlags);
1226
1227 return PDH_NOT_IMPLEMENTED;
1228 }
1229
1230 /***********************************************************************
1231 * PdhSetDefaultRealTimeDataSource (PDH.@)
1232 */
1233 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1234 {
1235 FIXME("%u\n", source);
1236 return ERROR_SUCCESS;
1237 }