Search in sources :

Example 1 with LibGraalScope

use of org.graalvm.libgraal.LibGraalScope in project graal by oracle.

the class LibGraalMBean method setAttributes.

/**
 * Sets the values of several attributes of the Dynamic MBean by delegating to
 * {@link DynamicMBean} instance in the libgraal heap.
 *
 * @param attributes a list of attributes: The identification of the attributes to be set and
 *            the values they are to be set to.
 */
@Override
public AttributeList setAttributes(AttributeList attributes) {
    try (LibGraalScope scope = new LibGraalScope(isolate)) {
        Map<String, Object> map = new LinkedHashMap<>();
        for (Object item : attributes) {
            Attribute attribute = (Attribute) item;
            map.put(attribute.getName(), attribute.getValue());
        }
        byte[] rawData = OptionsEncoder.encode(map);
        rawData = JMXToLibGraalCalls.setAttributes(scope.getIsolateThreadAddress(), handle, rawData);
        return rawToAttributeList(rawData);
    }
}
Also used : Attribute(javax.management.Attribute) LibGraalScope(org.graalvm.libgraal.LibGraalScope) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with LibGraalScope

use of org.graalvm.libgraal.LibGraalScope in project graal by oracle.

the class LibGraalMBean method getMBeanInfo.

/**
 * Provides the attributes and actions of the Dynamic MBean by delegating to
 * {@link DynamicMBean} instance in the libgraal heap.
 */
@Override
public MBeanInfo getMBeanInfo() {
    try (LibGraalScope scope = new LibGraalScope(isolate)) {
        byte[] rawData = JMXToLibGraalCalls.getMBeanInfo(scope.getIsolateThreadAddress(), handle);
        Map<String, Object> map = OptionsEncoder.decode(rawData);
        String className = null;
        String description = null;
        List<MBeanAttributeInfo> attributes = new ArrayList<>();
        List<MBeanOperationInfo> operations = new ArrayList<>();
        for (PushBackIterator<Map.Entry<String, Object>> it = new PushBackIterator<>(map.entrySet().iterator()); it.hasNext(); ) {
            Map.Entry<String, ?> entry = it.next();
            String key = entry.getKey();
            if (key.equals("bean.class")) {
                className = (String) entry.getValue();
            } else if (key.equals("bean.description")) {
                description = (String) entry.getValue();
            } else if (key.startsWith("attr.")) {
                String attrName = (String) entry.getValue();
                if (!key.equals("attr." + attrName + ".name")) {
                    throw new IllegalStateException("Invalid order of attribute properties");
                }
                MBeanAttributeInfo attr = createAttributeInfo(attrName, it);
                attributes.add(attr);
            } else if (key.startsWith("op.")) {
                int opId = (Integer) entry.getValue();
                if (!key.equals("op." + opId + ".id")) {
                    throw new IllegalStateException("Invalid order of operation properties");
                }
                MBeanOperationInfo op = createOperationInfo(opId, it);
                operations.add(op);
            }
        }
        Objects.requireNonNull(className, "ClassName must be non null.");
        Objects.requireNonNull(description, "Description must be non null.");
        return new MBeanInfo(className, description, attributes.toArray(new MBeanAttributeInfo[attributes.size()]), null, operations.toArray(new MBeanOperationInfo[operations.size()]), null);
    }
}
Also used : MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) ArrayList(java.util.ArrayList) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) LibGraalScope(org.graalvm.libgraal.LibGraalScope) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with LibGraalScope

use of org.graalvm.libgraal.LibGraalScope in project graal by oracle.

the class Factory method process.

/**
 * Creates {@link LibGraalMBean}s for pending libgraal {@link DynamicMBean}s and registers them
 * {@link MBeanServer}.
 *
 * @return {@code true}
 * @throws SecurityException can be thrown by {@link MBeanServer}
 * @throws UnsatisfiedLinkError can be thrown by {@link MBeanServer}
 * @throws NoClassDefFoundError can be thrown by {@link MBeanServer}
 * @throws UnsupportedOperationException can be thrown by {@link MBeanServer}
 */
