summaryrefslogtreecommitdiff
path: root/src/txn/txn_stat.c
blob: 62fe622d3615fa011201b0744902ebd345952efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/db_am.h"
#include "dbinc/txn.h"

#ifdef HAVE_STATISTICS
static int  __txn_compare __P((const void *, const void *));
static int  __txn_print_all __P((ENV *, u_int32_t));
static int  __txn_print_stats __P((ENV *, u_int32_t));
static int  __txn_stat __P((ENV *, DB_TXN_STAT **, u_int32_t));
static char *__txn_status __P((DB_TXN_ACTIVE *));
static char *__txn_xa_status __P((DB_TXN_ACTIVE *));
static void __txn_gid __P((ENV *, DB_MSGBUF *, DB_TXN_ACTIVE *));

/*
 * __txn_stat_pp --
 *	DB_ENV->txn_stat pre/post processing.
 *
 * PUBLIC: int __txn_stat_pp __P((DB_ENV *, DB_TXN_STAT **, u_int32_t));
 */
int
__txn_stat_pp(dbenv, statp, flags)
	DB_ENV *dbenv;
	DB_TXN_STAT **statp;
	u_int32_t flags;
{
	DB_THREAD_INFO *ip;
	ENV *env;
	int ret;

	env = dbenv->env;

	ENV_REQUIRES_CONFIG(env,
	    env->tx_handle, "DB_ENV->txn_stat", DB_INIT_TXN);

	if ((ret = __db_fchk(env,
	    "DB_ENV->txn_stat", flags, DB_STAT_CLEAR)) != 0)
		return (ret);

	ENV_ENTER(env, ip);
	REPLICATION_WRAP(env, (__txn_stat(env, statp, flags)), 0, ret);
	ENV_LEAVE(env, ip);
	return (ret);
}

/*
 * __txn_stat --
 *	ENV->txn_stat.
 */
static int
__txn_stat(env, statp, flags)
	ENV *env;
	DB_TXN_STAT **statp;
	u_int32_t flags;
{
	DB_TXNMGR *mgr;
	DB_TXNREGION *region;
	DB_TXN_STAT *stats;
	TXN_DETAIL *td;
	size_t nbytes;
	u_int32_t maxtxn, ndx;
	int ret;

	*statp = NULL;
	mgr = env->tx_handle;
	region = mgr->reginfo.primary;

	TXN_SYSTEM_LOCK(env);
	maxtxn = region->curtxns;
	nbytes = sizeof(DB_TXN_STAT) + sizeof(DB_TXN_ACTIVE) * maxtxn;
	if ((ret = __os_umalloc(env, nbytes, &stats)) != 0) {
		TXN_SYSTEM_UNLOCK(env);
		return (ret);
	}

	memcpy(stats, &region->stat, sizeof(region->stat));
	stats->st_last_txnid = region->last_txnid;
	stats->st_last_ckp = region->last_ckp;
	stats->st_time_ckp = region->time_ckp;
	stats->st_txnarray = (DB_TXN_ACTIVE *)&stats[1];

	for (ndx = 0,
	    td = SH_TAILQ_FIRST(&region->active_txn, __txn_detail);
	    td != NULL && ndx < maxtxn;
	    td = SH_TAILQ_NEXT(td, links, __txn_detail), ++ndx) {
		stats->st_txnarray[ndx].txnid = td->txnid;
		if (td->parent == INVALID_ROFF)
			stats->st_txnarray[ndx].parentid = TXN_INVALID;
		else
			stats->st_txnarray[ndx].parentid =
			    ((TXN_DETAIL *)R_ADDR(&mgr->reginfo,
			    td->parent))->txnid;
		stats->st_txnarray[ndx].pid = td->pid;
		stats->st_txnarray[ndx].tid = td->tid;
		stats->st_txnarray[ndx].lsn = td->begin_lsn;
		stats->st_txnarray[ndx].read_lsn = td->read_lsn;
		stats->st_txnarray[ndx].mvcc_ref = td->mvcc_ref;
		stats->st_txnarray[ndx].status = td->status;
		stats->st_txnarray[ndx].xa_status = td->xa_br_status;
		stats->st_txnarray[ndx].priority = td->priority;

		if (td->status == TXN_PREPARED)
			memcpy(stats->st_txnarray[ndx].gid,
			    td->gid, sizeof(td->gid));
		if (td->name != INVALID_ROFF) {
			(void)strncpy(stats->st_txnarray[ndx].name,
			    R_ADDR(&mgr->reginfo, td->name),
			    sizeof(stats->st_txnarray[ndx].name) - 1);
			stats->st_txnarray[ndx].name[
			    sizeof(stats->st_txnarray[ndx].name) - 1] = '\0';
		} else
			stats->st_txnarray[ndx].name[0] = '\0';
	}

	__mutex_set_wait_info(env, region->mtx_region,
	    &stats->st_region_wait, &stats->st_region_nowait);
	stats->st_regsize = (roff_t)mgr->reginfo.rp->size;
	if (LF_ISSET(DB_STAT_CLEAR)) {
		if (!LF_ISSET(DB_STAT_SUBSYSTEM))
			__mutex_clear(env, region->mtx_region);
		memset(&region->stat, 0, sizeof(region->stat));
		region->stat.st_maxtxns = region->maxtxns;
		region->stat.st_inittxns = region->inittxns;
		region->stat.st_maxnactive =
		    region->stat.st_nactive = stats->st_nactive;
		region->stat.st_maxnsnapshot =
		    region->stat.st_nsnapshot = stats->st_nsnapshot;
	}

	TXN_SYSTEM_UNLOCK(env);

	*statp = stats;
	return (0);
}

