X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fsummary.c;h=a369f30ed22f5022da5935021d276e5f603b6749;hp=d279131d3eec65561a9e86c3bb6e80412e7aef9b;hb=e6862ec8ae269217f3596f5af84747868b983afb;hpb=850c5cfed46cd4f38df79783791969c7b30ad9da diff --git a/ccan/tdb2/summary.c b/ccan/tdb2/summary.c index d279131d..a369f30e 100644 --- a/ccan/tdb2/summary.c +++ b/ccan/tdb2/summary.c @@ -19,15 +19,17 @@ #include #include -static int count_hash(struct tdb_context *tdb, - tdb_off_t hash_off, unsigned bits) +static tdb_off_t count_hash(struct tdb_context *tdb, + tdb_off_t hash_off, unsigned bits) { const tdb_off_t *h; - unsigned int i, count = 0; + tdb_off_t count = 0; + unsigned int i; h = tdb_access_read(tdb, hash_off, sizeof(*h) << bits, true); - if (!h) - return -1; + if (TDB_PTR_IS_ERR(h)) { + return TDB_PTR_ERR(h); + } for (i = 0; i < (1 << bits); i++) count += (h[i] != 0); @@ -35,22 +37,24 @@ static int count_hash(struct tdb_context *tdb, return count; } -static bool summarize(struct tdb_context *tdb, - struct tally *hashes, - struct tally *ftables, - struct tally *fr, - struct tally *keys, - struct tally *data, - struct tally *extra, - struct tally *uncoal, - struct tally *buckets, - struct tally *chains) +static enum TDB_ERROR summarize(struct tdb_context *tdb, + struct tally *hashes, + struct tally *ftables, + struct tally *fr, + struct tally *keys, + struct tally *data, + struct tally *extra, + struct tally *uncoal, + struct tally *buckets, + struct tally *chains) { tdb_off_t off; tdb_len_t len; tdb_len_t unc = 0; - for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) { + for (off = sizeof(struct tdb_header); + off < tdb->file->map_size; + off += len) { const union { struct tdb_used_record u; struct tdb_free_record f; @@ -58,8 +62,9 @@ static bool summarize(struct tdb_context *tdb, } *p; /* We might not be able to get the whole thing. */ p = tdb_access_read(tdb, off, sizeof(p->f), true); - if (!p) - return false; + if (TDB_PTR_IS_ERR(p)) { + return TDB_PTR_ERR(p); + } if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC || p->r.magic == TDB_RECOVERY_MAGIC) { if (unc) { @@ -87,11 +92,12 @@ static bool summarize(struct tdb_context *tdb, tally_add(data, rec_data_length(&p->u)); tally_add(extra, rec_extra_padding(&p->u)); } else if (rec_magic(&p->u) == TDB_HTABLE_MAGIC) { - int count = count_hash(tdb, - off + sizeof(p->u), - TDB_SUBLEVEL_HASH_BITS); - if (count == -1) - return false; + tdb_off_t count = count_hash(tdb, + off + sizeof(p->u), + TDB_SUBLEVEL_HASH_BITS); + if (TDB_OFF_IS_ERR(count)) { + return count; + } tally_add(hashes, count); tally_add(extra, rec_extra_padding(&p->u)); len = sizeof(p->u) @@ -109,13 +115,17 @@ static bool summarize(struct tdb_context *tdb, + rec_extra_padding(&p->u); tally_add(chains, 1); tally_add(extra, rec_extra_padding(&p->u)); - } else + } else { len = dead_space(tdb, off); + if (TDB_OFF_IS_ERR(len)) { + return len; + } + } tdb_access_release(tdb, p); } if (unc) tally_add(uncoal, unc); - return true; + return TDB_SUCCESS; } #define SUMMARY_FORMAT \ @@ -145,28 +155,27 @@ static bool summarize(struct tdb_context *tdb, #define HISTO_WIDTH 70 #define HISTO_HEIGHT 20 -char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) +enum TDB_ERROR tdb_summary(struct tdb_context *tdb, + enum tdb_summary_flags flags, + char **summary) { tdb_len_t len; struct tally *ftables, *hashes, *freet, *keys, *data, *extra, *uncoal, *buckets, *chains; char *hashesg, *freeg, *keysg, *datag, *extrag, *uncoalg, *bucketsg; - char *ret = NULL; enum TDB_ERROR ecode; hashesg = freeg = keysg = datag = extrag = uncoalg = bucketsg = NULL; ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false); if (ecode != TDB_SUCCESS) { - tdb->ecode = ecode; - return NULL; + return tdb->last_error = ecode; } ecode = tdb_lock_expand(tdb, F_RDLCK); if (ecode != TDB_SUCCESS) { - tdb->ecode = ecode; tdb_allrecord_unlock(tdb, F_RDLCK); - return NULL; + return tdb->last_error = ecode; } /* Start stats off empty. */ @@ -181,14 +190,17 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) chains = tally_new(HISTO_HEIGHT); if (!ftables || !hashes || !freet || !keys || !data || !extra || !uncoal || !buckets || !chains) { - tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, - "tdb_summary: failed to allocate tally structures"); + ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, + "tdb_summary: failed to allocate" + " tally structures"); goto unlock; } - if (!summarize(tdb, hashes, ftables, freet, keys, data, extra, uncoal, - buckets, chains)) + ecode = summarize(tdb, hashes, ftables, freet, keys, data, extra, + uncoal, buckets, chains); + if (ecode != TDB_SUCCESS) { goto unlock; + } if (flags & TDB_SUMMARY_HISTOGRAMS) { hashesg = tally_histogram(hashes, HISTO_WIDTH, HISTO_HEIGHT); @@ -210,48 +222,52 @@ char *tdb_summary(struct tdb_context *tdb, enum tdb_summary_flags flags) + (uncoalg ? strlen(uncoalg) : 0) + (bucketsg ? strlen(bucketsg) : 0); - ret = malloc(len); - if (!ret) + *summary = malloc(len); + if (!*summary) { + ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR, + "tdb_summary: failed to allocate string"); goto unlock; + } - len = sprintf(ret, SUMMARY_FORMAT, - (size_t)tdb->map_size, - tally_num(keys) + tally_num(data), - tally_num(keys), - tally_min(keys), tally_mean(keys), tally_max(keys), - keysg ? keysg : "", - tally_min(data), tally_mean(data), tally_max(data), - datag ? datag : "", - tally_min(extra), tally_mean(extra), tally_max(extra), - extrag ? extrag : "", - tally_num(freet), - tally_min(freet), tally_mean(freet), tally_max(freet), - freeg ? freeg : "", - tally_total(uncoal, NULL), - tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal), - uncoalg ? uncoalg : "", - tally_num(buckets), - bucketsg ? bucketsg : "", - count_hash(tdb, offsetof(struct tdb_header, hashtable), - TDB_TOPLEVEL_HASH_BITS), - 1 << TDB_TOPLEVEL_HASH_BITS, - tally_num(chains), - tally_num(hashes), - tally_min(hashes), tally_mean(hashes), tally_max(hashes), - hashesg ? hashesg : "", - tally_total(keys, NULL) * 100.0 / tdb->map_size, - tally_total(data, NULL) * 100.0 / tdb->map_size, - tally_total(extra, NULL) * 100.0 / tdb->map_size, - tally_total(freet, NULL) * 100.0 / tdb->map_size, - (tally_num(keys) + tally_num(freet) + tally_num(hashes)) - * sizeof(struct tdb_used_record) * 100.0 / tdb->map_size, - tally_num(ftables) * sizeof(struct tdb_freetable) - * 100.0 / tdb->map_size, - (tally_num(hashes) - * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) - + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS) - + sizeof(struct tdb_chain) * tally_num(chains)) - * 100.0 / tdb->map_size); + sprintf(*summary, SUMMARY_FORMAT, + (size_t)tdb->file->map_size, + tally_num(keys) + tally_num(data), + tally_num(keys), + tally_min(keys), tally_mean(keys), tally_max(keys), + keysg ? keysg : "", + tally_min(data), tally_mean(data), tally_max(data), + datag ? datag : "", + tally_min(extra), tally_mean(extra), tally_max(extra), + extrag ? extrag : "", + tally_num(freet), + tally_min(freet), tally_mean(freet), tally_max(freet), + freeg ? freeg : "", + tally_total(uncoal, NULL), + tally_min(uncoal), tally_mean(uncoal), tally_max(uncoal), + uncoalg ? uncoalg : "", + tally_num(buckets), + bucketsg ? bucketsg : "", + (unsigned)count_hash(tdb, offsetof(struct tdb_header, + hashtable), + TDB_TOPLEVEL_HASH_BITS), + 1 << TDB_TOPLEVEL_HASH_BITS, + tally_num(chains), + tally_num(hashes), + tally_min(hashes), tally_mean(hashes), tally_max(hashes), + hashesg ? hashesg : "", + tally_total(keys, NULL) * 100.0 / tdb->file->map_size, + tally_total(data, NULL) * 100.0 / tdb->file->map_size, + tally_total(extra, NULL) * 100.0 / tdb->file->map_size, + tally_total(freet, NULL) * 100.0 / tdb->file->map_size, + (tally_num(keys) + tally_num(freet) + tally_num(hashes)) + * sizeof(struct tdb_used_record) * 100.0 / tdb->file->map_size, + tally_num(ftables) * sizeof(struct tdb_freetable) + * 100.0 / tdb->file->map_size, + (tally_num(hashes) + * (sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) + + (sizeof(tdb_off_t) << TDB_TOPLEVEL_HASH_BITS) + + sizeof(struct tdb_chain) * tally_num(chains)) + * 100.0 / tdb->file->map_size); unlock: free(hashesg); @@ -273,5 +289,5 @@ unlock: tdb_allrecord_unlock(tdb, F_RDLCK); tdb_unlock_expand(tdb, F_RDLCK); - return ret; + return tdb->last_error = ecode; }