summaryrefslogtreecommitdiff
path: root/src/pmdas/windows/open.c
blob: 65be00df93a4cbc7cb3002017daf6289b5bbc136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
 * Copyright (c) 2008-2010 Aconex.  All Rights Reserved.
 * Copyright (c) 2004 Silicon Graphics, Inc.  All Rights Reserved.
 * Parts of this file contributed by Ken McDonell
 * (kenj At internode DoT on DoT net)
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "hypnotoad.h"
#include <winbase.h>

#define roundup(x,y)	((((x) + ((y) - 1)) / (y)) * (y))

char *windows_uname;
char *windows_build;
char *windows_machine;
unsigned long windows_pagesize;
int windows_indom_setup[NUMINDOMS];	/* initial setup done on instance */
int windows_indom_reset[NUMINDOMS];	/* instances changed on refresh */

/*
 * This block of functionality is required to map counter types from
 * their Windows semantics to equivalent PCP semantics.
 */

static struct {
    int		type;
    char	*desc;
} ctypetab[] = {
    { PERF_100NSEC_MULTI_TIMER, "PERF_100NSEC_MULTI_TIMER" },
    { PERF_100NSEC_MULTI_TIMER_INV, "PERF_100NSEC_MULTI_TIMER_INV" },
    { PERF_100NSEC_TIMER, "PERF_100NSEC_TIMER" },
    { PERF_100NSEC_TIMER_INV, "PERF_100NSEC_TIMER_INV" },
    { PERF_AVERAGE_BASE, "PERF_AVERAGE_BASE" },
    { PERF_AVERAGE_BULK, "PERF_AVERAGE_BULK" },
    { PERF_AVERAGE_TIMER, "PERF_AVERAGE_TIMER" },
    { PERF_COUNTER_100NS_QUEUELEN_TYPE, "PERF_COUNTER_100NS_QUEUELEN_TYPE" },
    { PERF_COUNTER_BULK_COUNT, "PERF_COUNTER_BULK_COUNT" },
    { PERF_COUNTER_COUNTER, "PERF_COUNTER_COUNTER" },
    { PERF_COUNTER_DELTA, "PERF_COUNTER_DELTA" },
    { PERF_COUNTER_LARGE_DELTA, "PERF_COUNTER_LARGE_DELTA" },
    { PERF_COUNTER_LARGE_QUEUELEN_TYPE, "PERF_COUNTER_LARGE_QUEUELEN_TYPE" },
    { PERF_COUNTER_LARGE_RAWCOUNT, "PERF_COUNTER_LARGE_RAWCOUNT" },
    { PERF_COUNTER_LARGE_RAWCOUNT_HEX, "PERF_COUNTER_LARGE_RAWCOUNT_HEX" },
    { PERF_COUNTER_MULTI_BASE, "PERF_COUNTER_MULTI_BASE" },
    { PERF_COUNTER_MULTI_TIMER, "PERF_COUNTER_MULTI_TIMER" },
    { PERF_COUNTER_MULTI_TIMER_INV, "PERF_COUNTER_MULTI_TIMER_INV" },
    { PERF_COUNTER_NODATA, "PERF_COUNTER_NODATA" },
    { PERF_COUNTER_QUEUELEN_TYPE, "PERF_COUNTER_QUEUELEN_TYPE" },
    { PERF_COUNTER_RAWCOUNT, "PERF_COUNTER_RAWCOUNT" },
    { PERF_COUNTER_RAWCOUNT_HEX, "PERF_COUNTER_RAWCOUNT_HEX" },
    { PERF_COUNTER_TEXT, "PERF_COUNTER_TEXT" },
    { PERF_COUNTER_TIMER, "PERF_COUNTER_TIMER" },
    { PERF_COUNTER_TIMER_INV, "PERF_COUNTER_TIMER_INV" },
    { PERF_ELAPSED_TIME, "PERF_ELAPSED_TIME" },
    { PERF_LARGE_RAW_BASE, "PERF_LARGE_RAW_BASE" },
    { PERF_OBJ_TIME_TIMER, "PERF_OBJ_TIME_TIMER" },
    { PERF_PRECISION_100NS_TIMER, "PERF_PRECISION_100NS_TIMER" },
    { PERF_PRECISION_OBJECT_TIMER, "PERF_PRECISION_OBJECT_TIMER" },
    { PERF_PRECISION_SYSTEM_TIMER, "PERF_PRECISION_SYSTEM_TIMER" },
    { PERF_RAW_BASE, "PERF_RAW_BASE" },
    { PERF_RAW_FRACTION, "PERF_RAW_FRACTION" },
    { PERF_LARGE_RAW_FRACTION, "PERF_LARGE_RAW_FRACTION" },
    { PERF_SAMPLE_BASE, "PERF_SAMPLE_BASE" },
    { PERF_SAMPLE_COUNTER, "PERF_SAMPLE_COUNTER" },
    { PERF_SAMPLE_FRACTION, "PERF_SAMPLE_FRACTION" }
};

