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/>.
23 #include "system/filesys.h"
24 #include "../librpc/gen_ndr/perfcount.h"
26 #include "reg_perfcount.h"
27 #include "../libcli/registry/util_reg.h"
31 #define DBGC_CLASS DBGC_REGISTRY
33 #define PERFCOUNT_MAX_LEN 256
35 #define PERFCOUNTDIR "perfmon"
36 #define NAMES_DB "names.tdb"
37 #define DATA_DB "data.tdb"
39 struct PERF_OBJECT_TYPE
*_reg_perfcount_find_obj(struct PERF_DATA_BLOCK
*block
, int objind
);
41 /*********************************************************************
42 *********************************************************************/
44 /* returns perfcount path for dbname allocated on talloc_tos */
45 static char *counters_directory(const char *dbname
)
47 char *dir_path
= NULL
;
48 char *db_subpath
= NULL
;
51 dir_path
= state_path(PERFCOUNTDIR
);
52 if (dir_path
== NULL
) {
56 if (!directory_create_or_exist(dir_path
, 0755)) {
57 TALLOC_FREE(dir_path
);
61 db_subpath
= talloc_asprintf(dir_path
, "%s/%s", PERFCOUNTDIR
, dbname
);
62 if (db_subpath
== NULL
) {
63 TALLOC_FREE(dir_path
);
67 ret
= state_path(db_subpath
);
68 TALLOC_FREE(dir_path
);
72 /*********************************************************************
73 *********************************************************************/
75 uint32_t reg_perfcount_get_base_index(void)
82 char buf
[PERFCOUNT_MAX_LEN
];
84 fname
= counters_directory(NAMES_DB
);
89 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
92 DEBUG(2, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname
));
96 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
97 where the total number of counters is stored. We're assuming no holes in the
99 The format for the counter_names.tdb file is:
106 even_num perf_counter<even_num>
107 even_num+1 perf_counter<even_num>_help
109 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
110 kbuf
= string_tdb_data(key
);
111 dbuf
= tdb_fetch(names
, kbuf
);
112 if(dbuf
.dptr
== NULL
)
114 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname
));
122 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
123 memcpy(buf
, dbuf
.dptr
, dbuf
.dsize
);
124 retval
= (uint32_t)atoi(buf
);
125 SAFE_FREE(dbuf
.dptr
);
129 /*********************************************************************
130 *********************************************************************/
132 uint32_t reg_perfcount_get_last_counter(uint32_t base_index
)
139 retval
= base_index
* 2;
144 /*********************************************************************
145 *********************************************************************/
147 uint32_t reg_perfcount_get_last_help(uint32_t last_counter
)
151 if(last_counter
== 0)
154 retval
= last_counter
+ 1;
160 /*********************************************************************
161 *********************************************************************/
163 static uint32_t _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT
*tdb
,
166 uint32_t buffer_size
)
170 char *buf1
= *retbuf
;
171 uint32_t working_size
= 0;
172 DATA_BLOB name_index
, name
;
175 memset(temp
, 0, sizeof(temp
));
176 snprintf(temp
, sizeof(temp
), "%d", keyval
);
177 kbuf
= string_tdb_data(temp
);
178 dbuf
= tdb_fetch(tdb
, kbuf
);
179 if(dbuf
.dptr
== NULL
)
181 /* If a key isn't there, just bypass it -- this really shouldn't
182 happen unless someone's mucking around with the tdb */
183 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
184 temp
, tdb_name(tdb
)));
187 /* First encode the name_index */
188 working_size
= (kbuf
.dsize
+ 1)*sizeof(uint16_t);
189 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ working_size
);
194 ok
= push_reg_sz(talloc_tos(), &name_index
, (const char *)kbuf
.dptr
);
199 memcpy(buf1
+buffer_size
, (char *)name_index
.data
, working_size
);
200 buffer_size
+= working_size
;
201 /* Now encode the actual name */
202 working_size
= (dbuf
.dsize
+ 1)*sizeof(uint16_t);
203 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ working_size
);
208 memset(temp
, 0, sizeof(temp
));
209 memcpy(temp
, dbuf
.dptr
, dbuf
.dsize
);
210 SAFE_FREE(dbuf
.dptr
);
211 ok
= push_reg_sz(talloc_tos(), &name
, temp
);
216 memcpy(buf1
+buffer_size
, (char *)name
.data
, working_size
);
217 buffer_size
+= working_size
;
224 /*********************************************************************
225 *********************************************************************/
227 uint32_t reg_perfcount_get_counter_help(uint32_t base_index
, char **retbuf
)
230 uint32_t buffer_size
= 0;
235 if (base_index
== 0) {
239 fname
= counters_directory(NAMES_DB
);
244 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
247 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname
));
253 for(i
= 1; i
<= base_index
; i
++)
255 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, (i
*2)+1, retbuf
, buffer_size
);
259 /* Now terminate the MULTI_SZ with a double unicode NULL */
261 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ 2);
265 buf1
[buffer_size
++] = '\0';
266 buf1
[buffer_size
++] = '\0';
274 /*********************************************************************
275 *********************************************************************/
277 uint32_t reg_perfcount_get_counter_names(uint32_t base_index
, char **retbuf
)
280 uint32_t buffer_size
= 0;
285 if (base_index
== 0) {
289 fname
= counters_directory(NAMES_DB
);
294 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
297 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname
));
303 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, 1, retbuf
, buffer_size
);
305 for(i
= 1; i
<= base_index
; i
++)
307 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, i
*2, retbuf
, buffer_size
);
311 /* Now terminate the MULTI_SZ with a double unicode NULL */
313 buf1
= (char *)SMB_REALLOC(buf1
, buffer_size
+ 2);
317 buf1
[buffer_size
++] = '\0';
318 buf1
[buffer_size
++] = '\0';
326 /*********************************************************************
327 *********************************************************************/
329 static void _reg_perfcount_make_key(TDB_DATA
*key
,
333 const char *key_part2
)
335 memset(buf
, 0, buflen
);
336 if(key_part2
!= NULL
)
337 snprintf(buf
, buflen
,"%d%s", key_part1
, key_part2
);
339 snprintf(buf
, buflen
, "%d", key_part1
);
341 *key
= string_tdb_data(buf
);
346 /*********************************************************************
347 *********************************************************************/
349 static bool _reg_perfcount_isparent(TDB_DATA data
)
353 if(data
.dptr
[0] == 'p')
361 /*********************************************************************
362 *********************************************************************/
364 static bool _reg_perfcount_ischild(TDB_DATA data
)
368 if(data
.dptr
[0] == 'c')
376 /*********************************************************************
377 *********************************************************************/
379 static uint32_t _reg_perfcount_get_numinst(int objInd
, TDB_CONTEXT
*names
)
382 char buf
[PERFCOUNT_MAX_LEN
];
384 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, objInd
, "inst");
385 data
= tdb_fetch(names
, key
);
387 if(data
.dptr
== NULL
)
388 return (uint32_t)PERF_NO_INSTANCES
;
390 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
391 memcpy(buf
, data
.dptr
, data
.dsize
);
392 SAFE_FREE(data
.dptr
);
393 return (uint32_t)atoi(buf
);
396 /*********************************************************************
397 *********************************************************************/
399 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE
*obj
,
404 static bool _reg_perfcount_add_object(struct PERF_DATA_BLOCK
*block
,
412 struct PERF_OBJECT_TYPE
*obj
;
414 block
->objects
= (struct PERF_OBJECT_TYPE
*)talloc_realloc(mem_ctx
,
416 struct PERF_OBJECT_TYPE
,
417 block
->NumObjectTypes
+1);
418 if(block
->objects
== NULL
)
420 obj
= &(block
->objects
[block
->NumObjectTypes
]);
421 memset((void *)&(block
->objects
[block
->NumObjectTypes
]), 0, sizeof(struct PERF_OBJECT_TYPE
));
422 block
->objects
[block
->NumObjectTypes
].ObjectNameTitleIndex
= num
;
423 block
->objects
[block
->NumObjectTypes
].ObjectNameTitlePointer
= 0;
424 block
->objects
[block
->NumObjectTypes
].ObjectHelpTitleIndex
= num
+1;
425 block
->objects
[block
->NumObjectTypes
].ObjectHelpTitlePointer
= 0;
426 block
->objects
[block
->NumObjectTypes
].NumCounters
= 0;
427 block
->objects
[block
->NumObjectTypes
].DefaultCounter
= 0;
428 block
->objects
[block
->NumObjectTypes
].NumInstances
= _reg_perfcount_get_numinst(num
, names
);
429 block
->objects
[block
->NumObjectTypes
].counters
= NULL
;
430 block
->objects
[block
->NumObjectTypes
].instances
= NULL
;
431 block
->objects
[block
->NumObjectTypes
].counter_data
.ByteLength
= sizeof(uint32_t);
432 block
->objects
[block
->NumObjectTypes
].counter_data
.data
= NULL
;
433 block
->objects
[block
->NumObjectTypes
].DetailLevel
= PERF_DETAIL_NOVICE
;
434 block
->NumObjectTypes
+=1;
436 for(i
= 0; i
< (int)obj
->NumInstances
; i
++) {
437 success
= _reg_perfcount_add_instance(obj
, mem_ctx
, i
, names
);
443 /*********************************************************************
444 *********************************************************************/
446 static bool _reg_perfcount_get_counter_data(TDB_DATA key
, TDB_DATA
*data
)
448 TDB_CONTEXT
*counters
;
451 fname
= counters_directory(DATA_DB
);
456 counters
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
458 if (counters
== NULL
) {
459 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname
));
465 *data
= tdb_fetch(counters
, key
);
472 /*********************************************************************
473 *********************************************************************/
475 static uint32_t _reg_perfcount_get_size_field(uint32_t CounterType
)
479 retval
= CounterType
;
481 /* First mask out reserved lower 8 bits */
482 retval
= retval
& 0xFFFFFF00;
483 retval
= retval
<< 22;
484 retval
= retval
>> 22;
489 /*********************************************************************
490 *********************************************************************/
492 static uint32_t _reg_perfcount_compute_scale(int64_t data
)
508 return (uint32_t)scale
;
511 /*********************************************************************
512 *********************************************************************/
514 static bool _reg_perfcount_get_counter_info(struct PERF_DATA_BLOCK
*block
,
517 struct PERF_OBJECT_TYPE
*obj
,
521 char buf
[PERFCOUNT_MAX_LEN
];
522 size_t dsize
, padding
;
523 long int data32
, dbuf
[2];
525 uint32_t counter_size
;
527 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
528 dbuf
[0] = dbuf
[1] = 0;
531 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, CounterIndex
, "type");
532 data
= tdb_fetch(names
, key
);
533 if(data
.dptr
== NULL
)
535 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex
));
538 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
539 memcpy(buf
, data
.dptr
, data
.dsize
);
540 obj
->counters
[obj
->NumCounters
].CounterType
= atoi(buf
);
541 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
542 obj
->counters
[obj
->NumCounters
].CounterType
, CounterIndex
));
543 SAFE_FREE(data
.dptr
);
545 /* Fetch the actual data */
546 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, CounterIndex
, "");
547 _reg_perfcount_get_counter_data(key
, &data
);
548 if(data
.dptr
== NULL
)
550 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex
));
554 counter_size
= _reg_perfcount_get_size_field(obj
->counters
[obj
->NumCounters
].CounterType
);
556 if(counter_size
== PERF_SIZE_DWORD
)
558 dsize
= sizeof(data32
);
559 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
560 memcpy(buf
, data
.dptr
, data
.dsize
);
561 data32
= strtol(buf
, NULL
, 0);
562 if((obj
->counters
[obj
->NumCounters
].CounterType
& 0x00000F00) == PERF_TYPE_NUMBER
)
563 obj
->counters
[obj
->NumCounters
].DefaultScale
= _reg_perfcount_compute_scale((int64_t)data32
);
565 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
567 padding
= (dsize
- (obj
->counter_data
.ByteLength
%dsize
)) % dsize
;
569 else if(counter_size
== PERF_SIZE_LARGE
)
571 dsize
= sizeof(data64
);
572 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
573 memcpy(buf
, data
.dptr
, data
.dsize
);
575 if((obj
->counters
[obj
->NumCounters
].CounterType
& 0x00000F00) == PERF_TYPE_NUMBER
)
576 obj
->counters
[obj
->NumCounters
].DefaultScale
= _reg_perfcount_compute_scale(data64
);
578 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
579 memcpy((void *)dbuf
, (const void *)&data64
, dsize
);
580 padding
= (dsize
- (obj
->counter_data
.ByteLength
%dsize
)) % dsize
;
582 else /* PERF_SIZE_VARIABLE_LEN */
585 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
586 memcpy(buf
, data
.dptr
, data
.dsize
);
588 SAFE_FREE(data
.dptr
);
590 obj
->counter_data
.ByteLength
+= dsize
+ padding
;
591 obj
->counter_data
.data
= talloc_realloc(mem_ctx
,
592 obj
->counter_data
.data
,
594 obj
->counter_data
.ByteLength
- sizeof(uint32_t));
595 if(obj
->counter_data
.data
== NULL
)
597 if(dbuf
[0] != 0 || dbuf
[1] != 0)
599 memcpy((void *)(obj
->counter_data
.data
+
600 (obj
->counter_data
.ByteLength
- (sizeof(uint32_t) + dsize
))),
601 (const void *)dbuf
, dsize
);
605 /* Handling PERF_SIZE_VARIABLE_LEN */
606 memcpy((void *)(obj
->counter_data
.data
+
607 (obj
->counter_data
.ByteLength
- (sizeof(uint32_t) + dsize
))),
608 (const void *)buf
, dsize
);
610 obj
->counters
[obj
->NumCounters
].CounterOffset
= obj
->counter_data
.ByteLength
- dsize
;
611 if(obj
->counters
[obj
->NumCounters
].CounterOffset
% dsize
!= 0)
613 DEBUG(3,("Improperly aligned counter [%d]\n", obj
->NumCounters
));
615 obj
->counters
[obj
->NumCounters
].CounterSize
= dsize
;
620 /*********************************************************************
621 *********************************************************************/
623 struct PERF_OBJECT_TYPE
*_reg_perfcount_find_obj(struct PERF_DATA_BLOCK
*block
, int objind
)
627 struct PERF_OBJECT_TYPE
*obj
= NULL
;
629 for(i
= 0; i
< block
->NumObjectTypes
; i
++)
631 if(block
->objects
[i
].ObjectNameTitleIndex
== objind
)
633 obj
= &(block
->objects
[i
]);
640 /*********************************************************************
641 *********************************************************************/
643 static bool _reg_perfcount_add_counter(struct PERF_DATA_BLOCK
*block
,
649 char *begin
, *end
, *start
, *stop
;
651 struct PERF_OBJECT_TYPE
*obj
;
653 char buf
[PERFCOUNT_MAX_LEN
];
656 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
657 memcpy(buf
, data
.dptr
, data
.dsize
);
658 begin
= strchr(buf
, '[');
659 end
= strchr(buf
, ']');
660 if(begin
== NULL
|| end
== NULL
)
665 stop
= strchr(start
, ',');
669 parent
= atoi(start
);
671 obj
= _reg_perfcount_find_obj(block
, parent
);
673 /* At this point we require that the parent object exist.
674 This can probably be handled better at some later time */
675 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
679 obj
->counters
= (struct PERF_COUNTER_DEFINITION
*)talloc_realloc(mem_ctx
,
681 struct PERF_COUNTER_DEFINITION
,
683 if(obj
->counters
== NULL
)
685 memset((void *)&(obj
->counters
[obj
->NumCounters
]), 0, sizeof(struct PERF_COUNTER_DEFINITION
));
686 obj
->counters
[obj
->NumCounters
].CounterNameTitleIndex
=num
;
687 obj
->counters
[obj
->NumCounters
].CounterHelpTitleIndex
=num
+1;
688 obj
->counters
[obj
->NumCounters
].DetailLevel
= PERF_DETAIL_NOVICE
;
689 obj
->counters
[obj
->NumCounters
].ByteLength
= sizeof(struct PERF_COUNTER_DEFINITION
);
690 success
= _reg_perfcount_get_counter_info(block
, mem_ctx
, num
, obj
, names
);
691 obj
->NumCounters
+= 1;
695 /* Handle case of Objects/Counters without any counter data, which would suggest
696 that the required instances are not there yet, so change NumInstances from
697 PERF_NO_INSTANCES to 0 */
702 /*********************************************************************
703 *********************************************************************/
705 static bool _reg_perfcount_get_instance_info(struct PERF_INSTANCE_DEFINITION
*inst
,
708 struct PERF_OBJECT_TYPE
*obj
,
712 char buf
[PERFCOUNT_MAX_LEN
], temp
[PERFCOUNT_MAX_LEN
];
713 smb_ucs2_t
*name
= NULL
;
716 /* First grab the instance data from the data file */
717 memset(temp
, 0, PERFCOUNT_MAX_LEN
);
718 snprintf(temp
, PERFCOUNT_MAX_LEN
, "i%d", instId
);
719 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, obj
->ObjectNameTitleIndex
, temp
);
720 if (!_reg_perfcount_get_counter_data(key
, &data
)) {
721 DEBUG(3, ("_reg_perfcount_get_counter_data failed\n"));
724 if(data
.dptr
== NULL
)
726 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
730 inst
->counter_data
.ByteLength
= data
.dsize
+ sizeof(inst
->counter_data
.ByteLength
);
731 inst
->counter_data
.data
= talloc_realloc(mem_ctx
,
732 inst
->counter_data
.data
,
735 if(inst
->counter_data
.data
== NULL
)
737 memset(inst
->counter_data
.data
, 0, data
.dsize
);
738 memcpy(inst
->counter_data
.data
, data
.dptr
, data
.dsize
);
739 SAFE_FREE(data
.dptr
);
741 /* Fetch instance name */
742 memset(temp
, 0, PERFCOUNT_MAX_LEN
);
743 snprintf(temp
, PERFCOUNT_MAX_LEN
, "i%dname", instId
);
744 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, obj
->ObjectNameTitleIndex
, temp
);
745 data
= tdb_fetch(names
, key
);
746 if(data
.dptr
== NULL
)
748 /* Not actually an error, but possibly unintended? -- just logging FYI */
749 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
751 inst
->NameLength
= 0;
755 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
756 memcpy(buf
, data
.dptr
, MIN(PERFCOUNT_MAX_LEN
-1,data
.dsize
));
757 buf
[PERFCOUNT_MAX_LEN
-1] = '\0';
758 inst
->NameLength
= rpcstr_push_talloc(mem_ctx
, &name
, buf
);
759 if (inst
->NameLength
== (uint32_t)-1 || !name
) {
760 SAFE_FREE(data
.dptr
);
763 inst
->data
= talloc_realloc(mem_ctx
,
767 if (inst
->data
== NULL
) {
768 SAFE_FREE(data
.dptr
);
771 memcpy(inst
->data
, name
, inst
->NameLength
);
772 SAFE_FREE(data
.dptr
);
775 inst
->ParentObjectTitleIndex
= 0;
776 inst
->ParentObjectTitlePointer
= 0;
777 inst
->UniqueID
= PERF_NO_UNIQUE_ID
;
778 inst
->NameOffset
= 6 * sizeof(uint32_t);
780 inst
->ByteLength
= inst
->NameOffset
+ inst
->NameLength
;
781 /* Need to be aligned on a 64-bit boundary here for counter_data */
782 if((pad
= (inst
->ByteLength
% 8)))
785 inst
->data
= talloc_realloc(mem_ctx
,
788 inst
->NameLength
+ pad
);
789 memset(inst
->data
+ inst
->NameLength
, 0, pad
);
790 inst
->ByteLength
+= pad
;
796 /*********************************************************************
797 *********************************************************************/
799 static bool _reg_perfcount_add_instance(struct PERF_OBJECT_TYPE
*obj
,
804 struct PERF_INSTANCE_DEFINITION
*inst
;
806 if(obj
->instances
== NULL
) {
807 obj
->instances
= talloc_realloc(mem_ctx
,
809 struct PERF_INSTANCE_DEFINITION
,
812 if(obj
->instances
== NULL
)
815 memset(&(obj
->instances
[instInd
]), 0, sizeof(struct PERF_INSTANCE_DEFINITION
));
816 inst
= &(obj
->instances
[instInd
]);
817 return _reg_perfcount_get_instance_info(inst
, mem_ctx
, instInd
, obj
, names
);
820 /*********************************************************************
821 *********************************************************************/
823 static int _reg_perfcount_assemble_global(struct PERF_DATA_BLOCK
*block
,
829 int i
, j
, retval
= 0;
830 char keybuf
[PERFCOUNT_MAX_LEN
];
833 for(i
= 1; i
<= base_index
; i
++)
836 _reg_perfcount_make_key(&key
, keybuf
, PERFCOUNT_MAX_LEN
, j
, "rel");
837 data
= tdb_fetch(names
, key
);
838 if(data
.dptr
!= NULL
)
840 if(_reg_perfcount_isparent(data
))
841 success
= _reg_perfcount_add_object(block
, mem_ctx
, j
, data
, names
);
842 else if(_reg_perfcount_ischild(data
))
843 success
= _reg_perfcount_add_counter(block
, mem_ctx
, j
, data
, names
);
846 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data
.dptr
, j
));
851 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j
));
854 SAFE_FREE(data
.dptr
);
857 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j
, keybuf
));
862 /*********************************************************************
863 *********************************************************************/
865 static bool _reg_perfcount_get_64(uint64_t *retval
,
868 const char *key_part2
)
871 char buf
[PERFCOUNT_MAX_LEN
];
873 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, key_part1
, key_part2
);
875 data
= tdb_fetch(tdb
, key
);
876 if(data
.dptr
== NULL
)
878 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key
.dptr
));
882 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
883 memcpy(buf
, data
.dptr
, data
.dsize
);
884 SAFE_FREE(data
.dptr
);
891 /*********************************************************************
892 *********************************************************************/
894 static bool _reg_perfcount_init_data_block_perf(struct PERF_DATA_BLOCK
*block
,
897 uint64_t PerfFreq
, PerfTime
, PerfTime100nSec
;
898 TDB_CONTEXT
*counters
;
902 fname
= counters_directory(DATA_DB
);
907 counters
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
909 if (counters
== NULL
) {
910 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname
));
916 status
= _reg_perfcount_get_64(&PerfFreq
, names
, 0, "PerfFreq");
922 memcpy((void *)&(block
->PerfFreq
), (const void *)&PerfFreq
, sizeof(PerfFreq
));
924 status
= _reg_perfcount_get_64(&PerfTime
, counters
, 0, "PerfTime");
930 memcpy((void *)&(block
->PerfTime
), (const void *)&PerfTime
, sizeof(PerfTime
));
932 status
= _reg_perfcount_get_64(&PerfTime100nSec
, counters
, 0, "PerfTime100nSec");
938 memcpy((void *)&(block
->PerfTime100nSec
), (const void *)&PerfTime100nSec
, sizeof(PerfTime100nSec
));
944 /*******************************************************************
945 ********************************************************************/
947 static bool make_systemtime(struct SYSTEMTIME
*systime
, struct tm
*unixtime
)
949 systime
->year
=unixtime
->tm_year
+1900;
950 systime
->month
=unixtime
->tm_mon
+1;
951 systime
->dayofweek
=unixtime
->tm_wday
;
952 systime
->day
=unixtime
->tm_mday
;
953 systime
->hour
=unixtime
->tm_hour
;
954 systime
->minute
=unixtime
->tm_min
;
955 systime
->second
=unixtime
->tm_sec
;
956 systime
->milliseconds
=0;
961 /*********************************************************************
962 *********************************************************************/
964 static bool _reg_perfcount_init_data_block(struct PERF_DATA_BLOCK
*block
,
965 TALLOC_CTX
*mem_ctx
, TDB_CONTEXT
*names
,
968 smb_ucs2_t
*temp
= NULL
;
969 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
973 sz
= rpcstr_push_talloc(tmp_ctx
, &temp
, "PERF");
974 if ((sz
== -1) || (temp
== NULL
)) {
977 memcpy(block
->Signature
, temp
, strlen_w(temp
) *2);
980 block
->LittleEndian
= 0;
982 block
->LittleEndian
= 1;
985 block
->TotalByteLength
= 0;
986 block
->NumObjectTypes
= 0;
987 block
->DefaultObject
= -1;
988 block
->objects
= NULL
;
990 make_systemtime(&(block
->SystemTime
), gmtime(&tm
));
991 _reg_perfcount_init_data_block_perf(block
, names
);
993 sz
= rpcstr_push_talloc(tmp_ctx
, &temp
, lp_netbios_name());
994 if ((sz
== -1) || (temp
== NULL
)) {
997 block
->SystemNameLength
= (strlen_w(temp
) * 2) + 2;
998 block
->data
= talloc_zero_array(mem_ctx
, uint8_t, block
->SystemNameLength
+ (8 - (block
->SystemNameLength
% 8)));
999 if (block
->data
== NULL
) {
1002 memcpy(block
->data
, temp
, block
->SystemNameLength
);
1003 block
->SystemNameOffset
= sizeof(struct PERF_DATA_BLOCK
) - sizeof(block
->objects
) - sizeof(block
->data
);
1004 block
->HeaderLength
= block
->SystemNameOffset
+ block
->SystemNameLength
;
1005 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
1006 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
1007 block
->HeaderLength
+= 8 - (block
->HeaderLength
% 8);
1008 talloc_free(tmp_ctx
);
1013 talloc_free(tmp_ctx
);
1017 /*********************************************************************
1018 *********************************************************************/
1020 static uint32_t _reg_perfcount_perf_data_block_fixup(struct PERF_DATA_BLOCK
*block
, TALLOC_CTX
*mem_ctx
)
1022 int obj
, cnt
, inst
, pad
, i
;
1023 struct PERF_OBJECT_TYPE
*object
;
1024 struct PERF_INSTANCE_DEFINITION
*instance
;
1025 struct PERF_COUNTER_DEFINITION
*counter
;
1026 struct PERF_COUNTER_BLOCK
*counter_data
;
1027 char *temp
= NULL
, *src_addr
, *dst_addr
;
1029 block
->TotalByteLength
= 0;
1030 object
= block
->objects
;
1031 for(obj
= 0; obj
< block
->NumObjectTypes
; obj
++)
1033 object
[obj
].TotalByteLength
= 0;
1034 object
[obj
].DefinitionLength
= 0;
1035 instance
= object
[obj
].instances
;
1036 counter
= object
[obj
].counters
;
1037 for(cnt
= 0; cnt
< object
[obj
].NumCounters
; cnt
++)
1039 object
[obj
].TotalByteLength
+= counter
[cnt
].ByteLength
;
1040 object
[obj
].DefinitionLength
+= counter
[cnt
].ByteLength
;
1042 if(object
[obj
].NumInstances
!= PERF_NO_INSTANCES
)
1044 for(inst
= 0; inst
< object
[obj
].NumInstances
; inst
++)
1046 instance
= &(object
[obj
].instances
[inst
]);
1047 object
[obj
].TotalByteLength
+= instance
->ByteLength
;
1048 counter_data
= &(instance
->counter_data
);
1049 counter
= &(object
[obj
].counters
[object
[obj
].NumCounters
- 1]);
1050 counter_data
->ByteLength
= counter
->CounterOffset
+ counter
->CounterSize
+ sizeof(counter_data
->ByteLength
);
1051 temp
= talloc_realloc(mem_ctx
,
1054 counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
1058 memset(temp
, 0, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
1059 src_addr
= (char *)counter_data
->data
;
1060 for(i
= 0; i
< object
[obj
].NumCounters
; i
++)
1062 counter
= &(object
[obj
].counters
[i
]);
1063 dst_addr
= temp
+ counter
->CounterOffset
- sizeof(counter_data
->ByteLength
);
1064 memcpy(dst_addr
, src_addr
, counter
->CounterSize
);
1065 src_addr
+= counter
->CounterSize
;
1067 /* Make sure to be 64-bit aligned */
1068 if((pad
= (counter_data
->ByteLength
% 8)))
1072 counter_data
->data
= talloc_realloc(mem_ctx
,
1075 counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
) + pad
);
1076 if (counter_data
->data
== NULL
) {
1079 memset(counter_data
->data
, 0, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
) + pad
);
1080 memcpy(counter_data
->data
, temp
, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
1081 counter_data
->ByteLength
+= pad
;
1082 object
[obj
].TotalByteLength
+= counter_data
->ByteLength
;
1087 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
1088 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
1089 if((pad
= (object
[obj
].counter_data
.ByteLength
% 8)))
1092 object
[obj
].counter_data
.data
= talloc_realloc(mem_ctx
,
1093 object
[obj
].counter_data
.data
,
1095 object
[obj
].counter_data
.ByteLength
+ pad
);
1096 memset((void *)(object
[obj
].counter_data
.data
+ object
[obj
].counter_data
.ByteLength
), 0, pad
);
1097 object
[obj
].counter_data
.ByteLength
+= pad
;
1099 object
[obj
].TotalByteLength
+= object
[obj
].counter_data
.ByteLength
;
1101 object
[obj
].HeaderLength
= sizeof(*object
) - (sizeof(counter
) + sizeof(instance
) + sizeof(struct PERF_COUNTER_BLOCK
));
1102 object
[obj
].TotalByteLength
+= object
[obj
].HeaderLength
;
1103 object
[obj
].DefinitionLength
+= object
[obj
].HeaderLength
;
1105 block
->TotalByteLength
+= object
[obj
].TotalByteLength
;
1108 return block
->TotalByteLength
;
1111 /*********************************************************************
1112 *********************************************************************/
1114 static uint32_t reg_perfcount_get_perf_data_block(uint32_t base_index
,
1115 TALLOC_CTX
*mem_ctx
,
1116 struct PERF_DATA_BLOCK
*block
,
1117 const char *object_ids
,
1118 bool bigendian_data
)
1120 uint32_t buffer_size
= 0;
1125 fname
= counters_directory(NAMES_DB
);
1126 if (fname
== NULL
) {
1130 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
1134 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname
));
1140 if (!_reg_perfcount_init_data_block(block
, mem_ctx
, names
, bigendian_data
)) {
1141 DEBUG(0, ("_reg_perfcount_init_data_block failed\n"));
1146 retval
= _reg_perfcount_assemble_global(block
, mem_ctx
, base_index
, names
);
1148 buffer_size
= _reg_perfcount_perf_data_block_fixup(block
, mem_ctx
);
1156 return buffer_size
+ block
->HeaderLength
;
1159 /*******************************************************************
1160 ********************************************************************/
1162 static bool smb_io_system_time(const char *desc
, prs_struct
*ps
, int depth
, struct SYSTEMTIME
*systime
)
1164 if(!prs_uint16("year", ps
, depth
, &systime
->year
))
1166 if(!prs_uint16("month", ps
, depth
, &systime
->month
))
1168 if(!prs_uint16("dayofweek", ps
, depth
, &systime
->dayofweek
))
1170 if(!prs_uint16("day", ps
, depth
, &systime
->day
))
1172 if(!prs_uint16("hour", ps
, depth
, &systime
->hour
))
1174 if(!prs_uint16("minute", ps
, depth
, &systime
->minute
))
1176 if(!prs_uint16("second", ps
, depth
, &systime
->second
))
1178 if(!prs_uint16("milliseconds", ps
, depth
, &systime
->milliseconds
))
1184 /*********************************************************************
1185 *********************************************************************/
1187 static bool _reg_perfcount_marshall_perf_data_block(prs_struct
*ps
, struct PERF_DATA_BLOCK block
, int depth
)
1190 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_data_block");
1195 for(i
= 0; i
< 4; i
++)
1197 if(!prs_uint16("Signature", ps
, depth
, &block
.Signature
[i
]))
1200 if(!prs_uint32("Little Endian", ps
, depth
, &block
.LittleEndian
))
1202 if(!prs_uint32("Version", ps
, depth
, &block
.Version
))
1204 if(!prs_uint32("Revision", ps
, depth
, &block
.Revision
))
1206 if(!prs_uint32("TotalByteLength", ps
, depth
, &block
.TotalByteLength
))
1208 if(!prs_uint32("HeaderLength", ps
, depth
, &block
.HeaderLength
))
1210 if(!prs_uint32("NumObjectTypes", ps
, depth
, &block
.NumObjectTypes
))
1212 if(!prs_uint32("DefaultObject", ps
, depth
, &block
.DefaultObject
))
1214 if(!smb_io_system_time("SystemTime", ps
, depth
, &block
.SystemTime
))
1216 if(!prs_uint32("Padding", ps
, depth
, &block
.Padding
))
1218 if(!prs_align_uint64(ps
))
1220 if(!prs_uint64("PerfTime", ps
, depth
, &block
.PerfTime
))
1222 if(!prs_uint64("PerfFreq", ps
, depth
, &block
.PerfFreq
))
1224 if(!prs_uint64("PerfTime100nSec", ps
, depth
, &block
.PerfTime100nSec
))
1226 if(!prs_uint32("SystemNameLength", ps
, depth
, &block
.SystemNameLength
))
1228 if(!prs_uint32("SystemNameOffset", ps
, depth
, &block
.SystemNameOffset
))
1230 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1231 if(!prs_uint8s(False
, "SystemName", ps
, depth
, block
.data
,
1232 block
.HeaderLength
- block
.SystemNameOffset
))
1238 /*********************************************************************
1239 *********************************************************************/
1241 static bool _reg_perfcount_marshall_perf_counters(prs_struct
*ps
,
1242 struct PERF_OBJECT_TYPE object
,
1246 struct PERF_COUNTER_DEFINITION counter
;
1248 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_counters");
1251 for(cnt
= 0; cnt
< object
.NumCounters
; cnt
++)
1253 counter
= object
.counters
[cnt
];
1257 if(!prs_uint32("ByteLength", ps
, depth
, &counter
.ByteLength
))
1259 if(!prs_uint32("CounterNameTitleIndex", ps
, depth
, &counter
.CounterNameTitleIndex
))
1261 if(!prs_uint32("CounterNameTitlePointer", ps
, depth
, &counter
.CounterNameTitlePointer
))
1263 if(!prs_uint32("CounterHelpTitleIndex", ps
, depth
, &counter
.CounterHelpTitleIndex
))
1265 if(!prs_uint32("CounterHelpTitlePointer", ps
, depth
, &counter
.CounterHelpTitlePointer
))
1267 if(!prs_uint32("DefaultScale", ps
, depth
, &counter
.DefaultScale
))
1269 if(!prs_uint32("DetailLevel", ps
, depth
, &counter
.DetailLevel
))
1271 if(!prs_uint32("CounterType", ps
, depth
, &counter
.CounterType
))
1273 if(!prs_uint32("CounterSize", ps
, depth
, &counter
.CounterSize
))
1275 if(!prs_uint32("CounterOffset", ps
, depth
, &counter
.CounterOffset
))
1282 /*********************************************************************
1283 *********************************************************************/
1285 static bool _reg_perfcount_marshall_perf_counter_data(prs_struct
*ps
,
1286 struct PERF_COUNTER_BLOCK counter_data
,
1289 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_counter_data");
1292 if(!prs_align_uint64(ps
))
1295 if(!prs_uint32("ByteLength", ps
, depth
, &counter_data
.ByteLength
))
1297 if(!prs_uint8s(False
, "CounterData", ps
, depth
, counter_data
.data
, counter_data
.ByteLength
- sizeof(uint32_t)))
1299 if(!prs_align_uint64(ps
))
1305 /*********************************************************************
1306 *********************************************************************/
1308 static bool _reg_perfcount_marshall_perf_instances(prs_struct
*ps
,
1309 struct PERF_OBJECT_TYPE object
,
1312 struct PERF_INSTANCE_DEFINITION instance
;
1315 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_instances");
1318 for(inst
= 0; inst
< object
.NumInstances
; inst
++)
1320 instance
= object
.instances
[inst
];
1324 if(!prs_uint32("ByteLength", ps
, depth
, &instance
.ByteLength
))
1326 if(!prs_uint32("ParentObjectTitleIndex", ps
, depth
, &instance
.ParentObjectTitleIndex
))
1328 if(!prs_uint32("ParentObjectTitlePointer", ps
, depth
, &instance
.ParentObjectTitlePointer
))
1330 if(!prs_uint32("UniqueID", ps
, depth
, &instance
.UniqueID
))
1332 if(!prs_uint32("NameOffset", ps
, depth
, &instance
.NameOffset
))
1334 if(!prs_uint32("NameLength", ps
, depth
, &instance
.NameLength
))
1336 if(!prs_uint8s(False
, "InstanceName", ps
, depth
, instance
.data
,
1337 instance
.ByteLength
- instance
.NameOffset
))
1339 if(_reg_perfcount_marshall_perf_counter_data(ps
, instance
.counter_data
, depth
) == False
)
1346 /*********************************************************************
1347 *********************************************************************/
1349 static bool _reg_perfcount_marshall_perf_objects(prs_struct
*ps
, struct PERF_DATA_BLOCK block
, int depth
)
1353 struct PERF_OBJECT_TYPE object
;
1355 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_objects");
1358 for(obj
= 0; obj
< block
.NumObjectTypes
; obj
++)
1360 object
= block
.objects
[obj
];
1365 if(!prs_uint32("TotalByteLength", ps
, depth
, &object
.TotalByteLength
))
1367 if(!prs_uint32("DefinitionLength", ps
, depth
, &object
.DefinitionLength
))
1369 if(!prs_uint32("HeaderLength", ps
, depth
, &object
.HeaderLength
))
1371 if(!prs_uint32("ObjectNameTitleIndex", ps
, depth
, &object
.ObjectNameTitleIndex
))
1373 if(!prs_uint32("ObjectNameTitlePointer", ps
, depth
, &object
.ObjectNameTitlePointer
))
1375 if(!prs_uint32("ObjectHelpTitleIndex", ps
, depth
, &object
.ObjectHelpTitleIndex
))
1377 if(!prs_uint32("ObjectHelpTitlePointer", ps
, depth
, &object
.ObjectHelpTitlePointer
))
1379 if(!prs_uint32("DetailLevel", ps
, depth
, &object
.DetailLevel
))
1381 if(!prs_uint32("NumCounters", ps
, depth
, &object
.NumCounters
))
1383 if(!prs_uint32("DefaultCounter", ps
, depth
, &object
.DefaultCounter
))
1385 if(!prs_uint32("NumInstances", ps
, depth
, &object
.NumInstances
))
1387 if(!prs_uint32("CodePage", ps
, depth
, &object
.CodePage
))
1389 if(!prs_align_uint64(ps
))
1391 if(!prs_uint64("PerfTime", ps
, depth
, &object
.PerfTime
))
1393 if(!prs_uint64("PerfFreq", ps
, depth
, &object
.PerfFreq
))
1396 /* Now do the counters */
1397 /* If no instances, encode counter_data */
1398 /* If instances, encode instace plus counter data for each instance */
1399 if(_reg_perfcount_marshall_perf_counters(ps
, object
, depth
) == False
)
1401 if(object
.NumInstances
== PERF_NO_INSTANCES
)
1403 if(_reg_perfcount_marshall_perf_counter_data(ps
, object
.counter_data
, depth
) == False
)
1408 if(_reg_perfcount_marshall_perf_instances(ps
, object
, depth
) == False
)
1416 /*********************************************************************
1417 *********************************************************************/
1419 WERROR
reg_perfcount_get_hkpd(prs_struct
*ps
, uint32_t max_buf_size
, uint32_t *outbuf_len
, const char *object_ids
)
1422 * For a detailed description of the layout of this structure,
1423 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1425 * By 2006-11-23 this link did not work anymore, I found something
1427 * http://msdn2.microsoft.com/en-us/library/aa373105.aspx -- vl
1429 struct PERF_DATA_BLOCK block
;
1430 uint32_t buffer_size
, base_index
;
1433 base_index
= reg_perfcount_get_base_index();
1436 buffer_size
= reg_perfcount_get_perf_data_block(base_index
, ps
->mem_ctx
, &block
, object_ids
, ps
->bigendian_data
);
1438 if(buffer_size
< max_buf_size
)
1440 *outbuf_len
= buffer_size
;
1442 if (!_reg_perfcount_marshall_perf_data_block(ps
, block
, 0))
1445 if (!_reg_perfcount_marshall_perf_objects(ps
, block
, 0))
1452 *outbuf_len
= max_buf_size
;
1453 if (!_reg_perfcount_marshall_perf_data_block(ps
, block
, 0))
1456 return WERR_INSUFFICIENT_BUFFER
;