|
@@ -24,7 +24,7 @@ class memfile_LeaseMgr {
|
|
|
public:
|
|
|
typedef std::map<uint32_t /* addr */, Lease4Ptr /* lease info */> IPv4Hash;
|
|
|
typedef std::map<uint32_t, Lease4Ptr>::iterator leaseIt;
|
|
|
- memfile_LeaseMgr(const std::string& filename);
|
|
|
+ memfile_LeaseMgr(const std::string& filename, bool sync);
|
|
|
~memfile_LeaseMgr();
|
|
|
bool addLease(Lease4Ptr lease);
|
|
|
Lease4Ptr getLease(uint32_t addr);
|
|
@@ -36,42 +36,49 @@ protected:
|
|
|
void writeLease(Lease4Ptr lease);
|
|
|
|
|
|
std::string Filename_;
|
|
|
- std::ofstream File_;
|
|
|
+ bool Sync_; // should we do flush after each operation?
|
|
|
+
|
|
|
+ // we have to use fe
|
|
|
+ FILE * File_;
|
|
|
IPv4Hash ip4Hash_;
|
|
|
};
|
|
|
|
|
|
-memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename)
|
|
|
- : File_(filename.c_str(), ios::trunc), Filename_(filename) {
|
|
|
- if (!File_.is_open()) {
|
|
|
+memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename, bool sync)
|
|
|
+ : Filename_(filename), Sync_(sync) {
|
|
|
+ File_ = fopen(filename.c_str(), "w");
|
|
|
+ if (!File_) {
|
|
|
throw "Failed to create file " + filename;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
memfile_LeaseMgr::~memfile_LeaseMgr() {
|
|
|
- File_.close();
|
|
|
+ fclose(File_);
|
|
|
}
|
|
|
|
|
|
void memfile_LeaseMgr::writeLease(Lease4Ptr lease) {
|
|
|
- File_ << "lease " << lease->addr << " {" << endl << " hw-addr ";
|
|
|
+ fprintf(File_, "lease %d {\n hw-addr ", lease->addr);
|
|
|
for (std::vector<uint8_t>::const_iterator it = lease->hwaddr.begin();
|
|
|
it != lease->hwaddr.end(); ++it) {
|
|
|
- File_ << *it;
|
|
|
+ fprintf(File_,"%02x:", *it);
|
|
|
}
|
|
|
- File_ << ";" << endl << " client-id ";
|
|
|
+ fprintf(File_, ";\n client-id ");
|
|
|
for (std::vector<uint8_t>::const_iterator it = lease->client_id.begin();
|
|
|
it != lease->client_id.end(); ++it) {
|
|
|
- File_ << *it;
|
|
|
+ fprintf(File_, "%02x:", *it);
|
|
|
+ }
|
|
|
+ fprintf(File_, ";\n valid-lifetime %d;\n recycle-time %d;\n"
|
|
|
+ " cltt %d;\n pool-id %d;\n fixed %s; hostname %s;\n"
|
|
|
+ " fqdn_fwd %s;\n fqdn_rev %s;\n};\n",
|
|
|
+ lease->valid_lft, lease->recycle_time, (int)lease->cltt,
|
|
|
+ lease->pool_id, lease->fixed?"true":"false",
|
|
|
+ lease->hostname.c_str(), lease->fqdn_fwd?"true":"false",
|
|
|
+ lease->fqdn_rev?"true":"false");
|
|
|
+
|
|
|
+ if (Sync_) {
|
|
|
+ fflush(File_);
|
|
|
+ fsync(fileno(File_));
|
|
|
}
|
|
|
- File_ << ";" << endl << " valid-lifetime " << lease->valid_lft << endl;
|
|
|
- File_ << " recycle-time " << lease->recycle_time << endl;
|
|
|
- File_ << " cltt " << lease->cltt << endl;
|
|
|
- File_ << " pool-id " << lease->pool_id << endl;
|
|
|
- File_ << " fixed " << lease->fixed << endl;
|
|
|
- File_ << " hostname " << lease->hostname << endl;
|
|
|
- File_ << " fqdn_fwd " << lease->fqdn_fwd << endl;
|
|
|
- File_ << " fqdn_rev " << lease->fqdn_rev << endl;
|
|
|
- File_ << "}" << endl;
|
|
|
}
|
|
|
|
|
|
bool memfile_LeaseMgr::addLease(Lease4Ptr lease) {
|
|
@@ -117,8 +124,11 @@ bool memfile_LeaseMgr::deleteLease(uint32_t addr) {
|
|
|
}
|
|
|
|
|
|
memfile_uBenchmark::memfile_uBenchmark(const string& filename,
|
|
|
- uint32_t num_iterations)
|
|
|
- :uBenchmark(num_iterations), Filename_(filename) {
|
|
|
+ uint32_t num_iterations,
|
|
|
+ bool sync,
|
|
|
+ bool verbose)
|
|
|
+ :uBenchmark(num_iterations, filename, sync, verbose),
|
|
|
+ Filename_(filename) {
|
|
|
|
|
|
}
|
|
|
|
|
@@ -128,7 +138,7 @@ void memfile_uBenchmark::failure(const char* operation) {
|
|
|
|
|
|
void memfile_uBenchmark::connect() {
|
|
|
try {
|
|
|
- LeaseMgr_ = new memfile_LeaseMgr(Filename_);
|
|
|
+ LeaseMgr_ = new memfile_LeaseMgr(Filename_, Sync_);
|
|
|
|
|
|
} catch (const std::string& e) {
|
|
|
failure(e.c_str());
|
|
@@ -192,7 +202,9 @@ void memfile_uBenchmark::createLease4Test() {
|
|
|
if (!LeaseMgr_->addLease(lease)) {
|
|
|
failure("addLease() failed");
|
|
|
} else {
|
|
|
- printf(".");
|
|
|
+ if (Verbose_) {
|
|
|
+ printf(".");
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
addr++;
|
|
@@ -215,10 +227,12 @@ void memfile_uBenchmark::searchLease4Test() {
|
|
|
uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
|
|
|
|
|
|
Lease4Ptr lease = LeaseMgr_->getLease(x);
|
|
|
- if (lease) {
|
|
|
- printf(".");
|
|
|
- } else {
|
|
|
- printf("X");
|
|
|
+ if (Verbose_) {
|
|
|
+ if (lease) {
|
|
|
+ printf(".");
|
|
|
+ } else {
|
|
|
+ printf("X");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -244,7 +258,9 @@ void memfile_uBenchmark::updateLease4Test() {
|
|
|
tmp << "UPDATE failed for lease " << hex << x << dec;
|
|
|
failure(tmp.str().c_str());
|
|
|
}
|
|
|
- printf(".");
|
|
|
+ if (Verbose_) {
|
|
|
+ printf(".");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
printf("\n");
|
|
@@ -267,7 +283,9 @@ void memfile_uBenchmark::deleteLease4Test() {
|
|
|
tmp << "UPDATE failed for lease " << hex << x << dec;
|
|
|
failure(tmp.str().c_str());
|
|
|
}
|
|
|
- printf(".");
|
|
|
+ if (Verbose_) {
|
|
|
+ printf(".");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
printf("\n");
|
|
@@ -281,9 +299,11 @@ void memfile_uBenchmark::printInfo() {
|
|
|
int main(int argc, const char * argv[]) {
|
|
|
|
|
|
const char * filename = "dhcpd.leases";
|
|
|
- uint32_t num = 10000;
|
|
|
+ uint32_t num = 100;
|
|
|
+ bool sync = true;
|
|
|
+ bool verbose = false;
|
|
|
|
|
|
- memfile_uBenchmark bench(filename, num);
|
|
|
+ memfile_uBenchmark bench(filename, num, sync, verbose);
|
|
|
|
|
|
int result = bench.run();
|
|
|
|