mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-09-12 05:48:40 +00:00
perf sched: Fix for getting task's execution time
In current code, task's execute time is got by reading '/proc/<pid>/sched' file, it's wrong if the task is created by pthread_create(), because every thread task has same pid. This way also has two demerits: 1: 'perf sched replay' can't work if the kernel is not compiled with the 'CONFIG_SCHED_DEBUG' option 2: perf tool should depend on proc file system So, this patch uses PERF_COUNT_SW_TASK_CLOCK to get task's execution time instead of reading /proc file. Changelog v2 -> v3: use PERF_COUNT_SW_TASK_CLOCK instead of rusage() as Ingo's suggestion Reported-by: Torok Edwin <edwintorok@gmail.com> Signed-off-by: Xiao Guangrong <xiaoguangrong@cn.fujitsu.com> Cc: Xiao Guangrong <ericxiao.gr@gmail.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Paul Mackerras <paulus@samba.org> LKML-Reference: <4B1F7322.80103@cn.fujitsu.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:
parent
822a696111
commit
c0c9e72150
1 changed files with 27 additions and 28 deletions
|
@ -13,7 +13,6 @@
|
||||||
#include "util/debug.h"
|
#include "util/debug.h"
|
||||||
#include "util/data_map.h"
|
#include "util/data_map.h"
|
||||||
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
|
|
||||||
#include <semaphore.h>
|
#include <semaphore.h>
|
||||||
|
@ -414,34 +413,33 @@ static u64 get_cpu_usage_nsec_parent(void)
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
static u64 get_cpu_usage_nsec_self(void)
|
static int self_open_counters(void)
|
||||||
{
|
{
|
||||||
char filename [] = "/proc/1234567890/sched";
|
struct perf_event_attr attr;
|
||||||
unsigned long msecs, nsecs;
|
int fd;
|
||||||
char *line = NULL;
|
|
||||||
u64 total = 0;
|
memset(&attr, 0, sizeof(attr));
|
||||||
size_t len = 0;
|
|
||||||
ssize_t chars;
|
attr.type = PERF_TYPE_SOFTWARE;
|
||||||
FILE *file;
|
attr.config = PERF_COUNT_SW_TASK_CLOCK;
|
||||||
|
|
||||||
|
fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
|
||||||
|
|
||||||
|
if (fd < 0)
|
||||||
|
die("Error: sys_perf_event_open() syscall returned"
|
||||||
|
"with %d (%s)\n", fd, strerror(errno));
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static u64 get_cpu_usage_nsec_self(int fd)
|
||||||
|
{
|
||||||
|
u64 runtime;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
sprintf(filename, "/proc/%d/sched", getpid());
|
ret = read(fd, &runtime, sizeof(runtime));
|
||||||
file = fopen(filename, "r");
|
BUG_ON(ret != sizeof(runtime));
|
||||||
BUG_ON(!file);
|
|
||||||
|
|
||||||
while ((chars = getline(&line, &len, file)) != -1) {
|
return runtime;
|
||||||
ret = sscanf(line, "se.sum_exec_runtime : %ld.%06ld\n",
|
|
||||||
&msecs, &nsecs);
|
|
||||||
if (ret == 2) {
|
|
||||||
total = msecs*1e6 + nsecs;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (line)
|
|
||||||
free(line);
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *thread_func(void *ctx)
|
static void *thread_func(void *ctx)
|
||||||
|
@ -450,9 +448,11 @@ static void *thread_func(void *ctx)
|
||||||
u64 cpu_usage_0, cpu_usage_1;
|
u64 cpu_usage_0, cpu_usage_1;
|
||||||
unsigned long i, ret;
|
unsigned long i, ret;
|
||||||
char comm2[22];
|
char comm2[22];
|
||||||
|
int fd;
|
||||||
|
|
||||||
sprintf(comm2, ":%s", this_task->comm);
|
sprintf(comm2, ":%s", this_task->comm);
|
||||||
prctl(PR_SET_NAME, comm2);
|
prctl(PR_SET_NAME, comm2);
|
||||||
|
fd = self_open_counters();
|
||||||
|
|
||||||
again:
|
again:
|
||||||
ret = sem_post(&this_task->ready_for_work);
|
ret = sem_post(&this_task->ready_for_work);
|
||||||
|
@ -462,16 +462,15 @@ static void *thread_func(void *ctx)
|
||||||
ret = pthread_mutex_unlock(&start_work_mutex);
|
ret = pthread_mutex_unlock(&start_work_mutex);
|
||||||
BUG_ON(ret);
|
BUG_ON(ret);
|
||||||
|
|
||||||
cpu_usage_0 = get_cpu_usage_nsec_self();
|
cpu_usage_0 = get_cpu_usage_nsec_self(fd);
|
||||||
|
|
||||||
for (i = 0; i < this_task->nr_events; i++) {
|
for (i = 0; i < this_task->nr_events; i++) {
|
||||||
this_task->curr_event = i;
|
this_task->curr_event = i;
|
||||||
process_sched_event(this_task, this_task->atoms[i]);
|
process_sched_event(this_task, this_task->atoms[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
cpu_usage_1 = get_cpu_usage_nsec_self();
|
cpu_usage_1 = get_cpu_usage_nsec_self(fd);
|
||||||
this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
|
this_task->cpu_usage = cpu_usage_1 - cpu_usage_0;
|
||||||
|
|
||||||
ret = sem_post(&this_task->work_done_sem);
|
ret = sem_post(&this_task->work_done_sem);
|
||||||
BUG_ON(ret);
|
BUG_ON(ret);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue