Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

txn_stat.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: txn_stat.c,v 12.8 2005/10/07 20:21:43 ubell Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 
00015 #if TIME_WITH_SYS_TIME
00016 #include <sys/time.h>
00017 #include <time.h>
00018 #else
00019 #if HAVE_SYS_TIME_H
00020 #include <sys/time.h>
00021 #else
00022 #include <time.h>
00023 #endif
00024 #endif
00025 
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #endif
00029 
00030 #include "db_int.h"
00031 #include "dbinc/db_page.h"
00032 #include "dbinc/db_am.h"
00033 #include "dbinc/log.h"
00034 #include "dbinc/txn.h"
00035 
00036 #ifdef HAVE_STATISTICS
00037 static int  __txn_compare __P((const void *, const void *));
00038 static int  __txn_print_all __P((DB_ENV *, u_int32_t));
00039 static int  __txn_print_stats __P((DB_ENV *, u_int32_t));
00040 static int  __txn_stat __P((DB_ENV *, DB_TXN_STAT **, u_int32_t));
00041 static void __txn_xid_stats __P((DB_ENV *, DB_MSGBUF *, DB_TXN_ACTIVE *));
00042 
00043 /*
00044  * __txn_stat_pp --
00045  *      DB_ENV->txn_stat pre/post processing.
00046  *
00047  * PUBLIC: int __txn_stat_pp __P((DB_ENV *, DB_TXN_STAT **, u_int32_t));
00048  */
00049 int
00050 __txn_stat_pp(dbenv, statp, flags)
00051         DB_ENV *dbenv;
00052         DB_TXN_STAT **statp;
00053         u_int32_t flags;
00054 {
00055         DB_THREAD_INFO *ip;
00056         int ret;
00057 
00058         PANIC_CHECK(dbenv);
00059         ENV_REQUIRES_CONFIG(dbenv,
00060             dbenv->tx_handle, "DB_ENV->txn_stat", DB_INIT_TXN);
00061 
00062         if ((ret = __db_fchk(dbenv,
00063             "DB_ENV->txn_stat", flags, DB_STAT_CLEAR)) != 0)
00064                 return (ret);
00065 
00066         ENV_ENTER(dbenv, ip);
00067         REPLICATION_WRAP(dbenv, (__txn_stat(dbenv, statp, flags)), ret);
00068         ENV_LEAVE(dbenv, ip);
00069         return (ret);
00070 }
00071 
00072 /*
00073  * __txn_stat --
00074  *      DB_ENV->txn_stat.
00075  */
00076 static int
00077 __txn_stat(dbenv, statp, flags)
00078         DB_ENV *dbenv;
00079         DB_TXN_STAT **statp;
00080         u_int32_t flags;
00081 {
00082         DB_TXNMGR *mgr;
00083         DB_TXNREGION *region;
00084         DB_TXN_STAT *stats;
00085         TXN_DETAIL *td;
00086         size_t nbytes;
00087         u_int32_t maxtxn, ndx;
00088         int ret;
00089 
00090         *statp = NULL;
00091         mgr = dbenv->tx_handle;
00092         region = mgr->reginfo.primary;
00093 
00094         /*
00095          * Allocate for the maximum active transactions -- the DB_TXN_ACTIVE
00096          * struct is small and the maximum number of active transactions is
00097          * not going to be that large.  Don't have to lock anything to look
00098          * at the region's maximum active transactions value, it's read-only
00099          * and never changes after the region is created.
00100          *
00101          * The maximum active transactions isn't a hard limit, so allocate
00102          * some extra room, and don't walk off the end.
00103          */
00104         maxtxn = region->maxtxns + (region->maxtxns / 10) + 10;
00105         nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * maxtxn;
00106         if ((ret = __os_umalloc(dbenv, nbytes, &stats)) != 0)
00107                 return (ret);
00108 
00109         TXN_SYSTEM_LOCK(dbenv);
00110         memcpy(stats, &region->stat, sizeof(*stats));
00111         stats->st_last_txnid = region->last_txnid;
00112         stats->st_last_ckp = region->last_ckp;
00113         stats->st_time_ckp = region->time_ckp;
00114         stats->st_txnarray = (DB_TXN_ACTIVE *)&stats[1];
00115 
00116         for (ndx = 0,
00117             td = SH_TAILQ_FIRST(&region->active_txn, __txn_detail);
00118             td != NULL && ndx < maxtxn;
00119             td = SH_TAILQ_NEXT(td, links, __txn_detail), ++ndx) {
00120                 stats->st_txnarray[ndx].txnid = td->txnid;
00121                 if (td->parent == INVALID_ROFF)
00122                         stats->st_txnarray[ndx].parentid = TXN_INVALID;
00123                 else
00124                         stats->st_txnarray[ndx].parentid =
00125                             ((TXN_DETAIL *)R_ADDR(&mgr->reginfo,
00126                             td->parent))->txnid;
00127                 stats->st_txnarray[ndx].pid = td->pid;
00128                 stats->st_txnarray[ndx].tid = td->tid;
00129                 stats->st_txnarray[ndx].lsn = td->begin_lsn;
00130                 if ((stats->st_txnarray[ndx].xa_status = td->xa_status) != 0)
00131                         memcpy(stats->st_txnarray[ndx].xid,
00132                             td->xid, DB_XIDDATASIZE);
00133                 if (td->name != INVALID_ROFF) {
00134                         (void)strncpy(stats->st_txnarray[ndx].name,
00135                             R_ADDR(&mgr->reginfo, td->name),
00136                             sizeof(stats->st_txnarray[ndx].name) - 1);
00137                         stats->st_txnarray[ndx].name[
00138                             sizeof(stats->st_txnarray[ndx].name) - 1] = '\0';
00139                 } else
00140                         stats->st_txnarray[ndx].name[0] = '\0';
00141         }
00142 
00143         __mutex_set_wait_info(dbenv, region->mtx_region,
00144             &stats->st_region_wait, &stats->st_region_nowait);
00145         stats->st_regsize = mgr->reginfo.rp->size;
00146         if (LF_ISSET(DB_STAT_CLEAR)) {
00147                 __mutex_clear(dbenv, region->mtx_region);
00148                 memset(&region->stat, 0, sizeof(region->stat));
00149                 region->stat.st_maxtxns = region->maxtxns;
00150                 region->stat.st_maxnactive =
00151                     region->stat.st_nactive = stats->st_nactive;
00152         }
00153 
00154         TXN_SYSTEM_UNLOCK(dbenv);
00155 
00156         *statp = stats;
00157         return (0);
00158 }
00159 
00160 /*
00161  * __txn_stat_print_pp --
00162  *      DB_ENV->txn_stat_print pre/post processing.
00163  *
00164  * PUBLIC: int __txn_stat_print_pp __P((DB_ENV *, u_int32_t));
00165  */
00166 int
00167 __txn_stat_print_pp(dbenv, flags)
00168         DB_ENV *dbenv;
00169         u_int32_t flags;
00170 {
00171         DB_THREAD_INFO *ip;
00172         int ret;
00173 
00174         PANIC_CHECK(dbenv);
00175         ENV_REQUIRES_CONFIG(dbenv,
00176             dbenv->tx_handle, "DB_ENV->txn_stat_print", DB_INIT_TXN);
00177 
00178         if ((ret = __db_fchk(dbenv, "DB_ENV->txn_stat",
00179             flags, DB_STAT_ALL | DB_STAT_CLEAR)) != 0)
00180                 return (ret);
00181 
00182         ENV_ENTER(dbenv, ip);
00183         REPLICATION_WRAP(dbenv, (__txn_stat_print(dbenv, flags)), ret);
00184         ENV_LEAVE(dbenv, ip);
00185         return (ret);
00186 }
00187 
00188 /*
00189  * __txn_stat_print
00190  *      DB_ENV->txn_stat_print method.
00191  *
00192  * PUBLIC: int  __txn_stat_print __P((DB_ENV *, u_int32_t));
00193  */
00194 int
00195 __txn_stat_print(dbenv, flags)
00196         DB_ENV *dbenv;
00197         u_int32_t flags;
00198 {
00199         u_int32_t orig_flags;
00200         int ret;
00201 
00202         orig_flags = flags;
00203         LF_CLR(DB_STAT_CLEAR);
00204         if (flags == 0 || LF_ISSET(DB_STAT_ALL)) {
00205                 ret = __txn_print_stats(dbenv, orig_flags);
00206                 if (flags == 0 || ret != 0)
00207                         return (ret);
00208         }
00209 
00210         if (LF_ISSET(DB_STAT_ALL) &&
00211             (ret = __txn_print_all(dbenv, orig_flags)) != 0)
00212                 return (ret);
00213 
00214         return (0);
00215 }
00216 
00217 /*
00218  * __txn_print_stats --
00219  *      Display default transaction region statistics.
00220  */
00221 static int
00222 __txn_print_stats(dbenv, flags)
00223         DB_ENV *dbenv;
00224         u_int32_t flags;
00225 {
00226         DB_MSGBUF mb;
00227         DB_TXN_STAT *sp;
00228         u_int32_t i;
00229         int ret;
00230         char buf[DB_THREADID_STRLEN];
00231 
00232         if ((ret = __txn_stat(dbenv, &sp, flags)) != 0)
00233                 return (ret);
00234 
00235         if (LF_ISSET(DB_STAT_ALL))
00236                 __db_msg(dbenv, "Default transaction region information:");
00237         __db_msg(dbenv, "%lu/%lu\t%s",
00238             (u_long)sp->st_last_ckp.file, (u_long)sp->st_last_ckp.offset,
00239             sp->st_last_ckp.file == 0 ?
00240             "No checkpoint LSN" : "File/offset for last checkpoint LSN");
00241         if (sp->st_time_ckp == 0)
00242                 __db_msg(dbenv, "0\tNo checkpoint timestamp");
00243         else
00244                 __db_msg(dbenv, "%.24s\tCheckpoint timestamp",
00245                     ctime(&sp->st_time_ckp));
00246         __db_msg(dbenv, "%#lx\tLast transaction ID allocated",
00247             (u_long)sp->st_last_txnid);
00248         __db_dl(dbenv, "Maximum number of active transactions configured",
00249             (u_long)sp->st_maxtxns);
00250         __db_dl(dbenv, "Active transactions", (u_long)sp->st_nactive);
00251         __db_dl(dbenv,
00252             "Maximum active transactions", (u_long)sp->st_maxnactive);
00253         __db_dl(dbenv,
00254             "Number of transactions begun", (u_long)sp->st_nbegins);
00255         __db_dl(dbenv,
00256             "Number of transactions aborted", (u_long)sp->st_naborts);
00257         __db_dl(dbenv,
00258             "Number of transactions committed", (u_long)sp->st_ncommits);
00259         __db_dl(dbenv,
00260             "Number of transactions restored", (u_long)sp->st_nrestores);
00261 
00262         __db_dlbytes(dbenv, "Transaction region size",
00263             (u_long)0, (u_long)0, (u_long)sp->st_regsize);
00264         __db_dl_pct(dbenv,
00265             "The number of region locks that required waiting",
00266             (u_long)sp->st_region_wait, DB_PCT(sp->st_region_wait,
00267             sp->st_region_wait + sp->st_region_nowait), NULL);
00268 
00269         qsort(sp->st_txnarray,
00270             sp->st_nactive, sizeof(sp->st_txnarray[0]), __txn_compare);
00271         __db_msg(dbenv, "Active transactions:");
00272         DB_MSGBUF_INIT(&mb);
00273         for (i = 0; i < sp->st_nactive; ++i) {
00274                 __db_msgadd(dbenv, &mb,
00275             "\t%lx: pid/thread %s; begin LSN: file/offset %lu/%lu",
00276                     (u_long)sp->st_txnarray[i].txnid,
00277                     dbenv->thread_id_string(dbenv,
00278                     sp->st_txnarray[i].pid, sp->st_txnarray[i].tid, buf),
00279                     (u_long)sp->st_txnarray[i].lsn.file,
00280                     (u_long)sp->st_txnarray[i].lsn.offset);
00281                 if (sp->st_txnarray[i].parentid != 0)
00282                         __db_msgadd(dbenv, &mb, "; parent: %lx",
00283                             (u_long)sp->st_txnarray[i].parentid);
00284                 if (sp->st_txnarray[i].xa_status != 0)
00285                         __txn_xid_stats(dbenv, &mb, &sp->st_txnarray[i]);
00286                 if (sp->st_txnarray[i].name[0] != '\0')
00287                         __db_msgadd(
00288                             dbenv, &mb, "; \"%s\"", sp->st_txnarray[i].name);
00289                 DB_MSGBUF_FLUSH(dbenv, &mb);
00290         }
00291 
00292         __os_ufree(dbenv, sp);
00293 
00294         return (0);
00295 }
00296 
00297 /*
00298  * __txn_print_all --
00299  *      Display debugging transaction region statistics.
00300  */
00301 static int
00302 __txn_print_all(dbenv, flags)
00303         DB_ENV *dbenv;
00304         u_int32_t flags;
00305 {
00306         static const FN fn[] = {
00307                 { TXN_IN_RECOVERY,      "TXN_IN_RECOVERY" },
00308                 { 0,                    NULL }
00309         };
00310         DB_TXNMGR *mgr;
00311         DB_TXNREGION *region;
00312 
00313         mgr = dbenv->tx_handle;
00314         region = mgr->reginfo.primary;
00315 
00316         TXN_SYSTEM_LOCK(dbenv);
00317 
00318         __db_print_reginfo(dbenv, &mgr->reginfo, "Transaction");
00319 
00320         __db_msg(dbenv, "%s", DB_GLOBAL(db_line));
00321         __db_msg(dbenv, "DB_TXNMGR handle information:");
00322         __mutex_print_debug_single(dbenv, "DB_TXNMGR mutex", mgr->mutex, flags);
00323         __db_dl(dbenv,
00324             "Number of transactions discarded", (u_long)mgr->n_discards);
00325 
00326         __db_msg(dbenv, "%s", DB_GLOBAL(db_line));
00327         __db_msg(dbenv, "DB_TXNREGION handle information:");
00328         __mutex_print_debug_single(
00329             dbenv, "DB_TXNREGION region mutex", region->mtx_region, flags);
00330         STAT_ULONG("Maximum number of active txns", region->maxtxns);
00331         STAT_HEX("Last transaction ID allocated", region->last_txnid);
00332         STAT_HEX("Current maximum unused ID", region->cur_maxid);
00333 
00334         __mutex_print_debug_single(
00335             dbenv, "checkpoint mutex", region->mtx_ckp, flags);
00336         STAT_LSN("Last checkpoint LSN", &region->last_ckp);
00337         __db_msg(dbenv,
00338             "%.24s\tLast checkpoint timestamp",
00339             region->time_ckp == 0 ? "0" : ctime(&region->time_ckp));
00340 
00341         __db_prflags(dbenv, NULL, region->flags, fn, NULL, "\tFlags");
00342 
00343         __db_msg(dbenv, "%s", DB_GLOBAL(db_line));
00344         __db_msg(dbenv, "XA information:");
00345         STAT_LONG("XA RMID", dbenv->xa_rmid);
00346         /*
00347          * XXX
00348          * Display list of XA transactions in the DB_ENV handle.
00349          */
00350 
00351         TXN_SYSTEM_UNLOCK(dbenv);
00352 
00353         return (0);
00354 }
00355 
00356 static void
00357 __txn_xid_stats(dbenv, mbp, txn_active)
00358         DB_ENV *dbenv;
00359         DB_MSGBUF *mbp;
00360         DB_TXN_ACTIVE *txn_active;
00361 {
00362         u_int32_t v, *xp;
00363         u_int i;
00364         int cnt;
00365         const char *s;
00366 
00367         switch (txn_active->xa_status) {
00368         case TXN_XA_ABORTED:
00369                 s = "ABORTED";
00370                 break;
00371         case TXN_XA_DEADLOCKED:
00372                 s = "DEADLOCKED";
00373                 break;
00374         case TXN_XA_ENDED:
00375                 s = "ENDED";
00376                 break;
00377         case TXN_XA_PREPARED:
00378                 s = "PREPARED";
00379                 break;
00380         case TXN_XA_STARTED:
00381                 s = "STARTED";
00382                 break;
00383         case TXN_XA_SUSPENDED:
00384                 s = "SUSPENDED";
00385                 break;
00386         default:
00387                 s = "UNKNOWN STATE";
00388                 __db_err(dbenv,
00389                     "XA: unknown state: %lu", (u_long)txn_active->xa_status);
00390                 break;
00391         }
00392         __db_msgadd(dbenv, mbp, "\tXA: %s; XID:\n\t\t", s == NULL ? "" : s);
00393         for (cnt = 0, xp = (u_int32_t *)txn_active->xid,
00394             i = 0; i < DB_XIDDATASIZE; i += sizeof(u_int32_t)) {
00395                 memcpy(&v, xp++, sizeof(u_int32_t));
00396                 __db_msgadd(dbenv, mbp, "%#lx ", (u_long)v);
00397                 if (++cnt == 4) {
00398                         DB_MSGBUF_FLUSH(dbenv, mbp);
00399                         __db_msgadd(dbenv, mbp, "\t\t");
00400                         cnt = 0;
00401                 }
00402         }
00403 }
00404 
00405 static int
00406 __txn_compare(a1, b1)
00407         const void *a1, *b1;
00408 {
00409         const DB_TXN_ACTIVE *a, *b;
00410 
00411         a = a1;
00412         b = b1;
00413 
00414         if (a->txnid > b->txnid)
00415                 return (1);
00416         if (a->txnid < b->txnid)
00417                 return (-1);
00418         return (0);
00419 }
00420 
00421 #else /* !HAVE_STATISTICS */
00422 
00423 int
00424 __txn_stat_pp(dbenv, statp, flags)
00425         DB_ENV *dbenv;
00426         DB_TXN_STAT **statp;
00427         u_int32_t flags;
00428 {
00429         COMPQUIET(statp, NULL);
00430         COMPQUIET(flags, 0);
00431 
00432         return (__db_stat_not_built(dbenv));
00433 }
00434 
00435 int
00436 __txn_stat_print_pp(dbenv, flags)
00437         DB_ENV *dbenv;
00438         u_int32_t flags;
00439 {
00440         COMPQUIET(flags, 0);
00441 
00442         return (__db_stat_not_built(dbenv));
00443 }
00444 #endif

Generated on Sun Dec 25 12:14:56 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2