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.

86 lines
2.0 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 "fs_readahead.hpp"
  16. #include "fmt/core.h"
  17. #include "fs_lstat.hpp"
  18. #include <cstdint>
  19. #include <fstream>
  20. #include <string>
  21. #include <sys/sysmacros.h>
  22. namespace l
  23. {
  24. static
  25. std::string
  26. generate_readahead_sys_path(const std::uint64_t major_,
  27. const std::uint64_t minor_)
  28. {
  29. return fmt::format("/sys/class/bdi/{}:{}/read_ahead_kb",major_,minor_);
  30. }
  31. }
  32. int
  33. fs::readahead(const std::uint64_t major_dev_,
  34. const std::uint64_t minor_dev_,
  35. const std::uint64_t size_in_kb_)
  36. {
  37. std::string syspath;
  38. std::ofstream ofs;
  39. syspath = l::generate_readahead_sys_path(major_dev_,minor_dev_);
  40. ofs.open(syspath);
  41. if(ofs)
  42. {
  43. ofs << fmt::format("{}\n",size_in_kb_);
  44. ofs.close();
  45. }
  46. return 0;
  47. }
  48. int
  49. fs::readahead(const std::uint64_t dev_,
  50. const std::uint64_t size_in_kb_)
  51. {
  52. std::uint32_t major_dev;
  53. std::uint32_t minor_dev;
  54. major_dev = major(dev_);
  55. minor_dev = minor(dev_);
  56. return fs::readahead(major_dev,minor_dev,size_in_kb_);
  57. }
  58. int
  59. fs::readahead(const std::string path_,
  60. const std::uint64_t size_in_kb_)
  61. {
  62. int rv;
  63. struct stat st;
  64. rv = fs::lstat(path_,&st);
  65. if(rv == -1)
  66. return -errno;
  67. return fs::readahead(st.st_dev,size_in_kb_);
  68. }