aboutsummaryrefslogtreecommitdiffstats
path: root/container-llama/src/main/java/ai/vespa/llama/LlamaBundleActivator.java
blob: 846a2008858d81b96abf1af32f2d503df1ef384f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package ai.vespa.llama;

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

import java.util.logging.Logger;

/**
 * @author arnej
 * Finds native libraries when the bundle is activated.
 **/
public class LlamaBundleActivator implements BundleActivator {

    private static final String SKIP_SUFFIX = ".skip";
    private static final String SKIP_VALUE = "true";
    private static final String PATH_PROPNAME = "de.kherud.llama.lib.path";
    private static final Logger log = Logger.getLogger(LlamaBundleActivator.class.getName());

    @Override
    public void start(BundleContext ctx) {
        log.fine("start bundle");
        String skipAll = LlamaBundleActivator.class.getSimpleName() + SKIP_SUFFIX;
        if (SKIP_VALUE.equals(System.getProperty(skipAll))) {
            log.info("skip loading of native libraries");
            return;
        }
        if (checkFilenames(
                    "/dev/nvidia0",
                    "/opt/vespa-deps/lib64/cuda/libllama.so",
                    "/opt/vespa-deps/lib64/cuda/libjllama.so")) {
            System.setProperty(PATH_PROPNAME, "/opt/vespa-deps/lib64/cuda");
        } else if (checkFilenames(
                    "/opt/vespa-deps/lib64/libllama.so",
                    "/opt/vespa-deps/lib64/libjllama.so")) {
            System.setProperty(PATH_PROPNAME, "/opt/vespa-deps/lib64");
        } else {
            throw new IllegalArgumentException("Cannot find shared libraries");
        }
    }

    @Override
    public void stop(BundleContext ctx) {
        log.fine("stop bundle");
    }

    private boolean checkFilenames(String... filenames) {
        for (String fn : filenames) {
            var f = new java.io.File(fn);
            if (! f.canRead()) {
                return false;
            }
        }
        return true;
    }

}