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