Merged trunk and subtype.
[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
26 #include "config.h"
27
28 #include "mm/memory.h"
29
30 #include "vm/jit/code.hpp"
31 #include "vm/jit/oprofile-agent.hpp"
32
33 #include <string.h>
34
35 /* static fields **************************************************************/
36 op_agent_t OprofileAgent::_handle = 0;
37
38 /**
39  * Initializes the OprofileAgent system.
40  *
41  */
42 /* void OprofileAgent_initialize() */
43 void OprofileAgent::initialize(void)
44 {
45         _handle = op_open_agent();
46         if (!_handle)
47                 vm_abort_errno("unable to open opagent handle:");
48 }
49
50 /**
51  * Reports the given method to oprofile.
52  *
53  * This has to be done once per JIT compilation step for a specific method.
54  *
55  * @param m Method to register.
56  */
57 /* void OprofileAgent_newmethod(methodinfo *m) */
58 void OprofileAgent::newmethod(methodinfo *m)
59 {
60         unsigned int real_length = (unsigned int) m->code->mcodelength -
61                 (unsigned int) (m->code->entrypoint - m->code->mcode);
62
63         char *buf;
64         s4    len;
65
66         len = utf_bytes(m->clazz->name) + strlen(".") +
67                 utf_bytes(m->name) + utf_bytes(m->descriptor) + strlen("0");
68
69         buf = MNEW(char, len);
70
71         utf_copy_classname(buf, m->clazz->name);
72         strcat(buf, ".");
73         utf_cat(buf, m->name);
74         utf_cat(buf, m->descriptor);
75
76         if (_handle)
77                 op_write_native_code(_handle, buf,
78                         (uint64_t) (ptrint) m->code->entrypoint,
79                         (const void *) m->code->entrypoint,
80                         real_length);
81
82         MFREE(buf, char, len);
83 }
84
85 /**
86  * Shuts down the OprofileAgent system.
87  *
88  */
89 /* void OprofileAgent_close() */
90 void OprofileAgent::close()
91 {
92         if (_handle)
93                 op_close_agent(_handle);
94
95         _handle = 0;
96 }
97 /* Legacy C interface *********************************************************/
98
99 extern "C" {
100
101 void OprofileAgent_initialize() { OprofileAgent::initialize(); }
102 void OprofileAgent_newmethod(methodinfo *m) { OprofileAgent::newmethod(m); }
103 void OprofileAgent_close() { OprofileAgent::close(); }
104
105 }
106
107 /*
108  * These are local overrides for various environment variables in Emacs.
109  * Please do not remove this and leave it at the end of the file, where
110  * Emacs will automagically detect them.
111  * ---------------------------------------------------------------------
112  * Local variables:
113  * mode: c++
114  * indent-tabs-mode: t
115  * c-basic-offset: 4
116  * tab-width: 4
117  * End:
118  */