Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a progress bar for a selected process' CPU value #25

Merged
merged 3 commits into from
Apr 7, 2018

Conversation

LauraAubin
Copy link
Owner

@LauraAubin LauraAubin commented Apr 4, 2018

What does this do?

Part of #6

This creates a progress bar for the selected process' CPU usage. The progress bar is out of 100%.

screen shot 2018-04-07 at 2 02 42 pm

How is this accomplished?

  • Value will update every 5 seconds

This makes use of the popen command. We use this to open a process command and read the values that it returns.

How to parse the output of a Linux command.


The following code snippet is from Chromium, an open source web browser provided by Google:

TimeTicks time = TimeTicks::Now();
int64_t task_time = TimeValToMicroseconds(task_timeval);

if (last_system_time_ == 0) {
    // First call, just set the last values.
    last_cpu_time_ = time;
    last_system_time_ = task_time;
    return 0;
}

int64_t system_time_delta = task_time - last_system_time_;
int64_t time_delta = (time - last_cpu_time_).InMicroseconds();
DCHECK_NE(0U, time_delta);

if (time_delta == 0)
    return 0;

    last_cpu_time_ = time;
    last_system_time_ = task_time;

   return static_cast<double>(system_time_delta * 100.0) / time_delta;
}

Sketch of the math:



Basically, we calculated the CPU percentage based on how long the process has been running. This is how long it has been running on the CPU, not as if the program was just opened now. We found this value by using the top command. To use an example:

Imagine that a process has been running for 30 seconds. If we check it again 1 second later and the process has been running on the CPU for 30.2 seconds, then we subtract the previous time from the current time:

30.2 - 30 = 0.2 seconds running on the CPU in 1 second.

0.2 * 100 = 20%.

This means that the process is taking up 20% of the CPU. In our case, we're checking on intervals of 5 seconds, so we subtract the final number by 5.

What did I learn?

ps doesn't actually return the current CPU percentage for a running process. While top is monitoring, ps gives you the average percentage over the life of the program. More context here.

@LauraAubin LauraAubin changed the title Create progress bar with an initial value and a max value Create progress bar for a selected process's CPU value Apr 4, 2018
@LauraAubin LauraAubin changed the title Create progress bar for a selected process's CPU value Create a progress bar for a selected process's CPU value Apr 4, 2018
@LauraAubin LauraAubin changed the title Create a progress bar for a selected process's CPU value Create a progress bar for a selected process' CPU value Apr 4, 2018
@@ -33,6 +35,10 @@ @implementation NSRunningApplication (params)
- (pid_t)selectedPID;
{
CurrentlySelectedProcessID = self.processIdentifier;

last_time = 0;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to reset these values when you click on a new process from the list

@LauraAubin LauraAubin merged commit 4829575 into master Apr 7, 2018
@LauraAubin LauraAubin deleted the individual-process-cpu-progess-bar branch April 7, 2018 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant