platform-nix.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <sys/statvfs.h>
  21. #include <dirent.h>
  22. #include <stdlib.h>
  23. #include <limits.h>
  24. #include <dlfcn.h>
  25. #include <unistd.h>
  26. #include <glob.h>
  27. #include <time.h>
  28. #include <signal.h>
  29. #include "obsconfig.h"
  30. #if !defined(__APPLE__)
  31. #include <sys/times.h>
  32. #include <sys/wait.h>
  33. #include <spawn.h>
  34. #endif
  35. #include "darray.h"
  36. #include "dstr.h"
  37. #include "platform.h"
  38. #include "threading.h"
  39. void *os_dlopen(const char *path)
  40. {
  41. struct dstr dylib_name;
  42. if (!path)
  43. return NULL;
  44. dstr_init_copy(&dylib_name, path);
  45. #ifdef __APPLE__
  46. if (!dstr_find(&dylib_name, ".so") && !dstr_find(&dylib_name, ".dylib"))
  47. #else
  48. if (!dstr_find(&dylib_name, ".so"))
  49. #endif
  50. dstr_cat(&dylib_name, ".so");
  51. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  52. if (!res)
  53. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  54. path, dylib_name.array, dlerror());
  55. dstr_free(&dylib_name);
  56. return res;
  57. }
  58. void *os_dlsym(void *module, const char *func)
  59. {
  60. return dlsym(module, func);
  61. }
  62. void os_dlclose(void *module)
  63. {
  64. if (module)
  65. dlclose(module);
  66. }
  67. #if !defined(__APPLE__)
  68. struct os_cpu_usage_info {
  69. clock_t last_cpu_time, last_sys_time, last_user_time;
  70. int core_count;
  71. };
  72. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  73. {
  74. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  75. struct tms time_sample;
  76. info->last_cpu_time = times(&time_sample);
  77. info->last_sys_time = time_sample.tms_stime;
  78. info->last_user_time = time_sample.tms_utime;
  79. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  80. return info;
  81. }
  82. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  83. {
  84. struct tms time_sample;
  85. clock_t cur_cpu_time;
  86. double percent;
  87. if (!info)
  88. return 0.0;
  89. cur_cpu_time = times(&time_sample);
  90. if (cur_cpu_time <= info->last_cpu_time ||
  91. time_sample.tms_stime < info->last_sys_time ||
  92. time_sample.tms_utime < info->last_user_time)
  93. return 0.0;
  94. percent = (double)(time_sample.tms_stime - info->last_sys_time +
  95. (time_sample.tms_utime - info->last_user_time));
  96. percent /= (double)(cur_cpu_time - info->last_cpu_time);
  97. percent /= (double)info->core_count;
  98. info->last_cpu_time = cur_cpu_time;
  99. info->last_sys_time = time_sample.tms_stime;
  100. info->last_user_time = time_sample.tms_utime;
  101. return percent * 100.0;
  102. }
  103. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  104. {
  105. if (info)
  106. bfree(info);
  107. }
  108. #endif
  109. bool os_sleepto_ns(uint64_t time_target)
  110. {
  111. uint64_t current = os_gettime_ns();
  112. if (time_target < current)
  113. return false;
  114. time_target -= current;
  115. struct timespec req, remain;
  116. memset(&req, 0, sizeof(req));
  117. memset(&remain, 0, sizeof(remain));
  118. req.tv_sec = time_target/1000000000;
  119. req.tv_nsec = time_target%1000000000;
  120. while (nanosleep(&req, &remain)) {
  121. req = remain;
  122. memset(&remain, 0, sizeof(remain));
  123. }
  124. return true;
  125. }
  126. void os_sleep_ms(uint32_t duration)
  127. {
  128. usleep(duration*1000);
  129. }
  130. #if !defined(__APPLE__)
  131. uint64_t os_gettime_ns(void)
  132. {
  133. struct timespec ts;
  134. clock_gettime(CLOCK_MONOTONIC, &ts);
  135. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  136. }
  137. /* should return $HOME/.[name], or when using XDG,
  138. * should return $HOME/.config/[name] as default */
  139. int os_get_config_path(char *dst, size_t size, const char *name)
  140. {
  141. #ifdef USE_XDG
  142. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  143. // If XDG_CONFIG_HOME is unset,
  144. // we use the default $HOME/.config/[name] instead
  145. if (xdg_ptr == NULL) {
  146. char *home_ptr = getenv("HOME");
  147. if (home_ptr == NULL)
  148. bcrash("Could not get $HOME\n");
  149. if (!name || !*name) {
  150. return snprintf(dst, size, "%s/.config", home_ptr);
  151. } else {
  152. return snprintf(dst, size, "%s/.config/%s", home_ptr,
  153. name);
  154. }
  155. } else {
  156. if (!name || !*name)
  157. return snprintf(dst, size, "%s", xdg_ptr);
  158. else
  159. return snprintf(dst, size, "%s/%s", xdg_ptr, name);
  160. }
  161. #else
  162. char *path_ptr = getenv("HOME");
  163. if (path_ptr == NULL)
  164. bcrash("Could not get $HOME\n");
  165. if (!name || !*name)
  166. return snprintf(dst, size, "%s", path_ptr);
  167. else
  168. return snprintf(dst, size, "%s/.%s", path_ptr, name);
  169. #endif
  170. }
  171. /* should return $HOME/.[name], or when using XDG,
  172. * should return $HOME/.config/[name] as default */
  173. char *os_get_config_path_ptr(const char *name)
  174. {
  175. #ifdef USE_XDG
  176. struct dstr path;
  177. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  178. /* If XDG_CONFIG_HOME is unset,
  179. * we use the default $HOME/.config/[name] instead */
  180. if (xdg_ptr == NULL) {
  181. char *home_ptr = getenv("HOME");
  182. if (home_ptr == NULL)
  183. bcrash("Could not get $HOME\n");
  184. dstr_init_copy(&path, home_ptr);
  185. dstr_cat(&path, "/.config/");
  186. dstr_cat(&path, name);
  187. } else {
  188. dstr_init_copy(&path, xdg_ptr);
  189. dstr_cat(&path, "/");
  190. dstr_cat(&path, name);
  191. }
  192. return path.array;
  193. #else
  194. char *path_ptr = getenv("HOME");
  195. if (path_ptr == NULL)
  196. bcrash("Could not get $HOME\n");
  197. struct dstr path;
  198. dstr_init_copy(&path, path_ptr);
  199. dstr_cat(&path, "/.");
  200. dstr_cat(&path, name);
  201. return path.array;
  202. #endif
  203. }
  204. int os_get_program_data_path(char *dst, size_t size, const char *name)
  205. {
  206. return snprintf(dst, size, "/usr/local/share/%s", !!name ? name : "");
  207. }
  208. char *os_get_program_data_path_ptr(const char *name)
  209. {
  210. size_t len = snprintf(NULL, 0, "/usr/local/share/%s", !!name ? name : "");
  211. char *str = bmalloc(len + 1);
  212. snprintf(str, len + 1, "/usr/local/share/%s", !!name ? name : "");
  213. str[len] = 0;
  214. return str;
  215. }
  216. #endif
  217. bool os_file_exists(const char *path)
  218. {
  219. return access(path, F_OK) == 0;
  220. }
  221. size_t os_get_abs_path(const char *path, char *abspath, size_t size)
  222. {
  223. size_t min_size = size < PATH_MAX ? size : PATH_MAX;
  224. char newpath[PATH_MAX];
  225. int ret;
  226. if (!abspath)
  227. return 0;
  228. if (!realpath(path, newpath))
  229. return 0;
  230. ret = snprintf(abspath, min_size, "%s", newpath);
  231. return ret >= 0 ? ret : 0;
  232. }
  233. char *os_get_abs_path_ptr(const char *path)
  234. {
  235. char *ptr = bmalloc(512);
  236. if (!os_get_abs_path(path, ptr, 512)) {
  237. bfree(ptr);
  238. ptr = NULL;
  239. }
  240. return ptr;
  241. }
  242. struct os_dir {
  243. const char *path;
  244. DIR *dir;
  245. struct dirent *cur_dirent;
  246. struct os_dirent out;
  247. };
  248. os_dir_t *os_opendir(const char *path)
  249. {
  250. struct os_dir *dir;
  251. DIR *dir_val;
  252. dir_val = opendir(path);
  253. if (!dir_val)
  254. return NULL;
  255. dir = bzalloc(sizeof(struct os_dir));
  256. dir->dir = dir_val;
  257. dir->path = path;
  258. return dir;
  259. }
  260. static inline bool is_dir(const char *path)
  261. {
  262. struct stat stat_info;
  263. if (stat(path, &stat_info) == 0)
  264. return !!S_ISDIR(stat_info.st_mode);
  265. blog(LOG_DEBUG, "is_dir: stat for %s failed, errno: %d", path, errno);
  266. return false;
  267. }
  268. struct os_dirent *os_readdir(os_dir_t *dir)
  269. {
  270. struct dstr file_path = {0};
  271. if (!dir) return NULL;
  272. dir->cur_dirent = readdir(dir->dir);
  273. if (!dir->cur_dirent)
  274. return NULL;
  275. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  276. dstr_copy(&file_path, dir->path);
  277. dstr_cat(&file_path, "/");
  278. dstr_cat(&file_path, dir->out.d_name);
  279. dir->out.directory = is_dir(file_path.array);
  280. dstr_free(&file_path);
  281. return &dir->out;
  282. }
  283. void os_closedir(os_dir_t *dir)
  284. {
  285. if (dir) {
  286. closedir(dir->dir);
  287. bfree(dir);
  288. }
  289. }
  290. int64_t os_get_free_space(const char *path)
  291. {
  292. struct statvfs info;
  293. int64_t ret = (int64_t)statvfs(path, &info);
  294. if (ret == 0)
  295. ret = (int64_t)info.f_bsize * (int64_t)info.f_bfree;
  296. return ret;
  297. }
  298. struct posix_glob_info {
  299. struct os_glob_info base;
  300. glob_t gl;
  301. };
  302. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  303. {
  304. struct posix_glob_info pgi;
  305. int ret = glob(pattern, 0, NULL, &pgi.gl);
  306. if (ret == 0) {
  307. DARRAY(struct os_globent) list;
  308. da_init(list);
  309. for (size_t i = 0; i < pgi.gl.gl_pathc; i++) {
  310. struct os_globent ent = {0};
  311. ent.path = pgi.gl.gl_pathv[i];
  312. ent.directory = is_dir(ent.path);
  313. da_push_back(list, &ent);
  314. }
  315. pgi.base.gl_pathc = list.num;
  316. pgi.base.gl_pathv = list.array;
  317. *pglob = bmemdup(&pgi, sizeof(pgi));
  318. } else {
  319. *pglob = NULL;
  320. }
  321. UNUSED_PARAMETER(flags);
  322. return ret;
  323. }
  324. void os_globfree(os_glob_t *pglob)
  325. {
  326. if (pglob) {
  327. struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
  328. globfree(&pgi->gl);
  329. bfree(pgi->base.gl_pathv);
  330. bfree(pgi);
  331. }
  332. }
  333. int os_unlink(const char *path)
  334. {
  335. return unlink(path);
  336. }
  337. int os_rmdir(const char *path)
  338. {
  339. return rmdir(path);
  340. }
  341. int os_mkdir(const char *path)
  342. {
  343. if (mkdir(path, 0755) == 0)
  344. return MKDIR_SUCCESS;
  345. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  346. }
  347. int os_rename(const char *old_path, const char *new_path)
  348. {
  349. return rename(old_path, new_path);
  350. }
  351. int os_safe_replace(const char *target, const char *from, const char *backup)
  352. {
  353. if (backup && os_file_exists(target) && rename(target, backup) != 0)
  354. return -1;
  355. return rename(from, target);
  356. }
  357. #if !defined(__APPLE__)
  358. os_performance_token_t *os_request_high_performance(const char *reason)
  359. {
  360. UNUSED_PARAMETER(reason);
  361. return NULL;
  362. }
  363. void os_end_high_performance(os_performance_token_t *token)
  364. {
  365. UNUSED_PARAMETER(token);
  366. }
  367. #endif
  368. int os_copyfile(const char *file_path_in, const char *file_path_out)
  369. {
  370. FILE *file_out = NULL;
  371. FILE *file_in = NULL;
  372. uint8_t data[4096];
  373. int ret = -1;
  374. size_t size;
  375. if (os_file_exists(file_path_out))
  376. return -1;
  377. file_in = fopen(file_path_in, "rb");
  378. if (!file_in)
  379. return -1;
  380. file_out = fopen(file_path_out, "ab+");
  381. if (!file_out)
  382. goto error;
  383. do {
  384. size = fread(data, 1, sizeof(data), file_in);
  385. if (size)
  386. size = fwrite(data, 1, size, file_out);
  387. } while (size == sizeof(data));
  388. ret = feof(file_in) ? 0 : -1;
  389. error:
  390. if (file_out)
  391. fclose(file_out);
  392. fclose(file_in);
  393. return ret;
  394. }
  395. char *os_getcwd(char *path, size_t size)
  396. {
  397. return getcwd(path, size);
  398. }
  399. int os_chdir(const char *path)
  400. {
  401. return chdir(path);
  402. }
  403. #if !defined(__APPLE__)
  404. #if HAVE_DBUS
  405. struct dbus_sleep_info;
  406. extern struct dbus_sleep_info *dbus_sleep_info_create(void);
  407. extern void dbus_inhibit_sleep(struct dbus_sleep_info *dbus, const char *sleep,
  408. bool active);
  409. extern void dbus_sleep_info_destroy(struct dbus_sleep_info *dbus);
  410. #endif
  411. struct os_inhibit_info {
  412. #if HAVE_DBUS
  413. struct dbus_sleep_info *dbus;
  414. #endif
  415. pthread_t screensaver_thread;
  416. os_event_t *stop_event;
  417. char *reason;
  418. posix_spawnattr_t attr;
  419. bool active;
  420. };
  421. os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  422. {
  423. struct os_inhibit_info *info = bzalloc(sizeof(*info));
  424. sigset_t set;
  425. #if HAVE_DBUS
  426. info->dbus = dbus_sleep_info_create();
  427. #endif
  428. os_event_init(&info->stop_event, OS_EVENT_TYPE_AUTO);
  429. posix_spawnattr_init(&info->attr);
  430. sigemptyset(&set);
  431. posix_spawnattr_setsigmask(&info->attr, &set);
  432. sigaddset(&set, SIGPIPE);
  433. posix_spawnattr_setsigdefault(&info->attr, &set);
  434. posix_spawnattr_setflags(&info->attr,
  435. POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK);
  436. info->reason = bstrdup(reason);
  437. return info;
  438. }
  439. extern char **environ;
  440. static void reset_screensaver(os_inhibit_t *info)
  441. {
  442. char *argv[3] = {(char*)"xdg-screensaver", (char*)"reset", NULL};
  443. pid_t pid;
  444. int err = posix_spawnp(&pid, "xdg-screensaver", NULL, &info->attr,
  445. argv, environ);
  446. if (err == 0) {
  447. int status;
  448. while (waitpid(pid, &status, 0) == -1);
  449. } else {
  450. blog(LOG_WARNING, "Failed to create xdg-screensaver: %d", err);
  451. }
  452. }
  453. static void *screensaver_thread(void *param)
  454. {
  455. os_inhibit_t *info = param;
  456. while (os_event_timedwait(info->stop_event, 30000) == ETIMEDOUT)
  457. reset_screensaver(info);
  458. return NULL;
  459. }
  460. bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  461. {
  462. int ret;
  463. if (!info)
  464. return false;
  465. if (info->active == active)
  466. return false;
  467. #if HAVE_DBUS
  468. if (info->dbus)
  469. dbus_inhibit_sleep(info->dbus, info->reason, active);
  470. #endif
  471. if (!info->stop_event)
  472. return true;
  473. if (active) {
  474. ret = pthread_create(&info->screensaver_thread, NULL,
  475. &screensaver_thread, info);
  476. if (ret < 0) {
  477. blog(LOG_ERROR, "Failed to create screensaver "
  478. "inhibitor thread");
  479. return false;
  480. }
  481. } else {
  482. os_event_signal(info->stop_event);
  483. pthread_join(info->screensaver_thread, NULL);
  484. }
  485. info->active = active;
  486. return true;
  487. }
  488. void os_inhibit_sleep_destroy(os_inhibit_t *info)
  489. {
  490. if (info) {
  491. os_inhibit_sleep_set_active(info, false);
  492. #if HAVE_DBUS
  493. dbus_sleep_info_destroy(info->dbus);
  494. #endif
  495. os_event_destroy(info->stop_event);
  496. posix_spawnattr_destroy(&info->attr);
  497. bfree(info->reason);
  498. bfree(info);
  499. }
  500. }
  501. #endif
  502. void os_breakpoint()
  503. {
  504. raise(SIGTRAP);
  505. }
  506. #ifndef __APPLE__
  507. static int physical_cores = 0;
  508. static int logical_cores = 0;
  509. static bool core_count_initialized = false;
  510. static void os_get_cores_internal(void)
  511. {
  512. if (core_count_initialized)
  513. return;
  514. core_count_initialized = true;
  515. logical_cores = sysconf(_SC_NPROCESSORS_ONLN);
  516. #if defined(__linux__)
  517. int physical_id = -1;
  518. int last_physical_id = -1;
  519. int core_count = 0;
  520. char *line = NULL;
  521. size_t linecap = 0;
  522. FILE *fp;
  523. struct dstr proc_phys_id;
  524. struct dstr proc_phys_ids;
  525. fp = fopen("/proc/cpuinfo", "r");
  526. if (!fp)
  527. return;
  528. dstr_init(&proc_phys_id);
  529. dstr_init(&proc_phys_ids);
  530. while (getline(&line, &linecap, fp) != -1) {
  531. if (!strncmp(line, "physical id", 11)) {
  532. char *start = strchr(line, ':');
  533. if (!start || *(++start) == '\0')
  534. continue;
  535. physical_id = atoi(start);
  536. dstr_free(&proc_phys_id);
  537. dstr_init(&proc_phys_id);
  538. dstr_catf(&proc_phys_id, "%d", physical_id);
  539. }
  540. if (!strncmp(line, "cpu cores", 9)) {
  541. char *start = strchr(line, ':');
  542. if (!start || *(++start) == '\0')
  543. continue;
  544. if (dstr_is_empty(&proc_phys_ids) ||
  545. (!dstr_is_empty(&proc_phys_ids) &&
  546. !dstr_find(&proc_phys_ids, proc_phys_id.array))) {
  547. dstr_cat_dstr(&proc_phys_ids, &proc_phys_id);
  548. dstr_cat(&proc_phys_ids, " ");
  549. core_count += atoi(start);
  550. }
  551. }
  552. if (*line == '\n' && physical_id != last_physical_id) {
  553. last_physical_id = physical_id;
  554. }
  555. }
  556. if (core_count == 0)
  557. physical_cores = logical_cores;
  558. else
  559. physical_cores = core_count;
  560. fclose(fp);
  561. dstr_free(&proc_phys_ids);
  562. dstr_free(&proc_phys_id);
  563. free(line);
  564. #elif defined(__FreeBSD__)
  565. char *text = os_quick_read_utf8_file("/var/run/dmesg.boot");
  566. char *core_count = text;
  567. int packages = 0;
  568. int cores = 0;
  569. struct dstr proc_packages;
  570. struct dstr proc_cores;
  571. dstr_init(&proc_packages);
  572. dstr_init(&proc_cores);
  573. if (!text || !*text) {
  574. physical_cores = logical_cores;
  575. return;
  576. }
  577. core_count = strstr(core_count, "\nFreeBSD/SMP: ");
  578. if (!core_count)
  579. goto FreeBSD_cores_cleanup;
  580. core_count++;
  581. core_count = strstr(core_count, "\nFreeBSD/SMP: ");
  582. if (!core_count)
  583. goto FreeBSD_cores_cleanup;
  584. core_count = strstr(core_count, ": ");
  585. core_count += 2;
  586. size_t len = strcspn(core_count, " ");
  587. dstr_ncopy(&proc_packages, core_count, len);
  588. core_count = strstr(core_count, "package(s) x ");
  589. if (!core_count)
  590. goto FreeBSD_cores_cleanup;
  591. core_count += 13;
  592. len = strcspn(core_count, " ");
  593. dstr_ncopy(&proc_cores, core_count, len);
  594. FreeBSD_cores_cleanup:
  595. if (!dstr_is_empty(&proc_packages))
  596. packages = atoi(proc_packages.array);
  597. if (!dstr_is_empty(&proc_cores))
  598. cores = atoi(proc_cores.array);
  599. if (packages == 0)
  600. physical_cores = logical_cores;
  601. else if (cores == 0)
  602. physical_cores = packages;
  603. else
  604. physical_cores = packages * cores;
  605. dstr_free(&proc_cores);
  606. dstr_free(&proc_packages);
  607. bfree(text);
  608. #else
  609. physical_cores = logical_cores;
  610. #endif
  611. }
  612. int os_get_physical_cores(void)
  613. {
  614. if (!core_count_initialized)
  615. os_get_cores_internal();
  616. return physical_cores;
  617. }
  618. int os_get_logical_cores(void)
  619. {
  620. if (!core_count_initialized)
  621. os_get_cores_internal();
  622. return logical_cores;
  623. }
  624. #endif
  625. uint64_t os_get_free_disk_space(const char *dir)
  626. {
  627. struct statvfs info;
  628. if (statvfs(dir, &info) != 0)
  629. return 0;
  630. return (uint64_t)info.f_frsize * (uint64_t)info.f_bavail;
  631. }