DragonFlyBSDMachine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. unsigned long long int memActive = 0;
  196. sysctl(MIB_vm_stats_vm_v_active_count, 4, &memActive, &len, NULL, 0);
  197. memActive *= this->pageSizeKb;
  198. unsigned long long int memWire = 0;
  199. sysctl(MIB_vm_stats_vm_v_wire_count, 4, &memWire, &len, NULL, 0);
  200. memWire *= this->pageSizeKb;
  201. sysctl(MIB_vfs_bufspace, 2, &(super->buffersMem), &len, NULL, 0);
  202. super->buffersMem /= 1024;
  203. sysctl(MIB_vm_stats_vm_v_cache_count, 4, &(super->cachedMem), &len, NULL, 0);
  204. super->cachedMem *= this->pageSizeKb;
  205. super->usedMem = memActive + memWire;
  206. struct kvm_swap swap[16];
  207. int nswap = kvm_getswapinfo(this->kd, swap, ARRAYSIZE(swap), 0);
  208. super->totalSwap = 0;
  209. super->usedSwap = 0;
  210. for (int i = 0; i < nswap; i++) {
  211. super->totalSwap += swap[i].ksw_total;
  212. super->usedSwap += swap[i].ksw_used;
  213. }
  214. super->totalSwap *= this->pageSizeKb;
  215. super->usedSwap *= this->pageSizeKb;
  216. }
  217. static void DragonFlyBSDMachine_scanJails(DragonFlyBSDMachine* this) {
  218. size_t len;
  219. char* jails; /* Jail list */
  220. char* curpos;
  221. char* nextpos;
  222. if (sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1) {
  223. CRT_fatalError("initial sysctlbyname / jail.list failed");
  224. }
  225. retry:
  226. if (len == 0)
  227. return;
  228. jails = xMalloc(len);
  229. if (sysctlbyname("jail.list", jails, &len, NULL, 0) == -1) {
  230. if (errno == ENOMEM) {
  231. free(jails);
  232. goto retry;
  233. }
  234. CRT_fatalError("sysctlbyname / jail.list failed");
  235. }
  236. if (this->jails) {
  237. Hashtable_delete(this->jails);
  238. }
  239. this->jails = Hashtable_new(20, true);
  240. curpos = jails;
  241. while (curpos) {
  242. int jailid;
  243. char* str_hostname;
  244. nextpos = strchr(curpos, '\n');
  245. if (nextpos) {
  246. *nextpos++ = 0;
  247. }
  248. jailid = atoi(strtok(curpos, " "));
  249. str_hostname = strtok(NULL, " ");
  250. char* jname = (char*) (Hashtable_get(this->jails, jailid));
  251. if (jname == NULL) {
  252. jname = xStrdup(str_hostname);
  253. Hashtable_put(this->jails, jailid, jname);
  254. }
  255. curpos = nextpos;
  256. }
  257. free(jails);
  258. }
  259. char* DragonFlyBSDMachine_readJailName(const DragonFlyBSDMachine* host, int jailid) {
  260. char* hostname;
  261. char* jname;
  262. if (jailid != 0 && host->jails && (hostname = (char*)Hashtable_get(host->jails, jailid))) {
  263. jname = xStrdup(hostname);
  264. } else {
  265. jname = xStrdup("-");
  266. }
  267. return jname;
  268. }
  269. void Machine_scan(Machine* super) {
  270. DragonFlyBSDMachine* this = (DragonFlyBSDMachine*) super;
  271. DragonFlyBSDMachine_scanMemoryInfo(super);
  272. DragonFlyBSDMachine_scanCPUTime(super);
  273. DragonFlyBSDMachine_scanJails(this);
  274. }
  275. bool Machine_isCPUonline(const Machine* host, unsigned int id) {
  276. assert(id < host->existingCPUs);
  277. (void)host; (void)id;
  278. // TODO: Support detecting online / offline CPUs.
  279. return true;
  280. }