Platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. htop - openbsd/Platform.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2015 Michael McConville
  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 "openbsd/Platform.h"
  10. #include <errno.h>
  11. #include <kvm.h>
  12. #include <limits.h>
  13. #include <math.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <time.h>
  17. #include <sys/signal.h> // needs to be included before <sys/proc.h> for 'struct sigaltstack'
  18. #include <sys/proc.h>
  19. #include <sys/resource.h>
  20. #include <sys/sensors.h>
  21. #include <sys/sysctl.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <uvm/uvmexp.h>
  25. #include "CPUMeter.h"
  26. #include "ClockMeter.h"
  27. #include "DateMeter.h"
  28. #include "DateTimeMeter.h"
  29. #include "FileDescriptorMeter.h"
  30. #include "HostnameMeter.h"
  31. #include "LoadAverageMeter.h"
  32. #include "Macros.h"
  33. #include "MemoryMeter.h"
  34. #include "MemorySwapMeter.h"
  35. #include "Meter.h"
  36. #include "Settings.h"
  37. #include "SignalsPanel.h"
  38. #include "SwapMeter.h"
  39. #include "SysArchMeter.h"
  40. #include "TasksMeter.h"
  41. #include "UptimeMeter.h"
  42. #include "XUtils.h"
  43. #include "openbsd/OpenBSDMachine.h"
  44. #include "openbsd/OpenBSDProcess.h"
  45. const ScreenDefaults Platform_defaultScreens[] = {
  46. {
  47. .name = "Main",
  48. .columns = "PID USER PRIORITY NICE M_VIRT M_RESIDENT STATE PERCENT_CPU PERCENT_MEM TIME Command",
  49. .sortKey = "PERCENT_CPU",
  50. },
  51. };
  52. const unsigned int Platform_numberOfDefaultScreens = ARRAYSIZE(Platform_defaultScreens);
  53. /*
  54. * See /usr/include/sys/signal.h
  55. */
  56. const SignalItem Platform_signals[] = {
  57. { .name = " 0 Cancel", .number = 0 },
  58. { .name = " 1 SIGHUP", .number = 1 },
  59. { .name = " 2 SIGINT", .number = 2 },
  60. { .name = " 3 SIGQUIT", .number = 3 },
  61. { .name = " 4 SIGILL", .number = 4 },
  62. { .name = " 5 SIGTRAP", .number = 5 },
  63. { .name = " 6 SIGABRT", .number = 6 },
  64. { .name = " 6 SIGIOT", .number = 6 },
  65. { .name = " 7 SIGEMT", .number = 7 },
  66. { .name = " 8 SIGFPE", .number = 8 },
  67. { .name = " 9 SIGKILL", .number = 9 },
  68. { .name = "10 SIGBUS", .number = 10 },
  69. { .name = "11 SIGSEGV", .number = 11 },
  70. { .name = "12 SIGSYS", .number = 12 },
  71. { .name = "13 SIGPIPE", .number = 13 },
  72. { .name = "14 SIGALRM", .number = 14 },
  73. { .name = "15 SIGTERM", .number = 15 },
  74. { .name = "16 SIGURG", .number = 16 },
  75. { .name = "17 SIGSTOP", .number = 17 },
  76. { .name = "18 SIGTSTP", .number = 18 },
  77. { .name = "19 SIGCONT", .number = 19 },
  78. { .name = "20 SIGCHLD", .number = 20 },
  79. { .name = "21 SIGTTIN", .number = 21 },
  80. { .name = "22 SIGTTOU", .number = 22 },
  81. { .name = "23 SIGIO", .number = 23 },
  82. { .name = "24 SIGXCPU", .number = 24 },
  83. { .name = "25 SIGXFSZ", .number = 25 },
  84. { .name = "26 SIGVTALRM", .number = 26 },
  85. { .name = "27 SIGPROF", .number = 27 },
  86. { .name = "28 SIGWINCH", .number = 28 },
  87. { .name = "29 SIGINFO", .number = 29 },
  88. { .name = "30 SIGUSR1", .number = 30 },
  89. { .name = "31 SIGUSR2", .number = 31 },
  90. { .name = "32 SIGTHR", .number = 32 },
  91. };
  92. const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
  93. const MeterClass* const Platform_meterTypes[] = {
  94. &CPUMeter_class,
  95. &ClockMeter_class,
  96. &DateMeter_class,
  97. &DateTimeMeter_class,
  98. &LoadAverageMeter_class,
  99. &LoadMeter_class,
  100. &MemoryMeter_class,
  101. &SwapMeter_class,
  102. &MemorySwapMeter_class,
  103. &TasksMeter_class,
  104. &UptimeMeter_class,
  105. &BatteryMeter_class,
  106. &HostnameMeter_class,
  107. &SysArchMeter_class,
  108. &AllCPUsMeter_class,
  109. &AllCPUs2Meter_class,
  110. &AllCPUs4Meter_class,
  111. &AllCPUs8Meter_class,
  112. &LeftCPUsMeter_class,
  113. &RightCPUsMeter_class,
  114. &LeftCPUs2Meter_class,
  115. &RightCPUs2Meter_class,
  116. &LeftCPUs4Meter_class,
  117. &RightCPUs4Meter_class,
  118. &LeftCPUs8Meter_class,
  119. &RightCPUs8Meter_class,
  120. &FileDescriptorMeter_class,
  121. &BlankMeter_class,
  122. NULL
  123. };
  124. bool Platform_init(void) {
  125. /* no platform-specific setup needed */
  126. return true;
  127. }
  128. void Platform_done(void) {
  129. /* no platform-specific cleanup needed */
  130. }
  131. void Platform_setBindings(Htop_Action* keys) {
  132. /* no platform-specific key bindings */
  133. (void) keys;
  134. }
  135. int Platform_getUptime(void) {
  136. struct timeval bootTime, currTime;
  137. const int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  138. size_t size = sizeof(bootTime);
  139. int err = sysctl(mib, 2, &bootTime, &size, NULL, 0);
  140. if (err) {
  141. return -1;
  142. }
  143. gettimeofday(&currTime, NULL);
  144. return (int) difftime(currTime.tv_sec, bootTime.tv_sec);
  145. }
  146. void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
  147. struct loadavg loadAverage;
  148. const int mib[2] = { CTL_VM, VM_LOADAVG };
  149. size_t size = sizeof(loadAverage);
  150. int err = sysctl(mib, 2, &loadAverage, &size, NULL, 0);
  151. if (err) {
  152. *one = 0;
  153. *five = 0;
  154. *fifteen = 0;
  155. return;
  156. }
  157. *one = (double) loadAverage.ldavg[0] / loadAverage.fscale;
  158. *five = (double) loadAverage.ldavg[1] / loadAverage.fscale;
  159. *fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale;
  160. }
  161. pid_t Platform_getMaxPid(void) {
  162. return 2 * THREAD_PID_OFFSET;
  163. }
  164. double Platform_setCPUValues(Meter* this, unsigned int cpu) {
  165. const Machine* host = this->host;
  166. const OpenBSDMachine* ohost = (const OpenBSDMachine*) host;
  167. const CPUData* cpuData = &ohost->cpuData[cpu];
  168. double total;
  169. double totalPercent;
  170. double* v = this->values;
  171. if (!cpuData->online) {
  172. this->curItems = 0;
  173. return NAN;
  174. }
  175. total = cpuData->totalPeriod == 0 ? 1 : cpuData->totalPeriod;
  176. v[CPU_METER_NICE] = cpuData->nicePeriod / total * 100.0;
  177. v[CPU_METER_NORMAL] = cpuData->userPeriod / total * 100.0;
  178. if (host->settings->detailedCPUTime) {
  179. v[CPU_METER_KERNEL] = cpuData->sysPeriod / total * 100.0;
  180. v[CPU_METER_IRQ] = cpuData->intrPeriod / total * 100.0;
  181. v[CPU_METER_SOFTIRQ] = 0.0;
  182. v[CPU_METER_STEAL] = 0.0;
  183. v[CPU_METER_GUEST] = 0.0;
  184. v[CPU_METER_IOWAIT] = 0.0;
  185. v[CPU_METER_FREQUENCY] = NAN;
  186. this->curItems = 8;
  187. } else {
  188. v[CPU_METER_KERNEL] = cpuData->sysAllPeriod / total * 100.0;
  189. v[CPU_METER_IRQ] = 0.0; // No steal nor guest on OpenBSD
  190. this->curItems = 4;
  191. }
  192. totalPercent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL] + v[CPU_METER_IRQ];
  193. totalPercent = CLAMP(totalPercent, 0.0, 100.0);
  194. v[CPU_METER_TEMPERATURE] = NAN;
  195. v[CPU_METER_FREQUENCY] = (ohost->cpuSpeed != -1) ? ohost->cpuSpeed : NAN;
  196. return totalPercent;
  197. }
  198. void Platform_setMemoryValues(Meter* this) {
  199. const Machine* host = this->host;
  200. long int usedMem = host->usedMem;
  201. long int buffersMem = host->buffersMem;
  202. long int cachedMem = host->cachedMem;
  203. usedMem -= buffersMem + cachedMem;
  204. this->total = host->totalMem;
  205. this->values[MEMORY_METER_USED] = usedMem;
  206. // this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
  207. // this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
  208. this->values[MEMORY_METER_BUFFERS] = buffersMem;
  209. this->values[MEMORY_METER_CACHE] = cachedMem;
  210. // this->values[MEMORY_METER_AVAILABLE] = "available memory"
  211. }
  212. void Platform_setSwapValues(Meter* this) {
  213. const Machine* host = this->host;
  214. this->total = host->totalSwap;
  215. this->values[SWAP_METER_USED] = host->usedSwap;
  216. // this->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux"
  217. // this->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux"
  218. }
  219. char* Platform_getProcessEnv(pid_t pid) {
  220. char errbuf[_POSIX2_LINE_MAX];
  221. char* env;
  222. char** ptr;
  223. int count;
  224. kvm_t* kt;
  225. struct kinfo_proc* kproc;
  226. size_t capacity = 4096, size = 0;
  227. if ((kt = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf)) == NULL) {
  228. return NULL;
  229. }
  230. if ((kproc = kvm_getprocs(kt, KERN_PROC_PID, pid,
  231. sizeof(struct kinfo_proc), &count)) == NULL) {
  232. (void) kvm_close(kt);
  233. return NULL;
  234. }
  235. if ((ptr = kvm_getenvv(kt, kproc, 0)) == NULL) {
  236. (void) kvm_close(kt);
  237. return NULL;
  238. }
  239. env = xMalloc(capacity);
  240. for (char** p = ptr; *p; p++) {
  241. size_t len = strlen(*p) + 1;
  242. while (size + len > capacity) {
  243. if (capacity > (SIZE_MAX / 2)) {
  244. free(env);
  245. env = NULL;
  246. goto end;
  247. }
  248. capacity *= 2;
  249. env = xRealloc(env, capacity);
  250. }
  251. strlcpy(env + size, *p, len);
  252. size += len;
  253. }
  254. if (size < 2 || env[size - 1] || env[size - 2]) {
  255. if (size + 2 < capacity)
  256. env = xRealloc(env, capacity + 2);
  257. env[size] = 0;
  258. env[size + 1] = 0;
  259. }
  260. end:
  261. (void) kvm_close(kt);
  262. return env;
  263. }
  264. FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
  265. (void)pid;
  266. return NULL;
  267. }
  268. void Platform_getFileDescriptors(double* used, double* max) {
  269. static const int mib_kern_maxfile[] = { CTL_KERN, KERN_MAXFILES };
  270. int sysctl_maxfile = 0;
  271. size_t size_maxfile = sizeof(int);
  272. if (sysctl(mib_kern_maxfile, ARRAYSIZE(mib_kern_maxfile), &sysctl_maxfile, &size_maxfile, NULL, 0) < 0) {
  273. *max = NAN;
  274. } else if (size_maxfile != sizeof(int) || sysctl_maxfile < 1) {
  275. *max = NAN;
  276. } else {
  277. *max = sysctl_maxfile;
  278. }
  279. static const int mib_kern_nfiles[] = { CTL_KERN, KERN_NFILES };
  280. int sysctl_nfiles = 0;
  281. size_t size_nfiles = sizeof(int);
  282. if (sysctl(mib_kern_nfiles, ARRAYSIZE(mib_kern_nfiles), &sysctl_nfiles, &size_nfiles, NULL, 0) < 0) {
  283. *used = NAN;
  284. } else if (size_nfiles != sizeof(int) || sysctl_nfiles < 0) {
  285. *used = NAN;
  286. } else {
  287. *used = sysctl_nfiles;
  288. }
  289. }
  290. bool Platform_getDiskIO(DiskIOData* data) {
  291. // TODO
  292. (void)data;
  293. return false;
  294. }
  295. bool Platform_getNetworkIO(NetworkIOData* data) {
  296. // TODO
  297. (void)data;
  298. return false;
  299. }
  300. static bool findDevice(const char* name, int* mib, struct sensordev* snsrdev, size_t* sdlen) {
  301. for (int devn = 0;; devn++) {
  302. mib[2] = devn;
  303. if (sysctl(mib, 3, snsrdev, sdlen, NULL, 0) == -1) {
  304. if (errno == ENXIO)
  305. continue;
  306. if (errno == ENOENT)
  307. return false;
  308. }
  309. if (String_eq(name, snsrdev->xname)) {
  310. return true;
  311. }
  312. }
  313. }
  314. void Platform_getBattery(double* percent, ACPresence* isOnAC) {
  315. int mib[] = {CTL_HW, HW_SENSORS, 0, 0, 0};
  316. struct sensor s;
  317. size_t slen = sizeof(struct sensor);
  318. struct sensordev snsrdev;
  319. size_t sdlen = sizeof(struct sensordev);
  320. bool found = findDevice("acpibat0", mib, &snsrdev, &sdlen);
  321. *percent = NAN;
  322. if (found) {
  323. /* last full capacity */
  324. mib[3] = 7;
  325. mib[4] = 0;
  326. double last_full_capacity = 0;
  327. if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1)
  328. last_full_capacity = s.value;
  329. if (last_full_capacity > 0) {
  330. /* remaining capacity */
  331. mib[3] = 7;
  332. mib[4] = 3;
  333. if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1) {
  334. double charge = s.value;
  335. *percent = 100 * (charge / last_full_capacity);
  336. if (charge >= last_full_capacity) {
  337. *percent = 100;
  338. }
  339. }
  340. }
  341. }
  342. found = findDevice("acpiac0", mib, &snsrdev, &sdlen);
  343. *isOnAC = AC_ERROR;
  344. if (found) {
  345. mib[3] = 9;
  346. mib[4] = 0;
  347. if (sysctl(mib, 5, &s, &slen, NULL, 0) != -1)
  348. *isOnAC = s.value;
  349. }
  350. }