summaryrefslogtreecommitdiff
path: root/source3/registry/reg_perfcount.c
blob: 297e27ecc70effd2aec36f184211404bdcfe8dca (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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
/* 
 *  Unix SMB/CIFS implementation.
 *  Virtual Windows Registry Layer
 *
 *  Copyright (C) Marcin Krzysztof Porwit    2005,
 *  Copyright (C) Gerald (Jerry) Carter      2005.
 *  
 *  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.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "includes.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_RPC_SRV

#define PERFCOUNT_MAX_LEN 256

#define PERFCOUNTDIR	"perfmon"
#define NAMES_DB	"names.tdb"
#define DATA_DB		"data.tdb"

PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind);

/*********************************************************************
*********************************************************************/

static char* counters_directory( const char *dbname )
{
	static pstring fname;
	fstring path;
	
	if ( !dbname )
		return NULL;
	
	fstr_sprintf( path, "%s/%s", PERFCOUNTDIR, dbname );
	
	pstrcpy( fname, lock_path( path ) );
	
	return fname;
}

/*********************************************************************
*********************************************************************/

void perfcount_init_keys( void )
{
	char *p = lock_path(PERFCOUNTDIR);

	/* no registry keys; just create the perfmon directory */
	
	if ( !directory_exist( p, NULL ) )
		mkdir( p, 0755 );
	
	return;
}

/*********************************************************************
*********************************************************************/

uint32 reg_perfcount_get_base_index(void)
{
	const char *fname = counters_directory( NAMES_DB );
	TDB_CONTEXT *names;
	TDB_DATA kbuf, dbuf;
	char key[] = "1";
	uint32 retval = 0;
	char buf[PERFCOUNT_MAX_LEN];

	names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);

	if ( !names ) {
		DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
		return 0;
	}    
	/* needs to read the value of key "1" from the counter_names.tdb file, as that is
	   where the total number of counters is stored. We're assuming no holes in the
	   enumeration.
	   The format for the counter_names.tdb file is:
	   key        value
	   1          num_counters
	   2          perf_counter1
	   3          perf_counter1_help
	   4          perf_counter2
	   5          perf_counter2_help
	   even_num   perf_counter<even_num>
	   even_num+1 perf_counter<even_num>_help
	   and so on.
	   So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
	kbuf = string_tdb_data(key);
	dbuf = tdb_fetch(names, kbuf);
	if(dbuf.dptr == NULL)
	{
		DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
		tdb_close(names);
		return 0;
	}
	else
	{
		tdb_close(names);
		memset(buf, 0, PERFCOUNT_MAX_LEN);
		memcpy(buf, dbuf.dptr, dbuf.dsize);
		retval = (uint32)atoi(buf);
		SAFE_FREE(dbuf.dptr);
		return retval;
	}
	return 0;
}

/*********************************************************************
*********************************************************************/

uint32 reg_perfcount_get_last_counter(uint32 base_index)
{
	uint32 retval;

	if(base_index == 0)
		retval = 0;
	else
		retval = base_index * 2;

	return retval;
}

/*********************************************************************
*********************************************************************/

uint32 reg_perfcount_get_last_help(uint32 last_counter)
{
	uint32 retval;

	if(last_counter == 0)
		retval = 0;
	else
		retval = last_counter + 1;

	return retval;
}


/*********************************************************************
*********************************************************************/

static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
					       int keyval,
					       char **retbuf,
					       uint32 buffer_size)
{
	TDB_DATA kbuf, dbuf;
	char temp[256];
	char *buf1 = *retbuf;
	uint32 working_size = 0;
	UNISTR2 name_index, name;

	memset(temp, 0, sizeof(temp));
	snprintf(temp, sizeof(temp), "%d", keyval);
	kbuf = string_tdb_data(temp);
	dbuf = tdb_fetch(tdb, kbuf);
	if(dbuf.dptr == NULL)
	{
		/* If a key isn't there, just bypass it -- this really shouldn't 
		   happen unless someone's mucking around with the tdb */
		DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
			  temp, tdb_name(tdb)));
		return buffer_size;
	}
	/* First encode the name_index */
	working_size = (kbuf.dsize + 1)*sizeof(uint16);
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
	if(!buf1) {
		buffer_size = 0;
		return buffer_size;
	}
	init_unistr2(&name_index, (const char *)kbuf.dptr, UNI_STR_TERMINATE);
	memcpy(buf1+buffer_size, (char *)name_index.buffer, working_size);
	buffer_size += working_size;
	/* Now encode the actual name */
	working_size = (dbuf.dsize + 1)*sizeof(uint16);
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
	if(!buf1) {
		buffer_size = 0;
		return buffer_size;
	}
	memset(temp, 0, sizeof(temp));
	memcpy(temp, dbuf.dptr, dbuf.dsize);
	SAFE_FREE(dbuf.dptr);
	init_unistr2(&name, temp, UNI_STR_TERMINATE);
	memcpy(buf1+buffer_size, (char *)name.buffer, working_size);
	buffer_size += working_size;

	*retbuf = buf1;

	return buffer_size;
}

