Skip to content

Commit

Permalink
Add the statistic function in callprofiler
Browse files Browse the repository at this point in the history
Add detailed report function in RegistryEntry

Write the pytest for statistic function
  • Loading branch information
ThreeMonth03 authored and threemonth03 committed Jul 8, 2024
1 parent ba1e4cb commit 4b0c17a
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 39 deletions.
1 change: 1 addition & 0 deletions cpp/modmesh/toggle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(MODMESH_TOGGLE_HEADERS
CACHE FILEPATH "" FORCE)

set(MODMESH_TOGGLE_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/profile.cpp
${CMAKE_CURRENT_SOURCE_DIR}/toggle.cpp
${CMAKE_CURRENT_SOURCE_DIR}/RadixTree.cpp
CACHE FILEPATH "" FORCE)
Expand Down
44 changes: 44 additions & 0 deletions cpp/modmesh/toggle/RadixTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

#include <modmesh/toggle/RadixTree.hpp>
#include <modmesh/toggle/profile.hpp>

namespace modmesh
{
Expand Down Expand Up @@ -72,6 +73,49 @@ void CallProfiler::print_profiling_result(const RadixTreeNode<CallerProfile> & n
}
}

void CallProfiler::print_statistics(const RadixTreeNode<CallerProfile> & node, std::ostream & outstream)
{
TimeRegistry::me().clear();

std::queue<const RadixTreeNode<CallerProfile> *> nodes_buffer;
for (const auto & child : node.children())
{
nodes_buffer.push(child.get());
}

// BFS algorithm and put the node information into TimeRegistry.
while (!nodes_buffer.empty())
{
const int nodes_buffer_size = nodes_buffer.size();
for (int i = 0; i < nodes_buffer_size; ++i)
{
const RadixTreeNode<CallerProfile> * current_node = nodes_buffer.front();
nodes_buffer.pop();
TimeRegistry::me().add(
current_node->data().caller_name,
current_node->data().total_time.count() / 1e9,
current_node->data().total_time.count() / 1e9,
current_node->data().call_count);

for (const auto & child : current_node->children())
{
nodes_buffer.push(child.get());
TimeRegistry::me().add(
current_node->data().caller_name,
0,
-child->data().total_time.count() / 1e9,
0);
}
}
}

// Print the statistics.
outstream << TimeRegistry::me().detailed_report();

// Reset the TimeRegistry.
TimeRegistry::me().clear();
}

void CallProfilerSerializer::serialize_call_profiler(const CallProfiler & profiler, std::ostream & outstream)
{
// Serialize the RadixTree in the CallProfiler.
Expand Down
7 changes: 7 additions & 0 deletions cpp/modmesh/toggle/RadixTree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ class CallProfiler
print_profiling_result(*(m_radix_tree.get_current_node()), 0, outstream);
}

// Print the statistics of the profiling result
void print_statistics(std::ostream & outstream) const
{
print_statistics(*(m_radix_tree.get_current_node()), outstream);
}

/// Reset the profiler
void reset();

Expand All @@ -291,6 +297,7 @@ class CallProfiler

private:
void print_profiling_result(const RadixTreeNode<CallerProfile> & node, const int depth, std::ostream & outstream) const;
static void print_statistics(const RadixTreeNode<CallerProfile> & node, std::ostream & outstream);

private:
RadixTree<CallerProfile> m_radix_tree; /// the data structure of the callers
Expand Down
69 changes: 69 additions & 0 deletions cpp/modmesh/toggle/profile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023, Yung-Yu Chen <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <modmesh/toggle/profile.hpp>

namespace modmesh
{

std::string TimeRegistry::detailed_report() const
{
std::ostringstream ostm;
/// Header
ostm
<< std::setw(40) << total_call_count()
<< " function calls in " << total_time()
<< " seconds" << std::endl;
ostm
<< std::endl
<< std::setw(40) << "Function Name"
<< std::setw(25) << "Call Count"
<< std::setw(25) << "Total Time (s)"
<< std::setw(25) << "Per Call (s)"
<< std::setw(25) << "Cumulative Time (s)"
<< std::setw(25) << "Per Call (s)"
<< std::endl;

/// Body
for (auto it = m_entry.begin(); it != m_entry.end(); ++it)
{
ostm
<< std::setw(40) << it->first
<< std::setw(25) << it->second.count()
<< std::setw(25) << it->second.time()
<< std::setw(25) << it->second.time() / it->second.count()
<< std::setw(25) << it->second.ctime()
<< std::setw(25) << it->second.ctime() / it->second.count()
<< std::endl;
}
return ostm.str();
}

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
39 changes: 36 additions & 3 deletions cpp/modmesh/toggle/profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/

#include <modmesh/base.hpp>

#include <iomanip>
#include <vector>
#include <string>
#include <chrono>
Expand Down Expand Up @@ -106,6 +106,7 @@ class TimedEntry

size_t count() const { return m_count; }
double time() const { return m_time; }
double ctime() const { return m_ctime; }

double start() { return m_sw.lap(); }
double stop()
Expand All @@ -122,10 +123,19 @@ class TimedEntry
return *this;
}

