2.3.90-13
[glibc/history.git] / nscd / nscd_stat.c
blobc1d0bffe20e2ee3b0f6b65bc126e5bfe93814a34
1 /* Copyright (c) 1998, 2003, 2004, 2005 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"
32 #include "selinux.h"
33 #ifdef HAVE_SELINUX
34 # include <selinux/selinux.h>
35 # include <selinux/avc.h>
36 #endif /* HAVE_SELINUX */
39 /* We use this to make sure the receiver is the same. */
40 static const char compilation[21] = __DATE__ " " __TIME__;
42 /* Statistic data for one database. */
43 struct dbstat
45 int enabled;
46 int check_file;
47 int shared;
48 int persistent;
49 size_t module;
51 unsigned long int postimeout;
52 unsigned long int negtimeout;
54 size_t nentries;
55 size_t maxnentries;
56 size_t maxnsearched;
57 size_t datasize;
58 size_t dataused;
60 uintmax_t poshit;
61 uintmax_t neghit;
62 uintmax_t posmiss;
63 uintmax_t negmiss;
65 uintmax_t rdlockdelayed;
66 uintmax_t wrlockdelayed;
68 uintmax_t addfailed;
71 /* Record for transmitting statistics. */
72 struct statdata
74 char version[sizeof (compilation)];
75 int debug_level;
76 time_t runtime;
77 unsigned long int client_queued;
78 int nthreads;
79 int max_nthreads;
80 int paranoia;
81 time_t restart_interval;
82 int ndbs;
83 struct dbstat dbs[lastdb];
84 #ifdef HAVE_SELINUX
85 struct avc_cache_stats cstats;
86 #endif /* HAVE_SELINUX */
90 void
91 send_stats (int fd, struct database_dyn dbs[lastdb])
93 struct statdata data;
94 int cnt;
96 memcpy (data.version, compilation, sizeof (compilation));
97 data.debug_level = debug_level;
98 data.runtime = time (NULL) - start_time;
99 data.client_queued = client_queued;
100 data.nthreads = nthreads;
101 data.max_nthreads = max_nthreads;
102 data.paranoia = paranoia;
103 data.restart_interval = restart_interval;
104 data.ndbs = lastdb;
106 for (cnt = 0; cnt < lastdb; ++cnt)
108 memset (&data.dbs[cnt], 0, sizeof (data.dbs[cnt]));
109 data.dbs[cnt].enabled = dbs[cnt].enabled;
110 data.dbs[cnt].check_file = dbs[cnt].check_file;
111 data.dbs[cnt].shared = dbs[cnt].shared;
112 data.dbs[cnt].persistent = dbs[cnt].persistent;
113 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
114 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
115 if (dbs[cnt].head != NULL)
117 data.dbs[cnt].module = dbs[cnt].head->module;
118 data.dbs[cnt].poshit = dbs[cnt].head->poshit;
119 data.dbs[cnt].neghit = dbs[cnt].head->neghit;
120 data.dbs[cnt].posmiss = dbs[cnt].head->posmiss;
121 data.dbs[cnt].negmiss = dbs[cnt].head->negmiss;
122 data.dbs[cnt].nentries = dbs[cnt].head->nentries;
123 data.dbs[cnt].maxnentries = dbs[cnt].head->maxnentries;
124 data.dbs[cnt].datasize = dbs[cnt].head->data_size;
125 data.dbs[cnt].dataused = dbs[cnt].head->first_free;
126 data.dbs[cnt].maxnsearched = dbs[cnt].head->maxnsearched;
127 data.dbs[cnt].rdlockdelayed = dbs[cnt].head->rdlockdelayed;
128 data.dbs[cnt].wrlockdelayed = dbs[cnt].head->wrlockdelayed;
129 data.dbs[cnt].addfailed = dbs[cnt].head->addfailed;
133 if (selinux_enabled)
134 nscd_avc_cache_stats (&data.cstats);
136 if (TEMP_FAILURE_RETRY (send (fd, &data, sizeof (data), MSG_NOSIGNAL))
137 != sizeof (data))
139 char buf[256];
140 dbg_log (_("cannot write statistics: %s"),
141 strerror_r (errno, buf, sizeof (buf)));
147 receive_print_stats (void)
149 struct statdata data;
150 request_header req;
151 ssize_t nbytes;
152 int fd;
153 int i;
154 uid_t uid = getuid ();
155 const char *yesstr = _("yes");
156 const char *nostr = _("no");
158 /* Find out whether there is another user but root allowed to
159 request statistics. */
160 if (uid != 0)
162 /* User specified? */
163 if(stat_user == NULL || stat_uid != uid)
165 if (stat_user != NULL)
166 error (EXIT_FAILURE, 0,
167 _("Only root or %s is allowed to use this option!"),
168 stat_user);
169 else
170 error (EXIT_FAILURE, 0,
171 _("Only root is allowed to use this option!"));
175 /* Open a socket to the running nscd. */
176 fd = nscd_open_socket ();
177 if (fd == -1)
178 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
180 /* Send the request. */
181 req.version = NSCD_VERSION;
182 req.type = GETSTAT;
183 req.key_len = 0;
184 nbytes = TEMP_FAILURE_RETRY (send (fd, &req, sizeof (request_header),
185 MSG_NOSIGNAL));
186 if (nbytes != sizeof (request_header))
188 int err = errno;
189 close (fd);
190 error (EXIT_FAILURE, err, _("write incomplete"));
193 /* Read as much data as we expect. */
194 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
195 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
196 /* Yes, this is an assignment! */
197 && (errno = EINVAL)))
199 /* Not the right version. */
200 int err = errno;
201 close (fd);
202 error (EXIT_FAILURE, err, _("cannot read statistics data"));
205 printf (_("nscd configuration:\n\n%15d server debug level\n"),
206 data.debug_level);
208 /* We know that we can simply subtract time_t values. */
209 unsigned long int diff = data.runtime;
210 unsigned int ndays = 0;
211 unsigned int nhours = 0;
212 unsigned int nmins = 0;
213 if (diff > 24 * 60 * 60)
215 ndays = diff / (24 * 60 * 60);
216 diff %= 24 * 60 * 60;
218 if (diff > 60 * 60)
220 nhours = diff / (60 * 60);
221 diff %= 60 * 60;
223 if (diff > 60)
225 nmins = diff / 60;
226 diff %= 60;
228 if (ndays != 0)
229 printf (_("%3ud %2uh %2um %2lus server runtime\n"),
230 ndays, nhours, nmins, diff);
231 else if (nhours != 0)
232 printf (_(" %2uh %2um %2lus server runtime\n"), nhours, nmins, diff);
233 else if (nmins != 0)
234 printf (_(" %2um %2lus server runtime\n"), nmins, diff);
235 else
236 printf (_(" %2lus server runtime\n"), diff);
238 printf (_("%15d current number of threads\n"
239 "%15d maximum number of threads\n"
240 "%15lu number of times clients had to wait\n"
241 "%15s paranoia mode enabled\n"
242 "%15lu restart internal\n"),
243 data.nthreads, data.max_nthreads, data.client_queued,
244 data.paranoia ? yesstr : nostr,
245 (unsigned long int) data.restart_interval);
247 for (i = 0; i < lastdb; ++i)
249 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
250 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
251 const char *enabled = data.dbs[i].enabled ? yesstr : nostr;
252 const char *check_file = data.dbs[i].check_file ? yesstr : nostr;
253 const char *shared = data.dbs[i].shared ? yesstr : nostr;
254 const char *persistent = data.dbs[i].persistent ? yesstr : nostr;
256 if (enabled[0] == '\0')
257 /* The locale does not provide this information so we have to
258 translate it ourself. Since we should avoid short translation
259 terms we artifically increase the length. */
260 enabled = data.dbs[i].enabled ? yesstr : nostr;
261 if (check_file[0] == '\0')
262 check_file = data.dbs[i].check_file ? yesstr : nostr;
263 if (shared[0] == '\0')
264 shared = data.dbs[i].shared ? yesstr : nostr;
265 if (persistent[0] == '\0')
266 persistent = data.dbs[i].persistent ? yesstr : nostr;
268 if (all == 0)
269 /* If nothing happened so far report a 0% hit rate. */
270 all = 1;
272 printf (_("\n%s cache:\n\n"
273 "%15s cache is enabled\n"
274 "%15s cache is persistent\n"
275 "%15s cache is shared\n"
276 "%15zu suggested size\n"
277 "%15zu total data pool size\n"
278 "%15zu used data pool size\n"
279 "%15lu seconds time to live for positive entries\n"
280 "%15lu seconds time to live for negative entries\n"
281 "%15" PRIuMAX " cache hits on positive entries\n"
282 "%15" PRIuMAX " cache hits on negative entries\n"
283 "%15" PRIuMAX " cache misses on positive entries\n"
284 "%15" PRIuMAX " cache misses on negative entries\n"
285 "%15lu%% cache hit rate\n"
286 "%15zu current number of cached values\n"
287 "%15zu maximum number of cached values\n"
288 "%15zu maximum chain length searched\n"
289 "%15" PRIuMAX " number of delays on rdlock\n"
290 "%15" PRIuMAX " number of delays on wrlock\n"
291 "%15" PRIuMAX " memory allocations failed\n"
292 "%15s check /etc/%s for changes\n"),
293 dbnames[i], enabled, persistent, shared,
294 data.dbs[i].module,
295 data.dbs[i].datasize, data.dbs[i].dataused,
296 data.dbs[i].postimeout, data.dbs[i].negtimeout,
297 data.dbs[i].poshit, data.dbs[i].neghit,
298 data.dbs[i].posmiss, data.dbs[i].negmiss,
299 (100 * hit) / all,
300 data.dbs[i].nentries, data.dbs[i].maxnentries,
301 data.dbs[i].maxnsearched,
302 data.dbs[i].rdlockdelayed,
303 data.dbs[i].wrlockdelayed,
304 data.dbs[i].addfailed, check_file, dbnames[i]);
307 if (selinux_enabled)
308 nscd_avc_print_stats (&data.cstats);
310 close (fd);
312 exit (0);