haiku.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Copyright libuv project contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "internal.h"
  23. #include <FindDirectory.h> /* find_path() */
  24. #include <OS.h>
  25. void uv_loadavg(double avg[3]) {
  26. avg[0] = 0;
  27. avg[1] = 0;
  28. avg[2] = 0;
  29. }
  30. int uv_exepath(char* buffer, size_t* size) {
  31. char abspath[B_PATH_NAME_LENGTH];
  32. status_t status;
  33. ssize_t abspath_len;
  34. if (buffer == NULL || size == NULL || *size == 0)
  35. return UV_EINVAL;
  36. status = find_path(B_APP_IMAGE_SYMBOL, B_FIND_PATH_IMAGE_PATH, NULL, abspath,
  37. sizeof(abspath));
  38. if (status != B_OK)
  39. return UV__ERR(status);
  40. abspath_len = uv__strscpy(buffer, abspath, *size);
  41. *size -= 1;
  42. if (abspath_len >= 0 && *size > (size_t)abspath_len)
  43. *size = (size_t)abspath_len;
  44. return 0;
  45. }
  46. uint64_t uv_get_free_memory(void) {
  47. status_t status;
  48. system_info sinfo;
  49. status = get_system_info(&sinfo);
  50. if (status != B_OK)
  51. return 0;
  52. return (sinfo.max_pages - sinfo.used_pages) * B_PAGE_SIZE;
  53. }
  54. uint64_t uv_get_total_memory(void) {
  55. status_t status;
  56. system_info sinfo;
  57. status = get_system_info(&sinfo);
  58. if (status != B_OK)
  59. return 0;
  60. return sinfo.max_pages * B_PAGE_SIZE;
  61. }
  62. uint64_t uv_get_constrained_memory(void) {
  63. return 0; /* Memory constraints are unknown. */
  64. }
  65. int uv_resident_set_memory(size_t* rss) {
  66. area_info area;
  67. ssize_t cookie;
  68. status_t status;
  69. thread_info thread;
  70. status = get_thread_info(find_thread(NULL), &thread);
  71. if (status != B_OK)
  72. return UV__ERR(status);
  73. cookie = 0;
  74. *rss = 0;
  75. while (get_next_area_info(thread.team, &cookie, &area) == B_OK)
  76. *rss += area.ram_size;
  77. return 0;
  78. }
  79. int uv_uptime(double* uptime) {
  80. /* system_time() returns time since booting in microseconds */
  81. *uptime = (double)system_time() / 1000000;
  82. return 0;
  83. }
  84. int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
  85. cpu_topology_node_info* topology_infos;
  86. int i;
  87. status_t status;
  88. system_info system;
  89. uint32_t topology_count;
  90. uint64_t cpuspeed;
  91. uv_cpu_info_t* cpu_info;
  92. if (cpu_infos == NULL || count == NULL)
  93. return UV_EINVAL;
  94. status = get_cpu_topology_info(NULL, &topology_count);
  95. if (status != B_OK)
  96. return UV__ERR(status);
  97. topology_infos = uv__malloc(topology_count * sizeof(*topology_infos));
  98. if (topology_infos == NULL)
  99. return UV_ENOMEM;
  100. status = get_cpu_topology_info(topology_infos, &topology_count);
  101. if (status != B_OK) {
  102. uv__free(topology_infos);
  103. return UV__ERR(status);
  104. }
  105. cpuspeed = 0;
  106. for (i = 0; i < (int)topology_count; i++) {
  107. if (topology_infos[i].type == B_TOPOLOGY_CORE) {
  108. cpuspeed = topology_infos[i].data.core.default_frequency;
  109. break;
  110. }
  111. }
  112. uv__free(topology_infos);
  113. status = get_system_info(&system);
  114. if (status != B_OK)
  115. return UV__ERR(status);
  116. *cpu_infos = uv__calloc(system.cpu_count, sizeof(**cpu_infos));
  117. if (*cpu_infos == NULL)
  118. return UV_ENOMEM;
  119. /* CPU time and model are not exposed by Haiku. */
  120. cpu_info = *cpu_infos;
  121. for (i = 0; i < (int)system.cpu_count; i++) {
  122. cpu_info->model = uv__strdup("unknown");
  123. cpu_info->speed = (int)(cpuspeed / 1000000);
  124. cpu_info++;
  125. }
  126. *count = system.cpu_count;
  127. return 0;
  128. }
  129. void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
  130. int i;
  131. for (i = 0; i < count; i++)
  132. uv__free(cpu_infos[i].model);
  133. uv__free(cpu_infos);
  134. }