ze-filter  (ze-filter-0.8.0-develop-180218)
ze-callback.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : Sat Dec 19 23:42:11 CET 2009
13  *
14  * This program is free software, but with restricted license :
15  *
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * More details about ze-filter license can be found at ze-filter
22  * web site : http://foss.jose-marcio.org
23  */
24 
25 
26 #include <ze-sys.h>
27 #include <ze-filter.h>
28 #include <ze-callback.h>
29 
30 /* ****************************************************************************
31  * *
32  * *
33  ******************************************************************************/
34 
35 static kstats_T callback_st[CALLBACK_LAST + 1];
36 static kstats_T callback_gst;
37 
38 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
39 
40 static bool st_ok = FALSE;
41 
42 
43 /* ****************************************************************************
44  * *
45  * *
46  ******************************************************************************/
47 static void
48 stats_init()
49 {
50  int i;
51 
52  MUTEX_LOCK(&mutex);
53 
54  for (i = CALLBACK_FIRST; i <= CALLBACK_LAST; i++)
55  zeKStatsReset(&callback_st[i]);
56  zeKStatsReset(&callback_gst);
57  st_ok = TRUE;
58 
59  MUTEX_UNLOCK(&mutex);
60 }
61 
62 
63 /* ****************************************************************************
64  * *
65  * *
66  ******************************************************************************/
67 bool
68 callback_stats_update(callback, dt)
69  int callback;
70  timems_T dt;
71 {
72  if (callback < CALLBACK_FIRST || callback > CALLBACK_LAST)
73  return FALSE;
74 
75  if (!st_ok)
76  stats_init();
77 
78  MUTEX_LOCK(&mutex);
79  zeKStatsUpdate(&(callback_st[callback]), (double) dt);
80  zeKStatsUpdate(&callback_gst, (double) dt);
82 
83  return TRUE;
84 }
85 
86 /* ****************************************************************************
87  * *
88  * *
89  ******************************************************************************/
90 bool
92  int fd;
93  bool line;
94 {
95  int i;
96 
97  MUTEX_LOCK(&mutex);
98 
99  if (line) {
100  char buf[2048];
101  time_t now = time(NULL);
102  char str[256];
103 
104  memset(buf, 0, sizeof (buf));
105  memset(str, 0, sizeof (str));
106 
107  snprintf(str, sizeof (str), "DATA TIMESTAMP=(%12lld) ", (long long) now);
108  strlcat(buf, str, sizeof (buf));
109  for (i = CALLBACK_FIRST; i <= CALLBACK_LAST; i++) {
110  snprintf(str, sizeof (str), "%s=(%.1f) ", CALLBACK_LABEL(i),
111  zeKMean(&callback_st[i]));
112  strlcat(buf, str, sizeof (buf));
113  }
114  FD_PRINTF(fd, "%s\n", buf);
115  } else {
116  if (fd >= 0) {
117  FD_PRINTF(fd, " Callback Handling Times Statistics\n");
118  FD_PRINTF(fd, "* %-15s : %8s %10s %10s %10s %10s\n",
119  "Callback", "Count", "Minimum", "Maximum", "Mean", "Std. Dev.");
120  FD_PRINTF(fd, "* %-15s : %8s %10s %10s %10s %10s\n",
121  "********", "********", "*********", "*********",
122  "*********", "*********");
123  } else {
124  ZE_MessageInfo(9, " Callback Handling Times Statistics");
125  ZE_MessageInfo(9, "* %-15s : %8s %10s %10s %10s %10s",
126  "Callback", "Count", "Minimum", "Maximum", "Mean",
127  "Std. Dev.");
128  ZE_MessageInfo(9, "* %-15s : %8s %10s %10s %10s %10s", "********",
129  "********", "*********", "*********", "*********",
130  "*********");
131  }
132  for (i = CALLBACK_FIRST; i <= CALLBACK_LAST; i++) {
133  if (fd >= 0)
134  FD_PRINTF(fd, "* %-15s : %8ld %10.1f %10.1f %10.1f %10.1f ms\n",
135  CALLBACK_LABEL(i),
136  (long int ) zeKCount(&callback_st[i]),
137  zeKMin(&callback_st[i]),
138  zeKMax(&callback_st[i]),
139  zeKMean(&callback_st[i]), zeKStdDev(&callback_st[i]));
140  else
141  ZE_MessageInfo(9, "* %-15s : %8ld %10.1f %10.1f %10.1f %10.1f ms",
142  CALLBACK_LABEL(i),
143  (long int ) zeKCount(&callback_st[i]),
144  zeKMin(&callback_st[i]),
145  zeKMax(&callback_st[i]),
146  zeKMean(&callback_st[i]), zeKStdDev(&callback_st[i]));
147  }
148  if (fd >= 0) {
149  FD_PRINTF(fd, "* %-15s : %8s %10s %10s %10s %10s\n",
150  "********", "********", "*********", "*********",
151  "*********", "*********");
152  FD_PRINTF(fd, "* %-15s : %8ld %10.1f %10.1f %10.1f %10.1f ms\n",
153  "Global",
154  (long int ) zeKCount(&callback_gst),
155  zeKMin(&callback_gst),
156  zeKMax(&callback_gst),
157  zeKMean(&callback_gst), zeKStdDev(&callback_gst));
158  } else {
159  ZE_MessageInfo(9, "* %-15s : %8s %10s %10s %10s %10s",
160  "********", "********", "*********", "*********",
161  "*********", "*********");
162  ZE_MessageInfo(9, "* %-15s : %8ld %10.1f %10.1f %10.1f %10.1f ms",
163  "Global",
164  (long int ) zeKCount(&callback_gst),
165  zeKMin(&callback_gst),
166  zeKMax(&callback_gst),
167  zeKMean(&callback_gst), zeKStdDev(&callback_gst));
168  }
169  }
170 
172 
173  return TRUE;
174 }
#define CALLBACK_LAST
Definition: ze-callback.h:40
#define MUTEX_UNLOCK(mutex)
Definition: macros.h:101
long zeKCount(kstats_T *s)
Definition: zeKStats.c:82
#define MUTEX_LOCK(mutex)
Definition: macros.h:93
void zeKStatsReset(kstats_T *)
Definition: zeKStats.c:92
pthread_mutex_t mutex
Definition: ze-connopen.c:63
double zeKMin(kstats_T *s)
Definition: zeKStats.c:62
bool callback_stats_dump(int fd, bool line)
Definition: ze-callback.c:91
#define FALSE
Definition: macros.h:160
#define FD_PRINTF(fdp,...)
Definition: macros.h:45
#define strlcat
Definition: zeString.h:28
void zeKStatsUpdate(kstats_T *, double)
Definition: zeKStats.c:101
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
#define CALLBACK_FIRST
Definition: ze-callback.h:27
bool callback_stats_update(int callback, timems_T dt)
Definition: ze-callback.c:68
#define CALLBACK_LABEL(i)
Definition: ze-callback.h:42
double zeKMean(kstats_T *s)
Definition: zeKStats.c:43
double zeKMax(kstats_T *s)
Definition: zeKStats.c:72
double zeKStdDev(kstats_T *s)
Definition: zeKStats.c:53
uint64_t timems_T
Definition: zeTime.h:32