assets.go 3.5 KB

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