SolarisMachine.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. htop - SolarisMachine.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2017,2018 Guy M. Broome
  5. Released under the GNU GPLv2+, see the COPYING file
  6. in the source distribution for its full text.
  7. */
  8. #include "config.h" // IWYU pragma: keep
  9. #include "solaris/SolarisMachine.h"
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <sys/types.h>
  13. #include <sys/user.h>
  14. #include <limits.h>
  15. #include <string.h>
  16. #include <procfs.h>
  17. #include <errno.h>
  18. #include <pwd.h>
  19. #include <math.h>
  20. #include <time.h>
  21. #include "CRT.h"
  22. #include "solaris/Platform.h"
  23. static void SolarisMachine_updateCPUcount(SolarisMachine* this) {
  24. Machine* super = &this->super;
  25. long int s;
  26. bool change = false;
  27. s = sysconf(_SC_NPROCESSORS_CONF);
  28. if (s < 1)
  29. CRT_fatalError("Cannot get existing CPU count by sysconf(_SC_NPROCESSORS_CONF)");
  30. if (s != super->existingCPUs) {
  31. if (s == 1) {
  32. this->cpus = xRealloc(this->cpus, sizeof(CPUData));
  33. this->cpus[0].online = true;
  34. } else {
  35. this->cpus = xReallocArray(this->cpus, s + 1, sizeof(CPUData));
  36. this->cpus[0].online = true; /* average is always "online" */
  37. for (int i = 1; i < s + 1; i++) {
  38. this->cpus[i].online = false;
  39. }
  40. }
  41. change = true;
  42. super->existingCPUs = s;
  43. }
  44. s = sysconf(_SC_NPROCESSORS_ONLN);
  45. if (s < 1)
  46. CRT_fatalError("Cannot get active CPU count by sysconf(_SC_NPROCESSORS_ONLN)");
  47. if (s != super->activeCPUs) {
  48. change = true;
  49. super->activeCPUs = s;
  50. }
  51. if (change) {
  52. kstat_close(this->kd);
  53. this->kd = kstat_open();
  54. if (!this->kd)
  55. CRT_fatalError("Cannot open kstat handle");
  56. }
  57. }
  58. static void SolarisMachine_scanCPUTime(SolarisMachine* this) {
  59. Machine* super = &this->super;
  60. unsigned int activeCPUs = super->activeCPUs;
  61. unsigned int existingCPUs = super->existingCPUs;
  62. kstat_t* cpuinfo = NULL;
  63. kstat_named_t* idletime = NULL;
  64. kstat_named_t* intrtime = NULL;
  65. kstat_named_t* krnltime = NULL;
  66. kstat_named_t* usertime = NULL;
  67. kstat_named_t* cpu_freq = NULL;
  68. double idlebuf = 0;
  69. double intrbuf = 0;
  70. double krnlbuf = 0;
  71. double userbuf = 0;
  72. int arrskip = 0;
  73. assert(existingCPUs > 0);
  74. assert(this->kd);
  75. if (existingCPUs > 1) {
  76. // Store values for the stats loop one extra element up in the array
  77. // to leave room for the average to be calculated afterwards
  78. arrskip++;
  79. }
  80. // Calculate per-CPU statistics first
  81. for (unsigned int i = 0; i < existingCPUs; i++) {
  82. CPUData* cpuData = &(this->cpus[i + arrskip]);
  83. if ((cpuinfo = kstat_lookup_wrapper(this->kd, "cpu", i, "sys")) != NULL) {
  84. cpuData->online = true;
  85. if (kstat_read(this->kd, cpuinfo, NULL) != -1) {
  86. idletime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_idle");
  87. intrtime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_intr");
  88. krnltime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_kernel");
  89. usertime = kstat_data_lookup_wrapper(cpuinfo, "cpu_nsec_user");
  90. }
  91. } else {
  92. cpuData->online = false;
  93. continue;
  94. }
  95. assert( (idletime != NULL) && (intrtime != NULL)
  96. && (krnltime != NULL) && (usertime != NULL) );
  97. if (super->settings->showCPUFrequency) {
  98. if ((cpuinfo = kstat_lookup_wrapper(this->kd, "cpu_info", i, NULL)) != NULL) {
  99. if (kstat_read(this->kd, cpuinfo, NULL) != -1) {
  100. cpu_freq = kstat_data_lookup_wrapper(cpuinfo, "current_clock_Hz");
  101. }
  102. }
  103. assert( cpu_freq != NULL );
  104. }
  105. uint64_t totaltime = (idletime->value.ui64 - cpuData->lidle)
  106. + (intrtime->value.ui64 - cpuData->lintr)
  107. + (krnltime->value.ui64 - cpuData->lkrnl)
  108. + (usertime->value.ui64 - cpuData->luser);
  109. // Calculate percentages of deltas since last reading
  110. cpuData->userPercent = ((usertime->value.ui64 - cpuData->luser) / (double)totaltime) * 100.0;
  111. cpuData->nicePercent = (double)0.0; // Not implemented on Solaris
  112. cpuData->systemPercent = ((krnltime->value.ui64 - cpuData->lkrnl) / (double)totaltime) * 100.0;
  113. cpuData->irqPercent = ((intrtime->value.ui64 - cpuData->lintr) / (double)totaltime) * 100.0;
  114. cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent;
  115. cpuData->idlePercent = ((idletime->value.ui64 - cpuData->lidle) / (double)totaltime) * 100.0;
  116. // Store current values to use for the next round of deltas
  117. cpuData->luser = usertime->value.ui64;
  118. cpuData->lkrnl = krnltime->value.ui64;
  119. cpuData->lintr = intrtime->value.ui64;
  120. cpuData->lidle = idletime->value.ui64;
  121. // Add frequency in MHz
  122. cpuData->frequency = super->settings->showCPUFrequency ? (double)cpu_freq->value.ui64 / 1E6 : NAN;
  123. // Accumulate the current percentages into buffers for later average calculation
  124. if (existingCPUs > 1) {
  125. userbuf += cpuData->userPercent;
  126. krnlbuf += cpuData->systemPercent;
  127. intrbuf += cpuData->irqPercent;
  128. idlebuf += cpuData->idlePercent;
  129. }
  130. }
  131. if (existingCPUs > 1) {
  132. CPUData* cpuData = &(this->cpus[0]);
  133. cpuData->userPercent = userbuf / activeCPUs;
  134. cpuData->nicePercent = (double)0.0; // Not implemented on Solaris
  135. cpuData->systemPercent = krnlbuf / activeCPUs;
  136. cpuData->irqPercent = intrbuf / activeCPUs;
  137. cpuData->systemAllPercent = cpuData->systemPercent + cpuData->irqPercent;
  138. cpuData->idlePercent = idlebuf / activeCPUs;
  139. }
  140. }
  141. static void SolarisMachine_scanMemoryInfo(SolarisMachine* this) {
  142. Machine* super = &this->super;
  143. static kstat_t *meminfo = NULL;
  144. int ksrphyserr = -1;
  145. kstat_named_t *totalmem_pgs = NULL;
  146. kstat_named_t *freemem_pgs = NULL;
  147. kstat_named_t *pages = NULL;
  148. struct swaptable *sl = NULL;
  149. struct swapent *swapdev = NULL;
  150. uint64_t totalswap = 0;
  151. uint64_t totalfree = 0;
  152. int nswap = 0;
  153. char *spath = NULL;
  154. char *spathbase = NULL;
  155. // Part 1 - physical memory
  156. if (this->kd != NULL && meminfo == NULL) {
  157. // Look up the kstat chain just once, it never changes
  158. meminfo = kstat_lookup_wrapper(this->kd, "unix", 0, "system_pages");
  159. }
  160. if (meminfo != NULL) {
  161. ksrphyserr = kstat_read(this->kd, meminfo, NULL);
  162. }
  163. if (ksrphyserr != -1) {
  164. totalmem_pgs = kstat_data_lookup_wrapper(meminfo, "physmem");
  165. freemem_pgs = kstat_data_lookup_wrapper(meminfo, "freemem");
  166. pages = kstat_data_lookup_wrapper(meminfo, "pagestotal");
  167. super->totalMem = totalmem_pgs->value.ui64 * this->pageSizeKB;
  168. if (super->totalMem > freemem_pgs->value.ui64 * this->pageSizeKB) {
  169. super->usedMem = super->totalMem - freemem_pgs->value.ui64 * this->pageSizeKB;
  170. } else {
  171. super->usedMem = 0; // This can happen in non-global zone (in theory)
  172. }
  173. // Not sure how to implement this on Solaris - suggestions welcome!
  174. super->cachedMem = 0;
  175. // Not really "buffers" but the best Solaris analogue that I can find to
  176. // "memory in use but not by programs or the kernel itself"
  177. super->buffersMem = (totalmem_pgs->value.ui64 - pages->value.ui64) * this->pageSizeKB;
  178. } else {
  179. // Fall back to basic sysconf if kstat isn't working
  180. super->totalMem = sysconf(_SC_PHYS_PAGES) * this->pageSize;
  181. super->buffersMem = 0;
  182. super->cachedMem = 0;
  183. super->usedMem = super->totalMem - (sysconf(_SC_AVPHYS_PAGES) * this->pageSize);
  184. }
  185. // Part 2 - swap
  186. nswap = swapctl(SC_GETNSWP, NULL);
  187. if (nswap > 0) {
  188. sl = xMalloc((nswap * sizeof(swapent_t)) + sizeof(int));
  189. }
  190. if (sl != NULL) {
  191. spathbase = xMalloc( nswap * MAXPATHLEN );
  192. }
  193. if (spathbase != NULL) {
  194. spath = spathbase;
  195. swapdev = sl->swt_ent;
  196. for (int i = 0; i < nswap; i++, swapdev++) {
  197. swapdev->ste_path = spath;
  198. spath += MAXPATHLEN;
  199. }
  200. sl->swt_n = nswap;
  201. }
  202. nswap = swapctl(SC_LIST, sl);
  203. if (nswap > 0) {
  204. swapdev = sl->swt_ent;
  205. for (int i = 0; i < nswap; i++, swapdev++) {
  206. totalswap += swapdev->ste_pages;
  207. totalfree += swapdev->ste_free;
  208. }
  209. }
  210. free(spathbase);
  211. free(sl);
  212. super->totalSwap = totalswap * this->pageSizeKB;
  213. super->usedSwap = super->totalSwap - (totalfree * this->pageSizeKB);
  214. }
  215. static void SolarisMachine_scanZfsArcstats(SolarisMachine* this) {
  216. kstat_named_t *cur_kstat;
  217. kstat_t *arcstats;
  218. int ksrphyserr;
  219. if (this->kd == NULL)
  220. return;
  221. arcstats = kstat_lookup_wrapper(this->kd, "zfs", 0, "arcstats");
  222. if (arcstats == NULL)
  223. return;
  224. ksrphyserr = kstat_read(this->kd, arcstats, NULL);
  225. if (ksrphyserr == -1)
  226. return;
  227. cur_kstat = kstat_data_lookup_wrapper( arcstats, "size" );
  228. this->zfs.size = cur_kstat->value.ui64 / 1024;
  229. this->zfs.enabled = this->zfs.size > 0 ? 1 : 0;
  230. cur_kstat = kstat_data_lookup_wrapper( arcstats, "c_max" );
  231. this->zfs.max = cur_kstat->value.ui64 / 1024;
  232. cur_kstat = kstat_data_lookup_wrapper( arcstats, "mfu_size" );
  233. this->zfs.MFU = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0;
  234. cur_kstat = kstat_data_lookup_wrapper( arcstats, "mru_size" );
  235. this->zfs.MRU = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0;
  236. cur_kstat = kstat_data_lookup_wrapper( arcstats, "anon_size" );
  237. this->zfs.anon = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0;
  238. cur_kstat = kstat_data_lookup_wrapper( arcstats, "hdr_size" );
  239. this->zfs.header = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0;
  240. cur_kstat = kstat_data_lookup_wrapper( arcstats, "other_size" );
  241. this->zfs.other = cur_kstat != NULL ? cur_kstat->value.ui64 / 1024 : 0;
  242. if ((cur_kstat = kstat_data_lookup_wrapper( arcstats, "compressed_size" )) != NULL) {
  243. this->zfs.compressed = cur_kstat->value.ui64 / 1024;
  244. this->zfs.isCompressed = 1;
  245. cur_kstat = kstat_data_lookup_wrapper( arcstats, "uncompressed_size" );
  246. this->zfs.uncompressed = cur_kstat->value.ui64 / 1024;
  247. } else {
  248. this->zfs.isCompressed = 0;
  249. }
  250. }
  251. void Machine_scan(Machine* super) {
  252. SolarisMachine* this = (SolarisMachine*) super;
  253. SolarisMachine_updateCPUcount(this);
  254. SolarisMachine_scanCPUTime(this);
  255. SolarisMachine_scanMemoryInfo(this);
  256. SolarisMachine_scanZfsArcstats(this);
  257. }
  258. Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
  259. SolarisMachine* this = xCalloc(1, sizeof(SolarisMachine));
  260. Machine* super = &this->super;
  261. Machine_init(super, usersTable, userId);
  262. this->pageSize = sysconf(_SC_PAGESIZE);
  263. if (this->pageSize == -1)
  264. CRT_fatalError("Cannot get pagesize by sysconf(_SC_PAGESIZE)");
  265. this->pageSizeKB = this->pageSize / 1024;
  266. this->kd = kstat_open();
  267. if (!this->kd)
  268. CRT_fatalError("Cannot open kstat handle");
  269. SolarisMachine_updateCPUcount(this);
  270. return super;
  271. }
  272. void Machine_delete(Machine* super) {
  273. SolarisMachine* this = (SolarisMachine*) super;
  274. Machine_done(super);
  275. free(this->cpus);
  276. if (this->kd) {
  277. kstat_close(this->kd);
  278. }
  279. free(this);
  280. }
  281. bool Machine_isCPUonline(const Machine* super, unsigned int id) {
  282. assert(id < super->existingCPUs);
  283. const SolarisMachine* this = (const SolarisMachine*) super;
  284. return (super->existingCPUs == 1) ? true : this->cpus[id + 1].online;
  285. }