-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
412 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <fcntl.h> | ||
#if defined(__linux__) | ||
# include <linux/fiemap.h> | ||
# include <linux/fs.h> | ||
#else | ||
# warning "The file fragments code is for Linux only and the fragmentation monitorning wont be available on other platforms." | ||
#endif | ||
#include <spdlog/spdlog.h> | ||
#include <string.h> | ||
#include <sys/ioctl.h> | ||
#include <unistd.h> | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
#define MAX_EXTENTS 32 | ||
|
||
struct extents { | ||
uint64_t start; | ||
uint64_t length; | ||
uint64_t flags; | ||
}; | ||
|
||
std::vector<extents> get_extents(const char *file_path); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <fcntl.h> | ||
#include <linux/fiemap.h> | ||
#include <linux/fs.h> | ||
#include <string.h> | ||
#include <sys/ioctl.h> | ||
#include <unistd.h> | ||
|
||
#include <iostream> | ||
|
||
#define MAX_EXTENTS 32 | ||
|
||
struct fiemap_extent_data { | ||
struct fiemap *fiemap_ptr; | ||
}; | ||
|
||
// Function to print the extents of a file | ||
void print_file_extents(const char *file_path) { | ||
int fd = open(file_path, O_RDONLY); | ||
if (fd < 0) { | ||
perror("open"); | ||
return; | ||
} | ||
|
||
struct fiemap_extent_data fed; | ||
memset(&fed, 0, sizeof(fed)); | ||
|
||
size_t fiemap_size = sizeof(struct fiemap) + sizeof(struct fiemap_extent) * MAX_EXTENTS; | ||
fed.fiemap_ptr = (struct fiemap *)malloc(fiemap_size); | ||
if (fed.fiemap_ptr == NULL) { | ||
perror("malloc"); | ||
close(fd); | ||
return; | ||
} | ||
memset(fed.fiemap_ptr, 0, fiemap_size); | ||
|
||
fed.fiemap_ptr->fm_start = 0; | ||
fed.fiemap_ptr->fm_length = ~0ULL; // Request all extents from the start | ||
fed.fiemap_ptr->fm_flags = FIEMAP_FLAG_SYNC; | ||
fed.fiemap_ptr->fm_extent_count = MAX_EXTENTS; | ||
|
||
if (ioctl(fd, FS_IOC_FIEMAP, fed.fiemap_ptr) == -1) { | ||
perror("ioctl"); | ||
free(fed.fiemap_ptr); | ||
close(fd); | ||
return; | ||
} | ||
|
||
std::cout << "Number of extents: " << fed.fiemap_ptr->fm_mapped_extents << std::endl; | ||
for (unsigned int i = 0; i < fed.fiemap_ptr->fm_mapped_extents; i++) { | ||
std::cout << "Extent " << i << ": Start=" << fed.fiemap_ptr->fm_extents[i].fe_logical << ", Length=" << fed.fiemap_ptr->fm_extents[i].fe_length | ||
<< ", Flags=" << fed.fiemap_ptr->fm_extents[i].fe_flags << std::endl; | ||
} | ||
|
||
free(fed.fiemap_ptr); | ||
close(fd); | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
if (argc != 2) { | ||
std::cerr << "Usage: " << argv[0] << " <file_path>" << std::endl; | ||
return 1; | ||
} | ||
|
||
print_file_extents(argv[1]); | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <filestorm/filefrag.h> | ||
|
||
struct fiemap_extent_data { | ||
struct fiemap *fiemap_ptr; | ||
}; | ||
|
||
std::vector<extents> get_extents(const char *file_path) { | ||
#if defined(__linux__) | ||
std::vector<extents> extents_list; | ||
int fd = open(file_path, O_RDONLY); | ||
if (fd < 0) { | ||
perror("open"); | ||
throw std::runtime_error("Error opening file, for extents scan."); | ||
} | ||
|
||
struct fiemap_extent_data fed; | ||
memset(&fed, 0, sizeof(fed)); | ||
|
||
size_t fiemap_size = sizeof(struct fiemap) + sizeof(struct fiemap_extent) * MAX_EXTENTS; | ||
fed.fiemap_ptr = (struct fiemap *)malloc(fiemap_size); | ||
if (fed.fiemap_ptr == NULL) { | ||
perror("malloc"); | ||
close(fd); | ||
throw std::runtime_error("Error allocating memory for fiemap."); | ||
} | ||
memset(fed.fiemap_ptr, 0, fiemap_size); | ||
|
||
fed.fiemap_ptr->fm_start = 0; | ||
fed.fiemap_ptr->fm_length = ~0ULL; // Request all extents from the start | ||
fed.fiemap_ptr->fm_flags = FIEMAP_FLAG_SYNC; | ||
fed.fiemap_ptr->fm_extent_count = MAX_EXTENTS; | ||
|
||
if (ioctl(fd, FS_IOC_FIEMAP, fed.fiemap_ptr) == -1) { | ||
perror("ioctl"); | ||
free(fed.fiemap_ptr); | ||
close(fd); | ||
throw std::runtime_error("Error getting file extents."); | ||
} | ||
|
||
for (unsigned int i = 0; i < fed.fiemap_ptr->fm_mapped_extents; i++) { | ||
extents_list.push_back({fed.fiemap_ptr->fm_extents[i].fe_logical, fed.fiemap_ptr->fm_extents[i].fe_length, fed.fiemap_ptr->fm_extents[i].fe_flags}); | ||
} | ||
|
||
free(fed.fiemap_ptr); | ||
close(fd); | ||
return extents_list; | ||
#else | ||
static bool warning_printed = false; | ||
if (!warning_printed) { | ||
spdlog::debug("This code is for Linux only and the fragmentation monitorning wont be available on other platforms."); | ||
warning_printed = true; | ||
} | ||
return {}; | ||
#endif | ||
} |
Oops, something went wrong.