xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/python/pprof_profile_builder.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/compiler/xla/python/pprof_profile_builder.h"
17 
18 #include <string>
19 
20 #include "tensorflow/compiler/xla/statusor.h"
21 #include "tensorflow/compiler/xla/util.h"
22 #include "tensorflow/core/platform/protobuf.h"
23 
24 namespace xla {
25 
26 namespace py = pybind11;
27 
PprofProfileBuilder()28 PprofProfileBuilder::PprofProfileBuilder() { CHECK_EQ(0, StringId("")); }
29 
StringId(const std::string & s)30 int PprofProfileBuilder::StringId(const std::string& s) {
31   auto ret = strings_.emplace(s, profile_.string_table_size());
32   if (ret.second) {
33     profile_.add_string_table(s);
34   }
35   return ret.first->second;
36 }
37 
FunctionId(PyCodeObject * code)38 int PprofProfileBuilder::FunctionId(PyCodeObject* code) {
39   // +1 because id 0 is reserved.
40   auto ret = functions_.emplace(code, profile_.function_size() + 1);
41   if (ret.second) {
42     auto* function = profile_.add_function();
43     function->set_id(ret.first->second);
44     int name = StringId(py::str(code->co_name));
45     function->set_name(name);
46     function->set_system_name(name);
47     function->set_filename(StringId(py::str(code->co_filename)));
48     function->set_start_line(code->co_firstlineno);
49   }
50   return ret.first->second;
51 }
52 
LocationId(PyCodeObject * code,int instruction)53 int PprofProfileBuilder::LocationId(PyCodeObject* code, int instruction) {
54   // +1 because id 0 is reserved.
55   auto ret = locations_.emplace(std::make_pair(code, instruction),
56                                 profile_.location_size() + 1);
57   if (ret.second) {
58     auto* location = profile_.add_location();
59     location->set_id(ret.first->second);
60     auto* line = location->add_line();
61     line->set_function_id(FunctionId(code));
62     line->set_line(PyCode_Addr2Line(code, instruction));
63   }
64   return ret.first->second;
65 }
66 
JsonToPprofProfile(std::string json)67 StatusOr<pybind11::bytes> JsonToPprofProfile(std::string json) {
68   tensorflow::tfprof::pprof::Profile profile;
69   auto status = tensorflow::protobuf::util::JsonStringToMessage(json, &profile);
70   if (!status.ok()) {
71     // TODO(phawkins): the explicit `std::string` cast here is to work around
72     // https://github.com/google/jax/issues/9534 which appears to be an ABSL and
73     // protobuf version compatibility problem.
74     return InvalidArgument("JSON parsing failed: %s",
75                            std::string{status.message()});
76   }
77   return py::bytes(profile.SerializeAsString());
78 }
79 
PprofProfileToJson(py::bytes binary_proto)80 StatusOr<std::string> PprofProfileToJson(py::bytes binary_proto) {
81   tensorflow::tfprof::pprof::Profile profile;
82   profile.ParseFromString(binary_proto);
83   std::string output;
84   auto status =
85       tensorflow::protobuf::util::MessageToJsonString(profile, &output);
86   if (!status.ok()) {
87     // TODO(phawkins): the explicit `std::string` cast here is to work around
88     // https://github.com/google/jax/issues/9534 which appears to be an ABSL and
89     // protobuf version compatibility problem.
90     return InvalidArgument("JSON printing failed: %s",
91                            std::string{status.message()});
92   }
93   return output;
94 }
95 
96 }  // namespace xla
97