Platform.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. htop - darwin/Platform.c
  3. (C) 2014 Hisham H. Muhammad
  4. (C) 2015 David C. Hunt
  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 "darwin/Platform.h"
  10. #include <errno.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <net/if.h>
  15. #include <net/if_types.h>
  16. #include <net/route.h>
  17. #include <sys/socket.h>
  18. #include <mach/port.h>
  19. #include <CoreFoundation/CFBase.h>
  20. #include <CoreFoundation/CFDictionary.h>
  21. #include <CoreFoundation/CFNumber.h>
  22. #include <CoreFoundation/CFString.h>
  23. #include <CoreFoundation/CoreFoundation.h>
  24. #include <IOKit/IOKitLib.h>
  25. #include <IOKit/IOTypes.h>
  26. #include <IOKit/ps/IOPowerSources.h>
  27. #include <IOKit/ps/IOPSKeys.h>
  28. #include <IOKit/storage/IOBlockStorageDriver.h>
  29. #include "ClockMeter.h"
  30. #include "CPUMeter.h"
  31. #include "CRT.h"
  32. #include "DateMeter.h"
  33. #include "DateTimeMeter.h"
  34. #include "FileDescriptorMeter.h"
  35. #include "HostnameMeter.h"
  36. #include "LoadAverageMeter.h"
  37. #include "Macros.h"
  38. #include "MemoryMeter.h"
  39. #include "MemorySwapMeter.h"
  40. #include "ProcessLocksScreen.h"
  41. #include "SwapMeter.h"
  42. #include "SysArchMeter.h"
  43. #include "TasksMeter.h"
  44. #include "UptimeMeter.h"
  45. #include "darwin/DarwinMachine.h"
  46. #include "darwin/PlatformHelpers.h"
  47. #include "generic/fdstat_sysctl.h"
  48. #include "zfs/ZfsArcMeter.h"
  49. #include "zfs/ZfsCompressedArcMeter.h"
  50. #ifdef HAVE_HOST_GET_CLOCK_SERVICE
  51. #include <mach/clock.h>
  52. #include <mach/mach.h>
  53. #endif
  54. #ifdef HAVE_MACH_MACH_TIME_H
  55. #include <mach/mach_time.h>
  56. #endif
  57. const ScreenDefaults Platform_defaultScreens[] = {
  58. {
  59. .name = "Main",
  60. .columns = "PID USER PRIORITY NICE M_VIRT M_RESIDENT STATE PERCENT_CPU PERCENT_MEM TIME Command",
  61. .sortKey = "PERCENT_CPU",
  62. },
  63. };
  64. const unsigned int Platform_numberOfDefaultScreens = ARRAYSIZE(Platform_defaultScreens);
  65. const SignalItem Platform_signals[] = {
  66. { .name = " 0 Cancel", .number = 0 },
  67. { .name = " 1 SIGHUP", .number = 1 },
  68. { .name = " 2 SIGINT", .number = 2 },
  69. { .name = " 3 SIGQUIT", .number = 3 },
  70. { .name = " 4 SIGILL", .number = 4 },
  71. { .name = " 5 SIGTRAP", .number = 5 },
  72. { .name = " 6 SIGABRT", .number = 6 },
  73. { .name = " 6 SIGIOT", .number = 6 },
  74. { .name = " 7 SIGEMT", .number = 7 },
  75. { .name = " 8 SIGFPE", .number = 8 },
  76. { .name = " 9 SIGKILL", .number = 9 },
  77. { .name = "10 SIGBUS", .number = 10 },
  78. { .name = "11 SIGSEGV", .number = 11 },
  79. { .name = "12 SIGSYS", .number = 12 },
  80. { .name = "13 SIGPIPE", .number = 13 },
  81. { .name = "14 SIGALRM", .number = 14 },
  82. { .name = "15 SIGTERM", .number = 15 },
  83. { .name = "16 SIGURG", .number = 16 },
  84. { .name = "17 SIGSTOP", .number = 17 },
  85. { .name = "18 SIGTSTP", .number = 18 },
  86. { .name = "19 SIGCONT", .number = 19 },
  87. { .name = "20 SIGCHLD", .number = 20 },
  88. { .name = "21 SIGTTIN", .number = 21 },
  89. { .name = "22 SIGTTOU", .number = 22 },
  90. { .name = "23 SIGIO", .number = 23 },
  91. { .name = "24 SIGXCPU", .number = 24 },
  92. { .name = "25 SIGXFSZ", .number = 25 },
  93. { .name = "26 SIGVTALRM", .number = 26 },
  94. { .name = "27 SIGPROF", .number = 27 },
  95. { .name = "28 SIGWINCH", .number = 28 },
  96. { .name = "29 SIGINFO", .number = 29 },
  97. { .name = "30 SIGUSR1", .number = 30 },
  98. { .name = "31 SIGUSR2", .number = 31 },
  99. };
  100. const unsigned int Platform_numberOfSignals = ARRAYSIZE(Platform_signals);
  101. const MeterClass* const Platform_meterTypes[] = {
  102. &CPUMeter_class,
  103. &ClockMeter_class,
  104. &DateMeter_class,
  105. &DateTimeMeter_class,
  106. &LoadAverageMeter_class,
  107. &LoadMeter_class,
  108. &MemoryMeter_class,
  109. &SwapMeter_class,
  110. &MemorySwapMeter_class,
  111. &TasksMeter_class,
  112. &BatteryMeter_class,
  113. &HostnameMeter_class,
  114. &SysArchMeter_class,
  115. &UptimeMeter_class,
  116. &AllCPUsMeter_class,
  117. &AllCPUs2Meter_class,
  118. &AllCPUs4Meter_class,
  119. &AllCPUs8Meter_class,
  120. &LeftCPUsMeter_class,
  121. &RightCPUsMeter_class,
  122. &LeftCPUs2Meter_class,
  123. &RightCPUs2Meter_class,
  124. &LeftCPUs4Meter_class,
  125. &RightCPUs4Meter_class,
  126. &LeftCPUs8Meter_class,
  127. &RightCPUs8Meter_class,
  128. &ZfsArcMeter_class,
  129. &ZfsCompressedArcMeter_class,
  130. &DiskIOMeter_class,
  131. &NetworkIOMeter_class,
  132. &FileDescriptorMeter_class,
  133. &BlankMeter_class,
  134. NULL
  135. };
  136. static double Platform_nanosecondsPerMachTick = 1.0;
  137. static double Platform_nanosecondsPerSchedulerTick = -1;
  138. static bool iokit_available = false;
  139. static mach_port_t iokit_port; // the mach port used to initiate communication with IOKit
  140. bool Platform_init(void) {
  141. Platform_nanosecondsPerMachTick = Platform_calculateNanosecondsPerMachTick();
  142. // Determine the number of scheduler clock ticks per second
  143. errno = 0;
  144. long scheduler_ticks_per_sec = sysconf(_SC_CLK_TCK);
  145. if (errno || scheduler_ticks_per_sec < 1) {
  146. CRT_fatalError("Unable to retrieve clock tick rate");
  147. }
  148. const double nanos_per_sec = 1e9;
  149. Platform_nanosecondsPerSchedulerTick = nanos_per_sec / scheduler_ticks_per_sec;
  150. // Since macOS 12.0, IOMasterPort is deprecated, and one should use IOMainPort instead
  151. #if defined(HAVE_DECL_IOMAINPORT) && HAVE_DECL_IOMAINPORT
  152. if (!IOMainPort(bootstrap_port, &iokit_port)) {
  153. iokit_available = true;
  154. }
  155. #elif defined(HAVE_DECL_IOMASTERPORT) && HAVE_DECL_IOMASTERPORT
  156. if (!IOMasterPort(bootstrap_port, &iokit_port)) {
  157. iokit_available = true;
  158. }
  159. #endif
  160. return true;
  161. }
  162. // Converts ticks in the Mach "timebase" to nanoseconds.
  163. // See `mach_timebase_info`, as used to define the `Platform_nanosecondsPerMachTick` constant.
  164. uint64_t Platform_machTicksToNanoseconds(uint64_t mach_ticks) {
  165. return (uint64_t) ((double) mach_ticks * Platform_nanosecondsPerMachTick);
  166. }
  167. // Converts "scheduler ticks" to nanoseconds.
  168. // See `sysconf(_SC_CLK_TCK)`, as used to define the `Platform_nanosecondsPerSchedulerTick` constant.
  169. double Platform_schedulerTicksToNanoseconds(const double scheduler_ticks) {
  170. return scheduler_ticks * Platform_nanosecondsPerSchedulerTick;
  171. }
  172. void Platform_done(void) {
  173. /* no platform-specific cleanup needed */
  174. }
  175. void Platform_setBindings(Htop_Action* keys) {
  176. /* no platform-specific key bindings */
  177. (void) keys;
  178. }
  179. int Platform_getUptime(void) {
  180. struct timeval bootTime, currTime;
  181. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  182. size_t size = sizeof(bootTime);
  183. int err = sysctl(mib, 2, &bootTime, &size, NULL, 0);
  184. if (err) {
  185. return -1;
  186. }
  187. gettimeofday(&currTime, NULL);
  188. return (int) difftime(currTime.tv_sec, bootTime.tv_sec);
  189. }
  190. void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
  191. double results[3];
  192. if (3 == getloadavg(results, 3)) {
  193. *one = results[0];
  194. *five = results[1];
  195. *fifteen = results[2];
  196. } else {
  197. *one = 0;
  198. *five = 0;
  199. *fifteen = 0;
  200. }
  201. }
  202. pid_t Platform_getMaxPid(void) {
  203. /* http://opensource.apple.com/source/xnu/xnu-2782.1.97/bsd/sys/proc_internal.hh */
  204. return 99999;
  205. }
  206. static double Platform_setCPUAverageValues(Meter* mtr) {
  207. const Machine* host = mtr->host;
  208. unsigned int activeCPUs = host->activeCPUs;
  209. double sumNice = 0.0;
  210. double sumNormal = 0.0;
  211. double sumKernel = 0.0;
  212. double sumPercent = 0.0;
  213. for (unsigned int i = 1; i <= host->existingCPUs; i++) {
  214. sumPercent += Platform_setCPUValues(mtr, i);
  215. sumNice += mtr->values[CPU_METER_NICE];
  216. sumNormal += mtr->values[CPU_METER_NORMAL];
  217. sumKernel += mtr->values[CPU_METER_KERNEL];
  218. }
  219. mtr->values[CPU_METER_NICE] = sumNice / activeCPUs;
  220. mtr->values[CPU_METER_NORMAL] = sumNormal / activeCPUs;
  221. mtr->values[CPU_METER_KERNEL] = sumKernel / activeCPUs;
  222. return sumPercent / activeCPUs;
  223. }
  224. double Platform_setCPUValues(Meter* mtr, unsigned int cpu) {
  225. if (cpu == 0) {
  226. return Platform_setCPUAverageValues(mtr);
  227. }
  228. const DarwinMachine* dhost = (const DarwinMachine*) mtr->host;
  229. const processor_cpu_load_info_t prev = &dhost->prev_load[cpu - 1];
  230. const processor_cpu_load_info_t curr = &dhost->curr_load[cpu - 1];
  231. double total = 0;
  232. /* Take the sums */
  233. for (size_t i = 0; i < CPU_STATE_MAX; ++i) {
  234. total += (double)curr->cpu_ticks[i] - (double)prev->cpu_ticks[i];
  235. }
  236. if (total > 1e-6) {
  237. mtr->values[CPU_METER_NICE]
  238. = ((double)curr->cpu_ticks[CPU_STATE_NICE] - (double)prev->cpu_ticks[CPU_STATE_NICE]) * 100.0 / total;
  239. mtr->values[CPU_METER_NORMAL]
  240. = ((double)curr->cpu_ticks[CPU_STATE_USER] - (double)prev->cpu_ticks[CPU_STATE_USER]) * 100.0 / total;
  241. mtr->values[CPU_METER_KERNEL]
  242. = ((double)curr->cpu_ticks[CPU_STATE_SYSTEM] - (double)prev->cpu_ticks[CPU_STATE_SYSTEM]) * 100.0 / total;
  243. } else {
  244. mtr->values[CPU_METER_NICE] = 0.0;
  245. mtr->values[CPU_METER_NORMAL] = 0.0;
  246. mtr->values[CPU_METER_KERNEL] = 0.0;
  247. }
  248. mtr->curItems = 3;
  249. /* Convert to percent and return */
  250. total = mtr->values[CPU_METER_NICE] + mtr->values[CPU_METER_NORMAL] + mtr->values[CPU_METER_KERNEL];
  251. mtr->values[CPU_METER_FREQUENCY] = NAN;
  252. mtr->values[CPU_METER_TEMPERATURE] = NAN;
  253. return CLAMP(total, 0.0, 100.0);
  254. }
  255. void Platform_setMemoryValues(Meter* mtr) {
  256. const DarwinMachine* dhost = (const DarwinMachine*) mtr->host;
  257. const struct vm_statistics* vm = &dhost->vm_stats;
  258. double page_K = (double)vm_page_size / (double)1024;
  259. mtr->total = dhost->host_info.max_mem / 1024;
  260. mtr->values[MEMORY_METER_USED] = (double)(vm->active_count + vm->wire_count) * page_K;
  261. // mtr->values[MEMORY_METER_SHARED] = "shared memory, like tmpfs and shm"
  262. // mtr->values[MEMORY_METER_COMPRESSED] = "compressed memory, like zswap on linux"
  263. mtr->values[MEMORY_METER_BUFFERS] = (double)vm->purgeable_count * page_K;
  264. mtr->values[MEMORY_METER_CACHE] = (double)vm->inactive_count * page_K;
  265. // mtr->values[MEMORY_METER_AVAILABLE] = "available memory"
  266. }
  267. void Platform_setSwapValues(Meter* mtr) {
  268. int mib[2] = {CTL_VM, VM_SWAPUSAGE};
  269. struct xsw_usage swapused;
  270. size_t swlen = sizeof(swapused);
  271. sysctl(mib, 2, &swapused, &swlen, NULL, 0);
  272. mtr->total = swapused.xsu_total / 1024;
  273. mtr->values[SWAP_METER_USED] = swapused.xsu_used / 1024;
  274. // mtr->values[SWAP_METER_CACHE] = "pages that are both in swap and RAM, like SwapCached on linux"
  275. // mtr->values[SWAP_METER_FRONTSWAP] = "pages that are accounted to swap but stored elsewhere, like frontswap on linux"
  276. }
  277. void Platform_setZfsArcValues(Meter* this) {
  278. const DarwinMachine* dhost = (const DarwinMachine*) this->host;
  279. ZfsArcMeter_readStats(this, &dhost->zfs);
  280. }
  281. void Platform_setZfsCompressedArcValues(Meter* this) {
  282. const DarwinMachine* dhost = (const DarwinMachine*) this->host;
  283. ZfsCompressedArcMeter_readStats(this, &dhost->zfs);
  284. }
  285. char* Platform_getProcessEnv(pid_t pid) {
  286. char* env = NULL;
  287. int argmax;
  288. size_t bufsz = sizeof(argmax);
  289. int mib[3];
  290. mib[0] = CTL_KERN;
  291. mib[1] = KERN_ARGMAX;
  292. if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) {
  293. char* buf = xMalloc(argmax);
  294. if (buf) {
  295. mib[0] = CTL_KERN;
  296. mib[1] = KERN_PROCARGS2;
  297. mib[2] = pid;
  298. bufsz = argmax;
  299. if (sysctl(mib, 3, buf, &bufsz, 0, 0) == 0) {
  300. if (bufsz > sizeof(int)) {
  301. char *p = buf, *endp = buf + bufsz;
  302. int argc = *(int*)(void*)p;
  303. p += sizeof(int);
  304. // skip exe
  305. p = strchr(p, 0) + 1;
  306. // skip padding
  307. while (!*p && p < endp)
  308. ++p;
  309. // skip argv
  310. for (; argc-- && p < endp; p = strrchr(p, 0) + 1)
  311. ;
  312. // skip padding
  313. while (!*p && p < endp)
  314. ++p;
  315. size_t size = endp - p;
  316. env = xMalloc(size + 2);
  317. memcpy(env, p, size);
  318. env[size] = 0;
  319. env[size + 1] = 0;
  320. }
  321. }
  322. free(buf);
  323. }
  324. }
  325. return env;
  326. }
  327. FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) {
  328. (void)pid;
  329. return NULL;
  330. }
  331. void Platform_getFileDescriptors(double* used, double* max) {
  332. Generic_getFileDescriptors_sysctl(used, max);
  333. }
  334. bool Platform_getDiskIO(DiskIOData* data) {
  335. if (!iokit_available)
  336. return false;
  337. io_iterator_t drive_list;
  338. /* Get the list of all drives */
  339. if (IOServiceGetMatchingServices(iokit_port, IOServiceMatching("IOBlockStorageDriver"), &drive_list))
  340. return false;
  341. unsigned long long int read_sum = 0, write_sum = 0, timeSpend_sum = 0;
  342. io_registry_entry_t drive;
  343. while ((drive = IOIteratorNext(drive_list)) != 0) {
  344. CFMutableDictionaryRef properties_tmp = NULL;
  345. /* Get the properties of this drive */
  346. if (IORegistryEntryCreateCFProperties(drive, &properties_tmp, kCFAllocatorDefault, 0)) {
  347. IOObjectRelease(drive);
  348. IOObjectRelease(drive_list);
  349. return false;
  350. }
  351. if (!properties_tmp) {
  352. IOObjectRelease(drive);
  353. continue;
  354. }
  355. CFDictionaryRef properties = properties_tmp;
  356. /* Get the statistics of this drive */
  357. CFDictionaryRef statistics = (CFDictionaryRef) CFDictionaryGetValue(properties, CFSTR(kIOBlockStorageDriverStatisticsKey));
  358. if (!statistics) {
  359. CFRelease(properties);
  360. IOObjectRelease(drive);
  361. continue;
  362. }
  363. CFNumberRef number;
  364. unsigned long long int value;
  365. /* Get bytes read */
  366. number = (CFNumberRef) CFDictionaryGetValue(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey));
  367. if (number != 0) {
  368. CFNumberGetValue(number, kCFNumberSInt64Type, &value);
  369. read_sum += value;
  370. }
  371. /* Get bytes written */
  372. number = (CFNumberRef) CFDictionaryGetValue(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey));
  373. if (number != 0) {
  374. CFNumberGetValue(number, kCFNumberSInt64Type, &value);
  375. write_sum += value;
  376. }
  377. /* Get total read time (in ns) */
  378. number = (CFNumberRef) CFDictionaryGetValue(statistics, CFSTR(kIOBlockStorageDriverStatisticsTotalReadTimeKey));
  379. if (number != 0) {
  380. CFNumberGetValue(number, kCFNumberSInt64Type, &value);
  381. timeSpend_sum += value;
  382. }
  383. /* Get total write time (in ns) */
  384. number = (CFNumberRef) CFDictionaryGetValue(statistics, CFSTR(kIOBlockStorageDriverStatisticsTotalWriteTimeKey));
  385. if (number != 0) {
  386. CFNumberGetValue(number, kCFNumberSInt64Type, &value);
  387. timeSpend_sum += value;
  388. }
  389. CFRelease(properties);
  390. IOObjectRelease(drive);
  391. }
  392. data->totalBytesRead = read_sum;
  393. data->totalBytesWritten = write_sum;
  394. data->totalMsTimeSpend = timeSpend_sum / 1e6; /* Convert from ns to ms */
  395. if (drive_list)
  396. IOObjectRelease(drive_list);
  397. return true;
  398. }
  399. /* Caution: Given that interfaces are dynamic, and it is not possible to get statistics on interfaces that no longer exist,
  400. if some interface disappears between the time of two samples, the values of the second sample may be lower than those of
  401. the first one. */
  402. bool Platform_getNetworkIO(NetworkIOData* data) {
  403. int mib[6] = {CTL_NET,
  404. PF_ROUTE, /* routing messages */
  405. 0, /* protocol number, currently always 0 */
  406. 0, /* select all address families */
  407. NET_RT_IFLIST2, /* interface list with addresses */
  408. 0};
  409. for (size_t retry = 0; retry < 4; retry++) {
  410. size_t len = 0;
  411. /* Determine len */
  412. if (sysctl(mib, ARRAYSIZE(mib), NULL, &len, NULL, 0) < 0 || len == 0)
  413. return false;
  414. len += 16 * retry * retry * sizeof(struct if_msghdr2);
  415. char *buf = xMalloc(len);
  416. if (sysctl(mib, ARRAYSIZE(mib), buf, &len, NULL, 0) < 0) {
  417. free(buf);
  418. if (errno == ENOMEM && retry < 3)
  419. continue;
  420. else
  421. return false;
  422. }
  423. uint64_t bytesReceived_sum = 0, packetsReceived_sum = 0, bytesTransmitted_sum = 0, packetsTransmitted_sum = 0;
  424. for (char *next = buf; next < buf + len;) {
  425. void *tmp = (void*) next;
  426. struct if_msghdr *ifm = (struct if_msghdr*) tmp;
  427. next += ifm->ifm_msglen;
  428. if (ifm->ifm_type != RTM_IFINFO2)
  429. continue;
  430. struct if_msghdr2 *ifm2 = (struct if_msghdr2*) ifm;
  431. if (ifm2->ifm_data.ifi_type != IFT_LOOP) { /* do not count loopback traffic */
  432. bytesReceived_sum += ifm2->ifm_data.ifi_ibytes;
  433. packetsReceived_sum += ifm2->ifm_data.ifi_ipackets;
  434. bytesTransmitted_sum += ifm2->ifm_data.ifi_obytes;
  435. packetsTransmitted_sum += ifm2->ifm_data.ifi_opackets;
  436. }
  437. }
  438. data->bytesReceived = bytesReceived_sum;
  439. data->packetsReceived = packetsReceived_sum;
  440. data->bytesTransmitted = bytesTransmitted_sum;
  441. data->packetsTransmitted = packetsTransmitted_sum;
  442. free(buf);
  443. }
  444. return true;
  445. }
  446. void Platform_getBattery(double* percent, ACPresence* isOnAC) {
  447. *percent = NAN;
  448. *isOnAC = AC_ERROR;
  449. CFArrayRef list = NULL;
  450. CFTypeRef power_sources = IOPSCopyPowerSourcesInfo();
  451. if (!power_sources)
  452. goto cleanup;
  453. list = IOPSCopyPowerSourcesList(power_sources);
  454. if (!list)
  455. goto cleanup;
  456. double cap_current = 0.0;
  457. double cap_max = 0.0;
  458. /* Get the battery */
  459. for (int i = 0, len = CFArrayGetCount(list); i < len; ++i) {
  460. CFDictionaryRef power_source = IOPSGetPowerSourceDescription(power_sources, CFArrayGetValueAtIndex(list, i)); /* GET rule */
  461. if (!power_source)
  462. continue;
  463. CFStringRef power_type = CFDictionaryGetValue(power_source, CFSTR(kIOPSTransportTypeKey)); /* GET rule */
  464. if (kCFCompareEqualTo != CFStringCompare(power_type, CFSTR(kIOPSInternalType), 0))
  465. continue;
  466. /* Determine the AC state */
  467. CFStringRef power_state = CFDictionaryGetValue(power_source, CFSTR(kIOPSPowerSourceStateKey));
  468. if (*isOnAC != AC_PRESENT)
  469. *isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0)) ? AC_PRESENT : AC_ABSENT;
  470. /* Get the percentage remaining */
  471. double tmp;
  472. CFNumberGetValue(CFDictionaryGetValue(power_source, CFSTR(kIOPSCurrentCapacityKey)), kCFNumberDoubleType, &tmp);
  473. cap_current += tmp;
  474. CFNumberGetValue(CFDictionaryGetValue(power_source, CFSTR(kIOPSMaxCapacityKey)), kCFNumberDoubleType, &tmp);
  475. cap_max += tmp;
  476. }
  477. if (cap_max > 0.0)
  478. *percent = 100.0 * cap_current / cap_max;
  479. cleanup:
  480. if (list)
  481. CFRelease(list);
  482. if (power_sources)
  483. CFRelease(power_sources);
  484. }
  485. void Platform_gettime_monotonic(uint64_t* msec) {
  486. #ifdef HAVE_HOST_GET_CLOCK_SERVICE
  487. clock_serv_t cclock;
  488. mach_timespec_t mts;
  489. host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
  490. clock_get_time(cclock, &mts);
  491. mach_port_deallocate(mach_task_self(), cclock);
  492. *msec = ((uint64_t)mts.tv_sec * 1000) + ((uint64_t)mts.tv_nsec / 1000000);
  493. #else
  494. Generic_gettime_monotonic(msec);
  495. #endif
  496. }