static int ctypetab_sz = sizeof(ctypetab) / sizeof(ctypetab[0]);

static char *
decode_ctype(DWORD ctype)
{
    static char	unknown[20];
    int		i;

    for (i = 0; i < ctypetab_sz; i++)
	if (ctype == ctypetab[i].type)
	    return ctypetab[i].desc;
    sprintf(unknown, "0x%08x unknown", (int)ctype);
    return unknown;
}

static char *
string_append(char *name, char *suff)
{
    if (name == NULL) {
	name = (char *)strdup(suff);
    }
    else {
	name = (char *)realloc(name, strlen(name)+strlen(suff)+1);
	strcat(name, suff);
    }
    return name;
}

static char *
_semstr(int sem)
{
    static char	msg[20];
    if (sem == PM_SEM_COUNTER)
	return "COUNTER";
    else if (sem == PM_SEM_INSTANT)
	return "INSTANT";
    else if (sem == PM_SEM_DISCRETE)
	return "DISCRETE";
    else {
	sprintf(msg, "UNKNOWN! (%d)", sem);
	return msg;
    }
}

static char *
_typestr(int type)
{
    static char msg[20];
    if (type == PM_TYPE_32)
	return "PM_TYPE_32";
    else if (type == PM_TYPE_U32)
	return "PM_TYPE_U32";
    else if (type == PM_TYPE_64)
	return "PM_TYPE_64";
    else if (type == PM_TYPE_U64)
	return "PM_TYPE_U64";
    else if (type == PM_TYPE_FLOAT)
	return "PM_TYPE_FLOAT";
    else if (type == PM_TYPE_DOUBLE)
	return "PM_TYPE_DOUBLE";
    else {
	sprintf(msg, "UNKNOWN! (%d)", type);
	return msg;
    }
}

#if 0	// debugging
static char *
_ctypestr(int ctype)
{
    if (ctype == PERF_COUNTER_COUNTER)
	return "PERF_COUNTER_COUNTER";
    else if (ctype == PERF_RAW_FRACTION)
	return "PERF_RAW_FRACTION";
    else if (ctype == PERF_LARGE_RAW_FRACTION)
	return "PERF_LARGE_RAW_FRACTION";
    else if (ctype == PERF_COUNTER_LARGE_RAWCOUNT_HEX)
	return "PERF_COUNTER_LARGE_RAWCOUNT_HEX";
    else if (ctype == PERF_COUNTER_LARGE_RAWCOUNT)
	return "PERF_COUNTER_LARGE_RAWCOUNT";
    else if (ctype == PERF_PRECISION_100NS_TIMER)
	return "PERF_PRECISION_100NS_TIMER";
    else if (ctype == PERF_100NSEC_TIMER)
	return "PERF_100NSEC_TIMER";
    else if (ctype == PERF_COUNTER_BULK_COUNT)
	return "PERF_COUNTER_BULK_COUNT";
    else if (ctype == PERF_COUNTER_RAWCOUNT_HEX)
	return "PERF_COUNTER_RAWCOUNT_HEX";
    else if (ctype == PERF_COUNTER_RAWCOUNT)
	return "PERF_COUNTER_RAWCOUNT";
    else if (ctype == PERF_COUNTER_COUNTER)
	return "PERF_COUNTER_COUNTER";
    else
    	return "UNKNOWN";
}
#endif

