summaryrefslogtreecommitdiff
path: root/libs/profiling/include/profiling/profiler.h
blob: 953042da37ff102c113faf4b187899ccf8c78e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// NOTE To minimize diff with upstream tensorflow, disable clang-format
// clang-format off

// NOTE This header is derived from the following file (in TensorFlow v1.12)
//        'externals/tensorflow/tensorflow/contrib/lite/profiling/profiler.h
#ifndef TENSORFLOW_CONTRIB_LITE_PROFILING_PROFILER_H_
#define TENSORFLOW_CONTRIB_LITE_PROFILING_PROFILER_H_

#include <vector>

#include "profiling/profile_buffer.h"

#ifdef TFLITE_PROFILING_ENABLED

namespace tflite {
namespace profiling {
class ScopedProfile;
class ScopedOperatorProfile;

// Controls whether profiling is enabled or disabled and collects profiles.
// TFLite is used on platforms that don't have posix threads, so the profiler is
// kept as simple as possible. It is designed to be used only on a single
// thread.
//
// Profiles are collected using Scoped*Profile objects that begin and end a
// profile event.
// An example usage is shown in the example below:
//
// Say Worker class has a DoWork method and we are interested in profiling
// the overall execution time for DoWork and time spent in Task1 and Task2
// functions.
//
// class Worker {
//  public:
//   void DoWork() {
//    ScopedProfile(&controller, "DoWork");
//    Task1();
//    Task2();
//    .....
//   }
//
//   void Task1() {
//    ScopedProfile(&controller, "Task1");
//    ....
//   }
//
//   void Task2() {
//    ScopedProfile(&controller, "Task2");
//   }
//
//    Profiler profiler;
// }
//
// We instrument the functions that need to be profiled.
//
// Profile can be collected by enable profiling and then getting profile
// events.
//
//  void ProfileWorker() {
//    Worker worker;
//    worker.profiler.EnableProfiling();
//    worker.DoWork();
//    worker.profiler.DisableProfiling();
//    // Profiling is complete, extract profiles.
//    auto profile_events = worker.profiler.GetProfiles();
//  }
//
//
class Profiler {
 public:
  Profiler() : buffer_(1024, false) {}

  void StartProfiling() { buffer_.SetEnabled(true); }
  void StopProfiling() { buffer_.SetEnabled(false); }
  void Reset() { buffer_.Reset(); }
  std::vector<const ProfileEvent*> GetProfileEvents() {
    std::vector<const ProfileEvent*> profile_events;
    profile_events.reserve(buffer_.Size());
    for (size_t i = 0; i < buffer_.Size(); i++) {
      profile_events.push_back(buffer_.At(i));
    }
    return profile_events;
  }

 private:
  friend class ScopedProfile;
  friend class ScopedOperatorProfile;
  ProfileBuffer* GetProfileBuffer() { return &buffer_; }
  ProfileBuffer buffer_;
};

class ScopedProfile {
 public:
  // Adds a profile event to profile that begins with the construction
  // of object and ends when the object goes out of scope.
  // The lifetime of tag should be at least the lifetime of profiler.

  ScopedProfile(Profiler* profiler, const char* tag)
      : buffer_(nullptr), event_handle_(0) {
    if (profiler) {
      buffer_ = profiler->GetProfileBuffer();
      event_handle_ =
          buffer_->BeginEvent(tag, ProfileEvent::EventType::DEFAULT, 0);
    }
  }
  ~ScopedProfile() {
    if (buffer_) {
      buffer_->EndEvent(event_handle_);
    }
  }

 private:
  ProfileBuffer* buffer_;
  int32_t event_handle_;
};

class ScopedOperatorProfile {
 public:
  // Adds a profile event to profile that begins with the construction
  // of object and ends when the object goes out of scope.
  // The lifetime of tag should be at least the lifetime of profiler.
  ScopedOperatorProfile(Profiler* profiler, const char* tag, int node_index)
      : buffer_(nullptr), event_handle_(0) {
    if (profiler) {
      buffer_ = profiler->GetProfileBuffer();
      event_handle_ = buffer_->BeginEvent(
          tag, ProfileEvent::EventType::OPERATOR_INVOKE_EVENT, node_index);
    }
  }

  ~ScopedOperatorProfile() {
    if (buffer_) {
      buffer_->EndEvent(event_handle_);
    }
  }

 private:
  ProfileBuffer* buffer_;
  int32_t event_handle_;
};

}  // namespace profiling
}  // namespace tflite

#define VARNAME_UNIQ(name, ctr) name##ctr

#define SCOPED_OPERATOR_PROFILE(profiler, node_index)    \
  tflite::profiling::ScopedOperatorProfile VARNAME_UNIQ( \
      _profile_, __COUNTER__)((profiler), "OpInvoke", (node_index))
#else

namespace tflite {
namespace profiling {
// A noop version of profiler when profiling is disabled.
class Profiler {
 public:
  Profiler() {}
  void StartProfiling() {}
  void StopProfiling() {}
  void Reset() {}
  std::vector<const ProfileEvent*> GetProfileEvents() { return {}; }
};
}  // namespace profiling
}  // namespace tflite

#define SCOPED_OPERATOR_PROFILE(profiler, node_index)

#endif  // TFLITE_PROFILING_ENABLED

#endif  // TENSORFLOW_CONTRIB_LITE_PROFILING_PROFILER_H_

// clang-format on