TimedEntry & add_time_and_count(double ttime, double ctime, long long count)
{
m_count += count;
m_time += ttime;
m_ctime += ctime;
return *this;
}

private:

size_t m_count = 0;
double m_time = 0.0;
double m_ctime = 0.0;
StopWatch m_sw;

}; /* end class TimedEntry */
Expand Down Expand Up @@ -166,6 +176,21 @@ class TimeRegistry
add(std::string(name), time);
}

void add(std::string const & name, double ttime, double ctime, long long count)
{
entry(name).add_time_and_count(ttime, ctime, count);
m_total_call_count += count;
m_total_time += ctime;
}

void add(const char * name, double ttime, double ctime, long long count)
{
add(std::string(name), ttime, ctime, count);
}

double total_time() const { return m_total_time; }
long long total_call_count() const { return m_total_call_count; }

std::vector<std::string> names() const
{
std::vector<std::string> ret;
Expand All @@ -186,7 +211,12 @@ class TimeRegistry
return it->second;
}

void clear() { m_entry.clear(); }
void clear()
{
m_entry.clear();
m_total_call_count = 0;
m_total_time = 0;
}

TimeRegistry(TimeRegistry const &) = delete;
TimeRegistry(TimeRegistry &&) = delete;
Expand All @@ -199,10 +229,13 @@ class TimeRegistry
// std::cout << report();
}

std::string detailed_report() const;

private:

long long m_total_call_count = 0;
double m_total_time = 0;
TimeRegistry() = default;

std::map<std::string, TimedEntry> m_entry;

}; /* end struct TimeRegistry */
Expand Down
90 changes: 54 additions & 36 deletions cpp/modmesh/toggle/pymod/wrap_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapCallProfiler : public WrapBase<WrapC
{
public:
friend root_base_type;
static pybind11::dict result(CallProfiler & profiler);

protected:
WrapCallProfiler(pybind11::module & mod, char const * pyname, char const * pydoc)
Expand All @@ -181,48 +182,65 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapCallProfiler : public WrapBase<WrapC
"instance",
[](py::object const &) -> wrapped_type &
{ return wrapped_type::instance(); })
.def("result", [](CallProfiler & profiler)
{
const RadixTreeNode<CallerProfile> * root = profiler.radix_tree().get_root();
if (root->empty_children()) {
return py::dict();
}
py::dict result;
std::queue<const RadixTreeNode<CallerProfile>*> node_queue;
std::unordered_map<const RadixTreeNode<CallerProfile>*, py::dict> dict_storage;

node_queue.push(root);
dict_storage[root] = result;

while (!node_queue.empty()) {
const RadixTreeNode<CallerProfile>* cur_node = node_queue.front();
const py::dict& current_dict = dict_storage[cur_node];
node_queue.pop();

current_dict["name"] = cur_node->name();
current_dict["total_time"] = cur_node->data().total_time.count() / 1e6;
current_dict["count"] = cur_node->data().call_count;
if (cur_node == profiler.radix_tree().get_current_node()){
current_dict["current_node"] = true;
}

py::list children_list;
for (const auto& child : cur_node->children()) {
dict_storage[child.get()] = py::dict();
py::dict& child_dict = dict_storage[child.get()];
children_list.append(child_dict);
node_queue.push(child.get());
}
current_dict["children"] = children_list;
}
return result; })
.def(
"stat",
[](CallProfiler & profiler)
{
std::stringstream ss;
profiler.print_statistics(ss);
return ss.str();
})
.def("result", &result)
.def("reset", &wrapped_type::reset);
;

mod.attr("call_profiler") = mod.attr("CallProfiler").attr("instance");
}
}; /* end class WrapCallProfiler */

pybind11::dict WrapCallProfiler::result(CallProfiler & profiler)
{
namespace py = pybind11;

const RadixTreeNode<CallerProfile> * root = profiler.radix_tree().get_root();
if (root->empty_children())
{
return {};
}
py::dict result;
std::queue<const RadixTreeNode<CallerProfile> *> node_queue;
std::unordered_map<const RadixTreeNode<CallerProfile> *, py::dict> dict_storage;

node_queue.push(root);
dict_storage[root] = result;

while (!node_queue.empty())
{
const RadixTreeNode<CallerProfile> * cur_node = node_queue.front();
const py::dict & current_dict = dict_storage[cur_node];
node_queue.pop();

current_dict["name"] = cur_node->name();
current_dict["total_time"] = cur_node->data().total_time.count() / 1e6;
current_dict["count"] = cur_node->data().call_count;
if (cur_node == profiler.radix_tree().get_current_node())
{
current_dict["current_node"] = true;
}

py::list children_list;
for (const auto & child : cur_node->children())
{
dict_storage[child.get()] = py::dict();
py::dict & child_dict = dict_storage[child.get()];
children_list.append(child_dict);
node_queue.push(child.get());
}
current_dict["children"] = children_list;
}
return result;
}

class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapCallProfilerProbe : public WrapBase<WrapCallProfilerProbe, CallProfilerProbe>
{
public:
Expand Down Expand Up @@ -260,4 +278,4 @@ void wrap_profile(pybind11::module & mod)

} /* end namespace modmesh */

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
Loading

0 comments on commit 4b0c17a

Please sign in to comment.