platform-nix.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. #ifdef __FreeBSD__
  34. #include <sys/param.h>
  35. #include <sys/queue.h>
  36. #include <sys/socket.h>
  37. #include <sys/user.h>
  38. #include <unistd.h>
  39. #include <libprocstat.h>
  40. #else
  41. #include <sys/resource.h>
  42. #endif
  43. #include <spawn.h>
  44. #endif
  45. #include "darray.h"
  46. #include "dstr.h"
  47. #include "platform.h"
  48. #include "threading.h"
  49. void *os_dlopen(const char *path)
  50. {
  51. struct dstr dylib_name;
  52. if (!path)
  53. return NULL;
  54. dstr_init_copy(&dylib_name, path);
  55. #ifdef __APPLE__
  56. if (!dstr_find(&dylib_name, ".so") && !dstr_find(&dylib_name, ".dylib"))
  57. #else
  58. if (!dstr_find(&dylib_name, ".so"))
  59. #endif
  60. dstr_cat(&dylib_name, ".so");
  61. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  62. if (!res)
  63. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  64. path, dylib_name.array, dlerror());
  65. dstr_free(&dylib_name);
  66. return res;
  67. }
  68. void *os_dlsym(void *module, const char *func)
  69. {
  70. return dlsym(module, func);
  71. }
  72. void os_dlclose(void *module)
  73. {
  74. if (module)
  75. dlclose(module);
  76. }
  77. #if !defined(__APPLE__)
  78. struct os_cpu_usage_info {
  79. clock_t last_cpu_time, last_sys_time, last_user_time;
  80. int core_count;
  81. };
  82. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  83. {
  84. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  85. struct tms time_sample;
  86. info->last_cpu_time = times(&time_sample);
  87. info->last_sys_time = time_sample.tms_stime;
  88. info->last_user_time = time_sample.tms_utime;
  89. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  90. return info;
  91. }
  92. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  93. {
  94. struct tms time_sample;
  95. clock_t cur_cpu_time;
  96. double percent;
  97. if (!info)
  98. return 0.0;
  99. cur_cpu_time = times(&time_sample);
  100. if (cur_cpu_time <= info->last_cpu_time ||
  101. time_sample.tms_stime < info->last_sys_time ||
  102. time_sample.tms_utime < info->last_user_time)
  103. return 0.0;
  104. percent = (double)(time_sample.tms_stime - info->last_sys_time +
  105. (time_sample.tms_utime - info->last_user_time));
  106. percent /= (double)(cur_cpu_time - info->last_cpu_time);
  107. percent /= (double)info->core_count;
  108. info->last_cpu_time = cur_cpu_time;
  109. info->last_sys_time = time_sample.tms_stime;
  110. info->last_user_time = time_sample.tms_utime;
  111. return percent * 100.0;
  112. }
  113. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  114. {
  115. if (info)
  116. bfree(info);
  117. }
  118. #endif
  119. bool os_sleepto_ns(uint64_t time_target)
  120. {
  121. uint64_t current = os_gettime_ns();
  122. if (time_target < current)
  123. return false;
  124. time_target -= current;
  125. struct timespec req, remain;
  126. memset(&req, 0, sizeof(req));
  127. memset(&remain, 0, sizeof(remain));
  128. req.tv_sec = time_target/1000000000;
  129. req.tv_nsec = time_target%1000000000;
  130. while (nanosleep(&req, &remain)) {
  131. req = remain;
  132. memset(&remain, 0, sizeof(remain));
  133. }
  134. return true;
  135. }
  136. void os_sleep_ms(uint32_t duration)
  137. {
  138. usleep(duration*1000);
  139. }
  140. #if !defined(__APPLE__)
  141. uint64_t os_gettime_ns(void)
  142. {
  143. struct timespec ts;
  144. clock_gettime(CLOCK_MONOTONIC, &ts);
  145. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  146. }
  147. /* should return $HOME/.[name], or when using XDG,
  148. * should return $HOME/.config/[name] as default */
  149. int os_get_config_path(char *dst, size_t size, const char *name)
  150. {
  151. #ifdef USE_XDG
  152. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  153. // If XDG_CONFIG_HOME is unset,
  154. // we use the default $HOME/.config/[name] instead
  155. if (xdg_ptr == NULL) {
  156. char *home_ptr = getenv("HOME");
  157. if (home_ptr == NULL)
  158. bcrash("Could not get $HOME\n");
  159. if (!name || !*name) {
  160. return snprintf(dst, size, "%s/.config", home_ptr);
  161. } else {
  162. return snprintf(dst, size, "%s/.config/%s", home_ptr,
  163. name);
  164. }
  165. } else {
  166. if (!name || !*name)
  167. return snprintf(dst, size, "%s", xdg_ptr);
  168. else
  169. return snprintf(dst, size, "%s/%s", xdg_ptr, name);
  170. }
  171. #else
  172. char *path_ptr = getenv("HOME");
  173. if (path_ptr == NULL)
  174. bcrash("Could not get $HOME\n");
  175. if (!name || !*name)
  176. return snprintf(dst, size, "%s", path_ptr);
  177. else
  178. return snprintf(dst, size, "%s/.%s", path_ptr, name);
  179. #endif
  180. }
  181. /* should return $HOME/.[name], or when using XDG,
  182. * should return $HOME/.config/[name] as default */
  183. char *os_get_config_path_ptr(const char *name)
  184. {
  185. #ifdef USE_XDG
  186. struct dstr path;
  187. char *xdg_ptr = getenv("XDG_CONFIG_HOME");
  188. /* If XDG_CONFIG_HOME is unset,
  189. * we use the default $HOME/.config/[name] instead */
  190. if (xdg_ptr == NULL) {
  191. char *home_ptr = getenv("HOME");
  192. if (home_ptr == NULL)
  193. bcrash("Could not get $HOME\n");
  194. dstr_init_copy(&path, home_ptr);
  195. dstr_cat(&path, "/.config/");
  196. dstr_cat(&path, name);
  197. } else {
  198. dstr_init_copy(&path, xdg_ptr);
  199. dstr_cat(&path, "/");
  200. dstr_cat(&path, name);
  201. }
  202. return path.array;
  203. #else
  204. char *path_ptr = getenv("HOME");
  205. if (path_ptr == NULL)
  206. bcrash("Could not get $HOME\n");
  207. struct dstr path;
  208. dstr_init_copy(&path, path_ptr);
  209. dstr_cat(&path, "/.");
  210. dstr_cat(&path, name);
  211. return path.array;
  212. #endif
  213. }
  214. int os_get_program_data_path(char *dst, size_t size, const char *name)
  215. {
  216. return snprintf(dst, size, "/usr/local/share/%s", !!name ? name : "");
  217. }
  218. char *os_get_program_data_path_ptr(const char *name)
  219. {
  220. size_t len = snprintf(NULL, 0, "/usr/local/share/%s", !!name ? name : "");
  221. char *str = bmalloc(len + 1);
  222. snprintf(str, len + 1, "/usr/local/share/%s", !!name ? name : "");
  223. str[len] = 0;
  224. return str;
  225. }
  226. #endif
  227. bool os_file_exists(const char *path)
  228. {
  229. return access(path, F_OK) == 0;
  230. }
  231. size_t os_get_abs_path(const char *path, char *abspath, size_t size)
  232. {
  233. size_t min_size = size < PATH_MAX ? size : PATH_MAX;
  234. char newpath[PATH_MAX];
  235. int ret;
  236. if (!abspath)
  237. return 0;
  238. if (!realpath(path, newpath))
  239. return 0;
  240. ret = snprintf(abspath, min_size, "%s", newpath);
  241. return ret >= 0 ? ret : 0;
  242. }
  243. char *os_get_abs_path_ptr(const char *path)
  244. {
  245. char *ptr = bmalloc(512);
  246. if (!os_get_abs_path(path, ptr, 512)) {
  247. bfree(ptr);
  248. ptr = NULL;
  249. }
  250. return ptr;
  251. }
  252. struct os_dir {
  253. const char *path;
  254. DIR *dir;
  255. struct dirent *cur_dirent;
  256. struct os_dirent out;
  257. };
  258. os_dir_t *os_opendir(const char *path)
  259. {
  260. struct os_dir *dir;
  261. DIR *dir_val;
  262. dir_val = opendir(path);
  263. if (!dir_val)
  264. return NULL;
  265. dir = bzalloc(sizeof(struct os_dir));
  266. dir->dir = dir_val;
  267. dir->path = path;
  268. return dir;
  269. }
  270. static inline bool is_dir(const char *path)
  271. {
  272. struct stat stat_info;
  273. if (stat(path, &stat_info) == 0)
  274. return !!S_ISDIR(stat_info.st_mode);
  275. blog(LOG_DEBUG, "is_dir: stat for %s failed, errno: %d", path, errno);
  276. return false;
  277. }
  278. struct os_dirent *os_readdir(os_dir_t *dir)
  279. {
  280. struct dstr file_path = {0};
  281. if (!dir) return NULL;
  282. dir->cur_dirent = readdir(dir->dir);
  283. if (!dir->cur_dirent)
  284. return NULL;
  285. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  286. dstr_copy(&file_path, dir->path);
  287. dstr_cat(&file_path, "/");
  288. dstr_cat(&file_path, dir->out.d_name);
  289. dir->out.directory = is_dir(file_path.array);
  290. dstr_free(&file_path);
  291. return &dir->out;
  292. }
  293. void os_closedir(os_dir_t *dir)
  294. {
  295. if (dir) {
  296. closedir(dir->dir);
  297. bfree(dir);
  298. }
  299. }
  300. int64_t os_get_free_space(const char *path)
  301. {
  302. struct statvfs info;
  303. int64_t ret = (int64_t)statvfs(path, &info);
  304. if (ret == 0)
  305. ret = (int64_t)info.f_bsize * (int64_t)info.f_bfree;
  306. return ret;
  307. }
  308. struct posix_glob_info {
  309. struct os_glob_info base;
  310. glob_t gl;
  311. };
  312. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  313. {
  314. struct posix_glob_info pgi;
  315. int ret = glob(pattern, 0, NULL, &pgi.gl);
  316. if (ret == 0) {
  317. DARRAY(struct os_globent) list;
  318. da_init(list);
  319. for (size_t i = 0; i < pgi.gl.gl_pathc; i++) {
  320. struct os_globent ent = {0};
  321. ent.path = pgi.gl.gl_pathv[i];
  322. ent.directory = is_dir(ent.path);
  323. da_push_back(list, &ent);
  324. }
  325. pgi.base.gl_pathc = list.num;
  326. pgi.base.gl_pathv = list.array;
  327. *pglob = bmemdup(&pgi, sizeof(pgi));
  328. } else {
  329. *pglob = NULL;
  330. }
  331. UNUSED_PARAMETER(flags);
  332. return ret;
  333. }
  334. void os_globfree(os_glob_t *pglob)
  335. {
  336. if (pglob) {
  337. struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
  338. globfree(&pgi->gl);
  339. bfree(pgi->base.gl_pathv);
  340. bfree(pgi);
  341. }
  342. }
  343. int os_unlink(const char *path)
  344. {
  345. return unlink(path);
  346. }
  347. int os_rmdir(const char *path)
  348. {
  349. return rmdir(path);
  350. }
  351. int os_mkdir(const char *path)
  352. {
  353. if (mkdir(path, 0755) == 0)
  354. return MKDIR_SUCCESS;
  355. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  356. }
  357. int os_rename(const char *old_path, const char *new_path)
  358. {
  359. return rename(old_path, new_path);
  360. }
  361. int os_safe_replace(const char *target, const char *from, const char *backup)
  362. {
  363. if (backup && os_file_exists(target) && rename(target, backup) != 0)
  364. return -1;
  365. return rename(from, target);
  366. }
  367. #if !defined(__APPLE__)
  368. os_performance_token_t *os_request_high_performance(const char *reason)
  369. {
  370. UNUSED_PARAMETER(reason);
  371. return NULL;
  372. }
  373. void os_end_high_performance(os_performance_token_t *token)
  374. {
  375. UNUSED_PARAMETER(token);
  376. }
  377. #endif
  378. int os_copyfile(const char *file_path_in, const char *file_path_out)
  379. {
  380. FILE *file_out = NULL;
  381. FILE *file_in = NULL;
  382. uint8_t data[4096];
  383. int ret = -1;
  384. size_t size;
  385. if (os_file_exists(file_path_out))
  386. return -1;
  387. file_in = fopen(file_path_in, "rb");
  388. if (!file_in)
  389. return -1;
  390. file_out = fopen(file_path_out, "ab+");
  391. if (!file_out)
  392. goto error;
  393. do {
  394. size = fread(data, 1, sizeof(data), file_in);
  395. if (size)
  396. size = fwrite(data, 1, size, file_out);
  397. } while (size == sizeof(data));
  398. ret = feof(file_in) ? 0 : -1;
  399. error:
  400. if (file_out)
  401. fclose(file_out);
  402. fclose(file_in);
  403. return ret;
  404. }
  405. char *os_getcwd(char *path, size_t size)
  406. {
  407. return getcwd(path, size);
  408. }
  409. int os_chdir(const char *path)
  410. {
  411. return chdir(path);
  412. }
  413. #if !defined(__APPLE__)
  414. #if HAVE_DBUS
  415. struct dbus_sleep_info;
  416. extern struct dbus_sleep_info *dbus_sleep_info_create(void);
  417. extern void dbus_inhibit_sleep(struct dbus_sleep_info *dbus, const char *sleep,
  418. bool active);
  419. extern void dbus_sleep_info_destroy(struct dbus_sleep_info *dbus);
  420. #endif
  421. struct os_inhibit_info {
  422. #if HAVE_DBUS
  423. struct dbus_sleep_info *dbus;
  424. #endif
  425. pthread_t screensaver_thread;
  426. os_event_t *stop_event;
  427. char *reason;
  428. posix_spawnattr_t attr;
  429. bool active;
  430. };
  431. os_inhibit_t *os_inhibit_sleep_create(const char *reason)
  432. {
  433. struct os_inhibit_info *info = bzalloc(sizeof(*info));
  434. sigset_t set;
  435. #if HAVE_DBUS
  436. info->dbus = dbus_sleep_info_create();
  437. #endif
  438. os_event_init(&info->stop_event, OS_EVENT_TYPE_AUTO);
  439. posix_spawnattr_init(&info->attr);
  440. sigemptyset(&set);
  441. posix_spawnattr_setsigmask(&info->attr, &set);
  442. sigaddset(&set, SIGPIPE);
  443. posix_spawnattr_setsigdefault(&info->attr, &set);
  444. posix_spawnattr_setflags(&info->attr,
  445. POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK);
  446. info->reason = bstrdup(reason);
  447. return info;
  448. }
  449. extern char **environ;
  450. static void reset_screensaver(os_inhibit_t *info)
  451. {
  452. char *argv[3] = {(char*)"xdg-screensaver", (char*)"reset", NULL};
  453. pid_t pid;
  454. int err = posix_spawnp(&pid, "xdg-screensaver", NULL, &info->attr,
  455. argv, environ);
  456. if (err == 0) {
  457. int status;
  458. while (waitpid(pid, &status, 0) == -1);
  459. } else {
  460. blog(LOG_WARNING, "Failed to create xdg-screensaver: %d", err);
  461. }
  462. }
  463. static void *screensaver_thread(void *param)
  464. {
  465. os_inhibit_t *info = param;
  466. while (os_event_timedwait(info->stop_event, 30000) == ETIMEDOUT)
  467. reset_screensaver(info);
  468. return NULL;
  469. }
  470. bool os_inhibit_sleep_set_active(os_inhibit_t *info, bool active)
  471. {
  472. int ret;
  473. if (!info)
  474. return false;
  475. if (info->active == active)
  476. return false;
  477. #if HAVE_DBUS
  478. if (info->dbus)
  479. dbus_inhibit_sleep(info->dbus, info->reason, active);
  480. #endif
  481. if (!info->stop_event)
  482. return true;
  483. if (active) {
  484. ret = pthread_create(&info->screensaver_thread, NULL,
  485. &screensaver_thread, info);
  486. if (ret < 0) {
  487. blog(LOG_ERROR, "Failed to create screensaver "
  488. "inhibitor thread");
  489. return false;
  490. }
  491. } else {
  492. os_event_signal(info->stop_event);
  493. pthread_join(info->screensaver_thread, NULL);
  494. }
  495. info->active = active;
  496. return true;
  497. }
  498. void os_inhibit_sleep_destroy(os_inhibit_t *info)
  499. {
  500. if (info) {
  501. os_inhibit_sleep_set_active(info, false);
  502. #if HAVE_DBUS
  503. dbus_sleep_info_destroy(info->dbus);
  504. #endif
  505. os_event_destroy(info->stop_event);
  506. posix_spawnattr_destroy(&info->attr);
  507. bfree(info->reason);
  508. bfree(info);
  509. }
  510. }
  511. #endif
  512. void os_breakpoint()
  513. {
  514. raise(SIGTRAP);
  515. }
  516. #ifndef __APPLE__
  517. static int physical_cores = 0;
  518. static int logical_cores = 0;
  519. static bool core_count_initialized = false;
  520. /* return sysconf(_SC_NPROCESSORS_ONLN); */
  521. static void os_get_cores_internal(void)
  522. {
  523. if (core_count_initialized)
  524. return;
  525. core_count_initialized = true;
  526. logical_cores = sysconf(_SC_NPROCESSORS_ONLN);
  527. #ifndef __linux__
  528. physical_cores = logical_cores;
  529. #else
  530. char *text = os_quick_read_utf8_file("/proc/cpuinfo");
  531. char *core_id = text;
  532. if (!text || !*text) {
  533. physical_cores = logical_cores;
  534. return;
  535. }
  536. for (;;) {
  537. core_id = strstr(core_id, "\ncore id");
  538. if (!core_id)
  539. break;
  540. physical_cores++;
  541. core_id++;
  542. }
  543. if (physical_cores == 0)
  544. physical_cores = logical_cores;
  545. bfree(text);
  546. #endif
  547. }
  548. int os_get_physical_cores(void)
  549. {
  550. if (!core_count_initialized)
  551. os_get_cores_internal();
  552. return physical_cores;
  553. }
  554. int os_get_logical_cores(void)
  555. {
  556. if (!core_count_initialized)
  557. os_get_cores_internal();
  558. return logical_cores;
  559. }
  560. #ifdef __FreeBSD__
  561. uint64_t os_get_sys_free_size(void)
  562. {
  563. uint64_t mem_free = 0;
  564. size_t length = sizeof(mem_free);
  565. if (sysctlbyname("vm.stats.vm.v_free_count", &mem_free, &length,
  566. NULL, 0) < 0)
  567. return 0;
  568. return mem_free;
  569. }
  570. static inline bool os_get_proc_memory_usage_internal(struct kinfo_proc *kinfo)
  571. {
  572. int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
  573. size_t length = sizeof(*kinfo);
  574. if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), kinfo, &length,
  575. NULL, 0) < 0)
  576. return false;
  577. return true;
  578. }
  579. bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
  580. {
  581. struct kinfo_proc kinfo;
  582. if (!os_get_proc_memory_usage_internal(&kinfo))
  583. return false;
  584. usage->resident_size =
  585. (uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
  586. usage->virtual_size = (uint64_t)kinfo.ki_size;
  587. return true;
  588. }
  589. uint64_t os_get_proc_resident_size(void)
  590. {
  591. struct kinfo_proc kinfo;
  592. if (!os_get_proc_memory_usage_internal(&kinfo))
  593. return 0;
  594. return (uint64_t)kinfo.ki_rssize * sysconf(_SC_PAGESIZE);
  595. }
  596. uint64_t os_get_proc_virtual_size(void)
  597. {
  598. struct kinfo_proc kinfo;
  599. if (!os_get_proc_memory_usage_internal(&kinfo))
  600. return 0;
  601. return (uint64_t)kinfo.ki_size;
  602. }
  603. #else
  604. uint64_t os_get_sys_free_size(void) {return 0;}
  605. typedef struct
  606. {
  607. unsigned long virtual_size;
  608. unsigned long resident_size;
  609. unsigned long share_pages;
  610. unsigned long text;
  611. unsigned long library;
  612. unsigned long data;
  613. unsigned long dirty_pages;
  614. } statm_t;
  615. static inline bool os_get_proc_memory_usage_internal(statm_t *statm)
  616. {
  617. const char *statm_path = "/proc/self/statm";
  618. FILE *f = fopen(statm_path, "r");
  619. if (!f)
  620. return false;
  621. if (fscanf(f, "%ld %ld %ld %ld %ld %ld %ld",
  622. &statm->virtual_size,
  623. &statm->resident_size,
  624. &statm->share_pages,
  625. &statm->text,
  626. &statm->library,
  627. &statm->data,
  628. &statm->dirty_pages) != 7) {
  629. fclose(f);
  630. return false;
  631. }
  632. fclose(f);
  633. return true;
  634. }
  635. bool os_get_proc_memory_usage(os_proc_memory_usage_t *usage)
  636. {
  637. statm_t statm = {};
  638. if (!os_get_proc_memory_usage_internal(&statm))
  639. return false;
  640. usage->resident_size = statm.resident_size;
  641. usage->virtual_size = statm.virtual_size;
  642. return true;
  643. }
  644. uint64_t os_get_proc_resident_size(void)
  645. {
  646. statm_t statm = {};
  647. if (!os_get_proc_memory_usage_internal(&statm))
  648. return 0;
  649. return (uint64_t)statm.resident_size;
  650. }
  651. uint64_t os_get_proc_virtual_size(void)
  652. {
  653. statm_t statm = {};
  654. if (!os_get_proc_memory_usage_internal(&statm))
  655. return 0;
  656. return (uint64_t)statm.virtual_size;
  657. }
  658. #endif
  659. #endif
  660. uint64_t os_get_free_disk_space(const char *dir)
  661. {
  662. struct statvfs info;
  663. if (statvfs(dir, &info) != 0)
  664. return 0;
  665. return (uint64_t)info.f_frsize * (uint64_t)info.f_bavail;
  666. }