/*
 * __txn_stat_print_pp --
 *	DB_ENV->txn_stat_print pre/post processing.
 *
 * PUBLIC: int __txn_stat_print_pp __P((DB_ENV *, u_int32_t));
 */
int
__txn_stat_print_pp(dbenv, flags)
	DB_ENV *dbenv;
	u_int32_t flags;
{
	DB_THREAD_INFO *ip;
	ENV *env;
	int ret;

	env = dbenv->env;

	ENV_REQUIRES_CONFIG(env,
	    env->tx_handle, "DB_ENV->txn_stat_print", DB_INIT_TXN);

	if ((ret = __db_fchk(env, "DB_ENV->txn_stat_print",
	    flags, DB_STAT_ALL | DB_STAT_ALLOC | DB_STAT_CLEAR)) != 0)
		return (ret);

	ENV_ENTER(env, ip);
	REPLICATION_WRAP(env, (__txn_stat_print(env, flags)), 0, ret);
	ENV_LEAVE(env, ip);
	return (ret);
}

/*
 * __txn_stat_print
 *	ENV->txn_stat_print method.
 *
 * PUBLIC: int  __txn_stat_print __P((ENV *, u_int32_t));
 */
int
__txn_stat_print(env, flags)
	ENV *env;
	u_int32_t flags;
{
	u_int32_t orig_flags;
	int ret;

	orig_flags = flags;
	LF_CLR(DB_STAT_CLEAR | DB_STAT_SUBSYSTEM);
	if (flags == 0 || LF_ISSET(DB_STAT_ALL)) {
		ret = __txn_print_stats(env, orig_flags);
		if (flags == 0 || ret != 0)
			return (ret);
	}

	if (LF_ISSET(DB_STAT_ALL) &&
	    (ret = __txn_print_all(env, orig_flags)) != 0)
		return (ret);

	return (0);
}

/*
 * __txn_print_stats --
 *	Display default transaction region statistics.
 */
