diff options
Diffstat (limited to 'usr.sbin/sa/pdb.c')
-rw-r--r-- | usr.sbin/sa/pdb.c | 170 |
1 files changed, 62 insertions, 108 deletions
diff --git a/usr.sbin/sa/pdb.c b/usr.sbin/sa/pdb.c index 0c92d34..38beb55 100644 --- a/usr.sbin/sa/pdb.c +++ b/usr.sbin/sa/pdb.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$"); #include <err.h> #include <errno.h> #include <fcntl.h> +#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> @@ -48,68 +49,61 @@ static void print_ci(const struct cmdinfo *, const struct cmdinfo *); static DB *pacct_db; -int -pacct_init() +/* Legacy format in AHZV1 units. */ +struct cmdinfov1 { + char ci_comm[MAXCOMLEN+2]; /* command name (+ '*') */ + uid_t ci_uid; /* user id */ + u_quad_t ci_calls; /* number of calls */ + u_quad_t ci_etime; /* elapsed time */ + u_quad_t ci_utime; /* user time */ + u_quad_t ci_stime; /* system time */ + u_quad_t ci_mem; /* memory use */ + u_quad_t ci_io; /* number of disk i/o ops */ + u_int ci_flags; /* flags; see below */ +}; + +/* + * Convert a v1 data record into the current version. + * Return 0 if OK, -1 on error, setting errno. + */ +static int +v1_to_v2(DBT *key __unused, DBT *data) { - DB *saved_pacct_db; - int error; + struct cmdinfov1 civ1; + static struct cmdinfo civ2; - pacct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, NULL); - if (pacct_db == NULL) + if (data->size != sizeof(civ1)) { + errno = EFTYPE; return (-1); - - error = 0; - if (!iflag) { - DBT key, data; - int serr, nerr; - - saved_pacct_db = dbopen(pdb_file, O_RDONLY, 0, DB_BTREE, - NULL); - if (saved_pacct_db == NULL) { - error = errno == ENOENT ? 0 : -1; - if (error) - warn("retrieving process accounting summary"); - goto out; - } - - serr = DB_SEQ(saved_pacct_db, &key, &data, R_FIRST); - if (serr < 0) { - warn("retrieving process accounting summary"); - error = -1; - goto closeout; - } - while (serr == 0) { - nerr = DB_PUT(pacct_db, &key, &data, 0); - if (nerr < 0) { - warn("initializing process accounting stats"); - error = -1; - break; - } - - serr = DB_SEQ(saved_pacct_db, &key, &data, R_NEXT); - if (serr < 0) { - warn("retrieving process accounting summary"); - error = -1; - break; - } - } - -closeout: if (DB_CLOSE(saved_pacct_db) < 0) { - warn("closing process accounting summary"); - error = -1; - } } + memcpy(&civ1, data->data, data->size); + memset(&civ2, 0, sizeof(civ2)); + memcpy(civ2.ci_comm, civ1.ci_comm, sizeof(civ2.ci_comm)); + civ2.ci_uid = civ1.ci_uid; + civ2.ci_calls = civ1.ci_calls; + civ2.ci_etime = ((double)civ1.ci_etime / AHZV1) * 1000000; + civ2.ci_utime = ((double)civ1.ci_utime / AHZV1) * 1000000; + civ2.ci_stime = ((double)civ1.ci_stime / AHZV1) * 1000000; + civ2.ci_mem = civ1.ci_mem; + civ2.ci_io = civ1.ci_io; + civ2.ci_flags = civ1.ci_flags; + data->size = sizeof(civ2); + data->data = &civ2; + return (0); +} -out: if (error != 0) - pacct_destroy(); - return (error); +/* Copy pdb_file to in-memory pacct_db. */ +int +pacct_init() +{ + return (db_copy_in(&pacct_db, pdb_file, "process accounting", + NULL, v1_to_v2)); } void pacct_destroy() { - if (DB_CLOSE(pacct_db) < 0) - warn("destroying process accounting stats"); + db_destroy(pacct_db, "process accounting"); } int @@ -154,52 +148,12 @@ pacct_add(const struct cmdinfo *ci) return (0); } +/* Copy in-memory pacct_db to pdb_file. */ int pacct_update() { - DB *saved_pacct_db; - DBT key, data; - int error, serr, nerr; - - saved_pacct_db = dbopen(pdb_file, O_RDWR|O_CREAT|O_TRUNC, 0644, - DB_BTREE, NULL); - if (saved_pacct_db == NULL) { - warn("creating process accounting summary"); - return (-1); - } - - error = 0; - - serr = DB_SEQ(pacct_db, &key, &data, R_FIRST); - if (serr < 0) { - warn("retrieving process accounting stats"); - error = -1; - } - while (serr == 0) { - nerr = DB_PUT(saved_pacct_db, &key, &data, 0); - if (nerr < 0) { - warn("saving process accounting summary"); - error = -1; - break; - } - - serr = DB_SEQ(pacct_db, &key, &data, R_NEXT); - if (serr < 0) { - warn("retrieving process accounting stats"); - error = -1; - break; - } - } - - if (DB_SYNC(saved_pacct_db, 0) < 0) { - warn("syncing process accounting summary"); - error = -1; - } - if (DB_CLOSE(saved_pacct_db) < 0) { - warn("closing process accounting summary"); - error = -1; - } - return error; + return (db_copy_out(pacct_db, pdb_file, "process accounting", + NULL)); } void @@ -330,7 +284,7 @@ print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) int uflow; c = cip->ci_calls ? cip->ci_calls : 1; - t = (cip->ci_utime + cip->ci_stime) / (double) AHZ; + t = (cip->ci_utime + cip->ci_stime) / 1000000; if (t < 0.01) { t = 0.01; uflow = 1; @@ -347,26 +301,26 @@ print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) } if (jflag) - printf("%11.2fre ", cip->ci_etime / (double) (AHZ * c)); + printf("%11.3fre ", cip->ci_etime / (1000000 * c)); else - printf("%11.2fre ", cip->ci_etime / (60.0 * AHZ)); + printf("%11.3fre ", cip->ci_etime / (60.0 * 1000000)); if (cflag) { if (cip != totalcip) printf(" %4.1f%% ", cip->ci_etime / - (double)totalcip->ci_etime * 100); + totalcip->ci_etime * 100); else printf(" %4s ", ""); } if (!lflag) { if (jflag) - printf("%11.2fcp ", t / (double) cip->ci_calls); + printf("%11.3fcp ", t / (double) cip->ci_calls); else printf("%11.2fcp ", t / 60.0); if (cflag) { if (cip != totalcip) printf(" %4.1f%% ", - (double)(cip->ci_utime + cip->ci_stime) / + (cip->ci_utime + cip->ci_stime) / (totalcip->ci_utime + totalcip->ci_stime) * 100); else @@ -374,9 +328,9 @@ print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) } } else { if (jflag) - printf("%11.2fu ", cip->ci_utime / (double) (AHZ * c)); + printf("%11.3fu ", cip->ci_utime / (1000000 * c)); else - printf("%11.2fu ", cip->ci_utime / (60.0 * AHZ)); + printf("%11.2fu ", cip->ci_utime / (60.0 * 1000000)); if (cflag) { if (cip != totalcip) printf(" %4.1f%% ", cip->ci_utime / @@ -385,9 +339,9 @@ print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) printf(" %4s ", ""); } if (jflag) - printf("%11.2fs ", cip->ci_stime / (double) (AHZ * c)); + printf("%11.3fs ", cip->ci_stime / (1000000 * c)); else - printf("%11.2fs ", cip->ci_stime / (60.0 * AHZ)); + printf("%11.2fs ", cip->ci_stime / (60.0 * 1000000)); if (cflag) { if (cip != totalcip) printf(" %4.1f%% ", cip->ci_stime / @@ -401,18 +355,18 @@ print_ci(const struct cmdinfo *cip, const struct cmdinfo *totalcip) if (!uflow) printf("%8.2fre/cp ", cip->ci_etime / - (double) (cip->ci_utime + cip->ci_stime)); + (cip->ci_utime + cip->ci_stime)); else printf("*ignore* "); } if (Dflag) - printf("%10jutio ", (uintmax_t)cip->ci_io); + printf("%10.0fio ", cip->ci_io); else printf("%8.0favio ", cip->ci_io / c); if (Kflag) - printf("%10juk*sec ", (uintmax_t)cip->ci_mem); + printf("%10.0fk*sec ", cip->ci_mem); else printf("%8.0fk ", cip->ci_mem / t); |