memfile_ubench.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <sstream>
  15. #include <iostream>
  16. #include <map>
  17. #include "memfile_ubench.h"
  18. using namespace std;
  19. /// @brief In-memory + lease file database implementation
  20. ///
  21. /// This is a simplified in-memory database that mimics ISC DHCP4 implementation.
  22. /// It uses STL and boost: std::map for storage, boost::shared ptr for memory
  23. /// management. It does use C file operations (fopen, fwrite, etc.), because
  24. /// C++ streams does not offer any easy way to flush their contents, like
  25. /// fflush() and fsync() does.
  26. ///
  27. /// IPv4 address is used as a key in the hash.
  28. class memfile_LeaseMgr {
  29. public:
  30. /// A hash table for Lease4 leases.
  31. typedef std::map<uint32_t /* addr */, Lease4Ptr /* lease info */> IPv4Hash;
  32. /// An iterator for Lease4 hash table.
  33. typedef std::map<uint32_t, Lease4Ptr>::iterator leaseIt;
  34. /// @brief The sole memfile lease manager constructor
  35. ///
  36. /// @param filename name of the lease file (will be overwritten)
  37. /// @param sync should operations be
  38. memfile_LeaseMgr(const std::string& filename, bool sync);
  39. /// @brief Destructor (closes file)
  40. ~memfile_LeaseMgr();
  41. /// @brief adds a lease to the hash
  42. ///
  43. /// @param lease lease to be added
  44. bool addLease(Lease4Ptr lease);
  45. /// @brief returns existing lease
  46. ///
  47. /// @param addr address of the searched lease
  48. ///
  49. /// @return smart pointer to the lease (or NULL if lease is not found)
  50. Lease4Ptr getLease(uint32_t addr);
  51. /// @brief Simplified lease update.
  52. ///
  53. /// Searches for a lease and then updates its client last transmission
  54. /// time. Writes new lease content to lease file (and calls fflush()/fsync(),
  55. /// if synchronous operation is enabled).
  56. ///
  57. /// @param addr IPv4 address
  58. /// @param new_cltt New client last transmission time
  59. ///
  60. /// @return pointer to the updated lease (or NULL)
  61. Lease4Ptr updateLease(uint32_t addr, uint32_t new_cltt);
  62. /// @brief Deletes a lease.
  63. ///
  64. /// @param addr IPv4 address of the lease to be deleted.
  65. ///
  66. /// @return true if deletion was successful, false if no such lease exists
  67. bool deleteLease(uint32_t addr);
  68. protected:
  69. /// @brief Writes updated lease to a file.
  70. ///
  71. /// @param lease lease to be written
  72. void writeLease(Lease4Ptr lease);
  73. /// Name of the lease file.
  74. std::string filename_;
  75. /// should we do flush after each operation?
  76. bool sync_;
  77. /// File handle to the open lease file.
  78. FILE * file_;
  79. /// Hash table for IPv4 leases
  80. IPv4Hash ip4Hash_;
  81. };
  82. memfile_LeaseMgr::memfile_LeaseMgr(const std::string& filename, bool sync)
  83. : filename_(filename), sync_(sync) {
  84. file_ = fopen(filename.c_str(), "w");
  85. if (!file_) {
  86. throw "Failed to create file " + filename;
  87. }
  88. }
  89. memfile_LeaseMgr::~memfile_LeaseMgr() {
  90. fclose(file_);
  91. }
  92. void memfile_LeaseMgr::writeLease(Lease4Ptr lease) {
  93. fprintf(file_, "lease %d {\n hw-addr ", lease->addr);
  94. for (std::vector<uint8_t>::const_iterator it = lease->hwaddr.begin();
  95. it != lease->hwaddr.end(); ++it) {
  96. fprintf(file_, "%02x:", *it);
  97. }
  98. fprintf(file_, ";\n client-id ");
  99. for (std::vector<uint8_t>::const_iterator it = lease->client_id.begin();
  100. it != lease->client_id.end(); ++it) {
  101. fprintf(file_, "%02x:", *it);
  102. }
  103. fprintf(file_, ";\n valid-lifetime %d;\n recycle-time %d;\n"
  104. " cltt %d;\n pool-id %d;\n fixed %s; hostname %s;\n"
  105. " fqdn_fwd %s;\n fqdn_rev %s;\n};\n",
  106. lease->valid_lft, lease->recycle_time, (int)lease->cltt,
  107. lease->pool_id, lease->fixed?"true":"false",
  108. lease->hostname.c_str(), lease->fqdn_fwd?"true":"false",
  109. lease->fqdn_rev?"true":"false");
  110. if (sync_) {
  111. fflush(file_);
  112. fsync(fileno(file_));
  113. }
  114. }
  115. bool memfile_LeaseMgr::addLease(Lease4Ptr lease) {
  116. if (ip4Hash_.find(lease->addr) != ip4Hash_.end()) {
  117. // there is such an address already in the hash
  118. return false;
  119. }
  120. ip4Hash_.insert(pair<uint32_t, Lease4Ptr>(lease->addr, lease));
  121. lease->hostname = "add";
  122. writeLease(lease);
  123. return (true);
  124. }
  125. Lease4Ptr memfile_LeaseMgr::getLease(uint32_t addr) {
  126. leaseIt x = ip4Hash_.find(addr);
  127. if (x != ip4Hash_.end()) {
  128. return x->second; // found
  129. }
  130. // not found
  131. return Lease4Ptr();
  132. }
  133. Lease4Ptr memfile_LeaseMgr::updateLease(uint32_t addr, uint32_t new_cltt) {
  134. leaseIt x = ip4Hash_.find(addr);
  135. if (x != ip4Hash_.end()) {
  136. x->second->cltt = new_cltt;
  137. x->second->hostname = "update";
  138. writeLease(x->second);
  139. return x->second;
  140. }
  141. return Lease4Ptr();
  142. }
  143. bool memfile_LeaseMgr::deleteLease(uint32_t addr) {
  144. leaseIt x = ip4Hash_.find(addr);
  145. if (x != ip4Hash_.end()) {
  146. x->second->hostname = "delete";
  147. writeLease(x->second);
  148. ip4Hash_.erase(x);
  149. return true;
  150. }
  151. return false;
  152. }
  153. memfile_uBenchmark::memfile_uBenchmark(const string& filename,
  154. uint32_t num_iterations,
  155. bool sync,
  156. bool verbose)
  157. :uBenchmark(num_iterations, filename, sync, verbose) {
  158. }
  159. void memfile_uBenchmark::connect() {
  160. try {
  161. leaseMgr_ = new memfile_LeaseMgr(dbname_, sync_);
  162. } catch (const std::string& e) {
  163. failure(e.c_str());
  164. }
  165. }
  166. void memfile_uBenchmark::disconnect() {
  167. delete leaseMgr_;
  168. leaseMgr_ = NULL;
  169. }
  170. void memfile_uBenchmark::createLease4Test() {
  171. if (!leaseMgr_) {
  172. throw "No LeaseMgr instantiated.";
  173. }
  174. uint32_t addr = BASE_ADDR4; // Let's start with 1.0.0.0 address
  175. const uint8_t hwaddr_len = 20; // Not a real field
  176. char hwaddr_tmp[hwaddr_len];
  177. const uint8_t client_id_len = 128;
  178. char client_id_tmp[client_id_len];
  179. uint32_t valid_lft = 1000; // We can use the same value for all leases
  180. uint32_t recycle_time = 0; // Not supported in any foreseeable future,
  181. // so keep this as 0
  182. time_t cltt = time(NULL); // Timestamp
  183. uint32_t pool_id = 0; // Let's use pools 0-99
  184. bool fixed = false;
  185. string hostname("foo"); // Will generate it dynamically
  186. bool fqdn_fwd = true; // Let's pretend to do AAAA update
  187. bool fqdn_rev = true; // Let's pretend to do PTR update
  188. cout << "CREATE: ";
  189. // While we could put the data directly into vector, I would like to
  190. // keep the code as similar to other benchmarks as possible
  191. for (uint8_t i = 0; i < hwaddr_len; ++i) {
  192. hwaddr_tmp[i] = 'A' + i; // let's make hwaddr consisting of letter
  193. }
  194. vector<uint8_t> hwaddr(hwaddr_tmp, hwaddr_tmp + hwaddr_len - 1);
  195. for (uint8_t i = 0; i < client_id_len; i++) {
  196. client_id_tmp[i] = 33 + i; // 33 is being the first, non whitespace
  197. // printable ASCII character
  198. }
  199. vector<uint8_t> client_id(client_id_tmp, client_id_tmp + client_id_len - 1);
  200. for (uint32_t i = 0; i < num_; ++i) {
  201. cltt++;
  202. Lease4Ptr lease = boost::shared_ptr<Lease4>(new Lease4());
  203. lease->addr = addr;
  204. lease->hwaddr = hwaddr;
  205. lease->client_id = client_id;
  206. lease->valid_lft = valid_lft;
  207. lease->recycle_time = recycle_time;
  208. lease->cltt = cltt;
  209. lease->pool_id = pool_id;
  210. lease->fixed = fixed;
  211. lease->hostname = "foo";
  212. lease->fqdn_fwd = fqdn_fwd;
  213. lease->fqdn_rev = fqdn_rev;
  214. if (!leaseMgr_->addLease(lease)) {
  215. failure("addLease() failed");
  216. } else {
  217. if (verbose_) {
  218. cout << ".";
  219. }
  220. };
  221. addr++;
  222. }
  223. cout << endl;
  224. }
  225. void memfile_uBenchmark::searchLease4Test() {
  226. if (!leaseMgr_) {
  227. throw "No LeaseMgr instantiated.";
  228. }
  229. cout << "RETRIEVE: ";
  230. for (uint32_t i = 0; i < num_; i++) {
  231. uint32_t x = BASE_ADDR4 + random() % int(num_ / hitratio_);
  232. Lease4Ptr lease = leaseMgr_->getLease(x);
  233. if (verbose_) {
  234. cout << (lease?".":"X");
  235. }
  236. }
  237. cout << endl;
  238. }
  239. void memfile_uBenchmark::updateLease4Test() {
  240. if (!leaseMgr_) {
  241. throw "No LeaseMgr instantiated.";
  242. }
  243. cout << "UPDATE: ";
  244. time_t cltt = time(NULL);
  245. for (uint32_t i = 0; i < num_; i++) {
  246. uint32_t x = BASE_ADDR4 + random() % num_;
  247. Lease4Ptr lease = leaseMgr_->updateLease(x, cltt);
  248. if (!lease) {
  249. stringstream tmp;
  250. tmp << "UPDATE failed for lease " << hex << x << dec;
  251. failure(tmp.str().c_str());
  252. }
  253. if (verbose_) {
  254. cout << ".";
  255. }
  256. }
  257. cout << endl;
  258. }
  259. void memfile_uBenchmark::deleteLease4Test() {
  260. if (!leaseMgr_) {
  261. throw "No LeaseMgr instantiated.";
  262. }
  263. cout << "DELETE: ";
  264. for (uint32_t i = 0; i < num_; i++) {
  265. uint32_t x = BASE_ADDR4 + i;
  266. if (!leaseMgr_->deleteLease(x)) {
  267. stringstream tmp;
  268. tmp << "UPDATE failed for lease " << hex << x << dec;
  269. failure(tmp.str().c_str());
  270. }
  271. if (verbose_) {
  272. cout << ".";
  273. }
  274. }
  275. cout << endl;
  276. }
  277. void memfile_uBenchmark::printInfo() {
  278. cout << "Memory db (using std::map) + write-only file." << endl;
  279. }
  280. int main(int argc, char * const argv[]) {
  281. const char * filename = "dhcpd.leases";
  282. uint32_t num = 100;
  283. bool sync = true;
  284. bool verbose = false;
  285. memfile_uBenchmark bench(filename, num, sync, verbose);
  286. bench.parseCmdline(argc, argv);
  287. int result = bench.run();
  288. return (result);
  289. }