|
| 1 | +;;; elsa-parallel.el --- -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2023 Matúš Goljer |
| 4 | + |
| 5 | +;; Author: Matúš Goljer <[email protected]> |
| 6 | +;; Maintainer: Matúš Goljer <[email protected]> |
| 7 | +;; Version: 0.0.1 |
| 8 | +;; Created: 9th March 2023 |
| 9 | +;; Keywords: |
| 10 | + |
| 11 | +;; This program is free software; you can redistribute it and/or |
| 12 | +;; modify it under the terms of the GNU General Public License |
| 13 | +;; as published by the Free Software Foundation; either version 3 |
| 14 | +;; of the License, or (at your option) any later version. |
| 15 | + |
| 16 | +;; This program is distributed in the hope that it will be useful, |
| 17 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +;; GNU General Public License for more details. |
| 20 | + |
| 21 | +;; You should have received a copy of the GNU General Public License |
| 22 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | + |
| 24 | +;;; Commentary: |
| 25 | + |
| 26 | +;;; Code: |
| 27 | + |
| 28 | +(require 'async) |
| 29 | + |
| 30 | +(defun elsa--worker-function-factory (worker-id project-directory) |
| 31 | + "Return function running in the Elsa analysis worker." |
| 32 | + (let ((load--path load-path)) |
| 33 | + (lambda () |
| 34 | + (setq load-path load--path) |
| 35 | + (setq elsa-is-language-server nil) |
| 36 | + (setq ansi-inhibit-ansi t) |
| 37 | + (require 'elsa) |
| 38 | + (require 'async) |
| 39 | + (let ((msg nil)) |
| 40 | + (setq elsa-global-state (elsa-global-state)) |
| 41 | + (oset elsa-global-state project-directory project-directory) |
| 42 | + (oset elsa-global-state number-of-files 1) |
| 43 | + (oset elsa-global-state processed-file-index 1) |
| 44 | + (catch 'done |
| 45 | + (while t |
| 46 | + (setq msg (async-receive)) |
| 47 | + (let ((op (plist-get msg :op))) |
| 48 | + (cond |
| 49 | + ((equal op "analyze") |
| 50 | + (let* ((dep (plist-get msg :file)) |
| 51 | + (library (file-truename |
| 52 | + (if (f-exists? dep) dep |
| 53 | + (elsa--find-dependency dep)))) |
| 54 | + (current-time (current-time))) |
| 55 | + (when library |
| 56 | + (condition-case err |
| 57 | + (let ((state (elsa-process-file library elsa-global-state))) |
| 58 | + ;; `subr' has no provide for some circular |
| 59 | + ;; dependency "bootstrap" issues. We add it here |
| 60 | + ;; artificially. |
| 61 | + (when (equal dep "subr") |
| 62 | + (oset state provide (list 'subr))) |
| 63 | + ;; (elsa-save-cache state elsa-global-state) |
| 64 | + ) |
| 65 | + (error (async-send :ack "error" :error err)))) |
| 66 | + (async-send :ack "ok" :op op |
| 67 | + :worker-id worker-id :file dep |
| 68 | + :duration (float-time |
| 69 | + (time-subtract |
| 70 | + (current-time) current-time))))) |
| 71 | + ((equal op "load-from-cache") |
| 72 | + (let ((files (plist-get msg :files))) |
| 73 | + (dolist (file files) (load (f-no-ext file) t t)) |
| 74 | + (async-send :ack "ok" :op op :worker-id worker-id))) |
| 75 | + ((equal op "quit") |
| 76 | + (throw 'done t)))))))))) |
| 77 | + |
| 78 | +(defun elsa--parent-function-factory (worker-id workers-state global-state) |
| 79 | + "Function handling child-to-parent messages and worker exit." |
| 80 | + (lambda (result) |
| 81 | + (if (async-message-p result) |
| 82 | + (let* ((worker-id (plist-get result :worker-id)) |
| 83 | + (worker-state (assoc worker-id workers-state)) |
| 84 | + (op (plist-get result :op))) |
| 85 | + (cond |
| 86 | + ((equal op "analyze") |
| 87 | + (let ((file (plist-get result :file)) |
| 88 | + (duration (plist-get result :duration))) |
| 89 | + (elsa-log |
| 90 | + (with-ansi |
| 91 | + (green "[%s]" (elsa-global-state-get-counter global-state)) |
| 92 | + (format " (worker %d) Processing file %s ... " worker-id file) |
| 93 | + (green "done") |
| 94 | + " after " |
| 95 | + (cond |
| 96 | + ((> duration 5) |
| 97 | + (bright-red "%.3fs" duration)) |
| 98 | + ((> duration 2) |
| 99 | + (bright-yellow "%.3fs" duration)) |
| 100 | + (t (green "%.3fs" duration)))))) |
| 101 | + (cl-incf (oref global-state processed-file-index)) |
| 102 | + (setf (cdr worker-state) (plist-put (cdr worker-state) :ready t))) |
| 103 | + ((equal op "load-from-cache") |
| 104 | + ;; (message "Worker %s loaded all cache files for this layer" worker-id) |
| 105 | + (setf (cdr worker-state) (plist-put (cdr worker-state) :ready t))))) |
| 106 | + ;; (message "Async process done in worker %d, result: %s" worker-id result) |
| 107 | + t |
| 108 | + ))) |
| 109 | + |
| 110 | + |
| 111 | +(defun elsa--wait-for-all (get-worker-states) |
| 112 | + (catch 'all-workers-ready |
| 113 | + (while t |
| 114 | + (when (--all? (plist-get (cdr it) :ready) (funcall get-worker-states)) |
| 115 | + (throw 'all-workers-ready t)) |
| 116 | + (sleep-for 0.2)))) |
| 117 | + |
| 118 | +(defun elsa-analyse-file-parallel (file global-state &optional already-loaded) |
| 119 | + "Analyse FILE with GLOBAL-STATE. |
| 120 | +
|
| 121 | +Optional argument ALREADY-LOADED is used to skip dependencies which |
| 122 | +are already loaded in the currently running Emacs process. This is |
| 123 | +used by the LSP server to not reload already processed files." |
| 124 | + (my-with-elapsed-timer "process dependencies" |
| 125 | + (let* ((dependencies (reverse |
| 126 | + (append |
| 127 | + (my-with-elapsed-timer "resolving dependencies" |
| 128 | + (elsa-get-dependencies-as-layers file)) |
| 129 | + '(("subr"))))) |
| 130 | + (visited nil) |
| 131 | + (file-state nil) |
| 132 | + ;; alist from worker ID to state |
| 133 | + (workers-state (--map (list it :ready t) (-iota 5))) |
| 134 | + (workers (--map (async-start |
| 135 | + (elsa--worker-function-factory it (f-parent file)) |
| 136 | + (elsa--parent-function-factory it workers-state global-state) |
| 137 | + ;;(lambda (result) (message "result %s" result)) |
| 138 | + ) |
| 139 | + (-iota 5))) |
| 140 | + (i 0)) |
| 141 | + (oset global-state project-directory (f-parent file)) |
| 142 | + (oset global-state processed-file-index 1) |
| 143 | + (oset global-state number-of-files (length (-flatten dependencies))) |
| 144 | + (elsa-log "Processing dependency layers: %s" dependencies) |
| 145 | + (dolist (layer dependencies) |
| 146 | + (cl-incf i) |
| 147 | + ;; (elsa-log "processing layer %s" i) |
| 148 | + (while layer |
| 149 | + (-each workers-state |
| 150 | + (-lambda ((state &as worker-id . (&plist :ready))) |
| 151 | + (when (and ready layer) |
| 152 | + ;(elsa-log "Worker %s is ready, submitting dependency %s" worker-id (car layer)) |
| 153 | + (setf (cdr state) (plist-put (cdr state) :ready nil)) |
| 154 | + (let ((worker (nth worker-id workers))) |
| 155 | + (async-send worker :op "analyze" :file (pop layer)))))) |
| 156 | + (sleep-for 0.2)) |
| 157 | + ;(elsa-log "All work for layer %s was distributed" i) |
| 158 | + (elsa--wait-for-all (lambda () workers-state)) |
| 159 | + ;; (catch 'all-workers-ready |
| 160 | + ;; (while t |
| 161 | + ;; (when (--all? (plist-get (cdr it) :ready) workers-state) |
| 162 | + ;; (throw 'all-workers-ready t)) |
| 163 | + ;; (sleep-for 0.2))) |
| 164 | + ;(elsa-log "All workers finished processing layer %s" i) |
| 165 | + (let ((cache-files (mapcar |
| 166 | + (lambda (dep) |
| 167 | + (elsa--get-cache-file-name global-state dep)) |
| 168 | + layer))) |
| 169 | + (-each workers-state |
| 170 | + (-lambda ((state &as worker-id)) |
| 171 | + (setf (cdr state) (plist-put (cdr state) :ready nil)) |
| 172 | + (let ((worker (nth worker-id workers))) |
| 173 | + (async-send worker :op "load-from-cache" :files cache-files))))) |
| 174 | + (elsa--wait-for-all (lambda () workers-state)) |
| 175 | + ;(elsa-log "All workers updated global state for layer %s" i) |
| 176 | + ) |
| 177 | + (--each workers |
| 178 | + (async-send it :op "quit"))))) |
| 179 | + |
| 180 | +(provide 'elsa-parallel) |
| 181 | +;;; elsa-parallel.el ends here |
0 commit comments