-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs.h
More file actions
87 lines (71 loc) · 2.11 KB
/
funcs.h
File metadata and controls
87 lines (71 loc) · 2.11 KB
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
#pragma once
#define _GNU_SOURCE 1
#include "constants.h"
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <random>
char* setupBuffer(int bytes) {
// Allocate memory for the buffer
char* buffer = (char *)malloc(bytes);
// Fill the buffer with some data (e.g., 'A')
for (int i = 0; i < bytes; i++) {
buffer[i] = 'A';
}
return buffer;
}
void write_one_gig(int bytesPerWrite, const char* pathToFile, char* buffer, const int seekAmount) {
// Open file
int fd = open(pathToFile, O_RDWR | O_CREAT);
for(int cur = 0; cur < MAX_BYTES; cur += (bytesPerWrite + seekAmount)) {
if(seekAmount > 0)
lseek(fd, seekAmount, SEEK_CUR);
write(fd, buffer, bytesPerWrite);
fsync(fd);
}
// Close file
close(fd);
}
void read_one_gig(int bytesPerRead, const char* pathToFile, char* buffer, const int seekAmount) {
// Open file
int fd = open(pathToFile, O_RDWR | O_CREAT);
for(int cur = 0; cur < MAX_BYTES; cur += (bytesPerRead + seekAmount)) {
if(seekAmount > 0)
lseek(fd, seekAmount, SEEK_CUR);
read(fd, buffer, bytesPerRead);
}
// Close file
close(fd);
}
void write_random_gig(int bytesPerWrite, const char* pathToFile, char* buffer, int lb, int ub) {
// Open file
int fd = open(pathToFile, O_RDWR | O_CREAT);
// Seed random generator
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(lb, ub);
for(int cur = 0; cur < MAX_BYTES; cur += bytesPerWrite) {
int offset = dist(rng);
lseek(fd, offset, SEEK_SET);
write(fd, buffer, bytesPerWrite);
fsync(fd);
}
// Close file
close(fd);
}
void read_random_gig(int bytesPerRead, const char* pathToFile, char* buffer, int lb, int ub) {
// Open file
int fd = open(pathToFile, O_RDWR | O_CREAT);
// Seed random generator
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(lb, ub);
for(int cur = 0; cur < MAX_BYTES; cur += bytesPerRead) {
int offset = dist(rng);
lseek(fd, offset, SEEK_SET);
read(fd, buffer, bytesPerRead);
}
// Close file
close(fd);
}