Merge pull request #3340

284fe64 db_lmdb: warn about slowness when running off a spinning disk (moneromooo-monero)
This commit is contained in:
luigi1111 2018-06-08 15:16:59 -05:00
commit 25e7a7d96f
No known key found for this signature in database
GPG Key ID: F4ACA0183641E010
3 changed files with 75 additions and 0 deletions

View File

@ -1178,6 +1178,9 @@ void BlockchainLMDB::open(const std::string& filename, const int db_flags)
throw DB_ERROR("Database could not be opened"); throw DB_ERROR("Database could not be opened");
} }
if (tools::is_hdd(filename.c_str()))
MCLOG_RED(el::Level::Warning, "global", "The blockchain is on a rotating drive: this will be very slow, use a SSD if possible");
m_folder = filename; m_folder = filename;
#ifdef __OpenBSD__ #ifdef __OpenBSD__

View File

@ -34,6 +34,17 @@
#include <gnu/libc-version.h> #include <gnu/libc-version.h>
#endif #endif
#ifdef __GLIBC__
#include <sys/types.h>
#include <sys/stat.h>
#include <ustat.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <ctype.h>
#include <string>
#endif
#include "unbound.h" #include "unbound.h"
#include "include_base_utils.h" #include "include_base_utils.h"
@ -632,6 +643,65 @@ std::string get_nix_version_display_string()
#endif #endif
} }
bool is_hdd(const char *path)
{
#ifdef __GLIBC__
std::string device = "";
struct stat st, dst;
if (stat(path, &st) < 0)
return 0;
DIR *dir = opendir("/dev/block");
if (!dir)
return 0;
struct dirent *de;
while ((de = readdir(dir)))
{
if (strcmp(de->d_name, ".") && strcmp(de->d_name, ".."))
{
std::string dev_path = std::string("/dev/block/") + de->d_name;
char resolved[PATH_MAX];
if (realpath(dev_path.c_str(), resolved) && !strncmp(resolved, "/dev/", 5))
{
if (stat(resolved, &dst) == 0)
{
if (dst.st_rdev == st.st_dev)
{
// take out trailing digits (eg, sda1 -> sda)
char *ptr = resolved;
while (*ptr)
++ptr;
while (ptr > resolved && isdigit(*--ptr))
*ptr = 0;
device = resolved + 5;
break;
}
}
}
}
}
closedir(dir);
if (device.empty())
return 0;
std::string sys_path = "/sys/block/" + device + "/queue/rotational";
FILE *f = fopen(sys_path.c_str(), "r");
if (!f)
return false;
char s[8];
char *ptr = fgets(s, sizeof(s), f);
fclose(f);
if (!ptr)
return 0;
s[sizeof(s) - 1] = 0;
int n = atoi(s); // returns 0 on parse error
return n == 1;
#else
return 0;
#endif
}
namespace namespace
{ {
boost::mutex max_concurrency_lock; boost::mutex max_concurrency_lock;

View File

@ -212,4 +212,6 @@ namespace tools
bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash); bool sha256sum(const uint8_t *data, size_t len, crypto::hash &hash);
bool sha256sum(const std::string &filename, crypto::hash &hash); bool sha256sum(const std::string &filename, crypto::hash &hash);
bool is_hdd(const char *path);
} }