aboutsummaryrefslogtreecommitdiffstats
path: root/container-onnxruntime/src/main/java/ai/vespa/onnxruntime/OnnxBundleActivator.java
blob: 59a0b4fd1ec7a8afd49e57e05bebe74a4b28b0e5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package ai.vespa.onnxruntime;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import java.util.logging.Logger;

/**
 * @author arnej
 * Loads native libraries when the bundle is activated.
 * Use system properties to ensure onnxruntime won't try
 * to load them itself.
 **/
public class OnnxBundleActivator implements BundleActivator {

    private static final String ONNX_PREFIX = "onnxruntime.native.";
    private static final String PATH_SUFFIX = "path";
    private static final String SKIP_SUFFIX = ".skip";
    private static final String SKIP_VALUE = "true";
    private static final String[] LIBRARY_NAMES = { "onnxruntime", "onnxruntime4j_jni" };
    private static final Logger log = Logger.getLogger(OnnxBundleActivator.class.getName());

    @Override
    public void start(BundleContext ctx) {
        String skipAll = OnnxBundleActivator.class.getSimpleName() + SKIP_SUFFIX;
        if (SKIP_VALUE.equals(System.getProperty(skipAll))) {
            log.info("skip loading of native libraries");
            return;
        }
        System.setProperty(ONNX_PREFIX + PATH_SUFFIX, "/opt/vespa-deps/lib64");
        for (String libName : LIBRARY_NAMES) {
            String skipProp = ONNX_PREFIX + libName + SKIP_SUFFIX;
            if (SKIP_VALUE.equals(System.getProperty(skipProp))) {
                log.fine("already loaded native library "+libName+", skipping");
            } else {
                log.fine("loading native library: "+libName);
                try {
                    System.loadLibrary(libName);
                    // this property also signals onnxruntime to skip loading:
                    System.setProperty(skipProp, SKIP_VALUE);
                    log.fine("loaded native library OK: "+libName);
                } catch (Exception|UnsatisfiedLinkError e) {
                    log.info("Could not load native library '"+libName+"' because: "+e.getMessage());
                }
            }
        }
    }

    @Override
    public void stop(BundleContext ctx) {
        // not sure how to test that loading and unloading multiple times actually works,
        // but this should in theory do the necessary thing.
        for (String libName : LIBRARY_NAMES) {
            String skipProp = ONNX_PREFIX + libName + SKIP_SUFFIX;
            if (SKIP_VALUE.equals(System.getProperty(skipProp))) {
                log.fine("will unload native library: "+libName);
            }
            System.clearProperty(skipProp);
        }
    }
}