-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
210 lines (184 loc) · 7.19 KB
/
utils.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <filesystem>
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <regex>
#include "Iterator.h"
#include "utils.h"
#include "Metrics.h"
/*
Return value:
* recordCount: Number of records to generate
* recordSize: Size of each record, must be 20-2000
* tracePath: trace log path
* outputPath: Output path
* inputPath: Input path. If not provided, value is empty
* removeDuplicates: remove duplicates, default is false
* if provided, also provides <removal-method>
*/
Config getArgs(int argc, char* argv[])
{
ArgumentParser parser("sort");
/*
Arguments:
* -c, --count: Number of records to generate
* -s, --size: Size of each record, must be 20-2000
* -t, --trace: Trace log path. Default is "trace"
* (optional) -o, --ouput: Output path. If not provided, default to "output.txt"
* (optional) -i, --input: Input path. If not provided, generate records
* (optional) -d, --duplicate-removal: Remove duplicates. Default is false. If provided, also provides <removal-method>
*/
parser.add_argument("-c", "--count")
.scan<'d', RowCount>()
.help("Number of records to generate")
.required();
parser.add_argument("-s", "--size")
.scan<'d', RowSize>()
.help("Size of each record, must be 20-2000")
.required();
parser.add_argument("-t", "--trace")
.help("Trace log path. If not provided, default to console")
.default_value(string(""));
parser.add_argument("-o", "--output")
.help("Output path. If not provided, default to 'output.txt'")
.default_value(string("output.txt"));
parser.add_argument("-i", "--input")
.help("Input path. If not provided, generate random records")
.default_value(string(""));
parser.add_argument("-d", "--duplicate-removal")
.help("Remove duplicates by providing the removal method, either 'insort' or 'instream'");
try {
parser.parse_args(argc, argv);
} catch (const std::runtime_error &err) {
std::cout << err.what() << std::endl;
std::cout << parser;
exit(1);
}
Config config;
config.recordCount = parser.get<RowCount>("-c");
config.recordSize = parser.get<RowSize>("-s");
config.tracePath = parser.get<string>("-t");
config.outputPath = parser.get<string>("-o");
config.inputPath = parser.get<string>("-i");
// first check if -d is provided
if (parser.present("-d")) {
config.removeDuplicates = true;
config.removalMethod = parser.get<string>("-d");
// if it's not insort or instream, raise an error
if (config.removalMethod != "insort" && config.removalMethod != "instream") {
std::cerr << "Error: Expected removal method to be insort or instream, got " << config.removalMethod << '\n';
exit(1);
}
} else {
config.removeDuplicates = false;
}
// check record size range
if (config.recordSize < 20 || config.recordSize > 2000) {
std::cerr << "Warning: Expected record size between 20 and 2000, got " << config.recordSize << '\n';
// do not exit, let the program continue
// exit(1);
}
return config;
}
std::tuple<string, string> separatePath(string path)
{
size_t lastSlash = path.rfind(SEPARATOR);
if (lastSlash == string::npos) {
return std::make_tuple("", path);
}
return std::make_tuple(
path.substr(0, lastSlash), path.substr(lastSlash+1));
}
string byteToHexString(byte byte) {
std::stringstream result;
result << "0x"
<< std::hex // Use hexadecimal format
<< std::uppercase // Optional: Use uppercase letters for A-F
<< std::setw(2) // Ensure the output is at least two digits
<< std::setfill('0') // Fill with leading zeros if necessary
<< static_cast<int>(byte); // Convert byte to int for correct formatting
return result.str();
}
string rowToHexString(byte * rowContent, RowSize size) {
if (rowContent == nullptr) {
return "nullptr";
}
size = std::min(size, (RowSize) 20); // Curtail long output/logs
string result = "";
for (int i = 0; i < size; ++i) {
byte byte = rowContent[i];
string hexString = byteToHexString(byte);
result += hexString + " ";
}
// get rid of the last space
result.pop_back();
return result;
}
u_int32_t getRecordCountPerRun(RowSize recordSize) {
int device = Metrics::getAvailableStorage();
u_int32_t recordCountPerRun = (MEMORY_SIZE - Metrics::getParams(device).pageSize) / recordSize;
return recordCountPerRun;
}
// convert a row of bytes to a string
// the bytes are already converted to alphanumeric characters
string rowToString(byte * rowContent, RowSize size) {
if (rowContent == nullptr) {
return "nullptr";
}
string result(size, ' ');
for (int i = 0; i < size; ++i) {
byte byte = rowContent[i];
result[i] = byte;
}
return result;
}
// convert a row of bytes to a string
// where the bytes are represented as their raw values
string rowRawValueToString(byte * rowContent, RowSize size) {
string result;
for (int i = 0; i < size; ++i) {
byte byte = rowContent[i];
result += std::to_string(byte) + " ";
}
return result;
}
tuple<vector<u_int8_t>, vector<u_int64_t>> parseDeviceType(const string &filename) {
vector<u_int8_t> deviceTypes;
vector<u_int64_t> switchPoints;
std::regex deviceTypePattern("device(\\d+)");
auto deviceTypeBegin = std::sregex_iterator(filename.begin(), filename.end(), deviceTypePattern);
auto deviceTypeEnd = std::sregex_iterator();
for (std::sregex_iterator i = deviceTypeBegin; i != deviceTypeEnd; ++i) {
std::smatch match = *i;
if (match.size() > 1) {
u_int8_t deviceType = std::stoi(match[1].str()); // Access the first capturing group
deviceTypes.push_back(deviceType);
}
}
std::regex switchPointPattern("device\\d+-(\\d+)-");
auto switchPointBegin = std::sregex_iterator(filename.begin(), filename.end(), switchPointPattern);
auto switchPointEnd = std::sregex_iterator();
for (std::sregex_iterator i = switchPointBegin; i != switchPointEnd; ++i) {
std::smatch match = *i;
if (match.size() > 1) {
u_int64_t switchPoint = std::stoull(match[1].str()); // Access the first capturing group
switchPoints.push_back(switchPoint);
}
}
Assert(deviceTypes.size() == switchPoints.size() + 1, __FILE__, __LINE__);
return std::make_tuple(deviceTypes, switchPoints);
}
u_int8_t getLargestDeviceType(const string &filename) {
vector<u_int8_t> deviceTypes;
vector<u_int64_t> switchPoints;
std::tie(deviceTypes, switchPoints) = parseDeviceType(filename);
if (deviceTypes.size() > 1) {
std::cerr << "Warning: Multiple device types found in " << filename << ". You are only getting the largest device.\n";
}
return *std::max_element(deviceTypes.begin(), deviceTypes.end());
}
void renameOutputFile(const string &outputPath) {
auto &answerPath = Trace::finalOutputFileName;
Assert(std::filesystem::exists(answerPath), __FILE__, __LINE__);
std::rename(answerPath.c_str(), outputPath.c_str());
}