platform-nix.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <errno.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <dirent.h>
  20. #include <stdlib.h>
  21. #include <dlfcn.h>
  22. #include <unistd.h>
  23. #include <time.h>
  24. #if !defined(__APPLE__)
  25. #include <sys/times.h>
  26. #include <sys/vtimes.h>
  27. #endif
  28. #include "dstr.h"
  29. #include "platform.h"
  30. void *os_dlopen(const char *path)
  31. {
  32. struct dstr dylib_name;
  33. if (!path)
  34. return NULL;
  35. dstr_init_copy(&dylib_name, path);
  36. if (!dstr_find(&dylib_name, ".so"))
  37. dstr_cat(&dylib_name, ".so");
  38. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  39. if (!res)
  40. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  41. path, dylib_name.array, dlerror());
  42. dstr_free(&dylib_name);
  43. return res;
  44. }
  45. void *os_dlsym(void *module, const char *func)
  46. {
  47. return dlsym(module, func);
  48. }
  49. void os_dlclose(void *module)
  50. {
  51. dlclose(module);
  52. }
  53. #if !defined(__APPLE__)
  54. struct os_cpu_usage_info {
  55. clock_t last_cpu_time, last_sys_time, last_user_time;
  56. int core_count;
  57. };
  58. os_cpu_usage_info_t os_cpu_usage_info_start(void)
  59. {
  60. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  61. struct tms time_sample;
  62. info->last_cpu_time = times(&time_sample);
  63. info->last_sys_time = time_sample.tms_stime;
  64. info->last_user_time = time_sample.tms_utime;
  65. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  66. return info;
  67. }
  68. double os_cpu_usage_info_query(os_cpu_usage_info_t info)
  69. {
  70. struct tms time_sample;
  71. clock_t cur_cpu_time;
  72. double percent;
  73. if (!info)
  74. return 0.0;
  75. cur_cpu_time = times(&time_sample);
  76. if (cur_cpu_time <= info->last_cpu_time ||
  77. time_sample.tms_stime < info->last_sys_time ||
  78. time_sample.tms_utime < info->last_user_time)
  79. return 0.0;
  80. percent = (double)(time_sample.tms_stime - info->last_sys_time +
  81. (time_sample.tms_utime - info->last_user_time));
  82. percent /= (double)(cur_cpu_time - info->last_cpu_time);
  83. percent /= (double)info->core_count;
  84. info->last_cpu_time = cur_cpu_time;
  85. info->last_sys_time = time_sample.tms_stime;
  86. info->last_user_time = time_sample.tms_utime;
  87. return percent * 100.0;
  88. }
  89. void os_cpu_usage_info_destroy(os_cpu_usage_info_t info)
  90. {
  91. if (info)
  92. bfree(info);
  93. }
  94. #endif
  95. bool os_sleepto_ns(uint64_t time_target)
  96. {
  97. uint64_t current = os_gettime_ns();
  98. if (time_target < current)
  99. return false;
  100. time_target -= current;
  101. struct timespec req, remain;
  102. memset(&req, 0, sizeof(req));
  103. memset(&remain, 0, sizeof(remain));
  104. req.tv_sec = time_target/1000000000;
  105. req.tv_nsec = time_target%1000000000;
  106. while (nanosleep(&req, &remain)) {
  107. req = remain;
  108. memset(&remain, 0, sizeof(remain));
  109. }
  110. return true;
  111. }
  112. void os_sleep_ms(uint32_t duration)
  113. {
  114. usleep(duration*1000);
  115. }
  116. #if !defined(__APPLE__)
  117. uint64_t os_gettime_ns(void)
  118. {
  119. struct timespec ts;
  120. clock_gettime(CLOCK_MONOTONIC, &ts);
  121. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  122. }
  123. /* should return $HOME/.[name] */
  124. char *os_get_config_path(const char *name)
  125. {
  126. char *path_ptr = getenv("HOME");
  127. if (path_ptr == NULL)
  128. bcrash("Could not get $HOME\n");
  129. struct dstr path;
  130. dstr_init_copy(&path, path_ptr);
  131. dstr_cat(&path, "/.");
  132. dstr_cat(&path, name);
  133. return path.array;
  134. }
  135. #endif
  136. bool os_file_exists(const char *path)
  137. {
  138. return access(path, F_OK) == 0;
  139. }
  140. struct os_dir {
  141. const char *path;
  142. DIR *dir;
  143. struct dirent *cur_dirent;
  144. struct os_dirent out;
  145. };
  146. os_dir_t os_opendir(const char *path)
  147. {
  148. struct os_dir *dir;
  149. DIR *dir_val;
  150. dir_val = opendir(path);
  151. if (!dir_val)
  152. return NULL;
  153. dir = bzalloc(sizeof(struct os_dir));
  154. dir->dir = dir_val;
  155. dir->path = path;
  156. return dir;
  157. }
  158. struct os_dirent *os_readdir(os_dir_t dir)
  159. {
  160. struct dstr file_path = {0};
  161. struct stat stat_info;
  162. if (!dir) return NULL;
  163. dir->cur_dirent = readdir(dir->dir);
  164. if (!dir->cur_dirent)
  165. return NULL;
  166. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  167. dstr_copy(&file_path, dir->path);
  168. dstr_cat(&file_path, "/");
  169. dstr_cat(&file_path, dir->out.d_name);
  170. if (stat(file_path.array, &stat_info) == 0)
  171. dir->out.directory = !!S_ISDIR(stat_info.st_mode);
  172. else
  173. blog(LOG_DEBUG, FILE_LINE "stat for %s failed, errno: %d",
  174. file_path.array, errno);
  175. dstr_free(&file_path);
  176. return &dir->out;
  177. }
  178. void os_closedir(os_dir_t dir)
  179. {
  180. if (dir) {
  181. closedir(dir->dir);
  182. bfree(dir);
  183. }
  184. }
  185. int os_unlink(const char *path)
  186. {
  187. return unlink(path);
  188. }
  189. int os_mkdir(const char *path)
  190. {
  191. if (mkdir(path, 0777) == 0)
  192. return MKDIR_SUCCESS;
  193. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  194. }