1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
14 #include "bluesky-private.h"
16 /* Management of the filesystem log/cache stored on local disk. This is used
17 * as a buffer for queueing writes to the cloud, a cache for satisfying reads,
18 * and is also used to provide durability for the filesystem across crashes
19 * even if data has not been flushed to the cloud.
21 * We use Berkeley DB since it provides durability of writes after a crash. */
23 void bluesky_localstore_init()
29 res = db_env_create(&env, 0);
33 cerr << db_strerror(res) << endl;
34 throw std::runtime_error("db_env_create fail");
37 /* Set Cache Size To Total Memory */
40 double use_fraction = 0.1;
41 uint64_t pages = sysconf(_SC_PHYS_PAGES);
42 uint64_t page_size = sysconf(_SC_PAGE_SIZE);
44 uint64_t bytes = pages * page_size * use_fraction / num_dbs;
46 uint32_t gbytes = bytes / (1024uLL * 1024uLL * 1024uLL);
47 uint32_t nbytes = bytes % (1024uLL * 1024uLL * 1024uLL);
48 uint32_t ncache = bytes / (1024uLL * 1024uLL * 1024uLL * 4uLL) + 1;
50 res = env->set_cachesize(env, gbytes, nbytes, ncache);
54 cerr << db_strerror(res) << endl;
55 throw std::runtime_error("set_cachesize");
61 res = env->set_flags(env, DB_LOG_IN_MEMORY, 1);
65 cerr << db_strerror(res) << endl;
66 throw std::runtime_error("BDB ENV DB_LOG_IN_MEMORY");
71 res = env->set_flags(env, DB_LOG_AUTO_REMOVE, 1);
75 cerr << db_strerror(res) << endl;
76 throw std::runtime_error("BDB ENV DB_LOG_AUTO_REMOVE");
79 res = env->open(env, _path.c_str(),
80 DB_CREATE | DB_RECOVER | DB_INIT_LOCK | DB_INIT_LOG
81 | DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD,
86 cerr << db_strerror(res) << endl;
87 throw std::runtime_error("BDB ENV Open Fail");
90 string dbfilename = _path + "/test.db";
95 res = env->dbremove(env, NULL, dbfilename.c_str(), "test", 0);
97 if (res != 0 && res != ENOENT)
99 cerr << db_strerror(res) << endl;
100 throw std::runtime_error("db remove failed");
104 res = db_create(&_db, env, 0);
108 cerr << db_strerror(res) << endl;
109 throw std::runtime_error("db_create fail");
112 uint32_t flags = DB_CREATE | DB_THREAD | DB_AUTO_COMMIT;
124 cerr << db_strerror(res) << endl;
125 throw std::runtime_error("BDB Open Fail");