forked from open-mmlab/mmdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Sdk profiler (open-mmlab#1274) * sdk-profiler * fix lint * support lift * sync net module when profile * use Scope* * update use task name * fix * use std::unique_ptr<Event> * remove mmdeploy::graph link for c and transform * fix * fix * fix * [Enhancement] refactor profiler (open-mmlab#1403) * reduce profile node name * add profiler for pipeline * add profiler for cond * udpate * fix total time (open-mmlab#1451)
- Loading branch information
Showing
19 changed files
with
4,217 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) OpenMMLab. All rights reserved. | ||
|
||
#include "mmdeploy/core/profiler.h" | ||
|
||
#include <iomanip> | ||
|
||
namespace mmdeploy { | ||
namespace profiler { | ||
|
||
Event* Scope::Add(Event::Type type, Index index, TimePoint time_point) { | ||
return profiler_->AddEvent({this, type, index, time_point}); | ||
} | ||
|
||
Scope* Scope::CreateScope(std::string_view name) { | ||
auto node = children_.emplace_back(profiler_->CreateScope(name)); | ||
node->parent_ = this; | ||
return node; | ||
} | ||
|
||
void Scope::Dump(Scope* scope, std::ofstream& ofs) { | ||
ofs << scope->name_ << " " << (void*)scope << " "; | ||
for (auto& child : scope->children_) { | ||
ofs << (void*)child << " "; | ||
} | ||
ofs << "\n"; | ||
for (const auto& child : scope->children_) { | ||
Dump(child, ofs); | ||
} | ||
} | ||
|
||
ScopedCounter::ScopedCounter(Scope* scope) { | ||
if (scope) { | ||
start_ = scope->Add(Event::kStart, scope->next_.fetch_add(1, std::memory_order_relaxed), | ||
Clock::now()); | ||
} | ||
} | ||
|
||
ScopedCounter::~ScopedCounter() { | ||
if (start_) { | ||
start_->scope->Add(Event::kEnd, start_->index, Clock::now()); | ||
} | ||
} | ||
|
||
Profiler::Profiler(std::string_view path) : path_(path) { root_ = CreateScope("."); } | ||
|
||
Scope* Profiler::CreateScope(std::string_view name) { | ||
auto& node = nodes_.emplace_back(); | ||
node.profiler_ = this; | ||
node.name_ = name; | ||
return &node; | ||
} | ||
|
||
Event* Profiler::AddEvent(Event e) { | ||
auto uptr = std::make_unique<Event>(e); | ||
Event* pe = uptr.get(); | ||
events_.enqueue(std::move(uptr)); | ||
return pe; | ||
} | ||
|
||
void Profiler::Release() { | ||
std::ofstream ofs(path_); | ||
root_->Dump(ofs); | ||
ofs << "----\n"; | ||
|
||
std::unique_ptr<Event> item; | ||
std::vector<std::unique_ptr<Event>> vec; | ||
while (events_.try_dequeue(item)) { | ||
vec.push_back(std::move(item)); | ||
} | ||
|
||
std::sort(vec.begin(), vec.end(), | ||
[](const std::unique_ptr<Event>& a, const std::unique_ptr<Event>& b) { | ||
return a->time_point < b->time_point; | ||
}); | ||
|
||
for (int i = 0; i < vec.size(); i++) { | ||
ofs << (void*)vec[i]->scope << " " << vec[i]->type << " " << vec[i]->index << " " | ||
<< std::chrono::duration_cast<std::chrono::microseconds>(vec[i]->time_point - | ||
vec[0]->time_point) | ||
.count() | ||
<< "\n"; | ||
} | ||
} | ||
|
||
} // namespace profiler | ||
|
||
} // namespace mmdeploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) OpenMMLab. All rights reserved. | ||
|
||
#ifndef MMDEPLOY_CSRC_MMDEPLOY_CORE_PROFILER_H_ | ||
#define MMDEPLOY_CSRC_MMDEPLOY_CORE_PROFILER_H_ | ||
|
||
#include <atomic> | ||
#include <chrono> | ||
#include <deque> | ||
#include <fstream> | ||
#include <iostream> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
#include "concurrentqueue.h" | ||
#include "mmdeploy/core/logger.h" | ||
#include "mmdeploy/core/macro.h" | ||
#include "mmdeploy/core/value.h" | ||
|
||
namespace mmdeploy { | ||
namespace profiler { | ||
|
||
struct Profiler; | ||
struct Scope; | ||
|
||
using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady, | ||
std::chrono::high_resolution_clock, std::chrono::steady_clock>; | ||
using TimePoint = Clock::time_point; | ||
using Index = uint64_t; | ||
|
||
struct Event { | ||
enum Type { kStart, kEnd }; | ||
Scope* scope; | ||
Type type; | ||
Index index; | ||
TimePoint time_point; | ||
}; | ||
|
||
struct MMDEPLOY_API Scope { | ||
Scope() = default; | ||
Scope(const Scope&) = delete; | ||
Scope(Scope&&) noexcept = delete; | ||
Scope& operator=(const Scope&) = delete; | ||
Scope& operator=(Scope&&) noexcept = delete; | ||
|
||
Event* Add(Event::Type type, Index index, TimePoint time_point); | ||
|
||
Scope* CreateScope(std::string_view name); | ||
|
||
void Dump(Scope* scope, std::ofstream& ofs); | ||
void Dump(std::ofstream& ofs) { Dump(this, ofs); } | ||
|
||
Profiler* profiler_{}; | ||
Scope* parent_{}; | ||
std::vector<Scope*> children_; | ||
std::atomic<Index> next_{}; | ||
std::string name_; | ||
}; | ||
|
||
struct MMDEPLOY_API ScopedCounter { | ||
explicit ScopedCounter(Scope* scope); | ||
~ScopedCounter(); | ||
|
||
Event* start_{}; | ||
}; | ||
|
||
struct MMDEPLOY_API Profiler { | ||
explicit Profiler(std::string_view path); | ||
Scope* CreateScope(std::string_view name); | ||
Event* AddEvent(Event e); | ||
Scope* scope() const noexcept { return root_; } | ||
void Release(); | ||
|
||
std::string path_; | ||
std::deque<Scope> nodes_; | ||
moodycamel::ConcurrentQueue<std::unique_ptr<Event>> events_; | ||
Scope* root_{}; | ||
}; | ||
|
||
} // namespace profiler | ||
|
||
MMDEPLOY_REGISTER_TYPE_ID(profiler::Scope*, 10); | ||
|
||
} // namespace mmdeploy | ||
|
||
#endif // MMDEPLOY_CSRC_MMDEPLOY_GRAPH_PROFILER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.