ec933b917492373dc668589259c6a9d4d89abd69
[openafs.git] / src / viced / fsstats.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* An abstracted interface for recording fs statistics data */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15 #include <roken.h>
16
17 #include <afs/afsint.h>
18 #include <afs/ihandle.h>
19 #include <afs/nfs.h>
20 #include "viced.h"
21 #include "fs_stats.h"
22
23 #if FS_STATS_DETAILED
24
25 void
26 fsstats_StartOp(struct fsstats *stats, int index)
27 {
28     stats->opP = &(afs_FullPerfStats.det.rpcOpTimes[index]);
29     FS_LOCK;
30     (stats->opP->numOps)++;
31     FS_UNLOCK;
32     FT_GetTimeOfDay(&stats->opStartTime, NULL);
33 }
34
35 void
36 fsstats_FinishOp(struct fsstats *stats, int code)
37 {
38     struct timeval opStopTime, elapsedTime;
39
40     FT_GetTimeOfDay(&opStopTime, NULL);
41     if (code == 0) {
42         FS_LOCK;
43         (stats->opP->numSuccesses)++;
44         fs_stats_GetDiff(elapsedTime, stats->opStartTime, opStopTime);
45         fs_stats_AddTo((stats->opP->sumTime), elapsedTime);
46         fs_stats_SquareAddTo((stats->opP->sqrTime), elapsedTime);
47         if (fs_stats_TimeLessThan(elapsedTime, (stats->opP->minTime))) {
48             fs_stats_TimeAssign((stats->opP->minTime), elapsedTime);
49         }
50         if (fs_stats_TimeGreaterThan(elapsedTime, (stats->opP->maxTime))) {
51             fs_stats_TimeAssign((stats->opP->maxTime), elapsedTime);
52         }
53         FS_UNLOCK;
54     }
55 }
56
57 void
58 fsstats_StartXfer(struct fsstats *stats, int index)
59 {
60     FT_GetTimeOfDay(&stats->xferStartTime, NULL);
61     stats->xferP = &(afs_FullPerfStats.det.xferOpTimes[index]);
62 }
63
64 void
65 fsstats_FinishXfer(struct fsstats *stats, int code,
66                    afs_sfsize_t bytesToXfer, afs_sfsize_t bytesXferred,
67                    int *remainder)
68 {
69     struct timeval xferStopTime;
70     struct timeval elapsedTime;
71
72     /*
73      * At this point, the data transfer is done, for good or ill.  Remember
74      * when the transfer ended, bump the number of successes/failures, and
75      * integrate the transfer size and elapsed time into the stats.  If the
76      * operation failed, we jump to the appropriate point.
77      */
78     FT_GetTimeOfDay(&xferStopTime, 0);
79     FS_LOCK;
80     (stats->xferP->numXfers)++;
81     if (code == 0) {
82         (stats->xferP->numSuccesses)++;
83
84         /*
85          * Bump the xfer sum by the number of bytes actually sent, NOT the
86          * target number.
87          */
88         *remainder += bytesXferred;
89         (stats->xferP->sumBytes) += (*remainder >> 10);
90         *remainder &= 0x3FF;
91         if (bytesXferred < stats->xferP->minBytes)
92             stats->xferP->minBytes = bytesXferred;
93         if (bytesXferred > stats->xferP->maxBytes)
94             stats->xferP->maxBytes = bytesXferred;
95
96         /*
97          * Tally the size of the object.  Note: we tally the actual size,
98          * NOT the number of bytes that made it out over the wire.
99          */
100         if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET0)
101             (stats->xferP->count[0])++;
102         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET1)
103             (stats->xferP->count[1])++;
104         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET2)
105             (stats->xferP->count[2])++;
106         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET3)
107             (stats->xferP->count[3])++;
108         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET4)
109             (stats->xferP->count[4])++;
110         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET5)
111             (stats->xferP->count[5])++;
112         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET6)
113             (stats->xferP->count[6])++;
114         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET7)
115             (stats->xferP->count[7])++;
116         else
117             (stats->xferP->count[8])++;
118
119         fs_stats_GetDiff(elapsedTime, stats->xferStartTime, xferStopTime);
120         fs_stats_AddTo((stats->xferP->sumTime), elapsedTime);
121         fs_stats_SquareAddTo((stats->xferP->sqrTime), elapsedTime);
122         if (fs_stats_TimeLessThan(elapsedTime, (stats->xferP->minTime))) {
123             fs_stats_TimeAssign((stats->xferP->minTime), elapsedTime);
124         }
125         if (fs_stats_TimeGreaterThan(elapsedTime, (stats->xferP->maxTime))) {
126             fs_stats_TimeAssign((stats->xferP->maxTime), elapsedTime);
127         }
128     }
129     FS_UNLOCK;
130 }
131
132 #else
133
134 void
135 fsstats_StartOp(struct fsstats *stats, int index)
136 {
137     return;
138 }
139
140 void
141 fsstats_FinishOp(struct fsstats *stats, int code)
142 {
143     return;
144 }
145
146 void
147 fsstats_StartXfer(struct fsstats *stats)
148 {
149     return;
150 }
151
152 void
153 fsstats_FinishXfer(struct fsstats *stats, int code,
154                    afs_sfsize_t bytesToXfer, afs_sfsize_t bytesXferred,
155                    int *remainder)
156 {
157     return;
158 }
159
160
161
162
163 #endif