/*
 * Based on documentation from ...
 * http://msdn.microsoft.com/library/default.asp?
 * 		url=/library/en-us/sysinfo/base/osversioninfoex_str.asp
 */
static void
windows_format_uname(OSVERSIONINFOEX osv)
{
    char		tbuf[80];
    char		*name = NULL;

    switch (osv.dwPlatformId) {
        case VER_PLATFORM_WIN32_NT:
	    if (osv.dwMajorVersion == 6 && osv.dwMinorVersion == 1) {
		if (osv.wProductType == VER_NT_WORKSTATION)
		    name = string_append(name, "Windows 7");
		else
		    name = string_append(name, "Windows Server 2008 R2");
	    }
	    else if (osv.dwMajorVersion == 6 && osv.dwMinorVersion == 0) {
		if (osv.wProductType == VER_NT_WORKSTATION)
		    name = string_append(name, "Windows Vista");
		else
		    name = string_append(name, "Windows Server 2008");
	    }
	    else if (osv.dwMajorVersion == 5 && osv.dwMinorVersion == 2)
		name = string_append(name, "Windows Server 2003");
	    else if (osv.dwMajorVersion == 5 && osv.dwMinorVersion == 1)
		name = string_append(name, "Windows XP");
	    else if (osv.dwMajorVersion == 5 && osv.dwMinorVersion == 0)
		name = string_append(name, "Windows 2000");
	    else if (osv.dwMajorVersion <= 4)
		name = string_append(name, "Windows NT");
	    else {
		sprintf(tbuf, "Windows Unknown (%ld.%ld)",
		    osv.dwMajorVersion, osv.dwMinorVersion); 
		name = string_append(name, tbuf);
	    }

	    /* service pack and build number etc */
	    if (osv.szCSDVersion[0] != '\0') {
		name = string_append(name, " ");
		name = string_append(name, osv.szCSDVersion);
	    }
	    sprintf(tbuf, " Build %ld", osv.dwBuildNumber & 0xFFFF);
	    windows_build = name + strlen(name) + 1;
	    windows_uname = string_append(name, tbuf);
	    break;

        default:
	    windows_uname = "Windows - Platform Unknown";
	    windows_build = "Unknown Build";
	    break;
    }
}

void
windows_setup_globals(void)
{
    SYSTEM_INFO		sysinfo;
    OSVERSIONINFOEX	osversion;

    ZeroMemory(&sysinfo, sizeof(SYSTEM_INFO));
    GetSystemInfo(&sysinfo);
    windows_pagesize = sysinfo.dwPageSize;

    switch (sysinfo.wProcessorArchitecture) {
    case PROCESSOR_ARCHITECTURE_AMD64:
	windows_machine = "x86_64";
	break;
    case PROCESSOR_ARCHITECTURE_IA64:
	windows_machine = "ia64";
	break;
    case PROCESSOR_ARCHITECTURE_INTEL:
	windows_machine = "i686";
	break;
    default:
	windows_machine = "Unknown";
	break;
    }

    ZeroMemory(&osversion, sizeof(OSVERSIONINFOEX));
    osversion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    GetVersionEx((OSVERSIONINFO *)&osversion);
    windows_format_uname(osversion);
}

