How to get total cpu usage in Linux using C++

cat /proc/stat http://www.linuxhowtos.org/System/procstat.htm I agree with this answer above. The cpu line in this file gives the total number of “jiffies” your system has spent doing different types of processing. What you need to do is take 2 readings of this file, seperated by whatever interval of time you require. The numbers are increasing values … Read more

Simulate steady CPU load and spikes

First off, you have to understand that CPU usage is always an average over a certain time. At any given time, the CPU is either working or it is not. The CPU is never 40% working. We can, however, simulate a 40% load over say a second by having the CPU work for 0.4 seconds … Read more

Why the cpu performance counter kept reporting 0% cpu usage?

The first iteration of he counter will always be 0, because it has nothing to compare to the last value. Try this: var cpuload = new PerformanceCounter(“Processor”, “% Processor Time”, “_Total”); Console.WriteLine(cpuload.NextValue() + “%”); Console.WriteLine(cpuload.NextValue() + “%”); Console.WriteLine(cpuload.NextValue() + “%”); Console.WriteLine(cpuload.NextValue() + “%”); Console.WriteLine(cpuload.NextValue() + “%”); Then you should see some data coming out. It’s … Read more

How to get the cpu usage per thread on windows (win32)

You must use these functions to get the cpu usage per thread and process. GetThreadTimes (Retrieves timing information for the specified thread.) GetProcessTimes (Retrieves timing information for the specified process.) GetSystemTime (Retrieves the current system date and time. The system time is expressed in Coordinated Universal Time UTC) Here a excellent article from Dr. Dobb’s … Read more

What is the correct Performance Counter to get CPU and Memory Usage of a Process?

From this post: To get the entire PC CPU and Memory usage: using System.Diagnostics; Then declare globally: private PerformanceCounter theCPUCounter = new PerformanceCounter(“Processor”, “% Processor Time”, “_Total”); Then to get the CPU time, simply call the NextValue() method: this.theCPUCounter.NextValue(); This will get you the CPU usage As for memory usage, same thing applies I believe: … Read more

iOS – Get CPU usage from application

Update. This code is working for me: Update 2. The thread_list was leaking, so added vm_deallocate #import <mach/mach.h> #import <assert.h> float cpu_usage() { kern_return_t kr; task_info_data_t tinfo; mach_msg_type_number_t task_info_count; task_info_count = TASK_INFO_MAX; kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count); if (kr != KERN_SUCCESS) { return -1; } task_basic_info_t basic_info; thread_array_t thread_list; mach_msg_type_number_t thread_count; thread_info_data_t thinfo; mach_msg_type_number_t … Read more

MySQL high CPU usage [closed]

First I’d say you probably want to turn off persistent connections as they almost always do more harm than good. Secondly I’d say you want to double check your MySQL users, just to make sure it’s not possible for anyone to be connecting from a remote server. This is also a major security thing to … Read more

Get Memory Usage in Android

I use this function to calculate cpu usage. Hope it can help you. private float readUsage() { try { RandomAccessFile reader = new RandomAccessFile(“/proc/stat”, “r”); String load = reader.readLine(); String[] toks = load.split(” +”); // Split on one or more spaces long idle1 = Long.parseLong(toks[4]); long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5]) + Long.parseLong(toks[6]) … Read more