]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/thread.c
cleanup subsystems monitoring OID handling
[openldap] / servers / slapd / back-monitor / thread.c
1 /* thread.c - deal with thread 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 "back-monitor.h"
29
30 #include <ldap_rq.h>
31
32 static int 
33 monitor_subsys_thread_update( 
34         Operation               *op,
35         SlapReply               *rs,
36         Entry                   *e );
37
38 /*
39  * initializes log subentry
40  */
41 int
42 monitor_subsys_thread_init(
43         BackendDB               *be,
44         monitor_subsys_t        *ms
45 )
46 {
47         monitor_info_t  *mi;
48         monitor_entry_t *mp;
49         Entry           *e, **ep, *e_thread;
50         static char     buf[ BACKMONITOR_BUFSIZE ];
51         struct berval bv;
52
53         ms->mss_update = monitor_subsys_thread_update;
54
55         mi = ( monitor_info_t * )be->be_private;
56
57         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_thread ) ) {
58                 Debug( LDAP_DEBUG_ANY,
59                         "monitor_subsys_thread_init: unable to get entry \"%s\"\n",
60                         ms->mss_ndn.bv_val, 
61                         0, 0 );
62                 return( -1 );
63         }
64
65         mp = ( monitor_entry_t * )e_thread->e_private;
66         mp->mp_children = NULL;
67         ep = &mp->mp_children;
68
69         /*
70          * Max
71          */
72         BER_BVSTR( &bv, "cn=Max" );
73         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
74                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
75         if ( e == NULL ) {
76                 Debug( LDAP_DEBUG_ANY,
77                         "monitor_subsys_thread_init: "
78                         "unable to create entry \"cn=Max,%s\"\n",
79                         ms->mss_ndn.bv_val, 0, 0 );
80                 return( -1 );
81         }
82         bv.bv_len = snprintf( buf, sizeof( buf ), "%d", connection_pool_max );
83         bv.bv_val = buf;
84         attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
85         
86         mp = monitor_entrypriv_create();
87         if ( mp == NULL ) {
88                 return -1;
89         }
90         e->e_private = ( void * )mp;
91         mp->mp_info = ms;
92         mp->mp_flags = ms->mss_flags \
93                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
94
95         if ( monitor_cache_add( mi, e ) ) {
96                 Debug( LDAP_DEBUG_ANY,
97                         "monitor_subsys_thread_init: "
98                         "unable to add entry \"cn=Max,%s\"\n",
99                         ms->mss_ndn.bv_val, 0, 0 );
100                 return( -1 );
101         }
102         
103         *ep = e;
104         ep = &mp->mp_next;
105
106         /*
107          * Backload
108          */
109         BER_BVSTR( &bv, "cn=Backload" );
110         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
111                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
112         if ( e == NULL ) {
113                 Debug( LDAP_DEBUG_ANY,
114                         "monitor_subsys_thread_init: "
115                         "unable to create entry \"cn=Backload,%s\"\n",
116                         ms->mss_ndn.bv_val, 0, 0 );
117                 return( -1 );
118         }
119         BER_BVSTR( &bv, "0" );
120         attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
121
122         mp = monitor_entrypriv_create();
123         if ( mp == NULL ) {
124                 return -1;
125         }
126         e->e_private = ( void * )mp;
127         mp->mp_info = ms;
128         mp->mp_flags = ms->mss_flags \
129                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
130
131         if ( monitor_cache_add( mi, e ) ) {
132                 Debug( LDAP_DEBUG_ANY,
133                         "monitor_subsys_thread_init: "
134                         "unable to add entry \"cn=Backload,%s\"\n",
135                         ms->mss_ndn.bv_val, 0, 0 );
136                 return( -1 );
137         }
138         
139         *ep = e;
140         ep = &mp->mp_next;
141
142         /*
143          * Runqueue runners
144          */
145         BER_BVSTR( &bv, "cn=Runqueue" );
146         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
147                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
148         if ( e == NULL ) {
149                 Debug( LDAP_DEBUG_ANY,
150                         "monitor_subsys_thread_init: "
151                         "unable to create entry \"cn=Runqueue,%s\"\n",
152                         ms->mss_ndn.bv_val, 0, 0 );
153                 return( -1 );
154         }
155
156         mp = monitor_entrypriv_create();
157         if ( mp == NULL ) {
158                 return -1;
159         }
160         e->e_private = ( void * )mp;
161         mp->mp_info = ms;
162         mp->mp_flags = ms->mss_flags \
163                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
164
165         if ( monitor_cache_add( mi, e ) ) {
166                 Debug( LDAP_DEBUG_ANY,
167                         "monitor_subsys_thread_init: "
168                         "unable to add entry \"cn=Runqueue,%s\"\n",
169                         ms->mss_ndn.bv_val, 0, 0 );
170                 return( -1 );
171         }
172         
173         *ep = e;
174         ep = &mp->mp_next;
175
176         /*
177          * Tasklist
178          */
179         BER_BVSTR( &bv, "cn=Tasklist" );
180         e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
181                 mi->mi_oc_monitoredObject, mi, NULL, NULL );
182         if ( e == NULL ) {
183                 Debug( LDAP_DEBUG_ANY,
184                         "monitor_subsys_thread_init: "
185                         "unable to create entry \"%s,%s\"\n",
186                         bv.bv_val, ms->mss_ndn.bv_val, 0 );
187                 return( -1 );
188         }
189
190         mp = monitor_entrypriv_create();
191         if ( mp == NULL ) {
192                 return -1;
193         }
194         e->e_private = ( void * )mp;
195         mp->mp_info = ms;
196         mp->mp_flags = ms->mss_flags \
197                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
198
199         if ( monitor_cache_add( mi, e ) ) {
200                 Debug( LDAP_DEBUG_ANY,
201                         "monitor_subsys_thread_init: "
202                         "unable to add entry \"%s\"\n",
203                         e->e_name.bv_val, 0, 0 );
204                 return( -1 );
205         }
206         
207         *ep = e;
208         ep = &mp->mp_next;
209
210         monitor_cache_release( mi, e_thread );
211
212         return( 0 );
213 }
214
215 static int 
216 monitor_subsys_thread_update( 
217         Operation               *op,
218         SlapReply               *rs,
219         Entry                   *e )
220 {
221         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
222         Attribute               *a;
223         BerVarray               vals = NULL;
224         char                    buf[ BACKMONITOR_BUFSIZE ];
225         enum {
226                 MT_UNKNOWN,
227                 MT_MAX,
228                 MT_BACKLOAD,
229                 MT_RUNQUEUE,
230                 MT_TASKLIST,
231
232                 MT_LAST
233         };
234         static slap_verbmasks   mt[] = {
235                 { BER_BVC( "cn=max" ),          MT_MAX          },
236                 { BER_BVC( "cn=backload" ),     MT_BACKLOAD     },
237                 { BER_BVC( "cn=runqueue" ),     MT_RUNQUEUE     },
238                 { BER_BVC( "cn=tasklist" ),     MT_TASKLIST     },
239                 { BER_BVNULL,                   MT_UNKNOWN      }
240         };
241         struct berval           rdn, bv;
242         int                     which, i;
243         struct re_s             *re;
244
245         assert( mi != NULL );
246
247         dnRdn( &e->e_nname, &rdn );
248
249         which = bverb_to_mask( &rdn, mt );
250         if ( BER_BVISNULL( &mt[ which ].word )
251                 || mt[ which ].mask == MT_UNKNOWN )
252         {
253                 return SLAP_CB_CONTINUE;
254         }
255
256         a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
257
258         switch ( mt[ which ].mask ) {
259         case MT_MAX:
260                 if ( a == NULL ) {
261                         return rs->sr_err = LDAP_OTHER;
262                 }
263                 bv.bv_val = buf;
264                 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", connection_pool_max );
265                 if ( bv.bv_len >= sizeof( buf ) ) {
266                         /* FIXME? */
267                         break;
268                 }
269                 ber_bvreplace( &a->a_vals[ 0 ], &bv );
270                 break;
271
272         case MT_BACKLOAD:
273                 if ( a == NULL ) {
274                         return rs->sr_err = LDAP_OTHER;
275                 }
276                 bv.bv_val = buf;
277                 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", 
278                         ldap_pvt_thread_pool_backload( &connection_pool ) );
279                 if ( bv.bv_len >= sizeof( buf ) ) {
280                         /* FIXME? */
281                         break;
282                 }
283                 ber_bvreplace( &a->a_vals[ 0 ], &bv );
284                 break;
285
286         case MT_RUNQUEUE:
287                 if ( a != NULL ) {
288                         if ( a->a_nvals != a->a_vals ) {
289                                 ber_bvarray_free( a->a_nvals );
290                         }
291                         ber_bvarray_free( a->a_vals );
292                         a->a_vals = NULL;
293                         a->a_nvals = NULL;
294                 }
295
296                 i = 0;
297                 bv.bv_val = buf;
298                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
299                 LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) {
300                         bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)",
301                                 i, re->tname, re->tspec );
302                         if ( bv.bv_len < sizeof( buf ) ) {
303                                 value_add_one( &vals, &bv );
304                         }
305                         i++;
306                 }
307                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
308
309                 if ( vals ) {
310                         attr_merge_normalize( e, mi->mi_ad_monitoredInfo, vals, NULL );
311                         ber_bvarray_free( vals );
312                 }
313                 break;
314
315         case MT_TASKLIST:
316                 if ( a != NULL ) {
317                         if ( a->a_nvals != a->a_vals ) {
318                                 ber_bvarray_free( a->a_nvals );
319                         }
320                         ber_bvarray_free( a->a_vals );
321                         a->a_vals = NULL;
322                         a->a_nvals = NULL;
323                 }
324
325                 i = 0;
326                 bv.bv_val = buf;
327                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
328                 LDAP_STAILQ_FOREACH( re, &slapd_rq.task_list, tnext ) {
329                         bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)",
330                                 i, re->tname, re->tspec );
331                         if ( bv.bv_len < sizeof( buf ) ) {
332                                 value_add_one( &vals, &bv );
333                         }
334                         i++;
335                 }
336                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
337
338                 if ( vals ) {
339                         attr_merge_normalize( e, mi->mi_ad_monitoredInfo, vals, NULL );
340                         ber_bvarray_free( vals );
341                 }
342                 break;
343         }
344
345         /* FIXME: touch modifyTimestamp? */
346
347         return SLAP_CB_CONTINUE;
348 }
349