os.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "os.h"
  3. // ----------------------------------------------------------------------------
  4. // system functions
  5. // to retrieve settings of the system
  6. int processors = 1;
  7. long get_system_cpus(void) {
  8. processors = 1;
  9. #ifdef __APPLE__
  10. int32_t tmp_processors;
  11. if (unlikely(GETSYSCTL_BY_NAME("hw.logicalcpu", tmp_processors))) {
  12. error("Assuming system has %d processors.", processors);
  13. } else {
  14. processors = tmp_processors;
  15. }
  16. return processors;
  17. #elif __FreeBSD__
  18. int32_t tmp_processors;
  19. if (unlikely(GETSYSCTL_BY_NAME("hw.ncpu", tmp_processors))) {
  20. error("Assuming system has %d processors.", processors);
  21. } else {
  22. processors = tmp_processors;
  23. }
  24. return processors;
  25. #else
  26. char filename[FILENAME_MAX + 1];
  27. snprintfz(filename, FILENAME_MAX, "%s/proc/stat", netdata_configured_host_prefix);
  28. procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
  29. if(!ff) {
  30. error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
  31. return processors;
  32. }
  33. ff = procfile_readall(ff);
  34. if(!ff) {
  35. error("Cannot open file '%s'. Assuming system has %d processors.", filename, processors);
  36. return processors;
  37. }
  38. processors = 0;
  39. unsigned int i;
  40. for(i = 0; i < procfile_lines(ff); i++) {
  41. if(!procfile_linewords(ff, i)) continue;
  42. if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
  43. }
  44. processors--;
  45. if(processors < 1) processors = 1;
  46. procfile_close(ff);
  47. debug(D_SYSTEM, "System has %d processors.", processors);
  48. return processors;
  49. #endif /* __APPLE__, __FreeBSD__ */
  50. }
  51. pid_t pid_max = 32768;
  52. pid_t get_system_pid_max(void) {
  53. #ifdef __APPLE__
  54. // As we currently do not know a solution to query pid_max from the os
  55. // we use the number defined in bsd/sys/proc_internal.h in XNU sources
  56. pid_max = 99999;
  57. return pid_max;
  58. #elif __FreeBSD__
  59. int32_t tmp_pid_max;
  60. if (unlikely(GETSYSCTL_BY_NAME("kern.pid_max", tmp_pid_max))) {
  61. pid_max = 99999;
  62. error("Assuming system's maximum pid is %d.", pid_max);
  63. } else {
  64. pid_max = tmp_pid_max;
  65. }
  66. return pid_max;
  67. #else
  68. static char read = 0;
  69. if(unlikely(read)) return pid_max;
  70. read = 1;
  71. char filename[FILENAME_MAX + 1];
  72. snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix);
  73. unsigned long long max = 0;
  74. if(read_single_number_file(filename, &max) != 0) {
  75. error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
  76. return pid_max;
  77. }
  78. if(!max) {
  79. error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
  80. return pid_max;
  81. }
  82. pid_max = (pid_t) max;
  83. return pid_max;
  84. #endif /* __APPLE__, __FreeBSD__ */
  85. }
  86. unsigned int system_hz;
  87. void get_system_HZ(void) {
  88. long ticks;
  89. if ((ticks = sysconf(_SC_CLK_TCK)) == -1) {
  90. error("Cannot get system clock ticks");
  91. }
  92. system_hz = (unsigned int) ticks;
  93. }
  94. // =====================================================================================================================
  95. // FreeBSD
  96. #if __FreeBSD__
  97. const char *os_type = "freebsd";
  98. int getsysctl_by_name(const char *name, void *ptr, size_t len) {
  99. size_t nlen = len;
  100. if (unlikely(sysctlbyname(name, ptr, &nlen, NULL, 0) == -1)) {
  101. error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
  102. return 1;
  103. }
  104. if (unlikely(nlen != len)) {
  105. error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. int getsysctl_simple(const char *name, int *mib, size_t miblen, void *ptr, size_t len) {
  111. size_t nlen = len;
  112. if (unlikely(!mib[0]))
  113. if (unlikely(getsysctl_mib(name, mib, miblen)))
  114. return 1;
  115. if (unlikely(sysctl(mib, miblen, ptr, &nlen, NULL, 0) == -1)) {
  116. error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
  117. return 1;
  118. }
  119. if (unlikely(nlen != len)) {
  120. error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. int getsysctl(const char *name, int *mib, size_t miblen, void *ptr, size_t *len) {
  126. size_t nlen = *len;
  127. if (unlikely(!mib[0]))
  128. if (unlikely(getsysctl_mib(name, mib, miblen)))
  129. return 1;
  130. if (unlikely(sysctl(mib, miblen, ptr, len, NULL, 0) == -1)) {
  131. error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
  132. return 1;
  133. }
  134. if (unlikely(ptr != NULL && nlen != *len)) {
  135. error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)*len, (unsigned long)nlen);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. int getsysctl_mib(const char *name, int *mib, size_t len) {
  141. size_t nlen = len;
  142. if (unlikely(sysctlnametomib(name, mib, &nlen) == -1)) {
  143. error("FREEBSD: sysctl(%s...) failed: %s", name, strerror(errno));
  144. return 1;
  145. }
  146. if (unlikely(nlen != len)) {
  147. error("FREEBSD: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. #endif
  153. // =====================================================================================================================
  154. // MacOS
  155. #if __APPLE__
  156. const char *os_type = "macos";
  157. int getsysctl_by_name(const char *name, void *ptr, size_t len) {
  158. size_t nlen = len;
  159. if (unlikely(sysctlbyname(name, ptr, &nlen, NULL, 0) == -1)) {
  160. error("MACOS: sysctl(%s...) failed: %s", name, strerror(errno));
  161. return 1;
  162. }
  163. if (unlikely(nlen != len)) {
  164. error("MACOS: sysctl(%s...) expected %lu, got %lu", name, (unsigned long)len, (unsigned long)nlen);
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. #endif
  170. // =====================================================================================================================
  171. // Linux
  172. #if __linux__
  173. const char *os_type = "linux";
  174. #endif