Add -U and -P options that allow the specification of the per-user

and per-process summary file location.
These make the program more flexible, and also make it possible to write
sane regression tests.
This commit is contained in:
Diomidis Spinellis 2007-05-18 12:36:10 +00:00
parent 539e7a7770
commit 5f6f6b2933
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=169670
5 changed files with 32 additions and 7 deletions

View File

@ -82,6 +82,7 @@ extern int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
extern int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
extern u_quad_t cutoff;
extern cmpf_t sa_cmp;
extern const char *pdb_file, *usrdb_file;
/* some #defines to help with db's stupidity */

View File

@ -71,6 +71,8 @@ static void usage(void);
int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag;
int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag;
u_quad_t cutoff = 1;
const char *pdb_file = _PATH_SAVACCT;
const char *usrdb_file = _PATH_USRACCT;
static char *dfltargv[] = { NULL };
static int dfltargc = (sizeof dfltargv/sizeof(char *));
@ -86,7 +88,7 @@ main(int argc, char **argv)
dfltargv[0] = pathacct;
while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1)
while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
switch (ch) {
case 'a':
/* print all commands */
@ -145,6 +147,10 @@ main(int argc, char **argv)
/* sort by number of calls */
sa_cmp = cmp_calls;
break;
case 'P':
/* specify program database summary file */
pdb_file = optarg;
break;
case 'q':
/* quiet; error messages only */
qflag = 1;
@ -165,6 +171,10 @@ main(int argc, char **argv)
/* first, print uid and command name */
uflag = 1;
break;
case 'U':
/* specify user database summary file */
usrdb_file = optarg;
break;
case 'v':
/* cull junk */
vflag = 1;
@ -294,7 +304,7 @@ static void
usage()
{
(void)fprintf(stderr,
"usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n");
"usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
exit(1);
}

View File

@ -63,7 +63,7 @@ pacct_init()
DBT key, data;
int serr, nerr;
saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE,
saved_pacct_db = dbopen(pdb_file, O_RDONLY, 0, DB_BTREE,
NULL);
if (saved_pacct_db == NULL) {
error = errno == ENOENT ? 0 : -1;
@ -161,7 +161,7 @@ pacct_update()
DBT key, data;
int error, serr, nerr;
saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
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");

View File

@ -29,7 +29,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd February 25, 1994
.Dd May 18, 2007
.Dt SA 8
.Os
.Sh NAME
@ -38,6 +38,8 @@
.Sh SYNOPSIS
.Nm
.Op Fl abcdDfijkKlmnqrstu
.Op Fl P Ar file
.Op Fl U Ar file
.Op Fl v Ar cutoff
.Op Ar
.Sh DESCRIPTION
@ -143,6 +145,12 @@ Separate system and user time; normally they are combined.
Print per-user statistics rather than per-command statistics.
.It Fl n
Sort by number of calls.
.It Fl P Ar file
Use the specified
.Ar file
for accessing the per-command accounting summary database,
instead of the default
.Pa /var/account/savacct .
.It Fl q
Create no output other than error messages.
.It Fl r
@ -155,6 +163,12 @@ For each command, report the ratio of real time to the sum
of user and system cpu times.
If the cpu time is too small to report, ``*ignore*'' appears in
this field.
.It Fl U Ar file
Use the specified
.Ar file
for accessing the per-user accounting summary database,
instead of the default
.Pa /var/account/usracct .
.It Fl u
Superseding all other flags, for each entry
in the accounting file, print the user ID, total seconds of cpu usage,

View File

@ -68,7 +68,7 @@ usracct_init()
DBT key, data;
int serr, nerr;
saved_usracct_db = dbopen(_PATH_USRACCT, O_RDONLY, 0, DB_BTREE,
saved_usracct_db = dbopen(usrdb_file, O_RDONLY, 0, DB_BTREE,
&bti);
if (saved_usracct_db == NULL) {
error = (errno == ENOENT) ? 0 : -1;
@ -180,7 +180,7 @@ usracct_update()
bzero(&bti, sizeof bti);
bti.compare = uid_compare;
saved_usracct_db = dbopen(_PATH_USRACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
saved_usracct_db = dbopen(usrdb_file, O_RDWR|O_CREAT|O_TRUNC, 0644,
DB_BTREE, &bti);
if (saved_usracct_db == NULL) {
warn("creating user accounting summary");