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.

114 lines
2.8 KiB

  1. /*
  2. ISC License
  3. Copyright (c) 2023, 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_readdir_factory.hpp"
  16. #include "fuse_readdir_cor.hpp"
  17. #include "fuse_readdir_cosr.hpp"
  18. #include "fuse_readdir_seq.hpp"
  19. #include <cassert>
  20. #include <cmath>
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <set>
  24. #define DEFAULT_MAX_QUEUE_DEPTH 3
  25. namespace l
  26. {
  27. static
  28. void
  29. read_cfg(std::string const str_,
  30. std::string &type_,
  31. unsigned &concurrency_,
  32. unsigned &max_queue_depth_)
  33. {
  34. char type[16];
  35. int concurrency;
  36. int max_queue_depth;
  37. concurrency = 0;
  38. max_queue_depth = 0;
  39. std::sscanf(str_.c_str(),
  40. "%15[a-z]:%d:%d",
  41. type,
  42. &concurrency,
  43. &max_queue_depth);
  44. if(concurrency == 0)
  45. concurrency = std::thread::hardware_concurrency();
  46. else if(concurrency < 0)
  47. concurrency = (std::thread::hardware_concurrency() / std::abs(concurrency));
  48. if(concurrency == 0)
  49. concurrency = 1;
  50. if(max_queue_depth == 0)
  51. max_queue_depth = DEFAULT_MAX_QUEUE_DEPTH;
  52. max_queue_depth *= concurrency;
  53. type_ = type;
  54. concurrency_ = concurrency;
  55. max_queue_depth_ = max_queue_depth;
  56. }
  57. }
  58. bool
  59. FUSE::ReadDirFactory::valid(std::string const str_)
  60. {
  61. unsigned concurrency;
  62. unsigned max_queue_depth;
  63. std::string type;
  64. static const std::set<std::string> types =
  65. {
  66. "seq", "cosr", "cor"
  67. };
  68. l::read_cfg(str_,type,concurrency,max_queue_depth);
  69. if(types.find(type) == types.end())
  70. return false;
  71. if(concurrency <= 0)
  72. return false;
  73. return true;
  74. }
  75. std::shared_ptr<FUSE::ReadDirBase>
  76. FUSE::ReadDirFactory::make(std::string const str_)
  77. {
  78. unsigned concurrency;
  79. unsigned max_queue_depth;
  80. std::string type;
  81. if(!valid(str_))
  82. return {};
  83. l::read_cfg(str_,type,concurrency,max_queue_depth);
  84. if(type == "seq")
  85. return std::make_shared<FUSE::ReadDirSeq>();
  86. if(type == "cosr")
  87. return std::make_shared<FUSE::ReadDirCOSR>(concurrency,max_queue_depth);
  88. if(type == "cor")
  89. return std::make_shared<FUSE::ReadDirCOR>(concurrency,max_queue_depth);
  90. return {};
  91. }