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