/*********************************************************************
*********************************************************************/

uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
{
	char *buf1 = NULL;
	uint32 buffer_size = 0;
	TDB_CONTEXT *names;
	const char *fname = counters_directory( NAMES_DB );
	int i;

	if(base_index == 0)
		return 0;

	names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);

	if(names == NULL)
	{
		DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
		return 0;
	}    

	for(i = 1; i <= base_index; i++)
	{
		buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
	}
	tdb_close(names);

	/* Now terminate the MULTI_SZ with a double unicode NULL */
	buf1 = *retbuf;
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
	if(!buf1) {
		buffer_size = 0;
	} else {
		buf1[buffer_size++] = '\0';
		buf1[buffer_size++] = '\0';
	}

	*retbuf = buf1;

	return buffer_size;
}

/*********************************************************************
*********************************************************************/

uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
{
	char *buf1 = NULL;
	uint32 buffer_size = 0;
	TDB_CONTEXT *names;
	const char *fname = counters_directory( NAMES_DB );
	int i;

	if(base_index == 0)
		return 0;

	names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);

	if(names == NULL)
	{
		DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
		return 0;
	}    

	buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);

	for(i = 1; i <= base_index; i++)
	{
		buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
	}
	tdb_close(names);

	/* Now terminate the MULTI_SZ with a double unicode NULL */
	buf1 = *retbuf;
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
	if(!buf1) {
		buffer_size = 0;
	} else {
		buf1[buffer_size++] = '\0';
		buf1[buffer_size++] = '\0';
	}

	*retbuf=buf1;

	return buffer_size;
}

/*********************************************************************
*********************************************************************/

static void _reg_perfcount_make_key(TDB_DATA *key,
				    char *buf,
				    int buflen,
				    int key_part1,
				    const char *key_part2)
{
	memset(buf, 0, buflen);
	if(key_part2 != NULL)
		snprintf(buf, buflen,"%d%s", key_part1, key_part2);
	else 
		snprintf(buf, buflen, "%d", key_part1);

	*key = string_tdb_data(buf);

	return;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_isparent(TDB_DATA data)
{
	if(data.dsize > 0)
	{
		if(data.dptr[0] == 'p')
			return True;
		else
			return False;
	}
	return False;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_ischild(TDB_DATA data)
{
	if(data.dsize > 0)
	{
		if(data.dptr[0] == 'c')
			return True;
		else
			return False;
	}
	return False;
}

/*********************************************************************
*********************************************************************/

static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
{
	TDB_DATA key, data;
	char buf[PERFCOUNT_MAX_LEN];

	_reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
	data = tdb_fetch(names, key);

	if(data.dptr == NULL)
		return (uint32)PERF_NO_INSTANCES;
    
	memset(buf, 0, PERFCOUNT_MAX_LEN);
	memcpy(buf, data.dptr, data.dsize);
	SAFE_FREE(data.dptr);
	return (uint32)atoi(buf);
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
				      prs_struct *ps,
				      int num,
				      TDB_DATA data,
				      TDB_CONTEXT *names)
{
	int i;
	BOOL success = True;
	PERF_OBJECT_TYPE *obj;

	block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
								  block->objects,
								  PERF_OBJECT_TYPE,
								  block->NumObjectTypes+1);
	if(block->objects == NULL)
		return False;
	obj = &(block->objects[block->NumObjectTypes]);
	memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(PERF_OBJECT_TYPE));
	block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
	block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
	block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
	block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
	block->objects[block->NumObjectTypes].NumCounters = 0;
	block->objects[block->NumObjectTypes].DefaultCounter = 0;
	block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
	block->objects[block->NumObjectTypes].counters = NULL;
	block->objects[block->NumObjectTypes].instances = NULL;
	block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
	block->objects[block->NumObjectTypes].counter_data.data = NULL;
	block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
	block->NumObjectTypes+=1;

	for(i = 0; i < (int)obj->NumInstances; i++) {
		success = _reg_perfcount_add_instance(obj, ps, i, names);
	}

	return success;
}

