-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
59 lines (46 loc) · 1.66 KB
/
main.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
#include "my_io.h"
#include <iostream>
#include <ctime>
#include <memory>
#include <unistd.h>
#include <omp.h>
#include <mutex>
double perFileTime;
std::mutex io_mutex; // Add a mutex to protect shared resources
void cat(const char *filename) {
std::lock_guard<std::mutex> lock(io_mutex); // Protect the entire file operation
my_file *mf = my_fopen(filename, "r");
if (!mf) {
perror("Failed to open file.");
return;
}
size_t bytesRead;
std::unique_ptr<char[]> buffer(new char[4096]);
auto start = std::clock();
while ((bytesRead = my_fread(buffer.get(), sizeof(char), sizeof(buffer), mf)) > 0) {
write(STDOUT_FILENO, buffer.get(), bytesRead);
}
//std::cout << std::endl;
auto end = std::clock();
double cpu_time_used = double(end - start) / CLOCKS_PER_SEC;
perFileTime += cpu_time_used;
std::cout << "\nCompleted reading '" << filename << "': Duration = " << cpu_time_used << " seconds, File Size = " << mf->fi->file_sz << " bytes.\n";
my_fclose(mf);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <filename>\n";
return 1;
}
perFileTime = 0.0;
auto start = std::clock();
for (int i = 1; i < argc; i++) {
cat(argv[i]);
}
auto end = std::clock();
double total_time = double(end - start) / CLOCKS_PER_SEC;
//std::cout << "Total time to read " << (argc - 1) << " files: " << total_time << " seconds\n";
//std::cout << "Average time per read file: " << perFileTime / (argc - 1) << " seconds\n";
//std::cout << "Use io_uring_enter system call times = " << systemTimes << std::endl;
return 0;
}