1 /*
2  *
3  *    nrstats.h
4  *
5  *    $Source: /Users/ekr/tmp/nrappkit-dump/nrappkit/src/stats/nrstats.h,v $
6  *    $Revision: 1.4 $
7  *    $Date: 2007/06/26 22:37:55 $
8  *
9  *    API for keeping and sharing statistics
10  *
11  *
12  *    Copyright (C) 2003-2005, Network Resonance, Inc.
13  *    Copyright (C) 2006, Network Resonance, Inc.
14  *    All Rights Reserved
15  *
16  *    Redistribution and use in source and binary forms, with or without
17  *    modification, are permitted provided that the following conditions
18  *    are met:
19  *
20  *    1. Redistributions of source code must retain the above copyright
21  *       notice, this list of conditions and the following disclaimer.
22  *    2. Redistributions in binary form must reproduce the above copyright
23  *       notice, this list of conditions and the following disclaimer in the
24  *       documentation and/or other materials provided with the distribution.
25  *    3. Neither the name of Network Resonance, Inc. nor the name of any
26  *       contributors to this software may be used to endorse or promote
27  *       products derived from this software without specific prior written
28  *       permission.
29  *
30  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
31  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  *    ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  *    POSSIBILITY OF SUCH DAMAGE.
41  *
42  *
43  */
44 
45 #ifndef __NRSTATS_H__
46 #define __NRSTATS_H__
47 
48 #include <sys/types.h>
49 #ifdef WIN32
50 #include <time.h>
51 #else
52 #include <sys/time.h>
53 #endif
54 #include <r_types.h>
55 
56 #ifndef CAPTURE_USER
57 #define CAPTURE_USER "pcecap"
58 #endif
59 
60 #define NR_MAX_STATS_TYPES         256   /* max number of stats objects */
61 #define NR_MAX_STATS_TYPE_NAME     26
62 
63 typedef struct NR_stats_type_ {
64       char name[NR_MAX_STATS_TYPE_NAME];
65       int (*reset)(void *stats);
66       int (*print)(void *stats, char *stat_namespace, void (*output)(void *handle, const char *fmt, ...), void *handle);
67       int  (*get_lib_name)(char **libname);
68       unsigned int size;
69 } NR_stats_type;
70 
71 typedef struct NR_stats_app_ {
72         time_t               last_counter_reset;
73         time_t               last_restart;
74         UINT8                total_restarts;
75         char                 version[64];
76 } NR_stats_app;
77 
78 extern NR_stats_type *NR_stats_type_app;
79 
80 /* everything measured in bytes */
81 typedef struct NR_stats_memory_ {
82         UINT8                current_size;
83         UINT8                max_size;
84         UINT8                in_use;
85         UINT8                in_use_max;
86 } NR_stats_memory;
87 
88 extern NR_stats_type *NR_stats_type_memory;
89 
90 /*  all functions below return 0 on success, else they return an
91  *  error code */
92 
93 /* if errprintf is null, warnings and errors will be sent to /dev/null */
94 extern int NR_stats_startup(char *app_name, char *user_name, void (*errprintf)(void *handle, const char *fmt, ...), void *errhandle);
95 extern int NR_stats_shutdown(void);
96 #define NR_STATS_CREATE  (1<<0)
97 extern int NR_stats_get(char *module_name, NR_stats_type *type, int flag, void **stats);
98 extern int NR_stats_clear(void *stats);   /* zeroizes */
99 extern int NR_stats_reset(void *stats);   /* zeros "speedometers" */
100 extern int NR_stats_register(NR_stats_type *type);
101 extern int NR_stats_acquire_mutex(void *stats);
102 extern int NR_stats_release_mutex(void *stats);
103 // TODO: should _get_names take an app_name argument (0==all)????
104 extern int NR_stats_get_names(unsigned int *nnames, char ***names);
105 extern int NR_stats_get_by_name(char *name, NR_stats_type **type, void **stats);
106 extern int NR_stats_get_lib_name(void *stats, char **lib_name);
107 extern int NR_stats_rmids(void);
108 
109 extern char *NR_prefix_to_stats_module(char *prefix);
110 
111 #define NR_INCREMENT_STAT(stat) do { \
112        stat++; if(stat>stat##_max) stat##_max=stat; \
113      } while (0)
114 #define NR_UPDATE_STAT(stat,newval) do { \
115        stat=newval; if(stat>stat##_max) stat##_max=stat; \
116      } while (0)
117 
118 #endif
119