/*********************************************************************
*********************************************************************/

BOOL _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
{
	TDB_CONTEXT *counters;
	const char *fname = counters_directory( DATA_DB );
    
	counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);

	if(counters == NULL)
	{
		DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
		return False;
	}    

	*data = tdb_fetch(counters, key);
    
	tdb_close(counters);

	return True;
}

/*********************************************************************
*********************************************************************/

static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
{
	uint32 retval;

	retval = CounterType;

	/* First mask out reserved lower 8 bits */
	retval = retval & 0xFFFFFF00;
	retval = retval << 22;
	retval = retval >> 22;

	return retval;
}

/*********************************************************************
*********************************************************************/

static uint32 _reg_perfcount_compute_scale(SMB_BIG_INT data)
{
	int scale = 0;
	if(data == 0)
		return scale;
	while(data > 100)
	{
		data /= 10;
		scale--;
	}
	while(data < 10)
	{
		data *= 10;
		scale++;
	}

	return (uint32)scale;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
					    prs_struct *ps,
					    int CounterIndex,
					    PERF_OBJECT_TYPE *obj,
					    TDB_CONTEXT *names)
{
	TDB_DATA key, data;
	char buf[PERFCOUNT_MAX_LEN];
	size_t dsize, padding;
	long int data32, dbuf[2];
	SMB_BIG_INT data64;
	uint32 counter_size;

	obj->counters[obj->NumCounters].DefaultScale = 0;
	dbuf[0] = dbuf[1] = 0;
	padding = 0;

	_reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
	data = tdb_fetch(names, key);
	if(data.dptr == NULL)
	{
		DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
		return False;
	}
	memset(buf, 0, PERFCOUNT_MAX_LEN);
	memcpy(buf, data.dptr, data.dsize);
	obj->counters[obj->NumCounters].CounterType = atoi(buf);
	DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
		   obj->counters[obj->NumCounters].CounterType, CounterIndex));
	SAFE_FREE(data.dptr);

	/* Fetch the actual data */
	_reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
	_reg_perfcount_get_counter_data(key, &data);
	if(data.dptr == NULL)
	{
		DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
		return False;
	}
    
	counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);

	if(counter_size == PERF_SIZE_DWORD)
	{
		dsize = sizeof(data32);
		memset(buf, 0, PERFCOUNT_MAX_LEN);
		memcpy(buf, data.dptr, data.dsize);
		data32 = strtol(buf, NULL, 0);
		if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
			obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((SMB_BIG_INT)data32);
		else
			obj->counters[obj->NumCounters].DefaultScale = 0;
		dbuf[0] = data32;
		padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
	}
	else if(counter_size == PERF_SIZE_LARGE)
	{
		dsize = sizeof(data64);
		memset(buf, 0, PERFCOUNT_MAX_LEN);
		memcpy(buf, data.dptr, data.dsize);
		data64 = atof(buf);
		if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
			obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
		else
			obj->counters[obj->NumCounters].DefaultScale = 0;
		memcpy((void *)dbuf, (const void *)&data64, dsize);
		padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
	}
	else /* PERF_SIZE_VARIABLE_LEN */
	{
		dsize = data.dsize;
		memset(buf, 0, PERFCOUNT_MAX_LEN);
		memcpy(buf, data.dptr, data.dsize);
	}
	SAFE_FREE(data.dptr);

	obj->counter_data.ByteLength += dsize + padding;
	obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
						      obj->counter_data.data,
						      uint8,
						      obj->counter_data.ByteLength - sizeof(uint32));
	if(obj->counter_data.data == NULL)
		return False;
	if(dbuf[0] != 0 || dbuf[1] != 0)
	{
		memcpy((void *)(obj->counter_data.data + 
				(obj->counter_data.ByteLength - (sizeof(uint32) + dsize))), 
		       (const void *)dbuf, dsize);
	}
	else
	{
		/* Handling PERF_SIZE_VARIABLE_LEN */
		memcpy((void *)(obj->counter_data.data +
				(obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
		       (const void *)buf, dsize);
	}
	obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
	if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
	{
		DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
	}
	obj->counters[obj->NumCounters].CounterSize = dsize;

	return True;
}

/*********************************************************************
*********************************************************************/

PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
{
	int i;

	PERF_OBJECT_TYPE *obj = NULL;

	for(i = 0; i < block->NumObjectTypes; i++)
	{
		if(block->objects[i].ObjectNameTitleIndex == objind)
		{
			obj = &(block->objects[i]);
		}
	}

	return obj;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
				       prs_struct *ps,
				       int num,
				       TDB_DATA data,
				       TDB_CONTEXT *names)
{
	char *begin, *end, *start, *stop;
	int parent;
	PERF_OBJECT_TYPE *obj;
	BOOL success = True;
	char buf[PERFCOUNT_MAX_LEN];
    
	obj = NULL;
	memset(buf, 0, PERFCOUNT_MAX_LEN);
	memcpy(buf, data.dptr, data.dsize);
	begin = index(buf, '[');
	end = index(buf, ']');
	if(begin == NULL || end == NULL)
		return False;
	start = begin+1;

	while(start < end) {
		stop = index(start, ',');
		if(stop == NULL)
			stop = end;
		*stop = '\0';
		parent = atoi(start);

		obj = _reg_perfcount_find_obj(block, parent);
		if(obj == NULL) {
			/* At this point we require that the parent object exist.
			   This can probably be handled better at some later time */
			DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
				  parent, num));
			return False;
		}
		obj->counters = (PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
										obj->counters,
										PERF_COUNTER_DEFINITION,
										obj->NumCounters+1);
		if(obj->counters == NULL)
			return False;
		memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(PERF_COUNTER_DEFINITION));
		obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
		obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
		obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
		obj->counters[obj->NumCounters].ByteLength = sizeof(PERF_COUNTER_DEFINITION);
		success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
		obj->NumCounters += 1;
		start = stop + 1;
	}
    	
	/* Handle case of Objects/Counters without any counter data, which would suggest
	   that the required instances are not there yet, so change NumInstances from
	   PERF_NO_INSTANCES to 0 */

	return success;
}

