[BZ #375]
[glibc.git] / nscd / nscd_stat.c
blob30f427bd20ed755588fb5b0f81717e1c21c906ea
1 /* Copyright (c) 1998, 2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <inttypes.h>
23 #include <langinfo.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <libintl.h>
30 #include "nscd.h"
31 #include "dbg_log.h"
33 /* We use this to make sure the receiver is the same. */
34 static const char compilation[21] = __DATE__ " " __TIME__;
36 /* Statistic data for one database. */
37 struct dbstat
39 int enabled;
40 int check_file;
41 size_t module;
43 unsigned long int postimeout;
44 unsigned long int negtimeout;
46 size_t nentries;
47 size_t maxnentries;
48 size_t maxnsearched;
49 size_t datasize;
50 size_t dataused;
52 uintmax_t poshit;
53 uintmax_t neghit;
54 uintmax_t posmiss;
55 uintmax_t negmiss;
57 uintmax_t rdlockdelayed;
58 uintmax_t wrlockdelayed;
60 uintmax_t addfailed;
63 /* Record for transmitting statistics. */
64 struct statdata
66 char version[sizeof (compilation)];
67 int debug_level;
68 time_t runtime;
69 unsigned long int client_queued;
70 int ndbs;
71 struct dbstat dbs[lastdb];
75 void
76 send_stats (int fd, struct database_dyn dbs[lastdb])
78 struct statdata data;
79 int cnt;
81 memcpy (data.version, compilation, sizeof (compilation));
82 data.debug_level = debug_level;
83 data.runtime = time (NULL) - start_time;
84 data.client_queued = client_queued;
85 data.ndbs = lastdb;
87 for (cnt = 0; cnt < lastdb; ++cnt)
89 data.dbs[cnt].enabled = dbs[cnt].enabled;
90 data.dbs[cnt].check_file = dbs[cnt].check_file;
91 data.dbs[cnt].module = dbs[cnt].head->module;
92 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
93 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
94 data.dbs[cnt].poshit = dbs[cnt].head->poshit;
95 data.dbs[cnt].neghit = dbs[cnt].head->neghit;
96 data.dbs[cnt].posmiss = dbs[cnt].head->posmiss;
97 data.dbs[cnt].negmiss = dbs[cnt].head->negmiss;
98 data.dbs[cnt].nentries = dbs[cnt].head->nentries;
99 data.dbs[cnt].maxnentries = dbs[cnt].head->maxnentries;
100 data.dbs[cnt].datasize = dbs[cnt].head->data_size;
101 data.dbs[cnt].dataused = dbs[cnt].head->first_free;
102 data.dbs[cnt].maxnsearched = dbs[cnt].head->maxnsearched;
103 data.dbs[cnt].rdlockdelayed = dbs[cnt].head->rdlockdelayed;
104 data.dbs[cnt].wrlockdelayed = dbs[cnt].head->wrlockdelayed;
105 data.dbs[cnt].addfailed = dbs[cnt].head->addfailed;
108 if (TEMP_FAILURE_RETRY (write (fd, &data, sizeof (data))) != sizeof (data))
110 char buf[256];
111 dbg_log (_("cannot write statistics: %s"),
112 strerror_r (errno, buf, sizeof (buf)));
118 receive_print_stats (void)
120 struct statdata data;
121 request_header req;
122 ssize_t nbytes;
123 int fd;
124 int i;
125 uid_t uid = getuid ();
127 /* Find out whether there is another user but root allowed to
128 request statistics. */
129 if (uid != 0)
131 /* User specified? */
132 if(stat_user == NULL || stat_uid != uid)
134 if (stat_user != NULL)
135 error (EXIT_FAILURE, 0,
136 _("Only root or %s is allowed to use this option!"),
137 stat_user);
138 else
139 error (EXIT_FAILURE, 0,
140 _("Only root is allowed to use this option!"));
144 /* Open a socket to the running nscd. */
145 fd = nscd_open_socket ();
146 if (fd == -1)
147 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
149 /* Send the request. */
150 req.version = NSCD_VERSION;
151 req.type = GETSTAT;
152 req.key_len = 0;
153 nbytes = TEMP_FAILURE_RETRY (write (fd, &req, sizeof (request_header)));
154 if (nbytes != sizeof (request_header))
156 int err = errno;
157 close (fd);
158 error (EXIT_FAILURE, err, _("write incomplete"));
161 /* Read as much data as we expect. */
162 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
163 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
164 /* Yes, this is an assignment! */
165 && (errno = EINVAL)))
167 /* Not the right version. */
168 int err = errno;
169 close (fd);
170 error (EXIT_FAILURE, err, _("cannot read statistics data"));
173 printf (_("nscd configuration:\n\n%15d server debug level\n"),
174 data.debug_level);
176 /* We know that we can simply subtract time_t values. */
177 unsigned long int diff = data.runtime;
178 unsigned int ndays = 0;
179 unsigned int nhours = 0;
180 unsigned int nmins = 0;
181 if (diff > 24 * 60 * 60)
183 ndays = diff / (24 * 60 * 60);
184 diff %= 24 * 60 * 60;
186 if (diff > 60 * 60)
188 nhours = diff / (60 * 60);
189 diff %= 60 * 60;
191 if (diff > 60)
193 nmins = diff / 60;
194 diff %= 60;
196 if (ndays != 0)
197 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
198 ndays, nhours, nmins, diff);
199 else if (nhours != 0)
200 printf (_(" %2uh %2um %2lus server runtime\n"), nhours, nmins, diff);
201 else if (nmins != 0)
202 printf (_(" %2um %2lus server runtime\n"), nmins, diff);
203 else
204 printf (_(" %2lus server runtime\n"), diff);
206 printf (_("%15lu number of times clients had to wait\n"),
207 data.client_queued);
209 for (i = 0; i < lastdb; ++i)
211 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
212 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
213 const char *enabled = nl_langinfo (data.dbs[i].enabled ? YESSTR : NOSTR);
214 const char *check_file = nl_langinfo (data.dbs[i].check_file
215 ? YESSTR : NOSTR);
217 if (enabled[0] == '\0')
218 /* The locale does not provide this information so we have to
219 translate it ourself. Since we should avoid short translation
220 terms we artifically increase the length. */
221 enabled = data.dbs[i].enabled ? _(" yes") : _(" no");
222 if (check_file[0] == '\0')
223 check_file = data.dbs[i].check_file ? _(" yes") : _(" no");
225 if (all == 0)
226 /* If nothing happened so far report a 0% hit rate. */
227 all = 1;
229 printf (_("\n%s cache:\n\n"
230 "%15s cache is enabled\n"
231 "%15zu suggested size\n"
232 "%15zu total data pool size\n"
233 "%15zu used data pool size\n"
234 "%15lu seconds time to live for positive entries\n"
235 "%15lu seconds time to live for negative entries\n"
236 "%15" PRIuMAX " cache hits on positive entries\n"
237 "%15" PRIuMAX " cache hits on negative entries\n"
238 "%15" PRIuMAX " cache misses on positive entries\n"
239 "%15" PRIuMAX " cache misses on negative entries\n"
240 "%15lu%% cache hit rate\n"
241 "%15zu current number of cached values\n"
242 "%15zu maximum number of cached values\n"
243 "%15zu maximum chain length searched\n"
244 "%15" PRIuMAX " number of delays on rdlock\n"
245 "%15" PRIuMAX " number of delays on wrlock\n"
246 "%15" PRIuMAX " memory allocations failed\n"
247 "%15s check /etc/%s for changes\n"),
248 dbnames[i], enabled,
249 data.dbs[i].module,
250 data.dbs[i].datasize, data.dbs[i].dataused,
251 data.dbs[i].postimeout, data.dbs[i].negtimeout,
252 data.dbs[i].poshit, data.dbs[i].neghit,
253 data.dbs[i].posmiss, data.dbs[i].negmiss,
254 (100 * hit) / all,
255 data.dbs[i].nentries, data.dbs[i].maxnentries,
256 data.dbs[i].maxnsearched,
257 data.dbs[i].rdlockdelayed,
258 data.dbs[i].wrlockdelayed,
259 data.dbs[i].addfailed, check_file, dbnames[i]);
262 close (fd);
264 exit (0);