Skip to content

Commit

Permalink
Add read barrier in background thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkernevez committed Aug 21, 2023
1 parent 22c7928 commit 259978f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
Expand Down Expand Up @@ -41,10 +42,12 @@ class AssessProcessAction(
private var data: Pair<Inventory, Comparator<MatrixColumnIndex>>? = null

override fun run(indicator: ProgressIndicator) {
val trace = traceSystemWithIndicator(indicator, file, processName, matchLabels)
val order = trace.getObservableOrder()
val inventory = Assessment(trace.getSystemValue(), trace.getEntryPoint()).inventory()
this.data = Pair(inventory, order)
runReadAction {
val trace = traceSystemWithIndicator(indicator, file, processName, matchLabels)
val order = trace.getObservableOrder()
val inventory = Assessment(trace.getSystemValue(), trace.getEntryPoint()).inventory()
this.data = Pair(inventory, order)
}
}

override fun onSuccess() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,69 +41,71 @@ class AssessProcessWithDataAction(

ProgressManager.getInstance().run(object : Task.Backgroundable(project, "Run with ${processName}.csv") {
override fun run(indicator: ProgressIndicator) {
try {
// read
indicator.fraction = 0.0
indicator.text = "Reading $processName.csv"
val csvFile = Path(containingDirectory.virtualFile.path, "$processName.csv").toFile()
val requests = csvFile.inputStream().use {
val requestReader = CsvRequestReader(processName, matchLabels, it)
requestReader.read()
}
runReadAction {
try {
// read
indicator.fraction = 0.0
indicator.text = "Reading $processName.csv"
val csvFile = Path(containingDirectory.virtualFile.path, "$processName.csv").toFile()
val requests = csvFile.inputStream().use {
val requestReader = CsvRequestReader(processName, matchLabels, it)
requestReader.read()
}

// process
val symbolTable = runReadAction {
val collector = LcaFileCollector(file.project)
val parser = LcaLangAbstractParser(collector.collect(file))
parser.load()
}
val csvProcessor = CsvProcessor(symbolTable)
val results = requests.map { request ->
ProgressManager.checkCanceled()
indicator.text = "Processing using ${request.arguments()}"
indicator.fraction = indicator.fraction + 1.0 / requests.size
csvProcessor.process(request)
}
// process
val symbolTable = runReadAction {
val collector = LcaFileCollector(file.project)
val parser = LcaLangAbstractParser(collector.collect(file))
parser.load()
}
val csvProcessor = CsvProcessor(symbolTable)
val results = requests.map { request ->
ProgressManager.checkCanceled()
indicator.text = "Processing using ${request.arguments()}"
indicator.fraction = indicator.fraction + 1.0 / requests.size
csvProcessor.process(request)
}

// write
indicator.text = "Writing to $processName.results.csv"
indicator.fraction = 1.0
val path = Path(containingDirectory.virtualFile.path, "$processName.results.csv")
val csvResultFile = path.toFile()
CsvResultWriter(csvResultFile.outputStream()).use { writer ->
writer.write(results)
}
// write
indicator.text = "Writing to $processName.results.csv"
indicator.fraction = 1.0
val path = Path(containingDirectory.virtualFile.path, "$processName.results.csv")
val csvResultFile = path.toFile()
CsvResultWriter(csvResultFile.outputStream()).use { writer ->
writer.write(results)
}

// done
indicator.text = "Written to $processName.results.csv"
indicator.fraction = 1.0
val title = "${requests.size} successful assessments of process $processName"
val message = "Results stored in ${processName}.results.csv"
VirtualFileManager.getInstance().refreshAndFindFileByNioPath(path)
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, message, NotificationType.INFORMATION)
.notify(project)
} catch (e: EvaluatorException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
LOG.warn("Unable to process computation", e)
} catch (e: NoSuchElementException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
LOG.warn("Unable to process computation", e)
} catch (e: FileNotFoundException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
// done
indicator.text = "Written to $processName.results.csv"
indicator.fraction = 1.0
val title = "${requests.size} successful assessments of process $processName"
val message = "Results stored in ${processName}.results.csv"
VirtualFileManager.getInstance().refreshAndFindFileByNioPath(path)
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, message, NotificationType.INFORMATION)
.notify(project)
} catch (e: EvaluatorException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
LOG.warn("Unable to process computation", e)
} catch (e: NoSuchElementException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
LOG.warn("Unable to process computation", e)
} catch (e: FileNotFoundException) {
val title = "Error while assessing $processName"
NotificationGroupManager.getInstance()
.getNotificationGroup("LcaAsCode")
.createNotification(title, e.message ?: "unknown error", NotificationType.ERROR)
.notify(project)
}
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.intellij.notification.NotificationType
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
Expand Down Expand Up @@ -47,9 +48,14 @@ class SankeyGraphAction(
private var graph: Graph? = null

override fun run(progress: ProgressIndicator) {
val trace = traceSystemWithIndicator(progress, file, processName, matchLabels)
val assessment = Assessment(trace.getSystemValue(), trace.getEntryPoint())
val inventory = assessment.inventory()
val (trace, assessment, inventory) =
runReadAction {
val rTrace = traceSystemWithIndicator(progress, file, processName, matchLabels)
val rAssessment = Assessment(rTrace.getSystemValue(), rTrace.getEntryPoint())
val rInventory = rAssessment.inventory()
val result = Triple(rTrace, rAssessment, rInventory)
result
}
val allocatedSystem = assessment.allocatedSystem
indicatorList = inventory.getControllablePorts().getElements()

Expand Down

0 comments on commit 259978f

Please sign in to comment.