summaryrefslogtreecommitdiff
path: root/examples/java/com/wiredtiger/examples/ex_stat.java
blob: 799f039675644e1c37797c4f80406dddcf8ab1f0 (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
/*-
 * Public Domain 2014-2017 MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * ex_stat.java
 *	This is an example demonstrating how to query database statistics.
 */
package com.wiredtiger.examples;
import com.wiredtiger.db.*;
import java.io.*;
import java.util.*;

public class ex_stat {

    public static String home;

    /*! [statistics display function] */
    int
    print_cursor(Cursor cursor)
        throws WiredTigerException
    {
        String desc, pvalue;
        long value;
        int ret;

        while ((ret = cursor.next()) == 0) {
            desc = cursor.getValueString();
            pvalue = cursor.getValueString();
            value = cursor.getValueLong();
            if (value != 0)
                System.out.println(desc + "=" + pvalue);
        }

        return (ret == wiredtiger.WT_NOTFOUND ? 0 : ret);
    }
    /*! [statistics display function] */

    int 
    print_database_stats(Session session)
        throws WiredTigerException
    {
        Cursor cursor;
        int ret;

        /*! [statistics database function] */
        cursor = session.open_cursor("statistics:", null, null);

        ret = print_cursor(cursor);
        ret = cursor.close();
        /*! [statistics database function] */

        return (ret);
    }

    int 
    print_file_stats(Session session)
        throws WiredTigerException
    {
        Cursor cursor;
        int ret;

        /*! [statistics table function] */
        cursor = session.open_cursor("statistics:table:access", null, null);
        ret = print_cursor(cursor);
        ret = cursor.close();
        /*! [statistics table function] */

        return (ret);
    }

    int 
    print_join_cursor_stats(Session session)
        throws WiredTigerException
    {
	Cursor idx_cursor, join_cursor, stat_cursor;
	int ret;

	ret = session.create("index:access:idx", "columns=(v)");
	idx_cursor = session.open_cursor("index:access:idx", null, null);
	ret = idx_cursor.next();
	join_cursor = session.open_cursor("join:table:access", null, null);
	ret = session.join(join_cursor, idx_cursor, "compare=gt");
	ret = join_cursor.next();

	/*! [statistics join cursor function] */
	stat_cursor = session.open_cursor("statistics:join", join_cursor, null);

	ret = print_cursor(stat_cursor);
	ret = stat_cursor.close();
	/*! [statistics join cursor function] */

	ret = join_cursor.close();
	ret = idx_cursor.close();

	return (ret);
    }

    int
    print_overflow_pages(Session session)
        throws WiredTigerException
    {
        /*! [statistics retrieve by key] */
        Cursor cursor;
        String desc, pvalue;
        long value;
        int ret;

        cursor = session.open_cursor("statistics:table:access", null, null);

        cursor.putKeyInt(wiredtiger.WT_STAT_DSRC_BTREE_OVERFLOW);
        ret = cursor.search();
        desc = cursor.getValueString();
        pvalue = cursor.getValueString();
        value = cursor.getValueLong();
        System.out.println(desc + "=" + pvalue);

        ret = cursor.close();
        /*! [statistics retrieve by key] */

        return (ret);
    }

    /*! [statistics calculation helper function] */
    long
    get_stat(Cursor cursor, int stat_field)
        throws WiredTigerException
    {
        long value;
        int ret;

        cursor.putKeyInt(stat_field);
        if ((ret = cursor.search()) != 0) {
            System.err.println("stat_field: " + stat_field + " not found");
            value = 0;
        }
        else {
            String desc = cursor.getValueString();
            String pvalue = cursor.getValueString();
            value = cursor.getValueLong();
        }
        return (value);
    }
    /*! [statistics calculation helper function] */

    int
    print_derived_stats(Session session)
        throws WiredTigerException
    {
        Cursor cursor;
        int ret;

        /*! [statistics calculate open table stats] */
        cursor = session.open_cursor("statistics:table:access", null, null);
        /*! [statistics calculate open table stats] */

            {
                /*! [statistics calculate table fragmentation] */
                long ckpt_size = get_stat(cursor,
                    wiredtiger.WT_STAT_DSRC_BLOCK_CHECKPOINT_SIZE);
                long file_size = get_stat(cursor,
                    wiredtiger.WT_STAT_DSRC_BLOCK_SIZE);

                System.out.println("File is " +
                    (int)(100 * (file_size - ckpt_size) / file_size) +
                    "% fragmented\n");
                /*! [statistics calculate table fragmentation] */
            }

                {
                    /*! [statistics calculate write amplification] */
                    long app_insert = get_stat(cursor,
                        wiredtiger.WT_STAT_DSRC_CURSOR_INSERT_BYTES);
                    long app_remove = get_stat(cursor,
                        wiredtiger.WT_STAT_DSRC_CURSOR_REMOVE_BYTES);
                    long app_update = get_stat(cursor,
                        wiredtiger.WT_STAT_DSRC_CURSOR_UPDATE_BYTES);

                    long fs_writes = get_stat(cursor,
                        wiredtiger.WT_STAT_DSRC_CACHE_BYTES_WRITE);

                    if (app_insert + app_remove + app_update != 0)
                        System.out.println("Write amplification is " +
                            (double)fs_writes / (app_insert + app_remove + app_update));
                    /*! [statistics calculate write amplification] */
                }

                ret = cursor.close();

                return (ret);
    }

    public int
    statExample()
        throws WiredTigerException
    {
        Connection conn;
        Cursor cursor;
        Session session;
        int ret;

        /*
         * Create a clean test directory for this run of the test program if the
         * environment variable isn't already set (as is done by make check).
         */
        if (System.getenv("WIREDTIGER_HOME") == null) {
            home = "WT_HOME";
            try {
                Process proc = Runtime.getRuntime().exec("/bin/rm -rf " + home);
                BufferedReader br = new BufferedReader(
                    new InputStreamReader(proc.getInputStream()));
                while(br.ready())
                    System.out.println(br.readLine());
                br.close();
                proc.waitFor();
                if (!(new File(home)).mkdir())
                    System.err.println("mkdir: failed");
            } catch (Exception ex) {
                System.err.println("Exception: " + home + ": " + ex);
                System.exit(1);
            }
        } else
            home = null;

        conn = wiredtiger.open(home, "create,statistics=(all)");
        session = conn.open_session(null);

        ret = session.create("table:access",
            "key_format=S,value_format=S,columns=(k,v)");

        cursor = session.open_cursor("table:access", null, null);
        cursor.putKeyString("key");
        cursor.putValueString("value");
        ret = cursor.insert();
        ret = cursor.close();

        ret = session.checkpoint(null);

        ret = print_database_stats(session);

        ret = print_file_stats(session);

        ret = print_join_cursor_stats(session);

        ret = print_overflow_pages(session);

        ret = print_derived_stats(session);

        return (conn.close(null) == 0 ? ret : -1);
    }

    public static void
    main(String[] argv)
    {
        try {
            System.exit((new ex_stat()).statExample());
        }
        catch (WiredTigerException wte) {
            System.err.println("Exception: " + wte);
            wte.printStackTrace();
            System.exit(1);
        }
    }
}