Skip to content

Commit

Permalink
Fixed translator execution report, added total execution time.
Browse files Browse the repository at this point in the history
	Change on 2016/08/02 by tball <tball@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129106666
  • Loading branch information
tomball committed Sep 7, 2016
1 parent c19b0e3 commit 6ed3fb3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
11 changes: 10 additions & 1 deletion doc/man/j2objc.1
Expand Up @@ -126,7 +126,16 @@ Do not generate metadata needed for Java reflection.
Generate code that facilitates Swift importing.
.TP
\fB\-t\fR, \fB\-\-timing\-info\fR
Print time spent in translation steps.
Print time spent in translation steps and total execution time.
.TP
\fB\-\-timing\-info:all\fR
Print time spent in translation steps and total execution time.
.TP
\fB\-\-timing\-info:total\fR
Print total execution time.
.TP
\fB\-\-timing\-info:none\fR
Do not print timing information (default).
.TP
.BI \-use\-arc
Generate Objective\-C code to support Automatic Reference Counting (ARC).
Expand Down
10 changes: 9 additions & 1 deletion translator/src/main/java/com/google/devtools/j2objc/J2ObjC.java
Expand Up @@ -19,6 +19,7 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.devtools.j2objc.Options.TimingLevel;
import com.google.devtools.j2objc.pipeline.AnnotationPreProcessor;
import com.google.devtools.j2objc.pipeline.GenerationBatch;
import com.google.devtools.j2objc.pipeline.InputFilePreprocessor;
Expand All @@ -30,7 +31,6 @@
import com.google.devtools.j2objc.util.JdtParser;
import com.google.devtools.j2objc.util.ProGuardUsageParser;
import com.google.devtools.j2objc.util.UnicodeUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -160,6 +160,8 @@ public static void main(String[] args) {
if (args.length == 0) {
Options.help(true);
}
long startTime = System.currentTimeMillis();

String[] files = null;
try {
files = Options.load(args);
Expand All @@ -173,6 +175,12 @@ public static void main(String[] args) {

run(Arrays.asList(files));

TimingLevel timingLevel = Options.timingLevel();
if (timingLevel == TimingLevel.TOTAL || timingLevel == TimingLevel.ALL) {
System.out.printf("j2objc execution time: %d ms\n", System.currentTimeMillis() - startTime);
}

// Run last, since it calls System.exit() with the number of errors.
checkErrors();
}
}
31 changes: 29 additions & 2 deletions translator/src/main/java/com/google/devtools/j2objc/Options.java
Expand Up @@ -94,6 +94,7 @@ public class Options {
private boolean nullability = false;
private EnumSet<LintOption> lintOptions = EnumSet.noneOf(LintOption.class);
private boolean includeGeneratedSources = false;
private TimingLevel timingLevel = TimingLevel.NONE;

private PackagePrefixes packagePrefixes = new PackagePrefixes();

Expand Down Expand Up @@ -124,6 +125,7 @@ public class Options {
private static final String XBOOTCLASSPATH = "-Xbootclasspath:";
private static String bootclasspath = System.getProperty("sun.boot.class.path");
private static final String BATCH_PROCESSING_MAX_FLAG = "--batch-translate-max=";
private static final String TIMING_INFO_ARG = "--timing-info";

static {
// Load string resources.
Expand Down Expand Up @@ -306,6 +308,20 @@ static EnumSet<LintOption> parse(String flag) {
}
}

/**
* What timing information should be printed, if any.
*/
public enum TimingLevel {
// Don't print any timing information.
NONE,

// Print the total execution time at the end.
TOTAL,

// Print all timing information.
ALL
}

/**
* Set all log handlers in this package with a common level.
*/
Expand Down Expand Up @@ -438,8 +454,15 @@ private String[] loadInternal(String[] args) throws IOException {
deprecatedDeclarations = true;
} else if (arg.equals("-q") || arg.equals("--quiet")) {
setLogLevel(Level.WARNING);
} else if (arg.equals("-t") || arg.equals("--timing-info")) {
setLogLevel(Level.FINE);
} else if (arg.equals("-t") || arg.equals(TIMING_INFO_ARG)) {
timingLevel = TimingLevel.ALL;
} else if (arg.startsWith(TIMING_INFO_ARG + ':')) {
String timingArg = arg.substring(TIMING_INFO_ARG.length() + 1);
try {
timingLevel = TimingLevel.valueOf(timingArg.toUpperCase());
} catch (IllegalArgumentException e) {
usage("invalid --timing-info argument");
}
} else if (arg.equals("-v") || arg.equals("--verbose")) {
setLogLevel(Level.FINEST);
} else if (arg.startsWith(XBOOTCLASSPATH)) {
Expand Down Expand Up @@ -985,4 +1008,8 @@ public static EnumSet<LintOption> lintOptions() {
public static boolean includeGeneratedSources() {
return instance.includeGeneratedSources;
}

public static TimingLevel timingLevel() {
return instance.timingLevel;
}
}
Expand Up @@ -96,7 +96,7 @@ protected void processConvertedTree(ProcessingContext input, CompilationUnit uni
if (logger.isLoggable(Level.INFO)) {
System.out.println("translating " + unitName);
}
TimeTracker ticker = getTicker(unitName);
TimeTracker ticker = TimeTracker.getTicker(unitName);
applyMutations(unit, deadCodeMap, ticker);
ticker.tick("Tree mutations");
ticker.printResults(System.out);
Expand Down Expand Up @@ -311,7 +311,7 @@ public static void applyMutations(
public static void generateObjectiveCSource(GenerationUnit unit) {
assert unit.getOutputPath() != null;
assert unit.isFullyParsed();
TimeTracker ticker = getTicker(unit.getOutputPath());
TimeTracker ticker = TimeTracker.getTicker(unit.getSourceName());
logger.fine("Generating " + unit.getOutputPath());
logger.finest("writing output file(s) to " + Options.getOutputDirectory().getAbsolutePath());
ticker.push();
Expand Down Expand Up @@ -370,12 +370,4 @@ private void checkDependencies(CompilationUnit unit) {
}
}
}

private static TimeTracker getTicker(String name) {
if (logger.isLoggable(Level.FINEST)) {
return TimeTracker.start(name);
} else {
return TimeTracker.noop();
}
}
}
Expand Up @@ -15,7 +15,8 @@
package com.google.devtools.j2objc.util;

import com.google.common.collect.Lists;

import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.Options.TimingLevel;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
Expand All @@ -28,6 +29,14 @@
*/
public class TimeTracker {

public static TimeTracker getTicker(String name) {
if (Options.timingLevel() == TimingLevel.ALL) {
return TimeTracker.start(name);
} else {
return TimeTracker.noop();
}
}

public static TimeTracker noop() {
return new TimeTracker();
}
Expand Down Expand Up @@ -72,22 +81,26 @@ private TimeTrackerImpl(String name) {
lastTicks[currentLevel] = System.currentTimeMillis();
}

@Override
public void tick(String event) {
long now = System.currentTimeMillis();
long time = now - lastTicks[currentLevel];
lastTicks[currentLevel] = now;
entries.add(String.format("%s%5d ms - %s", INDENTS[currentLevel], time, event));
}

@Override
public void push() {
currentLevel++;
lastTicks[currentLevel] = System.currentTimeMillis();
}

@Override
public void pop() {
currentLevel--;
}

@Override
public void printResults(PrintStream out) {
for (String entry : entries) {
out.println(entry);
Expand Down
Expand Up @@ -73,6 +73,7 @@ Other options:\n\
--strip-reflection Do not generate metadata needed for Java reflection.\n\
--swift-friendly Generate code that facilitates Swift importing.\n\
-t, --timing-info Print time spent in translation steps.\n\
--timing-info:{all,total,none} Print time spent in translation steps.\n\
-use-arc Generate Objective-C code to support Automatic\
\n Reference Counting (ARC).\n\
-use-reference-counting Generate Objective-C code to support iOS manual\
Expand Down

0 comments on commit 6ed3fb3

Please sign in to comment.