assets.go 2.7 KB

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