static void
windows_verify_metric(pdh_metric_t *mp, PDH_COUNTER_INFO_A *infop)
{
    char	*ctr_type;

    mp->ctype = infop->dwType;

    switch (mp->ctype) {
	/*
	 * Pdh metric sematics ... from WinPerf.h
	 *
	 * SIZE
	 *	DWORD		32-bit
	 *	LARGE		64-bit
	 *	ZERO		no support here
	 *	VARIABLE_LEN	no support here
	 *
	 * TYPE
	 *	NUMBER		PM_SEM_INSTANT
	 *	    HEX		display in hex (no support here)
	 *	    DECIMAL	display as decimal
	 *	    DEC_1000	display as value / 1000
	 *			(no support here)
	 *	COUNTER		PM_SEM_COUNTER
	 *	    VALUE	display value (no support here)
	 *	    RATE	time rate converted
	 *	    FRACTION	divide value by BASE
	 *	    BASE	used for FRACTION
	 *	    QUEUELEN	magic internal queuelen() routines
	 *			(you're joking, right?)
	 *	    HISTOGRAM	counter begins or ends a histo (?)
	 *			(definitely no support here)
	 *	    PRECISION	divide counter by private clock (?)
	 *			(definitely no support here)
	 *	TEXT		no support here
	 *	ZERO		no support here
	 */

	/*
	 * Known 32-bit counters
	 */
	case PERF_COUNTER_COUNTER:
	    /* 32-bit PM_SEM_COUNTER */
	    if (mp->desc.type != PM_TYPE_32 && mp->desc.type != PM_TYPE_U32) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR,
			"windows_open: PERF_COUNTER_COUNTER: "
			"metric %s: rewrite type from %s to PM_TYPE_U32\n",
			pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U32;
	    }
	    if (mp->desc.sem != PM_SEM_COUNTER) {
		__pmNotifyErr(LOG_ERR, "windows_open: PERF_COUNTER_COUNTER: "
			"metric %s: semantics %s (expected %s)\n",
			pmIDStr(mp->desc.pmid), _semstr(mp->desc.sem),
			_semstr(PM_SEM_COUNTER));
	    }
	    break;

	case PERF_COUNTER_RAWCOUNT:
	case PERF_COUNTER_RAWCOUNT_HEX:
	    if (mp->ctype == PERF_COUNTER_RAWCOUNT)
		ctr_type = "PERF_COUNTER_RAWCOUNT";
	    else
		ctr_type = "PERF_COUNTER_RAWCOUNT_HEX";
	    /* 32-bit PM_SEM_INSTANT or PM_SEM_DISCRETE */
	    if (mp->desc.type != PM_TYPE_32 && mp->desc.type != PM_TYPE_U32) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR,
			"windows_open: Warning: %s: metric %s: "
			"rewrite type from %s to PM_TYPE_U32\n",
			ctr_type, pmIDStr(mp->desc.pmid),
			_typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U32;
	    }
	    break;

	/*
	 * Known 64-bit counters
	 */
	case PERF_COUNTER_BULK_COUNT:
	    /* 64-bit PM_SEM_COUNTER */
	    if (mp->desc.type != PM_TYPE_64 && mp->desc.type != PM_TYPE_U64) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR,
			"windows_open: PERF_COUNTER_BULK_COUNT:"
			" metric %s: rewrite type from %s to PM_TYPE_U64\n",
			pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U64;
	    }
	    if (mp->desc.sem != PM_SEM_COUNTER) {
		__pmNotifyErr(LOG_ERR, "windows_open: PERF_COUNTER_BULK_COUNT:"
			" metric %s: semantics %s (expected %s)\n",
			pmIDStr(mp->desc.pmid), _semstr(mp->desc.sem),
			_semstr(PM_SEM_COUNTER));
		mp->desc.sem = PM_SEM_COUNTER;
	    }
	    break;

	case PERF_100NSEC_TIMER:
	case PERF_PRECISION_100NS_TIMER:
	    if (mp->ctype == PERF_100NSEC_TIMER)
		ctr_type = "PERF_100NSEC_TIMER";
	    else
		ctr_type = "PERF_PRECISION_100NS_TIMER";
	    /*
	     * 64-bit PM_SEM_COUNTER, units are 100's of nanosecs,
	     * we shall export 'em as microseconds
	     */
	    if (mp->desc.type != PM_TYPE_64 && mp->desc.type != PM_TYPE_U64) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR, "windows_open: Warning: %s: "
			"metric %s: rewrite type from %s to PM_TYPE_U64\n",
		    ctr_type, pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U64;
	    }
	    if (mp->desc.sem != PM_SEM_COUNTER) {
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: %s: "
			"metric %s: semantics %s (expected %s)\n",
		    ctr_type, pmIDStr(mp->desc.pmid), _semstr(mp->desc.sem),
		    _semstr(PM_SEM_COUNTER));
		mp->desc.sem = PM_SEM_COUNTER;
	    }
	    if (mp->desc.units.dimSpace != 0 ||
		mp->desc.units.dimTime != 1 ||
		mp->desc.units.dimCount != 0 ||
		mp->desc.units.scaleTime != PM_TIME_USEC) {
		pmUnits units = mp->desc.units;
		mp->desc.units.dimSpace = mp->desc.units.dimCount = 0;
		mp->desc.units.scaleSpace = mp->desc.units.scaleCount = 0;
		mp->desc.units.dimTime = 1;
		mp->desc.units.scaleTime = PM_TIME_USEC;
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: %s: "
			"metric %s: rewrite dimension and scale from %s to %s",
		    ctr_type, pmIDStr(mp->desc.pmid), pmUnitsStr(&units),
		    pmUnitsStr(&mp->desc.units));
	    }
	    break;

	case PERF_COUNTER_LARGE_RAWCOUNT:
	case PERF_COUNTER_LARGE_RAWCOUNT_HEX:
	    /* 64-bit PM_SEM_INSTANT or PM_SEM_DISCRETE */
	    if (mp->desc.type != PM_TYPE_64 &&
		mp->desc.type != PM_TYPE_U64) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR, "windows_open: Warning: "
				"PERF_COUNTER_LARGE_RAWCOUNT: metric %s: "
				"rewrite type from %s to PM_TYPE_U64\n",
		    pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U64;
	    }
	    break;

	case PERF_RAW_FRACTION:
	    /* Float PM_SEM_INSTANT or PM_SEM_DISCRETE */
	    if (mp->desc.type != PM_TYPE_FLOAT) {
		if (!(mp->flags & M_AUTO64))
		   __pmNotifyErr(LOG_ERR, "windows_open: Warning: "
				"PERF_RAW_FRACTION: metric %s: "
				"rewrite type from %s to PM_TYPE_FLOAT\n",
			pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_FLOAT;
	    }
 	    break;

	case PERF_LARGE_RAW_FRACTION:
	    /* Double PM_SEM_INSTANT or PM_SEM_DISCRETE */
	    if (mp->desc.type != PM_TYPE_DOUBLE) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR, "windows_open: Warning: "
				"PERF_LARGE_RAW_FRACTION: metric %s: "
				"rewrite type from %s to PM_TYPE_DOUBLE\n",
			pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_DOUBLE;
	    }
	    break;

	case PERF_AVERAGE_BULK:
	case PERF_AVERAGE_TIMER:
	    if (mp->ctype == PERF_AVERAGE_BULK)
		ctr_type = "PERF_AVERAGE_BULK";
	    else
		ctr_type = "PERF_AVERAGE_TIMER";
	    /* 64-bit PM_SEM_INSTANT or PM_SEM_DISCRETE */
	    if (mp->desc.sem != PM_SEM_INSTANT && mp->desc.sem != PM_SEM_DISCRETE) {
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: %s: "
				       "metric %s: semantics %s (expected %s)\n",
					ctr_type, pmIDStr(mp->desc.pmid),
					_semstr(mp->desc.sem), _semstr(PM_SEM_INSTANT));
		mp->desc.sem = PM_SEM_INSTANT;
	    }
	    if (mp->desc.type != PM_TYPE_64 && mp->desc.type != PM_TYPE_U64) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR, "windows_open: Warning: %s "
			"metric %s: rewrite type from %s to PM_TYPE_U64\n",
			ctr_type, pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U64;
	    }
	    break;

	case PERF_SAMPLE_COUNTER:
	    ctr_type = "PERF_SAMPLE_COUNTER";
	    /* floating point PM_SEM_INSTANT or PM_SEM_DISCRETE */
	    if (mp->desc.sem != PM_SEM_INSTANT && mp->desc.sem != PM_SEM_DISCRETE) {
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: %s: "
				       "metric %s: semantics %s (expected %s)\n",
					ctr_type, pmIDStr(mp->desc.pmid),
					_semstr(mp->desc.sem), _semstr(PM_SEM_INSTANT));
		mp->desc.sem = PM_SEM_INSTANT;
	    }
	    if (mp->desc.type != PM_TYPE_FLOAT && mp->desc.type != PM_TYPE_DOUBLE) {
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: %s "
			"metric %s: rewrite type from %s to PM_TYPE_FLOAT\n",
			ctr_type, pmIDStr(mp->desc.pmid), _typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_FLOAT;
	    }
	    break;

	case PERF_ELAPSED_TIME:
	    ctr_type = "PERF_ELAPSED_TIME";
	    if (mp->desc.units.dimSpace != 0 ||
		mp->desc.units.dimTime != 1 ||
		mp->desc.units.dimCount != 0) {
		pmUnits units = mp->desc.units;
		mp->desc.units.dimSpace = mp->desc.units.dimCount = 0;
		mp->desc.units.scaleSpace = mp->desc.units.scaleCount = 0;
		mp->desc.units.dimTime = 1;
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: %s: "
			"metric %s: rewrite dimension and scale from %s to %s",
		    ctr_type, pmIDStr(mp->desc.pmid), pmUnitsStr(&units),
		    pmUnitsStr(&mp->desc.units));
	    }
	    if (mp->desc.type != PM_TYPE_64 && mp->desc.type != PM_TYPE_U64) {
		if (!(mp->flags & M_AUTO64))
		    __pmNotifyErr(LOG_ERR, "windows_open: Warning: %s "
			"metric %s: rewrite type from %s to PM_TYPE_U64\n",
			ctr_type, pmIDStr(mp->desc.pmid),
			_typestr(mp->desc.type));
		mp->desc.type = PM_TYPE_U64;
	    }
	    break;

	default:
	    __pmNotifyErr(LOG_ERR, "windows_open: Warning: metric %s: "
				   "unexpected counter type: %s\n",
			pmIDStr(mp->desc.pmid), decode_ctype(infop->dwType));
    }
    mp->flags |= M_EXPANDED;
}

