1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 2013 Oracle and/or its affiliates.  All rights reserved.
5  *
6  * $Id$
7  */
8 
9 #include "db_config.h"
10 
11 #include "db_int.h"
12 #include "dbinc/db_page.h"
13 #include "dbinc/db_am.h"
14 #include "dbinc/txn.h"
15 
16 #ifdef HAVE_STATISTICS
17 static int  __txn_compare __P((const void *, const void *));
18 static int  __txn_print_all __P((ENV *, u_int32_t));
19 static int  __txn_print_stats __P((ENV *, u_int32_t));
20 static int  __txn_stat __P((ENV *, DB_TXN_STAT **, u_int32_t));
21 static char *__txn_status __P((DB_TXN_ACTIVE *));
22 static char *__txn_xa_status __P((DB_TXN_ACTIVE *));
23 static void __txn_gid __P((ENV *, DB_MSGBUF *, DB_TXN_ACTIVE *));
24 
25 /*
26  * __txn_stat_pp --
27  *	DB_ENV->txn_stat pre/post processing.
28  *
29  * PUBLIC: int __txn_stat_pp __P((DB_ENV *, DB_TXN_STAT **, u_int32_t));
30  */
31 int
__txn_stat_pp(dbenv,statp,flags)32 __txn_stat_pp(dbenv, statp, flags)
33 	DB_ENV *dbenv;
34 	DB_TXN_STAT **statp;
35 	u_int32_t flags;
36 {
37 	DB_THREAD_INFO *ip;
38 	ENV *env;
39 	int ret;
40 
41 	env = dbenv->env;
42 
43 	ENV_REQUIRES_CONFIG(env,
44 	    env->tx_handle, "DB_ENV->txn_stat", DB_INIT_TXN);
45 
46 	if ((ret = __db_fchk(env,
47 	    "DB_ENV->txn_stat", flags, DB_STAT_CLEAR)) != 0)
48 		return (ret);
49 
50 	ENV_ENTER(env, ip);
51 	REPLICATION_WRAP(env, (__txn_stat(env, statp, flags)), 0, ret);
52 	ENV_LEAVE(env, ip);
53 	return (ret);
54 }
55 
56 /*
57  * __txn_stat --
58  *	ENV->txn_stat.
59  */
60 static int
__txn_stat(env,statp,flags)61 __txn_stat(env, statp, flags)
62 	ENV *env;
63 	DB_TXN_STAT **statp;
64 	u_int32_t flags;
65 {
66 	DB_TXNMGR *mgr;
67 	DB_TXNREGION *region;
68 	DB_TXN_STAT *stats;
69 	TXN_DETAIL *td;
70 	size_t nbytes;
71 	u_int32_t maxtxn, ndx;
72 	int ret;
73 
74 	*statp = NULL;
75 	mgr = env->tx_handle;
76 	region = mgr->reginfo.primary;
77 
78 	TXN_SYSTEM_LOCK(env);
79 	maxtxn = region->curtxns;
80 	nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * maxtxn;
81 	if ((ret = __os_umalloc(env, nbytes, &stats)) != 0) {
82 		TXN_SYSTEM_UNLOCK(env);
83 		return (ret);
84 	}
85 
86 	memcpy(stats, &region->stat, sizeof(region->stat));
87 	stats->st_last_txnid = region->last_txnid;
88 	stats->st_last_ckp = region->last_ckp;
89 	stats->st_time_ckp = region->time_ckp;
90 	stats->st_txnarray = (DB_TXN_ACTIVE *)&stats[1];
91 
92 	for (ndx = 0,
93 	    td = SH_TAILQ_FIRST(&region->active_txn, __txn_detail);
94 	    td != NULL && ndx < maxtxn;
95 	    td = SH_TAILQ_NEXT(td, links, __txn_detail), ++ndx) {
96 		stats->st_txnarray[ndx].txnid = td->txnid;
97 		if (td->parent == INVALID_ROFF)
98 			stats->st_txnarray[ndx].parentid = TXN_INVALID;
99 		else
100 			stats->st_txnarray[ndx].parentid =
101 			    ((TXN_DETAIL *)R_ADDR(&mgr->reginfo,
102 			    td->parent))->txnid;
103 		stats->st_txnarray[ndx].pid = td->pid;
104 		stats->st_txnarray[ndx].tid = td->tid;
105 		stats->st_txnarray[ndx].lsn = td->begin_lsn;
106 		stats->st_txnarray[ndx].read_lsn = td->read_lsn;
107 		stats->st_txnarray[ndx].mvcc_ref = td->mvcc_ref;
108 		stats->st_txnarray[ndx].status = td->status;
109 		stats->st_txnarray[ndx].xa_status = td->xa_br_status;
110 		stats->st_txnarray[ndx].priority = td->priority;
111 
112 		if (td->status == TXN_PREPARED)
113 			memcpy(stats->st_txnarray[ndx].gid,
114 			    td->gid, sizeof(td->gid));
115 		if (td->name != INVALID_ROFF) {
116 			(void)strncpy(stats->st_txnarray[ndx].name,
117 			    R_ADDR(&mgr->reginfo, td->name),
118 			    sizeof(stats->st_txnarray[ndx].name) - 1);
119 			stats->st_txnarray[ndx].name[
120 			    sizeof(stats->st_txnarray[ndx].name) - 1] = '\0';
121 		} else
122 			stats->st_txnarray[ndx].name[0] = '\0';
123 	}
124 
125 	__mutex_set_wait_info(env, region->mtx_region,
126 	    &stats->st_region_wait, &stats->st_region_nowait);
127 	stats->st_regsize = (roff_t)mgr->reginfo.rp->size;
128 	if (LF_ISSET(DB_STAT_CLEAR)) {
129 		if (!LF_ISSET(DB_STAT_SUBSYSTEM))
130 			__mutex_clear(env, region->mtx_region);
131 		memset(&region->stat, 0, sizeof(region->stat));
132 		region->stat.st_maxtxns = region->maxtxns;
133 		region->stat.st_inittxns = region->inittxns;
134 		region->stat.st_maxnactive =
135 		    region->stat.st_nactive = stats->st_nactive;
136 		region->stat.st_maxnsnapshot =
137 		    region->stat.st_nsnapshot = stats->st_nsnapshot;
138 	}
139 
140 	TXN_SYSTEM_UNLOCK(env);
141 
142 	*statp = stats;
143 	return (0);
144 }
145 
146 /*
147  * __txn_stat_print_pp --
148  *	DB_ENV->txn_stat_print pre/post processing.
149  *
150  * PUBLIC: int __txn_stat_print_pp __P((DB_ENV *, u_int32_t));
151  */
152 int
__txn_stat_print_pp(dbenv,flags)153 __txn_stat_print_pp(dbenv, flags)
154 	DB_ENV *dbenv;
155 	u_int32_t flags;
156 {
157 	DB_THREAD_INFO *ip;
158 	ENV *env;
159 	int ret;
160 
161 	env = dbenv->env;
162 
163 	ENV_REQUIRES_CONFIG(env,
164 	    env->tx_handle, "DB_ENV->txn_stat_print", DB_INIT_TXN);
165 
166 	if ((ret = __db_fchk(env, "DB_ENV->txn_stat_print",
167 	    flags, DB_STAT_ALL | DB_STAT_ALLOC | DB_STAT_CLEAR)) != 0)
168 		return (ret);
169 
170 	ENV_ENTER(env, ip);
171 	REPLICATION_WRAP(env, (__txn_stat_print(env, flags)), 0, ret);
172 	ENV_LEAVE(env, ip);
173 	return (ret);
174 }
175 
176 /*
177  * __txn_stat_print
178  *	ENV->txn_stat_print method.
179  *
180  * PUBLIC: int  __txn_stat_print __P((ENV *, u_int32_t));
181  */
182 int
__txn_stat_print(env,flags)183 __txn_stat_print(env, flags)
184 	ENV *env;
185 	u_int32_t flags;
186 {
187 	u_int32_t orig_flags;
188 	int ret;
189 
190 	orig_flags = flags;
191 	LF_CLR(DB_STAT_CLEAR | DB_STAT_SUBSYSTEM);
192 	if (flags == 0 || LF_ISSET(DB_STAT_ALL)) {
193 		ret = __txn_print_stats(env, orig_flags);
194 		if (flags == 0 || ret != 0)
195 			return (ret);
196 	}
197 
198 	if (LF_ISSET(DB_STAT_ALL) &&
199 	    (ret = __txn_print_all(env, orig_flags)) != 0)
200 		return (ret);
201 
202 	return (0);
203 }
204 
205 /*
206  * __txn_print_stats --
207  *	Display default transaction region statistics.
208  */
209 static int
__txn_print_stats(env,flags)210 __txn_print_stats(env, flags)
211 	ENV *env;
212 	u_int32_t flags;
213 {
214 	DB_ENV *dbenv;
215 	DB_MSGBUF mb;
216 	DB_TXN_ACTIVE *txn;
217 	DB_TXN_STAT *sp;
218 	u_int32_t i;
219 	int ret;
220 	char buf[DB_THREADID_STRLEN], time_buf[CTIME_BUFLEN];
221 
222 	dbenv = env->dbenv;
223 
224 	if ((ret = __txn_stat(env, &sp, flags)) != 0)
225 		return (ret);
226 
227 	if (LF_ISSET(DB_STAT_ALL))
228 		__db_msg(env, "Default transaction region information:");
229 	__db_msg(env, "%lu/%lu\t%s",
230 	    (u_long)sp->st_last_ckp.file, (u_long)sp->st_last_ckp.offset,
231 	    sp->st_last_ckp.file == 0 ?
232 	    "No checkpoint LSN" : "File/offset for last checkpoint LSN");
233 	if (sp->st_time_ckp == 0)
234 		__db_msg(env, "0\tNo checkpoint timestamp");
235 	else
236 		__db_msg(env, "%.24s\tCheckpoint timestamp",
237 		    __os_ctime(&sp->st_time_ckp, time_buf));
238 	__db_msg(env, "%#lx\tLast transaction ID allocated",
239 	    (u_long)sp->st_last_txnid);
240 	__db_dl(env, "Maximum number of active transactions configured",
241 	    (u_long)sp->st_maxtxns);
242 	__db_dl(env, "Initial number of transactions configured",
243 	    (u_long)sp->st_inittxns);
244 	__db_dl(env, "Active transactions", (u_long)sp->st_nactive);
245 	__db_dl(env,
246 	    "Maximum active transactions", (u_long)sp->st_maxnactive);
247 	__db_dl(env,
248 	    "Number of transactions begun", (u_long)sp->st_nbegins);
249 	__db_dl(env,
250 	    "Number of transactions aborted", (u_long)sp->st_naborts);
251 	__db_dl(env,
252 	    "Number of transactions committed", (u_long)sp->st_ncommits);
253 	__db_dl(env, "Snapshot transactions", (u_long)sp->st_nsnapshot);
254 	__db_dl(env, "Maximum snapshot transactions",
255 	    (u_long)sp->st_maxnsnapshot);
256 	__db_dl(env,
257 	    "Number of transactions restored", (u_long)sp->st_nrestores);
258 
259 	__db_dlbytes(env, "Region size",
260 	    (u_long)0, (u_long)0, (u_long)sp->st_regsize);
261 	__db_dl_pct(env,
262 	    "The number of region locks that required waiting",
263 	    (u_long)sp->st_region_wait, DB_PCT(sp->st_region_wait,
264 	    sp->st_region_wait + sp->st_region_nowait), NULL);
265 
266 	qsort(sp->st_txnarray,
267 	    sp->st_nactive, sizeof(sp->st_txnarray[0]), __txn_compare);
268 	__db_msg(env, "Active transactions:");
269 	DB_MSGBUF_INIT(&mb);
270 	for (i = 0; i < sp->st_nactive; ++i) {
271 		txn = &sp->st_txnarray[i];
272 		__db_msgadd(env, &mb, "\t%lx: %s; xa_status %s;"
273 		    " pid/thread %s; begin LSN: file/offset %lu/%lu",
274 		    (u_long)txn->txnid, __txn_status(txn), __txn_xa_status(txn),
275 		    dbenv->thread_id_string(dbenv, txn->pid, txn->tid, buf),
276 		    (u_long)txn->lsn.file, (u_long)txn->lsn.offset);
277 		if (txn->parentid != 0)
278 			__db_msgadd(env, &mb,
279 			    "; parent: %lx", (u_long)txn->parentid);
280 		if (!IS_MAX_LSN(txn->read_lsn))
281 			__db_msgadd(env, &mb, "; read LSN: %lu/%lu",
282 			    (u_long)txn->read_lsn.file,
283 			    (u_long)txn->read_lsn.offset);
284 		if (txn->mvcc_ref != 0)
285 			__db_msgadd(env, &mb,
286 			    "; mvcc refcount: %lu", (u_long)txn->mvcc_ref);
287 		if (LOCKING_ON(env))
288 			__db_msgadd(env, &mb,
289 			    "; priority: %lu", (u_long)txn->priority);
290 		if (txn->name[0] != '\0')
291 			__db_msgadd(env, &mb, "; \"%s\"", txn->name);
292 		if (txn->status == TXN_PREPARE)
293 			__txn_gid(env, &mb, txn);
294 		DB_MSGBUF_FLUSH(env, &mb);
295 	}
296 
297 	__os_ufree(env, sp);
298 
299 	return (0);
300 }
301 
302 /*
303  * __txn_print_all --
304  *	Display debugging transaction region statistics.
305  */
306 static int
__txn_print_all(env,flags)307 __txn_print_all(env, flags)
308 	ENV *env;
309 	u_int32_t flags;
310 {
311 	static const FN fn[] = {
312 		{ TXN_IN_RECOVERY,	"TXN_IN_RECOVERY" },
313 		{ 0,			NULL }
314 	};
315 	DB_TXNMGR *mgr;
316 	DB_TXNREGION *region;
317 	char time_buf[CTIME_BUFLEN];
318 
319 	mgr = env->tx_handle;
320 	region = mgr->reginfo.primary;
321 
322 	TXN_SYSTEM_LOCK(env);
323 
324 	__db_print_reginfo(env, &mgr->reginfo, "Transaction", flags);
325 
326 	__db_msg(env, "%s", DB_GLOBAL(db_line));
327 	__db_msg(env, "DB_TXNMGR handle information:");
328 	__mutex_print_debug_single(env, "DB_TXNMGR mutex", mgr->mutex, flags);
329 	__db_dl(env,
330 	    "Number of transactions discarded", (u_long)mgr->n_discards);
331 
332 	__db_msg(env, "%s", DB_GLOBAL(db_line));
333 	__db_msg(env, "DB_TXNREGION handle information:");
334 	__mutex_print_debug_single(
335 	    env, "DB_TXNREGION region mutex", region->mtx_region, flags);
336 	STAT_ULONG("Maximum number of active txns", region->maxtxns);
337 	STAT_HEX("Last transaction ID allocated", region->last_txnid);
338 	STAT_HEX("Current maximum unused ID", region->cur_maxid);
339 
340 	__mutex_print_debug_single(
341 	    env, "checkpoint mutex", region->mtx_ckp, flags);
342 	STAT_LSN("Last checkpoint LSN", &region->last_ckp);
343 	__db_msg(env,
344 	    "%.24s\tLast checkpoint timestamp",
345 	    region->time_ckp == 0 ? "0" :
346 	    __os_ctime(&region->time_ckp, time_buf));
347 
348 	__db_prflags(env, NULL, region->flags, fn, NULL, "\tFlags");
349 
350 	__db_msg(env, "%s", DB_GLOBAL(db_line));
351 	TXN_SYSTEM_UNLOCK(env);
352 
353 	return (0);
354 }
355 
356 static char *
__txn_status(txn)357 __txn_status(txn)
358 	DB_TXN_ACTIVE *txn;
359 {
360 	switch (txn->status) {
361 	case TXN_ABORTED:
362 		return ("aborted");
363 	case TXN_COMMITTED:
364 		return ("committed");
365 	case TXN_NEED_ABORT:
366 		return ("need abort");
367 	case TXN_PREPARED:
368 		return ("prepared");
369 	case TXN_RUNNING:
370 		return ("running");
371 	default:
372 		break;
373 	}
374 	return ("unknown state");
375 }
376 
377 static char *
__txn_xa_status(txn)378 __txn_xa_status(txn)
379 	DB_TXN_ACTIVE *txn;
380 {
381 	switch (txn->xa_status) {
382 	case TXN_XA_ACTIVE:
383 		return ("xa active");
384 	case TXN_XA_DEADLOCKED:
385 		return ("xa deadlock");
386 	case TXN_XA_IDLE:
387 		return ("xa idle");
388 	case TXN_XA_PREPARED:
389 		return ("xa prepared");
390 	case TXN_XA_ROLLEDBACK:
391 		return ("xa rollback");
392 	default:
393 		break;
394 	}
395 	return ("no xa state");
396 }
397 
398 static void
__txn_gid(env,mbp,txn)399 __txn_gid(env, mbp, txn)
400 	ENV *env;
401 	DB_MSGBUF *mbp;
402 	DB_TXN_ACTIVE *txn;
403 {
404 	u_int32_t v, *xp;
405 	u_int i;
406 	int cnt;
407 
408 	__db_msgadd(env, mbp, "\n\tGID:");
409 	for (cnt = 0, xp = (u_int32_t *)txn->gid, i = 0;;) {
410 		memcpy(&v, xp++, sizeof(u_int32_t));
411 		__db_msgadd(env, mbp, "%#lx ", (u_long)v);
412 		if ((i += sizeof(u_int32_t)) >= DB_GID_SIZE)
413 			break;
414 		if (++cnt == 4) {
415 			DB_MSGBUF_FLUSH(env, mbp);
416 			__db_msgadd(env, mbp, "\t\t");
417 			cnt = 0;
418 		}
419 	}
420 }
421 
422 static int
__txn_compare(a1,b1)423 __txn_compare(a1, b1)
424 	const void *a1, *b1;
425 {
426 	const DB_TXN_ACTIVE *a, *b;
427 
428 	a = a1;
429 	b = b1;
430 
431 	if (a->txnid > b->txnid)
432 		return (1);
433 	if (a->txnid < b->txnid)
434 		return (-1);
435 	return (0);
436 }
437 
438 #else /* !HAVE_STATISTICS */
439 
440 int
__txn_stat_pp(dbenv,statp,flags)441 __txn_stat_pp(dbenv, statp, flags)
442 	DB_ENV *dbenv;
443 	DB_TXN_STAT **statp;
444 	u_int32_t flags;
445 {
446 	COMPQUIET(statp, NULL);
447 	COMPQUIET(flags, 0);
448 
449 	return (__db_stat_not_built(dbenv->env));
450 }
451 
452 int
__txn_stat_print_pp(dbenv,flags)453 __txn_stat_print_pp(dbenv, flags)
454 	DB_ENV *dbenv;
455 	u_int32_t flags;
456 {
457 	COMPQUIET(flags, 0);
458 
459 	return (__db_stat_not_built(dbenv->env));
460 }
461 #endif
462