X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=bluesky%2Flocalstore.c;fp=bluesky%2Flocalstore.c;h=0ce6c6a7112e67b8b9b8beeb0dc12c0ae445faf4;hb=35005a19d59c064d362e83ab8acaa8af4ac82a06;hp=0000000000000000000000000000000000000000;hpb=3b95f5ea43b13f2487598dff69a81bcbdc09bf3a;p=bluesky.git diff --git a/bluesky/localstore.c b/bluesky/localstore.c new file mode 100644 index 0000000..0ce6c6a --- /dev/null +++ b/bluesky/localstore.c @@ -0,0 +1,128 @@ +/* Blue Sky: File Systems in the Cloud + * + * Copyright (C) 2009 The Regents of the University of California + * Written by Michael Vrable + * + * TODO: Licensing + */ + +#include +#include +#include +#include + +#include "bluesky-private.h" + +/* Management of the filesystem log/cache stored on local disk. This is used + * as a buffer for queueing writes to the cloud, a cache for satisfying reads, + * and is also used to provide durability for the filesystem across crashes + * even if data has not been flushed to the cloud. + * + * We use Berkeley DB since it provides durability of writes after a crash. */ + +void bluesky_localstore_init() +{ + int res; + DB_ENV *env; + DB *db; + + res = db_env_create(&env, 0); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("db_env_create fail"); + } + + /* Set Cache Size To Total Memory */ + if (true) + { + double use_fraction = 0.1; + uint64_t pages = sysconf(_SC_PHYS_PAGES); + uint64_t page_size = sysconf(_SC_PAGE_SIZE); + + uint64_t bytes = pages * page_size * use_fraction / num_dbs; + + uint32_t gbytes = bytes / (1024uLL * 1024uLL * 1024uLL); + uint32_t nbytes = bytes % (1024uLL * 1024uLL * 1024uLL); + uint32_t ncache = bytes / (1024uLL * 1024uLL * 1024uLL * 4uLL) + 1; + + res = env->set_cachesize(env, gbytes, nbytes, ncache); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("set_cachesize"); + } + } + + if (log_in_memory) + { + res = env->set_flags(env, DB_LOG_IN_MEMORY, 1); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("BDB ENV DB_LOG_IN_MEMORY"); + } + + } + + res = env->set_flags(env, DB_LOG_AUTO_REMOVE, 1); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("BDB ENV DB_LOG_AUTO_REMOVE"); + } + + res = env->open(env, _path.c_str(), + DB_CREATE | DB_RECOVER | DB_INIT_LOCK | DB_INIT_LOG + | DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD, + 0644); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("BDB ENV Open Fail"); + } + + string dbfilename = _path + "/test.db"; + + /* Flush */ + if (flush) + { + res = env->dbremove(env, NULL, dbfilename.c_str(), "test", 0); + + if (res != 0 && res != ENOENT) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("db remove failed"); + } + } + + res = db_create(&_db, env, 0); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("db_create fail"); + } + + uint32_t flags = DB_CREATE | DB_THREAD | DB_AUTO_COMMIT; + + res = _db->open(_db, + NULL, /* TXN */ + dbfilename.c_str(), + "test", + DB_BTREE, + flags, + 0644); + + if (res != 0) + { + cerr << db_strerror(res) << endl; + throw std::runtime_error("BDB Open Fail"); + } + +}