summaryrefslogtreecommitdiff
path: root/dist/stat.py
blob: a70c09e8a63ce7aa53464505b024a933c686e4de (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
# Read the source files and output the statistics #defines and allocation code.

import re, string, sys, textwrap
from dist import compare_srcfile
from dist import source_paths_list

# Read the source files.
from stat_data import dsrc_stats, connection_stats

def print_struct(title, name, stats):
	'''Print the structures for the stat.h file.'''
	f.write('/*\n')
	f.write(' * Statistics entries for ' + title + '.\n')
	f.write(' */\n')
	f.write('struct __wt_' + name + '_stats {\n')

	for l in stats:
		f.write('\tWT_STATS ' + l.name + ';\n')
	f.write('};\n\n')

# Update the #defines in the stat.h file.
tmp_file = '__tmp'
f = open(tmp_file, 'w')
skip = 0
for line in open('../src/include/stat.h', 'r'):
	if not skip:
		f.write(line)
	if line.count('Statistics section: END'):
		f.write(line)
		skip = 0
	elif line.count('Statistics section: BEGIN'):
		f.write('\n')
		skip = 1
		print_struct('data sources', 'dsrc', dsrc_stats)
		print_struct('connections', 'connection', connection_stats)
f.close()
compare_srcfile(tmp_file, '../src/include/stat.h')

def print_defines():
	'''Print the #defines for the wiredtiger.in file.'''
	f.write('''
/*!
 * @name Connection statistics
 * @anchor statistics_keys
 * @anchor statistics_conn
 * Statistics are accessed through cursors with \c "statistics:" URIs.
 * Individual statistics can be queried through the cursor using the following
 * keys.
 * @{
 */
''')
	for v, l in enumerate(connection_stats):
		f.write('/*! %s */\n' % '\n * '.join(textwrap.wrap(l.desc, 70)))
		f.write('#define\tWT_STAT_CONN_' + l.name.upper() + "\t" *
		    max(1, 6 - int((len('WT_STAT_CONN_' + l.name)) / 8)) +
		    str(v) + '\n')
	f.write('''
/*!
 * @}
 * @name Statistics for data sources
 * @anchor statistics_dsrc
 * @{
 */
''')
	for v, l in enumerate(dsrc_stats):
		f.write('/*! %s */\n' % '\n * '.join(textwrap.wrap(l.desc, 70)))
		f.write('#define\tWT_STAT_DSRC_' + l.name.upper() + "\t" *
		    max(1, 6 - int((len('WT_STAT_DSRC_' + l.name)) / 8)) +
		    str(v) + '\n')
	f.write('/*! @} */\n')

# Update the #defines in the wiredtiger.in file.
tmp_file = '__tmp'
f = open(tmp_file, 'w')
skip = 0
for line in open('../src/include/wiredtiger.in', 'r'):
	if not skip:
		f.write(line)
	if line.count('Statistics section: END'):
		f.write(line)
		skip = 0
	elif line.count('Statistics section: BEGIN'):
		f.write(' */\n')
		skip = 1
		print_defines()
		f.write('/*\n')
f.close()
compare_srcfile(tmp_file, '../src/include/wiredtiger.in')

def print_func(name, list):
	'''Print the functions for the stat.c file.'''
	f.write('''
int
__wt_stat_alloc_''' + name + '''_stats(WT_SESSION_IMPL *session, WT_''' +
	    name.upper() + '''_STATS **statsp)
{
\tWT_''' + name.upper() + '''_STATS *stats;

\tWT_RET(__wt_calloc_def(session, 1, &stats));

''')

	for l in sorted(list):
		o = '\tstats->' + l.name + '.desc = "' + l.desc + '";\n'
		if len(o) + 7  > 80:
			o = o.replace('= ', '=\n\t    ')
		f.write(o)
	f.write('''
\t*statsp = stats;
\treturn (0);
}
''')

	f.write('''
void
__wt_stat_clear_''' + name + '''_stats(WT_STATS *stats_arg)
{
\tWT_''' + name.upper() + '''_STATS *stats;

\tstats = (WT_''' + name.upper() + '''_STATS *)stats_arg;
''')
	for l in sorted(list):
		# Items marked permanent aren't cleared by the stat clear
		# methods.
		if not l.flags.get('perm', 0):
			f.write('\tstats->' + l.name + '.v = 0;\n');
	f.write('}\n')

# Write the stat allocation and clear routines to the stat.c file.
f = open(tmp_file, 'w')
f.write('/* DO NOT EDIT: automatically built by dist/stat.py. */\n\n')
f.write('#include "wt_internal.h"\n')

print_func('dsrc', dsrc_stats)
print_func('connection', connection_stats)
f.close()
compare_srcfile(tmp_file, '../src/support/stat.c')