Search in sources :

Example 1 with ThreadsListener

use of com.oracle.truffle.api.instrumentation.ThreadsListener in project graal by oracle.

the class AllocatedBytesInstrument method onCreate.

@Override
protected synchronized void onCreate(Env env) {
    env.getInstrumenter().attachContextsListener(new ContextsListener() {

        @Override
        public void onContextCreated(TruffleContext c) {
        }

        @Override
        public void onLanguageContextCreated(TruffleContext context, LanguageInfo language) {
        }

        @Override
        public void onLanguageContextInitialized(TruffleContext context, LanguageInfo language) {
            try {
                InteropLibrary interopLibrary = InteropLibrary.getUncached();
                Object polyglotBindings = env.getPolyglotBindings();
                if (!interopLibrary.isMemberExisting(polyglotBindings, GET_ALLOCATED_BYTES)) {
                    interopLibrary.writeMember(polyglotBindings, GET_ALLOCATED_BYTES, new GetAllocatedBytesFunction());
                }
            } catch (UnsupportedMessageException | UnknownIdentifierException | UnsupportedTypeException e) {
                throw CompilerDirectives.shouldNotReachHere("Exception during interop.");
            }
        }

        @Override
        public void onLanguageContextFinalized(TruffleContext context, LanguageInfo language) {
        }

        @Override
        public void onLanguageContextDisposed(TruffleContext context, LanguageInfo language) {
        }

        @Override
        public void onContextClosed(TruffleContext context) {
        }
    }, true);
    env.getInstrumenter().attachThreadsListener(new ThreadsListener() {

        @Override
        public void onThreadInitialized(TruffleContext context, Thread thread) {
            synchronized (AllocatedBytesInstrument.this) {
                threads.add(thread);
            }
        }

        @Override
        public void onThreadDisposed(TruffleContext context, Thread thread) {
            synchronized (AllocatedBytesInstrument.this) {
                threads.remove(thread);
            }
        }
    }, true);
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleContext(com.oracle.truffle.api.TruffleContext) ThreadsListener(com.oracle.truffle.api.instrumentation.ThreadsListener) ContextsListener(com.oracle.truffle.api.instrumentation.ContextsListener) TruffleObject(com.oracle.truffle.api.interop.TruffleObject)

Aggregations

TruffleContext (com.oracle.truffle.api.TruffleContext)1 ContextsListener (com.oracle.truffle.api.instrumentation.ContextsListener)1 ThreadsListener (com.oracle.truffle.api.instrumentation.ThreadsListener)1 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)1 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)1 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)1