2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/types.h>
48 #include "pathnames.h"
50 static int uid_compare(const DBT *, const DBT *);
52 static DB *usracct_db;
54 /* Legacy format in AHZV1 units. */
56 uid_t ui_uid; /* user id; for consistency */
57 u_quad_t ui_calls; /* number of invocations */
58 u_quad_t ui_utime; /* user time */
59 u_quad_t ui_stime; /* system time */
60 u_quad_t ui_mem; /* memory use */
61 u_quad_t ui_io; /* number of disk i/o ops */
65 * Convert a v1 data record into the current version.
66 * Return 0 if OK, -1 on error, setting errno.
69 v1_to_v2(DBT *key, DBT *data)
71 struct userinfov1 uiv1;
72 static struct userinfo uiv2;
75 if (key->size != sizeof(u_long) || data->size != sizeof(uiv1)) {
81 key->size = sizeof(uid_t);
82 uid = (uid_t)*(u_long *)(key->data);
86 memcpy(&uiv1, data->data, data->size);
87 memset(&uiv2, 0, sizeof(uiv2));
88 uiv2.ui_uid = uiv1.ui_uid;
89 uiv2.ui_calls = uiv1.ui_calls;
90 uiv2.ui_utime = ((double)uiv1.ui_utime / AHZV1) * 1000000;
91 uiv2.ui_stime = ((double)uiv1.ui_stime / AHZV1) * 1000000;
92 uiv2.ui_mem = uiv1.ui_mem;
93 uiv2.ui_io = uiv1.ui_io;
94 data->size = sizeof(uiv2);
100 /* Copy usrdb_file to in-memory usracct_db. */
106 bzero(&bti, sizeof bti);
107 bti.compare = uid_compare;
109 return (db_copy_in(&usracct_db, usrdb_file, "user accounting",
114 usracct_destroy(void)
116 db_destroy(usracct_db, "user accounting");
120 usracct_add(const struct cmdinfo *ci)
123 struct userinfo newui;
129 key.size = sizeof uid;
131 rv = DB_GET(usracct_db, &key, &data, 0);
133 warn("get key %u from user accounting stats", uid);
135 } else if (rv == 0) { /* it's there; copy whole thing */
136 /* add the old data to the new data */
137 bcopy(data.data, &newui, data.size);
138 if (newui.ui_uid != uid) {
139 warnx("key %u != expected record number %u",
141 warnx("inconsistent user accounting stats");
144 } else { /* it's not there; zero it and copy the key */
145 bzero(&newui, sizeof newui);
146 newui.ui_uid = ci->ci_uid;
149 newui.ui_calls += ci->ci_calls;
150 newui.ui_utime += ci->ci_utime;
151 newui.ui_stime += ci->ci_stime;
152 newui.ui_mem += ci->ci_mem;
153 newui.ui_io += ci->ci_io;
156 data.size = sizeof newui;
157 rv = DB_PUT(usracct_db, &key, &data, 0);
159 warn("add key %u to user accounting stats", uid);
161 } else if (rv != 0) {
162 warnx("DB_PUT returned 1");
169 /* Copy in-memory usracct_db to usrdb_file. */
175 bzero(&bti, sizeof bti);
176 bti.compare = uid_compare;
178 return (db_copy_out(usracct_db, usrdb_file, "user accounting",
186 struct userinfo uistore, *ui = &uistore;
190 rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
192 warn("retrieving user accounting stats");
195 memcpy(ui, data.data, sizeof(struct userinfo));
197 printf("%-*s %9ju ", MAXLOGNAME - 1,
198 user_from_uid(ui->ui_uid, 0), (uintmax_t)ui->ui_calls);
200 t = (ui->ui_utime + ui->ui_stime) / 1000000;
201 if (t < 0.000001) /* kill divide by zero */
204 printf("%12.2f%s ", t / 60.0, "cpu");
206 /* ui->ui_calls is always != 0 */
209 ui->ui_io / ui->ui_calls, "avio");
211 printf("%12.0f%s", ui->ui_io, "tio");
213 /* t is always >= 0.000001; see above. */
215 printf("%12.0f%s", ui->ui_mem / t, "k");
217 printf("%12.0f%s", ui->ui_mem, "k*sec");
221 rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
223 warn("retrieving user accounting stats");
228 uid_compare(const DBT *k1, const DBT *k2)
232 bcopy(k1->data, &d1, sizeof d1);
233 bcopy(k2->data, &d2, sizeof d2);