static int
__txn_print_stats(env, flags)
	ENV *env;
	u_int32_t flags;
{
	DB_ENV *dbenv;
	DB_MSGBUF mb;
	DB_TXN_ACTIVE *txn;
	DB_TXN_STAT *sp;
	u_int32_t i;
	int ret;
	char buf[DB_THREADID_STRLEN], time_buf[CTIME_BUFLEN];

	dbenv = env->dbenv;

	if ((ret = __txn_stat(env, &sp, flags)) != 0)
		return (ret);

	if (LF_ISSET(DB_STAT_ALL))
		__db_msg(env, "Default transaction region information:");
	__db_msg(env, "%lu/%lu\t%s",
	    (u_long)sp->st_last_ckp.file, (u_long)sp->st_last_ckp.offset,
	    sp->st_last_ckp.file == 0 ?
	    "No checkpoint LSN" : "File/offset for last checkpoint LSN");
	if (sp->st_time_ckp == 0)
		__db_msg(env, "0\tNo checkpoint timestamp");
	else
		__db_msg(env, "%.24s\tCheckpoint timestamp",
		    __os_ctime(&sp->st_time_ckp, time_buf));
	__db_msg(env, "%#lx\tLast transaction ID allocated",
	    (u_long)sp->st_last_txnid);
	__db_dl(env, "Maximum number of active transactions configured",
	    (u_long)sp->st_maxtxns);
	__db_dl(env, "Initial number of transactions configured",
	    (u_long)sp->st_inittxns);
	__db_dl(env, "Active transactions", (u_long)sp->st_nactive);
	__db_dl(env,
	    "Maximum active transactions", (u_long)sp->st_maxnactive);
	__db_dl(env,
	    "Number of transactions begun", (u_long)sp->st_nbegins);
	__db_dl(env,
	    "Number of transactions aborted", (u_long)sp->st_naborts);
	__db_dl(env,
	    "Number of transactions committed", (u_long)sp->st_ncommits);
	__db_dl(env, "Snapshot transactions", (u_long)sp->st_nsnapshot);
	__db_dl(env, "Maximum snapshot transactions",
	    (u_long)sp->st_maxnsnapshot);
	__db_dl(env,
	    "Number of transactions restored", (u_long)sp->st_nrestores);

	__db_dlbytes(env, "Region size",
	    (u_long)0, (u_long)0, (u_long)sp->st_regsize);
	__db_dl_pct(env,
	    "The number of region locks that required waiting",
	    (u_long)sp->st_region_wait, DB_PCT(sp->st_region_wait,
	    sp->st_region_wait + sp->st_region_nowait), NULL);

	qsort(sp->st_txnarray,
	    sp->st_nactive, sizeof(sp->st_txnarray[0]), __txn_compare);
	__db_msg(env, "Active transactions:");
	DB_MSGBUF_INIT(&mb);
	for (i = 0; i < sp->st_nactive; ++i) {
		txn = &sp->st_txnarray[i];
		__db_msgadd(env, &mb, "\t%lx: %s; xa_status %s;"
		    " pid/thread %s; begin LSN: file/offset %lu/%lu",
		    (u_long)txn->txnid, __txn_status(txn), __txn_xa_status(txn),
		    dbenv->thread_id_string(dbenv, txn->pid, txn->tid, buf),
		    (u_long)txn->lsn.file, (u_long)txn->lsn.offset);
		if (txn->parentid != 0)
			__db_msgadd(env, &mb,
			    "; parent: %lx", (u_long)txn->parentid);
		if (!IS_MAX_LSN(txn->read_lsn))
			__db_msgadd(env, &mb, "; read LSN: %lu/%lu",
			    (u_long)txn->read_lsn.file,
			    (u_long)txn->read_lsn.offset);
		if (txn->mvcc_ref != 0)
			__db_msgadd(env, &mb,
			    "; mvcc refcount: %lu", (u_long)txn->mvcc_ref);
		if (LOCKING_ON(env))
			__db_msgadd(env, &mb,
			    "; priority: %lu", (u_long)txn->priority);
		if (txn->name[0] != '\0')
			__db_msgadd(env, &mb, "; \"%s\"", txn->name);
		if (txn->status == TXN_PREPARE)
			__txn_gid(env, &mb, txn);
		DB_MSGBUF_FLUSH(env, &mb);
	}

	__os_ufree(env, sp);

	return (0);
}

/*
 * __txn_print_all --
 *	Display debugging transaction region statistics.
 */
