]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/rww.c
import fix to back-monitor attribute normalization (ITS#3659)
[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 2001-2005 The OpenLDAP Foundation.
6  * Portions Copyright 2001-2003 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25 #include <ac/string.h>
26
27 #include "slap.h"
28 #include "lutil.h"
29 #include "back-monitor.h"
30
31 int
32 monitor_subsys_rww_init(
33         BackendDB               *be
34 )
35 {
36         struct monitorinfo      *mi;
37         
38         Entry                   *e, *e_tmp, *e_conn;
39         struct monitorentrypriv *mp;
40         char                    buf[ BACKMONITOR_BUFSIZE ];
41         struct berval           bv;
42
43         assert( be != NULL );
44
45         mi = ( struct monitorinfo * )be->be_private;
46
47         if ( monitor_cache_get( mi,
48                         &monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn, &e_conn ) ) {
49 #ifdef NEW_LOGGING
50                 LDAP_LOG( OPERATION, CRIT,
51                         "monitor_subsys_rww_init: "
52                         "unable to get entry '%s'\n",
53                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
54 #else
55                 Debug( LDAP_DEBUG_ANY,
56                         "monitor_subsys_rww_init: "
57                         "unable to get entry '%s'\n%s%s",
58                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 
59                         "", "" );
60 #endif
61                 return( -1 );
62         }
63
64         e_tmp = NULL;
65
66         /*
67          * Total conns
68          */
69         snprintf( buf, sizeof( buf ),
70                 "dn: cn=Read,%s\n"
71                 "objectClass: %s\n"
72                 "structuralObjectClass: %s\n"
73                 "cn: Read\n"
74                 "createTimestamp: %s\n"
75                 "modifyTimestamp: %s\n",
76                 monitor_subsys[SLAPD_MONITOR_RWW].mss_dn.bv_val,
77                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
78                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
79                 mi->mi_startTime.bv_val,
80                 mi->mi_startTime.bv_val );
81         
82         e = str2entry( buf );
83         if ( e == NULL ) {
84 #ifdef NEW_LOGGING
85                 LDAP_LOG( OPERATION, CRIT,
86                         "monitor_subsys_rww_init: "
87                         "unable to create entry 'cn=Read,%s'\n",
88                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
89 #else
90                 Debug( LDAP_DEBUG_ANY,
91                         "monitor_subsys_rww_init: "
92                         "unable to create entry 'cn=Read,%s'\n",
93                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
94 #endif
95                 return( -1 );
96         }
97         
98         bv.bv_val = "0";
99         bv.bv_len = 1;
100         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
101         
102         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
103         e->e_private = ( void * )mp;
104         mp->mp_next = e_tmp;
105         mp->mp_children = NULL;
106         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_RWW];
107         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_RWW].mss_flags \
108                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
109
110         if ( monitor_cache_add( mi, e ) ) {
111 #ifdef NEW_LOGGING
112                 LDAP_LOG( OPERATION, CRIT,
113                         "monitor_subsys_rww_init: "
114                         "unable to add 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 add 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         e_tmp = e;
126
127         /*
128          * Current conns
129          */
130         snprintf( buf, sizeof( buf ),
131                 "dn: cn=Write,%s\n"
132                 "objectClass: %s\n"
133                 "structuralObjectClass: %s\n"
134                 "cn: Write\n"
135                 "createTimestamp: %s\n"
136                 "modifyTimestamp: %s\n",
137                 monitor_subsys[SLAPD_MONITOR_RWW].mss_dn.bv_val,
138                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
139                 mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
140                 mi->mi_startTime.bv_val,
141                 mi->mi_startTime.bv_val );
142         
143         e = str2entry( buf );
144         if ( e == NULL ) {
145 #ifdef NEW_LOGGING
146                 LDAP_LOG( OPERATION, CRIT,
147                         "monitor_subsys_rww_init: "
148                         "unable to create entry 'cn=Write,%s'\n",
149                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
150 #else
151                 Debug( LDAP_DEBUG_ANY,
152                         "monitor_subsys_rww_init: "
153                         "unable to create entry 'cn=Write,%s'\n",
154                         monitor_subsys[SLAPD_MONITOR_RWW].mss_ndn.bv_val, 0, 0 );
155 #endif
156                 return( -1 );
157         }
158         
159         bv.bv_val = "0";
160         bv.bv_len = 1;
161         attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
162         
163         mp = ( struct monitorentrypriv * )ch_calloc( sizeof( struct monitorentrypriv ), 1 );
164         e->e_private = ( void * )mp;
165         mp->mp_next = e_tmp;
166         mp->mp_children = NULL;
167         mp->mp_info = &monitor_subsys[SLAPD_MONITOR_RWW];
168         mp->mp_flags = monitor_subsys[SLAPD_MONITOR_RWW].mss_flags \
169                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
170
171         if ( monitor_cache_add( mi, e ) ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, CRIT,
174                         "monitor_subsys_rww_init: "
175                         "unable to add 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 add 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         e_tmp = e;
187
188         mp = ( struct monitorentrypriv * )e_conn->e_private;
189         mp->mp_children = e_tmp;
190
191         monitor_cache_release( mi, e_conn );
192
193         return( 0 );
194 }
195
196 int
197 monitor_subsys_rww_update(
198         Operation               *op,
199         Entry                   *e
200 )
201 {
202         struct monitorinfo *mi = (struct monitorinfo *)op->o_bd->be_private;
203         Connection              *c;
204         int                     connindex;
205         long                    nconns, nwritewaiters, nreadwaiters;
206
207 #define RWW_NONE        0
208 #define RWW_READ        1
209 #define RWW_WRITE       2
210         int                     type = RWW_NONE;
211         
212         Attribute               *a;
213         char                    buf[] = "+9223372036854775807L";
214         long                    num = 0;
215
216         assert( mi != NULL );
217         assert( e != NULL );
218         
219         if ( strncasecmp( e->e_ndn, "cn=read", 
220                                 sizeof("cn=read")-1 ) == 0 ) {
221                 type = RWW_READ;
222
223         } else if ( strncasecmp( e->e_ndn, "cn=write", 
224                                 sizeof("cn=write")-1 ) == 0 ) {
225                 type = RWW_WRITE;
226
227         } else {
228                 return( 0 );
229         }
230
231         nconns = nwritewaiters = nreadwaiters = 0;
232         for ( c = connection_first( &connindex );
233                         c != NULL;
234                         c = connection_next( c, &connindex ), nconns++ ) {
235                 if ( c->c_writewaiter ) {
236                         nwritewaiters++;
237                 }
238                 if ( c->c_currentber != NULL ) {
239                         nreadwaiters++;
240                 }
241         }
242         connection_done(c);
243
244         switch ( type ) {
245         case RWW_READ:
246                 num = nreadwaiters;
247                 break;
248
249         case RWW_WRITE:
250                 num = nwritewaiters;
251                 break;
252
253         default:
254                 assert( 0 );
255         }
256
257         snprintf( buf, sizeof( buf ), "%ld", num );
258
259         a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
260         assert( a );
261         free( a->a_vals[0].bv_val );
262         ber_str2bv( buf, 0, 1, &a->a_vals[ 0 ] );
263
264         return( 0 );
265 }
266