1 /* thread.c - deal with thread subsystem */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2001-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2001-2003 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Pierangelo Masarati for inclusion
19 * in OpenLDAP Software.
25 #include <ac/string.h>
28 #include "back-monitor.h"
33 monitor_subsys_thread_update(
39 * initializes log subentry
42 monitor_subsys_thread_init(
49 Entry *e, **ep, *e_thread;
50 static char buf[ BACKMONITOR_BUFSIZE ];
53 ms->mss_update = monitor_subsys_thread_update;
55 mi = ( monitor_info_t * )be->be_private;
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",
65 mp = ( monitor_entry_t * )e_thread->e_private;
66 mp->mp_children = NULL;
67 ep = &mp->mp_children;
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 );
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 );
82 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", connection_pool_max );
84 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
86 mp = monitor_entrypriv_create();
90 e->e_private = ( void * )mp;
92 mp->mp_flags = ms->mss_flags \
93 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
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 );
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 );
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 );
119 BER_BVSTR( &bv, "0" );
120 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
122 mp = monitor_entrypriv_create();
126 e->e_private = ( void * )mp;
128 mp->mp_flags = ms->mss_flags \
129 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
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 );
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 );
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 );
155 BER_BVSTR( &bv, "0" );
156 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
158 mp = monitor_entrypriv_create();
162 e->e_private = ( void * )mp;
164 mp->mp_flags = ms->mss_flags \
165 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
167 if ( monitor_cache_add( mi, e ) ) {
168 Debug( LDAP_DEBUG_ANY,
169 "monitor_subsys_thread_init: "
170 "unable to add entry \"cn=Runqueue,%s\"\n",
171 ms->mss_ndn.bv_val, 0, 0 );
181 BER_BVSTR( &bv, "cn=Tasklist" );
182 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, &bv,
183 mi->mi_oc_monitoredObject, mi, NULL, NULL );
185 Debug( LDAP_DEBUG_ANY,
186 "monitor_subsys_thread_init: "
187 "unable to create entry \"%s,%s\"\n",
188 bv.bv_val, ms->mss_ndn.bv_val, 0 );
191 BER_BVSTR( &bv, "0" );
192 attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
194 mp = monitor_entrypriv_create();
198 e->e_private = ( void * )mp;
200 mp->mp_flags = ms->mss_flags \
201 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
203 if ( monitor_cache_add( mi, e ) ) {
204 Debug( LDAP_DEBUG_ANY,
205 "monitor_subsys_thread_init: "
206 "unable to add entry \"%s\"\n",
207 e->e_name.bv_val, 0, 0 );
214 monitor_cache_release( mi, e_thread );
220 monitor_subsys_thread_update(
225 monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private;
227 char buf[ BACKMONITOR_BUFSIZE ];
235 static slap_verbmasks mt[] = {
236 { BER_BVC( "cn=backload" ), MT_BACKLOAD },
237 { BER_BVC( "cn=runqueue" ), MT_RUNQUEUE },
238 { BER_BVC( "cn=tasklist" ), MT_TASKLIST },
239 { BER_BVC( "cn=max" ), MT_UNKNOWN },
240 { BER_BVNULL, MT_UNKNOWN }
242 struct berval rdn, bv;
247 assert( mi != NULL );
249 dnRdn( &e->e_nname, &rdn );
251 which = bverb_to_mask( &rdn, mt );
252 if ( BER_BVISNULL( &mt[ which ].word )
253 || mt[ which ].mask == MT_UNKNOWN )
255 return SLAP_CB_CONTINUE;
258 a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
260 return rs->sr_err = LDAP_OTHER;
263 switch ( mt[ which ].mask ) {
265 snprintf( buf, sizeof( buf ), "%d",
266 ldap_pvt_thread_pool_backload( &connection_pool ) );
268 if ( len > a->a_vals[ 0 ].bv_len ) {
269 a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
271 a->a_vals[ 0 ].bv_len = len;
272 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
276 for ( i = 0; !BER_BVISNULL( &a->a_vals[ i ] ); i++ ) {
277 ch_free( a->a_vals[ i ].bv_val );
278 BER_BVZERO( &a->a_vals[ i ] );
280 if ( a->a_nvals != a->a_vals ) {
281 ber_bvarray_free( a->a_nvals );
285 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
287 LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) {
288 bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)",
289 i, re->tname, re->tspec );
290 if ( bv.bv_len < sizeof( buf ) ) {
291 value_add_one( &a->a_vals, &bv );
295 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
297 /* don't leave 'round attributes with no values */
298 if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
299 BER_BVSTR( &bv, "{0}()" );
300 value_add_one( &a->a_vals, &bv );
302 a->a_nvals = a->a_vals;
306 for ( i = 0; !BER_BVISNULL( &a->a_vals[ i ] ); i++ ) {
307 ch_free( a->a_vals[ i ].bv_val );
308 BER_BVZERO( &a->a_vals[ i ] );
310 if ( a->a_nvals != a->a_vals ) {
311 ber_bvarray_free( a->a_nvals );
315 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
317 LDAP_STAILQ_FOREACH( re, &slapd_rq.task_list, rnext ) {
318 bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)",
319 i, re->tname, re->tspec );
320 if ( bv.bv_len < sizeof( buf ) ) {
321 value_add_one( &a->a_vals, &bv );
325 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
327 /* don't leave 'round attributes with no values */
328 if ( BER_BVISNULL( &a->a_vals[ 0 ] ) ) {
329 BER_BVSTR( &bv, "{0}()" );
330 value_add_one( &a->a_vals, &bv );
332 a->a_nvals = a->a_vals;
336 /* FIXME: touch modifyTimestamp? */
338 return SLAP_CB_CONTINUE;