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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
35 #include "wine/list.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(pdh
);
40 static CRITICAL_SECTION pdh_handle_cs
;
41 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug
=
44 { &pdh_handle_cs_debug
.ProcessLocksList
,
45 &pdh_handle_cs_debug
.ProcessLocksList
},
46 0, 0, { (DWORD_PTR
)(__FILE__
": pdh_handle_cs") }
48 static CRITICAL_SECTION pdh_handle_cs
= { &pdh_handle_cs_debug
, -1, 0, 0, 0, 0 };
50 static inline void *pdh_alloc( SIZE_T size
)
52 return HeapAlloc( GetProcessHeap(), 0, size
);
55 static inline void *pdh_alloc_zero( SIZE_T size
)
57 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
60 static inline void pdh_free( LPVOID mem
)
62 HeapFree( GetProcessHeap(), 0, mem
);
65 static inline WCHAR
*pdh_strdup( const WCHAR
*src
)
69 if (!src
) return NULL
;
70 if ((dst
= pdh_alloc( (strlenW( src
) + 1) * sizeof(WCHAR
) ))) strcpyW( dst
, src
);
74 static inline WCHAR
*pdh_strdup_aw( const char *src
)
79 if (!src
) return NULL
;
80 len
= MultiByteToWideChar( CP_ACP
, 0, src
, -1, NULL
, 0 );
81 if ((dst
= pdh_alloc( len
* sizeof(WCHAR
) ))) MultiByteToWideChar( CP_ACP
, 0, src
, -1, dst
, len
);
85 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
87 TRACE("(0x%p, %d, %p)\n",hinstDLL
,fdwReason
,lpvReserved
);
89 if (fdwReason
== DLL_WINE_PREATTACH
) return FALSE
; /* prefer native version */
91 if (fdwReason
== DLL_PROCESS_ATTACH
)
93 DisableThreadLibraryCalls( hinstDLL
);
108 DWORD magic
; /* signature */
109 struct list entry
; /* list entry */
110 WCHAR
*path
; /* identifier */
111 DWORD type
; /* counter type */
112 DWORD status
; /* update status */
113 LONG scale
; /* scale factor */
114 LONG defaultscale
; /* default scale factor */
115 DWORD_PTR user
; /* user data */
116 DWORD_PTR queryuser
; /* query user data */
117 LONGLONG base
; /* samples per second */
118 FILETIME stamp
; /* time stamp */
119 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
120 union value one
; /* first value */
121 union value two
; /* second value */
124 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
126 static struct counter
*create_counter( void )
128 struct counter
*counter
;
130 if ((counter
= pdh_alloc_zero( sizeof(struct counter
) )))
132 counter
->magic
= PDH_MAGIC_COUNTER
;
138 static void destroy_counter( struct counter
*counter
)
141 pdh_free( counter
->path
);
145 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
149 DWORD magic
; /* signature */
150 DWORD_PTR user
; /* user data */
151 HANDLE thread
; /* collect thread */
152 DWORD interval
; /* collect interval */
153 HANDLE wait
; /* wait event */
154 HANDLE stop
; /* stop event */
155 struct list counters
; /* counter list */
158 static struct query
*create_query( void )
162 if ((query
= pdh_alloc_zero( sizeof(struct query
) )))
164 query
->magic
= PDH_MAGIC_QUERY
;
165 list_init( &query
->counters
);
171 static void destroy_query( struct query
*query
)
179 DWORD index
; /* name index */
180 const WCHAR
*path
; /* identifier */
181 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
182 DWORD type
; /* counter type */
183 LONG scale
; /* default scale factor */
184 LONGLONG base
; /* samples per second */
187 static const WCHAR path_processor_time
[] =
188 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
189 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
190 static const WCHAR path_uptime
[] =
191 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
193 static void CALLBACK
collect_processor_time( struct counter
*counter
)
195 counter
->two
.largevalue
= 500000; /* FIXME */
196 counter
->status
= PDH_CSTATUS_VALID_DATA
;
199 static void CALLBACK
collect_uptime( struct counter
*counter
)
201 counter
->two
.largevalue
= GetTickCount64();
202 counter
->status
= PDH_CSTATUS_VALID_DATA
;
205 #define TYPE_PROCESSOR_TIME \
206 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
207 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
209 #define TYPE_UPTIME \
210 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
212 /* counter source registry */
213 static const struct source counter_sources
[] =
215 { 6, path_processor_time
, collect_processor_time
, TYPE_PROCESSOR_TIME
, -5, 10000000 },
216 { 674, path_uptime
, collect_uptime
, TYPE_UPTIME
, -3, 1000 }
219 static BOOL
pdh_match_path( LPCWSTR fullpath
, LPCWSTR path
)
223 if (strchrW( path
, '\\')) p
= fullpath
;
224 else p
= strrchrW( fullpath
, '\\' ) + 1;
225 if (strcmpW( p
, path
)) return FALSE
;
229 /***********************************************************************
230 * PdhAddCounterA (PDH.@)
232 PDH_STATUS WINAPI
PdhAddCounterA( PDH_HQUERY query
, LPCSTR path
,
233 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
238 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
240 if (!path
) return PDH_INVALID_ARGUMENT
;
242 if (!(pathW
= pdh_strdup_aw( path
)))
243 return PDH_MEMORY_ALLOCATION_FAILURE
;
245 ret
= PdhAddCounterW( query
, pathW
, userdata
, counter
);
251 /***********************************************************************
252 * PdhAddCounterW (PDH.@)
254 PDH_STATUS WINAPI
PdhAddCounterW( PDH_HQUERY hquery
, LPCWSTR path
,
255 DWORD_PTR userdata
, PDH_HCOUNTER
*hcounter
)
257 struct query
*query
= hquery
;
258 struct counter
*counter
;
261 TRACE("%p %s %lx %p\n", hquery
, debugstr_w(path
), userdata
, hcounter
);
263 if (!path
|| !hcounter
) return PDH_INVALID_ARGUMENT
;
265 EnterCriticalSection( &pdh_handle_cs
);
266 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
268 LeaveCriticalSection( &pdh_handle_cs
);
269 return PDH_INVALID_HANDLE
;
273 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
275 if (pdh_match_path( counter_sources
[i
].path
, path
))
277 if ((counter
= create_counter()))
279 counter
->path
= pdh_strdup( counter_sources
[i
].path
);
280 counter
->collect
= counter_sources
[i
].collect
;
281 counter
->type
= counter_sources
[i
].type
;
282 counter
->defaultscale
= counter_sources
[i
].scale
;
283 counter
->base
= counter_sources
[i
].base
;
284 counter
->queryuser
= query
->user
;
285 counter
->user
= userdata
;
287 list_add_tail( &query
->counters
, &counter
->entry
);
290 LeaveCriticalSection( &pdh_handle_cs
);
291 return ERROR_SUCCESS
;
293 LeaveCriticalSection( &pdh_handle_cs
);
294 return PDH_MEMORY_ALLOCATION_FAILURE
;
297 LeaveCriticalSection( &pdh_handle_cs
);
298 return PDH_CSTATUS_NO_COUNTER
;
301 /***********************************************************************
302 * PdhAddEnglishCounterA (PDH.@)
304 PDH_STATUS WINAPI
PdhAddEnglishCounterA( PDH_HQUERY query
, LPCSTR path
,
305 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
307 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
309 if (!query
) return PDH_INVALID_ARGUMENT
;
310 return PdhAddCounterA( query
, path
, userdata
, counter
);
313 /***********************************************************************
314 * PdhAddEnglishCounterW (PDH.@)
316 PDH_STATUS WINAPI
PdhAddEnglishCounterW( PDH_HQUERY query
, LPCWSTR path
,
317 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
319 TRACE("%p %s %lx %p\n", query
, debugstr_w(path
), userdata
, counter
);
321 if (!query
) return PDH_INVALID_ARGUMENT
;
322 return PdhAddCounterW( query
, path
, userdata
, counter
);
325 /* caller must hold counter lock */
326 static PDH_STATUS
format_value( struct counter
*counter
, DWORD format
, union value
*raw1
,
327 union value
*raw2
, PDH_FMT_COUNTERVALUE
*value
)
331 factor
= counter
->scale
? counter
->scale
: counter
->defaultscale
;
332 if (format
& PDH_FMT_LONG
)
334 if (format
& PDH_FMT_1000
) value
->u
.longValue
= raw2
->longvalue
* 1000;
335 else value
->u
.longValue
= raw2
->longvalue
* pow( 10, factor
);
337 else if (format
& PDH_FMT_LARGE
)
339 if (format
& PDH_FMT_1000
) value
->u
.largeValue
= raw2
->largevalue
* 1000;
340 else value
->u
.largeValue
= raw2
->largevalue
* pow( 10, factor
);
342 else if (format
& PDH_FMT_DOUBLE
)
344 if (format
& PDH_FMT_1000
) value
->u
.doubleValue
= raw2
->doublevalue
* 1000;
345 else value
->u
.doubleValue
= raw2
->doublevalue
* pow( 10, factor
);
349 WARN("unknown format %x\n", format
);
350 return PDH_INVALID_ARGUMENT
;
352 return ERROR_SUCCESS
;
355 /***********************************************************************
356 * PdhCalculateCounterFromRawValue (PDH.@)
358 PDH_STATUS WINAPI
PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle
, DWORD format
,
359 PPDH_RAW_COUNTER raw1
, PPDH_RAW_COUNTER raw2
,
360 PPDH_FMT_COUNTERVALUE value
)
363 struct counter
*counter
= handle
;
365 TRACE("%p 0x%08x %p %p %p\n", handle
, format
, raw1
, raw2
, value
);
367 if (!value
) return PDH_INVALID_ARGUMENT
;
369 EnterCriticalSection( &pdh_handle_cs
);
370 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
372 LeaveCriticalSection( &pdh_handle_cs
);
373 return PDH_INVALID_HANDLE
;
376 ret
= format_value( counter
, format
, (union value
*)&raw1
->SecondValue
,
377 (union value
*)&raw2
->SecondValue
, value
);
379 LeaveCriticalSection( &pdh_handle_cs
);
383 /* caller must hold query lock */
384 static void shutdown_query_thread( struct query
*query
)
386 SetEvent( query
->stop
);
387 WaitForSingleObject( query
->thread
, INFINITE
);
389 CloseHandle( query
->stop
);
390 CloseHandle( query
->thread
);
392 query
->thread
= NULL
;
395 /***********************************************************************
396 * PdhCloseQuery (PDH.@)
398 PDH_STATUS WINAPI
PdhCloseQuery( PDH_HQUERY handle
)
400 struct query
*query
= handle
;
401 struct list
*item
, *next
;
403 TRACE("%p\n", handle
);
405 EnterCriticalSection( &pdh_handle_cs
);
406 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
408 LeaveCriticalSection( &pdh_handle_cs
);
409 return PDH_INVALID_HANDLE
;
412 if (query
->thread
) shutdown_query_thread( query
);
414 LIST_FOR_EACH_SAFE( item
, next
, &query
->counters
)
416 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
418 list_remove( &counter
->entry
);
419 destroy_counter( counter
);
422 destroy_query( query
);
424 LeaveCriticalSection( &pdh_handle_cs
);
425 return ERROR_SUCCESS
;
428 /* caller must hold query lock */
429 static void collect_query_data( struct query
*query
)
433 LIST_FOR_EACH( item
, &query
->counters
)
436 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
438 counter
->collect( counter
);
440 GetLocalTime( &time
);
441 SystemTimeToFileTime( &time
, &counter
->stamp
);
445 /***********************************************************************
446 * PdhCollectQueryData (PDH.@)
448 PDH_STATUS WINAPI
PdhCollectQueryData( PDH_HQUERY handle
)
450 struct query
*query
= handle
;
452 TRACE("%p\n", handle
);
454 EnterCriticalSection( &pdh_handle_cs
);
455 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
457 LeaveCriticalSection( &pdh_handle_cs
);
458 return PDH_INVALID_HANDLE
;
461 if (list_empty( &query
->counters
))
463 LeaveCriticalSection( &pdh_handle_cs
);
467 collect_query_data( query
);
469 LeaveCriticalSection( &pdh_handle_cs
);
470 return ERROR_SUCCESS
;
473 static DWORD CALLBACK
collect_query_thread( void *arg
)
475 struct query
*query
= arg
;
476 DWORD interval
= query
->interval
;
477 HANDLE stop
= query
->stop
;
482 if (WaitForSingleObject( stop
, interval
) != WAIT_TIMEOUT
) ExitThread( 0 );
484 EnterCriticalSection( &pdh_handle_cs
);
485 if (query
->magic
!= PDH_MAGIC_QUERY
)
487 LeaveCriticalSection( &pdh_handle_cs
);
488 ExitThread( PDH_INVALID_HANDLE
);
491 collect_query_data( query
);
493 if (!SetEvent( query
->wait
))
495 LeaveCriticalSection( &pdh_handle_cs
);
498 LeaveCriticalSection( &pdh_handle_cs
);
502 /***********************************************************************
503 * PdhCollectQueryDataEx (PDH.@)
505 PDH_STATUS WINAPI
PdhCollectQueryDataEx( PDH_HQUERY handle
, DWORD interval
, HANDLE event
)
508 struct query
*query
= handle
;
510 TRACE("%p %d %p\n", handle
, interval
, event
);
512 EnterCriticalSection( &pdh_handle_cs
);
513 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
515 LeaveCriticalSection( &pdh_handle_cs
);
516 return PDH_INVALID_HANDLE
;
518 if (list_empty( &query
->counters
))
520 LeaveCriticalSection( &pdh_handle_cs
);
523 if (query
->thread
) shutdown_query_thread( query
);
524 if (!(query
->stop
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
526 ret
= GetLastError();
527 LeaveCriticalSection( &pdh_handle_cs
);
531 query
->interval
= interval
* 1000;
532 if (!(query
->thread
= CreateThread( NULL
, 0, collect_query_thread
, query
, 0, NULL
)))
534 ret
= GetLastError();
535 CloseHandle( query
->stop
);
537 LeaveCriticalSection( &pdh_handle_cs
);
540 WaitForSingleObject( query
->stop
, INFINITE
);
542 LeaveCriticalSection( &pdh_handle_cs
);
543 return ERROR_SUCCESS
;
546 /***********************************************************************
547 * PdhCollectQueryDataWithTime (PDH.@)
549 PDH_STATUS WINAPI
PdhCollectQueryDataWithTime( PDH_HQUERY handle
, LONGLONG
*timestamp
)
551 struct query
*query
= handle
;
552 struct counter
*counter
;
555 TRACE("%p %p\n", handle
, timestamp
);
557 if (!timestamp
) return PDH_INVALID_ARGUMENT
;
559 EnterCriticalSection( &pdh_handle_cs
);
560 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
562 LeaveCriticalSection( &pdh_handle_cs
);
563 return PDH_INVALID_HANDLE
;
565 if (list_empty( &query
->counters
))
567 LeaveCriticalSection( &pdh_handle_cs
);
571 collect_query_data( query
);
573 item
= list_head( &query
->counters
);
574 counter
= LIST_ENTRY( item
, struct counter
, entry
);
576 *timestamp
= ((LONGLONG
)counter
->stamp
.dwHighDateTime
<< 32) | counter
->stamp
.dwLowDateTime
;
578 LeaveCriticalSection( &pdh_handle_cs
);
579 return ERROR_SUCCESS
;
582 /***********************************************************************
583 * PdhGetCounterInfoA (PDH.@)
585 PDH_STATUS WINAPI
PdhGetCounterInfoA( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_A info
)
587 struct counter
*counter
= handle
;
589 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
591 EnterCriticalSection( &pdh_handle_cs
);
592 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
594 LeaveCriticalSection( &pdh_handle_cs
);
595 return PDH_INVALID_HANDLE
;
599 LeaveCriticalSection( &pdh_handle_cs
);
600 return PDH_INVALID_ARGUMENT
;
602 if (*size
< sizeof(PDH_COUNTER_INFO_A
))
604 *size
= sizeof(PDH_COUNTER_INFO_A
);
605 LeaveCriticalSection( &pdh_handle_cs
);
606 return PDH_MORE_DATA
;
609 memset( info
, 0, sizeof(PDH_COUNTER_INFO_A
) );
611 info
->dwType
= counter
->type
;
612 info
->CStatus
= counter
->status
;
613 info
->lScale
= counter
->scale
;
614 info
->lDefaultScale
= counter
->defaultscale
;
615 info
->dwUserData
= counter
->user
;
616 info
->dwQueryUserData
= counter
->queryuser
;
618 *size
= sizeof(PDH_COUNTER_INFO_A
);
620 LeaveCriticalSection( &pdh_handle_cs
);
621 return ERROR_SUCCESS
;
624 /***********************************************************************
625 * PdhGetCounterInfoW (PDH.@)
627 PDH_STATUS WINAPI
PdhGetCounterInfoW( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_W info
)
629 struct counter
*counter
= handle
;
631 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
633 EnterCriticalSection( &pdh_handle_cs
);
634 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
636 LeaveCriticalSection( &pdh_handle_cs
);
637 return PDH_INVALID_HANDLE
;
641 LeaveCriticalSection( &pdh_handle_cs
);
642 return PDH_INVALID_ARGUMENT
;
644 if (*size
< sizeof(PDH_COUNTER_INFO_W
))
646 *size
= sizeof(PDH_COUNTER_INFO_W
);
647 LeaveCriticalSection( &pdh_handle_cs
);
648 return PDH_MORE_DATA
;
651 memset( info
, 0, sizeof(PDH_COUNTER_INFO_W
) );
653 info
->dwType
= counter
->type
;
654 info
->CStatus
= counter
->status
;
655 info
->lScale
= counter
->scale
;
656 info
->lDefaultScale
= counter
->defaultscale
;
657 info
->dwUserData
= counter
->user
;
658 info
->dwQueryUserData
= counter
->queryuser
;
660 *size
= sizeof(PDH_COUNTER_INFO_W
);
662 LeaveCriticalSection( &pdh_handle_cs
);
663 return ERROR_SUCCESS
;
666 /***********************************************************************
667 * PdhGetCounterTimeBase (PDH.@)
669 PDH_STATUS WINAPI
PdhGetCounterTimeBase( PDH_HCOUNTER handle
, LONGLONG
*base
)
671 struct counter
*counter
= handle
;
673 TRACE("%p %p\n", handle
, base
);
675 if (!base
) return PDH_INVALID_ARGUMENT
;
677 EnterCriticalSection( &pdh_handle_cs
);
678 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
680 LeaveCriticalSection( &pdh_handle_cs
);
681 return PDH_INVALID_HANDLE
;
684 *base
= counter
->base
;
686 LeaveCriticalSection( &pdh_handle_cs
);
687 return ERROR_SUCCESS
;
690 /***********************************************************************
691 * PdhGetFormattedCounterValue (PDH.@)
693 PDH_STATUS WINAPI
PdhGetFormattedCounterValue( PDH_HCOUNTER handle
, DWORD format
,
694 LPDWORD type
, PPDH_FMT_COUNTERVALUE value
)
697 struct counter
*counter
= handle
;
699 TRACE("%p %x %p %p\n", handle
, format
, type
, value
);
701 if (!value
) return PDH_INVALID_ARGUMENT
;
703 EnterCriticalSection( &pdh_handle_cs
);
704 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
706 LeaveCriticalSection( &pdh_handle_cs
);
707 return PDH_INVALID_HANDLE
;
711 LeaveCriticalSection( &pdh_handle_cs
);
712 return PDH_INVALID_DATA
;
714 if (!(ret
= format_value( counter
, format
, &counter
->one
, &counter
->two
, value
)))
716 value
->CStatus
= ERROR_SUCCESS
;
717 if (type
) *type
= counter
->type
;
720 LeaveCriticalSection( &pdh_handle_cs
);
724 /***********************************************************************
725 * PdhGetRawCounterValue (PDH.@)
727 PDH_STATUS WINAPI
PdhGetRawCounterValue( PDH_HCOUNTER handle
, LPDWORD type
,
728 PPDH_RAW_COUNTER value
)
730 struct counter
*counter
= handle
;
732 TRACE("%p %p %p\n", handle
, type
, value
);
734 if (!value
) return PDH_INVALID_ARGUMENT
;
736 EnterCriticalSection( &pdh_handle_cs
);
737 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
739 LeaveCriticalSection( &pdh_handle_cs
);
740 return PDH_INVALID_HANDLE
;
743 value
->CStatus
= counter
->status
;
744 value
->TimeStamp
.dwLowDateTime
= counter
->stamp
.dwLowDateTime
;
745 value
->TimeStamp
.dwHighDateTime
= counter
->stamp
.dwHighDateTime
;
746 value
->FirstValue
= counter
->one
.largevalue
;
747 value
->SecondValue
= counter
->two
.largevalue
;
748 value
->MultiCount
= 1; /* FIXME */
750 if (type
) *type
= counter
->type
;
752 LeaveCriticalSection( &pdh_handle_cs
);
753 return ERROR_SUCCESS
;
756 /***********************************************************************
757 * PdhLookupPerfIndexByNameA (PDH.@)
759 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameA( LPCSTR machine
, LPCSTR name
, LPDWORD index
)
764 TRACE("%s %s %p\n", debugstr_a(machine
), debugstr_a(name
), index
);
766 if (!name
|| !index
) return PDH_INVALID_ARGUMENT
;
770 FIXME("remote machine not supported\n");
771 return PDH_CSTATUS_NO_MACHINE
;
773 if (!(nameW
= pdh_strdup_aw( name
)))
774 return PDH_MEMORY_ALLOCATION_FAILURE
;
776 ret
= PdhLookupPerfIndexByNameW( NULL
, nameW
, index
);
782 /***********************************************************************
783 * PdhLookupPerfIndexByNameW (PDH.@)
785 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameW( LPCWSTR machine
, LPCWSTR name
, LPDWORD index
)
789 TRACE("%s %s %p\n", debugstr_w(machine
), debugstr_w(name
), index
);
791 if (!name
|| !index
) return PDH_INVALID_ARGUMENT
;
795 FIXME("remote machine not supported\n");
796 return PDH_CSTATUS_NO_MACHINE
;
798 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
800 if (pdh_match_path( counter_sources
[i
].path
, name
))
802 *index
= counter_sources
[i
].index
;
803 return ERROR_SUCCESS
;
806 return PDH_STRING_NOT_FOUND
;
809 /***********************************************************************
810 * PdhLookupPerfNameByIndexA (PDH.@)
812 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexA( LPCSTR machine
, DWORD index
, LPSTR buffer
, LPDWORD size
)
815 WCHAR bufferW
[PDH_MAX_COUNTER_NAME
];
816 DWORD sizeW
= sizeof(bufferW
) / sizeof(WCHAR
);
818 TRACE("%s %d %p %p\n", debugstr_a(machine
), index
, buffer
, size
);
822 FIXME("remote machine not supported\n");
823 return PDH_CSTATUS_NO_MACHINE
;
826 if (!buffer
&& !size
) return PDH_INVALID_ARGUMENT
;
827 if (!index
) return ERROR_SUCCESS
;
829 if (!(ret
= PdhLookupPerfNameByIndexW( NULL
, index
, bufferW
, &sizeW
)))
831 int required
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
833 if (size
&& *size
< required
) ret
= PDH_MORE_DATA
;
834 else WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, required
, NULL
, NULL
);
835 if (size
) *size
= required
;
840 /***********************************************************************
841 * PdhLookupPerfNameByIndexW (PDH.@)
843 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexW( LPCWSTR machine
, DWORD index
, LPWSTR buffer
, LPDWORD size
)
848 TRACE("%s %d %p %p\n", debugstr_w(machine
), index
, buffer
, size
);
852 FIXME("remote machine not supported\n");
853 return PDH_CSTATUS_NO_MACHINE
;
856 if (!buffer
&& !size
) return PDH_INVALID_ARGUMENT
;
857 if (!index
) return ERROR_SUCCESS
;
859 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
861 if (counter_sources
[i
].index
== index
)
863 WCHAR
*p
= strrchrW( counter_sources
[i
].path
, '\\' ) + 1;
864 unsigned int required
= strlenW( p
) + 1;
866 if (*size
< required
) ret
= PDH_MORE_DATA
;
869 strcpyW( buffer
, p
);
876 return PDH_INVALID_ARGUMENT
;
879 /***********************************************************************
880 * PdhOpenQueryA (PDH.@)
882 PDH_STATUS WINAPI
PdhOpenQueryA( LPCSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*query
)
885 WCHAR
*sourceW
= NULL
;
887 TRACE("%s %lx %p\n", debugstr_a(source
), userdata
, query
);
889 if (source
&& !(sourceW
= pdh_strdup_aw( source
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
891 ret
= PdhOpenQueryW( sourceW
, userdata
, query
);
897 /***********************************************************************
898 * PdhOpenQueryW (PDH.@)
900 PDH_STATUS WINAPI
PdhOpenQueryW( LPCWSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*handle
)
904 TRACE("%s %lx %p\n", debugstr_w(source
), userdata
, handle
);
906 if (!handle
) return PDH_INVALID_ARGUMENT
;
910 FIXME("log file data source not supported\n");
911 return PDH_INVALID_ARGUMENT
;
913 if ((query
= create_query()))
915 query
->user
= userdata
;
918 return ERROR_SUCCESS
;
920 return PDH_MEMORY_ALLOCATION_FAILURE
;
923 /***********************************************************************
924 * PdhRemoveCounter (PDH.@)
926 PDH_STATUS WINAPI
PdhRemoveCounter( PDH_HCOUNTER handle
)
928 struct counter
*counter
= handle
;
930 TRACE("%p\n", handle
);
932 EnterCriticalSection( &pdh_handle_cs
);
933 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
935 LeaveCriticalSection( &pdh_handle_cs
);
936 return PDH_INVALID_HANDLE
;
939 list_remove( &counter
->entry
);
940 destroy_counter( counter
);
942 LeaveCriticalSection( &pdh_handle_cs
);
943 return ERROR_SUCCESS
;
946 /***********************************************************************
947 * PdhSetCounterScaleFactor (PDH.@)
949 PDH_STATUS WINAPI
PdhSetCounterScaleFactor( PDH_HCOUNTER handle
, LONG factor
)
951 struct counter
*counter
= handle
;
953 TRACE("%p\n", handle
);
955 EnterCriticalSection( &pdh_handle_cs
);
956 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
958 LeaveCriticalSection( &pdh_handle_cs
);
959 return PDH_INVALID_HANDLE
;
961 if (factor
< PDH_MIN_SCALE
|| factor
> PDH_MAX_SCALE
)
963 LeaveCriticalSection( &pdh_handle_cs
);
964 return PDH_INVALID_ARGUMENT
;
967 counter
->scale
= factor
;
969 LeaveCriticalSection( &pdh_handle_cs
);
970 return ERROR_SUCCESS
;
973 /***********************************************************************
974 * PdhValidatePathA (PDH.@)
976 PDH_STATUS WINAPI
PdhValidatePathA( LPCSTR path
)
981 TRACE("%s\n", debugstr_a(path
));
983 if (!path
) return PDH_INVALID_ARGUMENT
;
984 if (!(pathW
= pdh_strdup_aw( path
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
986 ret
= PdhValidatePathW( pathW
);
992 static PDH_STATUS
validate_path( LPCWSTR path
)
994 if (!path
|| !*path
) return PDH_INVALID_ARGUMENT
;
995 if (*path
++ != '\\' || !strchrW( path
, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME
;
996 return ERROR_SUCCESS
;
999 /***********************************************************************
1000 * PdhValidatePathW (PDH.@)
1002 PDH_STATUS WINAPI
PdhValidatePathW( LPCWSTR path
)
1007 TRACE("%s\n", debugstr_w(path
));
1009 if ((ret
= validate_path( path
))) return ret
;
1011 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
1012 if (pdh_match_path( counter_sources
[i
].path
, path
)) return ERROR_SUCCESS
;
1014 return PDH_CSTATUS_NO_COUNTER
;
1017 /***********************************************************************
1018 * PdhValidatePathExA (PDH.@)
1020 PDH_STATUS WINAPI
PdhValidatePathExA( PDH_HLOG source
, LPCSTR path
)
1022 TRACE("%p %s\n", source
, debugstr_a(path
));
1026 FIXME("log file data source not supported\n");
1027 return ERROR_SUCCESS
;
1029 return PdhValidatePathA( path
);
1032 /***********************************************************************
1033 * PdhValidatePathExW (PDH.@)
1035 PDH_STATUS WINAPI
PdhValidatePathExW( PDH_HLOG source
, LPCWSTR path
)
1037 TRACE("%p %s\n", source
, debugstr_w(path
));
1041 FIXME("log file data source not supported\n");
1042 return ERROR_SUCCESS
;
1044 return PdhValidatePathW( path
);