9c6f44ee4c3b264e94da04b39ef6c4be4c4040d2
[cacao.git] / src / vm / jit / oprofile-agent.cpp
1 /* src/vm/jit/oprofile-agent.cpp - oprofile agent implementation
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25 #include "config.h"
26
27 #include "mm/memory.hpp"
28
29 #include "vm/jit/code.hpp"
30 #include "vm/jit/oprofile-agent.hpp"
31
32 #include <string.h>
33
34 /* static fields **************************************************************/
35 op_agent_t OprofileAgent::_handle = 0;
36
37 /**
38  * Initializes the OprofileAgent system.
39  *
40  */
41 /* void OprofileAgent_initialize() */
42 void OprofileAgent::initialize(void)
43 {
44         _handle = op_open_agent();
45         if (!_handle)
46                 vm_abort_errno("unable to open opagent handle:");
47 }
48
49 /**
50  * Reports the given method to oprofile.
51  *
52  * This has to be done once per JIT compilation step for a specific method.
53  *
54  * @param m Method to register.
55  */
56 /* void OprofileAgent_newmethod(methodinfo *m) */
57 void OprofileAgent::newmethod(methodinfo *m)
58 {
59         unsigned int real_length = (unsigned int) m->code->mcodelength -
60                 (unsigned int) (m->code->entrypoint - m->code->mcode);
61
62         char *buf;
63         s4    len;
64
65         len = utf_bytes(m->clazz->name) + strlen(".") +
66                 utf_bytes(m->name) + utf_bytes(m->descriptor) + strlen("0");
67
68         buf = MNEW(char, len);
69
70         utf_copy_classname(buf, m->clazz->name);
71         strcat(buf, ".");
72         utf_cat(buf, m->name);
73         utf_cat(buf, m->descriptor);
74
75         if (_handle)
76                 op_write_native_code(_handle, buf,
77                         (uint64_t) (ptrint) m->code->entrypoint,
78                         (const void *) m->code->entrypoint,
79                         real_length);
80
81         MFREE(buf, char, len);
82 }
83
84 /**
85  * Shuts down the OprofileAgent system.
86  *
87  */
88 /* void OprofileAgent_close() */
89 void OprofileAgent::close()
90 {
91         if (_handle)
92                 op_close_agent(_handle);
93
94         _handle = 0;
95 }
96
97 /*
98  * These are local overrides for various environment variables in Emacs.
99  * Please do not remove this and leave it at the end of the file, where
100  * Emacs will automagically detect them.
101  * ---------------------------------------------------------------------
102  * Local variables:
103  * mode: c++
104  * indent-tabs-mode: t
105  * c-basic-offset: 4
106  * tab-width: 4
107  * End:
108  */