History

perf flamegraph: Explicitly set utf-8 encoding

On some platforms the default encoding is not utf-8, which causes an UnicodeDecodeError when reading the flamegraph template and writing the flamegraph

Andreas Gerstmayr committed 6 years ago

Commit c42ad5d1 parent 625d3441 file · +5 −3Browse files

Showing the changes to tools/perf/scripts/python (1 of 1 file). Show all

Unified · Split

tools/perf/scripts/python/flamegraph.py+5−3
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 from __future__ import print_function
1818 import sys
1919 import os
20+import io
2021 import argparse
2122 import json
2223
@@ -81,7 +82,7 @@ class FlameGraphCLI: · 58 unmodified lines
8182
8283 if self.args.format == "html":
8384 try:
84− with open(self.args.template) as f:
85+ with io.open(self.args.template, encoding="utf-8") as f:
8586 output_str = f.read().replace("/** @flamegraph_json **/",
8687 json_str)
8788 except IOError as e:
@@ -93,11 +94,12 @@ class FlameGraphCLI: · 5 unmodified lines
9394 output_fn = self.args.output or "stacks.json"
9495
9596 if output_fn == "-":
96− sys.stdout.write(output_str)
97+ with io.open(sys.stdout.fileno(), "w", encoding="utf-8", closefd=False) as out:
98+ out.write(output_str)
9799 else:
98100 print("dumping data to {}".format(output_fn))
99101 try:
100− with open(output_fn, "w") as out:
102+ with io.open(output_fn, "w", encoding="utf-8") as out:
101103 out.write(output_str)
102104 except IOError as e:
103105 print("Error writing output file: {}".format(e), file=sys.stderr)