Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop zerocopy #1053

Merged
merged 32 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4a1f818
Added SharedMemory header
TheMutta Jun 17, 2024
c36ab3b
Finalized SharedMemory class
TheMutta Jun 18, 2024
cc44859
Added differentiation between SharedMemory and VectorMemory, added he…
TheMutta Jun 19, 2024
557abfd
Replaced -1 fd with 0 fd
TheMutta Jun 19, 2024
185186d
This reverts commit 557abfd0477a5217cec6cd095477e91d526dd0d4.
TheMutta Jun 19, 2024
c4ff206
Made fd and mapping in SharedMemory protected instead of private
TheMutta Jun 24, 2024
ad66010
XLinkStream read helper functions for handling FDs
TheMutta Jun 24, 2024
3c1d976
Added SharedMemory support with FD in Buffer data structure
TheMutta Jun 25, 2024
cf35d8c
Added FD support in ImgFrame data structure
TheMutta Jun 25, 2024
a5016b6
Parsing fd in StreamMessageParser
TheMutta Jun 25, 2024
5019cca
Updated XLink version.
TheMutta Jun 26, 2024
fd710a6
Closing FDs when destructing SharedMemory
TheMutta Jun 27, 2024
1effc78
Added size parameters to stream write
TheMutta Jun 27, 2024
88e86f8
Passing FDs by value
TheMutta Jun 28, 2024
0d4b57d
Resolved conflict by reverting size changes.
TheMutta Jun 28, 2024
979573a
Added new max throughput example to test the efficency of the transfe…
TheMutta Jul 1, 2024
242d6ec
Added new example in CMakeLists.txt
TheMutta Jul 1, 2024
316c073
Disabled openssh that caused issues when cross compiling to RVC4
TheMutta Jul 1, 2024
74441bd
Added X_LINK_TCP_IP_OR_LOCAL_SHDMEM
TheMutta Jul 4, 2024
587f9a2
Changes to removed unused constructor to SharedMemory and changes to …
TheMutta Jul 5, 2024
21e4649
Removed MemoryKinds
TheMutta Jul 5, 2024
bc17b50
Re-added empty constructor.
TheMutta Jul 5, 2024
d8b81e8
Added tcpshd enviroment variable
TheMutta Jul 8, 2024
2efe453
Setting back connection in deviceInfo from connectionHandler
TheMutta Jul 10, 2024
cb9ac60
Added memfd_create constructors.
TheMutta Jul 10, 2024
9c4f05c
Readded kinds.
TheMutta Jul 11, 2024
950dbd4
Added SharedMemory Fd writing in data queue
TheMutta Jul 11, 2024
48f664d
Updated XLink
TheMutta Jul 12, 2024
26fb39c
Clangformat
TheMutta Jul 12, 2024
f99c0d8
Removed example file
TheMutta Jul 12, 2024
4560bb3
reset OpenSSL compile flag
TheMutta Jul 12, 2024
1a249f3
Merge branch 'v3_develop' into v3_develop_zerocopy
TheMutta Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clangformat
  • Loading branch information
TheMutta committed Jul 12, 2024
commit 26fb39c9bc2978a7919e57abf8c19877abdfda3b
71 changes: 0 additions & 71 deletions examples/host_side/host_only_image.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this one before the merge.

This file was deleted.

2 changes: 1 addition & 1 deletion include/depthai/pipeline/ThreadedHostNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ThreadedHostNode : public ThreadedNode {
}
};

template<typename T>
template <typename T>
using CustomThreadedNode = NodeCRTP<ThreadedHostNode, T>;
} // namespace node
} // namespace dai
1 change: 0 additions & 1 deletion include/depthai/pipeline/datatype/Buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ class Buffer : public ADatatype {
* @param data Moves data to internal buffer
*/
void setData(std::vector<std::uint8_t>&& data);


/**
* Retrieves timestamp related to dai::Clock::now()
Expand Down
2 changes: 1 addition & 1 deletion include/depthai/pipeline/node/host/HostNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class HostNode : public ThreadedHostNode {
}
};

template<typename T>
template <typename T>
using CustomNode = NodeCRTP<HostNode, T>;
} // namespace node
} // namespace dai
2 changes: 1 addition & 1 deletion include/depthai/utility/Memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class Memory {
};

MemoryKinds getKind() {
return kind;
return kind;
}

protected:
Expand Down
99 changes: 50 additions & 49 deletions include/depthai/utility/SharedMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include <functional>
#include <iostream>
#ifdef __unix__
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#endif

// project
Expand All @@ -24,100 +24,101 @@ class SharedMemory : public Memory {
long fd = -1;
void* mapping;
void mapFd() {
if (fd < 0) {
/* Error handling here */
}
if(fd < 0) {
/* Error handling here */
}

