forked from acozzette/BUSE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskStats.cpp
115 lines (101 loc) · 3.63 KB
/
diskStats.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//
// Created by andras on 5/6/15.
//
#include "diskStats.h"
#include <assert.h>
#include <fcntl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
diskStats::diskStats(int fd, string name) : name(name) {
aiocb.aio_fildes = fd;
/* Figure out the size of the underlying block device. */
int err = ioctl(fd, BLKGETSIZE64, &(this->diskSize));
assert(err != -1);
testSpeed();
}
diskStats::diskStats(int fd, string name, chrono::duration<double, std::micro> writeSpeed, chrono::duration<double, std::micro> readSpeed) : name(name) {
aiocb.aio_fildes = fd;
/* Figure out the size of the underlying block device. */
int err = ioctl(fd, BLKGETSIZE64, &(this->diskSize));
assert(err != -1);
this->writeSpeed = writeSpeed;
this->readSpeed = readSpeed;
}
#define READBYTESSIZE (128*1024*1024)
void diskStats::testSpeed() {
if (this->diskSize < 2 * READBYTESSIZE) {
this->readSpeed = chrono::duration<double, std::micro>::zero();
this->writeSpeed = chrono::duration<double, std::micro>::zero();
return;
}
char *buf0 = (char *) malloc(sizeof(char) * READBYTESSIZE);
char *buf1 = (char *) malloc(sizeof(char) * READBYTESSIZE);
char *buf2 = (char *) malloc(sizeof(char) * READBYTESSIZE);
char *buf3 = (char *) malloc(sizeof(char) * READBYTESSIZE);
if(unlikely(buf0 == NULL || buf1 == NULL || buf2 == NULL || buf3 == NULL)) {
cerr << "Unable to allocate memory for testSpeeds()" << endl;
goto cleanup;
}
for (uint i = 0; i < READBYTESSIZE; i++) { buf3[i] = (char) 0xA5; }
testSpeed(buf0, buf1, buf2, buf3, READBYTESSIZE);
cleanup:free(buf3);
free(buf2);
free(buf1);
free(buf0);
}
void diskStats::testSpeed(char *buf0, char *buf1, char *buf2, const char *buf3, const uint32_t bufSiz) {
DEBUGPRINTLN("Entered testSpeed");
assert(buf0 != NULL); assert(buf1 != NULL); assert(buf2 != NULL); assert(buf3 != NULL);
assert(this->diskSize > 2*bufSiz);
DEBUGPRINTLN("Testing readSpeed");
auto startTime = chrono::system_clock::now();
read(buf0, bufSiz, 0);
read(buf1, bufSiz, this->diskSize / 2);
read(buf2, bufSiz, this->diskSize - bufSiz);
this->readSpeed = chrono::system_clock::now() - startTime;
DEBUGPRINTLN("Testing writeSpeed");
startTime = chrono::system_clock::now();
write(buf3, bufSiz, 0);
write(buf3, bufSiz, this->diskSize / 2);
write(buf3, bufSiz, this->diskSize - bufSiz);
this->writeSpeed = chrono::system_clock::now() - startTime;
DEBUGPRINTLN("Cleaning up");
write(buf0, bufSiz, 0);
write(buf1, bufSiz, this->diskSize / 2);
write(buf2, bufSiz, this->diskSize - bufSiz);
}
int diskStats::aio_error() { return ::aio_error64(&aiocb); }
ssize_t diskStats::aio_return() { return ::aio_return64(&aiocb); }
int diskStats::aio_write(const void *buf, size_t len, off64_t offset) {
if (aio_error()) {
errno = EBUSY;
return -1;
}
aiocb.aio_nbytes = len;
aiocb.aio_offset = offset;
aiocb.aio_buf = (void *) buf;
return ::aio_write64(&aiocb);
}
int diskStats::aio_read(void *buf, size_t len, off64_t offset) {
if (aio_error()) {
errno = EBUSY;
return -1;
}
aiocb.aio_nbytes = len;
aiocb.aio_offset = offset;
aiocb.aio_buf = buf;
return ::aio_read64(&aiocb);
}
ssize_t diskStats::write(const void *buf, size_t len, off64_t offset) {
DEBUGPRINTLN("write(" << offset << ',' << len << ") with diskSize=" << this->diskSize);
if (unlikely(lseek64(getFD(), offset, SEEK_SET) != offset)) return -1;
return ::write(getFD(), buf, len);
}
ssize_t diskStats::read(void *buf, size_t len, off64_t offset) {
DEBUGPRINTLN("read(" << offset << ',' << len << ") with diskSize=" << this->diskSize);
if (unlikely(lseek64(getFD(), offset, SEEK_SET) != offset)) return -1;
return ::read(getFD(), buf, len);
}