2024-02-16 01:15:09 +00:00
|
|
|
package gpu
|
|
|
|
|
|
|
|
import (
|
2024-03-13 18:43:45 +00:00
|
|
|
"errors"
|
2024-02-16 01:15:09 +00:00
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2024-03-13 18:43:45 +00:00
|
|
|
"strconv"
|
2024-02-16 01:15:09 +00:00
|
|
|
"strings"
|
2024-03-08 17:45:55 +00:00
|
|
|
"sync"
|
2024-03-13 18:43:45 +00:00
|
|
|
"syscall"
|
2024-03-08 17:45:55 +00:00
|
|
|
)
|
2024-02-16 01:15:09 +00:00
|
|
|
|
2024-03-08 17:45:55 +00:00
|
|
|
var (
|
|
|
|
lock sync.Mutex
|
|
|
|
payloadsDir = ""
|
2024-02-16 01:15:09 +00:00
|
|
|
)
|
|
|
|
|
2024-03-08 17:45:55 +00:00
|
|
|
func PayloadsDir() (string, error) {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
if payloadsDir == "" {
|
2024-03-13 18:43:45 +00:00
|
|
|
cleanupTmpDirs()
|
2024-03-08 17:45:55 +00:00
|
|
|
tmpDir, err := os.MkdirTemp("", "ollama")
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to generate tmp dir: %w", err)
|
|
|
|
}
|
2024-03-13 18:43:45 +00:00
|
|
|
|
|
|
|
// Track our pid so we can clean up orphaned tmpdirs
|
|
|
|
pidFilePath := filepath.Join(tmpDir, "ollama.pid")
|
|
|
|
pidFile, err := os.OpenFile(pidFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if _, err := pidFile.Write([]byte(fmt.Sprint(os.Getpid()))); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-03-11 15:45:57 +00:00
|
|
|
// We create a distinct subdirectory for payloads within the tmpdir
|
|
|
|
// This will typically look like /tmp/ollama3208993108/runners on linux
|
|
|
|
payloadsDir = filepath.Join(tmpDir, "runners")
|
2024-02-16 01:15:09 +00:00
|
|
|
}
|
2024-03-08 17:45:55 +00:00
|
|
|
return payloadsDir, nil
|
|
|
|
}
|
|
|
|
|
2024-03-13 18:43:45 +00:00
|
|
|
// Best effort to clean up prior tmpdirs
|
|
|
|
func cleanupTmpDirs() {
|
|
|
|
dirs, err := filepath.Glob(filepath.Join(os.TempDir(), "ollama*"))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, d := range dirs {
|
|
|
|
info, err := os.Stat(d)
|
|
|
|
if err != nil || !info.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
raw, err := os.ReadFile(filepath.Join(d, "ollama.pid"))
|
|
|
|
if err == nil {
|
|
|
|
pid, err := strconv.Atoi(string(raw))
|
|
|
|
if err == nil {
|
|
|
|
if proc, err := os.FindProcess(int(pid)); err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) {
|
|
|
|
// Another running ollama, ignore this tmpdir
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
slog.Debug("failed to open ollama.pid", "path", d, "error", err)
|
|
|
|
}
|
|
|
|
err = os.RemoveAll(d)
|
|
|
|
if err != nil {
|
|
|
|
slog.Debug(fmt.Sprintf("unable to cleanup stale tmpdir %s: %s", d, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:45:55 +00:00
|
|
|
func Cleanup() {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
if payloadsDir != "" {
|
2024-03-11 15:45:57 +00:00
|
|
|
// We want to fully clean up the tmpdir parent of the payloads dir
|
|
|
|
tmpDir := filepath.Clean(filepath.Join(payloadsDir, ".."))
|
|
|
|
slog.Debug("cleaning up", "dir", tmpDir)
|
|
|
|
err := os.RemoveAll(tmpDir)
|
2024-03-08 17:45:55 +00:00
|
|
|
if err != nil {
|
2024-03-11 15:45:57 +00:00
|
|
|
slog.Warn("failed to clean up", "dir", tmpDir, "err", err)
|
2024-02-16 01:15:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdatePath(dir string) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
tmpDir := filepath.Dir(dir)
|
|
|
|
pathComponents := strings.Split(os.Getenv("PATH"), ";")
|
|
|
|
i := 0
|
|
|
|
for _, comp := range pathComponents {
|
|
|
|
if strings.EqualFold(comp, dir) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Remove any other prior paths to our temp dir
|
|
|
|
if !strings.HasPrefix(strings.ToLower(comp), strings.ToLower(tmpDir)) {
|
|
|
|
pathComponents[i] = comp
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newPath := strings.Join(append([]string{dir}, pathComponents...), ";")
|
|
|
|
slog.Info(fmt.Sprintf("Updating PATH to %s", newPath))
|
|
|
|
os.Setenv("PATH", newPath)
|
|
|
|
}
|
|
|
|
// linux and darwin rely on rpath
|
|
|
|
}
|