selftest: fix "none" test
[metze/samba/wip.git] / source3 / registry / reg_perfcount.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *
5  *  Copyright (C) Marcin Krzysztof Porwit    2005,
6  *  Copyright (C) Gerald (Jerry) Carter      2005.
7  *  
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *  
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *  
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 #define PERFCOUNT_MAX_LEN 256
28
29 #define PERFCOUNTDIR    "perfmon"
30 #define NAMES_DB        "names.tdb"
31 #define DATA_DB         "data.tdb"
32
33 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind);
34
35 /*********************************************************************
36 *********************************************************************/
37
38 static char *counters_directory(const char *dbname)
39 {
40         char *path = NULL;
41         char *ret = NULL;
42         TALLOC_CTX *ctx = talloc_tos();
43
44         if (!dbname)
45                 return NULL;
46
47         path = talloc_asprintf(ctx, "%s/%s", PERFCOUNTDIR, dbname);
48         if (!path) {
49                 return NULL;
50         }
51
52         ret = talloc_strdup(ctx, state_path(path));
53         TALLOC_FREE(path);
54         return ret;
55 }
56
57 /*********************************************************************
58 *********************************************************************/
59
60 void perfcount_init_keys( void )
61 {
62         char *p = state_path(PERFCOUNTDIR);
63
64         /* no registry keys; just create the perfmon directory */
65         
66         if ( !directory_exist( p ) )
67                 mkdir( p, 0755 );
68         
69         return;
70 }
71
72 /*********************************************************************
73 *********************************************************************/
74
75 uint32 reg_perfcount_get_base_index(void)
76 {
77         const char *fname = counters_directory( NAMES_DB );
78         TDB_CONTEXT *names;
79         TDB_DATA kbuf, dbuf;
80         char key[] = "1";
81         uint32 retval = 0;
82         char buf[PERFCOUNT_MAX_LEN];
83
84         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
85
86         if ( !names ) {
87                 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname));
88                 return 0;
89         }    
90         /* needs to read the value of key "1" from the counter_names.tdb file, as that is
91            where the total number of counters is stored. We're assuming no holes in the
92            enumeration.
93            The format for the counter_names.tdb file is:
94            key        value
95            1          num_counters
96            2          perf_counter1
97            3          perf_counter1_help
98            4          perf_counter2
99            5          perf_counter2_help
100            even_num   perf_counter<even_num>
101            even_num+1 perf_counter<even_num>_help
102            and so on.
103            So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
104         kbuf = string_tdb_data(key);
105         dbuf = tdb_fetch(names, kbuf);
106         if(dbuf.dptr == NULL)
107         {
108                 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname));
109                 tdb_close(names);
110                 return 0;
111         }
112         else
113         {
114                 tdb_close(names);
115                 memset(buf, 0, PERFCOUNT_MAX_LEN);
116                 memcpy(buf, dbuf.dptr, dbuf.dsize);
117                 retval = (uint32)atoi(buf);
118                 SAFE_FREE(dbuf.dptr);
119                 return retval;
120         }
121         return 0;
122 }
123
124 /*********************************************************************
125 *********************************************************************/
126
127 uint32 reg_perfcount_get_last_counter(uint32 base_index)
128 {
129         uint32 retval;
130
131         if(base_index == 0)
132                 retval = 0;
133         else
134                 retval = base_index * 2;
135
136         return retval;
137 }
138
139 /*********************************************************************
140 *********************************************************************/
141
142 uint32 reg_perfcount_get_last_help(uint32 last_counter)
143 {
144         uint32 retval;
145
146         if(last_counter == 0)
147                 retval = 0;
148         else
149                 retval = last_counter + 1;
150
151         return retval;
152 }
153
154
155 /*********************************************************************
156 *********************************************************************/
157
158 static uint32 _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb, 
159                                                int keyval,
160                                                char **retbuf,
161                                                uint32 buffer_size)
162 {
163         TDB_DATA kbuf, dbuf;
164         char temp[256];
165         char *buf1 = *retbuf;
166         uint32 working_size = 0;
167         UNISTR2 name_index, name;
168
169         memset(temp, 0, sizeof(temp));
170         snprintf(temp, sizeof(temp), "%d", keyval);
171         kbuf = string_tdb_data(temp);
172         dbuf = tdb_fetch(tdb, kbuf);
173         if(dbuf.dptr == NULL)
174         {
175                 /* If a key isn't there, just bypass it -- this really shouldn't 
176                    happen unless someone's mucking around with the tdb */
177                 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
178                           temp, tdb_name(tdb)));
179                 return buffer_size;
180         }
181         /* First encode the name_index */
182         working_size = (kbuf.dsize + 1)*sizeof(uint16);
183         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
184         if(!buf1) {
185                 buffer_size = 0;
186                 return buffer_size;
187         }
188         init_unistr2(&name_index, (const char *)kbuf.dptr, UNI_STR_TERMINATE);
189         memcpy(buf1+buffer_size, (char *)name_index.buffer, working_size);
190         buffer_size += working_size;
191         /* Now encode the actual name */
192         working_size = (dbuf.dsize + 1)*sizeof(uint16);
193         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
194         if(!buf1) {
195                 buffer_size = 0;
196                 return buffer_size;
197         }
198         memset(temp, 0, sizeof(temp));
199         memcpy(temp, dbuf.dptr, dbuf.dsize);
200         SAFE_FREE(dbuf.dptr);
201         init_unistr2(&name, temp, UNI_STR_TERMINATE);
202         memcpy(buf1+buffer_size, (char *)name.buffer, working_size);
203         buffer_size += working_size;
204
205         *retbuf = buf1;
206
207         return buffer_size;
208 }
209
210 /*********************************************************************
211 *********************************************************************/
212
213 uint32 reg_perfcount_get_counter_help(uint32 base_index, char **retbuf)
214 {
215         char *buf1 = NULL;
216         uint32 buffer_size = 0;
217         TDB_CONTEXT *names;
218         const char *fname = counters_directory( NAMES_DB );
219         int i;
220
221         if(base_index == 0)
222                 return 0;
223
224         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
225
226         if(names == NULL)
227         {
228                 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname));
229                 return 0;
230         }    
231
232         for(i = 1; i <= base_index; i++)
233         {
234                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, (i*2)+1, retbuf, buffer_size);
235         }
236         tdb_close(names);
237
238         /* Now terminate the MULTI_SZ with a double unicode NULL */
239         buf1 = *retbuf;
240         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
241         if(!buf1) {
242                 buffer_size = 0;
243         } else {
244                 buf1[buffer_size++] = '\0';
245                 buf1[buffer_size++] = '\0';
246         }
247
248         *retbuf = buf1;
249
250         return buffer_size;
251 }
252
253 /*********************************************************************
254 *********************************************************************/
255
256 uint32 reg_perfcount_get_counter_names(uint32 base_index, char **retbuf)
257 {
258         char *buf1 = NULL;
259         uint32 buffer_size = 0;
260         TDB_CONTEXT *names;
261         const char *fname = counters_directory( NAMES_DB );
262         int i;
263
264         if(base_index == 0)
265                 return 0;
266
267         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
268
269         if(names == NULL)
270         {
271                 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
272                 return 0;
273         }    
274
275         buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);
276
277         for(i = 1; i <= base_index; i++)
278         {
279                 buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
280         }
281         tdb_close(names);
282
283         /* Now terminate the MULTI_SZ with a double unicode NULL */
284         buf1 = *retbuf;
285         buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
286         if(!buf1) {
287                 buffer_size = 0;
288         } else {
289                 buf1[buffer_size++] = '\0';
290                 buf1[buffer_size++] = '\0';
291         }
292
293         *retbuf=buf1;
294
295         return buffer_size;
296 }
297
298 /*********************************************************************
299 *********************************************************************/
300
301 static void _reg_perfcount_make_key(TDB_DATA *key,
302                                     char *buf,
303                                     int buflen,
304                                     int key_part1,
305                                     const char *key_part2)
306 {
307         memset(buf, 0, buflen);
308         if(key_part2 != NULL)
309                 snprintf(buf, buflen,"%d%s", key_part1, key_part2);
310         else 
311                 snprintf(buf, buflen, "%d", key_part1);
312
313         *key = string_tdb_data(buf);
314
315         return;
316 }
317
318 /*********************************************************************
319 *********************************************************************/
320
321 static bool _reg_perfcount_isparent(TDB_DATA data)
322 {
323         if(data.dsize > 0)
324         {
325                 if(data.dptr[0] == 'p')
326                         return True;
327                 else
328                         return False;
329         }
330         return False;
331 }
332
333 /*********************************************************************
334 *********************************************************************/
335
336 static bool _reg_perfcount_ischild(TDB_DATA data)
337 {
338         if(data.dsize > 0)
339         {
340                 if(data.dptr[0] == 'c')
341                         return True;
342                 else
343                         return False;
344         }
345         return False;
346 }
347
348 /*********************************************************************
349 *********************************************************************/
350
351 static uint32 _reg_perfcount_get_numinst(int objInd, TDB_CONTEXT *names)
352 {
353         TDB_DATA key, data;
354         char buf[PERFCOUNT_MAX_LEN];
355
356         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, objInd, "inst");
357         data = tdb_fetch(names, key);
358
359         if(data.dptr == NULL)
360                 return (uint32)PERF_NO_INSTANCES;
361     
362         memset(buf, 0, PERFCOUNT_MAX_LEN);
363         memcpy(buf, data.dptr, data.dsize);
364         SAFE_FREE(data.dptr);
365         return (uint32)atoi(buf);
366 }
367
368 /*********************************************************************
369 *********************************************************************/
370
371 static bool _reg_perfcount_add_object(PERF_DATA_BLOCK *block,
372                                       prs_struct *ps,
373                                       int num,
374                                       TDB_DATA data,
375                                       TDB_CONTEXT *names)
376 {
377         int i;
378         bool success = True;
379         PERF_OBJECT_TYPE *obj;
380
381         block->objects = (PERF_OBJECT_TYPE *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
382                                                                   block->objects,
383                                                                   PERF_OBJECT_TYPE,
384                                                                   block->NumObjectTypes+1);
385         if(block->objects == NULL)
386                 return False;
387         obj = &(block->objects[block->NumObjectTypes]);
388         memset((void *)&(block->objects[block->NumObjectTypes]), 0, sizeof(PERF_OBJECT_TYPE));
389         block->objects[block->NumObjectTypes].ObjectNameTitleIndex = num;
390         block->objects[block->NumObjectTypes].ObjectNameTitlePointer = 0;
391         block->objects[block->NumObjectTypes].ObjectHelpTitleIndex = num+1;
392         block->objects[block->NumObjectTypes].ObjectHelpTitlePointer = 0;
393         block->objects[block->NumObjectTypes].NumCounters = 0;
394         block->objects[block->NumObjectTypes].DefaultCounter = 0;
395         block->objects[block->NumObjectTypes].NumInstances = _reg_perfcount_get_numinst(num, names);
396         block->objects[block->NumObjectTypes].counters = NULL;
397         block->objects[block->NumObjectTypes].instances = NULL;
398         block->objects[block->NumObjectTypes].counter_data.ByteLength = sizeof(uint32);
399         block->objects[block->NumObjectTypes].counter_data.data = NULL;
400         block->objects[block->NumObjectTypes].DetailLevel = PERF_DETAIL_NOVICE;
401         block->NumObjectTypes+=1;
402
403         for(i = 0; i < (int)obj->NumInstances; i++) {
404                 success = _reg_perfcount_add_instance(obj, ps, i, names);
405         }
406
407         return success;
408 }
409
410 /*********************************************************************
411 *********************************************************************/
412
413 bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
414 {
415         TDB_CONTEXT *counters;
416         const char *fname = counters_directory( DATA_DB );
417     
418         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
419
420         if(counters == NULL)
421         {
422                 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname));
423                 return False;
424         }    
425
426         *data = tdb_fetch(counters, key);
427     
428         tdb_close(counters);
429
430         return True;
431 }
432
433 /*********************************************************************
434 *********************************************************************/
435
436 static uint32 _reg_perfcount_get_size_field(uint32 CounterType)
437 {
438         uint32 retval;
439
440         retval = CounterType;
441
442         /* First mask out reserved lower 8 bits */
443         retval = retval & 0xFFFFFF00;
444         retval = retval << 22;
445         retval = retval >> 22;
446
447         return retval;
448 }
449
450 /*********************************************************************
451 *********************************************************************/
452
453 static uint32 _reg_perfcount_compute_scale(int64_t data)
454 {
455         int scale = 0;
456         if(data == 0)
457                 return scale;
458         while(data > 100)
459         {
460                 data /= 10;
461                 scale--;
462         }
463         while(data < 10)
464         {
465                 data *= 10;
466                 scale++;
467         }
468
469         return (uint32)scale;
470 }
471
472 /*********************************************************************
473 *********************************************************************/
474
475 static bool _reg_perfcount_get_counter_info(PERF_DATA_BLOCK *block,
476                                             prs_struct *ps,
477                                             int CounterIndex,
478                                             PERF_OBJECT_TYPE *obj,
479                                             TDB_CONTEXT *names)
480 {
481         TDB_DATA key, data;
482         char buf[PERFCOUNT_MAX_LEN];
483         size_t dsize, padding;
484         long int data32, dbuf[2];
485         int64_t data64;
486         uint32 counter_size;
487
488         obj->counters[obj->NumCounters].DefaultScale = 0;
489         dbuf[0] = dbuf[1] = 0;
490         padding = 0;
491
492         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "type");
493         data = tdb_fetch(names, key);
494         if(data.dptr == NULL)
495         {
496                 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex));
497                 return False;
498         }
499         memset(buf, 0, PERFCOUNT_MAX_LEN);
500         memcpy(buf, data.dptr, data.dsize);
501         obj->counters[obj->NumCounters].CounterType = atoi(buf);
502         DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
503                    obj->counters[obj->NumCounters].CounterType, CounterIndex));
504         SAFE_FREE(data.dptr);
505
506         /* Fetch the actual data */
507         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, CounterIndex, "");
508         _reg_perfcount_get_counter_data(key, &data);
509         if(data.dptr == NULL)
510         {
511                 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex));
512                 return False;
513         }
514     
515         counter_size = _reg_perfcount_get_size_field(obj->counters[obj->NumCounters].CounterType);
516
517         if(counter_size == PERF_SIZE_DWORD)
518         {
519                 dsize = sizeof(data32);
520                 memset(buf, 0, PERFCOUNT_MAX_LEN);
521                 memcpy(buf, data.dptr, data.dsize);
522                 data32 = strtol(buf, NULL, 0);
523                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
524                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale((int64_t)data32);
525                 else
526                         obj->counters[obj->NumCounters].DefaultScale = 0;
527                 dbuf[0] = data32;
528                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
529         }
530         else if(counter_size == PERF_SIZE_LARGE)
531         {
532                 dsize = sizeof(data64);
533                 memset(buf, 0, PERFCOUNT_MAX_LEN);
534                 memcpy(buf, data.dptr, data.dsize);
535                 data64 = atof(buf);
536                 if((obj->counters[obj->NumCounters].CounterType & 0x00000F00) == PERF_TYPE_NUMBER)
537                         obj->counters[obj->NumCounters].DefaultScale = _reg_perfcount_compute_scale(data64);
538                 else
539                         obj->counters[obj->NumCounters].DefaultScale = 0;
540                 memcpy((void *)dbuf, (const void *)&data64, dsize);
541                 padding = (dsize - (obj->counter_data.ByteLength%dsize)) % dsize;
542         }
543         else /* PERF_SIZE_VARIABLE_LEN */
544         {
545                 dsize = data.dsize;
546                 memset(buf, 0, PERFCOUNT_MAX_LEN);
547                 memcpy(buf, data.dptr, data.dsize);
548         }
549         SAFE_FREE(data.dptr);
550
551         obj->counter_data.ByteLength += dsize + padding;
552         obj->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
553                                                       obj->counter_data.data,
554                                                       uint8,
555                                                       obj->counter_data.ByteLength - sizeof(uint32));
556         if(obj->counter_data.data == NULL)
557                 return False;
558         if(dbuf[0] != 0 || dbuf[1] != 0)
559         {
560                 memcpy((void *)(obj->counter_data.data + 
561                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))), 
562                        (const void *)dbuf, dsize);
563         }
564         else
565         {
566                 /* Handling PERF_SIZE_VARIABLE_LEN */
567                 memcpy((void *)(obj->counter_data.data +
568                                 (obj->counter_data.ByteLength - (sizeof(uint32) + dsize))),
569                        (const void *)buf, dsize);
570         }
571         obj->counters[obj->NumCounters].CounterOffset = obj->counter_data.ByteLength - dsize;
572         if(obj->counters[obj->NumCounters].CounterOffset % dsize != 0)
573         {
574                 DEBUG(3,("Improperly aligned counter [%d]\n", obj->NumCounters));
575         }
576         obj->counters[obj->NumCounters].CounterSize = dsize;
577
578         return True;
579 }
580
581 /*********************************************************************
582 *********************************************************************/
583
584 PERF_OBJECT_TYPE *_reg_perfcount_find_obj(PERF_DATA_BLOCK *block, int objind)
585 {
586         int i;
587
588         PERF_OBJECT_TYPE *obj = NULL;
589
590         for(i = 0; i < block->NumObjectTypes; i++)
591         {
592                 if(block->objects[i].ObjectNameTitleIndex == objind)
593                 {
594                         obj = &(block->objects[i]);
595                 }
596         }
597
598         return obj;
599 }
600
601 /*********************************************************************
602 *********************************************************************/
603
604 static bool _reg_perfcount_add_counter(PERF_DATA_BLOCK *block,
605                                        prs_struct *ps,
606                                        int num,
607                                        TDB_DATA data,
608                                        TDB_CONTEXT *names)
609 {
610         char *begin, *end, *start, *stop;
611         int parent;
612         PERF_OBJECT_TYPE *obj;
613         bool success = True;
614         char buf[PERFCOUNT_MAX_LEN];
615     
616         obj = NULL;
617         memset(buf, 0, PERFCOUNT_MAX_LEN);
618         memcpy(buf, data.dptr, data.dsize);
619         begin = index(buf, '[');
620         end = index(buf, ']');
621         if(begin == NULL || end == NULL)
622                 return False;
623         start = begin+1;
624
625         while(start < end) {
626                 stop = index(start, ',');
627                 if(stop == NULL)
628                         stop = end;
629                 *stop = '\0';
630                 parent = atoi(start);
631
632                 obj = _reg_perfcount_find_obj(block, parent);
633                 if(obj == NULL) {
634                         /* At this point we require that the parent object exist.
635                            This can probably be handled better at some later time */
636                         DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
637                                   parent, num));
638                         return False;
639                 }
640                 obj->counters = (PERF_COUNTER_DEFINITION *)TALLOC_REALLOC_ARRAY(ps->mem_ctx,
641                                                                                 obj->counters,
642                                                                                 PERF_COUNTER_DEFINITION,
643                                                                                 obj->NumCounters+1);
644                 if(obj->counters == NULL)
645                         return False;
646                 memset((void *)&(obj->counters[obj->NumCounters]), 0, sizeof(PERF_COUNTER_DEFINITION));
647                 obj->counters[obj->NumCounters].CounterNameTitleIndex=num;
648                 obj->counters[obj->NumCounters].CounterHelpTitleIndex=num+1;
649                 obj->counters[obj->NumCounters].DetailLevel = PERF_DETAIL_NOVICE;
650                 obj->counters[obj->NumCounters].ByteLength = sizeof(PERF_COUNTER_DEFINITION);
651                 success = _reg_perfcount_get_counter_info(block, ps, num, obj, names);
652                 obj->NumCounters += 1;
653                 start = stop + 1;
654         }
655         
656         /* Handle case of Objects/Counters without any counter data, which would suggest
657            that the required instances are not there yet, so change NumInstances from
658            PERF_NO_INSTANCES to 0 */
659
660         return success;
661 }
662
663 /*********************************************************************
664 *********************************************************************/
665
666 bool _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION *inst,
667                                       prs_struct *ps,
668                                       int instId,
669                                       PERF_OBJECT_TYPE *obj,
670                                       TDB_CONTEXT *names)
671 {
672         TDB_DATA key, data;
673         char buf[PERFCOUNT_MAX_LEN], temp[PERFCOUNT_MAX_LEN];
674         smb_ucs2_t *name = NULL;
675         int pad;
676
677         /* First grab the instance data from the data file */
678         memset(temp, 0, PERFCOUNT_MAX_LEN);
679         snprintf(temp, PERFCOUNT_MAX_LEN, "i%d", instId);
680         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
681         if (!_reg_perfcount_get_counter_data(key, &data)) {
682                 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
683                 return false;
684         }
685         if(data.dptr == NULL)
686         {
687                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
688                           buf));
689                 return False;
690         }
691         inst->counter_data.ByteLength = data.dsize + sizeof(inst->counter_data.ByteLength);
692         inst->counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
693                                                        inst->counter_data.data,
694                                                        uint8,
695                                                        data.dsize);
696         if(inst->counter_data.data == NULL)
697                 return False;
698         memset(inst->counter_data.data, 0, data.dsize);
699         memcpy(inst->counter_data.data, data.dptr, data.dsize);
700         SAFE_FREE(data.dptr);
701
702         /* Fetch instance name */
703         memset(temp, 0, PERFCOUNT_MAX_LEN);
704         snprintf(temp, PERFCOUNT_MAX_LEN, "i%dname", instId);
705         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, obj->ObjectNameTitleIndex, temp);
706         data = tdb_fetch(names, key);
707         if(data.dptr == NULL)
708         {
709                 /* Not actually an error, but possibly unintended? -- just logging FYI */
710                 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
711                           buf));
712                 inst->NameLength = 0;
713         }
714         else
715         {
716                 memset(buf, 0, PERFCOUNT_MAX_LEN);
717                 memcpy(buf, data.dptr, MIN(PERFCOUNT_MAX_LEN-1,data.dsize));
718                 buf[PERFCOUNT_MAX_LEN-1] = '\0';
719                 inst->NameLength = rpcstr_push_talloc(ps->mem_ctx, &name, buf);
720                 if (inst->NameLength == (uint32_t)-1 || !name) {
721                         SAFE_FREE(data.dptr);
722                         return False;
723                 }
724                 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
725                                                   inst->data,
726                                                   uint8,
727                                                   inst->NameLength);
728                 if (inst->data == NULL) {
729                         SAFE_FREE(data.dptr);
730                         return False;
731                 }
732                 memcpy(inst->data, name, inst->NameLength);
733                 SAFE_FREE(data.dptr);
734         }
735
736         inst->ParentObjectTitleIndex = 0;
737         inst->ParentObjectTitlePointer = 0;
738         inst->UniqueID = PERF_NO_UNIQUE_ID;
739         inst->NameOffset = 6 * sizeof(uint32);
740     
741         inst->ByteLength = inst->NameOffset + inst->NameLength;
742         /* Need to be aligned on a 64-bit boundary here for counter_data */
743         if((pad = (inst->ByteLength % 8)))
744         {
745                 pad = 8 - pad;
746                 inst->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
747                                                   inst->data,
748                                                   uint8,
749                                                   inst->NameLength + pad);
750                 memset(inst->data + inst->NameLength, 0, pad);
751                 inst->ByteLength += pad;
752         }
753
754         return True;
755 }
756
757 /*********************************************************************
758 *********************************************************************/
759
760 bool _reg_perfcount_add_instance(PERF_OBJECT_TYPE *obj,
761                                  prs_struct *ps,
762                                  int instInd,
763                                  TDB_CONTEXT *names)
764 {
765         PERF_INSTANCE_DEFINITION *inst;
766
767         if(obj->instances == NULL) {
768                 obj->instances = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
769                                                       obj->instances,
770                                                       PERF_INSTANCE_DEFINITION,
771                                                       obj->NumInstances);
772         }
773         if(obj->instances == NULL)
774                 return False;
775     
776         memset(&(obj->instances[instInd]), 0, sizeof(PERF_INSTANCE_DEFINITION));
777         inst = &(obj->instances[instInd]);
778         return _reg_perfcount_get_instance_info(inst, ps, instInd, obj, names);
779 }
780
781 /*********************************************************************
782 *********************************************************************/
783
784 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK *block,
785                                           prs_struct *ps,
786                                           int base_index,
787                                           TDB_CONTEXT *names)
788 {
789         bool success;
790         int i, j, retval = 0;
791         char keybuf[PERFCOUNT_MAX_LEN];
792         TDB_DATA key, data;
793
794         for(i = 1; i <= base_index; i++)
795         {
796                 j = i*2;
797                 _reg_perfcount_make_key(&key, keybuf, PERFCOUNT_MAX_LEN, j, "rel");
798                 data = tdb_fetch(names, key);
799                 if(data.dptr != NULL)
800                 {
801                         if(_reg_perfcount_isparent(data))
802                                 success = _reg_perfcount_add_object(block, ps, j, data, names);
803                         else if(_reg_perfcount_ischild(data))
804                                 success = _reg_perfcount_add_counter(block, ps, j, data, names);
805                         else
806                         {
807                                 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data.dptr, j));
808                                 success = False;
809                         }
810                         if(success == False)
811                         {
812                                 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j));
813                                 retval = -1;
814                         }
815                         SAFE_FREE(data.dptr);
816                 }
817                 else
818                         DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j, keybuf));
819         }       
820         return retval;
821 }
822
823 /*********************************************************************
824 *********************************************************************/
825
826 static bool _reg_perfcount_get_64(uint64_t *retval,
827                                   TDB_CONTEXT *tdb,
828                                   int key_part1,
829                                   const char *key_part2)
830 {
831         TDB_DATA key, data;
832         char buf[PERFCOUNT_MAX_LEN];
833
834         _reg_perfcount_make_key(&key, buf, PERFCOUNT_MAX_LEN, key_part1, key_part2);
835
836         data = tdb_fetch(tdb, key);
837         if(data.dptr == NULL)
838         {
839                 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key.dptr));
840                 return False;
841         }
842
843         memset(buf, 0, PERFCOUNT_MAX_LEN);
844         memcpy(buf, data.dptr, data.dsize);
845         SAFE_FREE(data.dptr);
846
847         *retval = atof(buf);
848
849         return True;
850 }
851
852 /*********************************************************************
853 *********************************************************************/
854
855 static bool _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK *block,
856                                                 TDB_CONTEXT *names)
857 {
858         uint64_t PerfFreq, PerfTime, PerfTime100nSec;
859         TDB_CONTEXT *counters;
860         bool status = False;
861         const char *fname = counters_directory( DATA_DB );
862     
863         counters = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
864     
865         if(counters == NULL)
866         {
867                 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname));
868                 return False;
869         }    
870     
871         status = _reg_perfcount_get_64(&PerfFreq, names, 0, "PerfFreq");
872         if(status == False)
873         {
874                 tdb_close(counters);
875                 return status;
876         }
877         memcpy((void *)&(block->PerfFreq), (const void *)&PerfFreq, sizeof(PerfFreq));
878
879         status = _reg_perfcount_get_64(&PerfTime, counters, 0, "PerfTime");
880         if(status == False)
881         {
882                 tdb_close(counters);
883                 return status;
884         }
885         memcpy((void *)&(block->PerfTime), (const void *)&PerfTime, sizeof(PerfTime));
886
887         status = _reg_perfcount_get_64(&PerfTime100nSec, counters, 0, "PerfTime100nSec");
888         if(status == False)
889         {
890                 tdb_close(counters);
891                 return status;
892         }
893         memcpy((void *)&(block->PerfTime100nSec), (const void *)&PerfTime100nSec, sizeof(PerfTime100nSec));
894
895         tdb_close(counters);
896         return True;
897 }
898
899 /*********************************************************************
900 *********************************************************************/
901
902 static bool _reg_perfcount_init_data_block(PERF_DATA_BLOCK *block,
903                                            prs_struct *ps, TDB_CONTEXT *names)
904 {
905         smb_ucs2_t *temp = NULL;
906         time_t tm;
907
908         if (rpcstr_push_talloc(ps->mem_ctx, &temp, "PERF")==(size_t)-1) {
909                 return false;
910         }
911         if (!temp) {
912                 return false;
913         }
914         memcpy(block->Signature, temp, strlen_w(temp) *2);
915
916         if(ps->bigendian_data == RPC_BIG_ENDIAN)
917                 block->LittleEndian = 0;
918         else
919                 block->LittleEndian = 1;
920         block->Version = 1;
921         block->Revision = 1;
922         block->TotalByteLength = 0;
923         block->NumObjectTypes = 0;
924         block->DefaultObject = -1;
925         block->objects = NULL;
926         tm = time(NULL);
927         make_systemtime(&(block->SystemTime), gmtime(&tm));
928         _reg_perfcount_init_data_block_perf(block, names);
929         memset(temp, 0, sizeof(temp));
930         rpcstr_push((void *)temp, global_myname(), sizeof(temp), STR_TERMINATE);
931         block->SystemNameLength = (strlen_w(temp) * 2) + 2;
932         block->data = TALLOC_ZERO_ARRAY(ps->mem_ctx, uint8, block->SystemNameLength + (8 - (block->SystemNameLength % 8)));
933         if (block->data == NULL) {
934                 return False;
935         }
936         memcpy(block->data, temp, block->SystemNameLength);
937         block->SystemNameOffset = sizeof(PERF_DATA_BLOCK) - sizeof(block->objects) - sizeof(block->data); 
938         block->HeaderLength = block->SystemNameOffset + block->SystemNameLength;
939         /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
940            so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
941         block->HeaderLength += 8 - (block->HeaderLength % 8);
942
943         return True;
944 }
945
946 /*********************************************************************
947 *********************************************************************/
948
949 static uint32 _reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK *block, prs_struct *ps)
950 {
951         int obj, cnt, inst, pad, i;
952         PERF_OBJECT_TYPE *object;
953         PERF_INSTANCE_DEFINITION *instance;
954         PERF_COUNTER_DEFINITION *counter;
955         PERF_COUNTER_BLOCK *counter_data;
956         char *temp = NULL, *src_addr, *dst_addr;
957
958         block->TotalByteLength = 0;
959         object = block->objects;
960         for(obj = 0; obj < block->NumObjectTypes; obj++)
961         {
962                 object[obj].TotalByteLength = 0;
963                 object[obj].DefinitionLength = 0;
964                 instance = object[obj].instances;
965                 counter = object[obj].counters;
966                 for(cnt = 0; cnt < object[obj].NumCounters; cnt++)
967                 {
968                         object[obj].TotalByteLength += counter[cnt].ByteLength;
969                         object[obj].DefinitionLength += counter[cnt].ByteLength;
970                 }
971                 if(object[obj].NumInstances != PERF_NO_INSTANCES)
972                 {
973                         for(inst = 0; inst < object[obj].NumInstances; inst++)
974                         {
975                                 instance = &(object[obj].instances[inst]);
976                                 object[obj].TotalByteLength += instance->ByteLength;
977                                 counter_data = &(instance->counter_data);
978                                 counter = &(object[obj].counters[object[obj].NumCounters - 1]);
979                                 counter_data->ByteLength = counter->CounterOffset + counter->CounterSize + sizeof(counter_data->ByteLength);
980                                 temp = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
981                                                             temp, 
982                                                             char, 
983                                                             counter_data->ByteLength- sizeof(counter_data->ByteLength));
984                                 if (temp == NULL) {
985                                         return 0;
986                                 }
987                                 memset(temp, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength));
988                                 src_addr = (char *)counter_data->data;
989                                 for(i = 0; i < object[obj].NumCounters; i++)
990                                 {
991                                         counter = &(object[obj].counters[i]);
992                                         dst_addr = temp + counter->CounterOffset - sizeof(counter_data->ByteLength);
993                                         memcpy(dst_addr, src_addr, counter->CounterSize);
994                                         src_addr += counter->CounterSize;
995                                 }
996                                 /* Make sure to be 64-bit aligned */
997                                 if((pad = (counter_data->ByteLength % 8)))
998                                 {
999                                         pad = 8 - pad;
1000                                 }
1001                                 counter_data->data = TALLOC_REALLOC_ARRAY(ps->mem_ctx,
1002                                                                          counter_data->data,
1003                                                                          uint8,
1004                                                                          counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1005                                 if (counter_data->data == NULL) {
1006                                         return 0;
1007                                 }
1008                                 memset(counter_data->data, 0, counter_data->ByteLength - sizeof(counter_data->ByteLength) + pad);
1009                                 memcpy(counter_data->data, temp, counter_data->ByteLength - sizeof(counter_data->ByteLength));
1010                                 counter_data->ByteLength += pad;
1011                                 object[obj].TotalByteLength += counter_data->ByteLength;
1012                         }
1013                 }
1014                 else
1015                 {
1016                         /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1017                            so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1018                         if((pad = (object[obj].counter_data.ByteLength % 8)))
1019                         {
1020                                 pad = 8 - pad;
1021                                 object[obj].counter_data.data = TALLOC_REALLOC_ARRAY(ps->mem_ctx, 
1022                                                                                      object[obj].counter_data.data,
1023                                                                                      uint8, 
1024                                                                                      object[obj].counter_data.ByteLength + pad);
1025                                 memset((void *)(object[obj].counter_data.data + object[obj].counter_data.ByteLength), 0, pad);
1026                                 object[obj].counter_data.ByteLength += pad;
1027                         }
1028                         object[obj].TotalByteLength += object[obj].counter_data.ByteLength;
1029                 }
1030                 object[obj].HeaderLength = sizeof(*object) - (sizeof(counter) + sizeof(instance) + sizeof(PERF_COUNTER_BLOCK));
1031                 object[obj].TotalByteLength += object[obj].HeaderLength;
1032                 object[obj].DefinitionLength += object[obj].HeaderLength;
1033                 
1034                 block->TotalByteLength += object[obj].TotalByteLength;
1035         }
1036
1037         return block->TotalByteLength;
1038 }
1039
1040 /*********************************************************************
1041 *********************************************************************/
1042
1043 uint32 reg_perfcount_get_perf_data_block(uint32 base_index, 
1044                                          prs_struct *ps, 
1045                                          PERF_DATA_BLOCK *block,
1046                                          const char *object_ids)
1047 {
1048         uint32 buffer_size = 0;
1049         const char *fname = counters_directory( NAMES_DB );
1050         TDB_CONTEXT *names;
1051         int retval = 0;
1052         
1053         names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);
1054
1055         if(names == NULL)
1056         {
1057                 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname));
1058                 return 0;
1059         }
1060
1061         if (!_reg_perfcount_init_data_block(block, ps, names)) {
1062                 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1063                 tdb_close(names);
1064                 return 0;
1065         }
1066
1067         reg_perfcount_get_last_counter(base_index);
1068     
1069         if(object_ids == NULL)
1070         {
1071                 /* we're getting a request for "Global" here */
1072                 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1073         }
1074         else
1075         {
1076                 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1077                 retval = _reg_perfcount_assemble_global(block, ps, base_index, names);
1078         }
1079         buffer_size = _reg_perfcount_perf_data_block_fixup(block, ps);
1080
1081         tdb_close(names);
1082
1083         if (retval == -1) {
1084                 return 0;
1085         }
1086
1087         return buffer_size + block->HeaderLength;
1088 }
1089
1090 /*********************************************************************
1091 *********************************************************************/
1092
1093 static bool _reg_perfcount_marshall_perf_data_block(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1094 {
1095         int i;
1096         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_data_block");
1097         depth++;
1098
1099         if(!prs_align(ps))
1100                 return False;
1101         for(i = 0; i < 4; i++)
1102         {
1103                 if(!prs_uint16("Signature", ps, depth, &block.Signature[i]))
1104                         return False;
1105         }
1106         if(!prs_uint32("Little Endian", ps, depth, &block.LittleEndian))
1107                 return False;
1108         if(!prs_uint32("Version", ps, depth, &block.Version))
1109                 return False;
1110         if(!prs_uint32("Revision", ps, depth, &block.Revision))
1111                 return False;
1112         if(!prs_uint32("TotalByteLength", ps, depth, &block.TotalByteLength))
1113                 return False;
1114         if(!prs_uint32("HeaderLength", ps, depth, &block.HeaderLength))
1115                 return False;
1116         if(!prs_uint32("NumObjectTypes", ps, depth, &block.NumObjectTypes))
1117                 return False;
1118         if(!prs_uint32("DefaultObject", ps, depth, &block.DefaultObject))
1119                 return False;
1120         if(!spoolss_io_system_time("SystemTime", ps, depth, &block.SystemTime))
1121                 return False;
1122         if(!prs_uint32("Padding", ps, depth, &block.Padding))
1123                 return False;
1124         if(!prs_align_uint64(ps))
1125                 return False;
1126         if(!prs_uint64("PerfTime", ps, depth, &block.PerfTime))
1127                 return False;
1128         if(!prs_uint64("PerfFreq", ps, depth, &block.PerfFreq))
1129                 return False;
1130         if(!prs_uint64("PerfTime100nSec", ps, depth, &block.PerfTime100nSec))
1131                 return False;
1132         if(!prs_uint32("SystemNameLength", ps, depth, &block.SystemNameLength))
1133                 return False;
1134         if(!prs_uint32("SystemNameOffset", ps, depth, &block.SystemNameOffset))
1135                 return False;
1136         /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1137         if(!prs_uint8s(False, "SystemName", ps, depth, block.data, 
1138                        block.HeaderLength - block.SystemNameOffset)) 
1139                 return False;
1140
1141         return True;
1142 }
1143
1144 /*********************************************************************
1145 *********************************************************************/
1146
1147 static bool _reg_perfcount_marshall_perf_counters(prs_struct *ps,
1148                                                   PERF_OBJECT_TYPE object,
1149                                                   int depth)
1150 {
1151         int cnt;
1152         PERF_COUNTER_DEFINITION counter;
1153
1154         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counters");
1155         depth++;
1156     
1157         for(cnt = 0; cnt < object.NumCounters; cnt++)
1158         {
1159                 counter = object.counters[cnt];
1160
1161                 if(!prs_align(ps))
1162                         return False;
1163                 if(!prs_uint32("ByteLength", ps, depth, &counter.ByteLength))
1164                         return False;
1165                 if(!prs_uint32("CounterNameTitleIndex", ps, depth, &counter.CounterNameTitleIndex))
1166                         return False;
1167                 if(!prs_uint32("CounterNameTitlePointer", ps, depth, &counter.CounterNameTitlePointer))
1168                         return False;
1169                 if(!prs_uint32("CounterHelpTitleIndex", ps, depth, &counter.CounterHelpTitleIndex))
1170                         return False;
1171                 if(!prs_uint32("CounterHelpTitlePointer", ps, depth, &counter.CounterHelpTitlePointer))
1172                         return False;
1173                 if(!prs_uint32("DefaultScale", ps, depth, &counter.DefaultScale))
1174                         return False;
1175                 if(!prs_uint32("DetailLevel", ps, depth, &counter.DetailLevel))
1176                         return False;
1177                 if(!prs_uint32("CounterType", ps, depth, &counter.CounterType))
1178                         return False;
1179                 if(!prs_uint32("CounterSize", ps, depth, &counter.CounterSize))
1180                         return False;
1181                 if(!prs_uint32("CounterOffset", ps, depth, &counter.CounterOffset))
1182                         return False;
1183         }
1184
1185         return True;
1186 }
1187
1188 /*********************************************************************
1189 *********************************************************************/
1190
1191 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct *ps, 
1192                                                       PERF_COUNTER_BLOCK counter_data, 
1193                                                       int depth)
1194 {
1195         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_counter_data");
1196         depth++;
1197     
1198         if(!prs_align_uint64(ps))
1199                 return False;
1200     
1201         if(!prs_uint32("ByteLength", ps, depth, &counter_data.ByteLength))
1202                 return False;
1203         if(!prs_uint8s(False, "CounterData", ps, depth, counter_data.data, counter_data.ByteLength - sizeof(uint32)))
1204                 return False;
1205         if(!prs_align_uint64(ps))
1206                 return False;
1207
1208         return True;
1209 }
1210
1211 /*********************************************************************
1212 *********************************************************************/
1213
1214 static bool _reg_perfcount_marshall_perf_instances(prs_struct *ps,
1215                                                    PERF_OBJECT_TYPE object, 
1216                                                    int depth)
1217 {
1218         PERF_INSTANCE_DEFINITION instance;
1219         int inst;
1220
1221         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_instances");
1222         depth++;
1223
1224         for(inst = 0; inst < object.NumInstances; inst++)
1225         {
1226                 instance = object.instances[inst];
1227
1228                 if(!prs_align(ps))
1229                         return False;
1230                 if(!prs_uint32("ByteLength", ps, depth, &instance.ByteLength))
1231                         return False;
1232                 if(!prs_uint32("ParentObjectTitleIndex", ps, depth, &instance.ParentObjectTitleIndex))
1233                         return False;
1234                 if(!prs_uint32("ParentObjectTitlePointer", ps, depth, &instance.ParentObjectTitlePointer))
1235                         return False;
1236                 if(!prs_uint32("UniqueID", ps, depth, &instance.UniqueID))
1237                         return False;
1238                 if(!prs_uint32("NameOffset", ps, depth, &instance.NameOffset))
1239                         return False;
1240                 if(!prs_uint32("NameLength", ps, depth, &instance.NameLength))
1241                         return False;
1242                 if(!prs_uint8s(False, "InstanceName", ps, depth, instance.data,
1243                                instance.ByteLength - instance.NameOffset))
1244                         return False;
1245                 if(_reg_perfcount_marshall_perf_counter_data(ps, instance.counter_data, depth) == False)
1246                         return False;
1247         }
1248         
1249         return True;
1250 }
1251
1252 /*********************************************************************
1253 *********************************************************************/
1254
1255 static bool _reg_perfcount_marshall_perf_objects(prs_struct *ps, PERF_DATA_BLOCK block, int depth)
1256 {
1257         int obj;
1258
1259         PERF_OBJECT_TYPE object;
1260     
1261         prs_debug(ps, depth, "", "_reg_perfcount_marshall_perf_objects");
1262         depth++;
1263
1264         for(obj = 0; obj < block.NumObjectTypes; obj++)
1265         {
1266                 object = block.objects[obj];
1267
1268                 if(!prs_align(ps))
1269                         return False;
1270
1271                 if(!prs_uint32("TotalByteLength", ps, depth, &object.TotalByteLength))
1272                         return False;
1273                 if(!prs_uint32("DefinitionLength", ps, depth, &object.DefinitionLength))
1274                         return False;
1275                 if(!prs_uint32("HeaderLength", ps, depth, &object.HeaderLength))
1276                         return False;
1277                 if(!prs_uint32("ObjectNameTitleIndex", ps, depth, &object.ObjectNameTitleIndex))
1278                         return False;
1279                 if(!prs_uint32("ObjectNameTitlePointer", ps, depth, &object.ObjectNameTitlePointer))
1280                         return False;
1281                 if(!prs_uint32("ObjectHelpTitleIndex", ps, depth, &object.ObjectHelpTitleIndex))
1282                         return False;
1283                 if(!prs_uint32("ObjectHelpTitlePointer", ps, depth, &object.ObjectHelpTitlePointer))
1284                         return False;
1285                 if(!prs_uint32("DetailLevel", ps, depth, &object.DetailLevel))
1286                         return False;
1287                 if(!prs_uint32("NumCounters", ps, depth, &object.NumCounters))
1288                         return False;
1289                 if(!prs_uint32("DefaultCounter", ps, depth, &object.DefaultCounter))
1290                         return False;
1291                 if(!prs_uint32("NumInstances", ps, depth, &object.NumInstances))
1292                         return False;
1293                 if(!prs_uint32("CodePage", ps, depth, &object.CodePage))
1294                         return False;
1295                 if(!prs_align_uint64(ps))
1296                         return False;
1297                 if(!prs_uint64("PerfTime", ps, depth, &object.PerfTime))
1298                         return False;
1299                 if(!prs_uint64("PerfFreq", ps, depth, &object.PerfFreq))
1300                         return False;
1301
1302                 /* Now do the counters */
1303                 /* If no instances, encode counter_data */
1304                 /* If instances, encode instace plus counter data for each instance */
1305                 if(_reg_perfcount_marshall_perf_counters(ps, object, depth) == False)
1306                         return False;
1307                 if(object.NumInstances == PERF_NO_INSTANCES)
1308                 {
1309                         if(_reg_perfcount_marshall_perf_counter_data(ps, object.counter_data, depth) == False)
1310                                 return False;
1311                 }
1312                 else
1313                 {
1314                         if(_reg_perfcount_marshall_perf_instances(ps, object, depth) == False)
1315                                 return False;
1316                 }
1317         }
1318
1319         return True;
1320 }
1321
1322 /*********************************************************************
1323 *********************************************************************/
1324
1325 static bool _reg_perfcount_marshall_hkpd(prs_struct *ps, PERF_DATA_BLOCK block)
1326 {
1327         int depth = 0;
1328         if(_reg_perfcount_marshall_perf_data_block(ps, block, depth) == True)
1329         {
1330                 if(_reg_perfcount_marshall_perf_objects(ps, block, depth) == True)
1331                         return True;
1332         }
1333         return False;
1334 }
1335
1336 /*********************************************************************
1337 *********************************************************************/
1338
1339 WERROR reg_perfcount_get_hkpd(prs_struct *ps, uint32 max_buf_size, uint32 *outbuf_len, const char *object_ids)
1340 {
1341         /*
1342          * For a detailed description of the layout of this structure,
1343          * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1344          *
1345          * By 2006-11-23 this link did not work anymore, I found something
1346          * promising under
1347          * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1348          */
1349         PERF_DATA_BLOCK block;
1350         uint32 buffer_size, base_index; 
1351     
1352         buffer_size = 0;
1353         base_index = reg_perfcount_get_base_index();
1354         ZERO_STRUCT(block);
1355
1356         buffer_size = reg_perfcount_get_perf_data_block(base_index, ps, &block, object_ids);
1357
1358         if(buffer_size < max_buf_size)
1359         {
1360                 *outbuf_len = buffer_size;
1361                 if(_reg_perfcount_marshall_hkpd(ps, block) == True)
1362                         return WERR_OK;
1363                 else
1364                         return WERR_NOMEM;
1365         }
1366         else
1367         {
1368                 *outbuf_len = max_buf_size;
1369                 _reg_perfcount_marshall_perf_data_block(ps, block, 0);
1370                 return WERR_INSUFFICIENT_BUFFER;
1371         }
1372 }