private boolean process() {
    for (Iterator<Long> iter = pendingIsolates.iterator(); iter.hasNext(); ) {
        long isolate = iter.next();
        iter.remove();
        try (LibGraalScope scope = new LibGraalScope(isolate)) {
            long isolateThread = scope.getIsolateThreadAddress();
            long[] handles = JMXToLibGraalCalls.pollRegistrations(isolateThread);
            if (handles.length > 0) {
                List<ObjectName> objectNames = new ArrayList<>(handles.length);
                try {
                    for (long handle : handles) {
                        LibGraalMBean bean = new LibGraalMBean(isolate, handle);
                        String name = JMXToLibGraalCalls.getObjectName(isolateThread, handle);
                        try {
                            ObjectName objectName = new ObjectName(name);
                            objectNames.add(objectName);
                            if (isLibGraalMBean(objectName)) {
                                if (aggregatedMemoryPoolBean == null) {
                                    aggregatedMemoryPoolBean = new AggregatedMemoryPoolBean(bean, objectName);
                                    platformMBeanServer.registerMBean(aggregatedMemoryPoolBean, aggregatedMemoryPoolBean.getObjectName());
                                } else {
                                    aggregatedMemoryPoolBean.addDelegate(bean, objectName);
                                }
                            }
                            platformMBeanServer.registerMBean(bean, objectName);
                        } catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
                            e.printStackTrace(TTY.out);
                        }
                    }
                } finally {
                    if (mbeansForActiveIsolate == null) {
                        mbeansForActiveIsolate = new HashMap<>();
                    }
                    mbeansForActiveIsolate.put(isolate, objectNames.toArray(new ObjectName[objectNames.size()]));
                }
            }
        }
    }
    return true;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ArrayList(java.util.ArrayList) ObjectName(javax.management.ObjectName) LibGraalScope(org.graalvm.libgraal.LibGraalScope) MBeanRegistrationException(javax.management.MBeanRegistrationException)

Example 4 with LibGraalScope

use of org.graalvm.libgraal.LibGraalScope in project graal by oracle.

the class LibGraalHotSpotTruffleCompiler method installTruffleReservedOopMethod.

@Override
@SuppressWarnings("try")
public void installTruffleReservedOopMethod(ResolvedJavaMethod method) {
    try (LibGraalScope scope = new LibGraalScope(LibGraalScope.DetachAction.DETACH_RUNTIME_AND_RELEASE)) {
        Map<String, Object> options = previousOptions;
        assert options != null : "truffle compiler was never initialized";
        TruffleToLibGraalCalls.installTruffleReservedOopMethod(getIsolateThread(), handle(options, null), LibGraal.translate(method));
    }
}
Also used : LibGraalScope(org.graalvm.libgraal.LibGraalScope) LibGraalObject(org.graalvm.libgraal.LibGraalObject)

Example 5 with LibGraalScope

use of org.graalvm.libgraal.LibGraalScope in project graal by oracle.

the class LibGraalMBean method invoke.

/**
 * Invokes an action on the Dynamic MBean by delegating to {@link DynamicMBean} instance in the
 * libgraal heap.
 *
 * @param actionName the name of the action to be invoked
 * @param params an array containing the parameters to be set when the action is invoked
 * @param signature an array containing the signature of the action. The class objects will be
 *            loaded through the same class loader as the one used for loading the MBean on
 *            which the action is invoked
 *
 * @return The object returned by the action, which represents the result of invoking the action
 *         on the MBean specified.
 */
@Override
public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
    try (LibGraalScope scope = new LibGraalScope(isolate)) {
        Map<String, Object> paramsMap = new LinkedHashMap<>();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                paramsMap.put(Integer.toString(i), params[i]);
            }
        }
        byte[] rawData = OptionsEncoder.encode(paramsMap);
        rawData = JMXToLibGraalCalls.invoke(scope.getIsolateThreadAddress(), handle, actionName, rawData, signature);
        if (rawData == null) {
            throw new MBeanException(null);
        }
        AttributeList attributesList = rawToAttributeList(rawData);
        return attributesList.isEmpty() ? null : ((Attribute) attributesList.get(0)).getValue();
    }
}
Also used : AttributeList(javax.management.AttributeList) LibGraalScope(org.graalvm.libgraal.LibGraalScope) MBeanException(javax.management.MBeanException) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LibGraalScope (org.graalvm.libgraal.LibGraalScope)8 LinkedHashMap (java.util.LinkedHashMap)3 ArrayList (java.util.ArrayList)2 LibGraalObject (org.graalvm.libgraal.LibGraalObject)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Attribute (javax.management.Attribute)1 AttributeList (javax.management.AttributeList)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 MBeanAttributeInfo (javax.management.MBeanAttributeInfo)1 MBeanException (javax.management.MBeanException)1 MBeanInfo (javax.management.MBeanInfo)1 MBeanOperationInfo (javax.management.MBeanOperationInfo)1 MBeanRegistrationException (javax.management.MBeanRegistrationException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 ObjectName (javax.management.ObjectName)1