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
17from __future__ import print_function17from __future__ import print_function
18import sys18import sys
19import os19import os
20import io
20import argparse21import argparse
21import json22import json
22 23
@@ -81,7 +82,7 @@ class FlameGraphCLI: · 58 unmodified lines
81 82
82 if self.args.format == "html":83 if self.args.format == "html":
83 try:84 try:
84 with open(self.args.template) as f:85 with io.open(self.args.template, encoding="utf-8") as f:
85 output_str = f.read().replace("/** @flamegraph_json **/",86 output_str = f.read().replace("/** @flamegraph_json **/",
86 json_str)87 json_str)
87 except IOError as e:88 except IOError as e:
@@ -93,11 +94,12 @@ class FlameGraphCLI: · 5 unmodified lines
93 output_fn = self.args.output or "stacks.json"94 output_fn = self.args.output or "stacks.json"
94 95
95 if output_fn == "-":96 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)
97 else:99 else:
98 print("dumping data to {}".format(output_fn))100 print("dumping data to {}".format(output_fn))
99 try:101 try:
100 with open(output_fn, "w") as out:102 with io.open(output_fn, "w", encoding="utf-8") as out:
101 out.write(output_str)103 out.write(output_str)
102 except IOError as e:104 except IOError as e:
103 print("Error writing output file: {}".format(e), file=sys.stderr)105 print("Error writing output file: {}".format(e), file=sys.stderr)