mapping = mmap(NULL, getSize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mapping == NULL) {
/* Error handling here */
}
mapping = mmap(NULL, getSize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(mapping == NULL) {
/* Error handling here */
}
}
void unmapFd() {
if (mapping == NULL) {
return;
}
if(mapping == NULL) {
return;
}

munmap(mapping, getSize());
munmap(mapping, getSize());
}

public:
SharedMemory() {
kind = MemoryKinds::MEMORY_KIND_SHARED;
fd = -1;
kind = MemoryKinds::MEMORY_KIND_SHARED;
fd = -1;
}

SharedMemory(long argFd) : fd(argFd) {
kind = MemoryKinds::MEMORY_KIND_SHARED;
mapFd();
kind = MemoryKinds::MEMORY_KIND_SHARED;
mapFd();
}

SharedMemory(long argFd, std::size_t size) : fd(argFd) {
kind = MemoryKinds::MEMORY_KIND_SHARED;
setSize(size);
mapFd();
kind = MemoryKinds::MEMORY_KIND_SHARED;
setSize(size);
mapFd();
}

SharedMemory(const char *name) {
kind = MemoryKinds::MEMORY_KIND_SHARED;
fd = memfd_create(name, 0);
mapFd();
SharedMemory(const char* name) {
kind = MemoryKinds::MEMORY_KIND_SHARED;
fd = memfd_create(name, 0);
mapFd();
}

SharedMemory(const char *name, std::size_t size) {
kind = MemoryKinds::MEMORY_KIND_SHARED;
fd = memfd_create(name, 0);
SharedMemory(const char* name, std::size_t size) {
kind = MemoryKinds::MEMORY_KIND_SHARED;
fd = memfd_create(name, 0);

setSize(size);
mapFd();
setSize(size);
mapFd();
}

~SharedMemory() {
unmapFd();
close(fd);
unmapFd();
close(fd);
}

SharedMemory& operator=(long argFd) {
unmapFd();
fd = argFd;
mapFd();
unmapFd();
fd = argFd;
mapFd();

return *this;
}

span<std::uint8_t> getData() override {
if (mapping == NULL) {
mapFd();
}
if(mapping == NULL) {
mapFd();
}

return {(uint8_t*)mapping, getSize()};
}
span<const std::uint8_t> getData() const override {
return {(const uint8_t*)mapping, getSize()};
}
std::size_t getMaxSize() const override {
struct stat fileStats;
fstat(fd, &fileStats);
struct stat fileStats;
fstat(fd, &fileStats);

return fileStats.st_size;
}
std::size_t getOffset() const override {
return ftell(fdopen(fd, "r"));
}
void setSize(std::size_t size) override {
if (mapping != NULL) {
unmapFd();
}
if(mapping != NULL) {
unmapFd();
}

ftruncate(fd, size);
mapFd();
ftruncate(fd, size);
mapFd();
}

std::size_t getSize() const {
return getMaxSize();
return getMaxSize();
}

long getFd() const {
return fd;
return fd;
}
};

Expand Down
12 changes: 9 additions & 3 deletions include/depthai/utility/VectorMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ namespace dai {
class VectorMemory : public std::vector<std::uint8_t>, public Memory {
public:
// using std::vector<std::uint8_t>::vector;
VectorMemory() { kind = MemoryKinds::MEMORY_KIND_VECTOR; }
VectorMemory(const std::vector<std::uint8_t>& d) : vector(std::move(d)) { kind = MemoryKinds::MEMORY_KIND_VECTOR; }
VectorMemory(std::vector<std::uint8_t>&& d) : vector(std::move(d)) { kind = MemoryKinds::MEMORY_KIND_VECTOR; }
VectorMemory() {
kind = MemoryKinds::MEMORY_KIND_VECTOR;
}
VectorMemory(const std::vector<std::uint8_t>& d) : vector(std::move(d)) {
kind = MemoryKinds::MEMORY_KIND_VECTOR;
}
VectorMemory(std::vector<std::uint8_t>&& d) : vector(std::move(d)) {
kind = MemoryKinds::MEMORY_KIND_VECTOR;
}
VectorMemory& operator=(std::vector<std::uint8_t>&& d) {
std::vector<std::uint8_t>::operator=(std::move(d));
return *this;
Expand Down
30 changes: 15 additions & 15 deletions src/device/DataQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "depthai/pipeline/datatype/ADatatype.hpp"
#include "depthai/pipeline/datatype/DatatypeEnum.hpp"
#include "depthai/pipeline/datatype/MessageGroup.hpp"
#include "depthai/utility/SharedMemory.hpp"
#include "depthai/xlink/XLinkStream.hpp"
#include "pipeline/datatype/MessageGroup.hpp"
#include "pipeline/datatype/StreamMessageParser.hpp"
#include "depthai/utility/SharedMemory.hpp"

// shared
#include "depthai/xlink/XLinkConstants.hpp"
Expand Down Expand Up @@ -206,14 +206,14 @@ DataInputQueue::DataInputQueue(

// Blocking
auto t1 = steady_clock::now();
if(outgoing->data->getSize() > 0) {
if (outgoing->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
stream.write(outgoing->data->getData(), metadata);
} else if (outgoing->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
std::shared_ptr<SharedMemory> memory = std::dynamic_pointer_cast<SharedMemory>(outgoing->data);
stream.write(memory->getFd(), metadata);
}

if(outgoing->data->getSize() > 0) {
if(outgoing->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
stream.write(outgoing->data->getData(), metadata);
} else if(outgoing->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
std::shared_ptr<SharedMemory> memory = std::dynamic_pointer_cast<SharedMemory>(outgoing->data);
stream.write(memory->getFd(), metadata);
}
} else {
stream.write(metadata);
}
Expand All @@ -234,12 +234,12 @@ DataInputQueue::DataInputQueue(
logger::trace("Sending part of a group message: {}", msg.first);
auto metadata = StreamMessageParser::serializeMetadata(msg.second);
if(msg.second->data->getSize() > 0) {
if (msg.second->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
stream.write(msg.second->data->getData(), metadata);
} else if (msg.second->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
std::shared_ptr<SharedMemory> memory = std::dynamic_pointer_cast<SharedMemory>(msg.second->data);
stream.write(memory->getFd(), metadata);
}
if(msg.second->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
stream.write(msg.second->data->getData(), metadata);
} else if(msg.second->data->getKind() == dai::MemoryKinds::MEMORY_KIND_VECTOR) {
std::shared_ptr<SharedMemory> memory = std::dynamic_pointer_cast<SharedMemory>(msg.second->data);
stream.write(memory->getFd(), metadata);
}
} else {
stream.write(metadata);
}
Expand Down
5 changes: 2 additions & 3 deletions src/pipeline/datatype/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "depthai/pipeline/datatype/Buffer.hpp"

#include "depthai/utility/VectorMemory.hpp"
#include "depthai/utility/SharedMemory.hpp"
#include "depthai/utility/VectorMemory.hpp"

namespace dai {
Buffer::Buffer(size_t size) : Buffer() {
Expand All @@ -14,13 +14,12 @@ Buffer::Buffer(long fd) : Buffer() {
auto mem = std::make_shared<SharedMemory>(fd);
data = mem;
}

Buffer::Buffer(long fd, size_t size) : Buffer() {
auto mem = std::make_shared<SharedMemory>(fd, size);
data = mem;
}


span<uint8_t> Buffer::getData() {
return data->getData();
}
Expand Down
4 changes: 0 additions & 4 deletions src/pipeline/datatype/ImgFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "depthai/pipeline/datatype/ImgFrame.hpp"

#include "depthai/utility/SharedMemory.hpp"

#include "spdlog/fmt/fmt.h"
#include "spdlog/spdlog.h"
namespace dai {
Expand All @@ -21,16 +20,13 @@ ImgFrame::ImgFrame(size_t size) : ImgFrame() {
ImgFrame::ImgFrame(long fd) : ImgFrame() {
auto mem = std::make_shared<SharedMemory>(fd);
data = mem;

}

ImgFrame::ImgFrame(long fd, size_t size) : ImgFrame() {
auto mem = std::make_shared<SharedMemory>(fd, size);
data = mem;
}



std::chrono::time_point<std::chrono::steady_clock, std::chrono::steady_clock::duration> ImgFrame::getTimestamp(CameraExposureOffset offset) const {
auto ts = getTimestamp();
auto expTime = getExposureTime();
Expand Down
Loading