From 477b057424ae596ad02df72a088d2c6b9ff7d624 Mon Sep 17 00:00:00 2001 From: Stepan Fomenko Date: Thu, 17 Sep 2020 17:34:54 +0300 Subject: [PATCH] Fix for SHSTK --- can | 2 +- experiments/can/CMakeLists.txt | 7 ++- experiments/can/asyncdevicepool.cpp | 80 +++++++++++++++++++++++++++++ experiments/pip/CMakeLists.txt | 9 ++-- experiments/sm/CMakeLists.txt | 6 +-- 5 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 experiments/can/asyncdevicepool.cpp diff --git a/can b/can index d71e584..38d6d57 160000 --- a/can +++ b/can @@ -1 +1 @@ -Subproject commit d71e58468ec14085adec43aeafccff53b561a0c0 +Subproject commit 38d6d57271ce5e2030d15a833e0c63d3ea6cbb0b diff --git a/experiments/can/CMakeLists.txt b/experiments/can/CMakeLists.txt index f48d6d2..e2ee214 100644 --- a/experiments/can/CMakeLists.txt +++ b/experiments/can/CMakeLists.txt @@ -3,18 +3,21 @@ cmake_policy(SET CMP0020 NEW) find_package(PIP REQUIRED) +add_executable(asyncdevicepool asyncdevicepool.cpp) +target_link_libraries(asyncdevicepool PIP can) + if(WIN32) add_custom_target(copy_dependencies COMMAND ${CMAKE_COMMAND} -E copy ${PCAN_LIB} ${CMAKE_CURRENT_BINARY_DIR}/PCANBasic${CMAKE_SHARED_LIBRARY_SUFFIX} COMMAND ${CMAKE_COMMAND} -E copy ${VSCAN_LIB} ${CMAKE_CURRENT_BINARY_DIR}/vs_can_api${CMAKE_SHARED_LIBRARY_SUFFIX}) + add_dependencies(asyncdevicepool copy_dependencies) + add_executable(can_send_multithread can_send_multithread.cpp) - target_include_directories(can_send_multithread PUBLIC ${PIP_INCLUDES} ${CAN_INCLUDES}) target_link_libraries(can_send_multithread can) add_dependencies(can_send_multithread copy_dependencies) add_executable(can_send can_send.cpp) - target_include_directories(can_send PUBLIC ${PIP_INCLUDES} ${CAN_INCLUDES}) target_link_libraries(can_send can) add_dependencies(can_send copy_dependencies) endif() \ No newline at end of file diff --git a/experiments/can/asyncdevicepool.cpp b/experiments/can/asyncdevicepool.cpp new file mode 100644 index 0000000..2a59ad2 --- /dev/null +++ b/experiments/can/asyncdevicepool.cpp @@ -0,0 +1,80 @@ +#include + +using namespace std::chrono; + +class FakeDevice : public CANDevice { +PIOBJECT_SUBCLASS(FakeDevice, CANDevice) +public: + double duration_ms; + + explicit FakeDevice(int counter) : duration_ms(0.0), counter(counter) {} + + bool send(const CAN_Raw &m) override { + counter--; + if (counter <= 0) { + if (counter == 0) duration_ms = (duration_cast(high_resolution_clock::now().time_since_epoch()) - start_time).count(); + counter = -1; + throw can::error(0, can_category); + } + return true; + } + + bool open() override { + start_time = duration_cast(high_resolution_clock::now().time_since_epoch()); + return true; + } + + void close() override { + if (read_handle != can::event::invalid_handle) can::event::close(read_handle); + } + + can::event::handle getReadEventHandle() override { + if (read_handle == can::event::invalid_handle) { + read_handle = can::event::make(); + } + return read_handle; + } + +protected: + bool nextMsg(CAN_Raw &rawMsg) override { + return false; + } + +private: + int counter = 0; + milliseconds start_time; +}; + +int main() { + int device_count = 63; + PIBlockingQueue wait_queue(device_count); + + AsyncDevicePool asyncDevicePool; + asyncDevicePool.set_can_error_handler([&wait_queue](const can::error& error, CANDevice* device) { +// can::stdout_error_handler(error, device); + wait_queue.put(dynamic_cast(device)->duration_ms); + return false; + }); + + int it_count = 1'000'000; + PIVector devices; + for (int i = 0; i < device_count; ++i) { + devices << new FakeDevice(it_count / device_count); + asyncDevicePool.add(devices.back()); + } + + for (int i = 0; i < it_count; ++i) { + CAN_Raw msg = { .Id = 0x11, .Size = 4, .Data = { 0x0, 0x1, 0x2, 0x3 } }; + asyncDevicePool.send(devices[i % device_count], msg); + if (i % 1000 == 999) { + std::this_thread::yield(); +// std::cout << i / 1000 << "/" << counter / 1000 << std::endl; +// std::this_thread::sleep_for(milliseconds(1)); + } + } + + double duration_ms; + for (int i = 0; i < device_count; ++i) duration_ms = wait_queue.take(); + std::cout << duration_ms << " ms" << std::endl; + return 0; +} \ No newline at end of file diff --git a/experiments/pip/CMakeLists.txt b/experiments/pip/CMakeLists.txt index 382cc8c..ce4f96b 100644 --- a/experiments/pip/CMakeLists.txt +++ b/experiments/pip/CMakeLists.txt @@ -4,13 +4,10 @@ cmake_policy(SET CMP0020 NEW) find_package(PIP REQUIRED) add_executable(mutex mutex.cpp) -target_include_directories(mutex PUBLIC ${PIP_INCLUDES}) -target_link_libraries(mutex ${PIP_LIBRARY} ${PIP_CONCURRENT_LIBRARY}) +target_link_libraries(mutex PIP) add_executable(mutex_multithread mutex_multithread.cpp) -target_include_directories(mutex_multithread PUBLIC ${PIP_INCLUDES}) -target_link_libraries(mutex_multithread ${PIP_LIBRARY} ${PIP_CONCURRENT_LIBRARY}) +target_link_libraries(mutex_multithread PIP) add_executable(vectors vectors.cpp) -target_include_directories(vectors PUBLIC ${PIP_INCLUDES}) -target_link_libraries(vectors ${PIP_LIBRARY}) \ No newline at end of file +target_link_libraries(vectors PIP) \ No newline at end of file diff --git a/experiments/sm/CMakeLists.txt b/experiments/sm/CMakeLists.txt index b621ea9..5077c87 100644 --- a/experiments/sm/CMakeLists.txt +++ b/experiments/sm/CMakeLists.txt @@ -4,9 +4,7 @@ cmake_policy(SET CMP0020 NEW) find_package(SM REQUIRED) add_executable(block_choice block_choice.cpp) -target_include_directories(block_choice PUBLIC ${SMBRICKS_INCLUDES} ${PIP_INCLUDES}) -target_link_libraries(block_choice ${PIP_LIBRARY}) +target_link_libraries(block_choice PIP) add_executable(smbusdata_crash_test smbusdata_crash_test.cpp) -target_include_directories(smbusdata_crash_test PUBLIC ${SMBRICKS_INCLUDES} ${PIP_INCLUDES}) -target_link_libraries(smbusdata_crash_test SMBricks_shared ${PIP_LIBRARY} ${PIP_CRYPT_LIBRARY}) +target_link_libraries(smbusdata_crash_test SMBricks_shared PIP::Crypt)