thread.c 8.51 KB
Newer Older
1
/* thread.c - deal with thread subsystem */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
4
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
5
 * Copyright 2001-2020 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
 * Portions Copyright 2001-2003 Pierangelo Masarati.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
7
8
9
10
11
12
13
14
15
16
17
18
19
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Pierangelo Masarati for inclusion
 * in OpenLDAP Software.
20
21
22
23
24
 */

#include "portable.h"

#include <stdio.h>
25
#include <ac/string.h>
26
27
28
29

#include "slap.h"
#include "back-monitor.h"

30
31
#include <ldap_rq.h>

32
33
34
35
36
37
38
39
40
41
typedef enum {
	MT_UNKNOWN,
	MT_RUNQUEUE,
	MT_TASKLIST,

	MT_LAST
} monitor_thread_t;

static struct {
	struct berval			rdn;
42
	struct berval			desc;
43
44
45
46
	struct berval			nrdn;
	ldap_pvt_thread_pool_param_t	param;
	monitor_thread_t		mt;
}		mt[] = {
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
	{ BER_BVC( "cn=Max" ),
		BER_BVC("Maximum number of threads as configured"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_MAX,		MT_UNKNOWN },
	{ BER_BVC( "cn=Max Pending" ),
		BER_BVC("Maximum number of pending threads"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_MAX_PENDING,	MT_UNKNOWN },
	{ BER_BVC( "cn=Open" ),		
		BER_BVC("Number of open threads"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_OPEN,	MT_UNKNOWN },
	{ BER_BVC( "cn=Starting" ),	
		BER_BVC("Number of threads being started"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_STARTING,	MT_UNKNOWN },
	{ BER_BVC( "cn=Active" ),	
		BER_BVC("Number of active threads"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_ACTIVE,	MT_UNKNOWN },
	{ BER_BVC( "cn=Pending" ),	
		BER_BVC("Number of pending threads"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_PENDING,	MT_UNKNOWN },
	{ BER_BVC( "cn=Backload" ),	
		BER_BVC("Number of active plus pending threads"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD,	MT_UNKNOWN },
68
#if 0	/* not meaningful right now */
69
70
71
72
73
74
75
76
77
	{ BER_BVC( "cn=Active Max" ),
		BER_BVNULL,
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_ACTIVE_MAX,	MT_UNKNOWN },
	{ BER_BVC( "cn=Pending Max" ),
		BER_BVNULL,
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_PENDING_MAX,	MT_UNKNOWN },
	{ BER_BVC( "cn=Backload Max" ),
		BER_BVNULL,
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_BACKLOAD_MAX,MT_UNKNOWN },
78
#endif
79
80
81
	{ BER_BVC( "cn=State" ),
		BER_BVC("Thread pool state"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_STATE,	MT_UNKNOWN },
82

83
84
85
86
	{ BER_BVC( "cn=Runqueue" ),
		BER_BVC("Queue of running threads - besides those handling operations"),
		BER_BVNULL,	LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN,	MT_RUNQUEUE },
	{ 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
91
92

	{ BER_BVNULL }
};

93
94
95
96
97
98
static int 
monitor_subsys_thread_update( 
	Operation		*op,
	SlapReply		*rs,
	Entry 			*e );

99
/*
100
101
 * initializes log subentry
 */
102
103
int
monitor_subsys_thread_init(
104
	BackendDB       	*be,
105
	monitor_subsys_t	*ms )
106
{
107
108
109
	monitor_info_t	*mi;
	monitor_entry_t	*mp;
	Entry		*e, **ep, *e_thread;
110
	int		i;
111

112
113
	ms->mss_update = monitor_subsys_thread_update;

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
121
122
		return( -1 );
	}

123
	mp = ( monitor_entry_t * )e_thread->e_private;
124
125
126
	mp->mp_children = NULL;
	ep = &mp->mp_children;

127
128
129
130
131
132
133
134
135
136
137
	for ( i = 0; !BER_BVISNULL( &mt[ i ].rdn ); i++ ) {
		static char	buf[ BACKMONITOR_BUFSIZE ];
		int		count = -1;
		char		*state = NULL;
		struct berval	bv = BER_BVNULL;

		/*
		 * Max
		 */
		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
			&mt[ i ].rdn,
138
			mi->mi_oc_monitoredObject, NULL, NULL );
139
140
141
142
143
		if ( e == NULL ) {
			Debug( LDAP_DEBUG_ANY,
				"monitor_subsys_thread_init: "
				"unable to create entry \"%s,%s\"\n",
				mt[ i ].rdn.bv_val,
144
				ms->mss_ndn.bv_val );
145
146
			return( -1 );
		}
147

148
149
150
		/* NOTE: reference to the normalized DN of the entry,
		 * under the assumption it's not modified */
		dnRdn( &e->e_nname, &mt[ i ].nrdn );
151

152
153
154
		switch ( mt[ i ].param ) {
		case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
			break;
155

156
		case LDAP_PVT_THREAD_POOL_PARAM_STATE:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
157
158
159
160
161
162
163
164
			if ( ldap_pvt_thread_pool_query( &connection_pool,
				mt[ i ].param, (void *)&state ) == 0 )
			{
				ber_str2bv( state, 0, 0, &bv );

			} else {
				BER_BVSTR( &bv, "unknown" );
			}
165
			break;
166

167
		default:
Pierangelo Masarati's avatar
Pierangelo Masarati committed
168
169
			/* NOTE: in case of error, it'll be set to -1 */
			(void)ldap_pvt_thread_pool_query( &connection_pool,
170
171
172
173
174
				mt[ i ].param, (void *)&count );
			bv.bv_val = buf;
			bv.bv_len = snprintf( buf, sizeof( buf ), "%d", count );
			break;
		}
175

176
177
178
		if ( !BER_BVISNULL( &bv ) ) {
			attr_merge_normalize_one( e, mi->mi_ad_monitoredInfo, &bv, NULL );
		}
179
180
181
182
183
184

		if ( !BER_BVISNULL( &mt[ i ].desc ) ) {
			attr_merge_normalize_one( e,
				slap_schema.si_ad_description,
				&mt[ i ].desc, NULL );
		}
185
	
186
187
188
189
190
191
192
193
194
195
196
197
198
199
		mp = monitor_entrypriv_create();
		if ( mp == NULL ) {
			return -1;
		}
		e->e_private = ( void * )mp;
		mp->mp_info = ms;
		mp->mp_flags = ms->mss_flags \
			| MONITOR_F_SUB | MONITOR_F_PERSISTENT;

		if ( monitor_cache_add( mi, e ) ) {
			Debug( LDAP_DEBUG_ANY,
				"monitor_subsys_thread_init: "
				"unable to add entry \"%s,%s\"\n",
				mt[ i ].rdn.bv_val,
200
				ms->mss_dn.bv_val );
201
202
			return( -1 );
		}
203
	
204
205
206
		*ep = e;
		ep = &mp->mp_next;
	}
207

208
	monitor_cache_release( mi, e_thread );
209
210
211

	return( 0 );
}
212

213
static int 
214
monitor_subsys_thread_update( 
215
	Operation		*op,
216
	SlapReply		*rs,
217
	Entry 			*e )
218
{
219
	monitor_info_t	*mi = ( monitor_info_t * )op->o_bd->be_private;
220
	Attribute		*a;
221
	BerVarray		vals = NULL;
222
	char 			buf[ BACKMONITOR_BUFSIZE ];
223
	struct berval		rdn, bv;
224
225
	int			which, i;
	struct re_s		*re;
226
227
	int			count = -1;
	char			*state = NULL;
228

229
230
	assert( mi != NULL );

231
	dnRdn( &e->e_nname, &rdn );
232

233
234
235
236
237
238
	for ( i = 0; !BER_BVISNULL( &mt[ i ].nrdn ); i++ ) {
		if ( dn_match( &mt[ i ].nrdn, &rdn ) ) {
			break;
		}
	}

Howard Chu's avatar
Howard Chu committed
239
	which = i;
240
	if ( BER_BVISNULL( &mt[ which ].nrdn ) ) {
241
242
		return SLAP_CB_CONTINUE;
	}
243

Howard Chu's avatar
Howard Chu committed
244
245
	a = attr_find( e->e_attrs, mi->mi_ad_monitoredInfo );

246
247
248
249
250
251
252
253
254
255
256
	switch ( mt[ which ].param ) {
	case LDAP_PVT_THREAD_POOL_PARAM_UNKNOWN:
		switch ( mt[ which ].mt ) {
		case MT_RUNQUEUE:
			if ( a != NULL ) {
				if ( a->a_nvals != a->a_vals ) {
					ber_bvarray_free( a->a_nvals );
				}
				ber_bvarray_free( a->a_vals );
				a->a_vals = NULL;
				a->a_nvals = NULL;
257
				a->a_numvals = 0;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
			}

			i = 0;
			bv.bv_val = buf;
			ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
			LDAP_STAILQ_FOREACH( re, &slapd_rq.run_list, rnext ) {
				bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)",
					i, re->tname, re->tspec );
				if ( bv.bv_len < sizeof( buf ) ) {
					value_add_one( &vals, &bv );
				}
				i++;
			}
			ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
	
			if ( vals ) {
				attr_merge_normalize( e, mi->mi_ad_monitoredInfo, vals, NULL );
				ber_bvarray_free( vals );
276
277
278

			} else {
				attr_delete( &e->e_attrs, mi->mi_ad_monitoredInfo );
279
280
281
282
283
284
285
286
287
288
289
			}
			break;

		case MT_TASKLIST:
			if ( a != NULL ) {
				if ( a->a_nvals != a->a_vals ) {
					ber_bvarray_free( a->a_nvals );
				}
				ber_bvarray_free( a->a_vals );
				a->a_vals = NULL;
				a->a_nvals = NULL;
290
				a->a_numvals = 0;
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
			}
	
			i = 0;
			bv.bv_val = buf;
			ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
			LDAP_STAILQ_FOREACH( re, &slapd_rq.task_list, tnext ) {
				bv.bv_len = snprintf( buf, sizeof( buf ), "{%d}%s(%s)",
					i, re->tname, re->tspec );
				if ( bv.bv_len < sizeof( buf ) ) {
					value_add_one( &vals, &bv );
				}
				i++;
			}
			ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
	
			if ( vals ) {
				attr_merge_normalize( e, mi->mi_ad_monitoredInfo, vals, NULL );
				ber_bvarray_free( vals );
309
310
311

			} else {
				attr_delete( &e->e_attrs, mi->mi_ad_monitoredInfo );
312
313
314
315
316
317
318
319
320
			}
			break;

		default:
			assert( 0 );
		}
		break;

	case LDAP_PVT_THREAD_POOL_PARAM_STATE:
321
322
323
		if ( a == NULL ) {
			return rs->sr_err = LDAP_OTHER;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
324
325
326
327
328
329
		if ( ldap_pvt_thread_pool_query( &connection_pool,
			mt[ i ].param, (void *)&state ) == 0 )
		{
			ber_str2bv( state, 0, 0, &bv );
			ber_bvreplace( &a->a_vals[ 0 ], &bv );
		}
330
331
		break;

332
	default:
333
334
335
		if ( a == NULL ) {
			return rs->sr_err = LDAP_OTHER;
		}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
336
337
338
339
340
341
342
343
		if ( ldap_pvt_thread_pool_query( &connection_pool,
			mt[ i ].param, (void *)&count ) == 0 )
		{
			bv.bv_val = buf;
			bv.bv_len = snprintf( buf, sizeof( buf ), "%d", count );
			if ( bv.bv_len < sizeof( buf ) ) {
				ber_bvreplace( &a->a_vals[ 0 ], &bv );
			}
344
345
		}
		break;
346
347
	}

348
349
	/* FIXME: touch modifyTimestamp? */

350
	return SLAP_CB_CONTINUE;
351
}