Update.
[glibc.git] / nscd / nscd_stat.c
blob2d46278fc884d15be5dd22aa1b78a5825ead4490
1 /* Copyright (c) 1998 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 <langinfo.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libintl.h>
29 #include "nscd.h"
30 #include "dbg_log.h"
32 /* We use this to make sure the receiver is the same. */
33 static const char compilation[21] = __DATE__ " " __TIME__;
35 /* Statistic data for one database. */
36 struct dbstat
38 int enabled;
39 int check_file;
40 size_t module;
42 unsigned long int postimeout;
43 unsigned long int negtimeout;
45 unsigned long int poshit;
46 unsigned long int neghit;
47 unsigned long int posmiss;
48 unsigned long int negmiss;
51 /* Record for transmitting statistics. */
52 struct statdata
54 char version[sizeof (compilation)];
55 int debug_level;
56 int ndbs;
57 struct dbstat dbs[lastdb];
61 void
62 send_stats (int fd, struct database dbs[lastdb])
64 struct statdata data;
65 int cnt;
67 memcpy (data.version, compilation, sizeof (compilation));
68 data.debug_level = debug_level;
69 data.ndbs = lastdb;
71 for (cnt = 0; cnt < lastdb; ++cnt)
73 data.dbs[cnt].enabled = dbs[cnt].enabled;
74 data.dbs[cnt].check_file = dbs[cnt].check_file;
75 data.dbs[cnt].module = dbs[cnt].module;
76 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
77 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
78 data.dbs[cnt].poshit = dbs[cnt].poshit;
79 data.dbs[cnt].neghit = dbs[cnt].neghit;
80 data.dbs[cnt].posmiss = dbs[cnt].posmiss;
81 data.dbs[cnt].negmiss = dbs[cnt].negmiss;
84 if (TEMP_FAILURE_RETRY (write (fd, &data, sizeof (data))) != sizeof (data))
86 char buf[256];
87 dbg_log (_("cannot write statistics: %s"),
88 strerror_r (errno, buf, sizeof (buf)));
93 int
94 receive_print_stats (void)
96 struct statdata data;
97 request_header req;
98 ssize_t nbytes;
99 int fd;
100 int i;
102 /* Open a socket to the running nscd. */
103 fd = nscd_open_socket ();
104 if (fd == -1)
105 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
107 /* Send the request. */
108 req.version = NSCD_VERSION;
109 req.type = GETSTAT;
110 req.key_len = 0;
111 nbytes = TEMP_FAILURE_RETRY (write (fd, &req, sizeof (request_header)));
112 if (nbytes != sizeof (request_header))
114 int err = errno;
115 close (fd);
116 error (EXIT_FAILURE, err, _("write incomplete"));
119 /* Read as much data as we expect. */
120 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
121 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
122 /* Yes, this is an assignment! */
123 && errno == EINVAL))
125 /* Not the right version. */
126 int err = errno;
127 close (fd);
128 error (EXIT_FAILURE, err, _("cannot read statistics data"));
131 printf (_("nscd configuration:\n\n%15d server debug level\n"),
132 data.debug_level);
134 for (i = 0; i < lastdb; ++i)
136 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
137 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
138 const char *enabled = nl_langinfo (data.dbs[i].enabled ? YESSTR : NOSTR);
139 const char *check_file = nl_langinfo (data.dbs[i].check_file
140 ? YESSTR : NOSTR);
142 if (enabled[0] == '\0')
143 /* The locale does not provide this information so we have to
144 translate it ourself. Since we should avoid short translation
145 terms we artifically increase the length. */
146 enabled = data.dbs[i].enabled ? _(" yes") : _(" no");
147 if (check_file[0] == '\0')
148 check_file = data.dbs[i].check_file ? _(" yes") : _(" no");
150 if (all == 0)
151 /* If nothing happened so far report a 0% hit rate. */
152 all = 1;
154 printf (_("\n%s cache:\n\n"
155 "%15s cache is enabled\n"
156 "%15Zd suggested size\n"
157 "%15ld seconds time to live for positive entries\n"
158 "%15ld seconds time to live for negative entries\n"
159 "%15ld cache hits on positive entries\n"
160 "%15ld cache hits on negative entries\n"
161 "%15ld cache misses on positive entries\n"
162 "%15ld cache misses on negative entries\n"
163 "%15ld%% cache hit rate\n"
164 "%15s check /etc/%s for changes\n"),
165 dbnames[i], enabled,
166 data.dbs[i].module,
167 data.dbs[i].postimeout, data.dbs[i].negtimeout,
168 data.dbs[i].poshit, data.dbs[i].neghit,
169 data.dbs[i].posmiss, data.dbs[i].negmiss,
170 (100 * hit) / all,
171 check_file, dbnames[i]);
174 close (fd);
176 exit (0);