Skip to content
Snippets Groups Projects
builtin-stat.c 33.9 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		list_for_each_entry(counter, &evsel_list->entries, node)
    
    			print_counter_aggr(counter);
    	}
    
    	if (!csv_output) {
    
    			fprintf(output, "\n");
    		fprintf(output, " %17.9f seconds time elapsed",
    
    				avg_stats(&walltime_nsecs_stats)/1e9);
    		if (run_count > 1) {
    
    			fprintf(output, "                                        ");
    
    			print_noise_pct(stddev_stats(&walltime_nsecs_stats),
    					avg_stats(&walltime_nsecs_stats));
    
    		fprintf(output, "\n\n");
    
    static volatile int signr = -1;
    
    
    static void skip_signal(int signo)
    
    	signr = signo;
    }
    
    static void sig_atexit(void)
    {
    
    	if (child_pid != -1)
    		kill(child_pid, SIGTERM);
    
    
    	if (signr == -1)
    		return;
    
    	signal(signr, SIG_DFL);
    	kill(getpid(), signr);
    
    }
    
    static const char * const stat_usage[] = {
    
    	"perf stat [<options>] [<command>]",
    
    static int stat__set_big_num(const struct option *opt __used,
    			     const char *s __used, int unset)
    {
    	big_num_opt = unset ? 0 : 1;
    	return 0;
    }
    
    
    static bool append_file;
    
    
    static const struct option options[] = {
    
    	OPT_CALLBACK('e', "event", &evsel_list, "event",
    
    		     "event selector. use 'perf list' to list available events",
    
    		     parse_events_option),
    
    	OPT_CALLBACK(0, "filter", &evsel_list, "filter",
    		     "event filter", parse_filter),
    
    	OPT_BOOLEAN('i', "no-inherit", &no_inherit,
    		    "child tasks do not inherit counters"),
    
    	OPT_INTEGER('p', "pid", &target_pid,
    
    		    "stat events on existing process id"),
    	OPT_INTEGER('t', "tid", &target_tid,
    		    "stat events on existing thread id"),
    
    	OPT_BOOLEAN('a', "all-cpus", &system_wide,
    
    		    "system-wide collection from all CPUs"),
    
    	OPT_BOOLEAN('g', "group", &group,
    		    "put the counters into a counter group"),
    
    		    "scale/normalize counters"),
    
    		    "be more verbose (show counter open errors, etc)"),
    
    	OPT_INTEGER('r', "repeat", &run_count,
    		    "repeat command and print average + stddev (max: 100)"),
    
    	OPT_BOOLEAN('n', "null", &null_run,
    		    "null run - dont start any counters"),
    
    	OPT_INCR('d', "detailed", &detailed_run,
    
    		    "detailed run - start a lot of events"),
    
    	OPT_BOOLEAN('S', "sync", &sync_run,
    		    "call sync() before starting a run"),
    
    	OPT_CALLBACK_NOOPT('B', "big-num", NULL, NULL, 
    			   "print large numbers with thousands\' separators",
    			   stat__set_big_num),
    
    	OPT_STRING('C', "cpu", &cpu_list, "cpu",
    		    "list of cpus to monitor in system-wide"),
    
    	OPT_BOOLEAN('A', "no-aggr", &no_aggr,
    		    "disable CPU count aggregation"),
    
    	OPT_STRING('x', "field-separator", &csv_sep, "separator",
    		   "print counts with custom separator"),
    
    	OPT_CALLBACK('G', "cgroup", &evsel_list, "name",
    		     "monitor event in cgroup name only",
    		     parse_cgroups),
    
    	OPT_STRING('o', "output", &output_name, "file",
    		    "output file name"),
    	OPT_BOOLEAN(0, "append", &append_file, "append to the output file"),
    
    	OPT_INTEGER(0, "log-fd", &output_fd,
    		    "log output to fd, instead of stderr"),
    
    /*
     * Add default attributes, if there were no attributes specified or
     * if -d/--detailed, -d -d or -d -d -d is used:
     */
    static int add_default_attributes(void)
    {
    	struct perf_evsel *pos;
    	size_t attr_nr = 0;
    	size_t c;
    
    	/* Set attrs if no event is selected and !null_run: */
    	if (null_run)
    		return 0;
    
    	if (!evsel_list->nr_entries) {
    		for (c = 0; c < ARRAY_SIZE(default_attrs); c++) {
    			pos = perf_evsel__new(default_attrs + c, c + attr_nr);
    			if (pos == NULL)
    				return -1;
    			perf_evlist__add(evsel_list, pos);
    		}
    		attr_nr += c;
    	}
    
    	/* Detailed events get appended to the event list: */
    
    	if (detailed_run <  1)
    		return 0;
    
    	/* Append detailed run extra attributes: */
    	for (c = 0; c < ARRAY_SIZE(detailed_attrs); c++) {
    		pos = perf_evsel__new(detailed_attrs + c, c + attr_nr);
    		if (pos == NULL)
    			return -1;
    		perf_evlist__add(evsel_list, pos);
    	}
    	attr_nr += c;
    
    	if (detailed_run < 2)
    		return 0;
    
    	/* Append very detailed run extra attributes: */
    	for (c = 0; c < ARRAY_SIZE(very_detailed_attrs); c++) {
    		pos = perf_evsel__new(very_detailed_attrs + c, c + attr_nr);
    		if (pos == NULL)
    			return -1;
    		perf_evlist__add(evsel_list, pos);
    	}
    
    	if (detailed_run < 3)
    		return 0;
    
    	/* Append very, very detailed run extra attributes: */
    	for (c = 0; c < ARRAY_SIZE(very_very_detailed_attrs); c++) {
    		pos = perf_evsel__new(very_very_detailed_attrs + c, c + attr_nr);
    		if (pos == NULL)
    			return -1;
    		perf_evlist__add(evsel_list, pos);
    	}
    
    
    	return 0;
    }
    
    
    int cmd_stat(int argc, const char **argv, const char *prefix __used)
    
    	struct perf_evsel *pos;
    	int status = -ENOMEM;
    
    	const char *mode;
    
    	evsel_list = perf_evlist__new(NULL, NULL);
    
    	if (evsel_list == NULL)
    		return -ENOMEM;
    
    
    	argc = parse_options(argc, argv, options, stat_usage,
    		PARSE_OPT_STOP_AT_NON_OPTION);
    
    	output = stderr;
    	if (output_name && strcmp(output_name, "-"))
    		output = NULL;
    
    
    	if (output_name && output_fd) {
    		fprintf(stderr, "cannot use both --output and --log-fd\n");
    		usage_with_options(stat_usage, options);
    	}
    
    	if (!output) {
    		struct timespec tm;
    		mode = append_file ? "a" : "w";
    
    		output = fopen(output_name, mode);
    		if (!output) {
    			perror("failed to create output file");
    			exit(-1);
    		}
    		clock_gettime(CLOCK_REALTIME, &tm);
    		fprintf(output, "# started on %s\n", ctime(&tm.tv_sec));
    
    	} else if (output_fd != 2) {
    		mode = append_file ? "a" : "w";
    		output = fdopen(output_fd, mode);
    		if (!output) {
    			perror("Failed opening logfd");
    			return -errno;
    		}
    
    	if (csv_sep) {
    
    		csv_output = true;
    
    		if (!strcmp(csv_sep, "\\t"))
    			csv_sep = "\t";
    	} else
    
    		csv_sep = DEFAULT_SEPARATOR;
    
    	/*
    	 * let the spreadsheet do the pretty-printing
    	 */
    	if (csv_output) {
    
    		/* User explicitly passed -B? */
    
    		if (big_num_opt == 1) {
    			fprintf(stderr, "-B option not supported with -x\n");
    			usage_with_options(stat_usage, options);
    		} else /* Nope, so disable big number formatting */
    			big_num = false;
    	} else if (big_num_opt == 0) /* User passed --no-big-num */
    		big_num = false;
    
    
    	if (!argc && target_pid == -1 && target_tid == -1)
    
    		usage_with_options(stat_usage, options);
    
    	if (run_count <= 0)
    
    		usage_with_options(stat_usage, options);
    
    	/* no_aggr, cgroup are for system-wide only */
    	if ((no_aggr || nr_cgroups) && !system_wide) {
    		fprintf(stderr, "both cgroup and no-aggregation "
    			"modes only available in system-wide mode\n");
    
    
    		usage_with_options(stat_usage, options);
    
    	if (add_default_attributes())
    		goto out;
    
    	if (target_pid != -1)
    		target_tid = target_pid;
    
    
    	evsel_list->threads = thread_map__new(target_pid, target_tid);
    	if (evsel_list->threads == NULL) {
    
    		pr_err("Problems finding threads of monitor\n");
    		usage_with_options(stat_usage, options);
    	}
    
    
    		evsel_list->cpus = cpu_map__new(cpu_list);
    
    		evsel_list->cpus = cpu_map__dummy_new();
    
    	if (evsel_list->cpus == NULL) {
    
    		perror("failed to parse CPUs map");
    
    		usage_with_options(stat_usage, options);
    
    	list_for_each_entry(pos, &evsel_list->entries, node) {
    
    		if (perf_evsel__alloc_stat_priv(pos) < 0 ||
    
    		    perf_evsel__alloc_counts(pos, evsel_list->cpus->nr) < 0 ||
    		    perf_evsel__alloc_fd(pos, evsel_list->cpus->nr, evsel_list->threads->nr) < 0)
    
    Ingo Molnar's avatar
    Ingo Molnar committed
    	/*
    	 * We dont want to block the signals - that would cause
    	 * child tasks to inherit that and Ctrl-C would not work.
    	 * What we want is for Ctrl-C to work in the exec()-ed
    	 * task, but being ignored by perf stat itself:
    	 */
    
    	atexit(sig_atexit);
    
    Ingo Molnar's avatar
    Ingo Molnar committed
    	signal(SIGINT,  skip_signal);
    	signal(SIGALRM, skip_signal);
    	signal(SIGABRT, skip_signal);
    
    
    	status = 0;
    	for (run_idx = 0; run_idx < run_count; run_idx++) {
    		if (run_count != 1 && verbose)
    
    			fprintf(output, "[ perf stat: executing run #%d ... ]\n",
    				run_idx + 1);
    
    	if (status != -1)
    		print_stat(argc, argv);
    
    	list_for_each_entry(pos, &evsel_list->entries, node)
    
    		perf_evsel__free_stat_priv(pos);
    
    	perf_evlist__delete_maps(evsel_list);
    
    out:
    	perf_evlist__delete(evsel_list);