From 3d7bb52e48c506ae73cdc45e947b2c820cb8cde4 Mon Sep 17 00:00:00 2001 From: Jos Dehaes Date: Wed, 12 Jan 2022 00:04:26 +0100 Subject: [PATCH] Squashed commit of the following: commit 7add05599de74d8047a9802fd673ae62a91a3372 Author: Jos Dehaes Date: Tue Jan 11 23:48:16 2022 +0100 bring in line with upstream commit ddb2fb0fac530ef10a20413f311e3d970d038d3f Author: Jos Dehaes Date: Tue Jan 11 23:35:21 2022 +0100 fix CPU name commit 01a1dda7346e9c1842225a3883e7dc5b99cdfc56 Merge: 3b6dac6 96ac114 Author: Jos Dehaes Date: Tue Jan 11 23:29:17 2022 +0100 merge main into freebsd commit 3b6dac640e70613b5549db291781e25fa6506202 Author: Jos Dehaes Date: Tue Jan 11 23:17:07 2022 +0100 disk IO working commit 30b33730b3de6567e5df3243fd372eb30123c181 Author: Jos Dehaes Date: Tue Jan 11 22:47:44 2022 +0100 reduce memleak drastically commit 682746ff0ee6c8b83f52bc44e44dd29dc9d7cdd2 Merge: 9fdf6d0 891051c Author: Jakob P. Liljenberg Date: Sat Dec 18 19:50:13 2021 +0100 Merge pull request #186 from GuillaumeGomez/freebsd Remove duplicated fstype commit 891051c8ab9984f155caa6bac2d170b0bb7d2a06 Author: Guillaume Gomez Date: Sun Dec 5 23:03:17 2021 +0100 Remove duplicated fstype commit 9fdf6d02044615da8b2c827abc002b2b71dd6db8 Author: Jos Dehaes Date: Sat Nov 27 20:55:49 2021 +0100 iterate over ALL pools commit 860a9fe472c7f4b6cc3f2ef38fb06101f5af7e21 Author: Jos Dehaes Date: Sat Nov 27 20:30:43 2021 +0100 RAII commit 7c433be4a6b6b0cac84713e976a63d661a9338ae Author: Jos Dehaes Date: Sun Nov 21 21:36:44 2021 +0100 regular filesystems + ZFS commit 70d9777908e8cd37088e1e19b8eb864a4357daba Author: Jos Dehaes Date: Sat Nov 20 22:57:00 2021 +0100 some comment + minor cleanup commit a61df3ff9815463da1cc0bdc713aa20dddc3b95e Author: Jos Dehaes Date: Sat Nov 20 22:51:49 2021 +0100 it does some IO!! commit f32358bd47802b7af2ef5fe7f03bb4d4b5c9d544 Author: Jos Dehaes Date: Tue Nov 16 22:30:55 2021 +0100 correct unit number commit d12b4d9d2359c9216ad5977a60de85aea865a92b Author: Jos Dehaes Date: Tue Nov 16 21:58:12 2021 +0100 readability commit a6602ff22b4b00ecb7138a09f5c5a15e1b13ca62 Author: Jos Dehaes Date: Tue Nov 16 21:53:33 2021 +0100 correctly iterate over devstat. still no idea how to map the devices we get back here to mountpoints commit e64610a163c23ec862c5f65d1accdd10a6163d47 Author: Jos Dehaes Date: Tue Nov 16 21:20:42 2021 +0100 RAII commit 98693aac2502ac4903b95dffcbc9664abe7c4739 Author: Jos Dehaes Date: Sun Oct 24 21:04:04 2021 +0200 devstat WIP commit 8940d68f47e3c22e9372495752ff16de2fa122b0 Author: aristocratos Date: Sun Oct 24 16:15:29 2021 +0200 Fixed cpu usage in cpu box, process cpu usage still wrong commit b547ccb25816497e5b31581d4cc102a24c1ec00a Author: aristocratos Date: Sun Oct 24 11:25:28 2021 +0200 Makefile even with OSX Makefile commit a5eabe20b6dc11367a49c51b614769bbb2436e1d Author: aristocratos Date: Sun Oct 24 11:20:46 2021 +0200 Updated Makefile commit 18451ceb0587d8304997f0e202117a73e5986b70 Author: aristocratos Date: Sun Oct 24 11:13:15 2021 +0200 Updated Makefile commit ca183b2b85fe729a8974715054bac4dc9d96d4a4 Author: aristocratos Date: Sun Oct 24 11:09:29 2021 +0200 Fixed up Makefile commit 235c95274f7ddd4a555ab1eb341776e0bd2c1dc5 Author: Jos Dehaes Date: Sat Oct 23 23:49:51 2021 +0200 fix boottime (not sure if needed) commit cc2a4987efde6f3cf8ef8ab96d6793c8220375cb Author: Jos Dehaes Date: Sat Oct 23 23:38:00 2021 +0200 temp commit 30ef6ee05057c6bbb0ddc96b7897de31e692907c Author: Jos Dehaes Date: Sat Oct 23 22:58:17 2021 +0200 cpu freq commit 735894b6caddedcf69a200e9d29cf0115c08bcce Author: aristocratos Date: Sat Oct 23 21:46:57 2021 +0200 Fixed leaks and proc tree mode commit cd3fd1a529f72eba979607f6e758ea855e01fd79 Author: Jos Dehaes Date: Fri Oct 22 22:27:32 2021 +0200 correct makefile commit cd644cfc55de21312d1492f8a8c285cfcfe44535 Author: Jos Dehaes Date: Fri Oct 22 15:49:39 2021 +0200 get rid of warnings commit 81b63652bfbaaa50d4da16f9e76a2d96036ba434 Author: Jos Dehaes Date: Fri Oct 22 15:43:22 2021 +0200 battery commit 41ba98695400a1a82aa6ddb884344b7c1e556f49 Author: Jos Dehaes Date: Fri Oct 22 15:31:04 2021 +0200 process args commit 137e876da636963ba2e758ea9fe3f4e91f15a662 Author: Jos Dehaes Date: Fri Oct 22 14:29:36 2021 +0200 remove debug commit 5249be0a40d12081ebdea05d6e3cad5c6bfa9e11 Author: Jos Dehaes Date: Fri Oct 22 14:28:40 2021 +0200 net stats commit 96461f4d948a29bbd6748223e8bb014c8d2f2902 Author: Jos Dehaes Date: Fri Oct 22 13:54:00 2021 +0200 sane memory stats commit e5ceeee1978ed5121f157da554fad9f51e1a5de1 Author: Jos Dehaes Date: Fri Oct 22 13:38:27 2021 +0200 processes commit 889433d4c651e3f7f9466f2b7d3353cc664c5950 Author: Jos Dehaes Date: Fri Oct 22 12:08:47 2021 +0200 cpu model + cpu load commit f037ab306a6e08bc4582933f6ecc8cf352dae6cd Author: Jos Dehaes Date: Fri Oct 22 10:31:37 2021 +0200 freebsd first work commit de6216792154477a5ab7f564c9467761daf842e2 Merge: a590dd3 a0ee404 Author: Jos Dehaes Date: Wed Oct 20 23:10:36 2021 +0200 Merge branch 'main' into OSX # Conflicts: # CHANGELOG.md # Makefile # src/btop.cpp commit a590dd3f67c9f2f8f8dc3fa49920a40a5e8b369d Author: aristocratos Date: Mon Oct 18 11:20:12 2021 +0200 Ignore format-truncation commit 4c30742d412cf552de22b3746a68cd62bfe867f2 Author: Jos Dehaes Date: Mon Oct 18 10:08:01 2021 +0200 comments about temp sensors commit 46030de77cb1409eb8b0247c331408a240b0227a Author: Jos Dehaes Date: Mon Oct 18 08:10:37 2021 +0200 available = total - used commit 4c228de0ef8a75275178bad081e568f85fc579fd Author: Jos Dehaes Date: Sun Oct 17 22:19:41 2021 +0200 use sysconf to get arg_max - seems simpler commit c60fc29f0f62831730c04c23cd9ffbefef50a1d8 Author: Jos Dehaes Date: Sun Oct 17 22:10:50 2021 +0200 arg_max should be int on macos commit 0b5a931a6d5e8d1a434e88e8fd0613ab948995fa Author: Jos Dehaes Date: Sun Oct 17 22:01:42 2021 +0200 only fetch max_args once commit 71d5cd5fd91511a90e43e5e6dbf7a1b6a3c3f9c8 Author: aristocratos Date: Sat Oct 16 23:24:07 2021 +0200 Reverted mutexes back to custom atomic bool based locks commit 3f34a67df68f3f451cb48b023b7cd42ccf933ce4 Author: Jos Dehaes Date: Sat Oct 16 21:47:55 2021 +0200 these helpers can be static commit fc19c46c8aab03e252f60f6f9447d7e1861cbcb9 Author: Jos Dehaes Date: Sat Oct 16 21:44:49 2021 +0200 code cleanup: put the code in .cpp to enable incremental build commit c252c618c043c4b85783f56363121877d0db0727 Author: Jos Dehaes Date: Sat Oct 16 21:09:21 2021 +0200 don't crash on intel commit 9f88187c29821148c7a5272926d204bd3eb39f89 Author: Jos Dehaes Date: Sat Oct 16 21:06:18 2021 +0200 small improvement commit 808f09c97465e8cf511f7690b40a69cd8a2efa15 Author: Jos Dehaes Date: Sat Oct 16 20:59:48 2021 +0200 don't iterate 3 times commit d8408336e3b8336025a087a2bf79a2d15f51ee66 Author: Jos Dehaes Date: Sat Oct 16 20:59:27 2021 +0200 remove debug commit 4f078c3beb960fe06f4d8b44b9c913e2aacf7625 Author: Jos Dehaes Date: Wed Oct 13 23:38:27 2021 +0200 more temperature (M1 + intel) commit 289880aaa6d1a3625c01e9d7643138343b29db53 Merge: 88a2528 3ffb212 Author: aristocratos Date: Sat Oct 16 19:37:09 2021 +0200 Merge branch 'OSX' of github.com:aristocratos/btop into OSX commit 88a2528ca3a2390f2c94c7f1a18ba982a5e5f2b2 Author: aristocratos Date: Sat Oct 16 19:34:10 2021 +0200 Merge changes from main commit 3ffb21203aa3f4ad978533a3f2b1e67e636381ea Author: Jos Dehaes Date: Sat Oct 16 19:20:45 2021 +0200 release a bit more - still has negative temps for 2 cores on my system commit 70b48710626ba22df496ba741625ce03cba6abbd Author: aristocratos Date: Sat Oct 16 01:59:44 2021 +0200 Fixed leaks in Mem and attempt at fixing leaks in sensors.cpp commit fbae907720afbae47162666b6b0aea974be80c07 Author: Jos Dehaes Date: Fri Oct 15 23:41:37 2021 +0200 temperature sensors via IOKit commit cef0f0a68daa88a380000ce200a364e4701ba93a Author: aristocratos Date: Fri Oct 15 18:39:17 2021 +0200 Process command line arguments commit 921cfa01ffc104c57f2825d0bca648233ddff191 Author: aristocratos Date: Wed Oct 13 23:20:15 2021 +0200 Re-enable setuid and set default SU_GROUP to wheel for OSX commit a416c888c7356634ef7a5286130a56160d72f50a Author: Jos Dehaes Date: Wed Oct 13 21:16:41 2021 +0200 temperature commit e7afe00ce7960bfe6fb6ba2a9a2f5d498c5b4fca Author: aristocratos Date: Wed Oct 13 12:54:43 2021 +0200 Cpu usage working again commit 4193ef8921617e48ce07ce95d898386f7dd77f43 Author: aristocratos Date: Wed Oct 13 10:36:51 2021 +0200 Fixed cpu lazy sorting commit 93fcb6ff04d84c008ed9f7d28918eb9eb8adf740 Author: aristocratos Date: Tue Oct 12 22:22:45 2021 +0200 Update README.md commit 683354cd2ed8add79c6940e51800431e8b020635 Merge: 8a399c4 6d724d6 Author: aristocratos Date: Tue Oct 12 22:19:30 2021 +0200 Merge pull request #80 from ShrirajHegde/OSX Add github workflow for MacOS commit 8a399c499af87883a7e4b2cf7f6d193f76909923 Author: aristocratos Date: Tue Oct 12 21:50:46 2021 +0200 pointer to smart pointer, first pass commit 772605003af9c1c00f163ff75279cd0055074c96 Author: aristocratos Date: Tue Oct 12 18:54:38 2021 +0200 Fixed detailed memory not updating commit 28cb67753332fe8c93d23cbf8e4db636fc6823d6 Author: Jos Dehaes Date: Mon Oct 11 22:19:25 2021 +0200 more memory free-up - still leaks like crazy commit 304457863f6cdccf82fbe1cca3078c7f5d9f97cd Author: Jos Dehaes Date: Mon Oct 11 21:48:07 2021 +0200 more RAII cleanup commit 82e2e3c55c23e37dbf226952cca4587df3522fa3 Author: aristocratos Date: Mon Oct 11 12:40:25 2021 +0200 Removed non present cpu fields and fixed calculation for selectable cpu field graphs commit 68603f2b37e34828f78f0f675f66bd835dae8325 Author: aristocratos Date: Mon Oct 11 10:57:04 2021 +0200 RAII Wrappers for Cpu::get_battery() commit d5cb24fbeb19ae57507e1715aa402b2dc33f9b6b Author: Jos Dehaes Date: Sun Oct 10 20:23:11 2021 +0200 RAII commit 8fad5a61bee973c22f3a11fd7fee2c4e40390bbb Author: Jos Dehaes Date: Sat Oct 9 21:44:16 2021 +0200 get more disk IO stats commit 7fa903cf160b391fb316ea32a60984921a174066 Author: Jos Dehaes Date: Sat Oct 9 21:43:48 2021 +0200 fix build commit 98036db660e306626d41fb1b67d9938d9ffe168a Author: Jos Dehaes Date: Sat Oct 9 21:18:25 2021 +0200 remove unnecessary uptime param commit aae7ae35caec64ba611d9b78b24c0ad2716f333e Author: Jos Dehaes Date: Sat Oct 9 21:07:23 2021 +0200 remove debug logging commit 5187420b04973edcdd25c1b639795a2c8539b2d9 Author: Jos Dehaes Date: Sat Oct 9 21:06:43 2021 +0200 fix process elapsed time commit 89582c0ea6e9bdd7a658b5583d2ce2c5deeee8b0 Author: Jos Dehaes Date: Sat Oct 9 21:06:29 2021 +0200 don't double free commit 6d724d6155bad8c084e4c9bdb1d7dda13543a5ac Author: Shriraj Hegde Date: Sat Oct 9 21:46:33 2021 +0530 Change Upload filename commit 4f94ecc8ad7d7cdb7daed82fd98a5634d723ec6b Author: Shriraj Hegde Date: Sat Oct 9 21:40:11 2021 +0530 Fix upload path commit e1d6d0a1f224474417078a0a1b98a6c4b5c94a53 Author: Shriraj Hegde Date: Sat Oct 9 21:36:13 2021 +0530 Skip installing gcc via Homebrew Change job name commit 02cdd9d759d163519048d24746273e4eda2149d0 Author: Shriraj Hegde Date: Sat Oct 9 21:30:40 2021 +0530 Fix uploading Remove distclean commit 39eb6c396fc31f798d23c21e7f4b313930dca982 Author: Shriraj Hegde Date: Sat Oct 9 21:26:57 2021 +0530 Disable static compilation commit 099592bccdafe8d8d8c421bcad3e036cbea580ce Author: aristocratos Date: Sat Oct 9 17:52:10 2021 +0200 Ignore empty pid 0 to fix tree mode commit a28e17556e74bb618fc2b6eb74f250b139406cdd Author: Shriraj Hegde Date: Sat Oct 9 21:17:11 2021 +0530 Add workflow for MacOS commit aee9179c0a98bce54b55c8bb8ebdc1ac99bad69e Author: aristocratos Date: Sat Oct 9 17:36:46 2021 +0200 Disable failed tty mode detection for OSX commit 4b7b98058d7190a494fa522834217d786d71e5fb Author: aristocratos Date: Sat Oct 9 11:28:32 2021 +0200 Fixed disk io and added io activity based on read/write commit bfa0629e7d1e50f88d9a1207930e2f39270fc189 Author: Jos Dehaes Date: Fri Oct 8 22:28:10 2021 +0200 fill in 0 for ioticks commit a016ff8a039634bc37a22a4a84b31055b3cfab37 Author: Jos Dehaes Date: Fri Oct 8 22:16:01 2021 +0200 disk io from IOreg. Does not show any io though commit f98606c6db09d50c7ae234437ea03eda2cc8739c Author: Jos Dehaes Date: Fri Oct 8 09:32:06 2021 +0200 per process IO stats commit c8b50ed4883103f66c9ed869fd0252d48f18f58f Author: Jos Dehaes Date: Fri Oct 8 00:11:08 2021 +0200 don't show autofs, it's useless commit c4df64d4409c511847d76b37a0794b8bb4f6942d Author: aristocratos Date: Thu Oct 7 18:41:49 2021 +0200 Fixed compile time display for gmake and command timings commit b3e6f495f76bd71fba3442a557df22afdd3e642d Author: aristocratos Date: Thu Oct 7 18:26:15 2021 +0200 Fixed clk_tck -> clkTck commit e53799188ffc24f3948e7ced375b7fbb53911247 Author: aristocratos Date: Thu Oct 7 18:25:14 2021 +0200 Fixed better detection for OSX commit b864edf984e8f0daa0f10a6ee305074c68efeb42 Author: aristocratos Date: Thu Oct 7 18:24:37 2021 +0200 Fixed cumulative cpu usage commit 6a3c5d9b976c711461d08c373483c84bb69db257 Author: aristocratos Date: Thu Oct 7 13:20:30 2021 +0200 Proc::collect() better cpu percent accurazy commit 84d0596294432baef1b10f7d3a566a07382f69d3 Merge: 3564f8e 98e1e87 Author: aristocratos Date: Thu Oct 7 12:56:55 2021 +0200 Merge branch 'OSX' of github.com:aristocratos/btop into OSX commit 3564f8e4c26f04ec89482606e68a452a3e5f9693 Author: aristocratos Date: Thu Oct 7 12:56:27 2021 +0200 Proc::collect() fixed cputimes and cpu percentage calc commit 98e1e874059fe69968d4bb7e4685f292e3b7fd65 Merge: 60c5636 d96fdd7 Author: Jos Dehaes Date: Thu Oct 7 11:32:41 2021 +0200 Merge branch 'main' into OSX commit 60c5636cd7c18e6c806cb4cd88846db10092dd57 Author: Jos Dehaes Date: Wed Oct 6 22:45:54 2021 +0200 fix warning commit 489e446152ce00f8e92db8c4e10759f96e38f17f Author: Jos Dehaes Date: Wed Oct 6 22:38:40 2021 +0200 details + process states commit 7e5a808c731772bab35204f2e286975fca334f54 Author: Jos Dehaes Date: Wed Oct 6 22:38:19 2021 +0200 avoid details crash commit 9c9da4606b3f93c0701b820a875fb3db5d0c3daf Author: Jos Dehaes Date: Wed Oct 6 21:03:21 2021 +0200 fix quit on macos commit ec7415384d9d9b42892a1e36ff55bf7a117a253b Author: Jos Dehaes Date: Wed Oct 6 00:41:37 2021 +0200 fix mistake in makefile commit 5ac8fa4c8a8f73efac6ac1f15e458ed3b2c1164b Author: Jos Dehaes Date: Wed Oct 6 18:56:13 2021 +0200 don't show /dev commit d901bbebd94ec45c45431fcd1ab37a974f783d9b Author: aristocratos Date: Wed Oct 6 17:27:51 2021 +0200 Ignore tags and other branches commit c7f1e71e29bafb5334cd4e256c662e5ee6303b1b Author: Jos Dehaes Date: Wed Oct 6 16:00:41 2021 +0200 comment commit b9d58e3faf5b60ad4f5f65454b454ff52deb5eaa Author: Jos Dehaes Date: Wed Oct 6 15:55:58 2021 +0200 impossible to get CPU freq on M1 apparently commit 66072711c24b2694c4a0054c58829095ed97ada2 Author: Jos Dehaes Date: Wed Oct 6 15:33:43 2021 +0200 detect full commit 6bb0e930a2c44b4fb7d0d02a40f210fa12fdc657 Author: Jos Dehaes Date: Wed Oct 6 00:38:46 2021 +0200 CPU freq in GHz commit a5f10f1a0ff9d10fa8d9d3038aa160b2380aa294 Author: Jos Dehaes Date: Wed Oct 6 15:13:18 2021 +0200 check array length commit 155c848b97e1385bd29eaba91a87ecfb1d846bf7 Author: Jos Dehaes Date: Wed Oct 6 15:05:20 2021 +0200 switch to other way to get CPU freq (still does not work) commit cf51ba2ebe88dddd943040ced484581dc260c3c8 Author: Jos Dehaes Date: Wed Oct 6 14:45:44 2021 +0200 remove some warnings commit 775dff5f72b0631bba0ed01d72374d8c1d1cd059 Author: Jos Dehaes Date: Wed Oct 6 14:16:45 2021 +0200 fix link commit 8c67967775fb98bda80eee40cb6833a00bfb93a1 Author: Jos Dehaes Date: Wed Oct 6 14:16:38 2021 +0200 reduce diff more commit 70b47d2ca8a7e0927cf9613e8fe776d2405f60ee Author: Jos Dehaes Date: Wed Oct 6 14:10:23 2021 +0200 reduce diff with main branch commit ca9cb48054c5850cbb3c23d368e04644a1885de0 Merge: d0c6c0a c66b46f Author: Jos Dehaes Date: Wed Oct 6 13:49:20 2021 +0200 Merge remote-tracking branch 'origin/main' into OSX # Conflicts: # Makefile # src/osx/btop_collect.cpp commit c66b46f850d31c100226e519c55b39df9129aeb8 Author: Jos Dehaes Date: Wed Oct 6 13:41:57 2021 +0200 battery state via CoreFoundation commit d0c6c0a362d8dc4f76e2901d7b406a70d8a69b6b Author: Jos Dehaes Date: Wed Oct 6 10:51:36 2021 +0200 all disks + load averages commit ca67526dc175dba7d98193a6462e70ef1acab194 Author: Jos Dehaes Date: Wed Oct 6 10:33:55 2021 +0200 show all disks commit 56119f99a95ca541d6ab744a9ded54c84e0a3184 Author: Jos Dehaes Date: Wed Oct 6 00:17:41 2021 +0200 procs sorting/filtering commit 8d86011d72a07b3f86f6b525b026b805667a3172 Author: Jos Dehaes Date: Tue Oct 5 23:42:17 2021 +0200 battery states commit a9b64d62e4abf96fe3edcfa660871caef2041d0f Author: Jos Dehaes Date: Tue Oct 5 23:24:59 2021 +0200 battery hack works on M1 commit ce5103114246f5549017ef4823442a0ea916e1dd Author: Jos Dehaes Date: Tue Oct 5 23:18:22 2021 +0200 ugly hack to get battery commit d5e6725c6cd6973cc75dfe2993892e1c25ac8d38 Author: Jos Dehaes Date: Tue Oct 5 22:42:42 2021 +0200 CPU stuff commit 5c02bd8c8380c5c74837b982987b7bf31103245f Author: Jos Dehaes Date: Tue Oct 5 21:25:42 2021 +0200 network commit d5da9d49835cdb23ad5830c000e1b93fcb8f46fd Author: Jos Dehaes Date: Tue Oct 5 15:43:05 2021 +0200 correct cached size commit 5f11aba504254d86ed9a2319967590d50988e16f Author: Jos Dehaes Date: Tue Oct 5 12:03:48 2021 +0200 vm stats from syscall + swap commit 776fc968529e8e7e38fc3168c83b2727198722ad Author: Jos Dehaes Date: Tue Oct 5 10:48:07 2021 +0200 seems to work indeed commit 7b40e2835a085bfe0d5eb40f367a1ed50353e4d9 Author: Jos Dehaes Date: Tue Oct 5 10:46:14 2021 +0200 allow override optimization flag commit 005ea24e4c9bcc6608671a0635126bf10bdece34 Author: Jos Dehaes Date: Tue Oct 5 10:09:24 2021 +0200 update Makefile commit af8cec9debac328645f0f4e2f136e4572d88332e Author: Jos Dehaes Date: Mon Oct 4 15:32:55 2021 +0200 some more params commit 7ebe4f7594599ccafbf72a004ce37f965549f64d Author: Jos Dehaes Date: Mon Oct 4 15:15:55 2021 +0200 show more disks commit e50a56394a3702b6616d03694f5d23a5f7ba2f1c Author: Jos Dehaes Date: Mon Oct 4 14:52:56 2021 +0200 disks show something commit 6497a8c2021b67057dfed26488aec0d82e919a90 Author: Jos Dehaes Date: Mon Oct 4 09:15:35 2021 +0200 reformat commit 28e152b80c07c1fb9a4049cca8510f8bdc31adbb Author: Jos Dehaes Date: Sun Oct 3 23:21:13 2021 +0200 decrease diff with upstream commit 40da88e9ca91f3db540b63b15da5bc34dc9175e9 Author: Jos Dehaes Date: Sun Oct 3 22:56:14 2021 +0200 try to get disks to show commit eaf2bb56a503fd2bf3787bfd90b5bc8640b7d628 Author: Jos Dehaes Date: Sun Oct 3 22:42:01 2021 +0200 don't crash on deque::back() commit f66b6f712c361fafbe6d0cfd8f8c0838dc1bb4d2 Author: Jos Dehaes Date: Sun Oct 3 22:08:21 2021 +0200 cpu freq, name & process uid/name commit 34a8a61f4de964d3137637b045be8738c4b1b6e5 Author: Jos Dehaes Date: Sun Oct 3 21:46:11 2021 +0200 basic process info commit 29bb2dcc5fcc6b946511068f036590c18fb459ce Author: Jos Dehaes Date: Sun Oct 3 21:45:39 2021 +0200 initialize mutex (needed on macos apparently and not on linux) commit fb5970b0005793d760e652348d6acc30fb570f70 Author: Jos Dehaes Date: Sat Oct 2 23:53:41 2021 +0200 comment commit 49d16cdddd56ba5631269eb2ffbec87d9f36f81e Author: Jos Dehaes Date: Sat Oct 2 23:51:29 2021 +0200 extract delimiters commit 3db9d6647650bd836201b8150e320a7fbf28e3c2 Author: Jos Dehaes Date: Sat Oct 2 23:48:28 2021 +0200 first infos on macos: memory used & free commit f8acb2f8542429677116799ddb5f442488cf3f4f Author: Jos Dehaes Date: Tue Sep 28 23:37:03 2021 +0200 make it compile on macos (M1 - arm64). Does not run though commit bbba17cd35248e4e9ec9bfc1b113758cfcffde1f Author: Jos Dehaes Date: Wed Oct 6 10:51:36 2021 +0200 all disks + load averages commit 548203e93dfaf3ec9f24086bee08aac85891c4df Author: Jos Dehaes Date: Wed Oct 6 10:33:55 2021 +0200 show all disks commit 0ab2be39857fb3dcdb13b49bc9155f17c7d82a4e Author: Jos Dehaes Date: Wed Oct 6 00:17:41 2021 +0200 procs sorting/filtering commit 096104c90b571e931a3a7d9c813dbfc9aa47e212 Author: Jos Dehaes Date: Tue Oct 5 23:42:17 2021 +0200 battery states commit 0ad93684c2a72293b23d6a2163c9ec51b499dfa3 Author: Jos Dehaes Date: Tue Oct 5 23:24:59 2021 +0200 battery hack works on M1 commit c75b0f1cea34e6c4c70332ba7e2572ec9b70deef Author: Jos Dehaes Date: Tue Oct 5 23:18:22 2021 +0200 ugly hack to get battery commit 600b4f72b3bbbcd85bf5d148942bce7be8cf0b72 Author: Jos Dehaes Date: Tue Oct 5 22:42:42 2021 +0200 CPU stuff commit 4eb812d52c6e179ae386df0156021d7c35cbe5a3 Author: Jos Dehaes Date: Tue Oct 5 21:25:42 2021 +0200 network commit 899be68a78270216bfdcca5f0c87668a87c8792f Author: Jos Dehaes Date: Tue Oct 5 15:43:05 2021 +0200 correct cached size commit a1c7f935e3a5661688c0de1ad3226f7bc43b9979 Author: Jos Dehaes Date: Tue Oct 5 12:03:48 2021 +0200 vm stats from syscall + swap commit bd1050a7404f9766a0125523c868a27d5cfac8e8 Author: Jos Dehaes Date: Tue Oct 5 10:48:07 2021 +0200 seems to work indeed commit 5094b73758ee88617e8d5ce876211e1efa298769 Author: Jos Dehaes Date: Tue Oct 5 10:46:14 2021 +0200 allow override optimization flag commit 8811270332bc2276cd18c1116f4d3c2d64a6f721 Author: Jos Dehaes Date: Tue Oct 5 10:09:24 2021 +0200 update Makefile commit 42f966f448b9ad571db7849dc8fd525e0fe72309 Author: Jos Dehaes Date: Mon Oct 4 15:32:55 2021 +0200 some more params commit c1e6d6a62e2810f80fac372e666ec169540b0591 Author: Jos Dehaes Date: Mon Oct 4 15:15:55 2021 +0200 show more disks commit 50fcdaa8543f717bf36146cfa38636fd39009f23 Author: Jos Dehaes Date: Mon Oct 4 14:52:56 2021 +0200 disks show something commit 264bf2d7da0e3fabb5987cddee73762e52170a51 Author: Jos Dehaes Date: Mon Oct 4 09:15:35 2021 +0200 reformat commit 1fd625086ba42e9440c463ae940563d934b8b5c3 Author: Jos Dehaes Date: Sun Oct 3 23:21:13 2021 +0200 decrease diff with upstream commit 17f9f3703c1dd52a86176131a2239cd52ee285bd Author: Jos Dehaes Date: Sun Oct 3 22:56:14 2021 +0200 try to get disks to show commit 8462ae6431fcfe1985d1bbb4404452ddc03cc1de Author: Jos Dehaes Date: Sun Oct 3 22:42:01 2021 +0200 don't crash on deque::back() commit 78bce5b5a6c1c0f3b38f802acad49ec2e32482d6 Merge: 53e379d f9505a4 Author: Jos Dehaes Date: Sun Oct 3 22:08:34 2021 +0200 Merge branch 'aristocratos:main' into main commit 53e379d74dffe2282b089450728501b51d13d199 Author: Jos Dehaes Date: Sun Oct 3 22:08:21 2021 +0200 cpu freq, name & process uid/name commit 2a44b307ef9e947c1007a86988876668a5731e64 Author: Jos Dehaes Date: Sun Oct 3 21:46:11 2021 +0200 basic process info commit 66534eb5b50753217687de0414d390bcb2a14cf1 Author: Jos Dehaes Date: Sun Oct 3 21:45:39 2021 +0200 initialize mutex (needed on macos apparently and not on linux) commit 0983917f26948d83fd8da103903ec99f88058d87 Author: Jos Dehaes Date: Sat Oct 2 23:53:41 2021 +0200 comment commit 9732507248b30139d4af54615945c0b4737cff7d Author: Jos Dehaes Date: Sat Oct 2 23:51:29 2021 +0200 extract delimiters commit 6e704ce8387041c33022459fb6a084362f82a72c Merge: fe4db7c 7bfbd83 Author: Jos Dehaes Date: Sat Oct 2 23:48:43 2021 +0200 Merge branch 'main' of github.com:joske/btop commit fe4db7c16cd349053385eda62e9f2df2e7344d3d Author: Jos Dehaes Date: Sat Oct 2 23:48:28 2021 +0200 first infos on macos: memory used & free commit 7bfbd83a476c8cbe3b74e22b1e44f201026bf9d1 Merge: 8c8139b a15f961 Author: Jos Dehaes Date: Fri Oct 1 17:05:26 2021 +0200 Merge branch 'aristocratos:main' into main commit 8c8139bd1df0849061b5e1425a3138d5f45ec149 Merge: 679d21c a246c09 Author: Jos Dehaes Date: Wed Sep 29 21:50:00 2021 +0200 Merge branch 'aristocratos:main' into main commit 679d21cd223b1928e8dde95cac25e8bfb412bedf Merge: 4c70c5b a49b8f9 Author: Jos Dehaes Date: Wed Sep 29 20:23:34 2021 +0200 Merge branch 'aristocratos:main' into main commit 4c70c5bdd98bfcc0ae9d9d9e8e3713dc8af71d03 Merge: 84a9746 c70667e Author: Jos Dehaes Date: Wed Sep 29 08:30:05 2021 +0200 Merge branch 'aristocratos:main' into main commit 84a974695afaadc53d1d8576ea66255166ede482 Author: Jos Dehaes Date: Tue Sep 28 23:37:03 2021 +0200 make it compile on macos (M1 - arm64). Does not run though --- Makefile | 7 +- src/btop.cpp | 3 + src/freebsd/btop_collect.cpp | 1348 ++++++++++++++++++++++++++++++++++ 3 files changed, 1357 insertions(+), 1 deletion(-) create mode 100644 src/freebsd/btop_collect.cpp diff --git a/Makefile b/Makefile index 4b2d0a0..f844c21 100644 --- a/Makefile +++ b/Makefile @@ -86,7 +86,7 @@ else ifeq ($(PLATFORM_LC),freebsd) PLATFORM_DIR := freebsd THREADS := $(shell getconf NPROCESSORS_ONLN 2>/dev/null || echo 1) SU_GROUP := root - override ADDFLAGS += -lstdc++ -lm -lkvm -Wl,-rpath=/usr/local/lib/gcc11 + override ADDFLAGS += -lstdc++ -lm -lkvm -ldevstat -Wl,-rpath=/usr/local/lib/gcc11 export MAKE = gmake else ifeq ($(PLATFORM_LC),macos) PLATFORM_DIR := osx @@ -125,6 +125,10 @@ override LDFLAGS += $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS) INC := -I$(INCDIR) -I$(SRCDIR) SU_USER := root +ifdef DEBUG + override OPTFLAGS := -O0 -g +endif + SOURCES := $(shell find $(SRCDIR) -maxdepth 1 -type f -name *.$(SRCEXT)) SOURCES += $(shell find $(SRCDIR)/$(PLATFORM_DIR) -type f -name *.$(SRCEXT)) @@ -190,6 +194,7 @@ install: @printf "\033[1;92mInstalling themes to: \033[1;97m$(DESTDIR)$(PREFIX)/share/btop/themes\033[0m\n" @cp -pr themes $(DESTDIR)$(PREFIX)/share/btop + #? Set SUID bit for btop as $SU_USER in $SU_GROUP setuid: @printf "\033[1;97mFile: $(DESTDIR)$(PREFIX)/bin/btop\n" diff --git a/src/btop.cpp b/src/btop.cpp index 8ad11e8..6bf3ff4 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -19,6 +19,9 @@ tab-size = 4 #include #include #include +#ifdef __FreeBSD__ +#include +#endif #include #include #include diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp new file mode 100644 index 0000000..e913c79 --- /dev/null +++ b/src/freebsd/btop_collect.cpp @@ -0,0 +1,1348 @@ +/* Copyright 2021 Aristocratos (jakob@qvantnet.com) + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +indent = tab +tab-size = 4 +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +using std::clamp, std::string_literals::operator""s, std::cmp_equal, std::cmp_less, std::cmp_greater; +using std::ifstream, std::numeric_limits, std::streamsize, std::round, std::max, std::min; +namespace fs = std::filesystem; +namespace rng = std::ranges; +using namespace Tools; + +//? --------------------------------------------------- FUNCTIONS ----------------------------------------------------- + +namespace Cpu { + vector core_old_totals; + vector core_old_idles; + vector available_fields = {"total"}; + vector available_sensors = {"Auto"}; + cpu_info current_cpu; + bool got_sensors = false, cpu_temp_only = false; + + //* Populate found_sensors map + bool get_sensors(); + + //* Get current cpu clock speed + string get_cpuHz(); + + //* Search /proc/cpuinfo for a cpu name + string get_cpuName(); + + struct Sensor { + fs::path path; + string label; + int64_t temp = 0; + int64_t high = 0; + int64_t crit = 0; + }; + + string cpu_sensor; + vector core_sensors; + unordered_flat_map core_mapping; +} // namespace Cpu + +namespace Mem { + double old_uptime; + std::vector zpools; + + void get_zpools(); +} + +namespace Shared { + + fs::path passwd_path; + uint64_t totalMem; + long pageSize, clkTck, coreCount, physicalCoreCount, arg_max; + int totalMem_len; + long bootTime; + + void init() { + //? Shared global variables init + int mib[2]; + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + int ncpu; + size_t len = sizeof(ncpu); + if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) { + Logger::warning("Could not determine number of cores, defaulting to 1."); + } else { + coreCount = ncpu; + } + + pageSize = sysconf(_SC_PAGE_SIZE); + if (pageSize <= 0) { + pageSize = 4096; + Logger::warning("Could not get system page size. Defaulting to 4096, processes memory usage might be incorrect."); + } + + clkTck = sysconf(_SC_CLK_TCK); + if (clkTck <= 0) { + clkTck = 100; + Logger::warning("Could not get system clock ticks per second. Defaulting to 100, processes cpu usage might be incorrect."); + } + + int64_t memsize = 0; + size_t size = sizeof(memsize); + if (sysctlbyname("hw.physmem", &memsize, &size, NULL, 0) < 0) { + Logger::warning("Could not get memory size"); + } + totalMem = memsize; + + struct timeval result; + size = sizeof(result); + if (sysctlbyname("kern.boottime", &result, &size, NULL, 0) < 0) { + Logger::warning("Could not get boot time"); + } else { + bootTime = result.tv_sec; + } + + //* Get maximum length of process arguments + arg_max = sysconf(_SC_ARG_MAX); + + //? Init for namespace Cpu + Cpu::current_cpu.core_percent.insert(Cpu::current_cpu.core_percent.begin(), Shared::coreCount, {}); + Cpu::current_cpu.temp.insert(Cpu::current_cpu.temp.begin(), Shared::coreCount + 1, {}); + Cpu::core_old_totals.insert(Cpu::core_old_totals.begin(), Shared::coreCount, 0); + Cpu::core_old_idles.insert(Cpu::core_old_idles.begin(), Shared::coreCount, 0); + Cpu::collect(); + for (auto &[field, vec] : Cpu::current_cpu.cpu_percent) { + if (not vec.empty() and not v_contains(Cpu::available_fields, field)) Cpu::available_fields.push_back(field); + } + Cpu::cpuName = Cpu::get_cpuName(); + Cpu::got_sensors = Cpu::get_sensors(); + Cpu::core_mapping = Cpu::get_core_mapping(); + + //? Init for namespace Mem + Mem::old_uptime = system_uptime(); + Mem::collect(); + Mem::get_zpools(); + } + +} // namespace Shared + +namespace Cpu { + string cpuName; + string cpuHz; + bool has_battery = true; + tuple current_bat; + + const array time_names = {"user", "nice", "system", "idle"}; + + unordered_flat_map cpu_old = { + {"totals", 0}, + {"idles", 0}, + {"user", 0}, + {"nice", 0}, + {"system", 0}, + {"idle", 0} + }; + + string get_cpuName() { + string name; + char buffer[1024]; + size_t size = sizeof(buffer); + if (sysctlbyname("hw.model", &buffer, &size, NULL, 0) < 0) { + Logger::error("Failed to get CPU name"); + return name; + } + name = string(buffer); + + auto name_vec = ssplit(name); + + if ((s_contains(name, "Xeon"s) or v_contains(name_vec, "Duo"s)) and v_contains(name_vec, "CPU"s)) { + auto cpu_pos = v_index(name_vec, "CPU"s); + if (cpu_pos < name_vec.size() - 1 and not name_vec.at(cpu_pos + 1).ends_with(')')) + name = name_vec.at(cpu_pos + 1); + else + name.clear(); + } else if (v_contains(name_vec, "Ryzen"s)) { + auto ryz_pos = v_index(name_vec, "Ryzen"s); + name = "Ryzen" + (ryz_pos < name_vec.size() - 1 ? ' ' + name_vec.at(ryz_pos + 1) : "") + (ryz_pos < name_vec.size() - 2 ? ' ' + name_vec.at(ryz_pos + 2) : ""); + } else if (s_contains(name, "Intel"s) and v_contains(name_vec, "CPU"s)) { + auto cpu_pos = v_index(name_vec, "CPU"s); + if (cpu_pos < name_vec.size() - 1 and not name_vec.at(cpu_pos + 1).ends_with(')') and name_vec.at(cpu_pos + 1) != "@") + name = name_vec.at(cpu_pos + 1); + else + name.clear(); + } else + name.clear(); + + if (name.empty() and not name_vec.empty()) { + for (const auto &n : name_vec) { + if (n == "@") break; + name += n + ' '; + } + name.pop_back(); + for (const auto& replace : {"Processor", "CPU", "(R)", "(TM)", "Intel", "AMD", "Core"}) { + name = s_replace(name, replace, ""); + name = s_replace(name, " ", " "); + } + name = trim(name); + } + + return name; + } + + bool get_sensors() { + got_sensors = false; + if (Config::getB("show_coretemp") and Config::getB("check_temp")) { + int32_t temp; + size_t size = sizeof(temp); + if (sysctlbyname("dev.cpu.0.temperature", &temp, &size, NULL, 0) < 0) { + Logger::warning("Could not get temp sensor - maybe you need to load the coretemp module"); + } else { + got_sensors = true; + int temp; + size_t size = sizeof(temp); + sysctlbyname("dev.cpu.0.coretemp.tjmax", &temp, &size, NULL, 0); //asuming the max temp is same for all cores + temp = (temp - 2732) / 10; // since it's an int, it's multiplied by 10, and offset to absolute zero... + current_cpu.temp_max = temp; + } + } + return got_sensors; + } + + void update_sensors() { + int temp; + size_t size = sizeof(temp); + sysctlbyname("hw.acpi.thermal.tz0.temperature", &temp, &size, NULL, 0); + temp = (temp - 2732) / 10; // since it's an int, it's multiplied by 10, and offset to absolute zero... + current_cpu.temp.at(0).push_back(temp); + if (current_cpu.temp.at(0).size() > 20) + current_cpu.temp.at(0).pop_front(); + + for (int i = 0; i < Shared::coreCount; i++) { + string s = "dev.cpu." + std::to_string(i) + ".temperature"; + if (sysctlbyname(s.c_str(), &temp, &size, NULL, 0) < 0) { + Logger::warning("Could not get temp sensor - maybe you need to load the coretemp module"); + } else { + temp = (temp - 2732) / 10; + if (cmp_less(i + 1, current_cpu.temp.size())) { + current_cpu.temp.at(i + 1).push_back(temp); + if (current_cpu.temp.at(i + 1).size() > 20) + current_cpu.temp.at(i + 1).pop_front(); + } + } + } + + } + + string get_cpuHz() { + unsigned int freq = 1; + size_t size = sizeof(freq); + + if (sysctlbyname("dev.cpu.0.freq", &freq, &size, NULL, 0) < 0) { + return ""; + } + return std::to_string(freq / 1000.0 ).substr(0, 3); // seems to be in MHz + } + + auto get_core_mapping() -> unordered_flat_map { + unordered_flat_map core_map; + if (cpu_temp_only) return core_map; + + for (long i = 0; i < Shared::coreCount; i++) { + core_map[i] = i; + } + + //? If core mapping from cpuinfo was incomplete try to guess remainder, if missing completely, map 0-0 1-1 2-2 etc. + if (cmp_less(core_map.size(), Shared::coreCount)) { + if (Shared::coreCount % 2 == 0 and (long) core_map.size() == Shared::coreCount / 2) { + for (int i = 0, n = 0; i < Shared::coreCount / 2; i++) { + if (std::cmp_greater_equal(n, core_sensors.size())) n = 0; + core_map[Shared::coreCount / 2 + i] = n++; + } + } else { + core_map.clear(); + for (int i = 0, n = 0; i < Shared::coreCount; i++) { + if (std::cmp_greater_equal(n, core_sensors.size())) n = 0; + core_map[i] = n++; + } + } + } + + //? Apply user set custom mapping if any + const auto &custom_map = Config::getS("cpu_core_map"); + if (not custom_map.empty()) { + try { + for (const auto &split : ssplit(custom_map)) { + const auto vals = ssplit(split, ':'); + if (vals.size() != 2) continue; + int change_id = std::stoi(vals.at(0)); + int new_id = std::stoi(vals.at(1)); + if (not core_map.contains(change_id) or cmp_greater(new_id, core_sensors.size())) continue; + core_map.at(change_id) = new_id; + } + } catch (...) { + } + } + + return core_map; + } + + auto get_battery() -> tuple { + if (not has_battery) return {0, 0, ""}; + + long seconds = -1; + uint32_t percent = -1; + size_t size = sizeof(percent); + string status = "discharging"; + if (sysctlbyname("hw.acpi.battery.life", &percent, &size, NULL, 0) < 0) { + has_battery = false; + } else { + has_battery = true; + size_t size = sizeof(seconds); + if (sysctlbyname("hw.acpi.battery.time", &seconds, &size, NULL, 0) < 0) { + seconds = 0; + } + int state; + size = sizeof(state); + if (sysctlbyname("hw.acpi.battery.state", &state, &size, NULL, 0) < 0) { + status = "unknown"; + } else { + if (state == 2) { + status = "charging"; + } + } + if (percent == 100) { + status = "full"; + } + } + + return {percent, seconds, status}; + } + + auto collect(const bool no_update) -> cpu_info & { + if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty())) + return current_cpu; + auto &cpu = current_cpu; + + double avg[3]; + + if (getloadavg(avg, sizeof(avg)) < 0) { + Logger::error("failed to get load averages"); + } + + cpu.load_avg = { (float)avg[0], (float)avg[1], (float)avg[2]}; + + vector> cpu_time(Shared::coreCount); + size_t size = sizeof(long) * CPUSTATES * Shared::coreCount; + if (sysctlbyname("kern.cp_times", &cpu_time[0], &size, NULL, 0) == -1) { + Logger::error("failed to get CPU times"); + } + long long global_totals = 0; + long long global_idles = 0; + vector times_summed = {0, 0, 0, 0}; + + for (long i = 0; i < Shared::coreCount; i++) { + vector times; + //? 0=user, 1=nice, 2=system, 3=idle + for (int x = 0; const unsigned int c_state : {CP_USER, CP_NICE, CP_SYS, CP_IDLE}) { + auto val = cpu_time[i][c_state]; + times.push_back(val); + times_summed.at(x++) += val; + } + try { + //? All values + const long long totals = std::accumulate(times.begin(), times.end(), 0ll); + + //? Idle time + const long long idles = times.at(3); + + global_totals += totals; + global_idles += idles; + + //? Calculate cpu total for each core + if (i > Shared::coreCount) break; + const long long calc_totals = max(0ll, totals - core_old_totals.at(i)); + const long long calc_idles = max(0ll, idles - core_old_idles.at(i)); + core_old_totals.at(i) = totals; + core_old_idles.at(i) = idles; + + cpu.core_percent.at(i).push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); + + //? Reduce size if there are more values than needed for graph + if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front(); + + } catch (const std::exception &e) { + Logger::error("Cpu::collect() : " + (string)e.what()); + throw std::runtime_error("collect() : " + (string)e.what()); + } + + } + + const long long calc_totals = max(1ll, global_totals - cpu_old.at("totals")); + const long long calc_idles = max(1ll, global_idles - cpu_old.at("idles")); + + //? Populate cpu.cpu_percent with all fields from syscall + for (int ii = 0; const auto &val : times_summed) { + cpu.cpu_percent.at(time_names.at(ii)).push_back(clamp((long long)round((double)(val - cpu_old.at(time_names.at(ii))) * 100 / calc_totals), 0ll, 100ll)); + cpu_old.at(time_names.at(ii)) = val; + + //? Reduce size if there are more values than needed for graph + while (cmp_greater(cpu.cpu_percent.at(time_names.at(ii)).size(), width * 2)) cpu.cpu_percent.at(time_names.at(ii)).pop_front(); + + ii++; + } + + cpu_old.at("totals") = global_totals; + cpu_old.at("idles") = global_idles; + + //? Total usage of cpu + cpu.cpu_percent.at("total").push_back(clamp((long long)round((double)(calc_totals - calc_idles) * 100 / calc_totals), 0ll, 100ll)); + + //? Reduce size if there are more values than needed for graph + while (cmp_greater(cpu.cpu_percent.at("total").size(), width * 2)) cpu.cpu_percent.at("total").pop_front(); + + if (Config::getB("show_cpu_freq")) { + auto hz = get_cpuHz(); + if (hz != "") { + cpuHz = hz; + } + } + + if (Config::getB("check_temp") and got_sensors) + update_sensors(); + + if (Config::getB("show_battery") and has_battery) + current_bat = get_battery(); + + return cpu; + } +} // namespace Cpu + +namespace Mem { + bool has_swap = false; + vector fstab; + fs::file_time_type fstab_time; + int disk_ios = 0; + vector last_found; + + mem_info current_mem{}; + + uint64_t get_totalMem() { + return Shared::totalMem; + } + + void assign_values(struct disk_info& disk, int64_t readBytes, int64_t writeBytes) { + disk_ios++; + if (disk.io_read.empty()) { + disk.io_read.push_back(0); + } else { + disk.io_read.push_back(max((int64_t)0, (readBytes - disk.old_io.at(0)))); + } + disk.old_io.at(0) = readBytes; + while (cmp_greater(disk.io_read.size(), width * 2)) disk.io_read.pop_front(); + + if (disk.io_write.empty()) { + disk.io_write.push_back(0); + } else { + disk.io_write.push_back(max((int64_t)0, (writeBytes - disk.old_io.at(1)))); + } + disk.old_io.at(1) = writeBytes; + while (cmp_greater(disk.io_write.size(), width * 2)) disk.io_write.pop_front(); + + // no io times - need to push something anyway or we'll get an ABORT + if (disk.io_activity.empty()) + disk.io_activity.push_back(0); + else + disk.io_activity.push_back(clamp((long)round((double)(disk.io_write.back() + disk.io_read.back()) / (1 << 20)), 0l, 100l)); + while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); + } + + class PipeWrapper { + public: + PipeWrapper(const char *file, const char *mode) {fd = popen(file, mode);} + virtual ~PipeWrapper() {if (fd) pclose(fd);} + auto operator()() -> FILE* { return fd;}; + private: + FILE *fd; + }; + + // find all zpools in the system. Do this only at startup. + void get_zpools() { + PipeWrapper poolPipe = PipeWrapper("zpool list -H -o name", "r"); + while (not std::feof(poolPipe())) { + char poolName[512]; + size_t len = 512; + if (fgets(poolName, len, poolPipe())) { + poolName[strcspn(poolName, "\n")] = 0; + Logger::debug("zpool found: " + string(poolName)); + Mem::zpools.push_back(poolName); + } + } + } + + void collect_disk(unordered_flat_map &disks, unordered_flat_map &mapping) { + // this bit is for 'regular' mounts + static struct statinfo cur; + long double etime = 0; + uint64_t total_bytes_read; + uint64_t total_bytes_write; + + static std::unique_ptr curDevInfo (reinterpret_cast(std::calloc(1, sizeof(struct devinfo))), std::free); + cur.dinfo = curDevInfo.get(); + + if (devstat_getdevs(NULL, &cur) != -1) { + for (int i = 0; i < cur.dinfo->numdevs; i++) { + auto d = cur.dinfo->devices[i]; + string devStatName = "/dev/" + string(d.device_name) + std::to_string(d.unit_number); + for (auto& [ignored, disk] : disks) { // find matching mountpoints - could be multiple as d.device_name is only ada (and d.unit_number is the device number), while the disk.dev is like /dev/ada0s1 + if (disk.dev.string().rfind(devStatName, 0) == 0) { + devstat_compute_statistics(&d, NULL, etime, DSM_TOTAL_BYTES_READ, &total_bytes_read, DSM_TOTAL_BYTES_WRITE, &total_bytes_write, DSM_NONE); + assign_values(disk, total_bytes_read, total_bytes_write); + string mountpoint = mapping.at(disk.dev); + Logger::debug("dev " + devStatName + " -> " + mountpoint + " read=" + std::to_string(total_bytes_read) + " write=" + std::to_string(total_bytes_write)); + } + } + + } + Logger::debug(""); + } + + // this code is for ZFS mounts + for (string poolName : Mem::zpools) { + char sysCtl[1024]; + snprintf(sysCtl, sizeof(sysCtl), "sysctl kstat.zfs.%s.dataset | egrep \'dataset_name|nread|nwritten\'", poolName.c_str()); + PipeWrapper f = PipeWrapper(sysCtl, "r"); + if (f()) { + char buf[512]; + size_t len = 512; + while (not std::feof(f())) { + uint64_t nread, nwritten; + if (fgets(buf, len, f())) { + char *name = std::strtok(buf, ": \n"); + char *value = std::strtok(NULL, ": \n"); + if (string(name).find("dataset_name") != string::npos) { + // create entry if datasetname matches with anything in mapping + // relies on the fact that the dataset name is last value in the list + // alternatively you could parse the objset-0x... when this changes, you have a new entry + string datasetname = string(value);// this is the zfs volume, like 'zroot/usr/home' -> this maps onto the device we get back from getmntinfo(3) + if (mapping.contains(datasetname)) { + string mountpoint = mapping.at(datasetname); + if (disks.contains(mountpoint)) { + auto& disk = disks.at(mountpoint); + assign_values(disk, nread, nwritten); + } + } + } else if (string(name).find("nread") != string::npos) { + nread = atoll(value); + } else if (string(name).find("nwritten") != string::npos) { + nwritten = atoll(value); + } + } + } + } + } + + } + + auto collect(const bool no_update) -> mem_info & { + if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty())) + return current_mem; + + auto &show_swap = Config::getB("show_swap"); + auto &show_disks = Config::getB("show_disks"); + auto &swap_disk = Config::getB("swap_disk"); + auto &mem = current_mem; + static const bool snapped = (getenv("BTOP_SNAPPED") != NULL); + + int mib[4]; + u_int memActive, memWire, cachedMem, freeMem; + size_t len; + + len = 4; sysctlnametomib("vm.stats.vm.v_active_count", mib, &len); + len = sizeof(memActive); + sysctl(mib, 4, &(memActive), &len, NULL, 0); + memActive *= Shared::pageSize; + + len = 4; sysctlnametomib("vm.stats.vm.v_wire_count", mib, &len); + len = sizeof(memWire); + sysctl(mib, 4, &(memWire), &len, NULL, 0); + memWire *= Shared::pageSize; + + mem.stats.at("used") = memWire + memActive; + mem.stats.at("available") = Shared::totalMem - memActive - memWire; + + len = sizeof(cachedMem); + len = 4; sysctlnametomib("vm.stats.vm.v_cache_count", mib, &len); + sysctl(mib, 4, &(cachedMem), &len, NULL, 0); + cachedMem *= Shared::pageSize; + mem.stats.at("cached") = cachedMem; + + len = sizeof(freeMem); + len = 4; sysctlnametomib("vm.stats.vm.v_free_count", mib, &len); + sysctl(mib, 4, &(freeMem), &len, NULL, 0); + freeMem *= Shared::pageSize; + mem.stats.at("free") = freeMem; + + if (show_swap and mem.stats.at("swap_total") > 0) { + for (const auto &name : swap_names) { + mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / mem.stats.at("swap_total"))); + while (cmp_greater(mem.percent.at(name).size(), width * 2)) + mem.percent.at(name).pop_front(); + } + has_swap = true; + } else + has_swap = false; + //? Calculate percentages + for (const auto &name : mem_names) { + mem.percent.at(name).push_back(round((double)mem.stats.at(name) * 100 / Shared::totalMem)); + while (cmp_greater(mem.percent.at(name).size(), width * 2)) + mem.percent.at(name).pop_front(); + } + + if (show_disks) { + unordered_flat_map mapping; // keep mapping from device -> mountpoint, since IOKit doesn't give us the mountpoint + double uptime = system_uptime(); + auto &disks_filter = Config::getS("disks_filter"); + bool filter_exclude = false; + // auto &only_physical = Config::getB("only_physical"); + auto &disks = mem.disks; + vector filter; + if (not disks_filter.empty()) { + filter = ssplit(disks_filter); + if (filter.at(0).starts_with("exclude=")) { + filter_exclude = true; + filter.at(0) = filter.at(0).substr(8); + } + } + + struct statfs *stfs; + int count = getmntinfo(&stfs, MNT_WAIT); + vector found; + found.reserve(last_found.size()); + for (int i = 0; i < count; i++) { + auto fstype = string(stfs[i].f_fstypename); + if (fstype == "autofs" || fstype == "devfs" || fstype == "linprocfs" || fstype == "procfs" || fstype == "tmpfs" || fstype == "linsysfs" || + fstype == "fdesckfs") { + // in memory filesystems -> not useful to show + continue; + } + + std::error_code ec; + string mountpoint = stfs[i].f_mntonname; + string dev = stfs[i].f_mntfromname; + mapping[dev] = mountpoint; + + //? Match filter if not empty + if (not filter.empty()) { + bool match = v_contains(filter, mountpoint); + if ((filter_exclude and match) or (not filter_exclude and not match)) + continue; + } + + found.push_back(mountpoint); + if (not disks.contains(mountpoint)) { + disks[mountpoint] = disk_info{fs::canonical(dev, ec), fs::path(mountpoint).filename()}; + + if (disks.at(mountpoint).dev.empty()) + disks.at(mountpoint).dev = dev; + + if (disks.at(mountpoint).name.empty()) + disks.at(mountpoint).name = (mountpoint == "/" ? "root" : mountpoint); + } + + + if (not v_contains(last_found, mountpoint)) + redraw = true; + + disks.at(mountpoint).free = stfs[i].f_bfree; + disks.at(mountpoint).total = stfs[i].f_iosize; + } + + //? Remove disks no longer mounted or filtered out + if (swap_disk and has_swap) found.push_back("swap"); + for (auto it = disks.begin(); it != disks.end();) { + if (not v_contains(found, it->first)) + it = disks.erase(it); + else + it++; + } + if (found.size() != last_found.size()) redraw = true; + last_found = std::move(found); + + //? Get disk/partition stats + for (auto &[mountpoint, disk] : disks) { + if (std::error_code ec; not fs::exists(mountpoint, ec)) + continue; + struct statvfs vfs; + if (statvfs(mountpoint.c_str(), &vfs) < 0) { + Logger::warning("Failed to get disk/partition stats with statvfs() for: " + mountpoint); + continue; + } + disk.total = vfs.f_blocks * vfs.f_frsize; + disk.free = vfs.f_bfree * vfs.f_frsize; + disk.used = disk.total - disk.free; + disk.used_percent = round((double)disk.used * 100 / disk.total); + disk.free_percent = 100 - disk.used_percent; + } + + //? Setup disks order in UI and add swap if enabled + mem.disks_order.clear(); + if (snapped and disks.contains("/mnt")) + mem.disks_order.push_back("/mnt"); + else if (disks.contains("/")) + mem.disks_order.push_back("/"); + if (swap_disk and has_swap) { + mem.disks_order.push_back("swap"); + if (not disks.contains("swap")) + disks["swap"] = {"", "swap"}; + disks.at("swap").total = mem.stats.at("swap_total"); + disks.at("swap").used = mem.stats.at("swap_used"); + disks.at("swap").free = mem.stats.at("swap_free"); + disks.at("swap").used_percent = mem.percent.at("swap_used").back(); + disks.at("swap").free_percent = mem.percent.at("swap_free").back(); + } + for (const auto &name : last_found) + if (not is_in(name, "/", "swap", "/dev")) + mem.disks_order.push_back(name); + + disk_ios = 0; + collect_disk(disks, mapping); + + old_uptime = uptime; + } + return mem; + } + +} // namespace Mem + +namespace Net { + unordered_flat_map current_net; + net_info empty_net = {}; + vector interfaces; + string selected_iface; + int errors = 0; + unordered_flat_map graph_max = {{"download", {}}, {"upload", {}}}; + unordered_flat_map> max_count = {{"download", {}}, {"upload", {}}}; + bool rescale = true; + uint64_t timestamp = 0; + + //* RAII wrapper for getifaddrs + class getifaddr_wrapper { + struct ifaddrs *ifaddr; + + public: + int status; + getifaddr_wrapper() { status = getifaddrs(&ifaddr); } + ~getifaddr_wrapper() { freeifaddrs(ifaddr); } + auto operator()() -> struct ifaddrs * { return ifaddr; } + }; + + auto collect(const bool no_update) -> net_info & { + auto &net = current_net; + auto &config_iface = Config::getS("net_iface"); + auto &net_sync = Config::getB("net_sync"); + auto &net_auto = Config::getB("net_auto"); + auto new_timestamp = time_ms(); + + if (not no_update and errors < 3) { + //? Get interface list using getifaddrs() wrapper + getifaddr_wrapper if_wrap{}; + if (if_wrap.status != 0) { + errors++; + Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status)); + redraw = true; + return empty_net; + } + int family = 0; + char ip[NI_MAXHOST]; + interfaces.clear(); + string ipv4, ipv6; + + //? Iteration over all items in getifaddrs() list + for (auto *ifa = if_wrap(); ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) continue; + family = ifa->ifa_addr->sa_family; + const auto &iface = ifa->ifa_name; + //? Get IPv4 address + if (family == AF_INET) { + if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) + net[iface].ipv4 = ip; + } + //? Get IPv6 address + // else if (family == AF_INET6) { + // if (getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6), ip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) + // net[iface].ipv6 = ip; + // } + + //? Update available interfaces vector and get status of interface + if (not v_contains(interfaces, iface)) { + interfaces.push_back(iface); + net[iface].connected = (ifa->ifa_flags & IFF_RUNNING); + } + } + + unordered_flat_map> ifstats; + int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; + size_t len; + if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { + Logger::error("failed getting network interfaces"); + } else { + std::unique_ptr buf(new char[len]); + if (sysctl(mib, 6, buf.get(), &len, NULL, 0) < 0) { + Logger::error("failed getting network interfaces"); + } else { + char *lim = buf.get() + len; + char *next = NULL; + for (next = buf.get(); next < lim;) { + struct if_msghdr *ifm = (struct if_msghdr *)next; + next += ifm->ifm_msglen; + struct if_data ifm_data = ifm->ifm_data; + if (ifm->ifm_addrs & RTA_IFP) { + struct sockaddr_dl *sdl = (struct sockaddr_dl *)(ifm + 1); + char iface[32]; + strncpy(iface, sdl->sdl_data, sdl->sdl_nlen); + iface[sdl->sdl_nlen] = 0; + ifstats[iface] = std::tuple(ifm_data.ifi_ibytes, ifm_data.ifi_obytes); + } + } + } + } + + //? Get total recieved and transmitted bytes + device address if no ip was found + for (const auto &iface : interfaces) { + for (const string dir : {"download", "upload"}) { + auto &saved_stat = net.at(iface).stat.at(dir); + auto &bandwidth = net.at(iface).bandwidth.at(dir); + auto dirval = dir == "download" ? std::get<0>(ifstats[iface]) : std::get<1>(ifstats[iface]); + uint64_t val = saved_stat.last; + try { + val = max(dirval, val); + } catch (const std::invalid_argument &) { + } catch (const std::out_of_range &) { + } + + //? Update speed, total and top values + saved_stat.speed = round((double)(val - saved_stat.last) / ((double)(new_timestamp - timestamp) / 1000)); + if (saved_stat.speed > saved_stat.top) saved_stat.top = saved_stat.speed; + if (saved_stat.offset > val) saved_stat.offset = 0; + saved_stat.total = val - saved_stat.offset; + saved_stat.last = val; + + //? Add values to graph + bandwidth.push_back(saved_stat.speed); + while (cmp_greater(bandwidth.size(), width * 2)) bandwidth.pop_front(); + + //? Set counters for auto scaling + if (net_auto and selected_iface == iface) { + if (saved_stat.speed > graph_max[dir]) { + ++max_count[dir][0]; + if (max_count[dir][1] > 0) --max_count[dir][1]; + } else if (graph_max[dir] > 10 << 10 and saved_stat.speed < graph_max[dir] / 10) { + ++max_count[dir][1]; + if (max_count[dir][0] > 0) --max_count[dir][0]; + } + } + } + } + + //? Clean up net map if needed + if (net.size() > interfaces.size()) { + for (auto it = net.begin(); it != net.end();) { + if (not v_contains(interfaces, it->first)) + it = net.erase(it); + else + it++; + } + net.compact(); + } + + timestamp = new_timestamp; + } + //? Return empty net_info struct if no interfaces was found + if (net.empty()) + return empty_net; + + //? Find an interface to display if selected isn't set or valid + if (selected_iface.empty() or not v_contains(interfaces, selected_iface)) { + max_count["download"][0] = max_count["download"][1] = max_count["upload"][0] = max_count["upload"][1] = 0; + redraw = true; + if (net_auto) rescale = true; + if (not config_iface.empty() and v_contains(interfaces, config_iface)) + selected_iface = config_iface; + else { + //? Sort interfaces by total upload + download bytes + auto sorted_interfaces = interfaces; + rng::sort(sorted_interfaces, [&](const auto &a, const auto &b) { + return cmp_greater(net.at(a).stat["download"].total + net.at(a).stat["upload"].total, + net.at(b).stat["download"].total + net.at(b).stat["upload"].total); + }); + selected_iface.clear(); + //? Try to set to a connected interface + for (const auto &iface : sorted_interfaces) { + if (net.at(iface).connected) selected_iface = iface; + break; + } + //? If no interface is connected set to first available + if (selected_iface.empty() and not sorted_interfaces.empty()) + selected_iface = sorted_interfaces.at(0); + else if (sorted_interfaces.empty()) + return empty_net; + } + } + + //? Calculate max scale for graphs if needed + if (net_auto) { + bool sync = false; + for (const auto &dir : {"download", "upload"}) { + for (const auto &sel : {0, 1}) { + if (rescale or max_count[dir][sel] >= 5) { + const uint64_t avg_speed = (net[selected_iface].bandwidth[dir].size() > 5 + ? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0) / 5 + : net[selected_iface].stat[dir].speed); + graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), (uint64_t)10 << 10); + max_count[dir][0] = max_count[dir][1] = 0; + redraw = true; + if (net_sync) sync = true; + break; + } + } + //? Sync download/upload graphs if enabled + if (sync) { + const auto other = (string(dir) == "upload" ? "download" : "upload"); + graph_max[other] = graph_max[dir]; + max_count[other][0] = max_count[other][1] = 0; + break; + } + } + } + + rescale = false; + return net.at(selected_iface); + } +} // namespace Net + +namespace Proc { + + vector current_procs; + unordered_flat_map uid_user; + string current_sort; + string current_filter; + bool current_rev = false; + + fs::file_time_type passwd_time; + + uint64_t cputimes; + int collapse = -1, expand = -1; + uint64_t old_cputimes = 0; + atomic numpids = 0; + int filter_found = 0; + + detail_container detailed; + + //* Generate process tree list + void _tree_gen(proc_info &cur_proc, vector &in_procs, vector> &out_procs, int cur_depth, const bool collapsed, const string &filter, bool found = false, const bool no_update = false, const bool should_filter = false) { + auto cur_pos = out_procs.size(); + bool filtering = false; + + //? If filtering, include children of matching processes + if (not found and (should_filter or not filter.empty())) { + if (not s_contains(std::to_string(cur_proc.pid), filter) and not s_contains(cur_proc.name, filter) and not s_contains(cur_proc.cmd, filter) and not s_contains(cur_proc.user, filter)) { + filtering = true; + cur_proc.filtered = true; + filter_found++; + } else { + found = true; + cur_depth = 0; + } + } else if (cur_proc.filtered) + cur_proc.filtered = false; + + //? Set tree index position for process if not filtered out or currently in a collapsed sub-tree + if (not collapsed and not filtering) { + out_procs.push_back(std::ref(cur_proc)); + cur_proc.tree_index = out_procs.size() - 1; + //? Try to find name of the binary file and append to program name if not the same + if (cur_proc.short_cmd.empty() and not cur_proc.cmd.empty()) { + std::string_view cmd_view = cur_proc.cmd; + cmd_view = cmd_view.substr((size_t)0, min(cmd_view.find(' '), cmd_view.size())); + cmd_view = cmd_view.substr(min(cmd_view.find_last_of('/') + 1, cmd_view.size())); + cur_proc.short_cmd = (string)cmd_view; + } + } else { + cur_proc.tree_index = in_procs.size(); + } + + //? Recursive iteration over all children + int children = 0; + for (auto &p : rng::equal_range(in_procs, cur_proc.pid, rng::less{}, &proc_info::ppid)) { + if (not no_update and not filtering and (collapsed or cur_proc.collapsed)) { + out_procs.back().get().cpu_p += p.cpu_p; + out_procs.back().get().mem += p.mem; + out_procs.back().get().threads += p.threads; + filter_found++; + } + if (collapsed and not filtering) { + cur_proc.filtered = true; + } else + children++; + _tree_gen(p, in_procs, out_procs, cur_depth + 1, (collapsed ? true : cur_proc.collapsed), filter, found, no_update, should_filter); + } + if (collapsed or filtering) + return; + + //? Add tree terminator symbol if it's the last child in a sub-tree + if (out_procs.size() > cur_pos + 1 and not out_procs.back().get().prefix.ends_with("]─")) + out_procs.back().get().prefix.replace(out_procs.back().get().prefix.size() - 8, 8, " └─ "); + + //? Add collapse/expand symbols if process have any children + out_procs.at(cur_pos).get().prefix = " │ "s * cur_depth + (children > 0 ? (cur_proc.collapsed ? "[+]─" : "[-]─") : " ├─ "); + } + + string get_status(char s) { + if (s & SRUN) return "Running"; + if (s & SSLEEP) return "Sleeping"; + if (s & SIDL) return "Idle"; + if (s & SSTOP) return "Stopped"; + if (s & SZOMB) return "Zombie"; + return "Unknown"; + } + + //* Get detailed info for selected process + void _collect_details(const size_t pid, vector &procs) { + if (pid != detailed.last_pid) { + detailed = {}; + detailed.last_pid = pid; + detailed.skip_smaps = not Config::getB("proc_info_smaps"); + } + + //? Copy proc_info for process from proc vector + auto p_info = rng::find(procs, pid, &proc_info::pid); + detailed.entry = *p_info; + + //? Update cpu percent deque for process cpu graph + if (not Config::getB("proc_per_core")) detailed.entry.cpu_p *= Shared::coreCount; + detailed.cpu_percent.push_back(clamp((long long)round(detailed.entry.cpu_p), 0ll, 100ll)); + while (cmp_greater(detailed.cpu_percent.size(), width)) detailed.cpu_percent.pop_front(); + + //? Process runtime : current time - start time (both in unix time - seconds since epoch) + struct timeval currentTime; + gettimeofday(¤tTime, NULL); + detailed.elapsed = sec_to_dhms(currentTime.tv_sec - detailed.entry.cpu_s); // only interested in second granularity, so ignoring tc_usec + if (detailed.elapsed.size() > 8) detailed.elapsed.resize(detailed.elapsed.size() - 3); + + //? Get parent process name + if (detailed.parent.empty()) { + auto p_entry = rng::find(procs, detailed.entry.ppid, &proc_info::pid); + if (p_entry != procs.end()) detailed.parent = p_entry->name; + } + + //? Expand process status from single char to explanative string + detailed.status = get_status(detailed.entry.state); + + detailed.mem_bytes.push_back(detailed.entry.mem); + detailed.memory = floating_humanizer(detailed.entry.mem); + + if (detailed.first_mem == -1 or detailed.first_mem < detailed.mem_bytes.back() / 2 or detailed.first_mem > detailed.mem_bytes.back() * 4) { + detailed.first_mem = min((uint64_t)detailed.mem_bytes.back() * 2, Mem::get_totalMem()); + redraw = true; + } + + while (cmp_greater(detailed.mem_bytes.size(), width)) detailed.mem_bytes.pop_front(); + + // rusage_info_current rusage; + // if (proc_pid_rusage(pid, RUSAGE_INFO_CURRENT, (void **)&rusage) == 0) { + // // this fails for processes we don't own - same as in Linux + // detailed.io_read = floating_humanizer(rusage.ri_diskio_bytesread); + // detailed.io_write = floating_humanizer(rusage.ri_diskio_byteswritten); + // } + } + + //* RAII wrapper for kvm_openfiles + class kvm_openfiles_wrapper { + kvm_t* kd = NULL; + public: + kvm_openfiles_wrapper(const char* execf, const char* coref, const char* swapf, int flags, char* err) { + this->kd = kvm_openfiles(execf, coref, swapf, flags, err); + } + ~kvm_openfiles_wrapper() { kvm_close(kd); } + auto operator()() -> kvm_t* { return kd; } + }; + + //* Collects and sorts process information from /proc + auto collect(const bool no_update) -> vector & { + const auto &sorting = Config::getS("proc_sorting"); + const auto &reverse = Config::getB("proc_reversed"); + const auto &filter = Config::getS("proc_filter"); + const auto &per_core = Config::getB("proc_per_core"); + const auto &tree = Config::getB("proc_tree"); + const auto &show_detailed = Config::getB("show_detailed"); + const size_t detailed_pid = Config::getI("detailed_pid"); + bool should_filter = current_filter != filter; + if (should_filter) current_filter = filter; + const bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter); + if (sorted_change) { + current_sort = sorting; + current_rev = reverse; + } + + const int cmult = (per_core) ? Shared::coreCount : 1; + bool got_detailed = false; + + vector> cpu_time(Shared::coreCount); + size_t size = sizeof(long) * CPUSTATES * Shared::coreCount; + if (sysctlbyname("kern.cp_times", &cpu_time[0], &size, NULL, 0) == -1) { + Logger::error("failed to get CPU times"); + } + cputimes = 0; + for (const auto core : cpu_time) { + for (const unsigned int c_state : {CP_USER, CP_NICE, CP_SYS, CP_IDLE}) { + cputimes += core[c_state]; + } + } + + //* Use pids from last update if only changing filter, sorting or tree options + if (no_update and not current_procs.empty()) { + if (show_detailed and detailed_pid != detailed.last_pid) _collect_details(detailed_pid, current_procs); + } else { + //* ---------------------------------------------Collection start---------------------------------------------- + + should_filter = true; + vector found; + struct timeval currentTime; + gettimeofday(¤tTime, NULL); + const double timeNow = currentTime.tv_sec + (currentTime.tv_usec / 1'000'000); + + int count = 0; + char buf[_POSIX2_LINE_MAX]; + kvm_openfiles_wrapper kd(NULL, _PATH_DEVNULL, NULL, O_RDONLY, buf); + const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_PROC, 0, &count); + + for (int i = 0; i < count; i++) { + const struct kinfo_proc* kproc = &kprocs[i]; + const size_t pid = (size_t)kproc->ki_pid; + if (pid < 1) continue; + found.push_back(pid); + + //? Check if pid already exists in current_procs + bool no_cache = false; + auto find_old = rng::find(current_procs, pid, &proc_info::pid); + if (find_old == current_procs.end()) { + current_procs.push_back({pid}); + find_old = current_procs.end() - 1; + no_cache = true; + } + + auto &new_proc = *find_old; + + //? Get program name, command, username, parent pid, nice and status + if (no_cache) { + if (kproc->ki_comm == NULL or kproc->ki_comm == "idle"s) { + current_procs.pop_back(); + continue; + } + new_proc.name = kproc->ki_comm; + char** argv = kvm_getargv(kd(), kproc, 0); + if (argv) { + for (int i = 0; argv[i]; i++) { + new_proc.cmd += argv[i] + " "s; + } + if (not new_proc.cmd.empty()) new_proc.cmd.pop_back(); + } + if (new_proc.cmd.empty()) new_proc.cmd = new_proc.name; + new_proc.ppid = kproc->ki_ppid; + new_proc.cpu_s = round(kproc->ki_start.tv_sec); + struct passwd *pwd = getpwuid(kproc->ki_uid); + if (pwd) + new_proc.user = pwd->pw_name; + } + new_proc.p_nice = kproc->ki_nice; + new_proc.state = kproc->ki_stat; + + int cpu_t = 0; + cpu_t = kproc->ki_rusage.ru_utime.tv_sec * 1'000'000 + kproc->ki_rusage.ru_utime.tv_usec + + kproc->ki_rusage.ru_stime.tv_sec * 1'000'000 + kproc->ki_rusage.ru_stime.tv_usec; + + new_proc.mem = kproc->ki_rssize * Shared::pageSize; + new_proc.threads = kproc->ki_numthreads; + + //? Process cpu usage since last update + new_proc.cpu_p = clamp((100.0 * ((cpu_t - new_proc.cpu_t) / 1'000'000.0) / max((uint64_t)1, (cputimes - old_cputimes) * Shared::clkTck)) * cmult / 100'000.0, 0.0, 100.0 * Shared::coreCount); + + //? Process cumulative cpu usage since process start + new_proc.cpu_c = (double)(cpu_t * Shared::clkTck / 1'000'000) / max(1.0, timeNow - new_proc.cpu_s); + + //? Update cached value with latest cpu times + new_proc.cpu_t = cpu_t; + + if (show_detailed and not got_detailed and new_proc.pid == detailed_pid) { + got_detailed = true; + } + + // //? Clear dead processes from current_procs + auto eraser = rng::remove_if(current_procs, [&](const auto &element) { return not v_contains(found, element.pid); }); + current_procs.erase(eraser.begin(), eraser.end()); + + //? Update the details info box for process if active + if (show_detailed and got_detailed) { + _collect_details(detailed_pid, current_procs); + } else if (show_detailed and not got_detailed and detailed.status != "Dead") { + detailed.status = "Dead"; + redraw = true; + } + + old_cputimes = cputimes; + } + } + + //* ---------------------------------------------Collection done----------------------------------------------- + + //* Sort processes + if (sorted_change or not no_update) { + switch (v_index(sort_vector, sorting)) { + case 0: rng::sort(current_procs, rng::greater{}, &proc_info::pid); break; + case 1: rng::sort(current_procs, rng::greater{}, &proc_info::name); break; + case 2: rng::sort(current_procs, rng::greater{}, &proc_info::cmd); break; + case 3: rng::sort(current_procs, rng::greater{}, &proc_info::threads); break; + case 4: rng::sort(current_procs, rng::greater{}, &proc_info::user); break; + case 5: rng::sort(current_procs, rng::greater{}, &proc_info::mem); break; + case 6: rng::sort(current_procs, rng::greater{}, &proc_info::cpu_p); break; + case 7: rng::sort(current_procs, rng::greater{}, &proc_info::cpu_c); break; + } + if (reverse) rng::reverse(current_procs); + + //* When sorting with "cpu lazy" push processes over threshold cpu usage to the front regardless of cumulative usage + if (not tree and not reverse and sorting == "cpu lazy") { + double max = 10.0, target = 30.0; + for (size_t i = 0, x = 0, offset = 0; i < current_procs.size(); i++) { + if (i <= 5 and current_procs.at(i).cpu_p > max) + max = current_procs.at(i).cpu_p; + else if (i == 6) + target = (max > 30.0) ? max : 10.0; + if (i == offset and current_procs.at(i).cpu_p > 30.0) + offset++; + else if (current_procs.at(i).cpu_p > target) { + rotate(current_procs.begin() + offset, current_procs.begin() + i, current_procs.begin() + i + 1); + if (++x > 10) break; + } + } + } + } + + //* Match filter if defined + if (should_filter) { + filter_found = 0; + for (auto &p : current_procs) { + if (not tree and not filter.empty()) { + if (not s_contains(to_string(p.pid), filter) and not s_contains(p.name, filter) and not s_contains(p.cmd, filter) and not s_contains(p.user, filter)) { + p.filtered = true; + filter_found++; + } else { + p.filtered = false; + } + } else { + p.filtered = false; + } + } + } + + //* Generate tree view if enabled + if (tree and (not no_update or should_filter or sorted_change)) { + if (auto find_pid = (collapse != -1 ? collapse : expand); find_pid != -1) { + auto collapser = rng::find(current_procs, find_pid, &proc_info::pid); + if (collapser != current_procs.end()) { + if (collapse == expand) { + collapser->collapsed = not collapser->collapsed; + } else if (collapse > -1) { + collapser->collapsed = true; + } else if (expand > -1) { + collapser->collapsed = false; + } + } + collapse = expand = -1; + } + if (should_filter or not filter.empty()) filter_found = 0; + + vector> tree_procs; + tree_procs.reserve(current_procs.size()); + + //? Stable sort to retain selected sorting among processes with the same parent + rng::stable_sort(current_procs, rng::less{}, &proc_info::ppid); + + //? Start recursive iteration over processes with the lowest shared parent pids + for (auto &p : rng::equal_range(current_procs, current_procs.at(0).ppid, rng::less{}, &proc_info::ppid)) { + _tree_gen(p, current_procs, tree_procs, 0, false, filter, false, no_update, should_filter); + } + + //? Final sort based on tree index + rng::sort(current_procs, rng::less{}, &proc_info::tree_index); + if (reverse) rng::reverse(current_procs); + } + + numpids = (int)current_procs.size() - filter_found; + return current_procs; + } +} // namespace Proc + +namespace Tools { + double system_uptime() { + struct timeval ts, currTime; + std::size_t len = sizeof(ts); + int mib[2] = {CTL_KERN, KERN_BOOTTIME}; + if (sysctl(mib, 2, &ts, &len, NULL, 0) != -1) { + gettimeofday(&currTime, NULL); + return currTime.tv_sec - ts.tv_sec; + } + return 0.0; + } +} // namespace Tools