use of uk.me.parabola.mkgmap.combiners.OverviewBuilder in project mkgmap by openstreetmap.
the class Main method addTdbBuilder.
/**
* Add the builders for the TDB and overview map. These are always
* generated together as we use some info that is calculated when constructing
* the overview map in the TDB file.
*/
private void addTdbBuilder() {
if (!tdbBuilderAdded) {
OverviewBuilder overviewBuilder = new OverviewBuilder();
addCombiner("img", overviewBuilder);
TdbBuilder tdbBuilder = new TdbBuilder(overviewBuilder);
addCombiner("tdb", tdbBuilder);
tdbBuilderAdded = true;
}
}
use of uk.me.parabola.mkgmap.combiners.OverviewBuilder in project mkgmap by openstreetmap.
the class Main method endOptions.
public void endOptions(CommandArgs args) {
fileOptions(args);
log.info("Start tile processors");
int threadCount = maxJobs;
int taskCount = futures.size();
Runtime runtime = Runtime.getRuntime();
if (threadPool == null) {
if (threadCount == 0) {
threadCount = 1;
if (taskCount > 2) {
// run one task to see how much memory it uses
log.info("Max Memory: " + runtime.maxMemory());
futures.get(0).run();
long maxMemory = 0;
for (MemoryPoolMXBean mxBean : ManagementFactory.getMemoryPoolMXBeans()) {
if (mxBean.getType() == MemoryType.HEAP) {
MemoryUsage memoryUsage = mxBean.getPeakUsage();
log.info("Max: " + memoryUsage.getMax());
log.info("Used: " + memoryUsage.getUsed());
if (memoryUsage.getMax() > maxMemory && memoryUsage.getUsed() != 0) {
maxMemory = memoryUsage.getMax();
threadCount = (int) (memoryUsage.getMax() / memoryUsage.getUsed());
}
}
}
threadCount = Math.max(threadCount, 1);
threadCount = Math.min(threadCount, runtime.availableProcessors());
System.out.println("Setting max-jobs to " + threadCount);
}
}
log.info("Creating thread pool with " + threadCount + " threads");
threadPool = Executors.newFixedThreadPool(threadCount);
}
// process all input files
for (FilenameTask task : futures) {
threadPool.execute(task);
}
List<FilenameTask> filenames = new ArrayList<>();
int numMapFailedExceptions = 0;
if (threadPool != null) {
threadPool.shutdown();
while (!futures.isEmpty()) {
try {
try {
// don't call get() until a job has finished
if (futures.get(0).isDone()) {
FilenameTask future = futures.remove(0);
// Provoke any exceptions by calling get and then
// save the result for later use
future.setFilename(future.get());
filenames.add(future);
} else
Thread.sleep(100);
} catch (ExecutionException e) {
// Re throw the underlying exception
Throwable cause = e.getCause();
if (cause instanceof Exception)
// noinspection ProhibitedExceptionThrown
throw (Exception) cause;
else if (cause instanceof Error)
// noinspection ProhibitedExceptionThrown
throw (Error) cause;
else
throw e;
}
} catch (OutOfMemoryError | ExitException e) {
throw e;
} catch (MapFailedException mfe) {
// System.err.println(mfe.getMessage()); // already printed via log
numMapFailedExceptions++;
setProgramRC(-1);
} catch (Throwable t) {
t.printStackTrace();
if (!args.getProperties().getProperty("keep-going", false)) {
throw new ExitException("Exiting - if you want to carry on regardless, use the --keep-going option");
}
}
}
}
System.out.println("Number of MapFailedExceptions: " + numMapFailedExceptions);
if ((taskCount > threadCount + 1) && (maxJobs == 0) && (threadCount < runtime.availableProcessors())) {
System.out.println("To reduce the run time, consider increasing the amnount of memory available for use by mkgmap by using the Java -Xmx flag to set the memory to more than " + 100 * (1 + ((runtime.maxMemory() * runtime.availableProcessors()) / (threadCount * 1024 * 1024 * 100))) + " MB, providing this is less than the amount of physical memory installed.");
}
if (combiners.isEmpty())
return;
boolean hasFiles = false;
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled() || file.getFilename() == null) {
if (args.getProperties().getProperty("keep-going", false))
continue;
else
throw new ExitException("Exiting - if you want to carry on regardless, use the --keep-going option");
}
hasFiles = true;
}
if (!hasFiles) {
log.warn("nothing to do for combiners.");
return;
}
log.info("Combining maps");
args.setSort(getSort(args));
// Get them all set up.
for (Combiner c : combiners) c.init(args);
filenames.sort(new Comparator<FilenameTask>() {
public int compare(FilenameTask o1, FilenameTask o2) {
if (!o1.getFilename().endsWith(".img") || !o2.getFilename().endsWith(".img"))
return o1.getFilename().compareTo(o2.getFilename());
// Both end in .img
try {
int id1 = FileInfo.getFileInfo(o1.getFilename()).getHexname();
int id2 = FileInfo.getFileInfo(o2.getFilename()).getHexname();
if (id1 == id2)
return 0;
else if (id1 < id2)
return -1;
else
return 1;
} catch (FileNotFoundException ignored) {
}
return 0;
}
});
// will contain img files for which an additional ovm file was found
HashSet<String> foundOvmFiles = new HashSet<>();
// try OverviewBuilder with special files
if (tdbBuilderAdded) {
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled())
continue;
try {
String fileName = file.getFilename();
if (!fileName.endsWith(".img"))
continue;
fileName = OverviewBuilder.getOverviewImgName(fileName);
log.info(" " + fileName);
FileInfo fileInfo = FileInfo.getFileInfo(fileName);
fileInfo.setArgs(file.getArgs());
// add the real input file
foundOvmFiles.add(file.getFilename());
for (Combiner c : combiners) {
if (c instanceof OverviewBuilder)
c.onMapEnd(fileInfo);
}
} catch (FileNotFoundException ignored) {
}
}
}
// Tell them about each filename (OverviewBuilder excluded)
for (FilenameTask file : filenames) {
if (file == null || file.isCancelled())
continue;
try {
log.info(" " + file);
FileInfo fileInfo = FileInfo.getFileInfo(file.getFilename());
fileInfo.setArgs(file.getArgs());
for (Combiner c : combiners) {
if (c instanceof OverviewBuilder && foundOvmFiles.contains(file.getFilename()))
continue;
c.onMapEnd(fileInfo);
}
} catch (FileNotFoundException e) {
throw new MapFailedException("could not open file " + e.getMessage());
}
}
// All done, allow tidy up or file creation to happen
for (Combiner c : combiners) c.onFinish();
if (tdbBuilderAdded && args.getProperties().getProperty("remove-ovm-work-files", false)) {
for (String fName : foundOvmFiles) {
String ovmFile = OverviewBuilder.getOverviewImgName(fName);
log.info("removing " + ovmFile);
new File(ovmFile).delete();
}
}
}
Aggregations