Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

seq_stat.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2004-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: seq_stat.c,v 12.6 2005/10/07 20:21:39 ubell Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifdef HAVE_SEQUENCE
00013 #ifndef NO_SYSTEM_INCLUDES
00014 #include <sys/types.h>
00015 
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #endif
00019 
00020 #include "db_int.h"
00021 #include "dbinc/db_page.h"
00022 #include "dbinc/db_am.h"
00023 #include "dbinc_auto/sequence_ext.h"
00024 
00025 #ifdef HAVE_STATISTICS
00026 static int __seq_print_all __P((DB_SEQUENCE *, u_int32_t));
00027 static int __seq_print_stats __P((DB_SEQUENCE *, u_int32_t));
00028 
00029 /*
00030  * __seq_stat --
00031  *      Get statistics from the sequence.
00032  *
00033  * PUBLIC: int __seq_stat __P((DB_SEQUENCE *, DB_SEQUENCE_STAT **, u_int32_t));
00034  */
00035 int
00036 __seq_stat(seq, spp, flags)
00037         DB_SEQUENCE *seq;
00038         DB_SEQUENCE_STAT **spp;
00039         u_int32_t flags;
00040 {
00041         DB *dbp;
00042         DB_ENV *dbenv;
00043         DB_THREAD_INFO *ip;
00044         DB_SEQ_RECORD record;
00045         DB_SEQUENCE_STAT *sp;
00046         DBT data;
00047         int handle_check, ret, t_ret;
00048 
00049         dbp = seq->seq_dbp;
00050         dbenv = dbp->dbenv;
00051 
00052         switch (flags) {
00053         case DB_STAT_CLEAR:
00054         case DB_STAT_ALL:
00055         case 0:
00056                 break;
00057         default:
00058                 return (__db_ferr(dbenv, "DB_SEQUENCE->stat", 0));
00059         }
00060 
00061         ENV_ENTER(dbenv, ip);
00062 
00063         /* Check for replication block. */
00064         handle_check = IS_ENV_REPLICATED(dbenv);
00065         if (handle_check && (ret = __db_rep_enter(dbp, 1, 0, 0)) != 0) {
00066                 handle_check = 0;
00067                 goto err;
00068         }
00069 
00070         /* Allocate and clear the structure. */
00071         if ((ret = __os_umalloc(dbenv, sizeof(*sp), &sp)) != 0)
00072                 goto err;
00073         memset(sp, 0, sizeof(*sp));
00074 
00075         if (seq->mtx_seq != MUTEX_INVALID) {
00076                 __mutex_set_wait_info(
00077                     dbenv, seq->mtx_seq, &sp->st_wait, &sp->st_nowait);
00078 
00079                 if (LF_ISSET(DB_STAT_CLEAR))
00080                         __mutex_clear(dbenv, seq->mtx_seq);
00081         }
00082         memset(&data, 0, sizeof(data));
00083         data.data = &record;
00084         data.ulen = sizeof(record);
00085         data.flags = DB_DBT_USERMEM;
00086 retry:  if ((ret = __db_get(dbp, NULL, &seq->seq_key, &data, 0)) != 0) {
00087                 if (ret == DB_BUFFER_SMALL &&
00088                     data.size > sizeof(seq->seq_record)) {
00089                         if ((ret = __os_malloc(dbenv,
00090                             data.size, &data.data)) != 0)
00091                                 goto err;
00092                         data.ulen = data.size;
00093                         goto retry;
00094                 }
00095                 goto err;
00096         }
00097 
00098         if (data.data != &record)
00099                 memcpy(&record, data.data, sizeof(record));
00100         sp->st_current = record.seq_value;
00101         sp->st_value = seq->seq_record.seq_value;
00102         sp->st_last_value = seq->seq_last_value;
00103         sp->st_min = seq->seq_record.seq_min;
00104         sp->st_max = seq->seq_record.seq_max;
00105         sp->st_cache_size = seq->seq_cache_size;
00106         sp->st_flags = seq->seq_record.flags;
00107 
00108         *spp = sp;
00109         if (data.data != &record)
00110                 __os_free(dbenv, data.data);
00111 
00112         /* Release replication block. */
00113 err:    if (handle_check && (t_ret = __env_db_rep_exit(dbenv)) != 0 && ret == 0)
00114                 ret = t_ret;
00115         ENV_LEAVE(dbenv, ip);
00116         return (ret);
00117 }
00118 
00119 /*
00120  * __seq_stat_print --
00121  *      Print statistics from the sequence.
00122  *
00123  * PUBLIC: int __seq_stat_print __P((DB_SEQUENCE *, u_int32_t));
00124  */
00125 int
00126 __seq_stat_print(seq, flags)
00127         DB_SEQUENCE *seq;
00128         u_int32_t flags;
00129 {
00130         DB *dbp;
00131         DB_ENV *dbenv;
00132         DB_THREAD_INFO *ip;
00133         int handle_check, ret, t_ret;
00134 
00135         dbp = seq->seq_dbp;
00136         dbenv = dbp->dbenv;
00137 
00138         ENV_ENTER(dbenv, ip);
00139 
00140         /* Check for replication block. */
00141         handle_check = IS_ENV_REPLICATED(dbenv);
00142         if (handle_check && (ret = __db_rep_enter(dbp, 1, 0, 0)) != 0) {
00143                 handle_check = 0;
00144                 goto err;
00145         }
00146 
00147         if ((ret = __seq_print_stats(seq, flags)) != 0)
00148                 goto err;
00149 
00150         if (LF_ISSET(DB_STAT_ALL) &&
00151             (ret = __seq_print_all(seq, flags)) != 0)
00152                 goto err;
00153 
00154         /* Release replication block. */
00155 err:    if (handle_check && (t_ret = __env_db_rep_exit(dbenv)) != 0 && ret == 0)
00156                 ret = t_ret;
00157 
00158         ENV_LEAVE(dbenv, ip);
00159         return (ret);
00160 
00161 }
00162 
00163 static const FN __db_seq_flags_fn[] = {
00164         { DB_SEQ_DEC,           "decrement" },
00165         { DB_SEQ_INC,           "increment" },
00166         { DB_SEQ_RANGE_SET,     "range set (internal)" },
00167         { DB_SEQ_WRAP,          "wraparound at end" },
00168         { 0,                    NULL }
00169 };
00170 
00171 /*
00172  * __db_get_seq_flags_fn --
00173  *      Return the __db_seq_flags_fn array.
00174  *
00175  * PUBLIC: const FN * __db_get_seq_flags_fn __P((void));
00176  */
00177 const FN *
00178 __db_get_seq_flags_fn()
00179 {
00180         return (__db_seq_flags_fn);
00181 }
00182 
00183 /*
00184  * __seq_print_stats --
00185  *      Display sequence stat structure.
00186  */
00187 static int
00188 __seq_print_stats(seq, flags)
00189         DB_SEQUENCE *seq;
00190         u_int32_t flags;
00191 {
00192         DB_ENV *dbenv;
00193         DB_SEQUENCE_STAT *sp;
00194         int ret;
00195 
00196         dbenv = seq->seq_dbp->dbenv;
00197 
00198         if ((ret = __seq_stat(seq, &sp, flags)) != 0)
00199                 return (ret);
00200         __db_dl_pct(dbenv,
00201             "The number of sequence locks that required waiting",
00202             (u_long)sp->st_wait,
00203              DB_PCT(sp->st_wait, sp->st_wait + sp->st_nowait), NULL);
00204         STAT_FMT("The current sequence value",
00205             INT64_FMT, int64_t, sp->st_current);
00206         STAT_FMT("The cached sequence value",
00207             INT64_FMT, int64_t, sp->st_value);
00208         STAT_FMT("The last cached sequence value",
00209             INT64_FMT, int64_t, sp->st_last_value);
00210         STAT_FMT("The minimum sequence value",
00211             INT64_FMT, int64_t, sp->st_value);
00212         STAT_FMT("The maximum sequence value",
00213             INT64_FMT, int64_t, sp->st_value);
00214         STAT_ULONG("The cache size", sp->st_cache_size);
00215         __db_prflags(dbenv, NULL,
00216             sp->st_flags, __db_seq_flags_fn, NULL, "\tSequence flags");
00217         __os_ufree(seq->seq_dbp->dbenv, sp);
00218         return (0);
00219 }
00220 
00221 /*
00222  * __seq_print_all --
00223  *      Display sequence debugging information - none for now.
00224  *      (The name seems a bit strange, no?)
00225  */
00226 static int
00227 __seq_print_all(seq, flags)
00228         DB_SEQUENCE *seq;
00229         u_int32_t flags;
00230 {
00231         COMPQUIET(seq, NULL);
00232         COMPQUIET(flags, 0);
00233         return (0);
00234 }
00235 
00236 #else /* !HAVE_STATISTICS */
00237 
00238 int
00239 __seq_stat(seq, statp, flags)
00240         DB_SEQUENCE *seq;
00241         DB_SEQUENCE_STAT **statp;
00242         u_int32_t flags;
00243 {
00244         COMPQUIET(statp, NULL);
00245         COMPQUIET(flags, 0);
00246 
00247         return (__db_stat_not_built(seq->seq_dbp->dbenv));
00248 }
00249 
00250 int
00251 __seq_stat_print(seq, flags)
00252         DB_SEQUENCE *seq;
00253         u_int32_t flags;
00254 {
00255         COMPQUIET(flags, 0);
00256 
00257         return (__db_stat_not_built(seq->seq_dbp->dbenv));
00258 }
00259 
00260 /*
00261  * __db_get_seq_flags_fn --
00262  *      Return the __db_seq_flags_fn array.
00263  *
00264  * PUBLIC: const FN * __db_get_seq_flags_fn __P((void));
00265  */
00266 const FN *
00267 __db_get_seq_flags_fn()
00268 {
00269         static const FN __db_seq_flags_fn[] = {
00270                 { 0,    NULL }
00271         };
00272 
00273         /*
00274          * !!!
00275          * The Tcl API uses this interface, stub it off.
00276          */
00277         return (__db_seq_flags_fn);
00278 }
00279 #endif /* !HAVE_STATISTICS */
00280 #endif /* HAVE_SEQUENCE */

Generated on Sun Dec 25 12:14:49 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2