FreeBSDMachine.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. htop - FreeBSDMachine.c
  3. (C) 2014 Hisham H. Muhammad
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "config.h" // IWYU pragma: keep
  8. #include "freebsd/FreeBSDMachine.h"
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/_iovec.h>
  15. #include <sys/errno.h>
  16. #include <sys/param.h> // needs to be included before <sys/jail.h> for MAXPATHLEN
  17. #include <sys/jail.h>
  18. #include <sys/priority.h>
  19. #include <sys/proc.h>
  20. #include <sys/resource.h>
  21. #include <sys/sysctl.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <sys/user.h>
  25. #include <sys/vmmeter.h>
  26. #include "CRT.h"
  27. #include "Compat.h"
  28. #include "Macros.h"
  29. #include "Object.h"
  30. #include "Scheduling.h"
  31. #include "Settings.h"
  32. #include "XUtils.h"
  33. #include "generic/openzfs_sysctl.h"
  34. #include "zfs/ZfsArcStats.h"
  35. static int MIB_hw_physmem[2];
  36. static int MIB_vm_stats_vm_v_page_count[4];
  37. static int MIB_vm_stats_vm_v_wire_count[4];
  38. static int MIB_vm_stats_vm_v_active_count[4];
  39. static int MIB_vm_stats_vm_v_cache_count[4];
  40. static int MIB_vm_stats_vm_v_inactive_count[4];
  41. static int MIB_vm_stats_vm_v_free_count[4];
  42. static int MIB_vm_vmtotal[2];
  43. static int MIB_vfs_bufspace[2];
  44. static int MIB_kern_cp_time[2];
  45. static int MIB_kern_cp_times[2];
  46. Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
  47. FreeBSDMachine* this = xCalloc(1, sizeof(FreeBSDMachine));
  48. Machine* super = &this->super;
  49. char errbuf[_POSIX2_LINE_MAX];
  50. size_t len;
  51. Machine_init(super, usersTable, userId);
  52. // physical memory in system: hw.physmem
  53. // physical page size: hw.pagesize
  54. // usable pagesize : vm.stats.vm.v_page_size
  55. len = 2; sysctlnametomib("hw.physmem", MIB_hw_physmem, &len);
  56. len = sizeof(this->pageSize);
  57. if (sysctlbyname("vm.stats.vm.v_page_size", &this->pageSize, &len, NULL, 0) == -1)
  58. CRT_fatalError("Cannot get pagesize by sysctl");
  59. this->pageSizeKb = this->pageSize / ONE_K;
  60. // usable page count vm.stats.vm.v_page_count
  61. // actually usable memory : vm.stats.vm.v_page_count * vm.stats.vm.v_page_size
  62. len = 4; sysctlnametomib("vm.stats.vm.v_page_count", MIB_vm_stats_vm_v_page_count, &len);
  63. len = 4; sysctlnametomib("vm.stats.vm.v_wire_count", MIB_vm_stats_vm_v_wire_count, &len);
  64. len = 4; sysctlnametomib("vm.stats.vm.v_active_count", MIB_vm_stats_vm_v_active_count, &len);
  65. len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len);
  66. len = 4; sysctlnametomib("vm.stats.vm.v_inactive_count", MIB_vm_stats_vm_v_inactive_count, &len);
  67. len = 4; sysctlnametomib("vm.stats.vm.v_free_count", MIB_vm_stats_vm_v_free_count, &len);
  68. len = 2; sysctlnametomib("vm.vmtotal", MIB_vm_vmtotal, &len);
  69. len = 2; sysctlnametomib("vfs.bufspace", MIB_vfs_bufspace, &len);
  70. openzfs_sysctl_init(&this->zfs);
  71. openzfs_sysctl_updateArcStats(&this->zfs);
  72. int smp = 0;
  73. len = sizeof(smp);
  74. if (sysctlbyname("kern.smp.active", &smp, &len, NULL, 0) != 0 || len != sizeof(smp)) {
  75. smp = 0;
  76. }
  77. int cpus = 1;
  78. len = sizeof(cpus);
  79. if (smp) {
  80. int err = sysctlbyname("kern.smp.cpus", &cpus, &len, NULL, 0);
  81. if (err) {
  82. cpus = 1;
  83. }
  84. } else {
  85. cpus = 1;
  86. }
  87. size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
  88. len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len);
  89. this->cp_time_o = xCalloc(CPUSTATES, sizeof(unsigned long));
  90. this->cp_time_n = xCalloc(CPUSTATES, sizeof(unsigned long));
  91. len = sizeof_cp_time_array;
  92. // fetch initial single (or average) CPU clicks from kernel
  93. sysctl(MIB_kern_cp_time, 2, this->cp_time_o, &len, NULL, 0);
  94. // on smp box, fetch rest of initial CPU's clicks
  95. if (cpus > 1) {
  96. len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len);
  97. this->cp_times_o = xCalloc(cpus, sizeof_cp_time_array);
  98. this->cp_times_n = xCalloc(cpus, sizeof_cp_time_array);
  99. len = cpus * sizeof_cp_time_array;
  100. sysctl(MIB_kern_cp_times, 2, this->cp_times_o, &len, NULL, 0);
  101. }
  102. super->existingCPUs = MAXIMUM(cpus, 1);
  103. // TODO: support offline CPUs and hot swapping
  104. super->activeCPUs = super->existingCPUs;
  105. if (cpus == 1 ) {
  106. this->cpus = xRealloc(this->cpus, sizeof(CPUData));
  107. } else {
  108. // on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well)
  109. this->cpus = xRealloc(this->cpus, (super->existingCPUs + 1) * sizeof(CPUData));
  110. }
  111. len = sizeof(this->kernelFScale);
  112. if (sysctlbyname("kern.fscale", &this->kernelFScale, &len, NULL, 0) == -1 || this->kernelFScale <= 0) {
  113. //sane default for kernel provided CPU percentage scaling, at least on x86 machines, in case this sysctl call failed
  114. this->kernelFScale = 2048;
  115. }
  116. this->kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
  117. if (this->kd == NULL) {
  118. CRT_fatalError("kvm_openfiles() failed");
  119. }
  120. return super;
  121. }
  122. void Machine_delete(Machine* super) {
  123. FreeBSDMachine* this = (FreeBSDMachine*) super;
  124. Machine_done(super);
  125. if (this->kd) {
  126. kvm_close(this->kd);
  127. }
  128. free(this->cp_time_o);
  129. free(this->cp_time_n);
  130. free(this->cp_times_o);
  131. free(this->cp_times_n);
  132. free(this->cpus);
  133. free(this);
  134. }
  135. static inline void FreeBSDMachine_scanCPU(Machine* super) {
  136. const FreeBSDMachine* this = (FreeBSDMachine*) super;
  137. unsigned int cpus = super->existingCPUs; // actual CPU count
  138. unsigned int maxcpu = cpus; // max iteration (in case we have average + smp)
  139. int cp_times_offset;
  140. assert(cpus > 0);
  141. size_t sizeof_cp_time_array;
  142. unsigned long* cp_time_n; // old clicks state
  143. unsigned long* cp_time_o; // current clicks state
  144. unsigned long cp_time_d[CPUSTATES];
  145. double cp_time_p[CPUSTATES];
  146. // get averages or single CPU clicks
  147. sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
  148. sysctl(MIB_kern_cp_time, 2, this->cp_time_n, &sizeof_cp_time_array, NULL, 0);
  149. // get rest of CPUs
  150. if (cpus > 1) {
  151. // on smp systems FreeBSD kernel concats all CPU states into one long array in
  152. // kern.cp_times sysctl OID
  153. // we store averages in this->cpus[0], and actual cores after that
  154. maxcpu = cpus + 1;
  155. sizeof_cp_time_array = cpus * sizeof(unsigned long) * CPUSTATES;
  156. sysctl(MIB_kern_cp_times, 2, this->cp_times_n, &sizeof_cp_time_array, NULL, 0);
  157. }
  158. for (unsigned int i = 0; i < maxcpu; i++) {
  159. if (cpus == 1) {
  160. // single CPU box
  161. cp_time_n = this->cp_time_n;
  162. cp_time_o = this->cp_time_o;
  163. } else {
  164. if (i == 0 ) {
  165. // average
  166. cp_time_n = this->cp_time_n;
  167. cp_time_o = this->cp_time_o;
  168. } else {
  169. // specific smp cores
  170. cp_times_offset = i - 1;
  171. cp_time_n = this->cp_times_n + (cp_times_offset * CPUSTATES);
  172. cp_time_o = this->cp_times_o + (cp_times_offset * CPUSTATES);
  173. }
  174. }
  175. // diff old vs new
  176. unsigned long long total_o = 0;
  177. unsigned long long total_n = 0;
  178. unsigned long long total_d = 0;
  179. for (int s = 0; s < CPUSTATES; s++) {
  180. cp_time_d[s] = cp_time_n[s] - cp_time_o[s];
  181. total_o += cp_time_o[s];
  182. total_n += cp_time_n[s];
  183. }
  184. // totals
  185. total_d = total_n - total_o;
  186. if (total_d < 1 ) {
  187. total_d = 1;
  188. }
  189. // save current state as old and calc percentages
  190. for (int s = 0; s < CPUSTATES; ++s) {
  191. cp_time_o[s] = cp_time_n[s];
  192. cp_time_p[s] = ((double)cp_time_d[s]) / ((double)total_d) * 100;
  193. }
  194. CPUData* cpuData = &(this->cpus[i]);
  195. cpuData->userPercent = cp_time_p[CP_USER];
  196. cpuData->nicePercent = cp_time_p[CP_NICE];
  197. cpuData->systemPercent = cp_time_p[CP_SYS];
  198. cpuData->irqPercent = cp_time_p[CP_INTR];
  199. cpuData->systemAllPercent = cp_time_p[CP_SYS] + cp_time_p[CP_INTR];
  200. // this one is not really used
  201. //cpuData->idlePercent = cp_time_p[CP_IDLE];
  202. cpuData->temperature = NAN;
  203. cpuData->frequency = NAN;
  204. const int coreId = (cpus == 1) ? 0 : ((int)i - 1);
  205. if (coreId < 0)
  206. continue;
  207. // TODO: test with hyperthreading and multi-cpu systems
  208. if (super->settings->showCPUTemperature) {
  209. int temperature;
  210. size_t len = sizeof(temperature);
  211. char mibBuffer[32];
  212. xSnprintf(mibBuffer, sizeof(mibBuffer), "dev.cpu.%d.temperature", coreId);
  213. int r = sysctlbyname(mibBuffer, &temperature, &len, NULL, 0);
  214. if (r == 0)
  215. cpuData->temperature = (double)(temperature - 2732) / 10.0; // convert from deci-Kelvin to Celsius
  216. }
  217. // TODO: test with hyperthreading and multi-cpu systems
  218. if (super->settings->showCPUFrequency) {
  219. int frequency;
  220. size_t len = sizeof(frequency);
  221. char mibBuffer[32];
  222. xSnprintf(mibBuffer, sizeof(mibBuffer), "dev.cpu.%d.freq", coreId);
  223. int r = sysctlbyname(mibBuffer, &frequency, &len, NULL, 0);
  224. if (r == 0)
  225. cpuData->frequency = frequency; // keep in MHz
  226. }
  227. }
  228. // calculate max temperature and avg frequency for average meter and
  229. // propagate frequency to all cores if only supplied for CPU 0
  230. if (cpus > 1) {
  231. if (super->settings->showCPUTemperature) {
  232. double maxTemp = -HUGE_VAL;
  233. for (unsigned int i = 1; i < maxcpu; i++) {
  234. if (isgreater(this->cpus[i].temperature, maxTemp)) {
  235. maxTemp = this->cpus[i].temperature;
  236. this->cpus[0].temperature = maxTemp;
  237. }
  238. }
  239. }
  240. if (super->settings->showCPUFrequency) {
  241. const double coreZeroFreq = this->cpus[1].frequency;
  242. double freqSum = coreZeroFreq;
  243. if (isNonnegative(coreZeroFreq)) {
  244. for (unsigned int i = 2; i < maxcpu; i++) {
  245. if (!isNonnegative(this->cpus[i].frequency))
  246. this->cpus[i].frequency = coreZeroFreq;
  247. freqSum += this->cpus[i].frequency;
  248. }
  249. this->cpus[0].frequency = freqSum / (maxcpu - 1);
  250. }
  251. }
  252. }
  253. }
  254. static void FreeBSDMachine_scanMemoryInfo(Machine* super) {
  255. FreeBSDMachine* this = (FreeBSDMachine*) super;
  256. // @etosan:
  257. // memory counter relationships seem to be these:
  258. // total = active + wired + inactive + cache + free
  259. // htop_used (unavail to anybody) = active + wired
  260. // htop_cache (for cache meter) = buffers + cache
  261. // user_free (avail to procs) = buffers + inactive + cache + free
  262. //
  263. // with ZFS ARC situation becomes bit muddled, as ARC behaves like "user_free"
  264. // and belongs into cache, but is reported as wired by kernel
  265. //
  266. // htop_used = active + (wired - arc)
  267. // htop_cache = buffers + cache + arc
  268. u_long totalMem;
  269. u_int memActive, memWire, cachedMem;
  270. long buffersMem;
  271. size_t len;
  272. struct vmtotal vmtotal;
  273. //disabled for now, as it is always smaller than phycal amount of memory...
  274. //...to avoid "where is my memory?" questions
  275. //sysctl(MIB_vm_stats_vm_v_page_count, 4, &(super->totalMem), &len, NULL, 0);
  276. //super->totalMem *= this->pageSizeKb;
  277. len = sizeof(totalMem);
  278. sysctl(MIB_hw_physmem, 2, &(totalMem), &len, NULL, 0);
  279. totalMem /= 1024;
  280. super->totalMem = totalMem;
  281. len = sizeof(memActive);
  282. sysctl(MIB_vm_stats_vm_v_active_count, 4, &(memActive), &len, NULL, 0);
  283. memActive *= this->pageSizeKb;
  284. this->memActive = memActive;
  285. len = sizeof(memWire);
  286. sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(memWire), &len, NULL, 0);
  287. memWire *= this->pageSizeKb;
  288. this->memWire = memWire;
  289. len = sizeof(buffersMem);
  290. sysctl(MIB_vfs_bufspace, 2, &(buffersMem), &len, NULL, 0);
  291. buffersMem /= 1024;
  292. super->buffersMem = buffersMem;
  293. len = sizeof(cachedMem);
  294. sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(cachedMem), &len, NULL, 0);
  295. cachedMem *= this->pageSizeKb;
  296. super->cachedMem = cachedMem;
  297. len = sizeof(vmtotal);
  298. sysctl(MIB_vm_vmtotal, 2, &(vmtotal), &len, NULL, 0);
  299. super->sharedMem = vmtotal.t_rmshr * this->pageSizeKb;
  300. super->usedMem = this->memActive + this->memWire;
  301. struct kvm_swap swap[16];
  302. int nswap = kvm_getswapinfo(this->kd, swap, ARRAYSIZE(swap), 0);
  303. super->totalSwap = 0;
  304. super->usedSwap = 0;
  305. for (int i = 0; i < nswap; i++) {
  306. super->totalSwap += swap[i].ksw_total;
  307. super->usedSwap += swap[i].ksw_used;
  308. }
  309. super->totalSwap *= this->pageSizeKb;
  310. super->usedSwap *= this->pageSizeKb;
  311. }
  312. void Machine_scan(Machine* super) {
  313. FreeBSDMachine* this = (FreeBSDMachine*) super;
  314. openzfs_sysctl_updateArcStats(&this->zfs);
  315. FreeBSDMachine_scanMemoryInfo(super);
  316. FreeBSDMachine_scanCPU(super);
  317. }
  318. bool Machine_isCPUonline(const Machine* host, unsigned int id) {
  319. assert(id < host->existingCPUs);
  320. // TODO: support offline CPUs and hot swapping
  321. (void) host; (void) id;
  322. return true;
  323. }