Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft for a buffered writing of files #183

Merged
merged 17 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gama.core/src/gama/core/common/interfaces/IKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public interface IKeyword {

/** The browse. */
String BROWSE = "browse";

String BUFFERING = "buffering";

/** The camera. */
String CAMERA = "camera";
Expand Down Expand Up @@ -299,6 +301,8 @@ public interface IKeyword {

/** The enables. */
String ENABLES = "enables";

String END = "end";

/** The enter. */
String ENTER = "enter";
Expand Down
4 changes: 2 additions & 2 deletions gama.core/src/gama/core/common/interfaces/ISaveDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import gama.core.runtime.IScope;
import gama.gaml.expressions.IExpression;
import gama.gaml.statements.save.SaveOptions;
import gama.gaml.types.IType;
import gama.gaml.types.Types;

Expand Down Expand Up @@ -50,8 +51,7 @@ public interface ISaveDelegate {
* value> or a list<string>.
* @throws IOException
*/
void save(IScope scope, IExpression item, File file, String code, boolean addHeader, String type,
Object attributesToSave) throws IOException;
void save(IScope scope, IExpression item, File file, SaveOptions saveOptions) throws IOException;

/**
* The type of the item. Returns the gaml type required for triggering this save delegate. If no type is declared
Expand Down
15 changes: 15 additions & 0 deletions gama.core/src/gama/core/common/preferences/GamaPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import gama.core.outputs.layers.properties.ICameraDefinition;
import gama.core.runtime.GAMA;
import gama.core.runtime.PlatformHelper;
import gama.core.runtime.concurrent.BufferingController;
import gama.core.util.GamaColor;
import gama.core.util.GamaFont;
import gama.core.util.GamaMapFactory;
Expand Down Expand Up @@ -76,6 +77,9 @@ public class GamaPreferences {
() -> GamaColor.get(199, 234, 229), () -> GamaColor.get(128, 205, 193), () -> GamaColor.get(53, 151, 143),
() -> GamaColor.get(1, 102, 94), () -> GamaColor.get(0, 60, 48) };

public static final String PREF_SAVE_BUFFERING_STRATEGY = "pref_save_buffering_strategy";
public static final String PREF_WRITE_BUFFERING_STRATEGY = "pref_write_buffering_strategy";

/**
*
* Interface tab
Expand Down Expand Up @@ -864,6 +868,17 @@ public static class External {
"In-memory shapefile mapping (optimizes access to shapefile data in exchange for increased memory usage)",
true, IType.BOOL, true).in(NAME, OPTIMIZATIONS);

/** The Constant DEFAULT_BUFFERING_STRATEGY. */
public static final Pref<String> DEFAULT_SAVE_BUFFERING_STRATEGY =
create(PREF_SAVE_BUFFERING_STRATEGY, "Default buffering strategy for the save statement", BufferingController.NO_BUFFERING, IType.STRING, true)
.among(BufferingController.BUFFERING_STRATEGIES.stream().toList())
.in(NAME, OPTIMIZATIONS);

public static final Pref<String> DEFAULT_WRITE_BUFFERING_STRATEGY =
create(PREF_WRITE_BUFFERING_STRATEGY, "Default buffering strategy for the write statement", BufferingController.NO_BUFFERING, IType.STRING, true)
.among(BufferingController.BUFFERING_STRATEGIES.stream().toList())
.in(NAME, OPTIMIZATIONS);

/**
* Paths to libraries
*/
Expand Down
10 changes: 8 additions & 2 deletions gama.core/src/gama/core/kernel/simulation/SimulationAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ protected void postStep(final IScope scope) {
executer.executeOneShotActions();
if (outputs != null) { outputs.step(this.getScope()); }
ownClock.step();
GAMA.flushSaveFileStep(this);
GAMA.flushWriteStep(this);
}

@Override
Expand Down Expand Up @@ -437,8 +439,8 @@ public Object _init_(final IScope scope) {
public void dispose() {
if (dead) return;
executer.executeDisposeActions();
// hqnghi if simulation come from popultion extern, dispose pop first
// and then their outputs
// hqnghi if simulation comes from an external population, dispose this population first
// and then its outputs

if (externMicroPopulations != null) { externMicroPopulations.clear(); }

Expand All @@ -455,6 +457,10 @@ public void dispose() {
}
}
if (externalInitsAndParameters != null) { externalInitsAndParameters.clear(); }

//we make sure that all pending write operations are flushed
GAMA.flushSaveFilePerOwner(this);
GAMA.flushWritePerAgent(this);
GAMA.releaseScope(getScope());
// scope = null;
super.dispose();
Expand Down
2 changes: 2 additions & 0 deletions gama.core/src/gama/core/metamodel/agent/AbstractAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void dispose() {
attributes.clear();
attributes = null;
}
GAMA.flushSaveFilePerOwner(this);
GAMA.flushWritePerAgent(this);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions gama.core/src/gama/core/runtime/GAMA.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
********************************************************************************************************/
package gama.core.runtime;

import java.io.File;
lesquoyb marked this conversation as resolved.
Show resolved Hide resolved
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -32,15 +33,20 @@
import gama.core.kernel.model.IModel;
import gama.core.kernel.root.PlatformAgent;
import gama.core.kernel.simulation.SimulationAgent;
import gama.core.metamodel.agent.AbstractAgent;
import gama.core.runtime.IExperimentStateListener.State;
import gama.core.runtime.benchmark.Benchmark;
import gama.core.runtime.benchmark.StopWatch;
import gama.core.runtime.concurrent.BufferingController;
import gama.core.runtime.concurrent.BufferingController.BufferingStrategies;
import gama.core.runtime.exceptions.GamaRuntimeException;
import gama.core.runtime.exceptions.GamaRuntimeException.GamaRuntimeFileException;
import gama.core.util.GamaColor;
import gama.dev.DEBUG;
import gama.gaml.compilation.ISymbol;
import gama.gaml.compilation.kernel.GamaBundleLoader;
import gama.gaml.compilation.kernel.GamaMetaModel;
import gama.gaml.statements.save.SaveOptions;

/**
* Written by drogoul Modified on 23 nov. 2009
Expand Down Expand Up @@ -98,6 +104,31 @@ public class GAMA {
// hqnghi: add several controllers to have multi-thread experiments
private static final List<IExperimentController> controllers = new CopyOnWriteArrayList<>();

private static final BufferingController bufferingController = new BufferingController();



public static boolean askWriteFile(final IScope scope, final File f, final CharSequence content, final SaveOptions options) {
return bufferingController.askWriteFile(f.getAbsolutePath(), scope, content, options);
}
public static boolean askWriteConsole(final IScope scope, final StringBuilder content, final GamaColor color, final BufferingStrategies strategy) {
return bufferingController.askWriteConsole(scope, content, color, strategy);
}

public static boolean flushSaveFilePerOwner(final AbstractAgent owner) {
return bufferingController.flushSaveFilesOfOwner(owner);
}
public static boolean flushSaveFileStep(final SimulationAgent owner) {
return bufferingController.flushSaveFilesInCycle(owner);
}
public static void flushWriteStep(final SimulationAgent owner) {
bufferingController.flushWriteInCycle(owner);
}
public static void flushWritePerAgent(final AbstractAgent owner) {
bufferingController.flushWriteOfOwner(owner);
}


/**
* Gets the controllers.
*
Expand Down
Loading