2 * Copyright (c) 1994 Christopher G. Demetriou
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christopher G. Demetriou.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 static const char rcsid[] =
36 #include <sys/param.h>
37 #include <sys/types.h>
47 #include "pathnames.h"
49 static int uid_compare __P((const DBT *, const DBT *));
51 static DB *usracct_db;
60 bzero(&bti, sizeof bti);
61 bti.compare = uid_compare;
63 usracct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
64 if (usracct_db == NULL)
72 saved_usracct_db = dbopen(_PATH_USRACCT, O_RDONLY, 0, DB_BTREE,
74 if (saved_usracct_db == NULL) {
75 error = (errno == ENOENT) ? 0 : -1;
77 warn("retrieving user accounting summary");
81 serr = DB_SEQ(saved_usracct_db, &key, &data, R_FIRST);
83 warn("retrieving user accounting summary");
88 nerr = DB_PUT(usracct_db, &key, &data, 0);
90 warn("initializing user accounting stats");
95 serr = DB_SEQ(saved_usracct_db, &key, &data, R_NEXT);
97 warn("retrieving user accounting summary");
104 if (DB_CLOSE(saved_usracct_db) < 0) {
105 warn("closing user accounting summary");
119 if (DB_CLOSE(usracct_db) < 0)
120 warn("destroying user accounting stats");
125 const struct cmdinfo *ci;
128 struct userinfo newui;
134 key.size = sizeof uid;
136 rv = DB_GET(usracct_db, &key, &data, 0);
138 warn("get key %lu from user accounting stats", uid);
140 } else if (rv == 0) { /* it's there; copy whole thing */
141 /* add the old data to the new data */
142 bcopy(data.data, &newui, data.size);
143 if (newui.ui_uid != uid) {
144 warnx("key %lu != expected record number %lu",
146 warnx("inconsistent user accounting stats");
149 } else { /* it's not there; zero it and copy the key */
150 bzero(&newui, sizeof newui);
151 newui.ui_uid = ci->ci_uid;
154 newui.ui_calls += ci->ci_calls;
155 newui.ui_utime += ci->ci_utime;
156 newui.ui_stime += ci->ci_stime;
157 newui.ui_mem += ci->ci_mem;
158 newui.ui_io += ci->ci_io;
161 data.size = sizeof newui;
162 rv = DB_PUT(usracct_db, &key, &data, 0);
164 warn("add key %lu to user accounting stats", uid);
166 } else if (rv != 0) {
167 warnx("DB_PUT returned 1");
177 DB *saved_usracct_db;
180 int error, serr, nerr;
182 bzero(&bti, sizeof bti);
183 bti.compare = uid_compare;
185 saved_usracct_db = dbopen(_PATH_USRACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
187 if (saved_usracct_db == NULL) {
188 warn("creating user accounting summary");
194 serr = DB_SEQ(usracct_db, &key, &data, R_FIRST);
196 warn("retrieving user accounting stats");
200 nerr = DB_PUT(saved_usracct_db, &key, &data, 0);
202 warn("saving user accounting summary");
207 serr = DB_SEQ(usracct_db, &key, &data, R_NEXT);
209 warn("retrieving user accounting stats");
215 if (DB_SYNC(saved_usracct_db, 0) < 0) {
216 warn("syncing process accounting summary");
219 if (DB_CLOSE(saved_usracct_db) < 0) {
220 warn("closing process accounting summary");
230 struct userinfo uistore, *ui = &uistore;
234 rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
236 warn("retrieving user accounting stats");
239 memcpy(ui, data.data, sizeof(struct userinfo));
241 printf("%-*s %9qu ", MAXLOGNAME - 1,
242 user_from_uid(ui->ui_uid, 0), ui->ui_calls);
244 t = (double) (ui->ui_utime + ui->ui_stime) /
246 if (t < 0.0001) /* kill divide by zero */
249 printf("%12.2f%s ", t / 60.0, "cpu");
251 /* ui->ui_calls is always != 0 */
253 printf("%12qu%s", ui->ui_io / ui->ui_calls, "avio");
255 printf("%12qu%s", ui->ui_io, "tio");
257 /* t is always >= 0.0001; see above */
259 printf("%12.0f%s", ui->ui_mem / t, "k");
261 printf("%12qu%s", ui->ui_mem, "k*sec");
265 rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
267 warn("retrieving user accounting stats");
277 bcopy(k1->data, &d1, sizeof d1);
278 bcopy(k2->data, &d2, sizeof d2);