Skip to content

Commit

Permalink
Parse and dump collapsed format
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Mar 11, 2024
1 parent acc05a4 commit c244027
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions src/converter/one/convert/FlameGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public FlameGraph(Arguments args) {
this.args = args;
}

public void parse(Reader in) throws IOException {
public void parseCollapsed(Reader in) throws IOException {
CallStack stack = new CallStack();

try (BufferedReader br = new BufferedReader(in)) {
Expand Down Expand Up @@ -64,34 +64,30 @@ public void parse(Reader in) throws IOException {

public void parseHtml(Reader in) throws IOException {
try (BufferedReader br = new BufferedReader(in)) {
for (String line; !(line = readLine(br)).startsWith("const cpool"); ) ;
readLine(br);
for (String line; !(line = br.readLine()).startsWith("const cpool"); ) ;
br.readLine();

String s = "";
for (String line; (line = readLine(br)).startsWith("'"); ) {
for (String line; (line = br.readLine()).startsWith("'"); ) {
String packed = unescape(line.substring(1, line.lastIndexOf('\'')));
s = s.substring(0, packed.charAt(0) - ' ').concat(packed.substring(1));
cpool.put(s, cpool.size());
}

for (String line; !(line = readLine(br)).isEmpty(); ) ;
for (String line; !(line = br.readLine()).isEmpty(); ) ;

Frame[] levels = new Frame[128];
int level = 0;
long total = 0;

for (String line; !(line = readLine(br)).isEmpty(); ) {
for (String line; !(line = br.readLine()).isEmpty(); ) {
StringTokenizer st = new StringTokenizer(line.substring(2, line.length() - 1), ",");
int nameAndType = Integer.parseInt(st.nextToken());

char func = line.charAt(0);
if (func == 'f') {
int newLevel = Integer.parseInt(st.nextToken());
long self = Long.parseLong(st.nextToken());
if (newLevel > level) {
levels[level].self = self;
}
level = newLevel;
level = Integer.parseInt(st.nextToken());
st.nextToken();
} else if (func == 'u') {
level++;
} else if (func != 'n') {
Expand All @@ -109,13 +105,15 @@ public void parseHtml(Reader in) throws IOException {
}

Frame f = level > 0 ? new Frame(titleIndex, type) : root;
f.total = total;
f.self = f.total = total;
if (st.hasMoreTokens()) f.inlined = Long.parseLong(st.nextToken());
if (st.hasMoreTokens()) f.c1 = Long.parseLong(st.nextToken());
if (st.hasMoreTokens()) f.interpreted = Long.parseLong(st.nextToken());

if (level > 0) {
levels[level - 1].put(f.key, f);
Frame parent = levels[level - 1];
parent.put(f.key, f);
parent.self -= total;
depth = Math.max(depth, level);
}
if (level >= levels.length) {
Expand Down Expand Up @@ -361,14 +359,6 @@ private static String unescape(String s) {
return s;
}

private static String readLine(BufferedReader br) throws IOException {
String s = br.readLine();
if (s == null) {
throw new IllegalStateException("Unexpected end of file");
}
return s;
}

private static String getResource(String name) {
try (InputStream stream = FlameGraph.class.getResourceAsStream(name)) {
if (stream == null) {
Expand All @@ -394,7 +384,11 @@ public int compare(Frame f1, Frame f2) {
public static void convert(String input, String output, Arguments args) throws IOException {
FlameGraph fg = new FlameGraph(args);
try (InputStreamReader in = new InputStreamReader(new FileInputStream(input), StandardCharsets.UTF_8)) {
fg.parse(in);
if (input.endsWith(".html")) {
fg.parseHtml(in);
} else {
fg.parseCollapsed(in);
}
}
try (PrintStream out = new PrintStream(output, "UTF-8")) {
fg.dump(out);
Expand Down

0 comments on commit c244027

Please sign in to comment.