Skip to content

Commit 0740af5

Browse files
feat(autoware_utils_system): split package (#38)
* feat(autoware_utils_system): split package Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> * update for compatibility Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> --------- Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> Co-authored-by: Yutaka Kondo <yutaka.kondo@youtalk.jp>
1 parent 087e199 commit 0740af5

File tree

17 files changed

+469
-181
lines changed

17 files changed

+469
-181
lines changed

autoware_utils/README.md

-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ The ROS module provides utilities for working with ROS messages and nodes:
5151

5252
The system module provides low-level utilities for performance monitoring and error handling:
5353

54-
- **`backtrace.hpp`**: Prints backtraces for debugging.
55-
- **`lru_cache.hpp`**: Implements an LRU (Least Recently Used) cache.
56-
- **`stop_watch.hpp`**: Measures elapsed time for profiling.
5754
- **`time_keeper.hpp`**: Tracks and reports the processing time of various functions.
5855

5956
## Usage

autoware_utils/include/autoware_utils/system/backtrace.hpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Tier IV, Inc.
1+
// Copyright 2025 The Autoware Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,11 +15,14 @@
1515
#ifndef AUTOWARE_UTILS__SYSTEM__BACKTRACE_HPP_
1616
#define AUTOWARE_UTILS__SYSTEM__BACKTRACE_HPP_
1717

18-
namespace autoware_utils
19-
{
18+
// NOLINTBEGIN(build/namespaces, whitespace/line_length)
19+
// clang-format off
2020

21-
void print_backtrace();
21+
#pragma message("#include <autoware_utils/system/backtrace.hpp> is deprecated. Use #include <autoware_utils_system/backtrace.hpp> instead.")
22+
#include <autoware_utils_system/backtrace.hpp>
23+
namespace autoware_utils { using namespace autoware_utils_system; }
2224

23-
} // namespace autoware_utils
25+
// clang-format on
26+
// NOLINTEND
2427

2528
#endif // AUTOWARE_UTILS__SYSTEM__BACKTRACE_HPP_
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2024 TIER IV, Inc.
1+
// Copyright 2025 The Autoware Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -11,132 +11,18 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
#ifndef AUTOWARE_UTILS__SYSTEM__LRU_CACHE_HPP_
1516
#define AUTOWARE_UTILS__SYSTEM__LRU_CACHE_HPP_
1617

17-
#include <cstddef>
18-
#include <list>
19-
#include <optional>
20-
#include <unordered_map>
21-
#include <utility>
22-
23-
namespace autoware_utils
24-
{
25-
26-
/**
27-
* @brief A template class for LRU (Least Recently Used) Cache.
28-
*
29-
* This class implements a simple LRU cache using a combination of a list and a hash map.
30-
*
31-
* @tparam Key The type of keys.
32-
* @tparam Value The type of values.
33-
* @tparam Map The type of underlying map, defaulted to std::unordered_map.
34-
*/
35-
template <typename Key, typename Value, template <typename...> class Map = std::unordered_map>
36-
class LRUCache
37-
{
38-
private:
39-
size_t capacity_; ///< The maximum capacity of the cache.
40-
std::list<std::pair<Key, Value>> cache_list_; ///< List to maintain the order of elements.
41-
Map<Key, typename std::list<std::pair<Key, Value>>::iterator>
42-
cache_map_; ///< Map for fast access to elements.
43-
44-
public:
45-
/**
46-
* @brief Construct a new LRUCache object.
47-
*
48-
* @param size The capacity of the cache.
49-
*/
50-
explicit LRUCache(size_t size) : capacity_(size) {}
51-
52-
/**
53-
* @brief Get the capacity of the cache.
54-
*
55-
* @return The capacity of the cache.
56-
*/
57-
[[nodiscard]] size_t capacity() const { return capacity_; }
58-
59-
/**
60-
* @brief Insert a key-value pair into the cache.
61-
*
62-
* If the key already exists, its value is updated and it is moved to the front.
63-
* If the cache exceeds its capacity, the least recently used element is removed.
64-
*
65-
* @param key The key to insert.
66-
* @param value The value to insert.
67-
*/
68-
void put(const Key & key, const Value & value)
69-
{
70-
auto it = cache_map_.find(key);
71-
if (it != cache_map_.end()) {
72-
cache_list_.erase(it->second);
73-
}
74-
cache_list_.push_front({key, value});
75-
cache_map_[key] = cache_list_.begin();
76-
77-
if (cache_map_.size() > capacity_) {
78-
auto last = cache_list_.back();
79-
cache_map_.erase(last.first);
80-
cache_list_.pop_back();
81-
}
82-
}
83-
84-
/**
85-
* @brief Retrieve a value from the cache.
86-
*
87-
* If the key does not exist in the cache, std::nullopt is returned.
88-
* If the key exists, the value is returned and the element is moved to the front.
89-
*
90-
* @param key The key to retrieve.
91-
* @return The value associated with the key, or std::nullopt if the key does not exist.
92-
*/
93-
std::optional<Value> get(const Key & key)
94-
{
95-
auto it = cache_map_.find(key);
96-
if (it == cache_map_.end()) {
97-
return std::nullopt;
98-
}
99-
cache_list_.splice(cache_list_.begin(), cache_list_, it->second);
100-
return it->second->second;
101-
}
102-
103-
/**
104-
* @brief Clear the cache.
105-
*
106-
* This removes all elements from the cache.
107-
*/
108-
void clear()
109-
{
110-
cache_list_.clear();
111-
cache_map_.clear();
112-
}
113-
114-
/**
115-
* @brief Get the current size of the cache.
116-
*
117-
* @return The number of elements in the cache.
118-
*/
119-
[[nodiscard]] size_t size() const { return cache_map_.size(); }
120-
121-
/**
122-
* @brief Check if the cache is empty.
123-
*
124-
* @return True if the cache is empty, false otherwise.
125-
*/
126-
[[nodiscard]] bool empty() const { return cache_map_.empty(); }
18+
// NOLINTBEGIN(build/namespaces, whitespace/line_length)
19+
// clang-format off
12720

128-
/**
129-
* @brief Check if a key exists in the cache.
130-
*
131-
* @param key The key to check.
132-
* @return True if the key exists, false otherwise.
133-
*/
134-
[[nodiscard]] bool contains(const Key & key) const
135-
{
136-
return cache_map_.find(key) != cache_map_.end();
137-
}
138-
};
21+
#pragma message("#include <autoware_utils/system/lru_cache.hpp> is deprecated. Use #include <autoware_utils_system/lru_cache.hpp> instead.")
22+
#include <autoware_utils_system/lru_cache.hpp>
23+
namespace autoware_utils { using namespace autoware_utils_system; }
13924

140-
} // namespace autoware_utils
25+
// clang-format on
26+
// NOLINTEND
14127

14228
#endif // AUTOWARE_UTILS__SYSTEM__LRU_CACHE_HPP_
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Tier IV, Inc.
1+
// Copyright 2025 The Autoware Contributors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -15,49 +15,14 @@
1515
#ifndef AUTOWARE_UTILS__SYSTEM__STOP_WATCH_HPP_
1616
#define AUTOWARE_UTILS__SYSTEM__STOP_WATCH_HPP_
1717

18-
#include <chrono>
19-
#include <string>
20-
#include <unordered_map>
18+
// NOLINTBEGIN(build/namespaces, whitespace/line_length)
19+
// clang-format off
2120

22-
namespace autoware_utils
23-
{
24-
template <
25-
class OutputUnit = std::chrono::seconds, class InternalUnit = std::chrono::microseconds,
26-
class Clock = std::chrono::steady_clock>
27-
class StopWatch
28-
{
29-
public:
30-
StopWatch() { tic(default_name); }
21+
#pragma message("#include <autoware_utils/system/stop_watch.hpp> is deprecated. Use #include <autoware_utils_system/stop_watch.hpp> instead.")
22+
#include <autoware_utils_system/stop_watch.hpp>
23+
namespace autoware_utils { using namespace autoware_utils_system; }
3124

32-
void tic(const std::string & name = default_name) { t_start_[name] = Clock::now(); }
33-
34-
void tic(const char * name) { tic(std::string(name)); }
35-
36-
double toc(const std::string & name, const bool reset = false)
37-
{
38-
const auto t_start = t_start_.at(name);
39-
const auto t_end = Clock::now();
40-
const auto duration = std::chrono::duration_cast<InternalUnit>(t_end - t_start).count();
41-
42-
if (reset) {
43-
t_start_[name] = Clock::now();
44-
}
45-
46-
const auto one_sec = std::chrono::duration_cast<InternalUnit>(OutputUnit(1)).count();
47-
48-
return static_cast<double>(duration) / one_sec;
49-
}
50-
51-
double toc(const char * name, const bool reset = false) { return toc(std::string(name), reset); }
52-
53-
double toc(const bool reset = false) { return toc(default_name, reset); }
54-
55-
private:
56-
using Time = std::chrono::time_point<Clock>;
57-
static constexpr const char * default_name{"__auto__"};
58-
59-
std::unordered_map<std::string, Time> t_start_;
60-
};
61-
} // namespace autoware_utils
25+
// clang-format on
26+
// NOLINTEND
6227

6328
#endif // AUTOWARE_UTILS__SYSTEM__STOP_WATCH_HPP_

autoware_utils/include/autoware_utils/system/time_keeper.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
#ifndef AUTOWARE_UTILS__SYSTEM__TIME_KEEPER_HPP_
1515
#define AUTOWARE_UTILS__SYSTEM__TIME_KEEPER_HPP_
1616

17-
#include "autoware_utils/system/stop_watch.hpp"
18-
17+
#include <autoware_utils_system/stop_watch.hpp>
1918
#include <rclcpp/publisher.hpp>
2019

2120
#include <autoware_internal_debug_msgs/msg/processing_time_node.hpp>
@@ -173,7 +172,7 @@ class TimeKeeper
173172
current_time_node_; //!< Shared pointer to the current time node
174173
std::shared_ptr<ProcessingTimeNode> root_node_; //!< Shared pointer to the root time node
175174
std::thread::id root_node_thread_id_; //!< ID of the thread that started the tracking
176-
autoware_utils::StopWatch<
175+
autoware_utils_system::StopWatch<
177176
std::chrono::milliseconds, std::chrono::microseconds, std::chrono::steady_clock>
178177
stop_watch_; //!< StopWatch object for tracking the processing time
179178

autoware_utils/package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<depend>autoware_utils_logging</depend>
2424
<depend>autoware_utils_math</depend>
2525
<depend>autoware_utils_pcl</depend>
26+
<depend>autoware_utils_system</depend>
2627
<depend>autoware_utils_visualization</depend>
2728
<depend>autoware_vehicle_msgs</depend>
2829
<depend>builtin_interfaces</depend>

autoware_utils_system/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
project(autoware_utils_system)
3+
4+
find_package(autoware_cmake REQUIRED)
5+
autoware_package()
6+
7+
ament_auto_add_library(${PROJECT_NAME} SHARED
8+
"src/backtrace.cpp"
9+
)
10+
11+
if(BUILD_TESTING)
12+
file(GLOB_RECURSE test_files test/*.cpp)
13+
ament_auto_add_gtest(test_${PROJECT_NAME} ${test_files})
14+
endif()
15+
16+
ament_auto_package()

autoware_utils_system/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# autoware_utils_system
2+
3+
## Overview
4+
5+
The **autoware_utils** library is a comprehensive toolkit designed to facilitate the development of autonomous driving applications.
6+
This package provides essential utilities for system.
7+
It is extensively used in the Autoware project to handle common tasks such as performance monitoring and error handling.
8+
9+
## Design
10+
11+
- **`backtrace.hpp`**: Prints backtraces for debugging.
12+
- **`lru_cache.hpp`**: Implements an LRU (Least Recently Used) cache.
13+
- **`stop_watch.hpp`**: Measures elapsed time for profiling.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2023 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_UTILS_SYSTEM__BACKTRACE_HPP_
16+
#define AUTOWARE_UTILS_SYSTEM__BACKTRACE_HPP_
17+
18+
namespace autoware_utils_system
19+
{
20+
21+
void print_backtrace();
22+
23+
} // namespace autoware_utils_system
24+
25+
#endif // AUTOWARE_UTILS_SYSTEM__BACKTRACE_HPP_

0 commit comments

Comments
 (0)