int
windows_inform_metric(pdh_metric_t *pmp, LPTSTR p, pdh_value_t *pvp,
			BOOLEAN getExplainText, pdh_metric_inform_t informer)
{
    int			sts = -1;
    PDH_STATUS  	pdhsts;
    PDH_HQUERY		queryhdl = NULL;
    PDH_HCOUNTER	counterhdl = NULL;
    DWORD		result_sz;
    static DWORD	info_sz = 0;
    static LPSTR	info = NULL;

    pdhsts = PdhOpenQueryA(NULL, 0, &queryhdl);
    if (pdhsts != ERROR_SUCCESS) {
	__pmNotifyErr(LOG_ERR, "windows_open: PdhOpenQueryA failed: %s\n",
			pdherrstr(pdhsts));
	return sts;
    }

    pdhsts = PdhAddCounterA(queryhdl, p, pvp->inst, &counterhdl);
    if (pdhsts != ERROR_SUCCESS) {
	__pmNotifyErr(LOG_ERR, "windows_open: Warning: PdhAddCounterA "
				"@ pmid=%s pat=\"%s\": %s\n",
			    pmIDStr(pmp->desc.pmid), p, pdherrstr(pdhsts));
	PdhCloseQuery(queryhdl);
	return sts;
    }

    /*
     * check PCP metric semantics against PDH info
     */
    if (info_sz == 0) {
	/*
	 * We've observed an initial call to PdhGetCounterInfoA()
	 * hang with a zero sized buffer ... pander to this with
	 * an initial buffer allocation ... (size is a 100% guess).
	 */
	info_sz = 256;
	if ((info = (LPSTR)malloc(info_sz)) == NULL) {
	    __pmNotifyErr(LOG_ERR, "windows_open: PdhGetCounterInfoA "
				   "malloc (%d) failed @ metric %s: ",
				(int)info_sz, pmIDStr(pmp->desc.pmid));
	    goto done;
	}
    }
    result_sz = info_sz;
    pdhsts = PdhGetCounterInfoA(counterhdl, getExplainText, &result_sz,
					(PDH_COUNTER_INFO_A *)info);
    if (pdhsts == PDH_MORE_DATA) {
	info_sz = result_sz;
	if ((info = (LPSTR)realloc(info, info_sz)) == NULL) {
	    __pmNotifyErr(LOG_ERR, "windows_open: PdhGetCounterInfoA "
				   "realloc (%d) failed @ metric %s: ",
				(int)info_sz, pmIDStr(pmp->desc.pmid));
	    goto done;
	}
	pdhsts = PdhGetCounterInfoA(counterhdl, getExplainText, &result_sz,
				    (PDH_COUNTER_INFO_A *)info);
    }
    if (pdhsts != ERROR_SUCCESS) {
	__pmNotifyErr(LOG_ERR, "windows_open: PdhGetCounterInfoA "
				"failed @ metric %s: %s\n",
				pmIDStr(pmp->desc.pmid), pdherrstr(pdhsts));
	goto done;
    }
    else {
	informer(pmp, (PDH_COUNTER_INFO_A *)info);
	sts = 0;
    }

done:
    PdhRemoveCounter(counterhdl);
    PdhCloseQuery(queryhdl);
    return sts;
}

