2 * Performance Data Helper (pdh.dll)
4 * Copyright 2007 Andrey Turkin
5 * Copyright 2007 Hans Leidekker
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.
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.
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
22 #define WIN32_NO_STATUS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
34 //#include "winperf.h"
36 #include <wine/debug.h>
37 #include <wine/list.h>
38 #include <wine/unicode.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(pdh
);
42 static CRITICAL_SECTION pdh_handle_cs
;
43 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug
=
46 { &pdh_handle_cs_debug
.ProcessLocksList
,
47 &pdh_handle_cs_debug
.ProcessLocksList
},
48 0, 0, { (DWORD_PTR
)(__FILE__
": pdh_handle_cs") }
50 static CRITICAL_SECTION pdh_handle_cs
= { &pdh_handle_cs_debug
, -1, 0, 0, 0, 0 };
52 static inline void *heap_alloc( SIZE_T size
)
54 return HeapAlloc( GetProcessHeap(), 0, size
);
57 static inline void *heap_alloc_zero( SIZE_T size
)
59 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
62 static inline void heap_free( LPVOID mem
)
64 HeapFree( GetProcessHeap(), 0, mem
);
67 static inline WCHAR
*pdh_strdup( const WCHAR
*src
)
71 if (!src
) return NULL
;
72 if ((dst
= heap_alloc( (strlenW( src
) + 1) * sizeof(WCHAR
) ))) strcpyW( dst
, src
);
76 static inline WCHAR
*pdh_strdup_aw( const char *src
)
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
);
87 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
89 TRACE("(0x%p, %d, %p)\n",hinstDLL
,fdwReason
,lpvReserved
);
92 case DLL_WINE_PREATTACH
:
93 return FALSE
; /* prefer native version */
94 case DLL_PROCESS_ATTACH
:
95 DisableThreadLibraryCalls(hinstDLL
);
97 case DLL_PROCESS_DETACH
:
98 if (lpvReserved
) break;
99 DeleteCriticalSection(&pdh_handle_cs
);
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 */
131 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
133 static struct counter
*create_counter( void )
135 struct counter
*counter
;
137 if ((counter
= heap_alloc_zero( sizeof(struct counter
) )))
139 counter
->magic
= PDH_MAGIC_COUNTER
;
145 static void destroy_counter( struct counter
*counter
)
148 heap_free( counter
->path
);
149 heap_free( counter
);
152 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
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 */
165 static struct query
*create_query( void )
169 if ((query
= heap_alloc_zero( sizeof(struct query
) )))
171 query
->magic
= PDH_MAGIC_QUERY
;
172 list_init( &query
->counters
);
178 static void destroy_query( struct query
*query
)
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 */
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};
200 static void CALLBACK
collect_processor_time( struct counter
*counter
)
202 counter
->two
.largevalue
= 500000; /* FIXME */
203 counter
->status
= PDH_CSTATUS_VALID_DATA
;
206 static void CALLBACK
collect_uptime( struct counter
*counter
)
208 counter
->two
.largevalue
= GetTickCount64();
209 counter
->status
= PDH_CSTATUS_VALID_DATA
;
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)
216 #define TYPE_UPTIME \
217 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
219 /* counter source registry */
220 static const struct source counter_sources
[] =
222 { 6, path_processor_time
, collect_processor_time
, TYPE_PROCESSOR_TIME
, -5, 10000000 },
223 { 674, path_uptime
, collect_uptime
, TYPE_UPTIME
, -3, 1000 }
226 static BOOL
is_local_machine( const WCHAR
*name
, DWORD len
)
228 WCHAR buf
[MAX_COMPUTERNAME_LENGTH
+ 1];
229 DWORD buflen
= sizeof(buf
) / sizeof(buf
[0]);
231 if (!GetComputerNameW( buf
, &buflen
)) return FALSE
;
232 return len
== buflen
&& !memicmpW( name
, buf
, buflen
);
235 static BOOL
pdh_match_path( LPCWSTR fullpath
, LPCWSTR path
)
239 if (path
[0] == '\\' && path
[1] == '\\' && (p
= strchrW( path
+ 2, '\\' )) &&
240 is_local_machine( path
+ 2, p
- path
- 2 ))
244 if (strchrW( path
, '\\' )) p
= fullpath
;
245 else p
= strrchrW( fullpath
, '\\' ) + 1;
246 return !strcmpW( p
, path
);
249 /***********************************************************************
250 * PdhAddCounterA (PDH.@)
252 PDH_STATUS WINAPI
PdhAddCounterA( PDH_HQUERY query
, LPCSTR path
,
253 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
258 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
260 if (!path
) return PDH_INVALID_ARGUMENT
;
262 if (!(pathW
= pdh_strdup_aw( path
)))
263 return PDH_MEMORY_ALLOCATION_FAILURE
;
265 ret
= PdhAddCounterW( query
, pathW
, userdata
, counter
);
271 /***********************************************************************
272 * PdhAddCounterW (PDH.@)
274 PDH_STATUS WINAPI
PdhAddCounterW( PDH_HQUERY hquery
, LPCWSTR path
,
275 DWORD_PTR userdata
, PDH_HCOUNTER
*hcounter
)
277 struct query
*query
= hquery
;
278 struct counter
*counter
;
281 TRACE("%p %s %lx %p\n", hquery
, debugstr_w(path
), userdata
, hcounter
);
283 if (!path
|| !hcounter
) return PDH_INVALID_ARGUMENT
;
285 EnterCriticalSection( &pdh_handle_cs
);
286 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
288 LeaveCriticalSection( &pdh_handle_cs
);
289 return PDH_INVALID_HANDLE
;
293 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
295 if (pdh_match_path( counter_sources
[i
].path
, path
))
297 if ((counter
= create_counter()))
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
;
307 list_add_tail( &query
->counters
, &counter
->entry
);
310 LeaveCriticalSection( &pdh_handle_cs
);
311 return ERROR_SUCCESS
;
313 LeaveCriticalSection( &pdh_handle_cs
);
314 return PDH_MEMORY_ALLOCATION_FAILURE
;
317 LeaveCriticalSection( &pdh_handle_cs
);
318 return PDH_CSTATUS_NO_COUNTER
;
321 /***********************************************************************
322 * PdhAddEnglishCounterA (PDH.@)
324 PDH_STATUS WINAPI
PdhAddEnglishCounterA( PDH_HQUERY query
, LPCSTR path
,
325 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
327 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
329 if (!query
) return PDH_INVALID_ARGUMENT
;
330 return PdhAddCounterA( query
, path
, userdata
, counter
);
333 /***********************************************************************
334 * PdhAddEnglishCounterW (PDH.@)
336 PDH_STATUS WINAPI
PdhAddEnglishCounterW( PDH_HQUERY query
, LPCWSTR path
,
337 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
339 TRACE("%p %s %lx %p\n", query
, debugstr_w(path
), userdata
, counter
);
341 if (!query
) return PDH_INVALID_ARGUMENT
;
342 return PdhAddCounterW( query
, path
, userdata
, counter
);
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
)
351 factor
= counter
->scale
? counter
->scale
: counter
->defaultscale
;
352 if (format
& PDH_FMT_LONG
)
354 if (format
& PDH_FMT_1000
) value
->u
.longValue
= raw2
->longvalue
* 1000;
355 else value
->u
.longValue
= raw2
->longvalue
* pow( 10, factor
);
357 else if (format
& PDH_FMT_LARGE
)
359 if (format
& PDH_FMT_1000
) value
->u
.largeValue
= raw2
->largevalue
* 1000;
360 else value
->u
.largeValue
= raw2
->largevalue
* pow( 10, factor
);
362 else if (format
& PDH_FMT_DOUBLE
)
364 if (format
& PDH_FMT_1000
) value
->u
.doubleValue
= raw2
->doublevalue
* 1000;
365 else value
->u
.doubleValue
= raw2
->doublevalue
* pow( 10, factor
);
369 WARN("unknown format %x\n", format
);
370 return PDH_INVALID_ARGUMENT
;
372 return ERROR_SUCCESS
;
375 /***********************************************************************
376 * PdhCalculateCounterFromRawValue (PDH.@)
378 PDH_STATUS WINAPI
PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle
, DWORD format
,
379 PPDH_RAW_COUNTER raw1
, PPDH_RAW_COUNTER raw2
,
380 PPDH_FMT_COUNTERVALUE value
)
383 struct counter
*counter
= handle
;
385 TRACE("%p 0x%08x %p %p %p\n", handle
, format
, raw1
, raw2
, value
);
387 if (!value
) return PDH_INVALID_ARGUMENT
;
389 EnterCriticalSection( &pdh_handle_cs
);
390 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
392 LeaveCriticalSection( &pdh_handle_cs
);
393 return PDH_INVALID_HANDLE
;
396 ret
= format_value( counter
, format
, (union value
*)&raw1
->SecondValue
,
397 (union value
*)&raw2
->SecondValue
, value
);
399 LeaveCriticalSection( &pdh_handle_cs
);
404 /***********************************************************************
405 * PdhCloseQuery (PDH.@)
407 PDH_STATUS WINAPI
PdhCloseQuery( PDH_HQUERY handle
)
409 struct query
*query
= handle
;
410 struct list
*item
, *next
;
412 TRACE("%p\n", handle
);
414 EnterCriticalSection( &pdh_handle_cs
);
415 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
417 LeaveCriticalSection( &pdh_handle_cs
);
418 return PDH_INVALID_HANDLE
;
423 HANDLE thread
= query
->thread
;
424 SetEvent( query
->stop
);
425 LeaveCriticalSection( &pdh_handle_cs
);
427 WaitForSingleObject( thread
, INFINITE
);
429 EnterCriticalSection( &pdh_handle_cs
);
430 if (query
->magic
!= PDH_MAGIC_QUERY
)
432 LeaveCriticalSection( &pdh_handle_cs
);
433 return ERROR_SUCCESS
;
435 CloseHandle( query
->stop
);
436 CloseHandle( query
->thread
);
437 query
->thread
= NULL
;
440 LIST_FOR_EACH_SAFE( item
, next
, &query
->counters
)
442 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
444 list_remove( &counter
->entry
);
445 destroy_counter( counter
);
448 destroy_query( query
);
450 LeaveCriticalSection( &pdh_handle_cs
);
451 return ERROR_SUCCESS
;
454 /* caller must hold query lock */
455 static void collect_query_data( struct query
*query
)
459 LIST_FOR_EACH( item
, &query
->counters
)
462 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
464 counter
->collect( counter
);
466 GetLocalTime( &time
);
467 SystemTimeToFileTime( &time
, &counter
->stamp
);
471 /***********************************************************************
472 * PdhCollectQueryData (PDH.@)
474 PDH_STATUS WINAPI
PdhCollectQueryData( PDH_HQUERY handle
)
476 struct query
*query
= handle
;
478 TRACE("%p\n", handle
);
480 EnterCriticalSection( &pdh_handle_cs
);
481 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
483 LeaveCriticalSection( &pdh_handle_cs
);
484 return PDH_INVALID_HANDLE
;
487 if (list_empty( &query
->counters
))
489 LeaveCriticalSection( &pdh_handle_cs
);
493 collect_query_data( query
);
495 LeaveCriticalSection( &pdh_handle_cs
);
496 return ERROR_SUCCESS
;
499 static DWORD CALLBACK
collect_query_thread( void *arg
)
501 struct query
*query
= arg
;
502 DWORD interval
= query
->interval
;
503 HANDLE stop
= query
->stop
;
507 if (WaitForSingleObject( stop
, interval
) != WAIT_TIMEOUT
) ExitThread( 0 );
509 EnterCriticalSection( &pdh_handle_cs
);
510 if (query
->magic
!= PDH_MAGIC_QUERY
)
512 LeaveCriticalSection( &pdh_handle_cs
);
513 ExitThread( PDH_INVALID_HANDLE
);
516 collect_query_data( query
);
518 if (!SetEvent( query
->wait
))
520 LeaveCriticalSection( &pdh_handle_cs
);
523 LeaveCriticalSection( &pdh_handle_cs
);
527 /***********************************************************************
528 * PdhCollectQueryDataEx (PDH.@)
530 PDH_STATUS WINAPI
PdhCollectQueryDataEx( PDH_HQUERY handle
, DWORD interval
, HANDLE event
)
533 struct query
*query
= handle
;
535 TRACE("%p %d %p\n", handle
, interval
, event
);
537 EnterCriticalSection( &pdh_handle_cs
);
538 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
540 LeaveCriticalSection( &pdh_handle_cs
);
541 return PDH_INVALID_HANDLE
;
543 if (list_empty( &query
->counters
))
545 LeaveCriticalSection( &pdh_handle_cs
);
550 HANDLE thread
= query
->thread
;
551 SetEvent( query
->stop
);
552 LeaveCriticalSection( &pdh_handle_cs
);
554 WaitForSingleObject( thread
, INFINITE
);
556 EnterCriticalSection( &pdh_handle_cs
);
557 if (query
->magic
!= PDH_MAGIC_QUERY
)
559 LeaveCriticalSection( &pdh_handle_cs
);
560 return PDH_INVALID_HANDLE
;
562 CloseHandle( query
->thread
);
563 query
->thread
= NULL
;
565 else if (!(query
->stop
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
567 ret
= GetLastError();
568 LeaveCriticalSection( &pdh_handle_cs
);
572 query
->interval
= interval
* 1000;
573 if (!(query
->thread
= CreateThread( NULL
, 0, collect_query_thread
, query
, 0, NULL
)))
575 ret
= GetLastError();
576 CloseHandle( query
->stop
);
578 LeaveCriticalSection( &pdh_handle_cs
);
582 LeaveCriticalSection( &pdh_handle_cs
);
583 return ERROR_SUCCESS
;
586 /***********************************************************************
587 * PdhCollectQueryDataWithTime (PDH.@)
589 PDH_STATUS WINAPI
PdhCollectQueryDataWithTime( PDH_HQUERY handle
, LONGLONG
*timestamp
)
591 struct query
*query
= handle
;
592 struct counter
*counter
;
595 TRACE("%p %p\n", handle
, timestamp
);
597 if (!timestamp
) return PDH_INVALID_ARGUMENT
;
599 EnterCriticalSection( &pdh_handle_cs
);
600 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
602 LeaveCriticalSection( &pdh_handle_cs
);
603 return PDH_INVALID_HANDLE
;
605 if (list_empty( &query
->counters
))
607 LeaveCriticalSection( &pdh_handle_cs
);
611 collect_query_data( query
);
613 item
= list_head( &query
->counters
);
614 counter
= LIST_ENTRY( item
, struct counter
, entry
);
616 *timestamp
= ((LONGLONG
)counter
->stamp
.dwHighDateTime
<< 32) | counter
->stamp
.dwLowDateTime
;
618 LeaveCriticalSection( &pdh_handle_cs
);
619 return ERROR_SUCCESS
;
622 /***********************************************************************
623 * PdhExpandWildCardPathA (PDH.@)
625 PDH_STATUS WINAPI
PdhExpandWildCardPathA( LPCSTR szDataSource
, LPCSTR szWildCardPath
, LPSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
627 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
628 return PDH_NOT_IMPLEMENTED
;
631 /***********************************************************************
632 * PdhExpandWildCardPathW (PDH.@)
634 PDH_STATUS WINAPI
PdhExpandWildCardPathW( LPCWSTR szDataSource
, LPCWSTR szWildCardPath
, LPWSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
636 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
637 return PDH_NOT_IMPLEMENTED
;
640 /***********************************************************************
641 * PdhGetCounterInfoA (PDH.@)
643 PDH_STATUS WINAPI
PdhGetCounterInfoA( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_A info
)
645 struct counter
*counter
= handle
;
647 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
649 EnterCriticalSection( &pdh_handle_cs
);
650 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
652 LeaveCriticalSection( &pdh_handle_cs
);
653 return PDH_INVALID_HANDLE
;
657 LeaveCriticalSection( &pdh_handle_cs
);
658 return PDH_INVALID_ARGUMENT
;
660 if (*size
< sizeof(PDH_COUNTER_INFO_A
))
662 *size
= sizeof(PDH_COUNTER_INFO_A
);
663 LeaveCriticalSection( &pdh_handle_cs
);
664 return PDH_MORE_DATA
;
667 memset( info
, 0, sizeof(PDH_COUNTER_INFO_A
) );
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
;
676 *size
= sizeof(PDH_COUNTER_INFO_A
);
678 LeaveCriticalSection( &pdh_handle_cs
);
679 return ERROR_SUCCESS
;
682 /***********************************************************************
683 * PdhGetCounterInfoW (PDH.@)
685 PDH_STATUS WINAPI
PdhGetCounterInfoW( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_W info
)
687 struct counter
*counter
= handle
;
689 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
691 EnterCriticalSection( &pdh_handle_cs
);
692 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
694 LeaveCriticalSection( &pdh_handle_cs
);
695 return PDH_INVALID_HANDLE
;
699 LeaveCriticalSection( &pdh_handle_cs
);
700 return PDH_INVALID_ARGUMENT
;
702 if (*size
< sizeof(PDH_COUNTER_INFO_W
))
704 *size
= sizeof(PDH_COUNTER_INFO_W
);
705 LeaveCriticalSection( &pdh_handle_cs
);
706 return PDH_MORE_DATA
;
709 memset( info
, 0, sizeof(PDH_COUNTER_INFO_W
) );
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
;
718 *size
= sizeof(PDH_COUNTER_INFO_W
);
720 LeaveCriticalSection( &pdh_handle_cs
);
721 return ERROR_SUCCESS
;
724 /***********************************************************************
725 * PdhGetCounterTimeBase (PDH.@)
727 PDH_STATUS WINAPI
PdhGetCounterTimeBase( PDH_HCOUNTER handle
, LONGLONG
*base
)
729 struct counter
*counter
= handle
;
731 TRACE("%p %p\n", handle
, base
);
733 if (!base
) return PDH_INVALID_ARGUMENT
;
735 EnterCriticalSection( &pdh_handle_cs
);
736 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
738 LeaveCriticalSection( &pdh_handle_cs
);
739 return PDH_INVALID_HANDLE
;
742 *base
= counter
->base
;
744 LeaveCriticalSection( &pdh_handle_cs
);
745 return ERROR_SUCCESS
;
748 /***********************************************************************
749 * PdhGetDllVersion (PDH.@)
751 PDH_STATUS WINAPI
PdhGetDllVersion( LPDWORD version
)
754 return PDH_INVALID_ARGUMENT
;
756 *version
= PDH_VERSION
;
758 return ERROR_SUCCESS
;
761 /***********************************************************************
762 * PdhGetFormattedCounterValue (PDH.@)
764 PDH_STATUS WINAPI
PdhGetFormattedCounterValue( PDH_HCOUNTER handle
, DWORD format
,
765 LPDWORD type
, PPDH_FMT_COUNTERVALUE value
)
768 struct counter
*counter
= handle
;
770 TRACE("%p %x %p %p\n", handle
, format
, type
, value
);
772 if (!value
) return PDH_INVALID_ARGUMENT
;
774 EnterCriticalSection( &pdh_handle_cs
);
775 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
777 LeaveCriticalSection( &pdh_handle_cs
);
778 return PDH_INVALID_HANDLE
;
782 LeaveCriticalSection( &pdh_handle_cs
);
783 return PDH_INVALID_DATA
;
785 if (!(ret
= format_value( counter
, format
, &counter
->one
, &counter
->two
, value
)))
787 value
->CStatus
= ERROR_SUCCESS
;
788 if (type
) *type
= counter
->type
;
791 LeaveCriticalSection( &pdh_handle_cs
);
795 /***********************************************************************
796 * PdhGetRawCounterValue (PDH.@)
798 PDH_STATUS WINAPI
PdhGetRawCounterValue( PDH_HCOUNTER handle
, LPDWORD type
,
799 PPDH_RAW_COUNTER value
)
801 struct counter
*counter
= handle
;
803 TRACE("%p %p %p\n", handle
, type
, value
);
805 if (!value
) return PDH_INVALID_ARGUMENT
;
807 EnterCriticalSection( &pdh_handle_cs
);
808 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
810 LeaveCriticalSection( &pdh_handle_cs
);
811 return PDH_INVALID_HANDLE
;
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 */
821 if (type
) *type
= counter
->type
;
823 LeaveCriticalSection( &pdh_handle_cs
);
824 return ERROR_SUCCESS
;
827 /***********************************************************************
828 * PdhLookupPerfIndexByNameA (PDH.@)
830 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameA( LPCSTR machine
, LPCSTR name
, LPDWORD index
)
833 WCHAR
*machineW
= NULL
;
836 TRACE("%s %s %p\n", debugstr_a(machine
), debugstr_a(name
), index
);
838 if (!name
) return PDH_INVALID_ARGUMENT
;
840 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
842 if (!(nameW
= pdh_strdup_aw( name
)))
843 return PDH_MEMORY_ALLOCATION_FAILURE
;
845 ret
= PdhLookupPerfIndexByNameW( machineW
, nameW
, index
);
848 heap_free( machineW
);
852 /***********************************************************************
853 * PdhLookupPerfIndexByNameW (PDH.@)
855 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameW( LPCWSTR machine
, LPCWSTR name
, LPDWORD index
)
859 TRACE("%s %s %p\n", debugstr_w(machine
), debugstr_w(name
), index
);
861 if (!name
|| !index
) return PDH_INVALID_ARGUMENT
;
865 FIXME("remote machine not supported\n");
866 return PDH_CSTATUS_NO_MACHINE
;
868 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
870 if (pdh_match_path( counter_sources
[i
].path
, name
))
872 *index
= counter_sources
[i
].index
;
873 return ERROR_SUCCESS
;
876 return PDH_STRING_NOT_FOUND
;
879 /***********************************************************************
880 * PdhLookupPerfNameByIndexA (PDH.@)
882 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexA( LPCSTR machine
, DWORD index
, LPSTR buffer
, LPDWORD size
)
885 WCHAR
*machineW
= NULL
;
886 WCHAR bufferW
[PDH_MAX_COUNTER_NAME
];
887 DWORD sizeW
= sizeof(bufferW
) / sizeof(WCHAR
);
889 TRACE("%s %d %p %p\n", debugstr_a(machine
), index
, buffer
, size
);
891 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
893 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
895 if (!(ret
= PdhLookupPerfNameByIndexW( machineW
, index
, bufferW
, &sizeW
)))
897 int required
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
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
;
903 heap_free( machineW
);
907 /***********************************************************************
908 * PdhLookupPerfNameByIndexW (PDH.@)
910 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexW( LPCWSTR machine
, DWORD index
, LPWSTR buffer
, LPDWORD size
)
915 TRACE("%s %d %p %p\n", debugstr_w(machine
), index
, buffer
, size
);
919 FIXME("remote machine not supported\n");
920 return PDH_CSTATUS_NO_MACHINE
;
923 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
924 if (!index
) return ERROR_SUCCESS
;
926 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
928 if (counter_sources
[i
].index
== index
)
930 WCHAR
*p
= strrchrW( counter_sources
[i
].path
, '\\' ) + 1;
931 unsigned int required
= strlenW( p
) + 1;
933 if (*size
< required
) ret
= PDH_MORE_DATA
;
936 strcpyW( buffer
, p
);
943 return PDH_INVALID_ARGUMENT
;
946 /***********************************************************************
947 * PdhOpenQueryA (PDH.@)
949 PDH_STATUS WINAPI
PdhOpenQueryA( LPCSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*query
)
952 WCHAR
*sourceW
= NULL
;
954 TRACE("%s %lx %p\n", debugstr_a(source
), userdata
, query
);
956 if (source
&& !(sourceW
= pdh_strdup_aw( source
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
958 ret
= PdhOpenQueryW( sourceW
, userdata
, query
);
959 heap_free( sourceW
);
964 /***********************************************************************
965 * PdhOpenQueryW (PDH.@)
967 PDH_STATUS WINAPI
PdhOpenQueryW( LPCWSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*handle
)
971 TRACE("%s %lx %p\n", debugstr_w(source
), userdata
, handle
);
973 if (!handle
) return PDH_INVALID_ARGUMENT
;
977 FIXME("log file data source not supported\n");
978 return PDH_INVALID_ARGUMENT
;
980 if ((query
= create_query()))
982 query
->user
= userdata
;
985 return ERROR_SUCCESS
;
987 return PDH_MEMORY_ALLOCATION_FAILURE
;
990 /***********************************************************************
991 * PdhRemoveCounter (PDH.@)
993 PDH_STATUS WINAPI
PdhRemoveCounter( PDH_HCOUNTER handle
)
995 struct counter
*counter
= handle
;
997 TRACE("%p\n", handle
);
999 EnterCriticalSection( &pdh_handle_cs
);
1000 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
1002 LeaveCriticalSection( &pdh_handle_cs
);
1003 return PDH_INVALID_HANDLE
;
1006 list_remove( &counter
->entry
);
1007 destroy_counter( counter
);
1009 LeaveCriticalSection( &pdh_handle_cs
);
1010 return ERROR_SUCCESS
;
1013 /***********************************************************************
1014 * PdhSetCounterScaleFactor (PDH.@)
1016 PDH_STATUS WINAPI
PdhSetCounterScaleFactor( PDH_HCOUNTER handle
, LONG factor
)
1018 struct counter
*counter
= handle
;
1020 TRACE("%p\n", handle
);
1022 EnterCriticalSection( &pdh_handle_cs
);
1023 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
1025 LeaveCriticalSection( &pdh_handle_cs
);
1026 return PDH_INVALID_HANDLE
;
1028 if (factor
< PDH_MIN_SCALE
|| factor
> PDH_MAX_SCALE
)
1030 LeaveCriticalSection( &pdh_handle_cs
);
1031 return PDH_INVALID_ARGUMENT
;
1034 counter
->scale
= factor
;
1036 LeaveCriticalSection( &pdh_handle_cs
);
1037 return ERROR_SUCCESS
;
1040 /***********************************************************************
1041 * PdhValidatePathA (PDH.@)
1043 PDH_STATUS WINAPI
PdhValidatePathA( LPCSTR path
)
1048 TRACE("%s\n", debugstr_a(path
));
1050 if (!path
) return PDH_INVALID_ARGUMENT
;
1051 if (!(pathW
= pdh_strdup_aw( path
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
1053 ret
= PdhValidatePathW( pathW
);
1059 static PDH_STATUS
validate_path( LPCWSTR path
)
1061 if (!path
|| !*path
) return PDH_INVALID_ARGUMENT
;
1062 if (*path
++ != '\\' || !strchrW( path
, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME
;
1063 return ERROR_SUCCESS
;
1066 /***********************************************************************
1067 * PdhValidatePathW (PDH.@)
1069 PDH_STATUS WINAPI
PdhValidatePathW( LPCWSTR path
)
1074 TRACE("%s\n", debugstr_w(path
));
1076 if ((ret
= validate_path( path
))) return ret
;
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
;
1081 return PDH_CSTATUS_NO_COUNTER
;
1084 /***********************************************************************
1085 * PdhValidatePathExA (PDH.@)
1087 PDH_STATUS WINAPI
PdhValidatePathExA( PDH_HLOG source
, LPCSTR path
)
1089 TRACE("%p %s\n", source
, debugstr_a(path
));
1093 FIXME("log file data source not supported\n");
1094 return ERROR_SUCCESS
;
1096 return PdhValidatePathA( path
);
1099 /***********************************************************************
1100 * PdhValidatePathExW (PDH.@)
1102 PDH_STATUS WINAPI
PdhValidatePathExW( PDH_HLOG source
, LPCWSTR path
)
1104 TRACE("%p %s\n", source
, debugstr_w(path
));
1108 FIXME("log file data source not supported\n");
1109 return ERROR_SUCCESS
;
1111 return PdhValidatePathW( path
);
1114 /***********************************************************************
1115 * PdhMakeCounterPathA (PDH.@)
1117 PDH_STATUS WINAPI
PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A
*e
, LPSTR buffer
,
1118 LPDWORD buflen
, DWORD flags
)
1120 PDH_STATUS ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1121 PDH_COUNTER_PATH_ELEMENTS_W eW
;
1125 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1127 if (!e
|| !buflen
) return PDH_INVALID_ARGUMENT
;
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
;
1138 ret
= PdhMakeCounterPathW( &eW
, NULL
, &buflenW
, flags
);
1139 if (ret
== PDH_MORE_DATA
)
1141 if ((bufferW
= heap_alloc( buflenW
* sizeof(WCHAR
) )))
1143 if (!(ret
= PdhMakeCounterPathW( &eW
, bufferW
, &buflenW
, flags
)))
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
;
1150 heap_free( bufferW
);
1153 ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
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
);
1165 /***********************************************************************
1166 * PdhMakeCounterPathW (PDH.@)
1168 PDH_STATUS WINAPI
PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W
*e
, LPWSTR buffer
,
1169 LPDWORD buflen
, DWORD flags
)
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};
1177 WCHAR path
[PDH_MAX_COUNTER_NAME
], instance
[12];
1178 PDH_STATUS ret
= ERROR_SUCCESS
;
1181 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1183 if (flags
) FIXME("unimplemented flags 0x%08x\n", flags
);
1185 if (!e
|| !e
->szCounterName
|| !e
->szObjectName
|| !buflen
)
1186 return PDH_INVALID_ARGUMENT
;
1189 if (e
->szMachineName
)
1191 strcatW(path
, bslash
);
1192 strcatW(path
, bslash
);
1193 strcatW(path
, e
->szMachineName
);
1195 strcatW(path
, bslash
);
1196 strcatW(path
, e
->szObjectName
);
1197 if (e
->szInstanceName
)
1199 strcatW(path
, lparen
);
1200 if (e
->szParentInstance
)
1202 strcatW(path
, e
->szParentInstance
);
1203 strcatW(path
, fslash
);
1205 strcatW(path
, e
->szInstanceName
);
1206 sprintfW(instance
, fmt
, e
->dwInstanceIndex
);
1207 strcatW(path
, instance
);
1208 strcatW(path
, rparen
);
1210 strcatW(path
, bslash
);
1211 strcatW(path
, e
->szCounterName
);
1213 len
= strlenW(path
) + 1;
1214 if (*buflen
>= len
) strcpyW(buffer
, path
);
1215 else ret
= PDH_MORE_DATA
;
1220 /***********************************************************************
1221 * PdhEnumObjectItemsA (PDH.@)
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
)
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
);
1231 return PDH_NOT_IMPLEMENTED
;
1234 /***********************************************************************
1235 * PdhEnumObjectItemsW (PDH.@)
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
)
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
);
1245 return PDH_NOT_IMPLEMENTED
;
1248 /***********************************************************************
1249 * PdhSetDefaultRealTimeDataSource (PDH.@)
1251 PDH_STATUS WINAPI
PdhSetDefaultRealTimeDataSource( DWORD source
)
1253 FIXME("%u\n", source
);
1254 return ERROR_SUCCESS
;