| 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-2024 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 |
typedef enum { |
| 33 |
MT_UNKNOWN, |
| 34 |
MT_RUNQUEUE, |
| 35 |
MT_TASKLIST, |
| 36 |
|
| 37 |
MT_LAST |
| 38 |
} monitor_thread_t; |
| 39 |
|
| 40 |
static struct { |
| 41 |
struct berval rdn; |
| 42 |
struct berval desc; |
| 43 |
struct berval nrdn; |
| 44 |
ldap_pvt_thread_pool_param_t param; |
| 45 |
monitor_thread_t mt; |
| 46 |
} mt[] = { |
| 47 |
{ BER_BVC( "cn=Max" ), |
| 48 |
BER_BVC("Maximum number of threads as configured"), |
| 49 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_MAX, MT_UNKNOWN }, |
| 50 |
{ BER_BVC( "cn=Max Pending" ), |
| 51 |
BER_BVC("Maximum number of pending threads"), |
| 52 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING, MT_UNKNOWN }, |
| 53 |
{ BER_BVC( "cn=Open" ), |
| 54 |
BER_BVC("Number of open threads"), |
| 55 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_OPEN, MT_UNKNOWN }, |
| 56 |
{ BER_BVC( "cn=Starting" ), |
| 57 |
BER_BVC("Number of threads being started"), |
| 58 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_STARTING, MT_UNKNOWN }, |
| 59 |
{ BER_BVC( "cn=Active" ), |
| 60 |
BER_BVC("Number of active threads"), |
| 61 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_ACTIVE, MT_UNKNOWN }, |
| 62 |
{ BER_BVC( "cn=Pending" ), |
| 63 |
BER_BVC("Number of pending threads"), |
| 64 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_PENDING, MT_UNKNOWN }, |
| 65 |
{ BER_BVC( "cn=Backload" ), |
| 66 |
BER_BVC("Number of active plus pending threads"), |
| 67 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD, MT_UNKNOWN }, |
| 68 |
#if 0 /* not meaningful right now */ |
| 69 |
{ BER_BVC( "cn=Active Max" ), |
| 70 |
BER_BVNULL, |
| 71 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX, MT_UNKNOWN }, |
| 72 |
{ BER_BVC( "cn=Pending Max" ), |
| 73 |
BER_BVNULL, |
| 74 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX, MT_UNKNOWN }, |
| 75 |
{ BER_BVC( "cn=Backload Max" ), |
| 76 |
BER_BVNULL, |
| 77 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX,MT_UNKNOWN }, |
| 78 |
#endif |
| 79 |
{ BER_BVC( "cn=State" ), |
| 80 |
BER_BVC("Thread pool state"), |
| 81 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_STATE, MT_UNKNOWN }, |
| 82 |
|
| 83 |
{ BER_BVC( "cn=Runqueue" ), |
| 84 |
BER_BVC("Queue of running threads - besides those handling operations"), |
| 85 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN, MT_RUNQUEUE }, |
| 86 |
{ BER_BVC( "cn=Tasklist" ), |
| 87 |
BER_BVC("List of running plus standby threads - besides those handling operations"), |
| 88 |
BER_BVNULL, LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN, MT_TASKLIST }, |
| 89 |
|
| 90 |
{ BER_BVNULL } |
| 91 |
}; |
| 92 |
|
| 93 |
static int |
| 94 |
monitor_subsys_thread_update( |
| 95 |
Operation *op, |
| 96 |
SlapReply *rs, |
| 97 |
Entry *e ); |
| 98 |
|
| 99 |
/* |
| 100 |
* initializes log subentry |
| 101 |
*/ |
| 102 |
int |
| 103 |
monitor_subsys_thread_init( |
| 104 |
BackendDB *be, |
| 105 |
monitor_subsys_t *ms ) |
| 106 |
{ |
| 107 |
monitor_info_t *mi; |
| 108 |
monitor_entry_t *mp; |
| 109 |
Entry *e, *e_thread; |
| 110 |
int i; |
| 111 |
|
| 112 |
ms->mss_update = monitor_subsys_thread_update; |
| 113 |
|
| 114 |
mi = ( monitor_info_t * )be->be_private; |
| 115 |
|
| 116 |
if ( monitor_cache_get( mi, &ms->mss_ndn, &e_thread ) ) { |
| 117 |
Debug( LDAP_DEBUG_ANY, |
| 118 |
"monitor_subsys_thread_init: unable to get entry \"%s\"\n", |
| 119 |
ms->mss_dn.bv_val ); |
| 120 |
return( -1 ); |
| 121 |
} |
| 122 |
|
| 123 |
for ( i = 0; !BER_BVISNULL( &mt[ i ].rdn ); i++ ) { |
| 124 |
static char buf[ BACKMONITOR_BUFSIZE ]; |
| 125 |
int count = -1; |
| 126 |
char *state = NULL; |
| 127 |
struct berval bv = BER_BVNULL; |
| 128 |
|
| 129 |
/* |
| 130 |
* Max |
| 131 |
*/ |
| 132 |
e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn, |
| 133 |
&mt[ i ].rdn, |
| 134 |
mi->mi_oc_monitoredObject, NULL, NULL ); |
| 135 |
if ( e == NULL ) { |
| 136 |
Debug( LDAP_DEBUG_ANY, |
| 137 |
"monitor_subsys_thread_init: " |
| 138 |
"unable to create entry \"%s,%s\"\n", |
| 139 |
mt[ i ].rdn.bv_val, |
| 140 |
ms->mss_ndn.bv_val ); |
| 141 |
return( -1 ); |
| 142 |
} |
| 143 |
|
| 144 |
/* NOTE: reference to the normalized DN of the entry, |
| 145 |
* under the assumption it's not modified */ |
| 146 |
dnRdn( &e->e_nname, &mt[ i ].nrdn ); |
| 147 |
|
| 148 |
switch ( mt[ i ].param ) { |
| 149 |
case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN: |
| 150 |
break; |
| 151 |
|
| 152 |
case LDAP_PVT_THREAD_POOL_PARAM_STATE: |
| 153 |
if ( ldap_pvt_thread_pool_query( &connection_pool, |
| 154 |
mt[ i ].param, (void *)&state ) == 0 ) |
| 155 |
{ |
| 156 |
ber_str2bv( state, 0, 0, &bv ); |
| 157 |
|
| 158 |
} else { |
| 159 |
BER_BVSTR( &bv, "unknown" ); |
| 160 |
} |
| 161 |
break; |
| 162 |
|
| 163 |
default: |
| 164 |
/* NOTE: in case of error, it'll be set to -1 */ |
| 165 |
(void)ldap_pvt_thread_pool_query( &connection_pool, |
| 166 |
mt[ i ].param, (void *)&count ); |
| 167 |
bv.bv_val = buf; |
| 168 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%d", count ); |
| 169 |
break; |
| 170 |
} |
| 171 |
|
| 172 |
if ( !BER_BVISNULL( &bv ) ) { |
| 173 |
attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL ); |
| 174 |
} |
| 175 |
|
| 176 |
if ( !BER_BVISNULL( &mt[ i ].desc ) ) { |
| 177 |
attr_merge_normalize_one( e, |
| 178 |
slap_schema.si_ad_description, |
| 179 |
&mt[ i ].desc, NULL ); |
| 180 |
} |
| 181 |
|
| 182 |
mp = monitor_entrypriv_create(); |
| 183 |
if ( mp == NULL ) { |
| 184 |
return -1; |
| 185 |
} |
| 186 |
e->e_private = ( void * )mp; |
| 187 |
mp->mp_info = ms; |
| 188 |
mp->mp_flags = ms->mss_flags \ |
| 189 |
| MONITOR_F_SUB | MONITOR_F_PERSISTENT; |
| 190 |
|
| 191 |
if ( monitor_cache_add( mi, e, e_thread ) ) { |
| 192 |
Debug( LDAP_DEBUG_ANY, |
| 193 |
"monitor_subsys_thread_init: " |
| 194 |
"unable to add entry \"%s,%s\"\n", |
| 195 |
mt[ i ].rdn.bv_val, |
| 196 |
ms->mss_dn.bv_val ); |
| 197 |
return( -1 ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
monitor_cache_release( mi, e_thread ); |
| 202 |
|
| 203 |
return( 0 ); |
| 204 |
} |
| 205 |
|
| 206 |
static int |
| 207 |
monitor_subsys_thread_update( |
| 208 |
Operation *op, |
| 209 |
SlapReply *rs, |
| 210 |
Entry *e ) |
| 211 |
{ |
| 212 |
monitor_info_t *mi = ( monitor_info_t * )op->o_bd->be_private; |
| 213 |
Attribute *a; |
| 214 |
BerVarray vals = NULL; |
| 215 |
char buf[ BACKMONITOR_BUFSIZE ]; |
| 216 |
struct berval rdn, bv; |
| 217 |
int which, i; |
| 218 |
struct re_s *re; |
| 219 |
int count = -1; |
| 220 |
char *state = NULL; |
| 221 |
|
| 222 |
assert( mi != NULL ); |
| 223 |
|
| 224 |
dnRdn( &e->e_nname, &rdn ); |
| 225 |
|
| 226 |
for ( i = 0; !BER_BVISNULL( &mt[ i ].nrdn ); i++ ) { |
| 227 |
if ( dn_match( &mt[ i ].nrdn, &rdn ) ) { |
| 228 |
break; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
which = i; |
| 233 |
if ( BER_BVISNULL( &mt[ which ].nrdn ) ) { |
| 234 |
return SLAP_CB_CONTINUE; |
| 235 |
} |
| 236 |
|
| 237 |
a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo ); |
| 238 |
|
| 239 |
switch ( mt[ which ].param ) { |
| 240 |
case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN: |
| 241 |
switch ( mt[ which ].mt ) { |
| 242 |
case MT_RUNQUEUE: |
| 243 |
if ( a != NULL ) { |
| 244 |
if ( a->a_nvals != a->a_vals ) { |
| 245 |
ber_bvarray_free( a->a_nvals ); |
| 246 |
} |
| 247 |
ber_bvarray_free( a->a_vals ); |
| 248 |
a->a_vals = NULL; |
| 249 |
a->a_nvals = NULL; |
| 250 |
a->a_numvals = 0; |
| 251 |
} |
| 252 |
|
| 253 |
i = 0; |
| 254 |
bv.bv_val = buf; |
| 255 |
ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex ); |
| 256 |
LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) { |
| 257 |
bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)", |
| 258 |
i, re->tname, re->tspec ); |
| 259 |
if ( bv.bv_len < sizeof( buf ) ) { |
| 260 |
value_add_one( &vals, &bv ); |
| 261 |
} |
| 262 |
i++; |
| 263 |
} |
| 264 |
ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex ); |
| 265 |
|
| 266 |
if ( vals ) { |
| 267 |
attr_merge_normalize( e, mi->mi_ad_monitoredInfo, vals, NULL ); |
| 268 |
ber_bvarray_free( vals ); |
| 269 |
|
| 270 |
} else { |
| 271 |
attr_delete( &e->e_attrs, mi->mi_ad_monitoredInfo ); |
| 272 |
} |
| 273 |
break; |
| 274 |
|
| 275 |
case MT_TASKLIST: |
| 276 |
if ( a != NULL ) { |
| 277 |
if ( a->a_nvals != a->a_vals ) { |
| 278 |
ber_bvarray_free( a->a_nvals ); |
| 279 |
} |
| 280 |
ber_bvarray_free( a->a_vals ); |
| 281 |
a->a_vals = NULL; |
| 282 |
a->a_nvals = NULL; |
| 283 |
a->a_numvals = 0; |
| 284 |
} |
| 285 |
|
| 286 |
i = 0; |
| 287 |
bv.bv_val = buf; |
| 288 |
ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex ); |
| 289 |
LDAP_STAILQ_FOREACH( re, &slapd_rq.task_list, tnext ) { |
| 290 |
bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)", |
| 291 |
i, re->tname, re->tspec ); |
| 292 |
if ( bv.bv_len < sizeof( buf ) ) { |
| 293 |
value_add_one( &vals, &bv ); |
| 294 |
} |
| 295 |
i++; |
| 296 |
} |
| 297 |
ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex ); |
| 298 |
|
| 299 |
if ( vals ) { |
| 300 |
attr_merge_normalize( e, mi->mi_ad_monitoredInfo, vals, NULL ); |
| 301 |
ber_bvarray_free( vals ); |
| 302 |
|
| 303 |
} else { |
| 304 |
attr_delete( &e->e_attrs, mi->mi_ad_monitoredInfo ); |
| 305 |
} |
| 306 |
break; |
| 307 |
|
| 308 |
default: |
| 309 |
assert( 0 ); |
| 310 |
} |
| 311 |
break; |
| 312 |
|
| 313 |
case LDAP_PVT_THREAD_POOL_PARAM_STATE: |
| 314 |
if ( a == NULL ) { |
| 315 |
return rs->sr_err = LDAP_OTHER; |
| 316 |
} |
| 317 |
if ( ldap_pvt_thread_pool_query( &connection_pool, |
| 318 |
mt[ i ].param, (void *)&state ) == 0 ) |
| 319 |
{ |
| 320 |
ber_str2bv( state, 0, 0, &bv ); |
| 321 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 322 |
} |
| 323 |
break; |
| 324 |
|
| 325 |
default: |
| 326 |
if ( a == NULL ) { |
| 327 |
return rs->sr_err = LDAP_OTHER; |
| 328 |
} |
| 329 |
if ( ldap_pvt_thread_pool_query( &connection_pool, |
| 330 |
mt[ i ].param, (void *)&count ) == 0 ) |
| 331 |
{ |
| 332 |
bv.bv_val = buf; |
| 333 |
bv.bv_len = snprintf( buf, sizeof( buf ), "%d", count ); |
| 334 |
if ( bv.bv_len < sizeof( buf ) ) { |
| 335 |
ber_bvreplace( &a->a_vals[ 0 ], &bv ); |
| 336 |
} |
| 337 |
} |
| 338 |
break; |
| 339 |
} |
| 340 |
|
| 341 |
/* FIXME: touch modifyTimestamp? */ |
| 342 |
|
| 343 |
return SLAP_CB_CONTINUE; |
| 344 |
} |