void
windows_verify_callback(pdh_metric_t *pmp, LPSTR pat, pdh_value_t *pvp)
{
    int	v;

    if (!(pmp->flags & M_VERIFIED)) {
	v = windows_inform_metric(pmp, pat, pvp, FALSE, windows_verify_metric);
	if (v == 0)
	    pmp->flags |= M_VERIFIED;
    }
}


/*
 * General purpose metric regex iterator, call out on each instance
 */
int
windows_visit_metric(pdh_metric_t *pmp, pdh_metric_visitor_t visitor)
{
    size_t		size;
    int			index = 0;
    PDH_STATUS  	pdhsts;
    DWORD		result_sz;
    static DWORD	pattern_sz = 0;
    static LPSTR	pattern = NULL;
    LPSTR      		p;

    if (pmp->desc.indom != PM_INDOM_NULL) {
	index = pmInDom_serial(pmp->desc.indom);
	pmdaCacheOp(pmp->desc.indom, PMDA_CACHE_INACTIVE);
    }

    pmp->flags &= ~(M_EXPANDED|M_NOVALUES);
    memset(pmp->vals, 0, pmp->num_alloc * sizeof(pdh_value_t));
    pmp->num_vals = 0;

    result_sz = 0;
    pdhsts = PdhExpandCounterPathA(pmp->pat, NULL, &result_sz);
    if (pdhsts == PDH_MORE_DATA) {
	if (result_sz >= pattern_sz) {
	    pattern_sz = roundup(result_sz, 64);
	    if ((pattern = (LPSTR)realloc(pattern, pattern_sz)) == NULL) {
		__pmNotifyErr(LOG_ERR, "windows_open: PdhExpandCounterPathA "
					"realloc (%ld) failed @ metric %s: ",
				pattern_sz, pmIDStr(pmp->desc.pmid));
		return -1;
	    }
	}
	result_sz = pattern_sz;
	pdhsts = PdhExpandCounterPathA(pmp->pat, pattern, &result_sz);
    }
    if (pdhsts != PDH_CSTATUS_VALID_DATA) {
	if (pmp->pat[0] != '\\') {
	    /*
	     * Skip metrics that are derived and do not have an explicit
	     * PDH API retrieval needed ... do nothing here.
	     */
	    ;
	}
	else if (pmp->flags & M_OPTIONAL) {
	    pmp->flags |= M_NOVALUES;
	    return 0;
	}
	else {
	    __pmNotifyErr(LOG_ERR, "windows_open: PdhExpandCounterPathA "
			"failed @ metric pmid=%s pattern=\"%s\": %s\n",
			pmIDStr(pmp->desc.pmid), pmp->pat, pdherrstr(pdhsts));
	}
	pmp->flags |= M_NOVALUES;
	return -1;
    }

    /*
     * PdhExpandCounterPathA is apparently busted ... the length
     * returned includes one byte _after_ the last NULL byte
     * string terminator, but the final byte is apparently
     * not being set ... force the issue
     */
    pattern[result_sz-1] = '\0';
    for (p = pattern; *p; p += lstrlen(p) + 1) {
	pdh_value_t	*pvp;

	pmp->num_vals++;
	if (pmp->num_vals > pmp->num_alloc) {
	    size = pmp->num_vals * sizeof(pdh_value_t);
	    if ((pmp->vals = (pdh_value_t *)realloc(pmp->vals, size)) == NULL) {
		__pmNotifyErr(LOG_ERR, "windows_open: Error: values realloc "
				   "(%d x %d) failed @ metric %s [%s]: ",
				pmp->num_vals, sizeof(pdh_value_t),
				pmIDStr(pmp->desc.pmid), p);
		pmp->num_alloc = 0;
		return -1;
	    }
	    pmp->num_alloc = pmp->num_vals;
	}

	pvp = &pmp->vals[pmp->num_vals-1];
	if (pmp->desc.indom == PM_INDOM_NULL) {
	    /* singular instance */
	    pvp->inst = PM_IN_NULL;
	    if (pmp->num_vals > 1) {
		char 	*q;
		int	k;

		/*
		 * report only once per pattern
		 */
		__pmNotifyErr(LOG_ERR, "windows_open: Warning: singular "
				"metric %s has more than one instance ...\n",
				pmIDStr(pmp->desc.pmid));
		fprintf(stderr, "  pattern: \"%s\"\n", pmp->pat);
		for (k = 0, q = pattern; *q; q += lstrlen(q) + 1, k++)
		    fprintf(stderr, "  match[%d]: \"%s\"\n", k, q);
		fprintf(stderr, "... skip this counter\n");

		/* next realloc() will be a NOP */
		pmp->num_vals--;

		/* no more we can do here, onto next metric-pattern */
		break;
	    }
	}
	else {
	    /*
	     * if metric has instance domain, parse pattern using
	     * indom type to extract instance name and number, and
	     * add into indom cache data structures as needed.
	     */
	    if ((pvp->inst = windows_lookup_instance(p, pmp)) < 0) {
		/*
		 * error reported in windows_check_instance() ...
		 * we cannot return any values for this instance if
		 * we don't recognize the name ... skip this one,
		 * the next realloc() (if any) will be a NOP
		 */
		pmp->num_vals--;

		/* move onto next instance */
		continue;
	    }
	    windows_indom_setup[index] = 1;
	}

	if (visitor)
	    visitor(pmp, p, pvp);
    }

    return 0;
}

void
windows_open(int domain)
{
    int i;

    windows_setup_globals();

    for (i = 0; i < NUMINDOMS; i++) {
	if (windows_indom_fixed(i))
	    pmdaCacheOp(INDOM(domain, i), PMDA_CACHE_LOAD);
	windows_indom_reset[i] = 0;
    }

    /*
     * This initialisation can take a long time - we have many metrics
     * now for Windows.  Better to delay this until we need to do it,
     * and then only for the metrics needed.  However, we cannot delay
     * for those metrics that may change descriptors depending on the
     * type of platform (64/32 bit, kernel version, etc), so those we
     * verify up-front.
     */
    for (i = 0; i < metricdesc_sz; i++) {
	if ((metricdesc[i].flags & M_AUTO64) || (pmDebug & DBG_TRACE_LIBPMDA))
	    windows_visit_metric(&metricdesc[i], windows_verify_callback);
    }

    for (i = 0; i < NUMINDOMS; i++) {
	/* Do we want to persist this instance domain to disk? */
	if (windows_indom_reset[i] && windows_indom_fixed(i))
	    pmdaCacheOp(INDOM(domain, i), PMDA_CACHE_SAVE);
    }
}