Update.
[glibc.git] / nscd / nscd_stat.c
blobf9d21aeb01ba5b817947218cbad787c2dae990f3
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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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>
28 #include "nscd.h"
29 #include "dbg_log.h"
31 /* We use this to make sure the receiver is the same. */
32 static const char compilation[21] = __DATE__ " " __TIME__;
34 /* Statistic data for one database. */
35 struct dbstat
37 int enabled;
38 int check_file;
39 size_t module;
41 unsigned long int postimeout;
42 unsigned long int negtimeout;
44 unsigned long int poshit;
45 unsigned long int neghit;
46 unsigned long int posmiss;
47 unsigned long int negmiss;
50 /* Record for transmitting statistics. */
51 struct statdata
53 char version[sizeof (compilation)];
54 int debug_level;
55 int ndbs;
56 struct dbstat dbs[lastdb];
60 void
61 send_stats (int fd, struct database dbs[lastdb])
63 struct statdata data;
64 int cnt;
66 memcpy (data.version, compilation, sizeof (compilation));
67 data.debug_level = debug_level;
68 data.ndbs = lastdb;
70 for (cnt = 0; cnt < lastdb; ++cnt)
72 data.dbs[cnt].enabled = dbs[cnt].enabled;
73 data.dbs[cnt].check_file = dbs[cnt].check_file;
74 data.dbs[cnt].module = dbs[cnt].module;
75 data.dbs[cnt].postimeout = dbs[cnt].postimeout;
76 data.dbs[cnt].negtimeout = dbs[cnt].negtimeout;
77 data.dbs[cnt].poshit = dbs[cnt].poshit;
78 data.dbs[cnt].neghit = dbs[cnt].neghit;
79 data.dbs[cnt].posmiss = dbs[cnt].posmiss;
80 data.dbs[cnt].negmiss = dbs[cnt].negmiss;
83 if (TEMP_FAILURE_RETRY (write (fd, &data, sizeof (data))) != sizeof (data))
85 char buf[256];
86 dbg_log (_("cannot write statistics: %s"),
87 strerror_r (errno, buf, sizeof (buf)));
92 int
93 receive_print_stats (void)
95 struct statdata data;
96 request_header req;
97 ssize_t nbytes;
98 int fd;
99 int i;
101 /* Open a socket to the running nscd. */
102 fd = nscd_open_socket ();
103 if (fd == -1)
104 error (EXIT_FAILURE, 0, _("nscd not running!\n"));
106 /* Send the request. */
107 req.version = NSCD_VERSION;
108 req.type = GETSTAT;
109 req.key_len = 0;
110 nbytes = TEMP_FAILURE_RETRY (write (fd, &req, sizeof (request_header)));
111 if (nbytes != sizeof (request_header))
113 int err = errno;
114 close (fd);
115 error (EXIT_FAILURE, err, _("write incomplete"));
118 /* Read as much data as we expect. */
119 if (TEMP_FAILURE_RETRY (read (fd, &data, sizeof (data))) != sizeof (data)
120 || (memcmp (data.version, compilation, sizeof (compilation)) != 0
121 /* Yes, this is an assignment! */
122 && errno == EINVAL))
124 /* Not the right version. */
125 int err = errno;
126 close (fd);
127 error (EXIT_FAILURE, err, _("cannot read statistics data"));
130 printf (_("nscd configuration:\n\n%15d server debug level\n"),
131 data.debug_level);
133 for (i = 0; i < lastdb; ++i)
135 unsigned long int hit = data.dbs[i].poshit + data.dbs[i].neghit;
136 unsigned long int all = hit + data.dbs[i].posmiss + data.dbs[i].negmiss;
137 const char *enabled = nl_langinfo (data.dbs[i].enabled ? YESSTR : NOSTR);
138 const char *check_file = nl_langinfo (data.dbs[i].check_file
139 ? YESSTR : NOSTR);
141 if (enabled[0] == '\0')
142 /* The locale does not provide this information so we have to
143 translate it ourself. Since we should avoid short translation
144 terms we artifically increase the length. */
145 enabled = data.dbs[i].enabled ? _(" yes") : _(" no");
146 if (check_file[0] == '\0')
147 check_file = data.dbs[i].check_file ? _(" yes") : _(" no");
149 if (all == 0)
150 /* If nothing happened so far report a 0% hit rate. */
151 all = 1;
153 printf (_("\n%s cache:\n\n"
154 "%15s cache is enabled\n"
155 "%15Zd suggested size\n"
156 "%15ld seconds time to live for positive entries\n"
157 "%15ld seconds time to live for negative entries\n"
158 "%15ld cache hits on positive entries\n"
159 "%15ld cache hits on negative entries\n"
160 "%15ld cache misses on positive entries\n"
161 "%15ld cache misses on negative entries\n"
162 "%15ld%% cache hit rate\n"
163 "%15s check /etc/%s for changes\n"),
164 dbnames[i], enabled,
165 data.dbs[i].module,
166 data.dbs[i].postimeout, data.dbs[i].negtimeout,
167 data.dbs[i].poshit, data.dbs[i].neghit,
168 data.dbs[i].posmiss, data.dbs[i].negmiss,
169 (100 * hit) / all,
170 check_file, dbnames[i]);
173 close (fd);
175 exit (0);