Debian Codegraph @

lib/freeDiameter-1.2.1/extensions/dbg_monitor/dbg_monitor.c

raw
1 /*********************************************************************************************************
2 * Software License Agreement (BSD License) *
3 * Author: Sebastien Decugis <[email protected]> *
4 * *
5 * Copyright (c) 2013, WIDE Project and NICT *
6 * All rights reserved. *
7 * *
8 * Redistribution and use of this software in source and binary forms, with or without modification, are *
9 * permitted provided that the following conditions are met: *
10 * *
11 * * Redistributions of source code must retain the above *
12 * copyright notice, this list of conditions and the *
13 * following disclaimer. *
14 * *
15 * * Redistributions in binary form must reproduce the above *
16 * copyright notice, this list of conditions and the *
17 * following disclaimer in the documentation and/or other *
18 * materials provided with the distribution. *
19 * *
20 * * Neither the name of the WIDE Project or NICT nor the *
21 * names of its contributors may be used to endorse or *
22 * promote products derived from this software without *
23 * specific prior written permission of WIDE Project and *
24 * NICT. *
25 * *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34 *********************************************************************************************************/
35
36 /* Monitoring extension:
37 - periodically display queues and peers information
38 - upon SIGUSR2, display additional debug information
39 */
40
41 #include <freeDiameter/extension.h>
42 #include <signal.h>
43
44 #ifndef MONITOR_SIGNAL
45 #define MONITOR_SIGNAL SIGUSR2
46 #endif /* MONITOR_SIGNAL */
47
48 static int monitor_main(char * conffile);
49
50 EXTENSION_ENTRY("dbg_monitor", monitor_main);
51
52
53
54 /* Display information about a queue */
55 static void display_info(char * queue_desc, char * peer, int current_count, int limit_count, int highest_count, long long total_count,
56 struct timespec * total, struct timespec * blocking, struct timespec * last)
57 {
58 long long us = (total->tv_sec * 1000000) + (total->tv_nsec / 1000);
59 long double throughput = (long double)total_count * 1000000;
60 throughput /= us;
61 if (peer) {
62 TRACE_DEBUG(INFO, "'%s'@'%s': cur:%d/%d, h:%d, T:%lld in %ld.%06lds (%.2LFitems/s), blocked:%ld.%06lds, last processing:%ld.%06lds",
63 queue_desc, peer, current_count, limit_count, highest_count,
64 total_count, total->tv_sec, total->tv_nsec/1000, throughput,
65 blocking->tv_sec, blocking->tv_nsec/1000, last->tv_sec, last->tv_nsec/1000);
66 } else {
67 TRACE_DEBUG(INFO, "Global '%s': cur:%d/%d, h:%d, T:%lld in %ld.%06lds (%.2LFitems/s), blocked:%ld.%06lds, last processing:%ld.%06lds",
68 queue_desc, current_count, limit_count, highest_count,
69 total_count, total->tv_sec, total->tv_nsec/1000, throughput,
70 blocking->tv_sec, blocking->tv_nsec/1000, last->tv_sec, last->tv_nsec/1000);
71 }
72 }
73
74 /* Thread to display periodical debug information */
75 static pthread_t thr;
76 static void * mn_thr(void * arg)
77 {
78 int i = 0;
79 fd_log_threadname("Monitor thread");
80 char * buf = NULL;
81 size_t len;
82
83 /* Loop */
84 while (1) {
85 int current_count, limit_count, highest_count;
86 long long total_count;
87 struct timespec total, blocking, last;
88 struct fd_list * li;
89
90 #ifdef DEBUG
91 for (i++; i % 30; i++) {
92 fd_log_debug("[dbg_monitor] %ih%*im%*is", i/3600, 2, (i/60) % 60 , 2, i%60); /* This makes it easier to detect inactivity periods in the log file */
93 sleep(1);
94 }
95 #else /* DEBUG */
96 sleep(3599); /* 1 hour */
97 #endif /* DEBUG */
98 TRACE_DEBUG(INFO, "[dbg_monitor] Dumping queues statistics");
99
100 CHECK_FCT_DO( fd_stat_getstats(STAT_G_LOCAL, NULL, &current_count, &limit_count, &highest_count, &total_count, &total, &blocking, &last), );
101 display_info("Local delivery", NULL, current_count, limit_count, highest_count, total_count, &total, &blocking, &last);
102
103 CHECK_FCT_DO( fd_stat_getstats(STAT_G_INCOMING, NULL, &current_count, &limit_count, &highest_count, &total_count, &total, &blocking, &last), );
104 display_info("Total received", NULL, current_count, limit_count, highest_count, total_count, &total, &blocking, &last);
105
106 CHECK_FCT_DO( fd_stat_getstats(STAT_G_OUTGOING, NULL, &current_count, &limit_count, &highest_count, &total_count, &total, &blocking, &last), );
107 display_info("Total sending", NULL, current_count, limit_count, highest_count, total_count, &total, &blocking, &last);
108
109
110 CHECK_FCT_DO( pthread_rwlock_rdlock(&fd_g_peers_rw), /* continue */ );
111
112 for (li = fd_g_peers.next; li != &fd_g_peers; li = li->next) {
113 struct peer_hdr * p = (struct peer_hdr *)li->o;
114
115 TRACE_DEBUG(INFO, "%s", fd_peer_dump(&buf, &len, NULL, p, 1));
116
117 CHECK_FCT_DO( fd_stat_getstats(STAT_P_PSM, p, &current_count, &limit_count, &highest_count, &total_count, &total, &blocking, &last), );
118 display_info("Events, incl. recept", p->info.pi_diamid, current_count, limit_count, highest_count, total_count, &total, &blocking, &last);
119
120 CHECK_FCT_DO( fd_stat_getstats(STAT_P_TOSEND, p, &current_count, &limit_count, &highest_count, &total_count, &total, &blocking, &last), );
121 display_info("Outgoing", p->info.pi_diamid, current_count, limit_count, highest_count, total_count, &total, &blocking, &last);
122
123 }
124
125 CHECK_FCT_DO( pthread_rwlock_unlock(&fd_g_peers_rw), /* continue */ );
126
127 TRACE_DEBUG(INFO, "[dbg_monitor] Dumping servers information");
128 TRACE_DEBUG(INFO, "%s", fd_servers_dump(&buf, &len, NULL, 1));
129
130 sleep(1);
131 }
132
133 free(buf);
134 return NULL;
135 }
136
137 /* Function called on receipt of MONITOR_SIGNAL */
138 static void got_sig()
139 {
140 char * buf = NULL;
141 size_t len;
142 TRACE_DEBUG(INFO, "[dbg_monitor] Dumping config information");
143 TRACE_DEBUG(INFO, "%s", fd_conf_dump(&buf, &len, NULL));
144 TRACE_DEBUG(INFO, "[dbg_monitor] Dumping extensions information");
145 TRACE_DEBUG(INFO, "%s", fd_ext_dump(&buf, &len, NULL));
146 TRACE_DEBUG(INFO, "[dbg_monitor] Dumping dictionary information");
147 TRACE_DEBUG(INFO, "%s", fd_dict_dump(&buf, &len, NULL, fd_g_config->cnf_dict));
148 free(buf);
149 }
150
151 /* Entry point */
152 static int monitor_main(char * conffile)
153 {
154 TRACE_ENTRY("%p", conffile);
155
156 /* Catch signal SIGUSR1 */
157 CHECK_FCT( fd_event_trig_regcb(MONITOR_SIGNAL, "dbg_monitor", got_sig));
158
159 CHECK_POSIX( pthread_create( &thr, NULL, mn_thr, NULL ) );
160 return 0;
161 }
162
163 /* Cleanup */
164 void fd_ext_fini(void)
165 {
166 TRACE_ENTRY();
167 CHECK_FCT_DO( fd_thr_term(&thr), /* continue */ );
168 return ;
169 }
170