summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/schema/schema_stat.c
blob: c204d6b1a2432e9717facc1668687aed6fb86da1 (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
/*-
 * Copyright (c) 2014-2016 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __wt_curstat_colgroup_init --
 *	Initialize the statistics for a column group.
 */
int
__wt_curstat_colgroup_init(WT_SESSION_IMPL *session,
    const char *uri, const char *cfg[], WT_CURSOR_STAT *cst)
{
	WT_COLGROUP *colgroup;
	WT_DECL_ITEM(buf);
	WT_DECL_RET;

	WT_RET(__wt_schema_get_colgroup(session, uri, false, NULL, &colgroup));

	WT_RET(__wt_scr_alloc(session, 0, &buf));
	WT_ERR(__wt_buf_fmt(session, buf, "statistics:%s", colgroup->source));
	ret = __wt_curstat_init(session, buf->data, NULL, cfg, cst);

err:	__wt_scr_free(session, &buf);
	return (ret);
}

/*
 * __wt_curstat_index_init --
 *	Initialize the statistics for an index.
 */
int
__wt_curstat_index_init(WT_SESSION_IMPL *session,
    const char *uri, const char *cfg[], WT_CURSOR_STAT *cst)
{
	WT_DECL_ITEM(buf);
	WT_DECL_RET;
	WT_INDEX *idx;

	WT_RET(__wt_schema_get_index(session, uri, false, NULL, &idx));

	WT_RET(__wt_scr_alloc(session, 0, &buf));
	WT_ERR(__wt_buf_fmt(session, buf, "statistics:%s", idx->source));
	ret = __wt_curstat_init(session, buf->data, NULL, cfg, cst);

err:	__wt_scr_free(session, &buf);
	return (ret);
}

/*
 * __curstat_size_only --
 *	 For very simple tables we can avoid getting table handles if
 *	 configured to only retrieve the size. It's worthwhile because
 *	 workloads that create and drop a lot of tables can put a lot of
 *	 pressure on the table list lock.
 */
static int
__curstat_size_only(WT_SESSION_IMPL *session,
    const char *uri, bool *was_fast,WT_CURSOR_STAT *cst)
{
	WT_CONFIG cparser;
	WT_CONFIG_ITEM ckey, colconf, cval;
	WT_DECL_RET;
	WT_ITEM namebuf;
	wt_off_t filesize;
	char *tableconf;
	bool exist;

	WT_CLEAR(namebuf);
	*was_fast = false;

	/* Retrieve the metadata for this table. */
	WT_RET(__wt_metadata_search(session, uri, &tableconf));

	/*
	 * The fast path only works if the table consists of a single file
	 * and does not have any indexes. The absence of named columns is how
	 * we determine that neither of those conditions can be satisfied.
	 */
	WT_ERR(__wt_config_getones(session, tableconf, "columns", &colconf));
	WT_ERR(__wt_config_subinit(session, &cparser, &colconf));
	if ((ret = __wt_config_next(&cparser, &ckey, &cval)) == 0)
		goto err;

	/* Build up the file name from the table URI. */
	WT_ERR(__wt_buf_fmt(
	    session, &namebuf, "%s.wt", uri + strlen("table:")));

	/*
	 * Get the size of the underlying file. This will fail for anything
	 * other than simple tables (LSM for example) and will fail if there
	 * are concurrent schema level operations (for example drop). That is
	 * fine - failing here results in falling back to the slow path of
	 * opening the handle.
	 */
	WT_ERR(__wt_fs_exist(session, namebuf.data, &exist));
	if (exist) {
		WT_ERR(__wt_fs_size(session, namebuf.data, &filesize));

		/* Setup and populate the statistics structure */
		__wt_stat_dsrc_init_single(&cst->u.dsrc_stats);
		cst->u.dsrc_stats.block_size = filesize;
		__wt_curstat_dsrc_final(cst);

		*was_fast = true;
	}

err:	__wt_free(session, tableconf);
	__wt_buf_free(session, &namebuf);

	return (ret);
}

/*
 * __wt_curstat_table_init --
 *	Initialize the statistics for a table.
 */
int
__wt_curstat_table_init(WT_SESSION_IMPL *session,
    const char *uri, const char *cfg[], WT_CURSOR_STAT *cst)
{
	WT_CURSOR *stat_cursor;
	WT_DECL_ITEM(buf);
	WT_DECL_RET;
	WT_DSRC_STATS *new, *stats;
	WT_TABLE *table;
	u_int i;
	const char *name;
	bool was_fast;

	/*
	 * If only gathering table size statistics, try a fast path that
	 * avoids the schema and table list locks.
	 */
	if (F_ISSET(cst, WT_CONN_STAT_SIZE)) {
		WT_RET(__curstat_size_only(session, uri, &was_fast, cst));
		if (was_fast)
			return (0);
	}

	name = uri + strlen("table:");
	WT_RET(__wt_schema_get_table(
	    session, name, strlen(name), false, &table));

	WT_ERR(__wt_scr_alloc(session, 0, &buf));

	/*
	 * Process the column groups.
	 *
	 * Set the cursor to reference the data source statistics; we don't
	 * initialize it, instead we copy (rather than aggregate), the first
	 * column's statistics, which has the same effect.
	 */
	stats = &cst->u.dsrc_stats;
	for (i = 0; i < WT_COLGROUPS(table); i++) {
		WT_ERR(__wt_buf_fmt(
		    session, buf, "statistics:%s", table->cgroups[i]->name));
		WT_ERR(__wt_curstat_open(
		    session, buf->data, NULL, cfg, &stat_cursor));
		new = (WT_DSRC_STATS *)WT_CURSOR_STATS(stat_cursor);
		if (i == 0)
			*stats = *new;
		else
			__wt_stat_dsrc_aggregate_single(new, stats);
		WT_ERR(stat_cursor->close(stat_cursor));
	}

	/* Process the indices. */
	WT_ERR(__wt_schema_open_indices(session, table));
	for (i = 0; i < table->nindices; i++) {
		WT_ERR(__wt_buf_fmt(
		    session, buf, "statistics:%s", table->indices[i]->name));
		WT_ERR(__wt_curstat_open(
		    session, buf->data, NULL, cfg, &stat_cursor));
		new = (WT_DSRC_STATS *)WT_CURSOR_STATS(stat_cursor);
		__wt_stat_dsrc_aggregate_single(new, stats);
		WT_ERR(stat_cursor->close(stat_cursor));
	}

	__wt_curstat_dsrc_final(cst);

err:	__wt_schema_release_table(session, table);

	__wt_scr_free(session, &buf);
	return (ret);
}