4 #define DBGC_CLASS DBGC_RPC_SRV
6 #define PERFCOUNT_MAX_LEN 256
8 uint32
reg_perfcount_get_base_index(void)
15 char buf
[PERFCOUNT_MAX_LEN
];
16 const char *counter_dir
= lp_counters_dir();
22 pstr_sprintf( fname
, "%s/names.tdb", counter_dir
);
24 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
27 DEBUG(1, ("reg_perfcount_get_base_index: unable to open [%s].\n", fname
));
30 /* needs to read the value of key "1" from the counter_names.tdb file, as that is
31 where the total number of counters is stored. We're assuming no holes in the
33 The format for the counter_names.tdb file is:
40 even_num perf_counter<even_num>
41 even_num+1 perf_counter<even_num>_help
43 So last_counter becomes num_counters*2, and last_help will be last_counter+1 */
45 kbuf
.dsize
= strlen(key
);
46 dbuf
= tdb_fetch(names
, kbuf
);
49 DEBUG(1, ("reg_perfcount_get_base_index: failed to find key \'1\' in [%s].\n", fname
));
56 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
57 memcpy(buf
, dbuf
.dptr
, dbuf
.dsize
);
58 retval
= (uint32
)atoi(buf
);
65 uint32
reg_perfcount_get_last_counter(uint32 base_index
)
72 retval
= base_index
* 2;
77 uint32
reg_perfcount_get_last_help(uint32 last_counter
)
84 retval
= last_counter
+ 1;
89 static uint32
_reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT
*tdb
,
96 char *buf1
= *retbuf
, *buf2
= NULL
;
97 uint32 working_size
= 0;
98 UNISTR2 name_index
, name
;
100 memset(temp
, 0, sizeof(temp
));
101 snprintf(temp
, sizeof(temp
), "%d", keyval
);
103 kbuf
.dsize
= strlen(temp
);
104 dbuf
= tdb_fetch(tdb
, kbuf
);
105 if(dbuf
.dptr
== NULL
)
107 /* If a key isn't there, just bypass it -- this really shouldn't
108 happen unless someone's mucking around with the tdb */
109 DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
113 /* First encode the name_index */
114 working_size
= (kbuf
.dsize
+ 1)*sizeof(uint16
);
115 buf2
= SMB_REALLOC(buf1
, buffer_size
+ working_size
);
123 init_unistr2(&name_index
, kbuf
.dptr
, UNI_STR_TERMINATE
);
124 memcpy(buf1
+buffer_size
, (char *)name_index
.buffer
, working_size
);
125 buffer_size
+= working_size
;
126 /* Now encode the actual name */
127 working_size
= (dbuf
.dsize
+ 1)*sizeof(uint16
);
128 buf2
= SMB_REALLOC(buf1
, buffer_size
+ working_size
);
136 memset(temp
, 0, sizeof(temp
));
137 memcpy(temp
, dbuf
.dptr
, dbuf
.dsize
);
138 SAFE_FREE(dbuf
.dptr
);
139 init_unistr2(&name
, temp
, UNI_STR_TERMINATE
);
140 memcpy(buf1
+buffer_size
, (char *)name
.buffer
, working_size
);
141 buffer_size
+= working_size
;
148 uint32
reg_perfcount_get_counter_help(uint32 base_index
, char **retbuf
)
150 char *buf1
= NULL
, *buf2
= NULL
;
151 uint32 buffer_size
= 0;
159 pstrcpy(fname
, lp_counters_dir());
160 pstrcat(fname
, "/names.tdb");
162 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
166 DEBUG(1, ("reg_perfcount_get_counter_help: unable to open [%s].\n", fname
));
170 for(i
= 1; i
<= base_index
; i
++)
172 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, (i
*2)+1, retbuf
, buffer_size
);
176 /* Now terminate the MULTI_SZ with a double unicode NULL */
178 buf2
= SMB_REALLOC(buf1
, buffer_size
+ 2);
187 buf1
[buffer_size
++] = '\0';
188 buf1
[buffer_size
++] = '\0';
196 uint32
reg_perfcount_get_counter_names(uint32 base_index
, char **retbuf
)
198 char *buf1
= NULL
, *buf2
= NULL
;
199 uint32 buffer_size
= 0;
207 pstrcpy(fname
, lp_counters_dir());
208 pstrcat(fname
, "/names.tdb");
210 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
214 DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname
));
218 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, 1, retbuf
, buffer_size
);
220 for(i
= 1; i
<= base_index
; i
++)
222 buffer_size
= _reg_perfcount_multi_sz_from_tdb(names
, i
*2, retbuf
, buffer_size
);
226 /* Now terminate the MULTI_SZ with a double unicode NULL */
228 buf2
= SMB_REALLOC(buf1
, buffer_size
+ 2);
237 buf1
[buffer_size
++] = '\0';
238 buf1
[buffer_size
++] = '\0';
246 static void _reg_perfcount_make_key(TDB_DATA
*key
,
250 const char *key_part2
)
252 memset(buf
, 0, buflen
);
253 if(key_part2
!= NULL
)
254 snprintf(buf
, buflen
,"%d%s", key_part1
, key_part2
);
256 snprintf(buf
, buflen
, "%d", key_part1
);
259 key
->dsize
= strlen(buf
);
264 static BOOL
_reg_perfcount_isparent(TDB_DATA data
)
268 if(data
.dptr
[0] == 'p')
276 static BOOL
_reg_perfcount_ischild(TDB_DATA data
)
280 if(data
.dptr
[0] == 'c')
288 static uint32
_reg_perfcount_get_numinst(int objInd
, TDB_CONTEXT
*names
)
291 char buf
[PERFCOUNT_MAX_LEN
];
293 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, objInd
, "inst");
294 data
= tdb_fetch(names
, key
);
296 if(data
.dptr
== NULL
)
297 return (uint32
)PERF_NO_INSTANCES
;
299 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
300 memcpy(buf
, data
.dptr
, data
.dsize
);
301 return (uint32
)atoi(buf
);
304 static BOOL
_reg_perfcount_add_object(PERF_DATA_BLOCK
*block
,
311 BOOL success
= False
;
312 PERF_OBJECT_TYPE
*obj
;
314 block
->objects
= (PERF_OBJECT_TYPE
*)TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
317 block
->NumObjectTypes
+1);
318 if(block
->objects
== NULL
)
320 obj
= &(block
->objects
[block
->NumObjectTypes
]);
321 memset((void *)&(block
->objects
[block
->NumObjectTypes
]), 0, sizeof(PERF_OBJECT_TYPE
));
322 block
->objects
[block
->NumObjectTypes
].ObjectNameTitleIndex
= num
;
323 block
->objects
[block
->NumObjectTypes
].ObjectNameTitlePointer
= 0;
324 block
->objects
[block
->NumObjectTypes
].ObjectHelpTitleIndex
= num
+1;
325 block
->objects
[block
->NumObjectTypes
].ObjectHelpTitlePointer
= 0;
326 block
->objects
[block
->NumObjectTypes
].NumCounters
= 0;
327 block
->objects
[block
->NumObjectTypes
].DefaultCounter
= 0;
328 block
->objects
[block
->NumObjectTypes
].NumInstances
= _reg_perfcount_get_numinst(num
, names
);
329 block
->objects
[block
->NumObjectTypes
].counters
= NULL
;
330 block
->objects
[block
->NumObjectTypes
].instances
= NULL
;
331 block
->objects
[block
->NumObjectTypes
].counter_data
.ByteLength
= sizeof(uint32
);
332 block
->objects
[block
->NumObjectTypes
].counter_data
.data
= NULL
;
333 block
->objects
[block
->NumObjectTypes
].DetailLevel
= PERF_DETAIL_NOVICE
;
334 block
->NumObjectTypes
+=1;
336 for(i
= 0; i
< (int)obj
->NumInstances
; i
++)
338 success
= _reg_perfcount_add_instance(obj
, ps
, i
, names
);
344 BOOL
_reg_perfcount_get_counter_data(TDB_DATA key
, TDB_DATA
*data
)
346 TDB_CONTEXT
*counters
;
350 pstrcpy(fname
, lp_counters_dir());
351 pstrcat(fname
, "/data.tdb");
353 counters
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
357 DEBUG(1, ("reg_perfcount_get_counter_data: unable to open [%s].\n", fname
));
361 *data
= tdb_fetch(counters
, key
);
368 static uint32
_reg_perfcount_get_size_field(uint32 CounterType
)
372 retval
= CounterType
;
374 /* First mask out reserved lower 8 bits */
375 retval
= retval
& 0xFFFFFF00;
376 retval
= retval
<< 22;
377 retval
= retval
>> 22;
382 static uint32
_reg_perfcount_compute_scale(long long int data
)
398 return (uint32
)scale
;
401 static BOOL
_reg_perfcount_get_counter_info(PERF_DATA_BLOCK
*block
,
404 PERF_OBJECT_TYPE
*obj
,
408 char buf
[PERFCOUNT_MAX_LEN
];
409 size_t dsize
, padding
;
410 long int data32
, dbuf
[2];
411 long long int data64
;
414 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
415 dbuf
[0] = dbuf
[1] = 0;
418 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, CounterIndex
, "type");
419 data
= tdb_fetch(names
, key
);
420 if(data
.dptr
== NULL
)
422 DEBUG(3, ("_reg_perfcount_get_counter_info: No type data for counter [%d].\n", CounterIndex
));
425 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
426 memcpy(buf
, data
.dptr
, data
.dsize
);
427 obj
->counters
[obj
->NumCounters
].CounterType
= atoi(buf
);
428 DEBUG(10, ("_reg_perfcount_get_counter_info: Got type [%d] for counter [%d].\n",
429 obj
->counters
[obj
->NumCounters
].CounterType
, CounterIndex
));
432 /* Fetch the actual data */
433 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, CounterIndex
, "");
434 _reg_perfcount_get_counter_data(key
, &data
);
435 if(data
.dptr
== NULL
)
437 DEBUG(3, ("_reg_perfcount_get_counter_info: No counter data for counter [%d].\n", CounterIndex
));
441 counter_size
= _reg_perfcount_get_size_field(obj
->counters
[obj
->NumCounters
].CounterType
);
443 if(counter_size
== PERF_SIZE_DWORD
)
445 dsize
= sizeof(data32
);
446 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
447 memcpy(buf
, data
.dptr
, data
.dsize
);
448 data32
= strtol(buf
, NULL
, 0);
449 if((obj
->counters
[obj
->NumCounters
].CounterType
& 0x00000F00) == PERF_TYPE_NUMBER
)
450 obj
->counters
[obj
->NumCounters
].DefaultScale
= _reg_perfcount_compute_scale((long long int)data32
);
452 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
454 padding
= (dsize
- (obj
->counter_data
.ByteLength
%dsize
)) % dsize
;
456 else if(counter_size
== PERF_SIZE_LARGE
)
458 dsize
= sizeof(data64
);
459 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
460 memcpy(buf
, data
.dptr
, data
.dsize
);
461 data64
= strtoll(buf
, NULL
, 0);
462 if((obj
->counters
[obj
->NumCounters
].CounterType
& 0x00000F00) == PERF_TYPE_NUMBER
)
463 obj
->counters
[obj
->NumCounters
].DefaultScale
= _reg_perfcount_compute_scale(data64
);
465 obj
->counters
[obj
->NumCounters
].DefaultScale
= 0;
466 memcpy((void *)dbuf
, (const void *)&data64
, dsize
);
467 padding
= (dsize
- (obj
->counter_data
.ByteLength
%dsize
)) % dsize
;
469 else /* PERF_SIZE_VARIABLE_LEN */
472 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
473 memcpy(buf
, data
.dptr
, data
.dsize
);
477 obj
->counter_data
.ByteLength
+= dsize
+ padding
;
478 obj
->counter_data
.data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
479 obj
->counter_data
.data
,
481 obj
->counter_data
.ByteLength
- sizeof(uint32
));
482 if(obj
->counter_data
.data
== NULL
)
484 if(dbuf
[0] != 0 || dbuf
[1] != 0)
486 memcpy((void *)(obj
->counter_data
.data
+
487 (obj
->counter_data
.ByteLength
- (sizeof(uint32
) + dsize
))),
488 (const void *)dbuf
, dsize
);
492 /* Handling PERF_SIZE_VARIABLE_LEN */
493 memcpy((void *)(obj
->counter_data
.data
+
494 (obj
->counter_data
.ByteLength
- (sizeof(uint32
) + dsize
))),
495 (const void *)buf
, dsize
);
497 obj
->counters
[obj
->NumCounters
].CounterOffset
= obj
->counter_data
.ByteLength
- dsize
;
498 if(obj
->counters
[obj
->NumCounters
].CounterOffset
% dsize
!= 0)
500 DEBUG(3,("Improperly aligned counter [%d]\n", obj
->NumCounters
));
502 obj
->counters
[obj
->NumCounters
].CounterSize
= dsize
;
507 PERF_OBJECT_TYPE
*_reg_perfcount_find_obj(PERF_DATA_BLOCK
*block
, int objind
)
511 PERF_OBJECT_TYPE
*obj
= NULL
;
513 for(i
= 0; i
< block
->NumObjectTypes
; i
++)
515 if(block
->objects
[i
].ObjectNameTitleIndex
== objind
)
517 obj
= &(block
->objects
[i
]);
524 static BOOL
_reg_perfcount_add_counter(PERF_DATA_BLOCK
*block
,
530 char *begin
, *end
, *start
, *stop
;
532 PERF_OBJECT_TYPE
*obj
;
533 BOOL success
= False
;
534 char buf
[PERFCOUNT_MAX_LEN
];
537 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
538 memcpy(buf
, data
.dptr
, data
.dsize
);
539 begin
= index(buf
, '[');
540 end
= index(buf
, ']');
541 if(begin
== NULL
|| end
== NULL
)
547 stop
= index(start
, ',');
551 parent
= atoi(start
);
553 obj
= _reg_perfcount_find_obj(block
, parent
);
556 /* At this point we require that the parent object exist.
557 This can probably be handled better at some later time */
558 DEBUG(3, ("_reg_perfcount_add_counter: Could not find parent object [%d] for counter [%d].\n",
562 obj
->counters
= (PERF_COUNTER_DEFINITION
*)TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
564 PERF_COUNTER_DEFINITION
,
566 if(obj
->counters
== NULL
)
568 memset((void *)&(obj
->counters
[obj
->NumCounters
]), 0, sizeof(PERF_COUNTER_DEFINITION
));
569 obj
->counters
[obj
->NumCounters
].CounterNameTitleIndex
=num
;
570 obj
->counters
[obj
->NumCounters
].CounterHelpTitleIndex
=num
+1;
571 obj
->counters
[obj
->NumCounters
].DetailLevel
= PERF_DETAIL_NOVICE
;
572 obj
->counters
[obj
->NumCounters
].ByteLength
= sizeof(PERF_COUNTER_DEFINITION
);
573 success
= _reg_perfcount_get_counter_info(block
, ps
, num
, obj
, names
);
574 obj
->NumCounters
+= 1;
578 /* Handle case of Objects/Counters without any counter data, which would suggest
579 that the required instances are not there yet, so change NumInstances from
580 PERF_NO_INSTANCES to 0 */
585 BOOL
_reg_perfcount_get_instance_info(PERF_INSTANCE_DEFINITION
*inst
,
588 PERF_OBJECT_TYPE
*obj
,
592 char buf
[PERFCOUNT_MAX_LEN
], temp
[PERFCOUNT_MAX_LEN
];
596 /* First grab the instance data from the data file */
597 memset(temp
, 0, PERFCOUNT_MAX_LEN
);
598 snprintf(temp
, PERFCOUNT_MAX_LEN
, "i%d", instId
);
599 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, obj
->ObjectNameTitleIndex
, temp
);
600 _reg_perfcount_get_counter_data(key
, &data
);
601 if(data
.dptr
== NULL
)
603 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance data for instance [%s].\n",
607 inst
->counter_data
.ByteLength
= data
.dsize
+ sizeof(inst
->counter_data
.ByteLength
);
608 inst
->counter_data
.data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
609 inst
->counter_data
.data
,
612 if(inst
->counter_data
.data
== NULL
)
614 memset(inst
->counter_data
.data
, 0, data
.dsize
);
615 memcpy(inst
->counter_data
.data
, data
.dptr
, data
.dsize
);
618 /* Fetch instance name */
619 memset(temp
, 0, PERFCOUNT_MAX_LEN
);
620 snprintf(temp
, PERFCOUNT_MAX_LEN
, "i%dname", instId
);
621 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, obj
->ObjectNameTitleIndex
, temp
);
622 data
= tdb_fetch(names
, key
);
623 if(data
.dptr
== NULL
)
625 /* Not actually an error, but possibly unintended? -- just logging FYI */
626 DEBUG(3, ("_reg_perfcount_get_instance_info: No instance name for instance [%s].\n",
628 inst
->NameLength
= 0;
632 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
633 memcpy(buf
, data
.dptr
, data
.dsize
);
634 rpcstr_push((void *)name
, buf
, sizeof(name
), STR_TERMINATE
);
635 inst
->NameLength
= (strlen_w(name
) * 2) + 2;
636 inst
->data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
640 memcpy(inst
->data
, name
, inst
->NameLength
);
644 inst
->ParentObjectTitleIndex
= 0;
645 inst
->ParentObjectTitlePointer
= 0;
646 inst
->UniqueID
= PERF_NO_UNIQUE_ID
;
647 inst
->NameOffset
= 6 * sizeof(uint32
);
649 inst
->ByteLength
= inst
->NameOffset
+ inst
->NameLength
;
650 /* Need to be aligned on a 64-bit boundary here for counter_data */
651 if((pad
= (inst
->ByteLength
% 8)))
654 inst
->data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
657 inst
->NameLength
+ pad
);
658 memset(inst
->data
+ inst
->NameLength
, 0, pad
);
659 inst
->ByteLength
+= pad
;
665 BOOL
_reg_perfcount_add_instance(PERF_OBJECT_TYPE
*obj
,
671 PERF_INSTANCE_DEFINITION
*inst
;
675 if(obj
->instances
== NULL
)
677 obj
->instances
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
679 PERF_INSTANCE_DEFINITION
,
682 if(obj
->instances
== NULL
)
685 memset(&(obj
->instances
[instInd
]), 0, sizeof(PERF_INSTANCE_DEFINITION
));
686 inst
= &(obj
->instances
[instInd
]);
687 success
= _reg_perfcount_get_instance_info(inst
, ps
, instInd
, obj
, names
);
692 static int _reg_perfcount_assemble_global(PERF_DATA_BLOCK
*block
,
698 int i
, j
, retval
= 0;
699 char keybuf
[PERFCOUNT_MAX_LEN
];
702 for(i
= 1; i
<= base_index
; i
++)
705 _reg_perfcount_make_key(&key
, keybuf
, PERFCOUNT_MAX_LEN
, j
, "rel");
706 data
= tdb_fetch(names
, key
);
707 if(data
.dptr
!= NULL
)
709 if(_reg_perfcount_isparent(data
))
710 success
= _reg_perfcount_add_object(block
, ps
, j
, data
, names
);
711 else if(_reg_perfcount_ischild(data
))
712 success
= _reg_perfcount_add_counter(block
, ps
, j
, data
, names
);
715 DEBUG(3, ("Bogus relationship [%s] for counter [%d].\n", data
.dptr
, j
));
720 DEBUG(3, ("_reg_perfcount_assemble_global: Failed to add new relationship for counter [%d].\n", j
));
726 DEBUG(3, ("NULL relationship for counter [%d] using key [%s].\n", j
, keybuf
));
731 static BOOL
_reg_perfcount_get_64(unsigned long long *retval
,
734 const char *key_part2
)
737 char buf
[PERFCOUNT_MAX_LEN
];
739 _reg_perfcount_make_key(&key
, buf
, PERFCOUNT_MAX_LEN
, key_part1
, key_part2
);
741 data
= tdb_fetch(tdb
, key
);
742 if(data
.dptr
== NULL
)
744 DEBUG(3,("_reg_perfcount_get_64: No data found for key [%s].\n", key
.dptr
));
748 memset(buf
, 0, PERFCOUNT_MAX_LEN
);
749 memcpy(buf
, data
.dptr
, data
.dsize
);
752 *retval
= strtoll(buf
, NULL
, 0);
757 static BOOL
_reg_perfcount_init_data_block_perf(PERF_DATA_BLOCK
*block
,
760 unsigned long long PerfFreq
, PerfTime
, PerfTime100nSec
;
761 TDB_CONTEXT
*counters
;
767 pstrcpy(fname
, lp_counters_dir());
768 pstrcat(fname
, "/data.tdb");
770 counters
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
774 DEBUG(1, ("reg_perfcount_init_data_block_perf: unable to open [%s].\n", fname
));
778 status
= _reg_perfcount_get_64(&PerfFreq
, names
, 0, "PerfFreq");
784 memcpy((void *)&(block
->PerfFreq
), (const void *)&PerfFreq
, sizeof(PerfFreq
));
786 status
= _reg_perfcount_get_64(&PerfTime
, counters
, 0, "PerfTime");
792 memcpy((void *)&(block
->PerfTime
), (const void *)&PerfTime
, sizeof(PerfTime
));
794 status
= _reg_perfcount_get_64(&PerfTime100nSec
, counters
, 0, "PerfTime100nSec");
800 memcpy((void *)&(block
->PerfTime100nSec
), (const void *)&PerfTime100nSec
, sizeof(PerfTime100nSec
));
806 static void _reg_perfcount_init_data_block(PERF_DATA_BLOCK
*block
, prs_struct
*ps
, TDB_CONTEXT
*names
)
811 memset(temp
, 0, sizeof(temp
));
812 rpcstr_push((void *)temp
, "PERF", sizeof(temp
), STR_TERMINATE
);
813 memcpy(block
->Signature
, temp
, strlen_w(temp
) *2);
815 if(ps
->bigendian_data
== RPC_BIG_ENDIAN
)
816 block
->LittleEndian
= 0;
818 block
->LittleEndian
= 1;
821 block
->TotalByteLength
= 0;
822 block
->NumObjectTypes
= 0;
823 block
->DefaultObject
= -1;
824 block
->objects
= NULL
;
826 make_systemtime(&(block
->SystemTime
), gmtime(&tm
));
827 _reg_perfcount_init_data_block_perf(block
, names
);
828 memset(temp
, 0, sizeof(temp
));
829 rpcstr_push((void *)temp
, global_myname(), sizeof(temp
), STR_TERMINATE
);
830 block
->SystemNameLength
= (strlen_w(temp
) * 2) + 2;
831 block
->data
= TALLOC_ZERO_ARRAY(ps
->mem_ctx
, uint8
, block
->SystemNameLength
+ (8 - (block
->SystemNameLength
% 8)));
832 memcpy(block
->data
, temp
, block
->SystemNameLength
);
833 block
->SystemNameOffset
= sizeof(PERF_DATA_BLOCK
) - sizeof(block
->objects
) - sizeof(block
->data
);
834 block
->HeaderLength
= block
->SystemNameOffset
+ block
->SystemNameLength
;
835 /* Make sure to adjust for 64-bit alignment for when we finish writing the system name,
836 so that the PERF_OBJECT_TYPE struct comes out 64-bit aligned */
837 block
->HeaderLength
+= 8 - (block
->HeaderLength
% 8);
842 static uint32
_reg_perfcount_perf_data_block_fixup(PERF_DATA_BLOCK
*block
, prs_struct
*ps
)
844 int obj
, cnt
, inst
, pad
, i
;
845 PERF_OBJECT_TYPE
*object
;
846 PERF_INSTANCE_DEFINITION
*instance
;
847 PERF_COUNTER_DEFINITION
*counter
;
848 PERF_COUNTER_BLOCK
*counter_data
;
849 char *temp
= NULL
, *src_addr
, *dst_addr
;
851 block
->TotalByteLength
= 0;
852 object
= block
->objects
;
853 for(obj
= 0; obj
< block
->NumObjectTypes
; obj
++)
855 object
[obj
].TotalByteLength
= 0;
856 object
[obj
].DefinitionLength
= 0;
857 instance
= object
[obj
].instances
;
858 counter
= object
[obj
].counters
;
859 for(cnt
= 0; cnt
< object
[obj
].NumCounters
; cnt
++)
861 object
[obj
].TotalByteLength
+= counter
[cnt
].ByteLength
;
862 object
[obj
].DefinitionLength
+= counter
[cnt
].ByteLength
;
864 if(object
[obj
].NumInstances
!= PERF_NO_INSTANCES
)
866 for(inst
= 0; inst
< object
[obj
].NumInstances
; inst
++)
868 instance
= &(object
[obj
].instances
[inst
]);
869 object
[obj
].TotalByteLength
+= instance
->ByteLength
;
870 counter_data
= &(instance
->counter_data
);
871 counter
= &(object
[obj
].counters
[object
[obj
].NumCounters
- 1]);
872 counter_data
->ByteLength
= counter
->CounterOffset
+ counter
->CounterSize
+ sizeof(counter_data
->ByteLength
);
873 temp
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
876 counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
877 memset(temp
, 0, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
878 src_addr
= counter_data
->data
;
879 for(i
= 0; i
< object
[obj
].NumCounters
; i
++)
881 counter
= &(object
[obj
].counters
[i
]);
882 dst_addr
= temp
+ counter
->CounterOffset
- sizeof(counter_data
->ByteLength
);
883 memcpy(dst_addr
, src_addr
, counter
->CounterSize
);
884 src_addr
+= counter
->CounterSize
;
886 /* Make sure to be 64-bit aligned */
887 if((pad
= (counter_data
->ByteLength
% 8)))
891 counter_data
->data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
894 counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
) + pad
);
895 memset(counter_data
->data
, 0, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
) + pad
);
896 memcpy(counter_data
->data
, temp
, counter_data
->ByteLength
- sizeof(counter_data
->ByteLength
));
897 counter_data
->ByteLength
+= pad
;
898 object
[obj
].TotalByteLength
+= counter_data
->ByteLength
;
903 /* Need to be 64-bit aligned at the end of the counter_data block, so pad counter_data to a 64-bit boundary,
904 so that the next PERF_OBJECT_TYPE can start on a 64-bit alignment */
905 if((pad
= (object
[obj
].counter_data
.ByteLength
% 8)))
908 object
[obj
].counter_data
.data
= TALLOC_REALLOC_ARRAY(ps
->mem_ctx
,
909 object
[obj
].counter_data
.data
,
911 object
[obj
].counter_data
.ByteLength
+ pad
);
912 memset((void *)(object
[obj
].counter_data
.data
+ object
[obj
].counter_data
.ByteLength
), 0, pad
);
913 object
[obj
].counter_data
.ByteLength
+= pad
;
915 object
[obj
].TotalByteLength
+= object
[obj
].counter_data
.ByteLength
;
917 object
[obj
].HeaderLength
= sizeof(*object
) - (sizeof(counter
) + sizeof(instance
) + sizeof(PERF_COUNTER_BLOCK
));
918 object
[obj
].TotalByteLength
+= object
[obj
].HeaderLength
;
919 object
[obj
].DefinitionLength
+= object
[obj
].HeaderLength
;
921 block
->TotalByteLength
+= object
[obj
].TotalByteLength
;
924 return block
->TotalByteLength
;
927 uint32
reg_perfcount_get_perf_data_block(uint32 base_index
,
929 PERF_DATA_BLOCK
*block
,
932 uint32 buffer_size
= 0, last_counter
;
937 pstrcpy(fname
, lp_counters_dir());
938 pstrcat(fname
, "/names.tdb");
940 names
= tdb_open_log(fname
, 0, TDB_DEFAULT
, O_RDONLY
, 0444);
944 DEBUG(1, ("reg_perfcount_get_perf_data_block: unable to open [%s].\n", fname
));
948 _reg_perfcount_init_data_block(block
, ps
, names
);
950 last_counter
= reg_perfcount_get_last_counter(base_index
);
952 if(object_ids
== NULL
)
954 /* we're getting a request for "Global" here */
955 retval
= _reg_perfcount_assemble_global(block
, ps
, base_index
, names
);
959 /* we're getting a request for a specific set of PERF_OBJECT_TYPES */
960 retval
= _reg_perfcount_assemble_global(block
, ps
, base_index
, names
);
962 buffer_size
= _reg_perfcount_perf_data_block_fixup(block
, ps
);
966 return buffer_size
+ block
->HeaderLength
;
969 static BOOL
_reg_perfcount_marshall_perf_data_block(prs_struct
*ps
, PERF_DATA_BLOCK block
, int depth
)
972 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_data_block");
977 for(i
= 0; i
< 4; i
++)
979 if(!prs_uint16("Signature", ps
, depth
, &block
.Signature
[i
]))
982 if(!prs_uint32("Little Endian", ps
, depth
, &block
.LittleEndian
))
984 if(!prs_uint32("Version", ps
, depth
, &block
.Version
))
986 if(!prs_uint32("Revision", ps
, depth
, &block
.Revision
))
988 if(!prs_uint32("TotalByteLength", ps
, depth
, &block
.TotalByteLength
))
990 if(!prs_uint32("HeaderLength", ps
, depth
, &block
.HeaderLength
))
992 if(!prs_uint32("NumObjectTypes", ps
, depth
, &block
.NumObjectTypes
))
994 if(!prs_uint32("DefaultObject", ps
, depth
, &block
.DefaultObject
))
996 if(!spoolss_io_system_time("SystemTime", ps
, depth
, &block
.SystemTime
))
998 if(!prs_uint32("Padding", ps
, depth
, &block
.Padding
))
1000 if(!prs_align_uint64(ps
))
1002 if(!prs_uint64("PerfTime", ps
, depth
, &block
.PerfTime
))
1004 if(!prs_uint64("PerfFreq", ps
, depth
, &block
.PerfFreq
))
1006 if(!prs_uint64("PerfTime100nSec", ps
, depth
, &block
.PerfTime100nSec
))
1008 if(!prs_uint32("SystemNameLength", ps
, depth
, &block
.SystemNameLength
))
1010 if(!prs_uint32("SystemNameOffset", ps
, depth
, &block
.SystemNameOffset
))
1012 /* hack to make sure we're 64-bit aligned at the end of this whole mess */
1013 if(!prs_uint8s(False
, "SystemName", ps
, depth
, block
.data
,
1014 block
.HeaderLength
- block
.SystemNameOffset
))
1020 static BOOL
_reg_perfcount_marshall_perf_counters(prs_struct
*ps
,
1021 PERF_OBJECT_TYPE object
,
1025 PERF_COUNTER_DEFINITION counter
;
1027 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_counters");
1030 for(cnt
= 0; cnt
< object
.NumCounters
; cnt
++)
1032 counter
= object
.counters
[cnt
];
1036 if(!prs_uint32("ByteLength", ps
, depth
, &counter
.ByteLength
))
1038 if(!prs_uint32("CounterNameTitleIndex", ps
, depth
, &counter
.CounterNameTitleIndex
))
1040 if(!prs_uint32("CounterNameTitlePointer", ps
, depth
, &counter
.CounterNameTitlePointer
))
1042 if(!prs_uint32("CounterHelpTitleIndex", ps
, depth
, &counter
.CounterHelpTitleIndex
))
1044 if(!prs_uint32("CounterHelpTitlePointer", ps
, depth
, &counter
.CounterHelpTitlePointer
))
1046 if(!prs_uint32("DefaultScale", ps
, depth
, &counter
.DefaultScale
))
1048 if(!prs_uint32("DetailLevel", ps
, depth
, &counter
.DetailLevel
))
1050 if(!prs_uint32("CounterType", ps
, depth
, &counter
.CounterType
))
1052 if(!prs_uint32("CounterSize", ps
, depth
, &counter
.CounterSize
))
1054 if(!prs_uint32("CounterOffset", ps
, depth
, &counter
.CounterOffset
))
1061 static BOOL
_reg_perfcount_marshall_perf_counter_data(prs_struct
*ps
,
1062 PERF_COUNTER_BLOCK counter_data
,
1065 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_counter_data");
1068 if(!prs_align_uint64(ps
))
1071 if(!prs_uint32("ByteLength", ps
, depth
, &counter_data
.ByteLength
))
1073 if(!prs_uint8s(False
, "CounterData", ps
, depth
, counter_data
.data
, counter_data
.ByteLength
- sizeof(uint32
)))
1075 if(!prs_align_uint64(ps
))
1081 static BOOL
_reg_perfcount_marshall_perf_instances(prs_struct
*ps
,
1082 PERF_OBJECT_TYPE object
,
1085 PERF_INSTANCE_DEFINITION instance
;
1088 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_instances");
1091 for(inst
= 0; inst
< object
.NumInstances
; inst
++)
1093 instance
= object
.instances
[inst
];
1097 if(!prs_uint32("ByteLength", ps
, depth
, &instance
.ByteLength
))
1099 if(!prs_uint32("ParentObjectTitleIndex", ps
, depth
, &instance
.ParentObjectTitleIndex
))
1101 if(!prs_uint32("ParentObjectTitlePointer", ps
, depth
, &instance
.ParentObjectTitlePointer
))
1103 if(!prs_uint32("UniqueID", ps
, depth
, &instance
.UniqueID
))
1105 if(!prs_uint32("NameOffset", ps
, depth
, &instance
.NameOffset
))
1107 if(!prs_uint32("NameLength", ps
, depth
, &instance
.NameLength
))
1109 if(!prs_uint8s(False
, "InstanceName", ps
, depth
, instance
.data
,
1110 instance
.ByteLength
- instance
.NameOffset
))
1112 if(_reg_perfcount_marshall_perf_counter_data(ps
, instance
.counter_data
, depth
) == False
)
1119 static BOOL
_reg_perfcount_marshall_perf_objects(prs_struct
*ps
, PERF_DATA_BLOCK block
, int depth
)
1123 PERF_OBJECT_TYPE object
;
1125 prs_debug(ps
, depth
, "", "_reg_perfcount_marshall_perf_objects");
1128 for(obj
= 0; obj
< block
.NumObjectTypes
; obj
++)
1130 object
= block
.objects
[obj
];
1135 if(!prs_uint32("TotalByteLength", ps
, depth
, &object
.TotalByteLength
))
1137 if(!prs_uint32("DefinitionLength", ps
, depth
, &object
.DefinitionLength
))
1139 if(!prs_uint32("HeaderLength", ps
, depth
, &object
.HeaderLength
))
1141 if(!prs_uint32("ObjectNameTitleIndex", ps
, depth
, &object
.ObjectNameTitleIndex
))
1143 if(!prs_uint32("ObjectNameTitlePointer", ps
, depth
, &object
.ObjectNameTitlePointer
))
1145 if(!prs_uint32("ObjectHelpTitleIndex", ps
, depth
, &object
.ObjectHelpTitleIndex
))
1147 if(!prs_uint32("ObjectHelpTitlePointer", ps
, depth
, &object
.ObjectHelpTitlePointer
))
1149 if(!prs_uint32("DetailLevel", ps
, depth
, &object
.DetailLevel
))
1151 if(!prs_uint32("NumCounters", ps
, depth
, &object
.NumCounters
))
1153 if(!prs_uint32("DefaultCounter", ps
, depth
, &object
.DefaultCounter
))
1155 if(!prs_uint32("NumInstances", ps
, depth
, &object
.NumInstances
))
1157 if(!prs_uint32("CodePage", ps
, depth
, &object
.CodePage
))
1159 if(!prs_align_uint64(ps
))
1161 if(!prs_uint64("PerfTime", ps
, depth
, &object
.PerfTime
))
1163 if(!prs_uint64("PerfFreq", ps
, depth
, &object
.PerfFreq
))
1166 /* Now do the counters */
1167 /* If no instances, encode counter_data */
1168 /* If instances, encode instace plus counter data for each instance */
1169 if(_reg_perfcount_marshall_perf_counters(ps
, object
, depth
) == False
)
1171 if(object
.NumInstances
== PERF_NO_INSTANCES
)
1173 if(_reg_perfcount_marshall_perf_counter_data(ps
, object
.counter_data
, depth
) == False
)
1178 if(_reg_perfcount_marshall_perf_instances(ps
, object
, depth
) == False
)
1186 static BOOL
_reg_perfcount_marshall_hkpd(prs_struct
*ps
, PERF_DATA_BLOCK block
)
1189 if(_reg_perfcount_marshall_perf_data_block(ps
, block
, depth
) == True
)
1191 if(_reg_perfcount_marshall_perf_objects(ps
, block
, depth
) == True
)
1196 WERROR
reg_perfcount_get_hkpd(prs_struct
*ps
, uint32 max_buf_size
, uint32
*outbuf_len
, char *object_ids
)
1199 * For a detailed description of the layout of this structure,
1200 * see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/performance_data_format.asp
1202 PERF_DATA_BLOCK block
;
1203 uint32 buffer_size
, base_index
;
1206 base_index
= reg_perfcount_get_base_index();
1209 buffer_size
= reg_perfcount_get_perf_data_block(base_index
, ps
, &block
, object_ids
);
1211 if(buffer_size
< max_buf_size
)
1213 *outbuf_len
= buffer_size
;
1214 if(_reg_perfcount_marshall_hkpd(ps
, block
) == True
)
1221 *outbuf_len
= max_buf_size
;
1222 _reg_perfcount_marshall_perf_data_block(ps
, block
, 0);
1223 return WERR_INSUFFICIENT_BUFFER
;