/*********************************************************************
*********************************************************************/

BOOL _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
				      prs_struct *ps,
				      int instId,
				      PERF_OBJECT_TYPE *obj,
				      TDB_CONTEXT *names)
{
	TDB_DATA key, data;
	char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
	wpstring name;
	int pad;

	/* First grab the instance data from the data file */
	memset(temp, 0, PERFCOUNT_MAX_LEN);
	snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
	_reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
	_reg_perfcount_get_counter_data(key, &data);
	if(data.dptr == NULL)
	{
		DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
			  buf));
		return False;
	}
	inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
	inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
						       inst->counter_data.data,
						       uint8,
						       data.dsize);
	if(inst->counter_data.data == NULL)
		return False;
	memset(inst->counter_data.data, 0, data.dsize);
	memcpy(inst->counter_data.data, data.dptr, data.dsize);
	SAFE_FREE(data.dptr);

	/* Fetch instance name */
	memset(temp, 0, PERFCOUNT_MAX_LEN);
	snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
	_reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
	data = tdb_fetch(names, key);
	if(data.dptr == NULL)
	{
		/* Not actually an error, but possibly unintended? -- just logging FYI */
		DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
			  buf));
		inst->NameLength = 0;
	}
	else
	{
		memset(buf, 0, PERFCOUNT_MAX_LEN);
		memcpy(buf, data.dptr, data.dsize);
		rpcstr_push((void *)name, buf, sizeof(name), STR_TERMINATE);
		inst->NameLength = (strlen_w(name) * 2) + 2;
		inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
						  inst->data,
						  uint8,
						  inst->NameLength);
		if (inst->data == NULL) {
			SAFE_FREE(data.dptr);
			return False;
		}
		memcpy(inst->data, name, inst->NameLength);
		SAFE_FREE(data.dptr);
	}

	inst->ParentObjectTitleIndex = 0;
	inst->ParentObjectTitlePointer = 0;
	inst->UniqueID = PERF_NO_UNIQUE_ID;
	inst->NameOffset = 6 * sizeof(uint32);
    
	inst->ByteLength = inst->NameOffset + inst->NameLength;
	/* Need to be aligned on a 64-bit boundary here for counter_data */
	if((pad = (inst->ByteLength % 8)))
	{
		pad = 8 - pad;
		inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
						  inst->data,
						  uint8,
						  inst->NameLength + pad);
		memset(inst->data + inst->NameLength, 0, pad);
		inst->ByteLength += pad;
	}

	return True;
}

