Created
          April 30, 2021 11:37 
        
      - 
      
- 
        Save daverigby/6778a26bffd4d73012cd44fe0a07b683 to your computer and use it in GitHub Desktop. 
    Determine the maximum number of requests (OS-level queue depth) for the given file path
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <sys/sysmacros.h> | |
| #include <sys/types.h> | |
| #include <sys/stat.h> | |
| #include <fcntl.h> | |
| #include <unistd.h> | |
| #include <iostream> | |
| #include <string> | |
| int main(int argc, char** argv) { | |
| if (argc != 2) { | |
| std::cerr << "Usage: " << argv[0] << "<path>\n"; | |
| return 1; | |
| } | |
| struct stat statbuf; | |
| if (stat(argv[1], &statbuf)) { | |
| perror("stat() failed"); | |
| return 1; | |
| } | |
| std::string dqdPath = "/sys/dev/block/"; | |
| dqdPath += std::to_string(major(statbuf.st_dev)) + ":" + | |
| std::to_string(minor(statbuf.st_dev)) + "/queue/nr_requests"; | |
| auto fd = open(dqdPath.c_str(), O_RDONLY); | |
| if (fd == -1) { | |
| std::string msg{"open('" + dqdPath + "') failed"}; | |
| perror(msg.c_str()); | |
| return 1; | |
| } | |
| std::string buffer; | |
| buffer.resize(16); | |
| auto len = read(fd, &buffer[0], buffer.size()); | |
| if ((len <= 0) || (len >= buffer.size())) { | |
| std::string msg{"read('" + dqdPath + "') failed"}; | |
| perror(msg.c_str()); | |
| return 1; | |
| } | |
| buffer.resize(len); | |
| std::cout << "queue_depth: " << buffer << "\n"; | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment