xref: /csrg-svn/usr.sbin/sendmail/src/stats.c (revision 22714)
1 /*
2 **  Sendmail
3 **  Copyright (c) 1983  Eric P. Allman
4 **  Berkeley, California
5 **
6 **  Copyright (c) 1983 Regents of the University of California.
7 **  All rights reserved.  The Berkeley software License Agreement
8 **  specifies the terms and conditions for redistribution.
9 */
10 
11 #ifndef lint
12 static char	SccsId[] = "@(#)stats.c	5.1 (Berkeley) 06/07/85";
13 #endif not lint
14 
15 # include "sendmail.h"
16 
17 SCCSID(@(#)stats.c	5.1		06/07/85);
18 
19 /*
20 **  Statistics structure.
21 */
22 
23 struct statistics
24 {
25 	time_t	stat_itime;		/* file initialization time */
26 	short	stat_size;		/* size of this structure */
27 	long	stat_nf[MAXMAILERS];	/* # msgs from each mailer */
28 	long	stat_bf[MAXMAILERS];	/* kbytes from each mailer */
29 	long	stat_nt[MAXMAILERS];	/* # msgs to each mailer */
30 	long	stat_bt[MAXMAILERS];	/* kbytes to each mailer */
31 };
32 
33 struct statistics	Stat;
34 extern long		kbytes();	/* for _bf, _bt */
35 /*
36 **  MARKSTATS -- mark statistics
37 */
38 
39 markstats(e, to)
40 	register ENVELOPE *e;
41 	register ADDRESS *to;
42 {
43 	if (to == NULL)
44 	{
45 		Stat.stat_nf[e->e_from.q_mailer->m_mno]++;
46 		Stat.stat_bf[e->e_from.q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
47 	}
48 	else
49 	{
50 		Stat.stat_nt[to->q_mailer->m_mno]++;
51 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
52 	}
53 }
54 /*
55 **  POSTSTATS -- post statistics in the statistics file
56 **
57 **	Parameters:
58 **		sfile -- the name of the statistics file.
59 **
60 **	Returns:
61 **		none.
62 **
63 **	Side Effects:
64 **		merges the Stat structure with the sfile file.
65 */
66 
67 poststats(sfile)
68 	char *sfile;
69 {
70 	register int fd;
71 	struct statistics stat;
72 	extern long lseek();
73 
74 	(void) time(&Stat.stat_itime);
75 	Stat.stat_size = sizeof Stat;
76 
77 	fd = open(sfile, 2);
78 	if (fd < 0)
79 	{
80 		errno = 0;
81 		return;
82 	}
83 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
84 	    stat.stat_size == sizeof stat)
85 	{
86 		/* merge current statistics into statfile */
87 		register int i;
88 
89 		for (i = 0; i < MAXMAILERS; i++)
90 		{
91 			stat.stat_nf[i] += Stat.stat_nf[i];
92 			stat.stat_bf[i] += Stat.stat_bf[i];
93 			stat.stat_nt[i] += Stat.stat_nt[i];
94 			stat.stat_bt[i] += Stat.stat_bt[i];
95 		}
96 	}
97 	else
98 		bcopy((char *) &Stat, (char *) &stat, sizeof stat);
99 
100 	/* write out results */
101 	(void) lseek(fd, 0L, 0);
102 	(void) write(fd, (char *) &stat, sizeof stat);
103 	(void) close(fd);
104 }
105 /*
106 **  KBYTES -- given a number, returns the number of Kbytes.
107 **
108 **	Used in statistics gathering of message sizes to try to avoid
109 **	wraparound (at least for a while.....)
110 **
111 **	Parameters:
112 **		bytes -- actual number of bytes.
113 **
114 **	Returns:
115 **		number of kbytes.
116 **
117 **	Side Effects:
118 **		none.
119 **
120 **	Notes:
121 **		This function is actually a ceiling function to
122 **			the nearest K.
123 **		Honestly folks, floating point might be better.
124 **			Or perhaps a "statistical" log method.
125 */
126 
127 long
128 kbytes(bytes)
129 	long bytes;
130 {
131 	return ((bytes + 999) / 1000);
132 }
133