Skip to main content
replaced http://meta.stackexchange.com/ with https://meta.stackexchange.com/
Source Link

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting.

So, when a CPU usage percentage of 50% is reported for a process, that doesn't mean that only 50% of the CPU is being used. It means that over a specific time period, the CPU spent 50% of its time working on the process. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problemXY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting.

So, when a CPU usage percentage of 50% is reported for a process, that doesn't mean that only 50% of the CPU is being used. It means that over a specific time period, the CPU spent 50% of its time working on the process. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting.

So, when a CPU usage percentage of 50% is reported for a process, that doesn't mean that only 50% of the CPU is being used. It means that over a specific time period, the CPU spent 50% of its time working on the process. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

added 83 characters in body
Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting. When you see

So, when a CPU atusage percentage of 50% is reported for a process, that doesn't mean that only 50% of the CPU is being used, it is the percentage of time. It means that over a specific time period, the CPU spent 50% of its time working on the process. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting. When you see a CPU at 50%, that doesn't mean that only 50% of the CPU is being used, it is the percentage of time that the CPU spent working. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting.

So, when a CPU usage percentage of 50% is reported for a process, that doesn't mean that only 50% of the CPU is being used. It means that over a specific time period, the CPU spent 50% of its time working on the process. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.

Source Link
terdon
  • 252.3k
  • 69
  • 480
  • 718

Quick answer: no, that's not really possible.

The problem here is first with the concept of CPU "percentage". Strictly speaking, that doesn't actually mean anything. A CPU only has two states: it is either working on something (100%) or it is waiting. When you see a CPU at 50%, that doesn't mean that only 50% of the CPU is being used, it is the percentage of time that the CPU spent working. For example, if the refresh interval is set to 1 second and the CPU spent half a second working on process N, then the CPU% for process N will be shown as 50%.

With that in mind, we can have a look at the various ways of checking CPU%. For ps, this is measured as (from man ps):

CPU usage is currently expressed as the percentage of time spent
running during the entire lifetime of a process.  This is not ideal,
and it does not conform to the standards that ps otherwise conforms to.
CPU usage is unlikely to add up to exactly 100%.

So, the percentage as reported by ps is actually the average over the entire lifetime of the process. By contrast, top reports:

The task's share of the elapsed CPU time since the last screen
update, expressed as a percentage of total CPU time.

I assume gnome-system-monitor works in a similar way and its CPU % is also the percentage of time the CPU spent on a given process since the last refresh. My C++ knowledge is essentially non-existant but the following lines from proctable.cpp (part of the gnome-system-monitor source code) suggest that it is indeed taking the average since the last refresh:

/* FIXME: total cpu time elapsed should be calculated on an individual basis here
** should probably have a total_time_last gint in the ProcInfo structure */
glibtop_get_cpu (&cpu);
app->cpu_total_time = MAX(cpu.total - app->cpu_total_time_last, 1);
app->cpu_total_time_last = cpu.total;

So no, you can't get ps and gnome-system-monitor to agree because ps shows the percentage of time the CPU spent on a process since the process was started and gnome-system-monitor shows the percentage since the last refresh.

There is also no reason I can imagine why you would need to get those two values to agree. I think what we have here is some sort of XY problem. You might want to ask a new question describing what your final objective is, what problem you are trying to solve by making the two values agree.