memfile_ubench.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. // not found
  130. return Lease4Ptr();
  131. }
  132. Lease4Ptr memfile_LeaseMgr::updateLease(uint32_t addr, uint32_t new_cltt) {
  133. leaseIt x = ip4Hash_.find(addr);
  134. if (x != ip4Hash_.end()) {
  135. x->second->cltt = new_cltt;
  136. x->second->hostname = "update";
  137. writeLease(x->second);
  138. return x->second;
  139. }
  140. return Lease4Ptr();
  141. }
  142. bool memfile_LeaseMgr::deleteLease(uint32_t addr) {
  143. leaseIt x = ip4Hash_.find(addr);
  144. if (x != ip4Hash_.end()) {
  145. x->second->hostname = "delete";
  146. writeLease(x->second);
  147. ip4Hash_.erase(x);
  148. return true;
  149. }
  150. return false;
  151. }
  152. memfile_uBenchmark::memfile_uBenchmark(const string& filename,
  153. uint32_t num_iterations,
  154. bool sync,
  155. bool verbose)
  156. :uBenchmark(num_iterations, filename, sync, verbose),
  157. Filename_(filename) {
  158. }
  159. void memfile_uBenchmark::connect() {
  160. try {
  161. LeaseMgr_ = new memfile_LeaseMgr(Filename_, 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. printf("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] = 65 + i;
  193. }
  194. vector<uint8_t> hwaddr(hwaddr_tmp, hwaddr_tmp + 19);
  195. for (uint8_t i = 0; i < client_id_len; i++) {
  196. client_id_tmp[i] = 33 + i;
  197. }
  198. vector<uint8_t> client_id(client_id_tmp, client_id_tmp + 19);
  199. for (uint32_t i = 0; i < Num_; i++) {
  200. cltt++;
  201. Lease4Ptr lease = boost::shared_ptr<Lease4>(new Lease4());
  202. lease->addr = addr;
  203. lease->hwaddr = hwaddr;
  204. lease->client_id = client_id;
  205. lease->valid_lft = valid_lft;
  206. lease->recycle_time = recycle_time;
  207. lease->cltt = cltt;
  208. lease->pool_id = pool_id;
  209. lease->fixed = fixed;
  210. lease->hostname = "foo";
  211. lease->fqdn_fwd = fqdn_fwd;
  212. lease->fqdn_rev = fqdn_rev;
  213. if (!LeaseMgr_->addLease(lease)) {
  214. failure("addLease() failed");
  215. } else {
  216. if (Verbose_) {
  217. printf(".");
  218. }
  219. };
  220. addr++;
  221. }
  222. printf("\n");
  223. }
  224. void memfile_uBenchmark::searchLease4Test() {
  225. if (!LeaseMgr_) {
  226. throw "No LeaseMgr instantiated.";
  227. }
  228. // This formula should roughly find something a lease in 90% cases
  229. float hitRatio = 0.5;
  230. printf("RETRIEVE: ");
  231. for (uint32_t i = 0; i < Num_; i++) {
  232. uint32_t x = BASE_ADDR4 + random() % int(Num_ / hitRatio);
  233. Lease4Ptr lease = LeaseMgr_->getLease(x);
  234. if (Verbose_) {
  235. if (lease) {
  236. printf(".");
  237. } else {
  238. printf("X");
  239. }
  240. }
  241. }
  242. printf("\n");
  243. }
  244. void memfile_uBenchmark::updateLease4Test() {
  245. if (!LeaseMgr_) {
  246. throw "No LeaseMgr instantiated.";
  247. }
  248. printf("UPDATE: ");
  249. time_t cltt = time(NULL);
  250. for (uint32_t i = 0; i < Num_; i++) {
  251. uint32_t x = BASE_ADDR4 + random() % Num_;
  252. Lease4Ptr lease = LeaseMgr_->updateLease(x, cltt);
  253. if (!lease) {
  254. stringstream tmp;
  255. tmp << "UPDATE failed for lease " << hex << x << dec;
  256. failure(tmp.str().c_str());
  257. }
  258. if (Verbose_) {
  259. printf(".");
  260. }
  261. }
  262. printf("\n");
  263. }
  264. void memfile_uBenchmark::deleteLease4Test() {
  265. if (!LeaseMgr_) {
  266. throw "No LeaseMgr instantiated.";
  267. }
  268. printf("DELETE: ");
  269. for (uint32_t i = 0; i < Num_; i++) {
  270. uint32_t x = BASE_ADDR4 + i;
  271. if (!LeaseMgr_->deleteLease(x)) {
  272. stringstream tmp;
  273. tmp << "UPDATE failed for lease " << hex << x << dec;
  274. failure(tmp.str().c_str());
  275. }
  276. if (Verbose_) {
  277. printf(".");
  278. }
  279. }
  280. printf("\n");
  281. }
  282. void memfile_uBenchmark::printInfo() {
  283. cout << "Memory db (using std::map) + write-only file." << endl;
  284. }
  285. int main(int argc, char * const argv[]) {
  286. const char * filename = "dhcpd.leases";
  287. uint32_t num = 100;
  288. bool sync = true;
  289. bool verbose = false;
  290. memfile_uBenchmark bench(filename, num, sync, verbose);
  291. bench.parseCmdline(argc, argv);
  292. int result = bench.run();
  293. return (result);
  294. }