]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/rww.c
Happy new year!
[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-2006 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 static int
32 monitor_subsys_rww_destroy(
33         BackendDB               *be,
34         monitor_subsys_t        *ms );
35
36 static int
37 monitor_subsys_rww_update(
38         Operation               *op,
39         SlapReply               *rs,
40         Entry                   *e );
41
42 enum {
43         MONITOR_RWW_READ = 0,
44         MONITOR_RWW_WRITE,
45
46         MONITOR_RWW_LAST
47 };
48
49 static struct monitor_rww_t {
50         struct berval   rdn;
51         struct berval   nrdn;
52 } monitor_rww[] = {
53         { BER_BVC("cn=Read"),           BER_BVNULL },
54         { BER_BVC("cn=Write"),          BER_BVNULL },
55         { BER_BVNULL,                   BER_BVNULL }
56 };
57
58 int
59 monitor_subsys_rww_init(
60         BackendDB               *be,
61         monitor_subsys_t        *ms )
62 {
63         monitor_info_t  *mi;
64         
65         Entry           **ep, *e_conn;
66         monitor_entry_t *mp;
67         int                     i;
68
69         assert( be != NULL );
70
71         ms->mss_destroy = monitor_subsys_rww_destroy;
72         ms->mss_update = monitor_subsys_rww_update;
73
74         mi = ( monitor_info_t * )be->be_private;
75
76         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_conn ) ) {
77                 Debug( LDAP_DEBUG_ANY,
78                         "monitor_subsys_rww_init: "
79                         "unable to get entry \"%s\"\n",
80                         ms->mss_ndn.bv_val, 0, 0 );
81                 return( -1 );
82         }
83
84         mp = ( monitor_entry_t * )e_conn->e_private;
85         mp->mp_children = NULL;
86         ep = &mp->mp_children;
87
88         for ( i = 0; i < MONITOR_RWW_LAST; i++ ) {
89                 char                    buf[ BACKMONITOR_BUFSIZE ];
90                 struct berval           nrdn, bv;
91                 Entry                   *e;
92                 
93                 snprintf( buf, sizeof( buf ),
94                         "dn: %s,%s\n"
95                         "objectClass: %s\n"
96                         "structuralObjectClass: %s\n"
97                         "cn: %s\n"
98                         "creatorsName: %s\n"
99                         "modifiersName: %s\n"
100                         "createTimestamp: %s\n"
101                         "modifyTimestamp: %s\n",
102                         monitor_rww[ i ].rdn.bv_val,
103                         ms->mss_dn.bv_val,
104                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
105                         mi->mi_oc_monitorCounterObject->soc_cname.bv_val,
106                         &monitor_rww[ i ].rdn.bv_val[ STRLENOF( "cn=" ) ],
107                         mi->mi_creatorsName.bv_val,
108                         mi->mi_creatorsName.bv_val,
109                         mi->mi_startTime.bv_val,
110                         mi->mi_startTime.bv_val );
111         
112                 e = str2entry( buf );
113                 if ( e == NULL ) {
114                         Debug( LDAP_DEBUG_ANY,
115                                 "monitor_subsys_rww_init: "
116                                 "unable to create entry \"cn=Read,%s\"\n",
117                                 ms->mss_ndn.bv_val, 0, 0 );
118                         return( -1 );
119                 }
120
121                 /* steal normalized RDN */
122                 dnRdn( &e->e_nname, &nrdn );
123                 ber_dupbv( &monitor_rww[ i ].nrdn, &nrdn );
124         
125                 BER_BVSTR( &bv, "0" );
126                 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, &bv );
127         
128                 mp = monitor_entrypriv_create();
129                 if ( mp == NULL ) {
130                         return -1;
131                 }
132                 e->e_private = ( void * )mp;
133                 mp->mp_info = ms;
134                 mp->mp_flags = ms->mss_flags \
135                         | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
136
137                 if ( monitor_cache_add( mi, e ) ) {
138                         Debug( LDAP_DEBUG_ANY,
139                                 "monitor_subsys_rww_init: "
140                                 "unable to add entry \"%s,%s\"\n",
141                                 monitor_rww[ i ].rdn.bv_val,
142                                 ms->mss_ndn.bv_val, 0 );
143                         return( -1 );
144                 }
145         
146                 *ep = e;
147                 ep = &mp->mp_next;
148         }
149
150         monitor_cache_release( mi, e_conn );
151
152         return( 0 );
153 }
154
155 static int
156 monitor_subsys_rww_destroy(
157         BackendDB               *be,
158         monitor_subsys_t        *ms )
159 {
160         int             i;
161
162         for ( i = 0; i < MONITOR_RWW_LAST; i++ ) {
163                 ber_memfree_x( monitor_rww[ i ].nrdn.bv_val, NULL );
164         }
165
166         return 0;
167 }
168
169 static int
170 monitor_subsys_rww_update(
171         Operation               *op,
172         SlapReply               *rs,
173         Entry                   *e )
174 {
175         monitor_info_t *mi = (monitor_info_t *)op->o_bd->be_private;
176         Connection      *c;
177         int             connindex;
178         long            nconns, nwritewaiters, nreadwaiters;
179
180         int             i;
181         struct berval   nrdn;
182
183         Attribute       *a;
184         char            buf[] = "+9223372036854775807L";
185         long            num = 0;
186         ber_len_t       len;
187
188         assert( mi != NULL );
189         assert( e != NULL );
190
191         dnRdn( &e->e_nname, &nrdn );
192
193         for ( i = 0; !BER_BVISNULL( &monitor_rww[ i ].nrdn ); i++ ) {
194                 if ( dn_match( &nrdn, &monitor_rww[ i ].nrdn ) ) {
195                         break;
196                 }
197         }
198
199         if ( i == MONITOR_RWW_LAST ) {
200                 return SLAP_CB_CONTINUE;
201         }
202
203         nconns = nwritewaiters = nreadwaiters = 0;
204         for ( c = connection_first( &connindex );
205                         c != NULL;
206                         c = connection_next( c, &connindex ), nconns++ )
207         {
208                 if ( c->c_writewaiter ) {
209                         nwritewaiters++;
210                 }
211
212                 /* FIXME: ?!? */
213                 if ( c->c_currentber != NULL ) {
214                         nreadwaiters++;
215                 }
216         }
217         connection_done(c);
218
219         switch ( i ) {
220         case MONITOR_RWW_READ:
221                 num = nreadwaiters;
222                 break;
223
224         case MONITOR_RWW_WRITE:
225                 num = nwritewaiters;
226                 break;
227
228         default:
229                 assert( 0 );
230         }
231
232         snprintf( buf, sizeof( buf ), "%ld", num );
233
234         a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
235         assert( a != NULL );
236         len = strlen( buf );
237         if ( len > a->a_vals[ 0 ].bv_len ) {
238                 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
239                 if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
240                         BER_BVZERO( &a->a_vals[ 0 ] );
241                         return SLAP_CB_CONTINUE;
242                 }
243         }
244         AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
245         a->a_vals[ 0 ].bv_len = len;
246
247         /* FIXME: touch modifyTimestamp? */
248
249         return SLAP_CB_CONTINUE;
250 }
251