d48c3702e624de20fae425b7fee7b6a1320c4fea
[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 <assert.h>
16 #include <roken.h>
17
18 #include <afs/afsint.h>
19 #include <afs/ihandle.h>
20 #include <afs/nfs.h>
21 #include "viced.h"
22 #include "fs_stats.h"
23
24
25 void
26 fsstats_StartOp(struct fsstats *stats, int index)
27 {
28     assert(index >= 0 && index < FS_STATS_NUM_RPC_OPS);
29     stats->opP = &(afs_FullPerfStats.det.rpcOpTimes[index]);
30     FS_LOCK;
31     (stats->opP->numOps)++;
32     FS_UNLOCK;
33     FT_GetTimeOfDay(&stats->opStartTime, NULL);
34 }
35
36 void
37 fsstats_FinishOp(struct fsstats *stats, int code)
38 {
39     struct timeval opStopTime, elapsedTime;
40
41     FT_GetTimeOfDay(&opStopTime, NULL);
42     if (code == 0) {
43         FS_LOCK;
44         (stats->opP->numSuccesses)++;
45         fs_stats_GetDiff(elapsedTime, stats->opStartTime, opStopTime);
46         fs_stats_AddTo((stats->opP->sumTime), elapsedTime);
47         fs_stats_SquareAddTo((stats->opP->sqrTime), elapsedTime);
48         if (fs_stats_TimeLessThan(elapsedTime, (stats->opP->minTime))) {
49             fs_stats_TimeAssign((stats->opP->minTime), elapsedTime);
50         }
51         if (fs_stats_TimeGreaterThan(elapsedTime, (stats->opP->maxTime))) {
52             fs_stats_TimeAssign((stats->opP->maxTime), elapsedTime);
53         }
54         FS_UNLOCK;
55     }
56 }
57
58 void
59 fsstats_StartXfer(struct fsstats *stats, int index)
60 {
61     assert(index >= 0 && index < FS_STATS_NUM_XFER_OPS);
62     FT_GetTimeOfDay(&stats->xferStartTime, NULL);
63     stats->xferP = &(afs_FullPerfStats.det.xferOpTimes[index]);
64 }
65
66 void
67 fsstats_FinishXfer(struct fsstats *stats, int code,
68                    afs_sfsize_t bytesToXfer, afs_sfsize_t bytesXferred,
69                    int *remainder)
70 {
71     struct timeval xferStopTime;
72     struct timeval elapsedTime;
73
74     /*
75      * At this point, the data transfer is done, for good or ill.  Remember
76      * when the transfer ended, bump the number of successes/failures, and
77      * integrate the transfer size and elapsed time into the stats.  If the
78      * operation failed, we jump to the appropriate point.
79      */
80     FT_GetTimeOfDay(&xferStopTime, 0);
81     FS_LOCK;
82     (stats->xferP->numXfers)++;
83     if (code == 0) {
84         (stats->xferP->numSuccesses)++;
85
86         /*
87          * Bump the xfer sum by the number of bytes actually sent, NOT the
88          * target number.
89          */
90         *remainder += bytesXferred;
91         (stats->xferP->sumBytes) += (*remainder >> 10);
92         *remainder &= 0x3FF;
93         if (bytesXferred < stats->xferP->minBytes)
94             stats->xferP->minBytes = bytesXferred;
95         if (bytesXferred > stats->xferP->maxBytes)
96             stats->xferP->maxBytes = bytesXferred;
97
98         /*
99          * Tally the size of the object.  Note: we tally the actual size,
100          * NOT the number of bytes that made it out over the wire.
101          */
102         if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET0)
103             (stats->xferP->count[0])++;
104         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET1)
105             (stats->xferP->count[1])++;
106         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET2)
107             (stats->xferP->count[2])++;
108         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET3)
109             (stats->xferP->count[3])++;
110         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET4)
111             (stats->xferP->count[4])++;
112         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET5)
113             (stats->xferP->count[5])++;
114         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET6)
115             (stats->xferP->count[6])++;
116         else if (bytesToXfer <= FS_STATS_MAXBYTES_BUCKET7)
117             (stats->xferP->count[7])++;
118         else
119             (stats->xferP->count[8])++;
120
121         fs_stats_GetDiff(elapsedTime, stats->xferStartTime, xferStopTime);
122         fs_stats_AddTo((stats->xferP->sumTime), elapsedTime);
123         fs_stats_SquareAddTo((stats->xferP->sqrTime), elapsedTime);
124         if (fs_stats_TimeLessThan(elapsedTime, (stats->xferP->minTime))) {
125             fs_stats_TimeAssign((stats->xferP->minTime), elapsedTime);
126         }
127         if (fs_stats_TimeGreaterThan(elapsedTime, (stats->xferP->maxTime))) {
128             fs_stats_TimeAssign((stats->xferP->maxTime), elapsedTime);
129         }
130     }
131     FS_UNLOCK;
132 }