]> git.sur5r.net Git - openldap/blob - servers/slapd/back-monitor/thread.c
Add task names to runqueue entries. Display running tasks under
[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-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 "back-monitor.h"
29
30 #include <ldap_rq.h>
31
32 /*
33 *  * initializes log subentry
34 *   */
35 int
36 monitor_subsys_thread_init(
37         BackendDB               *be,
38         monitor_subsys_t        *ms
39 )
40 {
41         monitor_info_t  *mi;
42         monitor_entry_t *mp;
43         Entry           *e, **ep, *e_thread;
44         static char     buf[ BACKMONITOR_BUFSIZE ];
45
46         mi = ( monitor_info_t * )be->be_private;
47
48         if ( monitor_cache_get( mi, &ms->mss_ndn, &e_thread ) ) {
49                 Debug( LDAP_DEBUG_ANY,
50                         "monitor_subsys_thread_init: unable to get entry \"%s\"\n",
51                         ms->mss_ndn.bv_val, 
52                         0, 0 );
53                 return( -1 );
54         }
55
56         mp = ( monitor_entry_t * )e_thread->e_private;
57         mp->mp_children = NULL;
58         ep = &mp->mp_children;
59
60         /*
61          * Max
62          */
63         snprintf( buf, sizeof( buf ),
64                         "dn: cn=Max,%s\n"
65                         "objectClass: %s\n"
66                         "structuralObjectClass: %s\n"
67                         "cn: Max\n"
68                         "%s: %d\n"
69                         "creatorsName: %s\n"
70                         "modifiersName: %s\n"
71                         "createTimestamp: %s\n"
72                         "modifyTimestamp: %s\n", 
73                         ms->mss_dn.bv_val,
74                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
75                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
76                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
77                         connection_pool_max,
78                         mi->mi_creatorsName.bv_val,
79                         mi->mi_creatorsName.bv_val,
80                         mi->mi_startTime.bv_val,
81                         mi->mi_startTime.bv_val );
82
83         e = str2entry( buf );
84         if ( e == NULL ) {
85                 Debug( LDAP_DEBUG_ANY,
86                         "monitor_subsys_thread_init: "
87                         "unable to create entry \"cn=Max,%s\"\n",
88                         ms->mss_ndn.bv_val, 0, 0 );
89                 return( -1 );
90         }
91         
92         mp = monitor_entrypriv_create();
93         if ( mp == NULL ) {
94                 return -1;
95         }
96         e->e_private = ( void * )mp;
97         mp->mp_info = ms;
98         mp->mp_flags = ms->mss_flags \
99                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
100
101         if ( monitor_cache_add( mi, e ) ) {
102                 Debug( LDAP_DEBUG_ANY,
103                         "monitor_subsys_thread_init: "
104                         "unable to add entry \"cn=Max,%s\"\n",
105                         ms->mss_ndn.bv_val, 0, 0 );
106                 return( -1 );
107         }
108         
109         *ep = e;
110         ep = &mp->mp_next;
111
112         /*
113          * Backload
114          */
115         snprintf( buf, sizeof( buf ),
116                         "dn: cn=Backload,%s\n"
117                         "objectClass: %s\n"
118                         "structuralObjectClass: %s\n"
119                         "cn: Backload\n"
120                         "%s: 0\n"
121                         "creatorsName: %s\n"
122                         "modifiersName: %s\n"
123                         "createTimestamp: %s\n"
124                         "modifyTimestamp: %s\n",
125                         ms->mss_dn.bv_val,
126                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
127                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
128                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
129                         mi->mi_creatorsName.bv_val,
130                         mi->mi_creatorsName.bv_val,
131                         mi->mi_startTime.bv_val,
132                         mi->mi_startTime.bv_val );
133
134         e = str2entry( buf );
135         if ( e == NULL ) {
136                 Debug( LDAP_DEBUG_ANY,
137                         "monitor_subsys_thread_init: "
138                         "unable to create entry \"cn=Backload,%s\"\n",
139                         ms->mss_ndn.bv_val, 0, 0 );
140                 return( -1 );
141         }
142
143         mp = monitor_entrypriv_create();
144         if ( mp == NULL ) {
145                 return -1;
146         }
147         e->e_private = ( void * )mp;
148         mp->mp_info = ms;
149         mp->mp_flags = ms->mss_flags \
150                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
151
152         if ( monitor_cache_add( mi, e ) ) {
153                 Debug( LDAP_DEBUG_ANY,
154                         "monitor_subsys_thread_init: "
155                         "unable to add entry \"cn=Backload,%s\"\n",
156                         ms->mss_ndn.bv_val, 0, 0 );
157                 return( -1 );
158         }
159         
160         *ep = e;
161         ep = &mp->mp_next;
162
163         /*
164          * Runqueue runners
165          */
166         snprintf( buf, sizeof( buf ),
167                         "dn: cn=Runqueue,%s\n"
168                         "objectClass: %s\n"
169                         "structuralObjectClass: %s\n"
170                         "cn: Runqueue\n"
171                         "%s: 0\n"
172                         "creatorsName: %s\n"
173                         "modifiersName: %s\n"
174                         "createTimestamp: %s\n"
175                         "modifyTimestamp: %s\n",
176                         ms->mss_dn.bv_val,
177                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
178                         mi->mi_oc_monitoredObject->soc_cname.bv_val,
179                         mi->mi_ad_monitoredInfo->ad_cname.bv_val,
180                         mi->mi_creatorsName.bv_val,
181                         mi->mi_creatorsName.bv_val,
182                         mi->mi_startTime.bv_val,
183                         mi->mi_startTime.bv_val );
184
185         e = str2entry( buf );
186         if ( e == NULL ) {
187                 Debug( LDAP_DEBUG_ANY,
188                         "monitor_subsys_thread_init: "
189                         "unable to create entry \"cn=Runqueue,%s\"\n",
190                         ms->mss_ndn.bv_val, 0, 0 );
191                 return( -1 );
192         }
193
194         mp = monitor_entrypriv_create();
195         if ( mp == NULL ) {
196                 return -1;
197         }
198         e->e_private = ( void * )mp;
199         mp->mp_info = ms;
200         mp->mp_flags = ms->mss_flags \
201                 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
202
203         if ( monitor_cache_add( mi, e ) ) {
204                 Debug( LDAP_DEBUG_ANY,
205                         "monitor_subsys_thread_init: "
206                         "unable to add entry \"cn=Runqueue,%s\"\n",
207                         ms->mss_ndn.bv_val, 0, 0 );
208                 return( -1 );
209         }
210         
211         *ep = e;
212         ep = &mp->mp_next;
213
214         monitor_cache_release( mi, e_thread );
215
216         return( 0 );
217 }
218
219 int 
220 monitor_subsys_thread_update( 
221         Operation               *op,
222         Entry                   *e
223 )
224 {
225         monitor_info_t  *mi = ( monitor_info_t * )op->o_bd->be_private;
226         Attribute               *a;
227         char                    buf[ BACKMONITOR_BUFSIZE ];
228         static struct berval    backload_bv = BER_BVC( "cn=backload" );
229         static struct berval    runqueue_bv = BER_BVC( "cn=runqueue" );
230         struct berval           rdn, bv;
231         ber_len_t               len;
232         int which = 0;
233         struct re_s *re;
234
235         assert( mi != NULL );
236
237         dnRdn( &e->e_nname, &rdn );
238         if ( dn_match( &rdn, &backload_bv ))
239                 which = 1;
240         else if ( dn_match( &rdn, &runqueue_bv ))
241                 which = 2;
242         else
243                 return 0;
244
245         switch( which ) {
246         case 1:
247                 a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );
248                 if ( a == NULL ) {
249                         return -1;
250                 }
251                 snprintf( buf, sizeof( buf ), "%d", 
252                         ldap_pvt_thread_pool_backload( &connection_pool ) );
253                 len = strlen( buf );
254                 if ( len > a->a_vals[ 0 ].bv_len ) {
255                         a->a_vals[ 0 ].bv_val = ber_memrealloc( a->a_vals[ 0 ].bv_val, len + 1 );
256                 }
257                 a->a_vals[ 0 ].bv_len = len;
258                 AC_MEMCPY( a->a_vals[ 0 ].bv_val, buf, len + 1 );
259                 break;
260         case 2:
261                 attr_delete( &e->e_attrs, mi->mi_ad_monitoredInfo );
262                 bv.bv_val = buf;
263                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
264                 LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) {
265                         bv.bv_len = snprintf( buf, sizeof( buf ), "%s(%s)",
266                                 re->tname, re->tspec );
267                         attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
268                 }
269                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
270                 break;
271         }
272
273         /* FIXME: touch modifyTimestamp? */
274
275         return( 0 );
276 }
277