|
| 1 | +package com.semmle.js.extractor; |
| 2 | + |
| 3 | +import com.semmle.util.exception.Exceptions; |
| 4 | +import com.semmle.util.files.FileUtil; |
| 5 | +import com.semmle.util.trap.TrapWriter; |
| 6 | +import com.semmle.util.trap.TrapWriter.Label; |
| 7 | +import com.semmle.util.trap.pathtransformers.PathTransformer; |
| 8 | +import java.io.BufferedWriter; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileOutputStream; |
| 11 | +import java.io.OutputStreamWriter; |
| 12 | +import java.lang.management.ManagementFactory; |
| 13 | +import java.lang.management.ThreadMXBean; |
| 14 | +import java.nio.charset.Charset; |
| 15 | +import java.util.Stack; |
| 16 | +import java.util.zip.GZIPOutputStream; |
| 17 | + |
| 18 | +/** Metrics for the (single-threaded) extraction of a single file. */ |
| 19 | +public class ExtractionMetrics { |
| 20 | + /** |
| 21 | + * The phase of the extraction that should be measured time for. |
| 22 | + * |
| 23 | + * <p>Convention: the enum names have the format <code>{ClassName}_{MethodName}</code>, and should |
| 24 | + * identify the methods they correspond to. |
| 25 | + */ |
| 26 | + public enum ExtractionPhase { |
| 27 | + ASTExtractor_extract(0), |
| 28 | + CFGExtractor_extract(1), |
| 29 | + FileExtractor_extractContents(2), |
| 30 | + JSExtractor_extract(3), |
| 31 | + JSParser_parse(4), |
| 32 | + LexicalExtractor_extractLines(5), |
| 33 | + LexicalExtractor_extractTokens(6), |
| 34 | + TypeScriptASTConverter_convertAST(7), |
| 35 | + TypeScriptParser_talkToParserWrapper(8); |
| 36 | + |
| 37 | + /** The id used in the database for the time spent performing this phase of the extraction. */ |
| 38 | + final int dbschemeId; |
| 39 | + |
| 40 | + ExtractionPhase(int dbschemeId) { |
| 41 | + this.dbschemeId = dbschemeId; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** The cache file, if any. */ |
| 46 | + private File cacheFile; |
| 47 | + |
| 48 | + /** True iff the extraction of this file reuses an existing trap cache file. */ |
| 49 | + private boolean canReuseCacheFile; |
| 50 | + |
| 51 | + /** The cumulative CPU-time spent in each extraction phase so far. */ |
| 52 | + private final long[] cpuTimes = new long[ExtractionPhase.values().length]; |
| 53 | + |
| 54 | + /** The label for the file that is being extracted. */ |
| 55 | + private Label fileLabel; |
| 56 | + |
| 57 | + /** The number of characters in the file that is being extracted. */ |
| 58 | + private int length; |
| 59 | + |
| 60 | + /** The previous time a CPU-time measure was performed. */ |
| 61 | + private long previousCpuTime; |
| 62 | + |
| 63 | + /** The previous time a wallclock-time measure was performed. */ |
| 64 | + private long previousWallclockTime; |
| 65 | + |
| 66 | + /** The extraction phase stack. */ |
| 67 | + private final Stack<ExtractionPhase> stack = new Stack<>(); |
| 68 | + |
| 69 | + /** The current thread, used for measuring CPU-time. */ |
| 70 | + private final ThreadMXBean thread = ManagementFactory.getThreadMXBean(); |
| 71 | + |
| 72 | + /** The cumulative wallclock-time spent in each extraction phase so far. */ |
| 73 | + private final long[] wallclockTimes = new long[ExtractionPhase.values().length]; |
| 74 | + |
| 75 | + /** |
| 76 | + * True iff extraction metrics could not be obtained for this file (due to an unforeseen error |
| 77 | + * that should not prevent the ordinary extraction from succeeding). |
| 78 | + */ |
| 79 | + private boolean timingsFailed; |
| 80 | + |
| 81 | + /** |
| 82 | + * Appends these metrics to a trap file. Note that this makes the resulting trap file content |
| 83 | + * non-deterministic. |
| 84 | + */ |
| 85 | + public void appendToTrapFile(File trapFileToAppendTo) { |
| 86 | + if (trapFileToAppendTo == null) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + BufferedWriter out = null; |
| 91 | + FileOutputStream fos = null; |
| 92 | + GZIPOutputStream gzip = null; |
| 93 | + TrapWriter trapwriter = null; |
| 94 | + try { |
| 95 | + fos = new FileOutputStream(trapFileToAppendTo, true); |
| 96 | + gzip = new GZIPOutputStream(fos); |
| 97 | + out = new BufferedWriter(new OutputStreamWriter(gzip, Charset.forName("UTF-8"))); |
| 98 | + |
| 99 | + trapwriter = new TrapWriter(out, PathTransformer.std()); |
| 100 | + trapwriter.addTuple( |
| 101 | + "extraction_data", |
| 102 | + fileLabel, |
| 103 | + cacheFile != null ? cacheFile.getAbsolutePath() : "", |
| 104 | + canReuseCacheFile, |
| 105 | + length); |
| 106 | + |
| 107 | + if (!stack.isEmpty()) { |
| 108 | + failTimings( |
| 109 | + String.format( |
| 110 | + "Could not properly record extraction times for %s. (stack = %s)%n", |
| 111 | + fileLabel, stack.toString())); |
| 112 | + } |
| 113 | + if (!timingsFailed) { |
| 114 | + for (int i = 0; i < ExtractionPhase.values().length; i++) { |
| 115 | + trapwriter.addTuple("extraction_time", fileLabel, i, 0, (float) cpuTimes[i]); |
| 116 | + trapwriter.addTuple("extraction_time", fileLabel, i, 1, (float) wallclockTimes[i]); |
| 117 | + } |
| 118 | + } |
| 119 | + FileUtil.close(trapwriter); |
| 120 | + } catch (Exception e) { |
| 121 | + FileUtil.close(fos); |
| 122 | + FileUtil.close(gzip); |
| 123 | + FileUtil.close(out); |
| 124 | + FileUtil.close(trapwriter); |
| 125 | + Exceptions.ignore(e, "Ignoring exception for extraction metrics writing"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void failTimings(String msg) { |
| 130 | + System.err.printf(msg); |
| 131 | + System.err.flush(); |
| 132 | + this.timingsFailed = true; |
| 133 | + } |
| 134 | + |
| 135 | + private void incrementCurrentTimer() { |
| 136 | + long nowWallclock = System.nanoTime(); |
| 137 | + long nowCpu = thread.getCurrentThreadCpuTime(); |
| 138 | + |
| 139 | + if (!stack.isEmpty()) { |
| 140 | + // increment by the time elapsed |
| 141 | + wallclockTimes[stack.peek().dbschemeId] += nowWallclock - previousWallclockTime; |
| 142 | + cpuTimes[stack.peek().dbschemeId] += nowCpu - previousCpuTime; |
| 143 | + } |
| 144 | + |
| 145 | + // update the running clock |
| 146 | + previousWallclockTime = nowWallclock; |
| 147 | + previousCpuTime = nowCpu; |
| 148 | + } |
| 149 | + |
| 150 | + public void setCacheFile(File cacheFile) { |
| 151 | + this.cacheFile = cacheFile; |
| 152 | + } |
| 153 | + |
| 154 | + public void setCanReuseCacheFile(boolean canReuseCacheFile) { |
| 155 | + this.canReuseCacheFile = canReuseCacheFile; |
| 156 | + } |
| 157 | + |
| 158 | + public void setFileLabel(Label fileLabel) { |
| 159 | + this.fileLabel = fileLabel; |
| 160 | + } |
| 161 | + |
| 162 | + public void setLength(int length) { |
| 163 | + this.length = length; |
| 164 | + } |
| 165 | + |
| 166 | + public void startPhase(ExtractionPhase event) { |
| 167 | + incrementCurrentTimer(); |
| 168 | + stack.push(event); |
| 169 | + } |
| 170 | + |
| 171 | + public void stopPhase( |
| 172 | + ExtractionPhase |
| 173 | + event /* technically not needed, but useful for documentation and sanity checking */) { |
| 174 | + if (stack.isEmpty()) { |
| 175 | + failTimings( |
| 176 | + String.format( |
| 177 | + "Inconsistent extraction time recording: trying to stop timer %s, but no timer is running", |
| 178 | + event)); |
| 179 | + return; |
| 180 | + } |
| 181 | + if (stack.peek() != event) { |
| 182 | + failTimings( |
| 183 | + String.format( |
| 184 | + "Inconsistent extraction time recording: trying to stop timer %s, but current timer is: %s", |
| 185 | + event, stack.peek())); |
| 186 | + return; |
| 187 | + } |
| 188 | + incrementCurrentTimer(); |
| 189 | + stack.pop(); |
| 190 | + } |
| 191 | +} |
0 commit comments