/*********************************************************************
*********************************************************************/

BOOL _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
				 prs_struct *ps,
				 int instInd,
				 TDB_CONTEXT *names)
{
	PERF_INSTANCE_DEFINITION *inst;

	if(obj->instances == NULL) {
		obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
						      obj->instances,
						      PERF_INSTANCE_DEFINITION,
						      obj->NumInstances);
	}
	if(obj->instances == NULL)
		return False;
    
	memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
	inst = &(obj->instances[instInd]);
	return _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
}

/*********************************************************************
*********************************************************************/

static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
					  prs_struct *ps,
					  int base_index,
					  TDB_CONTEXT *names)
{
	BOOL success;
	int i, j, retval = 0;
	char keybuf[PERFCOUNT_MAX_LEN];
	TDB_DATA key, data;

	for(i = 1; i <= base_index; i++)
	{
		j = i*2;
		_reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
		data = tdb_fetch(names, key);
		if(data.dptr != NULL)
		{
			if(_reg_perfcount_isparent(data))
				success = _reg_perfcount_add_object(block, ps, j, data, names);
			else if(_reg_perfcount_ischild(data))
				success = _reg_perfcount_add_counter(block, ps, j, data, names);
			else
			{
				DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
				success = False;
			}
			if(success == False)
			{
				DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
				retval = -1;
			}
			SAFE_FREE(data.dptr);
		}
		else
			DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
	}	
	return retval;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_get_64(SMB_BIG_UINT *retval,
				  TDB_CONTEXT *tdb,
				  int key_part1,
				  const char *key_part2)
{
	TDB_DATA key, data;
	char buf[PERFCOUNT_MAX_LEN];

	_reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);

	data = tdb_fetch(tdb, key);
	if(data.dptr == NULL)
	{
		DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
		return False;
	}

	memset(buf, 0, PERFCOUNT_MAX_LEN);
	memcpy(buf, data.dptr, data.dsize);
	SAFE_FREE(data.dptr);

	*retval = atof(buf);

	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
						TDB_CONTEXT *names)
{
	SMB_BIG_UINT PerfFreq, PerfTime, PerfTime100nSec;
	TDB_CONTEXT *counters;
	BOOL status = False;
	const char *fname = counters_directory( DATA_DB );
    
	counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
    
	if(counters == NULL)
	{
		DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
		return False;
	}    
    
	status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
	if(status == False)
	{
		tdb_close(counters);
		return status;
	}
	memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));

	status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
	if(status == False)
	{
		tdb_close(counters);
		return status;
	}
	memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));

	status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
	if(status == False)
	{
		tdb_close(counters);
		return status;
	}
	memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));

	tdb_close(counters);
	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block,
					   prs_struct *ps, TDB_CONTEXT *names)
{
	wpstring temp;
	time_t tm;
 
	memset(temp, 0, sizeof(temp));
	rpcstr_push((void *)temp, "PERF", sizeof(temp), STR_TERMINATE);
	memcpy(block->Signature, temp, strlen_w(temp) *2);

	if(ps->bigendian_data == RPC_BIG_ENDIAN)
		block->LittleEndian = 0;
	else
		block->LittleEndian = 1;
	block->Version = 1;
	block->Revision = 1;
	block->TotalByteLength = 0;
	block->NumObjectTypes = 0;
	block->DefaultObject = -1;
	block->objects = NULL;
	tm = time(NULL);
	make_systemtime(&(block->SystemTime), gmtime(&tm));
	_reg_perfcount_init_data_block_perf(block, names);
	memset(temp, 0, sizeof(temp));
	rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
	block->SystemNameLength = (strlen_w(temp) * 2) + 2;
	block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
	if (block->data == NULL) {
		return False;
	}
	memcpy(block->data, temp, block->SystemNameLength);
	block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data); 
	block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
	/* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
	   so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
	block->HeaderLength += 8 - (block->HeaderLength % 8);

	return True;
}

/*********************************************************************
*********************************************************************/

