Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 147679
b: refs/heads/master
c: a21ca2c
h: refs/heads/master
i:
  147677: a69562f
  147675: 85f3b63
  147671: e960f00
  147663: 55ecf7f
  147647: 0b0be43
v: v3
  • Loading branch information
Ingo Molnar committed Jun 6, 2009
1 parent dc368a7 commit 7d036f7
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 275 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 2f335a02b3c816e77e7df1d15b12e3bbb8f4c8f0
refs/heads/master: a21ca2cac582886a3e95c8bb84ff7c52d4d15e54
105 changes: 47 additions & 58 deletions trunk/Documentation/perf_counter/builtin-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))

static long default_interval = 100000;
static long event_count[MAX_COUNTERS];

static int fd[MAX_NR_CPUS][MAX_COUNTERS];

static long default_interval = 100000;

static int nr_cpus = 0;
static unsigned int page_size;
static unsigned int mmap_pages = 128;
Expand All @@ -38,22 +38,44 @@ static int inherit = 1;
static int force = 0;
static int append_file = 0;

const unsigned int default_count[] = {
1000000,
1000000,
10000,
10000,
1000000,
10000,
static long samples;
static struct timeval last_read;
static struct timeval this_read;

static __u64 bytes_written;

static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];

static int nr_poll;
static int nr_cpu;

struct mmap_event {
struct perf_event_header header;
__u32 pid;
__u32 tid;
__u64 start;
__u64 len;
__u64 pgoff;
char filename[PATH_MAX];
};

struct comm_event {
struct perf_event_header header;
__u32 pid;
__u32 tid;
char comm[16];
};


struct mmap_data {
int counter;
void *base;
unsigned int mask;
unsigned int prev;
int counter;
void *base;
unsigned int mask;
unsigned int prev;
};

static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];

static unsigned int mmap_read_head(struct mmap_data *md)
{
struct perf_counter_mmap_page *pc = md->base;
Expand All @@ -65,11 +87,6 @@ static unsigned int mmap_read_head(struct mmap_data *md)
return head;
}

static long samples;
static struct timeval last_read, this_read;

static __u64 bytes_written;

static void mmap_read(struct mmap_data *md)
{
unsigned int head = mmap_read_head(md);
Expand Down Expand Up @@ -157,29 +174,6 @@ static void sig_handler(int sig)
done = 1;
}

static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];

static int nr_poll;
static int nr_cpu;

struct mmap_event {
struct perf_event_header header;
__u32 pid;
__u32 tid;
__u64 start;
__u64 len;
__u64 pgoff;
char filename[PATH_MAX];
};

struct comm_event {
struct perf_event_header header;
__u32 pid;
__u32 tid;
char comm[16];
};

static void pid_synthesize_comm_event(pid_t pid, int full)
{
struct comm_event comm_ev;
Expand Down Expand Up @@ -341,24 +335,21 @@ static int group_fd;

static void create_counter(int counter, int cpu, pid_t pid)
{
struct perf_counter_attr attr;
struct perf_counter_attr *attr = attrs + counter;
int track = 1;

memset(&attr, 0, sizeof(attr));
attr.config = event_id[counter];
attr.sample_period = event_count[counter];
attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD;
attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD;
if (freq) {
attr.freq = 1;
attr.sample_freq = freq;
attr->freq = 1;
attr->sample_freq = freq;
}
attr.mmap = track;
attr.comm = track;
attr.inherit = (cpu < 0) && inherit;
attr->mmap = track;
attr->comm = track;
attr->inherit = (cpu < 0) && inherit;

track = 0; /* only the first counter needs these */

fd[nr_cpu][counter] = sys_perf_counter_open(&attr, pid, cpu, group_fd, 0);
fd[nr_cpu][counter] = sys_perf_counter_open(attr, pid, cpu, group_fd, 0);

if (fd[nr_cpu][counter] < 0) {
int err = errno;
Expand Down Expand Up @@ -542,16 +533,14 @@ int cmd_record(int argc, const char **argv, const char *prefix)
if (!argc && target_pid == -1 && !system_wide)
usage_with_options(record_usage, options);

if (!nr_counters) {
if (!nr_counters)
nr_counters = 1;
event_id[0] = 0;
}

for (counter = 0; counter < nr_counters; counter++) {
if (event_count[counter])
if (attrs[counter].sample_period)
continue;

event_count[counter] = default_interval;
attrs[counter].sample_period = default_interval;
}

return __cmd_record(argc, argv);
Expand Down
76 changes: 32 additions & 44 deletions trunk/Documentation/perf_counter/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,22 @@

#include <sys/prctl.h>

static int system_wide = 0;
static int inherit = 1;
static struct perf_counter_attr default_attrs[MAX_COUNTERS] = {

static __u64 default_event_id[MAX_COUNTERS] = {
EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK),
EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CONTEXT_SWITCHES),
EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_MIGRATIONS),
EID(PERF_TYPE_SOFTWARE, PERF_COUNT_PAGE_FAULTS),
{ .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_TASK_CLOCK },
{ .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_CONTEXT_SWITCHES },
{ .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_CPU_MIGRATIONS },
{ .type = PERF_TYPE_SOFTWARE, .config = PERF_COUNT_PAGE_FAULTS },

EID(PERF_TYPE_HARDWARE, PERF_COUNT_CPU_CYCLES),
EID(PERF_TYPE_HARDWARE, PERF_COUNT_INSTRUCTIONS),
EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_REFERENCES),
EID(PERF_TYPE_HARDWARE, PERF_COUNT_CACHE_MISSES),
{ .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CPU_CYCLES },
{ .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_INSTRUCTIONS },
{ .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CACHE_REFERENCES },
{ .type = PERF_TYPE_HARDWARE, .config = PERF_COUNT_CACHE_MISSES },
};

