2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
5 * Copyright (C) Marcin Krzysztof Porwit 2005,
6 * Copyright (C) Gerald (Jerry) Carter 2005.
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.
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.
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/>.
25 #define DBGC_CLASS DBGC_REGISTRY
27 #define PERFCOUNT_MAX_LEN 256
29 #define PERFCOUNTDIR "perfmon"
30 #define NAMES_DB "names.tdb"
31 #define DATA_DB "data.tdb"
33 PERF_OBJECT_TYPE
*_reg_perfcount_find_obj(PERF_DATA_BLOCK
*block
, int objind
);
35 /*********************************************************************
36 *********************************************************************/
38 static char *counters_directory(const char *dbname
)
42 TALLOC_CTX
*ctx
= talloc_tos();
47 path
= talloc_asprintf(ctx
, "%s/%s", PERFCOUNTDIR
, dbname
);
52 ret
= talloc_strdup(ctx
, state_path(path
));
57 /*********************************************************************
58 *********************************************************************/
60 void perfcount_init_keys( void )
62 char *p
= state_path(PERFCOUNTDIR
);
64 /* no registry keys; just create the perfmon directory */
66 if ( !directory_exist( p
, NULL
) )
72 /*********************************************************************
73 *********************************************************************/
75 uint32
reg_perfcount_get_base_index(void)
77 const char *fname
= counters_directory( NAMES_DB
);
82 char buf
[PERFCOUNT_MAX_LEN
];
84 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
87 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname
));
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
93 The format for the counter_names.tdb file is:
100 even_num perf_counter<even_num>
101 even_num+1 perf_counter<even_num>_help
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
)
108 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname
));
115 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
116 memcpy(buf
, dbuf
.dptr
, dbuf
.dsize
);
117 retval
= (uint32
)atoi(buf
);
118 SAFE_FREE(dbuf
.dptr
);
124 /*********************************************************************
125 *********************************************************************/
127 uint32
reg_perfcount_get_last_counter(uint32 base_index
)
134 retval
= base_index
* 2;
139 /*********************************************************************
140 *********************************************************************/
142 uint32
reg_perfcount_get_last_help(uint32 last_counter
)
146 if(last_counter
== 0)
149 retval
= last_counter
+ 1;
155 /*********************************************************************
156 *********************************************************************/
158 static uint32
_reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT
*tdb
,
165 char *buf1
= *retbuf
;
166 uint32 working_size
= 0;
167 UNISTR2 name_index
, name
;
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
)
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
)));
181 /* First encode the name_index */
182 working_size
= (kbuf
.dsize
+ 1)*sizeof(uint16
);
183 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ working_size
);
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
);
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
;
210 /*********************************************************************
211 *********************************************************************/
213 uint32
reg_perfcount_get_counter_help(uint32 base_index
, char **retbuf
)
216 uint32 buffer_size
= 0;
218 const char *fname
= counters_directory( NAMES_DB
);
224 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
228 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname
));
232 for(i
= 1; i
<= base_index
; i
++)
234 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, (i
*2)+1, retbuf
, buffer_size
);
238 /* Now terminate the MULTI_SZ with a double unicode NULL */
240 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ 2);
244 buf1
[buffer_size
++] = '\0';
245 buf1
[buffer_size
++] = '\0';
253 /*********************************************************************
254 *********************************************************************/
256 uint32
reg_perfcount_get_counter_names(uint32 base_index
, char **retbuf
)
259 uint32 buffer_size
= 0;
261 const char *fname
= counters_directory( NAMES_DB
);
267 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
271 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname
));
275 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, 1, retbuf
, buffer_size
);
277 for(i
= 1; i
<= base_index
; i
++)
279 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, i
*2, retbuf
, buffer_size
);
283 /* Now terminate the MULTI_SZ with a double unicode NULL */
285 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ 2);
289 buf1
[buffer_size
++] = '\0';
290 buf1
[buffer_size
++] = '\0';
298 /*********************************************************************
299 *********************************************************************/
301 static void _reg_perfcount_make_key(TDB_DATA
*key
,
305 const char *key_part2
)
307 memset(buf
, 0, buflen
);
308 if(key_part2
!= NULL
)
309 snprintf(buf
, buflen
,"%d%s", key_part1
, key_part2
);
311 snprintf(buf
, buflen
, "%d", key_part1
);
313 *key
= string_tdb_data(buf
);
318 /*********************************************************************
319 *********************************************************************/
321 static bool _reg_perfcount_isparent(TDB_DATA data
)
325 if(data
.dptr
[0] == 'p')
333 /*********************************************************************
334 *********************************************************************/
336 static bool _reg_perfcount_ischild(TDB_DATA data
)
340 if(data
.dptr
[0] == 'c')
348 /*********************************************************************
349 *********************************************************************/
351 static uint32
_reg_perfcount_get_numinst(int objInd
, TDB_CONTEXT
*names
)
354 char buf
[PERFCOUNT_MAX_LEN
];
356 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, objInd
, "inst");
357 data
= tdb_fetch(names
, key
);
359 if(data
.dptr
== NULL
)
360 return (uint32
)PERF_NO_INSTANCES
;
362 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
363 memcpy(buf
, data
.dptr
, data
.dsize
);
364 SAFE_FREE(data
.dptr
);
365 return (uint32
)atoi(buf
);
368 /*********************************************************************
369 *********************************************************************/
371 static bool _reg_perfcount_add_object(PERF_DATA_BLOCK
*block
,
379 PERF_OBJECT_TYPE
*obj
;
381 block
->objects
= (PERF_OBJECT_TYPE
*)TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
384 block
->NumObjectTypes
+1);
385 if(block
->objects
== NULL
)
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;
403 for(i
= 0; i
< (int)obj
->NumInstances
; i
++) {
404 success
= _reg_perfcount_add_instance(obj
, ps
, i
, names
);
410 /*********************************************************************
411 *********************************************************************/
413 bool _reg_perfcount_get_counter_data(TDB_DATA key
, TDB_DATA
*data
)
415 TDB_CONTEXT
*counters
;
416 const char *fname
= counters_directory( DATA_DB
);
418 counters
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
422 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname
));
426 *data
= tdb_fetch(counters
, key
);
433 /*********************************************************************
434 *********************************************************************/
436 static uint32
_reg_perfcount_get_size_field(uint32 CounterType
)
440 retval
= CounterType
;
442 /* First mask out reserved lower 8 bits */
443 retval
= retval
& 0xFFFFFF00;
444 retval
= retval
<< 22;
445 retval
= retval
>> 22;
450 /*********************************************************************
451 *********************************************************************/
453 static uint32
_reg_perfcount_compute_scale(SMB_BIG_INT data
)
469 return (uint32
)scale
;
472 /*********************************************************************
473 *********************************************************************/
475 static bool _reg_perfcount_get_counter_info(PERF_DATA_BLOCK
*block
,
478 PERF_OBJECT_TYPE
*obj
,
482 char buf
[PERFCOUNT_MAX_LEN
];
483 size_t dsize
, padding
;
484 long int data32
, dbuf
[2];
488 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
489 dbuf
[0] = dbuf
[1] = 0;
492 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, CounterIndex
, "type");
493 data
= tdb_fetch(names
, key
);
494 if(data
.dptr
== NULL
)
496 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex
));
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
);
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
)
511 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex
));
515 counter_size
= _reg_perfcount_get_size_field(obj
->counters
[obj
->NumCounters
].CounterType
);
517 if(counter_size
== PERF_SIZE_DWORD
)
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((SMB_BIG_INT
)data32
);
526 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
528 padding
= (dsize
- (obj
->counter_data
.ByteLength
%dsize
)) % dsize
;
530 else if(counter_size
== PERF_SIZE_LARGE
)
532 dsize
= sizeof(data64
);
533 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
534 memcpy(buf
, data
.dptr
, data
.dsize
);
536 if((obj
->counters
[obj
->NumCounters
].CounterType
& 0x00000F00) == PERF_TYPE_NUMBER
)
537 obj
->counters
[obj
->NumCounters
].DefaultScale
= _reg_perfcount_compute_scale(data64
);
539 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
540 memcpy((void *)dbuf
, (const void *)&data64
, dsize
);
541 padding
= (dsize
- (obj
->counter_data
.ByteLength
%dsize
)) % dsize
;
543 else /* PERF_SIZE_VARIABLE_LEN */
546 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
547 memcpy(buf
, data
.dptr
, data
.dsize
);
549 SAFE_FREE(data
.dptr
);
551 obj
->counter_data
.ByteLength
+= dsize
+ padding
;
552 obj
->counter_data
.data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
553 obj
->counter_data
.data
,
555 obj
->counter_data
.ByteLength
- sizeof(uint32
));
556 if(obj
->counter_data
.data
== NULL
)
558 if(dbuf
[0] != 0 || dbuf
[1] != 0)
560 memcpy((void *)(obj
->counter_data
.data
+
561 (obj
->counter_data
.ByteLength
- (sizeof(uint32
) + dsize
))),
562 (const void *)dbuf
, dsize
);
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
);
571 obj
->counters
[obj
->NumCounters
].CounterOffset
= obj
->counter_data
.ByteLength
- dsize
;
572 if(obj
->counters
[obj
->NumCounters
].CounterOffset
% dsize
!= 0)
574 DEBUG(3,("Improperly aligned counter [%d]\n", obj
->NumCounters
));
576 obj
->counters
[obj
->NumCounters
].CounterSize
= dsize
;
581 /*********************************************************************
582 *********************************************************************/
584 PERF_OBJECT_TYPE
*_reg_perfcount_find_obj(PERF_DATA_BLOCK
*block
, int objind
)
588 PERF_OBJECT_TYPE
*obj
= NULL
;
590 for(i
= 0; i
< block
->NumObjectTypes
; i
++)
592 if(block
->objects
[i
].ObjectNameTitleIndex
== objind
)
594 obj
= &(block
->objects
[i
]);
601 /*********************************************************************
602 *********************************************************************/
604 static bool _reg_perfcount_add_counter(PERF_DATA_BLOCK
*block
,
610 char *begin
, *end
, *start
, *stop
;
612 PERF_OBJECT_TYPE
*obj
;
614 char buf
[PERFCOUNT_MAX_LEN
];
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
)
626 stop
= index(start
, ',');
630 parent
= atoi(start
);
632 obj
= _reg_perfcount_find_obj(block
, parent
);
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",
640 obj
->counters
= (PERF_COUNTER_DEFINITION
*)TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
642 PERF_COUNTER_DEFINITION
,
644 if(obj
->counters
== NULL
)
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;
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 */
663 /*********************************************************************
664 *********************************************************************/
666 bool _reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION
*inst
,
669 PERF_OBJECT_TYPE
*obj
,
673 char buf
[PERFCOUNT_MAX_LEN
], temp
[PERFCOUNT_MAX_LEN
];
674 smb_ucs2_t
*name
= NULL
;
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 _reg_perfcount_get_counter_data(key
, &data
);
682 if(data
.dptr
== NULL
)
684 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
688 inst
->counter_data
.ByteLength
= data
.dsize
+ sizeof(inst
->counter_data
.ByteLength
);
689 inst
->counter_data
.data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
690 inst
->counter_data
.data
,
693 if(inst
->counter_data
.data
== NULL
)
695 memset(inst
->counter_data
.data
, 0, data
.dsize
);
696 memcpy(inst
->counter_data
.data
, data
.dptr
, data
.dsize
);
697 SAFE_FREE(data
.dptr
);
699 /* Fetch instance name */
700 memset(temp
, 0, PERFCOUNT_MAX_LEN
);
701 snprintf(temp
, PERFCOUNT_MAX_LEN
, "i%dname", instId
);
702 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, obj
->ObjectNameTitleIndex
, temp
);
703 data
= tdb_fetch(names
, key
);
704 if(data
.dptr
== NULL
)
706 /* Not actually an error, but possibly unintended? -- just logging FYI */
707 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
709 inst
->NameLength
= 0;
713 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
714 memcpy(buf
, data
.dptr
, MIN(PERFCOUNT_MAX_LEN
-1,data
.dsize
));
715 buf
[PERFCOUNT_MAX_LEN
-1] = '\0';
716 inst
->NameLength
= rpcstr_push_talloc(ps
->mem_ctx
, &name
, buf
);
717 if (inst
->NameLength
== (uint32_t)-1 || !name
) {
718 SAFE_FREE(data
.dptr
);
721 inst
->data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
725 if (inst
->data
== NULL
) {
726 SAFE_FREE(data
.dptr
);
729 memcpy(inst
->data
, name
, inst
->NameLength
);
730 SAFE_FREE(data
.dptr
);
733 inst
->ParentObjectTitleIndex
= 0;
734 inst
->ParentObjectTitlePointer
= 0;
735 inst
->UniqueID
= PERF_NO_UNIQUE_ID
;
736 inst
->NameOffset
= 6 * sizeof(uint32
);
738 inst
->ByteLength
= inst
->NameOffset
+ inst
->NameLength
;
739 /* Need to be aligned on a 64-bit boundary here for counter_data */
740 if((pad
= (inst
->ByteLength
% 8)))
743 inst
->data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
746 inst
->NameLength
+ pad
);
747 memset(inst
->data
+ inst
->NameLength
, 0, pad
);
748 inst
->ByteLength
+= pad
;
754 /*********************************************************************
755 *********************************************************************/
757 bool _reg_perfcount_add_instance(PERF_OBJECT_TYPE
*obj
,
762 PERF_INSTANCE_DEFINITION
*inst
;
764 if(obj
->instances
== NULL
) {
765 obj
->instances
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
767 PERF_INSTANCE_DEFINITION
,
770 if(obj
->instances
== NULL
)
773 memset(&(obj
->instances
[instInd
]), 0, sizeof(PERF_INSTANCE_DEFINITION
));
774 inst
= &(obj
->instances
[instInd
]);
775 return _reg_perfcount_get_instance_info(inst
, ps
, instInd
, obj
, names
);
778 /*********************************************************************
779 *********************************************************************/
781 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK
*block
,
787 int i
, j
, retval
= 0;
788 char keybuf
[PERFCOUNT_MAX_LEN
];
791 for(i
= 1; i
<= base_index
; i
++)
794 _reg_perfcount_make_key(&key
, keybuf
, PERFCOUNT_MAX_LEN
, j
, "rel");
795 data
= tdb_fetch(names
, key
);
796 if(data
.dptr
!= NULL
)
798 if(_reg_perfcount_isparent(data
))
799 success
= _reg_perfcount_add_object(block
, ps
, j
, data
, names
);
800 else if(_reg_perfcount_ischild(data
))
801 success
= _reg_perfcount_add_counter(block
, ps
, j
, data
, names
);
804 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data
.dptr
, j
));
809 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j
));
812 SAFE_FREE(data
.dptr
);
815 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j
, keybuf
));
820 /*********************************************************************
821 *********************************************************************/
823 static bool _reg_perfcount_get_64(SMB_BIG_UINT
*retval
,
826 const char *key_part2
)
829 char buf
[PERFCOUNT_MAX_LEN
];
831 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, key_part1
, key_part2
);
833 data
= tdb_fetch(tdb
, key
);
834 if(data
.dptr
== NULL
)
836 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key
.dptr
));
840 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
841 memcpy(buf
, data
.dptr
, data
.dsize
);
842 SAFE_FREE(data
.dptr
);
849 /*********************************************************************
850 *********************************************************************/
852 static bool _reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK
*block
,
855 SMB_BIG_UINT PerfFreq
, PerfTime
, PerfTime100nSec
;
856 TDB_CONTEXT
*counters
;
858 const char *fname
= counters_directory( DATA_DB
);
860 counters
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
864 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname
));
868 status
= _reg_perfcount_get_64(&PerfFreq
, names
, 0, "PerfFreq");
874 memcpy((void *)&(block
->PerfFreq
), (const void *)&PerfFreq
, sizeof(PerfFreq
));
876 status
= _reg_perfcount_get_64(&PerfTime
, counters
, 0, "PerfTime");
882 memcpy((void *)&(block
->PerfTime
), (const void *)&PerfTime
, sizeof(PerfTime
));
884 status
= _reg_perfcount_get_64(&PerfTime100nSec
, counters
, 0, "PerfTime100nSec");
890 memcpy((void *)&(block
->PerfTime100nSec
), (const void *)&PerfTime100nSec
, sizeof(PerfTime100nSec
));
896 /*********************************************************************
897 *********************************************************************/
899 static bool _reg_perfcount_init_data_block(PERF_DATA_BLOCK
*block
,
900 prs_struct
*ps
, TDB_CONTEXT
*names
)
902 smb_ucs2_t
*temp
= NULL
;
905 if (rpcstr_push_talloc(ps
->mem_ctx
, &temp
, "PERF")==(size_t)-1) {
911 memcpy(block
->Signature
, temp
, strlen_w(temp
) *2);
913 if(ps
->bigendian_data
== RPC_BIG_ENDIAN
)
914 block
->LittleEndian
= 0;
916 block
->LittleEndian
= 1;
919 block
->TotalByteLength
= 0;
920 block
->NumObjectTypes
= 0;
921 block
->DefaultObject
= -1;
922 block
->objects
= NULL
;
924 make_systemtime(&(block
->SystemTime
), gmtime(&tm
));
925 _reg_perfcount_init_data_block_perf(block
, names
);
926 memset(temp
, 0, sizeof(temp
));
927 rpcstr_push((void *)temp
, global_myname(), sizeof(temp
), STR_TERMINATE
);
928 block
->SystemNameLength
= (strlen_w(temp
) * 2) + 2;
929 block
->data
= TALLOC_ZERO_ARRAY(ps
->mem_ctx
, uint8
, block
->SystemNameLength
+ (8 - (block
->SystemNameLength
% 8)));
930 if (block
->data
== NULL
) {
933 memcpy(block
->data
, temp
, block
->SystemNameLength
);
934 block
->SystemNameOffset
= sizeof(PERF_DATA_BLOCK
) - sizeof(block
->objects
) - sizeof(block
->data
);
935 block
->HeaderLength
= block
->SystemNameOffset
+ block
->SystemNameLength
;
936 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
937 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
938 block
->HeaderLength
+= 8 - (block
->HeaderLength
% 8);
943 /*********************************************************************
944 *********************************************************************/
946 static uint32
_reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK
*block
, prs_struct
*ps
)
948 int obj
, cnt
, inst
, pad
, i
;
949 PERF_OBJECT_TYPE
*object
;
950 PERF_INSTANCE_DEFINITION
*instance
;
951 PERF_COUNTER_DEFINITION
*counter
;
952 PERF_COUNTER_BLOCK
*counter_data
;
953 char *temp
= NULL
, *src_addr
, *dst_addr
;
955 block
->TotalByteLength
= 0;
956 object
= block
->objects
;
957 for(obj
= 0; obj
< block
->NumObjectTypes
; obj
++)
959 object
[obj
].TotalByteLength
= 0;
960 object
[obj
].DefinitionLength
= 0;
961 instance
= object
[obj
].instances
;
962 counter
= object
[obj
].counters
;
963 for(cnt
= 0; cnt
< object
[obj
].NumCounters
; cnt
++)
965 object
[obj
].TotalByteLength
+= counter
[cnt
].ByteLength
;
966 object
[obj
].DefinitionLength
+= counter
[cnt
].ByteLength
;
968 if(object
[obj
].NumInstances
!= PERF_NO_INSTANCES
)
970 for(inst
= 0; inst
< object
[obj
].NumInstances
; inst
++)
972 instance
= &(object
[obj
].instances
[inst
]);
973 object
[obj
].TotalByteLength
+= instance
->ByteLength
;
974 counter_data
= &(instance
->counter_data
);
975 counter
= &(object
[obj
].counters
[object
[obj
].NumCounters
- 1]);
976 counter_data
->ByteLength
= counter
->CounterOffset
+ counter
->CounterSize
+ sizeof(counter_data
->ByteLength
);
977 temp
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
980 counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
984 memset(temp
, 0, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
985 src_addr
= (char *)counter_data
->data
;
986 for(i
= 0; i
< object
[obj
].NumCounters
; i
++)
988 counter
= &(object
[obj
].counters
[i
]);
989 dst_addr
= temp
+ counter
->CounterOffset
- sizeof(counter_data
->ByteLength
);
990 memcpy(dst_addr
, src_addr
, counter
->CounterSize
);
991 src_addr
+= counter
->CounterSize
;
993 /* Make sure to be 64-bit aligned */
994 if((pad
= (counter_data
->ByteLength
% 8)))
998 counter_data
->data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
1001 counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
) + pad
);
1002 if (counter_data
->data
== NULL
) {
1005 memset(counter_data
->data
, 0, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
) + pad
);
1006 memcpy(counter_data
->data
, temp
, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
1007 counter_data
->ByteLength
+= pad
;
1008 object
[obj
].TotalByteLength
+= counter_data
->ByteLength
;
1013 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1014 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1015 if((pad
= (object
[obj
].counter_data
.ByteLength
% 8)))
1018 object
[obj
].counter_data
.data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
1019 object
[obj
].counter_data
.data
,
1021 object
[obj
].counter_data
.ByteLength
+ pad
);
1022 memset((void *)(object
[obj
].counter_data
.data
+ object
[obj
].counter_data
.ByteLength
), 0, pad
);
1023 object
[obj
].counter_data
.ByteLength
+= pad
;
1025 object
[obj
].TotalByteLength
+= object
[obj
].counter_data
.ByteLength
;
1027 object
[obj
].HeaderLength
= sizeof(*object
) - (sizeof(counter
) + sizeof(instance
) + sizeof(PERF_COUNTER_BLOCK
));
1028 object
[obj
].TotalByteLength
+= object
[obj
].HeaderLength
;
1029 object
[obj
].DefinitionLength
+= object
[obj
].HeaderLength
;
1031 block
->TotalByteLength
+= object
[obj
].TotalByteLength
;
1034 return block
->TotalByteLength
;
1037 /*********************************************************************
1038 *********************************************************************/
1040 uint32
reg_perfcount_get_perf_data_block(uint32 base_index
,
1042 PERF_DATA_BLOCK
*block
,
1043 const char *object_ids
)
1045 uint32 buffer_size
= 0;
1046 const char *fname
= counters_directory( NAMES_DB
);
1050 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
1054 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname
));
1058 if (!_reg_perfcount_init_data_block(block
, ps
, names
)) {
1059 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1064 reg_perfcount_get_last_counter(base_index
);
1066 if(object_ids
== NULL
)
1068 /* we're getting a request for "Global" here */
1069 retval
= _reg_perfcount_assemble_global(block
, ps
, base_index
, names
);
1073 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
1074 retval
= _reg_perfcount_assemble_global(block
, ps
, base_index
, names
);
1076 buffer_size
= _reg_perfcount_perf_data_block_fixup(block
, ps
);
1084 return buffer_size
+ block
->HeaderLength
;
1087 /*********************************************************************
1088 *********************************************************************/
1090 static bool _reg_perfcount_marshall_perf_data_block(prs_struct
*ps
, PERF_DATA_BLOCK block
, int depth
)
1093 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_data_block");
1098 for(i
= 0; i
< 4; i
++)
1100 if(!prs_uint16("Signature", ps
, depth
, &block
.Signature
[i
]))
1103 if(!prs_uint32("Little Endian", ps
, depth
, &block
.LittleEndian
))
1105 if(!prs_uint32("Version", ps
, depth
, &block
.Version
))
1107 if(!prs_uint32("Revision", ps
, depth
, &block
.Revision
))
1109 if(!prs_uint32("TotalByteLength", ps
, depth
, &block
.TotalByteLength
))
1111 if(!prs_uint32("HeaderLength", ps
, depth
, &block
.HeaderLength
))
1113 if(!prs_uint32("NumObjectTypes", ps
, depth
, &block
.NumObjectTypes
))
1115 if(!prs_uint32("DefaultObject", ps
, depth
, &block
.DefaultObject
))
1117 if(!spoolss_io_system_time("SystemTime", ps
, depth
, &block
.SystemTime
))
1119 if(!prs_uint32("Padding", ps
, depth
, &block
.Padding
))
1121 if(!prs_align_uint64(ps
))
1123 if(!prs_uint64("PerfTime", ps
, depth
, &block
.PerfTime
))
1125 if(!prs_uint64("PerfFreq", ps
, depth
, &block
.PerfFreq
))
1127 if(!prs_uint64("PerfTime100nSec", ps
, depth
, &block
.PerfTime100nSec
))
1129 if(!prs_uint32("SystemNameLength", ps
, depth
, &block
.SystemNameLength
))
1131 if(!prs_uint32("SystemNameOffset", ps
, depth
, &block
.SystemNameOffset
))
1133 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1134 if(!prs_uint8s(False
, "SystemName", ps
, depth
, block
.data
,
1135 block
.HeaderLength
- block
.SystemNameOffset
))
1141 /*********************************************************************
1142 *********************************************************************/
1144 static bool _reg_perfcount_marshall_perf_counters(prs_struct
*ps
,
1145 PERF_OBJECT_TYPE object
,
1149 PERF_COUNTER_DEFINITION counter
;
1151 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_counters");
1154 for(cnt
= 0; cnt
< object
.NumCounters
; cnt
++)
1156 counter
= object
.counters
[cnt
];
1160 if(!prs_uint32("ByteLength", ps
, depth
, &counter
.ByteLength
))
1162 if(!prs_uint32("CounterNameTitleIndex", ps
, depth
, &counter
.CounterNameTitleIndex
))
1164 if(!prs_uint32("CounterNameTitlePointer", ps
, depth
, &counter
.CounterNameTitlePointer
))
1166 if(!prs_uint32("CounterHelpTitleIndex", ps
, depth
, &counter
.CounterHelpTitleIndex
))
1168 if(!prs_uint32("CounterHelpTitlePointer", ps
, depth
, &counter
.CounterHelpTitlePointer
))
1170 if(!prs_uint32("DefaultScale", ps
, depth
, &counter
.DefaultScale
))
1172 if(!prs_uint32("DetailLevel", ps
, depth
, &counter
.DetailLevel
))
1174 if(!prs_uint32("CounterType", ps
, depth
, &counter
.CounterType
))
1176 if(!prs_uint32("CounterSize", ps
, depth
, &counter
.CounterSize
))
1178 if(!prs_uint32("CounterOffset", ps
, depth
, &counter
.CounterOffset
))
1185 /*********************************************************************
1186 *********************************************************************/
1188 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct
*ps
,
1189 PERF_COUNTER_BLOCK counter_data
,
1192 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_counter_data");
1195 if(!prs_align_uint64(ps
))
1198 if(!prs_uint32("ByteLength", ps
, depth
, &counter_data
.ByteLength
))
1200 if(!prs_uint8s(False
, "CounterData", ps
, depth
, counter_data
.data
, counter_data
.ByteLength
- sizeof(uint32
)))
1202 if(!prs_align_uint64(ps
))
1208 /*********************************************************************
1209 *********************************************************************/
1211 static bool _reg_perfcount_marshall_perf_instances(prs_struct
*ps
,
1212 PERF_OBJECT_TYPE object
,
1215 PERF_INSTANCE_DEFINITION instance
;
1218 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_instances");
1221 for(inst
= 0; inst
< object
.NumInstances
; inst
++)
1223 instance
= object
.instances
[inst
];
1227 if(!prs_uint32("ByteLength", ps
, depth
, &instance
.ByteLength
))
1229 if(!prs_uint32("ParentObjectTitleIndex", ps
, depth
, &instance
.ParentObjectTitleIndex
))
1231 if(!prs_uint32("ParentObjectTitlePointer", ps
, depth
, &instance
.ParentObjectTitlePointer
))
1233 if(!prs_uint32("UniqueID", ps
, depth
, &instance
.UniqueID
))
1235 if(!prs_uint32("NameOffset", ps
, depth
, &instance
.NameOffset
))
1237 if(!prs_uint32("NameLength", ps
, depth
, &instance
.NameLength
))
1239 if(!prs_uint8s(False
, "InstanceName", ps
, depth
, instance
.data
,
1240 instance
.ByteLength
- instance
.NameOffset
))
1242 if(_reg_perfcount_marshall_perf_counter_data(ps
, instance
.counter_data
, depth
) == False
)
1249 /*********************************************************************
1250 *********************************************************************/
1252 static bool _reg_perfcount_marshall_perf_objects(prs_struct
*ps
, PERF_DATA_BLOCK block
, int depth
)
1256 PERF_OBJECT_TYPE object
;
1258 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_objects");
1261 for(obj
= 0; obj
< block
.NumObjectTypes
; obj
++)
1263 object
= block
.objects
[obj
];
1268 if(!prs_uint32("TotalByteLength", ps
, depth
, &object
.TotalByteLength
))
1270 if(!prs_uint32("DefinitionLength", ps
, depth
, &object
.DefinitionLength
))
1272 if(!prs_uint32("HeaderLength", ps
, depth
, &object
.HeaderLength
))
1274 if(!prs_uint32("ObjectNameTitleIndex", ps
, depth
, &object
.ObjectNameTitleIndex
))
1276 if(!prs_uint32("ObjectNameTitlePointer", ps
, depth
, &object
.ObjectNameTitlePointer
))
1278 if(!prs_uint32("ObjectHelpTitleIndex", ps
, depth
, &object
.ObjectHelpTitleIndex
))
1280 if(!prs_uint32("ObjectHelpTitlePointer", ps
, depth
, &object
.ObjectHelpTitlePointer
))
1282 if(!prs_uint32("DetailLevel", ps
, depth
, &object
.DetailLevel
))
1284 if(!prs_uint32("NumCounters", ps
, depth
, &object
.NumCounters
))
1286 if(!prs_uint32("DefaultCounter", ps
, depth
, &object
.DefaultCounter
))
1288 if(!prs_uint32("NumInstances", ps
, depth
, &object
.NumInstances
))
1290 if(!prs_uint32("CodePage", ps
, depth
, &object
.CodePage
))
1292 if(!prs_align_uint64(ps
))
1294 if(!prs_uint64("PerfTime", ps
, depth
, &object
.PerfTime
))
1296 if(!prs_uint64("PerfFreq", ps
, depth
, &object
.PerfFreq
))
1299 /* Now do the counters */
1300 /* If no instances, encode counter_data */
1301 /* If instances, encode instace plus counter data for each instance */
1302 if(_reg_perfcount_marshall_perf_counters(ps
, object
, depth
) == False
)
1304 if(object
.NumInstances
== PERF_NO_INSTANCES
)
1306 if(_reg_perfcount_marshall_perf_counter_data(ps
, object
.counter_data
, depth
) == False
)
1311 if(_reg_perfcount_marshall_perf_instances(ps
, object
, depth
) == False
)
1319 /*********************************************************************
1320 *********************************************************************/
1322 static bool _reg_perfcount_marshall_hkpd(prs_struct
*ps
, PERF_DATA_BLOCK block
)
1325 if(_reg_perfcount_marshall_perf_data_block(ps
, block
, depth
) == True
)
1327 if(_reg_perfcount_marshall_perf_objects(ps
, block
, depth
) == True
)
1333 /*********************************************************************
1334 *********************************************************************/
1336 WERROR
reg_perfcount_get_hkpd(prs_struct
*ps
, uint32 max_buf_size
, uint32
*outbuf_len
, const char *object_ids
)
1339 * For a detailed description of the layout of this structure,
1340 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1342 * By 2006-11-23 this link did not work anymore, I found something
1344 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1346 PERF_DATA_BLOCK block
;
1347 uint32 buffer_size
, base_index
;
1350 base_index
= reg_perfcount_get_base_index();
1353 buffer_size
= reg_perfcount_get_perf_data_block(base_index
, ps
, &block
, object_ids
);
1355 if(buffer_size
< max_buf_size
)
1357 *outbuf_len
= buffer_size
;
1358 if(_reg_perfcount_marshall_hkpd(ps
, block
) == True
)
1365 *outbuf_len
= max_buf_size
;
1366 _reg_perfcount_marshall_perf_data_block(ps
, block
, 0);
1367 return WERR_INSUFFICIENT_BUFFER
;