assets.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package gpu
  2. import (
  3. "errors"
  4. "fmt"
  5. "log/slog"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "syscall"
  13. "time"
  14. )
  15. var (
  16. lock sync.Mutex
  17. payloadsDir = ""
  18. )
  19. func PayloadsDir() (string, error) {
  20. lock.Lock()
  21. defer lock.Unlock()
  22. if payloadsDir == "" {
  23. cleanupTmpDirs()
  24. tmpDir, err := os.MkdirTemp("", "ollama")
  25. if err != nil {
  26. return "", fmt.Errorf("failed to generate tmp dir: %w", err)
  27. }
  28. // Track our pid so we can clean up orphaned tmpdirs
  29. pidFilePath := filepath.Join(tmpDir, "ollama.pid")
  30. pidFile, err := os.OpenFile(pidFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, os.ModePerm)
  31. if err != nil {
  32. return "", err
  33. }
  34. if _, err := pidFile.Write([]byte(fmt.Sprint(os.Getpid()))); err != nil {
  35. return "", err
  36. }
  37. // We create a distinct subdirectory for payloads within the tmpdir
  38. // This will typically look like /tmp/ollama3208993108/runners on linux
  39. payloadsDir = filepath.Join(tmpDir, "runners")
  40. }
  41. return payloadsDir, nil
  42. }
  43. // Best effort to clean up prior tmpdirs
  44. func cleanupTmpDirs() {
  45. dirs, err := filepath.Glob(filepath.Join(os.TempDir(), "ollama*"))
  46. if err != nil {
  47. return
  48. }
  49. for _, d := range dirs {
  50. info, err := os.Stat(d)
  51. if err != nil || !info.IsDir() {
  52. continue
  53. }
  54. raw, err := os.ReadFile(filepath.Join(d, "ollama.pid"))
  55. if err == nil {
  56. pid, err := strconv.Atoi(string(raw))
  57. if err == nil {
  58. if proc, err := os.FindProcess(int(pid)); err == nil && !errors.Is(proc.Signal(syscall.Signal(0)), os.ErrProcessDone) {
  59. // Another running ollama, ignore this tmpdir
  60. continue
  61. }
  62. }
  63. } else {
  64. slog.Debug("failed to open ollama.pid", "path", d, "error", err)
  65. }
  66. err = os.RemoveAll(d)
  67. if err != nil {
  68. slog.Debug(fmt.Sprintf("unable to cleanup stale tmpdir %s: %s", d, err))
  69. }
  70. }
  71. }
  72. func Cleanup() {
  73. lock.Lock()
  74. defer lock.Unlock()
  75. if payloadsDir != "" {
  76. // We want to fully clean up the tmpdir parent of the payloads dir
  77. tmpDir := filepath.Clean(filepath.Join(payloadsDir, ".."))
  78. slog.Debug("cleaning up", "dir", tmpDir)
  79. err := os.RemoveAll(tmpDir)
  80. if err != nil {
  81. // On windows, if we remove too quickly the llama.dll may still be in-use and fail to remove
  82. time.Sleep(1000 * time.Millisecond)
  83. err = os.RemoveAll(tmpDir)
  84. if err != nil {
  85. slog.Warn("failed to clean up", "dir", tmpDir, "err", err)
  86. }
  87. }
  88. }
  89. }
  90. func UpdatePath(dir string) {
  91. if runtime.GOOS == "windows" {
  92. tmpDir := filepath.Dir(dir)
  93. pathComponents := strings.Split(os.Getenv("PATH"), ";")
  94. i := 0
  95. for _, comp := range pathComponents {
  96. if strings.EqualFold(comp, dir) {
  97. return
  98. }
  99. // Remove any other prior paths to our temp dir
  100. if !strings.HasPrefix(strings.ToLower(comp), strings.ToLower(tmpDir)) {
  101. pathComponents[i] = comp
  102. i++
  103. }
  104. }
  105. newPath := strings.Join(append([]string{dir}, pathComponents...), ";")
  106. slog.Info(fmt.Sprintf("Updating PATH to %s", newPath))
  107. os.Setenv("PATH", newPath)
  108. }
  109. // linux and darwin rely on rpath
  110. }