static int default_interval = 100000;
static int event_count[MAX_COUNTERS];
static int system_wide = 0;
static int inherit = 1;

static int fd[MAX_NR_CPUS][MAX_COUNTERS];

static int target_pid = -1;
Expand All @@ -86,33 +85,27 @@ static __u64 walltime_nsecs;

static void create_perfstat_counter(int counter)
{
struct perf_counter_attr attr;

memset(&attr, 0, sizeof(attr));
attr.config = event_id[counter];
attr.sample_type = 0;
attr.exclude_kernel = event_mask[counter] & EVENT_MASK_KERNEL;
attr.exclude_user = event_mask[counter] & EVENT_MASK_USER;
struct perf_counter_attr *attr = attrs + counter;

if (scale)
attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING;
attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING;

if (system_wide) {
int cpu;
for (cpu = 0; cpu < nr_cpus; cpu ++) {
fd[cpu][counter] = sys_perf_counter_open(&attr, -1, cpu, -1, 0);
fd[cpu][counter] = sys_perf_counter_open(attr, -1, cpu, -1, 0);
if (fd[cpu][counter] < 0) {
printf("perfstat error: syscall returned with %d (%s)\n",
fd[cpu][counter], strerror(errno));
exit(-1);
}
}
} else {
attr.inherit = inherit;
attr.disabled = 1;
attr->inherit = inherit;
attr->disabled = 1;

fd[0][counter] = sys_perf_counter_open(&attr, 0, -1, -1, 0);
fd[0][counter] = sys_perf_counter_open(attr, 0, -1, -1, 0);
if (fd[0][counter] < 0) {
printf("perfstat error: syscall returned with %d (%s)\n",
fd[0][counter], strerror(errno));
Expand All @@ -126,9 +119,13 @@ static void create_perfstat_counter(int counter)
*/
static inline int nsec_counter(int counter)
{
if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK))
if (attrs[counter].type != PERF_TYPE_SOFTWARE)
return 0;

if (attrs[counter].config == PERF_COUNT_CPU_CLOCK)
return 1;
if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))

if (attrs[counter].config == PERF_COUNT_TASK_CLOCK)
return 1;

return 0;
Expand Down Expand Up @@ -177,7 +174,8 @@ static void read_counter(int counter)
/*
* Save the full runtime - to allow normalization during printout:
*/
if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
attrs[counter].config == PERF_COUNT_TASK_CLOCK)
runtime_nsecs = count[0];
}

Expand All @@ -203,8 +201,8 @@ static void print_counter(int counter)

fprintf(stderr, " %14.6f %-20s",
msecs, event_name(counter));
if (event_id[counter] ==
EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK)) {
if (attrs[counter].type == PERF_TYPE_SOFTWARE &&
attrs[counter].config == PERF_COUNT_TASK_CLOCK) {

fprintf(stderr, " # %11.3f CPU utilization factor",
(double)count[0] / (double)walltime_nsecs);
Expand Down Expand Up @@ -300,8 +298,6 @@ static char events_help_msg[EVENTS_HELP_MAX];
static const struct option options[] = {
OPT_CALLBACK('e', "event", NULL, "event",
events_help_msg, parse_events),
OPT_INTEGER('c', "count", &default_interval,
"event period to sample"),
OPT_BOOLEAN('i', "inherit", &inherit,
"child tasks inherit counters"),
OPT_INTEGER('p', "pid", &target_pid,
Expand All @@ -315,27 +311,19 @@ static const struct option options[] = {

int cmd_stat(int argc, const char **argv, const char *prefix)
{
int counter;

page_size = sysconf(_SC_PAGE_SIZE);

create_events_help(events_help_msg);
memcpy(event_id, default_event_id, sizeof(default_event_id));

memcpy(attrs, default_attrs, sizeof(attrs));

argc = parse_options(argc, argv, options, stat_usage, 0);
if (!argc)
usage_with_options(stat_usage, options);

if (!nr_counters) {
if (!nr_counters)
nr_counters = 8;
}

for (counter = 0; counter < nr_counters; counter++) {
if (event_count[counter])
continue;

event_count[counter] = default_interval;
}
nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
assert(nr_cpus <= MAX_NR_CPUS);
assert(nr_cpus >= 0);
Expand Down
Loading

0 comments on commit 7d036f7

Please sign in to comment.