2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996-2009 Oracle. All rights reserved.
12 #include "dbinc/db_page.h"
13 #include "dbinc/db_am.h"
14 #include "dbinc/log.h"
15 #include "dbinc/txn.h"
17 #ifdef HAVE_STATISTICS
18 static int __txn_compare __P((const void *, const void *));
19 static int __txn_print_all __P((ENV *, u_int32_t));
20 static int __txn_print_stats __P((ENV *, u_int32_t));
21 static int __txn_stat __P((ENV *, DB_TXN_STAT **, u_int32_t));
22 static char *__txn_status __P((DB_TXN_ACTIVE *));
23 static void __txn_gid __P((ENV *, DB_MSGBUF *, DB_TXN_ACTIVE *));
27 * DB_ENV->txn_stat pre/post processing.
29 * PUBLIC: int __txn_stat_pp __P((DB_ENV *, DB_TXN_STAT **, u_int32_t));
32 __txn_stat_pp(dbenv, statp, flags)
43 ENV_REQUIRES_CONFIG(env,
44 env->tx_handle, "DB_ENV->txn_stat", DB_INIT_TXN);
46 if ((ret = __db_fchk(env,
47 "DB_ENV->txn_stat", flags, DB_STAT_CLEAR)) != 0)
51 REPLICATION_WRAP(env, (__txn_stat(env, statp, flags)), 0, ret);
61 __txn_stat(env, statp, flags)
71 u_int32_t maxtxn, ndx;
76 region = mgr->reginfo.primary;
79 * Allocate for the maximum active transactions -- the DB_TXN_ACTIVE
80 * struct is small and the maximum number of active transactions is
81 * not going to be that large. Don't have to lock anything to look
82 * at the region's maximum active transactions value, it's read-only
83 * and never changes after the region is created.
85 * The maximum active transactions isn't a hard limit, so allocate
86 * some extra room, and don't walk off the end.
88 maxtxn = region->maxtxns + (region->maxtxns / 10) + 10;
89 nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * maxtxn;
90 if ((ret = __os_umalloc(env, nbytes, &stats)) != 0)
94 memcpy(stats, ®ion->stat, sizeof(*stats));
95 stats->st_last_txnid = region->last_txnid;
96 stats->st_last_ckp = region->last_ckp;
97 stats->st_time_ckp = region->time_ckp;
98 stats->st_txnarray = (DB_TXN_ACTIVE *)&stats[1];
101 td = SH_TAILQ_FIRST(®ion->active_txn, __txn_detail);
102 td != NULL && ndx < maxtxn;
103 td = SH_TAILQ_NEXT(td, links, __txn_detail), ++ndx) {
104 stats->st_txnarray[ndx].txnid = td->txnid;
105 if (td->parent == INVALID_ROFF)
106 stats->st_txnarray[ndx].parentid = TXN_INVALID;
108 stats->st_txnarray[ndx].parentid =
109 ((TXN_DETAIL *)R_ADDR(&mgr->reginfo,
111 stats->st_txnarray[ndx].pid = td->pid;
112 stats->st_txnarray[ndx].tid = td->tid;
113 stats->st_txnarray[ndx].lsn = td->begin_lsn;
114 stats->st_txnarray[ndx].read_lsn = td->read_lsn;
115 stats->st_txnarray[ndx].mvcc_ref = td->mvcc_ref;
116 stats->st_txnarray[ndx].status = td->status;
117 if (td->status == TXN_PREPARED)
118 memcpy(stats->st_txnarray[ndx].gid,
119 td->gid, sizeof(td->gid));
120 if (td->name != INVALID_ROFF) {
121 (void)strncpy(stats->st_txnarray[ndx].name,
122 R_ADDR(&mgr->reginfo, td->name),
123 sizeof(stats->st_txnarray[ndx].name) - 1);
124 stats->st_txnarray[ndx].name[
125 sizeof(stats->st_txnarray[ndx].name) - 1] = '\0';
127 stats->st_txnarray[ndx].name[0] = '\0';
130 __mutex_set_wait_info(env, region->mtx_region,
131 &stats->st_region_wait, &stats->st_region_nowait);
132 stats->st_regsize = mgr->reginfo.rp->size;
133 if (LF_ISSET(DB_STAT_CLEAR)) {
134 if (!LF_ISSET(DB_STAT_SUBSYSTEM))
135 __mutex_clear(env, region->mtx_region);
136 memset(®ion->stat, 0, sizeof(region->stat));
137 region->stat.st_maxtxns = region->maxtxns;
138 region->stat.st_maxnactive =
139 region->stat.st_nactive = stats->st_nactive;
140 region->stat.st_maxnsnapshot =
141 region->stat.st_nsnapshot = stats->st_nsnapshot;
144 TXN_SYSTEM_UNLOCK(env);
151 * __txn_stat_print_pp --
152 * DB_ENV->txn_stat_print pre/post processing.
154 * PUBLIC: int __txn_stat_print_pp __P((DB_ENV *, u_int32_t));
157 __txn_stat_print_pp(dbenv, flags)
167 ENV_REQUIRES_CONFIG(env,
168 env->tx_handle, "DB_ENV->txn_stat_print", DB_INIT_TXN);
170 if ((ret = __db_fchk(env, "DB_ENV->txn_stat_print",
171 flags, DB_STAT_ALL | DB_STAT_CLEAR)) != 0)
175 REPLICATION_WRAP(env, (__txn_stat_print(env, flags)), 0, ret);
182 * ENV->txn_stat_print method.
184 * PUBLIC: int __txn_stat_print __P((ENV *, u_int32_t));
187 __txn_stat_print(env, flags)
191 u_int32_t orig_flags;
195 LF_CLR(DB_STAT_CLEAR | DB_STAT_SUBSYSTEM);
196 if (flags == 0 || LF_ISSET(DB_STAT_ALL)) {
197 ret = __txn_print_stats(env, orig_flags);
198 if (flags == 0 || ret != 0)
202 if (LF_ISSET(DB_STAT_ALL) &&
203 (ret = __txn_print_all(env, orig_flags)) != 0)
210 * __txn_print_stats --
211 * Display default transaction region statistics.
214 __txn_print_stats(env, flags)
224 char buf[DB_THREADID_STRLEN], time_buf[CTIME_BUFLEN];
228 if ((ret = __txn_stat(env, &sp, flags)) != 0)
231 if (LF_ISSET(DB_STAT_ALL))
232 __db_msg(env, "Default transaction region information:");
233 __db_msg(env, "%lu/%lu\t%s",
234 (u_long)sp->st_last_ckp.file, (u_long)sp->st_last_ckp.offset,
235 sp->st_last_ckp.file == 0 ?
236 "No checkpoint LSN" : "File/offset for last checkpoint LSN");
237 if (sp->st_time_ckp == 0)
238 __db_msg(env, "0\tNo checkpoint timestamp");
240 __db_msg(env, "%.24s\tCheckpoint timestamp",
241 __os_ctime(&sp->st_time_ckp, time_buf));
242 __db_msg(env, "%#lx\tLast transaction ID allocated",
243 (u_long)sp->st_last_txnid);
244 __db_dl(env, "Maximum number of active transactions configured",
245 (u_long)sp->st_maxtxns);
246 __db_dl(env, "Active transactions", (u_long)sp->st_nactive);
248 "Maximum active transactions", (u_long)sp->st_maxnactive);
250 "Number of transactions begun", (u_long)sp->st_nbegins);
252 "Number of transactions aborted", (u_long)sp->st_naborts);
254 "Number of transactions committed", (u_long)sp->st_ncommits);
255 __db_dl(env, "Snapshot transactions", (u_long)sp->st_nsnapshot);
256 __db_dl(env, "Maximum snapshot transactions",
257 (u_long)sp->st_maxnsnapshot);
259 "Number of transactions restored", (u_long)sp->st_nrestores);
261 __db_dlbytes(env, "Transaction region size",
262 (u_long)0, (u_long)0, (u_long)sp->st_regsize);
264 "The number of region locks that required waiting",
265 (u_long)sp->st_region_wait, DB_PCT(sp->st_region_wait,
266 sp->st_region_wait + sp->st_region_nowait), NULL);
268 qsort(sp->st_txnarray,
269 sp->st_nactive, sizeof(sp->st_txnarray[0]), __txn_compare);
270 __db_msg(env, "Active transactions:");
272 for (i = 0; i < sp->st_nactive; ++i) {
273 txn = &sp->st_txnarray[i];
274 __db_msgadd(env, &mb,
275 "\t%lx: %s; pid/thread %s; begin LSN: file/offset %lu/%lu",
276 (u_long)txn->txnid, __txn_status(txn),
277 dbenv->thread_id_string(dbenv, txn->pid, txn->tid, buf),
278 (u_long)txn->lsn.file, (u_long)txn->lsn.offset);
279 if (txn->parentid != 0)
280 __db_msgadd(env, &mb,
281 "; parent: %lx", (u_long)txn->parentid);
282 if (!IS_MAX_LSN(txn->read_lsn))
283 __db_msgadd(env, &mb, "; read LSN: %lu/%lu",
284 (u_long)txn->read_lsn.file,
285 (u_long)txn->read_lsn.offset);
286 if (txn->mvcc_ref != 0)
287 __db_msgadd(env, &mb,
288 "; mvcc refcount: %lu", (u_long)txn->mvcc_ref);
289 if (txn->name[0] != '\0')
290 __db_msgadd(env, &mb, "; \"%s\"", txn->name);
291 if (txn->status == TXN_PREPARE)
292 __txn_gid(env, &mb, txn);
293 DB_MSGBUF_FLUSH(env, &mb);
303 * Display debugging transaction region statistics.
306 __txn_print_all(env, flags)
310 static const FN fn[] = {
311 { TXN_IN_RECOVERY, "TXN_IN_RECOVERY" },
315 DB_TXNREGION *region;
316 char time_buf[CTIME_BUFLEN];
318 mgr = env->tx_handle;
319 region = mgr->reginfo.primary;
321 TXN_SYSTEM_LOCK(env);
323 __db_print_reginfo(env, &mgr->reginfo, "Transaction", flags);
325 __db_msg(env, "%s", DB_GLOBAL(db_line));
326 __db_msg(env, "DB_TXNMGR handle information:");
327 __mutex_print_debug_single(env, "DB_TXNMGR mutex", mgr->mutex, flags);
329 "Number of transactions discarded", (u_long)mgr->n_discards);
331 __db_msg(env, "%s", DB_GLOBAL(db_line));
332 __db_msg(env, "DB_TXNREGION handle information:");
333 __mutex_print_debug_single(
334 env, "DB_TXNREGION region mutex", region->mtx_region, flags);
335 STAT_ULONG("Maximum number of active txns", region->maxtxns);
336 STAT_HEX("Last transaction ID allocated", region->last_txnid);
337 STAT_HEX("Current maximum unused ID", region->cur_maxid);
339 __mutex_print_debug_single(
340 env, "checkpoint mutex", region->mtx_ckp, flags);
341 STAT_LSN("Last checkpoint LSN", ®ion->last_ckp);
343 "%.24s\tLast checkpoint timestamp",
344 region->time_ckp == 0 ? "0" :
345 __os_ctime(®ion->time_ckp, time_buf));
347 __db_prflags(env, NULL, region->flags, fn, NULL, "\tFlags");
349 __db_msg(env, "%s", DB_GLOBAL(db_line));
350 TXN_SYSTEM_UNLOCK(env);
359 switch (txn->status) {
363 return ("committed");
371 return ("unknown state");
375 __txn_gid(env, mbp, txn)
384 __db_msgadd(env, mbp, "\n\tGID:");
385 for (cnt = 0, xp = (u_int32_t *)txn->gid, i = 0;;) {
386 memcpy(&v, xp++, sizeof(u_int32_t));
387 __db_msgadd(env, mbp, "%#lx ", (u_long)v);
388 if ((i += sizeof(u_int32_t)) >= DB_GID_SIZE)
391 DB_MSGBUF_FLUSH(env, mbp);
392 __db_msgadd(env, mbp, "\t\t");
399 __txn_compare(a1, b1)
402 const DB_TXN_ACTIVE *a, *b;
407 if (a->txnid > b->txnid)
409 if (a->txnid < b->txnid)
414 #else /* !HAVE_STATISTICS */
417 __txn_stat_pp(dbenv, statp, flags)
422 COMPQUIET(statp, NULL);
425 return (__db_stat_not_built(dbenv->env));
429 __txn_stat_print_pp(dbenv, flags)
435 return (__db_stat_not_built(dbenv->env));