Platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. htop - freebsd/Platform.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/Platform.h"
  9. #include <devstat.h>
  10. #include <math.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <net/if.h>
  15. #include <net/if_mib.h>
  16. #include <sys/_types.h>
  17. #include <sys/devicestat.h>
  18. #include <sys/param.h>
  19. #include <sys/resource.h>
  20. #include <sys/socket.h>
  21. #include <sys/sysctl.h>
  22. #include <sys/time.h>
  23. #include <sys/types.h>
  24. #include <vm/vm_param.h>
  25. #include "CPUMeter.h"
  26. #include "ClockMeter.h"
  27. #include "DateMeter.h"
  28. #include "DateTimeMeter.h"
  29. #include "DiskIOMeter.h"
  30. #include "FileDescriptorMeter.h"
  31. #include "HostnameMeter.h"
  32. #include "LoadAverageMeter.h"
  33. #include "Machine.h"
  34. #include "Macros.h"
  35. #include "MemoryMeter.h"
  36. #include "MemorySwapMeter.h"
  37. #include "Meter.h"
  38. #include "NetworkIOMeter.h"
  39. #include "Settings.h"
  40. #include "SwapMeter.h"
  41. #include "SysArchMeter.h"
  42. #include "TasksMeter.h"
  43. #include "UptimeMeter.h"
  44. #include "XUtils.h"
  45. #include "freebsd/FreeBSDMachine.h"
  46. #include "generic/fdstat_sysctl.h"
  47. #include "zfs/ZfsArcMeter.h"
  48. #include "zfs/ZfsCompressedArcMeter.h"
  49. const ScreenDefaults Platform_defaultScreens[] = {
  50. {
  51. .name = "Main",
  52. .columns = "PID USER PRIORITY NICE M_VIRT M_RESIDENT STATE PERCENT_CPU PERCENT_MEM TIME Command",
  53. .sortKey = "PERCENT_CPU",
  54. },
  55. };
  56. const unsigned int Platform_numberOfDefaultScreens = ARRAYSIZE(Platform_defaultScreens);
  57. const SignalItem Platform_signals[] = {
  58. { .name = " 0 Cancel", .number = 0 },
  59. { .name = " 1 SIGHUP", .number = 1 },
  60. { .name = " 2 SIGINT", .number = 2 },
  61. { .name = " 3 SIGQUIT", .number = 3 },
  62. { .name = " 4 SIGILL", .number = 4 },
  63. { .name = " 5 SIGTRAP", .number = 5 },
  64. { .name = " 6 SIGABRT", .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. { .name = "33 SIGLIBRT", .number = 33 },
  92. };
  93. const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
  94. const MeterClass* const Platform_meterTypes[] = {
  95. &CPUMeter_class,
  96. &ClockMeter_class,
  97. &DateMeter_class,
  98. &DateTimeMeter_class,
  99. &LoadAverageMeter_class,
  100. &LoadMeter_class,
  101. &MemoryMeter_class,
  102. &SwapMeter_class,
  103. &MemorySwapMeter_class,
  104. &TasksMeter_class,
  105. &UptimeMeter_class,
  106. &BatteryMeter_class,
  107. &HostnameMeter_class,
  108. &SysArchMeter_class,
  109. &AllCPUsMeter_class,
  110. &AllCPUs2Meter_class,
  111. &AllCPUs4Meter_class,
  112. &AllCPUs8Meter_class,
  113. &LeftCPUsMeter_class,
  114. &RightCPUsMeter_class,
  115. &LeftCPUs2Meter_class,
  116. &RightCPUs2Meter_class,
  117. &LeftCPUs4Meter_class,
  118. &RightCPUs4Meter_class,
  119. &LeftCPUs8Meter_class,
  120. &RightCPUs8Meter_class,
  121. &BlankMeter_class,
  122. &ZfsArcMeter_class,
  123. &ZfsCompressedArcMeter_class,
  124. &DiskIOMeter_class,
  125. &FileDescriptorMeter_class,
  126. &NetworkIOMeter_class,
  127. NULL
  128. };
  129. bool Platform_init(void) {
  130. /* no platform-specific setup needed */
  131. return true;
  132. }
  133. void Platform_done(void) {
  134. /* no platform-specific cleanup needed */
  135. }
  136. void Platform_setBindings(Htop_Action* keys) {
  137. /* no platform-specific key bindings */
  138. (void) keys;
  139. }
  140. int Platform_getUptime(void) {
  141. struct timeval bootTime, currTime;
  142. const int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  143. size_t size = sizeof(bootTime);
  144. int err = sysctl(mib, 2, &bootTime, &size, NULL, 0);
  145. if (err) {
  146. return -1;
  147. }
  148. gettimeofday(&currTime, NULL);
  149. return (int) difftime(currTime.tv_sec, bootTime.tv_sec);
  150. }
  151. void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
  152. struct loadavg loadAverage;
  153. const int mib[2] = { CTL_VM, VM_LOADAVG };
  154. size_t size = sizeof(loadAverage);
  155. int err = sysctl(mib, 2, &loadAverage, &size, NULL, 0);
  156. if (err) {
  157. *one = 0;
  158. *five = 0;
  159. *fifteen = 0;
  160. return;
  161. }
  162. *one = (double) loadAverage.ldavg[0] / loadAverage.fscale;
  163. *five = (double) loadAverage.ldavg[1] / loadAverage.fscale;
  164. *fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale;
  165. }
  166. pid_t Platform_getMaxPid(void) {
  167. int maxPid;
  168. size_t size = sizeof(maxPid);
  169. int err = sysctlbyname("kern.pid_max", &maxPid, &size, NULL, 0);
  170. if (err) {
  171. return 99999;
  172. }
  173. return maxPid;
  174. }
  175. double Platform_setCPUValues(Meter* this, unsigned int cpu) {
  176. const Machine* host = this->host;
  177. const FreeBSDMachine* fhost = (const FreeBSDMachine*) host;
  178. unsigned int cpus = host->activeCPUs;
  179. // single CPU box has everything in fhost->cpus[0]
  180. const CPUData* cpuData = cpus == 1 ? &fhost->cpus[0] : &fhost->cpus[cpu];
  181. double percent;
  182. double* v = this->values;
  183. v[CPU_METER_NICE] = cpuData->nicePercent;
  184. v[CPU_METER_NORMAL] = cpuData->userPercent;
  185. if (host->settings->detailedCPUTime) {
  186. v[CPU_METER_KERNEL] = cpuData->systemPercent;
  187. v[CPU_METER_IRQ] = cpuData->irqPercent;
  188. this->curItems = 4;
  189. percent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL] + v[CPU_METER_IRQ];
  190. } else {
  191. v[CPU_METER_KERNEL] = cpuData->systemAllPercent;
  192. this->curItems = 3;
  193. percent = v[CPU_METER_NICE] + v[CPU_METER_NORMAL] + v[CPU_METER_KERNEL];
  194. }
  195. percent = CLAMP(percent, 0.0, 100.0);
  196. v[CPU_METER_FREQUENCY] = cpuData->frequency;
  197. v[CPU_METER_TEMPERATURE] = cpuData->temperature;
  198. return percent;
  199. }
  200. void Platform_setMemoryValues(Meter* this) {
  201. const Machine* host = this->host;
  202. const FreeBSDMachine* fhost = (const FreeBSDMachine*) host;
  203. this->total = host->totalMem;
  204. this->values[MEMORY_METER_USED] = host->usedMem;
  205. this->values[MEMORY_METER_SHARED] = host->sharedMem;
  206. // this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
  207. this->values[MEMORY_METER_BUFFERS] = host->buffersMem;
  208. this->values[MEMORY_METER_CACHE] = host->cachedMem;
  209. // this->values[MEMORY_METER_AVAILABLE] = "available memory"
  210. if (fhost->zfs.enabled) {
  211. // ZFS does not shrink below the value of zfs_arc_min.
  212. unsigned long long int shrinkableSize = 0;
  213. if (fhost->zfs.size > fhost->zfs.min)
  214. shrinkableSize = fhost->zfs.size - fhost->zfs.min;
  215. this->values[MEMORY_METER_USED] -= shrinkableSize;
  216. this->values[MEMORY_METER_CACHE] += shrinkableSize;
  217. // this->values[MEMORY_METER_AVAILABLE] += shrinkableSize;
  218. }
  219. }
  220. void Platform_setSwapValues(Meter* this) {
  221. const Machine* host = this->host;
  222. this->total = host->totalSwap;
  223. this->values[SWAP_METER_USED] = host->usedSwap;
  224. // this->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux"
  225. // this->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux"
  226. }
  227. void Platform_setZfsArcValues(Meter* this) {
  228. const FreeBSDMachine* fhost = (const FreeBSDMachine*) this->host;
  229. ZfsArcMeter_readStats(this, &fhost->zfs);
  230. }
  231. void Platform_setZfsCompressedArcValues(Meter* this) {
  232. const FreeBSDMachine* fhost = (const FreeBSDMachine*) this->host;
  233. ZfsCompressedArcMeter_readStats(this, &fhost->zfs);
  234. }
  235. char* Platform_getProcessEnv(pid_t pid) {
  236. const int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_ENV, pid };
  237. size_t capacity = ARG_MAX;
  238. char* env = xMalloc(capacity);
  239. int err = sysctl(mib, 4, env, &capacity, NULL, 0);
  240. if (err || capacity == 0) {
  241. free(env);
  242. return NULL;
  243. }
  244. if (env[capacity - 1] || env[capacity - 2]) {
  245. env = xRealloc(env, capacity + 2);
  246. env[capacity] = 0;
  247. env[capacity + 1] = 0;
  248. }
  249. return env;
  250. }
  251. FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
  252. (void)pid;
  253. return NULL;
  254. }
  255. void Platform_getFileDescriptors(double* used, double* max) {
  256. Generic_getFileDescriptors_sysctl(used, max);
  257. }
  258. bool Platform_getDiskIO(DiskIOData* data) {
  259. if (devstat_checkversion(NULL) < 0)
  260. return false;
  261. // use static to plug memory leak; see #841
  262. static struct devinfo info = { 0 };
  263. struct statinfo current = { .dinfo = &info };
  264. // get number of devices
  265. if (devstat_getdevs(NULL, &current) < 0)
  266. return false;
  267. int count = current.dinfo->numdevs;
  268. unsigned long long int bytesReadSum = 0, bytesWriteSum = 0, timeSpendSum = 0;
  269. // get data
  270. for (int i = 0; i < count; i++) {
  271. uint64_t bytes_read, bytes_write;
  272. long double busy_time;
  273. devstat_compute_statistics(&current.dinfo->devices[i],
  274. NULL,
  275. 1.0,
  276. DSM_TOTAL_BYTES_READ, &bytes_read,
  277. DSM_TOTAL_BYTES_WRITE, &bytes_write,
  278. DSM_TOTAL_BUSY_TIME, &busy_time,
  279. DSM_NONE);
  280. bytesReadSum += bytes_read;
  281. bytesWriteSum += bytes_write;
  282. timeSpendSum += 1000 * busy_time;
  283. }
  284. data->totalBytesRead = bytesReadSum;
  285. data->totalBytesWritten = bytesWriteSum;
  286. data->totalMsTimeSpend = timeSpendSum;
  287. return true;
  288. }
  289. bool Platform_getNetworkIO(NetworkIOData* data) {
  290. // get number of interfaces
  291. int count;
  292. size_t countLen = sizeof(count);
  293. const int countMib[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT };
  294. int r = sysctl(countMib, ARRAYSIZE(countMib), &count, &countLen, NULL, 0);
  295. if (r < 0)
  296. return false;
  297. memset(data, 0, sizeof(NetworkIOData));
  298. for (int i = 1; i <= count; i++) {
  299. struct ifmibdata ifmd;
  300. size_t ifmdLen = sizeof(ifmd);
  301. const int dataMib[] = { CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, i, IFDATA_GENERAL };
  302. r = sysctl(dataMib, ARRAYSIZE(dataMib), &ifmd, &ifmdLen, NULL, 0);
  303. if (r < 0)
  304. continue;
  305. if (ifmd.ifmd_flags & IFF_LOOPBACK)
  306. continue;
  307. data->bytesReceived += ifmd.ifmd_data.ifi_ibytes;
  308. data->packetsReceived += ifmd.ifmd_data.ifi_ipackets;
  309. data->bytesTransmitted += ifmd.ifmd_data.ifi_obytes;
  310. data->packetsTransmitted += ifmd.ifmd_data.ifi_opackets;
  311. }
  312. return true;
  313. }
  314. void Platform_getBattery(double* percent, ACPresence* isOnAC) {
  315. int life;
  316. size_t life_len = sizeof(life);
  317. if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
  318. *percent = NAN;
  319. else
  320. *percent = life;
  321. int acline;
  322. size_t acline_len = sizeof(acline);
  323. if (sysctlbyname("hw.acpi.acline", &acline, &acline_len, NULL, 0) == -1)
  324. *isOnAC = AC_ERROR;
  325. else
  326. *isOnAC = acline == 0 ? AC_ABSENT : AC_PRESENT;
  327. }