Platform.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. htop - dragonflybsd/Platform.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 "config.h" // IWYU pragma: keep
  9. #include "dragonflybsd/Platform.h"
  10. #include <devstat.h>
  11. #include <errno.h>
  12. #include <ifaddrs.h>
  13. #include <math.h>
  14. #include <time.h>
  15. #include <sys/resource.h>
  16. #include <sys/socket.h>
  17. #include <sys/sysctl.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <vm/vm_param.h>
  21. #include "ClockMeter.h"
  22. #include "CPUMeter.h"
  23. #include "DateMeter.h"
  24. #include "DateTimeMeter.h"
  25. #include "FileDescriptorMeter.h"
  26. #include "HostnameMeter.h"
  27. #include "LoadAverageMeter.h"
  28. #include "Macros.h"
  29. #include "MemoryMeter.h"
  30. #include "MemorySwapMeter.h"
  31. #include "ProcessTable.h"
  32. #include "SwapMeter.h"
  33. #include "SysArchMeter.h"
  34. #include "TasksMeter.h"
  35. #include "UptimeMeter.h"
  36. #include "XUtils.h"
  37. #include "dragonflybsd/DragonFlyBSDMachine.h"
  38. #include "dragonflybsd/DragonFlyBSDProcess.h"
  39. #include "dragonflybsd/DragonFlyBSDProcessTable.h"
  40. #include "generic/fdstat_sysctl.h"
  41. const ScreenDefaults Platform_defaultScreens[] = {
  42. {
  43. .name = "Main",
  44. .columns = "PID USER PRIORITY NICE M_VIRT M_RESIDENT STATE PERCENT_CPU PERCENT_MEM TIME Command",
  45. .sortKey = "PERCENT_CPU",
  46. },
  47. };
  48. const unsigned int Platform_numberOfDefaultScreens = ARRAYSIZE(Platform_defaultScreens);
  49. const SignalItem Platform_signals[] = {
  50. { .name = " 0 Cancel", .number = 0 },
  51. { .name = " 1 SIGHUP", .number = 1 },
  52. { .name = " 2 SIGINT", .number = 2 },
  53. { .name = " 3 SIGQUIT", .number = 3 },
  54. { .name = " 4 SIGILL", .number = 4 },
  55. { .name = " 5 SIGTRAP", .number = 5 },
  56. { .name = " 6 SIGABRT", .number = 6 },
  57. { .name = " 7 SIGEMT", .number = 7 },
  58. { .name = " 8 SIGFPE", .number = 8 },
  59. { .name = " 9 SIGKILL", .number = 9 },
  60. { .name = "10 SIGBUS", .number = 10 },
  61. { .name = "11 SIGSEGV", .number = 11 },
  62. { .name = "12 SIGSYS", .number = 12 },
  63. { .name = "13 SIGPIPE", .number = 13 },
  64. { .name = "14 SIGALRM", .number = 14 },
  65. { .name = "15 SIGTERM", .number = 15 },
  66. { .name = "16 SIGURG", .number = 16 },
  67. { .name = "17 SIGSTOP", .number = 17 },
  68. { .name = "18 SIGTSTP", .number = 18 },
  69. { .name = "19 SIGCONT", .number = 19 },
  70. { .name = "20 SIGCHLD", .number = 20 },
  71. { .name = "21 SIGTTIN", .number = 21 },
  72. { .name = "22 SIGTTOU", .number = 22 },
  73. { .name = "23 SIGIO", .number = 23 },
  74. { .name = "24 SIGXCPU", .number = 24 },
  75. { .name = "25 SIGXFSZ", .number = 25 },
  76. { .name = "26 SIGVTALRM", .number = 26 },
  77. { .name = "27 SIGPROF", .number = 27 },
  78. { .name = "28 SIGWINCH", .number = 28 },
  79. { .name = "29 SIGINFO", .number = 29 },
  80. { .name = "30 SIGUSR1", .number = 30 },
  81. { .name = "31 SIGUSR2", .number = 31 },
  82. { .name = "32 SIGTHR", .number = 32 },
  83. { .name = "33 SIGLIBRT", .number = 33 },
  84. };
  85. const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
  86. const MeterClass* const Platform_meterTypes[] = {
  87. &CPUMeter_class,
  88. &ClockMeter_class,
  89. &DateMeter_class,
  90. &DateTimeMeter_class,
  91. &LoadAverageMeter_class,
  92. &LoadMeter_class,
  93. &MemoryMeter_class,
  94. &MemorySwapMeter_class,
  95. &SwapMeter_class,
  96. &TasksMeter_class,
  97. &UptimeMeter_class,
  98. &BatteryMeter_class,
  99. &HostnameMeter_class,
  100. &SysArchMeter_class,
  101. &AllCPUsMeter_class,
  102. &AllCPUs2Meter_class,
  103. &AllCPUs4Meter_class,
  104. &AllCPUs8Meter_class,
  105. &LeftCPUsMeter_class,
  106. &RightCPUsMeter_class,
  107. &LeftCPUs2Meter_class,
  108. &RightCPUs2Meter_class,
  109. &LeftCPUs4Meter_class,
  110. &RightCPUs4Meter_class,
  111. &LeftCPUs8Meter_class,
  112. &RightCPUs8Meter_class,
  113. &DiskIOMeter_class,
  114. &NetworkIOMeter_class,
  115. &FileDescriptorMeter_class,
  116. &BlankMeter_class,
  117. NULL
  118. };
  119. bool Platform_init(void) {
  120. /* no platform-specific setup needed */
  121. return true;
  122. }
  123. void Platform_done(void) {
  124. /* no platform-specific cleanup needed */
  125. }
  126. void Platform_setBindings(Htop_Action* keys) {
  127. /* no platform-specific key bindings */
  128. (void) keys;
  129. }
  130. int Platform_getUptime(void) {
  131. struct timeval bootTime, currTime;
  132. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  133. size_t size = sizeof(bootTime);
  134. int err = sysctl(mib, 2, &bootTime, &size, NULL, 0);
  135. if (err) {
  136. return -1;
  137. }
  138. gettimeofday(&currTime, NULL);
  139. return (int) difftime(currTime.tv_sec, bootTime.tv_sec);
  140. }
  141. void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
  142. struct loadavg loadAverage;
  143. int mib[2] = { CTL_VM, VM_LOADAVG };
  144. size_t size = sizeof(loadAverage);
  145. int err = sysctl(mib, 2, &loadAverage, &size, NULL, 0);
  146. if (err) {
  147. *one = 0;
  148. *five = 0;
  149. *fifteen = 0;
  150. return;
  151. }
  152. *one = (double) loadAverage.ldavg[0] / loadAverage.fscale;
  153. *five = (double) loadAverage.ldavg[1] / loadAverage.fscale;
  154. *fifteen = (double) loadAverage.ldavg[2] / loadAverage.fscale;
  155. }
  156. pid_t Platform_getMaxPid(void) {
  157. int maxPid;
  158. size_t size = sizeof(maxPid);
  159. int err = sysctlbyname("kern.pid_max", &maxPid, &size, NULL, 0);
  160. if (err) {
  161. return 999999;
  162. }
  163. return maxPid;
  164. }
  165. double Platform_setCPUValues(Meter* this, unsigned int cpu) {
  166. const Machine* host = this->host;
  167. const DragonFlyBSDMachine* dhost = (const DragonFlyBSDMachine*) host;
  168. unsigned int cpus = host->activeCPUs;
  169. const CPUData* cpuData;
  170. if (cpus == 1) {
  171. // single CPU box has everything in fpl->cpus[0]
  172. cpuData = &(dhost->cpus[0]);
  173. } else {
  174. cpuData = &(dhost->cpus[cpu]);
  175. }
  176. double percent;
  177. double* v = this->values;
  178. v[CPU_METER_NICE] = cpuData->nicePercent;
  179. v[CPU_METER_NORMAL] = cpuData->userPercent;
  180. if (host->settings->detailedCPUTime) {
  181. v[CPU_METER_KERNEL] = cpuData->systemPercent;
  182. v[CPU_METER_IRQ] = cpuData->irqPercent;
  183. this->curItems = 4;
  184. } else {
  185. v[CPU_METER_KERNEL] = cpuData->systemAllPercent;
  186. this->curItems = 3;
  187. }
  188. percent = sumPositiveValues(v, this->curItems);
  189. percent = MINIMUM(percent, 100.0);
  190. v[CPU_METER_FREQUENCY] = NAN;
  191. v[CPU_METER_TEMPERATURE] = NAN;
  192. return percent;
  193. }
  194. void Platform_setMemoryValues(Meter* this) {
  195. const Machine* host = this->host;
  196. this->total = host->totalMem;
  197. this->values[MEMORY_METER_USED] = host->usedMem;
  198. // this->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
  199. // this->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
  200. this->values[MEMORY_METER_BUFFERS] = host->buffersMem;
  201. this->values[MEMORY_METER_CACHE] = host->cachedMem;
  202. // this->values[MEMORY_METER_AVAILABLE] = "available memory"
  203. }
  204. void Platform_setSwapValues(Meter* this) {
  205. const Machine* host = this->host;
  206. this->total = host->totalSwap;
  207. this->values[SWAP_METER_USED] = host->usedSwap;
  208. // mtr->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux"
  209. // mtr->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux"
  210. }
  211. char* Platform_getProcessEnv(pid_t pid) {
  212. // TODO
  213. (void)pid; // prevent unused warning
  214. return NULL;
  215. }
  216. FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
  217. (void)pid;
  218. return NULL;
  219. }
  220. void Platform_getFileDescriptors(double* used, double* max) {
  221. Generic_getFileDescriptors_sysctl(used, max);
  222. }
  223. bool Platform_getDiskIO(DiskIOData* data) {
  224. struct statinfo dev_stats = { 0 };
  225. struct device_selection* dev_sel = NULL;
  226. int n_selected, n_selections;
  227. long sel_gen;
  228. dev_stats.dinfo = xCalloc(1, sizeof(struct devinfo));
  229. int ret = getdevs(&dev_stats);
  230. if (ret < 0) {
  231. CRT_debug("getdevs() failed [%d]: %s", ret, strerror(errno));
  232. free(dev_stats.dinfo);
  233. return false;
  234. }
  235. ret = selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen,
  236. dev_stats.dinfo->generation, dev_stats.dinfo->devices, dev_stats.dinfo->numdevs,
  237. NULL, 0, NULL, 0, DS_SELECT_ONLY, dev_stats.dinfo->numdevs, 1);
  238. if (ret < 0) {
  239. CRT_debug("selectdevs() failed [%d]: %s", ret, strerror(errno));
  240. free(dev_stats.dinfo);
  241. return false;
  242. }
  243. uint64_t bytesReadSum = 0;
  244. uint64_t bytesWriteSum = 0;
  245. uint64_t busyMsTimeSum = 0;
  246. for (int i = 0; i < dev_stats.dinfo->numdevs; i++) {
  247. const struct devstat* device = &dev_stats.dinfo->devices[dev_sel[i].position];
  248. switch (device->device_type & DEVSTAT_TYPE_MASK) {
  249. case DEVSTAT_TYPE_DIRECT:
  250. case DEVSTAT_TYPE_SEQUENTIAL:
  251. case DEVSTAT_TYPE_WORM:
  252. case DEVSTAT_TYPE_CDROM:
  253. case DEVSTAT_TYPE_OPTICAL:
  254. case DEVSTAT_TYPE_CHANGER:
  255. case DEVSTAT_TYPE_STORARRAY:
  256. case DEVSTAT_TYPE_FLOPPY:
  257. break;
  258. default:
  259. continue;
  260. }
  261. bytesReadSum += device->bytes_read;
  262. bytesWriteSum += device->bytes_written;
  263. busyMsTimeSum += (device->busy_time.tv_sec * 1000 + device->busy_time.tv_usec / 1000);
  264. }
  265. data->totalBytesRead = bytesReadSum;
  266. data->totalBytesWritten = bytesWriteSum;
  267. data->totalMsTimeSpend = busyMsTimeSum;
  268. free(dev_stats.dinfo);
  269. return true;
  270. }
  271. bool Platform_getNetworkIO(NetworkIOData* data) {
  272. struct ifaddrs* ifaddrs = NULL;
  273. if (getifaddrs(&ifaddrs) != 0)
  274. return false;
  275. for (const struct ifaddrs* ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
  276. if (!ifa->ifa_addr)
  277. continue;
  278. if (ifa->ifa_addr->sa_family != AF_LINK)
  279. continue;
  280. if (ifa->ifa_flags & IFF_LOOPBACK)
  281. continue;
  282. const struct if_data* ifd = (const struct if_data*)ifa->ifa_data;
  283. data->bytesReceived += ifd->ifi_ibytes;
  284. data->packetsReceived += ifd->ifi_ipackets;
  285. data->bytesTransmitted += ifd->ifi_obytes;
  286. data->packetsTransmitted += ifd->ifi_opackets;
  287. }
  288. freeifaddrs(ifaddrs);
  289. return true;
  290. }
  291. void Platform_getBattery(double* percent, ACPresence* isOnAC) {
  292. int life;
  293. size_t life_len = sizeof(life);
  294. if (sysctlbyname("hw.acpi.battery.life", &life, &life_len, NULL, 0) == -1)
  295. *percent = NAN;
  296. else
  297. *percent = life;
  298. int acline;
  299. size_t acline_len = sizeof(acline);
  300. if (sysctlbyname("hw.acpi.acline", &acline, &acline_len, NULL, 0) == -1)
  301. *isOnAC = AC_ERROR;
  302. else
  303. *isOnAC = acline == 0 ? AC_ABSENT : AC_PRESENT;
  304. }