platform-nix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <glob.h>
  24. #include <time.h>
  25. #if !defined(__APPLE__)
  26. #include <sys/times.h>
  27. #include <sys/vtimes.h>
  28. #endif
  29. #include "darray.h"
  30. #include "dstr.h"
  31. #include "platform.h"
  32. void *os_dlopen(const char *path)
  33. {
  34. struct dstr dylib_name;
  35. if (!path)
  36. return NULL;
  37. dstr_init_copy(&dylib_name, path);
  38. if (!dstr_find(&dylib_name, ".so"))
  39. dstr_cat(&dylib_name, ".so");
  40. void *res = dlopen(dylib_name.array, RTLD_LAZY);
  41. if (!res)
  42. blog(LOG_ERROR, "os_dlopen(%s->%s): %s\n",
  43. path, dylib_name.array, dlerror());
  44. dstr_free(&dylib_name);
  45. return res;
  46. }
  47. void *os_dlsym(void *module, const char *func)
  48. {
  49. return dlsym(module, func);
  50. }
  51. void os_dlclose(void *module)
  52. {
  53. dlclose(module);
  54. }
  55. #if !defined(__APPLE__)
  56. struct os_cpu_usage_info {
  57. clock_t last_cpu_time, last_sys_time, last_user_time;
  58. int core_count;
  59. };
  60. os_cpu_usage_info_t *os_cpu_usage_info_start(void)
  61. {
  62. struct os_cpu_usage_info *info = bmalloc(sizeof(*info));
  63. struct tms time_sample;
  64. info->last_cpu_time = times(&time_sample);
  65. info->last_sys_time = time_sample.tms_stime;
  66. info->last_user_time = time_sample.tms_utime;
  67. info->core_count = sysconf(_SC_NPROCESSORS_ONLN);
  68. return info;
  69. }
  70. double os_cpu_usage_info_query(os_cpu_usage_info_t *info)
  71. {
  72. struct tms time_sample;
  73. clock_t cur_cpu_time;
  74. double percent;
  75. if (!info)
  76. return 0.0;
  77. cur_cpu_time = times(&time_sample);
  78. if (cur_cpu_time <= info->last_cpu_time ||
  79. time_sample.tms_stime < info->last_sys_time ||
  80. time_sample.tms_utime < info->last_user_time)
  81. return 0.0;
  82. percent = (double)(time_sample.tms_stime - info->last_sys_time +
  83. (time_sample.tms_utime - info->last_user_time));
  84. percent /= (double)(cur_cpu_time - info->last_cpu_time);
  85. percent /= (double)info->core_count;
  86. info->last_cpu_time = cur_cpu_time;
  87. info->last_sys_time = time_sample.tms_stime;
  88. info->last_user_time = time_sample.tms_utime;
  89. return percent * 100.0;
  90. }
  91. void os_cpu_usage_info_destroy(os_cpu_usage_info_t *info)
  92. {
  93. if (info)
  94. bfree(info);
  95. }
  96. #endif
  97. bool os_sleepto_ns(uint64_t time_target)
  98. {
  99. uint64_t current = os_gettime_ns();
  100. if (time_target < current)
  101. return false;
  102. time_target -= current;
  103. struct timespec req, remain;
  104. memset(&req, 0, sizeof(req));
  105. memset(&remain, 0, sizeof(remain));
  106. req.tv_sec = time_target/1000000000;
  107. req.tv_nsec = time_target%1000000000;
  108. while (nanosleep(&req, &remain)) {
  109. req = remain;
  110. memset(&remain, 0, sizeof(remain));
  111. }
  112. return true;
  113. }
  114. void os_sleep_ms(uint32_t duration)
  115. {
  116. usleep(duration*1000);
  117. }
  118. #if !defined(__APPLE__)
  119. uint64_t os_gettime_ns(void)
  120. {
  121. struct timespec ts;
  122. clock_gettime(CLOCK_MONOTONIC, &ts);
  123. return ((uint64_t) ts.tv_sec * 1000000000ULL + (uint64_t) ts.tv_nsec);
  124. }
  125. /* should return $HOME/.[name] */
  126. char *os_get_config_path(const char *name)
  127. {
  128. char *path_ptr = getenv("HOME");
  129. if (path_ptr == NULL)
  130. bcrash("Could not get $HOME\n");
  131. struct dstr path;
  132. dstr_init_copy(&path, path_ptr);
  133. dstr_cat(&path, "/.");
  134. dstr_cat(&path, name);
  135. return path.array;
  136. }
  137. #endif
  138. bool os_file_exists(const char *path)
  139. {
  140. return access(path, F_OK) == 0;
  141. }
  142. struct os_dir {
  143. const char *path;
  144. DIR *dir;
  145. struct dirent *cur_dirent;
  146. struct os_dirent out;
  147. };
  148. os_dir_t *os_opendir(const char *path)
  149. {
  150. struct os_dir *dir;
  151. DIR *dir_val;
  152. dir_val = opendir(path);
  153. if (!dir_val)
  154. return NULL;
  155. dir = bzalloc(sizeof(struct os_dir));
  156. dir->dir = dir_val;
  157. dir->path = path;
  158. return dir;
  159. }
  160. static inline bool is_dir(const char *path)
  161. {
  162. struct stat stat_info;
  163. if (stat(path, &stat_info) == 0)
  164. return !!S_ISDIR(stat_info.st_mode);
  165. blog(LOG_DEBUG, "is_dir: stat for %s failed, errno: %d", path, errno);
  166. return false;
  167. }
  168. struct os_dirent *os_readdir(os_dir_t *dir)
  169. {
  170. struct dstr file_path = {0};
  171. if (!dir) return NULL;
  172. dir->cur_dirent = readdir(dir->dir);
  173. if (!dir->cur_dirent)
  174. return NULL;
  175. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  176. dstr_copy(&file_path, dir->path);
  177. dstr_cat(&file_path, "/");
  178. dstr_cat(&file_path, dir->out.d_name);
  179. dir->out.directory = is_dir(file_path.array);
  180. dstr_free(&file_path);
  181. return &dir->out;
  182. }
  183. void os_closedir(os_dir_t *dir)
  184. {
  185. if (dir) {
  186. closedir(dir->dir);
  187. bfree(dir);
  188. }
  189. }
  190. struct posix_glob_info {
  191. struct os_glob_info base;
  192. glob_t gl;
  193. };
  194. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  195. {
  196. struct posix_glob_info pgi;
  197. int ret = glob(pattern, 0, NULL, &pgi.gl);
  198. if (ret == 0) {
  199. DARRAY(struct os_globent) list;
  200. da_init(list);
  201. for (size_t i = 0; i < pgi.gl.gl_pathc; i++) {
  202. struct os_globent ent = {0};
  203. ent.path = pgi.gl.gl_pathv[i];
  204. ent.directory = is_dir(ent.path);
  205. da_push_back(list, &ent);
  206. }
  207. pgi.base.gl_pathc = list.num;
  208. pgi.base.gl_pathv = list.array;
  209. *pglob = bmemdup(&pgi, sizeof(pgi));
  210. } else {
  211. *pglob = NULL;
  212. }
  213. UNUSED_PARAMETER(flags);
  214. return ret;
  215. }
  216. void os_globfree(os_glob_t *pglob)
  217. {
  218. if (pglob) {
  219. struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
  220. globfree(&pgi->gl);
  221. bfree(pgi->base.gl_pathv);
  222. bfree(pgi);
  223. }
  224. }
  225. int os_unlink(const char *path)
  226. {
  227. return unlink(path);
  228. }
  229. int os_mkdir(const char *path)
  230. {
  231. if (mkdir(path, 0777) == 0)
  232. return MKDIR_SUCCESS;
  233. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  234. }
  235. #if !defined(__APPLE__)
  236. os_performance_token_t *os_request_high_performance(const char *reason)
  237. {
  238. UNUSED_PARAMETER(reason);
  239. return NULL;
  240. }
  241. void os_end_high_performance(os_performance_token_t *token)
  242. {
  243. UNUSED_PARAMETER(token);
  244. }
  245. #endif