tools/perf/util/values.c

Source file repositories/reference/linux-study-clean/tools/perf/util/values.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/values.c
Extension
.c
Size
7407 bytes
Lines
290
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (!value) {
			pr_debug("failed to enlarge read_values ->values array");
			goto out_free_counters;
		}

		for (int j = values->counters_max; j < counters_max; j++)
			value[j] = 0;

		values->value[i] = value;
	}

	values->counters_max = counters_max;
	values->counters = new_counters;

	return 0;
out_free_counters:
	free(new_counters);
out_enomem:
	return -ENOMEM;
}

static int perf_read_values__findnew_counter(struct perf_read_values *values,
					     struct evsel *evsel)
{
	int i;

	for (i = 0; i < values->num_counters; i++)
		if (values->counters[i] == evsel)
			return i;

	if (values->num_counters == values->counters_max) {
		int err = perf_read_values__enlarge_counters(values);

		if (err)
			return err;
	}

	i = values->num_counters++;
	values->counters[i] = evsel;

	return i;
}

int perf_read_values_add_value(struct perf_read_values *values,
				u32 pid, u32 tid,
				struct evsel *evsel, u64 value)
{
	int tindex, cindex;

	tindex = perf_read_values__findnew_thread(values, pid, tid);
	if (tindex < 0)
		return tindex;
	cindex = perf_read_values__findnew_counter(values, evsel);
	if (cindex < 0)
		return cindex;

	values->value[tindex][cindex] += value;
	return 0;
}

static void perf_read_values__display_pretty(FILE *fp,
					     struct perf_read_values *values)
{
	int i, j;
	int pidwidth, tidwidth;
	int *counterwidth;

	counterwidth = malloc(values->num_counters * sizeof(*counterwidth));
	if (!counterwidth) {
		fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n");
		return;
	}
	tidwidth = 3;
	pidwidth = 3;
	for (j = 0; j < values->num_counters; j++)
		counterwidth[j] = strlen(evsel__name(values->counters[j]));
	for (i = 0; i < values->threads; i++) {
		int width;

		width = snprintf(NULL, 0, "%d", values->pid[i]);
		if (width > pidwidth)
			pidwidth = width;
		width = snprintf(NULL, 0, "%d", values->tid[i]);
		if (width > tidwidth)
			tidwidth = width;
		for (j = 0; j < values->num_counters; j++) {
			width = snprintf(NULL, 0, "%" PRIu64, values->value[i][j]);
			if (width > counterwidth[j])
				counterwidth[j] = width;
		}

Annotation

Implementation Notes