Skip to content

Commit 9eeb84b

Browse files
otenimclaude
andauthored
refactor: remove PageCacheGuard usage from bag_converter (#75)
* refactor: remove PageCacheGuard usage from bag_converter The periodic page cache dropping via PageCacheGuard added complexity without clear benefit, as fadvise_sequential_access already hints the kernel to manage page cache efficiently. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: remove unused page_cache_drop_interval constant No longer needed after removing PageCacheGuard usage. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * refactor: remove PageCacheGuard class and fadvise_drop_page_cache No longer used after removing page cache guard usage from bag_converter. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 8187e12 commit 9eeb84b

4 files changed

Lines changed: 0 additions & 79 deletions

File tree

src/bag_converter/include/bag_converter.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ inline constexpr bool keep_original_topics = false;
5454
// Progress logging interval
5555
inline constexpr size_t progress_log_interval = 1000;
5656

57-
// Page cache drop interval (in messages)
58-
inline constexpr size_t page_cache_drop_interval = 10000;
59-
6057
// Minimum valid header.stamp epoch (2000-01-01T00:00:00Z = 946684800 seconds)
6158
inline constexpr int64_t header_stamp_min_epoch_sec = 946'684'800;
6259

src/bag_converter/include/memory_management.hpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define BAG_CONVERTER__MEMORY_MANAGEMENT_HPP
1111

1212
#include <string>
13-
#include <vector>
1413

1514
namespace memory_management
1615
{
@@ -26,49 +25,6 @@ namespace memory_management
2625
*/
2726
void fadvise_sequential_access(const std::string & path);
2827

29-
/**
30-
* @brief Drop page cache for a file using posix_fadvise(POSIX_FADV_DONTNEED).
31-
*
32-
* Opens the file with a separate file descriptor, advises the kernel that the
33-
* cached pages are no longer needed, then closes the descriptor. This works
34-
* regardless of whether another FD (e.g. from rosbag2) is still open on the
35-
* same file — the advisory applies to the underlying page cache.
36-
*
37-
* @param path Path to the file whose cached pages should be evicted
38-
*/
39-
void fadvise_drop_page_cache(const std::string & path);
40-
41-
/**
42-
* @brief RAII guard that frees page cache for tracked files on destruction.
43-
*
44-
* Tracks file paths via track() and calls fadvise_drop_page_cache() for each on
45-
* destruction. Ensures page cache is freed regardless of how the scope is
46-
* exited (normal return, early error return, or Ctrl+C graceful shutdown).
47-
*/
48-
class PageCacheGuard
49-
{
50-
public:
51-
PageCacheGuard() = default;
52-
~PageCacheGuard() { drop(); }
53-
54-
PageCacheGuard(const PageCacheGuard &) = delete;
55-
PageCacheGuard & operator=(const PageCacheGuard &) = delete;
56-
PageCacheGuard(PageCacheGuard &&) = delete;
57-
PageCacheGuard & operator=(PageCacheGuard &&) = delete;
58-
59-
void track(const std::string & path) { paths_.push_back(path); }
60-
61-
void drop()
62-
{
63-
for (const auto & p : paths_) {
64-
fadvise_drop_page_cache(p);
65-
}
66-
}
67-
68-
private:
69-
std::vector<std::string> paths_;
70-
};
71-
7228
} // namespace memory_management
7329

7430
#endif // BAG_CONVERTER__MEMORY_MANAGEMENT_HPP

src/bag_converter/src/bag_converter.cpp

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,6 @@ BagConverterResultStatus run_impl(const BagConverterConfig & config)
596596
return BagConverterResultStatus::kError;
597597
}
598598

599-
// RAII guard: frees page cache for tracked files on any exit path
600-
memory_management::PageCacheGuard cache_guard;
601-
cache_guard.track(config.src_bag_path);
602-
603599
const auto bag_metadata = reader.get_metadata();
604600

605601
// Create TF transformer if frame is specified
@@ -666,8 +662,6 @@ BagConverterResultStatus run_impl(const BagConverterConfig & config)
666662
RCLCPP_ERROR(g_logger, "Error opening output bag: %s", e.what());
667663
return BagConverterResultStatus::kError;
668664
}
669-
cache_guard.track(dst_path.string());
670-
671665
// Create output topics based on input metadata
672666
std::set<std::string> created_topics;
673667
for (const auto & topic_info : bag_metadata.topics_with_message_count) {
@@ -768,9 +762,6 @@ BagConverterResultStatus run_impl(const BagConverterConfig & config)
768762
if (message_count % defaults::progress_log_interval == 0) {
769763
RCLCPP_INFO(g_logger, "Processed %zu messages...", message_count);
770764
}
771-
if (message_count % defaults::page_cache_drop_interval == 0) {
772-
cache_guard.drop();
773-
}
774765
continue;
775766
}
776767

@@ -789,9 +780,6 @@ BagConverterResultStatus run_impl(const BagConverterConfig & config)
789780
if (message_count % defaults::progress_log_interval == 0) {
790781
RCLCPP_INFO(g_logger, "Processed %zu messages...", message_count);
791782
}
792-
if (message_count % defaults::page_cache_drop_interval == 0) {
793-
cache_guard.drop();
794-
}
795783
}
796784

797785
if (!rclcpp::ok()) {
@@ -1024,13 +1012,6 @@ static int64_t merge_and_convert_group(
10241012
const std::vector<fs::path> & bag_files, const fs::path & output_path,
10251013
const std::string & storage_identifier, const BagConverterConfig & config)
10261014
{
1027-
// RAII guard: frees page cache for all tracked files on any exit path
1028-
memory_management::PageCacheGuard cache_guard;
1029-
for (const auto & bag_path : bag_files) {
1030-
cache_guard.track(bag_path.string());
1031-
}
1032-
cache_guard.track(output_path.string());
1033-
10341015
// 1. Collect topic union
10351016
auto topic_union = merge::collect_topic_union(bag_files);
10361017
if (!topic_union.has_value()) {
@@ -1221,10 +1202,6 @@ static int64_t merge_and_convert_group(
12211202
if (message_count % defaults::progress_log_interval == 0) {
12221203
RCLCPP_INFO(g_logger, " Processed %ld messages...", message_count);
12231204
}
1224-
if (message_count % defaults::page_cache_drop_interval == 0) {
1225-
cache_guard.drop();
1226-
}
1227-
12281205
// Read next message from the same reader
12291206
auto & reader = readers[entry.reader_index];
12301207
if (reader->has_next()) {

src/bag_converter/src/memory_management.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,4 @@ void fadvise_sequential_access(const std::string & path)
3737
#endif
3838
}
3939

40-
void fadvise_drop_page_cache(const std::string & path)
41-
{
42-
#ifdef __linux__
43-
fadvise(path, POSIX_FADV_DONTNEED);
44-
#else
45-
(void)path;
46-
#endif
47-
}
48-
4940
} // namespace memory_management

0 commit comments

Comments
 (0)