xref: /original-bsd/usr.sbin/sendmail/src/stats.c (revision f0fd5f8a)
1 # include "sendmail.h"
2 
3 SCCSID(@(#)stats.c	3.5		11/28/82);
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 struct statistics	Stat;
54 
55 poststats(sfile)
56 	char *sfile;
57 {
58 	register int fd;
59 	struct statistics stat;
60 	extern long lseek();
61 
62 	(void) time(&Stat.stat_itime);
63 	Stat.stat_size = sizeof Stat;
64 
65 	fd = open(sfile, 2);
66 	if (fd < 0)
67 		return;
68 	if (read(fd, (char *) &stat, sizeof stat) == sizeof stat &&
69 	    stat.stat_size == sizeof stat)
70 	{
71 		/* merge current statistics into statfile */
72 		register int i;
73 
74 		for (i = 0; i < MAXMAILERS; i++)
75 		{
76 			stat.stat_nf[i] += Stat.stat_nf[i];
77 			stat.stat_bf[i] += Stat.stat_bf[i];
78 			stat.stat_nt[i] += Stat.stat_nt[i];
79 			stat.stat_bt[i] += Stat.stat_bt[i];
80 		}
81 	}
82 	else
83 		bmove((char *) &Stat, (char *) &stat, sizeof stat);
84 
85 	/* write out results */
86 	(void) lseek(fd, 0L, 0);
87 	(void) write(fd, (char *) &stat, sizeof stat);
88 	(void) close(fd);
89 }
90 /*
91 **  KBYTES -- given a number, returns the number of Kbytes.
92 **
93 **	Used in statistics gathering of message sizes to try to avoid
94 **	wraparound (at least for a while.....)
95 **
96 **	Parameters:
97 **		bytes -- actual number of bytes.
98 **
99 **	Returns:
100 **		number of kbytes.
101 **
102 **	Side Effects:
103 **		none.
104 **
105 **	Notes:
106 **		This function is actually a ceiling function to
107 **			the nearest K.
108 **		Honestly folks, floating point might be better.
109 **			Or perhaps a "statistical" log method.
110 */
111 
112 long
113 kbytes(bytes)
114 	long bytes;
115 {
116 	return ((bytes + 999) / 1000);
117 }
118