DragonFlyBSDMachine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. htop - DragonFlyBSDMachine.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2017 Diederik de Groot
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "dragonflybsd/DragonFlyBSDMachine.h"
  9. #include <fcntl.h>
  10. #include <limits.h>
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/types.h>
  16. #include <sys/sysctl.h>
  17. #include <sys/user.h>
  18. #include <sys/param.h>
  19. #include "CRT.h"
  20. #include "Macros.h"
  21. #include "dragonflybsd/DragonFlyBSDProcess.h"
  22. static int MIB_hw_physmem[2];
  23. static int MIB_vm_stats_vm_v_page_count[4];
  24. static int MIB_vm_stats_vm_v_wire_count[4];
  25. static int MIB_vm_stats_vm_v_active_count[4];
  26. static int MIB_vm_stats_vm_v_cache_count[4];
  27. static int MIB_vm_stats_vm_v_inactive_count[4];
  28. static int MIB_vm_stats_vm_v_free_count[4];
  29. static int MIB_vfs_bufspace[2];
  30. static int MIB_kern_cp_time[2];
  31. static int MIB_kern_cp_times[2];
  32. Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
  33. size_t len;
  34. char errbuf[_POSIX2_LINE_MAX];
  35. DragonFlyBSDMachine* this = xCalloc(1, sizeof(DragonFlyBSDMachine));
  36. Machine* super = &this->super;
  37. Machine_init(super, usersTable, userId);
  38. // physical memory in system: hw.physmem
  39. // physical page size: hw.pagesize
  40. // usable pagesize : vm.stats.vm.v_page_size
  41. len = 2; sysctlnametomib("hw.physmem", MIB_hw_physmem, &len);
  42. len = sizeof(this->pageSize);
  43. if (sysctlbyname("vm.stats.vm.v_page_size", &this->pageSize, &len, NULL, 0) == -1)
  44. CRT_fatalError("Cannot get pagesize by sysctl");
  45. this->pageSizeKb = this->pageSize / ONE_K;
  46. // usable page count vm.stats.vm.v_page_count
  47. // actually usable memory : vm.stats.vm.v_page_count * vm.stats.vm.v_page_size
  48. len = 4; sysctlnametomib("vm.stats.vm.v_page_count", MIB_vm_stats_vm_v_page_count, &len);
  49. len = 4; sysctlnametomib("vm.stats.vm.v_wire_count", MIB_vm_stats_vm_v_wire_count, &len);
  50. len = 4; sysctlnametomib("vm.stats.vm.v_active_count", MIB_vm_stats_vm_v_active_count, &len);
  51. len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", MIB_vm_stats_vm_v_cache_count, &len);
  52. len = 4; sysctlnametomib("vm.stats.vm.v_inactive_count", MIB_vm_stats_vm_v_inactive_count, &len);
  53. len = 4; sysctlnametomib("vm.stats.vm.v_free_count", MIB_vm_stats_vm_v_free_count, &len);
  54. len = 2; sysctlnametomib("vfs.bufspace", MIB_vfs_bufspace, &len);
  55. int cpus = 1;
  56. len = sizeof(cpus);
  57. if (sysctlbyname("hw.ncpu", &cpus, &len, NULL, 0) != 0) {
  58. cpus = 1;
  59. }
  60. size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
  61. len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len);
  62. this->cp_time_o = xCalloc(CPUSTATES, sizeof(unsigned long));
  63. this->cp_time_n = xCalloc(CPUSTATES, sizeof(unsigned long));
  64. len = sizeof_cp_time_array;
  65. // fetch initial single (or average) CPU clicks from kernel
  66. sysctl(MIB_kern_cp_time, 2, this->cp_time_o, &len, NULL, 0);
  67. // on smp box, fetch rest of initial CPU's clicks
  68. if (cpus > 1) {
  69. len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len);
  70. this->cp_times_o = xCalloc(cpus, sizeof_cp_time_array);
  71. this->cp_times_n = xCalloc(cpus, sizeof_cp_time_array);
  72. len = cpus * sizeof_cp_time_array;
  73. sysctl(MIB_kern_cp_times, 2, this->cp_times_o, &len, NULL, 0);
  74. }
  75. super->existingCPUs = MAXIMUM(cpus, 1);
  76. // TODO: support offline CPUs and hot swapping
  77. super->activeCPUs = super->existingCPUs;
  78. if (cpus == 1 ) {
  79. this->cpus = xRealloc(this->cpus, sizeof(CPUData));
  80. } else {
  81. // on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well)
  82. this->cpus = xRealloc(this->cpus, (super->existingCPUs + 1) * sizeof(CPUData));
  83. }
  84. len = sizeof(this->kernelFScale);
  85. if (sysctlbyname("kern.fscale", &this->kernelFScale, &len, NULL, 0) == -1 || this->kernelFScale <= 0) {
  86. //sane default for kernel provided CPU percentage scaling, at least on x86 machines, in case this sysctl call failed
  87. this->kernelFScale = 2048;
  88. }
  89. this->kd = kvm_openfiles(NULL, "/dev/null", NULL, 0, errbuf);
  90. if (this->kd == NULL) {
  91. CRT_fatalError("kvm_openfiles() failed");
  92. }
  93. return super;
  94. }
  95. void Machine_delete(Machine* super) {
  96. DragonFlyBSDMachine* this = (DragonFlyBSDMachine*) super;
  97. Machine_done(super);
  98. if (this->kd) {
  99. kvm_close(this->kd);
  100. }
  101. if (this->jails) {
  102. Hashtable_delete(this->jails);
  103. }
  104. free(this->cp_time_o);
  105. free(this->cp_time_n);
  106. free(this->cp_times_o);
  107. free(this->cp_times_n);
  108. free(this->cpus);
  109. free(this);
  110. }
  111. static void DragonFlyBSDMachine_scanCPUTime(Machine* super) {
  112. const DragonFlyBSDMachine* this = (DragonFlyBSDMachine*) super;
  113. unsigned int cpus = super->existingCPUs; // actual CPU count
  114. unsigned int maxcpu = cpus; // max iteration (in case we have average + smp)
  115. int cp_times_offset;
  116. assert(cpus > 0);
  117. size_t sizeof_cp_time_array;
  118. unsigned long* cp_time_n; // old clicks state
  119. unsigned long* cp_time_o; // current clicks state
  120. unsigned long cp_time_d[CPUSTATES];
  121. double cp_time_p[CPUSTATES];
  122. // get averages or single CPU clicks
  123. sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
  124. sysctl(MIB_kern_cp_time, 2, this->cp_time_n, &sizeof_cp_time_array, NULL, 0);
  125. // get rest of CPUs
  126. if (cpus > 1) {
  127. // on smp systems DragonFlyBSD kernel concats all CPU states into one long array in
  128. // kern.cp_times sysctl OID
  129. // we store averages in dfpl->cpus[0], and actual cores after that
  130. maxcpu = cpus + 1;
  131. sizeof_cp_time_array = cpus * sizeof(unsigned long) * CPUSTATES;
  132. sysctl(MIB_kern_cp_times, 2, this->cp_times_n, &sizeof_cp_time_array, NULL, 0);
  133. }
  134. for (unsigned int i = 0; i < maxcpu; i++) {
  135. if (cpus == 1) {
  136. // single CPU box
  137. cp_time_n = this->cp_time_n;
  138. cp_time_o = this->cp_time_o;
  139. } else {
  140. if (i == 0 ) {
  141. // average
  142. cp_time_n = this->cp_time_n;
  143. cp_time_o = this->cp_time_o;
  144. } else {
  145. // specific smp cores
  146. cp_times_offset = i - 1;
  147. cp_time_n = this->cp_times_n + (cp_times_offset * CPUSTATES);
  148. cp_time_o = this->cp_times_o + (cp_times_offset * CPUSTATES);
  149. }
  150. }
  151. // diff old vs new
  152. unsigned long long total_o = 0;
  153. unsigned long long total_n = 0;
  154. unsigned long long total_d = 0;
  155. for (int s = 0; s < CPUSTATES; s++) {
  156. cp_time_d[s] = cp_time_n[s] - cp_time_o[s];
  157. total_o += cp_time_o[s];
  158. total_n += cp_time_n[s];
  159. }
  160. // totals
  161. total_d = total_n - total_o;
  162. if (total_d < 1 ) {
  163. total_d = 1;
  164. }
  165. // save current state as old and calc percentages
  166. for (int s = 0; s < CPUSTATES; ++s) {
  167. cp_time_o[s] = cp_time_n[s];
  168. cp_time_p[s] = ((double)cp_time_d[s]) / ((double)total_d) * 100;
  169. }
  170. CPUData* cpuData = &(this->cpus[i]);
  171. cpuData->userPercent = cp_time_p[CP_USER];
  172. cpuData->nicePercent = cp_time_p[CP_NICE];
  173. cpuData->systemPercent = cp_time_p[CP_SYS];
  174. cpuData->irqPercent = cp_time_p[CP_INTR];
  175. cpuData->systemAllPercent = cp_time_p[CP_SYS] + cp_time_p[CP_INTR];
  176. // this one is not really used, but we store it anyway
  177. cpuData->idlePercent = cp_time_p[CP_IDLE];
  178. }
  179. }
  180. static void DragonFlyBSDMachine_scanMemoryInfo(Machine* super) {
  181. DragonFlyBSDMachine* this = (DragonFlyBSDMachine*) super;
  182. // @etosan:
  183. // memory counter relationships seem to be these:
  184. // total = active + wired + inactive + cache + free
  185. // htop_used (unavail to anybody) = active + wired
  186. // htop_cache (for cache meter) = buffers + cache
  187. // user_free (avail to procs) = buffers + inactive + cache + free
  188. size_t len = sizeof(super->totalMem);
  189. //disabled for now, as it is always smaller than phycal amount of memory...
  190. //...to avoid "where is my memory?" questions
  191. //sysctl(MIB_vm_stats_vm_v_page_count, 4, &(this->totalMem), &len, NULL, 0);
  192. //this->totalMem *= pageSizeKb;
  193. sysctl(MIB_hw_physmem, 2, &(super->totalMem), &len, NULL, 0);
  194. super->totalMem /= 1024;
  195. sysctl(MIB_vm_stats_vm_v_active_count, 4, &(this->memActive), &len, NULL, 0);
  196. this->memActive *= this->pageSizeKb;
  197. sysctl(MIB_vm_stats_vm_v_wire_count, 4, &(this->memWire), &len, NULL, 0);
  198. this->memWire *= this->pageSizeKb;
  199. sysctl(MIB_vfs_bufspace, 2, &(super->buffersMem), &len, NULL, 0);
  200. super->buffersMem /= 1024;
  201. sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(super->cachedMem), &len, NULL, 0);
  202. super->cachedMem *= this->pageSizeKb;
  203. super->usedMem = this->memActive + this->memWire;
  204. struct kvm_swap swap[16];
  205. int nswap = kvm_getswapinfo(this->kd, swap, ARRAYSIZE(swap), 0);
  206. super->totalSwap = 0;
  207. super->usedSwap = 0;
  208. for (int i = 0; i < nswap; i++) {
  209. super->totalSwap += swap[i].ksw_total;
  210. super->usedSwap += swap[i].ksw_used;
  211. }
  212. super->totalSwap *= this->pageSizeKb;
  213. super->usedSwap *= this->pageSizeKb;
  214. }
  215. static void DragonFlyBSDMachine_scanJails(DragonFlyBSDMachine* this) {
  216. size_t len;
  217. char* jails; /* Jail list */
  218. char* curpos;
  219. char* nextpos;
  220. if (sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1) {
  221. CRT_fatalError("initial sysctlbyname / jail.list failed");
  222. }
  223. retry:
  224. if (len == 0)
  225. return;
  226. jails = xMalloc(len);
  227. if (sysctlbyname("jail.list", jails, &len, NULL, 0) == -1) {
  228. if (errno == ENOMEM) {
  229. free(jails);
  230. goto retry;
  231. }
  232. CRT_fatalError("sysctlbyname / jail.list failed");
  233. }
  234. if (this->jails) {
  235. Hashtable_delete(this->jails);
  236. }
  237. this->jails = Hashtable_new(20, true);
  238. curpos = jails;
  239. while (curpos) {
  240. int jailid;
  241. char* str_hostname;
  242. nextpos = strchr(curpos, '\n');
  243. if (nextpos) {
  244. *nextpos++ = 0;
  245. }
  246. jailid = atoi(strtok(curpos, " "));
  247. str_hostname = strtok(NULL, " ");
  248. char* jname = (char*) (Hashtable_get(this->jails, jailid));
  249. if (jname == NULL) {
  250. jname = xStrdup(str_hostname);
  251. Hashtable_put(this->jails, jailid, jname);
  252. }
  253. curpos = nextpos;
  254. }
  255. free(jails);
  256. }
  257. char* DragonFlyBSDMachine_readJailName(const DragonFlyBSDMachine* host, int jailid) {
  258. char* hostname;
  259. char* jname;
  260. if (jailid != 0 && host->jails && (hostname = (char*)Hashtable_get(host->jails, jailid))) {
  261. jname = xStrdup(hostname);
  262. } else {
  263. jname = xStrdup("-");
  264. }
  265. return jname;
  266. }
  267. void Machine_scan(Machine* super) {
  268. DragonFlyBSDMachine* this = (DragonFlyBSDMachine*) super;
  269. DragonFlyBSDMachine_scanMemoryInfo(super);
  270. DragonFlyBSDMachine_scanCPUTime(super);
  271. DragonFlyBSDMachine_scanJails(this);
  272. }
  273. bool Machine_isCPUonline(const Machine* host, unsigned int id) {
  274. assert(id < host->existingCPUs);
  275. (void)host; (void)id;
  276. // TODO: Support detecting online / offline CPUs.
  277. return true;
  278. }