cmd: DIR command outputs free space for the path.
[wine.git] / dlls / pdh / pdh_main.c
blob073a74e1b52acae472f8a8f3dca2b7546f54928b
1 /*
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 #include <stdarg.h>
23 #include <stdlib.h>
24 #include <math.h>
26 #include "windef.h"
27 #include "winbase.h"
29 #include "pdh.h"
30 #include "pdhmsg.h"
31 #include "winperf.h"
33 #include "wine/debug.h"
34 #include "wine/list.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(pdh);
38 static CRITICAL_SECTION pdh_handle_cs;
39 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug =
41 0, 0, &pdh_handle_cs,
42 { &pdh_handle_cs_debug.ProcessLocksList,
43 &pdh_handle_cs_debug.ProcessLocksList },
44 0, 0, { (DWORD_PTR)(__FILE__ ": pdh_handle_cs") }
46 static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 };
48 static inline WCHAR *pdh_strdup_aw( const char *src )
50 int len;
51 WCHAR *dst;
53 if (!src) return NULL;
54 len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
55 if ((dst = malloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
56 return dst;
59 union value
61 LONG longvalue;
62 double doublevalue;
63 LONGLONG largevalue;
66 struct counter
68 DWORD magic; /* signature */
69 struct list entry; /* list entry */
70 WCHAR *path; /* identifier */
71 DWORD type; /* counter type */
72 DWORD status; /* update status */
73 LONG scale; /* scale factor */
74 LONG defaultscale; /* default scale factor */
75 DWORD_PTR user; /* user data */
76 DWORD_PTR queryuser; /* query user data */
77 LONGLONG base; /* samples per second */
78 FILETIME stamp; /* time stamp */
79 void (CALLBACK *collect)( struct counter * ); /* collect callback */
80 union value one; /* first value */
81 union value two; /* second value */
84 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
86 static struct counter *create_counter( void )
88 struct counter *counter;
90 if ((counter = calloc( 1, sizeof(struct counter) )))
92 counter->magic = PDH_MAGIC_COUNTER;
93 return counter;
95 return NULL;
98 static void destroy_counter( struct counter *counter )
100 counter->magic = 0;
101 free( counter->path );
102 free( counter );
105 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
107 struct query
109 DWORD magic; /* signature */
110 DWORD_PTR user; /* user data */
111 HANDLE thread; /* collect thread */
112 DWORD interval; /* collect interval */
113 HANDLE wait; /* wait event */
114 HANDLE stop; /* stop event */
115 struct list counters; /* counter list */
118 static struct query *create_query( void )
120 struct query *query;
122 if ((query = calloc( 1, sizeof(struct query) )))
124 query->magic = PDH_MAGIC_QUERY;
125 list_init( &query->counters );
126 return query;
128 return NULL;
131 static void destroy_query( struct query *query )
133 query->magic = 0;
134 free( query );
137 struct source
139 DWORD index; /* name index */
140 const WCHAR *path; /* identifier */
141 void (CALLBACK *collect)( struct counter * ); /* collect callback */
142 DWORD type; /* counter type */
143 LONG scale; /* default scale factor */
144 LONGLONG base; /* samples per second */
147 static void CALLBACK collect_processor_time( struct counter *counter )
149 counter->two.largevalue = 500000; /* FIXME */
150 counter->status = PDH_CSTATUS_VALID_DATA;
153 static void CALLBACK collect_uptime( struct counter *counter )
155 counter->two.largevalue = GetTickCount64();
156 counter->status = PDH_CSTATUS_VALID_DATA;
159 #define TYPE_PROCESSOR_TIME \
160 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
161 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
163 #define TYPE_UPTIME \
164 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
166 /* counter source registry */
167 static const struct source counter_sources[] =
169 { 6, L"\\Processor(_Total)\\% Processor Time", collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
170 { 674, L"\\System\\System Up Time", collect_uptime, TYPE_UPTIME, -3, 1000 }
173 static BOOL is_local_machine( const WCHAR *name, DWORD len )
175 WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
176 DWORD buflen = ARRAY_SIZE(buf);
178 if (!GetComputerNameW( buf, &buflen )) return FALSE;
179 return len == buflen && !wcsnicmp( name, buf, buflen );
182 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
184 const WCHAR *p;
186 if (path[0] == '\\' && path[1] == '\\' && (p = wcschr( path + 2, '\\' )) &&
187 is_local_machine( path + 2, p - path - 2 ))
189 path += p - path;
191 if (wcschr( path, '\\' )) p = fullpath;
192 else p = wcsrchr( fullpath, '\\' ) + 1;
193 return !wcscmp( p, path );
196 /***********************************************************************
197 * PdhAddCounterA (PDH.@)
199 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
200 DWORD_PTR userdata, PDH_HCOUNTER *counter )
202 PDH_STATUS ret;
203 WCHAR *pathW;
205 TRACE("%p %s %Ix %p\n", query, debugstr_a(path), userdata, counter);
207 if (!path) return PDH_INVALID_ARGUMENT;
209 if (!(pathW = pdh_strdup_aw( path )))
210 return PDH_MEMORY_ALLOCATION_FAILURE;
212 ret = PdhAddCounterW( query, pathW, userdata, counter );
214 free( pathW );
215 return ret;
218 /***********************************************************************
219 * PdhAddCounterW (PDH.@)
221 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
222 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
224 struct query *query = hquery;
225 struct counter *counter;
226 unsigned int i;
228 TRACE("%p %s %Ix %p\n", hquery, debugstr_w(path), userdata, hcounter);
230 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
232 EnterCriticalSection( &pdh_handle_cs );
233 if (!query || query->magic != PDH_MAGIC_QUERY)
235 LeaveCriticalSection( &pdh_handle_cs );
236 return PDH_INVALID_HANDLE;
239 *hcounter = NULL;
240 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
242 if (pdh_match_path( counter_sources[i].path, path ))
244 if ((counter = create_counter()))
246 counter->path = wcsdup( counter_sources[i].path );
247 counter->collect = counter_sources[i].collect;
248 counter->type = counter_sources[i].type;
249 counter->defaultscale = counter_sources[i].scale;
250 counter->base = counter_sources[i].base;
251 counter->queryuser = query->user;
252 counter->user = userdata;
254 list_add_tail( &query->counters, &counter->entry );
255 *hcounter = counter;
257 LeaveCriticalSection( &pdh_handle_cs );
258 return ERROR_SUCCESS;
260 LeaveCriticalSection( &pdh_handle_cs );
261 return PDH_MEMORY_ALLOCATION_FAILURE;
264 LeaveCriticalSection( &pdh_handle_cs );
265 return PDH_CSTATUS_NO_COUNTER;
268 /***********************************************************************
269 * PdhAddEnglishCounterA (PDH.@)
271 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
272 DWORD_PTR userdata, PDH_HCOUNTER *counter )
274 TRACE("%p %s %Ix %p\n", query, debugstr_a(path), userdata, counter);
276 if (!counter) return PDH_INVALID_ARGUMENT;
277 if (!query) return PDH_INVALID_HANDLE;
278 return PdhAddCounterA( query, path, userdata, counter );
281 /***********************************************************************
282 * PdhAddEnglishCounterW (PDH.@)
284 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
285 DWORD_PTR userdata, PDH_HCOUNTER *counter )
287 TRACE("%p %s %Ix %p\n", query, debugstr_w(path), userdata, counter);
289 if (!counter) return PDH_INVALID_ARGUMENT;
290 if (!query) return PDH_INVALID_HANDLE;
291 return PdhAddCounterW( query, path, userdata, counter );
294 /* caller must hold counter lock */
295 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
296 union value *raw2, PDH_FMT_COUNTERVALUE *value )
298 LONG factor;
300 factor = counter->scale ? counter->scale : counter->defaultscale;
301 if (format & PDH_FMT_LONG)
303 if (format & PDH_FMT_1000) value->longValue = raw2->longvalue * 1000;
304 else value->longValue = raw2->longvalue * pow( 10, factor );
306 else if (format & PDH_FMT_LARGE)
308 if (format & PDH_FMT_1000) value->largeValue = raw2->largevalue * 1000;
309 else value->largeValue = raw2->largevalue * pow( 10, factor );
311 else if (format & PDH_FMT_DOUBLE)
313 if (format & PDH_FMT_1000) value->doubleValue = raw2->doublevalue * 1000;
314 else value->doubleValue = raw2->doublevalue * pow( 10, factor );
316 else
318 WARN("unknown format %lx\n", format);
319 return PDH_INVALID_ARGUMENT;
321 return ERROR_SUCCESS;
324 /***********************************************************************
325 * PdhCalculateCounterFromRawValue (PDH.@)
327 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
328 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
329 PPDH_FMT_COUNTERVALUE value )
331 PDH_STATUS ret;
332 struct counter *counter = handle;
334 TRACE("%p 0x%08lx %p %p %p\n", handle, format, raw1, raw2, value);
336 if (!value) return PDH_INVALID_ARGUMENT;
338 EnterCriticalSection( &pdh_handle_cs );
339 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
341 LeaveCriticalSection( &pdh_handle_cs );
342 return PDH_INVALID_HANDLE;
345 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
346 (union value *)&raw2->SecondValue, value );
348 LeaveCriticalSection( &pdh_handle_cs );
349 return ret;
353 /***********************************************************************
354 * PdhCloseQuery (PDH.@)
356 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
358 struct query *query = handle;
359 struct list *item, *next;
361 TRACE("%p\n", handle);
363 EnterCriticalSection( &pdh_handle_cs );
364 if (!query || query->magic != PDH_MAGIC_QUERY)
366 LeaveCriticalSection( &pdh_handle_cs );
367 return PDH_INVALID_HANDLE;
370 if (query->thread)
372 HANDLE thread = query->thread;
373 SetEvent( query->stop );
374 LeaveCriticalSection( &pdh_handle_cs );
376 WaitForSingleObject( thread, INFINITE );
378 EnterCriticalSection( &pdh_handle_cs );
379 if (query->magic != PDH_MAGIC_QUERY)
381 LeaveCriticalSection( &pdh_handle_cs );
382 return ERROR_SUCCESS;
384 CloseHandle( query->stop );
385 CloseHandle( query->thread );
386 query->thread = NULL;
389 LIST_FOR_EACH_SAFE( item, next, &query->counters )
391 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
393 list_remove( &counter->entry );
394 destroy_counter( counter );
397 destroy_query( query );
399 LeaveCriticalSection( &pdh_handle_cs );
400 return ERROR_SUCCESS;
403 /* caller must hold query lock */
404 static void collect_query_data( struct query *query )
406 struct list *item;
408 LIST_FOR_EACH( item, &query->counters )
410 SYSTEMTIME time;
411 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
413 counter->collect( counter );
415 GetLocalTime( &time );
416 SystemTimeToFileTime( &time, &counter->stamp );
420 /***********************************************************************
421 * PdhCollectQueryData (PDH.@)
423 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
425 struct query *query = handle;
427 TRACE("%p\n", handle);
429 EnterCriticalSection( &pdh_handle_cs );
430 if (!query || query->magic != PDH_MAGIC_QUERY)
432 LeaveCriticalSection( &pdh_handle_cs );
433 return PDH_INVALID_HANDLE;
436 if (list_empty( &query->counters ))
438 LeaveCriticalSection( &pdh_handle_cs );
439 return PDH_NO_DATA;
442 collect_query_data( query );
444 LeaveCriticalSection( &pdh_handle_cs );
445 return ERROR_SUCCESS;
448 static DWORD CALLBACK collect_query_thread( void *arg )
450 struct query *query = arg;
451 DWORD interval = query->interval;
452 HANDLE stop = query->stop;
454 for (;;)
456 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
458 EnterCriticalSection( &pdh_handle_cs );
459 if (query->magic != PDH_MAGIC_QUERY)
461 LeaveCriticalSection( &pdh_handle_cs );
462 ExitThread( PDH_INVALID_HANDLE );
465 collect_query_data( query );
467 if (!SetEvent( query->wait ))
469 LeaveCriticalSection( &pdh_handle_cs );
470 ExitThread( 0 );
472 LeaveCriticalSection( &pdh_handle_cs );
476 /***********************************************************************
477 * PdhCollectQueryDataEx (PDH.@)
479 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
481 PDH_STATUS ret;
482 struct query *query = handle;
484 TRACE("%p %ld %p\n", handle, interval, event);
486 EnterCriticalSection( &pdh_handle_cs );
487 if (!query || query->magic != PDH_MAGIC_QUERY)
489 LeaveCriticalSection( &pdh_handle_cs );
490 return PDH_INVALID_HANDLE;
492 if (list_empty( &query->counters ))
494 LeaveCriticalSection( &pdh_handle_cs );
495 return PDH_NO_DATA;
497 if (query->thread)
499 HANDLE thread = query->thread;
500 SetEvent( query->stop );
501 LeaveCriticalSection( &pdh_handle_cs );
503 WaitForSingleObject( thread, INFINITE );
505 EnterCriticalSection( &pdh_handle_cs );
506 if (query->magic != PDH_MAGIC_QUERY)
508 LeaveCriticalSection( &pdh_handle_cs );
509 return PDH_INVALID_HANDLE;
511 CloseHandle( query->thread );
512 query->thread = NULL;
514 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
516 ret = GetLastError();
517 LeaveCriticalSection( &pdh_handle_cs );
518 return ret;
520 query->wait = event;
521 query->interval = interval * 1000;
522 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
524 ret = GetLastError();
525 CloseHandle( query->stop );
527 LeaveCriticalSection( &pdh_handle_cs );
528 return ret;
531 LeaveCriticalSection( &pdh_handle_cs );
532 return ERROR_SUCCESS;
535 /***********************************************************************
536 * PdhCollectQueryDataWithTime (PDH.@)
538 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
540 struct query *query = handle;
541 struct counter *counter;
542 struct list *item;
544 TRACE("%p %p\n", handle, timestamp);
546 if (!timestamp) return PDH_INVALID_ARGUMENT;
548 EnterCriticalSection( &pdh_handle_cs );
549 if (!query || query->magic != PDH_MAGIC_QUERY)
551 LeaveCriticalSection( &pdh_handle_cs );
552 return PDH_INVALID_HANDLE;
554 if (list_empty( &query->counters ))
556 LeaveCriticalSection( &pdh_handle_cs );
557 return PDH_NO_DATA;
560 collect_query_data( query );
562 item = list_head( &query->counters );
563 counter = LIST_ENTRY( item, struct counter, entry );
565 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
567 LeaveCriticalSection( &pdh_handle_cs );
568 return ERROR_SUCCESS;
571 /***********************************************************************
572 * PdhExpandWildCardPathA (PDH.@)
574 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
576 FIXME("%s, %s, %p, %p, 0x%lx: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
577 return PDH_NOT_IMPLEMENTED;
580 /***********************************************************************
581 * PdhExpandWildCardPathW (PDH.@)
583 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
585 FIXME("%s, %s, %p, %p, 0x%lx: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
586 return PDH_NOT_IMPLEMENTED;
589 /***********************************************************************
590 * PdhExpandCounterPathA (PDH.@)
592 PDH_STATUS WINAPI PdhExpandCounterPathA( LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength )
594 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength);
595 return PdhExpandWildCardPathA(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
598 /***********************************************************************
599 * PdhExpandCounterPathW (PDH.@)
601 PDH_STATUS WINAPI PdhExpandCounterPathW( LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength )
603 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength);
604 return PdhExpandWildCardPathW(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
607 /***********************************************************************
608 * PdhGetCounterInfoA (PDH.@)
610 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
612 struct counter *counter = handle;
614 TRACE("%p %d %p %p\n", handle, text, size, info);
616 EnterCriticalSection( &pdh_handle_cs );
617 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
619 LeaveCriticalSection( &pdh_handle_cs );
620 return PDH_INVALID_HANDLE;
622 if (!size)
624 LeaveCriticalSection( &pdh_handle_cs );
625 return PDH_INVALID_ARGUMENT;
627 if (*size < sizeof(PDH_COUNTER_INFO_A))
629 *size = sizeof(PDH_COUNTER_INFO_A);
630 LeaveCriticalSection( &pdh_handle_cs );
631 return PDH_MORE_DATA;
634 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
636 info->dwType = counter->type;
637 info->CStatus = counter->status;
638 info->lScale = counter->scale;
639 info->lDefaultScale = counter->defaultscale;
640 info->dwUserData = counter->user;
641 info->dwQueryUserData = counter->queryuser;
643 *size = sizeof(PDH_COUNTER_INFO_A);
645 LeaveCriticalSection( &pdh_handle_cs );
646 return ERROR_SUCCESS;
649 /***********************************************************************
650 * PdhGetCounterInfoW (PDH.@)
652 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
654 struct counter *counter = handle;
656 TRACE("%p %d %p %p\n", handle, text, size, info);
658 EnterCriticalSection( &pdh_handle_cs );
659 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
661 LeaveCriticalSection( &pdh_handle_cs );
662 return PDH_INVALID_HANDLE;
664 if (!size)
666 LeaveCriticalSection( &pdh_handle_cs );
667 return PDH_INVALID_ARGUMENT;
669 if (*size < sizeof(PDH_COUNTER_INFO_W))
671 *size = sizeof(PDH_COUNTER_INFO_W);
672 LeaveCriticalSection( &pdh_handle_cs );
673 return PDH_MORE_DATA;
676 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
678 info->dwType = counter->type;
679 info->CStatus = counter->status;
680 info->lScale = counter->scale;
681 info->lDefaultScale = counter->defaultscale;
682 info->dwUserData = counter->user;
683 info->dwQueryUserData = counter->queryuser;
685 *size = sizeof(PDH_COUNTER_INFO_W);
687 LeaveCriticalSection( &pdh_handle_cs );
688 return ERROR_SUCCESS;
691 /***********************************************************************
692 * PdhGetCounterTimeBase (PDH.@)
694 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
696 struct counter *counter = handle;
698 TRACE("%p %p\n", handle, base);
700 if (!base) return PDH_INVALID_ARGUMENT;
702 EnterCriticalSection( &pdh_handle_cs );
703 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
705 LeaveCriticalSection( &pdh_handle_cs );
706 return PDH_INVALID_HANDLE;
709 *base = counter->base;
711 LeaveCriticalSection( &pdh_handle_cs );
712 return ERROR_SUCCESS;
715 /***********************************************************************
716 * PdhGetDllVersion (PDH.@)
718 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
720 if (!version)
721 return PDH_INVALID_ARGUMENT;
723 *version = PDH_VERSION;
725 return ERROR_SUCCESS;
728 /***********************************************************************
729 * PdhGetFormattedCounterValue (PDH.@)
731 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
732 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
734 PDH_STATUS ret;
735 struct counter *counter = handle;
737 TRACE("%p %lx %p %p\n", handle, format, type, value);
739 if (!value) return PDH_INVALID_ARGUMENT;
741 EnterCriticalSection( &pdh_handle_cs );
742 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
744 LeaveCriticalSection( &pdh_handle_cs );
745 return PDH_INVALID_HANDLE;
747 if (counter->status)
749 LeaveCriticalSection( &pdh_handle_cs );
750 return PDH_INVALID_DATA;
752 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
754 value->CStatus = ERROR_SUCCESS;
755 if (type) *type = counter->type;
758 LeaveCriticalSection( &pdh_handle_cs );
759 return ret;
762 /***********************************************************************
763 * PdhGetRawCounterValue (PDH.@)
765 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
766 PPDH_RAW_COUNTER value )
768 struct counter *counter = handle;
770 TRACE("%p %p %p\n", handle, 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;
781 value->CStatus = counter->status;
782 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
783 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
784 value->FirstValue = counter->one.largevalue;
785 value->SecondValue = counter->two.largevalue;
786 value->MultiCount = 1; /* FIXME */
788 if (type) *type = counter->type;
790 LeaveCriticalSection( &pdh_handle_cs );
791 return ERROR_SUCCESS;
794 /***********************************************************************
795 * PdhLookupPerfIndexByNameA (PDH.@)
797 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
799 PDH_STATUS ret;
800 WCHAR *machineW = NULL;
801 WCHAR *nameW;
803 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
805 if (!name) return PDH_INVALID_ARGUMENT;
807 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
809 if (!(nameW = pdh_strdup_aw( name )))
810 return PDH_MEMORY_ALLOCATION_FAILURE;
812 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
814 free( nameW );
815 free( machineW );
816 return ret;
819 /***********************************************************************
820 * PdhLookupPerfIndexByNameW (PDH.@)
822 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
824 unsigned int i;
826 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
828 if (!name || !index) return PDH_INVALID_ARGUMENT;
830 if (machine)
832 FIXME("remote machine not supported\n");
833 return PDH_CSTATUS_NO_MACHINE;
835 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
837 if (pdh_match_path( counter_sources[i].path, name ))
839 *index = counter_sources[i].index;
840 return ERROR_SUCCESS;
843 return PDH_STRING_NOT_FOUND;
846 /***********************************************************************
847 * PdhLookupPerfNameByIndexA (PDH.@)
849 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
851 PDH_STATUS ret;
852 WCHAR *machineW = NULL;
853 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
854 DWORD sizeW = ARRAY_SIZE(bufferW);
856 TRACE("%s %ld %p %p\n", debugstr_a(machine), index, buffer, size);
858 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
860 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
862 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
864 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
866 if (*size < required) ret = PDH_MORE_DATA;
867 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
868 *size = required;
870 free( machineW );
871 return ret;
874 /***********************************************************************
875 * PdhLookupPerfNameByIndexW (PDH.@)
877 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
879 PDH_STATUS ret;
880 unsigned int i;
882 TRACE("%s %ld %p %p\n", debugstr_w(machine), index, buffer, size);
884 if (machine)
886 FIXME("remote machine not supported\n");
887 return PDH_CSTATUS_NO_MACHINE;
890 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
891 if (!index) return ERROR_SUCCESS;
893 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
895 if (counter_sources[i].index == index)
897 WCHAR *p = wcsrchr( counter_sources[i].path, '\\' ) + 1;
898 unsigned int required = lstrlenW( p ) + 1;
900 if (*size < required) ret = PDH_MORE_DATA;
901 else
903 lstrcpyW( buffer, p );
904 ret = ERROR_SUCCESS;
906 *size = required;
907 return ret;
910 return PDH_INVALID_ARGUMENT;
913 /***********************************************************************
914 * PdhOpenQueryA (PDH.@)
916 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
918 PDH_STATUS ret;
919 WCHAR *sourceW = NULL;
921 TRACE("%s %Ix %p\n", debugstr_a(source), userdata, query);
923 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
925 ret = PdhOpenQueryW( sourceW, userdata, query );
926 free( sourceW );
928 return ret;
931 /***********************************************************************
932 * PdhOpenQueryW (PDH.@)
934 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
936 struct query *query;
938 TRACE("%s %Ix %p\n", debugstr_w(source), userdata, handle);
940 if (!handle) return PDH_INVALID_ARGUMENT;
942 if (source)
944 FIXME("log file data source not supported\n");
945 return PDH_INVALID_ARGUMENT;
947 if ((query = create_query()))
949 query->user = userdata;
950 *handle = query;
952 return ERROR_SUCCESS;
954 return PDH_MEMORY_ALLOCATION_FAILURE;
957 /***********************************************************************
958 * PdhRemoveCounter (PDH.@)
960 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
962 struct counter *counter = handle;
964 TRACE("%p\n", handle);
966 EnterCriticalSection( &pdh_handle_cs );
967 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
969 LeaveCriticalSection( &pdh_handle_cs );
970 return PDH_INVALID_HANDLE;
973 list_remove( &counter->entry );
974 destroy_counter( counter );
976 LeaveCriticalSection( &pdh_handle_cs );
977 return ERROR_SUCCESS;
980 /***********************************************************************
981 * PdhSetCounterScaleFactor (PDH.@)
983 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
985 struct counter *counter = handle;
987 TRACE("%p\n", handle);
989 EnterCriticalSection( &pdh_handle_cs );
990 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
992 LeaveCriticalSection( &pdh_handle_cs );
993 return PDH_INVALID_HANDLE;
995 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
997 LeaveCriticalSection( &pdh_handle_cs );
998 return PDH_INVALID_ARGUMENT;
1001 counter->scale = factor;
1003 LeaveCriticalSection( &pdh_handle_cs );
1004 return ERROR_SUCCESS;
1007 /***********************************************************************
1008 * PdhValidatePathA (PDH.@)
1010 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1012 PDH_STATUS ret;
1013 WCHAR *pathW;
1015 TRACE("%s\n", debugstr_a(path));
1017 if (!path) return PDH_INVALID_ARGUMENT;
1018 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1020 ret = PdhValidatePathW( pathW );
1022 free( pathW );
1023 return ret;
1026 static PDH_STATUS validate_path( LPCWSTR path )
1028 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1029 if (*path++ != '\\' || !wcschr( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1030 return ERROR_SUCCESS;
1033 /***********************************************************************
1034 * PdhValidatePathW (PDH.@)
1036 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1038 PDH_STATUS ret;
1039 unsigned int i;
1041 TRACE("%s\n", debugstr_w(path));
1043 if ((ret = validate_path( path ))) return ret;
1045 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
1046 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1048 return PDH_CSTATUS_NO_COUNTER;
1051 /***********************************************************************
1052 * PdhVbAddCounter (PDH.@)
1054 PDH_STATUS WINAPI PdhVbAddCounter( PDH_HQUERY query, LPCSTR path, PDH_HCOUNTER *counter )
1056 FIXME("%p, %s, %p: stub!\n", query, debugstr_a(path), counter);
1058 if (!path) return PDH_INVALID_ARGUMENT;
1060 return PDH_NOT_IMPLEMENTED;
1063 /***********************************************************************
1064 * PdhVbGetDoubleCounterValue (PDH.@)
1066 double WINAPI PdhVbGetDoubleCounterValue( PDH_HCOUNTER handle, PDH_STATUS *counter_status )
1068 PDH_FMT_COUNTERVALUE value;
1069 PDH_STATUS status;
1071 TRACE( "%p %p\n", handle, counter_status );
1073 memset( &value, 0, sizeof(value) );
1074 status = PdhGetFormattedCounterValue( handle, PDH_FMT_DOUBLE, NULL, &value );
1076 if (counter_status) *counter_status = status;
1077 return value.doubleValue;
1080 /***********************************************************************
1081 * PdhValidatePathExA (PDH.@)
1083 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1085 TRACE("%p %s\n", source, debugstr_a(path));
1087 if (source)
1089 FIXME("log file data source not supported\n");
1090 return ERROR_SUCCESS;
1092 return PdhValidatePathA( path );
1095 /***********************************************************************
1096 * PdhValidatePathExW (PDH.@)
1098 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1100 TRACE("%p %s\n", source, debugstr_w(path));
1102 if (source)
1104 FIXME("log file data source not supported\n");
1105 return ERROR_SUCCESS;
1107 return PdhValidatePathW( path );
1110 /***********************************************************************
1111 * PdhMakeCounterPathA (PDH.@)
1113 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1114 LPDWORD buflen, DWORD flags )
1116 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1117 PDH_COUNTER_PATH_ELEMENTS_W eW;
1118 WCHAR *bufferW;
1119 DWORD buflenW;
1121 TRACE("%p %p %p 0x%08lx\n", e, buffer, buflen, flags);
1123 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1125 memset( &eW, 0, sizeof(eW) );
1126 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1127 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1128 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1129 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1130 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1131 eW.dwInstanceIndex = e->dwInstanceIndex;
1133 buflenW = 0;
1134 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1135 if (ret == PDH_MORE_DATA)
1137 if ((bufferW = malloc( buflenW * sizeof(WCHAR) )))
1139 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1141 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1142 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1143 else ret = PDH_MORE_DATA;
1144 *buflen = len;
1146 free( bufferW );
1148 else
1149 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1152 done:
1153 free( eW.szMachineName );
1154 free( eW.szObjectName );
1155 free( eW.szInstanceName );
1156 free( eW.szParentInstance );
1157 free( eW.szCounterName );
1158 return ret;
1161 /***********************************************************************
1162 * PdhMakeCounterPathW (PDH.@)
1164 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1165 LPDWORD buflen, DWORD flags )
1167 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1168 PDH_STATUS ret = ERROR_SUCCESS;
1169 DWORD len;
1171 TRACE("%p %p %p 0x%08lx\n", e, buffer, buflen, flags);
1173 if (flags) FIXME("unimplemented flags 0x%08lx\n", flags);
1175 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1176 return PDH_INVALID_ARGUMENT;
1178 path[0] = 0;
1179 if (e->szMachineName)
1181 lstrcatW(path, L"\\\\");
1182 lstrcatW(path, e->szMachineName);
1184 lstrcatW(path, L"\\");
1185 lstrcatW(path, e->szObjectName);
1186 if (e->szInstanceName)
1188 lstrcatW(path, L"(");
1189 if (e->szParentInstance)
1191 lstrcatW(path, e->szParentInstance);
1192 lstrcatW(path, L"/");
1194 lstrcatW(path, e->szInstanceName);
1195 swprintf(instance, ARRAY_SIZE(instance), L"#%u", e->dwInstanceIndex);
1196 lstrcatW(path, instance);
1197 lstrcatW(path, L")");
1199 lstrcatW(path, L"\\");
1200 lstrcatW(path, e->szCounterName);
1202 len = lstrlenW(path) + 1;
1203 if (*buflen >= len) lstrcpyW(buffer, path);
1204 else ret = PDH_MORE_DATA;
1205 *buflen = len;
1206 return ret;
1209 /***********************************************************************
1210 * PdhEnumObjectItemsA (PDH.@)
1212 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1213 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1214 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1216 FIXME("%s, %s, %s, %p, %p, %p, %p, %ld, 0x%lx: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1217 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1218 pcchInstanceListLength, dwDetailLevel, dwFlags);
1220 return PDH_NOT_IMPLEMENTED;
1223 /***********************************************************************
1224 * PdhEnumObjectItemsW (PDH.@)
1226 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1227 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1228 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1230 FIXME("%s, %s, %s, %p, %p, %p, %p, %ld, 0x%lx: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1231 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1232 pcchInstanceListLength, dwDetailLevel, dwFlags);
1234 return PDH_NOT_IMPLEMENTED;
1237 /***********************************************************************
1238 * PdhSetDefaultRealTimeDataSource (PDH.@)
1240 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1242 FIXME("%lu\n", source);
1243 return ERROR_SUCCESS;
1246 /***********************************************************************
1247 * PdhGetLogFileTypeA (PDH.@)
1249 PDH_STATUS WINAPI PdhGetLogFileTypeA(const char *log, DWORD *type)
1251 FIXME("%s, %p: stub\n", debugstr_a(log), type);
1252 return PDH_NOT_IMPLEMENTED;
1255 /***********************************************************************
1256 * PdhGetLogFileTypeW (PDH.@)
1258 PDH_STATUS WINAPI PdhGetLogFileTypeW(const WCHAR *log, DWORD *type)
1260 FIXME("%s, %p: stub\n", debugstr_w(log), type);
1261 return PDH_NOT_IMPLEMENTED;
1264 /***********************************************************************
1265 * PdhBindInputDataSourceA (PDH.@)
1267 PDH_STATUS WINAPI PdhBindInputDataSourceA(PDH_HLOG *source, const char *filenamelist)
1269 FIXME("%p %s: stub\n", source, debugstr_a(filenamelist));
1270 return PDH_NOT_IMPLEMENTED;
1273 /***********************************************************************
1274 * PdhBindInputDataSourceW (PDH.@)
1276 PDH_STATUS WINAPI PdhBindInputDataSourceW(PDH_HLOG *source, const WCHAR *filenamelist)
1278 FIXME("%p %s: stub\n", source, debugstr_w(filenamelist));
1279 return PDH_NOT_IMPLEMENTED;