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