Home | History | Annotate | Line # | Download | only in back-monitor
sent.c revision 1.1.1.10
      1 /*	$NetBSD: sent.c,v 1.1.1.10 2025/09/05 21:09:48 christos Exp $	*/
      2 
      3 /* sent.c - deal with data sent subsystem */
      4 /* $OpenLDAP$ */
      5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
      6  *
      7  * Copyright 2001-2024 The OpenLDAP Foundation.
      8  * Portions Copyright 2001-2003 Pierangelo Masarati.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted only as authorized by the OpenLDAP
     13  * Public License.
     14  *
     15  * A copy of this license is available in file LICENSE in the
     16  * top-level directory of the distribution or, alternatively, at
     17  * <http://www.OpenLDAP.org/license.html>.
     18  */
     19 /* ACKNOWLEDGEMENTS:
     20  * This work was initially developed by Pierangelo Masarati for inclusion
     21  * in OpenLDAP Software.
     22  */
     23 
     24 #include <sys/cdefs.h>
     25 __RCSID("$NetBSD: sent.c,v 1.1.1.10 2025/09/05 21:09:48 christos Exp $");
     26 
     27 #include "portable.h"
     28 
     29 #include <stdio.h>
     30 #include <ac/string.h>
     31 
     32 #include "slap.h"
     33 #include "back-monitor.h"
     34 
     35 static int
     36 monitor_subsys_sent_destroy(
     37 	BackendDB		*be,
     38 	monitor_subsys_t	*ms );
     39 
     40 static int
     41 monitor_subsys_sent_update(
     42 	Operation		*op,
     43 	SlapReply		*rs,
     44 	Entry                   *e );
     45 
     46 enum {
     47 	MONITOR_SENT_BYTES = 0,
     48 	MONITOR_SENT_PDU,
     49 	MONITOR_SENT_ENTRIES,
     50 	MONITOR_SENT_REFERRALS,
     51 
     52 	MONITOR_SENT_LAST
     53 };
     54 
     55 struct monitor_sent_t {
     56 	struct berval	rdn;
     57 	struct berval	nrdn;
     58 } monitor_sent[] = {
     59 	{ BER_BVC("cn=Bytes"),		BER_BVNULL },
     60 	{ BER_BVC("cn=PDU"),		BER_BVNULL },
     61 	{ BER_BVC("cn=Entries"),	BER_BVNULL },
     62 	{ BER_BVC("cn=Referrals"),	BER_BVNULL },
     63 	{ BER_BVNULL,			BER_BVNULL }
     64 };
     65 
     66 int
     67 monitor_subsys_sent_init(
     68 	BackendDB		*be,
     69 	monitor_subsys_t	*ms )
     70 {
     71 	monitor_info_t	*mi;
     72 
     73 	Entry		*e_sent;
     74 	monitor_entry_t	*mp;
     75 	int			i;
     76 
     77 	assert( be != NULL );
     78 
     79 	ms->mss_destroy = monitor_subsys_sent_destroy;
     80 	ms->mss_update = monitor_subsys_sent_update;
     81 
     82 	mi = ( monitor_info_t * )be->be_private;
     83 
     84 	if ( monitor_cache_get( mi, &ms->mss_ndn, &e_sent ) ) {
     85 		Debug( LDAP_DEBUG_ANY,
     86 			"monitor_subsys_sent_init: "
     87 			"unable to get entry \"%s\"\n",
     88 			ms->mss_ndn.bv_val );
     89 		return( -1 );
     90 	}
     91 
     92 	for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
     93 		struct berval		nrdn, bv;
     94 		Entry			*e;
     95 
     96 		e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
     97 			&monitor_sent[i].rdn, mi->mi_oc_monitorCounterObject,
     98 			NULL, NULL );
     99 
    100 		if ( e == NULL ) {
    101 			Debug( LDAP_DEBUG_ANY,
    102 				"monitor_subsys_sent_init: "
    103 				"unable to create entry \"%s,%s\"\n",
    104 				monitor_sent[ i ].rdn.bv_val,
    105 				ms->mss_ndn.bv_val );
    106 			return( -1 );
    107 		}
    108 
    109 		/* steal normalized RDN */
    110 		dnRdn( &e->e_nname, &nrdn );
    111 		ber_dupbv( &monitor_sent[ i ].nrdn, &nrdn );
    112 
    113 		BER_BVSTR( &bv, "0" );
    114 		attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
    115 
    116 		mp = monitor_entrypriv_create();
    117 		if ( mp == NULL ) {
    118 			return -1;
    119 		}
    120 		e->e_private = ( void * )mp;
    121 		mp->mp_info = ms;
    122 		mp->mp_flags = ms->mss_flags \
    123 			| MONITOR_F_SUB | MONITOR_F_PERSISTENT;
    124 
    125 		if ( monitor_cache_add( mi, e, e_sent ) ) {
    126 			Debug( LDAP_DEBUG_ANY,
    127 				"monitor_subsys_sent_init: "
    128 				"unable to add entry \"%s,%s\"\n",
    129 				monitor_sent[ i ].rdn.bv_val,
    130 				ms->mss_ndn.bv_val );
    131 			return( -1 );
    132 		}
    133 	}
    134 
    135 	monitor_cache_release( mi, e_sent );
    136 
    137 	return( 0 );
    138 }
    139 
    140 static int
    141 monitor_subsys_sent_destroy(
    142 	BackendDB		*be,
    143 	monitor_subsys_t	*ms )
    144 {
    145 	int		i;
    146 
    147 	for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
    148 		if ( !BER_BVISNULL( &monitor_sent[ i ].nrdn ) ) {
    149 			ch_free( monitor_sent[ i ].nrdn.bv_val );
    150 		}
    151 	}
    152 
    153 	return 0;
    154 }
    155 
    156 static int
    157 monitor_subsys_sent_update(
    158 	Operation		*op,
    159 	SlapReply		*rs,
    160 	Entry                   *e )
    161 {
    162 	monitor_info_t	*mi = ( monitor_info_t *)op->o_bd->be_private;
    163 
    164 	struct berval		nrdn;
    165 	ldap_pvt_mp_t		n;
    166 	Attribute		*a;
    167 	slap_counters_t *sc;
    168 	int			i;
    169 
    170 	assert( mi != NULL );
    171 	assert( e != NULL );
    172 
    173 	dnRdn( &e->e_nname, &nrdn );
    174 
    175 	for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
    176 		if ( dn_match( &nrdn, &monitor_sent[ i ].nrdn ) ) {
    177 			break;
    178 		}
    179 	}
    180 
    181 	if ( i == MONITOR_SENT_LAST ) {
    182 		return SLAP_CB_CONTINUE;
    183 	}
    184 
    185 	ldap_pvt_thread_mutex_lock(&slap_counters.sc_mutex);
    186 	switch ( i ) {
    187 	case MONITOR_SENT_ENTRIES:
    188 		ldap_pvt_mp_init_set( n, slap_counters.sc_entries );
    189 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
    190 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
    191 			ldap_pvt_mp_add( n, sc->sc_entries );
    192 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
    193 		}
    194 		break;
    195 
    196 	case MONITOR_SENT_REFERRALS:
    197 		ldap_pvt_mp_init_set( n, slap_counters.sc_refs );
    198 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
    199 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
    200 			ldap_pvt_mp_add( n, sc->sc_refs );
    201 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
    202 		}
    203 		break;
    204 
    205 	case MONITOR_SENT_PDU:
    206 		ldap_pvt_mp_init_set( n, slap_counters.sc_pdu );
    207 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
    208 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
    209 			ldap_pvt_mp_add( n, sc->sc_pdu );
    210 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
    211 		}
    212 		break;
    213 
    214 	case MONITOR_SENT_BYTES:
    215 		ldap_pvt_mp_init_set( n, slap_counters.sc_bytes );
    216 		for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
    217 			ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
    218 			ldap_pvt_mp_add( n, sc->sc_bytes );
    219 			ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
    220 		}
    221 		break;
    222 
    223 	default:
    224 		assert(0);
    225 	}
    226 	ldap_pvt_thread_mutex_unlock(&slap_counters.sc_mutex);
    227 
    228 	a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
    229 	assert( a != NULL );
    230 
    231 	/* NOTE: no minus sign is allowed in the counters... */
    232 	UI2BV( &a->a_vals[ 0 ], n );
    233 	ldap_pvt_mp_clear( n );
    234 
    235 	/* FIXME: touch modifyTimestamp? */
    236 
    237 	return SLAP_CB_CONTINUE;
    238 }
    239 
    240