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
34 #include "wine/debug.h"
35 #include "wine/heap.h"
36 #include "wine/list.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 WCHAR
*pdh_strdup( const WCHAR
*src
)
54 if (!src
) return NULL
;
55 if ((dst
= heap_alloc( (lstrlenW( src
) + 1) * sizeof(WCHAR
) ))) lstrcpyW( dst
, src
);
59 static inline WCHAR
*pdh_strdup_aw( const char *src
)
64 if (!src
) return NULL
;
65 len
= MultiByteToWideChar( CP_ACP
, 0, src
, -1, NULL
, 0 );
66 if ((dst
= heap_alloc( len
* sizeof(WCHAR
) ))) MultiByteToWideChar( CP_ACP
, 0, src
, -1, dst
, len
);
79 DWORD magic
; /* signature */
80 struct list entry
; /* list entry */
81 WCHAR
*path
; /* identifier */
82 DWORD type
; /* counter type */
83 DWORD status
; /* update status */
84 LONG scale
; /* scale factor */
85 LONG defaultscale
; /* default scale factor */
86 DWORD_PTR user
; /* user data */
87 DWORD_PTR queryuser
; /* query user data */
88 LONGLONG base
; /* samples per second */
89 FILETIME stamp
; /* time stamp */
90 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
91 union value one
; /* first value */
92 union value two
; /* second value */
95 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
97 static struct counter
*create_counter( void )
99 struct counter
*counter
;
101 if ((counter
= heap_alloc_zero( sizeof(struct counter
) )))
103 counter
->magic
= PDH_MAGIC_COUNTER
;
109 static void destroy_counter( struct counter
*counter
)
112 heap_free( counter
->path
);
113 heap_free( counter
);
116 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
120 DWORD magic
; /* signature */
121 DWORD_PTR user
; /* user data */
122 HANDLE thread
; /* collect thread */
123 DWORD interval
; /* collect interval */
124 HANDLE wait
; /* wait event */
125 HANDLE stop
; /* stop event */
126 struct list counters
; /* counter list */
129 static struct query
*create_query( void )
133 if ((query
= heap_alloc_zero( sizeof(struct query
) )))
135 query
->magic
= PDH_MAGIC_QUERY
;
136 list_init( &query
->counters
);
142 static void destroy_query( struct query
*query
)
150 DWORD index
; /* name index */
151 const WCHAR
*path
; /* identifier */
152 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
153 DWORD type
; /* counter type */
154 LONG scale
; /* default scale factor */
155 LONGLONG base
; /* samples per second */
158 static void CALLBACK
collect_processor_time( struct counter
*counter
)
160 counter
->two
.largevalue
= 500000; /* FIXME */
161 counter
->status
= PDH_CSTATUS_VALID_DATA
;
164 static void CALLBACK
collect_uptime( struct counter
*counter
)
166 counter
->two
.largevalue
= GetTickCount64();
167 counter
->status
= PDH_CSTATUS_VALID_DATA
;
170 #define TYPE_PROCESSOR_TIME \
171 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
172 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
174 #define TYPE_UPTIME \
175 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
177 /* counter source registry */
178 static const struct source counter_sources
[] =
180 { 6, L
"\\Processor(_Total)\\% Processor Time", collect_processor_time
, TYPE_PROCESSOR_TIME
, -5, 10000000 },
181 { 674, L
"\\System\\System Up Time", collect_uptime
, TYPE_UPTIME
, -3, 1000 }
184 static BOOL
is_local_machine( const WCHAR
*name
, DWORD len
)
186 WCHAR buf
[MAX_COMPUTERNAME_LENGTH
+ 1];
187 DWORD buflen
= ARRAY_SIZE(buf
);
189 if (!GetComputerNameW( buf
, &buflen
)) return FALSE
;
190 return len
== buflen
&& !wcsnicmp( name
, buf
, buflen
);
193 static BOOL
pdh_match_path( LPCWSTR fullpath
, LPCWSTR path
)
197 if (path
[0] == '\\' && path
[1] == '\\' && (p
= wcschr( path
+ 2, '\\' )) &&
198 is_local_machine( path
+ 2, p
- path
- 2 ))
202 if (wcschr( path
, '\\' )) p
= fullpath
;
203 else p
= wcsrchr( fullpath
, '\\' ) + 1;
204 return !wcscmp( p
, path
);
207 /***********************************************************************
208 * PdhAddCounterA (PDH.@)
210 PDH_STATUS WINAPI
PdhAddCounterA( PDH_HQUERY query
, LPCSTR path
,
211 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
216 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
218 if (!path
) return PDH_INVALID_ARGUMENT
;
220 if (!(pathW
= pdh_strdup_aw( path
)))
221 return PDH_MEMORY_ALLOCATION_FAILURE
;
223 ret
= PdhAddCounterW( query
, pathW
, userdata
, counter
);
229 /***********************************************************************
230 * PdhAddCounterW (PDH.@)
232 PDH_STATUS WINAPI
PdhAddCounterW( PDH_HQUERY hquery
, LPCWSTR path
,
233 DWORD_PTR userdata
, PDH_HCOUNTER
*hcounter
)
235 struct query
*query
= hquery
;
236 struct counter
*counter
;
239 TRACE("%p %s %lx %p\n", hquery
, debugstr_w(path
), userdata
, hcounter
);
241 if (!path
|| !hcounter
) return PDH_INVALID_ARGUMENT
;
243 EnterCriticalSection( &pdh_handle_cs
);
244 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
246 LeaveCriticalSection( &pdh_handle_cs
);
247 return PDH_INVALID_HANDLE
;
251 for (i
= 0; i
< ARRAY_SIZE(counter_sources
); i
++)
253 if (pdh_match_path( counter_sources
[i
].path
, path
))
255 if ((counter
= create_counter()))
257 counter
->path
= pdh_strdup( counter_sources
[i
].path
);
258 counter
->collect
= counter_sources
[i
].collect
;
259 counter
->type
= counter_sources
[i
].type
;
260 counter
->defaultscale
= counter_sources
[i
].scale
;
261 counter
->base
= counter_sources
[i
].base
;
262 counter
->queryuser
= query
->user
;
263 counter
->user
= userdata
;
265 list_add_tail( &query
->counters
, &counter
->entry
);
268 LeaveCriticalSection( &pdh_handle_cs
);
269 return ERROR_SUCCESS
;
271 LeaveCriticalSection( &pdh_handle_cs
);
272 return PDH_MEMORY_ALLOCATION_FAILURE
;
275 LeaveCriticalSection( &pdh_handle_cs
);
276 return PDH_CSTATUS_NO_COUNTER
;
279 /***********************************************************************
280 * PdhAddEnglishCounterA (PDH.@)
282 PDH_STATUS WINAPI
PdhAddEnglishCounterA( PDH_HQUERY query
, LPCSTR path
,
283 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
285 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
287 if (!query
) return PDH_INVALID_ARGUMENT
;
288 return PdhAddCounterA( query
, path
, userdata
, counter
);
291 /***********************************************************************
292 * PdhAddEnglishCounterW (PDH.@)
294 PDH_STATUS WINAPI
PdhAddEnglishCounterW( PDH_HQUERY query
, LPCWSTR path
,
295 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
297 TRACE("%p %s %lx %p\n", query
, debugstr_w(path
), userdata
, counter
);
299 if (!query
) return PDH_INVALID_ARGUMENT
;
300 return PdhAddCounterW( query
, path
, userdata
, counter
);
303 /* caller must hold counter lock */
304 static PDH_STATUS
format_value( struct counter
*counter
, DWORD format
, union value
*raw1
,
305 union value
*raw2
, PDH_FMT_COUNTERVALUE
*value
)
309 factor
= counter
->scale
? counter
->scale
: counter
->defaultscale
;
310 if (format
& PDH_FMT_LONG
)
312 if (format
& PDH_FMT_1000
) value
->u
.longValue
= raw2
->longvalue
* 1000;
313 else value
->u
.longValue
= raw2
->longvalue
* pow( 10, factor
);
315 else if (format
& PDH_FMT_LARGE
)
317 if (format
& PDH_FMT_1000
) value
->u
.largeValue
= raw2
->largevalue
* 1000;
318 else value
->u
.largeValue
= raw2
->largevalue
* pow( 10, factor
);
320 else if (format
& PDH_FMT_DOUBLE
)
322 if (format
& PDH_FMT_1000
) value
->u
.doubleValue
= raw2
->doublevalue
* 1000;
323 else value
->u
.doubleValue
= raw2
->doublevalue
* pow( 10, factor
);
327 WARN("unknown format %x\n", format
);
328 return PDH_INVALID_ARGUMENT
;
330 return ERROR_SUCCESS
;
333 /***********************************************************************
334 * PdhCalculateCounterFromRawValue (PDH.@)
336 PDH_STATUS WINAPI
PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle
, DWORD format
,
337 PPDH_RAW_COUNTER raw1
, PPDH_RAW_COUNTER raw2
,
338 PPDH_FMT_COUNTERVALUE value
)
341 struct counter
*counter
= handle
;
343 TRACE("%p 0x%08x %p %p %p\n", handle
, format
, raw1
, raw2
, value
);
345 if (!value
) return PDH_INVALID_ARGUMENT
;
347 EnterCriticalSection( &pdh_handle_cs
);
348 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
350 LeaveCriticalSection( &pdh_handle_cs
);
351 return PDH_INVALID_HANDLE
;
354 ret
= format_value( counter
, format
, (union value
*)&raw1
->SecondValue
,
355 (union value
*)&raw2
->SecondValue
, value
);
357 LeaveCriticalSection( &pdh_handle_cs
);
362 /***********************************************************************
363 * PdhCloseQuery (PDH.@)
365 PDH_STATUS WINAPI
PdhCloseQuery( PDH_HQUERY handle
)
367 struct query
*query
= handle
;
368 struct list
*item
, *next
;
370 TRACE("%p\n", handle
);
372 EnterCriticalSection( &pdh_handle_cs
);
373 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
375 LeaveCriticalSection( &pdh_handle_cs
);
376 return PDH_INVALID_HANDLE
;
381 HANDLE thread
= query
->thread
;
382 SetEvent( query
->stop
);
383 LeaveCriticalSection( &pdh_handle_cs
);
385 WaitForSingleObject( thread
, INFINITE
);
387 EnterCriticalSection( &pdh_handle_cs
);
388 if (query
->magic
!= PDH_MAGIC_QUERY
)
390 LeaveCriticalSection( &pdh_handle_cs
);
391 return ERROR_SUCCESS
;
393 CloseHandle( query
->stop
);
394 CloseHandle( query
->thread
);
395 query
->thread
= NULL
;
398 LIST_FOR_EACH_SAFE( item
, next
, &query
->counters
)
400 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
402 list_remove( &counter
->entry
);
403 destroy_counter( counter
);
406 destroy_query( query
);
408 LeaveCriticalSection( &pdh_handle_cs
);
409 return ERROR_SUCCESS
;
412 /* caller must hold query lock */
413 static void collect_query_data( struct query
*query
)
417 LIST_FOR_EACH( item
, &query
->counters
)
420 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
422 counter
->collect( counter
);
424 GetLocalTime( &time
);
425 SystemTimeToFileTime( &time
, &counter
->stamp
);
429 /***********************************************************************
430 * PdhCollectQueryData (PDH.@)
432 PDH_STATUS WINAPI
PdhCollectQueryData( PDH_HQUERY handle
)
434 struct query
*query
= handle
;
436 TRACE("%p\n", handle
);
438 EnterCriticalSection( &pdh_handle_cs
);
439 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
441 LeaveCriticalSection( &pdh_handle_cs
);
442 return PDH_INVALID_HANDLE
;
445 if (list_empty( &query
->counters
))
447 LeaveCriticalSection( &pdh_handle_cs
);
451 collect_query_data( query
);
453 LeaveCriticalSection( &pdh_handle_cs
);
454 return ERROR_SUCCESS
;
457 static DWORD CALLBACK
collect_query_thread( void *arg
)
459 struct query
*query
= arg
;
460 DWORD interval
= query
->interval
;
461 HANDLE stop
= query
->stop
;
465 if (WaitForSingleObject( stop
, interval
) != WAIT_TIMEOUT
) ExitThread( 0 );
467 EnterCriticalSection( &pdh_handle_cs
);
468 if (query
->magic
!= PDH_MAGIC_QUERY
)
470 LeaveCriticalSection( &pdh_handle_cs
);
471 ExitThread( PDH_INVALID_HANDLE
);
474 collect_query_data( query
);
476 if (!SetEvent( query
->wait
))
478 LeaveCriticalSection( &pdh_handle_cs
);
481 LeaveCriticalSection( &pdh_handle_cs
);
485 /***********************************************************************
486 * PdhCollectQueryDataEx (PDH.@)
488 PDH_STATUS WINAPI
PdhCollectQueryDataEx( PDH_HQUERY handle
, DWORD interval
, HANDLE event
)
491 struct query
*query
= handle
;
493 TRACE("%p %d %p\n", handle
, interval
, event
);
495 EnterCriticalSection( &pdh_handle_cs
);
496 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
498 LeaveCriticalSection( &pdh_handle_cs
);
499 return PDH_INVALID_HANDLE
;
501 if (list_empty( &query
->counters
))
503 LeaveCriticalSection( &pdh_handle_cs
);
508 HANDLE thread
= query
->thread
;
509 SetEvent( query
->stop
);
510 LeaveCriticalSection( &pdh_handle_cs
);
512 WaitForSingleObject( thread
, INFINITE
);
514 EnterCriticalSection( &pdh_handle_cs
);
515 if (query
->magic
!= PDH_MAGIC_QUERY
)
517 LeaveCriticalSection( &pdh_handle_cs
);
518 return PDH_INVALID_HANDLE
;
520 CloseHandle( query
->thread
);
521 query
->thread
= NULL
;
523 else if (!(query
->stop
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
525 ret
= GetLastError();
526 LeaveCriticalSection( &pdh_handle_cs
);
530 query
->interval
= interval
* 1000;
531 if (!(query
->thread
= CreateThread( NULL
, 0, collect_query_thread
, query
, 0, NULL
)))
533 ret
= GetLastError();
534 CloseHandle( query
->stop
);
536 LeaveCriticalSection( &pdh_handle_cs
);
540 LeaveCriticalSection( &pdh_handle_cs
);
541 return ERROR_SUCCESS
;
544 /***********************************************************************
545 * PdhCollectQueryDataWithTime (PDH.@)
547 PDH_STATUS WINAPI
PdhCollectQueryDataWithTime( PDH_HQUERY handle
, LONGLONG
*timestamp
)
549 struct query
*query
= handle
;
550 struct counter
*counter
;
553 TRACE("%p %p\n", handle
, timestamp
);
555 if (!timestamp
) return PDH_INVALID_ARGUMENT
;
557 EnterCriticalSection( &pdh_handle_cs
);
558 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
560 LeaveCriticalSection( &pdh_handle_cs
);
561 return PDH_INVALID_HANDLE
;
563 if (list_empty( &query
->counters
))
565 LeaveCriticalSection( &pdh_handle_cs
);
569 collect_query_data( query
);
571 item
= list_head( &query
->counters
);
572 counter
= LIST_ENTRY( item
, struct counter
, entry
);
574 *timestamp
= ((LONGLONG
)counter
->stamp
.dwHighDateTime
<< 32) | counter
->stamp
.dwLowDateTime
;
576 LeaveCriticalSection( &pdh_handle_cs
);
577 return ERROR_SUCCESS
;
580 /***********************************************************************
581 * PdhExpandWildCardPathA (PDH.@)
583 PDH_STATUS WINAPI
PdhExpandWildCardPathA( LPCSTR szDataSource
, LPCSTR szWildCardPath
, LPSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
585 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
586 return PDH_NOT_IMPLEMENTED
;
589 /***********************************************************************
590 * PdhExpandWildCardPathW (PDH.@)
592 PDH_STATUS WINAPI
PdhExpandWildCardPathW( LPCWSTR szDataSource
, LPCWSTR szWildCardPath
, LPWSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
594 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
595 return PDH_NOT_IMPLEMENTED
;
598 /***********************************************************************
599 * PdhExpandCounterPathA (PDH.@)
601 PDH_STATUS WINAPI
PdhExpandCounterPathA( LPCSTR szWildCardPath
, LPSTR mszExpandedPathList
, LPDWORD pcchPathListLength
)
603 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
);
604 return PdhExpandWildCardPathA(NULL
, szWildCardPath
, mszExpandedPathList
, pcchPathListLength
, 0);
607 /***********************************************************************
608 * PdhExpandCounterPathW (PDH.@)
610 PDH_STATUS WINAPI
PdhExpandCounterPathW( LPCWSTR szWildCardPath
, LPWSTR mszExpandedPathList
, LPDWORD pcchPathListLength
)
612 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
);
613 return PdhExpandWildCardPathW(NULL
, szWildCardPath
, mszExpandedPathList
, pcchPathListLength
, 0);
616 /***********************************************************************
617 * PdhGetCounterInfoA (PDH.@)
619 PDH_STATUS WINAPI
PdhGetCounterInfoA( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_A info
)
621 struct counter
*counter
= handle
;
623 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
625 EnterCriticalSection( &pdh_handle_cs
);
626 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
628 LeaveCriticalSection( &pdh_handle_cs
);
629 return PDH_INVALID_HANDLE
;
633 LeaveCriticalSection( &pdh_handle_cs
);
634 return PDH_INVALID_ARGUMENT
;
636 if (*size
< sizeof(PDH_COUNTER_INFO_A
))
638 *size
= sizeof(PDH_COUNTER_INFO_A
);
639 LeaveCriticalSection( &pdh_handle_cs
);
640 return PDH_MORE_DATA
;
643 memset( info
, 0, sizeof(PDH_COUNTER_INFO_A
) );
645 info
->dwType
= counter
->type
;
646 info
->CStatus
= counter
->status
;
647 info
->lScale
= counter
->scale
;
648 info
->lDefaultScale
= counter
->defaultscale
;
649 info
->dwUserData
= counter
->user
;
650 info
->dwQueryUserData
= counter
->queryuser
;
652 *size
= sizeof(PDH_COUNTER_INFO_A
);
654 LeaveCriticalSection( &pdh_handle_cs
);
655 return ERROR_SUCCESS
;
658 /***********************************************************************
659 * PdhGetCounterInfoW (PDH.@)
661 PDH_STATUS WINAPI
PdhGetCounterInfoW( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_W info
)
663 struct counter
*counter
= handle
;
665 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
667 EnterCriticalSection( &pdh_handle_cs
);
668 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
670 LeaveCriticalSection( &pdh_handle_cs
);
671 return PDH_INVALID_HANDLE
;
675 LeaveCriticalSection( &pdh_handle_cs
);
676 return PDH_INVALID_ARGUMENT
;
678 if (*size
< sizeof(PDH_COUNTER_INFO_W
))
680 *size
= sizeof(PDH_COUNTER_INFO_W
);
681 LeaveCriticalSection( &pdh_handle_cs
);
682 return PDH_MORE_DATA
;
685 memset( info
, 0, sizeof(PDH_COUNTER_INFO_W
) );
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
;
694 *size
= sizeof(PDH_COUNTER_INFO_W
);
696 LeaveCriticalSection( &pdh_handle_cs
);
697 return ERROR_SUCCESS
;
700 /***********************************************************************
701 * PdhGetCounterTimeBase (PDH.@)
703 PDH_STATUS WINAPI
PdhGetCounterTimeBase( PDH_HCOUNTER handle
, LONGLONG
*base
)
705 struct counter
*counter
= handle
;
707 TRACE("%p %p\n", handle
, base
);
709 if (!base
) return PDH_INVALID_ARGUMENT
;
711 EnterCriticalSection( &pdh_handle_cs
);
712 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
714 LeaveCriticalSection( &pdh_handle_cs
);
715 return PDH_INVALID_HANDLE
;
718 *base
= counter
->base
;
720 LeaveCriticalSection( &pdh_handle_cs
);
721 return ERROR_SUCCESS
;
724 /***********************************************************************
725 * PdhGetDllVersion (PDH.@)
727 PDH_STATUS WINAPI
PdhGetDllVersion( LPDWORD version
)
730 return PDH_INVALID_ARGUMENT
;
732 *version
= PDH_VERSION
;
734 return ERROR_SUCCESS
;
737 /***********************************************************************
738 * PdhGetFormattedCounterValue (PDH.@)
740 PDH_STATUS WINAPI
PdhGetFormattedCounterValue( PDH_HCOUNTER handle
, DWORD format
,
741 LPDWORD type
, PPDH_FMT_COUNTERVALUE value
)
744 struct counter
*counter
= handle
;
746 TRACE("%p %x %p %p\n", handle
, format
, type
, value
);
748 if (!value
) return PDH_INVALID_ARGUMENT
;
750 EnterCriticalSection( &pdh_handle_cs
);
751 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
753 LeaveCriticalSection( &pdh_handle_cs
);
754 return PDH_INVALID_HANDLE
;
758 LeaveCriticalSection( &pdh_handle_cs
);
759 return PDH_INVALID_DATA
;
761 if (!(ret
= format_value( counter
, format
, &counter
->one
, &counter
->two
, value
)))
763 value
->CStatus
= ERROR_SUCCESS
;
764 if (type
) *type
= counter
->type
;
767 LeaveCriticalSection( &pdh_handle_cs
);
771 /***********************************************************************
772 * PdhGetRawCounterValue (PDH.@)
774 PDH_STATUS WINAPI
PdhGetRawCounterValue( PDH_HCOUNTER handle
, LPDWORD type
,
775 PPDH_RAW_COUNTER value
)
777 struct counter
*counter
= handle
;
779 TRACE("%p %p %p\n", handle
, type
, value
);
781 if (!value
) return PDH_INVALID_ARGUMENT
;
783 EnterCriticalSection( &pdh_handle_cs
);
784 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
786 LeaveCriticalSection( &pdh_handle_cs
);
787 return PDH_INVALID_HANDLE
;
790 value
->CStatus
= counter
->status
;
791 value
->TimeStamp
.dwLowDateTime
= counter
->stamp
.dwLowDateTime
;
792 value
->TimeStamp
.dwHighDateTime
= counter
->stamp
.dwHighDateTime
;
793 value
->FirstValue
= counter
->one
.largevalue
;
794 value
->SecondValue
= counter
->two
.largevalue
;
795 value
->MultiCount
= 1; /* FIXME */
797 if (type
) *type
= counter
->type
;
799 LeaveCriticalSection( &pdh_handle_cs
);
800 return ERROR_SUCCESS
;
803 /***********************************************************************
804 * PdhLookupPerfIndexByNameA (PDH.@)
806 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameA( LPCSTR machine
, LPCSTR name
, LPDWORD index
)
809 WCHAR
*machineW
= NULL
;
812 TRACE("%s %s %p\n", debugstr_a(machine
), debugstr_a(name
), index
);
814 if (!name
) return PDH_INVALID_ARGUMENT
;
816 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
818 if (!(nameW
= pdh_strdup_aw( name
)))
819 return PDH_MEMORY_ALLOCATION_FAILURE
;
821 ret
= PdhLookupPerfIndexByNameW( machineW
, nameW
, index
);
824 heap_free( machineW
);
828 /***********************************************************************
829 * PdhLookupPerfIndexByNameW (PDH.@)
831 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameW( LPCWSTR machine
, LPCWSTR name
, LPDWORD index
)
835 TRACE("%s %s %p\n", debugstr_w(machine
), debugstr_w(name
), index
);
837 if (!name
|| !index
) return PDH_INVALID_ARGUMENT
;
841 FIXME("remote machine not supported\n");
842 return PDH_CSTATUS_NO_MACHINE
;
844 for (i
= 0; i
< ARRAY_SIZE(counter_sources
); i
++)
846 if (pdh_match_path( counter_sources
[i
].path
, name
))
848 *index
= counter_sources
[i
].index
;
849 return ERROR_SUCCESS
;
852 return PDH_STRING_NOT_FOUND
;
855 /***********************************************************************
856 * PdhLookupPerfNameByIndexA (PDH.@)
858 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexA( LPCSTR machine
, DWORD index
, LPSTR buffer
, LPDWORD size
)
861 WCHAR
*machineW
= NULL
;
862 WCHAR bufferW
[PDH_MAX_COUNTER_NAME
];
863 DWORD sizeW
= ARRAY_SIZE(bufferW
);
865 TRACE("%s %d %p %p\n", debugstr_a(machine
), index
, buffer
, size
);
867 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
869 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
871 if (!(ret
= PdhLookupPerfNameByIndexW( machineW
, index
, bufferW
, &sizeW
)))
873 int required
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
875 if (*size
< required
) ret
= PDH_MORE_DATA
;
876 else WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, required
, NULL
, NULL
);
879 heap_free( machineW
);
883 /***********************************************************************
884 * PdhLookupPerfNameByIndexW (PDH.@)
886 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexW( LPCWSTR machine
, DWORD index
, LPWSTR buffer
, LPDWORD size
)
891 TRACE("%s %d %p %p\n", debugstr_w(machine
), index
, buffer
, size
);
895 FIXME("remote machine not supported\n");
896 return PDH_CSTATUS_NO_MACHINE
;
899 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
900 if (!index
) return ERROR_SUCCESS
;
902 for (i
= 0; i
< ARRAY_SIZE(counter_sources
); i
++)
904 if (counter_sources
[i
].index
== index
)
906 WCHAR
*p
= wcsrchr( counter_sources
[i
].path
, '\\' ) + 1;
907 unsigned int required
= lstrlenW( p
) + 1;
909 if (*size
< required
) ret
= PDH_MORE_DATA
;
912 lstrcpyW( buffer
, p
);
919 return PDH_INVALID_ARGUMENT
;
922 /***********************************************************************
923 * PdhOpenQueryA (PDH.@)
925 PDH_STATUS WINAPI
PdhOpenQueryA( LPCSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*query
)
928 WCHAR
*sourceW
= NULL
;
930 TRACE("%s %lx %p\n", debugstr_a(source
), userdata
, query
);
932 if (source
&& !(sourceW
= pdh_strdup_aw( source
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
934 ret
= PdhOpenQueryW( sourceW
, userdata
, query
);
935 heap_free( sourceW
);
940 /***********************************************************************
941 * PdhOpenQueryW (PDH.@)
943 PDH_STATUS WINAPI
PdhOpenQueryW( LPCWSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*handle
)
947 TRACE("%s %lx %p\n", debugstr_w(source
), userdata
, handle
);
949 if (!handle
) return PDH_INVALID_ARGUMENT
;
953 FIXME("log file data source not supported\n");
954 return PDH_INVALID_ARGUMENT
;
956 if ((query
= create_query()))
958 query
->user
= userdata
;
961 return ERROR_SUCCESS
;
963 return PDH_MEMORY_ALLOCATION_FAILURE
;
966 /***********************************************************************
967 * PdhRemoveCounter (PDH.@)
969 PDH_STATUS WINAPI
PdhRemoveCounter( PDH_HCOUNTER handle
)
971 struct counter
*counter
= handle
;
973 TRACE("%p\n", handle
);
975 EnterCriticalSection( &pdh_handle_cs
);
976 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
978 LeaveCriticalSection( &pdh_handle_cs
);
979 return PDH_INVALID_HANDLE
;
982 list_remove( &counter
->entry
);
983 destroy_counter( counter
);
985 LeaveCriticalSection( &pdh_handle_cs
);
986 return ERROR_SUCCESS
;
989 /***********************************************************************
990 * PdhSetCounterScaleFactor (PDH.@)
992 PDH_STATUS WINAPI
PdhSetCounterScaleFactor( PDH_HCOUNTER handle
, LONG factor
)
994 struct counter
*counter
= handle
;
996 TRACE("%p\n", handle
);
998 EnterCriticalSection( &pdh_handle_cs
);
999 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
1001 LeaveCriticalSection( &pdh_handle_cs
);
1002 return PDH_INVALID_HANDLE
;
1004 if (factor
< PDH_MIN_SCALE
|| factor
> PDH_MAX_SCALE
)
1006 LeaveCriticalSection( &pdh_handle_cs
);
1007 return PDH_INVALID_ARGUMENT
;
1010 counter
->scale
= factor
;
1012 LeaveCriticalSection( &pdh_handle_cs
);
1013 return ERROR_SUCCESS
;
1016 /***********************************************************************
1017 * PdhValidatePathA (PDH.@)
1019 PDH_STATUS WINAPI
PdhValidatePathA( LPCSTR path
)
1024 TRACE("%s\n", debugstr_a(path
));
1026 if (!path
) return PDH_INVALID_ARGUMENT
;
1027 if (!(pathW
= pdh_strdup_aw( path
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
1029 ret
= PdhValidatePathW( pathW
);
1035 static PDH_STATUS
validate_path( LPCWSTR path
)
1037 if (!path
|| !*path
) return PDH_INVALID_ARGUMENT
;
1038 if (*path
++ != '\\' || !wcschr( path
, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME
;
1039 return ERROR_SUCCESS
;
1042 /***********************************************************************
1043 * PdhValidatePathW (PDH.@)
1045 PDH_STATUS WINAPI
PdhValidatePathW( LPCWSTR path
)
1050 TRACE("%s\n", debugstr_w(path
));
1052 if ((ret
= validate_path( path
))) return ret
;
1054 for (i
= 0; i
< ARRAY_SIZE(counter_sources
); i
++)
1055 if (pdh_match_path( counter_sources
[i
].path
, path
)) return ERROR_SUCCESS
;
1057 return PDH_CSTATUS_NO_COUNTER
;
1060 /***********************************************************************
1061 * PdhVbAddCounter (PDH.@)
1063 PDH_STATUS WINAPI
PdhVbAddCounter( PDH_HQUERY query
, LPCSTR path
, PDH_HCOUNTER
*counter
)
1065 FIXME("%p, %s, %p: stub!\n", query
, debugstr_a(path
), counter
);
1067 if (!path
) return PDH_INVALID_ARGUMENT
;
1069 return PDH_NOT_IMPLEMENTED
;
1072 /***********************************************************************
1073 * PdhValidatePathExA (PDH.@)
1075 PDH_STATUS WINAPI
PdhValidatePathExA( PDH_HLOG source
, LPCSTR path
)
1077 TRACE("%p %s\n", source
, debugstr_a(path
));
1081 FIXME("log file data source not supported\n");
1082 return ERROR_SUCCESS
;
1084 return PdhValidatePathA( path
);
1087 /***********************************************************************
1088 * PdhValidatePathExW (PDH.@)
1090 PDH_STATUS WINAPI
PdhValidatePathExW( PDH_HLOG source
, LPCWSTR path
)
1092 TRACE("%p %s\n", source
, debugstr_w(path
));
1096 FIXME("log file data source not supported\n");
1097 return ERROR_SUCCESS
;
1099 return PdhValidatePathW( path
);
1102 /***********************************************************************
1103 * PdhMakeCounterPathA (PDH.@)
1105 PDH_STATUS WINAPI
PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A
*e
, LPSTR buffer
,
1106 LPDWORD buflen
, DWORD flags
)
1108 PDH_STATUS ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1109 PDH_COUNTER_PATH_ELEMENTS_W eW
;
1113 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1115 if (!e
|| !buflen
) return PDH_INVALID_ARGUMENT
;
1117 memset( &eW
, 0, sizeof(eW
) );
1118 if (e
->szMachineName
&& !(eW
.szMachineName
= pdh_strdup_aw( e
->szMachineName
))) goto done
;
1119 if (e
->szObjectName
&& !(eW
.szObjectName
= pdh_strdup_aw( e
->szObjectName
))) goto done
;
1120 if (e
->szInstanceName
&& !(eW
.szInstanceName
= pdh_strdup_aw( e
->szInstanceName
))) goto done
;
1121 if (e
->szParentInstance
&& !(eW
.szParentInstance
= pdh_strdup_aw( e
->szParentInstance
))) goto done
;
1122 if (e
->szCounterName
&& !(eW
.szCounterName
= pdh_strdup_aw( e
->szCounterName
))) goto done
;
1123 eW
.dwInstanceIndex
= e
->dwInstanceIndex
;
1126 ret
= PdhMakeCounterPathW( &eW
, NULL
, &buflenW
, flags
);
1127 if (ret
== PDH_MORE_DATA
)
1129 if ((bufferW
= heap_alloc( buflenW
* sizeof(WCHAR
) )))
1131 if (!(ret
= PdhMakeCounterPathW( &eW
, bufferW
, &buflenW
, flags
)))
1133 int len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1134 if (*buflen
>= len
) WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, *buflen
, NULL
, NULL
);
1135 else ret
= PDH_MORE_DATA
;
1138 heap_free( bufferW
);
1141 ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1145 heap_free( eW
.szMachineName
);
1146 heap_free( eW
.szObjectName
);
1147 heap_free( eW
.szInstanceName
);
1148 heap_free( eW
.szParentInstance
);
1149 heap_free( eW
.szCounterName
);
1153 /***********************************************************************
1154 * PdhMakeCounterPathW (PDH.@)
1156 PDH_STATUS WINAPI
PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W
*e
, LPWSTR buffer
,
1157 LPDWORD buflen
, DWORD flags
)
1159 WCHAR path
[PDH_MAX_COUNTER_NAME
], instance
[12];
1160 PDH_STATUS ret
= ERROR_SUCCESS
;
1163 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1165 if (flags
) FIXME("unimplemented flags 0x%08x\n", flags
);
1167 if (!e
|| !e
->szCounterName
|| !e
->szObjectName
|| !buflen
)
1168 return PDH_INVALID_ARGUMENT
;
1171 if (e
->szMachineName
)
1173 lstrcatW(path
, L
"\\\\");
1174 lstrcatW(path
, e
->szMachineName
);
1176 lstrcatW(path
, L
"\\");
1177 lstrcatW(path
, e
->szObjectName
);
1178 if (e
->szInstanceName
)
1180 lstrcatW(path
, L
"(");
1181 if (e
->szParentInstance
)
1183 lstrcatW(path
, e
->szParentInstance
);
1184 lstrcatW(path
, L
"/");
1186 lstrcatW(path
, e
->szInstanceName
);
1187 swprintf(instance
, ARRAY_SIZE(instance
), L
"#%u", e
->dwInstanceIndex
);
1188 lstrcatW(path
, instance
);
1189 lstrcatW(path
, L
")");
1191 lstrcatW(path
, L
"\\");
1192 lstrcatW(path
, e
->szCounterName
);
1194 len
= lstrlenW(path
) + 1;
1195 if (*buflen
>= len
) lstrcpyW(buffer
, path
);
1196 else ret
= PDH_MORE_DATA
;
1201 /***********************************************************************
1202 * PdhEnumObjectItemsA (PDH.@)
1204 PDH_STATUS WINAPI
PdhEnumObjectItemsA(LPCSTR szDataSource
, LPCSTR szMachineName
, LPCSTR szObjectName
,
1205 LPSTR mszCounterList
, LPDWORD pcchCounterListLength
, LPSTR mszInstanceList
,
1206 LPDWORD pcchInstanceListLength
, DWORD dwDetailLevel
, DWORD dwFlags
)
1208 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szMachineName
),
1209 debugstr_a(szObjectName
), mszCounterList
, pcchCounterListLength
, mszInstanceList
,
1210 pcchInstanceListLength
, dwDetailLevel
, dwFlags
);
1212 return PDH_NOT_IMPLEMENTED
;
1215 /***********************************************************************
1216 * PdhEnumObjectItemsW (PDH.@)
1218 PDH_STATUS WINAPI
PdhEnumObjectItemsW(LPCWSTR szDataSource
, LPCWSTR szMachineName
, LPCWSTR szObjectName
,
1219 LPWSTR mszCounterList
, LPDWORD pcchCounterListLength
, LPWSTR mszInstanceList
,
1220 LPDWORD pcchInstanceListLength
, DWORD dwDetailLevel
, DWORD dwFlags
)
1222 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szMachineName
),
1223 debugstr_w(szObjectName
), mszCounterList
, pcchCounterListLength
, mszInstanceList
,
1224 pcchInstanceListLength
, dwDetailLevel
, dwFlags
);
1226 return PDH_NOT_IMPLEMENTED
;
1229 /***********************************************************************
1230 * PdhSetDefaultRealTimeDataSource (PDH.@)
1232 PDH_STATUS WINAPI
PdhSetDefaultRealTimeDataSource( DWORD source
)
1234 FIXME("%u\n", source
);
1235 return ERROR_SUCCESS
;
1238 /***********************************************************************
1239 * PdhGetLogFileTypeA (PDH.@)
1241 PDH_STATUS WINAPI
PdhGetLogFileTypeA(const char *log
, DWORD
*type
)
1243 FIXME("%s, %p: stub\n", debugstr_a(log
), type
);
1244 return PDH_NOT_IMPLEMENTED
;
1247 /***********************************************************************
1248 * PdhGetLogFileTypeW (PDH.@)
1250 PDH_STATUS WINAPI
PdhGetLogFileTypeW(const WCHAR
*log
, DWORD
*type
)
1252 FIXME("%s, %p: stub\n", debugstr_w(log
), type
);
1253 return PDH_NOT_IMPLEMENTED
;
1256 /***********************************************************************
1257 * PdhBindInputDataSourceA (PDH.@)
1259 PDH_STATUS WINAPI
PdhBindInputDataSourceA(PDH_HLOG
*source
, const char *filenamelist
)
1261 FIXME("%p %s: stub\n", source
, debugstr_a(filenamelist
));
1262 return PDH_NOT_IMPLEMENTED
;
1265 /***********************************************************************
1266 * PdhBindInputDataSourceW (PDH.@)
1268 PDH_STATUS WINAPI
PdhBindInputDataSourceW(PDH_HLOG
*source
, const WCHAR
*filenamelist
)
1270 FIXME("%p %s: stub\n", source
, debugstr_w(filenamelist
));
1271 return PDH_NOT_IMPLEMENTED
;