]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/rww.c
d6da0aa1117aefe4a845cb26ab5c101418217c32
[openldap] / servers / slapd / back-monitor / rww.c
1 /* readw.c - deal with read waiters subsystem */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
8  * 
9  * This work has beed deveolped for the OpenLDAP Foundation 
10  * in the hope that it may be useful to the Open Source community, 
11  * but WITHOUT ANY WARRANTY.
12  * 
13  * Permission is granted to anyone to use this software for any purpose
14  * on any computer system, and to alter it and redistribute it, subject
15  * to the following restrictions:
16  * 
17  * 1. The author and SysNet s.n.c. are not responsible for the consequences
18  *    of use of this software, no matter how awful, even if they arise from
19  *    flaws in it.
20  * 
21  * 2. The origin of this software must not be misrepresented, either by
22  *    explicit claim or by omission.  Since few users ever read sources,
23  *    credits should appear in the documentation.
24  * 
25  * 3. Altered versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.  Since few users
27  *    ever read sources, credits should appear in the documentation.
28  *    SysNet s.n.c. cannot be responsible for the consequences of the
29  *    alterations.
30  * 
31  * 4. This notice may not be removed or altered.
32  */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include "slap.h"
39 #include "lutil.h"
40 #include "back-monitor.h"
41
42 int
43 monitor_subsys_rww_init(
44         BackendDB               *be
45 )
46 {
47         struct monitorinfo      *mi;
48         
49         Entry                   *e, *e_tmp, *e_conn;
50         struct monitorentrypriv *mp;
51         char                    buf[ BACKMONITOR_BUFSIZE ];
52         struct berval           bv;
53
54         assert( be != NULL );
55
56         mi = ( struct monitorinfo * )be->be_private;
57
58         if ( monitor_cache_get( mi,
59                         &monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn, &e_conn ) ) {
60 #ifdef NEW_LOGGING
61                 LDAP_LOG( OPERATION, CRIT,
62                         "monitor_subsys_rww_init: "
63                         "unable to get entry '%s'\n",
64                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
65 #else
66                 Debug( LDAP_DEBUG_ANY,
67                         "monitor_subsys_rww_init: "
68                         "unable to get entry '%s'\n%s%s",
69                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 
70                         "", "" );
71 #endif
72                 return( -1 );
73         }
74
75         e_tmp = NULL;
76
77         /*
78          * Total conns
79          */
80         snprintf( buf, sizeof( buf ),
81                 "dn: cn=Read,%s\n"
82                 "objectClass: %s\n"
83                 "structuralObjectClass: %s\n"
84                 "cn: Read\n",
85                 monitor_subsys[SLAPD_MONITOR_RWW].mss_dn.bv_val,
86                 mi->oc_monitorCounterObject->soc_cname.bv_val,
87                 mi->oc_monitorCounterObject->soc_cname.bv_val );
88         
89         e = str2entry( buf );
90         if ( e == NULL ) {
91 #ifdef NEW_LOGGING
92                 LDAP_LOG( OPERATION, CRIT,
93                         "monitor_subsys_rww_init: "
94                         "unable to create entry 'cn=Read,%s'\n",
95                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
96 #else
97                 Debug( LDAP_DEBUG_ANY,
98                         "monitor_subsys_rww_init: "
99                         "unable to create entry 'cn=Read,%s'\n",
100                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
101 #endif
102                 return( -1 );
103         }
104         
105         bv.bv_val = "0";
106         bv.bv_len = 1;
107         attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
108         
109         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
110         e->e_private = ( void * )mp;
111         mp->mp_next = e_tmp;
112         mp->mp_children = NULL;
113         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_RWW];
114         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_RWW].mss_flags \
115                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
116
117         if ( monitor_cache_add( mi, e ) ) {
118 #ifdef NEW_LOGGING
119                 LDAP_LOG( OPERATION, CRIT,
120                         "monitor_subsys_rww_init: "
121                         "unable to add entry 'cn=Read,%s'\n",
122                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
123 #else
124                 Debug( LDAP_DEBUG_ANY,
125                         "monitor_subsys_rww_init: "
126                         "unable to add entry 'cn=Read,%s'\n",
127                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
128 #endif
129                 return( -1 );
130         }
131         
132         e_tmp = e;
133
134         /*
135          * Current conns
136          */
137         snprintf( buf, sizeof( buf ),
138                 "dn: cn=Write,%s\n"
139                 "objectClass: %s\n"
140                 "structuralObjectClass: %s\n"
141                 "cn: Write\n",
142                 monitor_subsys[SLAPD_MONITOR_RWW].mss_dn.bv_val,
143                 mi->oc_monitorCounterObject->soc_cname.bv_val,
144                 mi->oc_monitorCounterObject->soc_cname.bv_val );
145         
146         e = str2entry( buf );
147         if ( e == NULL ) {
148 #ifdef NEW_LOGGING
149                 LDAP_LOG( OPERATION, CRIT,
150                         "monitor_subsys_rww_init: "
151                         "unable to create entry 'cn=Write,%s'\n",
152                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
153 #else
154                 Debug( LDAP_DEBUG_ANY,
155                         "monitor_subsys_rww_init: "
156                         "unable to create entry 'cn=Write,%s'\n",
157                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
158 #endif
159                 return( -1 );
160         }
161         
162         bv.bv_val = "0";
163         bv.bv_len = 1;
164         attr_merge_one( e, mi->ad_monitorCounter, &bv, NULL );
165         
166         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
167         e->e_private = ( void * )mp;
168         mp->mp_next = e_tmp;
169         mp->mp_children = NULL;
170         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_RWW];
171         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_RWW].mss_flags \
172                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
173
174         if ( monitor_cache_add( mi, e ) ) {
175 #ifdef NEW_LOGGING
176                 LDAP_LOG( OPERATION, CRIT,
177                         "monitor_subsys_rww_init: "
178                         "unable to add entry 'cn=Write,%s'\n",
179                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
180 #else
181                 Debug( LDAP_DEBUG_ANY,
182                         "monitor_subsys_rww_init: "
183                         "unable to add entry 'cn=Write,%s'\n",
184                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
185 #endif
186                 return( -1 );
187         }
188         
189         e_tmp = e;
190
191         mp = ( struct monitorentrypriv * )e_conn->e_private;
192         mp->mp_children = e_tmp;
193
194         monitor_cache_release( mi, e_conn );
195
196         return( 0 );
197 }
198
199 int
200 monitor_subsys_rww_update(
201         Operation               *op,
202         Entry                   *e
203 )
204 {
205         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
206         Connection              *c;
207         int                     connindex;
208         long                    nconns, nwritewaiters, nreadwaiters;
209
210 #define RWW_NONE        0
211 #define RWW_READ        1
212 #define RWW_WRITE       2
213         int                     type = RWW_NONE;
214         
215         Attribute               *a;
216         struct berval           *b = NULL;
217         char                    buf[] = "+9223372036854775807L";
218         
219         char                    *str = NULL;
220         long                    num = 0;
221
222         assert( mi != NULL );
223         assert( e != NULL );
224         
225         if ( strncasecmp( e->e_ndn, "cn=read", 
226                                 sizeof("cn=read")-1 ) == 0 ) {
227                 type = RWW_READ;
228
229         } else if ( strncasecmp( e->e_ndn, "cn=write", 
230                                 sizeof("cn=write")-1 ) == 0 ) {
231                 type = RWW_WRITE;
232
233         } else {
234                 return( 0 );
235         }
236
237         nconns = nwritewaiters = nreadwaiters = 0;
238         for ( c = connection_first( &connindex );
239                         c != NULL;
240                         c = connection_next( c, &connindex ), nconns++ ) {
241                 if ( c->c_writewaiter ) {
242                         nwritewaiters++;
243                 }
244                 if ( c->c_currentber != NULL ) {
245                         nreadwaiters++;
246                 }
247         }
248         connection_done(c);
249
250         switch ( type ) {
251         case RWW_READ:
252                 num = nreadwaiters;
253                 break;
254
255         case RWW_WRITE:
256                 num = nwritewaiters;
257                 break;
258
259         default:
260                 assert( 0 );
261         }
262
263         snprintf( buf, sizeof( buf ), "%ld", num );
264
265         a = attr_find( e->e_attrs, mi->ad_monitorCounter );
266         assert( a );
267         free( a->a_vals[0].bv_val );
268         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
269
270         return( 0 );
271 }
272