summaryrefslogtreecommitdiff
path: root/perf.c
blob: 1ce35a295cd7d1823df97d94dffbdb4e0a4934ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright 2009, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * Portions borrowed from the "perf" tool (C) Ingo Molnar and others
 *
 * This program file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 */

#define _GNU_SOURCE        /* or _BSD_SOURCE or _SVID_SOURCE */
#include <unistd.h>
#include <sys/syscall.h>   /* For SYS_xxx definitions */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>

#include <fcntl.h>

#include "perf_event.h"

#include "powertop.h"


/* some people have stale headers */
#ifndef __NR_perf_event_open
#ifdef __i386__
#define __NR_perf_event_open	336
#endif
#if __x86_64__
#define __NR_perf_event_open	298
#endif
#endif

static int perf_fd = -1;
static void * perf_mmap;
static void * data_mmap;

static struct perf_event_mmap_page *pc;

static inline int
sys_perf_event_open(struct perf_event_attr *attr,
                      pid_t pid, int cpu, int group_fd,
                      unsigned long flags)
{
	attr->size = sizeof(*attr);
	return syscall(__NR_perf_event_open, attr, pid, cpu,
			group_fd, flags);
}

static int this_trace = 0;

static int get_trace_type(void)
{
	FILE *file;
	char line[4096];
	file = fopen("/sys/kernel/debug/tracing/events/vfs/dirty_inode/id", "r");
	if (!file)
		return 0;

	if (fgets(line, 4095, file) == NULL)
		return 0;

	this_trace = strtoull(line, NULL, 10);
	fclose(file);
	return this_trace;
}

static void create_perf_event(void)
{
	struct perf_event_attr attr;
	int ret;

	struct {
		__u64 count;
		__u64 time_enabled;
		__u64 time_running;
		__u64 id;
	} read_data;

	memset(&attr, 0, sizeof(attr));

	attr.read_format	= PERF_FORMAT_TOTAL_TIME_ENABLED |
				  PERF_FORMAT_TOTAL_TIME_RUNNING |
				  PERF_FORMAT_ID;

	attr.sample_freq	= 0;
	attr.sample_period	= 1;
	attr.sample_type	|= PERF_SAMPLE_RAW;

	attr.mmap		= 1;
	attr.comm		= 1;
	attr.inherit		= 0;
	attr.disabled		= 1;

	attr.type		= PERF_TYPE_TRACEPOINT;
	attr.config		= get_trace_type();

	if (attr.config <= 0)
		return;

	perf_fd = sys_perf_event_open(&attr, -1, 0, -1, 0);

	if (perf_fd < 0) {
		fprintf(stderr, "Perf syscall failed: %i / %i (%s)\n", perf_fd, errno, strerror(errno));
		return;
	}
	if (read(perf_fd, &read_data, sizeof(read_data)) == -1) {
		perror("Unable to read perf file descriptor\n");
		exit(-1);
	}

	fcntl(perf_fd, F_SETFL, O_NONBLOCK);

	perf_mmap = mmap(NULL, (128+1)*getpagesize(),
				PROT_READ | PROT_WRITE, MAP_SHARED, perf_fd, 0);
	if (perf_mmap == MAP_FAILED) {
		fprintf(stderr, "failed to mmap with %d (%s)\n", errno, strerror(errno));
		return;
	}

	ret = ioctl(perf_fd, PERF_EVENT_IOC_ENABLE);

	if (ret < 0)
		fprintf(stderr, "failed to enable perf \n");

	pc = perf_mmap;
	data_mmap = perf_mmap + getpagesize();


}


void start_data_dirty_capture(void)
{
	create_perf_event();
}

void end_data_dirty_capture(void)
{
	if (perf_fd >= 0)
		close(perf_fd);
	perf_fd = -1;
}

struct trace_entry {
	__u32			size;
	unsigned short		type;
	unsigned char		flags;
	unsigned char		preempt_count;
	int			pid;
	int			tgid;
};

#define TASK_COMM_LEN 16
struct dirty_inode {
	char comm[TASK_COMM_LEN];
	int  pid;
	char dev[16];
	char file[32];
};


struct sample_event{
	struct perf_event_header        header;
	struct trace_entry		trace;
	struct dirty_inode		inode;
};


static void parse_event(void *ptr, int verbose)
{
	char line[8192];
	char pid[14];
	char realcomm[17];
	int suggested = 0;
	struct sample_event *event = ptr;

	memset(line, 0, sizeof(line));
	memset(realcomm, 0, 17);

	if (event->trace.type != this_trace)
		return;

	if (event->trace.size < sizeof(struct dirty_inode))
		return;

	if (event->inode.pid == 0)
		return;

	memcpy(realcomm, event->inode.comm, 16);

	if (strcmp(realcomm, "powertop") == 0)
		return;
	/*
	 * btrfs kernel threads are internal and only
	 * do IO on behalf of others that also got recorded
	 */

	if (strcmp(realcomm, "btrfs-") == 0)
		return;
	/*
	 * don't record "IO" to tmpfs or /proc
	 */
	if (strcmp(event->inode.dev, "tmpfs") == 0)
		return;
	if (strcmp(event->inode.dev, "proc") == 0)
		return;
	if (strcmp(event->inode.dev, "pipefs") == 0)
		return;
	if (strcmp(event->inode.dev, "sysfs") == 0)
		return;
	if (strcmp(event->inode.dev, "anon_inodefs") == 0)
		return;

	sprintf(pid, "%i", event->inode.pid);
	sprintf(line, "%s", realcomm);
	push_line_pid(line, 0, 1, pid);

	if (!suggested && strcmp(event->inode.file, "?")) {
		suggested = 1;
		sprintf(line,_("The program '%s' is writing to file '%s' on /dev/%s.\nThis prevents the disk from going to powersave mode."),
			realcomm, event->inode.file, event->inode.dev);
		add_suggestion(line, 30, 0, NULL, NULL);
	}
	if (verbose)
		printf(_("The application '%s' is writing to file '%s' on /dev/%s\n"),
			realcomm, event->inode.file, event->inode.dev);

}

void parse_data_dirty_buffer(void)
{
	struct perf_event_header *header;
	int i = 0;

	if (perf_fd < 0)
		return;

	if (dump)
		printf(_("Disk accesses:\n"));

	while (pc->data_tail != pc->data_head && i++ < 5000) {
		while (pc->data_tail >= 128U * getpagesize())
			pc->data_tail -= 128 * getpagesize();

		header = data_mmap + pc->data_tail;

		if (header->size == 0)
			break;

		pc->data_tail += header->size;

		while (pc->data_tail >= 128U * getpagesize())
			pc->data_tail -= 128 * getpagesize();

		if (header->type == PERF_RECORD_SAMPLE)
			parse_event(header, dump);
	}
	pc->data_tail = pc->data_head;
}