Debian Codegraph @

servers/slapd/back-monitor/sent.c

raw
1 /* sent.c - deal with data sent 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 static int
31 monitor_subsys_sent_destroy(
32 BackendDB *be,
33 monitor_subsys_t *ms );
34
35 static int
36 monitor_subsys_sent_update(
37 Operation *op,
38 SlapReply *rs,
39 Entry *e );
40
41 enum {
42 MONITOR_SENT_BYTES = 0,
43 MONITOR_SENT_PDU,
44 MONITOR_SENT_ENTRIES,
45 MONITOR_SENT_REFERRALS,
46
47 MONITOR_SENT_LAST
48 };
49
50 struct monitor_sent_t {
51 struct berval rdn;
52 struct berval nrdn;
53 } monitor_sent[] = {
54 { BER_BVC("cn=Bytes"), BER_BVNULL },
55 { BER_BVC("cn=PDU"), BER_BVNULL },
56 { BER_BVC("cn=Entries"), BER_BVNULL },
57 { BER_BVC("cn=Referrals"), BER_BVNULL },
58 { BER_BVNULL, BER_BVNULL }
59 };
60
61 int
62 monitor_subsys_sent_init(
63 BackendDB *be,
64 monitor_subsys_t *ms )
65 {
66 monitor_info_t *mi;
67
68 Entry *e_sent;
69 monitor_entry_t *mp;
70 int i;
71
72 assert( be != NULL );
73
74 ms->mss_destroy = monitor_subsys_sent_destroy;
75 ms->mss_update = monitor_subsys_sent_update;
76
77 mi = ( monitor_info_t * )be->be_private;
78
79 if ( monitor_cache_get( mi, &ms->mss_ndn, &e_sent ) ) {
80 Debug( LDAP_DEBUG_ANY,
81 "monitor_subsys_sent_init: "
82 "unable to get entry \"%s\"\n",
83 ms->mss_ndn.bv_val );
84 return( -1 );
85 }
86
87 for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
88 struct berval nrdn, bv;
89 Entry *e;
90
91 e = monitor_entry_stub( &ms->mss_dn, &ms->mss_ndn,
92 &monitor_sent[i].rdn, mi->mi_oc_monitorCounterObject,
93 NULL, NULL );
94
95 if ( e == NULL ) {
96 Debug( LDAP_DEBUG_ANY,
97 "monitor_subsys_sent_init: "
98 "unable to create entry \"%s,%s\"\n",
99 monitor_sent[ i ].rdn.bv_val,
100 ms->mss_ndn.bv_val );
101 return( -1 );
102 }
103
104 /* steal normalized RDN */
105 dnRdn( &e->e_nname, &nrdn );
106 ber_dupbv( &monitor_sent[ i ].nrdn, &nrdn );
107
108 BER_BVSTR( &bv, "0" );
109 attr_merge_one( e, mi->mi_ad_monitorCounter, &bv, NULL );
110
111 mp = monitor_entrypriv_create();
112 if ( mp == NULL ) {
113 return -1;
114 }
115 e->e_private = ( void * )mp;
116 mp->mp_info = ms;
117 mp->mp_flags = ms->mss_flags \
118 | MONITOR_F_SUB | MONITOR_F_PERSISTENT;
119
120 if ( monitor_cache_add( mi, e, e_sent ) ) {
121 Debug( LDAP_DEBUG_ANY,
122 "monitor_subsys_sent_init: "
123 "unable to add entry \"%s,%s\"\n",
124 monitor_sent[ i ].rdn.bv_val,
125 ms->mss_ndn.bv_val );
126 return( -1 );
127 }
128 }
129
130 monitor_cache_release( mi, e_sent );
131
132 return( 0 );
133 }
134
135 static int
136 monitor_subsys_sent_destroy(
137 BackendDB *be,
138 monitor_subsys_t *ms )
139 {
140 int i;
141
142 for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
143 if ( !BER_BVISNULL( &monitor_sent[ i ].nrdn ) ) {
144 ch_free( monitor_sent[ i ].nrdn.bv_val );
145 }
146 }
147
148 return 0;
149 }
150
151 static int
152 monitor_subsys_sent_update(
153 Operation *op,
154 SlapReply *rs,
155 Entry *e )
156 {
157 monitor_info_t *mi = ( monitor_info_t *)op->o_bd->be_private;
158
159 struct berval nrdn;
160 ldap_pvt_mp_t n;
161 Attribute *a;
162 slap_counters_t *sc;
163 int i;
164
165 assert( mi != NULL );
166 assert( e != NULL );
167
168 dnRdn( &e->e_nname, &nrdn );
169
170 for ( i = 0; i < MONITOR_SENT_LAST; i++ ) {
171 if ( dn_match( &nrdn, &monitor_sent[ i ].nrdn ) ) {
172 break;
173 }
174 }
175
176 if ( i == MONITOR_SENT_LAST ) {
177 return SLAP_CB_CONTINUE;
178 }
179
180 ldap_pvt_thread_mutex_lock(&slap_counters.sc_mutex);
181 switch ( i ) {
182 case MONITOR_SENT_ENTRIES:
183 ldap_pvt_mp_init_set( n, slap_counters.sc_entries );
184 for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
185 ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
186 ldap_pvt_mp_add( n, sc->sc_entries );
187 ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
188 }
189 break;
190
191 case MONITOR_SENT_REFERRALS:
192 ldap_pvt_mp_init_set( n, slap_counters.sc_refs );
193 for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
194 ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
195 ldap_pvt_mp_add( n, sc->sc_refs );
196 ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
197 }
198 break;
199
200 case MONITOR_SENT_PDU:
201 ldap_pvt_mp_init_set( n, slap_counters.sc_pdu );
202 for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
203 ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
204 ldap_pvt_mp_add( n, sc->sc_pdu );
205 ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
206 }
207 break;
208
209 case MONITOR_SENT_BYTES:
210 ldap_pvt_mp_init_set( n, slap_counters.sc_bytes );
211 for ( sc = slap_counters.sc_next; sc; sc = sc->sc_next ) {
212 ldap_pvt_thread_mutex_lock( &sc->sc_mutex );
213 ldap_pvt_mp_add( n, sc->sc_bytes );
214 ldap_pvt_thread_mutex_unlock( &sc->sc_mutex );
215 }
216 break;
217
218 default:
219 assert(0);
220 }
221 ldap_pvt_thread_mutex_unlock(&slap_counters.sc_mutex);
222
223 a = attr_find( e->e_attrs, mi->mi_ad_monitorCounter );
224 assert( a != NULL );
225
226 /* NOTE: no minus sign is allowed in the counters... */
227 UI2BV( &a->a_vals[ 0 ], n );
228 ldap_pvt_mp_clear( n );
229
230 /* FIXME: touch modifyTimestamp? */
231
232 return SLAP_CB_CONTINUE;
233 }
234