* Sync up to trunk head (r65270).
[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 * PdhExpandCounterPathA (PDH.@)
642 */
643 PDH_STATUS WINAPI PdhExpandCounterPathA( LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength )
644 {
645 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength);
646 return PdhExpandWildCardPathA(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
647 }
648
649 /***********************************************************************
650 * PdhExpandCounterPathW (PDH.@)
651 */
652 PDH_STATUS WINAPI PdhExpandCounterPathW( LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength )
653 {
654 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength);
655 return PdhExpandWildCardPathW(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
656 }
657
658 /***********************************************************************
659 * PdhGetCounterInfoA (PDH.@)
660 */
661 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
662 {
663 struct counter *counter = handle;
664
665 TRACE("%p %d %p %p\n", handle, text, size, info);
666
667 EnterCriticalSection( &pdh_handle_cs );
668 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
669 {
670 LeaveCriticalSection( &pdh_handle_cs );
671 return PDH_INVALID_HANDLE;
672 }
673 if (!size)
674 {
675 LeaveCriticalSection( &pdh_handle_cs );
676 return PDH_INVALID_ARGUMENT;
677 }
678 if (*size < sizeof(PDH_COUNTER_INFO_A))
679 {
680 *size = sizeof(PDH_COUNTER_INFO_A);
681 LeaveCriticalSection( &pdh_handle_cs );
682 return PDH_MORE_DATA;
683 }
684
685 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
686
687 info->dwType = counter->type;
688 info->CStatus = counter->status;
689 info->lScale = counter->scale;
690 info->lDefaultScale = counter->defaultscale;
691 info->dwUserData = counter->user;
692 info->dwQueryUserData = counter->queryuser;
693
694 *size = sizeof(PDH_COUNTER_INFO_A);
695
696 LeaveCriticalSection( &pdh_handle_cs );
697 return ERROR_SUCCESS;
698 }
699
700 /***********************************************************************
701 * PdhGetCounterInfoW (PDH.@)
702 */
703 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
704 {
705 struct counter *counter = handle;
706
707 TRACE("%p %d %p %p\n", handle, text, size, info);
708
709 EnterCriticalSection( &pdh_handle_cs );
710 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
711 {
712 LeaveCriticalSection( &pdh_handle_cs );
713 return PDH_INVALID_HANDLE;
714 }
715 if (!size)
716 {
717 LeaveCriticalSection( &pdh_handle_cs );
718 return PDH_INVALID_ARGUMENT;
719 }
720 if (*size < sizeof(PDH_COUNTER_INFO_W))
721 {
722 *size = sizeof(PDH_COUNTER_INFO_W);
723 LeaveCriticalSection( &pdh_handle_cs );
724 return PDH_MORE_DATA;
725 }
726
727 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
728
729 info->dwType = counter->type;
730 info->CStatus = counter->status;
731 info->lScale = counter->scale;
732 info->lDefaultScale = counter->defaultscale;
733 info->dwUserData = counter->user;
734 info->dwQueryUserData = counter->queryuser;
735
736 *size = sizeof(PDH_COUNTER_INFO_W);
737
738 LeaveCriticalSection( &pdh_handle_cs );
739 return ERROR_SUCCESS;
740 }
741
742 /***********************************************************************
743 * PdhGetCounterTimeBase (PDH.@)
744 */
745 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
746 {
747 struct counter *counter = handle;
748
749 TRACE("%p %p\n", handle, base);
750
751 if (!base) return PDH_INVALID_ARGUMENT;
752
753 EnterCriticalSection( &pdh_handle_cs );
754 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
755 {
756 LeaveCriticalSection( &pdh_handle_cs );
757 return PDH_INVALID_HANDLE;
758 }
759
760 *base = counter->base;
761
762 LeaveCriticalSection( &pdh_handle_cs );
763 return ERROR_SUCCESS;
764 }
765
766 /***********************************************************************
767 * PdhGetDllVersion (PDH.@)
768 */
769 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
770 {
771 if (!version)
772 return PDH_INVALID_ARGUMENT;
773
774 *version = PDH_VERSION;
775
776 return ERROR_SUCCESS;
777 }
778
779 /***********************************************************************
780 * PdhGetFormattedCounterValue (PDH.@)
781 */
782 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
783 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
784 {
785 PDH_STATUS ret;
786 struct counter *counter = handle;
787
788 TRACE("%p %x %p %p\n", handle, format, type, value);
789
790 if (!value) return PDH_INVALID_ARGUMENT;
791
792 EnterCriticalSection( &pdh_handle_cs );
793 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
794 {
795 LeaveCriticalSection( &pdh_handle_cs );
796 return PDH_INVALID_HANDLE;
797 }
798 if (counter->status)
799 {
800 LeaveCriticalSection( &pdh_handle_cs );
801 return PDH_INVALID_DATA;
802 }
803 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
804 {
805 value->CStatus = ERROR_SUCCESS;
806 if (type) *type = counter->type;
807 }
808
809 LeaveCriticalSection( &pdh_handle_cs );
810 return ret;
811 }
812
813 /***********************************************************************
814 * PdhGetRawCounterValue (PDH.@)
815 */
816 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
817 PPDH_RAW_COUNTER value )
818 {
819 struct counter *counter = handle;
820
821 TRACE("%p %p %p\n", handle, type, value);
822
823 if (!value) return PDH_INVALID_ARGUMENT;
824
825 EnterCriticalSection( &pdh_handle_cs );
826 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
827 {
828 LeaveCriticalSection( &pdh_handle_cs );
829 return PDH_INVALID_HANDLE;
830 }
831
832 value->CStatus = counter->status;
833 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
834 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
835 value->FirstValue = counter->one.largevalue;
836 value->SecondValue = counter->two.largevalue;
837 value->MultiCount = 1; /* FIXME */
838
839 if (type) *type = counter->type;
840
841 LeaveCriticalSection( &pdh_handle_cs );
842 return ERROR_SUCCESS;
843 }
844
845 /***********************************************************************
846 * PdhLookupPerfIndexByNameA (PDH.@)
847 */
848 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
849 {
850 PDH_STATUS ret;
851 WCHAR *machineW = NULL;
852 WCHAR *nameW;
853
854 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
855
856 if (!name) return PDH_INVALID_ARGUMENT;
857
858 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
859
860 if (!(nameW = pdh_strdup_aw( name )))
861 return PDH_MEMORY_ALLOCATION_FAILURE;
862
863 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
864
865 heap_free( nameW );
866 heap_free( machineW );
867 return ret;
868 }
869
870 /***********************************************************************
871 * PdhLookupPerfIndexByNameW (PDH.@)
872 */
873 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
874 {
875 unsigned int i;
876
877 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
878
879 if (!name || !index) return PDH_INVALID_ARGUMENT;
880
881 if (machine)
882 {
883 FIXME("remote machine not supported\n");
884 return PDH_CSTATUS_NO_MACHINE;
885 }
886 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
887 {
888 if (pdh_match_path( counter_sources[i].path, name ))
889 {
890 *index = counter_sources[i].index;
891 return ERROR_SUCCESS;
892 }
893 }
894 return PDH_STRING_NOT_FOUND;
895 }
896
897 /***********************************************************************
898 * PdhLookupPerfNameByIndexA (PDH.@)
899 */
900 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
901 {
902 PDH_STATUS ret;
903 WCHAR *machineW = NULL;
904 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
905 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
906
907 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
908
909 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
910
911 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
912
913 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
914 {
915 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
916
917 if (size && *size < required) ret = PDH_MORE_DATA;
918 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
919 if (size) *size = required;
920 }
921 heap_free( machineW );
922 return ret;
923 }
924
925 /***********************************************************************
926 * PdhLookupPerfNameByIndexW (PDH.@)
927 */
928 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
929 {
930 PDH_STATUS ret;
931 unsigned int i;
932
933 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
934
935 if (machine)
936 {
937 FIXME("remote machine not supported\n");
938 return PDH_CSTATUS_NO_MACHINE;
939 }
940
941 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
942 if (!index) return ERROR_SUCCESS;
943
944 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
945 {
946 if (counter_sources[i].index == index)
947 {
948 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
949 unsigned int required = strlenW( p ) + 1;
950
951 if (*size < required) ret = PDH_MORE_DATA;
952 else
953 {
954 strcpyW( buffer, p );
955 ret = ERROR_SUCCESS;
956 }
957 *size = required;
958 return ret;
959 }
960 }
961 return PDH_INVALID_ARGUMENT;
962 }
963
964 /***********************************************************************
965 * PdhOpenQueryA (PDH.@)
966 */
967 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
968 {
969 PDH_STATUS ret;
970 WCHAR *sourceW = NULL;
971
972 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
973
974 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
975
976 ret = PdhOpenQueryW( sourceW, userdata, query );
977 heap_free( sourceW );
978
979 return ret;
980 }
981
982 /***********************************************************************
983 * PdhOpenQueryW (PDH.@)
984 */
985 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
986 {
987 struct query *query;
988
989 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
990
991 if (!handle) return PDH_INVALID_ARGUMENT;
992
993 if (source)
994 {
995 FIXME("log file data source not supported\n");
996 return PDH_INVALID_ARGUMENT;
997 }
998 if ((query = create_query()))
999 {
1000 query->user = userdata;
1001 *handle = query;
1002
1003 return ERROR_SUCCESS;
1004 }
1005 return PDH_MEMORY_ALLOCATION_FAILURE;
1006 }
1007
1008 /***********************************************************************
1009 * PdhRemoveCounter (PDH.@)
1010 */
1011 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
1012 {
1013 struct counter *counter = handle;
1014
1015 TRACE("%p\n", handle);
1016
1017 EnterCriticalSection( &pdh_handle_cs );
1018 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1019 {
1020 LeaveCriticalSection( &pdh_handle_cs );
1021 return PDH_INVALID_HANDLE;
1022 }
1023
1024 list_remove( &counter->entry );
1025 destroy_counter( counter );
1026
1027 LeaveCriticalSection( &pdh_handle_cs );
1028 return ERROR_SUCCESS;
1029 }
1030
1031 /***********************************************************************
1032 * PdhSetCounterScaleFactor (PDH.@)
1033 */
1034 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
1035 {
1036 struct counter *counter = handle;
1037
1038 TRACE("%p\n", handle);
1039
1040 EnterCriticalSection( &pdh_handle_cs );
1041 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1042 {
1043 LeaveCriticalSection( &pdh_handle_cs );
1044 return PDH_INVALID_HANDLE;
1045 }
1046 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1047 {
1048 LeaveCriticalSection( &pdh_handle_cs );
1049 return PDH_INVALID_ARGUMENT;
1050 }
1051
1052 counter->scale = factor;
1053
1054 LeaveCriticalSection( &pdh_handle_cs );
1055 return ERROR_SUCCESS;
1056 }
1057
1058 /***********************************************************************
1059 * PdhValidatePathA (PDH.@)
1060 */
1061 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1062 {
1063 PDH_STATUS ret;
1064 WCHAR *pathW;
1065
1066 TRACE("%s\n", debugstr_a(path));
1067
1068 if (!path) return PDH_INVALID_ARGUMENT;
1069 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1070
1071 ret = PdhValidatePathW( pathW );
1072
1073 heap_free( pathW );
1074 return ret;
1075 }
1076
1077 static PDH_STATUS validate_path( LPCWSTR path )
1078 {
1079 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1080 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1081 return ERROR_SUCCESS;
1082 }
1083
1084 /***********************************************************************
1085 * PdhValidatePathW (PDH.@)
1086 */
1087 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1088 {
1089 PDH_STATUS ret;
1090 unsigned int i;
1091
1092 TRACE("%s\n", debugstr_w(path));
1093
1094 if ((ret = validate_path( path ))) return ret;
1095
1096 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1097 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1098
1099 return PDH_CSTATUS_NO_COUNTER;
1100 }
1101
1102 /***********************************************************************
1103 * PdhValidatePathExA (PDH.@)
1104 */
1105 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1106 {
1107 TRACE("%p %s\n", source, debugstr_a(path));
1108
1109 if (source)
1110 {
1111 FIXME("log file data source not supported\n");
1112 return ERROR_SUCCESS;
1113 }
1114 return PdhValidatePathA( path );
1115 }
1116
1117 /***********************************************************************
1118 * PdhValidatePathExW (PDH.@)
1119 */
1120 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1121 {
1122 TRACE("%p %s\n", source, debugstr_w(path));
1123
1124 if (source)
1125 {
1126 FIXME("log file data source not supported\n");
1127 return ERROR_SUCCESS;
1128 }
1129 return PdhValidatePathW( path );
1130 }
1131
1132 /***********************************************************************
1133 * PdhMakeCounterPathA (PDH.@)
1134 */
1135 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1136 LPDWORD buflen, DWORD flags )
1137 {
1138 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1139 PDH_COUNTER_PATH_ELEMENTS_W eW;
1140 WCHAR *bufferW;
1141 DWORD buflenW;
1142
1143 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1144
1145 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1146
1147 memset( &eW, 0, sizeof(eW) );
1148 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1149 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1150 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1151 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1152 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1153 eW.dwInstanceIndex = e->dwInstanceIndex;
1154
1155 buflenW = 0;
1156 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1157 if (ret == PDH_MORE_DATA)
1158 {
1159 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1160 {
1161 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1162 {
1163 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1164 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1165 else ret = PDH_MORE_DATA;
1166 *buflen = len;
1167 }
1168 heap_free( bufferW );
1169 }
1170 else
1171 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1172 }
1173
1174 done:
1175 heap_free( eW.szMachineName );
1176 heap_free( eW.szObjectName );
1177 heap_free( eW.szInstanceName );
1178 heap_free( eW.szParentInstance );
1179 heap_free( eW.szCounterName );
1180 return ret;
1181 }
1182
1183 /***********************************************************************
1184 * PdhMakeCounterPathW (PDH.@)
1185 */
1186 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1187 LPDWORD buflen, DWORD flags )
1188 {
1189 static const WCHAR bslash[] = {'\\',0};
1190 static const WCHAR fslash[] = {'/',0};
1191 static const WCHAR lparen[] = {'(',0};
1192 static const WCHAR rparen[] = {')',0};
1193 static const WCHAR fmt[] = {'#','%','u',0};
1194
1195 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1196 PDH_STATUS ret = ERROR_SUCCESS;
1197 DWORD len;
1198
1199 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1200
1201 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1202
1203 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1204 return PDH_INVALID_ARGUMENT;
1205
1206 path[0] = 0;
1207 if (e->szMachineName)
1208 {
1209 strcatW(path, bslash);
1210 strcatW(path, bslash);
1211 strcatW(path, e->szMachineName);
1212 }
1213 strcatW(path, bslash);
1214 strcatW(path, e->szObjectName);
1215 if (e->szInstanceName)
1216 {
1217 strcatW(path, lparen);
1218 if (e->szParentInstance)
1219 {
1220 strcatW(path, e->szParentInstance);
1221 strcatW(path, fslash);
1222 }
1223 strcatW(path, e->szInstanceName);
1224 sprintfW(instance, fmt, e->dwInstanceIndex);
1225 strcatW(path, instance);
1226 strcatW(path, rparen);
1227 }
1228 strcatW(path, bslash);
1229 strcatW(path, e->szCounterName);
1230
1231 len = strlenW(path) + 1;
1232 if (*buflen >= len) strcpyW(buffer, path);
1233 else ret = PDH_MORE_DATA;
1234 *buflen = len;
1235 return ret;
1236 }
1237
1238 /***********************************************************************
1239 * PdhEnumObjectItemsA (PDH.@)
1240 */
1241 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1242 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1243 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1244 {
1245 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1246 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1247 pcchInstanceListLength, dwDetailLevel, dwFlags);
1248
1249 return PDH_NOT_IMPLEMENTED;
1250 }
1251
1252 /***********************************************************************
1253 * PdhEnumObjectItemsW (PDH.@)
1254 */
1255 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1256 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1257 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1258 {
1259 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1260 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1261 pcchInstanceListLength, dwDetailLevel, dwFlags);
1262
1263 return PDH_NOT_IMPLEMENTED;
1264 }
1265
1266 /***********************************************************************
1267 * PdhSetDefaultRealTimeDataSource (PDH.@)
1268 */
1269 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1270 {
1271 FIXME("%u\n", source);
1272 return ERROR_SUCCESS;
1273 }