net/mlx5: Align sriov/eswitch modules with the new load/unload flow.
[linux/fpc-iii.git] / tools / perf / jvmti / jvmti_agent.c
blob55daefff0d54daae31f6b1134a6a499792a310d7
1 /*
2 * jvmti_agent.c: JVMTI agent interface
4 * Adapted from the Oprofile code in opagent.c:
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Copyright 2007 OProfile authors
20 * Jens Wilke
21 * Daniel Hansel
22 * Copyright IBM Corporation 2007
24 #include <sys/types.h>
25 #include <sys/stat.h> /* for mkdir() */
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <limits.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include <sys/mman.h>
36 #include <syscall.h> /* for gettid() */
37 #include <err.h>
39 #include "jvmti_agent.h"
40 #include "../util/jitdump.h"
42 #define JIT_LANG "java"
44 static char jit_path[PATH_MAX];
45 static void *marker_addr;
48 * padding buffer
50 static const char pad_bytes[7];
52 static inline pid_t gettid(void)
54 return (pid_t)syscall(__NR_gettid);
57 static int get_e_machine(struct jitheader *hdr)
59 ssize_t sret;
60 char id[16];
61 int fd, ret = -1;
62 struct {
63 uint16_t e_type;
64 uint16_t e_machine;
65 } info;
67 fd = open("/proc/self/exe", O_RDONLY);
68 if (fd == -1)
69 return -1;
71 sret = read(fd, id, sizeof(id));
72 if (sret != sizeof(id))
73 goto error;
75 /* check ELF signature */
76 if (id[0] != 0x7f || id[1] != 'E' || id[2] != 'L' || id[3] != 'F')
77 goto error;
79 sret = read(fd, &info, sizeof(info));
80 if (sret != sizeof(info))
81 goto error;
83 hdr->elf_mach = info.e_machine;
84 ret = 0;
85 error:
86 close(fd);
87 return ret;
90 static int use_arch_timestamp;
92 static inline uint64_t
93 get_arch_timestamp(void)
95 #if defined(__i386__) || defined(__x86_64__)
96 unsigned int low, high;
98 asm volatile("rdtsc" : "=a" (low), "=d" (high));
100 return low | ((uint64_t)high) << 32;
101 #else
102 return 0;
103 #endif
106 #define NSEC_PER_SEC 1000000000
107 static int perf_clk_id = CLOCK_MONOTONIC;
109 static inline uint64_t
110 timespec_to_ns(const struct timespec *ts)
112 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
115 static inline uint64_t
116 perf_get_timestamp(void)
118 struct timespec ts;
119 int ret;
121 if (use_arch_timestamp)
122 return get_arch_timestamp();
124 ret = clock_gettime(perf_clk_id, &ts);
125 if (ret)
126 return 0;
128 return timespec_to_ns(&ts);
131 static int
132 debug_cache_init(void)
134 char str[32];
135 char *base, *p;
136 struct tm tm;
137 time_t t;
138 int ret;
140 time(&t);
141 localtime_r(&t, &tm);
143 base = getenv("JITDUMPDIR");
144 if (!base)
145 base = getenv("HOME");
146 if (!base)
147 base = ".";
149 strftime(str, sizeof(str), JIT_LANG"-jit-%Y%m%d", &tm);
151 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/", base);
153 ret = mkdir(jit_path, 0755);
154 if (ret == -1) {
155 if (errno != EEXIST) {
156 warn("jvmti: cannot create jit cache dir %s", jit_path);
157 return -1;
161 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit", base);
162 ret = mkdir(jit_path, 0755);
163 if (ret == -1) {
164 if (errno != EEXIST) {
165 warn("cannot create jit cache dir %s", jit_path);
166 return -1;
170 snprintf(jit_path, PATH_MAX - 1, "%s/.debug/jit/%s.XXXXXXXX", base, str);
172 p = mkdtemp(jit_path);
173 if (p != jit_path) {
174 warn("cannot create jit cache dir %s", jit_path);
175 return -1;
178 return 0;
181 static int
182 perf_open_marker_file(int fd)
184 long pgsz;
186 pgsz = sysconf(_SC_PAGESIZE);
187 if (pgsz == -1)
188 return -1;
191 * we mmap the jitdump to create an MMAP RECORD in perf.data file.
192 * The mmap is captured either live (perf record running when we mmap)
193 * or in deferred mode, via /proc/PID/maps
194 * the MMAP record is used as a marker of a jitdump file for more meta
195 * data info about the jitted code. Perf report/annotate detect this
196 * special filename and process the jitdump file.
198 * mapping must be PROT_EXEC to ensure it is captured by perf record
199 * even when not using -d option
201 marker_addr = mmap(NULL, pgsz, PROT_READ|PROT_EXEC, MAP_PRIVATE, fd, 0);
202 return (marker_addr == MAP_FAILED) ? -1 : 0;
205 static void
206 perf_close_marker_file(void)
208 long pgsz;
210 if (!marker_addr)
211 return;
213 pgsz = sysconf(_SC_PAGESIZE);
214 if (pgsz == -1)
215 return;
217 munmap(marker_addr, pgsz);
220 static void
221 init_arch_timestamp(void)
223 char *str = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
225 if (!str || !*str || !strcmp(str, "0"))
226 return;
228 use_arch_timestamp = 1;
231 void *jvmti_open(void)
233 int pad_cnt;
234 char dump_path[PATH_MAX];
235 struct jitheader header;
236 int fd;
237 FILE *fp;
239 init_arch_timestamp();
242 * check if clockid is supported
244 if (!perf_get_timestamp()) {
245 if (use_arch_timestamp)
246 warnx("jvmti: arch timestamp not supported");
247 else
248 warnx("jvmti: kernel does not support %d clock id", perf_clk_id);
251 memset(&header, 0, sizeof(header));
253 debug_cache_init();
256 * jitdump file name
258 snprintf(dump_path, PATH_MAX, "%s/jit-%i.dump", jit_path, getpid());
260 fd = open(dump_path, O_CREAT|O_TRUNC|O_RDWR, 0666);
261 if (fd == -1)
262 return NULL;
265 * create perf.data maker for the jitdump file
267 if (perf_open_marker_file(fd)) {
268 warnx("jvmti: failed to create marker file");
269 return NULL;
272 fp = fdopen(fd, "w+");
273 if (!fp) {
274 warn("jvmti: cannot create %s", dump_path);
275 close(fd);
276 goto error;
279 warnx("jvmti: jitdump in %s", dump_path);
281 if (get_e_machine(&header)) {
282 warn("get_e_machine failed\n");
283 goto error;
286 header.magic = JITHEADER_MAGIC;
287 header.version = JITHEADER_VERSION;
288 header.total_size = sizeof(header);
289 header.pid = getpid();
291 /* calculate amount of padding '\0' */
292 pad_cnt = PADDING_8ALIGNED(header.total_size);
293 header.total_size += pad_cnt;
295 header.timestamp = perf_get_timestamp();
297 if (use_arch_timestamp)
298 header.flags |= JITDUMP_FLAGS_ARCH_TIMESTAMP;
300 if (!fwrite(&header, sizeof(header), 1, fp)) {
301 warn("jvmti: cannot write dumpfile header");
302 goto error;
305 /* write padding '\0' if necessary */
306 if (pad_cnt && !fwrite(pad_bytes, pad_cnt, 1, fp)) {
307 warn("jvmti: cannot write dumpfile header padding");
308 goto error;
311 return fp;
312 error:
313 fclose(fp);
314 return NULL;
318 jvmti_close(void *agent)
320 struct jr_code_close rec;
321 FILE *fp = agent;
323 if (!fp) {
324 warnx("jvmti: incalid fd in close_agent");
325 return -1;
328 rec.p.id = JIT_CODE_CLOSE;
329 rec.p.total_size = sizeof(rec);
331 rec.p.timestamp = perf_get_timestamp();
333 if (!fwrite(&rec, sizeof(rec), 1, fp))
334 return -1;
336 fclose(fp);
338 fp = NULL;
340 perf_close_marker_file();
342 return 0;
346 jvmti_write_code(void *agent, char const *sym,
347 uint64_t vma, void const *code, unsigned int const size)
349 static int code_generation = 1;
350 struct jr_code_load rec;
351 size_t sym_len;
352 size_t padding_count;
353 FILE *fp = agent;
354 int ret = -1;
356 /* don't care about 0 length function, no samples */
357 if (size == 0)
358 return 0;
360 if (!fp) {
361 warnx("jvmti: invalid fd in write_native_code");
362 return -1;
365 sym_len = strlen(sym) + 1;
367 rec.p.id = JIT_CODE_LOAD;
368 rec.p.total_size = sizeof(rec) + sym_len;
369 padding_count = PADDING_8ALIGNED(rec.p.total_size);
370 rec.p. total_size += padding_count;
371 rec.p.timestamp = perf_get_timestamp();
373 rec.code_size = size;
374 rec.vma = vma;
375 rec.code_addr = vma;
376 rec.pid = getpid();
377 rec.tid = gettid();
379 if (code)
380 rec.p.total_size += size;
383 * If JVM is multi-threaded, nultiple concurrent calls to agent
384 * may be possible, so protect file writes
386 flockfile(fp);
389 * get code index inside lock to avoid race condition
391 rec.code_index = code_generation++;
393 ret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
394 fwrite_unlocked(sym, sym_len, 1, fp);
396 if (padding_count)
397 fwrite_unlocked(pad_bytes, padding_count, 1, fp);
399 if (code)
400 fwrite_unlocked(code, size, 1, fp);
402 funlockfile(fp);
404 ret = 0;
406 return ret;
410 jvmti_write_debug_info(void *agent, uint64_t code, const char *file,
411 jvmti_line_info_t *li, int nr_lines)
413 struct jr_code_debug_info rec;
414 size_t sret, len, size, flen;
415 size_t padding_count;
416 uint64_t addr;
417 const char *fn = file;
418 FILE *fp = agent;
419 int i;
422 * no entry to write
424 if (!nr_lines)
425 return 0;
427 if (!fp) {
428 warnx("jvmti: invalid fd in write_debug_info");
429 return -1;
432 flen = strlen(file) + 1;
434 rec.p.id = JIT_CODE_DEBUG_INFO;
435 size = sizeof(rec);
436 rec.p.timestamp = perf_get_timestamp();
437 rec.code_addr = (uint64_t)(uintptr_t)code;
438 rec.nr_entry = nr_lines;
441 * on disk source line info layout:
442 * uint64_t : addr
443 * int : line number
444 * int : column discriminator
445 * file[] : source file name
446 * padding : pad to multiple of 8 bytes
448 size += nr_lines * sizeof(struct debug_entry);
449 size += flen * nr_lines;
451 * pad to 8 bytes
453 padding_count = PADDING_8ALIGNED(size);
455 rec.p.total_size = size + padding_count;
458 * If JVM is multi-threaded, nultiple concurrent calls to agent
459 * may be possible, so protect file writes
461 flockfile(fp);
463 sret = fwrite_unlocked(&rec, sizeof(rec), 1, fp);
464 if (sret != 1)
465 goto error;
467 for (i = 0; i < nr_lines; i++) {
469 addr = (uint64_t)li[i].pc;
470 len = sizeof(addr);
471 sret = fwrite_unlocked(&addr, len, 1, fp);
472 if (sret != 1)
473 goto error;
475 len = sizeof(li[0].line_number);
476 sret = fwrite_unlocked(&li[i].line_number, len, 1, fp);
477 if (sret != 1)
478 goto error;
480 len = sizeof(li[0].discrim);
481 sret = fwrite_unlocked(&li[i].discrim, len, 1, fp);
482 if (sret != 1)
483 goto error;
485 sret = fwrite_unlocked(fn, flen, 1, fp);
486 if (sret != 1)
487 goto error;
489 if (padding_count) {
490 sret = fwrite_unlocked(pad_bytes, padding_count, 1, fp);
491 if (sret != 1)
492 goto error;
495 funlockfile(fp);
496 return 0;
497 error:
498 funlockfile(fp);
499 return -1;