diff --git a/paddle/fluid/framework/new_executor/workqueue/CMakeLists.txt b/paddle/fluid/framework/new_executor/workqueue/CMakeLists.txt index 77130102d52e5dc3637e68958a6bf9de7ef0646d..f47a274aaa4e5264b5a5530e7ce10c8a053f386d 100644 --- a/paddle/fluid/framework/new_executor/workqueue/CMakeLists.txt +++ b/paddle/fluid/framework/new_executor/workqueue/CMakeLists.txt @@ -1,2 +1,3 @@ -cc_library(workqueue SRCS workqueue.cc workqueue_utils.cc events_waiter.cc DEPS enforce glog) +cc_library(workqueue_utils SRCS workqueue_utils.cc events_waiter.cc DEPS enforce glog) +cc_library(workqueue SRCS workqueue.cc DEPS workqueue_utils enforce glog) cc_test(workqueue_test SRCS workqueue_test.cc DEPS workqueue) diff --git a/paddle/fluid/framework/new_executor/workqueue/workqueue.cc b/paddle/fluid/framework/new_executor/workqueue/workqueue.cc index 3f06f3db23118a09b47c267dff995a8f43968292..45694349168a4e675f39f1a3693b3422a47580c7 100644 --- a/paddle/fluid/framework/new_executor/workqueue/workqueue.cc +++ b/paddle/fluid/framework/new_executor/workqueue/workqueue.cc @@ -198,7 +198,7 @@ std::unique_ptr CreateMultiThreadedWorkQueue( "WorkQueueOptions.num_threads must be " "greater than 1.")); std::unique_ptr ptr(new WorkQueueImpl(options)); - return std::move(ptr); + return ptr; } std::unique_ptr CreateWorkQueueGroup( @@ -208,7 +208,7 @@ std::unique_ptr CreateWorkQueueGroup( "For a WorkQueueGroup, the number of WorkQueueOptions " "must be greater than 1.")); std::unique_ptr ptr(new WorkQueueGroupImpl(queues_options)); - return std::move(ptr); + return ptr; } } // namespace framework diff --git a/paddle/fluid/platform/CMakeLists.txt b/paddle/fluid/platform/CMakeLists.txt index 1031d1ed6357df3962fc5827acfcd73daa5bd0e3..8a84429987d9003531d5b8bd1b6b6a72dc0a851a 100644 --- a/paddle/fluid/platform/CMakeLists.txt +++ b/paddle/fluid/platform/CMakeLists.txt @@ -169,7 +169,8 @@ cc_test(timer_test SRCS timer_test.cc DEPS timer) cc_library(lodtensor_printer SRCS lodtensor_printer.cc DEPS ddim place tensor scope lod_tensor variable_helper framework_proto) cc_test(lodtensor_printer_test SRCS lodtensor_printer_test.cc DEPS lodtensor_printer) -cc_library(host_event_recorder SRCS host_event_recorder.cc DEPS os_info) +add_subdirectory(profiler) + cc_library(device_tracer SRCS device_tracer.cc DEPS boost profiler_proto framework_proto ${GPU_CTX_DEPS}) if(WITH_GPU) nv_library(profiler SRCS profiler.cc profiler.cu DEPS host_event_recorder os_info device_tracer gpu_info enforce dynload_cuda) diff --git a/paddle/fluid/platform/event.h b/paddle/fluid/platform/event.h index 919266575e6ce7128958c144cd7d616a05590ff5..da5080cc86f0c2eadf2a91083962855d92348e11 100644 --- a/paddle/fluid/platform/event.h +++ b/paddle/fluid/platform/event.h @@ -201,39 +201,5 @@ class CudaEvent { #endif }; -struct CommonEvent { - public: - CommonEvent(const char *name, uint64_t start_ns, uint64_t end_ns, - EventRole role) - : name(name), start_ns(start_ns), end_ns(end_ns), role(role) {} - - CommonEvent(std::function &arena_allocator, - const std::string &name_str, uint64_t start_ns, uint64_t end_ns, - EventRole role, const std::string &attr_str) - : start_ns(start_ns), end_ns(end_ns), role(role) { - auto buf = static_cast(arena_allocator(name_str.length() + 1)); - strncpy(buf, name_str.c_str(), name_str.length() + 1); - name = buf; - buf = static_cast(arena_allocator(attr_str.length() + 1)); - strncpy(buf, attr_str.c_str(), attr_str.length() + 1); - attr = buf; - } - - CommonEvent(const std::function &arena_allocator, - const std::string &name_str, uint64_t start_ns, uint64_t end_ns, - EventRole role) - : start_ns(start_ns), end_ns(end_ns), role(role) { - auto buf = static_cast(arena_allocator(name_str.length() + 1)); - strncpy(buf, name_str.c_str(), name_str.length() + 1); - name = buf; - } - - const char *name = nullptr; // not owned, designed for performance - uint64_t start_ns = 0; - uint64_t end_ns = 0; - EventRole role = EventRole::kOrdinary; - const char *attr = nullptr; // not owned, designed for performance -}; - } // namespace platform } // namespace paddle diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index eaa77273c8fd4cd1b6f7038349d2b7027d988597..c4beac93ef1349e2b6fcb36c4487a5cfe161fc52 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -20,8 +20,8 @@ limitations under the License. */ #include "paddle/fluid/platform/device_tracer.h" #include "paddle/fluid/platform/enforce.h" -#include "paddle/fluid/platform/host_event_recorder.h" #include "paddle/fluid/platform/profiler.h" +#include "paddle/fluid/platform/profiler/host_event_recorder.h" #include "paddle/fluid/platform/profiler_helper.h" #ifdef PADDLE_WITH_CUDA #include "paddle/fluid/platform/dynload/nvtx.h" diff --git a/paddle/fluid/platform/profiler.h b/paddle/fluid/platform/profiler.h index 41cc3805f44daa78f900ab944be6a976fa1c8cc5..122e19b7c28080c3c7b38ba90cd8d5c3d61e6ecf 100644 --- a/paddle/fluid/platform/profiler.h +++ b/paddle/fluid/platform/profiler.h @@ -27,9 +27,9 @@ limitations under the License. */ #include "paddle/fluid/framework/type_defs.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/event.h" -#include "paddle/fluid/platform/event_tracing.h" #include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/profiler.pb.h" +#include "paddle/fluid/platform/profiler/event_tracing.h" #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) #include "paddle/fluid/platform/device/gpu/gpu_info.h" #endif diff --git a/paddle/fluid/platform/profiler/CMakeLists.txt b/paddle/fluid/platform/profiler/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..de22183df603475631454664b017d4a598328174 --- /dev/null +++ b/paddle/fluid/platform/profiler/CMakeLists.txt @@ -0,0 +1 @@ +cc_library(host_event_recorder SRCS host_event_recorder.cc DEPS os_info) diff --git a/paddle/fluid/platform/event_tracing.h b/paddle/fluid/platform/profiler/event_tracing.h similarity index 100% rename from paddle/fluid/platform/event_tracing.h rename to paddle/fluid/platform/profiler/event_tracing.h diff --git a/paddle/fluid/platform/host_event_recorder.cc b/paddle/fluid/platform/profiler/host_event_recorder.cc similarity index 93% rename from paddle/fluid/platform/host_event_recorder.cc rename to paddle/fluid/platform/profiler/host_event_recorder.cc index 750f39118d7d99d17dd1d16b7552469b861a4be9..14054418c5d248b87010ff0b143320ae2b25a5e4 100644 --- a/paddle/fluid/platform/host_event_recorder.cc +++ b/paddle/fluid/platform/profiler/host_event_recorder.cc @@ -9,7 +9,7 @@ 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. */ -#include "paddle/fluid/platform/host_event_recorder.h" +#include "paddle/fluid/platform/profiler/host_event_recorder.h" #include "paddle/fluid/platform/os_info.h" namespace paddle { @@ -26,7 +26,7 @@ HostEventSection HostEventRecorder::GatherEvents() { for (auto &kv : thread_recorders_) { host_sec.thr_sections.emplace_back(std::move(kv.second->GatherEvents())); } - return std::move(host_sec); + return host_sec; } } // namespace platform diff --git a/paddle/fluid/platform/host_event_recorder.h b/paddle/fluid/platform/profiler/host_event_recorder.h similarity index 84% rename from paddle/fluid/platform/host_event_recorder.h rename to paddle/fluid/platform/profiler/host_event_recorder.h index e8dd59ad4c6f1b31b84ab5d618ab8465c3bd2c1c..071f0d65bd0a638452cb1b5ba8b539202f7939da 100644 --- a/paddle/fluid/platform/host_event_recorder.h +++ b/paddle/fluid/platform/profiler/host_event_recorder.h @@ -25,6 +25,40 @@ limitations under the License. */ namespace paddle { namespace platform { +struct CommonEvent { + public: + CommonEvent(const char *name, uint64_t start_ns, uint64_t end_ns, + EventRole role) + : name(name), start_ns(start_ns), end_ns(end_ns), role(role) {} + + CommonEvent(std::function &arena_allocator, + const std::string &name_str, uint64_t start_ns, uint64_t end_ns, + EventRole role, const std::string &attr_str) + : start_ns(start_ns), end_ns(end_ns), role(role) { + auto buf = static_cast(arena_allocator(name_str.length() + 1)); + strncpy(buf, name_str.c_str(), name_str.length() + 1); + name = buf; + buf = static_cast(arena_allocator(attr_str.length() + 1)); + strncpy(buf, attr_str.c_str(), attr_str.length() + 1); + attr = buf; + } + + CommonEvent(const std::function &arena_allocator, + const std::string &name_str, uint64_t start_ns, uint64_t end_ns, + EventRole role) + : start_ns(start_ns), end_ns(end_ns), role(role) { + auto buf = static_cast(arena_allocator(name_str.length() + 1)); + strncpy(buf, name_str.c_str(), name_str.length() + 1); + name = buf; + } + + const char *name = nullptr; // not owned, designed for performance + uint64_t start_ns = 0; + uint64_t end_ns = 0; + EventRole role = EventRole::kOrdinary; + const char *attr = nullptr; // not owned, designed for performance +}; + template struct ContainsStdString : std::conditional_t< @@ -154,7 +188,7 @@ std::vector EventContainer::Reduce() { cur = next; } event_blocks_ = cur_event_block_ = new EventBlock; - return std::move(all_events); + return all_events; } template @@ -204,7 +238,7 @@ class ThreadEventRecorder { thr_sec.thread_name = thread_name_; thr_sec.thread_id = thread_id_; thr_sec.events = std::move(base_evt_cntr_.Reduce()); - return std::move(thr_sec); + return thr_sec; } private: diff --git a/paddle/fluid/platform/profiler/trace_event_collector.h b/paddle/fluid/platform/profiler/trace_event_collector.h new file mode 100644 index 0000000000000000000000000000000000000000..eabafb73542dc1c5c3ae45bd34c155d51757e0dc --- /dev/null +++ b/paddle/fluid/platform/profiler/trace_event_collector.h @@ -0,0 +1,65 @@ +/* Copyright (c) 2022 PaddlePaddle 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. */ + +#pragma once + +#include + +namespace paddle { +namespace platform { + +struct HostRecord { + std::string name; + uint64_t start_ns; + uint64_t end_ns; + uint64_t process_id; + uint64_t thread_id; +}; + +struct RuntimeRecord { + std::string name; + uint64_t start_ns; + uint64_t end_ns; + uint64_t process_id; + uint64_t thread_id; + uint32_t correlation_id; +}; + +struct DeviceRecord { + std::string name; + uint64_t start_ns; + uint64_t end_ns; + uint32_t correlation_id; +}; + +class TraceEventCollector { + public: + void AddHostRecord(HostRecord&& record) { host_records_.push_back(record); } + + void AddRuntimeRecord(RuntimeRecord&& record) { + runtime_records_.push_back(record); + } + + void AddDeviceRecord(DeviceRecord&& record) { + device_records_.push_back(record); + } + + private: + std::list host_records_; + std::list runtime_records_; + std::list device_records_; +}; + +} // namespace platform +} // namespace paddle diff --git a/paddle/fluid/platform/profiler/tracer_base.h b/paddle/fluid/platform/profiler/tracer_base.h new file mode 100644 index 0000000000000000000000000000000000000000..1d4e3447fe64e4b395d1e48056e59195dc7d15c5 --- /dev/null +++ b/paddle/fluid/platform/profiler/tracer_base.h @@ -0,0 +1,42 @@ +/* Copyright (c) 2022 PaddlePaddle 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. */ + +#pragma once + +#include "paddle/fluid/platform/profiler/trace_event_collector.h" + +namespace paddle { +namespace platform { + +class TracerBase { + public: + // The state machine for a Tracer. + enum class TracerState { UNINITED, READY, STARTED, STOPED }; + + virtual void PrepareTracing() { state_ = TracerState::READY; } + + virtual void StartTracing() = 0; + + virtual void StopTracing() = 0; + + virtual void CollectTraceData(TraceEventCollector* collector) = 0; + + virtual ~TracerBase() {} + + protected: + TracerState state_ = TracerState::UNINITED; +}; + +} // namespace platform +} // namespace paddle