static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
{
	int obj, cnt, inst, pad, i;
	PERF_OBJECT_TYPE *object;
	PERF_INSTANCE_DEFINITION *instance;
	PERF_COUNTER_DEFINITION *counter;
	PERF_COUNTER_BLOCK *counter_data;
	char *temp = NULL, *src_addr, *dst_addr;

	block->TotalByteLength = 0;
	object = block->objects;
	for(obj = 0; obj < block->NumObjectTypes; obj++)
	{
		object[obj].TotalByteLength = 0;
		object[obj].DefinitionLength = 0;
		instance = object[obj].instances;
		counter = object[obj].counters;
		for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
		{
			object[obj].TotalByteLength += counter[cnt].ByteLength;
			object[obj].DefinitionLength += counter[cnt].ByteLength;
		}
		if(object[obj].NumInstances != PERF_NO_INSTANCES)
		{
			for(inst = 0; inst < object[obj].NumInstances; inst++)
			{
				instance = &(object[obj].instances[inst]);
				object[obj].TotalByteLength += instance->ByteLength;
				counter_data = &(instance->counter_data);
				counter = &(object[obj].counters[object[obj].NumCounters - 1]);
				counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
				temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
							    temp, 
							    char, 
							    counter_data->ByteLength- sizeof(counter_data->ByteLength));
				if (temp == NULL) {
					return 0;
				}
				memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
				src_addr = (char *)counter_data->data;
				for(i = 0; i < object[obj].NumCounters; i++)
				{
					counter = &(object[obj].counters[i]);
					dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
					memcpy(dst_addr, src_addr, counter->CounterSize);
				        src_addr += counter->CounterSize;
				}
				/* Make sure to be 64-bit aligned */
				if((pad = (counter_data->ByteLength % 8)))
				{
					pad = 8 - pad;
				}
				counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
									 counter_data->data,
									 uint8,
									 counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
				if (counter_data->data == NULL) {
					return 0;
				}
				memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
				memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
				counter_data->ByteLength += pad;
				object[obj].TotalByteLength += counter_data->ByteLength;
			}
		}
		else
		{
			/* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
			   so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
			if((pad = (object[obj].counter_data.ByteLength % 8)))
			{
				pad = 8 - pad;
				object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
										     object[obj].counter_data.data,
										     uint8, 
										     object[obj].counter_data.ByteLength + pad);
				memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
				object[obj].counter_data.ByteLength += pad;
			}
			object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
		}
		object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK));
		object[obj].TotalByteLength += object[obj].HeaderLength;
		object[obj].DefinitionLength += object[obj].HeaderLength;
		
		block->TotalByteLength += object[obj].TotalByteLength;
	}

	return block->TotalByteLength;
}

/*********************************************************************
*********************************************************************/

uint32 reg_perfcount_get_perf_data_block(uint32 base_index, 
					 prs_struct *ps, 
					 PERF_DATA_BLOCK *block,
					 const char *object_ids)
{
	uint32 buffer_size = 0;
	const char *fname = counters_directory( NAMES_DB );
	TDB_CONTEXT *names;
	int retval = 0;
	
	names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);

	if(names == NULL)
	{
		DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
		return 0;
	}

	if (!_reg_perfcount_init_data_block(block, ps, names)) {
		DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
		tdb_close(names);
		return 0;
	}

	reg_perfcount_get_last_counter(base_index);
    
	if(object_ids == NULL)
	{
		/* we're getting a request for "Global" here */
		retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
	}
	else
	{
		/* we're getting a request for a specific set of PERF_OBJECT_TYPES */
		retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
	}
	buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);

	tdb_close(names);

	if (retval == -1) {
		return 0;
	}

	return buffer_size + block->HeaderLength;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
{
	int i;
	prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
	depth++;

	if(!prs_align(ps))
		return False;
	for(i = 0; i < 4; i++)
	{
		if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
			return False;
	}
	if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
		return False;
	if(!prs_uint32("Version", ps, depth, &block.Version))
		return False;
	if(!prs_uint32("Revision", ps, depth, &block.Revision))
		return False;
	if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
		return False;
	if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
		return False;
	if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
		return False;
	if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
		return False;
	if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime))
		return False;
	if(!prs_uint32("Padding", ps, depth, &block.Padding))
		return False;
	if(!prs_align_uint64(ps))
		return False;
	if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
		return False;
	if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
		return False;
	if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
		return False;
	if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
		return False;
	if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
		return False;
	/* hack to make sure we're 64-bit aligned at the end of this whole mess */
	if(!prs_uint8s(False, "SystemName", ps, depth, block.data, 
		       block.HeaderLength - block.SystemNameOffset)) 
		return False;

	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_marshall_perf_counters(prs_struct *ps,
						  PERF_OBJECT_TYPE object,
						  int depth)
{
	int cnt;
	PERF_COUNTER_DEFINITION counter;

	prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
	depth++;
    
	for(cnt = 0; cnt < object.NumCounters; cnt++)
	{
		counter = object.counters[cnt];

		if(!prs_align(ps))
			return False;
		if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
			return False;
		if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
			return False;
		if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
			return False;
		if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
			return False;
		if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
			return False;
		if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
			return False;
		if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
			return False;
		if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
			return False;
		if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
			return False;
		if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
			return False;
	}

	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
						      PERF_COUNTER_BLOCK counter_data, 
						      int depth)
{
	prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
	depth++;
    
	if(!prs_align_uint64(ps))
		return False;
    
	if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
		return False;
	if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
		return False;
	if(!prs_align_uint64(ps))
		return False;

	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_marshall_perf_instances(prs_struct *ps,
						   PERF_OBJECT_TYPE object, 
						   int depth)
{
	PERF_INSTANCE_DEFINITION instance;
	int inst;

	prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
	depth++;

	for(inst = 0; inst < object.NumInstances; inst++)
	{
		instance = object.instances[inst];

		if(!prs_align(ps))
			return False;
		if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
			return False;
		if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
			return False;
		if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
			return False;
		if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
			return False;
		if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
			return False;
		if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
			return False;
		if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
			       instance.ByteLength - instance.NameOffset))
			return False;
		if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
			return False;
	}
	
	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
{
	int obj;

	PERF_OBJECT_TYPE object;
    
	prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
	depth++;

	for(obj = 0; obj < block.NumObjectTypes; obj++)
	{
		object = block.objects[obj];

		if(!prs_align(ps))
			return False;

		if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
			return False;
		if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
			return False;
		if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
			return False;
		if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
			return False;
		if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
			return False;
		if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
			return False;
		if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
			return False;
		if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
			return False;
		if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
			return False;
		if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
			return False;
		if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
			return False;
		if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
			return False;
		if(!prs_align_uint64(ps))
			return False;
		if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
			return False;
		if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
			return False;

		/* Now do the counters */
		/* If no instances, encode counter_data */
		/* If instances, encode instace plus counter data for each instance */
		if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
			return False;
		if(object.NumInstances == PERF_NO_INSTANCES)
		{
			if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
				return False;
		}
		else
		{
			if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
				return False;
		}
	}

	return True;
}

/*********************************************************************
*********************************************************************/

static BOOL _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
{
	int depth = 0;
	if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
	{
		if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
			return True;
	}
	return False;
}

/*********************************************************************
*********************************************************************/

WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
{
	/*
	 * For a detailed description of the layout of this structure,
	 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
	 *
	 * By 2006-11-23 this link did not work anymore, I found something
	 * promising under
	 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
	 */
	PERF_DATA_BLOCK block;
	uint32 buffer_size, base_index; 
    
	buffer_size = 0;
	base_index = reg_perfcount_get_base_index();
	ZERO_STRUCT(block);

	buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);

	if(buffer_size < max_buf_size)
	{
		*outbuf_len = buffer_size;
		if(_reg_perfcount_marshall_hkpd(ps, block) == True)
			return WERR_OK;
		else
			return WERR_NOMEM;
	}
	else
	{
		*outbuf_len = max_buf_size;
		_reg_perfcount_marshall_perf_data_block(ps, block, 0);
		return WERR_INSUFFICIENT_BUFFER;
	}
}