You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
3.5 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2022, Antonio SJ Musumeci <trapexit@spawn.link>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include "fuse_msgbuf.hpp"
  16. #include "fuse.h"
  17. #include <unistd.h>
  18. #include <atomic>
  19. #include <cstdint>
  20. #include <cstdlib>
  21. #include <mutex>
  22. #include <vector>
  23. static std::uint32_t g_PAGESIZE = 0;
  24. static std::uint32_t g_BUFSIZE = 0;
  25. static std::atomic<std::uint_fast64_t> g_MSGBUF_ALLOC_COUNT;
  26. static std::mutex g_MUTEX;
  27. static std::vector<fuse_msgbuf_t*> g_MSGBUF_STACK;
  28. static
  29. __attribute__((constructor))
  30. void
  31. msgbuf_constructor()
  32. {
  33. g_PAGESIZE = sysconf(_SC_PAGESIZE);
  34. // +2 because to do O_DIRECT we need to offset the buffer to align
  35. g_BUFSIZE = (g_PAGESIZE * (FUSE_MAX_MAX_PAGES + 2));
  36. }
  37. static
  38. __attribute__((destructor))
  39. void
  40. msgbuf_destroy()
  41. {
  42. }
  43. uint64_t
  44. msgbuf_get_bufsize()
  45. {
  46. return g_BUFSIZE;
  47. }
  48. void
  49. msgbuf_set_bufsize(const uint32_t size_in_pages_)
  50. {
  51. g_BUFSIZE = ((size_in_pages_ + 1) * g_PAGESIZE);
  52. }
  53. static
  54. void*
  55. page_aligned_malloc(const uint64_t size_)
  56. {
  57. int rv;
  58. void *buf = NULL;
  59. rv = posix_memalign(&buf,g_PAGESIZE,size_);
  60. if(rv != 0)
  61. return NULL;
  62. return buf;
  63. }
  64. fuse_msgbuf_t*
  65. msgbuf_alloc()
  66. {
  67. fuse_msgbuf_t *msgbuf;
  68. g_MUTEX.lock();
  69. if(g_MSGBUF_STACK.empty())
  70. {
  71. g_MUTEX.unlock();
  72. msgbuf = (fuse_msgbuf_t*)page_aligned_malloc(g_BUFSIZE);
  73. if(msgbuf == NULL)
  74. return NULL;
  75. msgbuf->mem = (((char*)msgbuf) + g_PAGESIZE);
  76. msgbuf->size = g_BUFSIZE - g_PAGESIZE;
  77. g_MSGBUF_ALLOC_COUNT++;
  78. }
  79. else
  80. {
  81. msgbuf = g_MSGBUF_STACK.back();
  82. g_MSGBUF_STACK.pop_back();
  83. g_MUTEX.unlock();
  84. }
  85. return msgbuf;
  86. }
  87. static
  88. void
  89. msgbuf_destroy(fuse_msgbuf_t *msgbuf_)
  90. {
  91. // free(msgbuf_->mem);
  92. free(msgbuf_);
  93. }
  94. void
  95. msgbuf_free(fuse_msgbuf_t *msgbuf_)
  96. {
  97. std::lock_guard<std::mutex> lck(g_MUTEX);
  98. if(msgbuf_->size != (g_BUFSIZE - g_PAGESIZE))
  99. {
  100. msgbuf_destroy(msgbuf_);
  101. g_MSGBUF_ALLOC_COUNT--;
  102. return;
  103. }
  104. g_MSGBUF_STACK.emplace_back(msgbuf_);
  105. }
  106. uint64_t
  107. msgbuf_alloc_count()
  108. {
  109. return g_MSGBUF_ALLOC_COUNT;
  110. }
  111. uint64_t
  112. msgbuf_avail_count()
  113. {
  114. std::lock_guard<std::mutex> lck(g_MUTEX);
  115. return g_MSGBUF_STACK.size();
  116. }
  117. void
  118. msgbuf_gc_10percent()
  119. {
  120. std::vector<fuse_msgbuf_t*> togc;
  121. {
  122. std::size_t size;
  123. std::size_t ten_percent;
  124. std::lock_guard<std::mutex> lck(g_MUTEX);
  125. size = g_MSGBUF_STACK.size();
  126. ten_percent = (size / 10);
  127. for(std::size_t i = 0; i < ten_percent; i++)
  128. {
  129. togc.push_back(g_MSGBUF_STACK.back());
  130. g_MSGBUF_STACK.pop_back();
  131. }
  132. }
  133. for(auto msgbuf : togc)
  134. {
  135. msgbuf_destroy(msgbuf);
  136. g_MSGBUF_ALLOC_COUNT--;
  137. }
  138. }
  139. void
  140. msgbuf_gc()
  141. {
  142. std::vector<fuse_msgbuf_t*> oldstack;
  143. {
  144. std::lock_guard<std::mutex> lck(g_MUTEX);
  145. oldstack.swap(g_MSGBUF_STACK);
  146. }
  147. for(auto msgbuf: oldstack)
  148. {
  149. msgbuf_destroy(msgbuf);
  150. g_MSGBUF_ALLOC_COUNT--;
  151. }
  152. }