ctdb statistics information
*/
#define MAX_COUNT_BUCKETS 16
+#define MAX_HOT_KEYS 10
struct ctdb_statistics {
uint32_t num_clients;
/*
* db statistics
*/
+struct ctdb_db_hot_key {
+ uint32_t count;
+ TDB_DATA key;
+};
struct ctdb_db_statistics {
uint32_t db_ro_delegations;
uint32_t db_ro_revokes;
uint32_t hop_count_bucket[MAX_COUNT_BUCKETS];
+ uint32_t num_hot_keys;
+ struct ctdb_db_hot_key hot_keys[MAX_HOT_KEYS];
+};
+struct ctdb_db_statistics_wire {
+ uint32_t db_ro_delegations;
+ uint32_t db_ro_revokes;
+ uint32_t hop_count_bucket[MAX_COUNT_BUCKETS];
+ uint32_t num_hot_keys;
+ char hot_keys[1];
};
/*
{
struct ctdb_reply_control *reply;
struct ctdb_db_statistics *s;
+ struct ctdb_db_statistics_wire *wire;
+ int i;
+ char *ptr;
reply = unpack_reply_control(req, CTDB_CONTROL_GET_DB_STATISTICS);
if (!reply) {
DEBUG(ctdb, LOG_ERR, "ctdb_getpnn_recv: status -1");
return false;
}
- if (reply->datalen != sizeof(struct ctdb_db_statistics)) {
- DEBUG(ctdb, LOG_ERR, "ctdb_getdbstat_recv: returned data is %d bytes but should be %d", reply->datalen, (int)sizeof(struct ctdb_db_statistics));
+ if (reply->datalen < offsetof(struct ctdb_db_statistics_wire, hot_keys)) {
+ DEBUG(ctdb, LOG_ERR, "ctdb_getdbstat_recv: returned data is %d bytes but should be >= %d", reply->datalen, (int)sizeof(struct ctdb_db_statistics));
return false;
}
- s = malloc(sizeof(struct ctdb_db_statistics));
+ wire = reply->data;
+
+ s = malloc(offsetof(struct ctdb_db_statistics, hot_keys) + sizeof(struct ctdb_db_hot_key) * wire->num_hot_keys);
if (!s) {
return false;
}
- memcpy(s, reply->data, sizeof(struct ctdb_db_statistics));
+ s->db_ro_delegations = wire->db_ro_delegations;
+ s->db_ro_revokes = wire->db_ro_revokes;
+ for (i = 0; i < MAX_COUNT_BUCKETS; i++) {
+ s->hop_count_bucket[i] = wire->hop_count_bucket[i];
+ }
+ s->num_hot_keys = wire->num_hot_keys;
+ ptr = &wire->hot_keys[0];
+ for (i = 0; i < wire->num_hot_keys; i++) {
+ s->hot_keys[i].count = *(uint32_t *)ptr;
+ ptr += 4;
+
+ s->hot_keys[i].key.dsize = *(uint32_t *)ptr;
+ ptr += 4;
+
+ s->hot_keys[i].key.dptr = malloc(s->hot_keys[i].key.dsize);
+ memcpy(s->hot_keys[i].key.dptr, ptr, s->hot_keys[i].key.dsize);
+ ptr += s->hot_keys[i].key.dsize;
+ }
+
*stat = s;
return true;
void ctdb_free_dbstat(struct ctdb_db_statistics *stat)
{
+ int i;
+
if (stat == NULL) {
return;
}
+
+ for (i = 0; i < stat->num_hot_keys; i++) {
+ if (stat->hot_keys[i].key.dptr != NULL) {
+ free(stat->hot_keys[i].key.dptr);
+ }
+ }
+
free(stat);
}
return 0;
}
+static void
+ctdb_update_db_stat_hot_keys(struct ctdb_db_context *ctdb_db, TDB_DATA key, int hopcount)
+{
+ int i;
+
+ /* smallest value is always at index 0 */
+ if (hopcount <= ctdb_db->statistics.hot_keys[0].count) {
+ return;
+ }
+
+ /* see if we already know this key */
+ for (i = 0; i < MAX_HOT_KEYS; i++) {
+ if (key.dsize != ctdb_db->statistics.hot_keys[i].key.dsize) {
+ continue;
+ }
+ if (memcmp(key.dptr, ctdb_db->statistics.hot_keys[i].key.dptr, key.dsize)) {
+ continue;
+ }
+ /* found an entry for this key */
+ if (hopcount <= ctdb_db->statistics.hot_keys[i].count) {
+ return;
+ }
+ ctdb_db->statistics.hot_keys[i].count = hopcount;
+ goto sort_keys;
+ }
+
+ if (ctdb_db->statistics.hot_keys[0].key.dptr != NULL) {
+ talloc_free(ctdb_db->statistics.hot_keys[0].key.dptr);
+ }
+ ctdb_db->statistics.hot_keys[0].key.dsize = key.dsize;
+ ctdb_db->statistics.hot_keys[0].key.dptr = talloc_memdup(ctdb_db, key.dptr, key.dsize);
+ ctdb_db->statistics.hot_keys[0].count = hopcount;
+
+
+sort_keys:
+ for (i = 2; i < MAX_HOT_KEYS; i++) {
+ if (ctdb_db->statistics.hot_keys[i].count < ctdb_db->statistics.hot_keys[0].count) {
+ hopcount = ctdb_db->statistics.hot_keys[i].count;
+ ctdb_db->statistics.hot_keys[i].count = ctdb_db->statistics.hot_keys[0].count;
+ ctdb_db->statistics.hot_keys[0].count = hopcount;
+
+ key = ctdb_db->statistics.hot_keys[i].key;
+ ctdb_db->statistics.hot_keys[i].key = ctdb_db->statistics.hot_keys[0].key;
+ ctdb_db->statistics.hot_keys[0].key = key;
+ }
+ }
+}
+
/*
called when a CTDB_REQ_CALL packet comes in
*/
}
CTDB_INCREMENT_STAT(ctdb, hop_count_bucket[bucket]);
CTDB_INCREMENT_DB_STAT(ctdb_db, hop_count_bucket[bucket]);
-
+ ctdb_update_db_stat_hot_keys(ctdb_db, call->key, c->hopcount);
/* If this database supports sticky records, then check if the
hopcount is big. If it is it means the record is hot and we
TDB_DATA *outdata)
{
struct ctdb_db_context *ctdb_db;
+ struct ctdb_db_statistics_wire *stats;
+ int i;
+ int len;
+ char *ptr;
ctdb_db = find_ctdb_db(ctdb, db_id);
if (!ctdb_db) {
return -1;
}
- outdata->dptr = (uint8_t *)&(ctdb_db->statistics);
- outdata->dsize = sizeof(ctdb_db->statistics);
+ len = offsetof(struct ctdb_db_statistics_wire, hot_keys);
+ for (i = 0; i < MAX_HOT_KEYS; i++) {
+ len += 8 + ctdb_db->statistics.hot_keys[i].key.dsize;
+ }
+
+ stats = talloc_size(outdata, len);
+ if (stats == NULL) {
+ DEBUG(DEBUG_ERR,("Failed to allocate db statistics wire structure\n"));
+ return -1;
+ }
+
+ stats->db_ro_delegations = ctdb_db->statistics.db_ro_delegations;
+ stats->db_ro_revokes = ctdb_db->statistics.db_ro_revokes;
+ for (i = 0; i < MAX_COUNT_BUCKETS; i++) {
+ stats->hop_count_bucket[i] = ctdb_db->statistics.hop_count_bucket[i];
+ }
+ stats->num_hot_keys = MAX_HOT_KEYS;
+
+ ptr = &stats->hot_keys[0];
+ for (i = 0; i < MAX_HOT_KEYS; i++) {
+ *(uint32_t *)ptr = ctdb_db->statistics.hot_keys[i].count;
+ ptr += 4;
+
+ *(uint32_t *)ptr = ctdb_db->statistics.hot_keys[i].key.dsize;
+ ptr += 4;
+
+ memcpy(ptr, ctdb_db->statistics.hot_keys[i].key.dptr, ctdb_db->statistics.hot_keys[i].key.dsize);
+ ptr += ctdb_db->statistics.hot_keys[i].key.dsize;
+ }
+
+ outdata->dptr = (uint8_t *)stats;
+ outdata->dsize = len;
return 0;
}
printf(" %d", dbstatistics->hop_count_bucket[i]);
}
printf("\n");
+ printf("Num Hot Keys: %d\n", dbstatistics->num_hot_keys);
+ for (i = 0; i < dbstatistics->num_hot_keys; i++) {
+ int j;
+ printf("Count:%d Key:", dbstatistics->hot_keys[i].count);
+ for (j = 0; j < dbstatistics->hot_keys[i].key.dsize; j++) {
+ printf("%02x", dbstatistics->hot_keys[i].key.dptr[j]&0xff);
+ }
+ printf("\n");
+ }
ctdb_free_dbstat(dbstatistics);
return 0;