static int
__txn_print_all(env, flags)
	ENV *env;
	u_int32_t flags;
{
	static const FN fn[] = {
		{ TXN_IN_RECOVERY,	"TXN_IN_RECOVERY" },
		{ 0,			NULL }
	};
	DB_TXNMGR *mgr;
	DB_TXNREGION *region;
	char time_buf[CTIME_BUFLEN];

	mgr = env->tx_handle;
	region = mgr->reginfo.primary;

	TXN_SYSTEM_LOCK(env);

	__db_print_reginfo(env, &mgr->reginfo, "Transaction", flags);

	__db_msg(env, "%s", DB_GLOBAL(db_line));
	__db_msg(env, "DB_TXNMGR handle information:");
	__mutex_print_debug_single(env, "DB_TXNMGR mutex", mgr->mutex, flags);
	__db_dl(env,
	    "Number of transactions discarded", (u_long)mgr->n_discards);

	__db_msg(env, "%s", DB_GLOBAL(db_line));
	__db_msg(env, "DB_TXNREGION handle information:");
	__mutex_print_debug_single(
	    env, "DB_TXNREGION region mutex", region->mtx_region, flags);
	STAT_ULONG("Maximum number of active txns", region->maxtxns);
	STAT_HEX("Last transaction ID allocated", region->last_txnid);
	STAT_HEX("Current maximum unused ID", region->cur_maxid);

	__mutex_print_debug_single(
	    env, "checkpoint mutex", region->mtx_ckp, flags);
	STAT_LSN("Last checkpoint LSN", &region->last_ckp);
	__db_msg(env,
	    "%.24s\tLast checkpoint timestamp",
	    region->time_ckp == 0 ? "0" :
	    __os_ctime(&region->time_ckp, time_buf));

	__db_prflags(env, NULL, region->flags, fn, NULL, "\tFlags");

	__db_msg(env, "%s", DB_GLOBAL(db_line));
	TXN_SYSTEM_UNLOCK(env);

	return (0);
}

static char *
__txn_status(txn)
	DB_TXN_ACTIVE *txn;
{
	switch (txn->status) {
	case TXN_ABORTED:
		return ("aborted");
	case TXN_COMMITTED:
		return ("committed");
	case TXN_NEED_ABORT:
		return ("need abort");
	case TXN_PREPARED:
		return ("prepared");
	case TXN_RUNNING:
		return ("running");
	default:
		break;
	}
	return ("unknown state");
}

static char *
__txn_xa_status(txn)
	DB_TXN_ACTIVE *txn;
{
	switch (txn->xa_status) {
	case TXN_XA_ACTIVE:
		return ("xa active");
	case TXN_XA_DEADLOCKED:
		return ("xa deadlock");
	case TXN_XA_IDLE:
		return ("xa idle");
	case TXN_XA_PREPARED:
		return ("xa prepared");
	case TXN_XA_ROLLEDBACK:
		return ("xa rollback");
	default:
		break;
	}
	return ("no xa state");
}

static void
__txn_gid(env, mbp, txn)
	ENV *env;
	DB_MSGBUF *mbp;
	DB_TXN_ACTIVE *txn;
{
	u_int32_t v, *xp;
	u_int i;
	int cnt;

	__db_msgadd(env, mbp, "\n\tGID:");
	for (cnt = 0, xp = (u_int32_t *)txn->gid, i = 0;;) {
		memcpy(&v, xp++, sizeof(u_int32_t));
		__db_msgadd(env, mbp, "%#lx ", (u_long)v);
		if ((i += sizeof(u_int32_t)) >= DB_GID_SIZE)
			break;
		if (++cnt == 4) {
			DB_MSGBUF_FLUSH(env, mbp);
			__db_msgadd(env, mbp, "\t\t");
			cnt = 0;
		}
	}
}

static int
__txn_compare(a1, b1)
	const void *a1, *b1;
{
	const DB_TXN_ACTIVE *a, *b;

	a = a1;
	b = b1;

	if (a->txnid > b->txnid)
		return (1);
	if (a->txnid < b->txnid)
		return (-1);
	return (0);
}

#else /* !HAVE_STATISTICS */

int
__txn_stat_pp(dbenv, statp, flags)
	DB_ENV *dbenv;
	DB_TXN_STAT **statp;
	u_int32_t flags;
{
	COMPQUIET(statp, NULL);
	COMPQUIET(flags, 0);

	return (__db_stat_not_built(dbenv->env));
}

int
__txn_stat_print_pp(dbenv, flags)
	DB_ENV *dbenv;
	u_int32_t flags;
{
	COMPQUIET(flags, 0);

	return (__db_stat_not_built(dbenv->env));
}
#endif