platform-nix.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <dirent.h>
  21. #include <stdlib.h>
  22. #include <dlfcn.h>
  23. #include <unistd.h>
  24. #include <glob.h>
  25. #include <time.h>
  26. #if !defined(__APPLE__)
  27. #include <sys/times.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. int os_get_config_path(char *dst, size_t size, const char *name)
  127. {
  128. char *path_ptr = getenv("HOME");
  129. if (path_ptr == NULL)
  130. bcrash("Could not get $HOME\n");
  131. return snprintf(dst, size, "%s/.%s", path_ptr, name);
  132. }
  133. char *os_get_config_path_ptr(const char *name)
  134. {
  135. char *path_ptr = getenv("HOME");
  136. if (path_ptr == NULL)
  137. bcrash("Could not get $HOME\n");
  138. struct dstr path;
  139. dstr_init_copy(&path, path_ptr);
  140. dstr_cat(&path, "/.");
  141. dstr_cat(&path, name);
  142. return path.array;
  143. }
  144. #endif
  145. bool os_file_exists(const char *path)
  146. {
  147. return access(path, F_OK) == 0;
  148. }
  149. struct os_dir {
  150. const char *path;
  151. DIR *dir;
  152. struct dirent *cur_dirent;
  153. struct os_dirent out;
  154. };
  155. os_dir_t *os_opendir(const char *path)
  156. {
  157. struct os_dir *dir;
  158. DIR *dir_val;
  159. dir_val = opendir(path);
  160. if (!dir_val)
  161. return NULL;
  162. dir = bzalloc(sizeof(struct os_dir));
  163. dir->dir = dir_val;
  164. dir->path = path;
  165. return dir;
  166. }
  167. static inline bool is_dir(const char *path)
  168. {
  169. struct stat stat_info;
  170. if (stat(path, &stat_info) == 0)
  171. return !!S_ISDIR(stat_info.st_mode);
  172. blog(LOG_DEBUG, "is_dir: stat for %s failed, errno: %d", path, errno);
  173. return false;
  174. }
  175. struct os_dirent *os_readdir(os_dir_t *dir)
  176. {
  177. struct dstr file_path = {0};
  178. if (!dir) return NULL;
  179. dir->cur_dirent = readdir(dir->dir);
  180. if (!dir->cur_dirent)
  181. return NULL;
  182. strncpy(dir->out.d_name, dir->cur_dirent->d_name, 255);
  183. dstr_copy(&file_path, dir->path);
  184. dstr_cat(&file_path, "/");
  185. dstr_cat(&file_path, dir->out.d_name);
  186. dir->out.directory = is_dir(file_path.array);
  187. dstr_free(&file_path);
  188. return &dir->out;
  189. }
  190. void os_closedir(os_dir_t *dir)
  191. {
  192. if (dir) {
  193. closedir(dir->dir);
  194. bfree(dir);
  195. }
  196. }
  197. struct posix_glob_info {
  198. struct os_glob_info base;
  199. glob_t gl;
  200. };
  201. int os_glob(const char *pattern, int flags, os_glob_t **pglob)
  202. {
  203. struct posix_glob_info pgi;
  204. int ret = glob(pattern, 0, NULL, &pgi.gl);
  205. if (ret == 0) {
  206. DARRAY(struct os_globent) list;
  207. da_init(list);
  208. for (size_t i = 0; i < pgi.gl.gl_pathc; i++) {
  209. struct os_globent ent = {0};
  210. ent.path = pgi.gl.gl_pathv[i];
  211. ent.directory = is_dir(ent.path);
  212. da_push_back(list, &ent);
  213. }
  214. pgi.base.gl_pathc = list.num;
  215. pgi.base.gl_pathv = list.array;
  216. *pglob = bmemdup(&pgi, sizeof(pgi));
  217. } else {
  218. *pglob = NULL;
  219. }
  220. UNUSED_PARAMETER(flags);
  221. return ret;
  222. }
  223. void os_globfree(os_glob_t *pglob)
  224. {
  225. if (pglob) {
  226. struct posix_glob_info *pgi = (struct posix_glob_info*)pglob;
  227. globfree(&pgi->gl);
  228. bfree(pgi->base.gl_pathv);
  229. bfree(pgi);
  230. }
  231. }
  232. int os_unlink(const char *path)
  233. {
  234. return unlink(path);
  235. }
  236. int os_mkdir(const char *path)
  237. {
  238. if (mkdir(path, 0777) == 0)
  239. return MKDIR_SUCCESS;
  240. return (errno == EEXIST) ? MKDIR_EXISTS : MKDIR_ERROR;
  241. }
  242. int os_rename(const char *old_path, const char *new_path)
  243. {
  244. return rename(old_path, new_path);
  245. }
  246. #if !defined(__APPLE__)
  247. os_performance_token_t *os_request_high_performance(const char *reason)
  248. {
  249. UNUSED_PARAMETER(reason);
  250. return NULL;
  251. }
  252. void os_end_high_performance(os_performance_token_t *token)
  253. {
  254. UNUSED_PARAMETER(token);
  255. }
  256. #endif