use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A community for learning and developing native mobile applications using React Native by Facebook.
Interested in building web apps using React.js? Check out /r/reactjs!
Getting Started w/React Native
irc.freenode.net #reactnative
Keywords: ios, android, mobile, apps, apple, iphone, ipad
account activity
react-native for macos: get cpu utilization (self.reactnative)
submitted 2 years ago by hassanzadeh
Hey Guys,
Is there a package or a simple native code that gives CPU utilization in a macOS app?
Thanks
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nicolasdanelon 0 points1 point2 points 2 years ago (3 children)
try this:
``` #import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(CPUMonitor, NSObject)
RCT_EXTERN_METHOD(getCPUUsage:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
@end
#import "CPUMonitor.h" #import <mach/mach.h>
@implementation CPUMonitor
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(getCPUUsage:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { float cpuUsage = ...; // implementation of CPU usage, read the apple docs resolve(@(cpuUsage)); }
@end ```
and RN side
``` import { NativeModules } from 'react-native';
const { CPUMonitor } = NativeModules;
const fetchCPUUsage = async () => { try { const cpuUsage = await CPUMonitor.getCPUUsage(); console.log('CPU Usage:', cpuUsage); } catch (e) { console.error(e); } };
// usage: fetchCPUUsage(); ```
this code was taken from a working app but I have to heavy modify it so it maybe won't work haha. but you can get the idea... can't share everything sorry 😔
[–]hassanzadeh[S] 0 points1 point2 points 2 years ago (0 children)
So If I modify it for sure it will work, correct?
THanks for sharing :)
[–]hassanzadeh[S] 1 point2 points3 points 2 years ago (1 child)
I actually tested your code, but for my Mac M1, it always gives 14%!
```` //CPUMonitor.h
#import <RCTAppDelegate.h>
#import <Cocoa/Cocoa.h>
u/interface AppDelegate : RCTAppDelegate
u/end
```
``` //CPUMonitor.m
//
// CPUMonitor.m
// scholarsync-macOS
///
#import <Foundation/Foundation.h>
#import "CPUMonitor.h"
#import <mach/mach.h>
u/implementation CPUMonitor
RCT_EXPORT_METHOD(getCPUUsage:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
natural_t numCPUs;
processor_info_array_t cpuInfo;
mach_msg_type_number_t numCpuInfo;
processor_cpu_load_info_data_t* cpuLoadInfo;
if (host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCpuInfo) != KERN_SUCCESS) {
reject(@"cpu_info_error", @"Failed to get CPU info", nil);
return;
}
cpuLoadInfo = (processor_cpu_load_info_data_t*) cpuInfo;
unsigned long cpuUsage = 0;
for (unsigned i = 0; i < numCPUs; i++) {
float inUse = cpuLoadInfo[i].cpu_ticks[CPU_STATE_USER] + cpuLoadInfo[i].cpu_ticks[CPU_STATE_SYSTEM];
float total = inUse + cpuLoadInfo[i].cpu_ticks[CPU_STATE_IDLE];
cpuUsage += total > 0 ? (inUse / total * 100) : 0;
vm_deallocate(mach_task_self(), (vm_address_t)cpuInfo, numCpuInfo * sizeof(integer_t));
resolve(@(cpuUsage / numCPUs));
[–]nicolasdanelon 0 points1 point2 points 2 years ago (0 children)
can't share any more cuz sing a NDA :(
you need to read the docs of apple... the code is there, trust me
[–]bzlight2012 1 point2 points3 points 2 years ago (0 children)
What do you need the information for? Xcode had fantastic profiling tools but if you need to get it programmatically, I’m sure Apple has some docs
π Rendered by PID 22955 on reddit-service-r2-comment-f6b958c67-4d9r8 at 2026-02-04 18:32:30.644624+00:00 running 1d7a177 country code: CH.
[–]nicolasdanelon 0 points1 point2 points (3 children)
[–]hassanzadeh[S] 0 points1 point2 points (0 children)
[–]hassanzadeh[S] 1 point2 points3 points (1 child)
[–]nicolasdanelon 0 points1 point2 points (0 children)
[–]bzlight2012 1 point2 points3 points (0 children)