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