aboutsummaryrefslogtreecommitdiff
path: root/src/common/util.cpp
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-03-03 14:01:52 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2018-06-08 20:59:02 +0100
commit284fe6438d5444ab11fecce959b32106f5d2562f (patch)
tree35fa631f109fdfd018005ceb5e7ab346e7380335 /src/common/util.cpp
parentMerge pull request #3908 (diff)
downloadmonero-284fe6438d5444ab11fecce959b32106f5d2562f.tar.xz
db_lmdb: warn about slowness when running off a spinning disk
Diffstat (limited to 'src/common/util.cpp')
-rw-r--r--src/common/util.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/common/util.cpp b/src/common/util.cpp
index 7e77e19b1..008610117 100644
--- a/src/common/util.cpp
+++ b/src/common/util.cpp
@@ -34,6 +34,17 @@
#include <gnu/libc-version.h>
#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 "include_base_utils.h"
@@ -632,6 +643,65 @@ std::string get_nix_version_display_string()
#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
{
boost::mutex max_concurrency_lock;